| 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 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 void CrasAudioHandler::SetOutputMono(bool mono_on) { | 341 void CrasAudioHandler::SetOutputMono(bool mono_on) { |
| 342 output_mono_on_ = mono_on; | 342 output_mono_on_ = mono_on; |
| 343 if (mono_on) { | 343 if (mono_on) { |
| 344 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 344 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 345 SetGlobalOutputChannelRemix(output_channels_, kStereoToMono); | 345 SetGlobalOutputChannelRemix(output_channels_, kStereoToMono); |
| 346 } else { | 346 } else { |
| 347 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 347 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 348 SetGlobalOutputChannelRemix(output_channels_, kStereoToStereo); | 348 SetGlobalOutputChannelRemix(output_channels_, kStereoToStereo); |
| 349 } | 349 } |
| 350 | 350 |
| 351 FOR_EACH_OBSERVER( | 351 for (auto& observer : observers_) |
| 352 AudioObserver, observers_, | 352 observer.OnOuputChannelRemixingChanged(mono_on); |
| 353 OnOuputChannelRemixingChanged(mono_on)); | |
| 354 } | 353 } |
| 355 | 354 |
| 356 bool CrasAudioHandler::IsOutputMonoEnabled() const { | 355 bool CrasAudioHandler::IsOutputMonoEnabled() const { |
| 357 return output_mono_on_; | 356 return output_mono_on_; |
| 358 } | 357 } |
| 359 | 358 |
| 360 bool CrasAudioHandler::has_alternative_input() const { | 359 bool CrasAudioHandler::has_alternative_input() const { |
| 361 return has_alternative_input_; | 360 return has_alternative_input_; |
| 362 } | 361 } |
| 363 | 362 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 // Save the mute state for all active output audio devices. | 405 // Save the mute state for all active output audio devices. |
| 407 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 406 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 408 it != audio_devices_.end(); | 407 it != audio_devices_.end(); |
| 409 it++) { | 408 it++) { |
| 410 const AudioDevice& device = it->second; | 409 const AudioDevice& device = it->second; |
| 411 if (!device.is_input && device.active) { | 410 if (!device.is_input && device.active) { |
| 412 audio_pref_handler_->SetMuteValue(device, output_mute_on_); | 411 audio_pref_handler_->SetMuteValue(device, output_mute_on_); |
| 413 } | 412 } |
| 414 } | 413 } |
| 415 | 414 |
| 416 FOR_EACH_OBSERVER( | 415 for (auto& observer : observers_) |
| 417 AudioObserver, observers_, | 416 observer.OnOutputMuteChanged(output_mute_on_, false /* system_adjust */); |
| 418 OnOutputMuteChanged(output_mute_on_, false /* system_adjust */)); | |
| 419 } | 417 } |
| 420 | 418 |
| 421 void CrasAudioHandler::AdjustOutputVolumeToAudibleLevel() { | 419 void CrasAudioHandler::AdjustOutputVolumeToAudibleLevel() { |
| 422 if (output_volume_ <= kMuteThresholdPercent) { | 420 if (output_volume_ <= kMuteThresholdPercent) { |
| 423 // Avoid the situation when sound has been unmuted, but the volume | 421 // Avoid the situation when sound has been unmuted, but the volume |
| 424 // is set to a very low value, so user still can't hear any sound. | 422 // is set to a very low value, so user still can't hear any sound. |
| 425 SetOutputVolumePercent(kDefaultUnmuteVolumePercent); | 423 SetOutputVolumePercent(kDefaultUnmuteVolumePercent); |
| 426 } | 424 } |
| 427 } | 425 } |
| 428 | 426 |
| 429 void CrasAudioHandler::SetInputMute(bool mute_on) { | 427 void CrasAudioHandler::SetInputMute(bool mute_on) { |
| 430 SetInputMuteInternal(mute_on); | 428 SetInputMuteInternal(mute_on); |
| 431 FOR_EACH_OBSERVER(AudioObserver, observers_, | 429 for (auto& observer : observers_) |
| 432 OnInputMuteChanged(input_mute_on_)); | 430 observer.OnInputMuteChanged(input_mute_on_); |
| 433 } | 431 } |
| 434 | 432 |
| 435 void CrasAudioHandler::SetActiveDevice(const AudioDevice& active_device, | 433 void CrasAudioHandler::SetActiveDevice(const AudioDevice& active_device, |
| 436 bool notify, | 434 bool notify, |
| 437 DeviceActivateType activate_by) { | 435 DeviceActivateType activate_by) { |
| 438 if (active_device.is_input) { | 436 if (active_device.is_input) { |
| 439 chromeos::DBusThreadManager::Get() | 437 chromeos::DBusThreadManager::Get() |
| 440 ->GetCrasAudioClient() | 438 ->GetCrasAudioClient() |
| 441 ->SetActiveInputNode(active_device.id); | 439 ->SetActiveInputNode(active_device.id); |
| 442 } else { | 440 } else { |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 } | 638 } |
| 641 } | 639 } |
| 642 | 640 |
| 643 if (std::find(automated_volume_change_reasons_.begin(), | 641 if (std::find(automated_volume_change_reasons_.begin(), |
| 644 automated_volume_change_reasons_.end(), | 642 automated_volume_change_reasons_.end(), |
| 645 VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) == | 643 VOLUME_CHANGE_INITIALIZING_AUDIO_STATE) == |
| 646 automated_volume_change_reasons_.end()) | 644 automated_volume_change_reasons_.end()) |
| 647 initializing_audio_state_ = false; | 645 initializing_audio_state_ = false; |
| 648 | 646 |
| 649 if (should_notify) { | 647 if (should_notify) { |
| 650 FOR_EACH_OBSERVER(AudioObserver, observers_, | 648 for (auto& observer : observers_) |
| 651 OnOutputNodeVolumeChanged(node_id, volume)); | 649 observer.OnOutputNodeVolumeChanged(node_id, volume); |
| 652 } | 650 } |
| 653 } | 651 } |
| 654 | 652 |
| 655 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { | 653 void CrasAudioHandler::ActiveOutputNodeChanged(uint64_t node_id) { |
| 656 if (active_output_node_id_ == node_id) | 654 if (active_output_node_id_ == node_id) |
| 657 return; | 655 return; |
| 658 | 656 |
| 659 // Active audio output device should always be changed by chrome. | 657 // Active audio output device should always be changed by chrome. |
| 660 // During system boot, cras may change active input to unknown device 0x1, | 658 // During system boot, cras may change active input to unknown device 0x1, |
| 661 // we don't need to log it, since it is not an valid device. | 659 // we don't need to log it, since it is not an valid device. |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 | 877 |
| 880 // NOTE: We do not sanitize input gain values since the range is completely | 878 // NOTE: We do not sanitize input gain values since the range is completely |
| 881 // dependent on the device. | 879 // dependent on the device. |
| 882 if (active_input_node_id_ == node_id) | 880 if (active_input_node_id_ == node_id) |
| 883 input_gain_ = gain_percent; | 881 input_gain_ = gain_percent; |
| 884 | 882 |
| 885 audio_pref_handler_->SetVolumeGainValue(*device, gain_percent); | 883 audio_pref_handler_->SetVolumeGainValue(*device, gain_percent); |
| 886 | 884 |
| 887 if (device->active) { | 885 if (device->active) { |
| 888 SetInputNodeGain(node_id, gain_percent); | 886 SetInputNodeGain(node_id, gain_percent); |
| 889 FOR_EACH_OBSERVER(AudioObserver, observers_, | 887 for (auto& observer : observers_) |
| 890 OnInputNodeGainChanged(node_id, gain_percent)); | 888 observer.OnInputNodeGainChanged(node_id, gain_percent); |
| 891 } | 889 } |
| 892 } | 890 } |
| 893 | 891 |
| 894 void CrasAudioHandler::SetInputMuteInternal(bool mute_on) { | 892 void CrasAudioHandler::SetInputMuteInternal(bool mute_on) { |
| 895 input_mute_on_ = mute_on; | 893 input_mute_on_ = mute_on; |
| 896 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> | 894 chromeos::DBusThreadManager::Get()->GetCrasAudioClient()-> |
| 897 SetInputMute(mute_on); | 895 SetInputMute(mute_on); |
| 898 } | 896 } |
| 899 | 897 |
| 900 void CrasAudioHandler::GetNodes() { | 898 void CrasAudioHandler::GetNodes() { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 return CHANGED_DEVICE; | 1008 return CHANGED_DEVICE; |
| 1011 } else if (device.active != device_found->active) { | 1009 } else if (device.active != device_found->active) { |
| 1012 return CHANGED_DEVICE; | 1010 return CHANGED_DEVICE; |
| 1013 } | 1011 } |
| 1014 | 1012 |
| 1015 return OLD_DEVICE; | 1013 return OLD_DEVICE; |
| 1016 } | 1014 } |
| 1017 | 1015 |
| 1018 void CrasAudioHandler::NotifyActiveNodeChanged(bool is_input) { | 1016 void CrasAudioHandler::NotifyActiveNodeChanged(bool is_input) { |
| 1019 if (is_input) | 1017 if (is_input) |
| 1020 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveInputNodeChanged()); | 1018 for (auto& observer : observers_) |
| 1019 observer.OnActiveInputNodeChanged(); |
| 1021 else | 1020 else |
| 1022 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged()); | 1021 for (auto& observer : observers_) |
| 1022 observer.OnActiveOutputNodeChanged(); |
| 1023 } | 1023 } |
| 1024 | 1024 |
| 1025 bool CrasAudioHandler::GetActiveDeviceFromUserPref(bool is_input, | 1025 bool CrasAudioHandler::GetActiveDeviceFromUserPref(bool is_input, |
| 1026 AudioDevice* active_device) { | 1026 AudioDevice* active_device) { |
| 1027 bool found_active_device = false; | 1027 bool found_active_device = false; |
| 1028 bool last_active_device_activate_by_user = false; | 1028 bool last_active_device_activate_by_user = false; |
| 1029 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 1029 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 1030 it != audio_devices_.end(); ++it) { | 1030 it != audio_devices_.end(); ++it) { |
| 1031 AudioDevice device = it->second; | 1031 AudioDevice device = it->second; |
| 1032 if (device.is_input != is_input || !device.is_for_simple_usage()) | 1032 if (device.is_input != is_input || !device.is_for_simple_usage()) |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1293 } | 1293 } |
| 1294 | 1294 |
| 1295 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, | 1295 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, |
| 1296 bool success) { | 1296 bool success) { |
| 1297 if (!success) { | 1297 if (!success) { |
| 1298 LOG_IF(ERROR, log_errors_) << "Failed to retrieve audio nodes data"; | 1298 LOG_IF(ERROR, log_errors_) << "Failed to retrieve audio nodes data"; |
| 1299 return; | 1299 return; |
| 1300 } | 1300 } |
| 1301 | 1301 |
| 1302 UpdateDevicesAndSwitchActive(node_list); | 1302 UpdateDevicesAndSwitchActive(node_list); |
| 1303 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 1303 for (auto& observer : observers_) |
| 1304 observer.OnAudioNodesChanged(); |
| 1304 } | 1305 } |
| 1305 | 1306 |
| 1306 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, | 1307 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, |
| 1307 const std::string& error_msg) { | 1308 const std::string& error_msg) { |
| 1308 LOG_IF(ERROR, log_errors_) << "Failed to call GetNodes: " | 1309 LOG_IF(ERROR, log_errors_) << "Failed to call GetNodes: " |
| 1309 << error_name << ": " << error_msg; | 1310 << error_name << ": " << error_msg; |
| 1310 } | 1311 } |
| 1311 | 1312 |
| 1312 void CrasAudioHandler::AddAdditionalActiveNode(uint64_t node_id, bool notify) { | 1313 void CrasAudioHandler::AddAdditionalActiveNode(uint64_t node_id, bool notify) { |
| 1313 const AudioDevice* device = GetDeviceFromId(node_id); | 1314 const AudioDevice* device = GetDeviceFromId(node_id); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1367 | 1368 |
| 1368 void CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod() { | 1369 void CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod() { |
| 1369 VLOG(1) << "HDMI output re-discover grace period ends."; | 1370 VLOG(1) << "HDMI output re-discover grace period ends."; |
| 1370 hdmi_rediscovering_ = false; | 1371 hdmi_rediscovering_ = false; |
| 1371 if (!IsOutputMutedForDevice(active_output_node_id_)) { | 1372 if (!IsOutputMutedForDevice(active_output_node_id_)) { |
| 1372 // Unmute the audio output after the HDMI transition period. | 1373 // Unmute the audio output after the HDMI transition period. |
| 1373 VLOG(1) << "Unmute output after HDMI rediscovering grace period."; | 1374 VLOG(1) << "Unmute output after HDMI rediscovering grace period."; |
| 1374 SetOutputMuteInternal(false); | 1375 SetOutputMuteInternal(false); |
| 1375 | 1376 |
| 1376 // Notify UI about the mute state change. | 1377 // Notify UI about the mute state change. |
| 1377 FOR_EACH_OBSERVER( | 1378 for (auto& observer : observers_) { |
| 1378 AudioObserver, observers_, | 1379 observer.OnOutputMuteChanged(output_mute_on_, |
| 1379 OnOutputMuteChanged(output_mute_on_, true /* system adjustment */)); | 1380 true /* system adjustment */); |
| 1381 } |
| 1380 } | 1382 } |
| 1381 } | 1383 } |
| 1382 | 1384 |
| 1383 bool CrasAudioHandler::IsHDMIPrimaryOutputDevice() const { | 1385 bool CrasAudioHandler::IsHDMIPrimaryOutputDevice() const { |
| 1384 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); | 1386 const AudioDevice* device = GetDeviceFromId(active_output_node_id_); |
| 1385 return (device && device->type == chromeos::AUDIO_TYPE_HDMI); | 1387 return (device && device->type == chromeos::AUDIO_TYPE_HDMI); |
| 1386 } | 1388 } |
| 1387 | 1389 |
| 1388 void CrasAudioHandler::StartHDMIRediscoverGracePeriod() { | 1390 void CrasAudioHandler::StartHDMIRediscoverGracePeriod() { |
| 1389 VLOG(1) << "Start HDMI rediscovering grace period."; | 1391 VLOG(1) << "Start HDMI rediscovering grace period."; |
| 1390 hdmi_rediscovering_ = true; | 1392 hdmi_rediscovering_ = true; |
| 1391 hdmi_rediscover_timer_.Stop(); | 1393 hdmi_rediscover_timer_.Stop(); |
| 1392 hdmi_rediscover_timer_.Start( | 1394 hdmi_rediscover_timer_.Start( |
| 1393 FROM_HERE, base::TimeDelta::FromMilliseconds( | 1395 FROM_HERE, base::TimeDelta::FromMilliseconds( |
| 1394 hdmi_rediscover_grace_period_duration_in_ms_), | 1396 hdmi_rediscover_grace_period_duration_in_ms_), |
| 1395 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1397 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
| 1396 } | 1398 } |
| 1397 | 1399 |
| 1398 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1400 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
| 1399 int duration_in_ms) { | 1401 int duration_in_ms) { |
| 1400 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1402 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
| 1401 } | 1403 } |
| 1402 | 1404 |
| 1403 } // namespace chromeos | 1405 } // namespace chromeos |
| OLD | NEW |