blob: f83efe66f075b86e22361e5afd47a39c67bbadb7 [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;
Per Åhgrenc0424252019-12-10 13:04:15 +0100212 apm_config.pipeline.maximum_internal_processing_rate = 48000;
peah8271d042016-11-22 07:24:52 -0800213 ap->ApplyConfig(apm_config);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000214}
215
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000216// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000217template <class T>
218T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200219 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000220}
221
222int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800223 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700224 const int16_t* frame_data = frame.data();
225 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800226 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700227 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000228 }
229
230 return max_data;
231}
232
Alex Loiko890988c2017-08-31 10:25:48 +0200233void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700234 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 ASSERT_TRUE(file != NULL);
237
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100238 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800240 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200245 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000246 fclose(file);
247}
248
Alex Loiko890988c2017-08-31 10:25:48 +0200249std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200250 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000251 // Resource files are all stereo.
252 ss << name << sample_rate_hz / 1000 << "_stereo";
253 return test::ResourcePath(ss.str(), "pcm");
254}
255
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000256// Temporary filenames unique to this process. Used to be able to run these
257// tests in parallel as each process needs to be running in isolation they can't
258// have competing filenames.
259std::map<std::string, std::string> temp_filenames;
260
Alex Loiko890988c2017-08-31 10:25:48 +0200261std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000262 int input_rate,
263 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700264 int reverse_input_rate,
265 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800266 size_t num_input_channels,
267 size_t num_output_channels,
268 size_t num_reverse_input_channels,
269 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700270 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200271 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700272 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
273 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000274 if (num_output_channels == 1) {
275 ss << "mono";
276 } else if (num_output_channels == 2) {
277 ss << "stereo";
278 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700279 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000280 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700281 ss << output_rate / 1000;
282 if (num_reverse_output_channels == 1) {
283 ss << "_rmono";
284 } else if (num_reverse_output_channels == 2) {
285 ss << "_rstereo";
286 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700287 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700288 }
289 ss << reverse_output_rate / 1000;
290 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000291
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000292 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700293 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000294 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
295 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000296}
297
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000298void ClearTempFiles() {
299 for (auto& kv : temp_filenames)
300 remove(kv.second.c_str());
301}
302
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200303// Only remove "out" files. Keep "ref" files.
304void ClearTempOutFiles() {
305 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
306 const std::string& filename = it->first;
307 if (filename.substr(0, 3).compare("out") == 0) {
308 remove(it->second.c_str());
309 temp_filenames.erase(it++);
310 } else {
311 it++;
312 }
313 }
314}
315
Alex Loiko890988c2017-08-31 10:25:48 +0200316void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000317 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000318 ASSERT_TRUE(file != NULL);
319 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000320 fclose(file);
321}
322
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000323// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
324// stereo) file, converts to deinterleaved float (optionally downmixing) and
325// returns the result in |cb|. Returns false if the file ended (or on error) and
326// true otherwise.
327//
328// |int_data| and |float_data| are just temporary space that must be
329// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200330bool ReadChunk(FILE* file,
331 int16_t* int_data,
332 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000333 ChannelBuffer<float>* cb) {
334 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000335 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000336 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
337 if (read_count != frame_size) {
338 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700339 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000340 return false; // This is expected.
341 }
342
343 S16ToFloat(int_data, frame_size, float_data);
344 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000345 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000346 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200347 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000348 }
349
350 return true;
351}
352
niklase@google.com470e71d2011-07-07 08:21:25 +0000353class ApmTest : public ::testing::Test {
354 protected:
355 ApmTest();
356 virtual void SetUp();
357 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000358
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200359 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000360
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200361 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000362
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200364 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000365
366 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000367 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000368 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800369 size_t num_input_channels,
370 size_t num_output_channels,
371 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000372 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000373 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000374 void EnableAllComponents();
375 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000376 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000377 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200378 void ReadFrameWithRewind(FILE* file,
379 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000380 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000381 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200382 void ProcessDelayVerificationTest(int delay_ms,
383 int system_delay_ms,
384 int delay_min,
385 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700386 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800387 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700388 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800389 void TestChangingForwardChannels(size_t num_in_channels,
390 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700391 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800392 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700393 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000394 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
395 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000397 int ProcessStreamChooser(Format format);
398 int AnalyzeReverseStreamChooser(Format format);
399 void ProcessDebugDump(const std::string& in_filename,
400 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800401 Format format,
402 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000403 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000404
405 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000406 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800407 std::unique_ptr<AudioProcessing> apm_;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200408 AudioFrame frame_;
409 AudioFrame revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800410 std::unique_ptr<ChannelBuffer<float> > float_cb_;
411 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000412 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800413 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414 FILE* far_file_;
415 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000416 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417};
418
419ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000420 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000421#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200422 ref_filename_(
423 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000424#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200425 ref_filename_(
426 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000427#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000428 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000429 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000430 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000431 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000432 out_file_(NULL) {
Per Åhgren0695df12020-01-13 14:43:13 +0100433 apm_.reset(AudioProcessingBuilder().Create());
Per Åhgrenc0424252019-12-10 13:04:15 +0100434 AudioProcessing::Config apm_config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100435 apm_config.gain_controller1.analog_gain_controller.enabled = false;
Per Åhgrenc0424252019-12-10 13:04:15 +0100436 apm_config.pipeline.maximum_internal_processing_rate = 48000;
437 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000438}
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
440void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000441 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000443 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000444}
445
446void ApmTest::TearDown() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000447 if (far_file_) {
448 ASSERT_EQ(0, fclose(far_file_));
449 }
450 far_file_ = NULL;
451
452 if (near_file_) {
453 ASSERT_EQ(0, fclose(near_file_));
454 }
455 near_file_ = NULL;
456
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000457 if (out_file_) {
458 ASSERT_EQ(0, fclose(out_file_));
459 }
460 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000461}
462
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000463void ApmTest::Init(AudioProcessing* ap) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200464 ASSERT_EQ(
465 kNoErr,
466 ap->Initialize({{{frame_.sample_rate_hz_, frame_.num_channels_},
467 {output_sample_rate_hz_, num_output_channels_},
468 {revframe_.sample_rate_hz_, revframe_.num_channels_},
469 {revframe_.sample_rate_hz_, revframe_.num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000470}
471
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000472void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000474 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800475 size_t num_input_channels,
476 size_t num_output_channels,
477 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000478 bool open_output_file) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200479 SetContainerFormat(sample_rate_hz, num_input_channels, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000482
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200483 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, &revframe_,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000485 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000486
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000487 if (far_file_) {
488 ASSERT_EQ(0, fclose(far_file_));
489 }
490 std::string filename = ResourceFilePath("far", sample_rate_hz);
491 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200492 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000493
494 if (near_file_) {
495 ASSERT_EQ(0, fclose(near_file_));
496 }
497 filename = ResourceFilePath("near", sample_rate_hz);
498 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200499 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000500
501 if (open_output_file) {
502 if (out_file_) {
503 ASSERT_EQ(0, fclose(out_file_));
504 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700505 filename = OutputFilePath(
506 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
507 reverse_sample_rate_hz, num_input_channels, num_output_channels,
508 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000509 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200510 ASSERT_TRUE(out_file_ != NULL)
511 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000512 }
513}
514
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000515void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000516 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000517}
518
Jonas Olssona4d87372019-07-05 19:08:33 +0200519bool ApmTest::ReadFrame(FILE* file,
520 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000521 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000522 // The files always contain stereo audio.
523 size_t frame_size = frame->samples_per_channel_ * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200524 size_t read_count =
525 fread(frame->mutable_data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000526 if (read_count != frame_size) {
527 // Check that the file really ended.
528 EXPECT_NE(0, feof(file));
529 return false; // This is expected.
530 }
531
532 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700533 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000534 frame->samples_per_channel_);
535 }
536
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000537 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000538 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000539 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000540 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000541}
542
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000543bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
544 return ReadFrame(file, frame, NULL);
545}
546
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000547// If the end of the file has been reached, rewind it and attempt to read the
548// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200549void ApmTest::ReadFrameWithRewind(FILE* file,
550 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551 ChannelBuffer<float>* cb) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200552 if (!ReadFrame(near_file_, &frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000553 rewind(near_file_);
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200554 ASSERT_TRUE(ReadFrame(near_file_, &frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000555 }
556}
557
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000558void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
559 ReadFrameWithRewind(file, frame, NULL);
560}
561
andrew@webrtc.org81865342012-10-27 00:28:27 +0000562void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
563 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200564 apm_->set_stream_analog_level(127);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000565 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000566}
567
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000568int ApmTest::ProcessStreamChooser(Format format) {
569 if (format == kIntFormat) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200570 return apm_->ProcessStream(&frame_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200572 return apm_->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100573 float_cb_->channels(),
574 StreamConfig(frame_.sample_rate_hz_, frame_.num_channels_),
575 StreamConfig(output_sample_rate_hz_, num_output_channels_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200576 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577}
578
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000579int ApmTest::AnalyzeReverseStreamChooser(Format format) {
580 if (format == kIntFormat) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200581 return apm_->ProcessReverseStream(&revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000583 return apm_->AnalyzeReverseStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +0100584 revfloat_cb_->channels(),
585 StreamConfig(revframe_.sample_rate_hz_, revframe_.num_channels_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586}
587
Jonas Olssona4d87372019-07-05 19:08:33 +0200588void ApmTest::ProcessDelayVerificationTest(int delay_ms,
589 int system_delay_ms,
590 int delay_min,
591 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000592 // The |revframe_| and |frame_| should include the proper frame information,
593 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000594 AudioFrame tmp_frame;
595 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000596 bool causal = true;
597
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200598 tmp_frame.CopyFrom(revframe_);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000599 SetFrameTo(&tmp_frame, 0);
600
601 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
602 // Initialize the |frame_queue| with empty frames.
603 int frame_delay = delay_ms / 10;
604 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000605 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000606 frame->CopyFrom(tmp_frame);
607 frame_queue.push(frame);
608 frame_delay++;
609 causal = false;
610 }
611 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000612 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000613 frame->CopyFrom(tmp_frame);
614 frame_queue.push(frame);
615 frame_delay--;
616 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000617 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
618 // need enough frames with audio to have reliable estimates, but as few as
619 // possible to keep processing time down. 4.5 seconds seemed to be a good
620 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000621 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000622 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000623 frame->CopyFrom(tmp_frame);
624 // Use the near end recording, since that has more speech in it.
625 ASSERT_TRUE(ReadFrame(near_file_, frame));
626 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000627 AudioFrame* reverse_frame = frame;
628 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000629 if (!causal) {
630 reverse_frame = frame_queue.front();
631 // When we call ProcessStream() the frame is modified, so we can't use the
632 // pointer directly when things are non-causal. Use an intermediate frame
633 // and copy the data.
634 process_frame = &tmp_frame;
635 process_frame->CopyFrom(*frame);
636 }
aluebsb0319552016-03-17 20:39:53 -0700637 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000638 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
640 frame = frame_queue.front();
641 frame_queue.pop();
642 delete frame;
643
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000644 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 // Discard the first delay metrics to avoid convergence effects.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100646 static_cast<void>(apm_->GetStatistics());
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 }
648 }
649
650 rewind(near_file_);
651 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000652 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000653 frame_queue.pop();
654 delete frame;
655 }
656 // Calculate expected delay estimate and acceptable regions. Further,
657 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700658 const size_t samples_per_ms =
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200659 rtc::SafeMin<size_t>(16u, frame_.samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700660 const int expected_median =
661 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
662 const int expected_median_high = 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);
kwiberg07038562017-06-12 11:40:47 -0700665 const int expected_median_low = rtc::SafeClamp<int>(
666 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700667 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000668 // Verify delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +0100669 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200670 ASSERT_TRUE(stats.delay_median_ms.has_value());
671 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000672 EXPECT_GE(expected_median_high, median);
673 EXPECT_LE(expected_median_low, median);
674}
675
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000676void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000677 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000678 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000679
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000680 // -- Missing AGC level --
Sam Zackrisson41478c72019-10-15 10:10:26 +0200681 AudioProcessing::Config apm_config = apm_->GetConfig();
682 apm_config.gain_controller1.enabled = true;
683 apm_->ApplyConfig(apm_config);
Jonas Olssona4d87372019-07-05 19:08:33 +0200684 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000685
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000686 // Resets after successful ProcessStream().
Sam Zackrisson41478c72019-10-15 10:10:26 +0200687 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000688 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Jonas Olssona4d87372019-07-05 19:08:33 +0200689 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000691 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200692 apm_config.echo_canceller.enabled = true;
693 apm_config.echo_canceller.mobile_mode = false;
694 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000695 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200696 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200697 apm_config.gain_controller1.enabled = false;
698 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000699
700 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000701 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100702 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000703
704 // Resets after successful ProcessStream().
705 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000706 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100707 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708
709 // Other stream parameters set correctly.
Sam Zackrisson41478c72019-10-15 10:10:26 +0200710 apm_config.gain_controller1.enabled = true;
711 apm_->ApplyConfig(apm_config);
712 apm_->set_stream_analog_level(127);
Per Åhgren200feba2019-03-06 04:16:46 +0100713 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200714 apm_config.gain_controller1.enabled = false;
715 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200718 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100719 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000720
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000721 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sam Zackrisson41478c72019-10-15 10:10:26 +0200723 apm_->set_stream_analog_level(127);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000725}
726
727TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729}
730
731TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000733}
734
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000735TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
736 EXPECT_EQ(0, apm_->delay_offset_ms());
737 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
738 EXPECT_EQ(50, apm_->stream_delay_ms());
739}
740
741TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
742 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000743 apm_->set_delay_offset_ms(100);
744 EXPECT_EQ(100, apm_->delay_offset_ms());
745 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000746 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000747 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
748 EXPECT_EQ(200, apm_->stream_delay_ms());
749
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000750 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000751 apm_->set_delay_offset_ms(-50);
752 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000753 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
754 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000755 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
756 EXPECT_EQ(50, apm_->stream_delay_ms());
757}
758
Michael Graczyk86c6d332015-07-23 11:41:39 -0700759void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800760 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700761 AudioProcessing::Error expected_return) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200762 frame_.num_channels_ = num_channels;
763 EXPECT_EQ(expected_return, apm_->ProcessStream(&frame_));
764 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(&frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000765}
766
Michael Graczyk86c6d332015-07-23 11:41:39 -0700767void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800768 size_t num_in_channels,
769 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770 AudioProcessing::Error expected_return) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200771 const StreamConfig input_stream = {frame_.sample_rate_hz_, num_in_channels};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700772 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
773
774 EXPECT_EQ(expected_return,
775 apm_->ProcessStream(float_cb_->channels(), input_stream,
776 output_stream, float_cb_->channels()));
777}
778
779void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800780 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700781 AudioProcessing::Error expected_return) {
782 const ProcessingConfig processing_config = {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200783 {{frame_.sample_rate_hz_, apm_->num_input_channels()},
ekmeyerson60d9b332015-08-14 10:35:55 -0700784 {output_sample_rate_hz_, apm_->num_output_channels()},
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200785 {frame_.sample_rate_hz_, num_rev_channels},
786 {frame_.sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700787
ekmeyerson60d9b332015-08-14 10:35:55 -0700788 EXPECT_EQ(
789 expected_return,
790 apm_->ProcessReverseStream(
791 float_cb_->channels(), processing_config.reverse_input_stream(),
792 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700793}
794
795TEST_F(ApmTest, ChannelsInt16Interface) {
796 // Testing number of invalid and valid channels.
797 Init(16000, 16000, 16000, 4, 4, 4, false);
798
799 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
800
Peter Kasting69558702016-01-12 16:26:35 -0800801 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700802 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000803 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000804 }
805}
806
Michael Graczyk86c6d332015-07-23 11:41:39 -0700807TEST_F(ApmTest, Channels) {
808 // Testing number of invalid and valid channels.
809 Init(16000, 16000, 16000, 4, 4, 4, false);
810
811 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
812 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
813
Peter Kasting69558702016-01-12 16:26:35 -0800814 for (size_t i = 1; i < 4; ++i) {
815 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700816 // Output channels much be one or match input channels.
817 if (j == 1 || i == j) {
818 TestChangingForwardChannels(i, j, kNoErr);
819 TestChangingReverseChannels(i, kNoErr);
820
821 EXPECT_EQ(i, apm_->num_input_channels());
822 EXPECT_EQ(j, apm_->num_output_channels());
823 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800824 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 } else {
826 TestChangingForwardChannels(i, j,
827 AudioProcessing::kBadNumberChannelsError);
828 }
829 }
830 }
831}
832
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000833TEST_F(ApmTest, SampleRatesInt) {
Sam Zackrisson12e319a2020-01-03 14:54:20 +0100834 // Testing some valid sample rates.
835 for (int sample_rate : {8000, 12000, 16000, 32000, 44100, 48000, 96000}) {
836 SetContainerFormat(sample_rate, 2, &frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000837 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000838 }
839}
840
Sam Zackrissone277bde2019-10-25 10:07:54 +0200841// This test repeatedly reconfigures the pre-amplifier in APM, processes a
842// number of frames, and checks that output signal has the right level.
843TEST_F(ApmTest, PreAmplifier) {
844 // Fill the audio frame with a sawtooth pattern.
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200845 rtc::ArrayView<int16_t> frame_data = GetMutableFrameData(&frame_);
846 const size_t samples_per_channel = frame_.samples_per_channel();
Sam Zackrissone277bde2019-10-25 10:07:54 +0200847 for (size_t i = 0; i < samples_per_channel; i++) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200848 for (size_t ch = 0; ch < frame_.num_channels(); ++ch) {
Sam Zackrissone277bde2019-10-25 10:07:54 +0200849 frame_data[i + ch * samples_per_channel] = 10000 * ((i % 3) - 1);
850 }
851 }
852 // Cache the frame in tmp_frame.
853 AudioFrame tmp_frame;
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200854 tmp_frame.CopyFrom(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200855
856 auto compute_power = [](const AudioFrame& frame) {
857 rtc::ArrayView<const int16_t> data = GetFrameData(frame);
858 return std::accumulate(data.begin(), data.end(), 0.0f,
859 [](float a, float b) { return a + b * b; }) /
860 data.size() / 32768 / 32768;
861 };
862
863 const float input_power = compute_power(tmp_frame);
864 // Double-check that the input data is large compared to the error kEpsilon.
865 constexpr float kEpsilon = 1e-4f;
866 RTC_DCHECK_GE(input_power, 10 * kEpsilon);
867
868 // 1. Enable pre-amp with 0 dB gain.
869 AudioProcessing::Config config = apm_->GetConfig();
870 config.pre_amplifier.enabled = true;
871 config.pre_amplifier.fixed_gain_factor = 1.0f;
872 apm_->ApplyConfig(config);
873
874 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200875 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200876 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
877 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200878 float output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200879 EXPECT_NEAR(output_power, input_power, kEpsilon);
880 config = apm_->GetConfig();
881 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.0f);
882
883 // 2. Change pre-amp gain via ApplyConfig.
884 config.pre_amplifier.fixed_gain_factor = 2.0f;
885 apm_->ApplyConfig(config);
886
887 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200888 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200889 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
890 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200891 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200892 EXPECT_NEAR(output_power, 4 * input_power, kEpsilon);
893 config = apm_->GetConfig();
894 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 2.0f);
895
896 // 3. Change pre-amp gain via a RuntimeSetting.
897 apm_->SetRuntimeSetting(
898 AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.5f));
899
900 for (int i = 0; i < 20; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200901 frame_.CopyFrom(tmp_frame);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200902 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kIntFormat));
903 }
Sam Zackrisson70770ac2019-10-25 10:56:53 +0200904 output_power = compute_power(frame_);
Sam Zackrissone277bde2019-10-25 10:07:54 +0200905 EXPECT_NEAR(output_power, 2.25 * input_power, kEpsilon);
906 config = apm_->GetConfig();
907 EXPECT_EQ(config.pre_amplifier.fixed_gain_factor, 1.5f);
908}
909
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000910TEST_F(ApmTest, GainControl) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200911 AudioProcessing::Config config = apm_->GetConfig();
912 config.gain_controller1.enabled = false;
913 apm_->ApplyConfig(config);
914 config.gain_controller1.enabled = true;
915 apm_->ApplyConfig(config);
916
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 // Testing gain modes
Sam Zackrisson41478c72019-10-15 10:10:26 +0200918 for (auto mode :
919 {AudioProcessing::Config::GainController1::kAdaptiveDigital,
920 AudioProcessing::Config::GainController1::kFixedDigital,
921 AudioProcessing::Config::GainController1::kAdaptiveAnalog}) {
922 config.gain_controller1.mode = mode;
923 apm_->ApplyConfig(config);
924 apm_->set_stream_analog_level(100);
925 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000927
Sam Zackrisson41478c72019-10-15 10:10:26 +0200928 // Testing target levels
929 for (int target_level_dbfs : {0, 15, 31}) {
930 config.gain_controller1.target_level_dbfs = target_level_dbfs;
931 apm_->ApplyConfig(config);
932 apm_->set_stream_analog_level(100);
933 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000934 }
935
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100936 // Testing compression gains
Sam Zackrisson41478c72019-10-15 10:10:26 +0200937 for (int compression_gain_db : {0, 10, 90}) {
938 config.gain_controller1.compression_gain_db = compression_gain_db;
939 apm_->ApplyConfig(config);
940 apm_->set_stream_analog_level(100);
941 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000942 }
943
944 // Testing limiter off/on
Sam Zackrisson41478c72019-10-15 10:10:26 +0200945 for (bool enable : {false, true}) {
946 config.gain_controller1.enable_limiter = enable;
947 apm_->ApplyConfig(config);
948 apm_->set_stream_analog_level(100);
949 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
950 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000951
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100952 // Testing level limits
Sam Zackrisson41478c72019-10-15 10:10:26 +0200953 std::array<int, 4> kMinLevels = {0, 0, 255, 65000};
954 std::array<int, 4> kMaxLevels = {255, 1024, 65535, 65535};
955 for (size_t i = 0; i < kMinLevels.size(); ++i) {
956 int min_level = kMinLevels[i];
957 int max_level = kMaxLevels[i];
958 config.gain_controller1.analog_level_minimum = min_level;
959 config.gain_controller1.analog_level_maximum = max_level;
960 apm_->ApplyConfig(config);
961 apm_->set_stream_analog_level((min_level + max_level) / 2);
962 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(kFloatFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000963 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000964}
965
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100966#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
967TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200968 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100969 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200970 config.gain_controller1.target_level_dbfs = -1;
971 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100972}
973
974TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200975 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100976 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200977 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();
Per Åhgren0695df12020-01-13 14:43:13 +0100983 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200984 config.gain_controller1.compression_gain_db = -1;
985 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100986}
987
988TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200989 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100990 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200991 config.gain_controller1.compression_gain_db = 91;
992 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100993}
994
995TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +0200996 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +0100997 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +0200998 config.gain_controller1.analog_level_minimum = -1;
999 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001000}
1001
1002TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001003 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001004 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001005 config.gain_controller1.analog_level_maximum = 65536;
1006 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001007}
1008
1009TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001010 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001011 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001012 config.gain_controller1.analog_level_minimum = 512;
1013 config.gain_controller1.analog_level_maximum = 255;
1014 EXPECT_DEATH(apm_->ApplyConfig(config), "");
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001015}
1016
1017TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001018 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001019 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001020 config.gain_controller1.analog_level_minimum = 255;
1021 config.gain_controller1.analog_level_maximum = 512;
1022 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001023 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
1024}
1025
1026TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
Sam Zackrisson41478c72019-10-15 10:10:26 +02001027 auto config = apm_->GetConfig();
Per Åhgren0695df12020-01-13 14:43:13 +01001028 config.gain_controller1.enabled = true;
Sam Zackrisson41478c72019-10-15 10:10:26 +02001029 config.gain_controller1.analog_level_minimum = 255;
1030 config.gain_controller1.analog_level_maximum = 512;
1031 apm_->ApplyConfig(config);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001032 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
1033}
1034#endif
1035
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001036void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001037 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001038 auto config = apm_->GetConfig();
1039 config.gain_controller1.enabled = true;
1040 config.gain_controller1.mode =
1041 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1042 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001043
1044 int out_analog_level = 0;
1045 for (int i = 0; i < 2000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001046 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001047 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001048 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001049
1050 // Always pass in the same volume.
Sam Zackrisson41478c72019-10-15 10:10:26 +02001051 apm_->set_stream_analog_level(100);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001052 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001053 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001054 }
1055
1056 // Ensure the AGC is still able to reach the maximum.
1057 EXPECT_EQ(255, out_analog_level);
1058}
1059
1060// Verifies that despite volume slider quantization, the AGC can continue to
1061// increase its volume.
1062TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001063 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001064 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1065 }
1066}
1067
1068void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001069 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001070 auto config = apm_->GetConfig();
1071 config.gain_controller1.enabled = true;
1072 config.gain_controller1.mode =
1073 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
1074 apm_->ApplyConfig(config);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001075
1076 int out_analog_level = 100;
1077 for (int i = 0; i < 1000; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001078 ReadFrameWithRewind(near_file_, &frame_);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001079 // Ensure the audio is at a low level, so the AGC will try to increase it.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001080 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001081
Sam Zackrisson41478c72019-10-15 10:10:26 +02001082 apm_->set_stream_analog_level(out_analog_level);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001083 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001084 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001085 }
1086
1087 // Ensure the volume was raised.
1088 EXPECT_GT(out_analog_level, 100);
1089 int highest_level_reached = out_analog_level;
1090 // Simulate a user manual volume change.
1091 out_analog_level = 100;
1092
1093 for (int i = 0; i < 300; ++i) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001094 ReadFrameWithRewind(near_file_, &frame_);
1095 ScaleFrame(&frame_, 0.25);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001096
Sam Zackrisson41478c72019-10-15 10:10:26 +02001097 apm_->set_stream_analog_level(out_analog_level);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001098 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001099 out_analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001100 // Check that AGC respected the manually adjusted volume.
1101 EXPECT_LT(out_analog_level, highest_level_reached);
1102 }
1103 // Check that the volume was still raised.
1104 EXPECT_GT(out_analog_level, 100);
1105}
1106
1107TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001108 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001109 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1110 }
1111}
1112
niklase@google.com470e71d2011-07-07 08:21:25 +00001113TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001114 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001115 AudioProcessing::Config apm_config;
1116 apm_config.high_pass_filter.enabled = true;
1117 apm_->ApplyConfig(apm_config);
1118 apm_config.high_pass_filter.enabled = false;
1119 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001120}
1121
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001122TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001123 AudioProcessing::Config config = apm_->GetConfig();
1124 EXPECT_FALSE(config.echo_canceller.enabled);
1125 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson41478c72019-10-15 10:10:26 +02001126 EXPECT_FALSE(config.gain_controller1.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001127 EXPECT_FALSE(config.level_estimation.enabled);
saza0bad15f2019-10-16 11:46:11 +02001128 EXPECT_FALSE(config.noise_suppression.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001129 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001130}
1131
1132TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001133 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001134 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001135 SetFrameTo(&frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001136 AudioFrame frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001137 frame_copy.CopyFrom(frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001138 for (int j = 0; j < 1000; j++) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001139 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1140 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
1141 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&frame_));
1142 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001143 }
1144 }
1145}
1146
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001147TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1148 // Test that ProcessStream copies input to output even with no processing.
Per Åhgrenc8626b62019-08-23 15:49:51 +02001149 const size_t kSamples = 160;
1150 const int sample_rate = 16000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001151 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001152 float dest[kSamples] = {};
1153
1154 auto src_channels = &src[0];
1155 auto dest_channels = &dest[0];
1156
Ivo Creusen62337e52018-01-09 14:17:33 +01001157 apm_.reset(AudioProcessingBuilder().Create());
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001158 EXPECT_NOERR(apm_->ProcessStream(&src_channels, StreamConfig(sample_rate, 1),
1159 StreamConfig(sample_rate, 1),
1160 &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001161
1162 for (size_t i = 0; i < kSamples; ++i) {
1163 EXPECT_EQ(src[i], dest[i]);
1164 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001165
1166 // Same for ProcessReverseStream.
1167 float rev_dest[kSamples] = {};
1168 auto rev_dest_channels = &rev_dest[0];
1169
1170 StreamConfig input_stream = {sample_rate, 1};
1171 StreamConfig output_stream = {sample_rate, 1};
1172 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1173 output_stream, &rev_dest_channels));
1174
1175 for (size_t i = 0; i < kSamples; ++i) {
1176 EXPECT_EQ(src[i], rev_dest[i]);
1177 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001178}
1179
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001180TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1181 EnableAllComponents();
1182
pkasting25702cb2016-01-08 13:50:27 -08001183 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001184 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1185 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001186 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001187 ASSERT_EQ(0, feof(far_file_));
1188 ASSERT_EQ(0, feof(near_file_));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001189 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
1190 CopyLeftToRightChannel(revframe_.mutable_data(),
1191 revframe_.samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001192
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001193 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(&revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001194
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001195 CopyLeftToRightChannel(frame_.mutable_data(),
1196 frame_.samples_per_channel_);
1197 frame_.vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001198
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001199 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001200 apm_->set_stream_analog_level(analog_level);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001201 ASSERT_EQ(kNoErr, apm_->ProcessStream(&frame_));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001202 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001203
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001204 VerifyChannelsAreEqual(frame_.data(), frame_.samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001205 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001206 rewind(far_file_);
1207 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001208 }
1209}
1210
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001211TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001212 // Verify the filter is not active through undistorted audio when:
1213 // 1. No components are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001214 SetFrameTo(&frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001215 AudioFrame frame_copy;
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001216 frame_copy.CopyFrom(frame_);
1217 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1218 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1219 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001220
1221 // 2. Only the level estimator is enabled...
saza6787f232019-10-11 19:31:07 +02001222 auto apm_config = apm_->GetConfig();
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001223 SetFrameTo(&frame_, 1000);
1224 frame_copy.CopyFrom(frame_);
saza6787f232019-10-11 19:31:07 +02001225 apm_config.level_estimation.enabled = true;
1226 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001227 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1228 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1229 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
saza6787f232019-10-11 19:31:07 +02001230 apm_config.level_estimation.enabled = false;
1231 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001232
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001233 // 3. Only GetStatistics-reporting VAD is enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001234 SetFrameTo(&frame_, 1000);
1235 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001236 apm_config.voice_detection.enabled = true;
1237 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001238 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1239 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1240 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001241 apm_config.voice_detection.enabled = false;
1242 apm_->ApplyConfig(apm_config);
1243
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001244 // 4. Both the VAD and the level estimator are enabled...
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001245 SetFrameTo(&frame_, 1000);
1246 frame_copy.CopyFrom(frame_);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001247 apm_config.voice_detection.enabled = true;
saza6787f232019-10-11 19:31:07 +02001248 apm_config.level_estimation.enabled = true;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001249 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001250 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1251 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1252 EXPECT_TRUE(FrameDataAreEqual(frame_, frame_copy));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001253 apm_config.voice_detection.enabled = false;
saza6787f232019-10-11 19:31:07 +02001254 apm_config.level_estimation.enabled = false;
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001255 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001256
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001257 // Check the test is valid. We should have distortion from the filter
1258 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001259 apm_config.echo_canceller.enabled = true;
1260 apm_config.echo_canceller.mobile_mode = false;
1261 apm_->ApplyConfig(apm_config);
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001262 frame_.samples_per_channel_ = 320;
1263 frame_.num_channels_ = 2;
1264 frame_.sample_rate_hz_ = 32000;
1265 SetFrameTo(&frame_, 1000);
1266 frame_copy.CopyFrom(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001267 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001268 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1269 EXPECT_FALSE(FrameDataAreEqual(frame_, frame_copy));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001270}
1271
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001272#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1273void ApmTest::ProcessDebugDump(const std::string& in_filename,
1274 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001275 Format format,
1276 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001277 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001278 FILE* in_file = fopen(in_filename.c_str(), "rb");
1279 ASSERT_TRUE(in_file != NULL);
1280 audioproc::Event event_msg;
1281 bool first_init = true;
1282
1283 while (ReadMessageFromFile(in_file, &event_msg)) {
1284 if (event_msg.type() == audioproc::Event::INIT) {
1285 const audioproc::Init msg = event_msg.init();
1286 int reverse_sample_rate = msg.sample_rate();
1287 if (msg.has_reverse_sample_rate()) {
1288 reverse_sample_rate = msg.reverse_sample_rate();
1289 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001290 int output_sample_rate = msg.sample_rate();
1291 if (msg.has_output_sample_rate()) {
1292 output_sample_rate = msg.output_sample_rate();
1293 }
1294
Jonas Olssona4d87372019-07-05 19:08:33 +02001295 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1296 msg.num_input_channels(), msg.num_output_channels(),
1297 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001298 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001299 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001300 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001301 auto aec_dump =
1302 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1303 EXPECT_TRUE(aec_dump);
1304 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001305 first_init = false;
1306 }
1307
1308 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1309 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1310
1311 if (msg.channel_size() > 0) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001312 ASSERT_EQ(revframe_.num_channels_,
Peter Kasting69558702016-01-12 16:26:35 -08001313 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001314 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001315 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1316 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001317 }
1318 } else {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001319 memcpy(revframe_.mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001320 if (format == kFloatFormat) {
1321 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001322 ConvertToFloat(revframe_, revfloat_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001323 }
1324 }
1325 AnalyzeReverseStreamChooser(format);
1326
1327 } else if (event_msg.type() == audioproc::Event::STREAM) {
1328 const audioproc::Stream msg = event_msg.stream();
1329 // ProcessStream could have changed this for the output frame.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001330 frame_.num_channels_ = apm_->num_input_channels();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001331
Sam Zackrisson41478c72019-10-15 10:10:26 +02001332 apm_->set_stream_analog_level(msg.level());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001333 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001334 if (msg.has_keypress()) {
1335 apm_->set_stream_key_pressed(msg.keypress());
1336 } else {
1337 apm_->set_stream_key_pressed(true);
1338 }
1339
1340 if (msg.input_channel_size() > 0) {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001341 ASSERT_EQ(frame_.num_channels_,
Peter Kasting69558702016-01-12 16:26:35 -08001342 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001343 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001344 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1345 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001346 }
1347 } else {
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001348 memcpy(frame_.mutable_data(), msg.input_data().data(),
yujo36b1a5f2017-06-12 12:45:32 -07001349 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001350 if (format == kFloatFormat) {
1351 // We're using an int16 input file; convert to float.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001352 ConvertToFloat(frame_, float_cb_.get());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001353 }
1354 }
1355 ProcessStreamChooser(format);
1356 }
1357 }
aleloif4dd1912017-06-15 01:55:38 -07001358 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001359 fclose(in_file);
1360}
1361
1362void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001363 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001364 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001365 std::string format_string;
1366 switch (format) {
1367 case kIntFormat:
1368 format_string = "_int";
1369 break;
1370 case kFloatFormat:
1371 format_string = "_float";
1372 break;
1373 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001374 const std::string ref_filename = test::TempFilename(
1375 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1376 const std::string out_filename = test::TempFilename(
1377 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001378 const std::string limited_filename = test::TempFilename(
1379 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1380 const size_t logging_limit_bytes = 100000;
1381 // We expect at least this many bytes in the created logfile.
1382 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001383 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001384 ProcessDebugDump(in_filename, ref_filename, format, -1);
1385 ProcessDebugDump(ref_filename, out_filename, format, -1);
1386 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001387
1388 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1389 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001390 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001391 ASSERT_TRUE(ref_file != NULL);
1392 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001393 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001394 std::unique_ptr<uint8_t[]> ref_bytes;
1395 std::unique_ptr<uint8_t[]> out_bytes;
1396 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001397
1398 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1399 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001400 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001401 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001402 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001403 while (ref_size > 0 && out_size > 0) {
1404 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001405 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001406 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001407 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001408 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001409 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1411 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001412 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001413 }
1414 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001415 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1416 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001417 EXPECT_NE(0, feof(ref_file));
1418 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001419 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001420 ASSERT_EQ(0, fclose(ref_file));
1421 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001422 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001423 remove(ref_filename.c_str());
1424 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001425 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001426}
1427
pbosc7a65692016-05-06 12:50:04 -07001428TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001429 VerifyDebugDumpTest(kIntFormat);
1430}
1431
pbosc7a65692016-05-06 12:50:04 -07001432TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001433 VerifyDebugDumpTest(kFloatFormat);
1434}
1435#endif
1436
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001437// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001438TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001439 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001440 const std::string filename =
1441 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001442 {
1443 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1444 EXPECT_FALSE(aec_dump);
1445 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001446
1447#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1448 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001449 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001450
aleloif4dd1912017-06-15 01:55:38 -07001451 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1452 EXPECT_TRUE(aec_dump);
1453 apm_->AttachAecDump(std::move(aec_dump));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001454 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
1455 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001456 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001457
1458 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001459 FILE* fid = fopen(filename.c_str(), "r");
1460 ASSERT_TRUE(fid != NULL);
1461
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001462 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001463 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001464 ASSERT_EQ(0, remove(filename.c_str()));
1465#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001466 // Verify the file has NOT been written.
1467 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1468#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1469}
1470
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001471// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001472TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001473 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001474
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001475 const std::string filename =
1476 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001477 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1478 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001479
1480#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1481 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001482 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001483
Niels Möllere8e4dc42019-06-11 14:04:16 +02001484 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001485 EXPECT_TRUE(aec_dump);
1486 apm_->AttachAecDump(std::move(aec_dump));
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001487 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&revframe_));
1488 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
aleloif4dd1912017-06-15 01:55:38 -07001489 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001490
1491 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001492 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001493 ASSERT_TRUE(fid != NULL);
1494
1495 // Clean it up.
1496 ASSERT_EQ(0, fclose(fid));
1497 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001498#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1499}
1500
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001501// TODO(andrew): Add a test to process a few frames with different combinations
1502// of enabled components.
1503
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001504TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001505 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001506 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001507
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001508 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001509 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001510 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001511 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001512 for (size_t i = 0; i < arraysize(kChannels); i++) {
1513 for (size_t j = 0; j < arraysize(kChannels); j++) {
1514 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001515 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001516 test->set_num_reverse_channels(kChannels[i]);
1517 test->set_num_input_channels(kChannels[j]);
1518 test->set_num_output_channels(kChannels[j]);
1519 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001520 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001521 }
1522 }
1523 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001524#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1525 // To test the extended filter mode.
1526 audioproc::Test* test = ref_data.add_test();
1527 test->set_num_reverse_channels(2);
1528 test->set_num_input_channels(2);
1529 test->set_num_output_channels(2);
1530 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1531 test->set_use_aec_extended_filter(true);
1532#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001533 }
1534
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001535 for (int i = 0; i < ref_data.test_size(); i++) {
1536 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001537
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001538 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001539 // TODO(ajm): We no longer allow different input and output channels. Skip
1540 // these tests for now, but they should be removed from the set.
1541 if (test->num_input_channels() != test->num_output_channels())
1542 continue;
1543
Per Åhgren0695df12020-01-13 14:43:13 +01001544 apm_.reset(AudioProcessingBuilder().Create());
1545 AudioProcessing::Config apm_config = apm_->GetConfig();
1546 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1547 apm_->ApplyConfig(apm_config);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001548
1549 EnableAllComponents();
1550
Jonas Olssona4d87372019-07-05 19:08:33 +02001551 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001552 static_cast<size_t>(test->num_input_channels()),
1553 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001554 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001555
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001556 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001557 int has_voice_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001558 int analog_level = 127;
1559 int analog_level_average = 0;
1560 int max_output_average = 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001561 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001562#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001563 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001564#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001565
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001566 while (ReadFrame(far_file_, &revframe_) && ReadFrame(near_file_, &frame_)) {
1567 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(&revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001568
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001569 frame_.vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001570
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001571 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001572 apm_->set_stream_analog_level(analog_level);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001573
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001574 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(&frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001575
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001576 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001577 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001578 frame_.num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001579
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001580 max_output_average += MaxAudioFrame(frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001581
Sam Zackrisson41478c72019-10-15 10:10:26 +02001582 analog_level = apm_->recommended_stream_analog_level();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001583 analog_level_average += analog_level;
Per Åhgrencf4c8722019-12-30 14:32:14 +01001584 AudioProcessingStats stats = apm_->GetStatistics();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001585 EXPECT_TRUE(stats.voice_detected);
1586 EXPECT_TRUE(stats.output_rms_dbfs);
1587 has_voice_count += *stats.voice_detected ? 1 : 0;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001588 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001589
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001590 size_t frame_size = frame_.samples_per_channel_ * frame_.num_channels_;
Jonas Olssona4d87372019-07-05 19:08:33 +02001591 size_t write_count =
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001592 fwrite(frame_.data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001593 ASSERT_EQ(frame_size, write_count);
1594
1595 // Reset in case of downmixing.
Sam Zackrisson70770ac2019-10-25 10:56:53 +02001596 frame_.num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001597 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001598
1599#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1600 const int kStatsAggregationFrameNum = 100; // 1 second.
1601 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001602 // Get echo and delay metrics.
Per Åhgrencf4c8722019-12-30 14:32:14 +01001603 AudioProcessingStats stats = apm_->GetStatistics();
minyue58530ed2016-05-24 05:50:12 -07001604
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001605 // Echo metrics.
1606 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1607 const float echo_return_loss_enhancement =
1608 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001609 const float residual_echo_likelihood =
1610 stats.residual_echo_likelihood.value_or(-1.0f);
1611 const float residual_echo_likelihood_recent_max =
1612 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1613
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001614 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
minyue58530ed2016-05-24 05:50:12 -07001615 const audioproc::Test::EchoMetrics& reference =
1616 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001617 constexpr float kEpsilon = 0.01;
1618 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1619 EXPECT_NEAR(echo_return_loss_enhancement,
1620 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001621 EXPECT_NEAR(residual_echo_likelihood,
1622 reference.residual_echo_likelihood(), kEpsilon);
1623 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1624 reference.residual_echo_likelihood_recent_max(),
1625 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001626 ++stats_index;
1627 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001628 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1629 message_echo->set_echo_return_loss(echo_return_loss);
1630 message_echo->set_echo_return_loss_enhancement(
1631 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001632 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1633 message_echo->set_residual_echo_likelihood_recent_max(
1634 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001635 }
1636 }
1637#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001638 }
1639 max_output_average /= frame_count;
1640 analog_level_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001641 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001642
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001643 if (!absl::GetFlag(FLAGS_write_apm_ref_data)) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001644 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001645 // When running the test on a N7 we get a {2, 6} difference of
1646 // |has_voice_count| and |max_output_average| is up to 18 higher.
1647 // All numbers being consistently higher on N7 compare to ref_data.
1648 // TODO(bjornv): If we start getting more of these offsets on Android we
1649 // should consider a different approach. Either using one slack for all,
1650 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001651#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001652 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001653 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001654 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001655 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001656#else
1657 const int kHasVoiceCountOffset = 0;
1658 const int kHasVoiceCountNear = kIntNear;
1659 const int kMaxOutputAverageOffset = 0;
1660 const int kMaxOutputAverageNear = kIntNear;
1661#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001662 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001663 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001664
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001665 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001666 EXPECT_NEAR(test->max_output_average(),
1667 max_output_average - kMaxOutputAverageOffset,
1668 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001669#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001670 const double kFloatNear = 0.0005;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001671 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001672#endif
1673 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001674 test->set_has_voice_count(has_voice_count);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001675
1676 test->set_analog_level_average(analog_level_average);
1677 test->set_max_output_average(max_output_average);
1678
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001679#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrisson11b87032018-12-18 17:13:58 +01001680 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001681#endif
1682 }
1683
1684 rewind(far_file_);
1685 rewind(near_file_);
1686 }
1687
Sam Zackrisson6558fa52019-08-26 10:12:41 +02001688 if (absl::GetFlag(FLAGS_write_apm_ref_data)) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001689 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001690 }
1691}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001692
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001693TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1694 struct ChannelFormat {
1695 AudioProcessing::ChannelLayout in_layout;
1696 AudioProcessing::ChannelLayout out_layout;
1697 };
1698 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001699 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1700 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1701 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001702 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001703
Ivo Creusen62337e52018-01-09 14:17:33 +01001704 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001705 // Enable one component just to ensure some processing takes place.
saza0bad15f2019-10-16 11:46:11 +02001706 AudioProcessing::Config config;
1707 config.noise_suppression.enabled = true;
1708 ap->ApplyConfig(config);
pkasting25702cb2016-01-08 13:50:27 -08001709 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001710 const int in_rate = 44100;
1711 const int out_rate = 48000;
1712 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1713 TotalChannelsFromLayout(cf[i].in_layout));
1714 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1715 ChannelsFromLayout(cf[i].out_layout));
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001716 bool has_keyboard = cf[i].in_layout == AudioProcessing::kMonoAndKeyboard ||
1717 cf[i].in_layout == AudioProcessing::kStereoAndKeyboard;
1718 StreamConfig in_sc(in_rate, ChannelsFromLayout(cf[i].in_layout),
1719 has_keyboard);
1720 StreamConfig out_sc(out_rate, ChannelsFromLayout(cf[i].out_layout));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001721
1722 // Run over a few chunks.
1723 for (int j = 0; j < 10; ++j) {
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001724 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_sc, out_sc,
1725 out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001726 }
1727 }
1728}
1729
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001730// Compares the reference and test arrays over a region around the expected
1731// delay. Finds the highest SNR in that region and adds the variance and squared
1732// error results to the supplied accumulators.
1733void UpdateBestSNR(const float* ref,
1734 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001735 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001736 int expected_delay,
1737 double* variance_acc,
1738 double* sq_error_acc) {
1739 double best_snr = std::numeric_limits<double>::min();
1740 double best_variance = 0;
1741 double best_sq_error = 0;
1742 // Search over a region of eight samples around the expected delay.
1743 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1744 ++delay) {
1745 double sq_error = 0;
1746 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001747 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001748 double error = test[i + delay] - ref[i];
1749 sq_error += error * error;
1750 variance += ref[i] * ref[i];
1751 }
1752
1753 if (sq_error == 0) {
1754 *variance_acc += variance;
1755 return;
1756 }
1757 double snr = variance / sq_error;
1758 if (snr > best_snr) {
1759 best_snr = snr;
1760 best_variance = variance;
1761 best_sq_error = sq_error;
1762 }
1763 }
1764
1765 *variance_acc += best_variance;
1766 *sq_error_acc += best_sq_error;
1767}
1768
1769// Used to test a multitude of sample rate and channel combinations. It works
1770// by first producing a set of reference files (in SetUpTestCase) that are
1771// assumed to be correct, as the used parameters are verified by other tests
1772// in this collection. Primarily the reference files are all produced at
1773// "native" rates which do not involve any resampling.
1774
1775// Each test pass produces an output file with a particular format. The output
1776// is matched against the reference file closest to its internal processing
1777// format. If necessary the output is resampled back to its process format.
1778// Due to the resampling distortion, we don't expect identical results, but
1779// enforce SNR thresholds which vary depending on the format. 0 is a special
1780// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001781typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001782class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001783 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001784 public:
1785 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001786 : input_rate_(std::get<0>(GetParam())),
1787 output_rate_(std::get<1>(GetParam())),
1788 reverse_input_rate_(std::get<2>(GetParam())),
1789 reverse_output_rate_(std::get<3>(GetParam())),
1790 expected_snr_(std::get<4>(GetParam())),
1791 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001792
1793 virtual ~AudioProcessingTest() {}
1794
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001795 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001796 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001797 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001798 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001799 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1800 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1801 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001802 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001803 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1804 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1805 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001806 }
1807 }
1808 }
1809 }
1810
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001811 void TearDown() {
1812 // Remove "out" files after each test.
1813 ClearTempOutFiles();
1814 }
1815
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001816 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001817
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001818 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001819 // to a file specified with |output_file_prefix|. Both forward and reverse
1820 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001821 static void ProcessFormat(int input_rate,
1822 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001823 int reverse_input_rate,
1824 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001825 size_t num_input_channels,
1826 size_t num_output_channels,
1827 size_t num_reverse_input_channels,
1828 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001829 const std::string& output_file_prefix) {
Per Åhgren0695df12020-01-13 14:43:13 +01001830 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
1831 AudioProcessing::Config apm_config = ap->GetConfig();
1832 apm_config.gain_controller1.analog_gain_controller.enabled = false;
1833 ap->ApplyConfig(apm_config);
1834
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001835 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001836
ekmeyerson60d9b332015-08-14 10:35:55 -07001837 ProcessingConfig processing_config = {
1838 {{input_rate, num_input_channels},
1839 {output_rate, num_output_channels},
1840 {reverse_input_rate, num_reverse_input_channels},
1841 {reverse_output_rate, num_reverse_output_channels}}};
1842 ap->Initialize(processing_config);
1843
1844 FILE* far_file =
1845 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001846 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001847 FILE* out_file = fopen(
1848 OutputFilePath(
1849 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1850 reverse_output_rate, num_input_channels, num_output_channels,
1851 num_reverse_input_channels, num_reverse_output_channels, kForward)
1852 .c_str(),
1853 "wb");
1854 FILE* rev_out_file = fopen(
1855 OutputFilePath(
1856 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1857 reverse_output_rate, num_input_channels, num_output_channels,
1858 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1859 .c_str(),
1860 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001861 ASSERT_TRUE(far_file != NULL);
1862 ASSERT_TRUE(near_file != NULL);
1863 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001864 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001865
1866 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1867 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001868 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1869 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001870 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1871 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001872 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1873 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001874
1875 // Temporary buffers.
1876 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001877 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1878 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001879 std::unique_ptr<float[]> float_data(new float[max_length]);
1880 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001881
1882 int analog_level = 127;
1883 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1884 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001885 EXPECT_NOERR(ap->ProcessReverseStream(
1886 rev_cb.channels(), processing_config.reverse_input_stream(),
1887 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001888
1889 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sam Zackrisson41478c72019-10-15 10:10:26 +02001890 ap->set_stream_analog_level(analog_level);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001891
1892 EXPECT_NOERR(ap->ProcessStream(
Gustaf Ullbergcb307262019-10-29 09:30:44 +01001893 fwd_cb.channels(), StreamConfig(input_rate, num_input_channels),
1894 StreamConfig(output_rate, num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001895
ekmeyerson60d9b332015-08-14 10:35:55 -07001896 // Dump forward output to file.
1897 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001898 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001899 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001900
Jonas Olssona4d87372019-07-05 19:08:33 +02001901 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1902 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001903
ekmeyerson60d9b332015-08-14 10:35:55 -07001904 // Dump reverse output to file.
1905 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
1906 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001907 size_t rev_out_length =
1908 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001909
Jonas Olssona4d87372019-07-05 19:08:33 +02001910 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1911 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07001912
Sam Zackrisson41478c72019-10-15 10:10:26 +02001913 analog_level = ap->recommended_stream_analog_level();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001914 }
1915 fclose(far_file);
1916 fclose(near_file);
1917 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07001918 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001919 }
1920
1921 protected:
1922 int input_rate_;
1923 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001924 int reverse_input_rate_;
1925 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001926 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07001927 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001928};
1929
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00001930TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001931 struct ChannelFormat {
1932 int num_input;
1933 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07001934 int num_reverse_input;
1935 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001936 };
1937 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001938 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
1939 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001940 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001941
pkasting25702cb2016-01-08 13:50:27 -08001942 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001943 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
1944 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
1945 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07001946
ekmeyerson60d9b332015-08-14 10:35:55 -07001947 // Verify output for both directions.
1948 std::vector<StreamDirection> stream_directions;
1949 stream_directions.push_back(kForward);
1950 stream_directions.push_back(kReverse);
1951 for (StreamDirection file_direction : stream_directions) {
1952 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
1953 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
1954 const int out_num =
1955 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
1956 const double expected_snr =
1957 file_direction ? expected_reverse_snr_ : expected_snr_;
1958
1959 const int min_ref_rate = std::min(in_rate, out_rate);
1960 int ref_rate;
1961
1962 if (min_ref_rate > 32000) {
1963 ref_rate = 48000;
1964 } else if (min_ref_rate > 16000) {
1965 ref_rate = 32000;
1966 } else if (min_ref_rate > 8000) {
1967 ref_rate = 16000;
1968 } else {
1969 ref_rate = 8000;
1970 }
Per Åhgrenc0424252019-12-10 13:04:15 +01001971
ekmeyerson60d9b332015-08-14 10:35:55 -07001972 FILE* out_file = fopen(
1973 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
1974 reverse_output_rate_, cf[i].num_input,
1975 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02001976 cf[i].num_reverse_output, file_direction)
1977 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07001978 "rb");
1979 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02001980 FILE* ref_file =
1981 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
1982 cf[i].num_output, cf[i].num_output,
1983 cf[i].num_reverse_output,
1984 cf[i].num_reverse_output, file_direction)
1985 .c_str(),
1986 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07001987 ASSERT_TRUE(out_file != NULL);
1988 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001989
pkasting25702cb2016-01-08 13:50:27 -08001990 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
1991 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07001992 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08001993 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001994 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08001995 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07001996 // Data from the resampled output, in case the reference and output rates
1997 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08001998 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001999
ekmeyerson60d9b332015-08-14 10:35:55 -07002000 PushResampler<float> resampler;
2001 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002002
ekmeyerson60d9b332015-08-14 10:35:55 -07002003 // Compute the resampling delay of the output relative to the reference,
2004 // to find the region over which we should search for the best SNR.
2005 float expected_delay_sec = 0;
2006 if (in_rate != ref_rate) {
2007 // Input resampling delay.
2008 expected_delay_sec +=
2009 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2010 }
2011 if (out_rate != ref_rate) {
2012 // Output resampling delay.
2013 expected_delay_sec +=
2014 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2015 // Delay of converting the output back to its processing rate for
2016 // testing.
2017 expected_delay_sec +=
2018 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2019 }
2020 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002021 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002022
ekmeyerson60d9b332015-08-14 10:35:55 -07002023 double variance = 0;
2024 double sq_error = 0;
2025 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2026 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2027 float* out_ptr = out_data.get();
2028 if (out_rate != ref_rate) {
2029 // Resample the output back to its internal processing rate if
2030 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002031 ASSERT_EQ(ref_length,
2032 static_cast<size_t>(resampler.Resample(
2033 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002034 out_ptr = cmp_data.get();
2035 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002036
ekmeyerson60d9b332015-08-14 10:35:55 -07002037 // Update the |sq_error| and |variance| accumulators with the highest
2038 // SNR of reference vs output.
2039 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2040 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002041 }
2042
ekmeyerson60d9b332015-08-14 10:35:55 -07002043 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2044 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2045 << cf[i].num_input << ", " << cf[i].num_output << ", "
2046 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2047 << ", " << file_direction << "): ";
2048 if (sq_error > 0) {
2049 double snr = 10 * log10(variance / sq_error);
2050 EXPECT_GE(snr, expected_snr);
2051 EXPECT_NE(0, expected_snr);
2052 std::cout << "SNR=" << snr << " dB" << std::endl;
2053 } else {
aluebs776593b2016-03-15 14:04:58 -07002054 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002055 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002056
ekmeyerson60d9b332015-08-14 10:35:55 -07002057 fclose(out_file);
2058 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002059 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002060 }
2061}
2062
2063#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002064INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002065 CommonFormats,
2066 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002067 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2068 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2069 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2070 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2071 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2072 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2073 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2074 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2075 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2076 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2077 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2078 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002079
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002080 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2081 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2082 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2083 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2084 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2085 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2086 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2087 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2088 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2089 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2090 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2091 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002092
Per Åhgrenc0424252019-12-10 13:04:15 +01002093 std::make_tuple(32000, 48000, 48000, 48000, 15, 0),
2094 std::make_tuple(32000, 48000, 32000, 48000, 15, 30),
2095 std::make_tuple(32000, 48000, 16000, 48000, 15, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002096 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2097 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2098 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2099 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2100 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2101 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2102 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2103 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2104 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002105
Per Åhgrenc0424252019-12-10 13:04:15 +01002106 std::make_tuple(16000, 48000, 48000, 48000, 9, 0),
2107 std::make_tuple(16000, 48000, 32000, 48000, 9, 30),
2108 std::make_tuple(16000, 48000, 16000, 48000, 9, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002109 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2110 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2111 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2112 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2113 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2114 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2115 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2116 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2117 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002118
2119#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002120INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002121 CommonFormats,
2122 AudioProcessingTest,
Per Åhgren0aefbf02019-08-23 21:29:17 +02002123 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 19, 0),
2124 std::make_tuple(48000, 48000, 32000, 48000, 19, 30),
2125 std::make_tuple(48000, 48000, 16000, 48000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002126 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2127 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2128 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002129 std::make_tuple(48000, 32000, 48000, 32000, 19, 35),
2130 std::make_tuple(48000, 32000, 32000, 32000, 19, 0),
2131 std::make_tuple(48000, 32000, 16000, 32000, 19, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002132 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2133 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2134 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002135
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002136 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2137 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2138 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2139 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2140 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2141 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
Per Åhgren0aefbf02019-08-23 21:29:17 +02002142 std::make_tuple(44100, 32000, 48000, 32000, 18, 35),
2143 std::make_tuple(44100, 32000, 32000, 32000, 18, 0),
2144 std::make_tuple(44100, 32000, 16000, 32000, 18, 20),
2145 std::make_tuple(44100, 16000, 48000, 16000, 19, 20),
2146 std::make_tuple(44100, 16000, 32000, 16000, 19, 20),
2147 std::make_tuple(44100, 16000, 16000, 16000, 19, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002148
Per Åhgrenc0424252019-12-10 13:04:15 +01002149 std::make_tuple(32000, 48000, 48000, 48000, 17, 0),
2150 std::make_tuple(32000, 48000, 32000, 48000, 17, 30),
2151 std::make_tuple(32000, 48000, 16000, 48000, 17, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002152 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2153 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2154 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002155 std::make_tuple(32000, 32000, 48000, 32000, 27, 35),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002156 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002157 std::make_tuple(32000, 32000, 16000, 32000, 30, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002158 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2159 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2160 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002161
Per Åhgrenc0424252019-12-10 13:04:15 +01002162 std::make_tuple(16000, 48000, 48000, 48000, 11, 0),
2163 std::make_tuple(16000, 48000, 32000, 48000, 11, 30),
2164 std::make_tuple(16000, 48000, 16000, 48000, 11, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002165 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2166 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2167 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
Per Åhgren0cbb58e2019-10-29 22:59:44 +01002168 std::make_tuple(16000, 32000, 48000, 32000, 24, 35),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002169 std::make_tuple(16000, 32000, 32000, 32000, 24, 0),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002170 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
Per Åhgrene35b32c2019-11-22 18:22:04 +01002171 std::make_tuple(16000, 16000, 48000, 16000, 28, 20),
2172 std::make_tuple(16000, 16000, 32000, 16000, 28, 20),
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002173 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002174#endif
2175
Per Åhgren3e8bf282019-08-29 23:38:40 +02002176// Produces a scoped trace debug output.
2177std::string ProduceDebugText(int render_input_sample_rate_hz,
2178 int render_output_sample_rate_hz,
2179 int capture_input_sample_rate_hz,
2180 int capture_output_sample_rate_hz,
2181 size_t render_input_num_channels,
2182 size_t render_output_num_channels,
2183 size_t capture_input_num_channels,
2184 size_t capture_output_num_channels) {
2185 rtc::StringBuilder ss;
2186 ss << "Sample rates:"
Jonas Olssonb2b20312020-01-14 12:11:31 +01002187 "\n"
2188 " Render input: "
2189 << render_input_sample_rate_hz
2190 << " Hz"
2191 "\n"
2192 " Render output: "
2193 << render_output_sample_rate_hz
2194 << " Hz"
2195 "\n"
2196 " Capture input: "
2197 << capture_input_sample_rate_hz
2198 << " Hz"
2199 "\n"
2200 " Capture output: "
2201 << capture_output_sample_rate_hz
2202 << " Hz"
2203 "\n"
2204 "Number of channels:"
2205 "\n"
2206 " Render input: "
2207 << render_input_num_channels
Per Åhgren3e8bf282019-08-29 23:38:40 +02002208 << "\n"
Jonas Olssonb2b20312020-01-14 12:11:31 +01002209 " Render output: "
2210 << render_output_num_channels
Per Åhgren3e8bf282019-08-29 23:38:40 +02002211 << "\n"
Jonas Olssonb2b20312020-01-14 12:11:31 +01002212 " Capture input: "
2213 << capture_input_num_channels
Per Åhgren3e8bf282019-08-29 23:38:40 +02002214 << "\n"
Jonas Olssonb2b20312020-01-14 12:11:31 +01002215 " Capture output: "
2216 << capture_output_num_channels;
Per Åhgren3e8bf282019-08-29 23:38:40 +02002217 return ss.Release();
2218}
2219
2220// Validates that running the audio processing module using various combinations
2221// of sample rates and number of channels works as intended.
2222void RunApmRateAndChannelTest(
2223 rtc::ArrayView<const int> sample_rates_hz,
2224 rtc::ArrayView<const int> render_channel_counts,
2225 rtc::ArrayView<const int> capture_channel_counts) {
2226 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2227 webrtc::AudioProcessing::Config apm_config;
2228 apm_config.echo_canceller.enabled = true;
2229 apm->ApplyConfig(apm_config);
2230
2231 StreamConfig render_input_stream_config;
2232 StreamConfig render_output_stream_config;
2233 StreamConfig capture_input_stream_config;
2234 StreamConfig capture_output_stream_config;
2235
2236 std::vector<float> render_input_frame_channels;
2237 std::vector<float*> render_input_frame;
2238 std::vector<float> render_output_frame_channels;
2239 std::vector<float*> render_output_frame;
2240 std::vector<float> capture_input_frame_channels;
2241 std::vector<float*> capture_input_frame;
2242 std::vector<float> capture_output_frame_channels;
2243 std::vector<float*> capture_output_frame;
2244
2245 for (auto render_input_sample_rate_hz : sample_rates_hz) {
2246 for (auto render_output_sample_rate_hz : sample_rates_hz) {
2247 for (auto capture_input_sample_rate_hz : sample_rates_hz) {
2248 for (auto capture_output_sample_rate_hz : sample_rates_hz) {
2249 for (size_t render_input_num_channels : render_channel_counts) {
2250 for (size_t capture_input_num_channels : capture_channel_counts) {
2251 size_t render_output_num_channels = render_input_num_channels;
2252 size_t capture_output_num_channels = capture_input_num_channels;
2253 auto populate_audio_frame = [](int sample_rate_hz,
2254 size_t num_channels,
2255 StreamConfig* cfg,
2256 std::vector<float>* channels_data,
2257 std::vector<float*>* frame_data) {
2258 cfg->set_sample_rate_hz(sample_rate_hz);
2259 cfg->set_num_channels(num_channels);
2260 cfg->set_has_keyboard(false);
2261
2262 size_t max_frame_size = ceil(sample_rate_hz / 100.f);
2263 channels_data->resize(num_channels * max_frame_size);
2264 std::fill(channels_data->begin(), channels_data->end(), 0.5f);
2265 frame_data->resize(num_channels);
2266 for (size_t channel = 0; channel < num_channels; ++channel) {
2267 (*frame_data)[channel] =
2268 &(*channels_data)[channel * max_frame_size];
2269 }
2270 };
2271
2272 populate_audio_frame(
2273 render_input_sample_rate_hz, render_input_num_channels,
2274 &render_input_stream_config, &render_input_frame_channels,
2275 &render_input_frame);
2276 populate_audio_frame(
2277 render_output_sample_rate_hz, render_output_num_channels,
2278 &render_output_stream_config, &render_output_frame_channels,
2279 &render_output_frame);
2280 populate_audio_frame(
2281 capture_input_sample_rate_hz, capture_input_num_channels,
2282 &capture_input_stream_config, &capture_input_frame_channels,
2283 &capture_input_frame);
2284 populate_audio_frame(
2285 capture_output_sample_rate_hz, capture_output_num_channels,
2286 &capture_output_stream_config, &capture_output_frame_channels,
2287 &capture_output_frame);
2288
2289 for (size_t frame = 0; frame < 2; ++frame) {
2290 SCOPED_TRACE(ProduceDebugText(
2291 render_input_sample_rate_hz, render_output_sample_rate_hz,
2292 capture_input_sample_rate_hz, capture_output_sample_rate_hz,
2293 render_input_num_channels, render_output_num_channels,
2294 render_input_num_channels, capture_output_num_channels));
2295
2296 int result = apm->ProcessReverseStream(
2297 &render_input_frame[0], render_input_stream_config,
2298 render_output_stream_config, &render_output_frame[0]);
2299 EXPECT_EQ(result, AudioProcessing::kNoError);
2300 result = apm->ProcessStream(
2301 &capture_input_frame[0], capture_input_stream_config,
2302 capture_output_stream_config, &capture_output_frame[0]);
2303 EXPECT_EQ(result, AudioProcessing::kNoError);
2304 }
2305 }
2306 }
2307 }
2308 }
2309 }
2310 }
2311}
2312
niklase@google.com470e71d2011-07-07 08:21:25 +00002313} // namespace
peahc19f3122016-10-07 14:54:10 -07002314
Alessio Bazzicac054e782018-04-16 12:10:09 +02002315TEST(RuntimeSettingTest, TestDefaultCtor) {
2316 auto s = AudioProcessing::RuntimeSetting();
2317 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2318}
2319
2320TEST(RuntimeSettingTest, TestCapturePreGain) {
2321 using Type = AudioProcessing::RuntimeSetting::Type;
2322 {
2323 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2324 EXPECT_EQ(Type::kCapturePreGain, s.type());
2325 float v;
2326 s.GetFloat(&v);
2327 EXPECT_EQ(1.25f, v);
2328 }
2329
2330#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2331 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2332#endif
2333}
2334
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002335TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2336 using Type = AudioProcessing::RuntimeSetting::Type;
2337 {
2338 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2339 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2340 float v;
2341 s.GetFloat(&v);
2342 EXPECT_EQ(1.25f, v);
2343 }
2344
2345#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2346 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2347#endif
2348}
2349
Alessio Bazzicac054e782018-04-16 12:10:09 +02002350TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2351 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2352 auto s = AudioProcessing::RuntimeSetting();
2353 ASSERT_TRUE(q.Insert(&s));
2354 ASSERT_TRUE(q.Remove(&s));
2355 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2356}
2357
Sam Zackrisson0beac582017-09-25 12:04:02 +02002358TEST(ApmConfiguration, EnablePostProcessing) {
2359 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002360 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002361 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002362 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002363 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002364 rtc::scoped_refptr<AudioProcessing> apm =
2365 AudioProcessingBuilder()
2366 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002367 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002368
2369 AudioFrame audio;
2370 audio.num_channels_ = 1;
2371 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2372
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002373 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002374 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002375}
2376
Alex Loiko5825aa62017-12-18 16:02:40 +01002377TEST(ApmConfiguration, EnablePreProcessing) {
2378 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002379 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002380 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002381 auto mock_pre_processor =
2382 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002383 rtc::scoped_refptr<AudioProcessing> apm =
2384 AudioProcessingBuilder()
2385 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002386 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002387
2388 AudioFrame audio;
2389 audio.num_channels_ = 1;
2390 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2391
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002392 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002393 apm->ProcessReverseStream(&audio);
2394}
2395
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002396TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2397 // Verify that apm uses a capture analyzer if one is provided.
2398 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002399 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002400 auto mock_capture_analyzer =
2401 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2402 rtc::scoped_refptr<AudioProcessing> apm =
2403 AudioProcessingBuilder()
2404 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2405 .Create();
2406
2407 AudioFrame audio;
2408 audio.num_channels_ = 1;
2409 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2410
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002411 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002412 apm->ProcessStream(&audio);
2413}
2414
Alex Loiko73ec0192018-05-15 10:52:28 +02002415TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2416 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002417 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002418 auto mock_pre_processor =
2419 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2420 rtc::scoped_refptr<AudioProcessing> apm =
2421 AudioProcessingBuilder()
2422 .SetRenderPreProcessing(std::move(mock_pre_processor))
2423 .Create();
2424 apm->SetRuntimeSetting(
2425 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2426
2427 // RuntimeSettings forwarded during 'Process*Stream' calls.
2428 // Therefore we have to make one such call.
2429 AudioFrame audio;
2430 audio.num_channels_ = 1;
2431 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2432
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002433 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2434 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002435 apm->ProcessReverseStream(&audio);
2436}
2437
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002438class MyEchoControlFactory : public EchoControlFactory {
2439 public:
2440 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2441 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002442 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2443 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
Per Åhgrenc20a19c2019-11-13 11:12:29 +01002444 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_, ::testing::_))
2445 .Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002446 return std::unique_ptr<EchoControl>(ec);
2447 }
Per Åhgrence202a02019-09-02 17:01:19 +02002448
2449 std::unique_ptr<EchoControl> Create(int sample_rate_hz,
Per Åhgren4e5c7092019-11-01 20:44:11 +01002450 int num_render_channels,
2451 int num_capture_channels) {
Per Åhgrence202a02019-09-02 17:01:19 +02002452 return Create(sample_rate_hz);
2453 }
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002454};
2455
2456TEST(ApmConfiguration, EchoControlInjection) {
2457 // Verify that apm uses an injected echo controller if one is provided.
2458 webrtc::Config webrtc_config;
2459 std::unique_ptr<EchoControlFactory> echo_control_factory(
2460 new MyEchoControlFactory());
2461
Alex Loiko5825aa62017-12-18 16:02:40 +01002462 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002463 AudioProcessingBuilder()
2464 .SetEchoControlFactory(std::move(echo_control_factory))
2465 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002466
2467 AudioFrame audio;
2468 audio.num_channels_ = 1;
2469 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2470 apm->ProcessStream(&audio);
2471 apm->ProcessReverseStream(&audio);
2472 apm->ProcessStream(&audio);
2473}
Ivo Creusenae026092017-11-20 13:07:16 +01002474
Per Åhgren8607f842019-04-12 22:02:26 +02002475std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002476 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002477 std::unique_ptr<AudioProcessing> apm(
2478 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002479 if (!apm) {
2480 return apm;
2481 }
2482
2483 ProcessingConfig processing_config = {
2484 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2485
2486 if (apm->Initialize(processing_config) != 0) {
2487 return nullptr;
2488 }
2489
2490 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002491 AudioProcessing::Config apm_config;
2492 apm_config.residual_echo_detector.enabled = true;
2493 apm_config.high_pass_filter.enabled = false;
Sam Zackrisson41478c72019-10-15 10:10:26 +02002494 apm_config.gain_controller1.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002495 apm_config.gain_controller2.enabled = false;
2496 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002497 apm_config.echo_canceller.mobile_mode = mobile_aec;
saza0bad15f2019-10-16 11:46:11 +02002498 apm_config.noise_suppression.enabled = false;
2499 apm_config.level_estimation.enabled = false;
2500 apm_config.voice_detection.enabled = false;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002501 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002502 return apm;
2503}
2504
2505#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2506#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2507#else
2508#define MAYBE_ApmStatistics ApmStatistics
2509#endif
2510
Per Åhgren8607f842019-04-12 22:02:26 +02002511TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2512 // Set up APM with AEC3 and process some audio.
2513 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002514 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002515 AudioProcessing::Config apm_config;
2516 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002517 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002518
2519 // Set up an audioframe.
2520 AudioFrame frame;
2521 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002522 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002523
2524 // Fill the audio frame with a sawtooth pattern.
2525 int16_t* ptr = frame.mutable_data();
2526 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2527 ptr[i] = 10000 * ((i % 3) - 1);
2528 }
2529
2530 // Do some processing.
2531 for (int i = 0; i < 200; i++) {
2532 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2533 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2534 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2535 }
2536
2537 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002538 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002539 // We expect all statistics to be set and have a sensible value.
2540 ASSERT_TRUE(stats.residual_echo_likelihood);
2541 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2542 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2543 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2544 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2545 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2546 ASSERT_TRUE(stats.echo_return_loss);
2547 EXPECT_NE(*stats.echo_return_loss, -100.0);
2548 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2549 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002550}
2551
2552TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2553 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002554 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002555 ASSERT_TRUE(apm);
2556
2557 // Set up an audioframe.
2558 AudioFrame frame;
2559 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002560 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002561
2562 // Fill the audio frame with a sawtooth pattern.
2563 int16_t* ptr = frame.mutable_data();
2564 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2565 ptr[i] = 10000 * ((i % 3) - 1);
2566 }
2567
2568 // Do some processing.
2569 for (int i = 0; i < 200; i++) {
2570 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2571 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2572 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2573 }
2574
2575 // Test statistics interface.
Per Åhgrencf4c8722019-12-30 14:32:14 +01002576 AudioProcessingStats stats = apm->GetStatistics();
Ivo Creusenae026092017-11-20 13:07:16 +01002577 // We expect only the residual echo detector statistics to be set and have a
2578 // sensible value.
2579 EXPECT_TRUE(stats.residual_echo_likelihood);
2580 if (stats.residual_echo_likelihood) {
2581 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2582 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2583 }
2584 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2585 if (stats.residual_echo_likelihood_recent_max) {
2586 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2587 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2588 }
2589 EXPECT_FALSE(stats.echo_return_loss);
2590 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002591}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002592
2593TEST(ApmStatistics, ReportOutputRmsDbfs) {
2594 ProcessingConfig processing_config = {
2595 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2596 AudioProcessing::Config config;
2597
2598 // Set up an audioframe.
2599 AudioFrame frame;
2600 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002601 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002602
2603 // Fill the audio frame with a sawtooth pattern.
2604 int16_t* ptr = frame.mutable_data();
2605 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2606 ptr[i] = 10000 * ((i % 3) - 1);
2607 }
2608
2609 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2610 apm->Initialize(processing_config);
2611
2612 // If not enabled, no metric should be reported.
2613 EXPECT_EQ(apm->ProcessStream(&frame), 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002614 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002615
2616 // If enabled, metrics should be reported.
2617 config.level_estimation.enabled = true;
2618 apm->ApplyConfig(config);
2619 EXPECT_EQ(apm->ProcessStream(&frame), 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002620 auto stats = apm->GetStatistics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002621 EXPECT_TRUE(stats.output_rms_dbfs);
2622 EXPECT_GE(*stats.output_rms_dbfs, 0);
2623
2624 // If re-disabled, the value is again not reported.
2625 config.level_estimation.enabled = false;
2626 apm->ApplyConfig(config);
2627 EXPECT_EQ(apm->ProcessStream(&frame), 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002628 EXPECT_FALSE(apm->GetStatistics().output_rms_dbfs);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002629}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002630
2631TEST(ApmStatistics, ReportHasVoice) {
2632 ProcessingConfig processing_config = {
2633 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2634 AudioProcessing::Config config;
2635
2636 // Set up an audioframe.
2637 AudioFrame frame;
2638 frame.num_channels_ = 1;
2639 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2640
2641 // Fill the audio frame with a sawtooth pattern.
2642 int16_t* ptr = frame.mutable_data();
2643 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2644 ptr[i] = 10000 * ((i % 3) - 1);
2645 }
2646
2647 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2648 apm->Initialize(processing_config);
2649
2650 // If not enabled, no metric should be reported.
2651 EXPECT_EQ(apm->ProcessStream(&frame), 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002652 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002653
2654 // If enabled, metrics should be reported.
2655 config.voice_detection.enabled = true;
2656 apm->ApplyConfig(config);
2657 EXPECT_EQ(apm->ProcessStream(&frame), 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002658 auto stats = apm->GetStatistics();
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002659 EXPECT_TRUE(stats.voice_detected);
2660
2661 // If re-disabled, the value is again not reported.
2662 config.voice_detection.enabled = false;
2663 apm->ApplyConfig(config);
2664 EXPECT_EQ(apm->ProcessStream(&frame), 0);
Per Åhgrencf4c8722019-12-30 14:32:14 +01002665 EXPECT_FALSE(apm->GetStatistics().voice_detected);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002666}
Per Åhgren3e8bf282019-08-29 23:38:40 +02002667
2668TEST(ApmConfiguration, HandlingOfRateAndChannelCombinations) {
2669 std::array<int, 3> sample_rates_hz = {16000, 32000, 48000};
2670 std::array<int, 2> render_channel_counts = {1, 7};
2671 std::array<int, 2> capture_channel_counts = {1, 7};
2672 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2673 capture_channel_counts);
2674}
2675
2676TEST(ApmConfiguration, HandlingOfChannelCombinations) {
2677 std::array<int, 1> sample_rates_hz = {48000};
2678 std::array<int, 8> render_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2679 std::array<int, 8> capture_channel_counts = {1, 2, 3, 4, 5, 6, 7, 8};
2680 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2681 capture_channel_counts);
2682}
2683
2684TEST(ApmConfiguration, HandlingOfRateCombinations) {
2685 std::array<int, 9> sample_rates_hz = {8000, 11025, 16000, 22050, 32000,
2686 48000, 96000, 192000, 384000};
2687 std::array<int, 1> render_channel_counts = {2};
2688 std::array<int, 1> capture_channel_counts = {2};
2689 RunApmRateAndChannelTest(sample_rates_hz, render_channel_counts,
2690 capture_channel_counts);
2691}
2692
Yves Gerey1fce3f82019-12-05 17:45:31 +01002693TEST(ApmConfiguration, SelfAssignment) {
2694 // At some point memory sanitizer was complaining about self-assigment.
2695 // Make sure we don't regress.
2696 AudioProcessing::Config config;
2697 AudioProcessing::Config* config2 = &config;
2698 *config2 = *config2; // Workaround -Wself-assign-overloaded
2699 SUCCEED(); // Real success is absence of defects from asan/msan/ubsan.
2700}
2701
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002702} // namespace webrtc