blob: 1c1d741b3f881821220f19afd7b71eca66da3a04 [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 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
Oleh Prypin708eccc2019-03-27 09:38:52 +010014#include <cmath>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "common_audio/include/audio_util.h"
20#include "common_audio/resampler/include/push_resampler.h"
21#include "common_audio/resampler/push_sinc_resampler.h"
22#include "common_audio/signal_processing/include/signal_processing_library.h"
23#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
24#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020027#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/test/protobuf_utils.h"
29#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/arraysize.h"
31#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/fake_clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/gtest_prod_util.h"
34#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010035#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010036#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/protobuf_utils.h"
Steve Anton10542f22019-01-11 09:11:00 -080038#include "rtc_base/ref_counted_object.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020039#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020040#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020041#include "rtc_base/system/arch.h"
Danil Chapovalov07122bc2019-03-26 14:37:01 +010042#include "rtc_base/task_queue_for_test.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/thread.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080045#include "test/testsupport/file_utils.h"
kwiberg77eab702016-09-28 17:42:01 -070046
47RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000049#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#endif
kwiberg77eab702016-09-28 17:42:01 -070053RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000054
andrew@webrtc.org27c69802014-02-18 20:24:56 +000055namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000056namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000057
ekmeyerson60d9b332015-08-14 10:35:55 -070058// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
59// applicable.
60
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000061// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000062// re-implemented; see comment at the bottom of this file. For now, the user has
63// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000064// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000065// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000066// be set to true with the command-line switch --write_ref_data.
67bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070068const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070069const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000070
aluebseb3603b2016-04-20 15:27:58 -070071#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
72// Android doesn't support 48kHz.
73const int kProcessSampleRates[] = {8000, 16000, 32000};
74#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070075const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070076#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000077
ekmeyerson60d9b332015-08-14 10:35:55 -070078enum StreamDirection { kForward = 0, kReverse };
79
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082 cb->num_channels());
83 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000085 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080087 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 S16ToFloat(cb_int.channels()[i],
89 cb->num_frames(),
90 cb->channels()[i]);
91 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000092}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000093
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070095 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000096}
97
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080099size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000100 switch (layout) {
101 case AudioProcessing::kMono:
102 return 1;
103 case AudioProcessing::kMonoAndKeyboard:
104 case AudioProcessing::kStereo:
105 return 2;
106 case AudioProcessing::kStereoAndKeyboard:
107 return 3;
108 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700109 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800110 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000111}
112
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800114 size_t samples_per_channel) {
115 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000117}
118
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800120 size_t samples_per_channel) {
121 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
123}
124
pkasting25702cb2016-01-08 13:50:27 -0800125void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
126 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000127 stereo[i * 2 + 1] = stereo[i * 2];
128 }
129}
130
yujo36b1a5f2017-06-12 12:45:32 -0700131void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800132 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000133 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
134 }
135}
136
137void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700138 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700139 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
140 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700141 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000142 }
143}
144
145void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800146 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700147 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700148 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700149 frame_data[i] = left;
150 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000151 }
152}
153
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000154void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700155 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700156 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
157 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700158 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000159 }
160}
161
andrew@webrtc.org81865342012-10-27 00:28:27 +0000162bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000163 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000164 return false;
165 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
yujo36b1a5f2017-06-12 12:45:32 -0700169 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000171 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000172 return false;
173 }
174 return true;
175}
176
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000177void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200178 AudioProcessing::Config apm_config = ap->GetConfig();
179 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000180#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200181 apm_config.echo_canceller.mobile_mode = true;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100182
183 apm_config.gain_controller1.enabled = true;
184 apm_config.gain_controller1.mode =
185 AudioProcessing::Config::GainController1::kAdaptiveDigital;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200187 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000188
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100189 apm_config.gain_controller1.enabled = true;
190 apm_config.gain_controller1.mode =
191 AudioProcessing::Config::GainController1::kAdaptiveAnalog;
192 apm_config.gain_controller1.analog_level_minimum = 0;
193 apm_config.gain_controller1.analog_level_maximum = 255;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000194#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000195
peah8271d042016-11-22 07:24:52 -0800196 apm_config.high_pass_filter.enabled = true;
Sam Zackrisson11b87032018-12-18 17:13:58 +0100197 apm_config.level_estimation.enabled = true;
peah8271d042016-11-22 07:24:52 -0800198 ap->ApplyConfig(apm_config);
199
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200 EXPECT_NOERR(ap->level_estimator()->Enable(true));
201 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
202
203 EXPECT_NOERR(ap->voice_detection()->Enable(true));
204}
205
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000206// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000207template <class T>
208T AbsValue(T a) {
209 return a > 0 ? a: -a;
210}
211
212int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800213 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700214 const int16_t* frame_data = frame.data();
215 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800216 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700217 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000218 }
219
220 return max_data;
221}
222
Alex Loiko890988c2017-08-31 10:25:48 +0200223void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700224 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000226 ASSERT_TRUE(file != NULL);
227
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100228 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800230 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000231 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000235 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000236 fclose(file);
237}
238
Alex Loiko890988c2017-08-31 10:25:48 +0200239std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200240 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000241 // Resource files are all stereo.
242 ss << name << sample_rate_hz / 1000 << "_stereo";
243 return test::ResourcePath(ss.str(), "pcm");
244}
245
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000246// Temporary filenames unique to this process. Used to be able to run these
247// tests in parallel as each process needs to be running in isolation they can't
248// have competing filenames.
249std::map<std::string, std::string> temp_filenames;
250
Alex Loiko890988c2017-08-31 10:25:48 +0200251std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000252 int input_rate,
253 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700254 int reverse_input_rate,
255 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800256 size_t num_input_channels,
257 size_t num_output_channels,
258 size_t num_reverse_input_channels,
259 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700260 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200261 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700262 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
263 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000264 if (num_output_channels == 1) {
265 ss << "mono";
266 } else if (num_output_channels == 2) {
267 ss << "stereo";
268 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700269 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000270 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700271 ss << output_rate / 1000;
272 if (num_reverse_output_channels == 1) {
273 ss << "_rmono";
274 } else if (num_reverse_output_channels == 2) {
275 ss << "_rstereo";
276 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700277 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 }
279 ss << reverse_output_rate / 1000;
280 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000281
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000282 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700283 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000284 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
285 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000286}
287
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000288void ClearTempFiles() {
289 for (auto& kv : temp_filenames)
290 remove(kv.second.c_str());
291}
292
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200293// Only remove "out" files. Keep "ref" files.
294void ClearTempOutFiles() {
295 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
296 const std::string& filename = it->first;
297 if (filename.substr(0, 3).compare("out") == 0) {
298 remove(it->second.c_str());
299 temp_filenames.erase(it++);
300 } else {
301 it++;
302 }
303 }
304}
305
Alex Loiko890988c2017-08-31 10:25:48 +0200306void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000307 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000308 ASSERT_TRUE(file != NULL);
309 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000310 fclose(file);
311}
312
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000313// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
314// stereo) file, converts to deinterleaved float (optionally downmixing) and
315// returns the result in |cb|. Returns false if the file ended (or on error) and
316// true otherwise.
317//
318// |int_data| and |float_data| are just temporary space that must be
319// sufficiently large to hold the 10 ms chunk.
320bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
321 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 {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000335 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000336 cb->channels());
337 }
338
339 return true;
340}
341
niklase@google.com470e71d2011-07-07 08:21:25 +0000342class ApmTest : public ::testing::Test {
343 protected:
344 ApmTest();
345 virtual void SetUp();
346 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000347
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200348 static void SetUpTestSuite() {}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000349
Mirko Bonadei71061bc2019-06-04 09:01:51 +0200350 static void TearDownTestSuite() { ClearTempFiles(); }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000351
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000352 // Used to select between int and float interface tests.
353 enum Format {
354 kIntFormat,
355 kFloatFormat
356 };
357
358 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000359 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000360 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800361 size_t num_input_channels,
362 size_t num_output_channels,
363 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000364 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000365 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000366 void EnableAllComponents();
367 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000368 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000369 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000370 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
371 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000372 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000373 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
374 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700375 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800376 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700377 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800378 void TestChangingForwardChannels(size_t num_in_channels,
379 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700380 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800381 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000383 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
384 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000385 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000386 int ProcessStreamChooser(Format format);
387 int AnalyzeReverseStreamChooser(Format format);
388 void ProcessDebugDump(const std::string& in_filename,
389 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800390 Format format,
391 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000392 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000393
394 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000395 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800396 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000397 AudioFrame* frame_;
398 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800399 std::unique_ptr<ChannelBuffer<float> > float_cb_;
400 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000401 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800402 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403 FILE* far_file_;
404 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000405 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406};
407
408ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000409 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000410#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800411 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
412 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000413#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800414 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
415 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000416#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000418 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000419 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000420 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000421 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000422 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000423 out_file_(NULL) {
424 Config config;
425 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100426 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000427}
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
429void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000430 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000431
432 frame_ = new AudioFrame();
433 revframe_ = new AudioFrame();
434
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000435 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000436}
437
438void ApmTest::TearDown() {
439 if (frame_) {
440 delete frame_;
441 }
442 frame_ = NULL;
443
444 if (revframe_) {
445 delete revframe_;
446 }
447 revframe_ = NULL;
448
449 if (far_file_) {
450 ASSERT_EQ(0, fclose(far_file_));
451 }
452 far_file_ = NULL;
453
454 if (near_file_) {
455 ASSERT_EQ(0, fclose(near_file_));
456 }
457 near_file_ = NULL;
458
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000459 if (out_file_) {
460 ASSERT_EQ(0, fclose(out_file_));
461 }
462 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000465void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000466 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700467 ap->Initialize(
468 {{{frame_->sample_rate_hz_, frame_->num_channels_},
469 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700470 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700471 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000472}
473
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000474void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000476 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800477 size_t num_input_channels,
478 size_t num_output_channels,
479 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000480 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000482 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000483 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000484
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000485 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
486 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000487 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000488
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000489 if (far_file_) {
490 ASSERT_EQ(0, fclose(far_file_));
491 }
492 std::string filename = ResourceFilePath("far", sample_rate_hz);
493 far_file_ = fopen(filename.c_str(), "rb");
494 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
495 filename << "\n";
496
497 if (near_file_) {
498 ASSERT_EQ(0, fclose(near_file_));
499 }
500 filename = ResourceFilePath("near", sample_rate_hz);
501 near_file_ = fopen(filename.c_str(), "rb");
502 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
503 filename << "\n";
504
505 if (open_output_file) {
506 if (out_file_) {
507 ASSERT_EQ(0, fclose(out_file_));
508 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700509 filename = OutputFilePath(
510 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
511 reverse_sample_rate_hz, num_input_channels, num_output_channels,
512 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000513 out_file_ = fopen(filename.c_str(), "wb");
514 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
515 filename << "\n";
516 }
517}
518
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000519void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000520 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000521}
522
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000523bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
524 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000525 // The files always contain stereo audio.
526 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700527 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000528 sizeof(int16_t),
529 frame_size,
530 file);
531 if (read_count != frame_size) {
532 // Check that the file really ended.
533 EXPECT_NE(0, feof(file));
534 return false; // This is expected.
535 }
536
537 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700538 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000539 frame->samples_per_channel_);
540 }
541
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000542 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000543 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000544 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000545 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000546}
547
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
549 return ReadFrame(file, frame, NULL);
550}
551
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000552// If the end of the file has been reached, rewind it and attempt to read the
553// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
555 ChannelBuffer<float>* cb) {
556 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000557 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000558 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000559 }
560}
561
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000562void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
563 ReadFrameWithRewind(file, frame, NULL);
564}
565
andrew@webrtc.org81865342012-10-27 00:28:27 +0000566void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
567 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000568 EXPECT_EQ(apm_->kNoError,
569 apm_->gain_control()->set_stream_analog_level(127));
570 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000571}
572
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000573int ApmTest::ProcessStreamChooser(Format format) {
574 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575 return apm_->ProcessStream(frame_);
576 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577 return apm_->ProcessStream(float_cb_->channels(),
578 frame_->samples_per_channel_,
579 frame_->sample_rate_hz_,
580 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000581 output_sample_rate_hz_,
582 LayoutFromChannels(num_output_channels_),
583 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000584}
585
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000586int ApmTest::AnalyzeReverseStreamChooser(Format format) {
587 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700588 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000589 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590 return apm_->AnalyzeReverseStream(
591 revfloat_cb_->channels(),
592 revframe_->samples_per_channel_,
593 revframe_->sample_rate_hz_,
594 LayoutFromChannels(revframe_->num_channels_));
595}
596
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000597void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
598 int delay_min, int delay_max) {
599 // The |revframe_| and |frame_| should include the proper frame information,
600 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000601 AudioFrame tmp_frame;
602 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000603 bool causal = true;
604
605 tmp_frame.CopyFrom(*revframe_);
606 SetFrameTo(&tmp_frame, 0);
607
608 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
609 // Initialize the |frame_queue| with empty frames.
610 int frame_delay = delay_ms / 10;
611 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000612 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000613 frame->CopyFrom(tmp_frame);
614 frame_queue.push(frame);
615 frame_delay++;
616 causal = false;
617 }
618 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000619 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000620 frame->CopyFrom(tmp_frame);
621 frame_queue.push(frame);
622 frame_delay--;
623 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000624 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
625 // need enough frames with audio to have reliable estimates, but as few as
626 // possible to keep processing time down. 4.5 seconds seemed to be a good
627 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000628 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000629 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000630 frame->CopyFrom(tmp_frame);
631 // Use the near end recording, since that has more speech in it.
632 ASSERT_TRUE(ReadFrame(near_file_, frame));
633 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000634 AudioFrame* reverse_frame = frame;
635 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000636 if (!causal) {
637 reverse_frame = frame_queue.front();
638 // When we call ProcessStream() the frame is modified, so we can't use the
639 // pointer directly when things are non-causal. Use an intermediate frame
640 // and copy the data.
641 process_frame = &tmp_frame;
642 process_frame->CopyFrom(*frame);
643 }
aluebsb0319552016-03-17 20:39:53 -0700644 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
646 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
647 frame = frame_queue.front();
648 frame_queue.pop();
649 delete frame;
650
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000651 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200653 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000654 }
655 }
656
657 rewind(near_file_);
658 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000659 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000660 frame_queue.pop();
661 delete frame;
662 }
663 // Calculate expected delay estimate and acceptable regions. Further,
664 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700665 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700666 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700667 const int expected_median =
668 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
669 const int expected_median_high = rtc::SafeClamp<int>(
670 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700671 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700672 const int expected_median_low = rtc::SafeClamp<int>(
673 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700674 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000675 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200676 AudioProcessingStats stats =
677 apm_->GetStatistics(true /* has_remote_tracks */);
678 ASSERT_TRUE(stats.delay_median_ms.has_value());
679 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000680 EXPECT_GE(expected_median_high, median);
681 EXPECT_LE(expected_median_low, median);
682}
683
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000684void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000685 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000686 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000687
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000688 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000689 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000690 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000691 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000692
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000693 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 EXPECT_EQ(apm_->kNoError,
695 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000696 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000697 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000698 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000699
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000700 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200701 AudioProcessing::Config apm_config = apm_->GetConfig();
702 apm_config.echo_canceller.enabled = true;
703 apm_config.echo_canceller.mobile_mode = false;
704 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000705 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000707 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000709
710 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000711 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000713
714 // Resets after successful ProcessStream().
715 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000716 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100717 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000718
719 // Other stream parameters set correctly.
720 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
721 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000722 apm_->gain_control()->set_stream_analog_level(127));
Per Åhgren200feba2019-03-06 04:16:46 +0100723 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000724 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
725
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100729 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000730
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000731 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000732 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 EXPECT_EQ(apm_->kNoError,
734 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736}
737
738TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000740}
741
742TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000746TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
747 EXPECT_EQ(0, apm_->delay_offset_ms());
748 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
749 EXPECT_EQ(50, apm_->stream_delay_ms());
750}
751
752TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
753 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000754 apm_->set_delay_offset_ms(100);
755 EXPECT_EQ(100, apm_->delay_offset_ms());
756 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000757 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000758 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
759 EXPECT_EQ(200, apm_->stream_delay_ms());
760
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000761 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000762 apm_->set_delay_offset_ms(-50);
763 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000764 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
765 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000766 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
767 EXPECT_EQ(50, apm_->stream_delay_ms());
768}
769
Michael Graczyk86c6d332015-07-23 11:41:39 -0700770void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800771 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700772 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000773 frame_->num_channels_ = num_channels;
774 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700775 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000776}
777
Michael Graczyk86c6d332015-07-23 11:41:39 -0700778void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800779 size_t num_in_channels,
780 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700781 AudioProcessing::Error expected_return) {
782 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
783 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
784
785 EXPECT_EQ(expected_return,
786 apm_->ProcessStream(float_cb_->channels(), input_stream,
787 output_stream, float_cb_->channels()));
788}
789
790void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800791 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700792 AudioProcessing::Error expected_return) {
793 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700794 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
795 {output_sample_rate_hz_, apm_->num_output_channels()},
796 {frame_->sample_rate_hz_, num_rev_channels},
797 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700798
ekmeyerson60d9b332015-08-14 10:35:55 -0700799 EXPECT_EQ(
800 expected_return,
801 apm_->ProcessReverseStream(
802 float_cb_->channels(), processing_config.reverse_input_stream(),
803 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700804}
805
806TEST_F(ApmTest, ChannelsInt16Interface) {
807 // Testing number of invalid and valid channels.
808 Init(16000, 16000, 16000, 4, 4, 4, false);
809
810 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
811
Peter Kasting69558702016-01-12 16:26:35 -0800812 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700813 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000814 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000815 }
816}
817
Michael Graczyk86c6d332015-07-23 11:41:39 -0700818TEST_F(ApmTest, Channels) {
819 // Testing number of invalid and valid channels.
820 Init(16000, 16000, 16000, 4, 4, 4, false);
821
822 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
823 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
824
Peter Kasting69558702016-01-12 16:26:35 -0800825 for (size_t i = 1; i < 4; ++i) {
826 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700827 // Output channels much be one or match input channels.
828 if (j == 1 || i == j) {
829 TestChangingForwardChannels(i, j, kNoErr);
830 TestChangingReverseChannels(i, kNoErr);
831
832 EXPECT_EQ(i, apm_->num_input_channels());
833 EXPECT_EQ(j, apm_->num_output_channels());
834 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800835 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700836 } else {
837 TestChangingForwardChannels(i, j,
838 AudioProcessing::kBadNumberChannelsError);
839 }
840 }
841 }
842}
843
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000844TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000845 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000846 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000847 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000848 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700849 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800850 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000851 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000852 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000853 }
854}
855
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000856TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +0000858 EXPECT_EQ(apm_->kNoError,
859 apm_->gain_control()->set_mode(
860 apm_->gain_control()->mode()));
861
862 GainControl::Mode mode[] = {
863 GainControl::kAdaptiveAnalog,
864 GainControl::kAdaptiveDigital,
865 GainControl::kFixedDigital
866 };
pkasting25702cb2016-01-08 13:50:27 -0800867 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 EXPECT_EQ(apm_->kNoError,
869 apm_->gain_control()->set_mode(mode[i]));
870 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
871 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100872 // Testing target levels
niklase@google.com470e71d2011-07-07 08:21:25 +0000873 EXPECT_EQ(apm_->kNoError,
874 apm_->gain_control()->set_target_level_dbfs(
875 apm_->gain_control()->target_level_dbfs()));
876
877 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -0800878 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000879 EXPECT_EQ(apm_->kNoError,
880 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
881 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
882 }
883
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100884 // Testing compression gains
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 EXPECT_EQ(apm_->kNoError,
886 apm_->gain_control()->set_compression_gain_db(
887 apm_->gain_control()->compression_gain_db()));
888
889 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -0800890 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000891 EXPECT_EQ(apm_->kNoError,
892 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100893 ProcessStreamChooser(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
895 }
896
897 // Testing limiter off/on
898 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
899 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
900 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
901 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
902
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100903 // Testing level limits
niklase@google.com470e71d2011-07-07 08:21:25 +0000904 EXPECT_EQ(apm_->kNoError,
905 apm_->gain_control()->set_analog_level_limits(
906 apm_->gain_control()->analog_level_minimum(),
907 apm_->gain_control()->analog_level_maximum()));
908
909 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -0800910 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000911 EXPECT_EQ(apm_->kNoError,
912 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
913 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
914 }
915
916 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -0800917 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 EXPECT_EQ(apm_->kNoError,
919 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
920 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
921 }
922
923 // TODO(ajm): stream_is_saturated() and stream_analog_level()
924
925 // Turn AGC off
926 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
927 EXPECT_FALSE(apm_->gain_control()->is_enabled());
928}
929
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100930#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
931TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
932 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(-1), "");
933}
934
935TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
936 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(32), "");
937}
938
939TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
940 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(-1), "");
941}
942
943TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
944 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(91), "");
945}
946
947TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
948 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(-1, 512), "");
949}
950
951TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
952 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 65536), "");
953}
954
955TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
956 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 255), "");
957}
958
959TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
960 apm_->gain_control()->set_analog_level_limits(255, 512);
961 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
962}
963
964TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
965 apm_->gain_control()->set_analog_level_limits(255, 512);
966 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
967}
968#endif
969
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000970void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000971 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000972 EXPECT_EQ(apm_->kNoError,
973 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
974 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
975
976 int out_analog_level = 0;
977 for (int i = 0; i < 2000; ++i) {
978 ReadFrameWithRewind(near_file_, frame_);
979 // Ensure the audio is at a low level, so the AGC will try to increase it.
980 ScaleFrame(frame_, 0.25);
981
982 // Always pass in the same volume.
983 EXPECT_EQ(apm_->kNoError,
984 apm_->gain_control()->set_stream_analog_level(100));
985 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
986 out_analog_level = apm_->gain_control()->stream_analog_level();
987 }
988
989 // Ensure the AGC is still able to reach the maximum.
990 EXPECT_EQ(255, out_analog_level);
991}
992
993// Verifies that despite volume slider quantization, the AGC can continue to
994// increase its volume.
995TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -0800996 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000997 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
998 }
999}
1000
1001void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001002 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001003 EXPECT_EQ(apm_->kNoError,
1004 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1005 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1006
1007 int out_analog_level = 100;
1008 for (int i = 0; i < 1000; ++i) {
1009 ReadFrameWithRewind(near_file_, frame_);
1010 // Ensure the audio is at a low level, so the AGC will try to increase it.
1011 ScaleFrame(frame_, 0.25);
1012
1013 EXPECT_EQ(apm_->kNoError,
1014 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1015 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1016 out_analog_level = apm_->gain_control()->stream_analog_level();
1017 }
1018
1019 // Ensure the volume was raised.
1020 EXPECT_GT(out_analog_level, 100);
1021 int highest_level_reached = out_analog_level;
1022 // Simulate a user manual volume change.
1023 out_analog_level = 100;
1024
1025 for (int i = 0; i < 300; ++i) {
1026 ReadFrameWithRewind(near_file_, frame_);
1027 ScaleFrame(frame_, 0.25);
1028
1029 EXPECT_EQ(apm_->kNoError,
1030 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1031 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1032 out_analog_level = apm_->gain_control()->stream_analog_level();
1033 // Check that AGC respected the manually adjusted volume.
1034 EXPECT_LT(out_analog_level, highest_level_reached);
1035 }
1036 // Check that the volume was still raised.
1037 EXPECT_GT(out_analog_level, 100);
1038}
1039
1040TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001041 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001042 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1043 }
1044}
1045
niklase@google.com470e71d2011-07-07 08:21:25 +00001046TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001047 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 NoiseSuppression::Level level[] = {
1049 NoiseSuppression::kLow,
1050 NoiseSuppression::kModerate,
1051 NoiseSuppression::kHigh,
1052 NoiseSuppression::kVeryHigh
1053 };
pkasting25702cb2016-01-08 13:50:27 -08001054 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001055 EXPECT_EQ(apm_->kNoError,
1056 apm_->noise_suppression()->set_level(level[i]));
1057 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1058 }
1059
andrew@webrtc.org648af742012-02-08 01:57:29 +00001060 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001061 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1062 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1063 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1064 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1065}
1066
1067TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001068 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001069 AudioProcessing::Config apm_config;
1070 apm_config.high_pass_filter.enabled = true;
1071 apm_->ApplyConfig(apm_config);
1072 apm_config.high_pass_filter.enabled = false;
1073 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001074}
1075
1076TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001077 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001078 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001079 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001080
1081 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1082
1083 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1084 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1085
1086 // Run this test in wideband; in super-wb, the splitting filter distorts the
1087 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001088 frame_->samples_per_channel_ = 160;
1089 frame_->num_channels_ = 2;
1090 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001091
1092 // Min value if no frames have been processed.
1093 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1094
1095 // Min value on zero frames.
1096 SetFrameTo(frame_, 0);
1097 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1098 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1099 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1100
1101 // Try a few RMS values.
1102 // (These also test that the value resets after retrieving it.)
1103 SetFrameTo(frame_, 32767);
1104 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1105 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1106 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1107
1108 SetFrameTo(frame_, 30000);
1109 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1110 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1111 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1112
1113 SetFrameTo(frame_, 10000);
1114 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1115 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1116 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1117
1118 SetFrameTo(frame_, 10);
1119 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1120 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1121 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1122
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001123 // Verify reset after enable/disable.
1124 SetFrameTo(frame_, 32767);
1125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1126 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1127 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1128 SetFrameTo(frame_, 1);
1129 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1130 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1131
1132 // Verify reset after initialize.
1133 SetFrameTo(frame_, 32767);
1134 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1135 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1136 SetFrameTo(frame_, 1);
1137 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1138 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001139}
1140
1141TEST_F(ApmTest, VoiceDetection) {
1142 // Test external VAD
1143 EXPECT_EQ(apm_->kNoError,
1144 apm_->voice_detection()->set_stream_has_voice(true));
1145 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1146 EXPECT_EQ(apm_->kNoError,
1147 apm_->voice_detection()->set_stream_has_voice(false));
1148 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1149
andrew@webrtc.org648af742012-02-08 01:57:29 +00001150 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 VoiceDetection::Likelihood likelihood[] = {
1152 VoiceDetection::kVeryLowLikelihood,
1153 VoiceDetection::kLowLikelihood,
1154 VoiceDetection::kModerateLikelihood,
1155 VoiceDetection::kHighLikelihood
1156 };
pkasting25702cb2016-01-08 13:50:27 -08001157 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 EXPECT_EQ(apm_->kNoError,
1159 apm_->voice_detection()->set_likelihood(likelihood[i]));
1160 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1161 }
1162
1163 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001164 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 EXPECT_EQ(apm_->kBadParameterError,
1166 apm_->voice_detection()->set_frame_size_ms(12));
1167
andrew@webrtc.org648af742012-02-08 01:57:29 +00001168 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001169 for (int i = 10; i <= 30; i += 10) {
1170 EXPECT_EQ(apm_->kNoError,
1171 apm_->voice_detection()->set_frame_size_ms(i));
1172 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1173 }
1174 */
1175
andrew@webrtc.org648af742012-02-08 01:57:29 +00001176 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1178 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1179 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1180 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1181
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001182 // Test that AudioFrame activity is maintained when VAD is disabled.
1183 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1184 AudioFrame::VADActivity activity[] = {
1185 AudioFrame::kVadActive,
1186 AudioFrame::kVadPassive,
1187 AudioFrame::kVadUnknown
1188 };
pkasting25702cb2016-01-08 13:50:27 -08001189 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001190 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001191 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001192 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001193 }
1194
1195 // Test that AudioFrame activity is set when VAD is enabled.
1196 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001197 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001198 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001199 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001200
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1202}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001203
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001204TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001205 AudioProcessing::Config config = apm_->GetConfig();
1206 EXPECT_FALSE(config.echo_canceller.enabled);
1207 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001208 EXPECT_FALSE(config.level_estimation.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001209 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001210 EXPECT_FALSE(apm_->gain_control()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001211 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1212 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1213 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1214}
1215
1216TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001217 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001218 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001219 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001220 AudioFrame frame_copy;
1221 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001222 for (int j = 0; j < 1000; j++) {
1223 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1224 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001225 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1226 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001227 }
1228 }
1229}
1230
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001231TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1232 // Test that ProcessStream copies input to output even with no processing.
1233 const size_t kSamples = 80;
1234 const int sample_rate = 8000;
1235 const float src[kSamples] = {
1236 -1.0f, 0.0f, 1.0f
1237 };
1238 float dest[kSamples] = {};
1239
1240 auto src_channels = &src[0];
1241 auto dest_channels = &dest[0];
1242
Ivo Creusen62337e52018-01-09 14:17:33 +01001243 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001244 EXPECT_NOERR(apm_->ProcessStream(
1245 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1246 sample_rate, LayoutFromChannels(1), &dest_channels));
1247
1248 for (size_t i = 0; i < kSamples; ++i) {
1249 EXPECT_EQ(src[i], dest[i]);
1250 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001251
1252 // Same for ProcessReverseStream.
1253 float rev_dest[kSamples] = {};
1254 auto rev_dest_channels = &rev_dest[0];
1255
1256 StreamConfig input_stream = {sample_rate, 1};
1257 StreamConfig output_stream = {sample_rate, 1};
1258 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1259 output_stream, &rev_dest_channels));
1260
1261 for (size_t i = 0; i < kSamples; ++i) {
1262 EXPECT_EQ(src[i], rev_dest[i]);
1263 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001264}
1265
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001266TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1267 EnableAllComponents();
1268
pkasting25702cb2016-01-08 13:50:27 -08001269 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001270 Init(kProcessSampleRates[i],
1271 kProcessSampleRates[i],
1272 kProcessSampleRates[i],
1273 2,
1274 2,
1275 2,
1276 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001277 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001278 ASSERT_EQ(0, feof(far_file_));
1279 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001280 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001281 CopyLeftToRightChannel(revframe_->mutable_data(),
1282 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001283
aluebsb0319552016-03-17 20:39:53 -07001284 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001285
yujo36b1a5f2017-06-12 12:45:32 -07001286 CopyLeftToRightChannel(frame_->mutable_data(),
1287 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001288 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1289
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001290 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001291 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001292 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001293 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001294 analog_level = apm_->gain_control()->stream_analog_level();
1295
yujo36b1a5f2017-06-12 12:45:32 -07001296 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001297 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001298 rewind(far_file_);
1299 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001300 }
1301}
1302
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001303TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001304 // Verify the filter is not active through undistorted audio when:
1305 // 1. No components are enabled...
1306 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001307 AudioFrame frame_copy;
1308 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001309 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1310 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1311 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1312
1313 // 2. Only the level estimator is enabled...
1314 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001315 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001316 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1317 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1318 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1319 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1320 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1321
1322 // 3. Only VAD is enabled...
1323 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001324 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001325 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1326 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1327 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1328 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1329 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1330
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001331 // 4. Only GetStatistics-reporting VAD is enabled...
1332 SetFrameTo(frame_, 1000);
1333 frame_copy.CopyFrom(*frame_);
1334 auto apm_config = apm_->GetConfig();
1335 apm_config.voice_detection.enabled = true;
1336 apm_->ApplyConfig(apm_config);
1337 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1338 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1339 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1340 apm_config.voice_detection.enabled = false;
1341 apm_->ApplyConfig(apm_config);
1342
1343 // 5. Both VADs and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001344 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001345 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001346 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1347 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001348 apm_config.voice_detection.enabled = true;
1349 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001350 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1351 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1352 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1353 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1354 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001355 apm_config.voice_detection.enabled = false;
1356 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001357
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001358 // Check the test is valid. We should have distortion from the filter
1359 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001360 apm_config.echo_canceller.enabled = true;
1361 apm_config.echo_canceller.mobile_mode = false;
1362 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001363 frame_->samples_per_channel_ = 320;
1364 frame_->num_channels_ = 2;
1365 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001366 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001367 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001368 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001369 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1370 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1371}
1372
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001373#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1374void ApmTest::ProcessDebugDump(const std::string& in_filename,
1375 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001376 Format format,
1377 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001378 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001379 FILE* in_file = fopen(in_filename.c_str(), "rb");
1380 ASSERT_TRUE(in_file != NULL);
1381 audioproc::Event event_msg;
1382 bool first_init = true;
1383
1384 while (ReadMessageFromFile(in_file, &event_msg)) {
1385 if (event_msg.type() == audioproc::Event::INIT) {
1386 const audioproc::Init msg = event_msg.init();
1387 int reverse_sample_rate = msg.sample_rate();
1388 if (msg.has_reverse_sample_rate()) {
1389 reverse_sample_rate = msg.reverse_sample_rate();
1390 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001391 int output_sample_rate = msg.sample_rate();
1392 if (msg.has_output_sample_rate()) {
1393 output_sample_rate = msg.output_sample_rate();
1394 }
1395
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001396 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001397 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001398 reverse_sample_rate,
1399 msg.num_input_channels(),
1400 msg.num_output_channels(),
1401 msg.num_reverse_channels(),
1402 false);
1403 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001404 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001405 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001406 auto aec_dump =
1407 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1408 EXPECT_TRUE(aec_dump);
1409 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001410 first_init = false;
1411 }
1412
1413 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1414 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1415
1416 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001417 ASSERT_EQ(revframe_->num_channels_,
1418 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001419 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001420 memcpy(revfloat_cb_->channels()[i],
1421 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001422 msg.channel(i).size());
1423 }
1424 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001425 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001426 if (format == kFloatFormat) {
1427 // We're using an int16 input file; convert to float.
1428 ConvertToFloat(*revframe_, revfloat_cb_.get());
1429 }
1430 }
1431 AnalyzeReverseStreamChooser(format);
1432
1433 } else if (event_msg.type() == audioproc::Event::STREAM) {
1434 const audioproc::Stream msg = event_msg.stream();
1435 // ProcessStream could have changed this for the output frame.
1436 frame_->num_channels_ = apm_->num_input_channels();
1437
1438 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1439 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001440 if (msg.has_keypress()) {
1441 apm_->set_stream_key_pressed(msg.keypress());
1442 } else {
1443 apm_->set_stream_key_pressed(true);
1444 }
1445
1446 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001447 ASSERT_EQ(frame_->num_channels_,
1448 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001449 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001450 memcpy(float_cb_->channels()[i],
1451 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001452 msg.input_channel(i).size());
1453 }
1454 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001455 memcpy(frame_->mutable_data(), msg.input_data().data(),
1456 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001457 if (format == kFloatFormat) {
1458 // We're using an int16 input file; convert to float.
1459 ConvertToFloat(*frame_, float_cb_.get());
1460 }
1461 }
1462 ProcessStreamChooser(format);
1463 }
1464 }
aleloif4dd1912017-06-15 01:55:38 -07001465 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001466 fclose(in_file);
1467}
1468
1469void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001470 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001471 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001472 std::string format_string;
1473 switch (format) {
1474 case kIntFormat:
1475 format_string = "_int";
1476 break;
1477 case kFloatFormat:
1478 format_string = "_float";
1479 break;
1480 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001481 const std::string ref_filename = test::TempFilename(
1482 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1483 const std::string out_filename = test::TempFilename(
1484 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001485 const std::string limited_filename = test::TempFilename(
1486 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1487 const size_t logging_limit_bytes = 100000;
1488 // We expect at least this many bytes in the created logfile.
1489 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001490 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001491 ProcessDebugDump(in_filename, ref_filename, format, -1);
1492 ProcessDebugDump(ref_filename, out_filename, format, -1);
1493 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001494
1495 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1496 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001497 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001498 ASSERT_TRUE(ref_file != NULL);
1499 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001500 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001501 std::unique_ptr<uint8_t[]> ref_bytes;
1502 std::unique_ptr<uint8_t[]> out_bytes;
1503 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001504
1505 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1506 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001507 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001508 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001509 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001510 while (ref_size > 0 && out_size > 0) {
1511 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001512 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001513 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001514 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001515 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001516 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001517 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1518 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001519 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001520 }
1521 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001522 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1523 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001524 EXPECT_NE(0, feof(ref_file));
1525 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001526 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001527 ASSERT_EQ(0, fclose(ref_file));
1528 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001529 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001530 remove(ref_filename.c_str());
1531 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001532 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001533}
1534
pbosc7a65692016-05-06 12:50:04 -07001535TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001536 VerifyDebugDumpTest(kIntFormat);
1537}
1538
pbosc7a65692016-05-06 12:50:04 -07001539TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001540 VerifyDebugDumpTest(kFloatFormat);
1541}
1542#endif
1543
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001544// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001545TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001546 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001547 const std::string filename =
1548 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001549 {
1550 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1551 EXPECT_FALSE(aec_dump);
1552 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001553
1554#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1555 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001556 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001557
aleloif4dd1912017-06-15 01:55:38 -07001558 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1559 EXPECT_TRUE(aec_dump);
1560 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001561 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001562 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001563 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001564
1565 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001566 FILE* fid = fopen(filename.c_str(), "r");
1567 ASSERT_TRUE(fid != NULL);
1568
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001569 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001570 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001571 ASSERT_EQ(0, remove(filename.c_str()));
1572#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001573 // Verify the file has NOT been written.
1574 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1575#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1576}
1577
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001578// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001579TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001580 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001581
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001582 const std::string filename =
1583 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001584 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001585 ASSERT_TRUE(fid);
1586
1587#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1588 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001589 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001590
aleloif4dd1912017-06-15 01:55:38 -07001591 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1592 EXPECT_TRUE(aec_dump);
1593 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001594 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001595 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001596 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001597
1598 // Verify the file has been written.
1599 fid = fopen(filename.c_str(), "r");
1600 ASSERT_TRUE(fid != NULL);
1601
1602 // Clean it up.
1603 ASSERT_EQ(0, fclose(fid));
1604 ASSERT_EQ(0, remove(filename.c_str()));
1605#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001606 ASSERT_EQ(0, fclose(fid));
1607#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1608}
1609
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001610// TODO(andrew): Add a test to process a few frames with different combinations
1611// of enabled components.
1612
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001613TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001614 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001615 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001616
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001617 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001618 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001619 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001620 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001621 for (size_t i = 0; i < arraysize(kChannels); i++) {
1622 for (size_t j = 0; j < arraysize(kChannels); j++) {
1623 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001624 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001625 test->set_num_reverse_channels(kChannels[i]);
1626 test->set_num_input_channels(kChannels[j]);
1627 test->set_num_output_channels(kChannels[j]);
1628 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001629 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001630 }
1631 }
1632 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001633#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1634 // To test the extended filter mode.
1635 audioproc::Test* test = ref_data.add_test();
1636 test->set_num_reverse_channels(2);
1637 test->set_num_input_channels(2);
1638 test->set_num_output_channels(2);
1639 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1640 test->set_use_aec_extended_filter(true);
1641#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001642 }
1643
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001644 for (int i = 0; i < ref_data.test_size(); i++) {
1645 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001646
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001647 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001648 // TODO(ajm): We no longer allow different input and output channels. Skip
1649 // these tests for now, but they should be removed from the set.
1650 if (test->num_input_channels() != test->num_output_channels())
1651 continue;
1652
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001653 Config config;
1654 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001655 config.Set<ExtendedFilter>(
1656 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001657 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001658
1659 EnableAllComponents();
1660
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001661 Init(test->sample_rate(),
1662 test->sample_rate(),
1663 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001664 static_cast<size_t>(test->num_input_channels()),
1665 static_cast<size_t>(test->num_output_channels()),
1666 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001667 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001668
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001669 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001670 int has_voice_count = 0;
1671 int is_saturated_count = 0;
1672 int analog_level = 127;
1673 int analog_level_average = 0;
1674 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001675 float ns_speech_prob_average = 0.0f;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001676 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001677#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1678 int stats_index = 0;
1679#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001680
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001681 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001682 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001683
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001684 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1685
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687 EXPECT_EQ(apm_->kNoError,
1688 apm_->gain_control()->set_stream_analog_level(analog_level));
1689
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001690 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001691
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001692 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001693 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1694 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001695
1696 max_output_average += MaxAudioFrame(*frame_);
1697
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001698 analog_level = apm_->gain_control()->stream_analog_level();
1699 analog_level_average += analog_level;
1700 if (apm_->gain_control()->stream_is_saturated()) {
1701 is_saturated_count++;
1702 }
1703 if (apm_->voice_detection()->stream_has_voice()) {
1704 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001705 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001706 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001707 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001708 }
1709
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001710 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
Sam Zackrisson11b87032018-12-18 17:13:58 +01001711 AudioProcessingStats stats =
1712 apm_->GetStatistics(/*has_remote_tracks=*/false);
1713 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001714
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001715 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001716 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001717 sizeof(int16_t),
1718 frame_size,
1719 out_file_);
1720 ASSERT_EQ(frame_size, write_count);
1721
1722 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001723 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001724 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001725
1726#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1727 const int kStatsAggregationFrameNum = 100; // 1 second.
1728 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001729 // Get echo and delay metrics.
1730 AudioProcessingStats stats =
1731 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001732
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001733 // Echo metrics.
1734 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1735 const float echo_return_loss_enhancement =
1736 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001737 const float residual_echo_likelihood =
1738 stats.residual_echo_likelihood.value_or(-1.0f);
1739 const float residual_echo_likelihood_recent_max =
1740 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1741
minyue58530ed2016-05-24 05:50:12 -07001742 if (!write_ref_data) {
1743 const audioproc::Test::EchoMetrics& reference =
1744 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001745 constexpr float kEpsilon = 0.01;
1746 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1747 EXPECT_NEAR(echo_return_loss_enhancement,
1748 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001749 EXPECT_NEAR(residual_echo_likelihood,
1750 reference.residual_echo_likelihood(), kEpsilon);
1751 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1752 reference.residual_echo_likelihood_recent_max(),
1753 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001754 ++stats_index;
1755 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001756 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1757 message_echo->set_echo_return_loss(echo_return_loss);
1758 message_echo->set_echo_return_loss_enhancement(
1759 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001760 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1761 message_echo->set_residual_echo_likelihood_recent_max(
1762 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001763 }
1764 }
1765#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001766 }
1767 max_output_average /= frame_count;
1768 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001769 ns_speech_prob_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001770 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001771
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001772 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001773 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001774 // When running the test on a N7 we get a {2, 6} difference of
1775 // |has_voice_count| and |max_output_average| is up to 18 higher.
1776 // All numbers being consistently higher on N7 compare to ref_data.
1777 // TODO(bjornv): If we start getting more of these offsets on Android we
1778 // should consider a different approach. Either using one slack for all,
1779 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001780#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001781 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001782 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001783 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001784 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001785#else
1786 const int kHasVoiceCountOffset = 0;
1787 const int kHasVoiceCountNear = kIntNear;
1788 const int kMaxOutputAverageOffset = 0;
1789 const int kMaxOutputAverageNear = kIntNear;
1790#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001791 EXPECT_NEAR(test->has_voice_count(),
1792 has_voice_count - kHasVoiceCountOffset,
1793 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001794 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001795
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001796 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001797 EXPECT_NEAR(test->max_output_average(),
1798 max_output_average - kMaxOutputAverageOffset,
1799 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001800#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001801 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001802 EXPECT_NEAR(test->ns_speech_probability_average(),
1803 ns_speech_prob_average,
1804 kFloatNear);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001805 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001806#endif
1807 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001808 test->set_has_voice_count(has_voice_count);
1809 test->set_is_saturated_count(is_saturated_count);
1810
1811 test->set_analog_level_average(analog_level_average);
1812 test->set_max_output_average(max_output_average);
1813
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001814#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001815 EXPECT_LE(0.0f, ns_speech_prob_average);
1816 EXPECT_GE(1.0f, ns_speech_prob_average);
1817 test->set_ns_speech_probability_average(ns_speech_prob_average);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001818 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001819#endif
1820 }
1821
1822 rewind(far_file_);
1823 rewind(near_file_);
1824 }
1825
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001826 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001827 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001828 }
1829}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001830
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001831TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1832 struct ChannelFormat {
1833 AudioProcessing::ChannelLayout in_layout;
1834 AudioProcessing::ChannelLayout out_layout;
1835 };
1836 ChannelFormat cf[] = {
1837 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1838 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1839 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
1840 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001841
Ivo Creusen62337e52018-01-09 14:17:33 +01001842 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001843 // Enable one component just to ensure some processing takes place.
1844 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08001845 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001846 const int in_rate = 44100;
1847 const int out_rate = 48000;
1848 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1849 TotalChannelsFromLayout(cf[i].in_layout));
1850 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1851 ChannelsFromLayout(cf[i].out_layout));
1852
1853 // Run over a few chunks.
1854 for (int j = 0; j < 10; ++j) {
1855 EXPECT_NOERR(ap->ProcessStream(
1856 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001857 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001858 in_rate,
1859 cf[i].in_layout,
1860 out_rate,
1861 cf[i].out_layout,
1862 out_cb.channels()));
1863 }
1864 }
1865}
1866
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001867// Compares the reference and test arrays over a region around the expected
1868// delay. Finds the highest SNR in that region and adds the variance and squared
1869// error results to the supplied accumulators.
1870void UpdateBestSNR(const float* ref,
1871 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001872 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001873 int expected_delay,
1874 double* variance_acc,
1875 double* sq_error_acc) {
1876 double best_snr = std::numeric_limits<double>::min();
1877 double best_variance = 0;
1878 double best_sq_error = 0;
1879 // Search over a region of eight samples around the expected delay.
1880 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1881 ++delay) {
1882 double sq_error = 0;
1883 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001884 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001885 double error = test[i + delay] - ref[i];
1886 sq_error += error * error;
1887 variance += ref[i] * ref[i];
1888 }
1889
1890 if (sq_error == 0) {
1891 *variance_acc += variance;
1892 return;
1893 }
1894 double snr = variance / sq_error;
1895 if (snr > best_snr) {
1896 best_snr = snr;
1897 best_variance = variance;
1898 best_sq_error = sq_error;
1899 }
1900 }
1901
1902 *variance_acc += best_variance;
1903 *sq_error_acc += best_sq_error;
1904}
1905
1906// Used to test a multitude of sample rate and channel combinations. It works
1907// by first producing a set of reference files (in SetUpTestCase) that are
1908// assumed to be correct, as the used parameters are verified by other tests
1909// in this collection. Primarily the reference files are all produced at
1910// "native" rates which do not involve any resampling.
1911
1912// Each test pass produces an output file with a particular format. The output
1913// is matched against the reference file closest to its internal processing
1914// format. If necessary the output is resampled back to its process format.
1915// Due to the resampling distortion, we don't expect identical results, but
1916// enforce SNR thresholds which vary depending on the format. 0 is a special
1917// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001918typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001919class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001920 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001921 public:
1922 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001923 : input_rate_(std::get<0>(GetParam())),
1924 output_rate_(std::get<1>(GetParam())),
1925 reverse_input_rate_(std::get<2>(GetParam())),
1926 reverse_output_rate_(std::get<3>(GetParam())),
1927 expected_snr_(std::get<4>(GetParam())),
1928 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001929
1930 virtual ~AudioProcessingTest() {}
1931
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001932 static void SetUpTestSuite() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001933 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001934 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001935 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001936 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1937 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1938 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001939 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001940 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1941 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1942 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001943 }
1944 }
1945 }
1946 }
1947
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001948 void TearDown() {
1949 // Remove "out" files after each test.
1950 ClearTempOutFiles();
1951 }
1952
Mirko Bonadei71061bc2019-06-04 09:01:51 +02001953 static void TearDownTestSuite() { ClearTempFiles(); }
ekmeyerson60d9b332015-08-14 10:35:55 -07001954
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001955 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001956 // to a file specified with |output_file_prefix|. Both forward and reverse
1957 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001958 static void ProcessFormat(int input_rate,
1959 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001960 int reverse_input_rate,
1961 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001962 size_t num_input_channels,
1963 size_t num_output_channels,
1964 size_t num_reverse_input_channels,
1965 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001966 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001967 Config config;
1968 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001969 std::unique_ptr<AudioProcessing> ap(
1970 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001971 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001972
ekmeyerson60d9b332015-08-14 10:35:55 -07001973 ProcessingConfig processing_config = {
1974 {{input_rate, num_input_channels},
1975 {output_rate, num_output_channels},
1976 {reverse_input_rate, num_reverse_input_channels},
1977 {reverse_output_rate, num_reverse_output_channels}}};
1978 ap->Initialize(processing_config);
1979
1980 FILE* far_file =
1981 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001982 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07001983 FILE* out_file =
1984 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
1985 reverse_input_rate, reverse_output_rate,
1986 num_input_channels, num_output_channels,
1987 num_reverse_input_channels,
1988 num_reverse_output_channels, kForward).c_str(),
1989 "wb");
1990 FILE* rev_out_file =
1991 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
1992 reverse_input_rate, reverse_output_rate,
1993 num_input_channels, num_output_channels,
1994 num_reverse_input_channels,
1995 num_reverse_output_channels, kReverse).c_str(),
1996 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001997 ASSERT_TRUE(far_file != NULL);
1998 ASSERT_TRUE(near_file != NULL);
1999 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002000 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002001
2002 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2003 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002004 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2005 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002006 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2007 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002008 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2009 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002010
2011 // Temporary buffers.
2012 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002013 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2014 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002015 std::unique_ptr<float[]> float_data(new float[max_length]);
2016 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002017
2018 int analog_level = 127;
2019 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2020 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002021 EXPECT_NOERR(ap->ProcessReverseStream(
2022 rev_cb.channels(), processing_config.reverse_input_stream(),
2023 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002024
2025 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002026 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2027
2028 EXPECT_NOERR(ap->ProcessStream(
2029 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002030 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002031 input_rate,
2032 LayoutFromChannels(num_input_channels),
2033 output_rate,
2034 LayoutFromChannels(num_output_channels),
2035 out_cb.channels()));
2036
ekmeyerson60d9b332015-08-14 10:35:55 -07002037 // Dump forward output to file.
2038 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002039 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002040 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002041
pkasting25702cb2016-01-08 13:50:27 -08002042 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002043 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002044 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002045
ekmeyerson60d9b332015-08-14 10:35:55 -07002046 // Dump reverse output to file.
2047 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2048 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002049 size_t rev_out_length =
2050 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002051
pkasting25702cb2016-01-08 13:50:27 -08002052 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002053 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2054 rev_out_file));
2055
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002056 analog_level = ap->gain_control()->stream_analog_level();
2057 }
2058 fclose(far_file);
2059 fclose(near_file);
2060 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002061 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002062 }
2063
2064 protected:
2065 int input_rate_;
2066 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002067 int reverse_input_rate_;
2068 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002069 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002070 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002071};
2072
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002073TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002074 struct ChannelFormat {
2075 int num_input;
2076 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002077 int num_reverse_input;
2078 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002079 };
2080 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002081 {1, 1, 1, 1},
2082 {1, 1, 2, 1},
2083 {2, 1, 1, 1},
2084 {2, 1, 2, 1},
2085 {2, 2, 1, 1},
2086 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002087 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002088
pkasting25702cb2016-01-08 13:50:27 -08002089 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002090 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2091 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2092 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002093
ekmeyerson60d9b332015-08-14 10:35:55 -07002094 // Verify output for both directions.
2095 std::vector<StreamDirection> stream_directions;
2096 stream_directions.push_back(kForward);
2097 stream_directions.push_back(kReverse);
2098 for (StreamDirection file_direction : stream_directions) {
2099 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2100 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2101 const int out_num =
2102 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2103 const double expected_snr =
2104 file_direction ? expected_reverse_snr_ : expected_snr_;
2105
2106 const int min_ref_rate = std::min(in_rate, out_rate);
2107 int ref_rate;
2108
2109 if (min_ref_rate > 32000) {
2110 ref_rate = 48000;
2111 } else if (min_ref_rate > 16000) {
2112 ref_rate = 32000;
2113 } else if (min_ref_rate > 8000) {
2114 ref_rate = 16000;
2115 } else {
2116 ref_rate = 8000;
2117 }
aluebs776593b2016-03-15 14:04:58 -07002118#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002119 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002120 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002121 }
2122#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002123 FILE* out_file = fopen(
2124 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2125 reverse_output_rate_, cf[i].num_input,
2126 cf[i].num_output, cf[i].num_reverse_input,
2127 cf[i].num_reverse_output, file_direction).c_str(),
2128 "rb");
2129 // The reference files always have matching input and output channels.
2130 FILE* ref_file = fopen(
2131 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2132 cf[i].num_output, cf[i].num_output,
2133 cf[i].num_reverse_output, cf[i].num_reverse_output,
2134 file_direction).c_str(),
2135 "rb");
2136 ASSERT_TRUE(out_file != NULL);
2137 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002138
pkasting25702cb2016-01-08 13:50:27 -08002139 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2140 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002141 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002142 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002143 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002144 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002145 // Data from the resampled output, in case the reference and output rates
2146 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002147 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002148
ekmeyerson60d9b332015-08-14 10:35:55 -07002149 PushResampler<float> resampler;
2150 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002151
ekmeyerson60d9b332015-08-14 10:35:55 -07002152 // Compute the resampling delay of the output relative to the reference,
2153 // to find the region over which we should search for the best SNR.
2154 float expected_delay_sec = 0;
2155 if (in_rate != ref_rate) {
2156 // Input resampling delay.
2157 expected_delay_sec +=
2158 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2159 }
2160 if (out_rate != ref_rate) {
2161 // Output resampling delay.
2162 expected_delay_sec +=
2163 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2164 // Delay of converting the output back to its processing rate for
2165 // testing.
2166 expected_delay_sec +=
2167 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2168 }
2169 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002170 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002171
ekmeyerson60d9b332015-08-14 10:35:55 -07002172 double variance = 0;
2173 double sq_error = 0;
2174 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2175 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2176 float* out_ptr = out_data.get();
2177 if (out_rate != ref_rate) {
2178 // Resample the output back to its internal processing rate if
2179 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002180 ASSERT_EQ(ref_length,
2181 static_cast<size_t>(resampler.Resample(
2182 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002183 out_ptr = cmp_data.get();
2184 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002185
ekmeyerson60d9b332015-08-14 10:35:55 -07002186 // Update the |sq_error| and |variance| accumulators with the highest
2187 // SNR of reference vs output.
2188 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2189 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002190 }
2191
ekmeyerson60d9b332015-08-14 10:35:55 -07002192 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2193 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2194 << cf[i].num_input << ", " << cf[i].num_output << ", "
2195 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2196 << ", " << file_direction << "): ";
2197 if (sq_error > 0) {
2198 double snr = 10 * log10(variance / sq_error);
2199 EXPECT_GE(snr, expected_snr);
2200 EXPECT_NE(0, expected_snr);
2201 std::cout << "SNR=" << snr << " dB" << std::endl;
2202 } else {
aluebs776593b2016-03-15 14:04:58 -07002203 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002204 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002205
ekmeyerson60d9b332015-08-14 10:35:55 -07002206 fclose(out_file);
2207 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002208 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002209 }
2210}
2211
2212#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002213INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002214 CommonFormats,
2215 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002216 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2217 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2218 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2219 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2220 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2221 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2222 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2223 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2224 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2225 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2226 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2227 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002228
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002229 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2230 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2231 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2232 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2233 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2234 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2235 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2236 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2237 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2238 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2239 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2240 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002241
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002242 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2243 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2244 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2245 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2246 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2247 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2248 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2249 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2250 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2251 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2252 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2253 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002254
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002255 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2256 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2257 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2258 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2259 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2260 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2261 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2262 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2263 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2264 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2265 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2266 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002267
2268#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002269INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002270 CommonFormats,
2271 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002272 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2273 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2274 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2275 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2276 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2277 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2278 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2279 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2280 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2281 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2282 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2283 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002284
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002285 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2286 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2287 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2288 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2289 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2290 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2291 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2292 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2293 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2294 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2295 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2296 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002297
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002298 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2299 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2300 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2301 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2302 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2303 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2304 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2305 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2306 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2307 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2308 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2309 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002310
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002311 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2312 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2313 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2314 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2315 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2316 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2317 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2318 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2319 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2320 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2321 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2322 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002323#endif
2324
niklase@google.com470e71d2011-07-07 08:21:25 +00002325} // namespace
peahc19f3122016-10-07 14:54:10 -07002326
Alessio Bazzicac054e782018-04-16 12:10:09 +02002327TEST(RuntimeSettingTest, TestDefaultCtor) {
2328 auto s = AudioProcessing::RuntimeSetting();
2329 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2330}
2331
2332TEST(RuntimeSettingTest, TestCapturePreGain) {
2333 using Type = AudioProcessing::RuntimeSetting::Type;
2334 {
2335 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2336 EXPECT_EQ(Type::kCapturePreGain, s.type());
2337 float v;
2338 s.GetFloat(&v);
2339 EXPECT_EQ(1.25f, v);
2340 }
2341
2342#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2343 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2344#endif
2345}
2346
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002347TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2348 using Type = AudioProcessing::RuntimeSetting::Type;
2349 {
2350 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2351 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2352 float v;
2353 s.GetFloat(&v);
2354 EXPECT_EQ(1.25f, v);
2355 }
2356
2357#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2358 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2359#endif
2360}
2361
Alessio Bazzicac054e782018-04-16 12:10:09 +02002362TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2363 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2364 auto s = AudioProcessing::RuntimeSetting();
2365 ASSERT_TRUE(q.Insert(&s));
2366 ASSERT_TRUE(q.Remove(&s));
2367 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2368}
2369
Sam Zackrisson0beac582017-09-25 12:04:02 +02002370TEST(ApmConfiguration, EnablePostProcessing) {
2371 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002372 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002373 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002374 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002375 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002376 rtc::scoped_refptr<AudioProcessing> apm =
2377 AudioProcessingBuilder()
2378 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002379 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002380
2381 AudioFrame audio;
2382 audio.num_channels_ = 1;
2383 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2384
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002385 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002386 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002387}
2388
Alex Loiko5825aa62017-12-18 16:02:40 +01002389TEST(ApmConfiguration, EnablePreProcessing) {
2390 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002391 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002392 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002393 auto mock_pre_processor =
2394 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002395 rtc::scoped_refptr<AudioProcessing> apm =
2396 AudioProcessingBuilder()
2397 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002398 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002399
2400 AudioFrame audio;
2401 audio.num_channels_ = 1;
2402 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2403
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002404 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002405 apm->ProcessReverseStream(&audio);
2406}
2407
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002408TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2409 // Verify that apm uses a capture analyzer if one is provided.
2410 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002411 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002412 auto mock_capture_analyzer =
2413 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2414 rtc::scoped_refptr<AudioProcessing> apm =
2415 AudioProcessingBuilder()
2416 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2417 .Create();
2418
2419 AudioFrame audio;
2420 audio.num_channels_ = 1;
2421 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2422
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002423 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002424 apm->ProcessStream(&audio);
2425}
2426
Alex Loiko73ec0192018-05-15 10:52:28 +02002427TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2428 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002429 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002430 auto mock_pre_processor =
2431 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2432 rtc::scoped_refptr<AudioProcessing> apm =
2433 AudioProcessingBuilder()
2434 .SetRenderPreProcessing(std::move(mock_pre_processor))
2435 .Create();
2436 apm->SetRuntimeSetting(
2437 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2438
2439 // RuntimeSettings forwarded during 'Process*Stream' calls.
2440 // Therefore we have to make one such call.
2441 AudioFrame audio;
2442 audio.num_channels_ = 1;
2443 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2444
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002445 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2446 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002447 apm->ProcessReverseStream(&audio);
2448}
2449
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002450class MyEchoControlFactory : public EchoControlFactory {
2451 public:
2452 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2453 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002454 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2455 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
2456 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_)).Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002457 return std::unique_ptr<EchoControl>(ec);
2458 }
2459};
2460
2461TEST(ApmConfiguration, EchoControlInjection) {
2462 // Verify that apm uses an injected echo controller if one is provided.
2463 webrtc::Config webrtc_config;
2464 std::unique_ptr<EchoControlFactory> echo_control_factory(
2465 new MyEchoControlFactory());
2466
Alex Loiko5825aa62017-12-18 16:02:40 +01002467 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002468 AudioProcessingBuilder()
2469 .SetEchoControlFactory(std::move(echo_control_factory))
2470 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002471
2472 AudioFrame audio;
2473 audio.num_channels_ = 1;
2474 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2475 apm->ProcessStream(&audio);
2476 apm->ProcessReverseStream(&audio);
2477 apm->ProcessStream(&audio);
2478}
Ivo Creusenae026092017-11-20 13:07:16 +01002479
Per Åhgren8607f842019-04-12 22:02:26 +02002480std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002481 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002482 std::unique_ptr<AudioProcessing> apm(
2483 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002484 if (!apm) {
2485 return apm;
2486 }
2487
2488 ProcessingConfig processing_config = {
2489 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2490
2491 if (apm->Initialize(processing_config) != 0) {
2492 return nullptr;
2493 }
2494
2495 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002496 AudioProcessing::Config apm_config;
2497 apm_config.residual_echo_detector.enabled = true;
2498 apm_config.high_pass_filter.enabled = false;
2499 apm_config.gain_controller2.enabled = false;
2500 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002501 apm_config.echo_canceller.mobile_mode = mobile_aec;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002502 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002503 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2504 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2505 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2506 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002507 return apm;
2508}
2509
2510#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2511#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2512#else
2513#define MAYBE_ApmStatistics ApmStatistics
2514#endif
2515
Per Åhgren8607f842019-04-12 22:02:26 +02002516TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2517 // Set up APM with AEC3 and process some audio.
2518 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002519 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002520 AudioProcessing::Config apm_config;
2521 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002522 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002523
2524 // Set up an audioframe.
2525 AudioFrame frame;
2526 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002527 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002528
2529 // Fill the audio frame with a sawtooth pattern.
2530 int16_t* ptr = frame.mutable_data();
2531 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2532 ptr[i] = 10000 * ((i % 3) - 1);
2533 }
2534
2535 // Do some processing.
2536 for (int i = 0; i < 200; i++) {
2537 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2538 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2539 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2540 }
2541
2542 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002543 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002544 // We expect all statistics to be set and have a sensible value.
2545 ASSERT_TRUE(stats.residual_echo_likelihood);
2546 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2547 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2548 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2549 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2550 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2551 ASSERT_TRUE(stats.echo_return_loss);
2552 EXPECT_NE(*stats.echo_return_loss, -100.0);
2553 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2554 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002555
2556 // If there are no receive streams, we expect the stats not to be set. The
2557 // 'false' argument signals to APM that no receive streams are currently
2558 // active. In that situation the statistics would get stuck at their last
2559 // calculated value (AEC and echo detection need at least one stream in each
2560 // direction), so to avoid that, they should not be set by APM.
2561 stats = apm->GetStatistics(false);
2562 EXPECT_FALSE(stats.residual_echo_likelihood);
2563 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2564 EXPECT_FALSE(stats.echo_return_loss);
2565 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002566}
2567
2568TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2569 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002570 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002571 ASSERT_TRUE(apm);
2572
2573 // Set up an audioframe.
2574 AudioFrame frame;
2575 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002576 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002577
2578 // Fill the audio frame with a sawtooth pattern.
2579 int16_t* ptr = frame.mutable_data();
2580 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2581 ptr[i] = 10000 * ((i % 3) - 1);
2582 }
2583
2584 // Do some processing.
2585 for (int i = 0; i < 200; i++) {
2586 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2587 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2588 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2589 }
2590
2591 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002592 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002593 // We expect only the residual echo detector statistics to be set and have a
2594 // sensible value.
2595 EXPECT_TRUE(stats.residual_echo_likelihood);
2596 if (stats.residual_echo_likelihood) {
2597 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2598 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2599 }
2600 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2601 if (stats.residual_echo_likelihood_recent_max) {
2602 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2603 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2604 }
2605 EXPECT_FALSE(stats.echo_return_loss);
2606 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002607
2608 // If there are no receive streams, we expect the stats not to be set.
2609 stats = apm->GetStatistics(false);
2610 EXPECT_FALSE(stats.residual_echo_likelihood);
2611 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2612 EXPECT_FALSE(stats.echo_return_loss);
2613 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002614}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002615
2616TEST(ApmStatistics, ReportOutputRmsDbfs) {
2617 ProcessingConfig processing_config = {
2618 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2619 AudioProcessing::Config config;
2620
2621 // Set up an audioframe.
2622 AudioFrame frame;
2623 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002624 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002625
2626 // Fill the audio frame with a sawtooth pattern.
2627 int16_t* ptr = frame.mutable_data();
2628 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2629 ptr[i] = 10000 * ((i % 3) - 1);
2630 }
2631
2632 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2633 apm->Initialize(processing_config);
2634
2635 // If not enabled, no metric should be reported.
2636 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2637 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2638
2639 // If enabled, metrics should be reported.
2640 config.level_estimation.enabled = true;
2641 apm->ApplyConfig(config);
2642 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2643 auto stats = apm->GetStatistics(false);
2644 EXPECT_TRUE(stats.output_rms_dbfs);
2645 EXPECT_GE(*stats.output_rms_dbfs, 0);
2646
2647 // If re-disabled, the value is again not reported.
2648 config.level_estimation.enabled = false;
2649 apm->ApplyConfig(config);
2650 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2651 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2652}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002653
2654TEST(ApmStatistics, ReportHasVoice) {
2655 ProcessingConfig processing_config = {
2656 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2657 AudioProcessing::Config config;
2658
2659 // Set up an audioframe.
2660 AudioFrame frame;
2661 frame.num_channels_ = 1;
2662 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2663
2664 // Fill the audio frame with a sawtooth pattern.
2665 int16_t* ptr = frame.mutable_data();
2666 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2667 ptr[i] = 10000 * ((i % 3) - 1);
2668 }
2669
2670 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2671 apm->Initialize(processing_config);
2672
2673 // If not enabled, no metric should be reported.
2674 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2675 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2676
2677 // If enabled, metrics should be reported.
2678 config.voice_detection.enabled = true;
2679 apm->ApplyConfig(config);
2680 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2681 auto stats = apm->GetStatistics(false);
2682 EXPECT_TRUE(stats.voice_detected);
2683
2684 // If re-disabled, the value is again not reported.
2685 config.voice_detection.enabled = false;
2686 apm->ApplyConfig(config);
2687 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2688 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2689}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002690} // namespace webrtc