Extend TestAudioDeviceModule API.

Extend TestAudioDeviceModule API to be able to create WavReader/WavWriter from rtc::PlatformFile

Bug: webrtc:8946
Change-Id: Ieea16be91c40a5928689cdeaa8a17d75fee0cf82
Reviewed-on: https://webrtc-review.googlesource.com/63266
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22576}
This commit is contained in:
Artem Titov
2018-03-22 16:50:06 +01:00
committed by Commit Bot
parent 866e08d282
commit 8458cff411
3 changed files with 187 additions and 82 deletions

View File

@ -11,30 +11,78 @@
#include <algorithm>
#include <array>
#include "api/array_view.h"
#include "common_audio/wav_file.h"
#include "common_audio/wav_header.h"
#include "modules/audio_device/include/test_audio_device.h"
#include "rtc_base/logging.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/testsupport/fileutils.h"
namespace webrtc {
namespace {
void RunTest(const std::vector<int16_t>& input_samples,
const std::vector<int16_t>& expected_samples,
size_t samples_per_frame) {
void RunTestViaRtcPlatformFileAPI(
rtc::ArrayView<const int16_t> input_samples,
rtc::ArrayView<const int16_t> expected_samples,
size_t samples_per_frame) {
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
const std::string output_filename = test::OutputPath() +
"BoundedWavFileWriterTest_" +
test_info->name() + ".wav";
const std::string output_filename =
test::OutputPath() + "BoundedWavFileWriterTest_" + test_info->name() +
"_" + std::to_string(std::rand()) + ".wav";
static const size_t kSamplesPerFrame = 8;
static const int kSampleRate = kSamplesPerFrame * 100;
EXPECT_EQ(TestAudioDeviceModule::SamplesPerFrame(kSampleRate),
kSamplesPerFrame);
// Test through rtc::PlatformFile API.
{
auto file = rtc::CreatePlatformFile(output_filename);
std::unique_ptr<TestAudioDeviceModule::Renderer> writer =
TestAudioDeviceModule::CreateBoundedWavFileWriter(file, 800);
for (size_t i = 0; i < input_samples.size(); i += kSamplesPerFrame) {
EXPECT_TRUE(writer->Render(rtc::ArrayView<const int16_t>(
&input_samples[i],
std::min(kSamplesPerFrame, input_samples.size() - i))));
}
}
{
auto file = rtc::OpenPlatformFile(output_filename);
WavReader reader(file);
std::vector<int16_t> read_samples(expected_samples.size());
EXPECT_EQ(expected_samples.size(),
reader.ReadSamples(read_samples.size(), read_samples.data()));
EXPECT_THAT(expected_samples, testing::ElementsAreArray(read_samples));
EXPECT_EQ(0u, reader.ReadSamples(read_samples.size(), read_samples.data()));
}
remove(output_filename.c_str());
}
void RunTest(const std::vector<int16_t>& input_samples,
const std::vector<int16_t>& expected_samples,
size_t samples_per_frame) {
const ::testing::TestInfo* const test_info =
::testing::UnitTest::GetInstance()->current_test_info();
const std::string output_filename =
test::OutputPath() + "BoundedWavFileWriterTest_" + test_info->name() +
"_" + std::to_string(std::rand()) + ".wav";
static const size_t kSamplesPerFrame = 8;
static const int kSampleRate = kSamplesPerFrame * 100;
EXPECT_EQ(TestAudioDeviceModule::SamplesPerFrame(kSampleRate),
kSamplesPerFrame);
// Test through file name API.
{
std::unique_ptr<TestAudioDeviceModule::Renderer> writer =
TestAudioDeviceModule::CreateBoundedWavFileWriter(output_filename, 800);
@ -76,6 +124,14 @@ TEST(BoundedWavFileWriterTest, SomeStartSilence) {
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, SomeStartSilenceRtcFile) {
static const std::vector<int16_t> kInputSamples = {
0, 0, 0, 0, 3, 0, 0, 0, 0, 3, -13222, -7, -3525, 5787, -25247, 8};
static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin() + 10,
kInputSamples.end());
RunTestViaRtcPlatformFileAPI(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, NegativeStartSilence) {
static const std::vector<int16_t> kInputSamples = {
0, -4, -6, 0, 3, 0, 0, 0, 0, 3, -13222, -7, -3525, 5787, -25247, 8};