This source file includes following definitions.
- half_fill_
- GetMixer
- InitializeInputs
- ValidateAudioData
- ValidateAudioData
- RenderAndValidateAudioData
- FillAudioData
- StartTest
- PlayTest
- PlayVolumeAdjustedTest
- PlayPartialRenderTest
- PauseTest
- StopTest
- ACTION_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
- TEST_P
#define _USE_MATH_DEFINES
#include <cmath>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h"
#include "media/base/audio_renderer_mixer.h"
#include "media/base/audio_renderer_mixer_input.h"
#include "media/base/fake_audio_render_callback.h"
#include "media/base/mock_audio_renderer_sink.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
const int kMixerInputs = 8;
const int kMixerCycles = 3;
const int kBitsPerChannel = 32;
const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
const int kHighLatencyBufferSize = 8192;
const int kLowLatencyBufferSize = 256;
const int kSineCycles = 4;
typedef std::tr1::tuple<int, int, double> AudioRendererMixerTestData;
class AudioRendererMixerTest
: public testing::TestWithParam<AudioRendererMixerTestData> {
public:
AudioRendererMixerTest()
: epsilon_(std::tr1::get<2>(GetParam())),
half_fill_(false) {
input_parameters_ = AudioParameters(
AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
std::tr1::get<0>(GetParam()), kBitsPerChannel, kHighLatencyBufferSize);
output_parameters_ = AudioParameters(
AudioParameters::AUDIO_PCM_LOW_LATENCY, kChannelLayout,
std::tr1::get<1>(GetParam()), 16, kLowLatencyBufferSize);
sink_ = new MockAudioRendererSink();
EXPECT_CALL(*sink_.get(), Start());
EXPECT_CALL(*sink_.get(), Stop());
mixer_.reset(new AudioRendererMixer(
input_parameters_, output_parameters_, sink_));
mixer_callback_ = sink_->callback();
audio_bus_ = AudioBus::Create(output_parameters_);
expected_audio_bus_ = AudioBus::Create(output_parameters_);
double step = kSineCycles / static_cast<double>(
output_parameters_.frames_per_buffer());
expected_callback_.reset(new FakeAudioRenderCallback(step));
}
AudioRendererMixer* GetMixer(const AudioParameters& params) {
return mixer_.get();
}
MOCK_METHOD1(RemoveMixer, void(const AudioParameters&));
void InitializeInputs(int count) {
mixer_inputs_.reserve(count);
fake_callbacks_.reserve(count);
double scale_factor = input_parameters_.sample_rate() /
static_cast<double>(output_parameters_.sample_rate());
double step = kSineCycles / (scale_factor *
static_cast<double>(output_parameters_.frames_per_buffer()));
for (int i = 0; i < count; ++i) {
fake_callbacks_.push_back(new FakeAudioRenderCallback(step));
mixer_inputs_.push_back(new AudioRendererMixerInput(
base::Bind(&AudioRendererMixerTest::GetMixer,
base::Unretained(this)),
base::Bind(&AudioRendererMixerTest::RemoveMixer,
base::Unretained(this))));
mixer_inputs_[i]->Initialize(input_parameters_, fake_callbacks_[i]);
mixer_inputs_[i]->SetVolume(1.0f);
}
EXPECT_CALL(*this, RemoveMixer(testing::_)).Times(count);
}
bool ValidateAudioData(int index, int frames, float scale, double epsilon) {
for (int i = 0; i < audio_bus_->channels(); ++i) {
for (int j = index; j < frames; j++) {
double error = fabs(audio_bus_->channel(i)[j] -
expected_audio_bus_->channel(i)[j] * scale);
if (error > epsilon) {
EXPECT_NEAR(expected_audio_bus_->channel(i)[j] * scale,
audio_bus_->channel(i)[j], epsilon)
<< " i=" << i << ", j=" << j;
return false;
}
}
}
return true;
}
bool ValidateAudioData(int index, int frames, float scale) {
return ValidateAudioData(index, frames, scale, epsilon_);
}
bool RenderAndValidateAudioData(float scale) {
if (half_fill_) {
for (size_t i = 0; i < fake_callbacks_.size(); ++i)
fake_callbacks_[i]->set_half_fill(true);
expected_callback_->set_half_fill(true);
expected_audio_bus_->Zero();
}
int frames = mixer_callback_->Render(audio_bus_.get(), 0);
if (frames != audio_bus_->frames())
return false;
expected_callback_->Render(expected_audio_bus_.get(), 0);
if (half_fill_) {
return ValidateAudioData(
0, frames, 0, std::numeric_limits<double>::max());
} else {
return ValidateAudioData(0, frames, scale);
}
}
void FillAudioData(float value) {
for (int i = 0; i < audio_bus_->channels(); ++i) {
std::fill(audio_bus_->channel(i),
audio_bus_->channel(i) + audio_bus_->frames(), value);
}
}
void StartTest(int inputs) {
InitializeInputs(inputs);
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
for (size_t i = 0; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Start();
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
for (size_t i = 1; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Start();
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
void PlayTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
for (int i = 0; i < kMixerCycles; ++i)
ASSERT_TRUE(RenderAndValidateAudioData(mixer_inputs_.size()));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
void PlayVolumeAdjustedTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
float total_scale = 0;
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
float volume = static_cast<float>(i) / mixer_inputs_.size();
total_scale += volume;
EXPECT_TRUE(mixer_inputs_[i]->SetVolume(volume));
}
for (int i = 0; i < kMixerCycles; ++i)
ASSERT_TRUE(RenderAndValidateAudioData(total_scale));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
void PlayPartialRenderTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
half_fill_ = true;
ASSERT_TRUE(RenderAndValidateAudioData(mixer_inputs_.size()));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
void PauseTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
for (size_t i = 0; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Pause();
for (int i = 0; i < kMixerCycles; ++i)
ASSERT_TRUE(RenderAndValidateAudioData(mixer_inputs_.size() / 2));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
void StopTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Stop();
}
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
}
protected:
virtual ~AudioRendererMixerTest() {}
scoped_refptr<MockAudioRendererSink> sink_;
scoped_ptr<AudioRendererMixer> mixer_;
AudioRendererSink::RenderCallback* mixer_callback_;
AudioParameters input_parameters_;
AudioParameters output_parameters_;
scoped_ptr<AudioBus> audio_bus_;
scoped_ptr<AudioBus> expected_audio_bus_;
std::vector< scoped_refptr<AudioRendererMixerInput> > mixer_inputs_;
ScopedVector<FakeAudioRenderCallback> fake_callbacks_;
scoped_ptr<FakeAudioRenderCallback> expected_callback_;
double epsilon_;
bool half_fill_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerTest);
};
class AudioRendererMixerBehavioralTest : public AudioRendererMixerTest {};
ACTION_P(SignalEvent, event) {
event->Signal();
}
TEST_P(AudioRendererMixerTest, NoInputs) {
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
}
TEST_P(AudioRendererMixerTest, OneInputStart) {
StartTest(1);
}
TEST_P(AudioRendererMixerTest, ManyInputStart) {
StartTest(kMixerInputs);
}
TEST_P(AudioRendererMixerTest, OneInputPlay) {
PlayTest(1);
}
TEST_P(AudioRendererMixerTest, ManyInputPlay) {
PlayTest(kMixerInputs);
}
TEST_P(AudioRendererMixerTest, OneInputPlayVolumeAdjusted) {
PlayVolumeAdjustedTest(1);
}
TEST_P(AudioRendererMixerTest, ManyInputPlayVolumeAdjusted) {
PlayVolumeAdjustedTest(kMixerInputs);
}
TEST_P(AudioRendererMixerTest, OneInputPlayPartialRender) {
PlayPartialRenderTest(1);
}
TEST_P(AudioRendererMixerTest, ManyInputPlayPartialRender) {
PlayPartialRenderTest(kMixerInputs);
}
TEST_P(AudioRendererMixerTest, OneInputPause) {
PauseTest(1);
}
TEST_P(AudioRendererMixerTest, ManyInputPause) {
PauseTest(kMixerInputs);
}
TEST_P(AudioRendererMixerTest, OneInputStop) {
StopTest(1);
}
TEST_P(AudioRendererMixerTest, ManyInputStop) {
StopTest(kMixerInputs);
}
TEST_P(AudioRendererMixerTest, ManyInputMixedStopPlay) {
InitializeInputs(kMixerInputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Start();
for (size_t i = 1; i < mixer_inputs_.size(); i += 2) {
mixer_inputs_[i - 1]->Stop();
mixer_inputs_[i]->Play();
}
ASSERT_TRUE(RenderAndValidateAudioData(std::max(
mixer_inputs_.size() / 2, static_cast<size_t>(1))));
for (size_t i = 1; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Stop();
}
TEST_P(AudioRendererMixerBehavioralTest, OnRenderError) {
InitializeInputs(kMixerInputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
EXPECT_CALL(*fake_callbacks_[i], OnRenderError()).Times(1);
}
mixer_callback_->OnRenderError();
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
TEST_P(AudioRendererMixerBehavioralTest, NoInitialize) {
EXPECT_CALL(*this, RemoveMixer(testing::_)).Times(0);
scoped_refptr<AudioRendererMixerInput> audio_renderer_mixer =
new AudioRendererMixerInput(
base::Bind(&AudioRendererMixerTest::GetMixer,
base::Unretained(this)),
base::Bind(&AudioRendererMixerTest::RemoveMixer,
base::Unretained(this)));
}
TEST_P(AudioRendererMixerBehavioralTest, MixerPausesStream) {
const base::TimeDelta kPauseTime = base::TimeDelta::FromMilliseconds(500);
const base::TimeDelta kTestTimeout = 10 * kPauseTime;
mixer_->set_pause_delay_for_testing(kPauseTime);
base::WaitableEvent pause_event(true, false);
EXPECT_CALL(*sink_.get(), Pause()).Times(2)
.WillRepeatedly(SignalEvent(&pause_event));
InitializeInputs(1);
const base::TimeDelta kSleepTime = base::TimeDelta::FromMilliseconds(100);
base::TimeTicks start_time = base::TimeTicks::Now();
while (!pause_event.IsSignaled()) {
mixer_callback_->Render(audio_bus_.get(), 0);
base::PlatformThread::Sleep(kSleepTime);
ASSERT_TRUE(base::TimeTicks::Now() - start_time < kTestTimeout);
}
pause_event.Reset();
mixer_inputs_[0]->Start();
EXPECT_CALL(*sink_.get(), Play());
mixer_inputs_[0]->Play();
mixer_inputs_[0]->Pause();
start_time = base::TimeTicks::Now();
while (!pause_event.IsSignaled()) {
mixer_callback_->Render(audio_bus_.get(), 0);
base::PlatformThread::Sleep(kSleepTime);
ASSERT_TRUE(base::TimeTicks::Now() - start_time < kTestTimeout);
}
mixer_inputs_[0]->Stop();
}
INSTANTIATE_TEST_CASE_P(
AudioRendererMixerTest, AudioRendererMixerTest, testing::Values(
std::tr1::make_tuple(44100, 44100, 0.00000048),
std::tr1::make_tuple(44100, 48000, 0.033),
std::tr1::make_tuple(48000, 41000, 0.042)));
INSTANTIATE_TEST_CASE_P(
AudioRendererMixerBehavioralTest, AudioRendererMixerBehavioralTest,
testing::ValuesIn(std::vector<AudioRendererMixerTestData>(
1, std::tr1::make_tuple(44100, 44100, 0))));
}