blob: 7015042395343f185e9c9f64822b6ae96282aac2 [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
348 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000349 }
350
351 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000352 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000353 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000354
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000355 // Used to select between int and float interface tests.
356 enum Format {
357 kIntFormat,
358 kFloatFormat
359 };
360
361 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000362 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800364 size_t num_input_channels,
365 size_t num_output_channels,
366 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000367 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000368 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000369 void EnableAllComponents();
370 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000371 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000372 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000373 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
374 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000375 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000376 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
377 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700378 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800379 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700380 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800381 void TestChangingForwardChannels(size_t num_in_channels,
382 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800384 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000386 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
387 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000388 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 int ProcessStreamChooser(Format format);
390 int AnalyzeReverseStreamChooser(Format format);
391 void ProcessDebugDump(const std::string& in_filename,
392 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800393 Format format,
394 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000395 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000396
397 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000398 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800399 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000400 AudioFrame* frame_;
401 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800402 std::unique_ptr<ChannelBuffer<float> > float_cb_;
403 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000404 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800405 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406 FILE* far_file_;
407 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000408 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000409};
410
411ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000412 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000413#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800414 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
415 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000416#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800417 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
418 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000419#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000421 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000422 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000423 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000424 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000425 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000426 out_file_(NULL) {
427 Config config;
428 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100429 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000430}
niklase@google.com470e71d2011-07-07 08:21:25 +0000431
432void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000433 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
435 frame_ = new AudioFrame();
436 revframe_ = new AudioFrame();
437
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000438 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
441void ApmTest::TearDown() {
442 if (frame_) {
443 delete frame_;
444 }
445 frame_ = NULL;
446
447 if (revframe_) {
448 delete revframe_;
449 }
450 revframe_ = NULL;
451
452 if (far_file_) {
453 ASSERT_EQ(0, fclose(far_file_));
454 }
455 far_file_ = NULL;
456
457 if (near_file_) {
458 ASSERT_EQ(0, fclose(near_file_));
459 }
460 near_file_ = NULL;
461
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000462 if (out_file_) {
463 ASSERT_EQ(0, fclose(out_file_));
464 }
465 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000468void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000469 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700470 ap->Initialize(
471 {{{frame_->sample_rate_hz_, frame_->num_channels_},
472 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700473 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700474 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000475}
476
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000477void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000478 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000479 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800480 size_t num_input_channels,
481 size_t num_output_channels,
482 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000483 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000484 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000486 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000487
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000488 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
489 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000490 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000491
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000492 if (far_file_) {
493 ASSERT_EQ(0, fclose(far_file_));
494 }
495 std::string filename = ResourceFilePath("far", sample_rate_hz);
496 far_file_ = fopen(filename.c_str(), "rb");
497 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
498 filename << "\n";
499
500 if (near_file_) {
501 ASSERT_EQ(0, fclose(near_file_));
502 }
503 filename = ResourceFilePath("near", sample_rate_hz);
504 near_file_ = fopen(filename.c_str(), "rb");
505 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
506 filename << "\n";
507
508 if (open_output_file) {
509 if (out_file_) {
510 ASSERT_EQ(0, fclose(out_file_));
511 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700512 filename = OutputFilePath(
513 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
514 reverse_sample_rate_hz, num_input_channels, num_output_channels,
515 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000516 out_file_ = fopen(filename.c_str(), "wb");
517 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
518 filename << "\n";
519 }
520}
521
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000522void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000523 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000524}
525
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000526bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
527 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000528 // The files always contain stereo audio.
529 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700530 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000531 sizeof(int16_t),
532 frame_size,
533 file);
534 if (read_count != frame_size) {
535 // Check that the file really ended.
536 EXPECT_NE(0, feof(file));
537 return false; // This is expected.
538 }
539
540 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700541 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000542 frame->samples_per_channel_);
543 }
544
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000545 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000546 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000548 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000549}
550
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
552 return ReadFrame(file, frame, NULL);
553}
554
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000555// If the end of the file has been reached, rewind it and attempt to read the
556// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
558 ChannelBuffer<float>* cb) {
559 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000560 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000562 }
563}
564
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
566 ReadFrameWithRewind(file, frame, NULL);
567}
568
andrew@webrtc.org81865342012-10-27 00:28:27 +0000569void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
570 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000571 EXPECT_EQ(apm_->kNoError,
572 apm_->gain_control()->set_stream_analog_level(127));
573 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000574}
575
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000576int ApmTest::ProcessStreamChooser(Format format) {
577 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000578 return apm_->ProcessStream(frame_);
579 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580 return apm_->ProcessStream(float_cb_->channels(),
581 frame_->samples_per_channel_,
582 frame_->sample_rate_hz_,
583 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000584 output_sample_rate_hz_,
585 LayoutFromChannels(num_output_channels_),
586 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587}
588
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000589int ApmTest::AnalyzeReverseStreamChooser(Format format) {
590 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700591 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 return apm_->AnalyzeReverseStream(
594 revfloat_cb_->channels(),
595 revframe_->samples_per_channel_,
596 revframe_->sample_rate_hz_,
597 LayoutFromChannels(revframe_->num_channels_));
598}
599
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000600void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
601 int delay_min, int delay_max) {
602 // The |revframe_| and |frame_| should include the proper frame information,
603 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000604 AudioFrame tmp_frame;
605 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000606 bool causal = true;
607
608 tmp_frame.CopyFrom(*revframe_);
609 SetFrameTo(&tmp_frame, 0);
610
611 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
612 // Initialize the |frame_queue| with empty frames.
613 int frame_delay = delay_ms / 10;
614 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000615 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000616 frame->CopyFrom(tmp_frame);
617 frame_queue.push(frame);
618 frame_delay++;
619 causal = false;
620 }
621 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000622 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000623 frame->CopyFrom(tmp_frame);
624 frame_queue.push(frame);
625 frame_delay--;
626 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000627 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
628 // need enough frames with audio to have reliable estimates, but as few as
629 // possible to keep processing time down. 4.5 seconds seemed to be a good
630 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000632 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000633 frame->CopyFrom(tmp_frame);
634 // Use the near end recording, since that has more speech in it.
635 ASSERT_TRUE(ReadFrame(near_file_, frame));
636 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000637 AudioFrame* reverse_frame = frame;
638 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 if (!causal) {
640 reverse_frame = frame_queue.front();
641 // When we call ProcessStream() the frame is modified, so we can't use the
642 // pointer directly when things are non-causal. Use an intermediate frame
643 // and copy the data.
644 process_frame = &tmp_frame;
645 process_frame->CopyFrom(*frame);
646 }
aluebsb0319552016-03-17 20:39:53 -0700647 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000648 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
649 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
650 frame = frame_queue.front();
651 frame_queue.pop();
652 delete frame;
653
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000654 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000655 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200656 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000657 }
658 }
659
660 rewind(near_file_);
661 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000662 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 frame_queue.pop();
664 delete frame;
665 }
666 // Calculate expected delay estimate and acceptable regions. Further,
667 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700668 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700669 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700670 const int expected_median =
671 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
672 const int expected_median_high = 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);
kwiberg07038562017-06-12 11:40:47 -0700675 const int expected_median_low = rtc::SafeClamp<int>(
676 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000678 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200679 AudioProcessingStats stats =
680 apm_->GetStatistics(true /* has_remote_tracks */);
681 ASSERT_TRUE(stats.delay_median_ms.has_value());
682 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 EXPECT_GE(expected_median_high, median);
684 EXPECT_LE(expected_median_low, median);
685}
686
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000687void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000688 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000689 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000691 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000692 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000693 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000694 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000695
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000696 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000697 EXPECT_EQ(apm_->kNoError,
698 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000700 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000701 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000702
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000703 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200704 AudioProcessing::Config apm_config = apm_->GetConfig();
705 apm_config.echo_canceller.enabled = true;
706 apm_config.echo_canceller.mobile_mode = false;
707 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000708 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000710 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000711 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712
713 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000714 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100715 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716
717 // Resets after successful ProcessStream().
718 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100720 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000721
722 // Other stream parameters set correctly.
723 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
724 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725 apm_->gain_control()->set_stream_analog_level(127));
Per Åhgren200feba2019-03-06 04:16:46 +0100726 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
728
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000731 AnalyzeReverseStreamChooser(format));
Per Åhgren200feba2019-03-06 04:16:46 +0100732 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000736 EXPECT_EQ(apm_->kNoError,
737 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000738 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000739}
740
741TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000743}
744
745TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000746 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000747}
748
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000749TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
750 EXPECT_EQ(0, apm_->delay_offset_ms());
751 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
752 EXPECT_EQ(50, apm_->stream_delay_ms());
753}
754
755TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
756 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000757 apm_->set_delay_offset_ms(100);
758 EXPECT_EQ(100, apm_->delay_offset_ms());
759 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000760 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000761 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
762 EXPECT_EQ(200, apm_->stream_delay_ms());
763
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000764 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000765 apm_->set_delay_offset_ms(-50);
766 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000767 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
768 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000769 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
770 EXPECT_EQ(50, apm_->stream_delay_ms());
771}
772
Michael Graczyk86c6d332015-07-23 11:41:39 -0700773void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800774 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700775 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000776 frame_->num_channels_ = num_channels;
777 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700778 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000779}
780
Michael Graczyk86c6d332015-07-23 11:41:39 -0700781void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800782 size_t num_in_channels,
783 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700784 AudioProcessing::Error expected_return) {
785 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
786 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
787
788 EXPECT_EQ(expected_return,
789 apm_->ProcessStream(float_cb_->channels(), input_stream,
790 output_stream, float_cb_->channels()));
791}
792
793void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800794 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700795 AudioProcessing::Error expected_return) {
796 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700797 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
798 {output_sample_rate_hz_, apm_->num_output_channels()},
799 {frame_->sample_rate_hz_, num_rev_channels},
800 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700801
ekmeyerson60d9b332015-08-14 10:35:55 -0700802 EXPECT_EQ(
803 expected_return,
804 apm_->ProcessReverseStream(
805 float_cb_->channels(), processing_config.reverse_input_stream(),
806 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700807}
808
809TEST_F(ApmTest, ChannelsInt16Interface) {
810 // Testing number of invalid and valid channels.
811 Init(16000, 16000, 16000, 4, 4, 4, false);
812
813 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
814
Peter Kasting69558702016-01-12 16:26:35 -0800815 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700816 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000817 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000818 }
819}
820
Michael Graczyk86c6d332015-07-23 11:41:39 -0700821TEST_F(ApmTest, Channels) {
822 // Testing number of invalid and valid channels.
823 Init(16000, 16000, 16000, 4, 4, 4, false);
824
825 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
826 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
827
Peter Kasting69558702016-01-12 16:26:35 -0800828 for (size_t i = 1; i < 4; ++i) {
829 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700830 // Output channels much be one or match input channels.
831 if (j == 1 || i == j) {
832 TestChangingForwardChannels(i, j, kNoErr);
833 TestChangingReverseChannels(i, kNoErr);
834
835 EXPECT_EQ(i, apm_->num_input_channels());
836 EXPECT_EQ(j, apm_->num_output_channels());
837 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800838 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700839 } else {
840 TestChangingForwardChannels(i, j,
841 AudioProcessing::kBadNumberChannelsError);
842 }
843 }
844 }
845}
846
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000847TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000848 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000849 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000850 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000851 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700852 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800853 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000854 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000855 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000856 }
857}
858
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000859TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000860 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +0000861 EXPECT_EQ(apm_->kNoError,
862 apm_->gain_control()->set_mode(
863 apm_->gain_control()->mode()));
864
865 GainControl::Mode mode[] = {
866 GainControl::kAdaptiveAnalog,
867 GainControl::kAdaptiveDigital,
868 GainControl::kFixedDigital
869 };
pkasting25702cb2016-01-08 13:50:27 -0800870 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 EXPECT_EQ(apm_->kNoError,
872 apm_->gain_control()->set_mode(mode[i]));
873 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
874 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100875 // Testing target levels
niklase@google.com470e71d2011-07-07 08:21:25 +0000876 EXPECT_EQ(apm_->kNoError,
877 apm_->gain_control()->set_target_level_dbfs(
878 apm_->gain_control()->target_level_dbfs()));
879
880 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -0800881 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000882 EXPECT_EQ(apm_->kNoError,
883 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
884 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
885 }
886
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100887 // Testing compression gains
niklase@google.com470e71d2011-07-07 08:21:25 +0000888 EXPECT_EQ(apm_->kNoError,
889 apm_->gain_control()->set_compression_gain_db(
890 apm_->gain_control()->compression_gain_db()));
891
892 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -0800893 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 EXPECT_EQ(apm_->kNoError,
895 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100896 ProcessStreamChooser(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000897 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
898 }
899
900 // Testing limiter off/on
901 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
902 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
903 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
904 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
905
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100906 // Testing level limits
niklase@google.com470e71d2011-07-07 08:21:25 +0000907 EXPECT_EQ(apm_->kNoError,
908 apm_->gain_control()->set_analog_level_limits(
909 apm_->gain_control()->analog_level_minimum(),
910 apm_->gain_control()->analog_level_maximum()));
911
912 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -0800913 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 EXPECT_EQ(apm_->kNoError,
915 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
916 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
917 }
918
919 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -0800920 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000921 EXPECT_EQ(apm_->kNoError,
922 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
923 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
924 }
925
926 // TODO(ajm): stream_is_saturated() and stream_analog_level()
927
928 // Turn AGC off
929 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
930 EXPECT_FALSE(apm_->gain_control()->is_enabled());
931}
932
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100933#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
934TEST_F(ApmTest, GainControlDiesOnTooLowTargetLevelDbfs) {
935 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(-1), "");
936}
937
938TEST_F(ApmTest, GainControlDiesOnTooHighTargetLevelDbfs) {
939 EXPECT_DEATH(apm_->gain_control()->set_target_level_dbfs(32), "");
940}
941
942TEST_F(ApmTest, GainControlDiesOnTooLowCompressionGainDb) {
943 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(-1), "");
944}
945
946TEST_F(ApmTest, GainControlDiesOnTooHighCompressionGainDb) {
947 EXPECT_DEATH(apm_->gain_control()->set_compression_gain_db(91), "");
948}
949
950TEST_F(ApmTest, GainControlDiesOnTooLowAnalogLevelLowerLimit) {
951 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(-1, 512), "");
952}
953
954TEST_F(ApmTest, GainControlDiesOnTooHighAnalogLevelUpperLimit) {
955 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 65536), "");
956}
957
958TEST_F(ApmTest, GainControlDiesOnInvertedAnalogLevelLimits) {
959 EXPECT_DEATH(apm_->gain_control()->set_analog_level_limits(512, 255), "");
960}
961
962TEST_F(ApmTest, ApmDiesOnTooLowAnalogLevel) {
963 apm_->gain_control()->set_analog_level_limits(255, 512);
964 EXPECT_DEATH(apm_->set_stream_analog_level(254), "");
965}
966
967TEST_F(ApmTest, ApmDiesOnTooHighAnalogLevel) {
968 apm_->gain_control()->set_analog_level_limits(255, 512);
969 EXPECT_DEATH(apm_->set_stream_analog_level(513), "");
970}
971#endif
972
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000973void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000974 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000975 EXPECT_EQ(apm_->kNoError,
976 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
977 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
978
979 int out_analog_level = 0;
980 for (int i = 0; i < 2000; ++i) {
981 ReadFrameWithRewind(near_file_, frame_);
982 // Ensure the audio is at a low level, so the AGC will try to increase it.
983 ScaleFrame(frame_, 0.25);
984
985 // Always pass in the same volume.
986 EXPECT_EQ(apm_->kNoError,
987 apm_->gain_control()->set_stream_analog_level(100));
988 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
989 out_analog_level = apm_->gain_control()->stream_analog_level();
990 }
991
992 // Ensure the AGC is still able to reach the maximum.
993 EXPECT_EQ(255, out_analog_level);
994}
995
996// Verifies that despite volume slider quantization, the AGC can continue to
997// increase its volume.
998TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -0800999 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001000 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1001 }
1002}
1003
1004void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001005 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001006 EXPECT_EQ(apm_->kNoError,
1007 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1008 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1009
1010 int out_analog_level = 100;
1011 for (int i = 0; i < 1000; ++i) {
1012 ReadFrameWithRewind(near_file_, frame_);
1013 // Ensure the audio is at a low level, so the AGC will try to increase it.
1014 ScaleFrame(frame_, 0.25);
1015
1016 EXPECT_EQ(apm_->kNoError,
1017 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1018 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1019 out_analog_level = apm_->gain_control()->stream_analog_level();
1020 }
1021
1022 // Ensure the volume was raised.
1023 EXPECT_GT(out_analog_level, 100);
1024 int highest_level_reached = out_analog_level;
1025 // Simulate a user manual volume change.
1026 out_analog_level = 100;
1027
1028 for (int i = 0; i < 300; ++i) {
1029 ReadFrameWithRewind(near_file_, frame_);
1030 ScaleFrame(frame_, 0.25);
1031
1032 EXPECT_EQ(apm_->kNoError,
1033 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1034 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1035 out_analog_level = apm_->gain_control()->stream_analog_level();
1036 // Check that AGC respected the manually adjusted volume.
1037 EXPECT_LT(out_analog_level, highest_level_reached);
1038 }
1039 // Check that the volume was still raised.
1040 EXPECT_GT(out_analog_level, 100);
1041}
1042
1043TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001044 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001045 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1046 }
1047}
1048
niklase@google.com470e71d2011-07-07 08:21:25 +00001049TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001050 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 NoiseSuppression::Level level[] = {
1052 NoiseSuppression::kLow,
1053 NoiseSuppression::kModerate,
1054 NoiseSuppression::kHigh,
1055 NoiseSuppression::kVeryHigh
1056 };
pkasting25702cb2016-01-08 13:50:27 -08001057 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 EXPECT_EQ(apm_->kNoError,
1059 apm_->noise_suppression()->set_level(level[i]));
1060 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1061 }
1062
andrew@webrtc.org648af742012-02-08 01:57:29 +00001063 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1065 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1066 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1067 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1068}
1069
1070TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001071 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001072 AudioProcessing::Config apm_config;
1073 apm_config.high_pass_filter.enabled = true;
1074 apm_->ApplyConfig(apm_config);
1075 apm_config.high_pass_filter.enabled = false;
1076 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001077}
1078
1079TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001080 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001081 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001082 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001083
1084 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1085
1086 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1087 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1088
1089 // Run this test in wideband; in super-wb, the splitting filter distorts the
1090 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001091 frame_->samples_per_channel_ = 160;
1092 frame_->num_channels_ = 2;
1093 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001094
1095 // Min value if no frames have been processed.
1096 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1097
1098 // Min value on zero frames.
1099 SetFrameTo(frame_, 0);
1100 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1101 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1102 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1103
1104 // Try a few RMS values.
1105 // (These also test that the value resets after retrieving it.)
1106 SetFrameTo(frame_, 32767);
1107 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1108 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1109 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1110
1111 SetFrameTo(frame_, 30000);
1112 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1113 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1114 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1115
1116 SetFrameTo(frame_, 10000);
1117 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1118 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1119 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1120
1121 SetFrameTo(frame_, 10);
1122 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1123 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1124 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1125
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001126 // Verify reset after enable/disable.
1127 SetFrameTo(frame_, 32767);
1128 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1129 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1130 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1131 SetFrameTo(frame_, 1);
1132 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1133 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1134
1135 // Verify reset after initialize.
1136 SetFrameTo(frame_, 32767);
1137 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1138 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1139 SetFrameTo(frame_, 1);
1140 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1141 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001142}
1143
1144TEST_F(ApmTest, VoiceDetection) {
1145 // Test external VAD
1146 EXPECT_EQ(apm_->kNoError,
1147 apm_->voice_detection()->set_stream_has_voice(true));
1148 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1149 EXPECT_EQ(apm_->kNoError,
1150 apm_->voice_detection()->set_stream_has_voice(false));
1151 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1152
andrew@webrtc.org648af742012-02-08 01:57:29 +00001153 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001154 VoiceDetection::Likelihood likelihood[] = {
1155 VoiceDetection::kVeryLowLikelihood,
1156 VoiceDetection::kLowLikelihood,
1157 VoiceDetection::kModerateLikelihood,
1158 VoiceDetection::kHighLikelihood
1159 };
pkasting25702cb2016-01-08 13:50:27 -08001160 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001161 EXPECT_EQ(apm_->kNoError,
1162 apm_->voice_detection()->set_likelihood(likelihood[i]));
1163 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1164 }
1165
1166 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001167 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001168 EXPECT_EQ(apm_->kBadParameterError,
1169 apm_->voice_detection()->set_frame_size_ms(12));
1170
andrew@webrtc.org648af742012-02-08 01:57:29 +00001171 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001172 for (int i = 10; i <= 30; i += 10) {
1173 EXPECT_EQ(apm_->kNoError,
1174 apm_->voice_detection()->set_frame_size_ms(i));
1175 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1176 }
1177 */
1178
andrew@webrtc.org648af742012-02-08 01:57:29 +00001179 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001180 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1181 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1182 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1183 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1184
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001185 // Test that AudioFrame activity is maintained when VAD is disabled.
1186 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1187 AudioFrame::VADActivity activity[] = {
1188 AudioFrame::kVadActive,
1189 AudioFrame::kVadPassive,
1190 AudioFrame::kVadUnknown
1191 };
pkasting25702cb2016-01-08 13:50:27 -08001192 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001193 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001194 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001195 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001196 }
1197
1198 // Test that AudioFrame activity is set when VAD is enabled.
1199 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001200 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001201 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001202 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001203
niklase@google.com470e71d2011-07-07 08:21:25 +00001204 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1205}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001206
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001207TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001208 AudioProcessing::Config config = apm_->GetConfig();
1209 EXPECT_FALSE(config.echo_canceller.enabled);
1210 EXPECT_FALSE(config.high_pass_filter.enabled);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001211 EXPECT_FALSE(config.level_estimation.enabled);
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001212 EXPECT_FALSE(config.voice_detection.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001213 EXPECT_FALSE(apm_->gain_control()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001214 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1215 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1216 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1217}
1218
1219TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001220 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001221 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001222 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001223 AudioFrame frame_copy;
1224 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001225 for (int j = 0; j < 1000; j++) {
1226 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1227 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001228 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1229 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001230 }
1231 }
1232}
1233
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001234TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1235 // Test that ProcessStream copies input to output even with no processing.
1236 const size_t kSamples = 80;
1237 const int sample_rate = 8000;
1238 const float src[kSamples] = {
1239 -1.0f, 0.0f, 1.0f
1240 };
1241 float dest[kSamples] = {};
1242
1243 auto src_channels = &src[0];
1244 auto dest_channels = &dest[0];
1245
Ivo Creusen62337e52018-01-09 14:17:33 +01001246 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001247 EXPECT_NOERR(apm_->ProcessStream(
1248 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1249 sample_rate, LayoutFromChannels(1), &dest_channels));
1250
1251 for (size_t i = 0; i < kSamples; ++i) {
1252 EXPECT_EQ(src[i], dest[i]);
1253 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001254
1255 // Same for ProcessReverseStream.
1256 float rev_dest[kSamples] = {};
1257 auto rev_dest_channels = &rev_dest[0];
1258
1259 StreamConfig input_stream = {sample_rate, 1};
1260 StreamConfig output_stream = {sample_rate, 1};
1261 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1262 output_stream, &rev_dest_channels));
1263
1264 for (size_t i = 0; i < kSamples; ++i) {
1265 EXPECT_EQ(src[i], rev_dest[i]);
1266 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001267}
1268
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001269TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1270 EnableAllComponents();
1271
pkasting25702cb2016-01-08 13:50:27 -08001272 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001273 Init(kProcessSampleRates[i],
1274 kProcessSampleRates[i],
1275 kProcessSampleRates[i],
1276 2,
1277 2,
1278 2,
1279 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001280 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001281 ASSERT_EQ(0, feof(far_file_));
1282 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001283 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001284 CopyLeftToRightChannel(revframe_->mutable_data(),
1285 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001286
aluebsb0319552016-03-17 20:39:53 -07001287 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001288
yujo36b1a5f2017-06-12 12:45:32 -07001289 CopyLeftToRightChannel(frame_->mutable_data(),
1290 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001291 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1292
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001293 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001294 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001295 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001296 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001297 analog_level = apm_->gain_control()->stream_analog_level();
1298
yujo36b1a5f2017-06-12 12:45:32 -07001299 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001300 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001301 rewind(far_file_);
1302 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001303 }
1304}
1305
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001306TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001307 // Verify the filter is not active through undistorted audio when:
1308 // 1. No components are enabled...
1309 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001310 AudioFrame frame_copy;
1311 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001312 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1313 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1314 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1315
1316 // 2. Only the level estimator is enabled...
1317 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001318 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001319 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1320 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1321 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1322 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1323 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1324
1325 // 3. Only VAD is enabled...
1326 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001327 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001328 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1329 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1330 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1331 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1332 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1333
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001334 // 4. Only GetStatistics-reporting VAD is enabled...
1335 SetFrameTo(frame_, 1000);
1336 frame_copy.CopyFrom(*frame_);
1337 auto apm_config = apm_->GetConfig();
1338 apm_config.voice_detection.enabled = true;
1339 apm_->ApplyConfig(apm_config);
1340 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1341 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1342 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1343 apm_config.voice_detection.enabled = false;
1344 apm_->ApplyConfig(apm_config);
1345
1346 // 5. Both VADs and the level estimator are enabled...
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001347 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001348 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001349 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1350 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001351 apm_config.voice_detection.enabled = true;
1352 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001353 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1354 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1355 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1356 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1357 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
Sam Zackrisson6c330ab2019-01-04 10:35:53 +01001358 apm_config.voice_detection.enabled = false;
1359 apm_->ApplyConfig(apm_config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001360
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001361 // Check the test is valid. We should have distortion from the filter
1362 // when AEC is enabled (which won't affect the audio).
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001363 apm_config.echo_canceller.enabled = true;
1364 apm_config.echo_canceller.mobile_mode = false;
1365 apm_->ApplyConfig(apm_config);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001366 frame_->samples_per_channel_ = 320;
1367 frame_->num_channels_ = 2;
1368 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001369 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001370 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001371 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001372 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1373 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1374}
1375
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001376#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1377void ApmTest::ProcessDebugDump(const std::string& in_filename,
1378 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001379 Format format,
1380 int max_size_bytes) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001381 TaskQueueForTest worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001382 FILE* in_file = fopen(in_filename.c_str(), "rb");
1383 ASSERT_TRUE(in_file != NULL);
1384 audioproc::Event event_msg;
1385 bool first_init = true;
1386
1387 while (ReadMessageFromFile(in_file, &event_msg)) {
1388 if (event_msg.type() == audioproc::Event::INIT) {
1389 const audioproc::Init msg = event_msg.init();
1390 int reverse_sample_rate = msg.sample_rate();
1391 if (msg.has_reverse_sample_rate()) {
1392 reverse_sample_rate = msg.reverse_sample_rate();
1393 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001394 int output_sample_rate = msg.sample_rate();
1395 if (msg.has_output_sample_rate()) {
1396 output_sample_rate = msg.output_sample_rate();
1397 }
1398
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001399 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001400 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001401 reverse_sample_rate,
1402 msg.num_input_channels(),
1403 msg.num_output_channels(),
1404 msg.num_reverse_channels(),
1405 false);
1406 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001407 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001408 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001409 auto aec_dump =
1410 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1411 EXPECT_TRUE(aec_dump);
1412 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001413 first_init = false;
1414 }
1415
1416 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1417 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1418
1419 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001420 ASSERT_EQ(revframe_->num_channels_,
1421 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001422 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001423 memcpy(revfloat_cb_->channels()[i],
1424 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001425 msg.channel(i).size());
1426 }
1427 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001428 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001429 if (format == kFloatFormat) {
1430 // We're using an int16 input file; convert to float.
1431 ConvertToFloat(*revframe_, revfloat_cb_.get());
1432 }
1433 }
1434 AnalyzeReverseStreamChooser(format);
1435
1436 } else if (event_msg.type() == audioproc::Event::STREAM) {
1437 const audioproc::Stream msg = event_msg.stream();
1438 // ProcessStream could have changed this for the output frame.
1439 frame_->num_channels_ = apm_->num_input_channels();
1440
1441 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1442 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001443 if (msg.has_keypress()) {
1444 apm_->set_stream_key_pressed(msg.keypress());
1445 } else {
1446 apm_->set_stream_key_pressed(true);
1447 }
1448
1449 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001450 ASSERT_EQ(frame_->num_channels_,
1451 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001452 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001453 memcpy(float_cb_->channels()[i],
1454 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001455 msg.input_channel(i).size());
1456 }
1457 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001458 memcpy(frame_->mutable_data(), msg.input_data().data(),
1459 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001460 if (format == kFloatFormat) {
1461 // We're using an int16 input file; convert to float.
1462 ConvertToFloat(*frame_, float_cb_.get());
1463 }
1464 }
1465 ProcessStreamChooser(format);
1466 }
1467 }
aleloif4dd1912017-06-15 01:55:38 -07001468 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001469 fclose(in_file);
1470}
1471
1472void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001473 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001474 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001475 std::string format_string;
1476 switch (format) {
1477 case kIntFormat:
1478 format_string = "_int";
1479 break;
1480 case kFloatFormat:
1481 format_string = "_float";
1482 break;
1483 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001484 const std::string ref_filename = test::TempFilename(
1485 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1486 const std::string out_filename = test::TempFilename(
1487 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001488 const std::string limited_filename = test::TempFilename(
1489 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1490 const size_t logging_limit_bytes = 100000;
1491 // We expect at least this many bytes in the created logfile.
1492 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001493 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001494 ProcessDebugDump(in_filename, ref_filename, format, -1);
1495 ProcessDebugDump(ref_filename, out_filename, format, -1);
1496 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001497
1498 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1499 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001500 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001501 ASSERT_TRUE(ref_file != NULL);
1502 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001503 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001504 std::unique_ptr<uint8_t[]> ref_bytes;
1505 std::unique_ptr<uint8_t[]> out_bytes;
1506 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001507
1508 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1509 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001510 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001511 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001512 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001513 while (ref_size > 0 && out_size > 0) {
1514 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001515 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001516 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001517 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001518 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001519 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001520 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1521 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001522 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001523 }
1524 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001525 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1526 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001527 EXPECT_NE(0, feof(ref_file));
1528 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001529 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001530 ASSERT_EQ(0, fclose(ref_file));
1531 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001532 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001533 remove(ref_filename.c_str());
1534 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001535 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001536}
1537
pbosc7a65692016-05-06 12:50:04 -07001538TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001539 VerifyDebugDumpTest(kIntFormat);
1540}
1541
pbosc7a65692016-05-06 12:50:04 -07001542TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001543 VerifyDebugDumpTest(kFloatFormat);
1544}
1545#endif
1546
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001547// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001548TEST_F(ApmTest, DebugDump) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001549 TaskQueueForTest worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001550 const std::string filename =
1551 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001552 {
1553 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1554 EXPECT_FALSE(aec_dump);
1555 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001556
1557#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1558 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001559 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001560
aleloif4dd1912017-06-15 01:55:38 -07001561 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1562 EXPECT_TRUE(aec_dump);
1563 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001564 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001565 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001566 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001567
1568 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001569 FILE* fid = fopen(filename.c_str(), "r");
1570 ASSERT_TRUE(fid != NULL);
1571
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001572 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001573 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001574 ASSERT_EQ(0, remove(filename.c_str()));
1575#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001576 // Verify the file has NOT been written.
1577 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1578#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1579}
1580
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001581// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001582TEST_F(ApmTest, DebugDumpFromFileHandle) {
Danil Chapovalov07122bc2019-03-26 14:37:01 +01001583 TaskQueueForTest worker_queue("ApmTest_worker_queue");
aleloif4dd1912017-06-15 01:55:38 -07001584
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001585 const std::string filename =
1586 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001587 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001588 ASSERT_TRUE(fid);
1589
1590#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1591 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001592 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001593
aleloif4dd1912017-06-15 01:55:38 -07001594 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1595 EXPECT_TRUE(aec_dump);
1596 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001597 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001598 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001599 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001600
1601 // Verify the file has been written.
1602 fid = fopen(filename.c_str(), "r");
1603 ASSERT_TRUE(fid != NULL);
1604
1605 // Clean it up.
1606 ASSERT_EQ(0, fclose(fid));
1607 ASSERT_EQ(0, remove(filename.c_str()));
1608#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001609 ASSERT_EQ(0, fclose(fid));
1610#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1611}
1612
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001613// TODO(andrew): Add a test to process a few frames with different combinations
1614// of enabled components.
1615
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001616TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001618 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001619
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001620 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001621 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001622 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001623 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001624 for (size_t i = 0; i < arraysize(kChannels); i++) {
1625 for (size_t j = 0; j < arraysize(kChannels); j++) {
1626 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001627 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001628 test->set_num_reverse_channels(kChannels[i]);
1629 test->set_num_input_channels(kChannels[j]);
1630 test->set_num_output_channels(kChannels[j]);
1631 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001632 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001633 }
1634 }
1635 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001636#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1637 // To test the extended filter mode.
1638 audioproc::Test* test = ref_data.add_test();
1639 test->set_num_reverse_channels(2);
1640 test->set_num_input_channels(2);
1641 test->set_num_output_channels(2);
1642 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1643 test->set_use_aec_extended_filter(true);
1644#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001645 }
1646
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001647 for (int i = 0; i < ref_data.test_size(); i++) {
1648 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001649
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001650 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001651 // TODO(ajm): We no longer allow different input and output channels. Skip
1652 // these tests for now, but they should be removed from the set.
1653 if (test->num_input_channels() != test->num_output_channels())
1654 continue;
1655
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001656 Config config;
1657 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001658 config.Set<ExtendedFilter>(
1659 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001660 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001661
1662 EnableAllComponents();
1663
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001664 Init(test->sample_rate(),
1665 test->sample_rate(),
1666 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001667 static_cast<size_t>(test->num_input_channels()),
1668 static_cast<size_t>(test->num_output_channels()),
1669 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001670 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001671
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001672 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001673 int has_voice_count = 0;
1674 int is_saturated_count = 0;
1675 int analog_level = 127;
1676 int analog_level_average = 0;
1677 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001678 float ns_speech_prob_average = 0.0f;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001679 float rms_dbfs_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001680#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1681 int stats_index = 0;
1682#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001683
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001684 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001685 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001686
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001687 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1688
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001689 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001690 EXPECT_EQ(apm_->kNoError,
1691 apm_->gain_control()->set_stream_analog_level(analog_level));
1692
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001693 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001694
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001695 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001696 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1697 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001698
1699 max_output_average += MaxAudioFrame(*frame_);
1700
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001701 analog_level = apm_->gain_control()->stream_analog_level();
1702 analog_level_average += analog_level;
1703 if (apm_->gain_control()->stream_is_saturated()) {
1704 is_saturated_count++;
1705 }
1706 if (apm_->voice_detection()->stream_has_voice()) {
1707 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001708 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001709 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001710 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001711 }
1712
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001713 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
Sam Zackrisson11b87032018-12-18 17:13:58 +01001714 AudioProcessingStats stats =
1715 apm_->GetStatistics(/*has_remote_tracks=*/false);
1716 rms_dbfs_average += *stats.output_rms_dbfs;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001717
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001718 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001719 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001720 sizeof(int16_t),
1721 frame_size,
1722 out_file_);
1723 ASSERT_EQ(frame_size, write_count);
1724
1725 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001726 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001727 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001728
1729#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1730 const int kStatsAggregationFrameNum = 100; // 1 second.
1731 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001732 // Get echo and delay metrics.
1733 AudioProcessingStats stats =
1734 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001735
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001736 // Echo metrics.
1737 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1738 const float echo_return_loss_enhancement =
1739 stats.echo_return_loss_enhancement.value_or(-1.0f);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001740 const float residual_echo_likelihood =
1741 stats.residual_echo_likelihood.value_or(-1.0f);
1742 const float residual_echo_likelihood_recent_max =
1743 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1744
minyue58530ed2016-05-24 05:50:12 -07001745 if (!write_ref_data) {
1746 const audioproc::Test::EchoMetrics& reference =
1747 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001748 constexpr float kEpsilon = 0.01;
1749 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
1750 EXPECT_NEAR(echo_return_loss_enhancement,
1751 reference.echo_return_loss_enhancement(), kEpsilon);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001752 EXPECT_NEAR(residual_echo_likelihood,
1753 reference.residual_echo_likelihood(), kEpsilon);
1754 EXPECT_NEAR(residual_echo_likelihood_recent_max,
1755 reference.residual_echo_likelihood_recent_max(),
1756 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07001757 ++stats_index;
1758 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001759 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
1760 message_echo->set_echo_return_loss(echo_return_loss);
1761 message_echo->set_echo_return_loss_enhancement(
1762 echo_return_loss_enhancement);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001763 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
1764 message_echo->set_residual_echo_likelihood_recent_max(
1765 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07001766 }
1767 }
1768#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001769 }
1770 max_output_average /= frame_count;
1771 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001772 ns_speech_prob_average /= frame_count;
Sam Zackrisson11b87032018-12-18 17:13:58 +01001773 rms_dbfs_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001774
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001775 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001776 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001777 // When running the test on a N7 we get a {2, 6} difference of
1778 // |has_voice_count| and |max_output_average| is up to 18 higher.
1779 // All numbers being consistently higher on N7 compare to ref_data.
1780 // TODO(bjornv): If we start getting more of these offsets on Android we
1781 // should consider a different approach. Either using one slack for all,
1782 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02001783#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001784 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001785 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001786 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02001787 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001788#else
1789 const int kHasVoiceCountOffset = 0;
1790 const int kHasVoiceCountNear = kIntNear;
1791 const int kMaxOutputAverageOffset = 0;
1792 const int kMaxOutputAverageNear = kIntNear;
1793#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001794 EXPECT_NEAR(test->has_voice_count(),
1795 has_voice_count - kHasVoiceCountOffset,
1796 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001797 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001798
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001799 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001800 EXPECT_NEAR(test->max_output_average(),
1801 max_output_average - kMaxOutputAverageOffset,
1802 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001803#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001804 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00001805 EXPECT_NEAR(test->ns_speech_probability_average(),
1806 ns_speech_prob_average,
1807 kFloatNear);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001808 EXPECT_NEAR(test->rms_dbfs_average(), rms_dbfs_average, kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001809#endif
1810 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001811 test->set_has_voice_count(has_voice_count);
1812 test->set_is_saturated_count(is_saturated_count);
1813
1814 test->set_analog_level_average(analog_level_average);
1815 test->set_max_output_average(max_output_average);
1816
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001817#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001818 EXPECT_LE(0.0f, ns_speech_prob_average);
1819 EXPECT_GE(1.0f, ns_speech_prob_average);
1820 test->set_ns_speech_probability_average(ns_speech_prob_average);
Sam Zackrisson11b87032018-12-18 17:13:58 +01001821 test->set_rms_dbfs_average(rms_dbfs_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001822#endif
1823 }
1824
1825 rewind(far_file_);
1826 rewind(near_file_);
1827 }
1828
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001829 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001830 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001831 }
1832}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001833
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001834TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
1835 struct ChannelFormat {
1836 AudioProcessing::ChannelLayout in_layout;
1837 AudioProcessing::ChannelLayout out_layout;
1838 };
1839 ChannelFormat cf[] = {
1840 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
1841 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
1842 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
1843 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001844
Ivo Creusen62337e52018-01-09 14:17:33 +01001845 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001846 // Enable one component just to ensure some processing takes place.
1847 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08001848 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001849 const int in_rate = 44100;
1850 const int out_rate = 48000;
1851 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
1852 TotalChannelsFromLayout(cf[i].in_layout));
1853 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
1854 ChannelsFromLayout(cf[i].out_layout));
1855
1856 // Run over a few chunks.
1857 for (int j = 0; j < 10; ++j) {
1858 EXPECT_NOERR(ap->ProcessStream(
1859 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001860 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00001861 in_rate,
1862 cf[i].in_layout,
1863 out_rate,
1864 cf[i].out_layout,
1865 out_cb.channels()));
1866 }
1867 }
1868}
1869
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001870// Compares the reference and test arrays over a region around the expected
1871// delay. Finds the highest SNR in that region and adds the variance and squared
1872// error results to the supplied accumulators.
1873void UpdateBestSNR(const float* ref,
1874 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08001875 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001876 int expected_delay,
1877 double* variance_acc,
1878 double* sq_error_acc) {
1879 double best_snr = std::numeric_limits<double>::min();
1880 double best_variance = 0;
1881 double best_sq_error = 0;
1882 // Search over a region of eight samples around the expected delay.
1883 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
1884 ++delay) {
1885 double sq_error = 0;
1886 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08001887 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001888 double error = test[i + delay] - ref[i];
1889 sq_error += error * error;
1890 variance += ref[i] * ref[i];
1891 }
1892
1893 if (sq_error == 0) {
1894 *variance_acc += variance;
1895 return;
1896 }
1897 double snr = variance / sq_error;
1898 if (snr > best_snr) {
1899 best_snr = snr;
1900 best_variance = variance;
1901 best_sq_error = sq_error;
1902 }
1903 }
1904
1905 *variance_acc += best_variance;
1906 *sq_error_acc += best_sq_error;
1907}
1908
1909// Used to test a multitude of sample rate and channel combinations. It works
1910// by first producing a set of reference files (in SetUpTestCase) that are
1911// assumed to be correct, as the used parameters are verified by other tests
1912// in this collection. Primarily the reference files are all produced at
1913// "native" rates which do not involve any resampling.
1914
1915// Each test pass produces an output file with a particular format. The output
1916// is matched against the reference file closest to its internal processing
1917// format. If necessary the output is resampled back to its process format.
1918// Due to the resampling distortion, we don't expect identical results, but
1919// enforce SNR thresholds which vary depending on the format. 0 is a special
1920// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02001921typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001922class AudioProcessingTest
Mirko Bonadei6a489f22019-04-09 15:11:12 +02001923 : public ::testing::TestWithParam<AudioProcessingTestData> {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001924 public:
1925 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02001926 : input_rate_(std::get<0>(GetParam())),
1927 output_rate_(std::get<1>(GetParam())),
1928 reverse_input_rate_(std::get<2>(GetParam())),
1929 reverse_output_rate_(std::get<3>(GetParam())),
1930 expected_snr_(std::get<4>(GetParam())),
1931 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001932
1933 virtual ~AudioProcessingTest() {}
1934
1935 static void SetUpTestCase() {
1936 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07001937 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08001938 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08001939 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
1940 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
1941 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001942 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07001943 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
1944 kNativeRates[i], kNumChannels[j], kNumChannels[j],
1945 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001946 }
1947 }
1948 }
1949 }
1950
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02001951 void TearDown() {
1952 // Remove "out" files after each test.
1953 ClearTempOutFiles();
1954 }
1955
pbos@webrtc.org200ac002015-02-03 14:14:01 +00001956 static void TearDownTestCase() {
1957 ClearTempFiles();
1958 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001959
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001960 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07001961 // to a file specified with |output_file_prefix|. Both forward and reverse
1962 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001963 static void ProcessFormat(int input_rate,
1964 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07001965 int reverse_input_rate,
1966 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001967 size_t num_input_channels,
1968 size_t num_output_channels,
1969 size_t num_reverse_input_channels,
1970 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02001971 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001972 Config config;
1973 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001974 std::unique_ptr<AudioProcessing> ap(
1975 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001976 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001977
ekmeyerson60d9b332015-08-14 10:35:55 -07001978 ProcessingConfig processing_config = {
1979 {{input_rate, num_input_channels},
1980 {output_rate, num_output_channels},
1981 {reverse_input_rate, num_reverse_input_channels},
1982 {reverse_output_rate, num_reverse_output_channels}}};
1983 ap->Initialize(processing_config);
1984
1985 FILE* far_file =
1986 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001987 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07001988 FILE* out_file =
1989 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
1990 reverse_input_rate, reverse_output_rate,
1991 num_input_channels, num_output_channels,
1992 num_reverse_input_channels,
1993 num_reverse_output_channels, kForward).c_str(),
1994 "wb");
1995 FILE* rev_out_file =
1996 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
1997 reverse_input_rate, reverse_output_rate,
1998 num_input_channels, num_output_channels,
1999 num_reverse_input_channels,
2000 num_reverse_output_channels, kReverse).c_str(),
2001 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002002 ASSERT_TRUE(far_file != NULL);
2003 ASSERT_TRUE(near_file != NULL);
2004 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002005 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002006
2007 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2008 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002009 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2010 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002011 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2012 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002013 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2014 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002015
2016 // Temporary buffers.
2017 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002018 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2019 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002020 std::unique_ptr<float[]> float_data(new float[max_length]);
2021 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002022
2023 int analog_level = 127;
2024 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2025 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002026 EXPECT_NOERR(ap->ProcessReverseStream(
2027 rev_cb.channels(), processing_config.reverse_input_stream(),
2028 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002029
2030 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002031 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2032
2033 EXPECT_NOERR(ap->ProcessStream(
2034 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002035 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002036 input_rate,
2037 LayoutFromChannels(num_input_channels),
2038 output_rate,
2039 LayoutFromChannels(num_output_channels),
2040 out_cb.channels()));
2041
ekmeyerson60d9b332015-08-14 10:35:55 -07002042 // Dump forward output to file.
2043 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002044 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002045 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002046
pkasting25702cb2016-01-08 13:50:27 -08002047 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002048 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002049 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002050
ekmeyerson60d9b332015-08-14 10:35:55 -07002051 // Dump reverse output to file.
2052 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2053 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002054 size_t rev_out_length =
2055 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002056
pkasting25702cb2016-01-08 13:50:27 -08002057 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002058 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2059 rev_out_file));
2060
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002061 analog_level = ap->gain_control()->stream_analog_level();
2062 }
2063 fclose(far_file);
2064 fclose(near_file);
2065 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002066 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002067 }
2068
2069 protected:
2070 int input_rate_;
2071 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002072 int reverse_input_rate_;
2073 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002074 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002075 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002076};
2077
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002078TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002079 struct ChannelFormat {
2080 int num_input;
2081 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002082 int num_reverse_input;
2083 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002084 };
2085 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002086 {1, 1, 1, 1},
2087 {1, 1, 2, 1},
2088 {2, 1, 1, 1},
2089 {2, 1, 2, 1},
2090 {2, 2, 1, 1},
2091 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002092 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002093
pkasting25702cb2016-01-08 13:50:27 -08002094 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002095 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2096 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2097 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002098
ekmeyerson60d9b332015-08-14 10:35:55 -07002099 // Verify output for both directions.
2100 std::vector<StreamDirection> stream_directions;
2101 stream_directions.push_back(kForward);
2102 stream_directions.push_back(kReverse);
2103 for (StreamDirection file_direction : stream_directions) {
2104 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2105 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2106 const int out_num =
2107 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2108 const double expected_snr =
2109 file_direction ? expected_reverse_snr_ : expected_snr_;
2110
2111 const int min_ref_rate = std::min(in_rate, out_rate);
2112 int ref_rate;
2113
2114 if (min_ref_rate > 32000) {
2115 ref_rate = 48000;
2116 } else if (min_ref_rate > 16000) {
2117 ref_rate = 32000;
2118 } else if (min_ref_rate > 8000) {
2119 ref_rate = 16000;
2120 } else {
2121 ref_rate = 8000;
2122 }
aluebs776593b2016-03-15 14:04:58 -07002123#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002124 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002125 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002126 }
2127#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002128 FILE* out_file = fopen(
2129 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2130 reverse_output_rate_, cf[i].num_input,
2131 cf[i].num_output, cf[i].num_reverse_input,
2132 cf[i].num_reverse_output, file_direction).c_str(),
2133 "rb");
2134 // The reference files always have matching input and output channels.
2135 FILE* ref_file = fopen(
2136 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2137 cf[i].num_output, cf[i].num_output,
2138 cf[i].num_reverse_output, cf[i].num_reverse_output,
2139 file_direction).c_str(),
2140 "rb");
2141 ASSERT_TRUE(out_file != NULL);
2142 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002143
pkasting25702cb2016-01-08 13:50:27 -08002144 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2145 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002146 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002147 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002148 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002149 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002150 // Data from the resampled output, in case the reference and output rates
2151 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002152 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002153
ekmeyerson60d9b332015-08-14 10:35:55 -07002154 PushResampler<float> resampler;
2155 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002156
ekmeyerson60d9b332015-08-14 10:35:55 -07002157 // Compute the resampling delay of the output relative to the reference,
2158 // to find the region over which we should search for the best SNR.
2159 float expected_delay_sec = 0;
2160 if (in_rate != ref_rate) {
2161 // Input resampling delay.
2162 expected_delay_sec +=
2163 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2164 }
2165 if (out_rate != ref_rate) {
2166 // Output resampling delay.
2167 expected_delay_sec +=
2168 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2169 // Delay of converting the output back to its processing rate for
2170 // testing.
2171 expected_delay_sec +=
2172 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2173 }
2174 int expected_delay =
Oleh Prypin708eccc2019-03-27 09:38:52 +01002175 std::floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002176
ekmeyerson60d9b332015-08-14 10:35:55 -07002177 double variance = 0;
2178 double sq_error = 0;
2179 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2180 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2181 float* out_ptr = out_data.get();
2182 if (out_rate != ref_rate) {
2183 // Resample the output back to its internal processing rate if
2184 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002185 ASSERT_EQ(ref_length,
2186 static_cast<size_t>(resampler.Resample(
2187 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002188 out_ptr = cmp_data.get();
2189 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002190
ekmeyerson60d9b332015-08-14 10:35:55 -07002191 // Update the |sq_error| and |variance| accumulators with the highest
2192 // SNR of reference vs output.
2193 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2194 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002195 }
2196
ekmeyerson60d9b332015-08-14 10:35:55 -07002197 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2198 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2199 << cf[i].num_input << ", " << cf[i].num_output << ", "
2200 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2201 << ", " << file_direction << "): ";
2202 if (sq_error > 0) {
2203 double snr = 10 * log10(variance / sq_error);
2204 EXPECT_GE(snr, expected_snr);
2205 EXPECT_NE(0, expected_snr);
2206 std::cout << "SNR=" << snr << " dB" << std::endl;
2207 } else {
aluebs776593b2016-03-15 14:04:58 -07002208 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002209 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002210
ekmeyerson60d9b332015-08-14 10:35:55 -07002211 fclose(out_file);
2212 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002213 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002214 }
2215}
2216
2217#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002218INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002219 CommonFormats,
2220 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002221 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2222 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2223 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2224 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2225 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2226 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2227 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2228 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2229 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2230 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2231 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2232 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002233
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002234 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2235 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2236 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2237 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2238 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2239 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2240 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2241 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2242 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2243 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2244 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2245 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002246
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002247 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2248 std::make_tuple(32000, 48000, 32000, 48000, 32, 30),
2249 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2250 std::make_tuple(32000, 44100, 48000, 44100, 19, 20),
2251 std::make_tuple(32000, 44100, 32000, 44100, 19, 15),
2252 std::make_tuple(32000, 44100, 16000, 44100, 19, 15),
2253 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2254 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2255 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2256 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2257 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2258 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002259
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002260 std::make_tuple(16000, 48000, 48000, 48000, 24, 0),
2261 std::make_tuple(16000, 48000, 32000, 48000, 24, 30),
2262 std::make_tuple(16000, 48000, 16000, 48000, 24, 20),
2263 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2264 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2265 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2266 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2267 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2268 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2269 std::make_tuple(16000, 16000, 48000, 16000, 39, 20),
2270 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2271 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002272
2273#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Mirko Bonadeic84f6612019-01-31 12:20:57 +01002274INSTANTIATE_TEST_SUITE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002275 CommonFormats,
2276 AudioProcessingTest,
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002277 ::testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2278 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2279 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2280 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2281 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2282 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2283 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2284 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2285 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2286 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2287 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2288 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002289
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002290 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2291 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2292 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2293 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2294 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2295 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2296 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2297 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2298 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2299 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2300 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2301 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002302
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002303 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2304 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2305 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2306 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2307 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2308 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2309 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2310 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2311 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2312 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2313 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2314 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002315
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002316 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2317 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2318 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2319 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2320 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2321 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2322 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2323 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2324 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2325 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2326 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2327 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002328#endif
2329
niklase@google.com470e71d2011-07-07 08:21:25 +00002330} // namespace
peahc19f3122016-10-07 14:54:10 -07002331
Alessio Bazzicac054e782018-04-16 12:10:09 +02002332TEST(RuntimeSettingTest, TestDefaultCtor) {
2333 auto s = AudioProcessing::RuntimeSetting();
2334 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2335}
2336
2337TEST(RuntimeSettingTest, TestCapturePreGain) {
2338 using Type = AudioProcessing::RuntimeSetting::Type;
2339 {
2340 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2341 EXPECT_EQ(Type::kCapturePreGain, s.type());
2342 float v;
2343 s.GetFloat(&v);
2344 EXPECT_EQ(1.25f, v);
2345 }
2346
2347#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2348 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2349#endif
2350}
2351
Per Åhgren6ee75fd2019-04-26 11:33:37 +02002352TEST(RuntimeSettingTest, TestCaptureFixedPostGain) {
2353 using Type = AudioProcessing::RuntimeSetting::Type;
2354 {
2355 auto s = AudioProcessing::RuntimeSetting::CreateCaptureFixedPostGain(1.25f);
2356 EXPECT_EQ(Type::kCaptureFixedPostGain, s.type());
2357 float v;
2358 s.GetFloat(&v);
2359 EXPECT_EQ(1.25f, v);
2360 }
2361
2362#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2363 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2364#endif
2365}
2366
Alessio Bazzicac054e782018-04-16 12:10:09 +02002367TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2368 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2369 auto s = AudioProcessing::RuntimeSetting();
2370 ASSERT_TRUE(q.Insert(&s));
2371 ASSERT_TRUE(q.Remove(&s));
2372 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2373}
2374
Sam Zackrisson0beac582017-09-25 12:04:02 +02002375TEST(ApmConfiguration, EnablePostProcessing) {
2376 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002377 auto mock_post_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002378 new ::testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002379 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002380 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002381 rtc::scoped_refptr<AudioProcessing> apm =
2382 AudioProcessingBuilder()
2383 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002384 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002385
2386 AudioFrame audio;
2387 audio.num_channels_ = 1;
2388 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2389
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002390 EXPECT_CALL(*mock_post_processor_ptr, Process(::testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002391 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002392}
2393
Alex Loiko5825aa62017-12-18 16:02:40 +01002394TEST(ApmConfiguration, EnablePreProcessing) {
2395 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002396 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002397 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko5825aa62017-12-18 16:02:40 +01002398 auto mock_pre_processor =
2399 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002400 rtc::scoped_refptr<AudioProcessing> apm =
2401 AudioProcessingBuilder()
2402 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002403 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002404
2405 AudioFrame audio;
2406 audio.num_channels_ = 1;
2407 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2408
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002409 EXPECT_CALL(*mock_pre_processor_ptr, Process(::testing::_)).Times(1);
Alex Loiko5825aa62017-12-18 16:02:40 +01002410 apm->ProcessReverseStream(&audio);
2411}
2412
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002413TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2414 // Verify that apm uses a capture analyzer if one is provided.
2415 auto mock_capture_analyzer_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002416 new ::testing::NiceMock<test::MockCustomAudioAnalyzer>();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002417 auto mock_capture_analyzer =
2418 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2419 rtc::scoped_refptr<AudioProcessing> apm =
2420 AudioProcessingBuilder()
2421 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2422 .Create();
2423
2424 AudioFrame audio;
2425 audio.num_channels_ = 1;
2426 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2427
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002428 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(::testing::_)).Times(1);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002429 apm->ProcessStream(&audio);
2430}
2431
Alex Loiko73ec0192018-05-15 10:52:28 +02002432TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2433 auto mock_pre_processor_ptr =
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002434 new ::testing::NiceMock<test::MockCustomProcessing>();
Alex Loiko73ec0192018-05-15 10:52:28 +02002435 auto mock_pre_processor =
2436 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2437 rtc::scoped_refptr<AudioProcessing> apm =
2438 AudioProcessingBuilder()
2439 .SetRenderPreProcessing(std::move(mock_pre_processor))
2440 .Create();
2441 apm->SetRuntimeSetting(
2442 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2443
2444 // RuntimeSettings forwarded during 'Process*Stream' calls.
2445 // Therefore we have to make one such call.
2446 AudioFrame audio;
2447 audio.num_channels_ = 1;
2448 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2449
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002450 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(::testing::_))
2451 .Times(1);
Alex Loiko73ec0192018-05-15 10:52:28 +02002452 apm->ProcessReverseStream(&audio);
2453}
2454
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002455class MyEchoControlFactory : public EchoControlFactory {
2456 public:
2457 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2458 auto ec = new test::MockEchoControl();
Mirko Bonadei6a489f22019-04-09 15:11:12 +02002459 EXPECT_CALL(*ec, AnalyzeRender(::testing::_)).Times(1);
2460 EXPECT_CALL(*ec, AnalyzeCapture(::testing::_)).Times(2);
2461 EXPECT_CALL(*ec, ProcessCapture(::testing::_, ::testing::_)).Times(2);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002462 return std::unique_ptr<EchoControl>(ec);
2463 }
2464};
2465
2466TEST(ApmConfiguration, EchoControlInjection) {
2467 // Verify that apm uses an injected echo controller if one is provided.
2468 webrtc::Config webrtc_config;
2469 std::unique_ptr<EchoControlFactory> echo_control_factory(
2470 new MyEchoControlFactory());
2471
Alex Loiko5825aa62017-12-18 16:02:40 +01002472 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002473 AudioProcessingBuilder()
2474 .SetEchoControlFactory(std::move(echo_control_factory))
2475 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002476
2477 AudioFrame audio;
2478 audio.num_channels_ = 1;
2479 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2480 apm->ProcessStream(&audio);
2481 apm->ProcessReverseStream(&audio);
2482 apm->ProcessStream(&audio);
2483}
Ivo Creusenae026092017-11-20 13:07:16 +01002484
Per Åhgren8607f842019-04-12 22:02:26 +02002485std::unique_ptr<AudioProcessing> CreateApm(bool mobile_aec) {
Ivo Creusenae026092017-11-20 13:07:16 +01002486 Config old_config;
Ivo Creusen62337e52018-01-09 14:17:33 +01002487 std::unique_ptr<AudioProcessing> apm(
2488 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002489 if (!apm) {
2490 return apm;
2491 }
2492
2493 ProcessingConfig processing_config = {
2494 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2495
2496 if (apm->Initialize(processing_config) != 0) {
2497 return nullptr;
2498 }
2499
2500 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002501 AudioProcessing::Config apm_config;
2502 apm_config.residual_echo_detector.enabled = true;
2503 apm_config.high_pass_filter.enabled = false;
2504 apm_config.gain_controller2.enabled = false;
2505 apm_config.echo_canceller.enabled = true;
Per Åhgren8607f842019-04-12 22:02:26 +02002506 apm_config.echo_canceller.mobile_mode = mobile_aec;
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002507 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002508 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2509 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2510 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2511 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002512 return apm;
2513}
2514
2515#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2516#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2517#else
2518#define MAYBE_ApmStatistics ApmStatistics
2519#endif
2520
Per Åhgren8607f842019-04-12 22:02:26 +02002521TEST(MAYBE_ApmStatistics, AECEnabledTest) {
2522 // Set up APM with AEC3 and process some audio.
2523 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
Ivo Creusenae026092017-11-20 13:07:16 +01002524 ASSERT_TRUE(apm);
Per Åhgren200feba2019-03-06 04:16:46 +01002525 AudioProcessing::Config apm_config;
2526 apm_config.echo_canceller.enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01002527 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002528
2529 // Set up an audioframe.
2530 AudioFrame frame;
2531 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002532 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002533
2534 // Fill the audio frame with a sawtooth pattern.
2535 int16_t* ptr = frame.mutable_data();
2536 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2537 ptr[i] = 10000 * ((i % 3) - 1);
2538 }
2539
2540 // Do some processing.
2541 for (int i = 0; i < 200; i++) {
2542 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2543 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2544 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2545 }
2546
2547 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002548 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002549 // We expect all statistics to be set and have a sensible value.
2550 ASSERT_TRUE(stats.residual_echo_likelihood);
2551 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2552 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2553 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2554 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2555 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2556 ASSERT_TRUE(stats.echo_return_loss);
2557 EXPECT_NE(*stats.echo_return_loss, -100.0);
2558 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2559 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
Ivo Creusenae026092017-11-20 13:07:16 +01002560
2561 // If there are no receive streams, we expect the stats not to be set. The
2562 // 'false' argument signals to APM that no receive streams are currently
2563 // active. In that situation the statistics would get stuck at their last
2564 // calculated value (AEC and echo detection need at least one stream in each
2565 // direction), so to avoid that, they should not be set by APM.
2566 stats = apm->GetStatistics(false);
2567 EXPECT_FALSE(stats.residual_echo_likelihood);
2568 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2569 EXPECT_FALSE(stats.echo_return_loss);
2570 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002571}
2572
2573TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2574 // Set up APM with AECM and process some audio.
Per Åhgren8607f842019-04-12 22:02:26 +02002575 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002576 ASSERT_TRUE(apm);
2577
2578 // Set up an audioframe.
2579 AudioFrame frame;
2580 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002581 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Ivo Creusenae026092017-11-20 13:07:16 +01002582
2583 // Fill the audio frame with a sawtooth pattern.
2584 int16_t* ptr = frame.mutable_data();
2585 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2586 ptr[i] = 10000 * ((i % 3) - 1);
2587 }
2588
2589 // Do some processing.
2590 for (int i = 0; i < 200; i++) {
2591 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2592 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2593 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2594 }
2595
2596 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002597 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002598 // We expect only the residual echo detector statistics to be set and have a
2599 // sensible value.
2600 EXPECT_TRUE(stats.residual_echo_likelihood);
2601 if (stats.residual_echo_likelihood) {
2602 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2603 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2604 }
2605 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2606 if (stats.residual_echo_likelihood_recent_max) {
2607 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2608 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2609 }
2610 EXPECT_FALSE(stats.echo_return_loss);
2611 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002612
2613 // If there are no receive streams, we expect the stats not to be set.
2614 stats = apm->GetStatistics(false);
2615 EXPECT_FALSE(stats.residual_echo_likelihood);
2616 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2617 EXPECT_FALSE(stats.echo_return_loss);
2618 EXPECT_FALSE(stats.echo_return_loss_enhancement);
Ivo Creusenae026092017-11-20 13:07:16 +01002619}
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002620
2621TEST(ApmStatistics, ReportOutputRmsDbfs) {
2622 ProcessingConfig processing_config = {
2623 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2624 AudioProcessing::Config config;
2625
2626 // Set up an audioframe.
2627 AudioFrame frame;
2628 frame.num_channels_ = 1;
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002629 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01002630
2631 // Fill the audio frame with a sawtooth pattern.
2632 int16_t* ptr = frame.mutable_data();
2633 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2634 ptr[i] = 10000 * ((i % 3) - 1);
2635 }
2636
2637 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2638 apm->Initialize(processing_config);
2639
2640 // If not enabled, no metric should be reported.
2641 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2642 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2643
2644 // If enabled, metrics should be reported.
2645 config.level_estimation.enabled = true;
2646 apm->ApplyConfig(config);
2647 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2648 auto stats = apm->GetStatistics(false);
2649 EXPECT_TRUE(stats.output_rms_dbfs);
2650 EXPECT_GE(*stats.output_rms_dbfs, 0);
2651
2652 // If re-disabled, the value is again not reported.
2653 config.level_estimation.enabled = false;
2654 apm->ApplyConfig(config);
2655 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2656 EXPECT_FALSE(apm->GetStatistics(false).output_rms_dbfs);
2657}
Sam Zackrisson4db667b2018-12-21 16:29:27 +01002658
2659TEST(ApmStatistics, ReportHasVoice) {
2660 ProcessingConfig processing_config = {
2661 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2662 AudioProcessing::Config config;
2663
2664 // Set up an audioframe.
2665 AudioFrame frame;
2666 frame.num_channels_ = 1;
2667 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate32kHz);
2668
2669 // Fill the audio frame with a sawtooth pattern.
2670 int16_t* ptr = frame.mutable_data();
2671 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2672 ptr[i] = 10000 * ((i % 3) - 1);
2673 }
2674
2675 std::unique_ptr<AudioProcessing> apm(AudioProcessingBuilder().Create());
2676 apm->Initialize(processing_config);
2677
2678 // If not enabled, no metric should be reported.
2679 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2680 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2681
2682 // If enabled, metrics should be reported.
2683 config.voice_detection.enabled = true;
2684 apm->ApplyConfig(config);
2685 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2686 auto stats = apm->GetStatistics(false);
2687 EXPECT_TRUE(stats.voice_detected);
2688
2689 // If re-disabled, the value is again not reported.
2690 config.voice_detection.enabled = false;
2691 apm->ApplyConfig(config);
2692 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2693 EXPECT_FALSE(apm->GetStatistics(false).voice_detected);
2694}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002695} // namespace webrtc