This source file includes following definitions.
- ACTION_P3
- FormatToString
- LayoutToString
- ExpectedTimeBetweenCallbacks
- CheckDeviceNames
- RealOnMoreData
- pos_
- OnMoreData
- OnMoreIOData
- OnError
- file_size
- params_
- OnData
- OnError
- started_
- OnData
- OnError
- OnMoreData
- OnMoreIOData
- OnError
- BytesToMilliseconds
- audio_output_stream_
- audio_manager
- loop
- audio_output_parameters
- RunOnAudioThread
- RunOnAudioThreadImpl
- GetDefaultOutputStreamParametersOnAudioThread
- MakeAudioOutputStreamOnAudioThread
- OpenAndCloseAudioOutputStreamOnAudioThread
- OpenAndStartAudioOutputStreamOnAudioThread
- StopAndCloseAudioOutputStreamOnAudioThread
- AverageTimeBetweenCallbacks
- StartOutputStreamCallbacks
- GetDefaultOutputStreamParameters
- MakeOutputStream
- OpenAndClose
- OpenAndStart
- StopAndClose
- RunAudioRecordInputPathTests
- audio_input_parameters
- GetInputStreamParameters
- GetDefaultInputStreamParametersOnAudioThread
- MakeAudioInputStreamOnAudioThread
- OpenAndCloseAudioInputStreamOnAudioThread
- OpenAndStartAudioInputStreamOnAudioThread
- StopAndCloseAudioInputStreamOnAudioThread
- StartInputStreamCallbacks
- GetDefaultInputStreamParameters
- MakeInputStream
- OpenAndClose
- OpenAndStart
- StopAndClose
- TEST_P
- TEST_F
- TEST_F
- TEST_F
- TEST_P
- TEST_F
- TEST_P
- TEST_F
- TEST_P
- TEST_P
- TEST_F
- TEST_F
- TEST_F
- TEST_P
- TEST_P
- TEST_P
#include "base/android/build_info.h"
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/audio/android/audio_manager_android.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
#include "media/audio/mock_audio_source_callback.h"
#include "media/base/decoder_buffer.h"
#include "media/base/seekable_buffer.h"
#include "media/base/test_data_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::AtLeast;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::NotNull;
using ::testing::Return;
namespace media {
ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) {
if (++*count >= limit) {
loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure());
}
}
static const char kSpeechFile_16b_s_48k[] = "speech_16b_stereo_48kHz.raw";
static const char kSpeechFile_16b_m_48k[] = "speech_16b_mono_48kHz.raw";
static const char kSpeechFile_16b_s_44k[] = "speech_16b_stereo_44kHz.raw";
static const char kSpeechFile_16b_m_44k[] = "speech_16b_mono_44kHz.raw";
static const float kCallbackTestTimeMs = 2000.0;
static const int kBitsPerSample = 16;
static const int kBytesPerSample = kBitsPerSample / 8;
static std::string FormatToString(AudioParameters::Format format) {
switch (format) {
case AudioParameters::AUDIO_PCM_LINEAR:
return std::string("AUDIO_PCM_LINEAR");
case AudioParameters::AUDIO_PCM_LOW_LATENCY:
return std::string("AUDIO_PCM_LOW_LATENCY");
case AudioParameters::AUDIO_FAKE:
return std::string("AUDIO_FAKE");
case AudioParameters::AUDIO_LAST_FORMAT:
return std::string("AUDIO_LAST_FORMAT");
default:
return std::string();
}
}
static std::string LayoutToString(ChannelLayout channel_layout) {
switch (channel_layout) {
case CHANNEL_LAYOUT_NONE:
return std::string("CHANNEL_LAYOUT_NONE");
case CHANNEL_LAYOUT_MONO:
return std::string("CHANNEL_LAYOUT_MONO");
case CHANNEL_LAYOUT_STEREO:
return std::string("CHANNEL_LAYOUT_STEREO");
case CHANNEL_LAYOUT_UNSUPPORTED:
default:
return std::string("CHANNEL_LAYOUT_UNSUPPORTED");
}
}
static double ExpectedTimeBetweenCallbacks(AudioParameters params) {
return (base::TimeDelta::FromMicroseconds(
params.frames_per_buffer() * base::Time::kMicrosecondsPerSecond /
static_cast<double>(params.sample_rate()))).InMillisecondsF();
}
static void CheckDeviceNames(const AudioDeviceNames& device_names) {
VLOG(2) << "Got " << device_names.size() << " audio devices.";
if (device_names.empty()) {
LOG(WARNING) << "No input devices detected";
return;
}
AudioDeviceNames::const_iterator it = device_names.begin();
EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceName),
it->device_name);
EXPECT_EQ(std::string(AudioManagerBase::kDefaultDeviceId), it->unique_id);
++it;
while (it != device_names.end()) {
EXPECT_FALSE(it->device_name.empty());
EXPECT_FALSE(it->unique_id.empty());
VLOG(2) << "Device ID(" << it->unique_id
<< "), label: " << it->device_name;
EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceName),
it->device_name);
EXPECT_NE(std::string(AudioManagerBase::kDefaultDeviceId),
it->unique_id);
++it;
}
}
static int RealOnMoreData(AudioBus* dest, AudioBuffersState buffers_state) {
dest->Zero();
return dest->frames();
}
std::ostream& operator<<(std::ostream& os, const AudioParameters& params) {
using namespace std;
os << endl << "format: " << FormatToString(params.format()) << endl
<< "channel layout: " << LayoutToString(params.channel_layout()) << endl
<< "sample rate: " << params.sample_rate() << endl
<< "bits per sample: " << params.bits_per_sample() << endl
<< "frames per buffer: " << params.frames_per_buffer() << endl
<< "channels: " << params.channels() << endl
<< "bytes per buffer: " << params.GetBytesPerBuffer() << endl
<< "bytes per second: " << params.GetBytesPerSecond() << endl
<< "bytes per frame: " << params.GetBytesPerFrame() << endl
<< "chunk size in ms: " << ExpectedTimeBetweenCallbacks(params) << endl
<< "echo_canceller: "
<< (params.effects() & AudioParameters::ECHO_CANCELLER);
return os;
}
class MockAudioInputCallback : public AudioInputStream::AudioInputCallback {
public:
MOCK_METHOD5(OnData,
void(AudioInputStream* stream,
const uint8* src,
uint32 size,
uint32 hardware_delay_bytes,
double volume));
MOCK_METHOD1(OnError, void(AudioInputStream* stream));
};
class FileAudioSource : public AudioOutputStream::AudioSourceCallback {
public:
explicit FileAudioSource(base::WaitableEvent* event, const std::string& name)
: event_(event), pos_(0) {
file_ = ReadTestDataFile(name);
base::FilePath file_path = GetTestDataFilePath(name);
VLOG(0) << "Reading from file: " << file_path.value().c_str();
}
virtual ~FileAudioSource() {}
virtual int OnMoreData(AudioBus* audio_bus,
AudioBuffersState buffers_state) OVERRIDE {
bool stop_playing = false;
int max_size =
audio_bus->frames() * audio_bus->channels() * kBytesPerSample;
if (pos_ + max_size > file_size()) {
stop_playing = true;
max_size = file_size() - pos_;
}
int frames = max_size / (audio_bus->channels() * kBytesPerSample);
if (max_size) {
audio_bus->FromInterleaved(file_->data() + pos_, frames, kBytesPerSample);
pos_ += max_size;
}
if (stop_playing)
event_->Signal();
return frames;
}
virtual int OnMoreIOData(AudioBus* source,
AudioBus* dest,
AudioBuffersState buffers_state) OVERRIDE {
NOTREACHED();
return 0;
}
virtual void OnError(AudioOutputStream* stream) OVERRIDE {}
int file_size() { return file_->data_size(); }
private:
base::WaitableEvent* event_;
int pos_;
scoped_refptr<DecoderBuffer> file_;
DISALLOW_COPY_AND_ASSIGN(FileAudioSource);
};
class FileAudioSink : public AudioInputStream::AudioInputCallback {
public:
explicit FileAudioSink(base::WaitableEvent* event,
const AudioParameters& params,
const std::string& file_name)
: event_(event), params_(params) {
const int kMaxBufferSize = 10 * params.GetBytesPerSecond();
buffer_.reset(new media::SeekableBuffer(0, kMaxBufferSize));
base::FilePath file_path;
EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));
file_path = file_path.AppendASCII(file_name.c_str());
binary_file_ = base::OpenFile(file_path, "wb");
DLOG_IF(ERROR, !binary_file_) << "Failed to open binary PCM data file.";
VLOG(0) << "Writing to file: " << file_path.value().c_str();
}
virtual ~FileAudioSink() {
int bytes_written = 0;
while (bytes_written < buffer_->forward_capacity()) {
const uint8* chunk;
int chunk_size;
if (!buffer_->GetCurrentChunk(&chunk, &chunk_size))
break;
fwrite(chunk, 1, chunk_size, binary_file_);
buffer_->Seek(chunk_size);
bytes_written += chunk_size;
}
base::CloseFile(binary_file_);
}
virtual void OnData(AudioInputStream* stream,
const uint8* src,
uint32 size,
uint32 hardware_delay_bytes,
double volume) OVERRIDE {
if (!buffer_->Append(src, size))
event_->Signal();
}
virtual void OnError(AudioInputStream* stream) OVERRIDE {}
private:
base::WaitableEvent* event_;
AudioParameters params_;
scoped_ptr<media::SeekableBuffer> buffer_;
FILE* binary_file_;
DISALLOW_COPY_AND_ASSIGN(FileAudioSink);
};
class FullDuplexAudioSinkSource
: public AudioInputStream::AudioInputCallback,
public AudioOutputStream::AudioSourceCallback {
public:
explicit FullDuplexAudioSinkSource(const AudioParameters& params)
: params_(params),
previous_time_(base::TimeTicks::Now()),
started_(false) {
fifo_.reset(new media::SeekableBuffer(0, 2 * params.GetBytesPerBuffer()));
buffer_.reset(new uint8[params_.GetBytesPerBuffer()]);
}
virtual ~FullDuplexAudioSinkSource() {}
virtual void OnData(AudioInputStream* stream,
const uint8* src,
uint32 size,
uint32 hardware_delay_bytes,
double volume) OVERRIDE {
const base::TimeTicks now_time = base::TimeTicks::Now();
const int diff = (now_time - previous_time_).InMilliseconds();
base::AutoLock lock(lock_);
if (diff > 1000) {
started_ = true;
previous_time_ = now_time;
int extra_fifo_delay =
static_cast<int>(BytesToMilliseconds(fifo_->forward_bytes() + size));
DVLOG(1) << extra_fifo_delay;
}
if (!started_)
return;
if (!fifo_->Append(src, size)) {
fifo_->set_forward_capacity(2 * fifo_->forward_capacity());
fifo_->Clear();
}
}
virtual void OnError(AudioInputStream* stream) OVERRIDE {}
virtual int OnMoreData(AudioBus* dest,
AudioBuffersState buffers_state) OVERRIDE {
const int size_in_bytes =
(params_.bits_per_sample() / 8) * dest->frames() * dest->channels();
EXPECT_EQ(size_in_bytes, params_.GetBytesPerBuffer());
base::AutoLock lock(lock_);
if (!started_) {
dest->Zero();
return dest->frames();
}
if (fifo_->forward_bytes() < size_in_bytes) {
dest->Zero();
} else {
fifo_->Read(buffer_.get(), size_in_bytes);
dest->FromInterleaved(
buffer_.get(), dest->frames(), params_.bits_per_sample() / 8);
}
return dest->frames();
}
virtual int OnMoreIOData(AudioBus* source,
AudioBus* dest,
AudioBuffersState buffers_state) OVERRIDE {
NOTREACHED();
return 0;
}
virtual void OnError(AudioOutputStream* stream) OVERRIDE {}
private:
double BytesToMilliseconds(int bytes) const {
const int frames = bytes / params_.GetBytesPerFrame();
return (base::TimeDelta::FromMicroseconds(
frames * base::Time::kMicrosecondsPerSecond /
static_cast<double>(params_.sample_rate()))).InMillisecondsF();
}
AudioParameters params_;
base::TimeTicks previous_time_;
base::Lock lock_;
scoped_ptr<media::SeekableBuffer> fifo_;
scoped_ptr<uint8[]> buffer_;
bool started_;
DISALLOW_COPY_AND_ASSIGN(FullDuplexAudioSinkSource);
};
class AudioAndroidOutputTest : public testing::Test {
public:
AudioAndroidOutputTest()
: loop_(new base::MessageLoopForUI()),
audio_manager_(AudioManager::CreateForTesting()),
audio_output_stream_(NULL) {
}
virtual ~AudioAndroidOutputTest() {
}
protected:
AudioManager* audio_manager() { return audio_manager_.get(); }
base::MessageLoopForUI* loop() { return loop_.get(); }
const AudioParameters& audio_output_parameters() {
return audio_output_parameters_;
}
void RunOnAudioThread(const base::Closure& closure) {
if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
base::WaitableEvent event(false, false);
audio_manager()->GetTaskRunner()->PostTask(
FROM_HERE,
base::Bind(&AudioAndroidOutputTest::RunOnAudioThreadImpl,
base::Unretained(this),
closure,
&event));
event.Wait();
} else {
closure.Run();
}
}
void RunOnAudioThreadImpl(const base::Closure& closure,
base::WaitableEvent* event) {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
closure.Run();
event->Signal();
}
void GetDefaultOutputStreamParametersOnAudioThread() {
RunOnAudioThread(
base::Bind(&AudioAndroidOutputTest::GetDefaultOutputStreamParameters,
base::Unretained(this)));
}
void MakeAudioOutputStreamOnAudioThread(const AudioParameters& params) {
RunOnAudioThread(
base::Bind(&AudioAndroidOutputTest::MakeOutputStream,
base::Unretained(this),
params));
}
void OpenAndCloseAudioOutputStreamOnAudioThread() {
RunOnAudioThread(
base::Bind(&AudioAndroidOutputTest::OpenAndClose,
base::Unretained(this)));
}
void OpenAndStartAudioOutputStreamOnAudioThread(
AudioOutputStream::AudioSourceCallback* source) {
RunOnAudioThread(
base::Bind(&AudioAndroidOutputTest::OpenAndStart,
base::Unretained(this),
source));
}
void StopAndCloseAudioOutputStreamOnAudioThread() {
RunOnAudioThread(
base::Bind(&AudioAndroidOutputTest::StopAndClose,
base::Unretained(this)));
}
double AverageTimeBetweenCallbacks(int num_callbacks) const {
return ((end_time_ - start_time_) / static_cast<double>(num_callbacks - 1))
.InMillisecondsF();
}
void StartOutputStreamCallbacks(const AudioParameters& params) {
double expected_time_between_callbacks_ms =
ExpectedTimeBetweenCallbacks(params);
const int num_callbacks =
(kCallbackTestTimeMs / expected_time_between_callbacks_ms);
MakeAudioOutputStreamOnAudioThread(params);
int count = 0;
MockAudioSourceCallback source;
EXPECT_CALL(source, OnMoreData(NotNull(), _))
.Times(AtLeast(num_callbacks))
.WillRepeatedly(
DoAll(CheckCountAndPostQuitTask(&count, num_callbacks, loop()),
Invoke(RealOnMoreData)));
EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);
EXPECT_CALL(source, OnMoreIOData(_, _, _)).Times(0);
OpenAndStartAudioOutputStreamOnAudioThread(&source);
start_time_ = base::TimeTicks::Now();
loop()->Run();
end_time_ = base::TimeTicks::Now();
StopAndCloseAudioOutputStreamOnAudioThread();
double average_time_between_callbacks_ms =
AverageTimeBetweenCallbacks(num_callbacks);
VLOG(0) << "expected time between callbacks: "
<< expected_time_between_callbacks_ms << " ms";
VLOG(0) << "average time between callbacks: "
<< average_time_between_callbacks_ms << " ms";
EXPECT_GE(average_time_between_callbacks_ms,
0.70 * expected_time_between_callbacks_ms);
EXPECT_LE(average_time_between_callbacks_ms,
1.30 * expected_time_between_callbacks_ms);
}
void GetDefaultOutputStreamParameters() {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
audio_output_parameters_ =
audio_manager()->GetDefaultOutputStreamParameters();
EXPECT_TRUE(audio_output_parameters_.IsValid());
}
void MakeOutputStream(const AudioParameters& params) {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
audio_output_stream_ = audio_manager()->MakeAudioOutputStream(
params, std::string());
EXPECT_TRUE(audio_output_stream_);
}
void OpenAndClose() {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
EXPECT_TRUE(audio_output_stream_->Open());
audio_output_stream_->Close();
audio_output_stream_ = NULL;
}
void OpenAndStart(AudioOutputStream::AudioSourceCallback* source) {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
EXPECT_TRUE(audio_output_stream_->Open());
audio_output_stream_->Start(source);
}
void StopAndClose() {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
audio_output_stream_->Stop();
audio_output_stream_->Close();
audio_output_stream_ = NULL;
}
scoped_ptr<base::MessageLoopForUI> loop_;
scoped_ptr<AudioManager> audio_manager_;
AudioParameters audio_output_parameters_;
AudioOutputStream* audio_output_stream_;
base::TimeTicks start_time_;
base::TimeTicks end_time_;
private:
DISALLOW_COPY_AND_ASSIGN(AudioAndroidOutputTest);
};
std::vector<bool> RunAudioRecordInputPathTests() {
std::vector<bool> tests;
tests.push_back(false);
if (base::android::BuildInfo::GetInstance()->sdk_int() >= 16)
tests.push_back(true);
return tests;
}
class AudioAndroidInputTest : public AudioAndroidOutputTest,
public testing::WithParamInterface<bool> {
public:
AudioAndroidInputTest() : audio_input_stream_(NULL) {}
protected:
const AudioParameters& audio_input_parameters() {
return audio_input_parameters_;
}
AudioParameters GetInputStreamParameters() {
GetDefaultInputStreamParametersOnAudioThread();
int effects = GetParam() ? AudioParameters::ECHO_CANCELLER :
AudioParameters::NO_EFFECTS;
AudioParameters params(audio_input_parameters().format(),
audio_input_parameters().channel_layout(),
audio_input_parameters().input_channels(),
audio_input_parameters().sample_rate(),
audio_input_parameters().bits_per_sample(),
audio_input_parameters().frames_per_buffer(),
effects);
return params;
}
void GetDefaultInputStreamParametersOnAudioThread() {
RunOnAudioThread(
base::Bind(&AudioAndroidInputTest::GetDefaultInputStreamParameters,
base::Unretained(this)));
}
void MakeAudioInputStreamOnAudioThread(const AudioParameters& params) {
RunOnAudioThread(
base::Bind(&AudioAndroidInputTest::MakeInputStream,
base::Unretained(this),
params));
}
void OpenAndCloseAudioInputStreamOnAudioThread() {
RunOnAudioThread(
base::Bind(&AudioAndroidInputTest::OpenAndClose,
base::Unretained(this)));
}
void OpenAndStartAudioInputStreamOnAudioThread(
AudioInputStream::AudioInputCallback* sink) {
RunOnAudioThread(
base::Bind(&AudioAndroidInputTest::OpenAndStart,
base::Unretained(this),
sink));
}
void StopAndCloseAudioInputStreamOnAudioThread() {
RunOnAudioThread(
base::Bind(&AudioAndroidInputTest::StopAndClose,
base::Unretained(this)));
}
void StartInputStreamCallbacks(const AudioParameters& params) {
double expected_time_between_callbacks_ms =
ExpectedTimeBetweenCallbacks(params);
const int num_callbacks =
(kCallbackTestTimeMs / expected_time_between_callbacks_ms);
MakeAudioInputStreamOnAudioThread(params);
int count = 0;
MockAudioInputCallback sink;
EXPECT_CALL(sink,
OnData(audio_input_stream_,
NotNull(),
params.
GetBytesPerBuffer(), _, _))
.Times(AtLeast(num_callbacks))
.WillRepeatedly(
CheckCountAndPostQuitTask(&count, num_callbacks, loop()));
EXPECT_CALL(sink, OnError(audio_input_stream_)).Times(0);
OpenAndStartAudioInputStreamOnAudioThread(&sink);
start_time_ = base::TimeTicks::Now();
loop()->Run();
end_time_ = base::TimeTicks::Now();
StopAndCloseAudioInputStreamOnAudioThread();
double average_time_between_callbacks_ms =
AverageTimeBetweenCallbacks(num_callbacks);
VLOG(0) << "expected time between callbacks: "
<< expected_time_between_callbacks_ms << " ms";
VLOG(0) << "average time between callbacks: "
<< average_time_between_callbacks_ms << " ms";
EXPECT_GE(average_time_between_callbacks_ms,
0.70 * expected_time_between_callbacks_ms);
EXPECT_LE(average_time_between_callbacks_ms,
1.30 * expected_time_between_callbacks_ms);
}
void GetDefaultInputStreamParameters() {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
audio_input_parameters_ = audio_manager()->GetInputStreamParameters(
AudioManagerBase::kDefaultDeviceId);
}
void MakeInputStream(const AudioParameters& params) {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
audio_input_stream_ = audio_manager()->MakeAudioInputStream(
params, AudioManagerBase::kDefaultDeviceId);
EXPECT_TRUE(audio_input_stream_);
}
void OpenAndClose() {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
EXPECT_TRUE(audio_input_stream_->Open());
audio_input_stream_->Close();
audio_input_stream_ = NULL;
}
void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
EXPECT_TRUE(audio_input_stream_->Open());
audio_input_stream_->Start(sink);
}
void StopAndClose() {
DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
audio_input_stream_->Stop();
audio_input_stream_->Close();
audio_input_stream_ = NULL;
}
AudioInputStream* audio_input_stream_;
AudioParameters audio_input_parameters_;
private:
DISALLOW_COPY_AND_ASSIGN(AudioAndroidInputTest);
};
TEST_P(AudioAndroidInputTest, GetDefaultInputStreamParameters) {
GetDefaultInputStreamParametersOnAudioThread();
EXPECT_TRUE(audio_input_parameters().IsValid());
VLOG(1) << audio_input_parameters();
}
TEST_F(AudioAndroidOutputTest, GetDefaultOutputStreamParameters) {
GetDefaultOutputStreamParametersOnAudioThread();
VLOG(1) << audio_output_parameters();
}
TEST_F(AudioAndroidInputTest, GetAudioInputDeviceNames) {
if (!audio_manager()->HasAudioInputDevices())
return;
AudioDeviceNames devices;
RunOnAudioThread(
base::Bind(&AudioManager::GetAudioInputDeviceNames,
base::Unretained(audio_manager()),
&devices));
CheckDeviceNames(devices);
}
TEST_F(AudioAndroidOutputTest, GetAudioOutputDeviceNames) {
if (!audio_manager()->HasAudioOutputDevices())
return;
AudioDeviceNames devices;
RunOnAudioThread(
base::Bind(&AudioManager::GetAudioOutputDeviceNames,
base::Unretained(audio_manager()),
&devices));
CheckDeviceNames(devices);
}
TEST_P(AudioAndroidInputTest, CreateAndCloseInputStream) {
AudioParameters params = GetInputStreamParameters();
MakeAudioInputStreamOnAudioThread(params);
RunOnAudioThread(
base::Bind(&AudioInputStream::Close,
base::Unretained(audio_input_stream_)));
}
TEST_F(AudioAndroidOutputTest, CreateAndCloseOutputStream) {
GetDefaultOutputStreamParametersOnAudioThread();
MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
RunOnAudioThread(
base::Bind(&AudioOutputStream::Close,
base::Unretained(audio_output_stream_)));
}
TEST_P(AudioAndroidInputTest, OpenAndCloseInputStream) {
AudioParameters params = GetInputStreamParameters();
MakeAudioInputStreamOnAudioThread(params);
OpenAndCloseAudioInputStreamOnAudioThread();
}
TEST_F(AudioAndroidOutputTest, OpenAndCloseOutputStream) {
GetDefaultOutputStreamParametersOnAudioThread();
MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
OpenAndCloseAudioOutputStreamOnAudioThread();
}
TEST_P(AudioAndroidInputTest, DISABLED_StartInputStreamCallbacks) {
AudioParameters native_params = GetInputStreamParameters();
StartInputStreamCallbacks(native_params);
}
TEST_P(AudioAndroidInputTest,
DISABLED_StartInputStreamCallbacksNonDefaultParameters) {
AudioParameters native_params = GetInputStreamParameters();
AudioParameters params(native_params.format(),
native_params.channel_layout(),
native_params.input_channels(),
native_params.sample_rate(),
native_params.bits_per_sample(),
native_params.sample_rate() / 100,
native_params.effects());
StartInputStreamCallbacks(params);
}
TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacks) {
GetDefaultOutputStreamParametersOnAudioThread();
StartOutputStreamCallbacks(audio_output_parameters());
}
TEST_F(AudioAndroidOutputTest, StartOutputStreamCallbacksNonDefaultParameters) {
GetDefaultOutputStreamParametersOnAudioThread();
AudioParameters params(audio_output_parameters().format(),
CHANNEL_LAYOUT_MONO,
audio_output_parameters().sample_rate(),
audio_output_parameters().bits_per_sample(),
audio_output_parameters().sample_rate() / 100);
StartOutputStreamCallbacks(params);
}
TEST_F(AudioAndroidOutputTest, DISABLED_RunOutputStreamWithFileAsSource) {
GetDefaultOutputStreamParametersOnAudioThread();
VLOG(1) << audio_output_parameters();
MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
std::string file_name;
const AudioParameters params = audio_output_parameters();
if (params.sample_rate() == 48000 && params.channels() == 2) {
file_name = kSpeechFile_16b_s_48k;
} else if (params.sample_rate() == 48000 && params.channels() == 1) {
file_name = kSpeechFile_16b_m_48k;
} else if (params.sample_rate() == 44100 && params.channels() == 2) {
file_name = kSpeechFile_16b_s_44k;
} else if (params.sample_rate() == 44100 && params.channels() == 1) {
file_name = kSpeechFile_16b_m_44k;
} else {
FAIL() << "This test supports 44.1kHz and 48kHz mono/stereo only.";
return;
}
base::WaitableEvent event(false, false);
FileAudioSource source(&event, file_name);
OpenAndStartAudioOutputStreamOnAudioThread(&source);
VLOG(0) << ">> Verify that the file is played out correctly...";
EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
StopAndCloseAudioOutputStreamOnAudioThread();
}
TEST_P(AudioAndroidInputTest, DISABLED_RunSimplexInputStreamWithFileAsSink) {
AudioParameters params = GetInputStreamParameters();
VLOG(1) << params;
MakeAudioInputStreamOnAudioThread(params);
std::string file_name = base::StringPrintf("out_simplex_%d_%d_%d.pcm",
params.sample_rate(),
params.frames_per_buffer(),
params.channels());
base::WaitableEvent event(false, false);
FileAudioSink sink(&event, params, file_name);
OpenAndStartAudioInputStreamOnAudioThread(&sink);
VLOG(0) << ">> Speak into the microphone to record audio...";
EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
StopAndCloseAudioInputStreamOnAudioThread();
}
TEST_P(AudioAndroidInputTest, DISABLED_RunDuplexInputStreamWithFileAsSink) {
AudioParameters in_params = GetInputStreamParameters();
VLOG(1) << in_params;
MakeAudioInputStreamOnAudioThread(in_params);
GetDefaultOutputStreamParametersOnAudioThread();
VLOG(1) << audio_output_parameters();
MakeAudioOutputStreamOnAudioThread(audio_output_parameters());
std::string file_name = base::StringPrintf("out_duplex_%d_%d_%d.pcm",
in_params.sample_rate(),
in_params.frames_per_buffer(),
in_params.channels());
base::WaitableEvent event(false, false);
FileAudioSink sink(&event, in_params, file_name);
MockAudioSourceCallback source;
EXPECT_CALL(source, OnMoreData(NotNull(), _))
.WillRepeatedly(Invoke(RealOnMoreData));
EXPECT_CALL(source, OnError(audio_output_stream_)).Times(0);
EXPECT_CALL(source, OnMoreIOData(_, _, _)).Times(0);
OpenAndStartAudioInputStreamOnAudioThread(&sink);
OpenAndStartAudioOutputStreamOnAudioThread(&source);
VLOG(0) << ">> Speak into the microphone to record audio";
EXPECT_TRUE(event.TimedWait(TestTimeouts::action_max_timeout()));
StopAndCloseAudioOutputStreamOnAudioThread();
StopAndCloseAudioInputStreamOnAudioThread();
}
TEST_P(AudioAndroidInputTest,
DISABLED_RunSymmetricInputAndOutputStreamsInFullDuplex) {
AudioParameters default_input_params = GetInputStreamParameters();
AudioParameters io_params(default_input_params.format(),
default_input_params.channel_layout(),
ChannelLayoutToChannelCount(
default_input_params.channel_layout()),
default_input_params.sample_rate(),
default_input_params.bits_per_sample(),
default_input_params.sample_rate() / 100,
default_input_params.effects());
VLOG(1) << io_params;
MakeAudioInputStreamOnAudioThread(io_params);
MakeAudioOutputStreamOnAudioThread(io_params);
FullDuplexAudioSinkSource full_duplex(io_params);
OpenAndStartAudioInputStreamOnAudioThread(&full_duplex);
OpenAndStartAudioOutputStreamOnAudioThread(&full_duplex);
VLOG(1) << "HINT: an estimate of the extra FIFO delay will be updated "
<< "once per second during this test.";
VLOG(0) << ">> Speak into the mic and listen to the audio in loopback...";
fflush(stdout);
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(20));
printf("\n");
StopAndCloseAudioOutputStreamOnAudioThread();
StopAndCloseAudioInputStreamOnAudioThread();
}
INSTANTIATE_TEST_CASE_P(AudioAndroidInputTest, AudioAndroidInputTest,
testing::ValuesIn(RunAudioRecordInputPathTests()));
}