blob: 8f29a739ed39353ce495e1711f0915881eb53221 [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>
Sam Zackrissone277bde2019-10-25 10:07:54 +020019#include <numeric>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000020#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000021
Sam Zackrisson6558fa52019-08-26 10:12:41 +020022#include "absl/flags/flag.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
24#include "common_audio/resampler/include/push_resampler.h"
25#include "common_audio/resampler/push_sinc_resampler.h"
26#include "common_audio/signal_processing/include/signal_processing_library.h"
27#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
28#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020030#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_processing/test/protobuf_utils.h"
32#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/arraysize.h"
34#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080035#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/gtest_prod_util.h"
37#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010038#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010039#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020042#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020043#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020044#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010045#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070049
50RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000052#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000053#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020054#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000055#endif
kwiberg77eab702016-09-28 17:42:01 -070056RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000057
Sam Zackrisson6558fa52019-08-26 10:12:41 +020058ABSL_FLAG(bool,
59 write_apm_ref_data,
60 false,
61 "Write ApmTest.Process results to file, instead of comparing results "
62 "to the existing reference data file.");
63
andrew@webrtc.org27c69802014-02-18 20:24:56 +000064namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000065namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000066
ekmeyerson60d9b332015-08-14 10:35:55 -070067// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
68// applicable.
69
mbonadei7c2c8432017-04-07 00:59:12 -070070const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070071const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000072
aluebseb3603b2016-04-20 15:27:58 -070073#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
74// Android doesn't support 48kHz.
75const int kProcessSampleRates[] = {8000, 16000, 32000};
76#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070077const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070078#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000079
ekmeyerson60d9b332015-08-14 10:35:55 -070080enum StreamDirection { kForward = 0, kReverse };
81
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020083 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
84 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080086 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020087 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000089}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000090
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000091void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070092 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093}
94
andrew@webrtc.org103657b2014-04-24 18:28:56 +000095// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080096size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097 switch (layout) {
98 case AudioProcessing::kMono:
99 return 1;
100 case AudioProcessing::kMonoAndKeyboard:
101 case AudioProcessing::kStereo:
102 return 2;
103 case AudioProcessing::kStereoAndKeyboard:
104 return 3;
105 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700106 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800107 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000108}
109
Jonas Olssona4d87372019-07-05 19:08:33 +0200110void MixStereoToMono(const float* stereo,
111 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800112 size_t samples_per_channel) {
113 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000115}
116
Jonas Olssona4d87372019-07-05 19:08:33 +0200117void MixStereoToMono(const int16_t* stereo,
118 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800119 size_t samples_per_channel) {
120 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
122}
123
pkasting25702cb2016-01-08 13:50:27 -0800124void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000126 stereo[i * 2 + 1] = stereo[i * 2];
127 }
128}
129
yujo36b1a5f2017-06-12 12:45:32 -0700130void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800131 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000132 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
133 }
134}
135
136void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700137 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700138 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
139 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700140 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000141 }
142}
143
144void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800145 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700146 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700148 frame_data[i] = left;
149 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000150 }
151}
152
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000153void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700154 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700155 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
156 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700157 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158 }
159}
160
andrew@webrtc.org81865342012-10-27 00:28:27 +0000161bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000162 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000163 return false;
164 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166 return false;
167 }
yujo36b1a5f2017-06-12 12:45:32 -0700168 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000169 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 return false;
172 }
173 return true;
174}
175
Sam Zackrissone277bde2019-10-25 10:07:54 +0200176rtc::ArrayView<int16_t> GetMutableFrameData(AudioFrame* frame) {
177 int16_t* ptr = frame->mutable_data();
178 const size_t len = frame->samples_per_channel() * frame->num_channels();
179 return rtc::ArrayView<int16_t>(ptr, len);
180}
181
182rtc::ArrayView<const int16_t> GetFrameData(const AudioFrame& frame) {
183 const int16_t* ptr = frame.data();
184 const size_t len = frame.samples_per_channel() * frame.num_channels();
185 return rtc::ArrayView<const int16_t>(ptr, len);
186}
187
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000188void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200189 AudioProcessing::Config apm_config = ap->GetConfig();
190 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000191#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200192 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100193
194 apm_config.gain_controller1.enabled = true;
195 apm_config.gain_controller1.mode =
196 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000197#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200198 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000199
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100200 apm_config.gain_controller1.enabled = true;
201 apm_config.gain_controller1.mode =
202 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
203 apm_config.gain_controller1.analog_level_minimum = 0;
204 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000205#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000206
saza0bad15f2019-10-16 11:46:11 +0200207 apm_config.noise_suppression.enabled = true;
208
peah8271d042016-11-22 07:24:52 -0800209 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100210 apm_config.level_estimation.enabled = true;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200211 apm_config.voice_detection.enabled = true;
peah8271d042016-11-22 07:24:52 -0800212 ap->ApplyConfig(apm_config);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000213}
214
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000215// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000216template <class T>
217T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200218 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000219}
220
221int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800222 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700223 const int16_t* frame_data = frame.data();
224 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800225 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700226 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000227 }
228
229 return max_data;
230}
231
Alex Loiko890988c2017-08-31 10:25:48 +0200232void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700233 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000235 ASSERT_TRUE(file != NULL);
236
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100237 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800239 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000240 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000242 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200244 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000245 fclose(file);
246}
247
Alex Loiko890988c2017-08-31 10:25:48 +0200248std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200249 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000250 // Resource files are all stereo.
251 ss << name << sample_rate_hz / 1000 << "_stereo";
252 return test::ResourcePath(ss.str(), "pcm");
253}
254
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000255// Temporary filenames unique to this process. Used to be able to run these
256// tests in parallel as each process needs to be running in isolation they can't
257// have competing filenames.
258std::map<std::string, std::string> temp_filenames;
259
Alex Loiko890988c2017-08-31 10:25:48 +0200260std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000261 int input_rate,
262 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700263 int reverse_input_rate,
264 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800265 size_t num_input_channels,
266 size_t num_output_channels,
267 size_t num_reverse_input_channels,
268 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700269 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200270 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700271 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
272 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000273 if (num_output_channels == 1) {
274 ss << "mono";
275 } else if (num_output_channels == 2) {
276 ss << "stereo";
277 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700278 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700280 ss << output_rate / 1000;
281 if (num_reverse_output_channels == 1) {
282 ss << "_rmono";
283 } else if (num_reverse_output_channels == 2) {
284 ss << "_rstereo";
285 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700286 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700287 }
288 ss << reverse_output_rate / 1000;
289 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000290
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000291 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700292 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000293 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
294 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000295}
296
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000297void ClearTempFiles() {
298 for (auto& kv : temp_filenames)
299 remove(kv.second.c_str());
300}
301
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200302// Only remove "out" files. Keep "ref" files.
303void ClearTempOutFiles() {
304 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
305 const std::string& filename = it->first;
306 if (filename.substr(0, 3).compare("out") == 0) {
307 remove(it->second.c_str());
308 temp_filenames.erase(it++);
309 } else {
310 it++;
311 }
312 }
313}
314
Alex Loiko890988c2017-08-31 10:25:48 +0200315void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000316 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000317 ASSERT_TRUE(file != NULL);
318 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000319 fclose(file);
320}
321
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000322// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
323// stereo) file, converts to deinterleaved float (optionally downmixing) and
324// returns the result in |cb|. Returns false if the file ended (or on error) and
325// true otherwise.
326//
327// |int_data| and |float_data| are just temporary space that must be
328// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200329bool ReadChunk(FILE* file,
330 int16_t* int_data,
331 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000332 ChannelBuffer<float>* cb) {
333 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000334 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000335 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
336 if (read_count != frame_size) {
337 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700338 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000339 return false; // This is expected.
340 }
341
342 S16ToFloat(int_data, frame_size, float_data);
343 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000344 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000345 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200346 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000347 }
348
349 return true;
350}
351
niklase@google.com470e71d2011-07-07 08:21:25 +0000352class ApmTest : public ::testing::Test {
353 protected:
354 ApmTest();
355 virtual void SetUp();
356 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000357
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200358 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000359
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200360 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000361
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000362 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200363 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000364
365 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000366 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000367 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800368 size_t num_input_channels,
369 size_t num_output_channels,
370 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000371 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000372 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000373 void EnableAllComponents();
374 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000375 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000376 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200377 void ReadFrameWithRewind(FILE* file,
378 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000379 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000380 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200381 void ProcessDelayVerificationTest(int delay_ms,
382 int system_delay_ms,
383 int delay_min,
384 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800386 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700387 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800388 void TestChangingForwardChannels(size_t num_in_channels,
389 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800391 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700392 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000393 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
394 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 int ProcessStreamChooser(Format format);
397 int AnalyzeReverseStreamChooser(Format format);
398 void ProcessDebugDump(const std::string& in_filename,
399 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800400 Format format,
401 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000402 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403
404 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000405 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800406 std::unique_ptr<AudioProcessing> apm_;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200407 AudioFrame frame_;
408 AudioFrame revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800409 std::unique_ptr<ChannelBuffer<float> > float_cb_;
410 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800412 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 FILE* far_file_;
414 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000416};
417
418ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000419 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000420#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200421 ref_filename_(
422 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000423#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200424 ref_filename_(
425 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000426#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000427 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000428 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000429 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000430 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000431 out_file_(NULL) {
432 Config config;
433 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100434 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000435}
niklase@google.com470e71d2011-07-07 08:21:25 +0000436
437void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000438 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
443void ApmTest::TearDown() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 if (far_file_) {
445 ASSERT_EQ(0, fclose(far_file_));
446 }
447 far_file_ = NULL;
448
449 if (near_file_) {
450 ASSERT_EQ(0, fclose(near_file_));
451 }
452 near_file_ = NULL;
453
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000454 if (out_file_) {
455 ASSERT_EQ(0, fclose(out_file_));
456 }
457 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000460void ApmTest::Init(AudioProcessing* ap) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200461 ASSERT_EQ(
462 kNoErr,
463 ap->Initialize({{{frame_.sample_rate_hz_, frame_.num_channels_},
464 {output_sample_rate_hz_, num_output_channels_},
465 {revframe_.sample_rate_hz_, revframe_.num_channels_},
466 {revframe_.sample_rate_hz_, revframe_.num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000467}
468
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000469void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000471 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800472 size_t num_input_channels,
473 size_t num_output_channels,
474 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000475 bool open_output_file) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200476 SetContainerFormat(sample_rate_hz, num_input_channels, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000478 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000479
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200480 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, &revframe_,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000482 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000483
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000484 if (far_file_) {
485 ASSERT_EQ(0, fclose(far_file_));
486 }
487 std::string filename = ResourceFilePath("far", sample_rate_hz);
488 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200489 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490
491 if (near_file_) {
492 ASSERT_EQ(0, fclose(near_file_));
493 }
494 filename = ResourceFilePath("near", sample_rate_hz);
495 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200496 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000497
498 if (open_output_file) {
499 if (out_file_) {
500 ASSERT_EQ(0, fclose(out_file_));
501 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700502 filename = OutputFilePath(
503 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
504 reverse_sample_rate_hz, num_input_channels, num_output_channels,
505 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000506 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200507 ASSERT_TRUE(out_file_ != NULL)
508 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000509 }
510}
511
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000512void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000513 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000514}
515
Jonas Olssona4d87372019-07-05 19:08:33 +0200516bool ApmTest::ReadFrame(FILE* file,
517 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000519 // The files always contain stereo audio.
520 size_t frame_size = frame->samples_per_channel_ * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200521 size_t read_count =
522 fread(frame->mutable_data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000523 if (read_count != frame_size) {
524 // Check that the file really ended.
525 EXPECT_NE(0, feof(file));
526 return false; // This is expected.
527 }
528
529 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700530 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531 frame->samples_per_channel_);
532 }
533
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000535 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000536 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000537 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000538}
539
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
541 return ReadFrame(file, frame, NULL);
542}
543
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000544// If the end of the file has been reached, rewind it and attempt to read the
545// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200546void ApmTest::ReadFrameWithRewind(FILE* file,
547 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 ChannelBuffer<float>* cb) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200549 if (!ReadFrame(near_file_, &frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000550 rewind(near_file_);
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200551 ASSERT_TRUE(ReadFrame(near_file_, &frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000552 }
553}
554
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
556 ReadFrameWithRewind(file, frame, NULL);
557}
558
andrew@webrtc.org81865342012-10-27 00:28:27 +0000559void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
560 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200561 apm_->set_stream_analog_level(127);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000562 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000563}
564
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000565int ApmTest::ProcessStreamChooser(Format format) {
566 if (format == kIntFormat) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200567 return apm_->ProcessStream(&frame_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000568 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200569 return apm_->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100570 float_cb_->channels(),
571 StreamConfig(frame_.sample_rate_hz_, frame_.num_channels_),
572 StreamConfig(output_sample_rate_hz_, num_output_channels_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200573 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000574}
575
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000576int ApmTest::AnalyzeReverseStreamChooser(Format format) {
577 if (format == kIntFormat) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200578 return apm_->ProcessReverseStream(&revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000579 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580 return apm_->AnalyzeReverseStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100581 revfloat_cb_->channels(),
582 StreamConfig(revframe_.sample_rate_hz_, revframe_.num_channels_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000583}
584
Jonas Olssona4d87372019-07-05 19:08:33 +0200585void ApmTest::ProcessDelayVerificationTest(int delay_ms,
586 int system_delay_ms,
587 int delay_min,
588 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000589 // The |revframe_| and |frame_| should include the proper frame information,
590 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000591 AudioFrame tmp_frame;
592 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000593 bool causal = true;
594
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200595 tmp_frame.CopyFrom(revframe_);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000596 SetFrameTo(&tmp_frame, 0);
597
598 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
599 // Initialize the |frame_queue| with empty frames.
600 int frame_delay = delay_ms / 10;
601 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000602 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000603 frame->CopyFrom(tmp_frame);
604 frame_queue.push(frame);
605 frame_delay++;
606 causal = false;
607 }
608 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000609 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000610 frame->CopyFrom(tmp_frame);
611 frame_queue.push(frame);
612 frame_delay--;
613 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000614 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
615 // need enough frames with audio to have reliable estimates, but as few as
616 // possible to keep processing time down. 4.5 seconds seemed to be a good
617 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000618 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000619 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 frame->CopyFrom(tmp_frame);
621 // Use the near end recording, since that has more speech in it.
622 ASSERT_TRUE(ReadFrame(near_file_, frame));
623 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000624 AudioFrame* reverse_frame = frame;
625 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000626 if (!causal) {
627 reverse_frame = frame_queue.front();
628 // When we call ProcessStream() the frame is modified, so we can't use the
629 // pointer directly when things are non-causal. Use an intermediate frame
630 // and copy the data.
631 process_frame = &tmp_frame;
632 process_frame->CopyFrom(*frame);
633 }
aluebsb0319552016-03-17 20:39:53 -0700634 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000635 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
636 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
637 frame = frame_queue.front();
638 frame_queue.pop();
639 delete frame;
640
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000641 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000642 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200643 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000644 }
645 }
646
647 rewind(near_file_);
648 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000649 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000650 frame_queue.pop();
651 delete frame;
652 }
653 // Calculate expected delay estimate and acceptable regions. Further,
654 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700655 const size_t samples_per_ms =
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200656 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700657 const int expected_median =
658 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
659 const int expected_median_high = rtc::SafeClamp<int>(
660 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700661 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700662 const int expected_median_low = rtc::SafeClamp<int>(
663 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700664 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000665 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200666 AudioProcessingStats stats =
667 apm_->GetStatistics(true /* has_remote_tracks */);
668 ASSERT_TRUE(stats.delay_median_ms.has_value());
669 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 EXPECT_GE(expected_median_high, median);
671 EXPECT_LE(expected_median_low, median);
672}
673
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000674void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000675 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000676 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000677
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000678 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200679 AudioProcessing::Config apm_config = apm_->GetConfig();
680 apm_config.gain_controller1.enabled = true;
681 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200682 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000683
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000684 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200685 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000686 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200687 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000688
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000689 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200690 apm_config.echo_canceller.enabled = true;
691 apm_config.echo_canceller.mobile_mode = false;
692 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000693 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200694 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200695 apm_config.gain_controller1.enabled = false;
696 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000697
698 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100700 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000701
702 // Resets after successful ProcessStream().
703 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000704 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100705 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000706
707 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200708 apm_config.gain_controller1.enabled = true;
709 apm_->ApplyConfig(apm_config);
710 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100711 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200712 apm_config.gain_controller1.enabled = false;
713 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000714
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000715 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200716 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000718
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200721 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000723}
724
725TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000726 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000727}
728
729TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000731}
732
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000733TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
734 EXPECT_EQ(0, apm_->delay_offset_ms());
735 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
736 EXPECT_EQ(50, apm_->stream_delay_ms());
737}
738
739TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
740 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000741 apm_->set_delay_offset_ms(100);
742 EXPECT_EQ(100, apm_->delay_offset_ms());
743 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000744 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000745 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
746 EXPECT_EQ(200, apm_->stream_delay_ms());
747
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000748 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000749 apm_->set_delay_offset_ms(-50);
750 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000751 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
752 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000753 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
754 EXPECT_EQ(50, apm_->stream_delay_ms());
755}
756
Michael Graczyk86c6d332015-07-23 11:41:39 -0700757void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800758 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700759 AudioProcessing::Error expected_return) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200760 frame_.num_channels_ = num_channels;
761 EXPECT_EQ(expected_return, apm_->ProcessStream(&frame_));
762 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(&frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000763}
764
Michael Graczyk86c6d332015-07-23 11:41:39 -0700765void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800766 size_t num_in_channels,
767 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700768 AudioProcessing::Error expected_return) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200769 const StreamConfig input_stream = {frame_.sample_rate_hz_, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
771
772 EXPECT_EQ(expected_return,
773 apm_->ProcessStream(float_cb_->channels(), input_stream,
774 output_stream, float_cb_->channels()));
775}
776
777void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800778 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700779 AudioProcessing::Error expected_return) {
780 const ProcessingConfig processing_config = {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200781 {{frame_.sample_rate_hz_, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700782 {output_sample_rate_hz_, apm_->num_output_channels()},
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200783 {frame_.sample_rate_hz_, num_rev_channels},
784 {frame_.sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785
ekmeyerson60d9b332015-08-14 10:35:55 -0700786 EXPECT_EQ(
787 expected_return,
788 apm_->ProcessReverseStream(
789 float_cb_->channels(), processing_config.reverse_input_stream(),
790 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700791}
792
793TEST_F(ApmTest, ChannelsInt16Interface) {
794 // Testing number of invalid and valid channels.
795 Init(16000, 16000, 16000, 4, 4, 4, false);
796
797 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
798
Peter Kasting69558702016-01-12 16:26:35 -0800799 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700800 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
803}
804
Michael Graczyk86c6d332015-07-23 11:41:39 -0700805TEST_F(ApmTest, Channels) {
806 // Testing number of invalid and valid channels.
807 Init(16000, 16000, 16000, 4, 4, 4, false);
808
809 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
810 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
811
Peter Kasting69558702016-01-12 16:26:35 -0800812 for (size_t i = 1; i < 4; ++i) {
813 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 // Output channels much be one or match input channels.
815 if (j == 1 || i == j) {
816 TestChangingForwardChannels(i, j, kNoErr);
817 TestChangingReverseChannels(i, kNoErr);
818
819 EXPECT_EQ(i, apm_->num_input_channels());
820 EXPECT_EQ(j, apm_->num_output_channels());
821 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800822 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823 } else {
824 TestChangingForwardChannels(i, j,
825 AudioProcessing::kBadNumberChannelsError);
826 }
827 }
828 }
829}
830
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000831TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 // Testing invalid sample rates
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200833 SetContainerFormat(10000, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000834 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700836 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800837 for (size_t i = 0; i < arraysize(fs); i++) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200838 SetContainerFormat(fs[i], 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000839 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000840 }
841}
842
Sam Zackrissone277bde2019-10-25 10:07:54 +0200843// This test repeatedly reconfigures the pre-amplifier in APM, processes a
844// number of frames, and checks that output signal has the right level.
845TEST_F(ApmTest, PreAmplifier) {
846 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200847 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
848 const size_t samples_per_channel = frame_.samples_per_channel();
Sam Zackrissone277bde2019-10-25 10:07:54 +0200849 for (size_t i = 0; i < samples_per_channel; i++) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200850 for (size_t ch = 0; ch < frame_.num_channels(); ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200851 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
852 }
853 }
854 // Cache the frame in tmp_frame.
855 AudioFrame tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200856 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200857
858 auto compute_power = [](const AudioFrame& frame) {
859 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
860 return std::accumulate(data.begin(), data.end(), 0.0f,
861 [](float a, float b) { return a + b * b; }) /
862 data.size() / 32768 / 32768;
863 };
864
865 const float input_power = compute_power(tmp_frame);
866 // Double-check that the input data is large compared to the error kEpsilon.
867 constexpr float kEpsilon = 1e-4f;
868 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
869
870 // 1. Enable pre-amp with 0 dB gain.
871 AudioProcessing::Config config = apm_->GetConfig();
872 config.pre_amplifier.enabled = true;
873 config.pre_amplifier.fixed_gain_factor = 1.0f;
874 apm_->ApplyConfig(config);
875
876 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200877 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200878 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
879 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200880 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200881 EXPECT_NEAR(output_power, input_power, kEpsilon);
882 config = apm_->GetConfig();
883 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
884
885 // 2. Change pre-amp gain via ApplyConfig.
886 config.pre_amplifier.fixed_gain_factor = 2.0f;
887 apm_->ApplyConfig(config);
888
889 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200890 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200891 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
892 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200893 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200894 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
895 config = apm_->GetConfig();
896 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
897
898 // 3. Change pre-amp gain via a RuntimeSetting.
899 apm_->SetRuntimeSetting(
900 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
901
902 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200903 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200904 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
905 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200906 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200907 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
908 config = apm_->GetConfig();
909 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
910}
911
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000912TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200913 AudioProcessing::Config config = apm_->GetConfig();
914 config.gain_controller1.enabled = false;
915 apm_->ApplyConfig(config);
916 config.gain_controller1.enabled = true;
917 apm_->ApplyConfig(config);
918
niklase@google.com470e71d2011-07-07 08:21:25 +0000919 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200920 for (auto mode :
921 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
922 AudioProcessing::Config::GainController1::kFixedDigital,
923 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
924 config.gain_controller1.mode = mode;
925 apm_->ApplyConfig(config);
926 apm_->set_stream_analog_level(100);
927 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000928 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000929
Sam Zackrisson41478c72019-10-15 10:10:26 +0200930 // Testing target levels
931 for (int target_level_dbfs : {0, 15, 31}) {
932 config.gain_controller1.target_level_dbfs = target_level_dbfs;
933 apm_->ApplyConfig(config);
934 apm_->set_stream_analog_level(100);
935 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000936 }
937
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100938 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200939 for (int compression_gain_db : {0, 10, 90}) {
940 config.gain_controller1.compression_gain_db = compression_gain_db;
941 apm_->ApplyConfig(config);
942 apm_->set_stream_analog_level(100);
943 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 }
945
946 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200947 for (bool enable : {false, true}) {
948 config.gain_controller1.enable_limiter = enable;
949 apm_->ApplyConfig(config);
950 apm_->set_stream_analog_level(100);
951 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
952 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000953
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100954 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200955 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
956 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
957 for (size_t i = 0; i < kMinLevels.size(); ++i) {
958 int min_level = kMinLevels[i];
959 int max_level = kMaxLevels[i];
960 config.gain_controller1.analog_level_minimum = min_level;
961 config.gain_controller1.analog_level_maximum = max_level;
962 apm_->ApplyConfig(config);
963 apm_->set_stream_analog_level((min_level + max_level) / 2);
964 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000965 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000966}
967
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100968#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
969TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200970 auto config = apm_->GetConfig();
971 config.gain_controller1.target_level_dbfs = -1;
972 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100973}
974
975TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200976 auto config = apm_->GetConfig();
977 config.gain_controller1.target_level_dbfs = 32;
978 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100979}
980
981TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200982 auto config = apm_->GetConfig();
983 config.gain_controller1.compression_gain_db = -1;
984 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100985}
986
987TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200988 auto config = apm_->GetConfig();
989 config.gain_controller1.compression_gain_db = 91;
990 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100991}
992
993TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200994 auto config = apm_->GetConfig();
995 config.gain_controller1.analog_level_minimum = -1;
996 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100997}
998
999TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001000 auto config = apm_->GetConfig();
1001 config.gain_controller1.analog_level_maximum = 65536;
1002 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001003}
1004
1005TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001006 auto config = apm_->GetConfig();
1007 config.gain_controller1.analog_level_minimum = 512;
1008 config.gain_controller1.analog_level_maximum = 255;
1009 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001010}
1011
1012TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001013 auto config = apm_->GetConfig();
1014 config.gain_controller1.analog_level_minimum = 255;
1015 config.gain_controller1.analog_level_maximum = 512;
1016 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001017 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1018}
1019
1020TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001021 auto config = apm_->GetConfig();
1022 config.gain_controller1.analog_level_minimum = 255;
1023 config.gain_controller1.analog_level_maximum = 512;
1024 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001025 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1026}
1027#endif
1028
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001029void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001030 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001031 auto config = apm_->GetConfig();
1032 config.gain_controller1.enabled = true;
1033 config.gain_controller1.mode =
1034 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1035 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001036
1037 int out_analog_level = 0;
1038 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001039 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001040 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001041 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001042
1043 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001044 apm_->set_stream_analog_level(100);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001045 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001046 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001047 }
1048
1049 // Ensure the AGC is still able to reach the maximum.
1050 EXPECT_EQ(255, out_analog_level);
1051}
1052
1053// Verifies that despite volume slider quantization, the AGC can continue to
1054// increase its volume.
1055TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001056 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001057 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1058 }
1059}
1060
1061void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001062 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001063 auto config = apm_->GetConfig();
1064 config.gain_controller1.enabled = true;
1065 config.gain_controller1.mode =
1066 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1067 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001068
1069 int out_analog_level = 100;
1070 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001071 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001072 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001073 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001074
Sam Zackrisson41478c72019-10-15 10:10:26 +02001075 apm_->set_stream_analog_level(out_analog_level);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001076 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001077 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001078 }
1079
1080 // Ensure the volume was raised.
1081 EXPECT_GT(out_analog_level, 100);
1082 int highest_level_reached = out_analog_level;
1083 // Simulate a user manual volume change.
1084 out_analog_level = 100;
1085
1086 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001087 ReadFrameWithRewind(near_file_, &frame_);
1088 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001089
Sam Zackrisson41478c72019-10-15 10:10:26 +02001090 apm_->set_stream_analog_level(out_analog_level);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001091 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001092 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001093 // Check that AGC respected the manually adjusted volume.
1094 EXPECT_LT(out_analog_level, highest_level_reached);
1095 }
1096 // Check that the volume was still raised.
1097 EXPECT_GT(out_analog_level, 100);
1098}
1099
1100TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001101 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001102 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1103 }
1104}
1105
niklase@google.com470e71d2011-07-07 08:21:25 +00001106TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001107 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001108 AudioProcessing::Config apm_config;
1109 apm_config.high_pass_filter.enabled = true;
1110 apm_->ApplyConfig(apm_config);
1111 apm_config.high_pass_filter.enabled = false;
1112 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001113}
1114
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001115TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001116 AudioProcessing::Config config = apm_->GetConfig();
1117 EXPECT_FALSE(config.echo_canceller.enabled);
1118 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001119 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001120 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001121 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001122 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001123}
1124
1125TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001126 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001127 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001128 SetFrameTo(&frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001129 AudioFrame frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001130 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001131 for (int j = 0; j < 1000; j++) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001132 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1133 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
1134 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&frame_));
1135 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001136 }
1137 }
1138}
1139
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001140TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1141 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001142 const size_t kSamples = 160;
1143 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001144 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001145 float dest[kSamples] = {};
1146
1147 auto src_channels = &src[0];
1148 auto dest_channels = &dest[0];
1149
Ivo Creusen62337e52018-01-09 14:17:33 +01001150 apm_.reset(AudioProcessingBuilder().Create());
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001151 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1152 StreamConfig(sample_rate, 1),
1153 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001154
1155 for (size_t i = 0; i < kSamples; ++i) {
1156 EXPECT_EQ(src[i], dest[i]);
1157 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001158
1159 // Same for ProcessReverseStream.
1160 float rev_dest[kSamples] = {};
1161 auto rev_dest_channels = &rev_dest[0];
1162
1163 StreamConfig input_stream = {sample_rate, 1};
1164 StreamConfig output_stream = {sample_rate, 1};
1165 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1166 output_stream, &rev_dest_channels));
1167
1168 for (size_t i = 0; i < kSamples; ++i) {
1169 EXPECT_EQ(src[i], rev_dest[i]);
1170 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001171}
1172
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001173TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1174 EnableAllComponents();
1175
pkasting25702cb2016-01-08 13:50:27 -08001176 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001177 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1178 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001179 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001180 ASSERT_EQ(0, feof(far_file_));
1181 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001182 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
1183 CopyLeftToRightChannel(revframe_.mutable_data(),
1184 revframe_.samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001185
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001186 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(&revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001187
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001188 CopyLeftToRightChannel(frame_.mutable_data(),
1189 frame_.samples_per_channel_);
1190 frame_.vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001191
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001192 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001193 apm_->set_stream_analog_level(analog_level);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001194 ASSERT_EQ(kNoErr, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001195 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001196
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001197 VerifyChannelsAreEqual(frame_.data(), frame_.samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001198 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001199 rewind(far_file_);
1200 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001201 }
1202}
1203
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001204TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001205 // Verify the filter is not active through undistorted audio when:
1206 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001207 SetFrameTo(&frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001208 AudioFrame frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001209 frame_copy.CopyFrom(frame_);
1210 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1211 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1212 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001213
1214 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001215 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001216 SetFrameTo(&frame_, 1000);
1217 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001218 apm_config.level_estimation.enabled = true;
1219 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001220 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1221 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1222 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001223 apm_config.level_estimation.enabled = false;
1224 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001225
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001226 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001227 SetFrameTo(&frame_, 1000);
1228 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001229 apm_config.voice_detection.enabled = true;
1230 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001231 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1232 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1233 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001234 apm_config.voice_detection.enabled = false;
1235 apm_->ApplyConfig(apm_config);
1236
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001237 // 4. Both the VAD and the level estimator are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001238 SetFrameTo(&frame_, 1000);
1239 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001240 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001241 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001242 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001243 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1244 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1245 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001246 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001247 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001248 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001249
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001250 // Check the test is valid. We should have distortion from the filter
1251 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001252 apm_config.echo_canceller.enabled = true;
1253 apm_config.echo_canceller.mobile_mode = false;
1254 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001255 frame_.samples_per_channel_ = 320;
1256 frame_.num_channels_ = 2;
1257 frame_.sample_rate_hz_ = 32000;
1258 SetFrameTo(&frame_, 1000);
1259 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001260 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001261 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1262 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001263}
1264
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001265#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1266void ApmTest::ProcessDebugDump(const std::string& in_filename,
1267 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001268 Format format,
1269 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001270 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001271 FILE* in_file = fopen(in_filename.c_str(), "rb");
1272 ASSERT_TRUE(in_file != NULL);
1273 audioproc::Event event_msg;
1274 bool first_init = true;
1275
1276 while (ReadMessageFromFile(in_file, &event_msg)) {
1277 if (event_msg.type() == audioproc::Event::INIT) {
1278 const audioproc::Init msg = event_msg.init();
1279 int reverse_sample_rate = msg.sample_rate();
1280 if (msg.has_reverse_sample_rate()) {
1281 reverse_sample_rate = msg.reverse_sample_rate();
1282 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001283 int output_sample_rate = msg.sample_rate();
1284 if (msg.has_output_sample_rate()) {
1285 output_sample_rate = msg.output_sample_rate();
1286 }
1287
Jonas Olssona4d87372019-07-05 19:08:33 +02001288 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1289 msg.num_input_channels(), msg.num_output_channels(),
1290 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001291 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001292 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001293 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001294 auto aec_dump =
1295 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1296 EXPECT_TRUE(aec_dump);
1297 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001298 first_init = false;
1299 }
1300
1301 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1302 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1303
1304 if (msg.channel_size() > 0) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001305 ASSERT_EQ(revframe_.num_channels_,
Peter Kasting69558702016-01-12 16:26:35 -08001306 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001307 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001308 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1309 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001310 }
1311 } else {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001312 memcpy(revframe_.mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001313 if (format == kFloatFormat) {
1314 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001315 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001316 }
1317 }
1318 AnalyzeReverseStreamChooser(format);
1319
1320 } else if (event_msg.type() == audioproc::Event::STREAM) {
1321 const audioproc::Stream msg = event_msg.stream();
1322 // ProcessStream could have changed this for the output frame.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001323 frame_.num_channels_ = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001324
Sam Zackrisson41478c72019-10-15 10:10:26 +02001325 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001326 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001327 if (msg.has_keypress()) {
1328 apm_->set_stream_key_pressed(msg.keypress());
1329 } else {
1330 apm_->set_stream_key_pressed(true);
1331 }
1332
1333 if (msg.input_channel_size() > 0) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001334 ASSERT_EQ(frame_.num_channels_,
Peter Kasting69558702016-01-12 16:26:35 -08001335 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001336 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001337 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1338 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001339 }
1340 } else {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001341 memcpy(frame_.mutable_data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001342 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001343 if (format == kFloatFormat) {
1344 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001345 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001346 }
1347 }
1348 ProcessStreamChooser(format);
1349 }
1350 }
aleloif4dd1912017-06-15 01:55:38 -07001351 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001352 fclose(in_file);
1353}
1354
1355void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001356 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001357 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001358 std::string format_string;
1359 switch (format) {
1360 case kIntFormat:
1361 format_string = "_int";
1362 break;
1363 case kFloatFormat:
1364 format_string = "_float";
1365 break;
1366 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001367 const std::string ref_filename = test::TempFilename(
1368 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1369 const std::string out_filename = test::TempFilename(
1370 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001371 const std::string limited_filename = test::TempFilename(
1372 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1373 const size_t logging_limit_bytes = 100000;
1374 // We expect at least this many bytes in the created logfile.
1375 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001376 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001377 ProcessDebugDump(in_filename, ref_filename, format, -1);
1378 ProcessDebugDump(ref_filename, out_filename, format, -1);
1379 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001380
1381 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1382 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001383 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001384 ASSERT_TRUE(ref_file != NULL);
1385 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001386 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001387 std::unique_ptr<uint8_t[]> ref_bytes;
1388 std::unique_ptr<uint8_t[]> out_bytes;
1389 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001390
1391 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1392 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001393 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001394 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001395 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001396 while (ref_size > 0 && out_size > 0) {
1397 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001398 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001399 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001400 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001401 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001402 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001403 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1404 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001405 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001406 }
1407 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001408 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1409 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410 EXPECT_NE(0, feof(ref_file));
1411 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001412 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001413 ASSERT_EQ(0, fclose(ref_file));
1414 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001415 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001416 remove(ref_filename.c_str());
1417 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001418 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001419}
1420
pbosc7a65692016-05-06 12:50:04 -07001421TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001422 VerifyDebugDumpTest(kIntFormat);
1423}
1424
pbosc7a65692016-05-06 12:50:04 -07001425TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001426 VerifyDebugDumpTest(kFloatFormat);
1427}
1428#endif
1429
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001430// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001431TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001432 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001433 const std::string filename =
1434 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001435 {
1436 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1437 EXPECT_FALSE(aec_dump);
1438 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001439
1440#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1441 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001442 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001443
aleloif4dd1912017-06-15 01:55:38 -07001444 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1445 EXPECT_TRUE(aec_dump);
1446 apm_->AttachAecDump(std::move(aec_dump));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001449 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001450
1451 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001452 FILE* fid = fopen(filename.c_str(), "r");
1453 ASSERT_TRUE(fid != NULL);
1454
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001455 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001456 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001457 ASSERT_EQ(0, remove(filename.c_str()));
1458#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001459 // Verify the file has NOT been written.
1460 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1461#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1462}
1463
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001464// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001465TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001466 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001467
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001468 const std::string filename =
1469 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001470 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1471 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001472
1473#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1474 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001475 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001476
Niels Möllere8e4dc42019-06-11 14:04:16 +02001477 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001478 EXPECT_TRUE(aec_dump);
1479 apm_->AttachAecDump(std::move(aec_dump));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001480 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&revframe_));
1481 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
aleloif4dd1912017-06-15 01:55:38 -07001482 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001483
1484 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001485 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001486 ASSERT_TRUE(fid != NULL);
1487
1488 // Clean it up.
1489 ASSERT_EQ(0, fclose(fid));
1490 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001491#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1492}
1493
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001494// TODO(andrew): Add a test to process a few frames with different combinations
1495// of enabled components.
1496
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001497TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001498 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001499 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001500
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001501 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001502 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001503 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001504 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001505 for (size_t i = 0; i < arraysize(kChannels); i++) {
1506 for (size_t j = 0; j < arraysize(kChannels); j++) {
1507 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001508 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001509 test->set_num_reverse_channels(kChannels[i]);
1510 test->set_num_input_channels(kChannels[j]);
1511 test->set_num_output_channels(kChannels[j]);
1512 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001513 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001514 }
1515 }
1516 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001517#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1518 // To test the extended filter mode.
1519 audioproc::Test* test = ref_data.add_test();
1520 test->set_num_reverse_channels(2);
1521 test->set_num_input_channels(2);
1522 test->set_num_output_channels(2);
1523 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1524 test->set_use_aec_extended_filter(true);
1525#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001526 }
1527
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001528 for (int i = 0; i < ref_data.test_size(); i++) {
1529 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001530
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001531 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001532 // TODO(ajm): We no longer allow different input and output channels. Skip
1533 // these tests for now, but they should be removed from the set.
1534 if (test->num_input_channels() != test->num_output_channels())
1535 continue;
1536
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001537 Config config;
1538 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001539 config.Set<ExtendedFilter>(
1540 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001541 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001542
1543 EnableAllComponents();
1544
Jonas Olssona4d87372019-07-05 19:08:33 +02001545 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001546 static_cast<size_t>(test->num_input_channels()),
1547 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001548 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001549
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001550 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001551 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001552 int analog_level = 127;
1553 int analog_level_average = 0;
1554 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001555 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001556#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001557 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001558#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001559
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001560 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
1561 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001562
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001563 frame_.vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001564
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001565 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001566 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001567
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001568 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001569
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001570 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001571 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001572 frame_.num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001573
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001574 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001575
Sam Zackrisson41478c72019-10-15 10:10:26 +02001576 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001577 analog_level_average += analog_level;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001578 AudioProcessingStats stats =
1579 apm_->GetStatistics(/*has_remote_tracks=*/false);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001580 EXPECT_TRUE(stats.voice_detected);
1581 EXPECT_TRUE(stats.output_rms_dbfs);
1582 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001583 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001584
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001585 size_t frame_size = frame_.samples_per_channel_ * frame_.num_channels_;
Jonas Olssona4d87372019-07-05 19:08:33 +02001586 size_t write_count =
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001587 fwrite(frame_.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001588 ASSERT_EQ(frame_size, write_count);
1589
1590 // Reset in case of downmixing.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001591 frame_.num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001592 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001593
1594#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1595 const int kStatsAggregationFrameNum = 100; // 1 second.
1596 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001597 // Get echo and delay metrics.
1598 AudioProcessingStats stats =
1599 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001600
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001601 // Echo metrics.
1602 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1603 const float echo_return_loss_enhancement =
1604 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001605 const float residual_echo_likelihood =
1606 stats.residual_echo_likelihood.value_or(-1.0f);
1607 const float residual_echo_likelihood_recent_max =
1608 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1609
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001610 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001611 const audioproc::Test::EchoMetrics& reference =
1612 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001613 constexpr float kEpsilon = 0.01;
1614 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1615 EXPECT_NEAR(echo_return_loss_enhancement,
1616 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001617 EXPECT_NEAR(residual_echo_likelihood,
1618 reference.residual_echo_likelihood(), kEpsilon);
1619 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1620 reference.residual_echo_likelihood_recent_max(),
1621 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001622 ++stats_index;
1623 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001624 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1625 message_echo->set_echo_return_loss(echo_return_loss);
1626 message_echo->set_echo_return_loss_enhancement(
1627 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001628 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1629 message_echo->set_residual_echo_likelihood_recent_max(
1630 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001631 }
1632 }
1633#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634 }
1635 max_output_average /= frame_count;
1636 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001637 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001638
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001639 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001640 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001641 // When running the test on a N7 we get a {2, 6} difference of
1642 // |has_voice_count| and |max_output_average| is up to 18 higher.
1643 // All numbers being consistently higher on N7 compare to ref_data.
1644 // TODO(bjornv): If we start getting more of these offsets on Android we
1645 // should consider a different approach. Either using one slack for all,
1646 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001647#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001648 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001649 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001650 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001651 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001652#else
1653 const int kHasVoiceCountOffset = 0;
1654 const int kHasVoiceCountNear = kIntNear;
1655 const int kMaxOutputAverageOffset = 0;
1656 const int kMaxOutputAverageNear = kIntNear;
1657#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001658 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001659 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001660
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001661 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001662 EXPECT_NEAR(test->max_output_average(),
1663 max_output_average - kMaxOutputAverageOffset,
1664 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001665#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001666 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001667 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001668#endif
1669 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001670 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001671
1672 test->set_analog_level_average(analog_level_average);
1673 test->set_max_output_average(max_output_average);
1674
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001675#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001676 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001677#endif
1678 }
1679
1680 rewind(far_file_);
1681 rewind(near_file_);
1682 }
1683
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001684 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001685 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686 }
1687}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001688
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001689TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1690 struct ChannelFormat {
1691 AudioProcessing::ChannelLayout in_layout;
1692 AudioProcessing::ChannelLayout out_layout;
1693 };
1694 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001695 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1696 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1697 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001698 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001699
Ivo Creusen62337e52018-01-09 14:17:33 +01001700 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001701 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001702 AudioProcessing::Config config;
1703 config.noise_suppression.enabled = true;
1704 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001705 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001706 const int in_rate = 44100;
1707 const int out_rate = 48000;
1708 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1709 TotalChannelsFromLayout(cf[i].in_layout));
1710 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1711 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001712 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1713 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1714 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1715 has_keyboard);
1716 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001717
1718 // Run over a few chunks.
1719 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001720 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1721 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001722 }
1723 }
1724}
1725
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001726// Compares the reference and test arrays over a region around the expected
1727// delay. Finds the highest SNR in that region and adds the variance and squared
1728// error results to the supplied accumulators.
1729void UpdateBestSNR(const float* ref,
1730 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001731 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001732 int expected_delay,
1733 double* variance_acc,
1734 double* sq_error_acc) {
1735 double best_snr = std::numeric_limits<double>::min();
1736 double best_variance = 0;
1737 double best_sq_error = 0;
1738 // Search over a region of eight samples around the expected delay.
1739 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1740 ++delay) {
1741 double sq_error = 0;
1742 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001743 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001744 double error = test[i + delay] - ref[i];
1745 sq_error += error * error;
1746 variance += ref[i] * ref[i];
1747 }
1748
1749 if (sq_error == 0) {
1750 *variance_acc += variance;
1751 return;
1752 }
1753 double snr = variance / sq_error;
1754 if (snr > best_snr) {
1755 best_snr = snr;
1756 best_variance = variance;
1757 best_sq_error = sq_error;
1758 }
1759 }
1760
1761 *variance_acc += best_variance;
1762 *sq_error_acc += best_sq_error;
1763}
1764
1765// Used to test a multitude of sample rate and channel combinations. It works
1766// by first producing a set of reference files (in SetUpTestCase) that are
1767// assumed to be correct, as the used parameters are verified by other tests
1768// in this collection. Primarily the reference files are all produced at
1769// "native" rates which do not involve any resampling.
1770
1771// Each test pass produces an output file with a particular format. The output
1772// is matched against the reference file closest to its internal processing
1773// format. If necessary the output is resampled back to its process format.
1774// Due to the resampling distortion, we don't expect identical results, but
1775// enforce SNR thresholds which vary depending on the format. 0 is a special
1776// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001777typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001778class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001779 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001780 public:
1781 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001782 : input_rate_(std::get<0>(GetParam())),
1783 output_rate_(std::get<1>(GetParam())),
1784 reverse_input_rate_(std::get<2>(GetParam())),
1785 reverse_output_rate_(std::get<3>(GetParam())),
1786 expected_snr_(std::get<4>(GetParam())),
1787 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001788
1789 virtual ~AudioProcessingTest() {}
1790
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001791 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001792 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001793 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001794 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001795 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1796 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1797 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001798 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001799 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1800 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1801 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001802 }
1803 }
1804 }
1805 }
1806
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001807 void TearDown() {
1808 // Remove "out" files after each test.
1809 ClearTempOutFiles();
1810 }
1811
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001812 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001813
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001814 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001815 // to a file specified with |output_file_prefix|. Both forward and reverse
1816 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001817 static void ProcessFormat(int input_rate,
1818 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001819 int reverse_input_rate,
1820 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001821 size_t num_input_channels,
1822 size_t num_output_channels,
1823 size_t num_reverse_input_channels,
1824 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001825 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001826 Config config;
1827 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001828 std::unique_ptr<AudioProcessing> ap(
1829 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001830 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001831
ekmeyerson60d9b332015-08-14 10:35:55 -07001832 ProcessingConfig processing_config = {
1833 {{input_rate, num_input_channels},
1834 {output_rate, num_output_channels},
1835 {reverse_input_rate, num_reverse_input_channels},
1836 {reverse_output_rate, num_reverse_output_channels}}};
1837 ap->Initialize(processing_config);
1838
1839 FILE* far_file =
1840 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001841 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001842 FILE* out_file = fopen(
1843 OutputFilePath(
1844 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1845 reverse_output_rate, num_input_channels, num_output_channels,
1846 num_reverse_input_channels, num_reverse_output_channels, kForward)
1847 .c_str(),
1848 "wb");
1849 FILE* rev_out_file = fopen(
1850 OutputFilePath(
1851 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1852 reverse_output_rate, num_input_channels, num_output_channels,
1853 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1854 .c_str(),
1855 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001856 ASSERT_TRUE(far_file != NULL);
1857 ASSERT_TRUE(near_file != NULL);
1858 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001859 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001860
1861 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1862 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001863 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1864 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001865 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1866 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001867 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1868 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001869
1870 // Temporary buffers.
1871 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001872 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1873 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001874 std::unique_ptr<float[]> float_data(new float[max_length]);
1875 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001876
1877 int analog_level = 127;
1878 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1879 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001880 EXPECT_NOERR(ap->ProcessReverseStream(
1881 rev_cb.channels(), processing_config.reverse_input_stream(),
1882 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001883
1884 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001885 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001886
1887 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001888 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
1889 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001890
ekmeyerson60d9b332015-08-14 10:35:55 -07001891 // Dump forward output to file.
1892 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001893 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001894 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001895
Jonas Olssona4d87372019-07-05 19:08:33 +02001896 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1897 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001898
ekmeyerson60d9b332015-08-14 10:35:55 -07001899 // Dump reverse output to file.
1900 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
1901 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001902 size_t rev_out_length =
1903 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001904
Jonas Olssona4d87372019-07-05 19:08:33 +02001905 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1906 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07001907
Sam Zackrisson41478c72019-10-15 10:10:26 +02001908 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001909 }
1910 fclose(far_file);
1911 fclose(near_file);
1912 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07001913 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001914 }
1915
1916 protected:
1917 int input_rate_;
1918 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001919 int reverse_input_rate_;
1920 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001921 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001922 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001923};
1924
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00001925TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001926 struct ChannelFormat {
1927 int num_input;
1928 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07001929 int num_reverse_input;
1930 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001931 };
1932 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001933 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
1934 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001935 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001936
pkasting25702cb2016-01-08 13:50:27 -08001937 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001938 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
1939 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
1940 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07001941
ekmeyerson60d9b332015-08-14 10:35:55 -07001942 // Verify output for both directions.
1943 std::vector<StreamDirection> stream_directions;
1944 stream_directions.push_back(kForward);
1945 stream_directions.push_back(kReverse);
1946 for (StreamDirection file_direction : stream_directions) {
1947 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
1948 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
1949 const int out_num =
1950 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
1951 const double expected_snr =
1952 file_direction ? expected_reverse_snr_ : expected_snr_;
1953
1954 const int min_ref_rate = std::min(in_rate, out_rate);
1955 int ref_rate;
1956
1957 if (min_ref_rate > 32000) {
1958 ref_rate = 48000;
1959 } else if (min_ref_rate > 16000) {
1960 ref_rate = 32000;
1961 } else if (min_ref_rate > 8000) {
1962 ref_rate = 16000;
1963 } else {
1964 ref_rate = 8000;
1965 }
aluebs776593b2016-03-15 14:04:58 -07001966#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08001967 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07001968 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08001969 }
1970#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001971 FILE* out_file = fopen(
1972 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
1973 reverse_output_rate_, cf[i].num_input,
1974 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02001975 cf[i].num_reverse_output, file_direction)
1976 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07001977 "rb");
1978 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02001979 FILE* ref_file =
1980 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
1981 cf[i].num_output, cf[i].num_output,
1982 cf[i].num_reverse_output,
1983 cf[i].num_reverse_output, file_direction)
1984 .c_str(),
1985 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07001986 ASSERT_TRUE(out_file != NULL);
1987 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001988
pkasting25702cb2016-01-08 13:50:27 -08001989 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
1990 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07001991 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08001992 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001993 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08001994 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001995 // Data from the resampled output, in case the reference and output rates
1996 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08001997 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001998
ekmeyerson60d9b332015-08-14 10:35:55 -07001999 PushResampler<float> resampler;
2000 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002001
ekmeyerson60d9b332015-08-14 10:35:55 -07002002 // Compute the resampling delay of the output relative to the reference,
2003 // to find the region over which we should search for the best SNR.
2004 float expected_delay_sec = 0;
2005 if (in_rate != ref_rate) {
2006 // Input resampling delay.
2007 expected_delay_sec +=
2008 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2009 }
2010 if (out_rate != ref_rate) {
2011 // Output resampling delay.
2012 expected_delay_sec +=
2013 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2014 // Delay of converting the output back to its processing rate for
2015 // testing.
2016 expected_delay_sec +=
2017 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2018 }
2019 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002020 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002021
ekmeyerson60d9b332015-08-14 10:35:55 -07002022 double variance = 0;
2023 double sq_error = 0;
2024 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2025 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2026 float* out_ptr = out_data.get();
2027 if (out_rate != ref_rate) {
2028 // Resample the output back to its internal processing rate if
2029 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002030 ASSERT_EQ(ref_length,
2031 static_cast<size_t>(resampler.Resample(
2032 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002033 out_ptr = cmp_data.get();
2034 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002035
ekmeyerson60d9b332015-08-14 10:35:55 -07002036 // Update the |sq_error| and |variance| accumulators with the highest
2037 // SNR of reference vs output.
2038 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2039 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002040 }
2041
ekmeyerson60d9b332015-08-14 10:35:55 -07002042 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2043 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2044 << cf[i].num_input << ", " << cf[i].num_output << ", "
2045 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2046 << ", " << file_direction << "): ";
2047 if (sq_error > 0) {
2048 double snr = 10 * log10(variance / sq_error);
2049 EXPECT_GE(snr, expected_snr);
2050 EXPECT_NE(0, expected_snr);
2051 std::cout << "SNR=" << snr << " dB" << std::endl;
2052 } else {
aluebs776593b2016-03-15 14:04:58 -07002053 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002054 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002055
ekmeyerson60d9b332015-08-14 10:35:55 -07002056 fclose(out_file);
2057 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002058 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002059 }
2060}
2061
2062#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002063INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002064 CommonFormats,
2065 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002066 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2067 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2068 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2069 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2070 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2071 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2072 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2073 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2074 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2075 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2076 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2077 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002078
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002079 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2080 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2081 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2082 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2083 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2084 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2085 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2086 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2087 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2088 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2089 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2090 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002091
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002092 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2093 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2094 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2095 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2096 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2097 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2098 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2099 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2100 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2101 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2102 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2103 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002104
Per Åhgrene35b32c2019-11-22 18:22:04 +01002105 std::make_tuple(16000, 48000, 48000, 48000, 23, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002106 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2107 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2108 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2109 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2110 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2111 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2112 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2113 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2114 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2115 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2116 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002117
2118#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002119INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002120 CommonFormats,
2121 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002122 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2123 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2124 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002125 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2126 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2127 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002128 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2129 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2130 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002131 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2132 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2133 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002134
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002135 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2136 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2137 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2138 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2139 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2140 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002141 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2142 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2143 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2144 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2145 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2146 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002147
Per Åhgrene35b32c2019-11-22 18:22:04 +01002148 std::make_tuple(32000, 48000, 48000, 48000, 27, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002149 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002150 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002151 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2152 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2153 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002154 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002155 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002156 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002157 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2158 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2159 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002160
Per Åhgrene35b32c2019-11-22 18:22:04 +01002161 std::make_tuple(16000, 48000, 48000, 48000, 23, 0),
2162 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002163 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2164 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2165 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2166 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002167 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002168 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002169 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002170 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2171 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002172 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002173#endif
2174
Per Åhgren3e8bf282019-08-29 23:38:40 +02002175// Produces a scoped trace debug output.
2176std::string ProduceDebugText(int render_input_sample_rate_hz,
2177 int render_output_sample_rate_hz,
2178 int capture_input_sample_rate_hz,
2179 int capture_output_sample_rate_hz,
2180 size_t render_input_num_channels,
2181 size_t render_output_num_channels,
2182 size_t capture_input_num_channels,
2183 size_t capture_output_num_channels) {
2184 rtc::StringBuilder ss;
2185 ss << "Sample rates:"
2186 << "\n"
2187 << " Render input: " << render_input_sample_rate_hz << " Hz"
2188 << "\n"
2189 << " Render output: " << render_output_sample_rate_hz << " Hz"
2190 << "\n"
2191 << " Capture input: " << capture_input_sample_rate_hz << " Hz"
2192 << "\n"
2193 << " Capture output: " << capture_output_sample_rate_hz << " Hz"
2194 << "\n"
2195 << "Number of channels:"
2196 << "\n"
2197 << " Render input: " << render_input_num_channels << "\n"
2198 << " Render output: " << render_output_num_channels << "\n"
2199 << " Capture input: " << capture_input_num_channels << "\n"
2200 << " Capture output: " << capture_output_num_channels;
2201 return ss.Release();
2202}
2203
2204// Validates that running the audio processing module using various combinations
2205// of sample rates and number of channels works as intended.
2206void RunApmRateAndChannelTest(
2207 rtc::ArrayView<const int> sample_rates_hz,
2208 rtc::ArrayView<const int> render_channel_counts,
2209 rtc::ArrayView<const int> capture_channel_counts) {
2210 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2211 webrtc::AudioProcessing::Config apm_config;
2212 apm_config.echo_canceller.enabled = true;
2213 apm->ApplyConfig(apm_config);
2214
2215 StreamConfig render_input_stream_config;
2216 StreamConfig render_output_stream_config;
2217 StreamConfig capture_input_stream_config;
2218 StreamConfig capture_output_stream_config;
2219
2220 std::vector<float> render_input_frame_channels;
2221 std::vector<float*> render_input_frame;
2222 std::vector<float> render_output_frame_channels;
2223 std::vector<float*> render_output_frame;
2224 std::vector<float> capture_input_frame_channels;
2225 std::vector<float*> capture_input_frame;
2226 std::vector<float> capture_output_frame_channels;
2227 std::vector<float*> capture_output_frame;
2228
2229 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2230 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2231 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2232 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2233 for (size_t render_input_num_channels : render_channel_counts) {
2234 for (size_t capture_input_num_channels : capture_channel_counts) {
2235 size_t render_output_num_channels = render_input_num_channels;
2236 size_t capture_output_num_channels = capture_input_num_channels;
2237 auto populate_audio_frame = [](int sample_rate_hz,
2238 size_t num_channels,
2239 StreamConfig* cfg,
2240 std::vector<float>* channels_data,
2241 std::vector<float*>* frame_data) {
2242 cfg->set_sample_rate_hz(sample_rate_hz);
2243 cfg->set_num_channels(num_channels);
2244 cfg->set_has_keyboard(false);
2245
2246 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2247 channels_data->resize(num_channels * max_frame_size);
2248 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2249 frame_data->resize(num_channels);
2250 for (size_t channel = 0; channel < num_channels; ++channel) {
2251 (*frame_data)[channel] =
2252 &(*channels_data)[channel * max_frame_size];
2253 }
2254 };
2255
2256 populate_audio_frame(
2257 render_input_sample_rate_hz, render_input_num_channels,
2258 &render_input_stream_config, &render_input_frame_channels,
2259 &render_input_frame);
2260 populate_audio_frame(
2261 render_output_sample_rate_hz, render_output_num_channels,
2262 &render_output_stream_config, &render_output_frame_channels,
2263 &render_output_frame);
2264 populate_audio_frame(
2265 capture_input_sample_rate_hz, capture_input_num_channels,
2266 &capture_input_stream_config, &capture_input_frame_channels,
2267 &capture_input_frame);
2268 populate_audio_frame(
2269 capture_output_sample_rate_hz, capture_output_num_channels,
2270 &capture_output_stream_config, &capture_output_frame_channels,
2271 &capture_output_frame);
2272
2273 for (size_t frame = 0; frame < 2; ++frame) {
2274 SCOPED_TRACE(ProduceDebugText(
2275 render_input_sample_rate_hz, render_output_sample_rate_hz,
2276 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2277 render_input_num_channels, render_output_num_channels,
2278 render_input_num_channels, capture_output_num_channels));
2279
2280 int result = apm->ProcessReverseStream(
2281 &render_input_frame[0], render_input_stream_config,
2282 render_output_stream_config, &render_output_frame[0]);
2283 EXPECT_EQ(result, AudioProcessing::kNoError);
2284 result = apm->ProcessStream(
2285 &capture_input_frame[0], capture_input_stream_config,
2286 capture_output_stream_config, &capture_output_frame[0]);
2287 EXPECT_EQ(result, AudioProcessing::kNoError);
2288 }
2289 }
2290 }
2291 }
2292 }
2293 }
2294 }
2295}
2296
niklase@google.com470e71d2011-07-07 08:21:25 +00002297} // namespace
peahc19f3122016-10-07 14:54:10 -07002298
Alessio Bazzicac054e782018-04-16 12:10:09 +02002299TEST(RuntimeSettingTest, TestDefaultCtor) {
2300 auto s = AudioProcessing::RuntimeSetting();
2301 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2302}
2303
2304TEST(RuntimeSettingTest, TestCapturePreGain) {
2305 using Type = AudioProcessing::RuntimeSetting::Type;
2306 {
2307 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2308 EXPECT_EQ(Type::kCapturePreGain, s.type());
2309 float v;
2310 s.GetFloat(&v);
2311 EXPECT_EQ(1.25f, v);
2312 }
2313
2314#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2315 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2316#endif
2317}
2318
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002319TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2320 using Type = AudioProcessing::RuntimeSetting::Type;
2321 {
2322 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2323 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2324 float v;
2325 s.GetFloat(&v);
2326 EXPECT_EQ(1.25f, v);
2327 }
2328
2329#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2330 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2331#endif
2332}
2333
Alessio Bazzicac054e782018-04-16 12:10:09 +02002334TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2335 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2336 auto s = AudioProcessing::RuntimeSetting();
2337 ASSERT_TRUE(q.Insert(&s));
2338 ASSERT_TRUE(q.Remove(&s));
2339 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2340}
2341
Sam Zackrisson0beac582017-09-25 12:04:02 +02002342TEST(ApmConfiguration, EnablePostProcessing) {
2343 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002344 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002345 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002346 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002347 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002348 rtc::scoped_refptr<AudioProcessing> apm =
2349 AudioProcessingBuilder()
2350 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002351 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002352
2353 AudioFrame audio;
2354 audio.num_channels_ = 1;
2355 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2356
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002357 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002358 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002359}
2360
Alex Loiko5825aa62017-12-18 16:02:40 +01002361TEST(ApmConfiguration, EnablePreProcessing) {
2362 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002363 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002364 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002365 auto mock_pre_processor =
2366 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002367 rtc::scoped_refptr<AudioProcessing> apm =
2368 AudioProcessingBuilder()
2369 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002370 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002371
2372 AudioFrame audio;
2373 audio.num_channels_ = 1;
2374 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2375
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002376 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002377 apm->ProcessReverseStream(&audio);
2378}
2379
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002380TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2381 // Verify that apm uses a capture analyzer if one is provided.
2382 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002383 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002384 auto mock_capture_analyzer =
2385 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2386 rtc::scoped_refptr<AudioProcessing> apm =
2387 AudioProcessingBuilder()
2388 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2389 .Create();
2390
2391 AudioFrame audio;
2392 audio.num_channels_ = 1;
2393 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2394
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002395 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002396 apm->ProcessStream(&audio);
2397}
2398
Alex Loiko73ec0192018-05-15 10:52:28 +02002399TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2400 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002401 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002402 auto mock_pre_processor =
2403 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2404 rtc::scoped_refptr<AudioProcessing> apm =
2405 AudioProcessingBuilder()
2406 .SetRenderPreProcessing(std::move(mock_pre_processor))
2407 .Create();
2408 apm->SetRuntimeSetting(
2409 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2410
2411 // RuntimeSettings forwarded during 'Process*Stream' calls.
2412 // Therefore we have to make one such call.
2413 AudioFrame audio;
2414 audio.num_channels_ = 1;
2415 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2416
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002417 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2418 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002419 apm->ProcessReverseStream(&audio);
2420}
2421
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002422class MyEchoControlFactory : public EchoControlFactory {
2423 public:
2424 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2425 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002426 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2427 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002428 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2429 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002430 return std::unique_ptr<EchoControl>(ec);
2431 }
Per Åhgrence202a02019-09-02 17:01:19 +02002432
2433 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002434 int num_render_channels,
2435 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002436 return Create(sample_rate_hz);
2437 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002438};
2439
2440TEST(ApmConfiguration, EchoControlInjection) {
2441 // Verify that apm uses an injected echo controller if one is provided.
2442 webrtc::Config webrtc_config;
2443 std::unique_ptr<EchoControlFactory> echo_control_factory(
2444 new MyEchoControlFactory());
2445
Alex Loiko5825aa62017-12-18 16:02:40 +01002446 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002447 AudioProcessingBuilder()
2448 .SetEchoControlFactory(std::move(echo_control_factory))
2449 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002450
2451 AudioFrame audio;
2452 audio.num_channels_ = 1;
2453 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2454 apm->ProcessStream(&audio);
2455 apm->ProcessReverseStream(&audio);
2456 apm->ProcessStream(&audio);
2457}
Ivo Creusenae026092017-11-20 13:07:16 +01002458
Per Åhgren8607f842019-04-12 22:02:26 +02002459std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002460 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002461 std::unique_ptr<AudioProcessing> apm(
2462 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002463 if (!apm) {
2464 return apm;
2465 }
2466
2467 ProcessingConfig processing_config = {
2468 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2469
2470 if (apm->Initialize(processing_config) != 0) {
2471 return nullptr;
2472 }
2473
2474 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002475 AudioProcessing::Config apm_config;
2476 apm_config.residual_echo_detector.enabled = true;
2477 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002478 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002479 apm_config.gain_controller2.enabled = false;
2480 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002481 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002482 apm_config.noise_suppression.enabled = false;
2483 apm_config.level_estimation.enabled = false;
2484 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002485 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002486 return apm;
2487}
2488
2489#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2490#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2491#else
2492#define MAYBE_ApmStatistics ApmStatistics
2493#endif
2494
Per Åhgren8607f842019-04-12 22:02:26 +02002495TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2496 // Set up APM with AEC3 and process some audio.
2497 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002498 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002499 AudioProcessing::Config apm_config;
2500 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002501 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002502
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 all statistics to be set and have a sensible value.
2524 ASSERT_TRUE(stats.residual_echo_likelihood);
2525 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2526 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2527 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2528 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2529 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2530 ASSERT_TRUE(stats.echo_return_loss);
2531 EXPECT_NE(*stats.echo_return_loss, -100.0);
2532 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2533 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002534
2535 // If there are no receive streams, we expect the stats not to be set. The
2536 // 'false' argument signals to APM that no receive streams are currently
2537 // active. In that situation the statistics would get stuck at their last
2538 // calculated value (AEC and echo detection need at least one stream in each
2539 // direction), so to avoid that, they should not be set by APM.
2540 stats = apm->GetStatistics(false);
2541 EXPECT_FALSE(stats.residual_echo_likelihood);
2542 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2543 EXPECT_FALSE(stats.echo_return_loss);
2544 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002545}
2546
2547TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2548 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002549 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002550 ASSERT_TRUE(apm);
2551
2552 // Set up an audioframe.
2553 AudioFrame frame;
2554 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002555 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002556
2557 // Fill the audio frame with a sawtooth pattern.
2558 int16_t* ptr = frame.mutable_data();
2559 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2560 ptr[i] = 10000 * ((i % 3) - 1);
2561 }
2562
2563 // Do some processing.
2564 for (int i = 0; i < 200; i++) {
2565 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2566 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2567 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2568 }
2569
2570 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002571 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002572 // We expect only the residual echo detector statistics to be set and have a
2573 // sensible value.
2574 EXPECT_TRUE(stats.residual_echo_likelihood);
2575 if (stats.residual_echo_likelihood) {
2576 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2577 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2578 }
2579 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2580 if (stats.residual_echo_likelihood_recent_max) {
2581 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2582 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2583 }
2584 EXPECT_FALSE(stats.echo_return_loss);
2585 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002586
2587 // If there are no receive streams, we expect the stats not to be set.
2588 stats = apm->GetStatistics(false);
2589 EXPECT_FALSE(stats.residual_echo_likelihood);
2590 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2591 EXPECT_FALSE(stats.echo_return_loss);
2592 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002593}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002594
2595TEST(ApmStatistics, ReportOutputRmsDbfs) {
2596 ProcessingConfig processing_config = {
2597 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2598 AudioProcessing::Config config;
2599
2600 // Set up an audioframe.
2601 AudioFrame frame;
2602 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002603 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002604
2605 // Fill the audio frame with a sawtooth pattern.
2606 int16_t* ptr = frame.mutable_data();
2607 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2608 ptr[i] = 10000 * ((i % 3) - 1);
2609 }
2610
2611 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2612 apm->Initialize(processing_config);
2613
2614 // If not enabled, no metric should be reported.
2615 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2616 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2617
2618 // If enabled, metrics should be reported.
2619 config.level_estimation.enabled = true;
2620 apm->ApplyConfig(config);
2621 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2622 auto stats = apm->GetStatistics(false);
2623 EXPECT_TRUE(stats.output_rms_dbfs);
2624 EXPECT_GE(*stats.output_rms_dbfs, 0);
2625
2626 // If re-disabled, the value is again not reported.
2627 config.level_estimation.enabled = false;
2628 apm->ApplyConfig(config);
2629 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2630 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2631}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002632
2633TEST(ApmStatistics, ReportHasVoice) {
2634 ProcessingConfig processing_config = {
2635 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2636 AudioProcessing::Config config;
2637
2638 // Set up an audioframe.
2639 AudioFrame frame;
2640 frame.num_channels_ = 1;
2641 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2642
2643 // Fill the audio frame with a sawtooth pattern.
2644 int16_t* ptr = frame.mutable_data();
2645 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2646 ptr[i] = 10000 * ((i % 3) - 1);
2647 }
2648
2649 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2650 apm->Initialize(processing_config);
2651
2652 // If not enabled, no metric should be reported.
2653 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2654 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2655
2656 // If enabled, metrics should be reported.
2657 config.voice_detection.enabled = true;
2658 apm->ApplyConfig(config);
2659 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2660 auto stats = apm->GetStatistics(false);
2661 EXPECT_TRUE(stats.voice_detected);
2662
2663 // If re-disabled, the value is again not reported.
2664 config.voice_detection.enabled = false;
2665 apm->ApplyConfig(config);
2666 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2667 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2668}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002669
2670TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2671 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2672 std::array<int, 2> render_channel_counts = {1, 7};
2673 std::array<int, 2> capture_channel_counts = {1, 7};
2674 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2675 capture_channel_counts);
2676}
2677
2678TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2679 std::array<int, 1> sample_rates_hz = {48000};
2680 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2681 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2682 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2683 capture_channel_counts);
2684}
2685
2686TEST(ApmConfiguration, HandlingOfRateCombinations) {
2687 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2688 48000, 96000, 192000, 384000};
2689 std::array<int, 1> render_channel_counts = {2};
2690 std::array<int, 1> capture_channel_counts = {2};
2691 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2692 capture_channel_counts);
2693}
2694
Yves Gerey1fce3f82019-12-05 17:45:31 +01002695TEST(ApmConfiguration, SelfAssignment) {
2696 // At some point memory sanitizer was complaining about self-assigment.
2697 // Make sure we don't regress.
2698 AudioProcessing::Config config;
2699 AudioProcessing::Config* config2 = &config;
2700 *config2 = *config2; // Workaround -Wself-assign-overloaded
2701 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2702}
2703
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002704} // namespace webrtc