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> |
11 #include <cmath> | 11 #include <cmath> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
17 #include "base/system_monitor/system_monitor.h" | 17 #include "base/system_monitor/system_monitor.h" |
18 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 18 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
19 #include "chromeos/dbus/dbus_thread_manager.h" | 19 #include "chromeos/dbus/dbus_thread_manager.h" |
20 | 20 |
21 using std::max; | 21 using std::max; |
22 using std::min; | 22 using std::min; |
23 | 23 |
24 namespace chromeos { | 24 namespace chromeos { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 enum NonUserVolumeChangeReason { | |
Daniel Erat
2016/07/28 23:57:52
nit: s/NonUser/Automated/
(then also update comme
Qiang(Joe) Xu
2016/07/29 04:30:31
Done.
| |
29 INITIALIZING_AUDIO_STATE = 1 << 0, | |
Daniel Erat
2016/07/28 23:57:52
can you make this be an enum class instead or does
Qiang(Joe) Xu
2016/07/29 04:30:32
done by adding prefix
| |
30 MAXIMIZE_MODE_SCREENSHOT = 1 << 1, | |
31 }; | |
32 | |
28 // Default value for unmuting, as a percent in the range [0, 100]. | 33 // Default value for unmuting, as a percent in the range [0, 100]. |
29 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. | 34 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. |
30 const int kDefaultUnmuteVolumePercent = 4; | 35 const int kDefaultUnmuteVolumePercent = 4; |
31 | 36 |
32 // Volume value which should be considered as muted in range [0, 100]. | 37 // Volume value which should be considered as muted in range [0, 100]. |
33 const int kMuteThresholdPercent = 1; | 38 const int kMuteThresholdPercent = 1; |
34 | 39 |
35 // The duration of HDMI output re-discover grace period in milliseconds. | 40 // The duration of HDMI output re-discover grace period in milliseconds. |
36 const int kHDMIRediscoverGracePeriodDurationInMs = 2000; | 41 const int kHDMIRediscoverGracePeriodDurationInMs = 2000; |
37 | 42 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
369 // Set all active devices to the same volume. | 374 // Set all active devices to the same volume. |
370 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 375 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
371 it != audio_devices_.end(); | 376 it != audio_devices_.end(); |
372 it++) { | 377 it++) { |
373 const AudioDevice& device = it->second; | 378 const AudioDevice& device = it->second; |
374 if (!device.is_input && device.active) | 379 if (!device.is_input && device.active) |
375 SetOutputNodeVolumePercent(device.id, volume_percent); | 380 SetOutputNodeVolumePercent(device.id, volume_percent); |
376 } | 381 } |
377 } | 382 } |
378 | 383 |
384 void CrasAudioHandler::SetOutputVolumePercentInQuietMode(int volume_percent) { | |
385 // Currently SetOutputVolumePercentInQuietMode is only called from | |
386 // maximize mode screenshot in PowerButtonController. Everytime this method | |
387 // is called, DCHECK if MAXIMIZE_MODE_SCREENSHOT is already released. | |
388 DCHECK(!(volume_change_not_by_user_ & MAXIMIZE_MODE_SCREENSHOT)); | |
389 volume_change_not_by_user_ |= MAXIMIZE_MODE_SCREENSHOT; | |
390 SetOutputVolumePercent(volume_percent); | |
391 } | |
392 | |
379 // TODO: Rename the 'Percent' to something more meaningful. | 393 // TODO: Rename the 'Percent' to something more meaningful. |
380 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { | 394 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { |
381 // TODO(jennyz): Should we set all input devices' gain to the same level? | 395 // TODO(jennyz): Should we set all input devices' gain to the same level? |
382 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 396 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
383 it != audio_devices_.end(); | 397 it != audio_devices_.end(); |
384 it++) { | 398 it++) { |
385 const AudioDevice& device = it->second; | 399 const AudioDevice& device = it->second; |
386 if (device.is_input && device.active) | 400 if (device.is_input && device.active) |
387 SetInputNodeGainPercent(active_input_node_id_, gain_percent); | 401 SetInputNodeGainPercent(active_input_node_id_, gain_percent); |
388 } | 402 } |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
606 } | 620 } |
607 | 621 |
608 // Sync internal volume state and notify UI for the change. We trust cras | 622 // 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 | 623 // 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 | 624 // 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 | 625 // Bluetooth headset, etc. Assume all active output devices share a single |
612 // volume. | 626 // volume. |
613 output_volume_ = volume; | 627 output_volume_ = volume; |
614 audio_pref_handler_->SetVolumeGainValue(*device, volume); | 628 audio_pref_handler_->SetVolumeGainValue(*device, volume); |
615 | 629 |
616 if (initializing_audio_state_) { | 630 if (volume_change_not_by_user_) { |
617 // Do not notify the observers for volume changed event if CrasAudioHandler | 631 if (volume_change_not_by_user_ & INITIALIZING_AUDIO_STATE) { |
Daniel Erat
2016/07/28 23:57:52
using a mask for this seems a bit confusing. is th
Qiang(Joe) Xu
2016/07/29 04:30:31
Yes. They are called asynchronously by observing c
| |
618 // is initializing its state, i.e., the volume change event is in responding | 632 // Do not notify the observers for volume changed event if |
619 // to SetOutputNodeVolume request from intializaing audio state, not | 633 // CrasAudioHandler is initializing its state, i.e., the volume change |
620 // from user action, no need to notify UI to pop uo the volume slider bar. | 634 // event is in responding to SetOutputNodeVolume request from intializaing |
621 if (init_node_id_ == node_id && init_volume_ == volume) { | 635 // audio state, not from user action, no need to notify UI to pop up the |
622 init_volume_count_--; | 636 // volume slider bar. |
623 if (!init_volume_count_) | 637 if (init_node_id_ == node_id && init_volume_ == volume) { |
624 initializing_audio_state_ = false; | 638 init_volume_count_--; |
639 if (!init_volume_count_) | |
640 volume_change_not_by_user_ ^= INITIALIZING_AUDIO_STATE; | |
641 return; | |
642 } else { | |
643 // Reset the INITIALIZING_AUDIO_STATE in case SetOutputNodeVolume | |
644 // request is lost by cras due to cras is not ready when | |
645 // CrasAudioHandler is being initialized. | |
646 volume_change_not_by_user_ ^= INITIALIZING_AUDIO_STATE; | |
647 init_volume_count_ = 0; | |
648 } | |
649 } else if (volume_change_not_by_user_ & MAXIMIZE_MODE_SCREENSHOT) { | |
650 // Do not notify the observers for the volume changed event if that is | |
651 // coming from a restoring volume after a maximize mode screenshot is | |
652 // taken. Reset the MAXIMIZE_MODE_SCREENSHOT. | |
653 volume_change_not_by_user_ ^= MAXIMIZE_MODE_SCREENSHOT; | |
625 return; | 654 return; |
626 } else { | |
627 // Reset the initializing_audio_state_ in case SetOutputNodeVolume request | |
628 // is lost by cras due to cras is not ready when CrasAudioHandler is being | |
629 // initialized. | |
630 initializing_audio_state_ = false; | |
631 init_volume_count_ = 0; | |
632 } | 655 } |
633 } | 656 } |
634 | 657 |
635 FOR_EACH_OBSERVER(AudioObserver, observers_, | 658 FOR_EACH_OBSERVER(AudioObserver, observers_, |
636 OnOutputNodeVolumeChanged(node_id, volume)); | 659 OnOutputNodeVolumeChanged(node_id, volume)); |
637 } | 660 } |
638 | 661 |
639 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { | 662 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { |
640 if (active_output_node_id_ == node_id) | 663 if (active_output_node_id_ == node_id) |
641 return; | 664 return; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
731 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { | 754 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { |
732 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; | 755 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; |
733 output_mute_on_ = true; | 756 output_mute_on_ = true; |
734 } else { | 757 } else { |
735 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); | 758 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); |
736 } | 759 } |
737 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); | 760 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); |
738 | 761 |
739 SetOutputMuteInternal(output_mute_on_); | 762 SetOutputMuteInternal(output_mute_on_); |
740 | 763 |
741 if (initializing_audio_state_) { | 764 if (volume_change_not_by_user_ & INITIALIZING_AUDIO_STATE) { |
742 // During power up, InitializeAudioState() could be called twice, first | 765 // During power up, InitializeAudioState() could be called twice, first |
743 // by CrasAudioHandler constructor, then by cras server restarting signal, | 766 // by CrasAudioHandler constructor, then by cras server restarting signal, |
744 // both sending SetOutputNodeVolume requests, and could lead to two | 767 // both sending SetOutputNodeVolume requests, and could lead to two |
745 // OutputNodeVolumeChanged signals. | 768 // OutputNodeVolumeChanged signals. |
746 init_volume_count_++; | 769 init_volume_count_++; |
747 init_node_id_ = active_output_node_id_; | 770 init_node_id_ = active_output_node_id_; |
748 init_volume_ = output_volume_; | 771 init_volume_ = output_volume_; |
749 } | 772 } |
750 SetOutputNodeVolume(active_output_node_id_, output_volume_); | 773 SetOutputNodeVolume(active_output_node_id_, output_volume_); |
751 } | 774 } |
(...skipping 15 matching lines...) Expand all Loading... | |
767 // devices. For input devices, we don't restore their gain value so far. | 790 // 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 | 791 // TODO(jennyz): crbug.com/417418, track the status for the decison if |
769 // we should persist input gain value in prefs. | 792 // we should persist input gain value in prefs. |
770 if (!device->is_input) { | 793 if (!device->is_input) { |
771 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); | 794 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); |
772 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); | 795 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); |
773 } | 796 } |
774 } | 797 } |
775 | 798 |
776 void CrasAudioHandler::InitializeAudioState() { | 799 void CrasAudioHandler::InitializeAudioState() { |
777 initializing_audio_state_ = true; | 800 volume_change_not_by_user_ |= INITIALIZING_AUDIO_STATE; |
778 ApplyAudioPolicy(); | 801 ApplyAudioPolicy(); |
779 | 802 |
780 // Defer querying cras for GetNodes until cras service becomes available. | 803 // Defer querying cras for GetNodes until cras service becomes available. |
781 cras_service_available_ = false; | 804 cras_service_available_ = false; |
782 chromeos::DBusThreadManager::Get() | 805 chromeos::DBusThreadManager::Get() |
783 ->GetCrasAudioClient() | 806 ->GetCrasAudioClient() |
784 ->WaitForServiceToBeAvailable(base::Bind( | 807 ->WaitForServiceToBeAvailable(base::Bind( |
785 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, | 808 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, |
786 weak_ptr_factory_.GetWeakPtr())); | 809 weak_ptr_factory_.GetWeakPtr())); |
787 } | 810 } |
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1372 hdmi_rediscover_grace_period_duration_in_ms_), | 1395 hdmi_rediscover_grace_period_duration_in_ms_), |
1373 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1396 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
1374 } | 1397 } |
1375 | 1398 |
1376 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1399 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
1377 int duration_in_ms) { | 1400 int duration_in_ms) { |
1378 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1401 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
1379 } | 1402 } |
1380 | 1403 |
1381 } // namespace chromeos | 1404 } // namespace chromeos |
OLD | NEW |