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
3 // found in the LICENSE file.
5 #include "base/android/build_info.h"
6 #include "base/basictypes.h"
7 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/synchronization/lock.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/test/test_timeouts.h"
16 #include "base/time/time.h"
17 #include "build/build_config.h"
18 #include "media/audio/android/audio_manager_android.h"
19 #include "media/audio/audio_io.h"
20 #include "media/audio/audio_manager_base.h"
21 #include "media/audio/mock_audio_source_callback.h"
22 #include "media/base/decoder_buffer.h"
23 #include "media/base/seekable_buffer.h"
24 #include "media/base/test_data_util.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
29 using ::testing::AtLeast
;
30 using ::testing::DoAll
;
31 using ::testing::Invoke
;
32 using ::testing::NotNull
;
33 using ::testing::Return
;
37 ACTION_P3(CheckCountAndPostQuitTask
, count
, limit
, loop
) {
38 if (++*count
>= limit
) {
39 loop
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
43 static const char kSpeechFile_16b_s_48k
[] = "speech_16b_stereo_48kHz.raw";
44 static const char kSpeechFile_16b_m_48k
[] = "speech_16b_mono_48kHz.raw";
45 static const char kSpeechFile_16b_s_44k
[] = "speech_16b_stereo_44kHz.raw";
46 static const char kSpeechFile_16b_m_44k
[] = "speech_16b_mono_44kHz.raw";
48 static const float kCallbackTestTimeMs
= 2000.0;
49 static const int kBitsPerSample
= 16;
50 static const int kBytesPerSample
= kBitsPerSample
/ 8;
52 // Converts AudioParameters::Format enumerator to readable string.
53 static std::string
FormatToString(AudioParameters::Format format
) {
55 case AudioParameters::AUDIO_PCM_LINEAR
:
56 return std::string("AUDIO_PCM_LINEAR");
57 case AudioParameters::AUDIO_PCM_LOW_LATENCY
:
58 return std::string("AUDIO_PCM_LOW_LATENCY");
59 case AudioParameters::AUDIO_FAKE
:
60 return std::string("AUDIO_FAKE");
61 case AudioParameters::AUDIO_LAST_FORMAT
:
62 return std::string("AUDIO_LAST_FORMAT");
68 // Converts ChannelLayout enumerator to readable string. Does not include
69 // multi-channel cases since these layouts are not supported on Android.
70 static std::string
LayoutToString(ChannelLayout channel_layout
) {
71 switch (channel_layout
) {
72 case CHANNEL_LAYOUT_NONE
:
73 return std::string("CHANNEL_LAYOUT_NONE");
74 case CHANNEL_LAYOUT_MONO
:
75 return std::string("CHANNEL_LAYOUT_MONO");
76 case CHANNEL_LAYOUT_STEREO
:
77 return std::string("CHANNEL_LAYOUT_STEREO");
78 case CHANNEL_LAYOUT_UNSUPPORTED
:
80 return std::string("CHANNEL_LAYOUT_UNSUPPORTED");
84 static double ExpectedTimeBetweenCallbacks(AudioParameters params
) {
85 return (base::TimeDelta::FromMicroseconds(
86 params
.frames_per_buffer() * base::Time::kMicrosecondsPerSecond
/
87 static_cast<double>(params
.sample_rate()))).InMillisecondsF();
90 // Helper method which verifies that the device list starts with a valid
91 // default device name followed by non-default device names.
92 static void CheckDeviceNames(const AudioDeviceNames
& device_names
) {
93 VLOG(2) << "Got " << device_names
.size() << " audio devices.";
94 if (device_names
.empty()) {
95 // Log a warning so we can see the status on the build bots. No need to
96 // break the test though since this does successfully test the code and
97 // some failure cases.
98 LOG(WARNING
) << "No input devices detected";
102 AudioDeviceNames::const_iterator it
= device_names
.begin();
104 // The first device in the list should always be the default device.
105 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName
),
107 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId
), it
->unique_id
);
110 // Other devices should have non-empty name and id and should not contain
111 // default name or id.
112 while (it
!= device_names
.end()) {
113 EXPECT_FALSE(it
->device_name
.empty());
114 EXPECT_FALSE(it
->unique_id
.empty());
115 VLOG(2) << "Device ID(" << it
->unique_id
116 << "), label: " << it
->device_name
;
117 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName
),
119 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId
),
125 // We clear the data bus to ensure that the test does not cause noise.
126 static int RealOnMoreData(AudioBus
* dest
, AudioBuffersState buffers_state
) {
128 return dest
->frames();
131 std::ostream
& operator<<(std::ostream
& os
, const AudioParameters
& params
) {
133 os
<< endl
<< "format: " << FormatToString(params
.format()) << endl
134 << "channel layout: " << LayoutToString(params
.channel_layout()) << endl
135 << "sample rate: " << params
.sample_rate() << endl
136 << "bits per sample: " << params
.bits_per_sample() << endl
137 << "frames per buffer: " << params
.frames_per_buffer() << endl
138 << "channels: " << params
.channels() << endl
139 << "bytes per buffer: " << params
.GetBytesPerBuffer() << endl
140 << "bytes per second: " << params
.GetBytesPerSecond() << endl
141 << "bytes per frame: " << params
.GetBytesPerFrame() << endl
142 << "chunk size in ms: " << ExpectedTimeBetweenCallbacks(params
) << endl
143 << "echo_canceller: "
144 << (params
.effects() & AudioParameters::ECHO_CANCELLER
);
148 // Gmock implementation of AudioInputStream::AudioInputCallback.
149 class MockAudioInputCallback
: public AudioInputStream::AudioInputCallback
{
152 void(AudioInputStream
* stream
,
155 uint32 hardware_delay_bytes
,
157 MOCK_METHOD1(OnError
, void(AudioInputStream
* stream
));
160 // Implements AudioOutputStream::AudioSourceCallback and provides audio data
161 // by reading from a data file.
162 class FileAudioSource
: public AudioOutputStream::AudioSourceCallback
{
164 explicit FileAudioSource(base::WaitableEvent
* event
, const std::string
& name
)
165 : event_(event
), pos_(0) {
166 // Reads a test file from media/test/data directory and stores it in
168 file_
= ReadTestDataFile(name
);
170 // Log the name of the file which is used as input for this test.
171 base::FilePath file_path
= GetTestDataFilePath(name
);
172 VLOG(0) << "Reading from file: " << file_path
.value().c_str();
175 virtual ~FileAudioSource() {}
177 // AudioOutputStream::AudioSourceCallback implementation.
179 // Use samples read from a data file and fill up the audio buffer
180 // provided to us in the callback.
181 virtual int OnMoreData(AudioBus
* audio_bus
,
182 AudioBuffersState buffers_state
) OVERRIDE
{
183 bool stop_playing
= false;
185 audio_bus
->frames() * audio_bus
->channels() * kBytesPerSample
;
187 // Adjust data size and prepare for end signal if file has ended.
188 if (pos_
+ max_size
> file_size()) {
190 max_size
= file_size() - pos_
;
193 // File data is stored as interleaved 16-bit values. Copy data samples from
194 // the file and deinterleave to match the audio bus format.
195 // FromInterleaved() will zero out any unfilled frames when there is not
196 // sufficient data remaining in the file to fill up the complete frame.
197 int frames
= max_size
/ (audio_bus
->channels() * kBytesPerSample
);
199 audio_bus
->FromInterleaved(file_
->data() + pos_
, frames
, kBytesPerSample
);
203 // Set event to ensure that the test can stop when the file has ended.
210 virtual void OnError(AudioOutputStream
* stream
) OVERRIDE
{}
212 int file_size() { return file_
->data_size(); }
215 base::WaitableEvent
* event_
;
217 scoped_refptr
<DecoderBuffer
> file_
;
219 DISALLOW_COPY_AND_ASSIGN(FileAudioSource
);
222 // Implements AudioInputStream::AudioInputCallback and writes the recorded
223 // audio data to a local output file. Note that this implementation should
224 // only be used for manually invoked and evaluated tests, hence the created
225 // file will not be destroyed after the test is done since the intention is
226 // that it shall be available for off-line analysis.
227 class FileAudioSink
: public AudioInputStream::AudioInputCallback
{
229 explicit FileAudioSink(base::WaitableEvent
* event
,
230 const AudioParameters
& params
,
231 const std::string
& file_name
)
232 : event_(event
), params_(params
) {
233 // Allocate space for ~10 seconds of data.
234 const int kMaxBufferSize
= 10 * params
.GetBytesPerSecond();
235 buffer_
.reset(new media::SeekableBuffer(0, kMaxBufferSize
));
237 // Open up the binary file which will be written to in the destructor.
238 base::FilePath file_path
;
239 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &file_path
));
240 file_path
= file_path
.AppendASCII(file_name
.c_str());
241 binary_file_
= base::OpenFile(file_path
, "wb");
242 DLOG_IF(ERROR
, !binary_file_
) << "Failed to open binary PCM data file.";
243 VLOG(0) << "Writing to file: " << file_path
.value().c_str();
246 virtual ~FileAudioSink() {
247 int bytes_written
= 0;
248 while (bytes_written
< buffer_
->forward_capacity()) {
252 // Stop writing if no more data is available.
253 if (!buffer_
->GetCurrentChunk(&chunk
, &chunk_size
))
256 // Write recorded data chunk to the file and prepare for next chunk.
257 // TODO(henrika): use file_util:: instead.
258 fwrite(chunk
, 1, chunk_size
, binary_file_
);
259 buffer_
->Seek(chunk_size
);
260 bytes_written
+= chunk_size
;
262 base::CloseFile(binary_file_
);
265 // AudioInputStream::AudioInputCallback implementation.
266 virtual void OnData(AudioInputStream
* stream
,
269 uint32 hardware_delay_bytes
,
270 double volume
) OVERRIDE
{
271 // Store data data in a temporary buffer to avoid making blocking
272 // fwrite() calls in the audio callback. The complete buffer will be
273 // written to file in the destructor.
274 if (!buffer_
->Append(src
, size
))
278 virtual void OnError(AudioInputStream
* stream
) OVERRIDE
{}
281 base::WaitableEvent
* event_
;
282 AudioParameters params_
;
283 scoped_ptr
<media::SeekableBuffer
> buffer_
;
286 DISALLOW_COPY_AND_ASSIGN(FileAudioSink
);
289 // Implements AudioInputCallback and AudioSourceCallback to support full
290 // duplex audio where captured samples are played out in loopback after
291 // reading from a temporary FIFO storage.
292 class FullDuplexAudioSinkSource
293 : public AudioInputStream::AudioInputCallback
,
294 public AudioOutputStream::AudioSourceCallback
{
296 explicit FullDuplexAudioSinkSource(const AudioParameters
& params
)
298 previous_time_(base::TimeTicks::Now()),
300 // Start with a reasonably small FIFO size. It will be increased
301 // dynamically during the test if required.
302 fifo_
.reset(new media::SeekableBuffer(0, 2 * params
.GetBytesPerBuffer()));
303 buffer_
.reset(new uint8
[params_
.GetBytesPerBuffer()]);
306 virtual ~FullDuplexAudioSinkSource() {}
308 // AudioInputStream::AudioInputCallback implementation
309 virtual void OnData(AudioInputStream
* stream
,
312 uint32 hardware_delay_bytes
,
313 double volume
) OVERRIDE
{
314 const base::TimeTicks now_time
= base::TimeTicks::Now();
315 const int diff
= (now_time
- previous_time_
).InMilliseconds();
317 base::AutoLock
lock(lock_
);
320 previous_time_
= now_time
;
322 // Log out the extra delay added by the FIFO. This is a best effort
323 // estimate. We might be +- 10ms off here.
324 int extra_fifo_delay
=
325 static_cast<int>(BytesToMilliseconds(fifo_
->forward_bytes() + size
));
326 DVLOG(1) << extra_fifo_delay
;
329 // We add an initial delay of ~1 second before loopback starts to ensure
330 // a stable callback sequence and to avoid initial bursts which might add
331 // to the extra FIFO delay.
335 // Append new data to the FIFO and extend the size if the max capacity
336 // was exceeded. Flush the FIFO when extended just in case.
337 if (!fifo_
->Append(src
, size
)) {
338 fifo_
->set_forward_capacity(2 * fifo_
->forward_capacity());
343 virtual void OnError(AudioInputStream
* stream
) OVERRIDE
{}
345 // AudioOutputStream::AudioSourceCallback implementation
346 virtual int OnMoreData(AudioBus
* dest
,
347 AudioBuffersState buffers_state
) OVERRIDE
{
348 const int size_in_bytes
=
349 (params_
.bits_per_sample() / 8) * dest
->frames() * dest
->channels();
350 EXPECT_EQ(size_in_bytes
, params_
.GetBytesPerBuffer());
352 base::AutoLock
lock(lock_
);
354 // We add an initial delay of ~1 second before loopback starts to ensure
355 // a stable callback sequences and to avoid initial bursts which might add
356 // to the extra FIFO delay.
359 return dest
->frames();
362 // Fill up destination with zeros if the FIFO does not contain enough
363 // data to fulfill the request.
364 if (fifo_
->forward_bytes() < size_in_bytes
) {
367 fifo_
->Read(buffer_
.get(), size_in_bytes
);
368 dest
->FromInterleaved(
369 buffer_
.get(), dest
->frames(), params_
.bits_per_sample() / 8);
372 return dest
->frames();
375 virtual void OnError(AudioOutputStream
* stream
) OVERRIDE
{}
378 // Converts from bytes to milliseconds given number of bytes and existing
380 double BytesToMilliseconds(int bytes
) const {
381 const int frames
= bytes
/ params_
.GetBytesPerFrame();
382 return (base::TimeDelta::FromMicroseconds(
383 frames
* base::Time::kMicrosecondsPerSecond
/
384 static_cast<double>(params_
.sample_rate()))).InMillisecondsF();
387 AudioParameters params_
;
388 base::TimeTicks previous_time_
;
390 scoped_ptr
<media::SeekableBuffer
> fifo_
;
391 scoped_ptr
<uint8
[]> buffer_
;
394 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource
);
397 // Test fixture class for tests which only exercise the output path.
398 class AudioAndroidOutputTest
: public testing::Test
{
400 AudioAndroidOutputTest()
401 : loop_(new base::MessageLoopForUI()),
402 audio_manager_(AudioManager::CreateForTesting()),
403 audio_output_stream_(NULL
) {
406 virtual ~AudioAndroidOutputTest() {
410 AudioManager
* audio_manager() { return audio_manager_
.get(); }
411 base::MessageLoopForUI
* loop() { return loop_
.get(); }
412 const AudioParameters
& audio_output_parameters() {
413 return audio_output_parameters_
;
416 // Synchronously runs the provided callback/closure on the audio thread.
417 void RunOnAudioThread(const base::Closure
& closure
) {
418 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
419 base::WaitableEvent
event(false, false);
420 audio_manager()->GetTaskRunner()->PostTask(
422 base::Bind(&AudioAndroidOutputTest::RunOnAudioThreadImpl
,
423 base::Unretained(this),
432 void RunOnAudioThreadImpl(const base::Closure
& closure
,
433 base::WaitableEvent
* event
) {
434 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
439 void GetDefaultOutputStreamParametersOnAudioThread() {
441 base::Bind(&AudioAndroidOutputTest::GetDefaultOutputStreamParameters
,
442 base::Unretained(this)));
445 void MakeAudioOutputStreamOnAudioThread(const AudioParameters
& params
) {
447 base::Bind(&AudioAndroidOutputTest::MakeOutputStream
,
448 base::Unretained(this),
452 void OpenAndCloseAudioOutputStreamOnAudioThread() {
454 base::Bind(&AudioAndroidOutputTest::OpenAndClose
,
455 base::Unretained(this)));
458 void OpenAndStartAudioOutputStreamOnAudioThread(
459 AudioOutputStream::AudioSourceCallback
* source
) {
461 base::Bind(&AudioAndroidOutputTest::OpenAndStart
,
462 base::Unretained(this),
466 void StopAndCloseAudioOutputStreamOnAudioThread() {
468 base::Bind(&AudioAndroidOutputTest::StopAndClose
,
469 base::Unretained(this)));
472 double AverageTimeBetweenCallbacks(int num_callbacks
) const {
473 return ((end_time_
- start_time_
) / static_cast<double>(num_callbacks
- 1))
477 void StartOutputStreamCallbacks(const AudioParameters
& params
) {
478 double expected_time_between_callbacks_ms
=
479 ExpectedTimeBetweenCallbacks(params
);
480 const int num_callbacks
=
481 (kCallbackTestTimeMs
/ expected_time_between_callbacks_ms
);
482 MakeAudioOutputStreamOnAudioThread(params
);
485 MockAudioSourceCallback source
;
487 EXPECT_CALL(source
, OnMoreData(NotNull(), _
))
488 .Times(AtLeast(num_callbacks
))
490 DoAll(CheckCountAndPostQuitTask(&count
, num_callbacks
, loop()),
491 Invoke(RealOnMoreData
)));
492 EXPECT_CALL(source
, OnError(audio_output_stream_
)).Times(0);
494 OpenAndStartAudioOutputStreamOnAudioThread(&source
);
496 start_time_
= base::TimeTicks::Now();
498 end_time_
= base::TimeTicks::Now();
500 StopAndCloseAudioOutputStreamOnAudioThread();
502 double average_time_between_callbacks_ms
=
503 AverageTimeBetweenCallbacks(num_callbacks
);
504 VLOG(0) << "expected time between callbacks: "
505 << expected_time_between_callbacks_ms
<< " ms";
506 VLOG(0) << "average time between callbacks: "
507 << average_time_between_callbacks_ms
<< " ms";
508 EXPECT_GE(average_time_between_callbacks_ms
,
509 0.70 * expected_time_between_callbacks_ms
);
510 EXPECT_LE(average_time_between_callbacks_ms
,
511 1.35 * expected_time_between_callbacks_ms
);
514 void GetDefaultOutputStreamParameters() {
515 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
516 audio_output_parameters_
=
517 audio_manager()->GetDefaultOutputStreamParameters();
518 EXPECT_TRUE(audio_output_parameters_
.IsValid());
521 void MakeOutputStream(const AudioParameters
& params
) {
522 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
523 audio_output_stream_
= audio_manager()->MakeAudioOutputStream(
524 params
, std::string());
525 EXPECT_TRUE(audio_output_stream_
);
528 void OpenAndClose() {
529 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
530 EXPECT_TRUE(audio_output_stream_
->Open());
531 audio_output_stream_
->Close();
532 audio_output_stream_
= NULL
;
535 void OpenAndStart(AudioOutputStream::AudioSourceCallback
* source
) {
536 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
537 EXPECT_TRUE(audio_output_stream_
->Open());
538 audio_output_stream_
->Start(source
);
541 void StopAndClose() {
542 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
543 audio_output_stream_
->Stop();
544 audio_output_stream_
->Close();
545 audio_output_stream_
= NULL
;
548 scoped_ptr
<base::MessageLoopForUI
> loop_
;
549 scoped_ptr
<AudioManager
> audio_manager_
;
550 AudioParameters audio_output_parameters_
;
551 AudioOutputStream
* audio_output_stream_
;
552 base::TimeTicks start_time_
;
553 base::TimeTicks end_time_
;
556 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest
);
559 // AudioRecordInputStream should only be created on Jelly Bean and higher. This
560 // ensures we only test against the AudioRecord path when that is satisfied.
561 std::vector
<bool> RunAudioRecordInputPathTests() {
562 std::vector
<bool> tests
;
563 tests
.push_back(false);
564 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 16)
565 tests
.push_back(true);
569 // Test fixture class for tests which exercise the input path, or both input and
570 // output paths. It is value-parameterized to test against both the Java
571 // AudioRecord (when true) and native OpenSLES (when false) input paths.
572 class AudioAndroidInputTest
: public AudioAndroidOutputTest
,
573 public testing::WithParamInterface
<bool> {
575 AudioAndroidInputTest() : audio_input_stream_(NULL
) {}
578 const AudioParameters
& audio_input_parameters() {
579 return audio_input_parameters_
;
582 AudioParameters
GetInputStreamParameters() {
583 GetDefaultInputStreamParametersOnAudioThread();
585 // Override the platform effects setting to use the AudioRecord or OpenSLES
586 // path as requested.
587 int effects
= GetParam() ? AudioParameters::ECHO_CANCELLER
:
588 AudioParameters::NO_EFFECTS
;
589 AudioParameters
params(audio_input_parameters().format(),
590 audio_input_parameters().channel_layout(),
591 audio_input_parameters().input_channels(),
592 audio_input_parameters().sample_rate(),
593 audio_input_parameters().bits_per_sample(),
594 audio_input_parameters().frames_per_buffer(),
599 void GetDefaultInputStreamParametersOnAudioThread() {
601 base::Bind(&AudioAndroidInputTest::GetDefaultInputStreamParameters
,
602 base::Unretained(this)));
605 void MakeAudioInputStreamOnAudioThread(const AudioParameters
& params
) {
607 base::Bind(&AudioAndroidInputTest::MakeInputStream
,
608 base::Unretained(this),
612 void OpenAndCloseAudioInputStreamOnAudioThread() {
614 base::Bind(&AudioAndroidInputTest::OpenAndClose
,
615 base::Unretained(this)));
618 void OpenAndStartAudioInputStreamOnAudioThread(
619 AudioInputStream::AudioInputCallback
* sink
) {
621 base::Bind(&AudioAndroidInputTest::OpenAndStart
,
622 base::Unretained(this),
626 void StopAndCloseAudioInputStreamOnAudioThread() {
628 base::Bind(&AudioAndroidInputTest::StopAndClose
,
629 base::Unretained(this)));
632 void StartInputStreamCallbacks(const AudioParameters
& params
) {
633 double expected_time_between_callbacks_ms
=
634 ExpectedTimeBetweenCallbacks(params
);
635 const int num_callbacks
=
636 (kCallbackTestTimeMs
/ expected_time_between_callbacks_ms
);
638 MakeAudioInputStreamOnAudioThread(params
);
641 MockAudioInputCallback sink
;
644 OnData(audio_input_stream_
,
647 GetBytesPerBuffer(), _
, _
))
648 .Times(AtLeast(num_callbacks
))
650 CheckCountAndPostQuitTask(&count
, num_callbacks
, loop()));
651 EXPECT_CALL(sink
, OnError(audio_input_stream_
)).Times(0);
653 OpenAndStartAudioInputStreamOnAudioThread(&sink
);
655 start_time_
= base::TimeTicks::Now();
657 end_time_
= base::TimeTicks::Now();
659 StopAndCloseAudioInputStreamOnAudioThread();
661 double average_time_between_callbacks_ms
=
662 AverageTimeBetweenCallbacks(num_callbacks
);
663 VLOG(0) << "expected time between callbacks: "
664 << expected_time_between_callbacks_ms
<< " ms";
665 VLOG(0) << "average time between callbacks: "
666 << average_time_between_callbacks_ms
<< " ms";
667 EXPECT_GE(average_time_between_callbacks_ms
,
668 0.70 * expected_time_between_callbacks_ms
);
669 EXPECT_LE(average_time_between_callbacks_ms
,
670 1.30 * expected_time_between_callbacks_ms
);
673 void GetDefaultInputStreamParameters() {
674 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
675 audio_input_parameters_
= audio_manager()->GetInputStreamParameters(
676 AudioManagerBase::kDefaultDeviceId
);
679 void MakeInputStream(const AudioParameters
& params
) {
680 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
681 audio_input_stream_
= audio_manager()->MakeAudioInputStream(
682 params
, AudioManagerBase::kDefaultDeviceId
);
683 EXPECT_TRUE(audio_input_stream_
);
686 void OpenAndClose() {
687 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
688 EXPECT_TRUE(audio_input_stream_
->Open());
689 audio_input_stream_
->Close();
690 audio_input_stream_
= NULL
;
693 void OpenAndStart(AudioInputStream::AudioInputCallback
* sink
) {
694 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
695 EXPECT_TRUE(audio_input_stream_
->Open());
696 audio_input_stream_
->Start(sink
);
699 void StopAndClose() {
700 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
701 audio_input_stream_
->Stop();
702 audio_input_stream_
->Close();
703 audio_input_stream_
= NULL
;
706 AudioInputStream
* audio_input_stream_
;
707 AudioParameters audio_input_parameters_
;
710 DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest
);
713 // Get the default audio input parameters and log the result.
714 TEST_P(AudioAndroidInputTest
, GetDefaultInputStreamParameters
) {
715 // We don't go through AudioAndroidInputTest::GetInputStreamParameters() here
716 // so that we can log the real (non-overridden) values of the effects.
717 GetDefaultInputStreamParametersOnAudioThread();
718 EXPECT_TRUE(audio_input_parameters().IsValid());
719 VLOG(1) << audio_input_parameters();
722 // Get the default audio output parameters and log the result.
723 TEST_F(AudioAndroidOutputTest
, GetDefaultOutputStreamParameters
) {
724 GetDefaultOutputStreamParametersOnAudioThread();
725 VLOG(1) << audio_output_parameters();
728 // Verify input device enumeration.
729 TEST_F(AudioAndroidInputTest
, GetAudioInputDeviceNames
) {
730 if (!audio_manager()->HasAudioInputDevices())
732 AudioDeviceNames devices
;
734 base::Bind(&AudioManager::GetAudioInputDeviceNames
,
735 base::Unretained(audio_manager()),
737 CheckDeviceNames(devices
);
740 // Verify output device enumeration.
741 TEST_F(AudioAndroidOutputTest
, GetAudioOutputDeviceNames
) {
742 if (!audio_manager()->HasAudioOutputDevices())
744 AudioDeviceNames devices
;
746 base::Bind(&AudioManager::GetAudioOutputDeviceNames
,
747 base::Unretained(audio_manager()),
749 CheckDeviceNames(devices
);
752 // Ensure that a default input stream can be created and closed.
753 TEST_P(AudioAndroidInputTest
, CreateAndCloseInputStream
) {
754 AudioParameters params
= GetInputStreamParameters();
755 MakeAudioInputStreamOnAudioThread(params
);
757 base::Bind(&AudioInputStream::Close
,
758 base::Unretained(audio_input_stream_
)));
761 // Ensure that a default output stream can be created and closed.
762 // TODO(henrika): should we also verify that this API changes the audio mode
763 // to communication mode, and calls RegisterHeadsetReceiver, the first time
765 TEST_F(AudioAndroidOutputTest
, CreateAndCloseOutputStream
) {
766 GetDefaultOutputStreamParametersOnAudioThread();
767 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
769 base::Bind(&AudioOutputStream::Close
,
770 base::Unretained(audio_output_stream_
)));
773 // Ensure that a default input stream can be opened and closed.
774 TEST_P(AudioAndroidInputTest
, OpenAndCloseInputStream
) {
775 AudioParameters params
= GetInputStreamParameters();
776 MakeAudioInputStreamOnAudioThread(params
);
777 OpenAndCloseAudioInputStreamOnAudioThread();
780 // Ensure that a default output stream can be opened and closed.
781 TEST_F(AudioAndroidOutputTest
, OpenAndCloseOutputStream
) {
782 GetDefaultOutputStreamParametersOnAudioThread();
783 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
784 OpenAndCloseAudioOutputStreamOnAudioThread();
787 // Start input streaming using default input parameters and ensure that the
788 // callback sequence is sane.
789 TEST_P(AudioAndroidInputTest
, DISABLED_StartInputStreamCallbacks
) {
790 AudioParameters native_params
= GetInputStreamParameters();
791 StartInputStreamCallbacks(native_params
);
794 // Start input streaming using non default input parameters and ensure that the
795 // callback sequence is sane. The only change we make in this test is to select
796 // a 10ms buffer size instead of the default size.
797 TEST_P(AudioAndroidInputTest
,
798 DISABLED_StartInputStreamCallbacksNonDefaultParameters
) {
799 AudioParameters native_params
= GetInputStreamParameters();
800 AudioParameters
params(native_params
.format(),
801 native_params
.channel_layout(),
802 native_params
.input_channels(),
803 native_params
.sample_rate(),
804 native_params
.bits_per_sample(),
805 native_params
.sample_rate() / 100,
806 native_params
.effects());
807 StartInputStreamCallbacks(params
);
810 // Start output streaming using default output parameters and ensure that the
811 // callback sequence is sane.
812 TEST_F(AudioAndroidOutputTest
, StartOutputStreamCallbacks
) {
813 GetDefaultOutputStreamParametersOnAudioThread();
814 StartOutputStreamCallbacks(audio_output_parameters());
817 // Start output streaming using non default output parameters and ensure that
818 // the callback sequence is sane. The only change we make in this test is to
819 // select a 10ms buffer size instead of the default size and to open up the
821 // TODO(henrika): possibly add support for more variations.
822 TEST_F(AudioAndroidOutputTest
, DISABLED_StartOutputStreamCallbacksNonDefaultParameters
) {
823 GetDefaultOutputStreamParametersOnAudioThread();
824 AudioParameters
params(audio_output_parameters().format(),
826 audio_output_parameters().sample_rate(),
827 audio_output_parameters().bits_per_sample(),
828 audio_output_parameters().sample_rate() / 100);
829 StartOutputStreamCallbacks(params
);
832 // Play out a PCM file segment in real time and allow the user to verify that
833 // the rendered audio sounds OK.
834 // NOTE: this test requires user interaction and is not designed to run as an
835 // automatized test on bots.
836 TEST_F(AudioAndroidOutputTest
, DISABLED_RunOutputStreamWithFileAsSource
) {
837 GetDefaultOutputStreamParametersOnAudioThread();
838 VLOG(1) << audio_output_parameters();
839 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
841 std::string file_name
;
842 const AudioParameters params
= audio_output_parameters();
843 if (params
.sample_rate() == 48000 && params
.channels() == 2) {
844 file_name
= kSpeechFile_16b_s_48k
;
845 } else if (params
.sample_rate() == 48000 && params
.channels() == 1) {
846 file_name
= kSpeechFile_16b_m_48k
;
847 } else if (params
.sample_rate() == 44100 && params
.channels() == 2) {
848 file_name
= kSpeechFile_16b_s_44k
;
849 } else if (params
.sample_rate() == 44100 && params
.channels() == 1) {
850 file_name
= kSpeechFile_16b_m_44k
;
852 FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only.";
856 base::WaitableEvent
event(false, false);
857 FileAudioSource
source(&event
, file_name
);
859 OpenAndStartAudioOutputStreamOnAudioThread(&source
);
860 VLOG(0) << ">> Verify that the file is played out correctly...";
861 EXPECT_TRUE(event
.TimedWait(TestTimeouts::action_max_timeout()));
862 StopAndCloseAudioOutputStreamOnAudioThread();
865 // Start input streaming and run it for ten seconds while recording to a
867 // NOTE: this test requires user interaction and is not designed to run as an
868 // automatized test on bots.
869 TEST_P(AudioAndroidInputTest
, DISABLED_RunSimplexInputStreamWithFileAsSink
) {
870 AudioParameters params
= GetInputStreamParameters();
872 MakeAudioInputStreamOnAudioThread(params
);
874 std::string file_name
= base::StringPrintf("out_simplex_%d_%d_%d.pcm",
875 params
.sample_rate(),
876 params
.frames_per_buffer(),
879 base::WaitableEvent
event(false, false);
880 FileAudioSink
sink(&event
, params
, file_name
);
882 OpenAndStartAudioInputStreamOnAudioThread(&sink
);
883 VLOG(0) << ">> Speak into the microphone to record audio...";
884 EXPECT_TRUE(event
.TimedWait(TestTimeouts::action_max_timeout()));
885 StopAndCloseAudioInputStreamOnAudioThread();
888 // Same test as RunSimplexInputStreamWithFileAsSink but this time output
889 // streaming is active as well (reads zeros only).
890 // NOTE: this test requires user interaction and is not designed to run as an
891 // automatized test on bots.
892 TEST_P(AudioAndroidInputTest
, DISABLED_RunDuplexInputStreamWithFileAsSink
) {
893 AudioParameters in_params
= GetInputStreamParameters();
894 VLOG(1) << in_params
;
895 MakeAudioInputStreamOnAudioThread(in_params
);
897 GetDefaultOutputStreamParametersOnAudioThread();
898 VLOG(1) << audio_output_parameters();
899 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
901 std::string file_name
= base::StringPrintf("out_duplex_%d_%d_%d.pcm",
902 in_params
.sample_rate(),
903 in_params
.frames_per_buffer(),
904 in_params
.channels());
906 base::WaitableEvent
event(false, false);
907 FileAudioSink
sink(&event
, in_params
, file_name
);
908 MockAudioSourceCallback source
;
910 EXPECT_CALL(source
, OnMoreData(NotNull(), _
))
911 .WillRepeatedly(Invoke(RealOnMoreData
));
912 EXPECT_CALL(source
, OnError(audio_output_stream_
)).Times(0);
914 OpenAndStartAudioInputStreamOnAudioThread(&sink
);
915 OpenAndStartAudioOutputStreamOnAudioThread(&source
);
916 VLOG(0) << ">> Speak into the microphone to record audio";
917 EXPECT_TRUE(event
.TimedWait(TestTimeouts::action_max_timeout()));
918 StopAndCloseAudioOutputStreamOnAudioThread();
919 StopAndCloseAudioInputStreamOnAudioThread();
922 // Start audio in both directions while feeding captured data into a FIFO so
923 // it can be read directly (in loopback) by the render side. A small extra
924 // delay will be added by the FIFO and an estimate of this delay will be
925 // printed out during the test.
926 // NOTE: this test requires user interaction and is not designed to run as an
927 // automatized test on bots.
928 TEST_P(AudioAndroidInputTest
,
929 DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex
) {
930 // Get native audio parameters for the input side.
931 AudioParameters default_input_params
= GetInputStreamParameters();
933 // Modify the parameters so that both input and output can use the same
934 // parameters by selecting 10ms as buffer size. This will also ensure that
935 // the output stream will be a mono stream since mono is default for input
937 AudioParameters
io_params(default_input_params
.format(),
938 default_input_params
.channel_layout(),
939 ChannelLayoutToChannelCount(
940 default_input_params
.channel_layout()),
941 default_input_params
.sample_rate(),
942 default_input_params
.bits_per_sample(),
943 default_input_params
.sample_rate() / 100,
944 default_input_params
.effects());
945 VLOG(1) << io_params
;
947 // Create input and output streams using the common audio parameters.
948 MakeAudioInputStreamOnAudioThread(io_params
);
949 MakeAudioOutputStreamOnAudioThread(io_params
);
951 FullDuplexAudioSinkSource
full_duplex(io_params
);
953 // Start a full duplex audio session and print out estimates of the extra
954 // delay we should expect from the FIFO. If real-time delay measurements are
955 // performed, the result should be reduced by this extra delay since it is
956 // something that has been added by the test.
957 OpenAndStartAudioInputStreamOnAudioThread(&full_duplex
);
958 OpenAndStartAudioOutputStreamOnAudioThread(&full_duplex
);
959 VLOG(1) << "HINT: an estimate of the extra FIFO delay will be updated "
960 << "once per second during this test.";
961 VLOG(0) << ">> Speak into the mic and listen to the audio in loopback...";
963 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20));
965 StopAndCloseAudioOutputStreamOnAudioThread();
966 StopAndCloseAudioInputStreamOnAudioThread();
969 INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest
, AudioAndroidInputTest
,
970 testing::ValuesIn(RunAudioRecordInputPathTests()));