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/mock_cras_audio_handler.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 | 16 |
| 17 using std::max; | 17 using std::max; |
| 18 using std::min; | 18 using std::min; |
| 19 | 19 |
| 20 namespace chromeos { | 20 namespace chromeos { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Default value for unmuting, as a percent in the range [0, 100]. | 24 // Default value for unmuting, as a percent in the range [0, 100]. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 // static | 62 // static |
| 63 void CrasAudioHandler::Initialize( | 63 void CrasAudioHandler::Initialize( |
| 64 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) { | 64 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) { |
| 65 CHECK(!g_cras_audio_handler); | 65 CHECK(!g_cras_audio_handler); |
| 66 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); | 66 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // static | 69 // static |
| 70 void CrasAudioHandler::InitializeForTesting() { | 70 void CrasAudioHandler::InitializeForTesting() { |
| 71 CHECK(!g_cras_audio_handler); | 71 CHECK(!g_cras_audio_handler); |
| 72 g_cras_audio_handler = new MockCrasAudioHandler(); | 72 CrasAudioHandler::Initialize(new AudioDevicesPrefHandlerStub()); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // static | 75 // static |
| 76 void CrasAudioHandler::Shutdown() { | 76 void CrasAudioHandler::Shutdown() { |
| 77 CHECK(g_cras_audio_handler); | 77 CHECK(g_cras_audio_handler); |
| 78 delete g_cras_audio_handler; | 78 delete g_cras_audio_handler; |
| 79 g_cras_audio_handler = NULL; | 79 g_cras_audio_handler = NULL; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // static | 82 // static |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 | 157 |
| 158 uint64 CrasAudioHandler::GetActiveOutputNode() const { | 158 uint64 CrasAudioHandler::GetActiveOutputNode() const { |
| 159 return active_output_node_id_; | 159 return active_output_node_id_; |
| 160 } | 160 } |
| 161 | 161 |
| 162 uint64 CrasAudioHandler::GetActiveInputNode() const { | 162 uint64 CrasAudioHandler::GetActiveInputNode() const { |
| 163 return active_input_node_id_; | 163 return active_input_node_id_; |
| 164 } | 164 } |
| 165 | 165 |
| 166 void CrasAudioHandler::GetAudioDevices(AudioDeviceList* device_list) const { | 166 void CrasAudioHandler::GetAudioDevices(AudioDeviceList* device_list) const { |
| 167 device_list->clear(); | |
|
rkc
2013/07/23 20:37:43
A DCHECK(device_list) might be appropriate right b
jennyz
2013/07/24 21:12:25
I think it is OK for client to pass in an non-empt
| |
| 167 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 168 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 168 it != audio_devices_.end(); ++it) | 169 it != audio_devices_.end(); ++it) |
| 169 device_list->push_back(it->second); | 170 device_list->push_back(it->second); |
| 170 } | 171 } |
| 171 | 172 |
| 172 bool CrasAudioHandler::GetActiveOutputDevice(AudioDevice* device) const { | 173 bool CrasAudioHandler::GetActiveOutputDevice(AudioDevice* device) const { |
| 173 const AudioDevice* active_device = GetDeviceFromId(active_output_node_id_); | 174 const AudioDevice* active_device = GetDeviceFromId(active_output_node_id_); |
| 174 if (!device) | 175 if (!device) |
| 175 return false; | 176 return false; |
| 176 *device = *active_device; | 177 *device = *active_device; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 } | 213 } |
| 213 | 214 |
| 214 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) { | 215 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) { |
| 215 SetOutputVolumePercent(output_volume_ + adjust_by_percent); | 216 SetOutputVolumePercent(output_volume_ + adjust_by_percent); |
| 216 } | 217 } |
| 217 | 218 |
| 218 void CrasAudioHandler::SetOutputMute(bool mute_on) { | 219 void CrasAudioHandler::SetOutputMute(bool mute_on) { |
| 219 if (!SetOutputMuteInternal(mute_on)) | 220 if (!SetOutputMuteInternal(mute_on)) |
| 220 return; | 221 return; |
| 221 | 222 |
| 222 output_mute_on_ = mute_on; | |
| 223 | |
| 224 if (const AudioDevice* device = GetDeviceFromId(active_output_node_id_)) | 223 if (const AudioDevice* device = GetDeviceFromId(active_output_node_id_)) |
| 225 audio_pref_handler_->SetMuteValue(*device, output_mute_on_); | 224 audio_pref_handler_->SetMuteValue(*device, output_mute_on_); |
| 226 | 225 |
| 227 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputMuteChanged()); | 226 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputMuteChanged()); |
| 228 } | 227 } |
| 229 | 228 |
| 230 void CrasAudioHandler::AdjustOutputVolumeToAudibleLevel() { | 229 void CrasAudioHandler::AdjustOutputVolumeToAudibleLevel() { |
| 231 if (output_volume_ <= kMuteThresholdPercent) { | 230 if (output_volume_ <= kMuteThresholdPercent) { |
| 232 // Avoid the situation when sound has been unmuted, but the volume | 231 // Avoid the situation when sound has been unmuted, but the volume |
| 233 // is set to a very low value, so user still can't hear any sound. | 232 // is set to a very low value, so user still can't hear any sound. |
| 234 SetOutputVolumePercent(kDefaultUnmuteVolumePercent); | 233 SetOutputVolumePercent(kDefaultUnmuteVolumePercent); |
| 235 } | 234 } |
| 236 } | 235 } |
| 237 | 236 |
| 238 void CrasAudioHandler::SetInputMute(bool mute_on) { | 237 void CrasAudioHandler::SetInputMute(bool mute_on) { |
| 239 if (!SetInputMuteInternal(mute_on)) | 238 if (!SetInputMuteInternal(mute_on)) |
| 240 return; | 239 return; |
| 241 | 240 |
| 242 input_mute_on_ = mute_on; | |
| 243 | |
| 244 AudioDevice device; | 241 AudioDevice device; |
| 245 if (const AudioDevice* device = GetDeviceFromId(active_input_node_id_)) | 242 if (const AudioDevice* device = GetDeviceFromId(active_input_node_id_)) |
| 246 audio_pref_handler_->SetMuteValue(*device, input_mute_on_); | 243 audio_pref_handler_->SetMuteValue(*device, input_mute_on_); |
| 247 | 244 |
| 248 | 245 |
| 249 FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputMuteChanged()); | 246 FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputMuteChanged()); |
| 250 } | 247 } |
| 251 | 248 |
| 252 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) { | 249 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) { |
| 253 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 250 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 | 432 |
| 436 void CrasAudioHandler::SetOutputNodeVolume(uint64 node_id, int volume) { | 433 void CrasAudioHandler::SetOutputNodeVolume(uint64 node_id, int volume) { |
| 437 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 434 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 438 SetOutputNodeVolume(node_id, volume); | 435 SetOutputNodeVolume(node_id, volume); |
| 439 } | 436 } |
| 440 | 437 |
| 441 bool CrasAudioHandler::SetOutputMuteInternal(bool mute_on) { | 438 bool CrasAudioHandler::SetOutputMuteInternal(bool mute_on) { |
| 442 if (output_mute_locked_) | 439 if (output_mute_locked_) |
| 443 return false; | 440 return false; |
| 444 | 441 |
| 442 output_mute_on_ = mute_on; | |
| 445 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 443 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 446 SetOutputUserMute(mute_on); | 444 SetOutputUserMute(mute_on); |
| 447 return true; | 445 return true; |
| 448 } | 446 } |
| 449 | 447 |
| 450 void CrasAudioHandler::SetInputNodeGain(uint64 node_id, int gain) { | 448 void CrasAudioHandler::SetInputNodeGain(uint64 node_id, int gain) { |
| 451 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 449 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 452 SetInputNodeGain(node_id, gain); | 450 SetInputNodeGain(node_id, gain); |
| 453 } | 451 } |
| 454 | 452 |
| 455 bool CrasAudioHandler::SetInputMuteInternal(bool mute_on) { | 453 bool CrasAudioHandler::SetInputMuteInternal(bool mute_on) { |
| 456 if (input_mute_locked_) | 454 if (input_mute_locked_) |
| 457 return false; | 455 return false; |
| 458 | 456 |
| 457 input_mute_on_ = mute_on; | |
| 459 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 458 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 460 SetInputMute(mute_on); | 459 SetInputMute(mute_on); |
| 461 return true; | 460 return true; |
| 462 } | 461 } |
| 463 | 462 |
| 464 void CrasAudioHandler::GetNodes() { | 463 void CrasAudioHandler::GetNodes() { |
| 465 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( | 464 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( |
| 466 base::Bind(&CrasAudioHandler::HandleGetNodes, | 465 base::Bind(&CrasAudioHandler::HandleGetNodes, |
| 467 weak_ptr_factory_.GetWeakPtr())); | 466 weak_ptr_factory_.GetWeakPtr())); |
| 468 } | 467 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 539 if (!success) { | 538 if (!success) { |
| 540 LOG(ERROR) << "Failed to retrieve audio nodes data"; | 539 LOG(ERROR) << "Failed to retrieve audio nodes data"; |
| 541 return; | 540 return; |
| 542 } | 541 } |
| 543 | 542 |
| 544 UpdateDevicesAndSwitchActive(node_list); | 543 UpdateDevicesAndSwitchActive(node_list); |
| 545 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 544 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
| 546 } | 545 } |
| 547 | 546 |
| 548 } // namespace chromeos | 547 } // namespace chromeos |
| OLD | NEW |