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 <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "chromeos/audio/audio_devices_pref_handler.h" | 13 #include "chromeos/audio/audio_devices_pref_handler.h" |
| 14 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 14 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "media/audio/audio_manager.h" | |
| 16 | 17 |
| 17 using std::max; | 18 using std::max; |
| 18 using std::min; | 19 using std::min; |
| 19 | 20 |
| 20 namespace chromeos { | 21 namespace chromeos { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Default value for unmuting, as a percent in the range [0, 100]. | 25 // Default value for unmuting, as a percent in the range [0, 100]. |
| 25 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. | 26 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 | 75 |
| 75 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() { | 76 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() { |
| 76 } | 77 } |
| 77 | 78 |
| 78 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { | 79 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { |
| 79 } | 80 } |
| 80 | 81 |
| 81 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() { | 82 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() { |
| 82 } | 83 } |
| 83 | 84 |
| 85 CrasAudioHandler::AudioManagerWrapper::AudioManagerWrapper() { | |
| 86 } | |
| 87 CrasAudioHandler::AudioManagerWrapper::~AudioManagerWrapper() { | |
| 88 } | |
| 89 | |
| 90 CrasAudioHandler::AudioManagerWrapperImpl::AudioManagerWrapperImpl() { | |
| 91 } | |
| 92 CrasAudioHandler::AudioManagerWrapperImpl::~AudioManagerWrapperImpl() { | |
| 93 } | |
| 94 | |
| 95 void CrasAudioHandler::AudioManagerWrapperImpl::SetHasInputDevices( | |
| 96 bool has_input_devices) { | |
| 97 media::AudioManager::Get()->SetHasInputDevices(has_input_devices); | |
| 98 } | |
| 99 | |
| 84 // static | 100 // static |
| 85 void CrasAudioHandler::Initialize( | 101 void CrasAudioHandler::Initialize( |
| 86 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) { | 102 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler, |
| 103 scoped_ptr<AudioManagerWrapper> audio_manager) { | |
| 87 CHECK(!g_cras_audio_handler); | 104 CHECK(!g_cras_audio_handler); |
| 88 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); | 105 // Creates an AudioManagerWrapperImpl if audio_manager is not set by |
| 106 // caller. | |
| 107 if (!audio_manager.get()) | |
| 108 audio_manager.reset(new CrasAudioHandler::AudioManagerWrapperImpl()); | |
| 109 g_cras_audio_handler = | |
| 110 new CrasAudioHandler(audio_pref_handler, audio_manager.Pass()); | |
| 89 } | 111 } |
| 90 | 112 |
| 91 // static | 113 // static |
| 92 void CrasAudioHandler::InitializeForTesting() { | 114 void CrasAudioHandler::InitializeForTesting() { |
| 93 CHECK(!g_cras_audio_handler); | 115 CHECK(!g_cras_audio_handler); |
| 94 CrasAudioHandler::Initialize(new AudioDevicesPrefHandlerStub()); | 116 scoped_ptr<AudioManagerWrapper> audio_manager( |
| 117 new CrasAudioHandler::AudioManagerWrapperImpl()); | |
| 118 CrasAudioHandler::Initialize(new AudioDevicesPrefHandlerStub(), | |
| 119 audio_manager.Pass()); | |
| 95 } | 120 } |
| 96 | 121 |
| 97 // static | 122 // static |
| 98 void CrasAudioHandler::Shutdown() { | 123 void CrasAudioHandler::Shutdown() { |
| 99 CHECK(g_cras_audio_handler); | 124 CHECK(g_cras_audio_handler); |
| 100 delete g_cras_audio_handler; | 125 delete g_cras_audio_handler; |
| 101 g_cras_audio_handler = NULL; | 126 g_cras_audio_handler = NULL; |
| 102 } | 127 } |
| 103 | 128 |
| 104 // static | 129 // static |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 // Input device's mute state is not recorded in the pref. crbug.com/365050. | 451 // Input device's mute state is not recorded in the pref. crbug.com/365050. |
| 427 if (device && !device->is_input) | 452 if (device && !device->is_input) |
| 428 audio_pref_handler_->SetMuteValue(*device, mute_on); | 453 audio_pref_handler_->SetMuteValue(*device, mute_on); |
| 429 } | 454 } |
| 430 | 455 |
| 431 void CrasAudioHandler::LogErrors() { | 456 void CrasAudioHandler::LogErrors() { |
| 432 log_errors_ = true; | 457 log_errors_ = true; |
| 433 } | 458 } |
| 434 | 459 |
| 435 CrasAudioHandler::CrasAudioHandler( | 460 CrasAudioHandler::CrasAudioHandler( |
| 436 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) | 461 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler, |
| 462 scoped_ptr<AudioManagerWrapper> audio_manager) | |
| 437 : audio_pref_handler_(audio_pref_handler), | 463 : audio_pref_handler_(audio_pref_handler), |
| 438 output_mute_on_(false), | 464 output_mute_on_(false), |
| 439 input_mute_on_(false), | 465 input_mute_on_(false), |
| 440 output_volume_(0), | 466 output_volume_(0), |
| 441 input_gain_(0), | 467 input_gain_(0), |
| 442 active_output_node_id_(0), | 468 active_output_node_id_(0), |
| 443 active_input_node_id_(0), | 469 active_input_node_id_(0), |
| 444 has_alternative_input_(false), | 470 has_alternative_input_(false), |
| 445 has_alternative_output_(false), | 471 has_alternative_output_(false), |
| 446 output_mute_locked_(false), | 472 output_mute_locked_(false), |
| 447 log_errors_(false), | 473 log_errors_(false), |
| 448 weak_ptr_factory_(this) { | 474 weak_ptr_factory_(this) { |
| 449 if (!audio_pref_handler.get()) | 475 if (!audio_pref_handler.get()) |
|
Daniel Erat
2015/06/24 13:37:45
this isn't part of your change, but it seems stran
cychiang
2015/06/25 05:45:24
This was added long time ago in https://chromiumco
| |
| 450 return; | 476 return; |
| 477 audio_manager_ = audio_manager.Pass(); | |
| 451 // If the DBusThreadManager or the CrasAudioClient aren't available, there | 478 // If the DBusThreadManager or the CrasAudioClient aren't available, there |
| 452 // isn't much we can do. This should only happen when running tests. | 479 // isn't much we can do. This should only happen when running tests. |
| 453 if (!chromeos::DBusThreadManager::IsInitialized() || | 480 if (!chromeos::DBusThreadManager::IsInitialized() || |
| 454 !chromeos::DBusThreadManager::Get() || | 481 !chromeos::DBusThreadManager::Get() || |
| 455 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | 482 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) |
| 456 return; | 483 return; |
| 457 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); | 484 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); |
| 458 audio_pref_handler_->AddAudioPrefObserver(this); | 485 audio_pref_handler_->AddAudioPrefObserver(this); |
| 459 if (chromeos::DBusThreadManager::Get()->GetSessionManagerClient()) { | 486 if (chromeos::DBusThreadManager::Get()->GetSessionManagerClient()) { |
| 460 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | 487 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 896 } else { | 923 } else { |
| 897 // ditto input node case. | 924 // ditto input node case. |
| 898 if (!active_output_node_id_ || hotplug_output_nodes.empty() || | 925 if (!active_output_node_id_ || hotplug_output_nodes.empty() || |
| 899 IsNodeInTheList(output_devices_pq_.top().id, hotplug_output_nodes)) { | 926 IsNodeInTheList(output_devices_pq_.top().id, hotplug_output_nodes)) { |
| 900 SwitchToDevice(output_devices_pq_.top(), true); | 927 SwitchToDevice(output_devices_pq_.top(), true); |
| 901 } | 928 } |
| 902 } | 929 } |
| 903 } | 930 } |
| 904 } | 931 } |
| 905 | 932 |
| 933 void CrasAudioHandler::UpdateAudioManagerHasInputDevices() { | |
| 934 AudioDeviceList devices; | |
| 935 GetAudioDevices(&devices); | |
| 936 for (size_t i = 0; i < devices.size(); ++i) { | |
| 937 if (devices[i].is_input && devices[i].is_for_simple_usage()) { | |
| 938 audio_manager_->SetHasInputDevices(true); | |
| 939 return; | |
| 940 } | |
| 941 } | |
| 942 audio_manager_->SetHasInputDevices(false); | |
| 943 } | |
| 944 | |
| 906 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, | 945 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, |
| 907 bool success) { | 946 bool success) { |
| 908 if (!success) { | 947 if (!success) { |
| 909 LOG_IF(ERROR, log_errors_) << "Failed to retrieve audio nodes data"; | 948 LOG_IF(ERROR, log_errors_) << "Failed to retrieve audio nodes data"; |
| 910 return; | 949 return; |
| 911 } | 950 } |
| 912 | 951 |
| 913 UpdateDevicesAndSwitchActive(node_list); | 952 UpdateDevicesAndSwitchActive(node_list); |
| 953 UpdateAudioManagerHasInputDevices(); | |
| 914 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 954 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
| 915 } | 955 } |
| 916 | 956 |
| 917 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, | 957 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, |
| 918 const std::string& error_msg) { | 958 const std::string& error_msg) { |
| 919 LOG_IF(ERROR, log_errors_) << "Failed to call GetNodes: " | 959 LOG_IF(ERROR, log_errors_) << "Failed to call GetNodes: " |
| 920 << error_name << ": " << error_msg; | 960 << error_name << ": " << error_msg; |
| 921 } | 961 } |
| 922 | 962 |
| 923 void CrasAudioHandler::AddAdditionalActiveNode(uint64_t node_id, bool notify) { | 963 void CrasAudioHandler::AddAdditionalActiveNode(uint64_t node_id, bool notify) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 970 active_output_node_id_ = 0; | 1010 active_output_node_id_ = 0; |
| 971 chromeos::DBusThreadManager::Get() | 1011 chromeos::DBusThreadManager::Get() |
| 972 ->GetCrasAudioClient() | 1012 ->GetCrasAudioClient() |
| 973 ->RemoveActiveOutputNode(node_id); | 1013 ->RemoveActiveOutputNode(node_id); |
| 974 if (notify) | 1014 if (notify) |
| 975 NotifyActiveNodeChanged(false); | 1015 NotifyActiveNodeChanged(false); |
| 976 } | 1016 } |
| 977 } | 1017 } |
| 978 | 1018 |
| 979 } // namespace chromeos | 1019 } // namespace chromeos |
| OLD | NEW |