blob: 5f2ce87e10a61156ac895dc9bc8830c5b1300f83 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/include/audio_processing.h"
11
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000012#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000013#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080014
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000015#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010016#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000017#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000019#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000020
Sam Zackrisson6558fa52019-08-26 10:12:41 +020021#include "absl/flags/flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/include/audio_util.h"
23#include "common_audio/resampler/include/push_resampler.h"
24#include "common_audio/resampler/push_sinc_resampler.h"
25#include "common_audio/signal_processing/include/signal_processing_library.h"
26#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
27#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020029#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/test/protobuf_utils.h"
31#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/arraysize.h"
33#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/gtest_prod_util.h"
36#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010037#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010038#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020041#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020042#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020043#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010044#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070048
49RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000051#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020053#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000054#endif
kwiberg77eab702016-09-28 17:42:01 -070055RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000056
Sam Zackrisson6558fa52019-08-26 10:12:41 +020057ABSL_FLAG(bool,
58 write_apm_ref_data,
59 false,
60 "Write ApmTest.Process results to file, instead of comparing results "
61 "to the existing reference data file.");
62
andrew@webrtc.org27c69802014-02-18 20:24:56 +000063namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000064namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000065
ekmeyerson60d9b332015-08-14 10:35:55 -070066// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
67// applicable.
68
mbonadei7c2c8432017-04-07 00:59:12 -070069const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070070const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000071
aluebseb3603b2016-04-20 15:27:58 -070072#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
73// Android doesn't support 48kHz.
74const int kProcessSampleRates[] = {8000, 16000, 32000};
75#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070076const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070077#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000078
ekmeyerson60d9b332015-08-14 10:35:55 -070079enum StreamDirection { kForward = 0, kReverse };
80
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020082 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
83 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000084 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080085 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020086 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000088}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000089
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000090void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070091 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092}
93
andrew@webrtc.org103657b2014-04-24 18:28:56 +000094// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080095size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096 switch (layout) {
97 case AudioProcessing::kMono:
98 return 1;
99 case AudioProcessing::kMonoAndKeyboard:
100 case AudioProcessing::kStereo:
101 return 2;
102 case AudioProcessing::kStereoAndKeyboard:
103 return 3;
104 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700105 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800106 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000107}
108
Jonas Olssona4d87372019-07-05 19:08:33 +0200109void MixStereoToMono(const float* stereo,
110 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800111 size_t samples_per_channel) {
112 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000114}
115
Jonas Olssona4d87372019-07-05 19:08:33 +0200116void MixStereoToMono(const int16_t* stereo,
117 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
121}
122
pkasting25702cb2016-01-08 13:50:27 -0800123void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000125 stereo[i * 2 + 1] = stereo[i * 2];
126 }
127}
128
yujo36b1a5f2017-06-12 12:45:32 -0700129void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
132 }
133}
134
135void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700136 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
138 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700139 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000140 }
141}
142
143void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800144 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700145 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700147 frame_data[i] = left;
148 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000149 }
150}
151
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000152void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700154 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
155 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700156 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000157 }
158}
159
andrew@webrtc.org81865342012-10-27 00:28:27 +0000160bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000161 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000162 return false;
163 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000164 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165 return false;
166 }
yujo36b1a5f2017-06-12 12:45:32 -0700167 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
172 return true;
173}
174
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000175void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200176 AudioProcessing::Config apm_config = ap->GetConfig();
177 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000178#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200179 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100180
181 apm_config.gain_controller1.enabled = true;
182 apm_config.gain_controller1.mode =
183 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200185 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100187 apm_config.gain_controller1.enabled = true;
188 apm_config.gain_controller1.mode =
189 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
190 apm_config.gain_controller1.analog_level_minimum = 0;
191 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000192#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000193
saza0bad15f2019-10-16 11:46:11 +0200194 apm_config.noise_suppression.enabled = true;
195
peah8271d042016-11-22 07:24:52 -0800196 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100197 apm_config.level_estimation.enabled = true;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200198 apm_config.voice_detection.enabled = true;
peah8271d042016-11-22 07:24:52 -0800199 ap->ApplyConfig(apm_config);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200}
201
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000202// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000203template <class T>
204T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200205 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000206}
207
208int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800209 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700210 const int16_t* frame_data = frame.data();
211 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800212 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700213 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000214 }
215
216 return max_data;
217}
218
Alex Loiko890988c2017-08-31 10:25:48 +0200219void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700220 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000221 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000222 ASSERT_TRUE(file != NULL);
223
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100224 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800226 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000227 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000228
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000229 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200231 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232 fclose(file);
233}
234
Alex Loiko890988c2017-08-31 10:25:48 +0200235std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200236 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000237 // Resource files are all stereo.
238 ss << name << sample_rate_hz / 1000 << "_stereo";
239 return test::ResourcePath(ss.str(), "pcm");
240}
241
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000242// Temporary filenames unique to this process. Used to be able to run these
243// tests in parallel as each process needs to be running in isolation they can't
244// have competing filenames.
245std::map<std::string, std::string> temp_filenames;
246
Alex Loiko890988c2017-08-31 10:25:48 +0200247std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000248 int input_rate,
249 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700250 int reverse_input_rate,
251 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800252 size_t num_input_channels,
253 size_t num_output_channels,
254 size_t num_reverse_input_channels,
255 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700256 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200257 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700258 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
259 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000260 if (num_output_channels == 1) {
261 ss << "mono";
262 } else if (num_output_channels == 2) {
263 ss << "stereo";
264 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700265 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000266 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700267 ss << output_rate / 1000;
268 if (num_reverse_output_channels == 1) {
269 ss << "_rmono";
270 } else if (num_reverse_output_channels == 2) {
271 ss << "_rstereo";
272 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700273 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700274 }
275 ss << reverse_output_rate / 1000;
276 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000278 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700279 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000280 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
281 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282}
283
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000284void ClearTempFiles() {
285 for (auto& kv : temp_filenames)
286 remove(kv.second.c_str());
287}
288
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200289// Only remove "out" files. Keep "ref" files.
290void ClearTempOutFiles() {
291 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
292 const std::string& filename = it->first;
293 if (filename.substr(0, 3).compare("out") == 0) {
294 remove(it->second.c_str());
295 temp_filenames.erase(it++);
296 } else {
297 it++;
298 }
299 }
300}
301
Alex Loiko890988c2017-08-31 10:25:48 +0200302void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000303 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000304 ASSERT_TRUE(file != NULL);
305 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000306 fclose(file);
307}
308
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000309// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
310// stereo) file, converts to deinterleaved float (optionally downmixing) and
311// returns the result in |cb|. Returns false if the file ended (or on error) and
312// true otherwise.
313//
314// |int_data| and |float_data| are just temporary space that must be
315// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200316bool ReadChunk(FILE* file,
317 int16_t* int_data,
318 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000319 ChannelBuffer<float>* cb) {
320 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000321 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000322 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
323 if (read_count != frame_size) {
324 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700325 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326 return false; // This is expected.
327 }
328
329 S16ToFloat(int_data, frame_size, float_data);
330 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000331 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000332 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200333 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334 }
335
336 return true;
337}
338
niklase@google.com470e71d2011-07-07 08:21:25 +0000339class ApmTest : public ::testing::Test {
340 protected:
341 ApmTest();
342 virtual void SetUp();
343 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000344
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200345 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000346
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200347 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000348
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000349 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200350 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000351
352 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000353 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000354 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800355 size_t num_input_channels,
356 size_t num_output_channels,
357 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000358 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000359 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000360 void EnableAllComponents();
361 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000362 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000363 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200364 void ReadFrameWithRewind(FILE* file,
365 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000366 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000367 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200368 void ProcessDelayVerificationTest(int delay_ms,
369 int system_delay_ms,
370 int delay_min,
371 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700372 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800373 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700374 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800375 void TestChangingForwardChannels(size_t num_in_channels,
376 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700377 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800378 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700379 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000380 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
381 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000382 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000383 int ProcessStreamChooser(Format format);
384 int AnalyzeReverseStreamChooser(Format format);
385 void ProcessDebugDump(const std::string& in_filename,
386 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800387 Format format,
388 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000390
391 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000392 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800393 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000394 AudioFrame* frame_;
395 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800396 std::unique_ptr<ChannelBuffer<float> > float_cb_;
397 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000398 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800399 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 FILE* far_file_;
401 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000402 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403};
404
405ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000406 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000407#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200408 ref_filename_(
409 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000410#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200411 ref_filename_(
412 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000413#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000414 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000415 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000416 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000417 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000418 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000419 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000420 out_file_(NULL) {
421 Config config;
422 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100423 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000424}
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
426void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000427 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
429 frame_ = new AudioFrame();
430 revframe_ = new AudioFrame();
431
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000432 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000433}
434
435void ApmTest::TearDown() {
436 if (frame_) {
437 delete frame_;
438 }
439 frame_ = NULL;
440
441 if (revframe_) {
442 delete revframe_;
443 }
444 revframe_ = NULL;
445
446 if (far_file_) {
447 ASSERT_EQ(0, fclose(far_file_));
448 }
449 far_file_ = NULL;
450
451 if (near_file_) {
452 ASSERT_EQ(0, fclose(near_file_));
453 }
454 near_file_ = NULL;
455
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000456 if (out_file_) {
457 ASSERT_EQ(0, fclose(out_file_));
458 }
459 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000462void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000463 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700464 ap->Initialize(
465 {{{frame_->sample_rate_hz_, frame_->num_channels_},
466 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700467 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700468 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000469}
470
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000471void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000472 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000473 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800474 size_t num_input_channels,
475 size_t num_output_channels,
476 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000477 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000478 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000479 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000480 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000481
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000482 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
483 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000484 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000485
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000486 if (far_file_) {
487 ASSERT_EQ(0, fclose(far_file_));
488 }
489 std::string filename = ResourceFilePath("far", sample_rate_hz);
490 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200491 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000492
493 if (near_file_) {
494 ASSERT_EQ(0, fclose(near_file_));
495 }
496 filename = ResourceFilePath("near", sample_rate_hz);
497 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200498 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000499
500 if (open_output_file) {
501 if (out_file_) {
502 ASSERT_EQ(0, fclose(out_file_));
503 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700504 filename = OutputFilePath(
505 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
506 reverse_sample_rate_hz, num_input_channels, num_output_channels,
507 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000508 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200509 ASSERT_TRUE(out_file_ != NULL)
510 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000511 }
512}
513
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000514void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000515 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000516}
517
Jonas Olssona4d87372019-07-05 19:08:33 +0200518bool ApmTest::ReadFrame(FILE* file,
519 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000520 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000521 // The files always contain stereo audio.
522 size_t frame_size = frame->samples_per_channel_ * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200523 size_t read_count =
524 fread(frame->mutable_data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000525 if (read_count != frame_size) {
526 // Check that the file really ended.
527 EXPECT_NE(0, feof(file));
528 return false; // This is expected.
529 }
530
531 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700532 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000533 frame->samples_per_channel_);
534 }
535
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000536 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000537 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000538 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000539 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000540}
541
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000542bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
543 return ReadFrame(file, frame, NULL);
544}
545
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000546// If the end of the file has been reached, rewind it and attempt to read the
547// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200548void ApmTest::ReadFrameWithRewind(FILE* file,
549 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000550 ChannelBuffer<float>* cb) {
551 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000552 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000553 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000554 }
555}
556
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
558 ReadFrameWithRewind(file, frame, NULL);
559}
560
andrew@webrtc.org81865342012-10-27 00:28:27 +0000561void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
562 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200563 apm_->set_stream_analog_level(127);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000564 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000565}
566
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000567int ApmTest::ProcessStreamChooser(Format format) {
568 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569 return apm_->ProcessStream(frame_);
570 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200571 return apm_->ProcessStream(
572 float_cb_->channels(), frame_->samples_per_channel_,
573 frame_->sample_rate_hz_, LayoutFromChannels(frame_->num_channels_),
574 output_sample_rate_hz_, LayoutFromChannels(num_output_channels_),
575 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576}
577
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000578int ApmTest::AnalyzeReverseStreamChooser(Format format) {
579 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700580 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 return apm_->AnalyzeReverseStream(
Jonas Olssona4d87372019-07-05 19:08:33 +0200583 revfloat_cb_->channels(), revframe_->samples_per_channel_,
584 revframe_->sample_rate_hz_, LayoutFromChannels(revframe_->num_channels_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000585}
586
Jonas Olssona4d87372019-07-05 19:08:33 +0200587void ApmTest::ProcessDelayVerificationTest(int delay_ms,
588 int system_delay_ms,
589 int delay_min,
590 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000591 // The |revframe_| and |frame_| should include the proper frame information,
592 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000593 AudioFrame tmp_frame;
594 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000595 bool causal = true;
596
597 tmp_frame.CopyFrom(*revframe_);
598 SetFrameTo(&tmp_frame, 0);
599
600 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
601 // Initialize the |frame_queue| with empty frames.
602 int frame_delay = delay_ms / 10;
603 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000604 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000605 frame->CopyFrom(tmp_frame);
606 frame_queue.push(frame);
607 frame_delay++;
608 causal = false;
609 }
610 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000611 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000612 frame->CopyFrom(tmp_frame);
613 frame_queue.push(frame);
614 frame_delay--;
615 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000616 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
617 // need enough frames with audio to have reliable estimates, but as few as
618 // possible to keep processing time down. 4.5 seconds seemed to be a good
619 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000621 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000622 frame->CopyFrom(tmp_frame);
623 // Use the near end recording, since that has more speech in it.
624 ASSERT_TRUE(ReadFrame(near_file_, frame));
625 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000626 AudioFrame* reverse_frame = frame;
627 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 if (!causal) {
629 reverse_frame = frame_queue.front();
630 // When we call ProcessStream() the frame is modified, so we can't use the
631 // pointer directly when things are non-causal. Use an intermediate frame
632 // and copy the data.
633 process_frame = &tmp_frame;
634 process_frame->CopyFrom(*frame);
635 }
aluebsb0319552016-03-17 20:39:53 -0700636 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000637 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
638 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
639 frame = frame_queue.front();
640 frame_queue.pop();
641 delete frame;
642
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000643 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000644 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200645 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 }
647 }
648
649 rewind(near_file_);
650 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000651 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 frame_queue.pop();
653 delete frame;
654 }
655 // Calculate expected delay estimate and acceptable regions. Further,
656 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700657 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700658 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700659 const int expected_median =
660 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
661 const int expected_median_high = rtc::SafeClamp<int>(
662 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700663 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700664 const int expected_median_low = rtc::SafeClamp<int>(
665 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700666 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000667 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200668 AudioProcessingStats stats =
669 apm_->GetStatistics(true /* has_remote_tracks */);
670 ASSERT_TRUE(stats.delay_median_ms.has_value());
671 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000672 EXPECT_GE(expected_median_high, median);
673 EXPECT_LE(expected_median_low, median);
674}
675
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000676void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000677 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000678 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000679
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000680 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200681 AudioProcessing::Config apm_config = apm_->GetConfig();
682 apm_config.gain_controller1.enabled = true;
683 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200684 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000685
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000686 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200687 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000688 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200689 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000691 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200692 apm_config.echo_canceller.enabled = true;
693 apm_config.echo_canceller.mobile_mode = false;
694 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000695 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200696 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200697 apm_config.gain_controller1.enabled = false;
698 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000699
700 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000701 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100702 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000703
704 // Resets after successful ProcessStream().
705 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000706 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100707 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708
709 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200710 apm_config.gain_controller1.enabled = true;
711 apm_->ApplyConfig(apm_config);
712 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100713 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200714 apm_config.gain_controller1.enabled = false;
715 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200718 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100719 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000720
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000721 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200723 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000725}
726
727TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729}
730
731TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000733}
734
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000735TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
736 EXPECT_EQ(0, apm_->delay_offset_ms());
737 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
738 EXPECT_EQ(50, apm_->stream_delay_ms());
739}
740
741TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
742 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000743 apm_->set_delay_offset_ms(100);
744 EXPECT_EQ(100, apm_->delay_offset_ms());
745 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000746 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000747 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
748 EXPECT_EQ(200, apm_->stream_delay_ms());
749
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000750 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000751 apm_->set_delay_offset_ms(-50);
752 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000753 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
754 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000755 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
756 EXPECT_EQ(50, apm_->stream_delay_ms());
757}
758
Michael Graczyk86c6d332015-07-23 11:41:39 -0700759void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800760 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700761 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000762 frame_->num_channels_ = num_channels;
763 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700764 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000765}
766
Michael Graczyk86c6d332015-07-23 11:41:39 -0700767void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800768 size_t num_in_channels,
769 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770 AudioProcessing::Error expected_return) {
771 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
772 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
773
774 EXPECT_EQ(expected_return,
775 apm_->ProcessStream(float_cb_->channels(), input_stream,
776 output_stream, float_cb_->channels()));
777}
778
779void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800780 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700781 AudioProcessing::Error expected_return) {
782 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700783 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
784 {output_sample_rate_hz_, apm_->num_output_channels()},
785 {frame_->sample_rate_hz_, num_rev_channels},
786 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700787
ekmeyerson60d9b332015-08-14 10:35:55 -0700788 EXPECT_EQ(
789 expected_return,
790 apm_->ProcessReverseStream(
791 float_cb_->channels(), processing_config.reverse_input_stream(),
792 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700793}
794
795TEST_F(ApmTest, ChannelsInt16Interface) {
796 // Testing number of invalid and valid channels.
797 Init(16000, 16000, 16000, 4, 4, 4, false);
798
799 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
800
Peter Kasting69558702016-01-12 16:26:35 -0800801 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700802 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000803 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000804 }
805}
806
Michael Graczyk86c6d332015-07-23 11:41:39 -0700807TEST_F(ApmTest, Channels) {
808 // Testing number of invalid and valid channels.
809 Init(16000, 16000, 16000, 4, 4, 4, false);
810
811 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
812 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
813
Peter Kasting69558702016-01-12 16:26:35 -0800814 for (size_t i = 1; i < 4; ++i) {
815 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700816 // Output channels much be one or match input channels.
817 if (j == 1 || i == j) {
818 TestChangingForwardChannels(i, j, kNoErr);
819 TestChangingReverseChannels(i, kNoErr);
820
821 EXPECT_EQ(i, apm_->num_input_channels());
822 EXPECT_EQ(j, apm_->num_output_channels());
823 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800824 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 } else {
826 TestChangingForwardChannels(i, j,
827 AudioProcessing::kBadNumberChannelsError);
828 }
829 }
830 }
831}
832
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000833TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000834 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000835 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000836 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000837 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700838 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800839 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000840 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000841 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000842 }
843}
844
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000845TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200846 AudioProcessing::Config config = apm_->GetConfig();
847 config.gain_controller1.enabled = false;
848 apm_->ApplyConfig(config);
849 config.gain_controller1.enabled = true;
850 apm_->ApplyConfig(config);
851
niklase@google.com470e71d2011-07-07 08:21:25 +0000852 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200853 for (auto mode :
854 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
855 AudioProcessing::Config::GainController1::kFixedDigital,
856 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
857 config.gain_controller1.mode = mode;
858 apm_->ApplyConfig(config);
859 apm_->set_stream_analog_level(100);
860 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000861 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000862
Sam Zackrisson41478c72019-10-15 10:10:26 +0200863 // Testing target levels
864 for (int target_level_dbfs : {0, 15, 31}) {
865 config.gain_controller1.target_level_dbfs = target_level_dbfs;
866 apm_->ApplyConfig(config);
867 apm_->set_stream_analog_level(100);
868 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 }
870
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100871 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200872 for (int compression_gain_db : {0, 10, 90}) {
873 config.gain_controller1.compression_gain_db = compression_gain_db;
874 apm_->ApplyConfig(config);
875 apm_->set_stream_analog_level(100);
876 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000877 }
878
879 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200880 for (bool enable : {false, true}) {
881 config.gain_controller1.enable_limiter = enable;
882 apm_->ApplyConfig(config);
883 apm_->set_stream_analog_level(100);
884 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
885 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000886
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100887 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200888 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
889 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
890 for (size_t i = 0; i < kMinLevels.size(); ++i) {
891 int min_level = kMinLevels[i];
892 int max_level = kMaxLevels[i];
893 config.gain_controller1.analog_level_minimum = min_level;
894 config.gain_controller1.analog_level_maximum = max_level;
895 apm_->ApplyConfig(config);
896 apm_->set_stream_analog_level((min_level + max_level) / 2);
897 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000899}
900
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100901#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
902TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200903 auto config = apm_->GetConfig();
904 config.gain_controller1.target_level_dbfs = -1;
905 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100906}
907
908TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200909 auto config = apm_->GetConfig();
910 config.gain_controller1.target_level_dbfs = 32;
911 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100912}
913
914TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200915 auto config = apm_->GetConfig();
916 config.gain_controller1.compression_gain_db = -1;
917 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100918}
919
920TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200921 auto config = apm_->GetConfig();
922 config.gain_controller1.compression_gain_db = 91;
923 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100924}
925
926TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200927 auto config = apm_->GetConfig();
928 config.gain_controller1.analog_level_minimum = -1;
929 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100930}
931
932TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200933 auto config = apm_->GetConfig();
934 config.gain_controller1.analog_level_maximum = 65536;
935 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100936}
937
938TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200939 auto config = apm_->GetConfig();
940 config.gain_controller1.analog_level_minimum = 512;
941 config.gain_controller1.analog_level_maximum = 255;
942 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100943}
944
945TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200946 auto config = apm_->GetConfig();
947 config.gain_controller1.analog_level_minimum = 255;
948 config.gain_controller1.analog_level_maximum = 512;
949 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100950 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
951}
952
953TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200954 auto config = apm_->GetConfig();
955 config.gain_controller1.analog_level_minimum = 255;
956 config.gain_controller1.analog_level_maximum = 512;
957 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100958 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
959}
960#endif
961
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000962void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000963 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +0200964 auto config = apm_->GetConfig();
965 config.gain_controller1.enabled = true;
966 config.gain_controller1.mode =
967 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
968 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000969
970 int out_analog_level = 0;
971 for (int i = 0; i < 2000; ++i) {
972 ReadFrameWithRewind(near_file_, frame_);
973 // Ensure the audio is at a low level, so the AGC will try to increase it.
974 ScaleFrame(frame_, 0.25);
975
976 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200977 apm_->set_stream_analog_level(100);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000978 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200979 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000980 }
981
982 // Ensure the AGC is still able to reach the maximum.
983 EXPECT_EQ(255, out_analog_level);
984}
985
986// Verifies that despite volume slider quantization, the AGC can continue to
987// increase its volume.
988TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -0800989 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000990 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
991 }
992}
993
994void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000995 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +0200996 auto config = apm_->GetConfig();
997 config.gain_controller1.enabled = true;
998 config.gain_controller1.mode =
999 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1000 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001001
1002 int out_analog_level = 100;
1003 for (int i = 0; i < 1000; ++i) {
1004 ReadFrameWithRewind(near_file_, frame_);
1005 // Ensure the audio is at a low level, so the AGC will try to increase it.
1006 ScaleFrame(frame_, 0.25);
1007
Sam Zackrisson41478c72019-10-15 10:10:26 +02001008 apm_->set_stream_analog_level(out_analog_level);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001009 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001010 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001011 }
1012
1013 // Ensure the volume was raised.
1014 EXPECT_GT(out_analog_level, 100);
1015 int highest_level_reached = out_analog_level;
1016 // Simulate a user manual volume change.
1017 out_analog_level = 100;
1018
1019 for (int i = 0; i < 300; ++i) {
1020 ReadFrameWithRewind(near_file_, frame_);
1021 ScaleFrame(frame_, 0.25);
1022
Sam Zackrisson41478c72019-10-15 10:10:26 +02001023 apm_->set_stream_analog_level(out_analog_level);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001024 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001025 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001026 // Check that AGC respected the manually adjusted volume.
1027 EXPECT_LT(out_analog_level, highest_level_reached);
1028 }
1029 // Check that the volume was still raised.
1030 EXPECT_GT(out_analog_level, 100);
1031}
1032
1033TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001034 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001035 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1036 }
1037}
1038
niklase@google.com470e71d2011-07-07 08:21:25 +00001039TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001040 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001041 AudioProcessing::Config apm_config;
1042 apm_config.high_pass_filter.enabled = true;
1043 apm_->ApplyConfig(apm_config);
1044 apm_config.high_pass_filter.enabled = false;
1045 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001046}
1047
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001048TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001049 AudioProcessing::Config config = apm_->GetConfig();
1050 EXPECT_FALSE(config.echo_canceller.enabled);
1051 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001052 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001053 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001054 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001055 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001056}
1057
1058TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001059 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001060 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001061 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001062 AudioFrame frame_copy;
1063 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001064 for (int j = 0; j < 1000; j++) {
1065 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1066 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001067 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1068 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001069 }
1070 }
1071}
1072
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001073TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1074 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001075 const size_t kSamples = 160;
1076 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001077 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001078 float dest[kSamples] = {};
1079
1080 auto src_channels = &src[0];
1081 auto dest_channels = &dest[0];
1082
Ivo Creusen62337e52018-01-09 14:17:33 +01001083 apm_.reset(AudioProcessingBuilder().Create());
Jonas Olssona4d87372019-07-05 19:08:33 +02001084 EXPECT_NOERR(apm_->ProcessStream(&src_channels, kSamples, sample_rate,
1085 LayoutFromChannels(1), sample_rate,
1086 LayoutFromChannels(1), &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001087
1088 for (size_t i = 0; i < kSamples; ++i) {
1089 EXPECT_EQ(src[i], dest[i]);
1090 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001091
1092 // Same for ProcessReverseStream.
1093 float rev_dest[kSamples] = {};
1094 auto rev_dest_channels = &rev_dest[0];
1095
1096 StreamConfig input_stream = {sample_rate, 1};
1097 StreamConfig output_stream = {sample_rate, 1};
1098 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1099 output_stream, &rev_dest_channels));
1100
1101 for (size_t i = 0; i < kSamples; ++i) {
1102 EXPECT_EQ(src[i], rev_dest[i]);
1103 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001104}
1105
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001106TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1107 EnableAllComponents();
1108
pkasting25702cb2016-01-08 13:50:27 -08001109 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001110 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1111 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001112 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001113 ASSERT_EQ(0, feof(far_file_));
1114 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001115 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001116 CopyLeftToRightChannel(revframe_->mutable_data(),
1117 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001118
aluebsb0319552016-03-17 20:39:53 -07001119 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001120
yujo36b1a5f2017-06-12 12:45:32 -07001121 CopyLeftToRightChannel(frame_->mutable_data(),
1122 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001123 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1124
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001125 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001126 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001127 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001128 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001129
yujo36b1a5f2017-06-12 12:45:32 -07001130 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001131 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001132 rewind(far_file_);
1133 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001134 }
1135}
1136
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001137TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001138 // Verify the filter is not active through undistorted audio when:
1139 // 1. No components are enabled...
1140 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001141 AudioFrame frame_copy;
1142 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001143 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1144 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1145 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1146
1147 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001148 auto apm_config = apm_->GetConfig();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001149 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001150 frame_copy.CopyFrom(*frame_);
saza6787f232019-10-11 19:31:07 +02001151 apm_config.level_estimation.enabled = true;
1152 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001153 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1154 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1155 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001156 apm_config.level_estimation.enabled = false;
1157 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001158
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001159 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001160 SetFrameTo(frame_, 1000);
1161 frame_copy.CopyFrom(*frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001162 apm_config.voice_detection.enabled = true;
1163 apm_->ApplyConfig(apm_config);
1164 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1165 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1166 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1167 apm_config.voice_detection.enabled = false;
1168 apm_->ApplyConfig(apm_config);
1169
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001170 // 4. Both the VAD and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001171 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001172 frame_copy.CopyFrom(*frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001173 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001174 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001175 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001176 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1177 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1178 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001179 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001180 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001181 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001182
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001183 // Check the test is valid. We should have distortion from the filter
1184 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001185 apm_config.echo_canceller.enabled = true;
1186 apm_config.echo_canceller.mobile_mode = false;
1187 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001188 frame_->samples_per_channel_ = 320;
1189 frame_->num_channels_ = 2;
1190 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001191 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001192 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001193 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001194 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1195 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1196}
1197
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001198#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1199void ApmTest::ProcessDebugDump(const std::string& in_filename,
1200 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001201 Format format,
1202 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001203 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001204 FILE* in_file = fopen(in_filename.c_str(), "rb");
1205 ASSERT_TRUE(in_file != NULL);
1206 audioproc::Event event_msg;
1207 bool first_init = true;
1208
1209 while (ReadMessageFromFile(in_file, &event_msg)) {
1210 if (event_msg.type() == audioproc::Event::INIT) {
1211 const audioproc::Init msg = event_msg.init();
1212 int reverse_sample_rate = msg.sample_rate();
1213 if (msg.has_reverse_sample_rate()) {
1214 reverse_sample_rate = msg.reverse_sample_rate();
1215 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001216 int output_sample_rate = msg.sample_rate();
1217 if (msg.has_output_sample_rate()) {
1218 output_sample_rate = msg.output_sample_rate();
1219 }
1220
Jonas Olssona4d87372019-07-05 19:08:33 +02001221 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1222 msg.num_input_channels(), msg.num_output_channels(),
1223 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001224 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001225 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001226 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001227 auto aec_dump =
1228 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1229 EXPECT_TRUE(aec_dump);
1230 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001231 first_init = false;
1232 }
1233
1234 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1235 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1236
1237 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001238 ASSERT_EQ(revframe_->num_channels_,
1239 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001240 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001241 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1242 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001243 }
1244 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001245 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001246 if (format == kFloatFormat) {
1247 // We're using an int16 input file; convert to float.
1248 ConvertToFloat(*revframe_, revfloat_cb_.get());
1249 }
1250 }
1251 AnalyzeReverseStreamChooser(format);
1252
1253 } else if (event_msg.type() == audioproc::Event::STREAM) {
1254 const audioproc::Stream msg = event_msg.stream();
1255 // ProcessStream could have changed this for the output frame.
1256 frame_->num_channels_ = apm_->num_input_channels();
1257
Sam Zackrisson41478c72019-10-15 10:10:26 +02001258 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001259 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001260 if (msg.has_keypress()) {
1261 apm_->set_stream_key_pressed(msg.keypress());
1262 } else {
1263 apm_->set_stream_key_pressed(true);
1264 }
1265
1266 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001267 ASSERT_EQ(frame_->num_channels_,
1268 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001269 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001270 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1271 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001272 }
1273 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001274 memcpy(frame_->mutable_data(), msg.input_data().data(),
1275 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001276 if (format == kFloatFormat) {
1277 // We're using an int16 input file; convert to float.
1278 ConvertToFloat(*frame_, float_cb_.get());
1279 }
1280 }
1281 ProcessStreamChooser(format);
1282 }
1283 }
aleloif4dd1912017-06-15 01:55:38 -07001284 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001285 fclose(in_file);
1286}
1287
1288void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001289 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001290 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001291 std::string format_string;
1292 switch (format) {
1293 case kIntFormat:
1294 format_string = "_int";
1295 break;
1296 case kFloatFormat:
1297 format_string = "_float";
1298 break;
1299 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001300 const std::string ref_filename = test::TempFilename(
1301 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1302 const std::string out_filename = test::TempFilename(
1303 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001304 const std::string limited_filename = test::TempFilename(
1305 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1306 const size_t logging_limit_bytes = 100000;
1307 // We expect at least this many bytes in the created logfile.
1308 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001309 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001310 ProcessDebugDump(in_filename, ref_filename, format, -1);
1311 ProcessDebugDump(ref_filename, out_filename, format, -1);
1312 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001313
1314 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1315 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001316 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001317 ASSERT_TRUE(ref_file != NULL);
1318 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001319 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001320 std::unique_ptr<uint8_t[]> ref_bytes;
1321 std::unique_ptr<uint8_t[]> out_bytes;
1322 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001323
1324 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1325 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001326 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001327 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001328 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001329 while (ref_size > 0 && out_size > 0) {
1330 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001331 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001332 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001333 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001334 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001335 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001336 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1337 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001338 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001339 }
1340 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001341 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1342 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001343 EXPECT_NE(0, feof(ref_file));
1344 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001345 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001346 ASSERT_EQ(0, fclose(ref_file));
1347 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001348 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001349 remove(ref_filename.c_str());
1350 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001351 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001352}
1353
pbosc7a65692016-05-06 12:50:04 -07001354TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001355 VerifyDebugDumpTest(kIntFormat);
1356}
1357
pbosc7a65692016-05-06 12:50:04 -07001358TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001359 VerifyDebugDumpTest(kFloatFormat);
1360}
1361#endif
1362
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001363// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001364TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001365 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001366 const std::string filename =
1367 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001368 {
1369 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1370 EXPECT_FALSE(aec_dump);
1371 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001372
1373#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1374 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001375 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001376
aleloif4dd1912017-06-15 01:55:38 -07001377 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1378 EXPECT_TRUE(aec_dump);
1379 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001380 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001381 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001382 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001383
1384 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001385 FILE* fid = fopen(filename.c_str(), "r");
1386 ASSERT_TRUE(fid != NULL);
1387
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001388 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001389 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001390 ASSERT_EQ(0, remove(filename.c_str()));
1391#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001392 // Verify the file has NOT been written.
1393 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1394#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1395}
1396
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001397// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001398TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001399 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001400
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001401 const std::string filename =
1402 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001403 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1404 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001405
1406#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1407 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001408 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001409
Niels Möllere8e4dc42019-06-11 14:04:16 +02001410 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001411 EXPECT_TRUE(aec_dump);
1412 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001413 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001414 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001415 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001416
1417 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001418 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001419 ASSERT_TRUE(fid != NULL);
1420
1421 // Clean it up.
1422 ASSERT_EQ(0, fclose(fid));
1423 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001424#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1425}
1426
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001427// TODO(andrew): Add a test to process a few frames with different combinations
1428// of enabled components.
1429
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001430TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001431 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001432 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001433
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001434 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001435 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001436 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001437 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001438 for (size_t i = 0; i < arraysize(kChannels); i++) {
1439 for (size_t j = 0; j < arraysize(kChannels); j++) {
1440 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001441 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001442 test->set_num_reverse_channels(kChannels[i]);
1443 test->set_num_input_channels(kChannels[j]);
1444 test->set_num_output_channels(kChannels[j]);
1445 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001446 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001447 }
1448 }
1449 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001450#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1451 // To test the extended filter mode.
1452 audioproc::Test* test = ref_data.add_test();
1453 test->set_num_reverse_channels(2);
1454 test->set_num_input_channels(2);
1455 test->set_num_output_channels(2);
1456 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1457 test->set_use_aec_extended_filter(true);
1458#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001459 }
1460
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001461 for (int i = 0; i < ref_data.test_size(); i++) {
1462 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001463
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001464 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001465 // TODO(ajm): We no longer allow different input and output channels. Skip
1466 // these tests for now, but they should be removed from the set.
1467 if (test->num_input_channels() != test->num_output_channels())
1468 continue;
1469
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001470 Config config;
1471 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001472 config.Set<ExtendedFilter>(
1473 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001474 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001475
1476 EnableAllComponents();
1477
Jonas Olssona4d87372019-07-05 19:08:33 +02001478 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001479 static_cast<size_t>(test->num_input_channels()),
1480 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001481 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001482
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001483 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001484 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001485 int analog_level = 127;
1486 int analog_level_average = 0;
1487 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001488 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001489#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001490 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001491#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001492
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001493 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001494 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001495
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001496 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1497
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001498 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001499 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001500
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001501 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001502
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001503 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001504 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1505 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001506
1507 max_output_average += MaxAudioFrame(*frame_);
1508
Sam Zackrisson41478c72019-10-15 10:10:26 +02001509 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001510 analog_level_average += analog_level;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001511 AudioProcessingStats stats =
1512 apm_->GetStatistics(/*has_remote_tracks=*/false);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001513 EXPECT_TRUE(stats.voice_detected);
1514 EXPECT_TRUE(stats.output_rms_dbfs);
1515 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001516 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001517
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001518 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
Jonas Olssona4d87372019-07-05 19:08:33 +02001519 size_t write_count =
1520 fwrite(frame_->data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001521 ASSERT_EQ(frame_size, write_count);
1522
1523 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001524 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001525 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001526
1527#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1528 const int kStatsAggregationFrameNum = 100; // 1 second.
1529 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001530 // Get echo and delay metrics.
1531 AudioProcessingStats stats =
1532 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001533
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001534 // Echo metrics.
1535 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1536 const float echo_return_loss_enhancement =
1537 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001538 const float residual_echo_likelihood =
1539 stats.residual_echo_likelihood.value_or(-1.0f);
1540 const float residual_echo_likelihood_recent_max =
1541 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1542
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001543 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001544 const audioproc::Test::EchoMetrics& reference =
1545 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001546 constexpr float kEpsilon = 0.01;
1547 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1548 EXPECT_NEAR(echo_return_loss_enhancement,
1549 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001550 EXPECT_NEAR(residual_echo_likelihood,
1551 reference.residual_echo_likelihood(), kEpsilon);
1552 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1553 reference.residual_echo_likelihood_recent_max(),
1554 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001555 ++stats_index;
1556 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001557 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1558 message_echo->set_echo_return_loss(echo_return_loss);
1559 message_echo->set_echo_return_loss_enhancement(
1560 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001561 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1562 message_echo->set_residual_echo_likelihood_recent_max(
1563 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001564 }
1565 }
1566#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001567 }
1568 max_output_average /= frame_count;
1569 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001570 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001571
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001572 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001573 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001574 // When running the test on a N7 we get a {2, 6} difference of
1575 // |has_voice_count| and |max_output_average| is up to 18 higher.
1576 // All numbers being consistently higher on N7 compare to ref_data.
1577 // TODO(bjornv): If we start getting more of these offsets on Android we
1578 // should consider a different approach. Either using one slack for all,
1579 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001580#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001581 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001582 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001583 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001584 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001585#else
1586 const int kHasVoiceCountOffset = 0;
1587 const int kHasVoiceCountNear = kIntNear;
1588 const int kMaxOutputAverageOffset = 0;
1589 const int kMaxOutputAverageNear = kIntNear;
1590#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001591 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001592 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001593
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001594 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001595 EXPECT_NEAR(test->max_output_average(),
1596 max_output_average - kMaxOutputAverageOffset,
1597 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001598#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001599 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001600 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001601#endif
1602 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001603 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001604
1605 test->set_analog_level_average(analog_level_average);
1606 test->set_max_output_average(max_output_average);
1607
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001608#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001609 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001610#endif
1611 }
1612
1613 rewind(far_file_);
1614 rewind(near_file_);
1615 }
1616
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001617 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001618 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001619 }
1620}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001621
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001622TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1623 struct ChannelFormat {
1624 AudioProcessing::ChannelLayout in_layout;
1625 AudioProcessing::ChannelLayout out_layout;
1626 };
1627 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001628 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1629 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1630 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001631 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001632
Ivo Creusen62337e52018-01-09 14:17:33 +01001633 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001634 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001635 AudioProcessing::Config config;
1636 config.noise_suppression.enabled = true;
1637 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001638 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001639 const int in_rate = 44100;
1640 const int out_rate = 48000;
1641 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1642 TotalChannelsFromLayout(cf[i].in_layout));
1643 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1644 ChannelsFromLayout(cf[i].out_layout));
1645
1646 // Run over a few chunks.
1647 for (int j = 0; j < 10; ++j) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001648 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_cb.num_frames(),
1649 in_rate, cf[i].in_layout, out_rate,
1650 cf[i].out_layout, out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001651 }
1652 }
1653}
1654
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001655// Compares the reference and test arrays over a region around the expected
1656// delay. Finds the highest SNR in that region and adds the variance and squared
1657// error results to the supplied accumulators.
1658void UpdateBestSNR(const float* ref,
1659 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001660 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001661 int expected_delay,
1662 double* variance_acc,
1663 double* sq_error_acc) {
1664 double best_snr = std::numeric_limits<double>::min();
1665 double best_variance = 0;
1666 double best_sq_error = 0;
1667 // Search over a region of eight samples around the expected delay.
1668 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1669 ++delay) {
1670 double sq_error = 0;
1671 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001672 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001673 double error = test[i + delay] - ref[i];
1674 sq_error += error * error;
1675 variance += ref[i] * ref[i];
1676 }
1677
1678 if (sq_error == 0) {
1679 *variance_acc += variance;
1680 return;
1681 }
1682 double snr = variance / sq_error;
1683 if (snr > best_snr) {
1684 best_snr = snr;
1685 best_variance = variance;
1686 best_sq_error = sq_error;
1687 }
1688 }
1689
1690 *variance_acc += best_variance;
1691 *sq_error_acc += best_sq_error;
1692}
1693
1694// Used to test a multitude of sample rate and channel combinations. It works
1695// by first producing a set of reference files (in SetUpTestCase) that are
1696// assumed to be correct, as the used parameters are verified by other tests
1697// in this collection. Primarily the reference files are all produced at
1698// "native" rates which do not involve any resampling.
1699
1700// Each test pass produces an output file with a particular format. The output
1701// is matched against the reference file closest to its internal processing
1702// format. If necessary the output is resampled back to its process format.
1703// Due to the resampling distortion, we don't expect identical results, but
1704// enforce SNR thresholds which vary depending on the format. 0 is a special
1705// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001706typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001707class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001708 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001709 public:
1710 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001711 : input_rate_(std::get<0>(GetParam())),
1712 output_rate_(std::get<1>(GetParam())),
1713 reverse_input_rate_(std::get<2>(GetParam())),
1714 reverse_output_rate_(std::get<3>(GetParam())),
1715 expected_snr_(std::get<4>(GetParam())),
1716 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001717
1718 virtual ~AudioProcessingTest() {}
1719
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001720 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001721 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001722 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001723 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001724 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1725 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1726 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001727 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001728 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1729 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1730 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001731 }
1732 }
1733 }
1734 }
1735
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001736 void TearDown() {
1737 // Remove "out" files after each test.
1738 ClearTempOutFiles();
1739 }
1740
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001741 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001742
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001743 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001744 // to a file specified with |output_file_prefix|. Both forward and reverse
1745 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001746 static void ProcessFormat(int input_rate,
1747 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001748 int reverse_input_rate,
1749 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001750 size_t num_input_channels,
1751 size_t num_output_channels,
1752 size_t num_reverse_input_channels,
1753 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001754 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001755 Config config;
1756 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001757 std::unique_ptr<AudioProcessing> ap(
1758 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001759 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001760
ekmeyerson60d9b332015-08-14 10:35:55 -07001761 ProcessingConfig processing_config = {
1762 {{input_rate, num_input_channels},
1763 {output_rate, num_output_channels},
1764 {reverse_input_rate, num_reverse_input_channels},
1765 {reverse_output_rate, num_reverse_output_channels}}};
1766 ap->Initialize(processing_config);
1767
1768 FILE* far_file =
1769 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001770 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001771 FILE* out_file = fopen(
1772 OutputFilePath(
1773 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1774 reverse_output_rate, num_input_channels, num_output_channels,
1775 num_reverse_input_channels, num_reverse_output_channels, kForward)
1776 .c_str(),
1777 "wb");
1778 FILE* rev_out_file = fopen(
1779 OutputFilePath(
1780 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1781 reverse_output_rate, num_input_channels, num_output_channels,
1782 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1783 .c_str(),
1784 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001785 ASSERT_TRUE(far_file != NULL);
1786 ASSERT_TRUE(near_file != NULL);
1787 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001788 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001789
1790 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1791 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001792 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1793 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001794 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1795 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001796 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1797 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001798
1799 // Temporary buffers.
1800 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001801 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1802 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001803 std::unique_ptr<float[]> float_data(new float[max_length]);
1804 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001805
1806 int analog_level = 127;
1807 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1808 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001809 EXPECT_NOERR(ap->ProcessReverseStream(
1810 rev_cb.channels(), processing_config.reverse_input_stream(),
1811 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001812
1813 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001814 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001815
1816 EXPECT_NOERR(ap->ProcessStream(
Jonas Olssona4d87372019-07-05 19:08:33 +02001817 fwd_cb.channels(), fwd_cb.num_frames(), input_rate,
1818 LayoutFromChannels(num_input_channels), output_rate,
1819 LayoutFromChannels(num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001820
ekmeyerson60d9b332015-08-14 10:35:55 -07001821 // Dump forward output to file.
1822 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001823 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001824 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001825
Jonas Olssona4d87372019-07-05 19:08:33 +02001826 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1827 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001828
ekmeyerson60d9b332015-08-14 10:35:55 -07001829 // Dump reverse output to file.
1830 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
1831 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001832 size_t rev_out_length =
1833 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001834
Jonas Olssona4d87372019-07-05 19:08:33 +02001835 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1836 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07001837
Sam Zackrisson41478c72019-10-15 10:10:26 +02001838 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001839 }
1840 fclose(far_file);
1841 fclose(near_file);
1842 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07001843 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001844 }
1845
1846 protected:
1847 int input_rate_;
1848 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001849 int reverse_input_rate_;
1850 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001851 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001852 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001853};
1854
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00001855TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001856 struct ChannelFormat {
1857 int num_input;
1858 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07001859 int num_reverse_input;
1860 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001861 };
1862 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001863 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
1864 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001865 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001866
pkasting25702cb2016-01-08 13:50:27 -08001867 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001868 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
1869 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
1870 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07001871
ekmeyerson60d9b332015-08-14 10:35:55 -07001872 // Verify output for both directions.
1873 std::vector<StreamDirection> stream_directions;
1874 stream_directions.push_back(kForward);
1875 stream_directions.push_back(kReverse);
1876 for (StreamDirection file_direction : stream_directions) {
1877 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
1878 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
1879 const int out_num =
1880 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
1881 const double expected_snr =
1882 file_direction ? expected_reverse_snr_ : expected_snr_;
1883
1884 const int min_ref_rate = std::min(in_rate, out_rate);
1885 int ref_rate;
1886
1887 if (min_ref_rate > 32000) {
1888 ref_rate = 48000;
1889 } else if (min_ref_rate > 16000) {
1890 ref_rate = 32000;
1891 } else if (min_ref_rate > 8000) {
1892 ref_rate = 16000;
1893 } else {
1894 ref_rate = 8000;
1895 }
aluebs776593b2016-03-15 14:04:58 -07001896#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08001897 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07001898 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08001899 }
1900#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001901 FILE* out_file = fopen(
1902 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
1903 reverse_output_rate_, cf[i].num_input,
1904 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02001905 cf[i].num_reverse_output, file_direction)
1906 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07001907 "rb");
1908 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02001909 FILE* ref_file =
1910 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
1911 cf[i].num_output, cf[i].num_output,
1912 cf[i].num_reverse_output,
1913 cf[i].num_reverse_output, file_direction)
1914 .c_str(),
1915 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07001916 ASSERT_TRUE(out_file != NULL);
1917 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001918
pkasting25702cb2016-01-08 13:50:27 -08001919 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
1920 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07001921 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08001922 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001923 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08001924 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001925 // Data from the resampled output, in case the reference and output rates
1926 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08001927 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001928
ekmeyerson60d9b332015-08-14 10:35:55 -07001929 PushResampler<float> resampler;
1930 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001931
ekmeyerson60d9b332015-08-14 10:35:55 -07001932 // Compute the resampling delay of the output relative to the reference,
1933 // to find the region over which we should search for the best SNR.
1934 float expected_delay_sec = 0;
1935 if (in_rate != ref_rate) {
1936 // Input resampling delay.
1937 expected_delay_sec +=
1938 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
1939 }
1940 if (out_rate != ref_rate) {
1941 // Output resampling delay.
1942 expected_delay_sec +=
1943 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
1944 // Delay of converting the output back to its processing rate for
1945 // testing.
1946 expected_delay_sec +=
1947 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
1948 }
1949 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01001950 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001951
ekmeyerson60d9b332015-08-14 10:35:55 -07001952 double variance = 0;
1953 double sq_error = 0;
1954 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
1955 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
1956 float* out_ptr = out_data.get();
1957 if (out_rate != ref_rate) {
1958 // Resample the output back to its internal processing rate if
1959 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08001960 ASSERT_EQ(ref_length,
1961 static_cast<size_t>(resampler.Resample(
1962 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07001963 out_ptr = cmp_data.get();
1964 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001965
ekmeyerson60d9b332015-08-14 10:35:55 -07001966 // Update the |sq_error| and |variance| accumulators with the highest
1967 // SNR of reference vs output.
1968 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
1969 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001970 }
1971
ekmeyerson60d9b332015-08-14 10:35:55 -07001972 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
1973 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
1974 << cf[i].num_input << ", " << cf[i].num_output << ", "
1975 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
1976 << ", " << file_direction << "): ";
1977 if (sq_error > 0) {
1978 double snr = 10 * log10(variance / sq_error);
1979 EXPECT_GE(snr, expected_snr);
1980 EXPECT_NE(0, expected_snr);
1981 std::cout << "SNR=" << snr << " dB" << std::endl;
1982 } else {
aluebs776593b2016-03-15 14:04:58 -07001983 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07001984 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001985
ekmeyerson60d9b332015-08-14 10:35:55 -07001986 fclose(out_file);
1987 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001988 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001989 }
1990}
1991
1992#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01001993INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07001994 CommonFormats,
1995 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001996 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
1997 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
1998 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
1999 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2000 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2001 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2002 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2003 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2004 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2005 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2006 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2007 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002008
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002009 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2010 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2011 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2012 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2013 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2014 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2015 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2016 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2017 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2018 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2019 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2020 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002021
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002022 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2023 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2024 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2025 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2026 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2027 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2028 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2029 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2030 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2031 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2032 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2033 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002034
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002035 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2036 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2037 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2038 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2039 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2040 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2041 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2042 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2043 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2044 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2045 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2046 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002047
2048#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002049INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002050 CommonFormats,
2051 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002052 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2053 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2054 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002055 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2056 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2057 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002058 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2059 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2060 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002061 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2062 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2063 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002064
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002065 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2066 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2067 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2068 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2069 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2070 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002071 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2072 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2073 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2074 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2075 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2076 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002077
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002078 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2079 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2080 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2081 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2082 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2083 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2084 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2085 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2086 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2087 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2088 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2089 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002090
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002091 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2092 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2093 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2094 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2095 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2096 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2097 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2098 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2099 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2100 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2101 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2102 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002103#endif
2104
Per Åhgren3e8bf282019-08-29 23:38:40 +02002105// Produces a scoped trace debug output.
2106std::string ProduceDebugText(int render_input_sample_rate_hz,
2107 int render_output_sample_rate_hz,
2108 int capture_input_sample_rate_hz,
2109 int capture_output_sample_rate_hz,
2110 size_t render_input_num_channels,
2111 size_t render_output_num_channels,
2112 size_t capture_input_num_channels,
2113 size_t capture_output_num_channels) {
2114 rtc::StringBuilder ss;
2115 ss << "Sample rates:"
2116 << "\n"
2117 << " Render input: " << render_input_sample_rate_hz << " Hz"
2118 << "\n"
2119 << " Render output: " << render_output_sample_rate_hz << " Hz"
2120 << "\n"
2121 << " Capture input: " << capture_input_sample_rate_hz << " Hz"
2122 << "\n"
2123 << " Capture output: " << capture_output_sample_rate_hz << " Hz"
2124 << "\n"
2125 << "Number of channels:"
2126 << "\n"
2127 << " Render input: " << render_input_num_channels << "\n"
2128 << " Render output: " << render_output_num_channels << "\n"
2129 << " Capture input: " << capture_input_num_channels << "\n"
2130 << " Capture output: " << capture_output_num_channels;
2131 return ss.Release();
2132}
2133
2134// Validates that running the audio processing module using various combinations
2135// of sample rates and number of channels works as intended.
2136void RunApmRateAndChannelTest(
2137 rtc::ArrayView<const int> sample_rates_hz,
2138 rtc::ArrayView<const int> render_channel_counts,
2139 rtc::ArrayView<const int> capture_channel_counts) {
2140 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2141 webrtc::AudioProcessing::Config apm_config;
2142 apm_config.echo_canceller.enabled = true;
2143 apm->ApplyConfig(apm_config);
2144
2145 StreamConfig render_input_stream_config;
2146 StreamConfig render_output_stream_config;
2147 StreamConfig capture_input_stream_config;
2148 StreamConfig capture_output_stream_config;
2149
2150 std::vector<float> render_input_frame_channels;
2151 std::vector<float*> render_input_frame;
2152 std::vector<float> render_output_frame_channels;
2153 std::vector<float*> render_output_frame;
2154 std::vector<float> capture_input_frame_channels;
2155 std::vector<float*> capture_input_frame;
2156 std::vector<float> capture_output_frame_channels;
2157 std::vector<float*> capture_output_frame;
2158
2159 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2160 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2161 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2162 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2163 for (size_t render_input_num_channels : render_channel_counts) {
2164 for (size_t capture_input_num_channels : capture_channel_counts) {
2165 size_t render_output_num_channels = render_input_num_channels;
2166 size_t capture_output_num_channels = capture_input_num_channels;
2167 auto populate_audio_frame = [](int sample_rate_hz,
2168 size_t num_channels,
2169 StreamConfig* cfg,
2170 std::vector<float>* channels_data,
2171 std::vector<float*>* frame_data) {
2172 cfg->set_sample_rate_hz(sample_rate_hz);
2173 cfg->set_num_channels(num_channels);
2174 cfg->set_has_keyboard(false);
2175
2176 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2177 channels_data->resize(num_channels * max_frame_size);
2178 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2179 frame_data->resize(num_channels);
2180 for (size_t channel = 0; channel < num_channels; ++channel) {
2181 (*frame_data)[channel] =
2182 &(*channels_data)[channel * max_frame_size];
2183 }
2184 };
2185
2186 populate_audio_frame(
2187 render_input_sample_rate_hz, render_input_num_channels,
2188 &render_input_stream_config, &render_input_frame_channels,
2189 &render_input_frame);
2190 populate_audio_frame(
2191 render_output_sample_rate_hz, render_output_num_channels,
2192 &render_output_stream_config, &render_output_frame_channels,
2193 &render_output_frame);
2194 populate_audio_frame(
2195 capture_input_sample_rate_hz, capture_input_num_channels,
2196 &capture_input_stream_config, &capture_input_frame_channels,
2197 &capture_input_frame);
2198 populate_audio_frame(
2199 capture_output_sample_rate_hz, capture_output_num_channels,
2200 &capture_output_stream_config, &capture_output_frame_channels,
2201 &capture_output_frame);
2202
2203 for (size_t frame = 0; frame < 2; ++frame) {
2204 SCOPED_TRACE(ProduceDebugText(
2205 render_input_sample_rate_hz, render_output_sample_rate_hz,
2206 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2207 render_input_num_channels, render_output_num_channels,
2208 render_input_num_channels, capture_output_num_channels));
2209
2210 int result = apm->ProcessReverseStream(
2211 &render_input_frame[0], render_input_stream_config,
2212 render_output_stream_config, &render_output_frame[0]);
2213 EXPECT_EQ(result, AudioProcessing::kNoError);
2214 result = apm->ProcessStream(
2215 &capture_input_frame[0], capture_input_stream_config,
2216 capture_output_stream_config, &capture_output_frame[0]);
2217 EXPECT_EQ(result, AudioProcessing::kNoError);
2218 }
2219 }
2220 }
2221 }
2222 }
2223 }
2224 }
2225}
2226
niklase@google.com470e71d2011-07-07 08:21:25 +00002227} // namespace
peahc19f3122016-10-07 14:54:10 -07002228
Alessio Bazzicac054e782018-04-16 12:10:09 +02002229TEST(RuntimeSettingTest, TestDefaultCtor) {
2230 auto s = AudioProcessing::RuntimeSetting();
2231 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2232}
2233
2234TEST(RuntimeSettingTest, TestCapturePreGain) {
2235 using Type = AudioProcessing::RuntimeSetting::Type;
2236 {
2237 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2238 EXPECT_EQ(Type::kCapturePreGain, s.type());
2239 float v;
2240 s.GetFloat(&v);
2241 EXPECT_EQ(1.25f, v);
2242 }
2243
2244#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2245 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2246#endif
2247}
2248
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002249TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2250 using Type = AudioProcessing::RuntimeSetting::Type;
2251 {
2252 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2253 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2254 float v;
2255 s.GetFloat(&v);
2256 EXPECT_EQ(1.25f, v);
2257 }
2258
2259#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2260 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2261#endif
2262}
2263
Alessio Bazzicac054e782018-04-16 12:10:09 +02002264TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2265 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2266 auto s = AudioProcessing::RuntimeSetting();
2267 ASSERT_TRUE(q.Insert(&s));
2268 ASSERT_TRUE(q.Remove(&s));
2269 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2270}
2271
Sam Zackrisson0beac582017-09-25 12:04:02 +02002272TEST(ApmConfiguration, EnablePostProcessing) {
2273 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002274 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002275 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002276 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002277 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002278 rtc::scoped_refptr<AudioProcessing> apm =
2279 AudioProcessingBuilder()
2280 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002281 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002282
2283 AudioFrame audio;
2284 audio.num_channels_ = 1;
2285 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2286
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002287 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002288 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002289}
2290
Alex Loiko5825aa62017-12-18 16:02:40 +01002291TEST(ApmConfiguration, EnablePreProcessing) {
2292 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002293 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002294 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002295 auto mock_pre_processor =
2296 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002297 rtc::scoped_refptr<AudioProcessing> apm =
2298 AudioProcessingBuilder()
2299 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002300 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002301
2302 AudioFrame audio;
2303 audio.num_channels_ = 1;
2304 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2305
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002306 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002307 apm->ProcessReverseStream(&audio);
2308}
2309
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002310TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2311 // Verify that apm uses a capture analyzer if one is provided.
2312 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002313 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002314 auto mock_capture_analyzer =
2315 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2316 rtc::scoped_refptr<AudioProcessing> apm =
2317 AudioProcessingBuilder()
2318 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2319 .Create();
2320
2321 AudioFrame audio;
2322 audio.num_channels_ = 1;
2323 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2324
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002325 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002326 apm->ProcessStream(&audio);
2327}
2328
Alex Loiko73ec0192018-05-15 10:52:28 +02002329TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2330 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002331 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002332 auto mock_pre_processor =
2333 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2334 rtc::scoped_refptr<AudioProcessing> apm =
2335 AudioProcessingBuilder()
2336 .SetRenderPreProcessing(std::move(mock_pre_processor))
2337 .Create();
2338 apm->SetRuntimeSetting(
2339 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2340
2341 // RuntimeSettings forwarded during 'Process*Stream' calls.
2342 // Therefore we have to make one such call.
2343 AudioFrame audio;
2344 audio.num_channels_ = 1;
2345 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2346
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002347 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2348 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002349 apm->ProcessReverseStream(&audio);
2350}
2351
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002352class MyEchoControlFactory : public EchoControlFactory {
2353 public:
2354 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2355 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002356 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2357 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
2358 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_)).Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002359 return std::unique_ptr<EchoControl>(ec);
2360 }
Per Åhgrence202a02019-09-02 17:01:19 +02002361
2362 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
2363 size_t num_render_channels,
2364 size_t num_capture_channels) {
2365 return Create(sample_rate_hz);
2366 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002367};
2368
2369TEST(ApmConfiguration, EchoControlInjection) {
2370 // Verify that apm uses an injected echo controller if one is provided.
2371 webrtc::Config webrtc_config;
2372 std::unique_ptr<EchoControlFactory> echo_control_factory(
2373 new MyEchoControlFactory());
2374
Alex Loiko5825aa62017-12-18 16:02:40 +01002375 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002376 AudioProcessingBuilder()
2377 .SetEchoControlFactory(std::move(echo_control_factory))
2378 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002379
2380 AudioFrame audio;
2381 audio.num_channels_ = 1;
2382 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2383 apm->ProcessStream(&audio);
2384 apm->ProcessReverseStream(&audio);
2385 apm->ProcessStream(&audio);
2386}
Ivo Creusenae026092017-11-20 13:07:16 +01002387
Per Åhgren8607f842019-04-12 22:02:26 +02002388std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002389 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002390 std::unique_ptr<AudioProcessing> apm(
2391 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002392 if (!apm) {
2393 return apm;
2394 }
2395
2396 ProcessingConfig processing_config = {
2397 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2398
2399 if (apm->Initialize(processing_config) != 0) {
2400 return nullptr;
2401 }
2402
2403 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002404 AudioProcessing::Config apm_config;
2405 apm_config.residual_echo_detector.enabled = true;
2406 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002407 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002408 apm_config.gain_controller2.enabled = false;
2409 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002410 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002411 apm_config.noise_suppression.enabled = false;
2412 apm_config.level_estimation.enabled = false;
2413 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002414 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002415 return apm;
2416}
2417
2418#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2419#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2420#else
2421#define MAYBE_ApmStatistics ApmStatistics
2422#endif
2423
Per Åhgren8607f842019-04-12 22:02:26 +02002424TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2425 // Set up APM with AEC3 and process some audio.
2426 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002427 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002428 AudioProcessing::Config apm_config;
2429 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002430 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002431
2432 // Set up an audioframe.
2433 AudioFrame frame;
2434 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002435 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002436
2437 // Fill the audio frame with a sawtooth pattern.
2438 int16_t* ptr = frame.mutable_data();
2439 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2440 ptr[i] = 10000 * ((i % 3) - 1);
2441 }
2442
2443 // Do some processing.
2444 for (int i = 0; i < 200; i++) {
2445 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2446 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2447 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2448 }
2449
2450 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002451 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002452 // We expect all statistics to be set and have a sensible value.
2453 ASSERT_TRUE(stats.residual_echo_likelihood);
2454 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2455 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2456 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2457 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2458 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2459 ASSERT_TRUE(stats.echo_return_loss);
2460 EXPECT_NE(*stats.echo_return_loss, -100.0);
2461 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2462 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002463
2464 // If there are no receive streams, we expect the stats not to be set. The
2465 // 'false' argument signals to APM that no receive streams are currently
2466 // active. In that situation the statistics would get stuck at their last
2467 // calculated value (AEC and echo detection need at least one stream in each
2468 // direction), so to avoid that, they should not be set by APM.
2469 stats = apm->GetStatistics(false);
2470 EXPECT_FALSE(stats.residual_echo_likelihood);
2471 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2472 EXPECT_FALSE(stats.echo_return_loss);
2473 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002474}
2475
2476TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2477 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002478 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002479 ASSERT_TRUE(apm);
2480
2481 // Set up an audioframe.
2482 AudioFrame frame;
2483 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002484 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002485
2486 // Fill the audio frame with a sawtooth pattern.
2487 int16_t* ptr = frame.mutable_data();
2488 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2489 ptr[i] = 10000 * ((i % 3) - 1);
2490 }
2491
2492 // Do some processing.
2493 for (int i = 0; i < 200; i++) {
2494 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2495 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2496 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2497 }
2498
2499 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002500 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002501 // We expect only the residual echo detector statistics to be set and have a
2502 // sensible value.
2503 EXPECT_TRUE(stats.residual_echo_likelihood);
2504 if (stats.residual_echo_likelihood) {
2505 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2506 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2507 }
2508 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2509 if (stats.residual_echo_likelihood_recent_max) {
2510 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2511 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2512 }
2513 EXPECT_FALSE(stats.echo_return_loss);
2514 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002515
2516 // If there are no receive streams, we expect the stats not to be set.
2517 stats = apm->GetStatistics(false);
2518 EXPECT_FALSE(stats.residual_echo_likelihood);
2519 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2520 EXPECT_FALSE(stats.echo_return_loss);
2521 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002522}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002523
2524TEST(ApmStatistics, ReportOutputRmsDbfs) {
2525 ProcessingConfig processing_config = {
2526 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2527 AudioProcessing::Config config;
2528
2529 // Set up an audioframe.
2530 AudioFrame frame;
2531 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002532 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002533
2534 // Fill the audio frame with a sawtooth pattern.
2535 int16_t* ptr = frame.mutable_data();
2536 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2537 ptr[i] = 10000 * ((i % 3) - 1);
2538 }
2539
2540 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2541 apm->Initialize(processing_config);
2542
2543 // If not enabled, no metric should be reported.
2544 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2545 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2546
2547 // If enabled, metrics should be reported.
2548 config.level_estimation.enabled = true;
2549 apm->ApplyConfig(config);
2550 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2551 auto stats = apm->GetStatistics(false);
2552 EXPECT_TRUE(stats.output_rms_dbfs);
2553 EXPECT_GE(*stats.output_rms_dbfs, 0);
2554
2555 // If re-disabled, the value is again not reported.
2556 config.level_estimation.enabled = false;
2557 apm->ApplyConfig(config);
2558 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2559 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2560}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002561
2562TEST(ApmStatistics, ReportHasVoice) {
2563 ProcessingConfig processing_config = {
2564 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2565 AudioProcessing::Config config;
2566
2567 // Set up an audioframe.
2568 AudioFrame frame;
2569 frame.num_channels_ = 1;
2570 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2571
2572 // Fill the audio frame with a sawtooth pattern.
2573 int16_t* ptr = frame.mutable_data();
2574 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2575 ptr[i] = 10000 * ((i % 3) - 1);
2576 }
2577
2578 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2579 apm->Initialize(processing_config);
2580
2581 // If not enabled, no metric should be reported.
2582 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2583 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2584
2585 // If enabled, metrics should be reported.
2586 config.voice_detection.enabled = true;
2587 apm->ApplyConfig(config);
2588 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2589 auto stats = apm->GetStatistics(false);
2590 EXPECT_TRUE(stats.voice_detected);
2591
2592 // If re-disabled, the value is again not reported.
2593 config.voice_detection.enabled = false;
2594 apm->ApplyConfig(config);
2595 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2596 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2597}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002598
2599TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2600 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2601 std::array<int, 2> render_channel_counts = {1, 7};
2602 std::array<int, 2> capture_channel_counts = {1, 7};
2603 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2604 capture_channel_counts);
2605}
2606
2607TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2608 std::array<int, 1> sample_rates_hz = {48000};
2609 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2610 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2611 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2612 capture_channel_counts);
2613}
2614
2615TEST(ApmConfiguration, HandlingOfRateCombinations) {
2616 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2617 48000, 96000, 192000, 384000};
2618 std::array<int, 1> render_channel_counts = {2};
2619 std::array<int, 1> capture_channel_counts = {2};
2620 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2621 capture_channel_counts);
2622}
2623
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002624} // namespace webrtc