blob: 831799f77548753f58ab609aadef6b29a738d9bd [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Jonas Olssona4d87372019-07-05 19:08:33 +020010#include "modules/audio_processing/include/audio_processing.h"
11
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000012#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000013#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080014
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000015#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010016#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000017#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080018#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000019#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_audio/include/audio_util.h"
22#include "common_audio/resampler/include/push_resampler.h"
23#include "common_audio/resampler/push_sinc_resampler.h"
24#include "common_audio/signal_processing/include/signal_processing_library.h"
25#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
26#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/common.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020028#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/test/protobuf_utils.h"
30#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/arraysize.h"
32#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/gtest_prod_util.h"
35#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010036#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010037#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020040#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020041#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020042#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010043#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070047
48RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000050#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000053#endif
kwiberg77eab702016-09-28 17:42:01 -070054RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000055
andrew@webrtc.org27c69802014-02-18 20:24:56 +000056namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000057namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000058
ekmeyerson60d9b332015-08-14 10:35:55 -070059// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
60// applicable.
61
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000062// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000063// re-implemented; see comment at the bottom of this file. For now, the user has
64// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000065// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000066// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000067// be set to true with the command-line switch --write_ref_data.
68bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070069const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070070const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000071
aluebseb3603b2016-04-20 15:27:58 -070072#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
73// Android doesn't support 48kHz.
74const int kProcessSampleRates[] = {8000, 16000, 32000};
75#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070076const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070077#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000078
ekmeyerson60d9b332015-08-14 10:35:55 -070079enum StreamDirection { kForward = 0, kReverse };
80
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
Jonas Olssona4d87372019-07-05 19:08:33 +020082 ChannelBuffer<int16_t> cb_int(cb->num_frames(), cb->num_channels());
83 Deinterleave(int_data, cb->num_frames(), cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000084 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080085 for (size_t i = 0; i < cb->num_channels(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +020086 S16ToFloat(cb_int.channels()[i], cb->num_frames(), cb->channels()[i]);
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000088}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000089
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000090void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070091 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092}
93
andrew@webrtc.org103657b2014-04-24 18:28:56 +000094// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080095size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096 switch (layout) {
97 case AudioProcessing::kMono:
98 return 1;
99 case AudioProcessing::kMonoAndKeyboard:
100 case AudioProcessing::kStereo:
101 return 2;
102 case AudioProcessing::kStereoAndKeyboard:
103 return 3;
104 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700105 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800106 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000107}
108
Jonas Olssona4d87372019-07-05 19:08:33 +0200109void MixStereoToMono(const float* stereo,
110 float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800111 size_t samples_per_channel) {
112 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000114}
115
Jonas Olssona4d87372019-07-05 19:08:33 +0200116void MixStereoToMono(const int16_t* stereo,
117 int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
121}
122
pkasting25702cb2016-01-08 13:50:27 -0800123void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000125 stereo[i * 2 + 1] = stereo[i * 2];
126 }
127}
128
yujo36b1a5f2017-06-12 12:45:32 -0700129void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
132 }
133}
134
135void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700136 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700137 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
138 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700139 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000140 }
141}
142
143void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800144 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700145 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700147 frame_data[i] = left;
148 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000149 }
150}
151
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000152void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700154 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
155 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700156 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000157 }
158}
159
andrew@webrtc.org81865342012-10-27 00:28:27 +0000160bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000161 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000162 return false;
163 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000164 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165 return false;
166 }
yujo36b1a5f2017-06-12 12:45:32 -0700167 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
172 return true;
173}
174
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000175void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200176 AudioProcessing::Config apm_config = ap->GetConfig();
177 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000178#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200179 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100180
181 apm_config.gain_controller1.enabled = true;
182 apm_config.gain_controller1.mode =
183 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200185 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100187 apm_config.gain_controller1.enabled = true;
188 apm_config.gain_controller1.mode =
189 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
190 apm_config.gain_controller1.analog_level_minimum = 0;
191 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000192#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000193
peah8271d042016-11-22 07:24:52 -0800194 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100195 apm_config.level_estimation.enabled = true;
peah8271d042016-11-22 07:24:52 -0800196 ap->ApplyConfig(apm_config);
197
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000198 EXPECT_NOERR(ap->level_estimator()->Enable(true));
199 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
200
201 EXPECT_NOERR(ap->voice_detection()->Enable(true));
202}
203
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000204// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000205template <class T>
206T AbsValue(T a) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200207 return a > 0 ? a : -a;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000208}
209
210int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800211 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700212 const int16_t* frame_data = frame.data();
213 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800214 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700215 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000216 }
217
218 return max_data;
219}
220
Alex Loiko890988c2017-08-31 10:25:48 +0200221void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700222 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000223 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000224 ASSERT_TRUE(file != NULL);
225
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100226 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000227 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800228 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000229 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000231 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232 ASSERT_EQ(static_cast<size_t>(size),
Jonas Olssona4d87372019-07-05 19:08:33 +0200233 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 fclose(file);
235}
236
Alex Loiko890988c2017-08-31 10:25:48 +0200237std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200238 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000239 // Resource files are all stereo.
240 ss << name << sample_rate_hz / 1000 << "_stereo";
241 return test::ResourcePath(ss.str(), "pcm");
242}
243
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000244// Temporary filenames unique to this process. Used to be able to run these
245// tests in parallel as each process needs to be running in isolation they can't
246// have competing filenames.
247std::map<std::string, std::string> temp_filenames;
248
Alex Loiko890988c2017-08-31 10:25:48 +0200249std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000250 int input_rate,
251 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700252 int reverse_input_rate,
253 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800254 size_t num_input_channels,
255 size_t num_output_channels,
256 size_t num_reverse_input_channels,
257 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700258 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200259 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700260 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
261 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000262 if (num_output_channels == 1) {
263 ss << "mono";
264 } else if (num_output_channels == 2) {
265 ss << "stereo";
266 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700267 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000268 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700269 ss << output_rate / 1000;
270 if (num_reverse_output_channels == 1) {
271 ss << "_rmono";
272 } else if (num_reverse_output_channels == 2) {
273 ss << "_rstereo";
274 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700275 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700276 }
277 ss << reverse_output_rate / 1000;
278 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000280 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700281 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000282 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
283 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000284}
285
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000286void ClearTempFiles() {
287 for (auto& kv : temp_filenames)
288 remove(kv.second.c_str());
289}
290
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200291// Only remove "out" files. Keep "ref" files.
292void ClearTempOutFiles() {
293 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
294 const std::string& filename = it->first;
295 if (filename.substr(0, 3).compare("out") == 0) {
296 remove(it->second.c_str());
297 temp_filenames.erase(it++);
298 } else {
299 it++;
300 }
301 }
302}
303
Alex Loiko890988c2017-08-31 10:25:48 +0200304void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000305 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000306 ASSERT_TRUE(file != NULL);
307 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000308 fclose(file);
309}
310
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000311// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
312// stereo) file, converts to deinterleaved float (optionally downmixing) and
313// returns the result in |cb|. Returns false if the file ended (or on error) and
314// true otherwise.
315//
316// |int_data| and |float_data| are just temporary space that must be
317// sufficiently large to hold the 10 ms chunk.
Jonas Olssona4d87372019-07-05 19:08:33 +0200318bool ReadChunk(FILE* file,
319 int16_t* int_data,
320 float* float_data,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000321 ChannelBuffer<float>* cb) {
322 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000323 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000324 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
325 if (read_count != frame_size) {
326 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700327 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000328 return false; // This is expected.
329 }
330
331 S16ToFloat(int_data, frame_size, float_data);
332 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000333 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334 } else {
Jonas Olssona4d87372019-07-05 19:08:33 +0200335 Deinterleave(float_data, cb->num_frames(), 2, cb->channels());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000336 }
337
338 return true;
339}
340
niklase@google.com470e71d2011-07-07 08:21:25 +0000341class ApmTest : public ::testing::Test {
342 protected:
343 ApmTest();
344 virtual void SetUp();
345 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000346
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200347 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000348
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200349 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000350
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000351 // Used to select between int and float interface tests.
Jonas Olssona4d87372019-07-05 19:08:33 +0200352 enum Format { kIntFormat, kFloatFormat };
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000353
354 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000355 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000356 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800357 size_t num_input_channels,
358 size_t num_output_channels,
359 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000360 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000361 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000362 void EnableAllComponents();
363 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000364 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000365 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200366 void ReadFrameWithRewind(FILE* file,
367 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000368 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000369 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
Jonas Olssona4d87372019-07-05 19:08:33 +0200370 void ProcessDelayVerificationTest(int delay_ms,
371 int system_delay_ms,
372 int delay_min,
373 int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700374 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800375 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700376 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800377 void TestChangingForwardChannels(size_t num_in_channels,
378 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700379 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800380 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700381 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000382 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
383 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000384 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000385 int ProcessStreamChooser(Format format);
386 int AnalyzeReverseStreamChooser(Format format);
387 void ProcessDebugDump(const std::string& in_filename,
388 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800389 Format format,
390 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000391 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000392
393 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000394 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800395 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000396 AudioFrame* frame_;
397 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800398 std::unique_ptr<ChannelBuffer<float> > float_cb_;
399 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000400 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800401 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 FILE* far_file_;
403 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000404 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405};
406
407ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000408 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000409#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200410 ref_filename_(
411 test::ResourcePath("audio_processing/output_data_fixed", "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000412#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +0200413 ref_filename_(
414 test::ResourcePath("audio_processing/output_data_float", "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000415#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000416 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000417 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000418 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000419 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000420 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000421 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000422 out_file_(NULL) {
423 Config config;
424 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100425 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000426}
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
428void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000429 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
431 frame_ = new AudioFrame();
432 revframe_ = new AudioFrame();
433
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000434 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
437void ApmTest::TearDown() {
438 if (frame_) {
439 delete frame_;
440 }
441 frame_ = NULL;
442
443 if (revframe_) {
444 delete revframe_;
445 }
446 revframe_ = NULL;
447
448 if (far_file_) {
449 ASSERT_EQ(0, fclose(far_file_));
450 }
451 far_file_ = NULL;
452
453 if (near_file_) {
454 ASSERT_EQ(0, fclose(near_file_));
455 }
456 near_file_ = NULL;
457
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000458 if (out_file_) {
459 ASSERT_EQ(0, fclose(out_file_));
460 }
461 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000464void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000465 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700466 ap->Initialize(
467 {{{frame_->sample_rate_hz_, frame_->num_channels_},
468 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700469 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700470 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000471}
472
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000473void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000474 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000475 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800476 size_t num_input_channels,
477 size_t num_output_channels,
478 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000479 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000480 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000481 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000482 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000483
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
485 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000486 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000487
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000488 if (far_file_) {
489 ASSERT_EQ(0, fclose(far_file_));
490 }
491 std::string filename = ResourceFilePath("far", sample_rate_hz);
492 far_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200493 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000494
495 if (near_file_) {
496 ASSERT_EQ(0, fclose(near_file_));
497 }
498 filename = ResourceFilePath("near", sample_rate_hz);
499 near_file_ = fopen(filename.c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200500 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000501
502 if (open_output_file) {
503 if (out_file_) {
504 ASSERT_EQ(0, fclose(out_file_));
505 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700506 filename = OutputFilePath(
507 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
508 reverse_sample_rate_hz, num_input_channels, num_output_channels,
509 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000510 out_file_ = fopen(filename.c_str(), "wb");
Jonas Olssona4d87372019-07-05 19:08:33 +0200511 ASSERT_TRUE(out_file_ != NULL)
512 << "Could not open file " << filename << "\n";
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000513 }
514}
515
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000516void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000517 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000518}
519
Jonas Olssona4d87372019-07-05 19:08:33 +0200520bool ApmTest::ReadFrame(FILE* file,
521 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000522 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000523 // The files always contain stereo audio.
524 size_t frame_size = frame->samples_per_channel_ * 2;
Jonas Olssona4d87372019-07-05 19:08:33 +0200525 size_t read_count =
526 fread(frame->mutable_data(), sizeof(int16_t), frame_size, file);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000527 if (read_count != frame_size) {
528 // Check that the file really ended.
529 EXPECT_NE(0, feof(file));
530 return false; // This is expected.
531 }
532
533 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700534 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000535 frame->samples_per_channel_);
536 }
537
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000538 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000539 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000541 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000542}
543
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000544bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
545 return ReadFrame(file, frame, NULL);
546}
547
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000548// If the end of the file has been reached, rewind it and attempt to read the
549// frame again.
Jonas Olssona4d87372019-07-05 19:08:33 +0200550void ApmTest::ReadFrameWithRewind(FILE* file,
551 AudioFrame* frame,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552 ChannelBuffer<float>* cb) {
553 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000554 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000556 }
557}
558
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
560 ReadFrameWithRewind(file, frame, NULL);
561}
562
andrew@webrtc.org81865342012-10-27 00:28:27 +0000563void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
564 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Jonas Olssona4d87372019-07-05 19:08:33 +0200565 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000566 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000567}
568
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000569int ApmTest::ProcessStreamChooser(Format format) {
570 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571 return apm_->ProcessStream(frame_);
572 }
Jonas Olssona4d87372019-07-05 19:08:33 +0200573 return apm_->ProcessStream(
574 float_cb_->channels(), frame_->samples_per_channel_,
575 frame_->sample_rate_hz_, LayoutFromChannels(frame_->num_channels_),
576 output_sample_rate_hz_, LayoutFromChannels(num_output_channels_),
577 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000578}
579
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000580int ApmTest::AnalyzeReverseStreamChooser(Format format) {
581 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700582 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000583 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000584 return apm_->AnalyzeReverseStream(
Jonas Olssona4d87372019-07-05 19:08:33 +0200585 revfloat_cb_->channels(), revframe_->samples_per_channel_,
586 revframe_->sample_rate_hz_, LayoutFromChannels(revframe_->num_channels_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587}
588
Jonas Olssona4d87372019-07-05 19:08:33 +0200589void ApmTest::ProcessDelayVerificationTest(int delay_ms,
590 int system_delay_ms,
591 int delay_min,
592 int delay_max) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000593 // The |revframe_| and |frame_| should include the proper frame information,
594 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000595 AudioFrame tmp_frame;
596 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000597 bool causal = true;
598
599 tmp_frame.CopyFrom(*revframe_);
600 SetFrameTo(&tmp_frame, 0);
601
602 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
603 // Initialize the |frame_queue| with empty frames.
604 int frame_delay = delay_ms / 10;
605 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000606 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000607 frame->CopyFrom(tmp_frame);
608 frame_queue.push(frame);
609 frame_delay++;
610 causal = false;
611 }
612 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000613 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000614 frame->CopyFrom(tmp_frame);
615 frame_queue.push(frame);
616 frame_delay--;
617 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000618 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
619 // need enough frames with audio to have reliable estimates, but as few as
620 // possible to keep processing time down. 4.5 seconds seemed to be a good
621 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000622 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000623 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000624 frame->CopyFrom(tmp_frame);
625 // Use the near end recording, since that has more speech in it.
626 ASSERT_TRUE(ReadFrame(near_file_, frame));
627 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000628 AudioFrame* reverse_frame = frame;
629 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000630 if (!causal) {
631 reverse_frame = frame_queue.front();
632 // When we call ProcessStream() the frame is modified, so we can't use the
633 // pointer directly when things are non-causal. Use an intermediate frame
634 // and copy the data.
635 process_frame = &tmp_frame;
636 process_frame->CopyFrom(*frame);
637 }
aluebsb0319552016-03-17 20:39:53 -0700638 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
640 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
641 frame = frame_queue.front();
642 frame_queue.pop();
643 delete frame;
644
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000645 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200647 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000648 }
649 }
650
651 rewind(near_file_);
652 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000653 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000654 frame_queue.pop();
655 delete frame;
656 }
657 // Calculate expected delay estimate and acceptable regions. Further,
658 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700659 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700660 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700661 const int expected_median =
662 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
663 const int expected_median_high = rtc::SafeClamp<int>(
664 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700665 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700666 const int expected_median_low = rtc::SafeClamp<int>(
667 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700668 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000669 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200670 AudioProcessingStats stats =
671 apm_->GetStatistics(true /* has_remote_tracks */);
672 ASSERT_TRUE(stats.delay_median_ms.has_value());
673 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000674 EXPECT_GE(expected_median_high, median);
675 EXPECT_LE(expected_median_low, median);
676}
677
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000678void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000679 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000680 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000682 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000683 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
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().
Jonas Olssona4d87372019-07-05 19:08:33 +0200687 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->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 AudioProcessing::Config apm_config = apm_->GetConfig();
693 apm_config.echo_canceller.enabled = true;
694 apm_config.echo_canceller.mobile_mode = false;
695 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000696 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200697 EXPECT_EQ(apm_->kStreamParameterNotSetError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000698 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
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.
710 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
Jonas Olssona4d87372019-07-05 19:08:33 +0200711 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_stream_analog_level(127));
Per Åhgren200feba2019-03-06 04:16:46 +0100712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
714
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000715 // -- No stream parameters --
Jonas Olssona4d87372019-07-05 19:08:33 +0200716 EXPECT_EQ(apm_->kNoError, AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000718
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Jonas Olssona4d87372019-07-05 19:08:33 +0200721 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000723}
724
725TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000726 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000727}
728
729TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000731}
732
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000733TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
734 EXPECT_EQ(0, apm_->delay_offset_ms());
735 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
736 EXPECT_EQ(50, apm_->stream_delay_ms());
737}
738
739TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
740 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000741 apm_->set_delay_offset_ms(100);
742 EXPECT_EQ(100, apm_->delay_offset_ms());
743 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000744 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000745 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
746 EXPECT_EQ(200, apm_->stream_delay_ms());
747
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000748 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000749 apm_->set_delay_offset_ms(-50);
750 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000751 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
752 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000753 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
754 EXPECT_EQ(50, apm_->stream_delay_ms());
755}
756
Michael Graczyk86c6d332015-07-23 11:41:39 -0700757void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800758 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700759 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000760 frame_->num_channels_ = num_channels;
761 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700762 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000763}
764
Michael Graczyk86c6d332015-07-23 11:41:39 -0700765void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800766 size_t num_in_channels,
767 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700768 AudioProcessing::Error expected_return) {
769 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
770 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
771
772 EXPECT_EQ(expected_return,
773 apm_->ProcessStream(float_cb_->channels(), input_stream,
774 output_stream, float_cb_->channels()));
775}
776
777void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800778 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700779 AudioProcessing::Error expected_return) {
780 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700781 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
782 {output_sample_rate_hz_, apm_->num_output_channels()},
783 {frame_->sample_rate_hz_, num_rev_channels},
784 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785
ekmeyerson60d9b332015-08-14 10:35:55 -0700786 EXPECT_EQ(
787 expected_return,
788 apm_->ProcessReverseStream(
789 float_cb_->channels(), processing_config.reverse_input_stream(),
790 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700791}
792
793TEST_F(ApmTest, ChannelsInt16Interface) {
794 // Testing number of invalid and valid channels.
795 Init(16000, 16000, 16000, 4, 4, 4, false);
796
797 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
798
Peter Kasting69558702016-01-12 16:26:35 -0800799 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700800 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
803}
804
Michael Graczyk86c6d332015-07-23 11:41:39 -0700805TEST_F(ApmTest, Channels) {
806 // Testing number of invalid and valid channels.
807 Init(16000, 16000, 16000, 4, 4, 4, false);
808
809 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
810 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
811
Peter Kasting69558702016-01-12 16:26:35 -0800812 for (size_t i = 1; i < 4; ++i) {
813 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 // Output channels much be one or match input channels.
815 if (j == 1 || i == j) {
816 TestChangingForwardChannels(i, j, kNoErr);
817 TestChangingReverseChannels(i, kNoErr);
818
819 EXPECT_EQ(i, apm_->num_input_channels());
820 EXPECT_EQ(j, apm_->num_output_channels());
821 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800822 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823 } else {
824 TestChangingForwardChannels(i, j,
825 AudioProcessing::kBadNumberChannelsError);
826 }
827 }
828 }
829}
830
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000831TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000833 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000834 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700836 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800837 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000838 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000839 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000840 }
841}
842
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000843TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000844 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +0000845 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +0200846 apm_->gain_control()->set_mode(apm_->gain_control()->mode()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000847
Jonas Olssona4d87372019-07-05 19:08:33 +0200848 GainControl::Mode mode[] = {GainControl::kAdaptiveAnalog,
849 GainControl::kAdaptiveDigital,
850 GainControl::kFixedDigital};
pkasting25702cb2016-01-08 13:50:27 -0800851 for (size_t i = 0; i < arraysize(mode); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200852 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_mode(mode[i]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000853 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
854 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100855 // Testing target levels
Jonas Olssona4d87372019-07-05 19:08:33 +0200856 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_target_level_dbfs(
857 apm_->gain_control()->target_level_dbfs()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000858
859 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -0800860 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000861 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +0200862 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000863 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
864 }
865
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100866 // Testing compression gains
Jonas Olssona4d87372019-07-05 19:08:33 +0200867 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_compression_gain_db(
868 apm_->gain_control()->compression_gain_db()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000869
870 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -0800871 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000872 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +0200873 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100874 ProcessStreamChooser(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
876 }
877
878 // Testing limiter off/on
879 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
880 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
881 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
882 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
883
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100884 // Testing level limits
Jonas Olssona4d87372019-07-05 19:08:33 +0200885 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_analog_level_limits(
886 apm_->gain_control()->analog_level_minimum(),
887 apm_->gain_control()->analog_level_maximum()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000888
889 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -0800890 for (size_t i = 0; i < arraysize(min_level); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200891 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->set_analog_level_limits(
892 min_level[i], 1024));
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
894 }
895
896 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -0800897 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +0200899 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
901 }
902
903 // TODO(ajm): stream_is_saturated() and stream_analog_level()
904
905 // Turn AGC off
906 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
907 EXPECT_FALSE(apm_->gain_control()->is_enabled());
908}
909
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100910#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
911TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
912 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(-1), "");
913}
914
915TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
916 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(32), "");
917}
918
919TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
920 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(-1), "");
921}
922
923TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
924 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(91), "");
925}
926
927TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
928 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(-1, 512), "");
929}
930
931TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
932 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 65536), "");
933}
934
935TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
936 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 255), "");
937}
938
939TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
940 apm_->gain_control()->set_analog_level_limits(255, 512);
941 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
942}
943
944TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
945 apm_->gain_control()->set_analog_level_limits(255, 512);
946 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
947}
948#endif
949
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000950void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000951 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000952 EXPECT_EQ(apm_->kNoError,
953 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
954 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
955
956 int out_analog_level = 0;
957 for (int i = 0; i < 2000; ++i) {
958 ReadFrameWithRewind(near_file_, frame_);
959 // Ensure the audio is at a low level, so the AGC will try to increase it.
960 ScaleFrame(frame_, 0.25);
961
962 // Always pass in the same volume.
963 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +0200964 apm_->gain_control()->set_stream_analog_level(100));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000965 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
966 out_analog_level = apm_->gain_control()->stream_analog_level();
967 }
968
969 // Ensure the AGC is still able to reach the maximum.
970 EXPECT_EQ(255, out_analog_level);
971}
972
973// Verifies that despite volume slider quantization, the AGC can continue to
974// increase its volume.
975TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -0800976 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000977 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
978 }
979}
980
981void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000982 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000983 EXPECT_EQ(apm_->kNoError,
984 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
985 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
986
987 int out_analog_level = 100;
988 for (int i = 0; i < 1000; ++i) {
989 ReadFrameWithRewind(near_file_, frame_);
990 // Ensure the audio is at a low level, so the AGC will try to increase it.
991 ScaleFrame(frame_, 0.25);
992
993 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +0200994 apm_->gain_control()->set_stream_analog_level(out_analog_level));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000995 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
996 out_analog_level = apm_->gain_control()->stream_analog_level();
997 }
998
999 // Ensure the volume was raised.
1000 EXPECT_GT(out_analog_level, 100);
1001 int highest_level_reached = out_analog_level;
1002 // Simulate a user manual volume change.
1003 out_analog_level = 100;
1004
1005 for (int i = 0; i < 300; ++i) {
1006 ReadFrameWithRewind(near_file_, frame_);
1007 ScaleFrame(frame_, 0.25);
1008
1009 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +02001010 apm_->gain_control()->set_stream_analog_level(out_analog_level));
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001011 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1012 out_analog_level = apm_->gain_control()->stream_analog_level();
1013 // Check that AGC respected the manually adjusted volume.
1014 EXPECT_LT(out_analog_level, highest_level_reached);
1015 }
1016 // Check that the volume was still raised.
1017 EXPECT_GT(out_analog_level, 100);
1018}
1019
1020TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001021 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001022 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1023 }
1024}
1025
niklase@google.com470e71d2011-07-07 08:21:25 +00001026TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001027 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001028 NoiseSuppression::Level level[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001029 NoiseSuppression::kLow, NoiseSuppression::kModerate,
1030 NoiseSuppression::kHigh, NoiseSuppression::kVeryHigh};
pkasting25702cb2016-01-08 13:50:27 -08001031 for (size_t i = 0; i < arraysize(level); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001032 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->set_level(level[i]));
niklase@google.com470e71d2011-07-07 08:21:25 +00001033 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1034 }
1035
andrew@webrtc.org648af742012-02-08 01:57:29 +00001036 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001037 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1038 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1039 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1040 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1041}
1042
1043TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001044 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001045 AudioProcessing::Config apm_config;
1046 apm_config.high_pass_filter.enabled = true;
1047 apm_->ApplyConfig(apm_config);
1048 apm_config.high_pass_filter.enabled = false;
1049 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001050}
1051
1052TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001053 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001054 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001056
1057 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1058
1059 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1060 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1061
1062 // Run this test in wideband; in super-wb, the splitting filter distorts the
1063 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001064 frame_->samples_per_channel_ = 160;
1065 frame_->num_channels_ = 2;
1066 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001067
1068 // Min value if no frames have been processed.
1069 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1070
1071 // Min value on zero frames.
1072 SetFrameTo(frame_, 0);
1073 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1074 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1075 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1076
1077 // Try a few RMS values.
1078 // (These also test that the value resets after retrieving it.)
1079 SetFrameTo(frame_, 32767);
1080 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1081 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1082 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1083
1084 SetFrameTo(frame_, 30000);
1085 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1086 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1087 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1088
1089 SetFrameTo(frame_, 10000);
1090 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1091 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1092 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1093
1094 SetFrameTo(frame_, 10);
1095 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1096 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1097 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1098
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001099 // Verify reset after enable/disable.
1100 SetFrameTo(frame_, 32767);
1101 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1102 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1103 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1104 SetFrameTo(frame_, 1);
1105 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1106 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1107
1108 // Verify reset after initialize.
1109 SetFrameTo(frame_, 32767);
1110 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1111 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1112 SetFrameTo(frame_, 1);
1113 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1114 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001115}
1116
1117TEST_F(ApmTest, VoiceDetection) {
1118 // Test external VAD
1119 EXPECT_EQ(apm_->kNoError,
1120 apm_->voice_detection()->set_stream_has_voice(true));
1121 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1122 EXPECT_EQ(apm_->kNoError,
1123 apm_->voice_detection()->set_stream_has_voice(false));
1124 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1125
andrew@webrtc.org648af742012-02-08 01:57:29 +00001126 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001127 VoiceDetection::Likelihood likelihood[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001128 VoiceDetection::kVeryLowLikelihood, VoiceDetection::kLowLikelihood,
1129 VoiceDetection::kModerateLikelihood, VoiceDetection::kHighLikelihood};
pkasting25702cb2016-01-08 13:50:27 -08001130 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 EXPECT_EQ(apm_->kNoError,
1132 apm_->voice_detection()->set_likelihood(likelihood[i]));
1133 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1134 }
1135
1136 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001137 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001138 EXPECT_EQ(apm_->kBadParameterError,
1139 apm_->voice_detection()->set_frame_size_ms(12));
1140
andrew@webrtc.org648af742012-02-08 01:57:29 +00001141 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 for (int i = 10; i <= 30; i += 10) {
1143 EXPECT_EQ(apm_->kNoError,
1144 apm_->voice_detection()->set_frame_size_ms(i));
1145 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1146 }
1147 */
1148
andrew@webrtc.org648af742012-02-08 01:57:29 +00001149 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001150 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1151 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1152 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1153 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1154
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001155 // Test that AudioFrame activity is maintained when VAD is disabled.
1156 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1157 AudioFrame::VADActivity activity[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001158 AudioFrame::kVadActive, AudioFrame::kVadPassive, AudioFrame::kVadUnknown};
pkasting25702cb2016-01-08 13:50:27 -08001159 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001160 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001161 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001162 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001163 }
1164
1165 // Test that AudioFrame activity is set when VAD is enabled.
1166 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001167 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001168 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001169 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001170
niklase@google.com470e71d2011-07-07 08:21:25 +00001171 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1172}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001173
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001174TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001175 AudioProcessing::Config config = apm_->GetConfig();
1176 EXPECT_FALSE(config.echo_canceller.enabled);
1177 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001178 EXPECT_FALSE(config.level_estimation.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001179 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001180 EXPECT_FALSE(apm_->gain_control()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001181 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1182 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1183 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1184}
1185
1186TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001187 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001188 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001189 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001190 AudioFrame frame_copy;
1191 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001192 for (int j = 0; j < 1000; j++) {
1193 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1194 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001195 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1196 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001197 }
1198 }
1199}
1200
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001201TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1202 // Test that ProcessStream copies input to output even with no processing.
1203 const size_t kSamples = 80;
1204 const int sample_rate = 8000;
Jonas Olssona4d87372019-07-05 19:08:33 +02001205 const float src[kSamples] = {-1.0f, 0.0f, 1.0f};
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001206 float dest[kSamples] = {};
1207
1208 auto src_channels = &src[0];
1209 auto dest_channels = &dest[0];
1210
Ivo Creusen62337e52018-01-09 14:17:33 +01001211 apm_.reset(AudioProcessingBuilder().Create());
Jonas Olssona4d87372019-07-05 19:08:33 +02001212 EXPECT_NOERR(apm_->ProcessStream(&src_channels, kSamples, sample_rate,
1213 LayoutFromChannels(1), sample_rate,
1214 LayoutFromChannels(1), &dest_channels));
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001215
1216 for (size_t i = 0; i < kSamples; ++i) {
1217 EXPECT_EQ(src[i], dest[i]);
1218 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001219
1220 // Same for ProcessReverseStream.
1221 float rev_dest[kSamples] = {};
1222 auto rev_dest_channels = &rev_dest[0];
1223
1224 StreamConfig input_stream = {sample_rate, 1};
1225 StreamConfig output_stream = {sample_rate, 1};
1226 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1227 output_stream, &rev_dest_channels));
1228
1229 for (size_t i = 0; i < kSamples; ++i) {
1230 EXPECT_EQ(src[i], rev_dest[i]);
1231 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001232}
1233
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001234TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1235 EnableAllComponents();
1236
pkasting25702cb2016-01-08 13:50:27 -08001237 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001238 Init(kProcessSampleRates[i], kProcessSampleRates[i], kProcessSampleRates[i],
1239 2, 2, 2, false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001240 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001241 ASSERT_EQ(0, feof(far_file_));
1242 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001243 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001244 CopyLeftToRightChannel(revframe_->mutable_data(),
1245 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001246
aluebsb0319552016-03-17 20:39:53 -07001247 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001248
yujo36b1a5f2017-06-12 12:45:32 -07001249 CopyLeftToRightChannel(frame_->mutable_data(),
1250 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001251 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1252
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001253 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001254 ASSERT_EQ(kNoErr,
Jonas Olssona4d87372019-07-05 19:08:33 +02001255 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001256 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001257 analog_level = apm_->gain_control()->stream_analog_level();
1258
yujo36b1a5f2017-06-12 12:45:32 -07001259 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001260 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001261 rewind(far_file_);
1262 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001263 }
1264}
1265
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001266TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001267 // Verify the filter is not active through undistorted audio when:
1268 // 1. No components are enabled...
1269 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001270 AudioFrame frame_copy;
1271 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001272 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1273 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1274 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1275
1276 // 2. Only the level estimator is enabled...
1277 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001278 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001279 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1280 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1281 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1282 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1283 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1284
1285 // 3. Only VAD is enabled...
1286 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001287 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001288 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1289 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1290 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1291 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1292 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1293
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001294 // 4. Only GetStatistics-reporting VAD is enabled...
1295 SetFrameTo(frame_, 1000);
1296 frame_copy.CopyFrom(*frame_);
1297 auto apm_config = apm_->GetConfig();
1298 apm_config.voice_detection.enabled = true;
1299 apm_->ApplyConfig(apm_config);
1300 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1301 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1302 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1303 apm_config.voice_detection.enabled = false;
1304 apm_->ApplyConfig(apm_config);
1305
1306 // 5. Both VADs and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001307 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001308 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001309 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1310 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001311 apm_config.voice_detection.enabled = true;
1312 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001313 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1314 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1315 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1316 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1317 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001318 apm_config.voice_detection.enabled = false;
1319 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001320
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001321 // Check the test is valid. We should have distortion from the filter
1322 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001323 apm_config.echo_canceller.enabled = true;
1324 apm_config.echo_canceller.mobile_mode = false;
1325 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001326 frame_->samples_per_channel_ = 320;
1327 frame_->num_channels_ = 2;
1328 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001329 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001330 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001331 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001332 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1333 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1334}
1335
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001336#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1337void ApmTest::ProcessDebugDump(const std::string& in_filename,
1338 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001339 Format format,
1340 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001341 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001342 FILE* in_file = fopen(in_filename.c_str(), "rb");
1343 ASSERT_TRUE(in_file != NULL);
1344 audioproc::Event event_msg;
1345 bool first_init = true;
1346
1347 while (ReadMessageFromFile(in_file, &event_msg)) {
1348 if (event_msg.type() == audioproc::Event::INIT) {
1349 const audioproc::Init msg = event_msg.init();
1350 int reverse_sample_rate = msg.sample_rate();
1351 if (msg.has_reverse_sample_rate()) {
1352 reverse_sample_rate = msg.reverse_sample_rate();
1353 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001354 int output_sample_rate = msg.sample_rate();
1355 if (msg.has_output_sample_rate()) {
1356 output_sample_rate = msg.output_sample_rate();
1357 }
1358
Jonas Olssona4d87372019-07-05 19:08:33 +02001359 Init(msg.sample_rate(), output_sample_rate, reverse_sample_rate,
1360 msg.num_input_channels(), msg.num_output_channels(),
1361 msg.num_reverse_channels(), false);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001362 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001363 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001364 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001365 auto aec_dump =
1366 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1367 EXPECT_TRUE(aec_dump);
1368 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001369 first_init = false;
1370 }
1371
1372 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1373 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1374
1375 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001376 ASSERT_EQ(revframe_->num_channels_,
1377 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001378 for (int i = 0; i < msg.channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001379 memcpy(revfloat_cb_->channels()[i], msg.channel(i).data(),
1380 msg.channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001381 }
1382 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001383 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001384 if (format == kFloatFormat) {
1385 // We're using an int16 input file; convert to float.
1386 ConvertToFloat(*revframe_, revfloat_cb_.get());
1387 }
1388 }
1389 AnalyzeReverseStreamChooser(format);
1390
1391 } else if (event_msg.type() == audioproc::Event::STREAM) {
1392 const audioproc::Stream msg = event_msg.stream();
1393 // ProcessStream could have changed this for the output frame.
1394 frame_->num_channels_ = apm_->num_input_channels();
1395
1396 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1397 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001398 if (msg.has_keypress()) {
1399 apm_->set_stream_key_pressed(msg.keypress());
1400 } else {
1401 apm_->set_stream_key_pressed(true);
1402 }
1403
1404 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001405 ASSERT_EQ(frame_->num_channels_,
1406 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001407 for (int i = 0; i < msg.input_channel_size(); ++i) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001408 memcpy(float_cb_->channels()[i], msg.input_channel(i).data(),
1409 msg.input_channel(i).size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410 }
1411 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001412 memcpy(frame_->mutable_data(), msg.input_data().data(),
1413 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001414 if (format == kFloatFormat) {
1415 // We're using an int16 input file; convert to float.
1416 ConvertToFloat(*frame_, float_cb_.get());
1417 }
1418 }
1419 ProcessStreamChooser(format);
1420 }
1421 }
aleloif4dd1912017-06-15 01:55:38 -07001422 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001423 fclose(in_file);
1424}
1425
1426void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001427 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001428 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001429 std::string format_string;
1430 switch (format) {
1431 case kIntFormat:
1432 format_string = "_int";
1433 break;
1434 case kFloatFormat:
1435 format_string = "_float";
1436 break;
1437 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001438 const std::string ref_filename = test::TempFilename(
1439 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1440 const std::string out_filename = test::TempFilename(
1441 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001442 const std::string limited_filename = test::TempFilename(
1443 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1444 const size_t logging_limit_bytes = 100000;
1445 // We expect at least this many bytes in the created logfile.
1446 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001447 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001448 ProcessDebugDump(in_filename, ref_filename, format, -1);
1449 ProcessDebugDump(ref_filename, out_filename, format, -1);
1450 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001451
1452 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1453 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001454 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001455 ASSERT_TRUE(ref_file != NULL);
1456 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001457 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001458 std::unique_ptr<uint8_t[]> ref_bytes;
1459 std::unique_ptr<uint8_t[]> out_bytes;
1460 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001461
1462 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1463 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001464 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001465 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001466 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001467 while (ref_size > 0 && out_size > 0) {
1468 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001469 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001470 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001471 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001472 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001473 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001474 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1475 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001476 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001477 }
1478 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001479 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1480 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001481 EXPECT_NE(0, feof(ref_file));
1482 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001483 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001484 ASSERT_EQ(0, fclose(ref_file));
1485 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001486 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001487 remove(ref_filename.c_str());
1488 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001489 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001490}
1491
pbosc7a65692016-05-06 12:50:04 -07001492TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001493 VerifyDebugDumpTest(kIntFormat);
1494}
1495
pbosc7a65692016-05-06 12:50:04 -07001496TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001497 VerifyDebugDumpTest(kFloatFormat);
1498}
1499#endif
1500
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001501// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001502TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001503 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001504 const std::string filename =
1505 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001506 {
1507 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1508 EXPECT_FALSE(aec_dump);
1509 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001510
1511#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1512 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001513 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001514
aleloif4dd1912017-06-15 01:55:38 -07001515 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1516 EXPECT_TRUE(aec_dump);
1517 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001518 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001519 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001520 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001521
1522 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001523 FILE* fid = fopen(filename.c_str(), "r");
1524 ASSERT_TRUE(fid != NULL);
1525
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001526 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001527 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001528 ASSERT_EQ(0, remove(filename.c_str()));
1529#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001530 // Verify the file has NOT been written.
1531 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1532#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1533}
1534
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001535// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001536TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001537 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001538
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001539 const std::string filename =
1540 test::TempFilename(test::OutputPath(), "debug_aec");
Niels Möllere8e4dc42019-06-11 14:04:16 +02001541 FileWrapper f = FileWrapper::OpenWriteOnly(filename.c_str());
1542 ASSERT_TRUE(f.is_open());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001543
1544#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1545 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001546 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001547
Niels Möllere8e4dc42019-06-11 14:04:16 +02001548 auto aec_dump = AecDumpFactory::Create(std::move(f), -1, &worker_queue);
aleloif4dd1912017-06-15 01:55:38 -07001549 EXPECT_TRUE(aec_dump);
1550 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001551 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001552 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001553 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001554
1555 // Verify the file has been written.
Niels Möllere8e4dc42019-06-11 14:04:16 +02001556 FILE* fid = fopen(filename.c_str(), "r");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001557 ASSERT_TRUE(fid != NULL);
1558
1559 // Clean it up.
1560 ASSERT_EQ(0, fclose(fid));
1561 ASSERT_EQ(0, remove(filename.c_str()));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001562#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1563}
1564
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001565// TODO(andrew): Add a test to process a few frames with different combinations
1566// of enabled components.
1567
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001568TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001569 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001570 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001571
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001572 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001573 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001574 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001575 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001576 for (size_t i = 0; i < arraysize(kChannels); i++) {
1577 for (size_t j = 0; j < arraysize(kChannels); j++) {
1578 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001579 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001580 test->set_num_reverse_channels(kChannels[i]);
1581 test->set_num_input_channels(kChannels[j]);
1582 test->set_num_output_channels(kChannels[j]);
1583 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001584 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001585 }
1586 }
1587 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001588#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1589 // To test the extended filter mode.
1590 audioproc::Test* test = ref_data.add_test();
1591 test->set_num_reverse_channels(2);
1592 test->set_num_input_channels(2);
1593 test->set_num_output_channels(2);
1594 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1595 test->set_use_aec_extended_filter(true);
1596#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001597 }
1598
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001599 for (int i = 0; i < ref_data.test_size(); i++) {
1600 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001601
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001602 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001603 // TODO(ajm): We no longer allow different input and output channels. Skip
1604 // these tests for now, but they should be removed from the set.
1605 if (test->num_input_channels() != test->num_output_channels())
1606 continue;
1607
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001608 Config config;
1609 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001610 config.Set<ExtendedFilter>(
1611 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001612 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001613
1614 EnableAllComponents();
1615
Jonas Olssona4d87372019-07-05 19:08:33 +02001616 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001617 static_cast<size_t>(test->num_input_channels()),
1618 static_cast<size_t>(test->num_output_channels()),
Jonas Olssona4d87372019-07-05 19:08:33 +02001619 static_cast<size_t>(test->num_reverse_channels()), true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001620
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001621 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001622 int has_voice_count = 0;
1623 int is_saturated_count = 0;
1624 int analog_level = 127;
1625 int analog_level_average = 0;
1626 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001627 float ns_speech_prob_average = 0.0f;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001628 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001629#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Jonas Olssona4d87372019-07-05 19:08:33 +02001630 int stats_index = 0;
minyue58530ed2016-05-24 05:50:12 -07001631#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001633 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001634 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001635
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001636 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1637
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001638 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001639 EXPECT_EQ(apm_->kNoError,
Jonas Olssona4d87372019-07-05 19:08:33 +02001640 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001641
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001642 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001643
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001644 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001645 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1646 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001647
1648 max_output_average += MaxAudioFrame(*frame_);
1649
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001650 analog_level = apm_->gain_control()->stream_analog_level();
1651 analog_level_average += analog_level;
1652 if (apm_->gain_control()->stream_is_saturated()) {
1653 is_saturated_count++;
1654 }
1655 if (apm_->voice_detection()->stream_has_voice()) {
1656 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001657 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001658 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001659 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001660 }
1661
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001662 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
Sam Zackrisson11b87032018-12-18 17:13:58 +01001663 AudioProcessingStats stats =
1664 apm_->GetStatistics(/*has_remote_tracks=*/false);
1665 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001666
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001667 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
Jonas Olssona4d87372019-07-05 19:08:33 +02001668 size_t write_count =
1669 fwrite(frame_->data(), sizeof(int16_t), frame_size, out_file_);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001670 ASSERT_EQ(frame_size, write_count);
1671
1672 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001673 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001674 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001675
1676#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1677 const int kStatsAggregationFrameNum = 100; // 1 second.
1678 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001679 // Get echo and delay metrics.
1680 AudioProcessingStats stats =
1681 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001682
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001683 // Echo metrics.
1684 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1685 const float echo_return_loss_enhancement =
1686 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001687 const float residual_echo_likelihood =
1688 stats.residual_echo_likelihood.value_or(-1.0f);
1689 const float residual_echo_likelihood_recent_max =
1690 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1691
minyue58530ed2016-05-24 05:50:12 -07001692 if (!write_ref_data) {
1693 const audioproc::Test::EchoMetrics& reference =
1694 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001695 constexpr float kEpsilon = 0.01;
1696 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1697 EXPECT_NEAR(echo_return_loss_enhancement,
1698 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001699 EXPECT_NEAR(residual_echo_likelihood,
1700 reference.residual_echo_likelihood(), kEpsilon);
1701 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1702 reference.residual_echo_likelihood_recent_max(),
1703 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001704 ++stats_index;
1705 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001706 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1707 message_echo->set_echo_return_loss(echo_return_loss);
1708 message_echo->set_echo_return_loss_enhancement(
1709 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001710 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1711 message_echo->set_residual_echo_likelihood_recent_max(
1712 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001713 }
1714 }
1715#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001716 }
1717 max_output_average /= frame_count;
1718 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001719 ns_speech_prob_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001720 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001721
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001722 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001723 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001724 // When running the test on a N7 we get a {2, 6} difference of
1725 // |has_voice_count| and |max_output_average| is up to 18 higher.
1726 // All numbers being consistently higher on N7 compare to ref_data.
1727 // TODO(bjornv): If we start getting more of these offsets on Android we
1728 // should consider a different approach. Either using one slack for all,
1729 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001730#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001731 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001732 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001733 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001734 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001735#else
1736 const int kHasVoiceCountOffset = 0;
1737 const int kHasVoiceCountNear = kIntNear;
1738 const int kMaxOutputAverageOffset = 0;
1739 const int kMaxOutputAverageNear = kIntNear;
1740#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001741 EXPECT_NEAR(test->has_voice_count(),
Jonas Olssona4d87372019-07-05 19:08:33 +02001742 has_voice_count - kHasVoiceCountOffset, kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001743 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001744
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001745 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001746 EXPECT_NEAR(test->max_output_average(),
1747 max_output_average - kMaxOutputAverageOffset,
1748 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001749#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001750 const double kFloatNear = 0.0005;
Jonas Olssona4d87372019-07-05 19:08:33 +02001751 EXPECT_NEAR(test->ns_speech_probability_average(), ns_speech_prob_average,
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001752 kFloatNear);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001753 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001754#endif
1755 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001756 test->set_has_voice_count(has_voice_count);
1757 test->set_is_saturated_count(is_saturated_count);
1758
1759 test->set_analog_level_average(analog_level_average);
1760 test->set_max_output_average(max_output_average);
1761
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001762#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001763 EXPECT_LE(0.0f, ns_speech_prob_average);
1764 EXPECT_GE(1.0f, ns_speech_prob_average);
1765 test->set_ns_speech_probability_average(ns_speech_prob_average);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001766 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001767#endif
1768 }
1769
1770 rewind(far_file_);
1771 rewind(near_file_);
1772 }
1773
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001774 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001775 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001776 }
1777}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001778
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001779TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1780 struct ChannelFormat {
1781 AudioProcessing::ChannelLayout in_layout;
1782 AudioProcessing::ChannelLayout out_layout;
1783 };
1784 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001785 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1786 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1787 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001788 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001789
Ivo Creusen62337e52018-01-09 14:17:33 +01001790 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001791 // Enable one component just to ensure some processing takes place.
1792 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08001793 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001794 const int in_rate = 44100;
1795 const int out_rate = 48000;
1796 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1797 TotalChannelsFromLayout(cf[i].in_layout));
1798 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1799 ChannelsFromLayout(cf[i].out_layout));
1800
1801 // Run over a few chunks.
1802 for (int j = 0; j < 10; ++j) {
Jonas Olssona4d87372019-07-05 19:08:33 +02001803 EXPECT_NOERR(ap->ProcessStream(in_cb.channels(), in_cb.num_frames(),
1804 in_rate, cf[i].in_layout, out_rate,
1805 cf[i].out_layout, out_cb.channels()));
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001806 }
1807 }
1808}
1809
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001810// Compares the reference and test arrays over a region around the expected
1811// delay. Finds the highest SNR in that region and adds the variance and squared
1812// error results to the supplied accumulators.
1813void UpdateBestSNR(const float* ref,
1814 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001815 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001816 int expected_delay,
1817 double* variance_acc,
1818 double* sq_error_acc) {
1819 double best_snr = std::numeric_limits<double>::min();
1820 double best_variance = 0;
1821 double best_sq_error = 0;
1822 // Search over a region of eight samples around the expected delay.
1823 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1824 ++delay) {
1825 double sq_error = 0;
1826 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001827 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001828 double error = test[i + delay] - ref[i];
1829 sq_error += error * error;
1830 variance += ref[i] * ref[i];
1831 }
1832
1833 if (sq_error == 0) {
1834 *variance_acc += variance;
1835 return;
1836 }
1837 double snr = variance / sq_error;
1838 if (snr > best_snr) {
1839 best_snr = snr;
1840 best_variance = variance;
1841 best_sq_error = sq_error;
1842 }
1843 }
1844
1845 *variance_acc += best_variance;
1846 *sq_error_acc += best_sq_error;
1847}
1848
1849// Used to test a multitude of sample rate and channel combinations. It works
1850// by first producing a set of reference files (in SetUpTestCase) that are
1851// assumed to be correct, as the used parameters are verified by other tests
1852// in this collection. Primarily the reference files are all produced at
1853// "native" rates which do not involve any resampling.
1854
1855// Each test pass produces an output file with a particular format. The output
1856// is matched against the reference file closest to its internal processing
1857// format. If necessary the output is resampled back to its process format.
1858// Due to the resampling distortion, we don't expect identical results, but
1859// enforce SNR thresholds which vary depending on the format. 0 is a special
1860// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001861typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001862class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001863 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001864 public:
1865 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001866 : input_rate_(std::get<0>(GetParam())),
1867 output_rate_(std::get<1>(GetParam())),
1868 reverse_input_rate_(std::get<2>(GetParam())),
1869 reverse_output_rate_(std::get<3>(GetParam())),
1870 expected_snr_(std::get<4>(GetParam())),
1871 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001872
1873 virtual ~AudioProcessingTest() {}
1874
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001875 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001876 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001877 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001878 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001879 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1880 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1881 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001882 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001883 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1884 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1885 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001886 }
1887 }
1888 }
1889 }
1890
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001891 void TearDown() {
1892 // Remove "out" files after each test.
1893 ClearTempOutFiles();
1894 }
1895
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001896 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001897
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001898 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001899 // to a file specified with |output_file_prefix|. Both forward and reverse
1900 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001901 static void ProcessFormat(int input_rate,
1902 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001903 int reverse_input_rate,
1904 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001905 size_t num_input_channels,
1906 size_t num_output_channels,
1907 size_t num_reverse_input_channels,
1908 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001909 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001910 Config config;
1911 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001912 std::unique_ptr<AudioProcessing> ap(
1913 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001914 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001915
ekmeyerson60d9b332015-08-14 10:35:55 -07001916 ProcessingConfig processing_config = {
1917 {{input_rate, num_input_channels},
1918 {output_rate, num_output_channels},
1919 {reverse_input_rate, num_reverse_input_channels},
1920 {reverse_output_rate, num_reverse_output_channels}}};
1921 ap->Initialize(processing_config);
1922
1923 FILE* far_file =
1924 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001925 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
Jonas Olssona4d87372019-07-05 19:08:33 +02001926 FILE* out_file = fopen(
1927 OutputFilePath(
1928 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1929 reverse_output_rate, num_input_channels, num_output_channels,
1930 num_reverse_input_channels, num_reverse_output_channels, kForward)
1931 .c_str(),
1932 "wb");
1933 FILE* rev_out_file = fopen(
1934 OutputFilePath(
1935 output_file_prefix, input_rate, output_rate, reverse_input_rate,
1936 reverse_output_rate, num_input_channels, num_output_channels,
1937 num_reverse_input_channels, num_reverse_output_channels, kReverse)
1938 .c_str(),
1939 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001940 ASSERT_TRUE(far_file != NULL);
1941 ASSERT_TRUE(near_file != NULL);
1942 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07001943 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001944
1945 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
1946 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001947 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
1948 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001949 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
1950 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07001951 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
1952 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001953
1954 // Temporary buffers.
1955 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07001956 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
1957 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08001958 std::unique_ptr<float[]> float_data(new float[max_length]);
1959 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001960
1961 int analog_level = 127;
1962 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
1963 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07001964 EXPECT_NOERR(ap->ProcessReverseStream(
1965 rev_cb.channels(), processing_config.reverse_input_stream(),
1966 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001967
1968 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001969 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
1970
1971 EXPECT_NOERR(ap->ProcessStream(
Jonas Olssona4d87372019-07-05 19:08:33 +02001972 fwd_cb.channels(), fwd_cb.num_frames(), input_rate,
1973 LayoutFromChannels(num_input_channels), output_rate,
1974 LayoutFromChannels(num_output_channels), out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001975
ekmeyerson60d9b332015-08-14 10:35:55 -07001976 // Dump forward output to file.
1977 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001978 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001979 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001980
Jonas Olssona4d87372019-07-05 19:08:33 +02001981 ASSERT_EQ(out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1982 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001983
ekmeyerson60d9b332015-08-14 10:35:55 -07001984 // Dump reverse output to file.
1985 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
1986 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08001987 size_t rev_out_length =
1988 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07001989
Jonas Olssona4d87372019-07-05 19:08:33 +02001990 ASSERT_EQ(rev_out_length, fwrite(float_data.get(), sizeof(float_data[0]),
1991 rev_out_length, rev_out_file));
ekmeyerson60d9b332015-08-14 10:35:55 -07001992
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001993 analog_level = ap->gain_control()->stream_analog_level();
1994 }
1995 fclose(far_file);
1996 fclose(near_file);
1997 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07001998 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001999 }
2000
2001 protected:
2002 int input_rate_;
2003 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002004 int reverse_input_rate_;
2005 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002006 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002007 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002008};
2009
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002010TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002011 struct ChannelFormat {
2012 int num_input;
2013 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002014 int num_reverse_input;
2015 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002016 };
2017 ChannelFormat cf[] = {
Jonas Olssona4d87372019-07-05 19:08:33 +02002018 {1, 1, 1, 1}, {1, 1, 2, 1}, {2, 1, 1, 1},
2019 {2, 1, 2, 1}, {2, 2, 1, 1}, {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002020 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002021
pkasting25702cb2016-01-08 13:50:27 -08002022 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002023 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2024 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2025 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002026
ekmeyerson60d9b332015-08-14 10:35:55 -07002027 // Verify output for both directions.
2028 std::vector<StreamDirection> stream_directions;
2029 stream_directions.push_back(kForward);
2030 stream_directions.push_back(kReverse);
2031 for (StreamDirection file_direction : stream_directions) {
2032 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2033 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2034 const int out_num =
2035 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2036 const double expected_snr =
2037 file_direction ? expected_reverse_snr_ : expected_snr_;
2038
2039 const int min_ref_rate = std::min(in_rate, out_rate);
2040 int ref_rate;
2041
2042 if (min_ref_rate > 32000) {
2043 ref_rate = 48000;
2044 } else if (min_ref_rate > 16000) {
2045 ref_rate = 32000;
2046 } else if (min_ref_rate > 8000) {
2047 ref_rate = 16000;
2048 } else {
2049 ref_rate = 8000;
2050 }
aluebs776593b2016-03-15 14:04:58 -07002051#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002052 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002053 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002054 }
2055#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002056 FILE* out_file = fopen(
2057 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2058 reverse_output_rate_, cf[i].num_input,
2059 cf[i].num_output, cf[i].num_reverse_input,
Jonas Olssona4d87372019-07-05 19:08:33 +02002060 cf[i].num_reverse_output, file_direction)
2061 .c_str(),
ekmeyerson60d9b332015-08-14 10:35:55 -07002062 "rb");
2063 // The reference files always have matching input and output channels.
Jonas Olssona4d87372019-07-05 19:08:33 +02002064 FILE* ref_file =
2065 fopen(OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2066 cf[i].num_output, cf[i].num_output,
2067 cf[i].num_reverse_output,
2068 cf[i].num_reverse_output, file_direction)
2069 .c_str(),
2070 "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002071 ASSERT_TRUE(out_file != NULL);
2072 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002073
pkasting25702cb2016-01-08 13:50:27 -08002074 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2075 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002076 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002077 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002078 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002079 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002080 // Data from the resampled output, in case the reference and output rates
2081 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002082 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002083
ekmeyerson60d9b332015-08-14 10:35:55 -07002084 PushResampler<float> resampler;
2085 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002086
ekmeyerson60d9b332015-08-14 10:35:55 -07002087 // Compute the resampling delay of the output relative to the reference,
2088 // to find the region over which we should search for the best SNR.
2089 float expected_delay_sec = 0;
2090 if (in_rate != ref_rate) {
2091 // Input resampling delay.
2092 expected_delay_sec +=
2093 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2094 }
2095 if (out_rate != ref_rate) {
2096 // Output resampling delay.
2097 expected_delay_sec +=
2098 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2099 // Delay of converting the output back to its processing rate for
2100 // testing.
2101 expected_delay_sec +=
2102 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2103 }
2104 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002105 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002106
ekmeyerson60d9b332015-08-14 10:35:55 -07002107 double variance = 0;
2108 double sq_error = 0;
2109 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2110 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2111 float* out_ptr = out_data.get();
2112 if (out_rate != ref_rate) {
2113 // Resample the output back to its internal processing rate if
2114 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002115 ASSERT_EQ(ref_length,
2116 static_cast<size_t>(resampler.Resample(
2117 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002118 out_ptr = cmp_data.get();
2119 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002120
ekmeyerson60d9b332015-08-14 10:35:55 -07002121 // Update the |sq_error| and |variance| accumulators with the highest
2122 // SNR of reference vs output.
2123 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2124 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002125 }
2126
ekmeyerson60d9b332015-08-14 10:35:55 -07002127 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2128 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2129 << cf[i].num_input << ", " << cf[i].num_output << ", "
2130 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2131 << ", " << file_direction << "): ";
2132 if (sq_error > 0) {
2133 double snr = 10 * log10(variance / sq_error);
2134 EXPECT_GE(snr, expected_snr);
2135 EXPECT_NE(0, expected_snr);
2136 std::cout << "SNR=" << snr << " dB" << std::endl;
2137 } else {
aluebs776593b2016-03-15 14:04:58 -07002138 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002139 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002140
ekmeyerson60d9b332015-08-14 10:35:55 -07002141 fclose(out_file);
2142 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002143 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002144 }
2145}
2146
2147#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002148INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002149 CommonFormats,
2150 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002151 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2152 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2153 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2154 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2155 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2156 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2157 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2158 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2159 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2160 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2161 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2162 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002163
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002164 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2165 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2166 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2167 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2168 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2169 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2170 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2171 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2172 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2173 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2174 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2175 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002176
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002177 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2178 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2179 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2180 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2181 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2182 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2183 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2184 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2185 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2186 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2187 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2188 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002189
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002190 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2191 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2192 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2193 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2194 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2195 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2196 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2197 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2198 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2199 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2200 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2201 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002202
2203#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002204INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002205 CommonFormats,
2206 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002207 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2208 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2209 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2210 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2211 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2212 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2213 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2214 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2215 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2216 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2217 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2218 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002219
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002220 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2221 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2222 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2223 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2224 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2225 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2226 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2227 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2228 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2229 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2230 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2231 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002232
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002233 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2234 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2235 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2236 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2237 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2238 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2239 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2240 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2241 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2242 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2243 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2244 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002245
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002246 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2247 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2248 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2249 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2250 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2251 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2252 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2253 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2254 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2255 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2256 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2257 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002258#endif
2259
niklase@google.com470e71d2011-07-07 08:21:25 +00002260} // namespace
peahc19f3122016-10-07 14:54:10 -07002261
Alessio Bazzicac054e782018-04-16 12:10:09 +02002262TEST(RuntimeSettingTest, TestDefaultCtor) {
2263 auto s = AudioProcessing::RuntimeSetting();
2264 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2265}
2266
2267TEST(RuntimeSettingTest, TestCapturePreGain) {
2268 using Type = AudioProcessing::RuntimeSetting::Type;
2269 {
2270 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2271 EXPECT_EQ(Type::kCapturePreGain, s.type());
2272 float v;
2273 s.GetFloat(&v);
2274 EXPECT_EQ(1.25f, v);
2275 }
2276
2277#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2278 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2279#endif
2280}
2281
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002282TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2283 using Type = AudioProcessing::RuntimeSetting::Type;
2284 {
2285 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2286 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2287 float v;
2288 s.GetFloat(&v);
2289 EXPECT_EQ(1.25f, v);
2290 }
2291
2292#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2293 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2294#endif
2295}
2296
Alessio Bazzicac054e782018-04-16 12:10:09 +02002297TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2298 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2299 auto s = AudioProcessing::RuntimeSetting();
2300 ASSERT_TRUE(q.Insert(&s));
2301 ASSERT_TRUE(q.Remove(&s));
2302 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2303}
2304
Sam Zackrisson0beac582017-09-25 12:04:02 +02002305TEST(ApmConfiguration, EnablePostProcessing) {
2306 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002307 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002308 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002309 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002310 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002311 rtc::scoped_refptr<AudioProcessing> apm =
2312 AudioProcessingBuilder()
2313 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002314 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002315
2316 AudioFrame audio;
2317 audio.num_channels_ = 1;
2318 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2319
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002320 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002321 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002322}
2323
Alex Loiko5825aa62017-12-18 16:02:40 +01002324TEST(ApmConfiguration, EnablePreProcessing) {
2325 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002326 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002327 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002328 auto mock_pre_processor =
2329 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002330 rtc::scoped_refptr<AudioProcessing> apm =
2331 AudioProcessingBuilder()
2332 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002333 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002334
2335 AudioFrame audio;
2336 audio.num_channels_ = 1;
2337 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2338
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002339 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002340 apm->ProcessReverseStream(&audio);
2341}
2342
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002343TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2344 // Verify that apm uses a capture analyzer if one is provided.
2345 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002346 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002347 auto mock_capture_analyzer =
2348 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2349 rtc::scoped_refptr<AudioProcessing> apm =
2350 AudioProcessingBuilder()
2351 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2352 .Create();
2353
2354 AudioFrame audio;
2355 audio.num_channels_ = 1;
2356 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2357
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002358 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002359 apm->ProcessStream(&audio);
2360}
2361
Alex Loiko73ec0192018-05-15 10:52:28 +02002362TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2363 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002364 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002365 auto mock_pre_processor =
2366 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2367 rtc::scoped_refptr<AudioProcessing> apm =
2368 AudioProcessingBuilder()
2369 .SetRenderPreProcessing(std::move(mock_pre_processor))
2370 .Create();
2371 apm->SetRuntimeSetting(
2372 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2373
2374 // RuntimeSettings forwarded during 'Process*Stream' calls.
2375 // Therefore we have to make one such call.
2376 AudioFrame audio;
2377 audio.num_channels_ = 1;
2378 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2379
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002380 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2381 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002382 apm->ProcessReverseStream(&audio);
2383}
2384
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002385class MyEchoControlFactory : public EchoControlFactory {
2386 public:
2387 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2388 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002389 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2390 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
2391 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_)).Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002392 return std::unique_ptr<EchoControl>(ec);
2393 }
2394};
2395
2396TEST(ApmConfiguration, EchoControlInjection) {
2397 // Verify that apm uses an injected echo controller if one is provided.
2398 webrtc::Config webrtc_config;
2399 std::unique_ptr<EchoControlFactory> echo_control_factory(
2400 new MyEchoControlFactory());
2401
Alex Loiko5825aa62017-12-18 16:02:40 +01002402 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002403 AudioProcessingBuilder()
2404 .SetEchoControlFactory(std::move(echo_control_factory))
2405 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002406
2407 AudioFrame audio;
2408 audio.num_channels_ = 1;
2409 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2410 apm->ProcessStream(&audio);
2411 apm->ProcessReverseStream(&audio);
2412 apm->ProcessStream(&audio);
2413}
Ivo Creusenae026092017-11-20 13:07:16 +01002414
Per Åhgren8607f842019-04-12 22:02:26 +02002415std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002416 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002417 std::unique_ptr<AudioProcessing> apm(
2418 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002419 if (!apm) {
2420 return apm;
2421 }
2422
2423 ProcessingConfig processing_config = {
2424 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2425
2426 if (apm->Initialize(processing_config) != 0) {
2427 return nullptr;
2428 }
2429
2430 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002431 AudioProcessing::Config apm_config;
2432 apm_config.residual_echo_detector.enabled = true;
2433 apm_config.high_pass_filter.enabled = false;
2434 apm_config.gain_controller2.enabled = false;
2435 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002436 apm_config.echo_canceller.mobile_mode = mobile_aec;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002437 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002438 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2439 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2440 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2441 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002442 return apm;
2443}
2444
2445#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2446#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2447#else
2448#define MAYBE_ApmStatistics ApmStatistics
2449#endif
2450
Per Åhgren8607f842019-04-12 22:02:26 +02002451TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2452 // Set up APM with AEC3 and process some audio.
2453 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002454 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002455 AudioProcessing::Config apm_config;
2456 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002457 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002458
2459 // Set up an audioframe.
2460 AudioFrame frame;
2461 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002462 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002463
2464 // Fill the audio frame with a sawtooth pattern.
2465 int16_t* ptr = frame.mutable_data();
2466 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2467 ptr[i] = 10000 * ((i % 3) - 1);
2468 }
2469
2470 // Do some processing.
2471 for (int i = 0; i < 200; i++) {
2472 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2473 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2474 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2475 }
2476
2477 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002478 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002479 // We expect all statistics to be set and have a sensible value.
2480 ASSERT_TRUE(stats.residual_echo_likelihood);
2481 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2482 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2483 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2484 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2485 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2486 ASSERT_TRUE(stats.echo_return_loss);
2487 EXPECT_NE(*stats.echo_return_loss, -100.0);
2488 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2489 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002490
2491 // If there are no receive streams, we expect the stats not to be set. The
2492 // 'false' argument signals to APM that no receive streams are currently
2493 // active. In that situation the statistics would get stuck at their last
2494 // calculated value (AEC and echo detection need at least one stream in each
2495 // direction), so to avoid that, they should not be set by APM.
2496 stats = apm->GetStatistics(false);
2497 EXPECT_FALSE(stats.residual_echo_likelihood);
2498 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2499 EXPECT_FALSE(stats.echo_return_loss);
2500 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002501}
2502
2503TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2504 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002505 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002506 ASSERT_TRUE(apm);
2507
2508 // Set up an audioframe.
2509 AudioFrame frame;
2510 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002511 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002512
2513 // Fill the audio frame with a sawtooth pattern.
2514 int16_t* ptr = frame.mutable_data();
2515 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2516 ptr[i] = 10000 * ((i % 3) - 1);
2517 }
2518
2519 // Do some processing.
2520 for (int i = 0; i < 200; i++) {
2521 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2522 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2523 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2524 }
2525
2526 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002527 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002528 // We expect only the residual echo detector statistics to be set and have a
2529 // sensible value.
2530 EXPECT_TRUE(stats.residual_echo_likelihood);
2531 if (stats.residual_echo_likelihood) {
2532 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2533 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2534 }
2535 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2536 if (stats.residual_echo_likelihood_recent_max) {
2537 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2538 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2539 }
2540 EXPECT_FALSE(stats.echo_return_loss);
2541 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002542
2543 // If there are no receive streams, we expect the stats not to be set.
2544 stats = apm->GetStatistics(false);
2545 EXPECT_FALSE(stats.residual_echo_likelihood);
2546 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2547 EXPECT_FALSE(stats.echo_return_loss);
2548 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002549}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002550
2551TEST(ApmStatistics, ReportOutputRmsDbfs) {
2552 ProcessingConfig processing_config = {
2553 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2554 AudioProcessing::Config config;
2555
2556 // Set up an audioframe.
2557 AudioFrame frame;
2558 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002559 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002560
2561 // Fill the audio frame with a sawtooth pattern.
2562 int16_t* ptr = frame.mutable_data();
2563 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2564 ptr[i] = 10000 * ((i % 3) - 1);
2565 }
2566
2567 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2568 apm->Initialize(processing_config);
2569
2570 // If not enabled, no metric should be reported.
2571 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2572 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2573
2574 // If enabled, metrics should be reported.
2575 config.level_estimation.enabled = true;
2576 apm->ApplyConfig(config);
2577 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2578 auto stats = apm->GetStatistics(false);
2579 EXPECT_TRUE(stats.output_rms_dbfs);
2580 EXPECT_GE(*stats.output_rms_dbfs, 0);
2581
2582 // If re-disabled, the value is again not reported.
2583 config.level_estimation.enabled = false;
2584 apm->ApplyConfig(config);
2585 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2586 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2587}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002588
2589TEST(ApmStatistics, ReportHasVoice) {
2590 ProcessingConfig processing_config = {
2591 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2592 AudioProcessing::Config config;
2593
2594 // Set up an audioframe.
2595 AudioFrame frame;
2596 frame.num_channels_ = 1;
2597 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2598
2599 // Fill the audio frame with a sawtooth pattern.
2600 int16_t* ptr = frame.mutable_data();
2601 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2602 ptr[i] = 10000 * ((i % 3) - 1);
2603 }
2604
2605 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2606 apm->Initialize(processing_config);
2607
2608 // If not enabled, no metric should be reported.
2609 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2610 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2611
2612 // If enabled, metrics should be reported.
2613 config.voice_detection.enabled = true;
2614 apm->ApplyConfig(config);
2615 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2616 auto stats = apm->GetStatistics(false);
2617 EXPECT_TRUE(stats.voice_detected);
2618
2619 // If re-disabled, the value is again not reported.
2620 config.voice_detection.enabled = false;
2621 apm->ApplyConfig(config);
2622 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2623 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2624}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002625} // namespace webrtc