OLD | NEW |
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 <windows.h> | 5 #include <windows.h> |
6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/environment.h" | 10 #include "base/environment.h" |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 246 |
247 private: | 247 private: |
248 AudioInputStream* stream_; | 248 AudioInputStream* stream_; |
249 | 249 |
250 DISALLOW_COPY_AND_ASSIGN(ScopedAudioInputStream); | 250 DISALLOW_COPY_AND_ASSIGN(ScopedAudioInputStream); |
251 }; | 251 }; |
252 | 252 |
253 // Verify that we can retrieve the current hardware/mixing sample rate | 253 // Verify that we can retrieve the current hardware/mixing sample rate |
254 // for all available input devices. | 254 // for all available input devices. |
255 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { | 255 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { |
256 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 256 base::MessageLoop loop; |
| 257 ScopedAudioManagerPtr audio_manager( |
| 258 AudioManager::CreateForTesting(loop.task_runner())); |
257 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 259 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
258 | 260 |
259 // Retrieve a list of all available input devices. | 261 // Retrieve a list of all available input devices. |
260 media::AudioDeviceNames device_names; | 262 media::AudioDeviceNames device_names; |
261 audio_manager->GetAudioInputDeviceNames(&device_names); | 263 audio_manager->GetAudioInputDeviceNames(&device_names); |
262 | 264 |
263 // Scan all available input devices and repeat the same test for all of them. | 265 // Scan all available input devices and repeat the same test for all of them. |
264 for (media::AudioDeviceNames::const_iterator it = device_names.begin(); | 266 for (media::AudioDeviceNames::const_iterator it = device_names.begin(); |
265 it != device_names.end(); ++it) { | 267 it != device_names.end(); ++it) { |
266 // Retrieve the hardware sample rate given a specified audio input device. | 268 // Retrieve the hardware sample rate given a specified audio input device. |
267 AudioParameters params; | 269 AudioParameters params; |
268 ASSERT_TRUE(SUCCEEDED(CoreAudioUtil::GetPreferredAudioParameters( | 270 ASSERT_TRUE(SUCCEEDED(CoreAudioUtil::GetPreferredAudioParameters( |
269 it->unique_id, false, ¶ms))); | 271 it->unique_id, false, ¶ms))); |
270 EXPECT_GE(params.sample_rate(), 0); | 272 EXPECT_GE(params.sample_rate(), 0); |
271 } | 273 } |
272 } | 274 } |
273 | 275 |
274 // Test Create(), Close() calling sequence. | 276 // Test Create(), Close() calling sequence. |
275 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { | 277 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { |
276 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 278 base::MessageLoop loop; |
| 279 ScopedAudioManagerPtr audio_manager( |
| 280 AudioManager::CreateForTesting(loop.task_runner())); |
277 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 281 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
278 ScopedAudioInputStream ais( | 282 ScopedAudioInputStream ais( |
279 CreateDefaultAudioInputStream(audio_manager.get())); | 283 CreateDefaultAudioInputStream(audio_manager.get())); |
280 ais.Close(); | 284 ais.Close(); |
281 } | 285 } |
282 | 286 |
283 // Test Open(), Close() calling sequence. | 287 // Test Open(), Close() calling sequence. |
284 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { | 288 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { |
285 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 289 base::MessageLoop loop; |
| 290 ScopedAudioManagerPtr audio_manager( |
| 291 AudioManager::CreateForTesting(loop.task_runner())); |
286 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 292 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
287 ScopedAudioInputStream ais( | 293 ScopedAudioInputStream ais( |
288 CreateDefaultAudioInputStream(audio_manager.get())); | 294 CreateDefaultAudioInputStream(audio_manager.get())); |
289 EXPECT_TRUE(ais->Open()); | 295 EXPECT_TRUE(ais->Open()); |
290 ais.Close(); | 296 ais.Close(); |
291 } | 297 } |
292 | 298 |
293 // Test Open(), Start(), Close() calling sequence. | 299 // Test Open(), Start(), Close() calling sequence. |
294 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { | 300 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { |
295 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 301 base::MessageLoop loop; |
| 302 ScopedAudioManagerPtr audio_manager( |
| 303 AudioManager::CreateForTesting(loop.task_runner())); |
296 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 304 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
297 ScopedAudioInputStream ais( | 305 ScopedAudioInputStream ais( |
298 CreateDefaultAudioInputStream(audio_manager.get())); | 306 CreateDefaultAudioInputStream(audio_manager.get())); |
299 EXPECT_TRUE(ais->Open()); | 307 EXPECT_TRUE(ais->Open()); |
300 MockAudioInputCallback sink; | 308 MockAudioInputCallback sink; |
301 ais->Start(&sink); | 309 ais->Start(&sink); |
302 ais.Close(); | 310 ais.Close(); |
303 } | 311 } |
304 | 312 |
305 // Test Open(), Start(), Stop(), Close() calling sequence. | 313 // Test Open(), Start(), Stop(), Close() calling sequence. |
306 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { | 314 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { |
307 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 315 base::MessageLoop loop; |
| 316 ScopedAudioManagerPtr audio_manager( |
| 317 AudioManager::CreateForTesting(loop.task_runner())); |
308 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 318 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
309 ScopedAudioInputStream ais( | 319 ScopedAudioInputStream ais( |
310 CreateDefaultAudioInputStream(audio_manager.get())); | 320 CreateDefaultAudioInputStream(audio_manager.get())); |
311 EXPECT_TRUE(ais->Open()); | 321 EXPECT_TRUE(ais->Open()); |
312 MockAudioInputCallback sink; | 322 MockAudioInputCallback sink; |
313 ais->Start(&sink); | 323 ais->Start(&sink); |
314 ais->Stop(); | 324 ais->Stop(); |
315 ais.Close(); | 325 ais.Close(); |
316 } | 326 } |
317 | 327 |
318 // Test some additional calling sequences. | 328 // Test some additional calling sequences. |
319 TEST(WinAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { | 329 TEST(WinAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { |
320 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 330 base::MessageLoop loop; |
| 331 ScopedAudioManagerPtr audio_manager( |
| 332 AudioManager::CreateForTesting(loop.task_runner())); |
321 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 333 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
322 ScopedAudioInputStream ais( | 334 ScopedAudioInputStream ais( |
323 CreateDefaultAudioInputStream(audio_manager.get())); | 335 CreateDefaultAudioInputStream(audio_manager.get())); |
324 WASAPIAudioInputStream* wais = | 336 WASAPIAudioInputStream* wais = |
325 static_cast<WASAPIAudioInputStream*>(ais.get()); | 337 static_cast<WASAPIAudioInputStream*>(ais.get()); |
326 | 338 |
327 // Open(), Open() should fail the second time. | 339 // Open(), Open() should fail the second time. |
328 EXPECT_TRUE(ais->Open()); | 340 EXPECT_TRUE(ais->Open()); |
329 EXPECT_FALSE(ais->Open()); | 341 EXPECT_FALSE(ais->Open()); |
330 | 342 |
331 MockAudioInputCallback sink; | 343 MockAudioInputCallback sink; |
332 | 344 |
333 // Start(), Start() is a valid calling sequence (second call does nothing). | 345 // Start(), Start() is a valid calling sequence (second call does nothing). |
334 ais->Start(&sink); | 346 ais->Start(&sink); |
335 EXPECT_TRUE(wais->started()); | 347 EXPECT_TRUE(wais->started()); |
336 ais->Start(&sink); | 348 ais->Start(&sink); |
337 EXPECT_TRUE(wais->started()); | 349 EXPECT_TRUE(wais->started()); |
338 | 350 |
339 // Stop(), Stop() is a valid calling sequence (second call does nothing). | 351 // Stop(), Stop() is a valid calling sequence (second call does nothing). |
340 ais->Stop(); | 352 ais->Stop(); |
341 EXPECT_FALSE(wais->started()); | 353 EXPECT_FALSE(wais->started()); |
342 ais->Stop(); | 354 ais->Stop(); |
343 EXPECT_FALSE(wais->started()); | 355 EXPECT_FALSE(wais->started()); |
344 ais.Close(); | 356 ais.Close(); |
345 } | 357 } |
346 | 358 |
347 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { | 359 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { |
348 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 360 base::MessageLoopForUI loop; |
| 361 ScopedAudioManagerPtr audio_manager( |
| 362 AudioManager::CreateForTesting(loop.task_runner())); |
349 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 363 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
350 | 364 |
351 int count = 0; | 365 int count = 0; |
352 base::MessageLoopForUI loop; | |
353 | 366 |
354 // 10 ms packet size. | 367 // 10 ms packet size. |
355 | 368 |
356 // Create default WASAPI input stream which records in stereo using | 369 // Create default WASAPI input stream which records in stereo using |
357 // the shared mixing rate. The default buffer size is 10ms. | 370 // the shared mixing rate. The default buffer size is 10ms. |
358 AudioInputStreamWrapper aisw(audio_manager.get()); | 371 AudioInputStreamWrapper aisw(audio_manager.get()); |
359 ScopedAudioInputStream ais(aisw.Create()); | 372 ScopedAudioInputStream ais(aisw.Create()); |
360 EXPECT_TRUE(ais->Open()); | 373 EXPECT_TRUE(ais->Open()); |
361 | 374 |
362 MockAudioInputCallback sink; | 375 MockAudioInputCallback sink; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 .Times(AtLeast(10)) | 421 .Times(AtLeast(10)) |
409 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); | 422 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); |
410 ais->Start(&sink); | 423 ais->Start(&sink); |
411 loop.Run(); | 424 loop.Run(); |
412 ais->Stop(); | 425 ais->Stop(); |
413 ais.Close(); | 426 ais.Close(); |
414 } | 427 } |
415 | 428 |
416 // Test that we can capture a stream in loopback. | 429 // Test that we can capture a stream in loopback. |
417 TEST(WinAudioInputTest, WASAPIAudioInputStreamLoopback) { | 430 TEST(WinAudioInputTest, WASAPIAudioInputStreamLoopback) { |
418 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 431 base::MessageLoop loop; |
| 432 ScopedAudioManagerPtr audio_manager( |
| 433 AudioManager::CreateForTesting(loop.task_runner())); |
419 ABORT_AUDIO_TEST_IF_NOT(audio_manager->HasAudioOutputDevices() && | 434 ABORT_AUDIO_TEST_IF_NOT(audio_manager->HasAudioOutputDevices() && |
420 CoreAudioUtil::IsSupported()); | 435 CoreAudioUtil::IsSupported()); |
421 | 436 |
422 AudioParameters params = audio_manager->GetInputStreamParameters( | 437 AudioParameters params = audio_manager->GetInputStreamParameters( |
423 AudioManagerBase::kLoopbackInputDeviceId); | 438 AudioManagerBase::kLoopbackInputDeviceId); |
424 EXPECT_EQ(params.effects(), 0); | 439 EXPECT_EQ(params.effects(), 0); |
425 | 440 |
426 AudioParameters output_params = | 441 AudioParameters output_params = |
427 audio_manager->GetOutputStreamParameters(std::string()); | 442 audio_manager->GetOutputStreamParameters(std::string()); |
428 EXPECT_EQ(params.sample_rate(), output_params.sample_rate()); | 443 EXPECT_EQ(params.sample_rate(), output_params.sample_rate()); |
(...skipping 13 matching lines...) Expand all Loading... |
442 EXPECT_FALSE(sink.error()); | 457 EXPECT_FALSE(sink.error()); |
443 } | 458 } |
444 | 459 |
445 // This test is intended for manual tests and should only be enabled | 460 // This test is intended for manual tests and should only be enabled |
446 // when it is required to store the captured data on a local file. | 461 // when it is required to store the captured data on a local file. |
447 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. | 462 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. |
448 // To include disabled tests in test execution, just invoke the test program | 463 // To include disabled tests in test execution, just invoke the test program |
449 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS | 464 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS |
450 // environment variable to a value greater than 0. | 465 // environment variable to a value greater than 0. |
451 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { | 466 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { |
452 scoped_ptr<AudioManager> audio_manager(AudioManager::CreateForTesting()); | 467 base::MessageLoop loop; |
| 468 ScopedAudioManagerPtr audio_manager( |
| 469 AudioManager::CreateForTesting(loop.task_runner())); |
453 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); | 470 ABORT_AUDIO_TEST_IF_NOT(HasCoreAudioAndInputDevices(audio_manager.get())); |
454 | 471 |
455 // Name of the output PCM file containing captured data. The output file | 472 // Name of the output PCM file containing captured data. The output file |
456 // will be stored in the directory containing 'media_unittests.exe'. | 473 // will be stored in the directory containing 'media_unittests.exe'. |
457 // Example of full name: \src\build\Debug\out_stereo_10sec.pcm. | 474 // Example of full name: \src\build\Debug\out_stereo_10sec.pcm. |
458 const char* file_name = "out_stereo_10sec.pcm"; | 475 const char* file_name = "out_stereo_10sec.pcm"; |
459 | 476 |
460 AudioInputStreamWrapper aisw(audio_manager.get()); | 477 AudioInputStreamWrapper aisw(audio_manager.get()); |
461 ScopedAudioInputStream ais(aisw.Create()); | 478 ScopedAudioInputStream ais(aisw.Create()); |
462 EXPECT_TRUE(ais->Open()); | 479 EXPECT_TRUE(ais->Open()); |
463 | 480 |
464 VLOG(0) << ">> Sample rate: " << aisw.sample_rate() << " [Hz]"; | 481 VLOG(0) << ">> Sample rate: " << aisw.sample_rate() << " [Hz]"; |
465 WriteToFileAudioSink file_sink(file_name, aisw.bits_per_sample()); | 482 WriteToFileAudioSink file_sink(file_name, aisw.bits_per_sample()); |
466 VLOG(0) << ">> Speak into the default microphone while recording."; | 483 VLOG(0) << ">> Speak into the default microphone while recording."; |
467 ais->Start(&file_sink); | 484 ais->Start(&file_sink); |
468 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 485 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
469 ais->Stop(); | 486 ais->Stop(); |
470 VLOG(0) << ">> Recording has stopped."; | 487 VLOG(0) << ">> Recording has stopped."; |
471 ais.Close(); | 488 ais.Close(); |
472 } | 489 } |
473 | 490 |
474 } // namespace media | 491 } // namespace media |
OLD | NEW |