OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <windows.h> | 5 #include <windows.h> |
6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/environment.h" | 9 #include "base/environment.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 virtual void OnError(AudioInputStream* stream, int code) {} | 81 virtual void OnError(AudioInputStream* stream, int code) {} |
82 | 82 |
83 private: | 83 private: |
84 media::SeekableBuffer buffer_; | 84 media::SeekableBuffer buffer_; |
85 FILE* file_; | 85 FILE* file_; |
86 size_t bytes_to_write_; | 86 size_t bytes_to_write_; |
87 }; | 87 }; |
88 | 88 |
89 // Convenience method which ensures that we are not running on the build | 89 // Convenience method which ensures that we are not running on the build |
90 // bots and that at least one valid input device can be found. | 90 // bots and that at least one valid input device can be found. |
91 static bool CanRunAudioTests() { | 91 static bool CanRunAudioTests(AudioManager* audio_man) { |
92 scoped_ptr<base::Environment> env(base::Environment::Create()); | 92 scoped_ptr<base::Environment> env(base::Environment::Create()); |
93 if (env->HasVar("CHROME_HEADLESS")) | 93 if (env->HasVar("CHROME_HEADLESS")) |
94 return false; | 94 return false; |
95 AudioManager* audio_man = AudioManager::GetAudioManager(); | 95 |
96 if (NULL == audio_man) | |
97 return false; | |
98 // TODO(henrika): note that we use Wave today to query the number of | 96 // TODO(henrika): note that we use Wave today to query the number of |
99 // existing input devices. | 97 // existing input devices. |
100 return audio_man->HasAudioInputDevices(); | 98 return audio_man->HasAudioInputDevices(); |
101 } | 99 } |
102 | 100 |
103 // Convenience method which creates a default AudioInputStream object but | 101 // Convenience method which creates a default AudioInputStream object but |
104 // also allows the user to modify the default settings. | 102 // also allows the user to modify the default settings. |
105 class AudioInputStreamWrapper { | 103 class AudioInputStreamWrapper { |
106 public: | 104 public: |
107 AudioInputStreamWrapper() | 105 explicit AudioInputStreamWrapper(AudioManager* audio_manager) |
108 : com_init_(ScopedCOMInitializer::kMTA), | 106 : com_init_(ScopedCOMInitializer::kMTA), |
109 audio_man_(AudioManager::GetAudioManager()), | 107 audio_man_(audio_manager), |
110 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), | 108 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), |
111 channel_layout_(CHANNEL_LAYOUT_STEREO), | 109 channel_layout_(CHANNEL_LAYOUT_STEREO), |
112 bits_per_sample_(16) { | 110 bits_per_sample_(16) { |
113 // Use native/mixing sample rate and 10ms frame size as default. | 111 // Use native/mixing sample rate and 10ms frame size as default. |
114 sample_rate_ = static_cast<int>( | 112 sample_rate_ = static_cast<int>( |
115 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | 113 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); |
116 samples_per_packet_ = sample_rate_ / 100; | 114 samples_per_packet_ = sample_rate_ / 100; |
117 } | 115 } |
118 | 116 |
119 ~AudioInputStreamWrapper() {} | 117 ~AudioInputStreamWrapper() {} |
(...skipping 22 matching lines...) Expand all Loading... |
142 AudioInputStream* CreateInputStream() { | 140 AudioInputStream* CreateInputStream() { |
143 AudioInputStream* ais = audio_man_->MakeAudioInputStream( | 141 AudioInputStream* ais = audio_man_->MakeAudioInputStream( |
144 AudioParameters(format_, channel_layout_, sample_rate_, | 142 AudioParameters(format_, channel_layout_, sample_rate_, |
145 bits_per_sample_, samples_per_packet_), | 143 bits_per_sample_, samples_per_packet_), |
146 AudioManagerBase::kDefaultDeviceId); | 144 AudioManagerBase::kDefaultDeviceId); |
147 EXPECT_TRUE(ais); | 145 EXPECT_TRUE(ais); |
148 return ais; | 146 return ais; |
149 } | 147 } |
150 | 148 |
151 ScopedCOMInitializer com_init_; | 149 ScopedCOMInitializer com_init_; |
152 AudioManager* audio_man_; | 150 scoped_refptr<AudioManager> audio_man_; |
153 AudioParameters::Format format_; | 151 AudioParameters::Format format_; |
154 ChannelLayout channel_layout_; | 152 ChannelLayout channel_layout_; |
155 int bits_per_sample_; | 153 int bits_per_sample_; |
156 int sample_rate_; | 154 int sample_rate_; |
157 int samples_per_packet_; | 155 int samples_per_packet_; |
158 }; | 156 }; |
159 | 157 |
160 // Convenience method which creates a default AudioInputStream object. | 158 // Convenience method which creates a default AudioInputStream object. |
161 static AudioInputStream* CreateDefaultAudioInputStream() { | 159 static AudioInputStream* CreateDefaultAudioInputStream( |
162 AudioInputStreamWrapper aisw; | 160 AudioManager* audio_manager) { |
| 161 AudioInputStreamWrapper aisw(audio_manager); |
163 AudioInputStream* ais = aisw.Create(); | 162 AudioInputStream* ais = aisw.Create(); |
164 return ais; | 163 return ais; |
165 } | 164 } |
166 | 165 |
167 // Verify that we can retrieve the current hardware/mixing sample rate | 166 // Verify that we can retrieve the current hardware/mixing sample rate |
168 // for all supported device roles. The ERole enumeration defines constants | 167 // for all supported device roles. The ERole enumeration defines constants |
169 // that indicate the role that the system/user has assigned to an audio | 168 // that indicate the role that the system/user has assigned to an audio |
170 // endpoint device. | 169 // endpoint device. |
171 // TODO(henrika): modify this test when we suport full device enumeration. | 170 // TODO(henrika): modify this test when we suport full device enumeration. |
172 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { | 171 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { |
173 if (!CanRunAudioTests()) | 172 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 173 if (!CanRunAudioTests(audio_manager)) |
174 return; | 174 return; |
175 | 175 |
176 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); | 176 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); |
177 | 177 |
178 // Default device intended for games, system notification sounds, | 178 // Default device intended for games, system notification sounds, |
179 // and voice commands. | 179 // and voice commands. |
180 int fs = static_cast<int>( | 180 int fs = static_cast<int>( |
181 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | 181 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); |
182 EXPECT_GE(fs, 0); | 182 EXPECT_GE(fs, 0); |
183 | 183 |
184 // Default communication device intended for e.g. VoIP communication. | 184 // Default communication device intended for e.g. VoIP communication. |
185 fs = static_cast<int>( | 185 fs = static_cast<int>( |
186 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); | 186 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); |
187 EXPECT_GE(fs, 0); | 187 EXPECT_GE(fs, 0); |
188 | 188 |
189 // Multimedia device for music, movies and live music recording. | 189 // Multimedia device for music, movies and live music recording. |
190 fs = static_cast<int>( | 190 fs = static_cast<int>( |
191 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); | 191 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); |
192 EXPECT_GE(fs, 0); | 192 EXPECT_GE(fs, 0); |
193 } | 193 } |
194 | 194 |
195 // Test Create(), Close() calling sequence. | 195 // Test Create(), Close() calling sequence. |
196 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { | 196 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { |
197 if (!CanRunAudioTests()) | 197 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 198 if (!CanRunAudioTests(audio_manager)) |
198 return; | 199 return; |
199 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 200 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager); |
200 ais->Close(); | 201 ais->Close(); |
201 } | 202 } |
202 | 203 |
203 // Test Open(), Close() calling sequence. | 204 // Test Open(), Close() calling sequence. |
204 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { | 205 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { |
205 if (!CanRunAudioTests()) | 206 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 207 if (!CanRunAudioTests(audio_manager)) |
206 return; | 208 return; |
207 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 209 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager); |
208 EXPECT_TRUE(ais->Open()); | 210 EXPECT_TRUE(ais->Open()); |
209 ais->Close(); | 211 ais->Close(); |
210 } | 212 } |
211 | 213 |
212 // Test Open(), Start(), Close() calling sequence. | 214 // Test Open(), Start(), Close() calling sequence. |
213 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { | 215 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { |
214 if (!CanRunAudioTests()) | 216 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 217 if (!CanRunAudioTests(audio_manager)) |
215 return; | 218 return; |
216 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 219 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager); |
217 EXPECT_TRUE(ais->Open()); | 220 EXPECT_TRUE(ais->Open()); |
218 MockAudioInputCallback sink; | 221 MockAudioInputCallback sink; |
219 ais->Start(&sink); | 222 ais->Start(&sink); |
220 EXPECT_CALL(sink, OnClose(ais)) | 223 EXPECT_CALL(sink, OnClose(ais)) |
221 .Times(1); | 224 .Times(1); |
222 ais->Close(); | 225 ais->Close(); |
223 } | 226 } |
224 | 227 |
225 // Test Open(), Start(), Stop(), Close() calling sequence. | 228 // Test Open(), Start(), Stop(), Close() calling sequence. |
226 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { | 229 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { |
227 if (!CanRunAudioTests()) | 230 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 231 if (!CanRunAudioTests(audio_manager)) |
228 return; | 232 return; |
229 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 233 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager); |
230 EXPECT_TRUE(ais->Open()); | 234 EXPECT_TRUE(ais->Open()); |
231 MockAudioInputCallback sink; | 235 MockAudioInputCallback sink; |
232 ais->Start(&sink); | 236 ais->Start(&sink); |
233 ais->Stop(); | 237 ais->Stop(); |
234 EXPECT_CALL(sink, OnClose(ais)) | 238 EXPECT_CALL(sink, OnClose(ais)) |
235 .Times(1); | 239 .Times(1); |
236 ais->Close(); | 240 ais->Close(); |
237 } | 241 } |
238 | 242 |
239 // Test some additional calling sequences. | 243 // Test some additional calling sequences. |
240 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { | 244 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { |
241 if (!CanRunAudioTests()) | 245 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 246 if (!CanRunAudioTests(audio_manager)) |
242 return; | 247 return; |
243 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 248 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager); |
244 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); | 249 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); |
245 | 250 |
246 // Open(), Open() should fail the second time. | 251 // Open(), Open() should fail the second time. |
247 EXPECT_TRUE(ais->Open()); | 252 EXPECT_TRUE(ais->Open()); |
248 EXPECT_FALSE(ais->Open()); | 253 EXPECT_FALSE(ais->Open()); |
249 | 254 |
250 MockAudioInputCallback sink; | 255 MockAudioInputCallback sink; |
251 | 256 |
252 // Start(), Start() is a valid calling sequence (second call does nothing). | 257 // Start(), Start() is a valid calling sequence (second call does nothing). |
253 ais->Start(&sink); | 258 ais->Start(&sink); |
254 EXPECT_TRUE(wais->started()); | 259 EXPECT_TRUE(wais->started()); |
255 ais->Start(&sink); | 260 ais->Start(&sink); |
256 EXPECT_TRUE(wais->started()); | 261 EXPECT_TRUE(wais->started()); |
257 | 262 |
258 // Stop(), Stop() is a valid calling sequence (second call does nothing). | 263 // Stop(), Stop() is a valid calling sequence (second call does nothing). |
259 ais->Stop(); | 264 ais->Stop(); |
260 EXPECT_FALSE(wais->started()); | 265 EXPECT_FALSE(wais->started()); |
261 ais->Stop(); | 266 ais->Stop(); |
262 EXPECT_FALSE(wais->started()); | 267 EXPECT_FALSE(wais->started()); |
263 | 268 |
264 EXPECT_CALL(sink, OnClose(ais)) | 269 EXPECT_CALL(sink, OnClose(ais)) |
265 .Times(1); | 270 .Times(1); |
266 ais->Close(); | 271 ais->Close(); |
267 } | 272 } |
268 | 273 |
269 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { | 274 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { |
270 if (!CanRunAudioTests()) | 275 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 276 if (!CanRunAudioTests(audio_manager)) |
271 return; | 277 return; |
272 | 278 |
273 // 10 ms packet size. | 279 // 10 ms packet size. |
274 | 280 |
275 // Create default WASAPI input stream which records in stereo using | 281 // Create default WASAPI input stream which records in stereo using |
276 // the shared mixing rate. The default buffer size is 10ms. | 282 // the shared mixing rate. The default buffer size is 10ms. |
277 AudioInputStreamWrapper aisw; | 283 AudioInputStreamWrapper aisw(audio_manager); |
278 AudioInputStream* ais = aisw.Create(); | 284 AudioInputStream* ais = aisw.Create(); |
279 EXPECT_TRUE(ais->Open()); | 285 EXPECT_TRUE(ais->Open()); |
280 | 286 |
281 MockAudioInputCallback sink; | 287 MockAudioInputCallback sink; |
282 | 288 |
283 // Derive the expected size in bytes of each recorded packet. | 289 // Derive the expected size in bytes of each recorded packet. |
284 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | 290 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * |
285 (aisw.bits_per_sample() / 8); | 291 (aisw.bits_per_sample() / 8); |
286 | 292 |
287 // We use 10ms packets and will run the test for ~100ms. Given that the | 293 // We use 10ms packets and will run the test for ~100ms. Given that the |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 ais->Close(); | 346 ais->Close(); |
341 } | 347 } |
342 | 348 |
343 // This test is intended for manual tests and should only be enabled | 349 // This test is intended for manual tests and should only be enabled |
344 // when it is required to store the captured data on a local file. | 350 // when it is required to store the captured data on a local file. |
345 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. | 351 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. |
346 // To include disabled tests in test execution, just invoke the test program | 352 // To include disabled tests in test execution, just invoke the test program |
347 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS | 353 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS |
348 // environment variable to a value greater than 0. | 354 // environment variable to a value greater than 0. |
349 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { | 355 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { |
350 if (!CanRunAudioTests()) | 356 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 357 if (!CanRunAudioTests(audio_manager)) |
351 return; | 358 return; |
352 | 359 |
353 const char* file_name = "out_stereo_10sec.pcm"; | 360 const char* file_name = "out_stereo_10sec.pcm"; |
354 | 361 |
355 AudioInputStreamWrapper aisw; | 362 AudioInputStreamWrapper aisw(audio_manager); |
356 AudioInputStream* ais = aisw.Create(); | 363 AudioInputStream* ais = aisw.Create(); |
357 EXPECT_TRUE(ais->Open()); | 364 EXPECT_TRUE(ais->Open()); |
358 | 365 |
359 fprintf(stderr, " File name : %s\n", file_name); | 366 fprintf(stderr, " File name : %s\n", file_name); |
360 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); | 367 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); |
361 WriteToFileAudioSink file_sink(file_name); | 368 WriteToFileAudioSink file_sink(file_name); |
362 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 369 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
363 ais->Start(&file_sink); | 370 ais->Start(&file_sink); |
364 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | 371 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); |
365 ais->Stop(); | 372 ais->Stop(); |
366 fprintf(stderr, " >> Recording has stopped.\n"); | 373 fprintf(stderr, " >> Recording has stopped.\n"); |
367 ais->Close(); | 374 ais->Close(); |
368 } | 375 } |
OLD | NEW |