webrtc_m130/modules/audio_device/include/test_audio_device_unittest.cc
Jeremy Leconte b035dcc0a2 Revert "Reland "Migrate TestAudioDeviceModule on AudioDeviceModuleImpl""
This reverts commit eeae96299784515f573379a64655eb07a5973a3a.

Reason for revert: breaks WebRTC Chromium FYI ios-device
https://ci.chromium.org/ui/p/chromium/builders/webrtc.fyi/WebRTC%20Chromium%20FYI%20ios-device/14896/overview

Original change's description:
> Reland "Migrate TestAudioDeviceModule on AudioDeviceModuleImpl"
>
> This reverts commit 69c8d3c843326aff9dee32cc639741c1cd7f8ae9.
>
> Reason for revert: Reland with a fix
>
> Original change's description:
> > Revert "Migrate TestAudioDeviceModule on AudioDeviceModuleImpl"
> >
> > This reverts commit e42bf81486d2f08b6dcbf1442287202e937ce52b.
> >
> > Reason for revert: Breaks iOS simulator bots and thus blocks chromium roll, https://chromium-review.googlesource.com/c/chromium/src/+/4433814
> >
> > Original change's description:
> > > Migrate TestAudioDeviceModule on AudioDeviceModuleImpl
> > >
> > > Bug: b/272350185
> > > Change-Id: Ia3d85d6fa3b0d4809e987a39d60d3eb022687132
> > > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/300363
> > > Commit-Queue: Artem Titov <titovartem@webrtc.org>
> > > Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
> > > Cr-Commit-Position: refs/heads/main@{#39877}
> >
> > Bug: b/272350185
> > Change-Id: I1e3b542fc1278797f283afedeae01cbb7412d353
> > No-Presubmit: true
> > No-Tree-Checks: true
> > No-Try: true
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/301701
> > Commit-Queue: Jeremy Leconte <jleconte@google.com>
> > Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
> > Reviewed-by: Jeremy Leconte <jleconte@google.com>
> > Auto-Submit: Christoffer Jansson <jansson@google.com>
> > Owners-Override: Christoffer Jansson <jansson@google.com>
> > Cr-Commit-Position: refs/heads/main@{#39881}
>
> Bug: b/272350185
> Change-Id: I809466306b2e1fd54c44b90311059c98a53ef8ee
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/301704
> Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
> Commit-Queue: Artem Titov <titovartem@webrtc.org>
> Cr-Commit-Position: refs/heads/main@{#39936}

Bug: b/272350185
Change-Id: If0a10717bf14a0a618e52728fc3a61b9c55f3bd2
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/303460
Commit-Queue: Jeremy Leconte <jleconte@google.com>
Owners-Override: Jeremy Leconte <jleconte@google.com>
Bot-Commit: rubber-stamper@appspot.gserviceaccount.com <rubber-stamper@appspot.gserviceaccount.com>
Cr-Commit-Position: refs/heads/main@{#39947}
2023-04-25 10:24:56 +00:00

193 lines
7.5 KiB
C++

/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_device/include/test_audio_device.h"
#include <algorithm>
#include <array>
#include "api/array_view.h"
#include "common_audio/wav_file.h"
#include "common_audio/wav_header.h"
#include "rtc_base/logging.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.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) {
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);
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))));
}
}
{
WavReader reader(output_filename);
std::vector<int16_t> read_samples(expected_samples.size());
EXPECT_EQ(expected_samples.size(),
reader.ReadSamples(read_samples.size(), read_samples.data()));
EXPECT_EQ(expected_samples, read_samples);
EXPECT_EQ(0u, reader.ReadSamples(read_samples.size(), read_samples.data()));
}
remove(output_filename.c_str());
}
} // namespace
TEST(BoundedWavFileWriterTest, NoSilence) {
static const std::vector<int16_t> kInputSamples = {
75, 1234, 243, -1231, -22222, 0, 3, 88,
1222, -1213, -13222, -7, -3525, 5787, -25247, 8};
static const std::vector<int16_t> kExpectedSamples = kInputSamples;
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, SomeStartSilence) {
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());
RunTest(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};
static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin() + 2,
kInputSamples.end());
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, SomeEndSilence) {
static const std::vector<int16_t> kInputSamples = {
75, 1234, 243, -1231, -22222, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin(),
kInputSamples.end() - 9);
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, DoubleEndSilence) {
static const std::vector<int16_t> kInputSamples = {
75, 1234, 243, -1231, -22222, 0, 0, 0,
0, -1213, -13222, -7, -3525, 5787, 0, 0};
static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin(),
kInputSamples.end() - 2);
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, DoubleSilence) {
static const std::vector<int16_t> kInputSamples = {0, -1213, -13222, -7,
-3525, 5787, 0, 0};
static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin() + 1,
kInputSamples.end() - 2);
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(BoundedWavFileWriterTest, EndSilenceCutoff) {
static const std::vector<int16_t> kInputSamples = {
75, 1234, 243, -1231, -22222, 0, 1, 0, 0, 0, 0};
static const std::vector<int16_t> kExpectedSamples(kInputSamples.begin(),
kInputSamples.end() - 4);
RunTest(kInputSamples, kExpectedSamples, 8);
}
TEST(WavFileReaderTest, RepeatedTrueWithSingleFrameFileReadTwice) {
static const std::vector<int16_t> kInputSamples = {75, 1234, 243, -1231,
-22222, 0, 3, 88};
static const rtc::BufferT<int16_t> kExpectedSamples(kInputSamples.data(),
kInputSamples.size());
const std::string output_filename = test::OutputPath() +
"WavFileReaderTest_RepeatedTrue_" +
std::to_string(std::rand()) + ".wav";
static const size_t kSamplesPerFrame = 8;
static const int kSampleRate = kSamplesPerFrame * 100;
EXPECT_EQ(TestAudioDeviceModule::SamplesPerFrame(kSampleRate),
kSamplesPerFrame);
// Create wav file to read.
{
std::unique_ptr<TestAudioDeviceModule::Renderer> writer =
TestAudioDeviceModule::CreateWavFileWriter(output_filename, 800);
for (size_t i = 0; i < kInputSamples.size(); i += kSamplesPerFrame) {
EXPECT_TRUE(writer->Render(rtc::ArrayView<const int16_t>(
&kInputSamples[i],
std::min(kSamplesPerFrame, kInputSamples.size() - i))));
}
}
{
std::unique_ptr<TestAudioDeviceModule::Capturer> reader =
TestAudioDeviceModule::CreateWavFileReader(output_filename, true);
rtc::BufferT<int16_t> buffer(kExpectedSamples.size());
EXPECT_TRUE(reader->Capture(&buffer));
EXPECT_EQ(kExpectedSamples, buffer);
EXPECT_TRUE(reader->Capture(&buffer));
EXPECT_EQ(kExpectedSamples, buffer);
}
remove(output_filename.c_str());
}
TEST(PulsedNoiseCapturerTest, SetMaxAmplitude) {
const int16_t kAmplitude = 50;
std::unique_ptr<TestAudioDeviceModule::PulsedNoiseCapturer> capturer =
TestAudioDeviceModule::CreatePulsedNoiseCapturer(
kAmplitude, /*sampling_frequency_in_hz=*/8000);
rtc::BufferT<int16_t> recording_buffer;
// Verify that the capturer doesn't create entries louder than than
// kAmplitude. Since the pulse generator alternates between writing
// zeroes and actual entries, we need to do the capturing twice.
capturer->Capture(&recording_buffer);
capturer->Capture(&recording_buffer);
int16_t max_sample =
*std::max_element(recording_buffer.begin(), recording_buffer.end());
EXPECT_LE(max_sample, kAmplitude);
// Increase the amplitude and verify that the samples can now be louder
// than the previous max.
capturer->SetMaxAmplitude(kAmplitude * 2);
capturer->Capture(&recording_buffer);
capturer->Capture(&recording_buffer);
max_sample =
*std::max_element(recording_buffer.begin(), recording_buffer.end());
EXPECT_GT(max_sample, kAmplitude);
}
} // namespace webrtc