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 automated_volume_change_reasons_.clear(); |
jennyz
2016/08/02 18:02:33
Can we add a warning log here for diagnostic purpo
Qiang(Joe) Xu
2016/08/02 22:45:58
Done.
| |
626 } else { | 633 } else { |
627 // Reset the initializing_audio_state_ in case SetOutputNodeVolume request | 634 // In other cases, sequential AutomatedVolumeChangeReason corresponds to |
628 // is lost by cras due to cras is not ready when CrasAudioHandler is being | 635 // sequential avoiding notifying observers. |
629 // initialized. | 636 should_notify = false; |
630 initializing_audio_state_ = false; | 637 automated_volume_change_reasons_.pop_front(); |
631 init_volume_count_ = 0; | |
632 } | 638 } |
633 } | 639 } |
634 | 640 |
635 FOR_EACH_OBSERVER(AudioObserver, observers_, | 641 if (should_notify) { |
636 OnOutputNodeVolumeChanged(node_id, volume)); | 642 FOR_EACH_OBSERVER(AudioObserver, observers_, |
643 OnOutputNodeVolumeChanged(node_id, volume)); | |
644 } | |
637 } | 645 } |
638 | 646 |
639 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { | 647 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { |
640 if (active_output_node_id_ == node_id) | 648 if (active_output_node_id_ == node_id) |
641 return; | 649 return; |
642 | 650 |
643 // Active audio output device should always be changed by chrome. | 651 // Active audio output device should always be changed by chrome. |
644 // During system boot, cras may change active input to unknown device 0x1, | 652 // 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. | 653 // we don't need to log it, since it is not an valid device. |
646 if (GetDeviceFromId(node_id)) { | 654 if (GetDeviceFromId(node_id)) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
731 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { | 739 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { |
732 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; | 740 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; |
733 output_mute_on_ = true; | 741 output_mute_on_ = true; |
734 } else { | 742 } else { |
735 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); | 743 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); |
736 } | 744 } |
737 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); | 745 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); |
738 | 746 |
739 SetOutputMuteInternal(output_mute_on_); | 747 SetOutputMuteInternal(output_mute_on_); |
740 | 748 |
741 if (initializing_audio_state_) { | 749 if (!automated_volume_change_reasons_.empty() && |
750 automated_volume_change_reasons_.front() == | |
751 VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) { | |
jennyz
2016/08/02 18:02:33
Will it be more straight forward by keeping initia
Qiang(Joe) Xu
2016/08/02 22:45:58
It is great, thanks. In this way, automated_volume
| |
742 // During power up, InitializeAudioState() could be called twice, first | 752 // During power up, InitializeAudioState() could be called twice, first |
743 // by CrasAudioHandler constructor, then by cras server restarting signal, | 753 // by CrasAudioHandler constructor, then by cras server restarting signal, |
744 // both sending SetOutputNodeVolume requests, and could lead to two | 754 // both sending SetOutputNodeVolume requests, and could lead to two |
745 // OutputNodeVolumeChanged signals. | 755 // OutputNodeVolumeChanged signals. |
Daniel Erat
2016/08/02 16:31:38
i don't understand why you don't need to push anot
Qiang(Joe) Xu
2016/08/02 22:45:58
Yeah, I may change the code logic from jennyz's. I
| |
746 init_volume_count_++; | |
747 init_node_id_ = active_output_node_id_; | 756 init_node_id_ = active_output_node_id_; |
748 init_volume_ = output_volume_; | 757 init_volume_ = output_volume_; |
749 } | 758 } |
750 SetOutputNodeVolume(active_output_node_id_, output_volume_); | 759 SetOutputNodeVolume(active_output_node_id_, output_volume_); |
751 } | 760 } |
752 | 761 |
753 // This sets up the state of an additional active node. | 762 // This sets up the state of an additional active node. |
754 void CrasAudioHandler::SetupAdditionalActiveAudioNodeState(uint64_t node_id) { | 763 void CrasAudioHandler::SetupAdditionalActiveAudioNodeState(uint64_t node_id) { |
755 const AudioDevice* device = GetDeviceFromId(node_id); | 764 const AudioDevice* device = GetDeviceFromId(node_id); |
756 if (!device) { | 765 if (!device) { |
(...skipping 10 matching lines...) Expand all Loading... | |
767 // devices. For input devices, we don't restore their gain value so far. | 776 // devices. For input devices, we don't restore their gain value so far. |
768 // TODO(jennyz): crbug.com/417418, track the status for the decison if | 777 // TODO(jennyz): crbug.com/417418, track the status for the decison if |
769 // we should persist input gain value in prefs. | 778 // we should persist input gain value in prefs. |
770 if (!device->is_input) { | 779 if (!device->is_input) { |
771 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); | 780 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); |
772 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); | 781 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); |
773 } | 782 } |
774 } | 783 } |
775 | 784 |
776 void CrasAudioHandler::InitializeAudioState() { | 785 void CrasAudioHandler::InitializeAudioState() { |
777 initializing_audio_state_ = true; | 786 automated_volume_change_reasons_.push_back( |
787 VOLUME_CHANGE_INITIALIZING_AUDIO_STATE); | |
778 ApplyAudioPolicy(); | 788 ApplyAudioPolicy(); |
779 | 789 |
780 // Defer querying cras for GetNodes until cras service becomes available. | 790 // Defer querying cras for GetNodes until cras service becomes available. |
781 cras_service_available_ = false; | 791 cras_service_available_ = false; |
782 chromeos::DBusThreadManager::Get() | 792 chromeos::DBusThreadManager::Get() |
783 ->GetCrasAudioClient() | 793 ->GetCrasAudioClient() |
784 ->WaitForServiceToBeAvailable(base::Bind( | 794 ->WaitForServiceToBeAvailable(base::Bind( |
785 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, | 795 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, |
786 weak_ptr_factory_.GetWeakPtr())); | 796 weak_ptr_factory_.GetWeakPtr())); |
787 } | 797 } |
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1378 hdmi_rediscover_grace_period_duration_in_ms_), | 1388 hdmi_rediscover_grace_period_duration_in_ms_), |
1379 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1389 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
1380 } | 1390 } |
1381 | 1391 |
1382 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1392 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
1383 int duration_in_ms) { | 1393 int duration_in_ms) { |
1384 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1394 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
1385 } | 1395 } |
1386 | 1396 |
1387 } // namespace chromeos | 1397 } // namespace chromeos |
OLD | NEW |