Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / media / audio / android / audio_android_unittest.cc
blobb8ebccf3b2b5a4e1c0694d6bafd265ecef4b3c0c
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/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/lock.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/test/test_timeouts.h"
17 #include "base/time/time.h"
18 #include "build/build_config.h"
19 #include "media/audio/android/audio_manager_android.h"
20 #include "media/audio/audio_io.h"
21 #include "media/audio/audio_manager_base.h"
22 #include "media/audio/audio_unittest_util.h"
23 #include "media/audio/mock_audio_source_callback.h"
24 #include "media/base/decoder_buffer.h"
25 #include "media/base/seekable_buffer.h"
26 #include "media/base/test_data_util.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 using ::testing::_;
31 using ::testing::AtLeast;
32 using ::testing::DoAll;
33 using ::testing::Invoke;
34 using ::testing::NotNull;
35 using ::testing::Return;
37 namespace media {
39 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) {
40 if (++*count >= limit) {
41 loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
45 static const char kSpeechFile_16b_s_48k[] = "speech_16b_stereo_48kHz.raw";
46 static const char kSpeechFile_16b_m_48k[] = "speech_16b_mono_48kHz.raw";
47 static const char kSpeechFile_16b_s_44k[] = "speech_16b_stereo_44kHz.raw";
48 static const char kSpeechFile_16b_m_44k[] = "speech_16b_mono_44kHz.raw";
50 static const float kCallbackTestTimeMs = 2000.0;
51 static const int kBitsPerSample = 16;
52 static const int kBytesPerSample = kBitsPerSample / 8;
54 // Converts AudioParameters::Format enumerator to readable string.
55 static std::string FormatToString(AudioParameters::Format format) {
56 switch (format) {
57 case AudioParameters::AUDIO_PCM_LINEAR:
58 return std::string("AUDIO_PCM_LINEAR");
59 case AudioParameters::AUDIO_PCM_LOW_LATENCY:
60 return std::string("AUDIO_PCM_LOW_LATENCY");
61 case AudioParameters::AUDIO_FAKE:
62 return std::string("AUDIO_FAKE");
63 case AudioParameters::AUDIO_LAST_FORMAT:
64 return std::string("AUDIO_LAST_FORMAT");
65 default:
66 return std::string();
70 // Converts ChannelLayout enumerator to readable string. Does not include
71 // multi-channel cases since these layouts are not supported on Android.
72 static std::string LayoutToString(ChannelLayout channel_layout) {
73 switch (channel_layout) {
74 case CHANNEL_LAYOUT_NONE:
75 return std::string("CHANNEL_LAYOUT_NONE");
76 case CHANNEL_LAYOUT_MONO:
77 return std::string("CHANNEL_LAYOUT_MONO");
78 case CHANNEL_LAYOUT_STEREO:
79 return std::string("CHANNEL_LAYOUT_STEREO");
80 case CHANNEL_LAYOUT_UNSUPPORTED:
81 default:
82 return std::string("CHANNEL_LAYOUT_UNSUPPORTED");
86 static double ExpectedTimeBetweenCallbacks(AudioParameters params) {
87 return (base::TimeDelta::FromMicroseconds(
88 params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
89 static_cast<double>(params.sample_rate()))).InMillisecondsF();
92 // Helper method which verifies that the device list starts with a valid
93 // default device name followed by non-default device names.
94 static void CheckDeviceNames(const AudioDeviceNames& device_names) {
95 DVLOG(2) << "Got " << device_names.size() << " audio devices.";
96 if (device_names.empty()) {
97 // Log a warning so we can see the status on the build bots. No need to
98 // break the test though since this does successfully test the code and
99 // some failure cases.
100 LOG(WARNING) << "No input devices detected";
101 return;
104 AudioDeviceNames::const_iterator it = device_names.begin();
106 // The first device in the list should always be the default device.
107 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName),
108 it->device_name);
109 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id);
110 ++it;
112 // Other devices should have non-empty name and id and should not contain
113 // default name or id.
114 while (it != device_names.end()) {
115 EXPECT_FALSE(it->device_name.empty());
116 EXPECT_FALSE(it->unique_id.empty());
117 DVLOG(2) << "Device ID(" << it->unique_id
118 << "), label: " << it->device_name;
119 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName),
120 it->device_name);
121 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId),
122 it->unique_id);
123 ++it;
127 // We clear the data bus to ensure that the test does not cause noise.
128 static int RealOnMoreData(AudioBus* dest, uint32 total_bytes_delay) {
129 dest->Zero();
130 return dest->frames();
133 std::ostream& operator<<(std::ostream& os, const AudioParameters& params) {
134 using namespace std;
135 os << endl << "format: " << FormatToString(params.format()) << endl
136 << "channel layout: " << LayoutToString(params.channel_layout()) << endl
137 << "sample rate: " << params.sample_rate() << endl
138 << "bits per sample: " << params.bits_per_sample() << endl
139 << "frames per buffer: " << params.frames_per_buffer() << endl
140 << "channels: " << params.channels() << endl
141 << "bytes per buffer: " << params.GetBytesPerBuffer() << endl
142 << "bytes per second: " << params.GetBytesPerSecond() << endl
143 << "bytes per frame: " << params.GetBytesPerFrame() << endl
144 << "chunk size in ms: " << ExpectedTimeBetweenCallbacks(params) << endl
145 << "echo_canceller: "
146 << (params.effects() & AudioParameters::ECHO_CANCELLER);
147 return os;
150 // Gmock implementation of AudioInputStream::AudioInputCallback.
151 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
152 public:
153 MOCK_METHOD4(OnData,
154 void(AudioInputStream* stream,
155 const AudioBus* src,
156 uint32 hardware_delay_bytes,
157 double volume));
158 MOCK_METHOD1(OnError, void(AudioInputStream* stream));
161 // Implements AudioOutputStream::AudioSourceCallback and provides audio data
162 // by reading from a data file.
163 class FileAudioSource : public AudioOutputStream::AudioSourceCallback {
164 public:
165 explicit FileAudioSource(base::WaitableEvent* event, const std::string& name)
166 : event_(event), pos_(0) {
167 // Reads a test file from media/test/data directory and stores it in
168 // a DecoderBuffer.
169 file_ = ReadTestDataFile(name);
171 // Log the name of the file which is used as input for this test.
172 base::FilePath file_path = GetTestDataFilePath(name);
173 DVLOG(0) << "Reading from file: " << file_path.value().c_str();
176 ~FileAudioSource() override {}
178 // AudioOutputStream::AudioSourceCallback implementation.
180 // Use samples read from a data file and fill up the audio buffer
181 // provided to us in the callback.
182 int OnMoreData(AudioBus* audio_bus, uint32 total_bytes_delay) override {
183 bool stop_playing = false;
184 int max_size =
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()) {
189 stop_playing = true;
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);
198 if (max_size) {
199 audio_bus->FromInterleaved(file_->data() + pos_, frames, kBytesPerSample);
200 pos_ += max_size;
203 // Set event to ensure that the test can stop when the file has ended.
204 if (stop_playing)
205 event_->Signal();
207 return frames;
210 void OnError(AudioOutputStream* stream) override {}
212 int file_size() { return file_->data_size(); }
214 private:
215 base::WaitableEvent* event_;
216 int pos_;
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 {
228 public:
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 DVLOG(0) << "Writing to file: " << file_path.value().c_str();
246 ~FileAudioSink() override {
247 int bytes_written = 0;
248 while (bytes_written < buffer_->forward_capacity()) {
249 const uint8* chunk;
250 int chunk_size;
252 // Stop writing if no more data is available.
253 if (!buffer_->GetCurrentChunk(&chunk, &chunk_size))
254 break;
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 void OnData(AudioInputStream* stream,
267 const AudioBus* src,
268 uint32 hardware_delay_bytes,
269 double volume) override {
270 const int num_samples = src->frames() * src->channels();
271 scoped_ptr<int16> interleaved(new int16[num_samples]);
272 const int bytes_per_sample = sizeof(*interleaved);
273 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get());
275 // Store data data in a temporary buffer to avoid making blocking
276 // fwrite() calls in the audio callback. The complete buffer will be
277 // written to file in the destructor.
278 const int size = bytes_per_sample * num_samples;
279 if (!buffer_->Append((const uint8*)interleaved.get(), size))
280 event_->Signal();
283 void OnError(AudioInputStream* stream) override {}
285 private:
286 base::WaitableEvent* event_;
287 AudioParameters params_;
288 scoped_ptr<media::SeekableBuffer> buffer_;
289 FILE* binary_file_;
291 DISALLOW_COPY_AND_ASSIGN(FileAudioSink);
294 // Implements AudioInputCallback and AudioSourceCallback to support full
295 // duplex audio where captured samples are played out in loopback after
296 // reading from a temporary FIFO storage.
297 class FullDuplexAudioSinkSource
298 : public AudioInputStream::AudioInputCallback,
299 public AudioOutputStream::AudioSourceCallback {
300 public:
301 explicit FullDuplexAudioSinkSource(const AudioParameters& params)
302 : params_(params),
303 previous_time_(base::TimeTicks::Now()),
304 started_(false) {
305 // Start with a reasonably small FIFO size. It will be increased
306 // dynamically during the test if required.
307 fifo_.reset(new media::SeekableBuffer(0, 2 * params.GetBytesPerBuffer()));
308 buffer_.reset(new uint8[params_.GetBytesPerBuffer()]);
311 ~FullDuplexAudioSinkSource() override {}
313 // AudioInputStream::AudioInputCallback implementation
314 void OnData(AudioInputStream* stream,
315 const AudioBus* src,
316 uint32 hardware_delay_bytes,
317 double volume) override {
318 const base::TimeTicks now_time = base::TimeTicks::Now();
319 const int diff = (now_time - previous_time_).InMilliseconds();
321 EXPECT_EQ(params_.bits_per_sample(), 16);
322 const int num_samples = src->frames() * src->channels();
323 scoped_ptr<int16> interleaved(new int16[num_samples]);
324 const int bytes_per_sample = sizeof(*interleaved);
325 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get());
326 const int size = bytes_per_sample * num_samples;
328 base::AutoLock lock(lock_);
329 if (diff > 1000) {
330 started_ = true;
331 previous_time_ = now_time;
333 // Log out the extra delay added by the FIFO. This is a best effort
334 // estimate. We might be +- 10ms off here.
335 int extra_fifo_delay =
336 static_cast<int>(BytesToMilliseconds(fifo_->forward_bytes() + size));
337 DVLOG(1) << extra_fifo_delay;
340 // We add an initial delay of ~1 second before loopback starts to ensure
341 // a stable callback sequence and to avoid initial bursts which might add
342 // to the extra FIFO delay.
343 if (!started_)
344 return;
346 // Append new data to the FIFO and extend the size if the max capacity
347 // was exceeded. Flush the FIFO when extended just in case.
348 if (!fifo_->Append((const uint8*)interleaved.get(), size)) {
349 fifo_->set_forward_capacity(2 * fifo_->forward_capacity());
350 fifo_->Clear();
354 void OnError(AudioInputStream* stream) override {}
356 // AudioOutputStream::AudioSourceCallback implementation
357 int OnMoreData(AudioBus* dest, uint32 total_bytes_delay) override {
358 const int size_in_bytes =
359 (params_.bits_per_sample() / 8) * dest->frames() * dest->channels();
360 EXPECT_EQ(size_in_bytes, params_.GetBytesPerBuffer());
362 base::AutoLock lock(lock_);
364 // We add an initial delay of ~1 second before loopback starts to ensure
365 // a stable callback sequences and to avoid initial bursts which might add
366 // to the extra FIFO delay.
367 if (!started_) {
368 dest->Zero();
369 return dest->frames();
372 // Fill up destination with zeros if the FIFO does not contain enough
373 // data to fulfill the request.
374 if (fifo_->forward_bytes() < size_in_bytes) {
375 dest->Zero();
376 } else {
377 fifo_->Read(buffer_.get(), size_in_bytes);
378 dest->FromInterleaved(
379 buffer_.get(), dest->frames(), params_.bits_per_sample() / 8);
382 return dest->frames();
385 void OnError(AudioOutputStream* stream) override {}
387 private:
388 // Converts from bytes to milliseconds given number of bytes and existing
389 // audio parameters.
390 double BytesToMilliseconds(int bytes) const {
391 const int frames = bytes / params_.GetBytesPerFrame();
392 return (base::TimeDelta::FromMicroseconds(
393 frames * base::Time::kMicrosecondsPerSecond /
394 static_cast<double>(params_.sample_rate()))).InMillisecondsF();
397 AudioParameters params_;
398 base::TimeTicks previous_time_;
399 base::Lock lock_;
400 scoped_ptr<media::SeekableBuffer> fifo_;
401 scoped_ptr<uint8[]> buffer_;
402 bool started_;
404 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource);
407 // Test fixture class for tests which only exercise the output path.
408 class AudioAndroidOutputTest : public testing::Test {
409 public:
410 AudioAndroidOutputTest()
411 : loop_(new base::MessageLoopForUI()),
412 audio_manager_(AudioManager::CreateForTesting()),
413 audio_output_stream_(NULL) {
416 ~AudioAndroidOutputTest() override {}
418 protected:
419 AudioManager* audio_manager() { return audio_manager_.get(); }
420 base::MessageLoopForUI* loop() { return loop_.get(); }
421 const AudioParameters& audio_output_parameters() {
422 return audio_output_parameters_;
425 // Synchronously runs the provided callback/closure on the audio thread.
426 void RunOnAudioThread(const base::Closure& closure) {
427 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
428 base::WaitableEvent event(false, false);
429 audio_manager()->GetTaskRunner()->PostTask(
430 FROM_HERE,
431 base::Bind(&AudioAndroidOutputTest::RunOnAudioThreadImpl,
432 base::Unretained(this),
433 closure,
434 &event));
435 event.Wait();
436 } else {
437 closure.Run();
441 void RunOnAudioThreadImpl(const base::Closure& closure,
442 base::WaitableEvent* event) {
443 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
444 closure.Run();
445 event->Signal();
448 void GetDefaultOutputStreamParametersOnAudioThread() {
449 RunOnAudioThread(
450 base::Bind(&AudioAndroidOutputTest::GetDefaultOutputStreamParameters,
451 base::Unretained(this)));
454 void MakeAudioOutputStreamOnAudioThread(const AudioParameters& params) {
455 RunOnAudioThread(
456 base::Bind(&AudioAndroidOutputTest::MakeOutputStream,
457 base::Unretained(this),
458 params));
461 void OpenAndCloseAudioOutputStreamOnAudioThread() {
462 RunOnAudioThread(
463 base::Bind(&AudioAndroidOutputTest::OpenAndClose,
464 base::Unretained(this)));
467 void OpenAndStartAudioOutputStreamOnAudioThread(
468 AudioOutputStream::AudioSourceCallback* source) {
469 RunOnAudioThread(
470 base::Bind(&AudioAndroidOutputTest::OpenAndStart,
471 base::Unretained(this),
472 source));
475 void StopAndCloseAudioOutputStreamOnAudioThread() {
476 RunOnAudioThread(
477 base::Bind(&AudioAndroidOutputTest::StopAndClose,
478 base::Unretained(this)));
481 double AverageTimeBetweenCallbacks(int num_callbacks) const {
482 return ((end_time_ - start_time_) / static_cast<double>(num_callbacks - 1))
483 .InMillisecondsF();
486 void StartOutputStreamCallbacks(const AudioParameters& params) {
487 double expected_time_between_callbacks_ms =
488 ExpectedTimeBetweenCallbacks(params);
489 const int num_callbacks =
490 (kCallbackTestTimeMs / expected_time_between_callbacks_ms);
491 MakeAudioOutputStreamOnAudioThread(params);
493 int count = 0;
494 MockAudioSourceCallback source;
496 EXPECT_CALL(source, OnMoreData(NotNull(), _))
497 .Times(AtLeast(num_callbacks))
498 .WillRepeatedly(
499 DoAll(CheckCountAndPostQuitTask(&count, num_callbacks, loop()),
500 Invoke(RealOnMoreData)));
501 EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);
503 OpenAndStartAudioOutputStreamOnAudioThread(&source);
505 start_time_ = base::TimeTicks::Now();
506 loop()->Run();
507 end_time_ = base::TimeTicks::Now();
509 StopAndCloseAudioOutputStreamOnAudioThread();
511 double average_time_between_callbacks_ms =
512 AverageTimeBetweenCallbacks(num_callbacks);
513 DVLOG(0) << "expected time between callbacks: "
514 << expected_time_between_callbacks_ms << " ms";
515 DVLOG(0) << "average time between callbacks: "
516 << average_time_between_callbacks_ms << " ms";
517 EXPECT_GE(average_time_between_callbacks_ms,
518 0.70 * expected_time_between_callbacks_ms);
519 EXPECT_LE(average_time_between_callbacks_ms,
520 1.50 * expected_time_between_callbacks_ms);
523 void GetDefaultOutputStreamParameters() {
524 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
525 audio_output_parameters_ =
526 audio_manager()->GetDefaultOutputStreamParameters();
527 EXPECT_TRUE(audio_output_parameters_.IsValid());
530 void MakeOutputStream(const AudioParameters& params) {
531 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
532 audio_output_stream_ = audio_manager()->MakeAudioOutputStream(
533 params, std::string());
534 EXPECT_TRUE(audio_output_stream_);
537 void OpenAndClose() {
538 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
539 EXPECT_TRUE(audio_output_stream_->Open());
540 audio_output_stream_->Close();
541 audio_output_stream_ = NULL;
544 void OpenAndStart(AudioOutputStream::AudioSourceCallback* source) {
545 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
546 EXPECT_TRUE(audio_output_stream_->Open());
547 audio_output_stream_->Start(source);
550 void StopAndClose() {
551 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
552 audio_output_stream_->Stop();
553 audio_output_stream_->Close();
554 audio_output_stream_ = NULL;
557 scoped_ptr<base::MessageLoopForUI> loop_;
558 scoped_ptr<AudioManager> audio_manager_;
559 AudioParameters audio_output_parameters_;
560 AudioOutputStream* audio_output_stream_;
561 base::TimeTicks start_time_;
562 base::TimeTicks end_time_;
564 private:
565 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest);
568 // AudioRecordInputStream should only be created on Jelly Bean and higher. This
569 // ensures we only test against the AudioRecord path when that is satisfied.
570 std::vector<bool> RunAudioRecordInputPathTests() {
571 std::vector<bool> tests;
572 tests.push_back(false);
573 if (base::android::BuildInfo::GetInstance()->sdk_int() >= 16)
574 tests.push_back(true);
575 return tests;
578 // Test fixture class for tests which exercise the input path, or both input and
579 // output paths. It is value-parameterized to test against both the Java
580 // AudioRecord (when true) and native OpenSLES (when false) input paths.
581 class AudioAndroidInputTest : public AudioAndroidOutputTest,
582 public testing::WithParamInterface<bool> {
583 public:
584 AudioAndroidInputTest() : audio_input_stream_(NULL) {}
586 protected:
587 const AudioParameters& audio_input_parameters() {
588 return audio_input_parameters_;
591 AudioParameters GetInputStreamParameters() {
592 GetDefaultInputStreamParametersOnAudioThread();
594 // Override the platform effects setting to use the AudioRecord or OpenSLES
595 // path as requested.
596 int effects = GetParam() ? AudioParameters::ECHO_CANCELLER :
597 AudioParameters::NO_EFFECTS;
598 AudioParameters params(audio_input_parameters().format(),
599 audio_input_parameters().channel_layout(),
600 audio_input_parameters().sample_rate(),
601 audio_input_parameters().bits_per_sample(),
602 audio_input_parameters().frames_per_buffer(),
603 effects);
604 return params;
607 void GetDefaultInputStreamParametersOnAudioThread() {
608 RunOnAudioThread(
609 base::Bind(&AudioAndroidInputTest::GetDefaultInputStreamParameters,
610 base::Unretained(this)));
613 void MakeAudioInputStreamOnAudioThread(const AudioParameters& params) {
614 RunOnAudioThread(
615 base::Bind(&AudioAndroidInputTest::MakeInputStream,
616 base::Unretained(this),
617 params));
620 void OpenAndCloseAudioInputStreamOnAudioThread() {
621 RunOnAudioThread(
622 base::Bind(&AudioAndroidInputTest::OpenAndClose,
623 base::Unretained(this)));
626 void OpenAndStartAudioInputStreamOnAudioThread(
627 AudioInputStream::AudioInputCallback* sink) {
628 RunOnAudioThread(
629 base::Bind(&AudioAndroidInputTest::OpenAndStart,
630 base::Unretained(this),
631 sink));
634 void StopAndCloseAudioInputStreamOnAudioThread() {
635 RunOnAudioThread(
636 base::Bind(&AudioAndroidInputTest::StopAndClose,
637 base::Unretained(this)));
640 void StartInputStreamCallbacks(const AudioParameters& params) {
641 double expected_time_between_callbacks_ms =
642 ExpectedTimeBetweenCallbacks(params);
643 const int num_callbacks =
644 (kCallbackTestTimeMs / expected_time_between_callbacks_ms);
646 MakeAudioInputStreamOnAudioThread(params);
648 int count = 0;
649 MockAudioInputCallback sink;
651 EXPECT_CALL(sink, OnData(audio_input_stream_, NotNull(), _, _))
652 .Times(AtLeast(num_callbacks))
653 .WillRepeatedly(
654 CheckCountAndPostQuitTask(&count, num_callbacks, loop()));
655 EXPECT_CALL(sink, OnError(audio_input_stream_)).Times(0);
657 OpenAndStartAudioInputStreamOnAudioThread(&sink);
659 start_time_ = base::TimeTicks::Now();
660 loop()->Run();
661 end_time_ = base::TimeTicks::Now();
663 StopAndCloseAudioInputStreamOnAudioThread();
665 double average_time_between_callbacks_ms =
666 AverageTimeBetweenCallbacks(num_callbacks);
667 DVLOG(0) << "expected time between callbacks: "
668 << expected_time_between_callbacks_ms << " ms";
669 DVLOG(0) << "average time between callbacks: "
670 << average_time_between_callbacks_ms << " ms";
671 EXPECT_GE(average_time_between_callbacks_ms,
672 0.70 * expected_time_between_callbacks_ms);
673 EXPECT_LE(average_time_between_callbacks_ms,
674 1.30 * expected_time_between_callbacks_ms);
677 void GetDefaultInputStreamParameters() {
678 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
679 audio_input_parameters_ = audio_manager()->GetInputStreamParameters(
680 AudioManagerBase::kDefaultDeviceId);
683 void MakeInputStream(const AudioParameters& params) {
684 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
685 audio_input_stream_ = audio_manager()->MakeAudioInputStream(
686 params, AudioManagerBase::kDefaultDeviceId);
687 EXPECT_TRUE(audio_input_stream_);
690 void OpenAndClose() {
691 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
692 EXPECT_TRUE(audio_input_stream_->Open());
693 audio_input_stream_->Close();
694 audio_input_stream_ = NULL;
697 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
698 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
699 EXPECT_TRUE(audio_input_stream_->Open());
700 audio_input_stream_->Start(sink);
703 void StopAndClose() {
704 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
705 audio_input_stream_->Stop();
706 audio_input_stream_->Close();
707 audio_input_stream_ = NULL;
710 AudioInputStream* audio_input_stream_;
711 AudioParameters audio_input_parameters_;
713 private:
714 DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest);
717 // Get the default audio input parameters and log the result.
718 TEST_P(AudioAndroidInputTest, GetDefaultInputStreamParameters) {
719 // We don't go through AudioAndroidInputTest::GetInputStreamParameters() here
720 // so that we can log the real (non-overridden) values of the effects.
721 GetDefaultInputStreamParametersOnAudioThread();
722 EXPECT_TRUE(audio_input_parameters().IsValid());
723 DVLOG(1) << audio_input_parameters();
726 // Get the default audio output parameters and log the result.
727 TEST_F(AudioAndroidOutputTest, GetDefaultOutputStreamParameters) {
728 GetDefaultOutputStreamParametersOnAudioThread();
729 DVLOG(1) << audio_output_parameters();
732 // Verify input device enumeration.
733 TEST_F(AudioAndroidInputTest, GetAudioInputDeviceNames) {
734 ABORT_AUDIO_TEST_IF_NOT(audio_manager()->HasAudioInputDevices());
735 AudioDeviceNames devices;
736 RunOnAudioThread(
737 base::Bind(&AudioManager::GetAudioInputDeviceNames,
738 base::Unretained(audio_manager()),
739 &devices));
740 CheckDeviceNames(devices);
743 // Verify output device enumeration.
744 TEST_F(AudioAndroidOutputTest, GetAudioOutputDeviceNames) {
745 ABORT_AUDIO_TEST_IF_NOT(audio_manager()->HasAudioOutputDevices());
746 AudioDeviceNames devices;
747 RunOnAudioThread(
748 base::Bind(&AudioManager::GetAudioOutputDeviceNames,
749 base::Unretained(audio_manager()),
750 &devices));
751 CheckDeviceNames(devices);
754 // Ensure that a default input stream can be created and closed.
755 TEST_P(AudioAndroidInputTest, CreateAndCloseInputStream) {
756 AudioParameters params = GetInputStreamParameters();
757 MakeAudioInputStreamOnAudioThread(params);
758 RunOnAudioThread(
759 base::Bind(&AudioInputStream::Close,
760 base::Unretained(audio_input_stream_)));
763 // Ensure that a default output stream can be created and closed.
764 // TODO(henrika): should we also verify that this API changes the audio mode
765 // to communication mode, and calls RegisterHeadsetReceiver, the first time
766 // it is called?
767 TEST_F(AudioAndroidOutputTest, CreateAndCloseOutputStream) {
768 GetDefaultOutputStreamParametersOnAudioThread();
769 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
770 RunOnAudioThread(
771 base::Bind(&AudioOutputStream::Close,
772 base::Unretained(audio_output_stream_)));
775 // Ensure that a default input stream can be opened and closed.
776 TEST_P(AudioAndroidInputTest, OpenAndCloseInputStream) {
777 AudioParameters params = GetInputStreamParameters();
778 MakeAudioInputStreamOnAudioThread(params);
779 OpenAndCloseAudioInputStreamOnAudioThread();
782 // Ensure that a default output stream can be opened and closed.
783 TEST_F(AudioAndroidOutputTest, OpenAndCloseOutputStream) {
784 GetDefaultOutputStreamParametersOnAudioThread();
785 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
786 OpenAndCloseAudioOutputStreamOnAudioThread();
789 // Start input streaming using default input parameters and ensure that the
790 // callback sequence is sane.
791 TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacks) {
792 AudioParameters native_params = GetInputStreamParameters();
793 StartInputStreamCallbacks(native_params);
796 // Start input streaming using non default input parameters and ensure that the
797 // callback sequence is sane. The only change we make in this test is to select
798 // a 10ms buffer size instead of the default size.
799 TEST_P(AudioAndroidInputTest,
800 DISABLED_StartInputStreamCallbacksNonDefaultParameters) {
801 AudioParameters native_params = GetInputStreamParameters();
802 AudioParameters params(native_params.format(),
803 native_params.channel_layout(),
804 native_params.sample_rate(),
805 native_params.bits_per_sample(),
806 native_params.sample_rate() / 100,
807 native_params.effects());
808 StartInputStreamCallbacks(params);
811 // Start output streaming using default output parameters and ensure that the
812 // callback sequence is sane.
813 TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacks) {
814 GetDefaultOutputStreamParametersOnAudioThread();
815 StartOutputStreamCallbacks(audio_output_parameters());
818 // Start output streaming using non default output parameters and ensure that
819 // the callback sequence is sane. The only change we make in this test is to
820 // select a 10ms buffer size instead of the default size and to open up the
821 // device in mono.
822 // TODO(henrika): possibly add support for more variations.
823 TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacksNonDefaultParameters) {
824 GetDefaultOutputStreamParametersOnAudioThread();
825 AudioParameters params(audio_output_parameters().format(),
826 CHANNEL_LAYOUT_MONO,
827 audio_output_parameters().sample_rate(),
828 audio_output_parameters().bits_per_sample(),
829 audio_output_parameters().sample_rate() / 100);
830 StartOutputStreamCallbacks(params);
833 // Play out a PCM file segment in real time and allow the user to verify that
834 // the rendered audio sounds OK.
835 // NOTE: this test requires user interaction and is not designed to run as an
836 // automatized test on bots.
837 TEST_F(AudioAndroidOutputTest, DISABLED_RunOutputStreamWithFileAsSource) {
838 GetDefaultOutputStreamParametersOnAudioThread();
839 DVLOG(1) << audio_output_parameters();
840 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
842 std::string file_name;
843 const AudioParameters params = audio_output_parameters();
844 if (params.sample_rate() == 48000 && params.channels() == 2) {
845 file_name = kSpeechFile_16b_s_48k;
846 } else if (params.sample_rate() == 48000 && params.channels() == 1) {
847 file_name = kSpeechFile_16b_m_48k;
848 } else if (params.sample_rate() == 44100 && params.channels() == 2) {
849 file_name = kSpeechFile_16b_s_44k;
850 } else if (params.sample_rate() == 44100 && params.channels() == 1) {
851 file_name = kSpeechFile_16b_m_44k;
852 } else {
853 FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only.";
854 return;
857 base::WaitableEvent event(false, false);
858 FileAudioSource source(&event, file_name);
860 OpenAndStartAudioOutputStreamOnAudioThread(&source);
861 DVLOG(0) << ">> Verify that the file is played out correctly...";
862 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
863 StopAndCloseAudioOutputStreamOnAudioThread();
866 // Start input streaming and run it for ten seconds while recording to a
867 // local audio file.
868 // NOTE: this test requires user interaction and is not designed to run as an
869 // automatized test on bots.
870 TEST_P(AudioAndroidInputTest, DISABLED_RunSimplexInputStreamWithFileAsSink) {
871 AudioParameters params = GetInputStreamParameters();
872 DVLOG(1) << params;
873 MakeAudioInputStreamOnAudioThread(params);
875 std::string file_name = base::StringPrintf("out_simplex_%d_%d_%d.pcm",
876 params.sample_rate(),
877 params.frames_per_buffer(),
878 params.channels());
880 base::WaitableEvent event(false, false);
881 FileAudioSink sink(&event, params, file_name);
883 OpenAndStartAudioInputStreamOnAudioThread(&sink);
884 DVLOG(0) << ">> Speak into the microphone to record audio...";
885 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
886 StopAndCloseAudioInputStreamOnAudioThread();
889 // Same test as RunSimplexInputStreamWithFileAsSink but this time output
890 // streaming is active as well (reads zeros only).
891 // NOTE: this test requires user interaction and is not designed to run as an
892 // automatized test on bots.
893 TEST_P(AudioAndroidInputTest, DISABLED_RunDuplexInputStreamWithFileAsSink) {
894 AudioParameters in_params = GetInputStreamParameters();
895 DVLOG(1) << in_params;
896 MakeAudioInputStreamOnAudioThread(in_params);
898 GetDefaultOutputStreamParametersOnAudioThread();
899 DVLOG(1) << audio_output_parameters();
900 MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
902 std::string file_name = base::StringPrintf("out_duplex_%d_%d_%d.pcm",
903 in_params.sample_rate(),
904 in_params.frames_per_buffer(),
905 in_params.channels());
907 base::WaitableEvent event(false, false);
908 FileAudioSink sink(&event, in_params, file_name);
909 MockAudioSourceCallback source;
911 EXPECT_CALL(source, OnMoreData(NotNull(), _))
912 .WillRepeatedly(Invoke(RealOnMoreData));
913 EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);
915 OpenAndStartAudioInputStreamOnAudioThread(&sink);
916 OpenAndStartAudioOutputStreamOnAudioThread(&source);
917 DVLOG(0) << ">> Speak into the microphone to record audio";
918 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
919 StopAndCloseAudioOutputStreamOnAudioThread();
920 StopAndCloseAudioInputStreamOnAudioThread();
923 // Start audio in both directions while feeding captured data into a FIFO so
924 // it can be read directly (in loopback) by the render side. A small extra
925 // delay will be added by the FIFO and an estimate of this delay will be
926 // printed out during the test.
927 // NOTE: this test requires user interaction and is not designed to run as an
928 // automatized test on bots.
929 TEST_P(AudioAndroidInputTest,
930 DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex) {
931 // Get native audio parameters for the input side.
932 AudioParameters default_input_params = GetInputStreamParameters();
934 // Modify the parameters so that both input and output can use the same
935 // parameters by selecting 10ms as buffer size. This will also ensure that
936 // the output stream will be a mono stream since mono is default for input
937 // audio on Android.
938 AudioParameters io_params(default_input_params.format(),
939 default_input_params.channel_layout(),
940 ChannelLayoutToChannelCount(
941 default_input_params.channel_layout()),
942 default_input_params.sample_rate(),
943 default_input_params.bits_per_sample(),
944 default_input_params.sample_rate() / 100,
945 default_input_params.effects());
946 DVLOG(1) << io_params;
948 // Create input and output streams using the common audio parameters.
949 MakeAudioInputStreamOnAudioThread(io_params);
950 MakeAudioOutputStreamOnAudioThread(io_params);
952 FullDuplexAudioSinkSource full_duplex(io_params);
954 // Start a full duplex audio session and print out estimates of the extra
955 // delay we should expect from the FIFO. If real-time delay measurements are
956 // performed, the result should be reduced by this extra delay since it is
957 // something that has been added by the test.
958 OpenAndStartAudioInputStreamOnAudioThread(&full_duplex);
959 OpenAndStartAudioOutputStreamOnAudioThread(&full_duplex);
960 DVLOG(1) << "HINT: an estimate of the extra FIFO delay will be updated "
961 << "once per second during this test.";
962 DVLOG(0) << ">> Speak into the mic and listen to the audio in loopback...";
963 fflush(stdout);
964 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20));
965 printf("\n");
966 StopAndCloseAudioOutputStreamOnAudioThread();
967 StopAndCloseAudioInputStreamOnAudioThread();
970 INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest,
971 testing::ValuesIn(RunAudioRecordInputPathTests()));
973 } // namespace media