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

Side by Side Diff: media/audio/android/audio_android_unittest.cc

Issue 131503006: Initialization of audio manager for Android is now done on the audio thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: now compiles Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 "base/android/build_info.h" 5 #include "base/android/build_info.h"
6 #include "base/basictypes.h" 6 #include "base/basictypes.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
12 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
13 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
14 #include "base/test/test_timeouts.h" 15 #include "base/test/test_timeouts.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "build/build_config.h" 17 #include "build/build_config.h"
17 #include "media/audio/android/audio_manager_android.h" 18 #include "media/audio/android/audio_manager_android.h"
18 #include "media/audio/audio_io.h" 19 #include "media/audio/audio_io.h"
19 #include "media/audio/audio_manager_base.h" 20 #include "media/audio/audio_manager_base.h"
20 #include "media/audio/mock_audio_source_callback.h" 21 #include "media/audio/mock_audio_source_callback.h"
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 scoped_ptr<media::SeekableBuffer> fifo_; 404 scoped_ptr<media::SeekableBuffer> fifo_;
404 scoped_ptr<uint8[]> buffer_; 405 scoped_ptr<uint8[]> buffer_;
405 bool started_; 406 bool started_;
406 407
407 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource); 408 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource);
408 }; 409 };
409 410
410 // Test fixture class for tests which only exercise the output path. 411 // Test fixture class for tests which only exercise the output path.
411 class AudioAndroidOutputTest : public testing::Test { 412 class AudioAndroidOutputTest : public testing::Test {
412 public: 413 public:
413 AudioAndroidOutputTest() {} 414 AudioAndroidOutputTest()
415 : loop_(new base::MessageLoopForUI()),
416 audio_manager_(AudioManager::CreateForTesting()),
417 audio_output_stream_(NULL) {
418 }
419
420 virtual ~AudioAndroidOutputTest() {
421 }
414 422
415 protected: 423 protected:
416 virtual void SetUp() { 424 AudioManager* audio_manager() { return audio_manager_.get(); }
417 audio_manager_.reset(AudioManager::CreateForTesting()); 425 base::MessageLoopForUI* loop() { return loop_.get(); }
418 loop_.reset(new base::MessageLoopForUI()); 426 const AudioParameters& audio_output_parameters() {
427 return audio_output_parameters_;
419 } 428 }
420 429
421 virtual void TearDown() {} 430 // Synchronously runs the provided callback/closure on the audio thread.
431 void RunOnAudioThread(const base::Closure& closure) {
432 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
433 base::WaitableEvent event(false, false);
434 audio_manager()->GetTaskRunner()->PostTask(
435 FROM_HERE,
436 base::Bind(&AudioAndroidOutputTest::RunOnAudioThreadImpl,
437 base::Unretained(this),
438 closure,
439 &event));
440 event.Wait();
441 } else {
442 closure.Run();
443 }
444 }
422 445
423 AudioManager* audio_manager() { return audio_manager_.get(); } 446 void RunOnAudioThreadImpl(const base::Closure& closure,
424 base::MessageLoopForUI* loop() { return loop_.get(); } 447 base::WaitableEvent* event) {
448 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
449 closure.Run();
450 event->Signal();
451 }
425 452
426 AudioParameters GetDefaultOutputStreamParameters() { 453 void GetDefaultOutputStreamParametersOnAudioThread() {
427 return audio_manager()->GetDefaultOutputStreamParameters(); 454 RunOnAudioThread(
455 base::Bind(&AudioAndroidOutputTest::GetDefaultOutputStreamParameters,
456 base::Unretained(this)));
457 }
458
459 void MakeAudioOutputStreamOnAudioThread(const AudioParameters& params) {
460 RunOnAudioThread(
461 base::Bind(&AudioAndroidOutputTest::MakeOutputStream,
462 base::Unretained(this),
463 params));
464 }
465
466 void OpenAndCloseAudioOutputStreamOnAudioThread() {
467 RunOnAudioThread(
468 base::Bind(&AudioAndroidOutputTest::OpenAndClose,
469 base::Unretained(this)));
470 }
471
472 void OpenAndStartAudioOutputStreamOnAudioThread(
473 AudioOutputStream::AudioSourceCallback* source) {
474 RunOnAudioThread(
475 base::Bind(&AudioAndroidOutputTest::OpenAndStart,
476 base::Unretained(this),
477 source));
478 }
479
480 void StopAndCloseAudioOutputStreamOnAudioThread() {
481 RunOnAudioThread(
482 base::Bind(&AudioAndroidOutputTest::StopAndClose,
483 base::Unretained(this)));
428 } 484 }
429 485
430 double AverageTimeBetweenCallbacks(int num_callbacks) const { 486 double AverageTimeBetweenCallbacks(int num_callbacks) const {
431 return ((end_time_ - start_time_) / static_cast<double>(num_callbacks - 1)) 487 return ((end_time_ - start_time_) / static_cast<double>(num_callbacks - 1))
432 .InMillisecondsF(); 488 .InMillisecondsF();
433 } 489 }
434 490
435 void StartOutputStreamCallbacks(const AudioParameters& params) { 491 void StartOutputStreamCallbacks(const AudioParameters& params) {
436 double expected_time_between_callbacks_ms = 492 double expected_time_between_callbacks_ms =
437 ExpectedTimeBetweenCallbacks(params); 493 ExpectedTimeBetweenCallbacks(params);
438 const int num_callbacks = 494 const int num_callbacks =
439 (kCallbackTestTimeMs / expected_time_between_callbacks_ms); 495 (kCallbackTestTimeMs / expected_time_between_callbacks_ms);
440 AudioOutputStream* stream = audio_manager()->MakeAudioOutputStream( 496 MakeAudioOutputStreamOnAudioThread(params);
441 params, std::string());
442 EXPECT_TRUE(stream);
443 497
444 int count = 0; 498 int count = 0;
445 MockAudioSourceCallback source; 499 MockAudioSourceCallback source;
446 500
447 EXPECT_CALL(source, OnMoreData(NotNull(), _)) 501 EXPECT_CALL(source, OnMoreData(NotNull(), _))
448 .Times(AtLeast(num_callbacks)) 502 .Times(AtLeast(num_callbacks))
449 .WillRepeatedly( 503 .WillRepeatedly(
450 DoAll(CheckCountAndPostQuitTask(&count, num_callbacks, loop()), 504 DoAll(CheckCountAndPostQuitTask(&count, num_callbacks, loop()),
451 Invoke(RealOnMoreData))); 505 Invoke(RealOnMoreData)));
452 EXPECT_CALL(source, OnError(stream)).Times(0); 506 EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);
453 EXPECT_CALL(source, OnMoreIOData(_, _, _)).Times(0); 507 EXPECT_CALL(source, OnMoreIOData(_, _, _)).Times(0);
454 508
455 EXPECT_TRUE(stream->Open()); 509 OpenAndStartAudioOutputStreamOnAudioThread(&source);
456 stream->Start(&source); 510
457 start_time_ = base::TimeTicks::Now(); 511 start_time_ = base::TimeTicks::Now();
458 loop()->Run(); 512 loop()->Run();
459 end_time_ = base::TimeTicks::Now(); 513 end_time_ = base::TimeTicks::Now();
460 stream->Stop(); 514
461 stream->Close(); 515 StopAndCloseAudioOutputStreamOnAudioThread();
462 516
463 double average_time_between_callbacks_ms = 517 double average_time_between_callbacks_ms =
464 AverageTimeBetweenCallbacks(num_callbacks); 518 AverageTimeBetweenCallbacks(num_callbacks);
465 VLOG(0) << "expected time between callbacks: " 519 VLOG(0) << "expected time between callbacks: "
466 << expected_time_between_callbacks_ms << " ms"; 520 << expected_time_between_callbacks_ms << " ms";
467 VLOG(0) << "average time between callbacks: " 521 VLOG(0) << "average time between callbacks: "
468 << average_time_between_callbacks_ms << " ms"; 522 << average_time_between_callbacks_ms << " ms";
469 EXPECT_GE(average_time_between_callbacks_ms, 523 EXPECT_GE(average_time_between_callbacks_ms,
470 0.70 * expected_time_between_callbacks_ms); 524 0.70 * expected_time_between_callbacks_ms);
471 EXPECT_LE(average_time_between_callbacks_ms, 525 EXPECT_LE(average_time_between_callbacks_ms,
472 1.30 * expected_time_between_callbacks_ms); 526 1.30 * expected_time_between_callbacks_ms);
473 } 527 }
474 528
529 void GetDefaultOutputStreamParameters() {
530 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
531 audio_output_parameters_ =
532 audio_manager()->GetDefaultOutputStreamParameters();
533 EXPECT_TRUE(audio_output_parameters_.IsValid());
534 }
535
536 void MakeOutputStream(const AudioParameters& params) {
537 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
538 audio_output_stream_ = audio_manager()->MakeAudioOutputStream(
539 params, std::string());
540 EXPECT_TRUE(audio_output_stream_);
541 }
542
543 void OpenAndClose() {
544 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
545 EXPECT_TRUE(audio_output_stream_->Open());
546 audio_output_stream_->Close();
547 audio_output_stream_ = NULL;
548 }
549
550 void OpenAndStart(AudioOutputStream::AudioSourceCallback* source) {
551 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
552 EXPECT_TRUE(audio_output_stream_->Open());
553 audio_output_stream_->Start(source);
554 }
555
556 void StopAndClose() {
557 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
558 audio_output_stream_->Stop();
559 audio_output_stream_->Close();
560 audio_output_stream_ = NULL;
561 }
562
475 scoped_ptr<base::MessageLoopForUI> loop_; 563 scoped_ptr<base::MessageLoopForUI> loop_;
476 scoped_ptr<AudioManager> audio_manager_; 564 scoped_ptr<AudioManager> audio_manager_;
565 AudioParameters audio_output_parameters_;
566 AudioOutputStream* audio_output_stream_;
477 base::TimeTicks start_time_; 567 base::TimeTicks start_time_;
478 base::TimeTicks end_time_; 568 base::TimeTicks end_time_;
479 569
480 private: 570 private:
481 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest); 571 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest);
482 }; 572 };
483 573
484 // AudioRecordInputStream should only be created on Jelly Bean and higher. This 574 // AudioRecordInputStream should only be created on Jelly Bean and higher. This
485 // ensures we only test against the AudioRecord path when that is satisfied. 575 // ensures we only test against the AudioRecord path when that is satisfied.
486 std::vector<bool> RunAudioRecordInputPathTests() { 576 std::vector<bool> RunAudioRecordInputPathTests() {
487 std::vector<bool> tests; 577 std::vector<bool> tests;
488 tests.push_back(false); 578 tests.push_back(false);
489 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 16) 579 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 16)
490 tests.push_back(true); 580 tests.push_back(true);
491 return tests; 581 return tests;
492 } 582 }
493 583
494 // Test fixture class for tests which exercise the input path, or both input and 584 // Test fixture class for tests which exercise the input path, or both input and
495 // output paths. It is value-parameterized to test against both the Java 585 // output paths. It is value-parameterized to test against both the Java
496 // AudioRecord (when true) and native OpenSLES (when false) input paths. 586 // AudioRecord (when true) and native OpenSLES (when false) input paths.
497 class AudioAndroidInputTest : public AudioAndroidOutputTest, 587 class AudioAndroidInputTest : public AudioAndroidOutputTest,
498 public testing::WithParamInterface<bool> { 588 public testing::WithParamInterface<bool> {
499 public: 589 public:
500 AudioAndroidInputTest() {} 590 AudioAndroidInputTest() : audio_input_stream_(NULL) {}
501 591
502 protected: 592 protected:
593 const AudioParameters& audio_input_parameters() {
594 return audio_input_parameters_;
595 }
596
503 AudioParameters GetInputStreamParameters() { 597 AudioParameters GetInputStreamParameters() {
504 AudioParameters input_params = audio_manager()->GetInputStreamParameters( 598 GetDefaultInputStreamParametersOnAudioThread();
505 AudioManagerBase::kDefaultDeviceId); 599
506 // Override the platform effects setting to use the AudioRecord or OpenSLES 600 // Override the platform effects setting to use the AudioRecord or OpenSLES
507 // path as requested. 601 // path as requested.
508 int effects = GetParam() ? AudioParameters::ECHO_CANCELLER : 602 int effects = GetParam() ? AudioParameters::ECHO_CANCELLER :
509 AudioParameters::NO_EFFECTS; 603 AudioParameters::NO_EFFECTS;
510 AudioParameters params(input_params.format(), 604 AudioParameters params(audio_input_parameters().format(),
511 input_params.channel_layout(), 605 audio_input_parameters().channel_layout(),
512 input_params.input_channels(), 606 audio_input_parameters().input_channels(),
513 input_params.sample_rate(), 607 audio_input_parameters().sample_rate(),
514 input_params.bits_per_sample(), 608 audio_input_parameters().bits_per_sample(),
515 input_params.frames_per_buffer(), 609 audio_input_parameters().frames_per_buffer(),
516 effects); 610 effects);
517 return params; 611 return params;
518 } 612 }
519 613
614 void GetDefaultInputStreamParametersOnAudioThread() {
615 RunOnAudioThread(
616 base::Bind(&AudioAndroidInputTest::GetDefaultInputStreamParameters,
617 base::Unretained(this)));
618 }
619
620 void MakeAudioInputStreamOnAudioThread(const AudioParameters& params) {
621 RunOnAudioThread(
622 base::Bind(&AudioAndroidInputTest::MakeInputStream,
623 base::Unretained(this),
624 params));
625 }
626
627 void OpenAndCloseAudioInputStreamOnAudioThread() {
628 RunOnAudioThread(
629 base::Bind(&AudioAndroidInputTest::OpenAndClose,
630 base::Unretained(this)));
631 }
632
633 void OpenAndStartAudioInputStreamOnAudioThread(
634 AudioInputStream::AudioInputCallback* sink) {
635 RunOnAudioThread(
636 base::Bind(&AudioAndroidInputTest::OpenAndStart,
637 base::Unretained(this),
638 sink));
639 }
640
641 void StopAndCloseAudioInputStreamOnAudioThread() {
642 RunOnAudioThread(
643 base::Bind(&AudioAndroidInputTest::StopAndClose,
644 base::Unretained(this)));
645 }
646
520 void StartInputStreamCallbacks(const AudioParameters& params) { 647 void StartInputStreamCallbacks(const AudioParameters& params) {
521 double expected_time_between_callbacks_ms = 648 double expected_time_between_callbacks_ms =
522 ExpectedTimeBetweenCallbacks(params); 649 ExpectedTimeBetweenCallbacks(params);
523 const int num_callbacks = 650 const int num_callbacks =
524 (kCallbackTestTimeMs / expected_time_between_callbacks_ms); 651 (kCallbackTestTimeMs / expected_time_between_callbacks_ms);
525 AudioInputStream* stream = audio_manager()->MakeAudioInputStream( 652
526 params, AudioManagerBase::kDefaultDeviceId); 653 MakeAudioInputStreamOnAudioThread(params);
527 EXPECT_TRUE(stream);
528 654
529 int count = 0; 655 int count = 0;
530 MockAudioInputCallback sink; 656 MockAudioInputCallback sink;
531 657
532 EXPECT_CALL(sink, 658 EXPECT_CALL(sink,
533 OnData(stream, NotNull(), params.GetBytesPerBuffer(), _, _)) 659 OnData(audio_input_stream_,
660 NotNull(),
661 params.
662 GetBytesPerBuffer(), _, _))
534 .Times(AtLeast(num_callbacks)) 663 .Times(AtLeast(num_callbacks))
535 .WillRepeatedly( 664 .WillRepeatedly(
536 CheckCountAndPostQuitTask(&count, num_callbacks, loop())); 665 CheckCountAndPostQuitTask(&count, num_callbacks, loop()));
537 EXPECT_CALL(sink, OnError(stream)).Times(0); 666 EXPECT_CALL(sink, OnError(audio_input_stream_)).Times(0);
538 667
539 EXPECT_TRUE(stream->Open()); 668 OpenAndStartAudioInputStreamOnAudioThread(&sink);
540 stream->Start(&sink); 669
541 start_time_ = base::TimeTicks::Now(); 670 start_time_ = base::TimeTicks::Now();
542 loop()->Run(); 671 loop()->Run();
543 end_time_ = base::TimeTicks::Now(); 672 end_time_ = base::TimeTicks::Now();
544 stream->Stop(); 673
545 stream->Close(); 674 StopAndCloseAudioInputStreamOnAudioThread();
546 675
547 double average_time_between_callbacks_ms = 676 double average_time_between_callbacks_ms =
548 AverageTimeBetweenCallbacks(num_callbacks); 677 AverageTimeBetweenCallbacks(num_callbacks);
549 VLOG(0) << "expected time between callbacks: " 678 VLOG(0) << "expected time between callbacks: "
550 << expected_time_between_callbacks_ms << " ms"; 679 << expected_time_between_callbacks_ms << " ms";
551 VLOG(0) << "average time between callbacks: " 680 VLOG(0) << "average time between callbacks: "
552 << average_time_between_callbacks_ms << " ms"; 681 << average_time_between_callbacks_ms << " ms";
553 EXPECT_GE(average_time_between_callbacks_ms, 682 EXPECT_GE(average_time_between_callbacks_ms,
554 0.70 * expected_time_between_callbacks_ms); 683 0.70 * expected_time_between_callbacks_ms);
555 EXPECT_LE(average_time_between_callbacks_ms, 684 EXPECT_LE(average_time_between_callbacks_ms,
556 1.30 * expected_time_between_callbacks_ms); 685 1.30 * expected_time_between_callbacks_ms);
557 } 686 }
558 687
688 void GetDefaultInputStreamParameters() {
689 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
690 audio_input_parameters_ = audio_manager()->GetInputStreamParameters(
691 AudioManagerBase::kDefaultDeviceId);
692 }
693
694 void MakeInputStream(const AudioParameters& params) {
695 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
696 audio_input_stream_ = audio_manager()->MakeAudioInputStream(
697 params, AudioManagerBase::kDefaultDeviceId);
698 EXPECT_TRUE(audio_input_stream_);
699 }
700
701 void OpenAndClose() {
702 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
703 EXPECT_TRUE(audio_input_stream_->Open());
704 audio_input_stream_->Close();
705 audio_input_stream_ = NULL;
706 }
707
708 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
709 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
710 EXPECT_TRUE(audio_input_stream_->Open());
711 audio_input_stream_->Start(sink);
712 }
713
714 void StopAndClose() {
715 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
716 audio_input_stream_->Stop();
717 audio_input_stream_->Close();
718 audio_input_stream_ = NULL;
719 }
720
721 AudioInputStream* audio_input_stream_;
722 AudioParameters audio_input_parameters_;
559 723
560 private: 724 private:
561 DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest); 725 DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest);
562 }; 726 };
563 727
564 // Get the default audio input parameters and log the result. 728 // Get the default audio input parameters and log the result.
565 TEST_P(AudioAndroidInputTest, GetDefaultInputStreamParameters) { 729 TEST_P(AudioAndroidInputTest, GetDefaultInputStreamParameters) {
566 // We don't go through AudioAndroidInputTest::GetInputStreamParameters() here 730 // We don't go through AudioAndroidInputTest::GetInputStreamParameters() here
567 // so that we can log the real (non-overridden) values of the effects. 731 // so that we can log the real (non-overridden) values of the effects.
568 AudioParameters params = audio_manager()->GetInputStreamParameters( 732 GetDefaultInputStreamParametersOnAudioThread();
569 AudioManagerBase::kDefaultDeviceId); 733 EXPECT_TRUE(audio_input_parameters().IsValid());
570 EXPECT_TRUE(params.IsValid()); 734 VLOG(1) << audio_input_parameters();
571 VLOG(1) << params;
572 } 735 }
573 736
574 // Get the default audio output parameters and log the result. 737 // Get the default audio output parameters and log the result.
575 TEST_F(AudioAndroidOutputTest, GetDefaultOutputStreamParameters) { 738 TEST_F(AudioAndroidOutputTest, GetDefaultOutputStreamParameters) {
576 AudioParameters params = GetDefaultOutputStreamParameters(); 739 GetDefaultOutputStreamParametersOnAudioThread();
577 EXPECT_TRUE(params.IsValid()); 740 VLOG(1) << audio_output_parameters();
578 VLOG(1) << params;
579 }
580
581 // Check if low-latency output is supported and log the result as output.
582 TEST_F(AudioAndroidOutputTest, IsAudioLowLatencySupported) {
583 AudioManagerAndroid* manager =
584 static_cast<AudioManagerAndroid*>(audio_manager());
585 bool low_latency = manager->IsAudioLowLatencySupported();
586 low_latency ? VLOG(0) << "Low latency output is supported"
587 : VLOG(0) << "Low latency output is *not* supported";
588 } 741 }
589 742
590 // Verify input device enumeration. 743 // Verify input device enumeration.
591 TEST_F(AudioAndroidInputTest, GetAudioInputDeviceNames) { 744 TEST_F(AudioAndroidInputTest, GetAudioInputDeviceNames) {
592 if (!audio_manager()->HasAudioInputDevices()) 745 if (!audio_manager()->HasAudioInputDevices())
593 return; 746 return;
594 AudioDeviceNames devices; 747 AudioDeviceNames devices;
595 audio_manager()->GetAudioInputDeviceNames(&devices); 748 RunOnAudioThread(
749 base::Bind(&AudioManager::GetAudioInputDeviceNames,
750 base::Unretained(audio_manager()),
751 &devices));
596 CheckDeviceNames(devices); 752 CheckDeviceNames(devices);
597 } 753 }
598 754
599 // Verify output device enumeration. 755 // Verify output device enumeration.
600 TEST_F(AudioAndroidOutputTest, GetAudioOutputDeviceNames) { 756 TEST_F(AudioAndroidOutputTest, GetAudioOutputDeviceNames) {
601 if (!audio_manager()->HasAudioOutputDevices()) 757 if (!audio_manager()->HasAudioOutputDevices())
602 return; 758 return;
603 AudioDeviceNames devices; 759 AudioDeviceNames devices;
604 audio_manager()->GetAudioOutputDeviceNames(&devices); 760 RunOnAudioThread(
761 base::Bind(&AudioManager::GetAudioOutputDeviceNames,
762 base::Unretained(audio_manager()),
763 &devices));
605 CheckDeviceNames(devices); 764 CheckDeviceNames(devices);
606 } 765 }
607 766
608 // Ensure that a default input stream can be created and closed. 767 // Ensure that a default input stream can be created and closed.
609 TEST_P(AudioAndroidInputTest, CreateAndCloseInputStream) { 768 TEST_P(AudioAndroidInputTest, CreateAndCloseInputStream) {
610 AudioParameters params = GetInputStreamParameters(); 769 AudioParameters params = GetInputStreamParameters();
611 AudioInputStream* ais = audio_manager()->MakeAudioInputStream( 770 MakeAudioInputStreamOnAudioThread(params);
612 params, AudioManagerBase::kDefaultDeviceId); 771 RunOnAudioThread(
613 EXPECT_TRUE(ais); 772 base::Bind(&AudioInputStream::Close,
614 ais->Close(); 773 base::Unretained(audio_input_stream_)));
615 } 774 }
616 775
617 // Ensure that a default output stream can be created and closed. 776 // Ensure that a default output stream can be created and closed.
618 // TODO(henrika): should we also verify that this API changes the audio mode 777 // TODO(henrika): should we also verify that this API changes the audio mode
619 // to communication mode, and calls RegisterHeadsetReceiver, the first time 778 // to communication mode, and calls RegisterHeadsetReceiver, the first time
620 // it is called? 779 // it is called?
621 TEST_F(AudioAndroidOutputTest, CreateAndCloseOutputStream) { 780 TEST_F(AudioAndroidOutputTest, CreateAndCloseOutputStream) {
622 AudioParameters params = GetDefaultOutputStreamParameters(); 781 GetDefaultOutputStreamParametersOnAudioThread();
623 AudioOutputStream* aos = audio_manager()->MakeAudioOutputStream( 782 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
624 params, std::string()); 783 RunOnAudioThread(
625 EXPECT_TRUE(aos); 784 base::Bind(&AudioOutputStream::Close,
626 aos->Close(); 785 base::Unretained(audio_output_stream_)));
627 } 786 }
628 787
629 // Ensure that a default input stream can be opened and closed. 788 // Ensure that a default input stream can be opened and closed.
630 TEST_P(AudioAndroidInputTest, OpenAndCloseInputStream) { 789 TEST_P(AudioAndroidInputTest, OpenAndCloseInputStream) {
631 AudioParameters params = GetInputStreamParameters(); 790 AudioParameters params = GetInputStreamParameters();
632 AudioInputStream* ais = audio_manager()->MakeAudioInputStream( 791 MakeAudioInputStreamOnAudioThread(params);
633 params, AudioManagerBase::kDefaultDeviceId); 792 OpenAndCloseAudioInputStreamOnAudioThread();
634 EXPECT_TRUE(ais);
635 EXPECT_TRUE(ais->Open());
636 ais->Close();
637 } 793 }
638 794
639 // Ensure that a default output stream can be opened and closed. 795 // Ensure that a default output stream can be opened and closed.
640 TEST_F(AudioAndroidOutputTest, OpenAndCloseOutputStream) { 796 TEST_F(AudioAndroidOutputTest, OpenAndCloseOutputStream) {
641 AudioParameters params = GetDefaultOutputStreamParameters(); 797 GetDefaultOutputStreamParametersOnAudioThread();
642 AudioOutputStream* aos = audio_manager()->MakeAudioOutputStream( 798 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
643 params, std::string()); 799 OpenAndCloseAudioOutputStreamOnAudioThread();
644 EXPECT_TRUE(aos);
645 EXPECT_TRUE(aos->Open());
646 aos->Close();
647 } 800 }
648 801
649 // Start input streaming using default input parameters and ensure that the 802 // Start input streaming using default input parameters and ensure that the
650 // callback sequence is sane. 803 // callback sequence is sane.
651 // Disabled per crbug/337867
652 TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacks) { 804 TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacks) {
653 AudioParameters params = GetInputStreamParameters(); 805 AudioParameters native_params = GetInputStreamParameters();
654 StartInputStreamCallbacks(params); 806 StartInputStreamCallbacks(native_params);
655 } 807 }
656 808
657 // Start input streaming using non default input parameters and ensure that the 809 // Start input streaming using non default input parameters and ensure that the
658 // callback sequence is sane. The only change we make in this test is to select 810 // callback sequence is sane. The only change we make in this test is to select
659 // a 10ms buffer size instead of the default size. 811 // a 10ms buffer size instead of the default size.
660 // TODO(henrika): possibly add support for more variations. 812 TEST_P(AudioAndroidInputTest,
661 // Disabled per crbug/337867 813 DISABLED_StartInputStreamCallbacksNonDefaultParameters) {
662 TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacksNonDefaultParame ters) {
663 AudioParameters native_params = GetInputStreamParameters(); 814 AudioParameters native_params = GetInputStreamParameters();
664 AudioParameters params(native_params.format(), 815 AudioParameters params(native_params.format(),
665 native_params.channel_layout(), 816 native_params.channel_layout(),
666 native_params.input_channels(), 817 native_params.input_channels(),
667 native_params.sample_rate(), 818 native_params.sample_rate(),
668 native_params.bits_per_sample(), 819 native_params.bits_per_sample(),
669 native_params.sample_rate() / 100, 820 native_params.sample_rate() / 100,
670 native_params.effects()); 821 native_params.effects());
671 StartInputStreamCallbacks(params); 822 StartInputStreamCallbacks(params);
672 } 823 }
673 824
674 // Start output streaming using default output parameters and ensure that the 825 // Start output streaming using default output parameters and ensure that the
675 // callback sequence is sane. 826 // callback sequence is sane.
676 TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacks) { 827 TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacks) {
677 AudioParameters params = GetDefaultOutputStreamParameters(); 828 GetDefaultOutputStreamParametersOnAudioThread();
678 StartOutputStreamCallbacks(params); 829 StartOutputStreamCallbacks(audio_output_parameters());
679 } 830 }
680 831
681 // Start output streaming using non default output parameters and ensure that 832 // Start output streaming using non default output parameters and ensure that
682 // the callback sequence is sane. The only change we make in this test is to 833 // the callback sequence is sane. The only change we make in this test is to
683 // select a 10ms buffer size instead of the default size and to open up the 834 // select a 10ms buffer size instead of the default size and to open up the
684 // device in mono. 835 // device in mono.
685 // TODO(henrika): possibly add support for more variations. 836 // TODO(henrika): possibly add support for more variations.
686 TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacksNonDefaultParameters) { 837 TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacksNonDefaultParameters) {
687 AudioParameters native_params = GetDefaultOutputStreamParameters(); 838 GetDefaultOutputStreamParametersOnAudioThread();
688 AudioParameters params(native_params.format(), 839 AudioParameters params(audio_output_parameters().format(),
689 CHANNEL_LAYOUT_MONO, 840 CHANNEL_LAYOUT_MONO,
690 native_params.sample_rate(), 841 audio_output_parameters().sample_rate(),
691 native_params.bits_per_sample(), 842 audio_output_parameters().bits_per_sample(),
692 native_params.sample_rate() / 100); 843 audio_output_parameters().sample_rate() / 100);
693 StartOutputStreamCallbacks(params); 844 StartOutputStreamCallbacks(params);
694 } 845 }
695 846
696 // Play out a PCM file segment in real time and allow the user to verify that 847 // Play out a PCM file segment in real time and allow the user to verify that
697 // the rendered audio sounds OK. 848 // the rendered audio sounds OK.
698 // NOTE: this test requires user interaction and is not designed to run as an 849 // NOTE: this test requires user interaction and is not designed to run as an
699 // automatized test on bots. 850 // automatized test on bots.
700 TEST_F(AudioAndroidOutputTest, DISABLED_RunOutputStreamWithFileAsSource) { 851 TEST_F(AudioAndroidOutputTest, DISABLED_RunOutputStreamWithFileAsSource) {
701 AudioParameters params = GetDefaultOutputStreamParameters(); 852 GetDefaultOutputStreamParametersOnAudioThread();
702 VLOG(1) << params; 853 VLOG(1) << audio_output_parameters();
703 AudioOutputStream* aos = audio_manager()->MakeAudioOutputStream( 854 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
704 params, std::string());
705 EXPECT_TRUE(aos);
706 855
707 std::string file_name; 856 std::string file_name;
857 const AudioParameters params = audio_output_parameters();
708 if (params.sample_rate() == 48000 && params.channels() == 2) { 858 if (params.sample_rate() == 48000 && params.channels() == 2) {
709 file_name = kSpeechFile_16b_s_48k; 859 file_name = kSpeechFile_16b_s_48k;
710 } else if (params.sample_rate() == 48000 && params.channels() == 1) { 860 } else if (params.sample_rate() == 48000 && params.channels() == 1) {
711 file_name = kSpeechFile_16b_m_48k; 861 file_name = kSpeechFile_16b_m_48k;
712 } else if (params.sample_rate() == 44100 && params.channels() == 2) { 862 } else if (params.sample_rate() == 44100 && params.channels() == 2) {
713 file_name = kSpeechFile_16b_s_44k; 863 file_name = kSpeechFile_16b_s_44k;
714 } else if (params.sample_rate() == 44100 && params.channels() == 1) { 864 } else if (params.sample_rate() == 44100 && params.channels() == 1) {
715 file_name = kSpeechFile_16b_m_44k; 865 file_name = kSpeechFile_16b_m_44k;
716 } else { 866 } else {
717 FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only."; 867 FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only.";
718 return; 868 return;
719 } 869 }
720 870
721 base::WaitableEvent event(false, false); 871 base::WaitableEvent event(false, false);
722 FileAudioSource source(&event, file_name); 872 FileAudioSource source(&event, file_name);
723 873
724 EXPECT_TRUE(aos->Open()); 874 OpenAndStartAudioOutputStreamOnAudioThread(&source);
725 aos->SetVolume(1.0);
726 aos->Start(&source);
727 VLOG(0) << ">> Verify that the file is played out correctly..."; 875 VLOG(0) << ">> Verify that the file is played out correctly...";
728 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout())); 876 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
729 aos->Stop(); 877 StopAndCloseAudioOutputStreamOnAudioThread();
730 aos->Close();
731 } 878 }
732 879
733 // Start input streaming and run it for ten seconds while recording to a 880 // Start input streaming and run it for ten seconds while recording to a
734 // local audio file. 881 // local audio file.
735 // NOTE: this test requires user interaction and is not designed to run as an 882 // NOTE: this test requires user interaction and is not designed to run as an
736 // automatized test on bots. 883 // automatized test on bots.
737 TEST_P(AudioAndroidInputTest, DISABLED_RunSimplexInputStreamWithFileAsSink) { 884 TEST_P(AudioAndroidInputTest, DISABLED_RunSimplexInputStreamWithFileAsSink) {
738 AudioParameters params = GetInputStreamParameters(); 885 AudioParameters params = GetInputStreamParameters();
739 VLOG(1) << params; 886 VLOG(1) << params;
740 AudioInputStream* ais = audio_manager()->MakeAudioInputStream( 887 MakeAudioInputStreamOnAudioThread(params);
741 params, AudioManagerBase::kDefaultDeviceId);
742 EXPECT_TRUE(ais);
743 888
744 std::string file_name = base::StringPrintf("out_simplex_%d_%d_%d.pcm", 889 std::string file_name = base::StringPrintf("out_simplex_%d_%d_%d.pcm",
745 params.sample_rate(), 890 params.sample_rate(),
746 params.frames_per_buffer(), 891 params.frames_per_buffer(),
747 params.channels()); 892 params.channels());
748 893
749 base::WaitableEvent event(false, false); 894 base::WaitableEvent event(false, false);
750 FileAudioSink sink(&event, params, file_name); 895 FileAudioSink sink(&event, params, file_name);
751 896
752 EXPECT_TRUE(ais->Open()); 897 OpenAndStartAudioInputStreamOnAudioThread(&sink);
753 ais->Start(&sink);
754 VLOG(0) << ">> Speak into the microphone to record audio..."; 898 VLOG(0) << ">> Speak into the microphone to record audio...";
755 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout())); 899 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
756 ais->Stop(); 900 StopAndCloseAudioInputStreamOnAudioThread();
757 ais->Close();
758 } 901 }
759 902
760 // Same test as RunSimplexInputStreamWithFileAsSink but this time output 903 // Same test as RunSimplexInputStreamWithFileAsSink but this time output
761 // streaming is active as well (reads zeros only). 904 // streaming is active as well (reads zeros only).
762 // NOTE: this test requires user interaction and is not designed to run as an 905 // NOTE: this test requires user interaction and is not designed to run as an
763 // automatized test on bots. 906 // automatized test on bots.
764 TEST_P(AudioAndroidInputTest, DISABLED_RunDuplexInputStreamWithFileAsSink) { 907 TEST_P(AudioAndroidInputTest, DISABLED_RunDuplexInputStreamWithFileAsSink) {
765 AudioParameters in_params = GetInputStreamParameters(); 908 AudioParameters in_params = GetInputStreamParameters();
766 AudioInputStream* ais = audio_manager()->MakeAudioInputStream( 909 VLOG(1) << in_params;
767 in_params, AudioManagerBase::kDefaultDeviceId); 910 MakeAudioInputStreamOnAudioThread(in_params);
768 EXPECT_TRUE(ais);
769 911
770 AudioParameters out_params = 912 GetDefaultOutputStreamParametersOnAudioThread();
771 audio_manager()->GetDefaultOutputStreamParameters(); 913 VLOG(1) << audio_output_parameters();
772 AudioOutputStream* aos = audio_manager()->MakeAudioOutputStream( 914 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
773 out_params, std::string());
774 EXPECT_TRUE(aos);
775 915
776 std::string file_name = base::StringPrintf("out_duplex_%d_%d_%d.pcm", 916 std::string file_name = base::StringPrintf("out_duplex_%d_%d_%d.pcm",
777 in_params.sample_rate(), 917 in_params.sample_rate(),
778 in_params.frames_per_buffer(), 918 in_params.frames_per_buffer(),
779 in_params.channels()); 919 in_params.channels());
780 920
781 base::WaitableEvent event(false, false); 921 base::WaitableEvent event(false, false);
782 FileAudioSink sink(&event, in_params, file_name); 922 FileAudioSink sink(&event, in_params, file_name);
783 MockAudioSourceCallback source; 923 MockAudioSourceCallback source;
784 924
785 EXPECT_CALL(source, OnMoreData(NotNull(), _)) 925 EXPECT_CALL(source, OnMoreData(NotNull(), _))
786 .WillRepeatedly(Invoke(RealOnMoreData)); 926 .WillRepeatedly(Invoke(RealOnMoreData));
787 EXPECT_CALL(source, OnError(aos)).Times(0); 927 EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);
788 EXPECT_CALL(source, OnMoreIOData(_, _, _)).Times(0); 928 EXPECT_CALL(source, OnMoreIOData(_, _, _)).Times(0);
789 929
790 EXPECT_TRUE(ais->Open()); 930 OpenAndStartAudioInputStreamOnAudioThread(&sink);
791 EXPECT_TRUE(aos->Open()); 931 OpenAndStartAudioOutputStreamOnAudioThread(&source);
792 ais->Start(&sink);
793 aos->Start(&source);
794 VLOG(0) << ">> Speak into the microphone to record audio"; 932 VLOG(0) << ">> Speak into the microphone to record audio";
795 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout())); 933 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
796 aos->Stop(); 934 StopAndCloseAudioOutputStreamOnAudioThread();
797 ais->Stop(); 935 StopAndCloseAudioInputStreamOnAudioThread();
798 aos->Close();
799 ais->Close();
800 } 936 }
801 937
802 // Start audio in both directions while feeding captured data into a FIFO so 938 // Start audio in both directions while feeding captured data into a FIFO so
803 // it can be read directly (in loopback) by the render side. A small extra 939 // it can be read directly (in loopback) by the render side. A small extra
804 // delay will be added by the FIFO and an estimate of this delay will be 940 // delay will be added by the FIFO and an estimate of this delay will be
805 // printed out during the test. 941 // printed out during the test.
806 // NOTE: this test requires user interaction and is not designed to run as an 942 // NOTE: this test requires user interaction and is not designed to run as an
807 // automatized test on bots. 943 // automatized test on bots.
808 TEST_P(AudioAndroidInputTest, 944 TEST_P(AudioAndroidInputTest,
809 DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex) { 945 DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex) {
810 // Get native audio parameters for the input side. 946 // Get native audio parameters for the input side.
811 AudioParameters default_input_params = GetInputStreamParameters(); 947 AudioParameters default_input_params = GetInputStreamParameters();
812 948
813 // Modify the parameters so that both input and output can use the same 949 // Modify the parameters so that both input and output can use the same
814 // parameters by selecting 10ms as buffer size. This will also ensure that 950 // parameters by selecting 10ms as buffer size. This will also ensure that
815 // the output stream will be a mono stream since mono is default for input 951 // the output stream will be a mono stream since mono is default for input
816 // audio on Android. 952 // audio on Android.
817 AudioParameters io_params(default_input_params.format(), 953 AudioParameters io_params(default_input_params.format(),
818 default_input_params.channel_layout(), 954 default_input_params.channel_layout(),
819 ChannelLayoutToChannelCount( 955 ChannelLayoutToChannelCount(
820 default_input_params.channel_layout()), 956 default_input_params.channel_layout()),
821 default_input_params.sample_rate(), 957 default_input_params.sample_rate(),
822 default_input_params.bits_per_sample(), 958 default_input_params.bits_per_sample(),
823 default_input_params.sample_rate() / 100, 959 default_input_params.sample_rate() / 100,
824 default_input_params.effects()); 960 default_input_params.effects());
825 VLOG(1) << io_params; 961 VLOG(1) << io_params;
826 962
827 // Create input and output streams using the common audio parameters. 963 // Create input and output streams using the common audio parameters.
828 AudioInputStream* ais = audio_manager()->MakeAudioInputStream( 964 MakeAudioInputStreamOnAudioThread(io_params);
829 io_params, AudioManagerBase::kDefaultDeviceId); 965 MakeAudioOutputStreamOnAudioThread(io_params);
830 EXPECT_TRUE(ais);
831 AudioOutputStream* aos = audio_manager()->MakeAudioOutputStream(
832 io_params, std::string());
833 EXPECT_TRUE(aos);
834 966
835 FullDuplexAudioSinkSource full_duplex(io_params); 967 FullDuplexAudioSinkSource full_duplex(io_params);
836 968
837 // Start a full duplex audio session and print out estimates of the extra 969 // Start a full duplex audio session and print out estimates of the extra
838 // delay we should expect from the FIFO. If real-time delay measurements are 970 // delay we should expect from the FIFO. If real-time delay measurements are
839 // performed, the result should be reduced by this extra delay since it is 971 // performed, the result should be reduced by this extra delay since it is
840 // something that has been added by the test. 972 // something that has been added by the test.
841 EXPECT_TRUE(ais->Open()); 973 OpenAndStartAudioInputStreamOnAudioThread(&full_duplex);
842 EXPECT_TRUE(aos->Open()); 974 OpenAndStartAudioOutputStreamOnAudioThread(&full_duplex);
843 ais->Start(&full_duplex);
844 aos->Start(&full_duplex);
845 VLOG(1) << "HINT: an estimate of the extra FIFO delay will be updated " 975 VLOG(1) << "HINT: an estimate of the extra FIFO delay will be updated "
846 << "once per second during this test."; 976 << "once per second during this test.";
847 VLOG(0) << ">> Speak into the mic and listen to the audio in loopback..."; 977 VLOG(0) << ">> Speak into the mic and listen to the audio in loopback...";
848 fflush(stdout); 978 fflush(stdout);
849 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20)); 979 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20));
850 printf("\n"); 980 printf("\n");
851 aos->Stop(); 981 StopAndCloseAudioOutputStreamOnAudioThread();
852 ais->Stop(); 982 StopAndCloseAudioInputStreamOnAudioThread();
853 aos->Close();
854 ais->Close();
855 } 983 }
856 984
857 INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest, 985 INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest,
858 testing::ValuesIn(RunAudioRecordInputPathTests())); 986 testing::ValuesIn(RunAudioRecordInputPathTests()));
859 987
860 } // namespace media 988 } // namespace media
OLDNEW
« no previous file with comments | « content/browser/renderer_host/render_message_filter.cc ('k') | media/audio/android/audio_manager_android.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698