| 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> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 const int kMuteThresholdPercent = 1; | 34 const int kMuteThresholdPercent = 1; |
| 35 | 35 |
| 36 // The duration of HDMI output re-discover grace period in milliseconds. | 36 // The duration of HDMI output re-discover grace period in milliseconds. |
| 37 const int kHDMIRediscoverGracePeriodDurationInMs = 2000; | 37 const int kHDMIRediscoverGracePeriodDurationInMs = 2000; |
| 38 | 38 |
| 39 // Mixer matrix, [0.5, 0.5; 0.5, 0.5] | 39 // Mixer matrix, [0.5, 0.5; 0.5, 0.5] |
| 40 const double kStereoToMono[] = {0.5, 0.5, 0.5, 0.5}; | 40 const double kStereoToMono[] = {0.5, 0.5, 0.5, 0.5}; |
| 41 // Mixer matrix, [1, 0; 0, 1] | 41 // Mixer matrix, [1, 0; 0, 1] |
| 42 const double kStereoToStereo[] = {1, 0, 0, 1}; | 42 const double kStereoToStereo[] = {1, 0, 0, 1}; |
| 43 | 43 |
| 44 static CrasAudioHandler* g_cras_audio_handler = nullptr; | 44 CrasAudioHandler* g_cras_audio_handler = nullptr; |
| 45 | 45 |
| 46 bool IsSameAudioDevice(const AudioDevice& a, const AudioDevice& b) { | 46 bool IsSameAudioDevice(const AudioDevice& a, const AudioDevice& b) { |
| 47 return a.stable_device_id == b.stable_device_id && a.is_input == b.is_input && | 47 return a.stable_device_id == b.stable_device_id && a.is_input == b.is_input && |
| 48 a.type == b.type && a.device_name == b.device_name; | 48 a.type == b.type && a.device_name == b.device_name; |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool IsDeviceInList(const AudioDevice& device, const AudioNodeList& node_list) { | 51 bool IsDeviceInList(const AudioDevice& device, const AudioNodeList& node_list) { |
| 52 for (const AudioNode& node : node_list) { | 52 for (const AudioNode& node : node_list) { |
| 53 if (device.stable_device_id == node.StableDeviceId()) | 53 if (device.stable_device_id == node.StableDeviceId()) |
| 54 return true; | 54 return true; |
| 55 } | 55 } |
| 56 return false; | 56 return false; |
| 57 } | 57 } |
| 58 | 58 |
| 59 CrasAudioClient* GetCrasAudioClient() { |
| 60 return DBusThreadManager::Get()->GetCrasAudioClient(); |
| 61 } |
| 62 |
| 63 bool HasCrasAudioClient() { |
| 64 return DBusThreadManager::IsInitialized() && DBusThreadManager::Get() && |
| 65 DBusThreadManager::Get()->GetCrasAudioClient(); |
| 66 } |
| 67 |
| 59 } // namespace | 68 } // namespace |
| 60 | 69 |
| 61 CrasAudioHandler::AudioObserver::AudioObserver() { | 70 CrasAudioHandler::AudioObserver::AudioObserver() { |
| 62 } | 71 } |
| 63 | 72 |
| 64 CrasAudioHandler::AudioObserver::~AudioObserver() { | 73 CrasAudioHandler::AudioObserver::~AudioObserver() { |
| 65 } | 74 } |
| 66 | 75 |
| 67 void CrasAudioHandler::AudioObserver::OnOutputNodeVolumeChanged( | 76 void CrasAudioHandler::AudioObserver::OnOutputNodeVolumeChanged( |
| 68 uint64_t /* node_id */, | 77 uint64_t /* node_id */, |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 | 258 |
| 250 int CrasAudioHandler::GetOutputDefaultVolumeMuteThreshold() { | 259 int CrasAudioHandler::GetOutputDefaultVolumeMuteThreshold() { |
| 251 return kMuteThresholdPercent; | 260 return kMuteThresholdPercent; |
| 252 } | 261 } |
| 253 | 262 |
| 254 int CrasAudioHandler::GetOutputVolumePercent() { | 263 int CrasAudioHandler::GetOutputVolumePercent() { |
| 255 return output_volume_; | 264 return output_volume_; |
| 256 } | 265 } |
| 257 | 266 |
| 258 int CrasAudioHandler::GetOutputVolumePercentForDevice(uint64_t device_id) { | 267 int CrasAudioHandler::GetOutputVolumePercentForDevice(uint64_t device_id) { |
| 259 if (device_id == active_output_node_id_) { | 268 if (device_id == active_output_node_id_) |
| 260 return output_volume_; | 269 return output_volume_; |
| 261 } else { | 270 const AudioDevice* device = GetDeviceFromId(device_id); |
| 262 const AudioDevice* device = GetDeviceFromId(device_id); | 271 return static_cast<int>(audio_pref_handler_->GetOutputVolumeValue(device)); |
| 263 return static_cast<int>(audio_pref_handler_->GetOutputVolumeValue(device)); | |
| 264 } | |
| 265 } | 272 } |
| 266 | 273 |
| 267 int CrasAudioHandler::GetInputGainPercent() { | 274 int CrasAudioHandler::GetInputGainPercent() { |
| 268 return input_gain_; | 275 return input_gain_; |
| 269 } | 276 } |
| 270 | 277 |
| 271 int CrasAudioHandler::GetInputGainPercentForDevice(uint64_t device_id) { | 278 int CrasAudioHandler::GetInputGainPercentForDevice(uint64_t device_id) { |
| 272 if (device_id == active_input_node_id_) { | 279 if (device_id == active_input_node_id_) |
| 273 return input_gain_; | 280 return input_gain_; |
| 274 } else { | 281 const AudioDevice* device = GetDeviceFromId(device_id); |
| 275 const AudioDevice* device = GetDeviceFromId(device_id); | 282 return static_cast<int>(audio_pref_handler_->GetInputGainValue(device)); |
| 276 return static_cast<int>(audio_pref_handler_->GetInputGainValue(device)); | |
| 277 } | |
| 278 } | 283 } |
| 279 | 284 |
| 280 uint64_t CrasAudioHandler::GetPrimaryActiveOutputNode() const { | 285 uint64_t CrasAudioHandler::GetPrimaryActiveOutputNode() const { |
| 281 return active_output_node_id_; | 286 return active_output_node_id_; |
| 282 } | 287 } |
| 283 | 288 |
| 284 uint64_t CrasAudioHandler::GetPrimaryActiveInputNode() const { | 289 uint64_t CrasAudioHandler::GetPrimaryActiveInputNode() const { |
| 285 return active_input_node_id_; | 290 return active_input_node_id_; |
| 286 } | 291 } |
| 287 | 292 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 if ((device->is_input && !active_input_node_id_) || | 331 if ((device->is_input && !active_input_node_id_) || |
| 327 (!device->is_input && !active_output_node_id_)) { | 332 (!device->is_input && !active_output_node_id_)) { |
| 328 SwitchToDevice(*device, notify, ACTIVATE_BY_USER); | 333 SwitchToDevice(*device, notify, ACTIVATE_BY_USER); |
| 329 return; | 334 return; |
| 330 } | 335 } |
| 331 | 336 |
| 332 AddAdditionalActiveNode(node_id, notify); | 337 AddAdditionalActiveNode(node_id, notify); |
| 333 } | 338 } |
| 334 | 339 |
| 335 void CrasAudioHandler::ChangeActiveNodes(const NodeIdList& new_active_ids) { | 340 void CrasAudioHandler::ChangeActiveNodes(const NodeIdList& new_active_ids) { |
| 336 chromeos::AudioDeviceList input_devices; | 341 AudioDeviceList input_devices; |
| 337 chromeos::AudioDeviceList output_devices; | 342 AudioDeviceList output_devices; |
| 338 | 343 |
| 339 for (uint64_t id : new_active_ids) { | 344 for (uint64_t id : new_active_ids) { |
| 340 const chromeos::AudioDevice* device = GetDeviceFromId(id); | 345 const AudioDevice* device = GetDeviceFromId(id); |
| 341 if (!device) | 346 if (!device) |
| 342 continue; | 347 continue; |
| 343 if (device->is_input) | 348 if (device->is_input) |
| 344 input_devices.push_back(*device); | 349 input_devices.push_back(*device); |
| 345 else | 350 else |
| 346 output_devices.push_back(*device); | 351 output_devices.push_back(*device); |
| 347 } | 352 } |
| 348 if (!input_devices.empty()) | 353 if (!input_devices.empty()) |
| 349 SetActiveDevices(input_devices, true /* is_input */); | 354 SetActiveDevices(input_devices, true /* is_input */); |
| 350 if (!output_devices.empty()) | 355 if (!output_devices.empty()) |
| 351 SetActiveDevices(output_devices, false /* is_input */); | 356 SetActiveDevices(output_devices, false /* is_input */); |
| 352 } | 357 } |
| 353 | 358 |
| 354 bool CrasAudioHandler::SetActiveInputNodes(const NodeIdList& node_ids) { | 359 bool CrasAudioHandler::SetActiveInputNodes(const NodeIdList& node_ids) { |
| 355 return SetActiveNodes(node_ids, true /* is_input */); | 360 return SetActiveNodes(node_ids, true /* is_input */); |
| 356 } | 361 } |
| 357 | 362 |
| 358 bool CrasAudioHandler::SetActiveOutputNodes(const NodeIdList& node_ids) { | 363 bool CrasAudioHandler::SetActiveOutputNodes(const NodeIdList& node_ids) { |
| 359 return SetActiveNodes(node_ids, false /* is_input */); | 364 return SetActiveNodes(node_ids, false /* is_input */); |
| 360 } | 365 } |
| 361 | 366 |
| 362 bool CrasAudioHandler::SetActiveNodes(const NodeIdList& node_ids, | 367 bool CrasAudioHandler::SetActiveNodes(const NodeIdList& node_ids, |
| 363 bool is_input) { | 368 bool is_input) { |
| 364 chromeos::AudioDeviceList devices; | 369 AudioDeviceList devices; |
| 365 for (uint64_t id : node_ids) { | 370 for (uint64_t id : node_ids) { |
| 366 const chromeos::AudioDevice* device = GetDeviceFromId(id); | 371 const AudioDevice* device = GetDeviceFromId(id); |
| 367 if (!device || device->is_input != is_input) | 372 if (!device || device->is_input != is_input) |
| 368 return false; | 373 return false; |
| 369 | 374 |
| 370 devices.push_back(*device); | 375 devices.push_back(*device); |
| 371 } | 376 } |
| 372 | 377 |
| 373 SetActiveDevices(devices, is_input); | 378 SetActiveDevices(devices, is_input); |
| 374 return true; | 379 return true; |
| 375 } | 380 } |
| 376 | 381 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 } | 418 } |
| 414 | 419 |
| 415 if (active_devices_changed) | 420 if (active_devices_changed) |
| 416 NotifyActiveNodeChanged(is_input); | 421 NotifyActiveNodeChanged(is_input); |
| 417 } | 422 } |
| 418 | 423 |
| 419 void CrasAudioHandler::SwapInternalSpeakerLeftRightChannel(bool swap) { | 424 void CrasAudioHandler::SwapInternalSpeakerLeftRightChannel(bool swap) { |
| 420 for (const auto& item : audio_devices_) { | 425 for (const auto& item : audio_devices_) { |
| 421 const AudioDevice& device = item.second; | 426 const AudioDevice& device = item.second; |
| 422 if (!device.is_input && device.type == AUDIO_TYPE_INTERNAL_SPEAKER) { | 427 if (!device.is_input && device.type == AUDIO_TYPE_INTERNAL_SPEAKER) { |
| 423 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->SwapLeftRight( | 428 GetCrasAudioClient()->SwapLeftRight(device.id, swap); |
| 424 device.id, swap); | |
| 425 break; | 429 break; |
| 426 } | 430 } |
| 427 } | 431 } |
| 428 } | 432 } |
| 429 | 433 |
| 430 void CrasAudioHandler::SetOutputMono(bool mono_on) { | 434 void CrasAudioHandler::SetOutputMono(bool mono_on) { |
| 431 output_mono_on_ = mono_on; | 435 output_mono_on_ = mono_on; |
| 432 if (mono_on) { | 436 if (mono_on) { |
| 433 chromeos::DBusThreadManager::Get() | 437 GetCrasAudioClient()->SetGlobalOutputChannelRemix( |
| 434 ->GetCrasAudioClient() | 438 output_channels_, |
| 435 ->SetGlobalOutputChannelRemix( | 439 std::vector<double>(kStereoToMono, std::end(kStereoToMono))); |
| 436 output_channels_, | |
| 437 std::vector<double>(kStereoToMono, std::end(kStereoToMono))); | |
| 438 } else { | 440 } else { |
| 439 chromeos::DBusThreadManager::Get() | 441 GetCrasAudioClient()->SetGlobalOutputChannelRemix( |
| 440 ->GetCrasAudioClient() | 442 output_channels_, |
| 441 ->SetGlobalOutputChannelRemix( | 443 std::vector<double>(kStereoToStereo, std::end(kStereoToStereo))); |
| 442 output_channels_, | |
| 443 std::vector<double>(kStereoToStereo, std::end(kStereoToStereo))); | |
| 444 } | 444 } |
| 445 | 445 |
| 446 for (auto& observer : observers_) | 446 for (auto& observer : observers_) |
| 447 observer.OnOuputChannelRemixingChanged(mono_on); | 447 observer.OnOuputChannelRemixingChanged(mono_on); |
| 448 } | 448 } |
| 449 | 449 |
| 450 bool CrasAudioHandler::IsOutputMonoEnabled() const { | 450 bool CrasAudioHandler::IsOutputMonoEnabled() const { |
| 451 return output_mono_on_; | 451 return output_mono_on_; |
| 452 } | 452 } |
| 453 | 453 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 | 515 |
| 516 void CrasAudioHandler::SetInputMute(bool mute_on) { | 516 void CrasAudioHandler::SetInputMute(bool mute_on) { |
| 517 SetInputMuteInternal(mute_on); | 517 SetInputMuteInternal(mute_on); |
| 518 for (auto& observer : observers_) | 518 for (auto& observer : observers_) |
| 519 observer.OnInputMuteChanged(input_mute_on_); | 519 observer.OnInputMuteChanged(input_mute_on_); |
| 520 } | 520 } |
| 521 | 521 |
| 522 void CrasAudioHandler::SetActiveDevice(const AudioDevice& active_device, | 522 void CrasAudioHandler::SetActiveDevice(const AudioDevice& active_device, |
| 523 bool notify, | 523 bool notify, |
| 524 DeviceActivateType activate_by) { | 524 DeviceActivateType activate_by) { |
| 525 if (active_device.is_input) { | 525 if (active_device.is_input) |
| 526 chromeos::DBusThreadManager::Get() | 526 GetCrasAudioClient()->SetActiveInputNode(active_device.id); |
| 527 ->GetCrasAudioClient() | 527 else |
| 528 ->SetActiveInputNode(active_device.id); | 528 GetCrasAudioClient()->SetActiveOutputNode(active_device.id); |
| 529 } else { | |
| 530 chromeos::DBusThreadManager::Get() | |
| 531 ->GetCrasAudioClient() | |
| 532 ->SetActiveOutputNode(active_device.id); | |
| 533 } | |
| 534 | 529 |
| 535 if (notify) | 530 if (notify) |
| 536 NotifyActiveNodeChanged(active_device.is_input); | 531 NotifyActiveNodeChanged(active_device.is_input); |
| 537 | 532 |
| 538 // Save active state for the nodes. | 533 // Save active state for the nodes. |
| 539 for (const auto& item : audio_devices_) { | 534 for (const auto& item : audio_devices_) { |
| 540 const AudioDevice& device = item.second; | 535 const AudioDevice& device = item.second; |
| 541 if (device.is_input != active_device.is_input) | 536 if (device.is_input != active_device.is_input) |
| 542 continue; | 537 continue; |
| 543 SaveDeviceState(device, device.active, activate_by); | 538 SaveDeviceState(device, device.active, activate_by); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 579 if (device->is_input) | 574 if (device->is_input) |
| 580 SetInputNodeGainPercent(device_id, value); | 575 SetInputNodeGainPercent(device_id, value); |
| 581 else | 576 else |
| 582 SetOutputNodeVolumePercent(device_id, value); | 577 SetOutputNodeVolumePercent(device_id, value); |
| 583 } | 578 } |
| 584 | 579 |
| 585 void CrasAudioHandler::SetMuteForDevice(uint64_t device_id, bool mute_on) { | 580 void CrasAudioHandler::SetMuteForDevice(uint64_t device_id, bool mute_on) { |
| 586 if (device_id == active_output_node_id_) { | 581 if (device_id == active_output_node_id_) { |
| 587 SetOutputMute(mute_on); | 582 SetOutputMute(mute_on); |
| 588 return; | 583 return; |
| 589 } else if (device_id == active_input_node_id_) { | 584 } |
| 585 if (device_id == active_input_node_id_) { |
| 590 VLOG(1) << "SetMuteForDevice sets active input device id=" | 586 VLOG(1) << "SetMuteForDevice sets active input device id=" |
| 591 << "0x" << std::hex << device_id << " mute=" << mute_on; | 587 << "0x" << std::hex << device_id << " mute=" << mute_on; |
| 592 SetInputMute(mute_on); | 588 SetInputMute(mute_on); |
| 593 return; | 589 return; |
| 594 } | 590 } |
| 595 | 591 |
| 596 const AudioDevice* device = GetDeviceFromId(device_id); | 592 const AudioDevice* device = GetDeviceFromId(device_id); |
| 597 // Input device's mute state is not recorded in the pref. crbug.com/365050. | 593 // Input device's mute state is not recorded in the pref. crbug.com/365050. |
| 598 if (device && !device->is_input) | 594 if (device && !device->is_input) |
| 599 audio_pref_handler_->SetMuteValue(*device, mute_on); | 595 audio_pref_handler_->SetMuteValue(*device, mute_on); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 output_mono_on_(false), | 635 output_mono_on_(false), |
| 640 log_errors_(false), | 636 log_errors_(false), |
| 641 hdmi_rediscover_grace_period_duration_in_ms_( | 637 hdmi_rediscover_grace_period_duration_in_ms_( |
| 642 kHDMIRediscoverGracePeriodDurationInMs), | 638 kHDMIRediscoverGracePeriodDurationInMs), |
| 643 hdmi_rediscovering_(false), | 639 hdmi_rediscovering_(false), |
| 644 weak_ptr_factory_(this) { | 640 weak_ptr_factory_(this) { |
| 645 if (!audio_pref_handler.get()) | 641 if (!audio_pref_handler.get()) |
| 646 return; | 642 return; |
| 647 // If the DBusThreadManager or the CrasAudioClient aren't available, there | 643 // If the DBusThreadManager or the CrasAudioClient aren't available, there |
| 648 // isn't much we can do. This should only happen when running tests. | 644 // isn't much we can do. This should only happen when running tests. |
| 649 if (!chromeos::DBusThreadManager::IsInitialized() || | 645 if (!HasCrasAudioClient()) |
| 650 !chromeos::DBusThreadManager::Get() || | |
| 651 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | |
| 652 return; | 646 return; |
| 653 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->AddObserver(this); | 647 GetCrasAudioClient()->AddObserver(this); |
| 654 audio_pref_handler_->AddAudioPrefObserver(this); | 648 audio_pref_handler_->AddAudioPrefObserver(this); |
| 655 if (chromeos::DBusThreadManager::Get()->GetSessionManagerClient()) { | 649 if (DBusThreadManager::Get()->GetSessionManagerClient()) |
| 656 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | 650 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); |
| 657 AddObserver(this); | |
| 658 } | |
| 659 InitializeAudioState(); | 651 InitializeAudioState(); |
| 660 } | 652 } |
| 661 | 653 |
| 662 CrasAudioHandler::~CrasAudioHandler() { | 654 CrasAudioHandler::~CrasAudioHandler() { |
| 663 hdmi_rediscover_timer_.Stop(); | 655 hdmi_rediscover_timer_.Stop(); |
| 664 if (!chromeos::DBusThreadManager::IsInitialized() || | 656 if (!HasCrasAudioClient()) |
| 665 !chromeos::DBusThreadManager::Get() || | |
| 666 !chromeos::DBusThreadManager::Get()->GetCrasAudioClient()) | |
| 667 return; | 657 return; |
| 668 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 658 GetCrasAudioClient()->RemoveObserver(this); |
| 669 RemoveObserver(this); | 659 if (DBusThreadManager::Get()->GetSessionManagerClient()) |
| 670 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()-> | 660 DBusThreadManager::Get()->GetSessionManagerClient()->RemoveObserver(this); |
| 671 RemoveObserver(this); | |
| 672 if (audio_pref_handler_.get()) | 661 if (audio_pref_handler_.get()) |
| 673 audio_pref_handler_->RemoveAudioPrefObserver(this); | 662 audio_pref_handler_->RemoveAudioPrefObserver(this); |
| 674 audio_pref_handler_ = nullptr; | 663 audio_pref_handler_ = nullptr; |
| 675 } | 664 } |
| 676 | 665 |
| 677 void CrasAudioHandler::AudioClientRestarted() { | 666 void CrasAudioHandler::AudioClientRestarted() { |
| 678 // Make sure the logging is enabled in case cras server | 667 // Make sure the logging is enabled in case cras server |
| 679 // restarts after crashing. | 668 // restarts after crashing. |
| 680 LogErrors(); | 669 LogErrors(); |
| 681 InitializeAudioState(); | 670 InitializeAudioState(); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 773 void CrasAudioHandler::EmitLoginPromptVisibleCalled() { | 762 void CrasAudioHandler::EmitLoginPromptVisibleCalled() { |
| 774 // Enable logging after cras server is started, which will be after | 763 // Enable logging after cras server is started, which will be after |
| 775 // EmitLoginPromptVisible. | 764 // EmitLoginPromptVisible. |
| 776 LogErrors(); | 765 LogErrors(); |
| 777 } | 766 } |
| 778 | 767 |
| 779 const AudioDevice* CrasAudioHandler::GetDeviceFromId(uint64_t device_id) const { | 768 const AudioDevice* CrasAudioHandler::GetDeviceFromId(uint64_t device_id) const { |
| 780 AudioDeviceMap::const_iterator it = audio_devices_.find(device_id); | 769 AudioDeviceMap::const_iterator it = audio_devices_.find(device_id); |
| 781 if (it == audio_devices_.end()) | 770 if (it == audio_devices_.end()) |
| 782 return nullptr; | 771 return nullptr; |
| 783 | 772 return &it->second; |
| 784 return &(it->second); | |
| 785 } | 773 } |
| 786 | 774 |
| 787 const AudioDevice* CrasAudioHandler::GetDeviceFromStableDeviceId( | 775 const AudioDevice* CrasAudioHandler::GetDeviceFromStableDeviceId( |
| 788 uint64_t stable_device_id) const { | 776 uint64_t stable_device_id) const { |
| 789 for (const auto& item : audio_devices_) { | 777 for (const auto& item : audio_devices_) { |
| 790 const AudioDevice& device = item.second; | 778 const AudioDevice& device = item.second; |
| 791 if (device.stable_device_id == stable_device_id) | 779 if (device.stable_device_id == stable_device_id) |
| 792 return &device; | 780 return &device; |
| 793 } | 781 } |
| 794 return nullptr; | 782 return nullptr; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); | 864 SetOutputNodeVolumePercent(node_id, GetOutputVolumePercent()); |
| 877 } | 865 } |
| 878 } | 866 } |
| 879 | 867 |
| 880 void CrasAudioHandler::InitializeAudioState() { | 868 void CrasAudioHandler::InitializeAudioState() { |
| 881 initializing_audio_state_ = true; | 869 initializing_audio_state_ = true; |
| 882 ApplyAudioPolicy(); | 870 ApplyAudioPolicy(); |
| 883 | 871 |
| 884 // Defer querying cras for GetNodes until cras service becomes available. | 872 // Defer querying cras for GetNodes until cras service becomes available. |
| 885 cras_service_available_ = false; | 873 cras_service_available_ = false; |
| 886 chromeos::DBusThreadManager::Get() | 874 GetCrasAudioClient()->WaitForServiceToBeAvailable( |
| 887 ->GetCrasAudioClient() | 875 base::Bind(&CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, |
| 888 ->WaitForServiceToBeAvailable(base::Bind( | 876 weak_ptr_factory_.GetWeakPtr())); |
| 889 &CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable, | |
| 890 weak_ptr_factory_.GetWeakPtr())); | |
| 891 } | 877 } |
| 892 | 878 |
| 893 void CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable( | 879 void CrasAudioHandler::InitializeAudioAfterCrasServiceAvailable( |
| 894 bool service_is_available) { | 880 bool service_is_available) { |
| 895 if (!service_is_available) { | 881 if (!service_is_available) { |
| 896 LOG(ERROR) << "Cras service is not available"; | 882 LOG(ERROR) << "Cras service is not available"; |
| 897 cras_service_available_ = false; | 883 cras_service_available_ = false; |
| 898 return; | 884 return; |
| 899 } | 885 } |
| 900 | 886 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 913 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); | 899 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); |
| 914 if (device) | 900 if (device) |
| 915 SetOutputMuteInternal(audio_pref_handler_->GetMuteValue(*device)); | 901 SetOutputMuteInternal(audio_pref_handler_->GetMuteValue(*device)); |
| 916 } | 902 } |
| 917 | 903 |
| 918 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome | 904 // Policy for audio input is handled by kAudioCaptureAllowed in the Chrome |
| 919 // media system. | 905 // media system. |
| 920 } | 906 } |
| 921 | 907 |
| 922 void CrasAudioHandler::SetOutputNodeVolume(uint64_t node_id, int volume) { | 908 void CrasAudioHandler::SetOutputNodeVolume(uint64_t node_id, int volume) { |
| 923 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 909 GetCrasAudioClient()->SetOutputNodeVolume(node_id, volume); |
| 924 SetOutputNodeVolume(node_id, volume); | |
| 925 } | 910 } |
| 926 | 911 |
| 927 void CrasAudioHandler::SetOutputNodeVolumePercent(uint64_t node_id, | 912 void CrasAudioHandler::SetOutputNodeVolumePercent(uint64_t node_id, |
| 928 int volume_percent) { | 913 int volume_percent) { |
| 929 const AudioDevice* device = GetDeviceFromId(node_id); | 914 const AudioDevice* device = GetDeviceFromId(node_id); |
| 930 if (!device || device->is_input) | 915 if (!device || device->is_input) |
| 931 return; | 916 return; |
| 932 | 917 |
| 933 volume_percent = min(max(volume_percent, 0), 100); | 918 volume_percent = min(max(volume_percent, 0), 100); |
| 934 if (volume_percent <= kMuteThresholdPercent) | 919 if (volume_percent <= kMuteThresholdPercent) |
| 935 volume_percent = 0; | 920 volume_percent = 0; |
| 936 | 921 |
| 937 // Save the volume setting in pref in case this is called on non-active | 922 // Save the volume setting in pref in case this is called on non-active |
| 938 // node for configuration. | 923 // node for configuration. |
| 939 audio_pref_handler_->SetVolumeGainValue(*device, volume_percent); | 924 audio_pref_handler_->SetVolumeGainValue(*device, volume_percent); |
| 940 | 925 |
| 941 if (device->active) | 926 if (device->active) |
| 942 SetOutputNodeVolume(node_id, volume_percent); | 927 SetOutputNodeVolume(node_id, volume_percent); |
| 943 } | 928 } |
| 944 | 929 |
| 945 bool CrasAudioHandler::SetOutputMuteInternal(bool mute_on) { | 930 bool CrasAudioHandler::SetOutputMuteInternal(bool mute_on) { |
| 946 if (output_mute_locked_) | 931 if (output_mute_locked_) |
| 947 return false; | 932 return false; |
| 948 | 933 |
| 949 output_mute_on_ = mute_on; | 934 output_mute_on_ = mute_on; |
| 950 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 935 GetCrasAudioClient()->SetOutputUserMute(mute_on); |
| 951 SetOutputUserMute(mute_on); | |
| 952 return true; | 936 return true; |
| 953 } | 937 } |
| 954 | 938 |
| 955 void CrasAudioHandler::SetInputNodeGain(uint64_t node_id, int gain) { | 939 void CrasAudioHandler::SetInputNodeGain(uint64_t node_id, int gain) { |
| 956 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 940 GetCrasAudioClient()->SetInputNodeGain(node_id, gain); |
| 957 SetInputNodeGain(node_id, gain); | |
| 958 } | 941 } |
| 959 | 942 |
| 960 void CrasAudioHandler::SetInputNodeGainPercent(uint64_t node_id, | 943 void CrasAudioHandler::SetInputNodeGainPercent(uint64_t node_id, |
| 961 int gain_percent) { | 944 int gain_percent) { |
| 962 const AudioDevice* device = GetDeviceFromId(node_id); | 945 const AudioDevice* device = GetDeviceFromId(node_id); |
| 963 if (!device || !device->is_input) | 946 if (!device || !device->is_input) |
| 964 return; | 947 return; |
| 965 | 948 |
| 966 // NOTE: We do not sanitize input gain values since the range is completely | 949 // NOTE: We do not sanitize input gain values since the range is completely |
| 967 // dependent on the device. | 950 // dependent on the device. |
| 968 if (active_input_node_id_ == node_id) | 951 if (active_input_node_id_ == node_id) |
| 969 input_gain_ = gain_percent; | 952 input_gain_ = gain_percent; |
| 970 | 953 |
| 971 audio_pref_handler_->SetVolumeGainValue(*device, gain_percent); | 954 audio_pref_handler_->SetVolumeGainValue(*device, gain_percent); |
| 972 | 955 |
| 973 if (device->active) { | 956 if (device->active) { |
| 974 SetInputNodeGain(node_id, gain_percent); | 957 SetInputNodeGain(node_id, gain_percent); |
| 975 for (auto& observer : observers_) | 958 for (auto& observer : observers_) |
| 976 observer.OnInputNodeGainChanged(node_id, gain_percent); | 959 observer.OnInputNodeGainChanged(node_id, gain_percent); |
| 977 } | 960 } |
| 978 } | 961 } |
| 979 | 962 |
| 980 void CrasAudioHandler::SetInputMuteInternal(bool mute_on) { | 963 void CrasAudioHandler::SetInputMuteInternal(bool mute_on) { |
| 981 input_mute_on_ = mute_on; | 964 input_mute_on_ = mute_on; |
| 982 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 965 GetCrasAudioClient()->SetInputMute(mute_on); |
| 983 SetInputMute(mute_on); | |
| 984 } | 966 } |
| 985 | 967 |
| 986 void CrasAudioHandler::GetNodes() { | 968 void CrasAudioHandler::GetNodes() { |
| 987 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()->GetNodes( | 969 GetCrasAudioClient()->GetNodes( |
| 988 base::Bind(&CrasAudioHandler::HandleGetNodes, | 970 base::Bind(&CrasAudioHandler::HandleGetNodes, |
| 989 weak_ptr_factory_.GetWeakPtr()), | 971 weak_ptr_factory_.GetWeakPtr()), |
| 990 base::Bind(&CrasAudioHandler::HandleGetNodesError, | 972 base::Bind(&CrasAudioHandler::HandleGetNodesError, |
| 991 weak_ptr_factory_.GetWeakPtr())); | 973 weak_ptr_factory_.GetWeakPtr())); |
| 992 } | 974 } |
| 993 | 975 |
| 994 bool CrasAudioHandler::ChangeActiveDevice( | 976 bool CrasAudioHandler::ChangeActiveDevice( |
| 995 const AudioDevice& new_active_device) { | 977 const AudioDevice& new_active_device) { |
| 996 uint64_t& current_active_node_id = new_active_device.is_input | 978 uint64_t& current_active_node_id = new_active_device.is_input |
| 997 ? active_input_node_id_ | 979 ? active_input_node_id_ |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1086 const AudioDevice* device_found = | 1068 const AudioDevice* device_found = |
| 1087 GetDeviceFromStableDeviceId(device.stable_device_id); | 1069 GetDeviceFromStableDeviceId(device.stable_device_id); |
| 1088 if (!device_found) | 1070 if (!device_found) |
| 1089 return NEW_DEVICE; | 1071 return NEW_DEVICE; |
| 1090 | 1072 |
| 1091 if (!IsSameAudioDevice(device, *device_found)) { | 1073 if (!IsSameAudioDevice(device, *device_found)) { |
| 1092 LOG(ERROR) << "Different Audio devices with same stable device id:" | 1074 LOG(ERROR) << "Different Audio devices with same stable device id:" |
| 1093 << " new device: " << device.ToString() | 1075 << " new device: " << device.ToString() |
| 1094 << " old device: " << device_found->ToString(); | 1076 << " old device: " << device_found->ToString(); |
| 1095 return CHANGED_DEVICE; | 1077 return CHANGED_DEVICE; |
| 1096 } else if (device.active != device_found->active) { | 1078 } |
| 1079 if (device.active != device_found->active) |
| 1097 return CHANGED_DEVICE; | 1080 return CHANGED_DEVICE; |
| 1098 } | |
| 1099 | |
| 1100 return OLD_DEVICE; | 1081 return OLD_DEVICE; |
| 1101 } | 1082 } |
| 1102 | 1083 |
| 1103 void CrasAudioHandler::NotifyActiveNodeChanged(bool is_input) { | 1084 void CrasAudioHandler::NotifyActiveNodeChanged(bool is_input) { |
| 1104 if (is_input) | 1085 if (is_input) { |
| 1105 for (auto& observer : observers_) | 1086 for (auto& observer : observers_) |
| 1106 observer.OnActiveInputNodeChanged(); | 1087 observer.OnActiveInputNodeChanged(); |
| 1107 else | 1088 } else { |
| 1108 for (auto& observer : observers_) | 1089 for (auto& observer : observers_) |
| 1109 observer.OnActiveOutputNodeChanged(); | 1090 observer.OnActiveOutputNodeChanged(); |
| 1091 } |
| 1110 } | 1092 } |
| 1111 | 1093 |
| 1112 bool CrasAudioHandler::GetActiveDeviceFromUserPref(bool is_input, | 1094 bool CrasAudioHandler::GetActiveDeviceFromUserPref(bool is_input, |
| 1113 AudioDevice* active_device) { | 1095 AudioDevice* active_device) { |
| 1114 bool found_active_device = false; | 1096 bool found_active_device = false; |
| 1115 bool last_active_device_activate_by_user = false; | 1097 bool last_active_device_activate_by_user = false; |
| 1116 for (const auto& item : audio_devices_) { | 1098 for (const auto& item : audio_devices_) { |
| 1117 const AudioDevice& device = item.second; | 1099 const AudioDevice& device = item.second; |
| 1118 if (device.is_input != is_input || !device.is_for_simple_usage()) | 1100 if (device.is_input != is_input || !device.is_for_simple_usage()) |
| 1119 continue; | 1101 continue; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1134 | 1116 |
| 1135 if (!found_active_device) { | 1117 if (!found_active_device) { |
| 1136 found_active_device = true; | 1118 found_active_device = true; |
| 1137 *active_device = device; | 1119 *active_device = device; |
| 1138 last_active_device_activate_by_user = activate_by_user; | 1120 last_active_device_activate_by_user = activate_by_user; |
| 1139 continue; | 1121 continue; |
| 1140 } | 1122 } |
| 1141 | 1123 |
| 1142 // Choose the best one among multiple active devices from prefs. | 1124 // Choose the best one among multiple active devices from prefs. |
| 1143 if (activate_by_user) { | 1125 if (activate_by_user) { |
| 1144 if (!last_active_device_activate_by_user) { | 1126 if (last_active_device_activate_by_user) { |
| 1145 // Device activated by user has higher priority than the one | |
| 1146 // is not activated by user. | |
| 1147 *active_device = device; | |
| 1148 last_active_device_activate_by_user = true; | |
| 1149 } else { | |
| 1150 // If there are more than one active devices activated by user in the | 1127 // If there are more than one active devices activated by user in the |
| 1151 // prefs, most likely, after the device was shut down, and before it | 1128 // prefs, most likely, after the device was shut down, and before it |
| 1152 // is rebooted, user has plugged in some previously unplugged audio | 1129 // is rebooted, user has plugged in some previously unplugged audio |
| 1153 // devices. For such case, it does not make sense to honor the active | 1130 // devices. For such case, it does not make sense to honor the active |
| 1154 // states in the prefs. | 1131 // states in the prefs. |
| 1155 VLOG(1) << "Found more than one user activated devices in the prefs."; | 1132 VLOG(1) << "Found more than one user activated devices in the prefs."; |
| 1156 return false; | 1133 return false; |
| 1157 } | 1134 } |
| 1135 // Device activated by user has higher priority than the one |
| 1136 // is not activated by user. |
| 1137 *active_device = device; |
| 1138 last_active_device_activate_by_user = true; |
| 1158 } else if (!last_active_device_activate_by_user) { | 1139 } else if (!last_active_device_activate_by_user) { |
| 1159 // If there are more than one active devices activated by priority in the | 1140 // If there are more than one active devices activated by priority in the |
| 1160 // prefs, most likely, cras is still enumerating the audio devices | 1141 // prefs, most likely, cras is still enumerating the audio devices |
| 1161 // progressively. For such case, it does not make sense to honor the | 1142 // progressively. For such case, it does not make sense to honor the |
| 1162 // active states in the prefs. | 1143 // active states in the prefs. |
| 1163 VLOG(1) << "Found more than one active devices by priority in the prefs."; | 1144 VLOG(1) << "Found more than one active devices by priority in the prefs."; |
| 1164 return false; | 1145 return false; |
| 1165 } | 1146 } |
| 1166 } | 1147 } |
| 1167 | 1148 |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1422 VLOG(1) << "AddActiveInputNode: Cannot find device id=" | 1403 VLOG(1) << "AddActiveInputNode: Cannot find device id=" |
| 1423 << "0x" << std::hex << node_id; | 1404 << "0x" << std::hex << node_id; |
| 1424 return; | 1405 return; |
| 1425 } | 1406 } |
| 1426 | 1407 |
| 1427 audio_devices_[node_id].active = true; | 1408 audio_devices_[node_id].active = true; |
| 1428 SetupAdditionalActiveAudioNodeState(node_id); | 1409 SetupAdditionalActiveAudioNodeState(node_id); |
| 1429 | 1410 |
| 1430 if (device->is_input) { | 1411 if (device->is_input) { |
| 1431 DCHECK(node_id != active_input_node_id_); | 1412 DCHECK(node_id != active_input_node_id_); |
| 1432 chromeos::DBusThreadManager::Get() | 1413 GetCrasAudioClient()->AddActiveInputNode(node_id); |
| 1433 ->GetCrasAudioClient() | |
| 1434 ->AddActiveInputNode(node_id); | |
| 1435 if (notify) | 1414 if (notify) |
| 1436 NotifyActiveNodeChanged(true); | 1415 NotifyActiveNodeChanged(true); |
| 1437 } else { | 1416 } else { |
| 1438 DCHECK(node_id != active_output_node_id_); | 1417 DCHECK(node_id != active_output_node_id_); |
| 1439 chromeos::DBusThreadManager::Get() | 1418 GetCrasAudioClient()->AddActiveOutputNode(node_id); |
| 1440 ->GetCrasAudioClient() | |
| 1441 ->AddActiveOutputNode(node_id); | |
| 1442 if (notify) | 1419 if (notify) |
| 1443 NotifyActiveNodeChanged(false); | 1420 NotifyActiveNodeChanged(false); |
| 1444 } | 1421 } |
| 1445 } | 1422 } |
| 1446 | 1423 |
| 1447 void CrasAudioHandler::RemoveActiveNodeInternal(uint64_t node_id, bool notify) { | 1424 void CrasAudioHandler::RemoveActiveNodeInternal(uint64_t node_id, bool notify) { |
| 1448 const AudioDevice* device = GetDeviceFromId(node_id); | 1425 const AudioDevice* device = GetDeviceFromId(node_id); |
| 1449 if (!device) { | 1426 if (!device) { |
| 1450 VLOG(1) << "RemoveActiveInputNode: Cannot find device id=" | 1427 VLOG(1) << "RemoveActiveInputNode: Cannot find device id=" |
| 1451 << "0x" << std::hex << node_id; | 1428 << "0x" << std::hex << node_id; |
| 1452 return; | 1429 return; |
| 1453 } | 1430 } |
| 1454 | 1431 |
| 1455 audio_devices_[node_id].active = false; | 1432 audio_devices_[node_id].active = false; |
| 1456 if (device->is_input) { | 1433 if (device->is_input) { |
| 1457 if (node_id == active_input_node_id_) | 1434 if (node_id == active_input_node_id_) |
| 1458 active_input_node_id_ = 0; | 1435 active_input_node_id_ = 0; |
| 1459 chromeos::DBusThreadManager::Get() | 1436 GetCrasAudioClient()->RemoveActiveInputNode(node_id); |
| 1460 ->GetCrasAudioClient() | |
| 1461 ->RemoveActiveInputNode(node_id); | |
| 1462 if (notify) | 1437 if (notify) |
| 1463 NotifyActiveNodeChanged(true); | 1438 NotifyActiveNodeChanged(true); |
| 1464 } else { | 1439 } else { |
| 1465 if (node_id == active_output_node_id_) | 1440 if (node_id == active_output_node_id_) |
| 1466 active_output_node_id_ = 0; | 1441 active_output_node_id_ = 0; |
| 1467 chromeos::DBusThreadManager::Get() | 1442 GetCrasAudioClient()->RemoveActiveOutputNode(node_id); |
| 1468 ->GetCrasAudioClient() | |
| 1469 ->RemoveActiveOutputNode(node_id); | |
| 1470 if (notify) | 1443 if (notify) |
| 1471 NotifyActiveNodeChanged(false); | 1444 NotifyActiveNodeChanged(false); |
| 1472 } | 1445 } |
| 1473 } | 1446 } |
| 1474 | 1447 |
| 1475 void CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod() { | 1448 void CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod() { |
| 1476 VLOG(1) << "HDMI output re-discover grace period ends."; | 1449 VLOG(1) << "HDMI output re-discover grace period ends."; |
| 1477 hdmi_rediscovering_ = false; | 1450 hdmi_rediscovering_ = false; |
| 1478 if (!IsOutputMutedForDevice(active_output_node_id_)) { | 1451 if (!IsOutputMutedForDevice(active_output_node_id_)) { |
| 1479 // Unmute the audio output after the HDMI transition period. | 1452 // Unmute the audio output after the HDMI transition period. |
| 1480 VLOG(1) << "Unmute output after HDMI rediscovering grace period."; | 1453 VLOG(1) << "Unmute output after HDMI rediscovering grace period."; |
| 1481 SetOutputMuteInternal(false); | 1454 SetOutputMuteInternal(false); |
| 1482 | 1455 |
| 1483 // Notify UI about the mute state change. | 1456 // Notify UI about the mute state change. |
| 1484 for (auto& observer : observers_) { | 1457 for (auto& observer : observers_) { |
| 1485 observer.OnOutputMuteChanged(output_mute_on_, | 1458 observer.OnOutputMuteChanged(output_mute_on_, |
| 1486 true /* system adjustment */); | 1459 true /* system adjustment */); |
| 1487 } | 1460 } |
| 1488 } | 1461 } |
| 1489 } | 1462 } |
| 1490 | 1463 |
| 1491 bool CrasAudioHandler::IsHDMIPrimaryOutputDevice() const { | 1464 bool CrasAudioHandler::IsHDMIPrimaryOutputDevice() const { |
| 1492 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); | 1465 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); |
| 1493 return (device && device->type == chromeos::AUDIO_TYPE_HDMI); | 1466 return device && device->type == AUDIO_TYPE_HDMI; |
| 1494 } | 1467 } |
| 1495 | 1468 |
| 1496 void CrasAudioHandler::StartHDMIRediscoverGracePeriod() { | 1469 void CrasAudioHandler::StartHDMIRediscoverGracePeriod() { |
| 1497 VLOG(1) << "Start HDMI rediscovering grace period."; | 1470 VLOG(1) << "Start HDMI rediscovering grace period."; |
| 1498 hdmi_rediscovering_ = true; | 1471 hdmi_rediscovering_ = true; |
| 1499 hdmi_rediscover_timer_.Stop(); | 1472 hdmi_rediscover_timer_.Stop(); |
| 1500 hdmi_rediscover_timer_.Start( | 1473 hdmi_rediscover_timer_.Start( |
| 1501 FROM_HERE, base::TimeDelta::FromMilliseconds( | 1474 FROM_HERE, base::TimeDelta::FromMilliseconds( |
| 1502 hdmi_rediscover_grace_period_duration_in_ms_), | 1475 hdmi_rediscover_grace_period_duration_in_ms_), |
| 1503 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1476 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1592 bool CrasAudioHandler::HasExternalDevice(bool is_input) const { | 1565 bool CrasAudioHandler::HasExternalDevice(bool is_input) const { |
| 1593 for (const auto& item : audio_devices_) { | 1566 for (const auto& item : audio_devices_) { |
| 1594 const AudioDevice& device = item.second; | 1567 const AudioDevice& device = item.second; |
| 1595 if (is_input == device.is_input && device.IsExternalDevice()) | 1568 if (is_input == device.is_input && device.IsExternalDevice()) |
| 1596 return true; | 1569 return true; |
| 1597 } | 1570 } |
| 1598 return false; | 1571 return false; |
| 1599 } | 1572 } |
| 1600 | 1573 |
| 1601 } // namespace chromeos | 1574 } // namespace chromeos |
| OLD | NEW |