| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/audio/cras_audio_handler.h" | 5 #include "chromeos/audio/cras_audio_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 // Set all active devices to the same volume. | 369 // Set all active devices to the same volume. |
| 370 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 370 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 371 it != audio_devices_.end(); | 371 it != audio_devices_.end(); |
| 372 it++) { | 372 it++) { |
| 373 const AudioDevice& device = it->second; | 373 const AudioDevice& device = it->second; |
| 374 if (!device.is_input && device.active) | 374 if (!device.is_input && device.active) |
| 375 SetOutputNodeVolumePercent(device.id, volume_percent); | 375 SetOutputNodeVolumePercent(device.id, volume_percent); |
| 376 } | 376 } |
| 377 } | 377 } |
| 378 | 378 |
| 379 void CrasAudioHandler::SetOutputVolumePercentWithoutNotifyingObservers( |
| 380 int volume_percent, |
| 381 AutomatedVolumeChangeReason reason) { |
| 382 automated_volume_change_reasons_.push_back(reason); |
| 383 SetOutputVolumePercent(volume_percent); |
| 384 } |
| 385 |
| 379 // TODO: Rename the 'Percent' to something more meaningful. | 386 // TODO: Rename the 'Percent' to something more meaningful. |
| 380 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { | 387 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { |
| 381 // TODO(jennyz): Should we set all input devices' gain to the same level? | 388 // TODO(jennyz): Should we set all input devices' gain to the same level? |
| 382 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 389 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 383 it != audio_devices_.end(); | 390 it != audio_devices_.end(); |
| 384 it++) { | 391 it++) { |
| 385 const AudioDevice& device = it->second; | 392 const AudioDevice& device = it->second; |
| 386 if (device.is_input && device.active) | 393 if (device.is_input && device.active) |
| 387 SetInputNodeGainPercent(active_input_node_id_, gain_percent); | 394 SetInputNodeGainPercent(active_input_node_id_, gain_percent); |
| 388 } | 395 } |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 } | 613 } |
| 607 | 614 |
| 608 // Sync internal volume state and notify UI for the change. We trust cras | 615 // Sync internal volume state and notify UI for the change. We trust cras |
| 609 // signal to report the volume state of the device, no matter which source | 616 // signal to report the volume state of the device, no matter which source |
| 610 // set the volume, i.e., volume could be set from non-chrome source, like | 617 // set the volume, i.e., volume could be set from non-chrome source, like |
| 611 // Bluetooth headset, etc. Assume all active output devices share a single | 618 // Bluetooth headset, etc. Assume all active output devices share a single |
| 612 // volume. | 619 // volume. |
| 613 output_volume_ = volume; | 620 output_volume_ = volume; |
| 614 audio_pref_handler_->SetVolumeGainValue(*device, volume); | 621 audio_pref_handler_->SetVolumeGainValue(*device, volume); |
| 615 | 622 |
| 616 if (initializing_audio_state_) { | 623 bool should_notify = true; |
| 617 // Do not notify the observers for volume changed event if CrasAudioHandler | 624 if (!automated_volume_change_reasons_.empty()) { |
| 618 // is initializing its state, i.e., the volume change event is in responding | 625 AutomatedVolumeChangeReason reason = |
| 619 // to SetOutputNodeVolume request from intializaing audio state, not | 626 automated_volume_change_reasons_.front(); |
| 620 // from user action, no need to notify UI to pop uo the volume slider bar. | 627 if (reason == VOLUME_CHANGE_INITIALIZING_AUDIO_STATE && |
| 621 if (init_node_id_ == node_id && init_volume_ == volume) { | 628 (init_node_id_ != node_id || init_volume_ != volume)) { |
| 622 init_volume_count_--; | 629 // A SetOutputNodeVolume request may be dropped if cras isn't ready |
| 623 if (!init_volume_count_) | 630 // during initialization. In this case, we clear the pending automated |
| 624 initializing_audio_state_ = false; | 631 // volume change reasons as they might also be dropped by cras. |
| 625 return; | 632 LOG(WARNING) << "OutputNodeVolumeChanged signal dropped during the " |
| 633 "initialization."; |
| 634 automated_volume_change_reasons_.clear(); |
| 626 } else { | 635 } else { |
| 627 // Reset the initializing_audio_state_ in case SetOutputNodeVolume request | 636 // In other cases, sequential AutomatedVolumeChangeReason corresponds to |
| 628 // is lost by cras due to cras is not ready when CrasAudioHandler is being | 637 // sequential avoiding notifying observers. |
| 629 // initialized. | 638 should_notify = false; |
| 630 initializing_audio_state_ = false; | 639 automated_volume_change_reasons_.pop_front(); |
| 631 init_volume_count_ = 0; | |
| 632 } | 640 } |
| 633 } | 641 } |
| 634 | 642 |
| 635 FOR_EACH_OBSERVER(AudioObserver, observers_, | 643 if (std::find(automated_volume_change_reasons_.begin(), |
| 636 OnOutputNodeVolumeChanged(node_id, volume)); | 644 automated_volume_change_reasons_.end(), |
| 645 VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) == |
| 646 automated_volume_change_reasons_.end()) |
| 647 initializing_audio_state_ = false; |
| 648 |
| 649 if (should_notify) { |
| 650 FOR_EACH_OBSERVER(AudioObserver, observers_, |
| 651 OnOutputNodeVolumeChanged(node_id, volume)); |
| 652 } |
| 637 } | 653 } |
| 638 | 654 |
| 639 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { | 655 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { |
| 640 if (active_output_node_id_ == node_id) | 656 if (active_output_node_id_ == node_id) |
| 641 return; | 657 return; |
| 642 | 658 |
| 643 // Active audio output device should always be changed by chrome. | 659 // Active audio output device should always be changed by chrome. |
| 644 // During system boot, cras may change active input to unknown device 0x1, | 660 // During system boot, cras may change active input to unknown device 0x1, |
| 645 // we don't need to log it, since it is not an valid device. | 661 // we don't need to log it, since it is not an valid device. |
| 646 if (GetDeviceFromId(node_id)) { | 662 if (GetDeviceFromId(node_id)) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 736 } | 752 } |
| 737 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); | 753 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); |
| 738 | 754 |
| 739 SetOutputMuteInternal(output_mute_on_); | 755 SetOutputMuteInternal(output_mute_on_); |
| 740 | 756 |
| 741 if (initializing_audio_state_) { | 757 if (initializing_audio_state_) { |
| 742 // During power up, InitializeAudioState() could be called twice, first | 758 // During power up, InitializeAudioState() could be called twice, first |
| 743 // by CrasAudioHandler constructor, then by cras server restarting signal, | 759 // by CrasAudioHandler constructor, then by cras server restarting signal, |
| 744 // both sending SetOutputNodeVolume requests, and could lead to two | 760 // both sending SetOutputNodeVolume requests, and could lead to two |
| 745 // OutputNodeVolumeChanged signals. | 761 // OutputNodeVolumeChanged signals. |
| 746 init_volume_count_++; | |
| 747 init_node_id_ = active_output_node_id_; | 762 init_node_id_ = active_output_node_id_; |
| 748 init_volume_ = output_volume_; | 763 init_volume_ = output_volume_; |
| 764 SetOutputVolumePercentWithoutNotifyingObservers( |
| 765 output_volume_, VOLUME_CHANGE_INITIALIZING_AUDIO_STATE); |
| 766 return; |
| 749 } | 767 } |
| 750 SetOutputNodeVolume(active_output_node_id_, output_volume_); | 768 SetOutputNodeVolume(active_output_node_id_, output_volume_); |
| 751 } | 769 } |
| 752 | 770 |
| 753 // This sets up the state of an additional active node. | 771 // This sets up the state of an additional active node. |
| 754 void CrasAudioHandler::SetupAdditionalActiveAudioNodeState(uint64_t node_id) { | 772 void CrasAudioHandler::SetupAdditionalActiveAudioNodeState(uint64_t node_id) { |
| 755 const AudioDevice* device = GetDeviceFromId(node_id); | 773 const AudioDevice* device = GetDeviceFromId(node_id); |
| 756 if (!device) { | 774 if (!device) { |
| 757 VLOG(1) << "Can't set up audio state for unknown device id =" | 775 VLOG(1) << "Can't set up audio state for unknown device id =" |
| 758 << "0x" << std::hex << node_id; | 776 << "0x" << std::hex << node_id; |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1378 hdmi_rediscover_grace_period_duration_in_ms_), | 1396 hdmi_rediscover_grace_period_duration_in_ms_), |
| 1379 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1397 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
| 1380 } | 1398 } |
| 1381 | 1399 |
| 1382 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1400 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
| 1383 int duration_in_ms) { | 1401 int duration_in_ms) { |
| 1384 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1402 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
| 1385 } | 1403 } |
| 1386 | 1404 |
| 1387 } // namespace chromeos | 1405 } // namespace chromeos |
| OLD | NEW |