audio_android_unittest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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. 4 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" 27 28using ::testing::_; 29using ::testing::AtLeast; 30using ::testing::DoAll; 31using ::testing::Invoke; 32using ::testing::NotNull; 33using ::testing::Return; 34 35namespace media { 36 37ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) { 38 if (++*count >= limit) { 39 loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); 40 } 41} 42 43static const char kSpeechFile_16b_s_48k[] = "speech_16b_stereo_48kHz.raw"; 44static const char kSpeechFile_16b_m_48k[] = "speech_16b_mono_48kHz.raw"; 45static const char kSpeechFile_16b_s_44k[] = "speech_16b_stereo_44kHz.raw"; 46static const char kSpeechFile_16b_m_44k[] = "speech_16b_mono_44kHz.raw"; 47 48static const float kCallbackTestTimeMs = 2000.0; 49static const int kBitsPerSample = 16; 50static const int kBytesPerSample = kBitsPerSample / 8; 51 52// Converts AudioParameters::Format enumerator to readable string. 53static std::string FormatToString(AudioParameters::Format format) { 54 switch (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"); 63 default: 64 return std::string(); 65 } 66} 67 68// Converts ChannelLayout enumerator to readable string. Does not include 69// multi-channel cases since these layouts are not supported on Android. 70static 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: 79 default: 80 return std::string("CHANNEL_LAYOUT_UNSUPPORTED"); 81 } 82} 83 84static 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(); 88} 89 90// Helper method which verifies that the device list starts with a valid 91// default device name followed by non-default device names. 92static 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"; 99 return; 100 } 101 102 AudioDeviceNames::const_iterator it = device_names.begin(); 103 104 // The first device in the list should always be the default device. 105 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName), 106 it->device_name); 107 EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id); 108 ++it; 109 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), 118 it->device_name); 119 EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId), 120 it->unique_id); 121 ++it; 122 } 123} 124 125// We clear the data bus to ensure that the test does not cause noise. 126static int RealOnMoreData(AudioBus* dest, AudioBuffersState buffers_state) { 127 dest->Zero(); 128 return dest->frames(); 129} 130 131std::ostream& operator<<(std::ostream& os, const AudioParameters& params) { 132 using namespace std; 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); 145 return os; 146} 147 148// Gmock implementation of AudioInputStream::AudioInputCallback. 149class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { 150 public: 151 MOCK_METHOD5(OnData, 152 void(AudioInputStream* stream, 153 const uint8* src, 154 uint32 size, 155 uint32 hardware_delay_bytes, 156 double volume)); 157 MOCK_METHOD1(OnError, void(AudioInputStream* stream)); 158}; 159 160// Implements AudioOutputStream::AudioSourceCallback and provides audio data 161// by reading from a data file. 162class FileAudioSource : public AudioOutputStream::AudioSourceCallback { 163 public: 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 167 // a DecoderBuffer. 168 file_ = ReadTestDataFile(name); 169 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(); 173 } 174 175 virtual ~FileAudioSource() {} 176 177 // AudioOutputStream::AudioSourceCallback implementation. 178 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; 184 int max_size = 185 audio_bus->frames() * audio_bus->channels() * kBytesPerSample; 186 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_; 191 } 192 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; 201 } 202 203 // Set event to ensure that the test can stop when the file has ended. 204 if (stop_playing) 205 event_->Signal(); 206 207 return frames; 208 } 209 210 virtual void OnError(AudioOutputStream* stream) OVERRIDE {} 211 212 int file_size() { return file_->data_size(); } 213 214 private: 215 base::WaitableEvent* event_; 216 int pos_; 217 scoped_refptr<DecoderBuffer> file_; 218 219 DISALLOW_COPY_AND_ASSIGN(FileAudioSource); 220}; 221 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. 227class 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)); 236 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(); 244 } 245 246 virtual ~FileAudioSink() { 247 int bytes_written = 0; 248 while (bytes_written < buffer_->forward_capacity()) { 249 const uint8* chunk; 250 int chunk_size; 251 252 // Stop writing if no more data is available. 253 if (!buffer_->GetCurrentChunk(&chunk, &chunk_size)) 254 break; 255 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; 261 } 262 base::CloseFile(binary_file_); 263 } 264 265 // AudioInputStream::AudioInputCallback implementation. 266 virtual void OnData(AudioInputStream* stream, 267 const uint8* src, 268 uint32 size, 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)) 275 event_->Signal(); 276 } 277 278 virtual void OnError(AudioInputStream* stream) OVERRIDE {} 279 280 private: 281 base::WaitableEvent* event_; 282 AudioParameters params_; 283 scoped_ptr<media::SeekableBuffer> buffer_; 284 FILE* binary_file_; 285 286 DISALLOW_COPY_AND_ASSIGN(FileAudioSink); 287}; 288 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. 292class FullDuplexAudioSinkSource 293 : public AudioInputStream::AudioInputCallback, 294 public AudioOutputStream::AudioSourceCallback { 295 public: 296 explicit FullDuplexAudioSinkSource(const AudioParameters& params) 297 : params_(params), 298 previous_time_(base::TimeTicks::Now()), 299 started_(false) { 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()]); 304 } 305 306 virtual ~FullDuplexAudioSinkSource() {} 307 308 // AudioInputStream::AudioInputCallback implementation 309 virtual void OnData(AudioInputStream* stream, 310 const uint8* src, 311 uint32 size, 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(); 316 317 base::AutoLock lock(lock_); 318 if (diff > 1000) { 319 started_ = true; 320 previous_time_ = now_time; 321 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; 327 } 328 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. 332 if (!started_) 333 return; 334 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()); 339 fifo_->Clear(); 340 } 341 } 342 343 virtual void OnError(AudioInputStream* stream) OVERRIDE {} 344 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()); 351 352 base::AutoLock lock(lock_); 353 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. 357 if (!started_) { 358 dest->Zero(); 359 return dest->frames(); 360 } 361 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) { 365 dest->Zero(); 366 } else { 367 fifo_->Read(buffer_.get(), size_in_bytes); 368 dest->FromInterleaved( 369 buffer_.get(), dest->frames(), params_.bits_per_sample() / 8); 370 } 371 372 return dest->frames(); 373 } 374 375 virtual void OnError(AudioOutputStream* stream) OVERRIDE {} 376 377 private: 378 // Converts from bytes to milliseconds given number of bytes and existing 379 // audio parameters. 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(); 385 } 386 387 AudioParameters params_; 388 base::TimeTicks previous_time_; 389 base::Lock lock_; 390 scoped_ptr<media::SeekableBuffer> fifo_; 391 scoped_ptr<uint8[]> buffer_; 392 bool started_; 393 394 DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource); 395}; 396 397// Test fixture class for tests which only exercise the output path. 398class AudioAndroidOutputTest : public testing::Test { 399 public: 400 AudioAndroidOutputTest() 401 : loop_(new base::MessageLoopForUI()), 402 audio_manager_(AudioManager::CreateForTesting()), 403 audio_output_stream_(NULL) { 404 } 405 406 virtual ~AudioAndroidOutputTest() { 407 } 408 409 protected: 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_; 414 } 415 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( 421 FROM_HERE, 422 base::Bind(&AudioAndroidOutputTest::RunOnAudioThreadImpl, 423 base::Unretained(this), 424 closure, 425 &event)); 426 event.Wait(); 427 } else { 428 closure.Run(); 429 } 430 } 431 432 void RunOnAudioThreadImpl(const base::Closure& closure, 433 base::WaitableEvent* event) { 434 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 435 closure.Run(); 436 event->Signal(); 437 } 438 439 void GetDefaultOutputStreamParametersOnAudioThread() { 440 RunOnAudioThread( 441 base::Bind(&AudioAndroidOutputTest::GetDefaultOutputStreamParameters, 442 base::Unretained(this))); 443 } 444 445 void MakeAudioOutputStreamOnAudioThread(const AudioParameters& params) { 446 RunOnAudioThread( 447 base::Bind(&AudioAndroidOutputTest::MakeOutputStream, 448 base::Unretained(this), 449 params)); 450 } 451 452 void OpenAndCloseAudioOutputStreamOnAudioThread() { 453 RunOnAudioThread( 454 base::Bind(&AudioAndroidOutputTest::OpenAndClose, 455 base::Unretained(this))); 456 } 457 458 void OpenAndStartAudioOutputStreamOnAudioThread( 459 AudioOutputStream::AudioSourceCallback* source) { 460 RunOnAudioThread( 461 base::Bind(&AudioAndroidOutputTest::OpenAndStart, 462 base::Unretained(this), 463 source)); 464 } 465 466 void StopAndCloseAudioOutputStreamOnAudioThread() { 467 RunOnAudioThread( 468 base::Bind(&AudioAndroidOutputTest::StopAndClose, 469 base::Unretained(this))); 470 } 471 472 double AverageTimeBetweenCallbacks(int num_callbacks) const { 473 return ((end_time_ - start_time_) / static_cast<double>(num_callbacks - 1)) 474 .InMillisecondsF(); 475 } 476 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); 483 484 int count = 0; 485 MockAudioSourceCallback source; 486 487 EXPECT_CALL(source, OnMoreData(NotNull(), _)) 488 .Times(AtLeast(num_callbacks)) 489 .WillRepeatedly( 490 DoAll(CheckCountAndPostQuitTask(&count, num_callbacks, loop()), 491 Invoke(RealOnMoreData))); 492 EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0); 493 494 OpenAndStartAudioOutputStreamOnAudioThread(&source); 495 496 start_time_ = base::TimeTicks::Now(); 497 loop()->Run(); 498 end_time_ = base::TimeTicks::Now(); 499 500 StopAndCloseAudioOutputStreamOnAudioThread(); 501 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); 512 } 513 514 void GetDefaultOutputStreamParameters() { 515 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 516 audio_output_parameters_ = 517 audio_manager()->GetDefaultOutputStreamParameters(); 518 EXPECT_TRUE(audio_output_parameters_.IsValid()); 519 } 520 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_); 526 } 527 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; 533 } 534 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); 539 } 540 541 void StopAndClose() { 542 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 543 audio_output_stream_->Stop(); 544 audio_output_stream_->Close(); 545 audio_output_stream_ = NULL; 546 } 547 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_; 554 555 private: 556 DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest); 557}; 558 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. 561std::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); 566 return tests; 567} 568 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. 572class AudioAndroidInputTest : public AudioAndroidOutputTest, 573 public testing::WithParamInterface<bool> { 574 public: 575 AudioAndroidInputTest() : audio_input_stream_(NULL) {} 576 577 protected: 578 const AudioParameters& audio_input_parameters() { 579 return audio_input_parameters_; 580 } 581 582 AudioParameters GetInputStreamParameters() { 583 GetDefaultInputStreamParametersOnAudioThread(); 584 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(), 595 effects); 596 return params; 597 } 598 599 void GetDefaultInputStreamParametersOnAudioThread() { 600 RunOnAudioThread( 601 base::Bind(&AudioAndroidInputTest::GetDefaultInputStreamParameters, 602 base::Unretained(this))); 603 } 604 605 void MakeAudioInputStreamOnAudioThread(const AudioParameters& params) { 606 RunOnAudioThread( 607 base::Bind(&AudioAndroidInputTest::MakeInputStream, 608 base::Unretained(this), 609 params)); 610 } 611 612 void OpenAndCloseAudioInputStreamOnAudioThread() { 613 RunOnAudioThread( 614 base::Bind(&AudioAndroidInputTest::OpenAndClose, 615 base::Unretained(this))); 616 } 617 618 void OpenAndStartAudioInputStreamOnAudioThread( 619 AudioInputStream::AudioInputCallback* sink) { 620 RunOnAudioThread( 621 base::Bind(&AudioAndroidInputTest::OpenAndStart, 622 base::Unretained(this), 623 sink)); 624 } 625 626 void StopAndCloseAudioInputStreamOnAudioThread() { 627 RunOnAudioThread( 628 base::Bind(&AudioAndroidInputTest::StopAndClose, 629 base::Unretained(this))); 630 } 631 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); 637 638 MakeAudioInputStreamOnAudioThread(params); 639 640 int count = 0; 641 MockAudioInputCallback sink; 642 643 EXPECT_CALL(sink, 644 OnData(audio_input_stream_, 645 NotNull(), 646 params. 647 GetBytesPerBuffer(), _, _)) 648 .Times(AtLeast(num_callbacks)) 649 .WillRepeatedly( 650 CheckCountAndPostQuitTask(&count, num_callbacks, loop())); 651 EXPECT_CALL(sink, OnError(audio_input_stream_)).Times(0); 652 653 OpenAndStartAudioInputStreamOnAudioThread(&sink); 654 655 start_time_ = base::TimeTicks::Now(); 656 loop()->Run(); 657 end_time_ = base::TimeTicks::Now(); 658 659 StopAndCloseAudioInputStreamOnAudioThread(); 660 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); 671 } 672 673 void GetDefaultInputStreamParameters() { 674 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 675 audio_input_parameters_ = audio_manager()->GetInputStreamParameters( 676 AudioManagerBase::kDefaultDeviceId); 677 } 678 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_); 684 } 685 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; 691 } 692 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); 697 } 698 699 void StopAndClose() { 700 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 701 audio_input_stream_->Stop(); 702 audio_input_stream_->Close(); 703 audio_input_stream_ = NULL; 704 } 705 706 AudioInputStream* audio_input_stream_; 707 AudioParameters audio_input_parameters_; 708 709 private: 710 DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest); 711}; 712 713// Get the default audio input parameters and log the result. 714TEST_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(); 720} 721 722// Get the default audio output parameters and log the result. 723TEST_F(AudioAndroidOutputTest, GetDefaultOutputStreamParameters) { 724 GetDefaultOutputStreamParametersOnAudioThread(); 725 VLOG(1) << audio_output_parameters(); 726} 727 728// Verify input device enumeration. 729TEST_F(AudioAndroidInputTest, GetAudioInputDeviceNames) { 730 if (!audio_manager()->HasAudioInputDevices()) 731 return; 732 AudioDeviceNames devices; 733 RunOnAudioThread( 734 base::Bind(&AudioManager::GetAudioInputDeviceNames, 735 base::Unretained(audio_manager()), 736 &devices)); 737 CheckDeviceNames(devices); 738} 739 740// Verify output device enumeration. 741TEST_F(AudioAndroidOutputTest, GetAudioOutputDeviceNames) { 742 if (!audio_manager()->HasAudioOutputDevices()) 743 return; 744 AudioDeviceNames devices; 745 RunOnAudioThread( 746 base::Bind(&AudioManager::GetAudioOutputDeviceNames, 747 base::Unretained(audio_manager()), 748 &devices)); 749 CheckDeviceNames(devices); 750} 751 752// Ensure that a default input stream can be created and closed. 753TEST_P(AudioAndroidInputTest, CreateAndCloseInputStream) { 754 AudioParameters params = GetInputStreamParameters(); 755 MakeAudioInputStreamOnAudioThread(params); 756 RunOnAudioThread( 757 base::Bind(&AudioInputStream::Close, 758 base::Unretained(audio_input_stream_))); 759} 760 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 764// it is called? 765TEST_F(AudioAndroidOutputTest, CreateAndCloseOutputStream) { 766 GetDefaultOutputStreamParametersOnAudioThread(); 767 MakeAudioOutputStreamOnAudioThread(audio_output_parameters()); 768 RunOnAudioThread( 769 base::Bind(&AudioOutputStream::Close, 770 base::Unretained(audio_output_stream_))); 771} 772 773// Ensure that a default input stream can be opened and closed. 774TEST_P(AudioAndroidInputTest, OpenAndCloseInputStream) { 775 AudioParameters params = GetInputStreamParameters(); 776 MakeAudioInputStreamOnAudioThread(params); 777 OpenAndCloseAudioInputStreamOnAudioThread(); 778} 779 780// Ensure that a default output stream can be opened and closed. 781TEST_F(AudioAndroidOutputTest, OpenAndCloseOutputStream) { 782 GetDefaultOutputStreamParametersOnAudioThread(); 783 MakeAudioOutputStreamOnAudioThread(audio_output_parameters()); 784 OpenAndCloseAudioOutputStreamOnAudioThread(); 785} 786 787// Start input streaming using default input parameters and ensure that the 788// callback sequence is sane. 789TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacks) { 790 AudioParameters native_params = GetInputStreamParameters(); 791 StartInputStreamCallbacks(native_params); 792} 793 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. 797TEST_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); 808} 809 810// Start output streaming using default output parameters and ensure that the 811// callback sequence is sane. 812TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacks) { 813 GetDefaultOutputStreamParametersOnAudioThread(); 814 StartOutputStreamCallbacks(audio_output_parameters()); 815} 816 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 820// device in mono. 821// TODO(henrika): possibly add support for more variations. 822TEST_F(AudioAndroidOutputTest, DISABLED_StartOutputStreamCallbacksNonDefaultParameters) { 823 GetDefaultOutputStreamParametersOnAudioThread(); 824 AudioParameters params(audio_output_parameters().format(), 825 CHANNEL_LAYOUT_MONO, 826 audio_output_parameters().sample_rate(), 827 audio_output_parameters().bits_per_sample(), 828 audio_output_parameters().sample_rate() / 100); 829 StartOutputStreamCallbacks(params); 830} 831 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. 836TEST_F(AudioAndroidOutputTest, DISABLED_RunOutputStreamWithFileAsSource) { 837 GetDefaultOutputStreamParametersOnAudioThread(); 838 VLOG(1) << audio_output_parameters(); 839 MakeAudioOutputStreamOnAudioThread(audio_output_parameters()); 840 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; 851 } else { 852 FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only."; 853 return; 854 } 855 856 base::WaitableEvent event(false, false); 857 FileAudioSource source(&event, file_name); 858 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(); 863} 864 865// Start input streaming and run it for ten seconds while recording to a 866// local audio file. 867// NOTE: this test requires user interaction and is not designed to run as an 868// automatized test on bots. 869TEST_P(AudioAndroidInputTest, DISABLED_RunSimplexInputStreamWithFileAsSink) { 870 AudioParameters params = GetInputStreamParameters(); 871 VLOG(1) << params; 872 MakeAudioInputStreamOnAudioThread(params); 873 874 std::string file_name = base::StringPrintf("out_simplex_%d_%d_%d.pcm", 875 params.sample_rate(), 876 params.frames_per_buffer(), 877 params.channels()); 878 879 base::WaitableEvent event(false, false); 880 FileAudioSink sink(&event, params, file_name); 881 882 OpenAndStartAudioInputStreamOnAudioThread(&sink); 883 VLOG(0) << ">> Speak into the microphone to record audio..."; 884 EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout())); 885 StopAndCloseAudioInputStreamOnAudioThread(); 886} 887 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. 892TEST_P(AudioAndroidInputTest, DISABLED_RunDuplexInputStreamWithFileAsSink) { 893 AudioParameters in_params = GetInputStreamParameters(); 894 VLOG(1) << in_params; 895 MakeAudioInputStreamOnAudioThread(in_params); 896 897 GetDefaultOutputStreamParametersOnAudioThread(); 898 VLOG(1) << audio_output_parameters(); 899 MakeAudioOutputStreamOnAudioThread(audio_output_parameters()); 900 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()); 905 906 base::WaitableEvent event(false, false); 907 FileAudioSink sink(&event, in_params, file_name); 908 MockAudioSourceCallback source; 909 910 EXPECT_CALL(source, OnMoreData(NotNull(), _)) 911 .WillRepeatedly(Invoke(RealOnMoreData)); 912 EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0); 913 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(); 920} 921 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. 928TEST_P(AudioAndroidInputTest, 929 DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex) { 930 // Get native audio parameters for the input side. 931 AudioParameters default_input_params = GetInputStreamParameters(); 932 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 936 // audio on Android. 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; 946 947 // Create input and output streams using the common audio parameters. 948 MakeAudioInputStreamOnAudioThread(io_params); 949 MakeAudioOutputStreamOnAudioThread(io_params); 950 951 FullDuplexAudioSinkSource full_duplex(io_params); 952 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..."; 962 fflush(stdout); 963 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20)); 964 printf("\n"); 965 StopAndCloseAudioOutputStreamOnAudioThread(); 966 StopAndCloseAudioInputStreamOnAudioThread(); 967} 968 969INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest, 970 testing::ValuesIn(RunAudioRecordInputPathTests())); 971 972} // namespace media 973