This source file includes following definitions.
- InitializeInputs
- Reset
- SetVolume
- ValidateAudioData
- RenderAndValidateAudioData
- FillAudioData
- RunTest
- TEST
- TEST_P
- TEST_P
- TEST_P
- TEST_P
#define _USE_MATH_DEFINES
#include <cmath>
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/string_number_conversions.h"
#include "media/base/audio_converter.h"
#include "media/base/fake_audio_render_callback.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
static const int kConvertInputs = 8;
static const int kConvertCycles = 3;
static const int kBitsPerChannel = 32;
static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
static const int kHighLatencyBufferSize = 2048;
static const int kLowLatencyBufferSize = 256;
static const int kSampleRate = 48000;
static const int kSineCycles = 4;
typedef std::tr1::tuple<int, int, ChannelLayout, double> AudioConverterTestData;
class AudioConverterTest
: public testing::TestWithParam<AudioConverterTestData> {
public:
AudioConverterTest()
: epsilon_(std::tr1::get<3>(GetParam())) {
input_parameters_ = AudioParameters(
AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout,
std::tr1::get<0>(GetParam()), kBitsPerChannel, kHighLatencyBufferSize);
output_parameters_ = AudioParameters(
AudioParameters::AUDIO_PCM_LOW_LATENCY, std::tr1::get<2>(GetParam()),
std::tr1::get<1>(GetParam()), 16, kLowLatencyBufferSize);
converter_.reset(new AudioConverter(
input_parameters_, output_parameters_, false));
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));
}
void InitializeInputs(int 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));
converter_->AddInput(fake_callbacks_[i]);
}
}
void Reset() {
converter_->Reset();
for (size_t i = 0; i < fake_callbacks_.size(); ++i)
fake_callbacks_[i]->reset();
expected_callback_->reset();
}
void SetVolume(float volume) {
for (size_t i = 0; i < fake_callbacks_.size(); ++i)
fake_callbacks_[i]->set_volume(volume);
}
bool ValidateAudioData(int index, int frames, float scale) {
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 RenderAndValidateAudioData(float scale) {
converter_->Convert(audio_bus_.get());
expected_callback_->Render(expected_audio_bus_.get(), 0);
for (int i = input_parameters_.channels();
i < output_parameters_.channels(); ++i) {
memset(expected_audio_bus_->channel(i), 0,
audio_bus_->frames() * sizeof(*audio_bus_->channel(i)));
}
return ValidateAudioData(0, audio_bus_->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 RunTest(int inputs) {
InitializeInputs(inputs);
SetVolume(0);
for (int i = 0; i < kConvertCycles; ++i)
ASSERT_TRUE(RenderAndValidateAudioData(0));
Reset();
float total_scale = 0;
for (size_t i = 0; i < fake_callbacks_.size(); ++i) {
float volume = static_cast<float>(i) / fake_callbacks_.size();
total_scale += volume;
fake_callbacks_[i]->set_volume(volume);
}
for (int i = 0; i < kConvertCycles; ++i)
ASSERT_TRUE(RenderAndValidateAudioData(total_scale));
Reset();
for (size_t i = 1; i < fake_callbacks_.size(); i += 2)
converter_->RemoveInput(fake_callbacks_[i]);
SetVolume(1);
float scale = inputs > 1 ? inputs / 2.0f : inputs;
for (int i = 0; i < kConvertCycles; ++i)
ASSERT_TRUE(RenderAndValidateAudioData(scale));
}
protected:
virtual ~AudioConverterTest() {}
scoped_ptr<AudioConverter> converter_;
AudioParameters input_parameters_;
AudioParameters output_parameters_;
scoped_ptr<AudioBus> audio_bus_;
scoped_ptr<AudioBus> expected_audio_bus_;
ScopedVector<FakeAudioRenderCallback> fake_callbacks_;
scoped_ptr<FakeAudioRenderCallback> expected_callback_;
double epsilon_;
DISALLOW_COPY_AND_ASSIGN(AudioConverterTest);
};
TEST(AudioConverterTest, AudioDelay) {
AudioParameters input_parameters = AudioParameters(
AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate,
kBitsPerChannel, kLowLatencyBufferSize);
AudioParameters output_parameters = AudioParameters(
AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate * 2,
kBitsPerChannel, kHighLatencyBufferSize);
AudioConverter converter(input_parameters, output_parameters, false);
FakeAudioRenderCallback callback(0.2);
scoped_ptr<AudioBus> audio_bus = AudioBus::Create(output_parameters);
converter.AddInput(&callback);
converter.Convert(audio_bus.get());
double input_sample_rate = input_parameters.sample_rate();
int fill_count =
(output_parameters.frames_per_buffer() * input_sample_rate /
output_parameters.sample_rate()) / input_parameters.frames_per_buffer();
base::TimeDelta input_frame_duration = base::TimeDelta::FromMicroseconds(
base::Time::kMicrosecondsPerSecond / input_sample_rate);
int expected_last_delay_milliseconds =
fill_count * input_parameters.frames_per_buffer() *
input_frame_duration.InMillisecondsF();
EXPECT_EQ(expected_last_delay_milliseconds,
callback.last_audio_delay_milliseconds());
}
TEST_P(AudioConverterTest, ArbitraryOutputRequestSize) {
audio_bus_ = AudioBus::Create(output_parameters_.channels(),
output_parameters_.frames_per_buffer() / 2);
RunTest(1);
}
TEST_P(AudioConverterTest, NoInputs) {
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
}
TEST_P(AudioConverterTest, OneInput) {
RunTest(1);
}
TEST_P(AudioConverterTest, ManyInputs) {
RunTest(kConvertInputs);
}
INSTANTIATE_TEST_CASE_P(
AudioConverterTest, AudioConverterTest, testing::Values(
std::tr1::make_tuple(44100, 44100, CHANNEL_LAYOUT_STEREO, 0.00000048),
std::tr1::make_tuple(44100, 48000, CHANNEL_LAYOUT_QUAD, 0.033),
std::tr1::make_tuple(48000, 41000, CHANNEL_LAYOUT_MONO, 0.042)));
}