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

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