Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: media/audio/audio_input_device_unittest.cc

Issue 23475037: Implement GetAudioOutputDevices for remaining platforms, consolidate tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Merge LKGR, remove anonymous namespaces. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/audio/android/audio_manager_android.cc ('k') | media/audio/audio_manager_base.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/environment.h" 5 #include "base/environment.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "media/audio/audio_manager.h" 8 #include "media/audio/audio_manager.h"
9 #include "media/audio/audio_manager_base.h" 9 #include "media/audio/audio_manager_base.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 #if defined(OS_LINUX)
13 #include "media/audio/linux/audio_manager_linux.h"
14 #endif // defined(OS_LINUX)
15
12 #if defined(OS_WIN) 16 #if defined(OS_WIN)
13 #include "base/win/scoped_com_initializer.h" 17 #include "base/win/scoped_com_initializer.h"
14 #include "media/audio/win/audio_manager_win.h" 18 #include "media/audio/win/audio_manager_win.h"
15 #include "media/audio/win/wavein_input_win.h" 19 #include "media/audio/win/wavein_input_win.h"
16 #endif 20 #endif
17 21
22 #if defined(USE_PULSEAUDIO)
23 #include "media/audio/pulse/audio_manager_pulse.h"
24 #endif // defined(USE_PULSEAUDIO)
25
18 namespace media { 26 namespace media {
19 27
20 // Test fixture which allows us to override the default enumeration API on 28 // Test fixture which allows us to override the default enumeration API on
21 // Windows. 29 // Windows.
22 class AudioInputDeviceTest 30 class AudioInputDeviceTest
23 : public ::testing::Test { 31 : public ::testing::Test {
24 protected: 32 protected:
25 AudioInputDeviceTest() 33 AudioInputDeviceTest()
26 : audio_manager_(AudioManager::Create()) 34 : audio_manager_(AudioManager::Create())
27 #if defined(OS_WIN) 35 #if defined(OS_WIN)
(...skipping 29 matching lines...) Expand all
57 scoped_ptr<PCMWaveInAudioInputStream> stream( 65 scoped_ptr<PCMWaveInAudioInputStream> stream(
58 static_cast<PCMWaveInAudioInputStream*>( 66 static_cast<PCMWaveInAudioInputStream*>(
59 amw->CreatePCMWaveInAudioInputStream(parameters, device_id))); 67 amw->CreatePCMWaveInAudioInputStream(parameters, device_id)));
60 return stream.get() ? stream->device_id_ : std::string(); 68 return stream.get() ? stream->device_id_ : std::string();
61 } 69 }
62 #endif 70 #endif
63 71
64 // Helper method which verifies that the device list starts with a valid 72 // Helper method which verifies that the device list starts with a valid
65 // default record followed by non-default device names. 73 // default record followed by non-default device names.
66 static void CheckDeviceNames(const AudioDeviceNames& device_names) { 74 static void CheckDeviceNames(const AudioDeviceNames& device_names) {
75 VLOG(2) << "Got " << device_names.size() << " audio devices.";
67 if (!device_names.empty()) { 76 if (!device_names.empty()) {
68 AudioDeviceNames::const_iterator it = device_names.begin(); 77 AudioDeviceNames::const_iterator it = device_names.begin();
69 78
70 // The first device in the list should always be the default device. 79 // The first device in the list should always be the default device.
71 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName), 80 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName),
72 it->device_name); 81 it->device_name);
73 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id); 82 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id);
74 ++it; 83 ++it;
75 84
76 // Other devices should have non-empty name and id and should not contain 85 // Other devices should have non-empty name and id and should not contain
77 // default name or id. 86 // default name or id.
78 while (it != device_names.end()) { 87 while (it != device_names.end()) {
79 EXPECT_FALSE(it->device_name.empty()); 88 EXPECT_FALSE(it->device_name.empty());
80 EXPECT_FALSE(it->unique_id.empty()); 89 EXPECT_FALSE(it->unique_id.empty());
90 VLOG(2) << "Device ID(" << it->unique_id
91 << "), label: " << it->device_name;
81 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName), 92 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName),
82 it->device_name); 93 it->device_name);
83 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId), 94 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId),
84 it->unique_id); 95 it->unique_id);
85 ++it; 96 ++it;
86 } 97 }
87 } else { 98 } else {
88 // Log a warning so we can see the status on the build bots. No need to 99 // Log a warning so we can see the status on the build bots. No need to
89 // break the test though since this does successfully test the code and 100 // break the test though since this does successfully test the code and
90 // some failure cases. 101 // some failure cases.
91 LOG(WARNING) << "No input devices detected"; 102 LOG(WARNING) << "No input devices detected";
92 } 103 }
93 } 104 }
94 105
95 bool CanRunAudioTest() { 106 bool CanRunInputTest() {
96 return audio_manager_->HasAudioInputDevices(); 107 return audio_manager_->HasAudioInputDevices();
97 } 108 }
98 109
110 bool CanRunOutputTest() {
111 return audio_manager_->HasAudioOutputDevices();
112 }
113
99 scoped_ptr<AudioManager> audio_manager_; 114 scoped_ptr<AudioManager> audio_manager_;
100 115
101 #if defined(OS_WIN) 116 #if defined(OS_WIN)
102 // The MMDevice API requires COM to be initialized on the current thread. 117 // The MMDevice API requires COM to be initialized on the current thread.
103 base::win::ScopedCOMInitializer com_init_; 118 base::win::ScopedCOMInitializer com_init_;
104 #endif 119 #endif
105 }; 120 };
106 121
107 // Test that devices can be enumerated. 122 // Test that devices can be enumerated.
108 TEST_F(AudioInputDeviceTest, EnumerateDevices) { 123 TEST_F(AudioInputDeviceTest, EnumerateInputDevices) {
109 if (!CanRunAudioTest()) 124 if (!CanRunInputTest())
110 return; 125 return;
111 126
112 AudioDeviceNames device_names; 127 AudioDeviceNames device_names;
113 audio_manager_->GetAudioInputDeviceNames(&device_names); 128 audio_manager_->GetAudioInputDeviceNames(&device_names);
114 CheckDeviceNames(device_names); 129 CheckDeviceNames(device_names);
115 } 130 }
116 131
132 // Test that devices can be enumerated.
133 TEST_F(AudioInputDeviceTest, EnumerateOutputDevices) {
134 if (!CanRunOutputTest())
135 return;
136
137 AudioDeviceNames device_names;
138 audio_manager_->GetAudioOutputDeviceNames(&device_names);
139 CheckDeviceNames(device_names);
140 }
141
117 // Run additional tests for Windows since enumeration can be done using 142 // Run additional tests for Windows since enumeration can be done using
118 // two different APIs. MMDevice is default for Vista and higher and Wave 143 // two different APIs. MMDevice is default for Vista and higher and Wave
119 // is default for XP and lower. 144 // is default for XP and lower.
120 #if defined(OS_WIN) 145 #if defined(OS_WIN)
121 146
122 // Override default enumeration API and force usage of Windows MMDevice. 147 // Override default enumeration API and force usage of Windows MMDevice.
123 // This test will only run on Windows Vista and higher. 148 // This test will only run on Windows Vista and higher.
124 TEST_F(AudioInputDeviceTest, EnumerateDevicesWinMMDevice) { 149 TEST_F(AudioInputDeviceTest, EnumerateInputDevicesWinMMDevice) {
125 if (!CanRunAudioTest()) 150 if (!CanRunInputTest())
126 return; 151 return;
127 152
128 AudioDeviceNames device_names; 153 AudioDeviceNames device_names;
129 if (!SetMMDeviceEnumeration()) { 154 if (!SetMMDeviceEnumeration()) {
130 // Usage of MMDevice will fail on XP and lower. 155 // Usage of MMDevice will fail on XP and lower.
131 LOG(WARNING) << "MM device enumeration is not supported."; 156 LOG(WARNING) << "MM device enumeration is not supported.";
132 return; 157 return;
133 } 158 }
134 audio_manager_->GetAudioInputDeviceNames(&device_names); 159 audio_manager_->GetAudioInputDeviceNames(&device_names);
135 CheckDeviceNames(device_names); 160 CheckDeviceNames(device_names);
136 } 161 }
137 162
163 TEST_F(AudioInputDeviceTest, EnumerateOutputDevicesWinMMDevice) {
164 if (!CanRunOutputTest())
165 return;
166
167 AudioDeviceNames device_names;
168 if (!SetMMDeviceEnumeration()) {
169 // Usage of MMDevice will fail on XP and lower.
170 LOG(WARNING) << "MM device enumeration is not supported.";
171 return;
172 }
173 audio_manager_->GetAudioOutputDeviceNames(&device_names);
174 CheckDeviceNames(device_names);
175 }
176
138 // Override default enumeration API and force usage of Windows Wave. 177 // Override default enumeration API and force usage of Windows Wave.
139 // This test will run on Windows XP, Windows Vista and Windows 7. 178 // This test will run on Windows XP, Windows Vista and Windows 7.
140 TEST_F(AudioInputDeviceTest, EnumerateDevicesWinWave) { 179 TEST_F(AudioInputDeviceTest, EnumerateInputDevicesWinWave) {
141 if (!CanRunAudioTest()) 180 if (!CanRunInputTest())
142 return; 181 return;
143 182
144 AudioDeviceNames device_names; 183 AudioDeviceNames device_names;
145 SetWaveEnumeration(); 184 SetWaveEnumeration();
146 audio_manager_->GetAudioInputDeviceNames(&device_names); 185 audio_manager_->GetAudioInputDeviceNames(&device_names);
147 CheckDeviceNames(device_names); 186 CheckDeviceNames(device_names);
148 } 187 }
149 188
189 TEST_F(AudioInputDeviceTest, EnumerateOutputDevicesWinWave) {
190 if (!CanRunOutputTest())
191 return;
192
193 AudioDeviceNames device_names;
194 SetWaveEnumeration();
195 audio_manager_->GetAudioOutputDeviceNames(&device_names);
196 CheckDeviceNames(device_names);
197 }
198
150 TEST_F(AudioInputDeviceTest, WinXPDeviceIdUnchanged) { 199 TEST_F(AudioInputDeviceTest, WinXPDeviceIdUnchanged) {
151 if (!CanRunAudioTest()) 200 if (!CanRunInputTest())
152 return; 201 return;
153 202
154 AudioDeviceNames xp_device_names; 203 AudioDeviceNames xp_device_names;
155 SetWaveEnumeration(); 204 SetWaveEnumeration();
156 audio_manager_->GetAudioInputDeviceNames(&xp_device_names); 205 audio_manager_->GetAudioInputDeviceNames(&xp_device_names);
157 CheckDeviceNames(xp_device_names); 206 CheckDeviceNames(xp_device_names);
158 207
159 // Device ID should remain unchanged, including the default device ID. 208 // Device ID should remain unchanged, including the default device ID.
160 for (AudioDeviceNames::iterator i = xp_device_names.begin(); 209 for (AudioDeviceNames::iterator i = xp_device_names.begin();
161 i != xp_device_names.end(); ++i) { 210 i != xp_device_names.end(); ++i) {
162 EXPECT_EQ(i->unique_id, 211 EXPECT_EQ(i->unique_id,
163 GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id)); 212 GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id));
164 } 213 }
165 } 214 }
166 215
167 TEST_F(AudioInputDeviceTest, ConvertToWinXPInputDeviceId) { 216 TEST_F(AudioInputDeviceTest, ConvertToWinXPInputDeviceId) {
168 if (!CanRunAudioTest()) 217 if (!CanRunInputTest())
169 return; 218 return;
170 219
171 if (!SetMMDeviceEnumeration()) { 220 if (!SetMMDeviceEnumeration()) {
172 // Usage of MMDevice will fail on XP and lower. 221 // Usage of MMDevice will fail on XP and lower.
173 LOG(WARNING) << "MM device enumeration is not supported."; 222 LOG(WARNING) << "MM device enumeration is not supported.";
174 return; 223 return;
175 } 224 }
176 225
177 AudioDeviceNames device_names; 226 AudioDeviceNames device_names;
178 audio_manager_->GetAudioInputDeviceNames(&device_names); 227 audio_manager_->GetAudioInputDeviceNames(&device_names);
179 CheckDeviceNames(device_names); 228 CheckDeviceNames(device_names);
180 229
181 for (AudioDeviceNames::iterator i = device_names.begin(); 230 for (AudioDeviceNames::iterator i = device_names.begin();
182 i != device_names.end(); ++i) { 231 i != device_names.end(); ++i) {
183 std::string converted_id = 232 std::string converted_id =
184 GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id); 233 GetDeviceIdFromPCMWaveInAudioInputStream(i->unique_id);
185 if (i == device_names.begin()) { 234 if (i == device_names.begin()) {
186 // The first in the list is the default device ID, which should not be 235 // The first in the list is the default device ID, which should not be
187 // changed when passed to PCMWaveInAudioInputStream. 236 // changed when passed to PCMWaveInAudioInputStream.
188 EXPECT_EQ(i->unique_id, converted_id); 237 EXPECT_EQ(i->unique_id, converted_id);
189 } else { 238 } else {
190 // MMDevice-style device IDs should be converted to WaveIn-style device 239 // MMDevice-style device IDs should be converted to WaveIn-style device
191 // IDs. 240 // IDs.
192 EXPECT_NE(i->unique_id, converted_id); 241 EXPECT_NE(i->unique_id, converted_id);
193 } 242 }
194 } 243 }
195 } 244 }
196 245
197 #endif 246 #endif // defined(OS_WIN)
247
248 #if defined(USE_PULSEAUDIO)
249 // On Linux, there are two implementations available and both can
250 // sometimes be tested on a single system. These tests specifically
251 // test Pulseaudio.
252
253 TEST_F(AudioInputDeviceTest, EnumerateInputDevicesPulseaudio) {
254 if (!CanRunInputTest())
255 return;
256
257 audio_manager_.reset(AudioManagerPulse::Create());
258 if (audio_manager_.get()) {
259 AudioDeviceNames device_names;
260 audio_manager_->GetAudioInputDeviceNames(&device_names);
261 CheckDeviceNames(device_names);
262 } else {
263 LOG(WARNING) << "No pulseaudio on this system.";
264 }
265 }
266
267 TEST_F(AudioInputDeviceTest, EnumerateOutputDevicesPulseaudio) {
268 if (!CanRunOutputTest())
269 return;
270
271 audio_manager_.reset(AudioManagerPulse::Create());
272 if (audio_manager_.get()) {
273 AudioDeviceNames device_names;
274 audio_manager_->GetAudioOutputDeviceNames(&device_names);
275 CheckDeviceNames(device_names);
276 } else {
277 LOG(WARNING) << "No pulseaudio on this system.";
278 }
279 }
280 #endif // defined(USE_PULSEAUDIO)
281
282 #if defined(USE_ALSA)
283 // On Linux, there are two implementations available and both can
284 // sometimes be tested on a single system. These tests specifically
285 // test Alsa.
286
287 TEST_F(AudioInputDeviceTest, EnumerateInputDevicesAlsa) {
288 if (!CanRunInputTest())
289 return;
290
291 VLOG(2) << "Testing AudioManagerLinux.";
292 audio_manager_.reset(new AudioManagerLinux());
293 AudioDeviceNames device_names;
294 audio_manager_->GetAudioInputDeviceNames(&device_names);
295 CheckDeviceNames(device_names);
296 }
297
298 TEST_F(AudioInputDeviceTest, EnumerateOutputDevicesAlsa) {
299 if (!CanRunOutputTest())
300 return;
301
302 VLOG(2) << "Testing AudioManagerLinux.";
303 audio_manager_.reset(new AudioManagerLinux());
304 AudioDeviceNames device_names;
305 audio_manager_->GetAudioOutputDeviceNames(&device_names);
306 CheckDeviceNames(device_names);
307 }
308 #endif // defined(USE_ALSA)
309
310 TEST_F(AudioInputDeviceTest, GetDefaultOutputStreamParameters) {
311 #if defined(OS_WIN) || defined(OS_MACOSX)
312 if (!CanRunInputTest())
313 return;
314
315 AudioParameters params = audio_manager_->GetDefaultOutputStreamParameters();
316 EXPECT_TRUE(params.IsValid());
317 #endif // defined(OS_WIN) || defined(OS_MACOSX)
318 }
319
320 TEST_F(AudioInputDeviceTest, GetAssociatedOutputDeviceID) {
321 #if defined(OS_WIN) || defined(OS_MACOSX)
322 if (!CanRunInputTest() || !CanRunOutputTest())
323 return;
324
325 AudioDeviceNames device_names;
326 audio_manager_->GetAudioInputDeviceNames(&device_names);
327 bool found_an_associated_device = false;
328 for (AudioDeviceNames::iterator it = device_names.begin();
329 it != device_names.end();
330 ++it) {
331 EXPECT_FALSE(it->unique_id.empty());
332 EXPECT_FALSE(it->device_name.empty());
333 std::string output_device_id(
334 audio_manager_->GetAssociatedOutputDeviceID(it->unique_id));
335 if (!output_device_id.empty()) {
336 VLOG(2) << it->unique_id << " matches with " << output_device_id;
337 found_an_associated_device = true;
338 }
339 }
340
341 EXPECT_TRUE(found_an_associated_device);
342 #endif // defined(OS_WIN) || defined(OS_MACOSX)
343 }
198 344
199 } // namespace media 345 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/android/audio_manager_android.cc ('k') | media/audio/audio_manager_base.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698