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" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 weak_ptr_factory_(this), | 294 weak_ptr_factory_(this), |
295 output_mute_on_(false), | 295 output_mute_on_(false), |
296 input_mute_on_(false), | 296 input_mute_on_(false), |
297 output_volume_(0), | 297 output_volume_(0), |
298 input_gain_(0), | 298 input_gain_(0), |
299 active_output_node_id_(0), | 299 active_output_node_id_(0), |
300 active_input_node_id_(0), | 300 active_input_node_id_(0), |
301 has_alternative_input_(false), | 301 has_alternative_input_(false), |
302 has_alternative_output_(false), | 302 has_alternative_output_(false), |
303 output_mute_locked_(false), | 303 output_mute_locked_(false), |
304 input_mute_locked_(false) { | 304 input_mute_locked_(false), |
305 enable_log_(false) { | |
305 if (!audio_pref_handler.get()) | 306 if (!audio_pref_handler.get()) |
306 return; | 307 return; |
307 // If the DBusThreadManager or the CrasAudioClient aren't available, there | 308 // If the DBusThreadManager or the CrasAudioClient aren't available, there |
308 // isn't much we can do. This should only happen when running tests. | 309 // isn't much we can do. This should only happen when running tests. |
309 if (!chromeos::DBusThreadManager::IsInitialized() || | 310 if (!chromeos::DBusThreadManager::IsInitialized() || |
310 !chromeos::DBusThreadManager::Get() || | 311 !chromeos::DBusThreadManager::Get() || |
311 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | 312 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) |
312 return; | 313 return; |
313 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); | 314 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); |
314 audio_pref_handler_->AddAudioPrefObserver(this); | 315 audio_pref_handler_->AddAudioPrefObserver(this); |
316 if (chromeos::DBusThreadManager::Get()->GetSessionManagerClient()) { | |
317 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | |
318 AddObserver(this); | |
319 } | |
315 InitializeAudioState(); | 320 InitializeAudioState(); |
316 } | 321 } |
317 | 322 |
318 CrasAudioHandler::~CrasAudioHandler() { | 323 CrasAudioHandler::~CrasAudioHandler() { |
319 if (!chromeos::DBusThreadManager::IsInitialized() || | 324 if (!chromeos::DBusThreadManager::IsInitialized() || |
320 !chromeos::DBusThreadManager::Get() || | 325 !chromeos::DBusThreadManager::Get() || |
321 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | 326 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) |
322 return; | 327 return; |
323 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 328 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
324 RemoveObserver(this); | 329 RemoveObserver(this); |
330 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | |
331 RemoveObserver(this); | |
325 if (audio_pref_handler_.get()) | 332 if (audio_pref_handler_.get()) |
326 audio_pref_handler_->RemoveAudioPrefObserver(this); | 333 audio_pref_handler_->RemoveAudioPrefObserver(this); |
327 audio_pref_handler_ = NULL; | 334 audio_pref_handler_ = NULL; |
328 } | 335 } |
329 | 336 |
330 void CrasAudioHandler::AudioClientRestarted() { | 337 void CrasAudioHandler::AudioClientRestarted() { |
331 InitializeAudioState(); | 338 InitializeAudioState(); |
332 } | 339 } |
333 | 340 |
334 void CrasAudioHandler::NodesChanged() { | 341 void CrasAudioHandler::NodesChanged() { |
335 // Refresh audio nodes data. | 342 // Refresh audio nodes data. |
336 GetNodes(); | 343 GetNodes(); |
337 } | 344 } |
338 | 345 |
339 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) { | 346 void CrasAudioHandler::ActiveOutputNodeChanged(uint64 node_id) { |
340 if (active_output_node_id_ == node_id) | 347 if (active_output_node_id_ == node_id) |
341 return; | 348 return; |
342 | 349 |
343 // Active audio output device should always be changed by chrome. | 350 // Active audio output device should always be changed by chrome. |
344 LOG(WARNING) << "Active output node changed unexpectedly by system node_id=" | 351 // During system boot, cras may change active input to unknown device 0x1, |
345 << "0x" << std::hex << node_id; | 352 // we don't need to log it, since it is not an valid device. |
353 if (GetDeviceFromId(node_id)) { | |
354 LOG_IF(WARNING, enable_log_) | |
355 << "Active output node changed unexpectedly by system node_id=" | |
356 << "0x" << std::hex << node_id; | |
357 } | |
346 } | 358 } |
347 | 359 |
348 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) { | 360 void CrasAudioHandler::ActiveInputNodeChanged(uint64 node_id) { |
349 if (active_input_node_id_ == node_id) | 361 if (active_input_node_id_ == node_id) |
350 return; | 362 return; |
351 | 363 |
352 // Active audio input device should always be changed by chrome. | 364 // Active audio input device should always be changed by chrome. |
353 LOG(WARNING) << "Active input node changed unexpectedly by system node_id=" | 365 // During system boot, cras may change active input to unknown device 0x2, |
354 << "0x" << std::hex << node_id; | 366 // we don't need to log it, since it is not an valid device. |
367 if (GetDeviceFromId(node_id)) { | |
368 LOG_IF(WARNING, enable_log_) | |
369 << "Active input node changed unexpectedly by system node_id=" | |
370 << "0x" << std::hex << node_id; | |
371 } | |
355 } | 372 } |
356 | 373 |
357 void CrasAudioHandler::OnAudioPolicyPrefChanged() { | 374 void CrasAudioHandler::OnAudioPolicyPrefChanged() { |
358 ApplyAudioPolicy(); | 375 ApplyAudioPolicy(); |
359 } | 376 } |
360 | 377 |
378 void CrasAudioHandler::EmitLoginPromptVisibleCalled() { | |
379 enable_log_ = true; | |
Daniel Erat
2013/09/12 21:10:05
what happens if chrome crashes and is restarted? i
jennyz
2013/09/12 22:34:45
Good pointed, enable CrasAudioHandler logging when
| |
380 } | |
381 | |
361 const AudioDevice* CrasAudioHandler::GetDeviceFromId(uint64 device_id) const { | 382 const AudioDevice* CrasAudioHandler::GetDeviceFromId(uint64 device_id) const { |
362 AudioDeviceMap::const_iterator it = audio_devices_.find(device_id); | 383 AudioDeviceMap::const_iterator it = audio_devices_.find(device_id); |
363 if (it == audio_devices_.end()) | 384 if (it == audio_devices_.end()) |
364 return NULL; | 385 return NULL; |
365 | 386 |
366 return &(it->second); | 387 return &(it->second); |
367 } | 388 } |
368 | 389 |
369 void CrasAudioHandler::SetupAudioInputState() { | 390 void CrasAudioHandler::SetupAudioInputState() { |
370 // Set the initial audio state to the ones read from audio prefs. | 391 // Set the initial audio state to the ones read from audio prefs. |
371 const AudioDevice* device = GetDeviceFromId(active_input_node_id_); | 392 const AudioDevice* device = GetDeviceFromId(active_input_node_id_); |
372 if (!device) { | 393 if (!device) { |
373 LOG(ERROR) << "Can't set up audio state for unknow input device id =" | 394 LOG_IF(ERROR, enable_log_) |
395 << "Can't set up audio state for unknown input device id =" | |
374 << "0x" << std::hex << active_input_node_id_; | 396 << "0x" << std::hex << active_input_node_id_; |
375 return; | 397 return; |
376 } | 398 } |
377 input_mute_on_ = audio_pref_handler_->GetMuteValue(*device); | 399 input_mute_on_ = audio_pref_handler_->GetMuteValue(*device); |
378 input_gain_ = audio_pref_handler_->GetInputGainValue(device); | 400 input_gain_ = audio_pref_handler_->GetInputGainValue(device); |
379 SetInputMuteInternal(input_mute_on_); | 401 SetInputMuteInternal(input_mute_on_); |
380 // TODO(rkc,jennyz): Set input gain once we decide on how to store | 402 // TODO(rkc,jennyz): Set input gain once we decide on how to store |
381 // the gain values since the range and step are both device specific. | 403 // the gain values since the range and step are both device specific. |
382 } | 404 } |
383 | 405 |
384 void CrasAudioHandler::SetupAudioOutputState() { | 406 void CrasAudioHandler::SetupAudioOutputState() { |
385 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); | 407 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); |
386 if (!device) { | 408 if (!device) { |
387 LOG(ERROR) << "Can't set up audio state for unknow output device id =" | 409 LOG_IF(ERROR, enable_log_) |
388 << "0x" << std::hex << active_output_node_id_; | 410 << "Can't set up audio state for unknown output device id =" |
411 << "0x" << std::hex << active_output_node_id_; | |
389 return; | 412 return; |
390 } | 413 } |
391 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); | 414 output_mute_on_ = audio_pref_handler_->GetMuteValue(*device); |
392 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); | 415 output_volume_ = audio_pref_handler_->GetOutputVolumeValue(device); |
393 | 416 |
394 SetOutputMuteInternal(output_mute_on_); | 417 SetOutputMuteInternal(output_mute_on_); |
395 SetOutputNodeVolume(active_output_node_id_, output_volume_); | 418 SetOutputNodeVolume(active_output_node_id_, output_volume_); |
396 } | 419 } |
397 | 420 |
398 void CrasAudioHandler::InitializeAudioState() { | 421 void CrasAudioHandler::InitializeAudioState() { |
399 ApplyAudioPolicy(); | 422 ApplyAudioPolicy(); |
400 GetNodes(); | 423 GetNodes(); |
401 } | 424 } |
402 | 425 |
403 void CrasAudioHandler::ApplyAudioPolicy() { | 426 void CrasAudioHandler::ApplyAudioPolicy() { |
404 output_mute_locked_ = false; | 427 output_mute_locked_ = false; |
405 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) { | 428 if (!audio_pref_handler_->GetAudioOutputAllowedValue()) { |
406 // Mute the device, but do not update the preference. | 429 // Mute the device, but do not update the preference. |
407 SetOutputMuteInternal(true); | 430 SetOutputMuteInternal(true); |
408 output_mute_locked_ = true; | 431 output_mute_locked_ = true; |
409 } else { | 432 } else { |
410 // Restore the mute state. | 433 // Restore the mute state. |
411 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); | 434 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); |
412 if (device) | 435 if (device) |
413 SetOutputMuteInternal(audio_pref_handler_->GetMuteValue(*device)); | 436 SetOutputMuteInternal(audio_pref_handler_->GetMuteValue(*device)); |
414 } | 437 } |
415 | 438 |
416 input_mute_locked_ = false; | 439 input_mute_locked_ = false; |
417 if (audio_pref_handler_->GetAudioCaptureAllowedValue()) { | 440 if (audio_pref_handler_->GetAudioCaptureAllowedValue()) { |
418 SetInputMute(false); | 441 // Set input mute if we have discovered active input device. |
442 const AudioDevice* device = GetDeviceFromId(active_input_node_id_); | |
443 if (device) | |
444 SetInputMuteInternal(false); | |
419 } else { | 445 } else { |
420 SetInputMute(true); | 446 SetInputMute(true); |
421 input_mute_locked_ = true; | 447 input_mute_locked_ = true; |
422 } | 448 } |
423 } | 449 } |
424 | 450 |
425 void CrasAudioHandler::SetOutputNodeVolume(uint64 node_id, int volume) { | 451 void CrasAudioHandler::SetOutputNodeVolume(uint64 node_id, int volume) { |
426 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 452 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
427 SetOutputNodeVolume(node_id, volume); | 453 SetOutputNodeVolume(node_id, volume); |
428 } | 454 } |
(...skipping 19 matching lines...) Expand all Loading... | |
448 | 474 |
449 input_mute_on_ = mute_on; | 475 input_mute_on_ = mute_on; |
450 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 476 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
451 SetInputMute(mute_on); | 477 SetInputMute(mute_on); |
452 return true; | 478 return true; |
453 } | 479 } |
454 | 480 |
455 void CrasAudioHandler::GetNodes() { | 481 void CrasAudioHandler::GetNodes() { |
456 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( | 482 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( |
457 base::Bind(&CrasAudioHandler::HandleGetNodes, | 483 base::Bind(&CrasAudioHandler::HandleGetNodes, |
484 weak_ptr_factory_.GetWeakPtr()), | |
485 base::Bind(&CrasAudioHandler::HandleGetNodesError, | |
458 weak_ptr_factory_.GetWeakPtr())); | 486 weak_ptr_factory_.GetWeakPtr())); |
459 } | 487 } |
460 | 488 |
461 bool CrasAudioHandler::ChangeActiveDevice(const AudioDevice& new_active_device, | 489 bool CrasAudioHandler::ChangeActiveDevice(const AudioDevice& new_active_device, |
462 uint64* current_active_node_id) { | 490 uint64* current_active_node_id) { |
463 // If the device we want to switch to is already the current active device, | 491 // If the device we want to switch to is already the current active device, |
464 // do nothing. | 492 // do nothing. |
465 if (new_active_device.active && | 493 if (new_active_device.active && |
466 new_active_device.id == *current_active_node_id) { | 494 new_active_device.id == *current_active_node_id) { |
467 return false; | 495 return false; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
579 audio_devices_.size(), | 607 audio_devices_.size(), |
580 active_output_node_id_) && | 608 active_output_node_id_) && |
581 !output_devices_pq_.empty()) { | 609 !output_devices_pq_.empty()) { |
582 SwitchToDevice(output_devices_pq_.top()); | 610 SwitchToDevice(output_devices_pq_.top()); |
583 } | 611 } |
584 } | 612 } |
585 | 613 |
586 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, | 614 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, |
587 bool success) { | 615 bool success) { |
588 if (!success) { | 616 if (!success) { |
589 LOG(ERROR) << "Failed to retrieve audio nodes data"; | 617 LOG_IF(ERROR, enable_log_) << "Failed to retrieve audio nodes data"; |
590 return; | 618 return; |
591 } | 619 } |
592 | 620 |
593 UpdateDevicesAndSwitchActive(node_list); | 621 UpdateDevicesAndSwitchActive(node_list); |
594 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 622 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
595 } | 623 } |
596 | 624 |
625 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, | |
626 const std::string& error_msg) { | |
627 LOG_IF(ERROR, enable_log_) << "Failed to call GetNodes: " | |
628 << error_name << ": " << error_msg; | |
629 } | |
597 } // namespace chromeos | 630 } // namespace chromeos |
OLD | NEW |