Chromium Code Reviews| 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 AutomatedVolumeChangeReason { | |
| 29 VOLUME_CHANGE_INITIALIZING_AUDIO_STATE = 1 << 0, | |
| 30 VOLUME_CHANGE_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::SetOutputVolumePercentWithoutNotifyingObservers( | |
| 385 int volume_percent) { | |
| 386 // Currently SetOutputVolumePercentInQuietMode is only called from | |
| 387 // maximize mode screenshot in PowerButtonController. Everytime this method | |
| 388 // is called, DCHECK if MAXIMIZE_MODE_SCREENSHOT is already released. | |
| 389 DCHECK(!(automated_volume_change_ & VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT)); | |
|
Daniel Erat
2016/07/29 17:53:38
this dcheck makes me nervous. if MAXIMIZE_MODE_SCR
Qiang(Joe) Xu
2016/07/29 18:58:23
Your concern is reasonable. I am assuming CRAS is
| |
| 390 automated_volume_change_ |= VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT; | |
| 391 SetOutputVolumePercent(volume_percent); | |
| 392 } | |
| 393 | |
| 379 // TODO: Rename the 'Percent' to something more meaningful. | 394 // TODO: Rename the 'Percent' to something more meaningful. |
| 380 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { | 395 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { |
| 381 // TODO(jennyz): Should we set all input devices' gain to the same level? | 396 // TODO(jennyz): Should we set all input devices' gain to the same level? |
| 382 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 397 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 383 it != audio_devices_.end(); | 398 it != audio_devices_.end(); |
| 384 it++) { | 399 it++) { |
| 385 const AudioDevice& device = it->second; | 400 const AudioDevice& device = it->second; |
| 386 if (device.is_input && device.active) | 401 if (device.is_input && device.active) |
| 387 SetInputNodeGainPercent(active_input_node_id_, gain_percent); | 402 SetInputNodeGainPercent(active_input_node_id_, gain_percent); |
| 388 } | 403 } |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 } | 621 } |
| 607 | 622 |
| 608 // Sync internal volume state and notify UI for the change. We trust cras | 623 // 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 | 624 // 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 | 625 // 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 | 626 // Bluetooth headset, etc. Assume all active output devices share a single |
| 612 // volume. | 627 // volume. |
| 613 output_volume_ = volume; | 628 output_volume_ = volume; |
| 614 audio_pref_handler_->SetVolumeGainValue(*device, volume); | 629 audio_pref_handler_->SetVolumeGainValue(*device, volume); |
| 615 | 630 |
| 616 if (initializing_audio_state_) { | 631 bool should_not_notify_observers = false; |
| 632 if (automated_volume_change_ & VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) { | |
| 617 // Do not notify the observers for volume changed event if CrasAudioHandler | 633 // Do not notify the observers for volume changed event if CrasAudioHandler |
| 618 // is initializing its state, i.e., the volume change event is in responding | 634 // is initializing its state, i.e., the volume change event is in responding |
| 619 // to SetOutputNodeVolume request from intializaing audio state, not | 635 // to SetOutputNodeVolume request from intializaing audio state, not from |
|
Daniel Erat
2016/07/29 17:53:38
nit: s/intializaing/initializing/
Qiang(Joe) Xu
2016/07/29 18:58:23
Done.
| |
| 620 // from user action, no need to notify UI to pop uo the volume slider bar. | 636 // user action, no need to notify UI to pop up the volume slider bar. |
| 621 if (init_node_id_ == node_id && init_volume_ == volume) { | 637 if (init_node_id_ == node_id && init_volume_ == volume) { |
| 622 init_volume_count_--; | 638 init_volume_count_--; |
| 623 if (!init_volume_count_) | 639 if (!init_volume_count_) |
| 624 initializing_audio_state_ = false; | 640 automated_volume_change_ ^= VOLUME_CHANGE_INITIALIZING_AUDIO_STATE; |
| 625 return; | 641 should_not_notify_observers = true; |
| 626 } else { | 642 } else { |
| 627 // Reset the initializing_audio_state_ in case SetOutputNodeVolume request | 643 // Reset the INITIALIZING_AUDIO_STATE in case SetOutputNodeVolume request |
| 628 // is lost by cras due to cras is not ready when CrasAudioHandler is being | 644 // is lost by cras due to cras is not ready when CrasAudioHandler is |
| 629 // initialized. | 645 // being initialized. |
| 630 initializing_audio_state_ = false; | 646 automated_volume_change_ ^= VOLUME_CHANGE_INITIALIZING_AUDIO_STATE; |
| 631 init_volume_count_ = 0; | 647 init_volume_count_ = 0; |
| 632 } | 648 } |
| 633 } | 649 } |
| 634 | 650 |
| 635 FOR_EACH_OBSERVER(AudioObserver, observers_, | 651 if (automated_volume_change_ & VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT) { |
| 636 OnOutputNodeVolumeChanged(node_id, volume)); | 652 // Do not notify the observers for the volume changed event if that is |
| 653 // coming from a restoring volume after a maximize mode screenshot is | |
| 654 // taken. Reset the MAXIMIZE_MODE_SCREENSHOT. | |
| 655 automated_volume_change_ ^= VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT; | |
| 656 should_not_notify_observers = true; | |
| 657 } | |
| 658 | |
| 659 if (!should_not_notify_observers) { | |
| 660 FOR_EACH_OBSERVER(AudioObserver, observers_, | |
| 661 OnOutputNodeVolumeChanged(node_id, volume)); | |
| 662 } | |
| 637 } | 663 } |
| 638 | 664 |
| 639 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { | 665 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { |
| 640 if (active_output_node_id_ == node_id) | 666 if (active_output_node_id_ == node_id) |
| 641 return; | 667 return; |
| 642 | 668 |
| 643 // Active audio output device should always be changed by chrome. | 669 // Active audio output device should always be changed by chrome. |
| 644 // During system boot, cras may change active input to unknown device 0x1, | 670 // 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. | 671 // we don't need to log it, since it is not an valid device. |
| 646 if (GetDeviceFromId(node_id)) { | 672 if (GetDeviceFromId(node_id)) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { | 757 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { |
| 732 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; | 758 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; |
| 733 output_mute_on_ = true; | 759 output_mute_on_ = true; |
| 734 } else { | 760 } else { |
| 735 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); | 761 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); |
| 736 } | 762 } |
| 737 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); | 763 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); |
| 738 | 764 |
| 739 SetOutputMuteInternal(output_mute_on_); | 765 SetOutputMuteInternal(output_mute_on_); |
| 740 | 766 |
| 741 if (initializing_audio_state_) { | 767 if (automated_volume_change_ & VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) { |
| 742 // During power up, InitializeAudioState() could be called twice, first | 768 // During power up, InitializeAudioState() could be called twice, first |
| 743 // by CrasAudioHandler constructor, then by cras server restarting signal, | 769 // by CrasAudioHandler constructor, then by cras server restarting signal, |
| 744 // both sending SetOutputNodeVolume requests, and could lead to two | 770 // both sending SetOutputNodeVolume requests, and could lead to two |
| 745 // OutputNodeVolumeChanged signals. | 771 // OutputNodeVolumeChanged signals. |
| 746 init_volume_count_++; | 772 init_volume_count_++; |
| 747 init_node_id_ = active_output_node_id_; | 773 init_node_id_ = active_output_node_id_; |
| 748 init_volume_ = output_volume_; | 774 init_volume_ = output_volume_; |
| 749 } | 775 } |
| 750 SetOutputNodeVolume(active_output_node_id_, output_volume_); | 776 SetOutputNodeVolume(active_output_node_id_, output_volume_); |
| 751 } | 777 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 767 // devices. For input devices, we don't restore their gain value so far. | 793 // 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 | 794 // TODO(jennyz): crbug.com/417418, track the status for the decison if |
| 769 // we should persist input gain value in prefs. | 795 // we should persist input gain value in prefs. |
| 770 if (!device->is_input) { | 796 if (!device->is_input) { |
| 771 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); | 797 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); |
| 772 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); | 798 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); |
| 773 } | 799 } |
| 774 } | 800 } |
| 775 | 801 |
| 776 void CrasAudioHandler::InitializeAudioState() { | 802 void CrasAudioHandler::InitializeAudioState() { |
| 777 initializing_audio_state_ = true; | 803 automated_volume_change_ |= VOLUME_CHANGE_INITIALIZING_AUDIO_STATE; |
| 778 ApplyAudioPolicy(); | 804 ApplyAudioPolicy(); |
| 779 | 805 |
| 780 // Defer querying cras for GetNodes until cras service becomes available. | 806 // Defer querying cras for GetNodes until cras service becomes available. |
| 781 cras_service_available_ = false; | 807 cras_service_available_ = false; |
| 782 chromeos::DBusThreadManager::Get() | 808 chromeos::DBusThreadManager::Get() |
| 783 ->GetCrasAudioClient() | 809 ->GetCrasAudioClient() |
| 784 ->WaitForServiceToBeAvailable(base::Bind( | 810 ->WaitForServiceToBeAvailable(base::Bind( |
| 785 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, | 811 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, |
| 786 weak_ptr_factory_.GetWeakPtr())); | 812 weak_ptr_factory_.GetWeakPtr())); |
| 787 } | 813 } |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1372 hdmi_rediscover_grace_period_duration_in_ms_), | 1398 hdmi_rediscover_grace_period_duration_in_ms_), |
| 1373 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1399 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
| 1374 } | 1400 } |
| 1375 | 1401 |
| 1376 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1402 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
| 1377 int duration_in_ms) { | 1403 int duration_in_ms) { |
| 1378 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1404 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
| 1379 } | 1405 } |
| 1380 | 1406 |
| 1381 } // namespace chromeos | 1407 } // namespace chromeos |
| OLD | NEW |