| 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/cras_audio_switch_handler.h" | |
| 15 #include "chromeos/audio/mock_cras_audio_handler.h" | 14 #include "chromeos/audio/mock_cras_audio_handler.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 | 16 |
| 18 using std::max; | 17 using std::max; |
| 19 using std::min; | 18 using std::min; |
| 20 | 19 |
| 21 namespace chromeos { | 20 namespace chromeos { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 | 34 |
| 36 CrasAudioHandler::AudioObserver::AudioObserver() { | 35 CrasAudioHandler::AudioObserver::AudioObserver() { |
| 37 } | 36 } |
| 38 | 37 |
| 39 CrasAudioHandler::AudioObserver::~AudioObserver() { | 38 CrasAudioHandler::AudioObserver::~AudioObserver() { |
| 40 } | 39 } |
| 41 | 40 |
| 42 void CrasAudioHandler::AudioObserver::OnOutputVolumeChanged() { | 41 void CrasAudioHandler::AudioObserver::OnOutputVolumeChanged() { |
| 43 } | 42 } |
| 44 | 43 |
| 45 void CrasAudioHandler::AudioObserver::OnInputGainChanged() { | |
| 46 } | |
| 47 | |
| 48 void CrasAudioHandler::AudioObserver::OnOutputMuteChanged() { | 44 void CrasAudioHandler::AudioObserver::OnOutputMuteChanged() { |
| 49 } | 45 } |
| 50 | 46 |
| 51 void CrasAudioHandler::AudioObserver::OnInputMuteChanged() { | 47 void CrasAudioHandler::AudioObserver::OnInputMuteChanged() { |
| 52 } | 48 } |
| 53 | 49 |
| 54 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() { | 50 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() { |
| 55 } | 51 } |
| 56 | 52 |
| 57 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { | 53 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 101 } |
| 106 | 102 |
| 107 bool CrasAudioHandler::IsInputMuted() { | 103 bool CrasAudioHandler::IsInputMuted() { |
| 108 return input_mute_on_; | 104 return input_mute_on_; |
| 109 } | 105 } |
| 110 | 106 |
| 111 int CrasAudioHandler::GetOutputVolumePercent() { | 107 int CrasAudioHandler::GetOutputVolumePercent() { |
| 112 return output_volume_; | 108 return output_volume_; |
| 113 } | 109 } |
| 114 | 110 |
| 115 int CrasAudioHandler::GetOutputVolumePercentForDevice(uint64 device_id) { | |
| 116 if (device_id == active_output_node_id_) | |
| 117 return output_volume_; | |
| 118 else | |
| 119 return (int) audio_pref_handler_->GetVolumeGainValue(device_id); | |
| 120 } | |
| 121 | |
| 122 int CrasAudioHandler::GetInputGainPercent() { | |
| 123 return input_gain_; | |
| 124 } | |
| 125 | |
| 126 int CrasAudioHandler::GetInputGainPercentForDevice(uint64 device_id) { | |
| 127 if (device_id == active_input_node_id_) | |
| 128 return input_gain_; | |
| 129 else | |
| 130 return (int) audio_pref_handler_->GetVolumeGainValue(device_id); | |
| 131 } | |
| 132 | |
| 133 uint64 CrasAudioHandler::GetActiveOutputNode() const { | 111 uint64 CrasAudioHandler::GetActiveOutputNode() const { |
| 134 return active_output_node_id_; | 112 return active_output_node_id_; |
| 135 } | 113 } |
| 136 | 114 |
| 137 uint64 CrasAudioHandler::GetActiveInputNode() const { | 115 uint64 CrasAudioHandler::GetActiveInputNode() const { |
| 138 return active_input_node_id_; | 116 return active_input_node_id_; |
| 139 } | 117 } |
| 140 | 118 |
| 141 void CrasAudioHandler::GetAudioDevices(AudioDeviceList* device_list) const { | 119 void CrasAudioHandler::GetAudioDevices(AudioDeviceList* device_list) const { |
| 142 for (size_t i = 0; i < audio_devices_.size(); ++i) | 120 for (size_t i = 0; i < audio_devices_.size(); ++i) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 165 volume_percent = min(max(volume_percent, 0), 100); | 143 volume_percent = min(max(volume_percent, 0), 100); |
| 166 if (volume_percent <= kMuteThresholdPercent) | 144 if (volume_percent <= kMuteThresholdPercent) |
| 167 volume_percent = 0; | 145 volume_percent = 0; |
| 168 SetOutputVolumeInternal(volume_percent); | 146 SetOutputVolumeInternal(volume_percent); |
| 169 if (IsOutputMuted() && volume_percent > 0) | 147 if (IsOutputMuted() && volume_percent > 0) |
| 170 SetOutputMute(false); | 148 SetOutputMute(false); |
| 171 if (!IsOutputMuted() && volume_percent == 0) | 149 if (!IsOutputMuted() && volume_percent == 0) |
| 172 SetOutputMute(true); | 150 SetOutputMute(true); |
| 173 } | 151 } |
| 174 | 152 |
| 175 void CrasAudioHandler::SetInputGainPercent(int gain_percent) { | |
| 176 gain_percent = min(max(gain_percent, 0), 100); | |
| 177 if (gain_percent <= kMuteThresholdPercent) | |
| 178 gain_percent = 0; | |
| 179 SetInputGainInternal(gain_percent); | |
| 180 if (IsInputMuted() && gain_percent > 0) | |
| 181 SetInputMute(false); | |
| 182 if (!IsInputMuted() && gain_percent == 0) | |
| 183 SetInputMute(true); | |
| 184 } | |
| 185 | |
| 186 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) { | 153 void CrasAudioHandler::AdjustOutputVolumeByPercent(int adjust_by_percent) { |
| 187 SetOutputVolumePercent(output_volume_ + adjust_by_percent); | 154 SetOutputVolumePercent(output_volume_ + adjust_by_percent); |
| 188 } | 155 } |
| 189 | 156 |
| 190 void CrasAudioHandler::SetOutputMute(bool mute_on) { | 157 void CrasAudioHandler::SetOutputMute(bool mute_on) { |
| 191 if (output_mute_locked_) | 158 if (output_mute_locked_) |
| 192 return; | 159 return; |
| 193 | 160 |
| 194 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 161 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 195 SetOutputMute(mute_on); | 162 SetOutputMute(mute_on); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 214 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) { | 181 void CrasAudioHandler::SetActiveOutputNode(uint64 node_id) { |
| 215 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 182 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 216 SetActiveOutputNode(node_id); | 183 SetActiveOutputNode(node_id); |
| 217 } | 184 } |
| 218 | 185 |
| 219 void CrasAudioHandler::SetActiveInputNode(uint64 node_id) { | 186 void CrasAudioHandler::SetActiveInputNode(uint64 node_id) { |
| 220 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 187 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 221 SetActiveInputNode(node_id); | 188 SetActiveInputNode(node_id); |
| 222 } | 189 } |
| 223 | 190 |
| 224 void CrasAudioHandler::SetVolumeGainPercentForDevice(uint64 device_id, | |
| 225 int value) { | |
| 226 if (device_id == active_output_node_id_) { | |
| 227 SetOutputVolumePercent(value); | |
| 228 return; | |
| 229 } else if (device_id == active_input_node_id_) { | |
| 230 SetInputGainPercent(value); | |
| 231 return; | |
| 232 } | |
| 233 | |
| 234 value = min(max(value, 0), 100); | |
| 235 if (value <= kMuteThresholdPercent) | |
| 236 value = 0; | |
| 237 | |
| 238 audio_pref_handler_->SetVolumeGainValue(device_id, value); | |
| 239 } | |
| 240 | |
| 241 CrasAudioHandler::CrasAudioHandler( | 191 CrasAudioHandler::CrasAudioHandler( |
| 242 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) | 192 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) |
| 243 : audio_pref_handler_(audio_pref_handler), | 193 : audio_pref_handler_(audio_pref_handler), |
| 244 weak_ptr_factory_(this), | 194 weak_ptr_factory_(this), |
| 245 output_mute_on_(false), | 195 output_mute_on_(false), |
| 246 input_mute_on_(false), | 196 input_mute_on_(false), |
| 247 output_volume_(0), | 197 output_volume_(0), |
| 248 input_gain_(0), | |
| 249 active_output_node_id_(0), | 198 active_output_node_id_(0), |
| 250 active_input_node_id_(0), | 199 active_input_node_id_(0), |
| 251 has_alternative_input_(false), | 200 has_alternative_input_(false), |
| 252 has_alternative_output_(false), | 201 has_alternative_output_(false), |
| 253 output_mute_locked_(false), | 202 output_mute_locked_(false), |
| 254 input_mute_locked_(false) { | 203 input_mute_locked_(false) { |
| 255 if (!audio_pref_handler) | 204 if (!audio_pref_handler) |
| 256 return; | 205 return; |
| 257 // If the DBusThreadManager or the CrasAudioClient aren't available, there | 206 // If the DBusThreadManager or the CrasAudioClient aren't available, there |
| 258 // isn't much we can do. This should only happen when running tests. | 207 // isn't much we can do. This should only happen when running tests. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 279 | 228 |
| 280 void CrasAudioHandler::AudioClientRestarted() { | 229 void CrasAudioHandler::AudioClientRestarted() { |
| 281 GetNodes(); | 230 GetNodes(); |
| 282 } | 231 } |
| 283 | 232 |
| 284 void CrasAudioHandler::OutputVolumeChanged(int volume) { | 233 void CrasAudioHandler::OutputVolumeChanged(int volume) { |
| 285 if (output_volume_ == volume) | 234 if (output_volume_ == volume) |
| 286 return; | 235 return; |
| 287 | 236 |
| 288 output_volume_ = volume; | 237 output_volume_ = volume; |
| 289 audio_pref_handler_->SetVolumeGainValue(active_output_node_id_, | 238 audio_pref_handler_->SetOutputVolumeValue(output_volume_); |
| 290 output_volume_); | |
| 291 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputVolumeChanged()); | 239 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputVolumeChanged()); |
| 292 } | 240 } |
| 293 | 241 |
| 294 void CrasAudioHandler::InputGainChanged(int gain) { | |
| 295 if (input_gain_ == gain) | |
| 296 return; | |
| 297 | |
| 298 input_gain_ = gain; | |
| 299 audio_pref_handler_->SetVolumeGainValue(active_input_node_id_, input_gain_); | |
| 300 FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputGainChanged()); | |
| 301 } | |
| 302 | |
| 303 void CrasAudioHandler::OutputMuteChanged(bool mute_on) { | 242 void CrasAudioHandler::OutputMuteChanged(bool mute_on) { |
| 304 if (output_mute_on_ == mute_on) | 243 if (output_mute_on_ == mute_on) |
| 305 return; | 244 return; |
| 306 | 245 |
| 307 output_mute_on_ = mute_on; | 246 output_mute_on_ = mute_on; |
| 308 audio_pref_handler_->SetMuteValue(active_output_node_id_, | 247 audio_pref_handler_->SetOutputMuteValue(mute_on); |
| 309 mute_on); | |
| 310 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputMuteChanged()); | 248 FOR_EACH_OBSERVER(AudioObserver, observers_, OnOutputMuteChanged()); |
| 311 } | 249 } |
| 312 | 250 |
| 313 void CrasAudioHandler::InputMuteChanged(bool mute_on) { | 251 void CrasAudioHandler::InputMuteChanged(bool mute_on) { |
| 314 if (input_mute_on_ == mute_on) | 252 if (input_mute_on_ == mute_on) |
| 315 return; | 253 return; |
| 316 | 254 |
| 317 input_mute_on_ = mute_on; | 255 input_mute_on_ = mute_on; |
| 318 audio_pref_handler_->SetMuteValue(active_input_node_id_, | |
| 319 mute_on); | |
| 320 FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputMuteChanged()); | 256 FOR_EACH_OBSERVER(AudioObserver, observers_, OnInputMuteChanged()); |
| 321 } | 257 } |
| 322 | 258 |
| 323 void CrasAudioHandler::NodesChanged() { | 259 void CrasAudioHandler::NodesChanged() { |
| 324 // Refresh audio nodes data. | 260 // Refresh audio nodes data. |
| 325 GetNodes(); | 261 GetNodes(); |
| 326 } | 262 } |
| 327 | 263 |
| 328 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) { | 264 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) { |
| 329 if (active_output_node_id_ == node_id) | 265 if (active_output_node_id_ == node_id) |
| 330 return; | 266 return; |
| 331 | 267 |
| 332 active_output_node_id_ = node_id; | 268 active_output_node_id_ = node_id; |
| 333 SetupAudioState(); | 269 SetupAudioState(); |
| 334 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged()); | 270 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged()); |
| 335 } | 271 } |
| 336 | 272 |
| 337 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) { | 273 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) { |
| 338 if (active_input_node_id_ == node_id) | 274 if (active_input_node_id_ == node_id) |
| 339 return; | 275 return; |
| 340 | 276 |
| 341 active_input_node_id_ = node_id; | 277 active_input_node_id_ = node_id; |
| 342 SetupAudioState(); | |
| 343 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveInputNodeChanged()); | 278 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveInputNodeChanged()); |
| 344 } | 279 } |
| 345 | 280 |
| 346 void CrasAudioHandler::OnAudioPolicyPrefChanged() { | 281 void CrasAudioHandler::OnAudioPolicyPrefChanged() { |
| 347 ApplyAudioPolicy(); | 282 ApplyAudioPolicy(); |
| 348 } | 283 } |
| 349 | 284 |
| 350 void CrasAudioHandler::SetupAudioState() { | 285 void CrasAudioHandler::SetupAudioState() { |
| 351 ApplyAudioPolicy(); | 286 ApplyAudioPolicy(); |
| 352 | 287 |
| 353 // Set the initial audio state to the ones read from audio prefs. | 288 // Set the initial audio state to the ones read from audio prefs. |
| 354 if (active_input_node_id_) { | 289 output_mute_on_ = audio_pref_handler_->GetOutputMuteValue(); |
| 355 input_mute_on_ = audio_pref_handler_->GetMuteValue(active_input_node_id_); | 290 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(); |
| 356 input_gain_ = audio_pref_handler_->GetVolumeGainValue( | 291 SetOutputVolumeInternal(output_volume_); |
| 357 active_input_node_id_); | 292 SetOutputMute(output_mute_on_); |
| 358 SetInputMute(input_mute_on_); | |
| 359 SetInputGainInternal(input_gain_); | |
| 360 } else { | |
| 361 SetInputMute(kPrefMuteOff); | |
| 362 SetInputGainInternal(kDefaultVolumeGainPercent); | |
| 363 } | |
| 364 | |
| 365 if (active_output_node_id_) { | |
| 366 output_mute_on_ = audio_pref_handler_->GetMuteValue(active_output_node_id_); | |
| 367 output_volume_ = audio_pref_handler_->GetVolumeGainValue( | |
| 368 active_output_node_id_); | |
| 369 SetOutputMute(output_mute_on_); | |
| 370 SetOutputVolumeInternal(output_volume_); | |
| 371 } else { | |
| 372 SetOutputMute(kPrefMuteOff); | |
| 373 SetOutputVolumeInternal(kDefaultVolumeGainPercent); | |
| 374 } | |
| 375 | |
| 376 if (!active_output_node_id_ || !active_input_node_id_) | |
| 377 CrasAudioSwitchHandler::Get()->UpdateActiveDevice(); | |
| 378 } | 293 } |
| 379 | 294 |
| 380 void CrasAudioHandler::ApplyAudioPolicy() { | 295 void CrasAudioHandler::ApplyAudioPolicy() { |
| 381 output_mute_locked_ = false; | 296 output_mute_locked_ = false; |
| 382 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) { | 297 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) { |
| 383 SetOutputMute(true); | 298 SetOutputMute(true); |
| 384 output_mute_locked_ = true; | 299 output_mute_locked_ = true; |
| 385 } | 300 } |
| 386 | 301 |
| 387 input_mute_locked_ = false; | 302 input_mute_locked_ = false; |
| 388 if (audio_pref_handler_->GetAudioCaptureAllowedValue()) { | 303 if (audio_pref_handler_->GetAudioCaptureAllowedValue()) { |
| 389 SetInputMute(false); | 304 SetInputMute(false); |
| 390 } else { | 305 } else { |
| 391 SetInputMute(true); | 306 SetInputMute(true); |
| 392 input_mute_locked_ = true; | 307 input_mute_locked_ = true; |
| 393 } | 308 } |
| 394 } | 309 } |
| 395 | 310 |
| 396 void CrasAudioHandler::SetOutputVolumeInternal(int volume) { | 311 void CrasAudioHandler::SetOutputVolumeInternal(int volume) { |
| 397 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 312 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 398 SetOutputVolume(volume); | 313 SetOutputVolume(volume); |
| 399 } | 314 } |
| 400 | 315 |
| 401 void CrasAudioHandler::SetInputGainInternal(int gain) { | |
| 402 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | |
| 403 SetInputGain(gain); | |
| 404 } | |
| 405 | |
| 406 void CrasAudioHandler::GetNodes() { | 316 void CrasAudioHandler::GetNodes() { |
| 407 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( | 317 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( |
| 408 base::Bind(&CrasAudioHandler::HandleGetNodes, | 318 base::Bind(&CrasAudioHandler::HandleGetNodes, |
| 409 weak_ptr_factory_.GetWeakPtr())); | 319 weak_ptr_factory_.GetWeakPtr())); |
| 410 } | 320 } |
| 411 | 321 |
| 412 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, | 322 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, |
| 413 bool success) { | 323 bool success) { |
| 414 if (!success) { | 324 if (!success) { |
| 415 LOG(ERROR) << "Failed to retrieve audio nodes data"; | 325 LOG(ERROR) << "Failed to retrieve audio nodes data"; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 438 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { | 348 device.type != AUDIO_TYPE_INTERNAL_SPEAKER) { |
| 439 has_alternative_output_ = true; | 349 has_alternative_output_ = true; |
| 440 } | 350 } |
| 441 } | 351 } |
| 442 | 352 |
| 443 SetupAudioState(); | 353 SetupAudioState(); |
| 444 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 354 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
| 445 } | 355 } |
| 446 | 356 |
| 447 } // namespace chromeos | 357 } // namespace chromeos |
| OLD | NEW |