| 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 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 const AudioDevice& device = it->second; | 418 const AudioDevice& device = it->second; |
| 419 if (device.is_input != active_device.is_input) | 419 if (device.is_input != active_device.is_input) |
| 420 continue; | 420 continue; |
| 421 SaveDeviceState(device, device.active, activate_by); | 421 SaveDeviceState(device, device.active, activate_by); |
| 422 } | 422 } |
| 423 } | 423 } |
| 424 | 424 |
| 425 void CrasAudioHandler::SaveDeviceState(const AudioDevice& device, | 425 void CrasAudioHandler::SaveDeviceState(const AudioDevice& device, |
| 426 bool active, | 426 bool active, |
| 427 DeviceActivateType activate_by) { | 427 DeviceActivateType activate_by) { |
| 428 // Don't save the active state for non-simple usage device, which is invisible |
| 429 // to end users. |
| 430 if (!device.is_for_simple_usage()) |
| 431 return; |
| 432 |
| 428 if (!active) { | 433 if (!active) { |
| 429 audio_pref_handler_->SetDeviceActive(device, false, false); | 434 audio_pref_handler_->SetDeviceActive(device, false, false); |
| 430 } else { | 435 } else { |
| 431 switch (activate_by) { | 436 switch (activate_by) { |
| 432 case ACTIVATE_BY_USER: | 437 case ACTIVATE_BY_USER: |
| 433 audio_pref_handler_->SetDeviceActive(device, true, true); | 438 audio_pref_handler_->SetDeviceActive(device, true, true); |
| 434 break; | 439 break; |
| 435 case ACTIVATE_BY_PRIORITY: | 440 case ACTIVATE_BY_PRIORITY: |
| 436 audio_pref_handler_->SetDeviceActive(device, true, false); | 441 audio_pref_handler_->SetDeviceActive(device, true, false); |
| 437 break; | 442 break; |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged()); | 922 FOR_EACH_OBSERVER(AudioObserver, observers_, OnActiveOutputNodeChanged()); |
| 918 } | 923 } |
| 919 | 924 |
| 920 bool CrasAudioHandler::GetActiveDeviceFromUserPref(bool is_input, | 925 bool CrasAudioHandler::GetActiveDeviceFromUserPref(bool is_input, |
| 921 AudioDevice* active_device) { | 926 AudioDevice* active_device) { |
| 922 bool found_active_device = false; | 927 bool found_active_device = false; |
| 923 bool last_active_device_activate_by_user = false; | 928 bool last_active_device_activate_by_user = false; |
| 924 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); | 929 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 925 it != audio_devices_.end(); ++it) { | 930 it != audio_devices_.end(); ++it) { |
| 926 AudioDevice device = it->second; | 931 AudioDevice device = it->second; |
| 927 if (device.is_input != is_input) | 932 if (device.is_input != is_input || !device.is_for_simple_usage()) |
| 928 continue; | 933 continue; |
| 929 | 934 |
| 930 bool active = false; | 935 bool active = false; |
| 931 bool activate_by_user = false; | 936 bool activate_by_user = false; |
| 932 if (!audio_pref_handler_->GetDeviceActive(device, &active, | 937 if (!audio_pref_handler_->GetDeviceActive(device, &active, |
| 933 &activate_by_user) || | 938 &activate_by_user) || |
| 934 !active) { | 939 !active) { |
| 935 continue; | 940 continue; |
| 936 } | 941 } |
| 937 | 942 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 bool has_device_removed, | 991 bool has_device_removed, |
| 987 bool active_device_removed) { | 992 bool active_device_removed) { |
| 988 bool has_current_active_node = | 993 bool has_current_active_node = |
| 989 is_input ? active_input_node_id_ : active_output_node_id_; | 994 is_input ? active_input_node_id_ : active_output_node_id_; |
| 990 | 995 |
| 991 // No device change, extra NodesChanged signal received. | 996 // No device change, extra NodesChanged signal received. |
| 992 if (!has_device_change && has_current_active_node) | 997 if (!has_device_change && has_current_active_node) |
| 993 return; | 998 return; |
| 994 | 999 |
| 995 if (hotplug_nodes.empty()) { | 1000 if (hotplug_nodes.empty()) { |
| 996 // Unplugged a non-active device. | |
| 997 if (has_device_removed) { | 1001 if (has_device_removed) { |
| 998 if (!active_device_removed && has_current_active_node) { | 1002 if (!active_device_removed && has_current_active_node) { |
| 999 // Removed a non-active device, keep the current active device. | 1003 // Removed a non-active device, keep the current active device. |
| 1000 return; | 1004 return; |
| 1001 } | 1005 } |
| 1002 | 1006 |
| 1003 if (active_device_removed) { | 1007 if (active_device_removed) { |
| 1004 // Unplugged the current active device. | 1008 // Unplugged the current active device. |
| 1005 SwitchToTopPriorityDevice(is_input); | 1009 SwitchToTopPriorityDevice(is_input); |
| 1006 return; | 1010 return; |
| 1007 } | 1011 } |
| 1008 } | 1012 } |
| 1009 | 1013 |
| 1010 // Some unexpected error happens on cras side. See crbug.com/586026. | 1014 // Some unexpected error happens on cras side. See crbug.com/586026. |
| 1011 // Either cras sent stale nodes to chrome again or cras triggered some | 1015 // Either cras sent stale nodes to chrome again or cras triggered some |
| 1012 // error. Restore the previously selected active. | 1016 // error. Restore the previously selected active. |
| 1013 VLOG(1) << "Odd case from cras, the active node is lost unexpectedly. "; | 1017 VLOG(1) << "Odd case from cras, the active node is lost unexpectedly."; |
| 1014 SwitchToPreviousActiveDeviceIfAvailable(is_input); | 1018 SwitchToPreviousActiveDeviceIfAvailable(is_input); |
| 1015 } else { | 1019 } else { |
| 1016 // Looks like a new chrome session starts. | 1020 // Looks like a new chrome session starts. |
| 1017 SwitchToPreviousActiveDeviceIfAvailable(is_input); | 1021 SwitchToPreviousActiveDeviceIfAvailable(is_input); |
| 1018 } | 1022 } |
| 1019 } | 1023 } |
| 1020 | 1024 |
| 1021 void CrasAudioHandler::HandleHotPlugDevice( | 1025 void CrasAudioHandler::HandleHotPlugDevice( |
| 1022 const AudioDevice& hotplug_device, | 1026 const AudioDevice& hotplug_device, |
| 1023 const AudioDevicePriorityQueue& device_priority_queue) { | 1027 const AudioDevicePriorityQueue& device_priority_queue) { |
| 1028 // This most likely may happen during the transition period of cras |
| 1029 // initialization phase, in which a non-simple-usage node may appear like |
| 1030 // a hotplug node. |
| 1031 if (!hotplug_device.is_for_simple_usage()) |
| 1032 return; |
| 1033 |
| 1024 bool last_state_active = false; | 1034 bool last_state_active = false; |
| 1025 bool last_activate_by_user = false; | 1035 bool last_activate_by_user = false; |
| 1026 if (!audio_pref_handler_->GetDeviceActive(hotplug_device, &last_state_active, | 1036 if (!audio_pref_handler_->GetDeviceActive(hotplug_device, &last_state_active, |
| 1027 &last_activate_by_user)) { | 1037 &last_activate_by_user)) { |
| 1028 // |hotplug_device| is plugged in for the first time, activate it if it | 1038 // |hotplug_device| is plugged in for the first time, activate it if it |
| 1029 // is of the highest priority. | 1039 // is of the highest priority. |
| 1030 if (device_priority_queue.top().id == hotplug_device.id) { | 1040 if (device_priority_queue.top().id == hotplug_device.id) { |
| 1031 VLOG(1) << "Hotplug a device for the first time: " | 1041 VLOG(1) << "Hotplug a device for the first time: " |
| 1032 << hotplug_device.ToString(); | 1042 << hotplug_device.ToString(); |
| 1033 SwitchToDevice(hotplug_device, true, ACTIVATE_BY_PRIORITY); | 1043 SwitchToDevice(hotplug_device, true, ACTIVATE_BY_PRIORITY); |
| 1034 } | 1044 } |
| 1035 } else if (last_state_active) { | 1045 } else if (last_state_active) { |
| 1036 VLOG(1) << "Hotplug a device, restore to active: " | 1046 VLOG(1) << "Hotplug a device, restore to active: " |
| 1037 << hotplug_device.ToString(); | 1047 << hotplug_device.ToString(); |
| 1038 SwitchToDevice(hotplug_device, true, ACTIVATE_BY_RESTORE_PREVIOUS_STATE); | 1048 SwitchToDevice(hotplug_device, true, ACTIVATE_BY_RESTORE_PREVIOUS_STATE); |
| 1039 } else { | 1049 } else { |
| 1040 // Do not active the device if its previous state is inactive. | 1050 // Do not active the device if its previous state is inactive. |
| 1041 VLOG(1) << "Hotplug device remains inactive as its previous state:" | 1051 VLOG(1) << "Hotplug device remains inactive as its previous state:" |
| 1042 << hotplug_device.ToString(); | 1052 << hotplug_device.ToString(); |
| 1043 } | 1053 } |
| 1044 } | 1054 } |
| 1045 | 1055 |
| 1046 void CrasAudioHandler::SwitchToTopPriorityDevice(bool is_input) { | 1056 void CrasAudioHandler::SwitchToTopPriorityDevice(bool is_input) { |
| 1047 AudioDevice top_device = | 1057 AudioDevice top_device = |
| 1048 is_input ? input_devices_pq_.top() : output_devices_pq_.top(); | 1058 is_input ? input_devices_pq_.top() : output_devices_pq_.top(); |
| 1049 SwitchToDevice(top_device, true, ACTIVATE_BY_PRIORITY); | 1059 if (top_device.is_for_simple_usage()) |
| 1060 SwitchToDevice(top_device, true, ACTIVATE_BY_PRIORITY); |
| 1050 } | 1061 } |
| 1051 | 1062 |
| 1052 void CrasAudioHandler::SwitchToPreviousActiveDeviceIfAvailable(bool is_input) { | 1063 void CrasAudioHandler::SwitchToPreviousActiveDeviceIfAvailable(bool is_input) { |
| 1053 AudioDevice previous_active_device; | 1064 AudioDevice previous_active_device; |
| 1054 if (GetActiveDeviceFromUserPref(is_input, &previous_active_device)) { | 1065 if (GetActiveDeviceFromUserPref(is_input, &previous_active_device)) { |
| 1066 DCHECK(previous_active_device.is_for_simple_usage()); |
| 1055 // Switch to previous active device stored in user prefs. | 1067 // Switch to previous active device stored in user prefs. |
| 1056 SwitchToDevice(previous_active_device, true, | 1068 SwitchToDevice(previous_active_device, true, |
| 1057 ACTIVATE_BY_RESTORE_PREVIOUS_STATE); | 1069 ACTIVATE_BY_RESTORE_PREVIOUS_STATE); |
| 1058 } else { | 1070 } else { |
| 1059 // No previous active device, switch to the top priority device. | 1071 // No previous active device, switch to the top priority device. |
| 1060 SwitchToTopPriorityDevice(is_input); | 1072 SwitchToTopPriorityDevice(is_input); |
| 1061 } | 1073 } |
| 1062 } | 1074 } |
| 1063 | 1075 |
| 1064 void CrasAudioHandler::UpdateDevicesAndSwitchActive( | 1076 void CrasAudioHandler::UpdateDevicesAndSwitchActive( |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1266 hdmi_rediscover_grace_period_duration_in_ms_), | 1278 hdmi_rediscover_grace_period_duration_in_ms_), |
| 1267 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); | 1279 this, &CrasAudioHandler::UpdateAudioAfterHDMIRediscoverGracePeriod); |
| 1268 } | 1280 } |
| 1269 | 1281 |
| 1270 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( | 1282 void CrasAudioHandler::SetHDMIRediscoverGracePeriodForTesting( |
| 1271 int duration_in_ms) { | 1283 int duration_in_ms) { |
| 1272 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; | 1284 hdmi_rediscover_grace_period_duration_in_ms_ = duration_in_ms; |
| 1273 } | 1285 } |
| 1274 | 1286 |
| 1275 } // namespace chromeos | 1287 } // namespace chromeos |
| OLD | NEW |