| 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 SetOutputVolumePercentWithoutNotifyingObservers is only called |
| 387 // from maximize mode screenshot in PowerButtonController. |
| 388 automated_volume_change_ |= VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT; |
| 389 maximize_mode_screenshot_count_++; |
| 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 bool should_not_notify_observers = false; |
| 631 if (automated_volume_change_ & VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) { |
| 617 // Do not notify the observers for volume changed event if CrasAudioHandler | 632 // 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 | 633 // is initializing its state, i.e., the volume change event is in responding |
| 619 // to SetOutputNodeVolume request from intializaing audio state, not | 634 // to SetOutputNodeVolume request from initializing audio state, not from |
| 620 // from user action, no need to notify UI to pop uo the volume slider bar. | 635 // user action, no need to notify UI to pop up the volume slider bar. |
| 621 if (init_node_id_ == node_id && init_volume_ == volume) { | 636 if (init_node_id_ == node_id && init_volume_ == volume) { |
| 622 init_volume_count_--; | 637 init_volume_count_--; |
| 623 if (!init_volume_count_) | 638 if (!init_volume_count_) |
| 624 initializing_audio_state_ = false; | 639 automated_volume_change_ ^= VOLUME_CHANGE_INITIALIZING_AUDIO_STATE; |
| 625 return; | 640 should_not_notify_observers = true; |
| 626 } else { | 641 } else { |
| 627 // Reset the initializing_audio_state_ in case SetOutputNodeVolume request | 642 // Reset the INITIALIZING_AUDIO_STATE in case SetOutputNodeVolume request |
| 628 // is lost by cras due to cras is not ready when CrasAudioHandler is being | 643 // is lost by cras due to cras is not ready when CrasAudioHandler is |
| 629 // initialized. | 644 // being initialized. |
| 630 initializing_audio_state_ = false; | 645 automated_volume_change_ ^= VOLUME_CHANGE_INITIALIZING_AUDIO_STATE; |
| 631 init_volume_count_ = 0; | 646 init_volume_count_ = 0; |
| 632 } | 647 } |
| 633 } | 648 } |
| 634 | 649 |
| 635 FOR_EACH_OBSERVER(AudioObserver, observers_, | 650 if (automated_volume_change_ & VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT) { |
| 636 OnOutputNodeVolumeChanged(node_id, volume)); | 651 // Do not notify the observers for the volume changed event if that is |
| 652 // coming from SetOutputVolumePercentWithoutNotifyingObservers, i.e., a |
| 653 // restoring volume after a maximize mode screenshot is taken. |
| 654 // Reset the MAXIMIZE_MODE_SCREENSHOT when there are no more |
| 655 // OutputNodeVolumeChanged being fired. |
| 656 DCHECK_GT(maximize_mode_screenshot_count_, 0); |
| 657 maximize_mode_screenshot_count_--; |
| 658 if (!maximize_mode_screenshot_count_) |
| 659 automated_volume_change_ ^= VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT; |
| 660 should_not_notify_observers = true; |
| 661 } |
| 662 |
| 663 if (!should_not_notify_observers) { |
| 664 FOR_EACH_OBSERVER(AudioObserver, observers_, |
| 665 OnOutputNodeVolumeChanged(node_id, volume)); |
| 666 } |
| 637 } | 667 } |
| 638 | 668 |
| 639 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { | 669 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { |
| 640 if (active_output_node_id_ == node_id) | 670 if (active_output_node_id_ == node_id) |
| 641 return; | 671 return; |
| 642 | 672 |
| 643 // Active audio output device should always be changed by chrome. | 673 // Active audio output device should always be changed by chrome. |
| 644 // During system boot, cras may change active input to unknown device 0x1, | 674 // 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. | 675 // we don't need to log it, since it is not an valid device. |
| 646 if (GetDeviceFromId(node_id)) { | 676 if (GetDeviceFromId(node_id)) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { | 761 if (hdmi_rediscovering_ && !IsHDMIPrimaryOutputDevice()) { |
| 732 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; | 762 VLOG(1) << "Mute the output during HDMI re-discovering grace period"; |
| 733 output_mute_on_ = true; | 763 output_mute_on_ = true; |
| 734 } else { | 764 } else { |
| 735 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); | 765 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); |
| 736 } | 766 } |
| 737 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); | 767 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); |
| 738 | 768 |
| 739 SetOutputMuteInternal(output_mute_on_); | 769 SetOutputMuteInternal(output_mute_on_); |
| 740 | 770 |
| 741 if (initializing_audio_state_) { | 771 if (automated_volume_change_ & VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) { |
| 742 // During power up, InitializeAudioState() could be called twice, first | 772 // During power up, InitializeAudioState() could be called twice, first |
| 743 // by CrasAudioHandler constructor, then by cras server restarting signal, | 773 // by CrasAudioHandler constructor, then by cras server restarting signal, |
| 744 // both sending SetOutputNodeVolume requests, and could lead to two | 774 // both sending SetOutputNodeVolume requests, and could lead to two |
| 745 // OutputNodeVolumeChanged signals. | 775 // OutputNodeVolumeChanged signals. |
| 746 init_volume_count_++; | 776 init_volume_count_++; |
| 747 init_node_id_ = active_output_node_id_; | 777 init_node_id_ = active_output_node_id_; |
| 748 init_volume_ = output_volume_; | 778 init_volume_ = output_volume_; |
| 749 } | 779 } |
| 750 SetOutputNodeVolume(active_output_node_id_, output_volume_); | 780 SetOutputNodeVolume(active_output_node_id_, output_volume_); |
| 751 } | 781 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 767 // devices. For input devices, we don't restore their gain value so far. | 797 // 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 | 798 // TODO(jennyz): crbug.com/417418, track the status for the decison if |
| 769 // we should persist input gain value in prefs. | 799 // we should persist input gain value in prefs. |
| 770 if (!device->is_input) { | 800 if (!device->is_input) { |
| 771 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); | 801 audio_pref_handler_->SetMuteValue(*device, IsOutputMuted()); |
| 772 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); | 802 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); |
| 773 } | 803 } |
| 774 } | 804 } |
| 775 | 805 |
| 776 void CrasAudioHandler::InitializeAudioState() { | 806 void CrasAudioHandler::InitializeAudioState() { |
| 777 initializing_audio_state_ = true; | 807 automated_volume_change_ |= VOLUME_CHANGE_INITIALIZING_AUDIO_STATE; |
| 778 ApplyAudioPolicy(); | 808 ApplyAudioPolicy(); |
| 779 | 809 |
| 780 // Defer querying cras for GetNodes until cras service becomes available. | 810 // Defer querying cras for GetNodes until cras service becomes available. |
| 781 cras_service_available_ = false; | 811 cras_service_available_ = false; |
| 782 chromeos::DBusThreadManager::Get() | 812 chromeos::DBusThreadManager::Get() |
| 783 ->GetCrasAudioClient() | 813 ->GetCrasAudioClient() |
| 784 ->WaitForServiceToBeAvailable(base::Bind( | 814 ->WaitForServiceToBeAvailable(base::Bind( |
| 785 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, | 815 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, |
| 786 weak_ptr_factory_.GetWeakPtr())); | 816 weak_ptr_factory_.GetWeakPtr())); |
| 787 } | 817 } |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1372 hdmi_rediscover_grace_period_duration_in_ms_), | 1402 hdmi_rediscover_grace_period_duration_in_ms_), |
| 1373 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1403 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
| 1374 } | 1404 } |
| 1375 | 1405 |
| 1376 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1406 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
| 1377 int duration_in_ms) { | 1407 int duration_in_ms) { |
| 1378 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1408 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
| 1379 } | 1409 } |
| 1380 | 1410 |
| 1381 } // namespace chromeos | 1411 } // namespace chromeos |
| OLD | NEW |