blob: 78a7220423781e7e101aa27c4f055b8feeca831e [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
peah8271d042016-11-22 07:24:52 -0800194 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100195 apm_config.level_estimation.enabled = true;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200196 apm_config.voice_detection.enabled = true;
peah8271d042016-11-22 07:24:52 -0800197 ap->ApplyConfig(apm_config);
198
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000199 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
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, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001040 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001041 NoiseSuppression::Level level[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001042 NoiseSuppression::kLow, NoiseSuppression::kModerate,
1043 NoiseSuppression::kHigh, NoiseSuppression::kVeryHigh};
pkasting25702cb2016-01-08 13:50:27 -08001044 for (size_t i = 0; i < arraysize(level); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001045 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->set_level(level[i]));
niklase@google.com470e71d2011-07-07 08:21:25 +00001046 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1047 }
1048
andrew@webrtc.org648af742012-02-08 01:57:29 +00001049 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1051 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1052 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1053 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1054}
1055
1056TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001057 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001058 AudioProcessing::Config apm_config;
1059 apm_config.high_pass_filter.enabled = true;
1060 apm_->ApplyConfig(apm_config);
1061 apm_config.high_pass_filter.enabled = false;
1062 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001063}
1064
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001065TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001066 AudioProcessing::Config config = apm_->GetConfig();
1067 EXPECT_FALSE(config.echo_canceller.enabled);
1068 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001069 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001070 EXPECT_FALSE(config.level_estimation.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001071 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001072 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001073}
1074
1075TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001076 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001077 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001078 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001079 AudioFrame frame_copy;
1080 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001081 for (int j = 0; j < 1000; j++) {
1082 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1083 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001084 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1085 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001086 }
1087 }
1088}
1089
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001090TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1091 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001092 const size_t kSamples = 160;
1093 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001094 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001095 float dest[kSamples] = {};
1096
1097 auto src_channels = &src[0];
1098 auto dest_channels = &dest[0];
1099
Ivo Creusen62337e52018-01-09 14:17:33 +01001100 apm_.reset(AudioProcessingBuilder().Create());
Jonas Olssona4d87372019-07-05 19:08:33 +02001101 EXPECT_NOERR(apm_->ProcessStream(&src_channels, kSamples, sample_rate,
1102 LayoutFromChannels(1), sample_rate,
1103 LayoutFromChannels(1), &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001104
1105 for (size_t i = 0; i < kSamples; ++i) {
1106 EXPECT_EQ(src[i], dest[i]);
1107 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001108
1109 // Same for ProcessReverseStream.
1110 float rev_dest[kSamples] = {};
1111 auto rev_dest_channels = &rev_dest[0];
1112
1113 StreamConfig input_stream = {sample_rate, 1};
1114 StreamConfig output_stream = {sample_rate, 1};
1115 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1116 output_stream, &rev_dest_channels));
1117
1118 for (size_t i = 0; i < kSamples; ++i) {
1119 EXPECT_EQ(src[i], rev_dest[i]);
1120 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001121}
1122
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001123TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1124 EnableAllComponents();
1125
pkasting25702cb2016-01-08 13:50:27 -08001126 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001127 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1128 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001129 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001130 ASSERT_EQ(0, feof(far_file_));
1131 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001132 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001133 CopyLeftToRightChannel(revframe_->mutable_data(),
1134 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001135
aluebsb0319552016-03-17 20:39:53 -07001136 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001137
yujo36b1a5f2017-06-12 12:45:32 -07001138 CopyLeftToRightChannel(frame_->mutable_data(),
1139 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001140 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1141
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001142 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001143 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001144 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001145 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001146
yujo36b1a5f2017-06-12 12:45:32 -07001147 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001148 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001149 rewind(far_file_);
1150 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001151 }
1152}
1153
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001154TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001155 // Verify the filter is not active through undistorted audio when:
1156 // 1. No components are enabled...
1157 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001158 AudioFrame frame_copy;
1159 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001160 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1161 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1162 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1163
1164 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001165 auto apm_config = apm_->GetConfig();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001166 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001167 frame_copy.CopyFrom(*frame_);
saza6787f232019-10-11 19:31:07 +02001168 apm_config.level_estimation.enabled = true;
1169 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001170 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1171 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1172 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001173 apm_config.level_estimation.enabled = false;
1174 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001175
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001176 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001177 SetFrameTo(frame_, 1000);
1178 frame_copy.CopyFrom(*frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001179 apm_config.voice_detection.enabled = true;
1180 apm_->ApplyConfig(apm_config);
1181 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1182 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1183 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1184 apm_config.voice_detection.enabled = false;
1185 apm_->ApplyConfig(apm_config);
1186
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001187 // 4. Both the VAD and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001188 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001189 frame_copy.CopyFrom(*frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001190 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001191 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001192 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001193 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1194 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1195 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001196 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001197 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001198 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001199
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001200 // Check the test is valid. We should have distortion from the filter
1201 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001202 apm_config.echo_canceller.enabled = true;
1203 apm_config.echo_canceller.mobile_mode = false;
1204 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001205 frame_->samples_per_channel_ = 320;
1206 frame_->num_channels_ = 2;
1207 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001208 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001209 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001210 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001211 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1212 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1213}
1214
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001215#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1216void ApmTest::ProcessDebugDump(const std::string& in_filename,
1217 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001218 Format format,
1219 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001220 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001221 FILE* in_file = fopen(in_filename.c_str(), "rb");
1222 ASSERT_TRUE(in_file != NULL);
1223 audioproc::Event event_msg;
1224 bool first_init = true;
1225
1226 while (ReadMessageFromFile(in_file, &event_msg)) {
1227 if (event_msg.type() == audioproc::Event::INIT) {
1228 const audioproc::Init msg = event_msg.init();
1229 int reverse_sample_rate = msg.sample_rate();
1230 if (msg.has_reverse_sample_rate()) {
1231 reverse_sample_rate = msg.reverse_sample_rate();
1232 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001233 int output_sample_rate = msg.sample_rate();
1234 if (msg.has_output_sample_rate()) {
1235 output_sample_rate = msg.output_sample_rate();
1236 }
1237
Jonas Olssona4d87372019-07-05 19:08:33 +02001238 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1239 msg.num_input_channels(), msg.num_output_channels(),
1240 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001241 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001242 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001243 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001244 auto aec_dump =
1245 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1246 EXPECT_TRUE(aec_dump);
1247 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001248 first_init = false;
1249 }
1250
1251 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1252 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1253
1254 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001255 ASSERT_EQ(revframe_->num_channels_,
1256 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001257 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001258 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1259 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001260 }
1261 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001262 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001263 if (format == kFloatFormat) {
1264 // We're using an int16 input file; convert to float.
1265 ConvertToFloat(*revframe_, revfloat_cb_.get());
1266 }
1267 }
1268 AnalyzeReverseStreamChooser(format);
1269
1270 } else if (event_msg.type() == audioproc::Event::STREAM) {
1271 const audioproc::Stream msg = event_msg.stream();
1272 // ProcessStream could have changed this for the output frame.
1273 frame_->num_channels_ = apm_->num_input_channels();
1274
Sam Zackrisson41478c72019-10-15 10:10:26 +02001275 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001276 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001277 if (msg.has_keypress()) {
1278 apm_->set_stream_key_pressed(msg.keypress());
1279 } else {
1280 apm_->set_stream_key_pressed(true);
1281 }
1282
1283 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001284 ASSERT_EQ(frame_->num_channels_,
1285 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001286 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001287 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1288 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001289 }
1290 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001291 memcpy(frame_->mutable_data(), msg.input_data().data(),
1292 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001293 if (format == kFloatFormat) {
1294 // We're using an int16 input file; convert to float.
1295 ConvertToFloat(*frame_, float_cb_.get());
1296 }
1297 }
1298 ProcessStreamChooser(format);
1299 }
1300 }
aleloif4dd1912017-06-15 01:55:38 -07001301 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001302 fclose(in_file);
1303}
1304
1305void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001306 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001307 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001308 std::string format_string;
1309 switch (format) {
1310 case kIntFormat:
1311 format_string = "_int";
1312 break;
1313 case kFloatFormat:
1314 format_string = "_float";
1315 break;
1316 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001317 const std::string ref_filename = test::TempFilename(
1318 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1319 const std::string out_filename = test::TempFilename(
1320 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001321 const std::string limited_filename = test::TempFilename(
1322 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1323 const size_t logging_limit_bytes = 100000;
1324 // We expect at least this many bytes in the created logfile.
1325 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001326 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001327 ProcessDebugDump(in_filename, ref_filename, format, -1);
1328 ProcessDebugDump(ref_filename, out_filename, format, -1);
1329 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001330
1331 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1332 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001333 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001334 ASSERT_TRUE(ref_file != NULL);
1335 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001336 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001337 std::unique_ptr<uint8_t[]> ref_bytes;
1338 std::unique_ptr<uint8_t[]> out_bytes;
1339 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001340
1341 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1342 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001343 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001344 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001345 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001346 while (ref_size > 0 && out_size > 0) {
1347 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001348 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001349 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001350 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001351 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001352 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001353 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1354 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001355 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001356 }
1357 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001358 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1359 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001360 EXPECT_NE(0, feof(ref_file));
1361 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001362 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001363 ASSERT_EQ(0, fclose(ref_file));
1364 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001365 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001366 remove(ref_filename.c_str());
1367 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001368 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001369}
1370
pbosc7a65692016-05-06 12:50:04 -07001371TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001372 VerifyDebugDumpTest(kIntFormat);
1373}
1374
pbosc7a65692016-05-06 12:50:04 -07001375TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001376 VerifyDebugDumpTest(kFloatFormat);
1377}
1378#endif
1379
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001380// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001381TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001382 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001383 const std::string filename =
1384 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001385 {
1386 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1387 EXPECT_FALSE(aec_dump);
1388 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001389
1390#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1391 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001392 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001393
aleloif4dd1912017-06-15 01:55:38 -07001394 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1395 EXPECT_TRUE(aec_dump);
1396 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001397 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001398 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001399 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001400
1401 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001402 FILE* fid = fopen(filename.c_str(), "r");
1403 ASSERT_TRUE(fid != NULL);
1404
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001405 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001406 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001407 ASSERT_EQ(0, remove(filename.c_str()));
1408#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001409 // Verify the file has NOT been written.
1410 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1411#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1412}
1413
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001414// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001415TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001416 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001417
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001418 const std::string filename =
1419 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001420 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1421 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001422
1423#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1424 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001425 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001426
Niels Möllere8e4dc42019-06-11 14:04:16 +02001427 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001428 EXPECT_TRUE(aec_dump);
1429 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001430 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001432 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001433
1434 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001435 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001436 ASSERT_TRUE(fid != NULL);
1437
1438 // Clean it up.
1439 ASSERT_EQ(0, fclose(fid));
1440 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001441#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1442}
1443
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001444// TODO(andrew): Add a test to process a few frames with different combinations
1445// of enabled components.
1446
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001447TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001448 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001449 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001450
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001451 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001452 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001453 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001454 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001455 for (size_t i = 0; i < arraysize(kChannels); i++) {
1456 for (size_t j = 0; j < arraysize(kChannels); j++) {
1457 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001458 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001459 test->set_num_reverse_channels(kChannels[i]);
1460 test->set_num_input_channels(kChannels[j]);
1461 test->set_num_output_channels(kChannels[j]);
1462 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001463 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001464 }
1465 }
1466 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001467#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1468 // To test the extended filter mode.
1469 audioproc::Test* test = ref_data.add_test();
1470 test->set_num_reverse_channels(2);
1471 test->set_num_input_channels(2);
1472 test->set_num_output_channels(2);
1473 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1474 test->set_use_aec_extended_filter(true);
1475#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001476 }
1477
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001478 for (int i = 0; i < ref_data.test_size(); i++) {
1479 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001480
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001481 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001482 // TODO(ajm): We no longer allow different input and output channels. Skip
1483 // these tests for now, but they should be removed from the set.
1484 if (test->num_input_channels() != test->num_output_channels())
1485 continue;
1486
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001487 Config config;
1488 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001489 config.Set<ExtendedFilter>(
1490 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001491 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001492
1493 EnableAllComponents();
1494
Jonas Olssona4d87372019-07-05 19:08:33 +02001495 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001496 static_cast<size_t>(test->num_input_channels()),
1497 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001498 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001499
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001500 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001501 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001502 int analog_level = 127;
1503 int analog_level_average = 0;
1504 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001505 float ns_speech_prob_average = 0.0f;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001506 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001507#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001508 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001509#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001510
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001511 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001512 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001513
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001514 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1515
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001516 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001517 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001518
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001519 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001520
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001521 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001522 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1523 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001524
1525 max_output_average += MaxAudioFrame(*frame_);
1526
Sam Zackrisson41478c72019-10-15 10:10:26 +02001527 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001528 analog_level_average += analog_level;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001529 AudioProcessingStats stats =
1530 apm_->GetStatistics(/*has_remote_tracks=*/false);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001531 EXPECT_TRUE(stats.voice_detected);
1532 EXPECT_TRUE(stats.output_rms_dbfs);
1533 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001534 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001535
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001536 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
1537
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001538 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
Jonas Olssona4d87372019-07-05 19:08:33 +02001539 size_t write_count =
1540 fwrite(frame_->data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001541 ASSERT_EQ(frame_size, write_count);
1542
1543 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001544 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001545 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001546
1547#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1548 const int kStatsAggregationFrameNum = 100; // 1 second.
1549 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001550 // Get echo and delay metrics.
1551 AudioProcessingStats stats =
1552 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001553
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001554 // Echo metrics.
1555 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1556 const float echo_return_loss_enhancement =
1557 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001558 const float residual_echo_likelihood =
1559 stats.residual_echo_likelihood.value_or(-1.0f);
1560 const float residual_echo_likelihood_recent_max =
1561 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1562
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001563 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001564 const audioproc::Test::EchoMetrics& reference =
1565 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001566 constexpr float kEpsilon = 0.01;
1567 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1568 EXPECT_NEAR(echo_return_loss_enhancement,
1569 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001570 EXPECT_NEAR(residual_echo_likelihood,
1571 reference.residual_echo_likelihood(), kEpsilon);
1572 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1573 reference.residual_echo_likelihood_recent_max(),
1574 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001575 ++stats_index;
1576 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001577 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1578 message_echo->set_echo_return_loss(echo_return_loss);
1579 message_echo->set_echo_return_loss_enhancement(
1580 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001581 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1582 message_echo->set_residual_echo_likelihood_recent_max(
1583 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001584 }
1585 }
1586#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001587 }
1588 max_output_average /= frame_count;
1589 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001590 ns_speech_prob_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001591 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001592
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001593 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001594 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001595 // When running the test on a N7 we get a {2, 6} difference of
1596 // |has_voice_count| and |max_output_average| is up to 18 higher.
1597 // All numbers being consistently higher on N7 compare to ref_data.
1598 // TODO(bjornv): If we start getting more of these offsets on Android we
1599 // should consider a different approach. Either using one slack for all,
1600 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001601#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001602 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001603 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001604 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001605 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001606#else
1607 const int kHasVoiceCountOffset = 0;
1608 const int kHasVoiceCountNear = kIntNear;
1609 const int kMaxOutputAverageOffset = 0;
1610 const int kMaxOutputAverageNear = kIntNear;
1611#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001612 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001613 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001614
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001615 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001616 EXPECT_NEAR(test->max_output_average(),
1617 max_output_average - kMaxOutputAverageOffset,
1618 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001619#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001620 const double kFloatNear = 0.0005;
Jonas Olssona4d87372019-07-05 19:08:33 +02001621 EXPECT_NEAR(test->ns_speech_probability_average(), ns_speech_prob_average,
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001622 kFloatNear);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001623 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001624#endif
1625 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001626 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001627
1628 test->set_analog_level_average(analog_level_average);
1629 test->set_max_output_average(max_output_average);
1630
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001631#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001632 EXPECT_LE(0.0f, ns_speech_prob_average);
1633 EXPECT_GE(1.0f, ns_speech_prob_average);
1634 test->set_ns_speech_probability_average(ns_speech_prob_average);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001635 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001636#endif
1637 }
1638
1639 rewind(far_file_);
1640 rewind(near_file_);
1641 }
1642
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001643 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001644 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001645 }
1646}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001647
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001648TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1649 struct ChannelFormat {
1650 AudioProcessing::ChannelLayout in_layout;
1651 AudioProcessing::ChannelLayout out_layout;
1652 };
1653 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001654 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1655 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1656 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001657 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001658
Ivo Creusen62337e52018-01-09 14:17:33 +01001659 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001660 // Enable one component just to ensure some processing takes place.
1661 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08001662 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001663 const int in_rate = 44100;
1664 const int out_rate = 48000;
1665 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1666 TotalChannelsFromLayout(cf[i].in_layout));
1667 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1668 ChannelsFromLayout(cf[i].out_layout));
1669
1670 // Run over a few chunks.
1671 for (int j = 0; j < 10; ++j) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001672 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_cb.num_frames(),
1673 in_rate, cf[i].in_layout, out_rate,
1674 cf[i].out_layout, out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001675 }
1676 }
1677}
1678
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001679// Compares the reference and test arrays over a region around the expected
1680// delay. Finds the highest SNR in that region and adds the variance and squared
1681// error results to the supplied accumulators.
1682void UpdateBestSNR(const float* ref,
1683 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001684 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001685 int expected_delay,
1686 double* variance_acc,
1687 double* sq_error_acc) {
1688 double best_snr = std::numeric_limits<double>::min();
1689 double best_variance = 0;
1690 double best_sq_error = 0;
1691 // Search over a region of eight samples around the expected delay.
1692 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1693 ++delay) {
1694 double sq_error = 0;
1695 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001696 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001697 double error = test[i + delay] - ref[i];
1698 sq_error += error * error;
1699 variance += ref[i] * ref[i];
1700 }
1701
1702 if (sq_error == 0) {
1703 *variance_acc += variance;
1704 return;
1705 }
1706 double snr = variance / sq_error;
1707 if (snr > best_snr) {
1708 best_snr = snr;
1709 best_variance = variance;
1710 best_sq_error = sq_error;
1711 }
1712 }
1713
1714 *variance_acc += best_variance;
1715 *sq_error_acc += best_sq_error;
1716}
1717
1718// Used to test a multitude of sample rate and channel combinations. It works
1719// by first producing a set of reference files (in SetUpTestCase) that are
1720// assumed to be correct, as the used parameters are verified by other tests
1721// in this collection. Primarily the reference files are all produced at
1722// "native" rates which do not involve any resampling.
1723
1724// Each test pass produces an output file with a particular format. The output
1725// is matched against the reference file closest to its internal processing
1726// format. If necessary the output is resampled back to its process format.
1727// Due to the resampling distortion, we don't expect identical results, but
1728// enforce SNR thresholds which vary depending on the format. 0 is a special
1729// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001730typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001731class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001732 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001733 public:
1734 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001735 : input_rate_(std::get<0>(GetParam())),
1736 output_rate_(std::get<1>(GetParam())),
1737 reverse_input_rate_(std::get<2>(GetParam())),
1738 reverse_output_rate_(std::get<3>(GetParam())),
1739 expected_snr_(std::get<4>(GetParam())),
1740 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001741
1742 virtual ~AudioProcessingTest() {}
1743
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001744 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001745 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001746 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001747 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001748 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1749 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1750 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001751 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001752 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1753 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1754 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001755 }
1756 }
1757 }
1758 }
1759
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001760 void TearDown() {
1761 // Remove "out" files after each test.
1762 ClearTempOutFiles();
1763 }
1764
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001765 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001766
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001767 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001768 // to a file specified with |output_file_prefix|. Both forward and reverse
1769 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001770 static void ProcessFormat(int input_rate,
1771 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001772 int reverse_input_rate,
1773 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001774 size_t num_input_channels,
1775 size_t num_output_channels,
1776 size_t num_reverse_input_channels,
1777 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001778 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001779 Config config;
1780 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001781 std::unique_ptr<AudioProcessing> ap(
1782 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001783 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001784
ekmeyerson60d9b332015-08-14 10:35:55 -07001785 ProcessingConfig processing_config = {
1786 {{input_rate, num_input_channels},
1787 {output_rate, num_output_channels},
1788 {reverse_input_rate, num_reverse_input_channels},
1789 {reverse_output_rate, num_reverse_output_channels}}};
1790 ap->Initialize(processing_config);
1791
1792 FILE* far_file =
1793 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001794 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001795 FILE* out_file = fopen(
1796 OutputFilePath(
1797 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1798 reverse_output_rate, num_input_channels, num_output_channels,
1799 num_reverse_input_channels, num_reverse_output_channels, kForward)
1800 .c_str(),
1801 "wb");
1802 FILE* rev_out_file = fopen(
1803 OutputFilePath(
1804 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1805 reverse_output_rate, num_input_channels, num_output_channels,
1806 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1807 .c_str(),
1808 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001809 ASSERT_TRUE(far_file != NULL);
1810 ASSERT_TRUE(near_file != NULL);
1811 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001812 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001813
1814 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1815 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001816 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1817 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001818 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1819 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001820 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1821 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001822
1823 // Temporary buffers.
1824 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001825 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1826 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001827 std::unique_ptr<float[]> float_data(new float[max_length]);
1828 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001829
1830 int analog_level = 127;
1831 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1832 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001833 EXPECT_NOERR(ap->ProcessReverseStream(
1834 rev_cb.channels(), processing_config.reverse_input_stream(),
1835 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001836
1837 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001838 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001839
1840 EXPECT_NOERR(ap->ProcessStream(
Jonas Olssona4d87372019-07-05 19:08:33 +02001841 fwd_cb.channels(), fwd_cb.num_frames(), input_rate,
1842 LayoutFromChannels(num_input_channels), output_rate,
1843 LayoutFromChannels(num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001844
ekmeyerson60d9b332015-08-14 10:35:55 -07001845 // Dump forward output to file.
1846 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001847 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001848 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001849
Jonas Olssona4d87372019-07-05 19:08:33 +02001850 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1851 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001852
ekmeyerson60d9b332015-08-14 10:35:55 -07001853 // Dump reverse output to file.
1854 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
1855 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001856 size_t rev_out_length =
1857 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001858
Jonas Olssona4d87372019-07-05 19:08:33 +02001859 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1860 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07001861
Sam Zackrisson41478c72019-10-15 10:10:26 +02001862 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001863 }
1864 fclose(far_file);
1865 fclose(near_file);
1866 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07001867 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001868 }
1869
1870 protected:
1871 int input_rate_;
1872 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001873 int reverse_input_rate_;
1874 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001875 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001876 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001877};
1878
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00001879TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001880 struct ChannelFormat {
1881 int num_input;
1882 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07001883 int num_reverse_input;
1884 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001885 };
1886 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001887 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
1888 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001889 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001890
pkasting25702cb2016-01-08 13:50:27 -08001891 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001892 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
1893 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
1894 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07001895
ekmeyerson60d9b332015-08-14 10:35:55 -07001896 // Verify output for both directions.
1897 std::vector<StreamDirection> stream_directions;
1898 stream_directions.push_back(kForward);
1899 stream_directions.push_back(kReverse);
1900 for (StreamDirection file_direction : stream_directions) {
1901 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
1902 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
1903 const int out_num =
1904 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
1905 const double expected_snr =
1906 file_direction ? expected_reverse_snr_ : expected_snr_;
1907
1908 const int min_ref_rate = std::min(in_rate, out_rate);
1909 int ref_rate;
1910
1911 if (min_ref_rate > 32000) {
1912 ref_rate = 48000;
1913 } else if (min_ref_rate > 16000) {
1914 ref_rate = 32000;
1915 } else if (min_ref_rate > 8000) {
1916 ref_rate = 16000;
1917 } else {
1918 ref_rate = 8000;
1919 }
aluebs776593b2016-03-15 14:04:58 -07001920#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08001921 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07001922 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08001923 }
1924#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001925 FILE* out_file = fopen(
1926 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
1927 reverse_output_rate_, cf[i].num_input,
1928 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02001929 cf[i].num_reverse_output, file_direction)
1930 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07001931 "rb");
1932 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02001933 FILE* ref_file =
1934 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
1935 cf[i].num_output, cf[i].num_output,
1936 cf[i].num_reverse_output,
1937 cf[i].num_reverse_output, file_direction)
1938 .c_str(),
1939 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07001940 ASSERT_TRUE(out_file != NULL);
1941 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001942
pkasting25702cb2016-01-08 13:50:27 -08001943 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
1944 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07001945 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08001946 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001947 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08001948 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001949 // Data from the resampled output, in case the reference and output rates
1950 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08001951 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001952
ekmeyerson60d9b332015-08-14 10:35:55 -07001953 PushResampler<float> resampler;
1954 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001955
ekmeyerson60d9b332015-08-14 10:35:55 -07001956 // Compute the resampling delay of the output relative to the reference,
1957 // to find the region over which we should search for the best SNR.
1958 float expected_delay_sec = 0;
1959 if (in_rate != ref_rate) {
1960 // Input resampling delay.
1961 expected_delay_sec +=
1962 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
1963 }
1964 if (out_rate != ref_rate) {
1965 // Output resampling delay.
1966 expected_delay_sec +=
1967 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
1968 // Delay of converting the output back to its processing rate for
1969 // testing.
1970 expected_delay_sec +=
1971 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
1972 }
1973 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01001974 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001975
ekmeyerson60d9b332015-08-14 10:35:55 -07001976 double variance = 0;
1977 double sq_error = 0;
1978 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
1979 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
1980 float* out_ptr = out_data.get();
1981 if (out_rate != ref_rate) {
1982 // Resample the output back to its internal processing rate if
1983 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08001984 ASSERT_EQ(ref_length,
1985 static_cast<size_t>(resampler.Resample(
1986 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07001987 out_ptr = cmp_data.get();
1988 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001989
ekmeyerson60d9b332015-08-14 10:35:55 -07001990 // Update the |sq_error| and |variance| accumulators with the highest
1991 // SNR of reference vs output.
1992 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
1993 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001994 }
1995
ekmeyerson60d9b332015-08-14 10:35:55 -07001996 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
1997 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
1998 << cf[i].num_input << ", " << cf[i].num_output << ", "
1999 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2000 << ", " << file_direction << "): ";
2001 if (sq_error > 0) {
2002 double snr = 10 * log10(variance / sq_error);
2003 EXPECT_GE(snr, expected_snr);
2004 EXPECT_NE(0, expected_snr);
2005 std::cout << "SNR=" << snr << " dB" << std::endl;
2006 } else {
aluebs776593b2016-03-15 14:04:58 -07002007 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002008 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002009
ekmeyerson60d9b332015-08-14 10:35:55 -07002010 fclose(out_file);
2011 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002012 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002013 }
2014}
2015
2016#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002017INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002018 CommonFormats,
2019 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002020 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2021 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2022 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2023 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2024 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2025 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2026 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2027 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2028 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2029 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2030 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2031 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002032
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002033 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2034 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2035 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2036 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2037 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2038 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2039 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2040 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2041 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2042 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2043 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2044 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002045
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002046 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2047 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2048 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2049 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2050 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2051 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2052 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2053 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2054 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2055 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2056 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2057 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002058
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002059 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2060 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2061 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2062 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2063 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2064 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2065 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2066 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2067 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2068 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2069 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2070 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002071
2072#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002073INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002074 CommonFormats,
2075 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002076 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2077 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2078 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002079 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2080 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2081 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002082 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2083 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2084 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002085 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2086 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2087 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002088
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002089 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2090 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2091 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2092 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2093 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2094 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002095 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2096 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2097 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2098 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2099 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2100 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002101
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002102 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2103 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2104 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2105 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2106 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2107 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2108 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2109 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2110 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2111 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2112 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2113 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002114
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002115 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2116 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2117 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2118 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2119 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2120 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2121 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2122 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2123 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2124 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2125 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2126 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002127#endif
2128
Per Åhgren3e8bf282019-08-29 23:38:40 +02002129// Produces a scoped trace debug output.
2130std::string ProduceDebugText(int render_input_sample_rate_hz,
2131 int render_output_sample_rate_hz,
2132 int capture_input_sample_rate_hz,
2133 int capture_output_sample_rate_hz,
2134 size_t render_input_num_channels,
2135 size_t render_output_num_channels,
2136 size_t capture_input_num_channels,
2137 size_t capture_output_num_channels) {
2138 rtc::StringBuilder ss;
2139 ss << "Sample rates:"
2140 << "\n"
2141 << " Render input: " << render_input_sample_rate_hz << " Hz"
2142 << "\n"
2143 << " Render output: " << render_output_sample_rate_hz << " Hz"
2144 << "\n"
2145 << " Capture input: " << capture_input_sample_rate_hz << " Hz"
2146 << "\n"
2147 << " Capture output: " << capture_output_sample_rate_hz << " Hz"
2148 << "\n"
2149 << "Number of channels:"
2150 << "\n"
2151 << " Render input: " << render_input_num_channels << "\n"
2152 << " Render output: " << render_output_num_channels << "\n"
2153 << " Capture input: " << capture_input_num_channels << "\n"
2154 << " Capture output: " << capture_output_num_channels;
2155 return ss.Release();
2156}
2157
2158// Validates that running the audio processing module using various combinations
2159// of sample rates and number of channels works as intended.
2160void RunApmRateAndChannelTest(
2161 rtc::ArrayView<const int> sample_rates_hz,
2162 rtc::ArrayView<const int> render_channel_counts,
2163 rtc::ArrayView<const int> capture_channel_counts) {
2164 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2165 webrtc::AudioProcessing::Config apm_config;
2166 apm_config.echo_canceller.enabled = true;
2167 apm->ApplyConfig(apm_config);
2168
2169 StreamConfig render_input_stream_config;
2170 StreamConfig render_output_stream_config;
2171 StreamConfig capture_input_stream_config;
2172 StreamConfig capture_output_stream_config;
2173
2174 std::vector<float> render_input_frame_channels;
2175 std::vector<float*> render_input_frame;
2176 std::vector<float> render_output_frame_channels;
2177 std::vector<float*> render_output_frame;
2178 std::vector<float> capture_input_frame_channels;
2179 std::vector<float*> capture_input_frame;
2180 std::vector<float> capture_output_frame_channels;
2181 std::vector<float*> capture_output_frame;
2182
2183 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2184 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2185 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2186 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2187 for (size_t render_input_num_channels : render_channel_counts) {
2188 for (size_t capture_input_num_channels : capture_channel_counts) {
2189 size_t render_output_num_channels = render_input_num_channels;
2190 size_t capture_output_num_channels = capture_input_num_channels;
2191 auto populate_audio_frame = [](int sample_rate_hz,
2192 size_t num_channels,
2193 StreamConfig* cfg,
2194 std::vector<float>* channels_data,
2195 std::vector<float*>* frame_data) {
2196 cfg->set_sample_rate_hz(sample_rate_hz);
2197 cfg->set_num_channels(num_channels);
2198 cfg->set_has_keyboard(false);
2199
2200 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2201 channels_data->resize(num_channels * max_frame_size);
2202 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2203 frame_data->resize(num_channels);
2204 for (size_t channel = 0; channel < num_channels; ++channel) {
2205 (*frame_data)[channel] =
2206 &(*channels_data)[channel * max_frame_size];
2207 }
2208 };
2209
2210 populate_audio_frame(
2211 render_input_sample_rate_hz, render_input_num_channels,
2212 &render_input_stream_config, &render_input_frame_channels,
2213 &render_input_frame);
2214 populate_audio_frame(
2215 render_output_sample_rate_hz, render_output_num_channels,
2216 &render_output_stream_config, &render_output_frame_channels,
2217 &render_output_frame);
2218 populate_audio_frame(
2219 capture_input_sample_rate_hz, capture_input_num_channels,
2220 &capture_input_stream_config, &capture_input_frame_channels,
2221 &capture_input_frame);
2222 populate_audio_frame(
2223 capture_output_sample_rate_hz, capture_output_num_channels,
2224 &capture_output_stream_config, &capture_output_frame_channels,
2225 &capture_output_frame);
2226
2227 for (size_t frame = 0; frame < 2; ++frame) {
2228 SCOPED_TRACE(ProduceDebugText(
2229 render_input_sample_rate_hz, render_output_sample_rate_hz,
2230 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2231 render_input_num_channels, render_output_num_channels,
2232 render_input_num_channels, capture_output_num_channels));
2233
2234 int result = apm->ProcessReverseStream(
2235 &render_input_frame[0], render_input_stream_config,
2236 render_output_stream_config, &render_output_frame[0]);
2237 EXPECT_EQ(result, AudioProcessing::kNoError);
2238 result = apm->ProcessStream(
2239 &capture_input_frame[0], capture_input_stream_config,
2240 capture_output_stream_config, &capture_output_frame[0]);
2241 EXPECT_EQ(result, AudioProcessing::kNoError);
2242 }
2243 }
2244 }
2245 }
2246 }
2247 }
2248 }
2249}
2250
niklase@google.com470e71d2011-07-07 08:21:25 +00002251} // namespace
peahc19f3122016-10-07 14:54:10 -07002252
Alessio Bazzicac054e782018-04-16 12:10:09 +02002253TEST(RuntimeSettingTest, TestDefaultCtor) {
2254 auto s = AudioProcessing::RuntimeSetting();
2255 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2256}
2257
2258TEST(RuntimeSettingTest, TestCapturePreGain) {
2259 using Type = AudioProcessing::RuntimeSetting::Type;
2260 {
2261 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2262 EXPECT_EQ(Type::kCapturePreGain, s.type());
2263 float v;
2264 s.GetFloat(&v);
2265 EXPECT_EQ(1.25f, v);
2266 }
2267
2268#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2269 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2270#endif
2271}
2272
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002273TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2274 using Type = AudioProcessing::RuntimeSetting::Type;
2275 {
2276 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2277 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2278 float v;
2279 s.GetFloat(&v);
2280 EXPECT_EQ(1.25f, v);
2281 }
2282
2283#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2284 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2285#endif
2286}
2287
Alessio Bazzicac054e782018-04-16 12:10:09 +02002288TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2289 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2290 auto s = AudioProcessing::RuntimeSetting();
2291 ASSERT_TRUE(q.Insert(&s));
2292 ASSERT_TRUE(q.Remove(&s));
2293 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2294}
2295
Sam Zackrisson0beac582017-09-25 12:04:02 +02002296TEST(ApmConfiguration, EnablePostProcessing) {
2297 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002298 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002299 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002300 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002301 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002302 rtc::scoped_refptr<AudioProcessing> apm =
2303 AudioProcessingBuilder()
2304 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002305 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002306
2307 AudioFrame audio;
2308 audio.num_channels_ = 1;
2309 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2310
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002311 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002312 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002313}
2314
Alex Loiko5825aa62017-12-18 16:02:40 +01002315TEST(ApmConfiguration, EnablePreProcessing) {
2316 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002317 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002318 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002319 auto mock_pre_processor =
2320 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002321 rtc::scoped_refptr<AudioProcessing> apm =
2322 AudioProcessingBuilder()
2323 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002324 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002325
2326 AudioFrame audio;
2327 audio.num_channels_ = 1;
2328 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2329
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002330 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002331 apm->ProcessReverseStream(&audio);
2332}
2333
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002334TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2335 // Verify that apm uses a capture analyzer if one is provided.
2336 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002337 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002338 auto mock_capture_analyzer =
2339 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2340 rtc::scoped_refptr<AudioProcessing> apm =
2341 AudioProcessingBuilder()
2342 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2343 .Create();
2344
2345 AudioFrame audio;
2346 audio.num_channels_ = 1;
2347 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2348
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002349 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002350 apm->ProcessStream(&audio);
2351}
2352
Alex Loiko73ec0192018-05-15 10:52:28 +02002353TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2354 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002355 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002356 auto mock_pre_processor =
2357 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2358 rtc::scoped_refptr<AudioProcessing> apm =
2359 AudioProcessingBuilder()
2360 .SetRenderPreProcessing(std::move(mock_pre_processor))
2361 .Create();
2362 apm->SetRuntimeSetting(
2363 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2364
2365 // RuntimeSettings forwarded during 'Process*Stream' calls.
2366 // Therefore we have to make one such call.
2367 AudioFrame audio;
2368 audio.num_channels_ = 1;
2369 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2370
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002371 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2372 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002373 apm->ProcessReverseStream(&audio);
2374}
2375
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002376class MyEchoControlFactory : public EchoControlFactory {
2377 public:
2378 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2379 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002380 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2381 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
2382 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_)).Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002383 return std::unique_ptr<EchoControl>(ec);
2384 }
Per Åhgrence202a02019-09-02 17:01:19 +02002385
2386 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
2387 size_t num_render_channels,
2388 size_t num_capture_channels) {
2389 return Create(sample_rate_hz);
2390 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002391};
2392
2393TEST(ApmConfiguration, EchoControlInjection) {
2394 // Verify that apm uses an injected echo controller if one is provided.
2395 webrtc::Config webrtc_config;
2396 std::unique_ptr<EchoControlFactory> echo_control_factory(
2397 new MyEchoControlFactory());
2398
Alex Loiko5825aa62017-12-18 16:02:40 +01002399 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002400 AudioProcessingBuilder()
2401 .SetEchoControlFactory(std::move(echo_control_factory))
2402 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002403
2404 AudioFrame audio;
2405 audio.num_channels_ = 1;
2406 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2407 apm->ProcessStream(&audio);
2408 apm->ProcessReverseStream(&audio);
2409 apm->ProcessStream(&audio);
2410}
Ivo Creusenae026092017-11-20 13:07:16 +01002411
Per Åhgren8607f842019-04-12 22:02:26 +02002412std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002413 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002414 std::unique_ptr<AudioProcessing> apm(
2415 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002416 if (!apm) {
2417 return apm;
2418 }
2419
2420 ProcessingConfig processing_config = {
2421 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2422
2423 if (apm->Initialize(processing_config) != 0) {
2424 return nullptr;
2425 }
2426
2427 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002428 AudioProcessing::Config apm_config;
2429 apm_config.residual_echo_detector.enabled = true;
2430 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002431 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002432 apm_config.gain_controller2.enabled = false;
2433 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002434 apm_config.echo_canceller.mobile_mode = mobile_aec;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002435 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002436 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002437 return apm;
2438}
2439
2440#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2441#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2442#else
2443#define MAYBE_ApmStatistics ApmStatistics
2444#endif
2445
Per Åhgren8607f842019-04-12 22:02:26 +02002446TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2447 // Set up APM with AEC3 and process some audio.
2448 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002449 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002450 AudioProcessing::Config apm_config;
2451 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002452 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002453
2454 // Set up an audioframe.
2455 AudioFrame frame;
2456 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002457 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002458
2459 // Fill the audio frame with a sawtooth pattern.
2460 int16_t* ptr = frame.mutable_data();
2461 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2462 ptr[i] = 10000 * ((i % 3) - 1);
2463 }
2464
2465 // Do some processing.
2466 for (int i = 0; i < 200; i++) {
2467 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2468 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2469 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2470 }
2471
2472 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002473 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002474 // We expect all statistics to be set and have a sensible value.
2475 ASSERT_TRUE(stats.residual_echo_likelihood);
2476 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2477 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2478 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2479 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2480 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2481 ASSERT_TRUE(stats.echo_return_loss);
2482 EXPECT_NE(*stats.echo_return_loss, -100.0);
2483 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2484 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002485
2486 // If there are no receive streams, we expect the stats not to be set. The
2487 // 'false' argument signals to APM that no receive streams are currently
2488 // active. In that situation the statistics would get stuck at their last
2489 // calculated value (AEC and echo detection need at least one stream in each
2490 // direction), so to avoid that, they should not be set by APM.
2491 stats = apm->GetStatistics(false);
2492 EXPECT_FALSE(stats.residual_echo_likelihood);
2493 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2494 EXPECT_FALSE(stats.echo_return_loss);
2495 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002496}
2497
2498TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2499 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002500 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002501 ASSERT_TRUE(apm);
2502
2503 // Set up an audioframe.
2504 AudioFrame frame;
2505 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002506 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002507
2508 // Fill the audio frame with a sawtooth pattern.
2509 int16_t* ptr = frame.mutable_data();
2510 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2511 ptr[i] = 10000 * ((i % 3) - 1);
2512 }
2513
2514 // Do some processing.
2515 for (int i = 0; i < 200; i++) {
2516 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2517 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2518 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2519 }
2520
2521 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002522 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002523 // We expect only the residual echo detector statistics to be set and have a
2524 // sensible value.
2525 EXPECT_TRUE(stats.residual_echo_likelihood);
2526 if (stats.residual_echo_likelihood) {
2527 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2528 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2529 }
2530 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2531 if (stats.residual_echo_likelihood_recent_max) {
2532 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2533 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2534 }
2535 EXPECT_FALSE(stats.echo_return_loss);
2536 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002537
2538 // If there are no receive streams, we expect the stats not to be set.
2539 stats = apm->GetStatistics(false);
2540 EXPECT_FALSE(stats.residual_echo_likelihood);
2541 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2542 EXPECT_FALSE(stats.echo_return_loss);
2543 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002544}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002545
2546TEST(ApmStatistics, ReportOutputRmsDbfs) {
2547 ProcessingConfig processing_config = {
2548 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2549 AudioProcessing::Config config;
2550
2551 // Set up an audioframe.
2552 AudioFrame frame;
2553 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002554 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002555
2556 // Fill the audio frame with a sawtooth pattern.
2557 int16_t* ptr = frame.mutable_data();
2558 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2559 ptr[i] = 10000 * ((i % 3) - 1);
2560 }
2561
2562 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2563 apm->Initialize(processing_config);
2564
2565 // If not enabled, no metric should be reported.
2566 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2567 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2568
2569 // If enabled, metrics should be reported.
2570 config.level_estimation.enabled = true;
2571 apm->ApplyConfig(config);
2572 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2573 auto stats = apm->GetStatistics(false);
2574 EXPECT_TRUE(stats.output_rms_dbfs);
2575 EXPECT_GE(*stats.output_rms_dbfs, 0);
2576
2577 // If re-disabled, the value is again not reported.
2578 config.level_estimation.enabled = false;
2579 apm->ApplyConfig(config);
2580 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2581 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2582}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002583
2584TEST(ApmStatistics, ReportHasVoice) {
2585 ProcessingConfig processing_config = {
2586 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2587 AudioProcessing::Config config;
2588
2589 // Set up an audioframe.
2590 AudioFrame frame;
2591 frame.num_channels_ = 1;
2592 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2593
2594 // Fill the audio frame with a sawtooth pattern.
2595 int16_t* ptr = frame.mutable_data();
2596 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2597 ptr[i] = 10000 * ((i % 3) - 1);
2598 }
2599
2600 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2601 apm->Initialize(processing_config);
2602
2603 // If not enabled, no metric should be reported.
2604 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2605 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2606
2607 // If enabled, metrics should be reported.
2608 config.voice_detection.enabled = true;
2609 apm->ApplyConfig(config);
2610 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2611 auto stats = apm->GetStatistics(false);
2612 EXPECT_TRUE(stats.voice_detected);
2613
2614 // If re-disabled, the value is again not reported.
2615 config.voice_detection.enabled = false;
2616 apm->ApplyConfig(config);
2617 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2618 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2619}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002620
2621TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2622 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2623 std::array<int, 2> render_channel_counts = {1, 7};
2624 std::array<int, 2> capture_channel_counts = {1, 7};
2625 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2626 capture_channel_counts);
2627}
2628
2629TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2630 std::array<int, 1> sample_rates_hz = {48000};
2631 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2632 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2633 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2634 capture_channel_counts);
2635}
2636
2637TEST(ApmConfiguration, HandlingOfRateCombinations) {
2638 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2639 48000, 96000, 192000, 384000};
2640 std::array<int, 1> render_channel_counts = {2};
2641 std::array<int, 1> capture_channel_counts = {2};
2642 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2643 capture_channel_counts);
2644}
2645
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002646} // namespace webrtc