| 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 "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 const uint64_t kMicJackId = 10009; | 35 const uint64_t kMicJackId = 10009; |
| 36 const uint64_t kKeyboardMicId = 10010; | 36 const uint64_t kKeyboardMicId = 10010; |
| 37 const uint64_t kOtherTypeOutputId = 90001; | 37 const uint64_t kOtherTypeOutputId = 90001; |
| 38 const uint64_t kOtherTypeInputId = 90002; | 38 const uint64_t kOtherTypeInputId = 90002; |
| 39 const uint64_t kUSBJabraSpeakerOutputId1 = 90003; | 39 const uint64_t kUSBJabraSpeakerOutputId1 = 90003; |
| 40 const uint64_t kUSBJabraSpeakerOutputId2 = 90004; | 40 const uint64_t kUSBJabraSpeakerOutputId2 = 90004; |
| 41 const uint64_t kUSBJabraSpeakerInputId1 = 90005; | 41 const uint64_t kUSBJabraSpeakerInputId1 = 90005; |
| 42 const uint64_t kUSBJabraSpeakerInputId2 = 90006; | 42 const uint64_t kUSBJabraSpeakerInputId2 = 90006; |
| 43 const uint64_t kUSBCameraInputId = 90007; | 43 const uint64_t kUSBCameraInputId = 90007; |
| 44 | 44 |
| 45 const AudioNode kInternalSpeaker( | 45 const AudioNode kInternalSpeaker(false, |
| 46 false, | 46 kInternalSpeakerId, |
| 47 kInternalSpeakerId, | 47 kInternalSpeakerId, |
| 48 "Fake Speaker", | 48 "Fake Speaker", |
| 49 "INTERNAL_SPEAKER", | 49 "INTERNAL_SPEAKER", |
| 50 "Speaker", | 50 "Speaker", |
| 51 false, | 51 false, |
| 52 0 | 52 0); |
| 53 ); | |
| 54 | 53 |
| 55 const AudioNode kHeadphone( | 54 const AudioNode kHeadphone(false, |
| 56 false, | 55 kHeadphoneId, |
| 57 kHeadphoneId, | 56 kHeadphoneId, |
| 58 "Fake Headphone", | 57 "Fake Headphone", |
| 59 "HEADPHONE", | 58 "HEADPHONE", |
| 60 "Headphone", | 59 "Headphone", |
| 61 false, | 60 false, |
| 62 0 | 61 0); |
| 63 ); | |
| 64 | 62 |
| 65 const AudioNode kInternalMic( | 63 const AudioNode kInternalMic(true, |
| 66 true, | 64 kInternalMicId, |
| 67 kInternalMicId, | 65 kInternalMicId, |
| 68 "Fake Mic", | 66 "Fake Mic", |
| 69 "INTERNAL_MIC", | 67 "INTERNAL_MIC", |
| 70 "Internal Mic", | 68 "Internal Mic", |
| 71 false, | 69 false, |
| 72 0 | 70 0); |
| 73 ); | |
| 74 | 71 |
| 75 const AudioNode kMicJack( | 72 const AudioNode kMicJack(true, |
| 76 true, | 73 kMicJackId, |
| 77 kMicJackId, | 74 kMicJackId, |
| 78 "Fake Mic Jack", | 75 "Fake Mic Jack", |
| 79 "MIC", | 76 "MIC", |
| 80 "Mic Jack", | 77 "Mic Jack", |
| 81 false, | 78 false, |
| 82 0 | 79 0); |
| 83 ); | |
| 84 | 80 |
| 85 const AudioNode kUSBMic( | 81 const AudioNode kUSBMic(true, |
| 86 true, | 82 kUSBMicId, |
| 87 kUSBMicId, | 83 kUSBMicId, |
| 88 "Fake USB Mic", | 84 "Fake USB Mic", |
| 89 "USB", | 85 "USB", |
| 90 "USB Microphone", | 86 "USB Microphone", |
| 91 false, | 87 false, |
| 92 0 | 88 0); |
| 93 ); | |
| 94 | 89 |
| 95 const AudioNode kKeyboardMic( | 90 const AudioNode kKeyboardMic(true, |
| 96 true, | 91 kKeyboardMicId, |
| 97 kKeyboardMicId, | 92 kKeyboardMicId, |
| 98 "Fake Keyboard Mic", | 93 "Fake Keyboard Mic", |
| 99 "KEYBOARD_MIC", | 94 "KEYBOARD_MIC", |
| 100 "Keyboard Mic", | 95 "Keyboard Mic", |
| 101 false, | 96 false, |
| 102 0 | 97 0); |
| 103 ); | |
| 104 | 98 |
| 105 const AudioNode kOtherTypeOutput( | 99 const AudioNode kOtherTypeOutput(false, |
| 106 false, | 100 kOtherTypeOutputId, |
| 107 kOtherTypeOutputId, | 101 kOtherTypeOutputId, |
| 108 "Output Device", | 102 "Output Device", |
| 109 "SOME_OTHER_TYPE", | 103 "SOME_OTHER_TYPE", |
| 110 "Other Type Output Device", | 104 "Other Type Output Device", |
| 111 false, | 105 false, |
| 112 0 | 106 0); |
| 113 ); | |
| 114 | 107 |
| 115 const AudioNode kOtherTypeInput( | 108 const AudioNode kOtherTypeInput(true, |
| 116 true, | 109 kOtherTypeInputId, |
| 117 kOtherTypeInputId, | 110 kOtherTypeInputId, |
| 118 "Input Device", | 111 "Input Device", |
| 119 "SOME_OTHER_TYPE", | 112 "SOME_OTHER_TYPE", |
| 120 "Other Type Input Device", | 113 "Other Type Input Device", |
| 121 false, | 114 false, |
| 122 0 | 115 0); |
| 123 ); | |
| 124 | 116 |
| 125 const AudioNode kBluetoothHeadset(false, | 117 const AudioNode kBluetoothHeadset(false, |
| 126 kBluetoothHeadsetId, | 118 kBluetoothHeadsetId, |
| 119 kBluetoothHeadsetId, |
| 127 "Bluetooth Headset", | 120 "Bluetooth Headset", |
| 128 "BLUETOOTH", | 121 "BLUETOOTH", |
| 129 "Bluetooth Headset 1", | 122 "Bluetooth Headset 1", |
| 130 false, | 123 false, |
| 131 0); | 124 0); |
| 132 | 125 |
| 133 const AudioNode kHDMIOutput(false, | 126 const AudioNode kHDMIOutput(false, |
| 134 kHDMIOutputId, | 127 kHDMIOutputId, |
| 128 kHDMIOutputId, |
| 135 "HDMI output", | 129 "HDMI output", |
| 136 "HDMI", | 130 "HDMI", |
| 137 "HDMI output", | 131 "HDMI output", |
| 138 false, | 132 false, |
| 139 0); | 133 0); |
| 140 | 134 |
| 141 const AudioNode kUSBHeadphone1(false, | 135 const AudioNode kUSBHeadphone1(false, |
| 142 kUSBHeadphoneId1, | 136 kUSBHeadphoneId1, |
| 137 kUSBHeadphoneId1, |
| 143 "USB Headphone", | 138 "USB Headphone", |
| 144 "USB", | 139 "USB", |
| 145 "USB Headphone 1", | 140 "USB Headphone 1", |
| 146 false, | 141 false, |
| 147 0); | 142 0); |
| 148 | 143 |
| 149 const AudioNode kUSBHeadphone2(false, | 144 const AudioNode kUSBHeadphone2(false, |
| 150 kUSBHeadphoneId2, | 145 kUSBHeadphoneId2, |
| 146 kUSBHeadphoneId2, |
| 151 "USB Headphone", | 147 "USB Headphone", |
| 152 "USB", | 148 "USB", |
| 153 "USB Headphone 1", | 149 "USB Headphone 1", |
| 154 false, | 150 false, |
| 155 0); | 151 0); |
| 156 | 152 |
| 157 const AudioNode kUSBJabraSpeakerOutput1(false, | 153 const AudioNode kUSBJabraSpeakerOutput1(false, |
| 158 kUSBJabraSpeakerOutputId1, | 154 kUSBJabraSpeakerOutputId1, |
| 155 kUSBJabraSpeakerOutputId1, |
| 159 "Jabra Speaker 1", | 156 "Jabra Speaker 1", |
| 160 "USB", | 157 "USB", |
| 161 "Jabra Speaker 1", | 158 "Jabra Speaker 1", |
| 162 false, | 159 false, |
| 163 0); | 160 0); |
| 164 | 161 |
| 165 const AudioNode kUSBJabraSpeakerOutput2(false, | 162 const AudioNode kUSBJabraSpeakerOutput2(false, |
| 166 kUSBJabraSpeakerOutputId2, | 163 kUSBJabraSpeakerOutputId2, |
| 164 kUSBJabraSpeakerOutputId2, |
| 167 "Jabra Speaker 2", | 165 "Jabra Speaker 2", |
| 168 "USB", | 166 "USB", |
| 169 "Jabra Speaker 2", | 167 "Jabra Speaker 2", |
| 170 false, | 168 false, |
| 171 0); | 169 0); |
| 172 | 170 |
| 173 const AudioNode kUSBJabraSpeakerInput1(true, | 171 const AudioNode kUSBJabraSpeakerInput1(true, |
| 174 kUSBJabraSpeakerInputId1, | 172 kUSBJabraSpeakerInputId1, |
| 173 kUSBJabraSpeakerInputId1, |
| 175 "Jabra Speaker 1", | 174 "Jabra Speaker 1", |
| 176 "USB", | 175 "USB", |
| 177 "Jabra Speaker", | 176 "Jabra Speaker", |
| 178 false, | 177 false, |
| 179 0); | 178 0); |
| 180 | 179 |
| 181 const AudioNode kUSBJabraSpeakerInput2(true, | 180 const AudioNode kUSBJabraSpeakerInput2(true, |
| 182 kUSBJabraSpeakerInputId2, | 181 kUSBJabraSpeakerInputId2, |
| 182 kUSBJabraSpeakerInputId2, |
| 183 "Jabra Speaker 2", | 183 "Jabra Speaker 2", |
| 184 "USB", | 184 "USB", |
| 185 "Jabra Speaker 2", | 185 "Jabra Speaker 2", |
| 186 false, | 186 false, |
| 187 0); | 187 0); |
| 188 | 188 |
| 189 const AudioNode kUSBCameraInput(true, | 189 const AudioNode kUSBCameraInput(true, |
| 190 kUSBCameraInputId, | 190 kUSBCameraInputId, |
| 191 kUSBCameraInputId, |
| 191 "USB Camera", | 192 "USB Camera", |
| 192 "USB", | 193 "USB", |
| 193 "USB Camera", | 194 "USB Camera", |
| 194 false, | 195 false, |
| 195 0); | 196 0); |
| 196 | 197 |
| 197 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { | 198 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { |
| 198 public: | 199 public: |
| 199 TestObserver() | 200 TestObserver() |
| 200 : active_output_node_changed_count_(0), | 201 : active_output_node_changed_count_(0), |
| (...skipping 1835 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2036 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2037 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2037 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); | 2038 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); |
| 2038 | 2039 |
| 2039 // Ensure the internal microphone has been selected as the active input, | 2040 // Ensure the internal microphone has been selected as the active input, |
| 2040 // and the input device with some random unknown type is handled gracefully. | 2041 // and the input device with some random unknown type is handled gracefully. |
| 2041 AudioDevice active_input; | 2042 AudioDevice active_input; |
| 2042 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); | 2043 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); |
| 2043 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); | 2044 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); |
| 2044 } | 2045 } |
| 2045 | 2046 |
| 2047 TEST_F(CrasAudioHandlerTest, ActiveDeviceSelectionWithStableDeviceId) { |
| 2048 AudioNodeList audio_nodes; |
| 2049 AudioNode internal_speaker(kInternalSpeaker); |
| 2050 audio_nodes.push_back(internal_speaker); |
| 2051 AudioNode usb_headset(kUSBHeadphone1); |
| 2052 usb_headset.plugged_time = 80000000; |
| 2053 audio_nodes.push_back(usb_headset); |
| 2054 SetUpCrasAudioHandler(audio_nodes); |
| 2055 |
| 2056 // Verify the audio devices size. |
| 2057 AudioDeviceList audio_devices; |
| 2058 cras_audio_handler_->GetAudioDevices(&audio_devices); |
| 2059 EXPECT_EQ(audio_nodes.size(), audio_devices.size()); |
| 2060 |
| 2061 // Initially active node is selected base on priority, so USB headphone |
| 2062 // is selected. |
| 2063 EXPECT_EQ(kUSBHeadphone1.id, |
| 2064 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2065 |
| 2066 // Change the active device to internal speaker, now USB headphone becomes |
| 2067 // inactive. |
| 2068 AudioDevice speaker(kInternalSpeaker); |
| 2069 cras_audio_handler_->SwitchToDevice(speaker, true); |
| 2070 EXPECT_NE(kUSBHeadphone1.id, |
| 2071 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2072 |
| 2073 // Unplug USB headset. |
| 2074 audio_nodes.clear(); |
| 2075 internal_speaker.active = true; |
| 2076 audio_nodes.push_back(internal_speaker); |
| 2077 ChangeAudioNodes(audio_nodes); |
| 2078 EXPECT_EQ(kInternalSpeaker.id, |
| 2079 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2080 |
| 2081 // Plug the same USB headset back, id is different, but stable_device_id |
| 2082 // remains the same. |
| 2083 usb_headset.active = false; |
| 2084 usb_headset.id = 98765; |
| 2085 audio_nodes.push_back(usb_headset); |
| 2086 ChangeAudioNodes(audio_nodes); |
| 2087 |
| 2088 // Since USB headset was inactive before it was unplugged, it won't be |
| 2089 // selected as active after it's plugged in again. |
| 2090 EXPECT_EQ(kInternalSpeaker.id, |
| 2091 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2092 |
| 2093 // Plug the second USB headset. |
| 2094 AudioNode usb_headset2(kUSBHeadphone2); |
| 2095 usb_headset2.plugged_time = 80000001; |
| 2096 audio_nodes.push_back(usb_headset2); |
| 2097 ChangeAudioNodes(audio_nodes); |
| 2098 |
| 2099 // Since the second USB device is new, it's selected as the active device |
| 2100 // by its priority. |
| 2101 EXPECT_EQ(kUSBHeadphone2.id, |
| 2102 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2103 |
| 2104 // Unplug the second USB headset. |
| 2105 audio_nodes.clear(); |
| 2106 internal_speaker.active = false; |
| 2107 audio_nodes.push_back(internal_speaker); |
| 2108 audio_nodes.push_back(usb_headset); |
| 2109 ChangeAudioNodes(audio_nodes); |
| 2110 |
| 2111 // There is no active node after USB2 unplugged, the 1st USB got selected |
| 2112 // by its priority. |
| 2113 EXPECT_EQ(usb_headset.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2114 |
| 2115 usb_headset2.active = false; |
| 2116 usb_headset2.plugged_time = 80000002; |
| 2117 audio_nodes.push_back(usb_headset2); |
| 2118 ChangeAudioNodes(audio_nodes); |
| 2119 |
| 2120 // Plug the second USB again. Since it was the active node before it got |
| 2121 // unplugged, it is now selected as the active node. |
| 2122 EXPECT_EQ(usb_headset2.id, cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2123 } |
| 2124 |
| 2046 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) { | 2125 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) { |
| 2047 AudioNodeList audio_nodes; | 2126 AudioNodeList audio_nodes; |
| 2048 audio_nodes.push_back(kHDMIOutput); | 2127 audio_nodes.push_back(kHDMIOutput); |
| 2049 audio_nodes.push_back(kUSBJabraSpeakerOutput1); | 2128 audio_nodes.push_back(kUSBJabraSpeakerOutput1); |
| 2050 audio_nodes.push_back(kUSBJabraSpeakerOutput2); | 2129 audio_nodes.push_back(kUSBJabraSpeakerOutput2); |
| 2051 audio_nodes.push_back(kUSBJabraSpeakerInput1); | 2130 audio_nodes.push_back(kUSBJabraSpeakerInput1); |
| 2052 audio_nodes.push_back(kUSBJabraSpeakerInput2); | 2131 audio_nodes.push_back(kUSBJabraSpeakerInput2); |
| 2053 audio_nodes.push_back(kUSBCameraInput); | 2132 audio_nodes.push_back(kUSBCameraInput); |
| 2054 SetUpCrasAudioHandler(audio_nodes); | 2133 SetUpCrasAudioHandler(audio_nodes); |
| 2055 | 2134 |
| (...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2655 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 2734 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 2656 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 2735 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
| 2657 EXPECT_EQ(kInternalSpeaker.id, | 2736 EXPECT_EQ(kInternalSpeaker.id, |
| 2658 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 2737 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 2659 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); | 2738 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 2660 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); | 2739 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); |
| 2661 EXPECT_TRUE(test_observer_->output_mute_by_system()); | 2740 EXPECT_TRUE(test_observer_->output_mute_by_system()); |
| 2662 } | 2741 } |
| 2663 | 2742 |
| 2664 } // namespace chromeos | 2743 } // namespace chromeos |
| OLD | NEW |