blob: dae5db2ac448e19a33b93f896bad6233b79fb961 [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>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/common.h"
25#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020026#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/test/protobuf_utils.h"
28#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/arraysize.h"
30#include "rtc_base/checks.h"
Minyue Li656d6092018-08-10 15:38:52 +020031#include "rtc_base/fakeclock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/gtest_prod_util.h"
33#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Niels Möller84255bb2017-10-06 13:43:23 +020037#include "rtc_base/refcountedobject.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020038#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020039#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020040#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/task_queue.h"
42#include "rtc_base/thread.h"
43#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gtest.h"
45#include "test/testsupport/fileutils.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
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000113int TruncateToMultipleOf10(int value) {
114 return (value / 10) * 10;
115}
116
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000121}
122
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800124 size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000126 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
127}
128
pkasting25702cb2016-01-08 13:50:27 -0800129void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 stereo[i * 2 + 1] = stereo[i * 2];
132 }
133}
134
yujo36b1a5f2017-06-12 12:45:32 -0700135void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800136 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000137 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
138 }
139}
140
141void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700142 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
144 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700145 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000146 }
147}
148
149void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800150 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700151 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 frame_data[i] = left;
154 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000155 }
156}
157
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700159 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
161 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700162 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000163 }
164}
165
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 return false;
172 }
yujo36b1a5f2017-06-12 12:45:32 -0700173 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000175 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000176 return false;
177 }
178 return true;
179}
180
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000181void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200182 AudioProcessing::Config apm_config = ap->GetConfig();
183 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200185 apm_config.echo_canceller.mobile_mode = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186
187 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
188 EXPECT_NOERR(ap->gain_control()->Enable(true));
189#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200190 apm_config.echo_canceller.mobile_mode = false;
Sergey Silkin271812a2018-09-13 14:55:17 +0000191 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
192 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
193 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
194 EXPECT_NOERR(ap->echo_cancellation()->set_suppression_level(
195 EchoCancellation::SuppressionLevel::kModerateSuppression));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000196
197 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
198 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
199 EXPECT_NOERR(ap->gain_control()->Enable(true));
200#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000201
peah8271d042016-11-22 07:24:52 -0800202 apm_config.high_pass_filter.enabled = true;
203 ap->ApplyConfig(apm_config);
204
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000205 EXPECT_NOERR(ap->level_estimator()->Enable(true));
206 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
207
208 EXPECT_NOERR(ap->voice_detection()->Enable(true));
209}
210
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000211// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000212template <class T>
213T AbsValue(T a) {
214 return a > 0 ? a: -a;
215}
216
217int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800218 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700219 const int16_t* frame_data = frame.data();
220 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800221 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700222 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000223 }
224
225 return max_data;
226}
227
Alex Loiko890988c2017-08-31 10:25:48 +0200228void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700229 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000231 ASSERT_TRUE(file != NULL);
232
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100233 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800235 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000238 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000240 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241 fclose(file);
242}
243
Alex Loiko890988c2017-08-31 10:25:48 +0200244std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200245 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000246 // Resource files are all stereo.
247 ss << name << sample_rate_hz / 1000 << "_stereo";
248 return test::ResourcePath(ss.str(), "pcm");
249}
250
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000251// Temporary filenames unique to this process. Used to be able to run these
252// tests in parallel as each process needs to be running in isolation they can't
253// have competing filenames.
254std::map<std::string, std::string> temp_filenames;
255
Alex Loiko890988c2017-08-31 10:25:48 +0200256std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000257 int input_rate,
258 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700259 int reverse_input_rate,
260 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800261 size_t num_input_channels,
262 size_t num_output_channels,
263 size_t num_reverse_input_channels,
264 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700265 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200266 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700267 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
268 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000269 if (num_output_channels == 1) {
270 ss << "mono";
271 } else if (num_output_channels == 2) {
272 ss << "stereo";
273 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700274 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000275 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700276 ss << output_rate / 1000;
277 if (num_reverse_output_channels == 1) {
278 ss << "_rmono";
279 } else if (num_reverse_output_channels == 2) {
280 ss << "_rstereo";
281 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700282 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700283 }
284 ss << reverse_output_rate / 1000;
285 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000286
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000287 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700288 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000289 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
290 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000291}
292
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000293void ClearTempFiles() {
294 for (auto& kv : temp_filenames)
295 remove(kv.second.c_str());
296}
297
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200298// Only remove "out" files. Keep "ref" files.
299void ClearTempOutFiles() {
300 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
301 const std::string& filename = it->first;
302 if (filename.substr(0, 3).compare("out") == 0) {
303 remove(it->second.c_str());
304 temp_filenames.erase(it++);
305 } else {
306 it++;
307 }
308 }
309}
310
Alex Loiko890988c2017-08-31 10:25:48 +0200311void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000312 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000313 ASSERT_TRUE(file != NULL);
314 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000315 fclose(file);
316}
317
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000318// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
319// stereo) file, converts to deinterleaved float (optionally downmixing) and
320// returns the result in |cb|. Returns false if the file ended (or on error) and
321// true otherwise.
322//
323// |int_data| and |float_data| are just temporary space that must be
324// sufficiently large to hold the 10 ms chunk.
325bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
326 ChannelBuffer<float>* cb) {
327 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000328 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000329 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
330 if (read_count != frame_size) {
331 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700332 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000333 return false; // This is expected.
334 }
335
336 S16ToFloat(int_data, frame_size, float_data);
337 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000338 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000339 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000340 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000341 cb->channels());
342 }
343
344 return true;
345}
346
niklase@google.com470e71d2011-07-07 08:21:25 +0000347class ApmTest : public ::testing::Test {
348 protected:
349 ApmTest();
350 virtual void SetUp();
351 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000352
353 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000354 }
355
356 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000357 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000358 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000359
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000360 // Used to select between int and float interface tests.
361 enum Format {
362 kIntFormat,
363 kFloatFormat
364 };
365
366 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000367 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000368 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800369 size_t num_input_channels,
370 size_t num_output_channels,
371 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000372 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000373 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000374 void EnableAllComponents();
375 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000376 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000377 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000378 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
379 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000380 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000381 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
382 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700383 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800384 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700385 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800386 void TestChangingForwardChannels(size_t num_in_channels,
387 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700388 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800389 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000391 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
392 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000394 int ProcessStreamChooser(Format format);
395 int AnalyzeReverseStreamChooser(Format format);
396 void ProcessDebugDump(const std::string& in_filename,
397 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800398 Format format,
399 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000400 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000401
402 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000403 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800404 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000405 AudioFrame* frame_;
406 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800407 std::unique_ptr<ChannelBuffer<float> > float_cb_;
408 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800410 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 FILE* far_file_;
412 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414};
415
416ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000417 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000418#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800419 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
420 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000421#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000422#if defined(WEBRTC_MAC)
423 // A different file for Mac is needed because on this platform the AEC
424 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800425 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
426 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000427#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800428 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
429 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000430#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000431#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000433 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000434 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000435 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000436 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000437 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000438 out_file_(NULL) {
439 Config config;
440 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100441 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000442}
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
444void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000445 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
447 frame_ = new AudioFrame();
448 revframe_ = new AudioFrame();
449
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000450 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
453void ApmTest::TearDown() {
454 if (frame_) {
455 delete frame_;
456 }
457 frame_ = NULL;
458
459 if (revframe_) {
460 delete revframe_;
461 }
462 revframe_ = NULL;
463
464 if (far_file_) {
465 ASSERT_EQ(0, fclose(far_file_));
466 }
467 far_file_ = NULL;
468
469 if (near_file_) {
470 ASSERT_EQ(0, fclose(near_file_));
471 }
472 near_file_ = NULL;
473
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000474 if (out_file_) {
475 ASSERT_EQ(0, fclose(out_file_));
476 }
477 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478}
479
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000480void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000481 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700482 ap->Initialize(
483 {{{frame_->sample_rate_hz_, frame_->num_channels_},
484 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700485 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700486 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000487}
488
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000489void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000490 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000491 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800492 size_t num_input_channels,
493 size_t num_output_channels,
494 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000495 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000496 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000497 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000498 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000499
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000500 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
501 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000502 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000503
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000504 if (far_file_) {
505 ASSERT_EQ(0, fclose(far_file_));
506 }
507 std::string filename = ResourceFilePath("far", sample_rate_hz);
508 far_file_ = fopen(filename.c_str(), "rb");
509 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
510 filename << "\n";
511
512 if (near_file_) {
513 ASSERT_EQ(0, fclose(near_file_));
514 }
515 filename = ResourceFilePath("near", sample_rate_hz);
516 near_file_ = fopen(filename.c_str(), "rb");
517 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
518 filename << "\n";
519
520 if (open_output_file) {
521 if (out_file_) {
522 ASSERT_EQ(0, fclose(out_file_));
523 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700524 filename = OutputFilePath(
525 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
526 reverse_sample_rate_hz, num_input_channels, num_output_channels,
527 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000528 out_file_ = fopen(filename.c_str(), "wb");
529 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
530 filename << "\n";
531 }
532}
533
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000534void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000536}
537
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000538bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
539 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000540 // The files always contain stereo audio.
541 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700542 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000543 sizeof(int16_t),
544 frame_size,
545 file);
546 if (read_count != frame_size) {
547 // Check that the file really ended.
548 EXPECT_NE(0, feof(file));
549 return false; // This is expected.
550 }
551
552 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700553 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000554 frame->samples_per_channel_);
555 }
556
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000557 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000558 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000560 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000561}
562
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000563bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
564 return ReadFrame(file, frame, NULL);
565}
566
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000567// If the end of the file has been reached, rewind it and attempt to read the
568// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
570 ChannelBuffer<float>* cb) {
571 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000572 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000574 }
575}
576
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
578 ReadFrameWithRewind(file, frame, NULL);
579}
580
andrew@webrtc.org81865342012-10-27 00:28:27 +0000581void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
582 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +0000583 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000584 EXPECT_EQ(apm_->kNoError,
585 apm_->gain_control()->set_stream_analog_level(127));
586 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000587}
588
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000589int ApmTest::ProcessStreamChooser(Format format) {
590 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591 return apm_->ProcessStream(frame_);
592 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 return apm_->ProcessStream(float_cb_->channels(),
594 frame_->samples_per_channel_,
595 frame_->sample_rate_hz_,
596 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000597 output_sample_rate_hz_,
598 LayoutFromChannels(num_output_channels_),
599 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000600}
601
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000602int ApmTest::AnalyzeReverseStreamChooser(Format format) {
603 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700604 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000605 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000606 return apm_->AnalyzeReverseStream(
607 revfloat_cb_->channels(),
608 revframe_->samples_per_channel_,
609 revframe_->sample_rate_hz_,
610 LayoutFromChannels(revframe_->num_channels_));
611}
612
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000613void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
614 int delay_min, int delay_max) {
615 // The |revframe_| and |frame_| should include the proper frame information,
616 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000617 AudioFrame tmp_frame;
618 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000619 bool causal = true;
620
621 tmp_frame.CopyFrom(*revframe_);
622 SetFrameTo(&tmp_frame, 0);
623
624 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
625 // Initialize the |frame_queue| with empty frames.
626 int frame_delay = delay_ms / 10;
627 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000628 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000629 frame->CopyFrom(tmp_frame);
630 frame_queue.push(frame);
631 frame_delay++;
632 causal = false;
633 }
634 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000635 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000636 frame->CopyFrom(tmp_frame);
637 frame_queue.push(frame);
638 frame_delay--;
639 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000640 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
641 // need enough frames with audio to have reliable estimates, but as few as
642 // possible to keep processing time down. 4.5 seconds seemed to be a good
643 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000644 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000645 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 frame->CopyFrom(tmp_frame);
647 // Use the near end recording, since that has more speech in it.
648 ASSERT_TRUE(ReadFrame(near_file_, frame));
649 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000650 AudioFrame* reverse_frame = frame;
651 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 if (!causal) {
653 reverse_frame = frame_queue.front();
654 // When we call ProcessStream() the frame is modified, so we can't use the
655 // pointer directly when things are non-causal. Use an intermediate frame
656 // and copy the data.
657 process_frame = &tmp_frame;
658 process_frame->CopyFrom(*frame);
659 }
aluebsb0319552016-03-17 20:39:53 -0700660 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000661 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
662 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
663 frame = frame_queue.front();
664 frame_queue.pop();
665 delete frame;
666
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000667 if (frame_count == 250) {
Sergey Silkin271812a2018-09-13 14:55:17 +0000668 int median;
669 int std;
670 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000671 // Discard the first delay metrics to avoid convergence effects.
Sergey Silkin271812a2018-09-13 14:55:17 +0000672 EXPECT_EQ(apm_->kNoError,
673 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
674 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000675 }
676 }
677
678 rewind(near_file_);
679 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000680 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000681 frame_queue.pop();
682 delete frame;
683 }
684 // Calculate expected delay estimate and acceptable regions. Further,
685 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700687 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700688 const int expected_median =
689 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
690 const int expected_median_high = rtc::SafeClamp<int>(
691 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700692 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700693 const int expected_median_low = rtc::SafeClamp<int>(
694 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700695 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000696 // Verify delay metrics.
Sergey Silkin271812a2018-09-13 14:55:17 +0000697 int median;
698 int std;
699 float poor_fraction;
700 EXPECT_EQ(apm_->kNoError,
701 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
702 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000703 EXPECT_GE(expected_median_high, median);
704 EXPECT_LE(expected_median_low, median);
705}
706
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000707void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000709 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000710
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000711 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000713 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000714 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000715
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 EXPECT_EQ(apm_->kNoError,
718 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000720 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000721 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000723 // Other stream parameters set correctly.
Sergey Silkin271812a2018-09-13 14:55:17 +0000724 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
725 EXPECT_EQ(apm_->kNoError,
726 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sergey Silkin271812a2018-09-13 14:55:17 +0000728 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000731 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
Sergey Silkin271812a2018-09-13 14:55:17 +0000732 EXPECT_EQ(apm_->kNoError,
733 apm_->echo_cancellation()->enable_drift_compensation(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734
735 // -- Missing delay --
Sergey Silkin271812a2018-09-13 14:55:17 +0000736 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000737 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000738 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000740
741 // Resets after successful ProcessStream().
742 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000745 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000746
747 // Other stream parameters set correctly.
748 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
749 EXPECT_EQ(apm_->kNoError,
Sergey Silkin271812a2018-09-13 14:55:17 +0000750 apm_->echo_cancellation()->enable_drift_compensation(true));
751 apm_->echo_cancellation()->set_stream_drift_samples(0);
752 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000754 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000756 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
757
Sergey Silkin271812a2018-09-13 14:55:17 +0000758 // -- Missing drift --
759 EXPECT_EQ(apm_->kStreamParameterNotSetError,
760 ProcessStreamChooser(format));
761
762 // Resets after successful ProcessStream().
763 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
764 apm_->echo_cancellation()->set_stream_drift_samples(0);
765 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
766 EXPECT_EQ(apm_->kStreamParameterNotSetError,
767 ProcessStreamChooser(format));
768
769 // Other stream parameters set correctly.
770 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
771 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
772 EXPECT_EQ(apm_->kNoError,
773 apm_->gain_control()->set_stream_analog_level(127));
774 EXPECT_EQ(apm_->kStreamParameterNotSetError,
775 ProcessStreamChooser(format));
776
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000777 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000778 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000779 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000783 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000784 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
Sergey Silkin271812a2018-09-13 14:55:17 +0000785 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 EXPECT_EQ(apm_->kNoError,
787 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000788 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000789}
790
791TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000792 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000793}
794
795TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000796 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000799TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
800 EXPECT_EQ(0, apm_->delay_offset_ms());
801 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
802 EXPECT_EQ(50, apm_->stream_delay_ms());
803}
804
805TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
806 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000807 apm_->set_delay_offset_ms(100);
808 EXPECT_EQ(100, apm_->delay_offset_ms());
809 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000810 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000811 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
812 EXPECT_EQ(200, apm_->stream_delay_ms());
813
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000814 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000815 apm_->set_delay_offset_ms(-50);
816 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000817 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
818 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000819 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
820 EXPECT_EQ(50, apm_->stream_delay_ms());
821}
822
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800824 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000826 frame_->num_channels_ = num_channels;
827 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700828 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000829}
830
Michael Graczyk86c6d332015-07-23 11:41:39 -0700831void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800832 size_t num_in_channels,
833 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700834 AudioProcessing::Error expected_return) {
835 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
836 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
837
838 EXPECT_EQ(expected_return,
839 apm_->ProcessStream(float_cb_->channels(), input_stream,
840 output_stream, float_cb_->channels()));
841}
842
843void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800844 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700845 AudioProcessing::Error expected_return) {
846 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700847 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
848 {output_sample_rate_hz_, apm_->num_output_channels()},
849 {frame_->sample_rate_hz_, num_rev_channels},
850 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851
ekmeyerson60d9b332015-08-14 10:35:55 -0700852 EXPECT_EQ(
853 expected_return,
854 apm_->ProcessReverseStream(
855 float_cb_->channels(), processing_config.reverse_input_stream(),
856 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700857}
858
859TEST_F(ApmTest, ChannelsInt16Interface) {
860 // Testing number of invalid and valid channels.
861 Init(16000, 16000, 16000, 4, 4, 4, false);
862
863 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
864
Peter Kasting69558702016-01-12 16:26:35 -0800865 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700866 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 }
869}
870
Michael Graczyk86c6d332015-07-23 11:41:39 -0700871TEST_F(ApmTest, Channels) {
872 // Testing number of invalid and valid channels.
873 Init(16000, 16000, 16000, 4, 4, 4, false);
874
875 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
876 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
877
Peter Kasting69558702016-01-12 16:26:35 -0800878 for (size_t i = 1; i < 4; ++i) {
879 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700880 // Output channels much be one or match input channels.
881 if (j == 1 || i == j) {
882 TestChangingForwardChannels(i, j, kNoErr);
883 TestChangingReverseChannels(i, kNoErr);
884
885 EXPECT_EQ(i, apm_->num_input_channels());
886 EXPECT_EQ(j, apm_->num_output_channels());
887 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800888 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700889 } else {
890 TestChangingForwardChannels(i, j,
891 AudioProcessing::kBadNumberChannelsError);
892 }
893 }
894 }
895}
896
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000897TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000899 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000900 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700902 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800903 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000904 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000905 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000906 }
907}
908
Sergey Silkin271812a2018-09-13 14:55:17 +0000909TEST_F(ApmTest, EchoCancellation) {
910 EXPECT_EQ(apm_->kNoError,
911 apm_->echo_cancellation()->enable_drift_compensation(true));
912 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
913 EXPECT_EQ(apm_->kNoError,
914 apm_->echo_cancellation()->enable_drift_compensation(false));
915 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
916
917 EchoCancellation::SuppressionLevel level[] = {
918 EchoCancellation::kLowSuppression,
919 EchoCancellation::kModerateSuppression,
920 EchoCancellation::kHighSuppression,
921 };
922 for (size_t i = 0; i < arraysize(level); i++) {
923 EXPECT_EQ(apm_->kNoError,
924 apm_->echo_cancellation()->set_suppression_level(level[i]));
925 EXPECT_EQ(level[i],
926 apm_->echo_cancellation()->suppression_level());
927 }
928
929 EchoCancellation::Metrics metrics;
930 EXPECT_EQ(apm_->kNotEnabledError,
931 apm_->echo_cancellation()->GetMetrics(&metrics));
932
933 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
934 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
935
936 EXPECT_EQ(apm_->kNoError,
937 apm_->echo_cancellation()->enable_metrics(true));
938 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
939 EXPECT_EQ(apm_->kNoError,
940 apm_->echo_cancellation()->enable_metrics(false));
941 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
942
943 EXPECT_EQ(apm_->kNoError,
944 apm_->echo_cancellation()->enable_delay_logging(true));
945 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
946 EXPECT_EQ(apm_->kNoError,
947 apm_->echo_cancellation()->enable_delay_logging(false));
948 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
949
950 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
951 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
952
953 int median = 0;
954 int std = 0;
955 float poor_fraction = 0;
956 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
957 &median, &std, &poor_fraction));
958
959 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
960 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
961 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
962 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
963
964 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
965 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
966 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
967 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
968 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
969 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
970}
971
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000972TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000973 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000974 // Enable AEC only.
Sergey Silkin271812a2018-09-13 14:55:17 +0000975 EXPECT_EQ(apm_->kNoError,
976 apm_->echo_cancellation()->enable_drift_compensation(false));
977 EXPECT_EQ(apm_->kNoError,
978 apm_->echo_cancellation()->enable_metrics(false));
979 EXPECT_EQ(apm_->kNoError,
980 apm_->echo_cancellation()->enable_delay_logging(true));
981 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000982 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700983 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000984 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000985
986 // Internally in the AEC the amount of lookahead the delay estimation can
987 // handle is 15 blocks and the maximum delay is set to 60 blocks.
988 const int kLookaheadBlocks = 15;
989 const int kMaxDelayBlocks = 60;
990 // The AEC has a startup time before it actually starts to process. This
991 // procedure can flush the internal far-end buffer, which of course affects
992 // the delay estimation. Therefore, we set a system_delay high enough to
993 // avoid that. The smallest system_delay you can report without flushing the
994 // buffer is 66 ms in 8 kHz.
995 //
996 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
997 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
998 // delay estimation. This should be noted though. In case of test failure,
999 // this could be the cause.
1000 const int kSystemDelayMs = 66;
1001 // Test a couple of corner cases and verify that the estimated delay is
1002 // within a valid region (set to +-1.5 blocks). Note that these cases are
1003 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001004 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001005 Init(kProcessSampleRates[i],
1006 kProcessSampleRates[i],
1007 kProcessSampleRates[i],
1008 2,
1009 2,
1010 2,
1011 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001012 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001013 const int num_ms_per_block =
1014 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001015 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1016 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1017
1018 // 1) Verify correct delay estimate at lookahead boundary.
1019 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1020 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1021 delay_max_ms);
1022 // 2) A delay less than maximum lookahead should give an delay estimate at
1023 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1024 delay_ms -= 20;
1025 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1026 delay_max_ms);
1027 // 3) Three values around zero delay. Note that we need to compensate for
1028 // the fake system_delay.
1029 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1030 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1031 delay_max_ms);
1032 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1033 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1034 delay_max_ms);
1035 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1036 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1037 delay_max_ms);
1038 // 4) Verify correct delay estimate at maximum delay boundary.
1039 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1040 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1041 delay_max_ms);
1042 // 5) A delay above the maximum delay should give an estimate at the
1043 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1044 delay_ms += 20;
1045 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1046 delay_max_ms);
1047 }
1048}
1049
niklase@google.com470e71d2011-07-07 08:21:25 +00001050TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001052 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1054 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1055
niklase@google.com470e71d2011-07-07 08:21:25 +00001056 // Toggle routing modes
1057 EchoControlMobile::RoutingMode mode[] = {
1058 EchoControlMobile::kQuietEarpieceOrHeadset,
1059 EchoControlMobile::kEarpiece,
1060 EchoControlMobile::kLoudEarpiece,
1061 EchoControlMobile::kSpeakerphone,
1062 EchoControlMobile::kLoudSpeakerphone,
1063 };
pkasting25702cb2016-01-08 13:50:27 -08001064 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 EXPECT_EQ(apm_->kNoError,
1066 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1067 EXPECT_EQ(mode[i],
1068 apm_->echo_control_mobile()->routing_mode());
1069 }
1070 // Turn comfort noise off/on
1071 EXPECT_EQ(apm_->kNoError,
1072 apm_->echo_control_mobile()->enable_comfort_noise(false));
1073 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1074 EXPECT_EQ(apm_->kNoError,
1075 apm_->echo_control_mobile()->enable_comfort_noise(true));
1076 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001077 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001078 const size_t echo_path_size =
1079 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001080 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1081 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001082 EXPECT_EQ(apm_->kNullPointerError,
1083 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1084 EXPECT_EQ(apm_->kNullPointerError,
1085 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1086 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001087 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001088 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001089 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001091 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001092 echo_path_in[i] = echo_path_out[i] + 1;
1093 }
1094 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001095 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001096 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001097 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1098 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001099 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001100 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1101 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001102 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001103 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1104 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001105
1106 // Process a few frames with NS in the default disabled state. This exercises
1107 // a different codepath than with it enabled.
1108 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1109 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1110 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1111 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1112
niklase@google.com470e71d2011-07-07 08:21:25 +00001113 // Turn AECM off
1114 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1115 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1116}
1117
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001118TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001120 EXPECT_EQ(apm_->kNoError,
1121 apm_->gain_control()->set_mode(
1122 apm_->gain_control()->mode()));
1123
1124 GainControl::Mode mode[] = {
1125 GainControl::kAdaptiveAnalog,
1126 GainControl::kAdaptiveDigital,
1127 GainControl::kFixedDigital
1128 };
pkasting25702cb2016-01-08 13:50:27 -08001129 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001130 EXPECT_EQ(apm_->kNoError,
1131 apm_->gain_control()->set_mode(mode[i]));
1132 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1133 }
1134 // Testing invalid target levels
1135 EXPECT_EQ(apm_->kBadParameterError,
1136 apm_->gain_control()->set_target_level_dbfs(-3));
1137 EXPECT_EQ(apm_->kBadParameterError,
1138 apm_->gain_control()->set_target_level_dbfs(-40));
1139 // Testing valid target levels
1140 EXPECT_EQ(apm_->kNoError,
1141 apm_->gain_control()->set_target_level_dbfs(
1142 apm_->gain_control()->target_level_dbfs()));
1143
1144 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001145 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001146 EXPECT_EQ(apm_->kNoError,
1147 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1148 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1149 }
1150
1151 // Testing invalid compression gains
1152 EXPECT_EQ(apm_->kBadParameterError,
1153 apm_->gain_control()->set_compression_gain_db(-1));
1154 EXPECT_EQ(apm_->kBadParameterError,
1155 apm_->gain_control()->set_compression_gain_db(100));
1156
1157 // Testing valid compression gains
1158 EXPECT_EQ(apm_->kNoError,
1159 apm_->gain_control()->set_compression_gain_db(
1160 apm_->gain_control()->compression_gain_db()));
1161
1162 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001163 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001164 EXPECT_EQ(apm_->kNoError,
1165 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1166 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1167 }
1168
1169 // Testing limiter off/on
1170 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1171 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1172 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1173 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1174
1175 // Testing invalid level limits
1176 EXPECT_EQ(apm_->kBadParameterError,
1177 apm_->gain_control()->set_analog_level_limits(-1, 512));
1178 EXPECT_EQ(apm_->kBadParameterError,
1179 apm_->gain_control()->set_analog_level_limits(100000, 512));
1180 EXPECT_EQ(apm_->kBadParameterError,
1181 apm_->gain_control()->set_analog_level_limits(512, -1));
1182 EXPECT_EQ(apm_->kBadParameterError,
1183 apm_->gain_control()->set_analog_level_limits(512, 100000));
1184 EXPECT_EQ(apm_->kBadParameterError,
1185 apm_->gain_control()->set_analog_level_limits(512, 255));
1186
1187 // Testing valid level limits
1188 EXPECT_EQ(apm_->kNoError,
1189 apm_->gain_control()->set_analog_level_limits(
1190 apm_->gain_control()->analog_level_minimum(),
1191 apm_->gain_control()->analog_level_maximum()));
1192
1193 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001194 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001195 EXPECT_EQ(apm_->kNoError,
1196 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1197 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1198 }
1199
1200 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001201 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001202 EXPECT_EQ(apm_->kNoError,
1203 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1204 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1205 }
1206
1207 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1208
1209 // Turn AGC off
1210 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1211 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1212}
1213
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001214void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001215 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001216 EXPECT_EQ(apm_->kNoError,
1217 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1218 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1219
1220 int out_analog_level = 0;
1221 for (int i = 0; i < 2000; ++i) {
1222 ReadFrameWithRewind(near_file_, frame_);
1223 // Ensure the audio is at a low level, so the AGC will try to increase it.
1224 ScaleFrame(frame_, 0.25);
1225
1226 // Always pass in the same volume.
1227 EXPECT_EQ(apm_->kNoError,
1228 apm_->gain_control()->set_stream_analog_level(100));
1229 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1230 out_analog_level = apm_->gain_control()->stream_analog_level();
1231 }
1232
1233 // Ensure the AGC is still able to reach the maximum.
1234 EXPECT_EQ(255, out_analog_level);
1235}
1236
1237// Verifies that despite volume slider quantization, the AGC can continue to
1238// increase its volume.
1239TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001240 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001241 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1242 }
1243}
1244
1245void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001246 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001247 EXPECT_EQ(apm_->kNoError,
1248 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1249 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1250
1251 int out_analog_level = 100;
1252 for (int i = 0; i < 1000; ++i) {
1253 ReadFrameWithRewind(near_file_, frame_);
1254 // Ensure the audio is at a low level, so the AGC will try to increase it.
1255 ScaleFrame(frame_, 0.25);
1256
1257 EXPECT_EQ(apm_->kNoError,
1258 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1259 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1260 out_analog_level = apm_->gain_control()->stream_analog_level();
1261 }
1262
1263 // Ensure the volume was raised.
1264 EXPECT_GT(out_analog_level, 100);
1265 int highest_level_reached = out_analog_level;
1266 // Simulate a user manual volume change.
1267 out_analog_level = 100;
1268
1269 for (int i = 0; i < 300; ++i) {
1270 ReadFrameWithRewind(near_file_, frame_);
1271 ScaleFrame(frame_, 0.25);
1272
1273 EXPECT_EQ(apm_->kNoError,
1274 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1275 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1276 out_analog_level = apm_->gain_control()->stream_analog_level();
1277 // Check that AGC respected the manually adjusted volume.
1278 EXPECT_LT(out_analog_level, highest_level_reached);
1279 }
1280 // Check that the volume was still raised.
1281 EXPECT_GT(out_analog_level, 100);
1282}
1283
1284TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001285 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001286 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1287 }
1288}
1289
niklase@google.com470e71d2011-07-07 08:21:25 +00001290TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001291 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001292 NoiseSuppression::Level level[] = {
1293 NoiseSuppression::kLow,
1294 NoiseSuppression::kModerate,
1295 NoiseSuppression::kHigh,
1296 NoiseSuppression::kVeryHigh
1297 };
pkasting25702cb2016-01-08 13:50:27 -08001298 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001299 EXPECT_EQ(apm_->kNoError,
1300 apm_->noise_suppression()->set_level(level[i]));
1301 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1302 }
1303
andrew@webrtc.org648af742012-02-08 01:57:29 +00001304 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1306 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1307 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1308 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1309}
1310
1311TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001312 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001313 AudioProcessing::Config apm_config;
1314 apm_config.high_pass_filter.enabled = true;
1315 apm_->ApplyConfig(apm_config);
1316 apm_config.high_pass_filter.enabled = false;
1317 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001318}
1319
1320TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001321 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001322 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001324
1325 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1326
1327 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1328 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1329
1330 // Run this test in wideband; in super-wb, the splitting filter distorts the
1331 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001332 frame_->samples_per_channel_ = 160;
1333 frame_->num_channels_ = 2;
1334 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001335
1336 // Min value if no frames have been processed.
1337 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1338
1339 // Min value on zero frames.
1340 SetFrameTo(frame_, 0);
1341 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1342 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1343 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1344
1345 // Try a few RMS values.
1346 // (These also test that the value resets after retrieving it.)
1347 SetFrameTo(frame_, 32767);
1348 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1349 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1350 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1351
1352 SetFrameTo(frame_, 30000);
1353 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1354 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1355 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1356
1357 SetFrameTo(frame_, 10000);
1358 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1359 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1360 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1361
1362 SetFrameTo(frame_, 10);
1363 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1364 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1365 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1366
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001367 // Verify reset after enable/disable.
1368 SetFrameTo(frame_, 32767);
1369 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1370 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1371 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1372 SetFrameTo(frame_, 1);
1373 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1374 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1375
1376 // Verify reset after initialize.
1377 SetFrameTo(frame_, 32767);
1378 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1379 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1380 SetFrameTo(frame_, 1);
1381 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1382 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001383}
1384
1385TEST_F(ApmTest, VoiceDetection) {
1386 // Test external VAD
1387 EXPECT_EQ(apm_->kNoError,
1388 apm_->voice_detection()->set_stream_has_voice(true));
1389 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1390 EXPECT_EQ(apm_->kNoError,
1391 apm_->voice_detection()->set_stream_has_voice(false));
1392 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1393
andrew@webrtc.org648af742012-02-08 01:57:29 +00001394 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001395 VoiceDetection::Likelihood likelihood[] = {
1396 VoiceDetection::kVeryLowLikelihood,
1397 VoiceDetection::kLowLikelihood,
1398 VoiceDetection::kModerateLikelihood,
1399 VoiceDetection::kHighLikelihood
1400 };
pkasting25702cb2016-01-08 13:50:27 -08001401 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 EXPECT_EQ(apm_->kNoError,
1403 apm_->voice_detection()->set_likelihood(likelihood[i]));
1404 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1405 }
1406
1407 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001408 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 EXPECT_EQ(apm_->kBadParameterError,
1410 apm_->voice_detection()->set_frame_size_ms(12));
1411
andrew@webrtc.org648af742012-02-08 01:57:29 +00001412 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001413 for (int i = 10; i <= 30; i += 10) {
1414 EXPECT_EQ(apm_->kNoError,
1415 apm_->voice_detection()->set_frame_size_ms(i));
1416 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1417 }
1418 */
1419
andrew@webrtc.org648af742012-02-08 01:57:29 +00001420 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001421 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1422 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1423 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1424 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1425
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001426 // Test that AudioFrame activity is maintained when VAD is disabled.
1427 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1428 AudioFrame::VADActivity activity[] = {
1429 AudioFrame::kVadActive,
1430 AudioFrame::kVadPassive,
1431 AudioFrame::kVadUnknown
1432 };
pkasting25702cb2016-01-08 13:50:27 -08001433 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001434 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001435 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001436 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001437 }
1438
1439 // Test that AudioFrame activity is set when VAD is enabled.
1440 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001441 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001443 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001444
niklase@google.com470e71d2011-07-07 08:21:25 +00001445 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1446}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001447
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001448TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sergey Silkin271812a2018-09-13 14:55:17 +00001449 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1450 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001451 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1452 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1453 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1454 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1455 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1456}
1457
1458TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001459 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001460 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001461 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001462 AudioFrame frame_copy;
1463 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001464 for (int j = 0; j < 1000; j++) {
1465 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1466 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001467 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1468 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001469 }
1470 }
1471}
1472
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001473TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1474 // Test that ProcessStream copies input to output even with no processing.
1475 const size_t kSamples = 80;
1476 const int sample_rate = 8000;
1477 const float src[kSamples] = {
1478 -1.0f, 0.0f, 1.0f
1479 };
1480 float dest[kSamples] = {};
1481
1482 auto src_channels = &src[0];
1483 auto dest_channels = &dest[0];
1484
Ivo Creusen62337e52018-01-09 14:17:33 +01001485 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001486 EXPECT_NOERR(apm_->ProcessStream(
1487 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1488 sample_rate, LayoutFromChannels(1), &dest_channels));
1489
1490 for (size_t i = 0; i < kSamples; ++i) {
1491 EXPECT_EQ(src[i], dest[i]);
1492 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001493
1494 // Same for ProcessReverseStream.
1495 float rev_dest[kSamples] = {};
1496 auto rev_dest_channels = &rev_dest[0];
1497
1498 StreamConfig input_stream = {sample_rate, 1};
1499 StreamConfig output_stream = {sample_rate, 1};
1500 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1501 output_stream, &rev_dest_channels));
1502
1503 for (size_t i = 0; i < kSamples; ++i) {
1504 EXPECT_EQ(src[i], rev_dest[i]);
1505 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001506}
1507
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001508TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1509 EnableAllComponents();
1510
pkasting25702cb2016-01-08 13:50:27 -08001511 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001512 Init(kProcessSampleRates[i],
1513 kProcessSampleRates[i],
1514 kProcessSampleRates[i],
1515 2,
1516 2,
1517 2,
1518 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001519 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001520 ASSERT_EQ(0, feof(far_file_));
1521 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001522 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001523 CopyLeftToRightChannel(revframe_->mutable_data(),
1524 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001525
aluebsb0319552016-03-17 20:39:53 -07001526 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001527
yujo36b1a5f2017-06-12 12:45:32 -07001528 CopyLeftToRightChannel(frame_->mutable_data(),
1529 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001530 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1531
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001532 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00001533 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001534 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001535 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001536 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001537 analog_level = apm_->gain_control()->stream_analog_level();
1538
yujo36b1a5f2017-06-12 12:45:32 -07001539 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001540 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001541 rewind(far_file_);
1542 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001543 }
1544}
1545
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001546TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001547 // Verify the filter is not active through undistorted audio when:
1548 // 1. No components are enabled...
1549 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001550 AudioFrame frame_copy;
1551 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001552 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1553 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1554 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1555
1556 // 2. Only the level estimator is enabled...
1557 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001558 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001559 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1560 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1561 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1562 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1563 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1564
1565 // 3. Only VAD is enabled...
1566 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001567 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001568 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1569 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1570 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1571 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1572 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1573
1574 // 4. Both VAD and the level estimator are enabled...
1575 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001576 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001577 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1578 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1579 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1580 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1581 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1582 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1583 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1584
1585 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001586 frame_->samples_per_channel_ = 160;
1587 frame_->num_channels_ = 2;
1588 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001589 // Enable AEC, which would require the filter in super-wb. We rely on the
1590 // first few frames of data being unaffected by the AEC.
1591 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1592 // behavior of the AEC. Think of something more robust.
Sergey Silkin271812a2018-09-13 14:55:17 +00001593 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001594 // Make sure we have extended filter enabled. This makes sure nothing is
1595 // touched until we have a farend frame.
1596 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001597 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001598 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001599 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001600 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001601 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00001602 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001603 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1604 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00001605 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001606 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1607 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1608
1609 // Check the test is valid. We should have distortion from the filter
1610 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001611 frame_->samples_per_channel_ = 320;
1612 frame_->num_channels_ = 2;
1613 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001614 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001615 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001616 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00001617 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001618 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1619 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1620}
1621
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001622#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1623void ApmTest::ProcessDebugDump(const std::string& in_filename,
1624 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001625 Format format,
1626 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001627 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001628 FILE* in_file = fopen(in_filename.c_str(), "rb");
1629 ASSERT_TRUE(in_file != NULL);
1630 audioproc::Event event_msg;
1631 bool first_init = true;
1632
1633 while (ReadMessageFromFile(in_file, &event_msg)) {
1634 if (event_msg.type() == audioproc::Event::INIT) {
1635 const audioproc::Init msg = event_msg.init();
1636 int reverse_sample_rate = msg.sample_rate();
1637 if (msg.has_reverse_sample_rate()) {
1638 reverse_sample_rate = msg.reverse_sample_rate();
1639 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001640 int output_sample_rate = msg.sample_rate();
1641 if (msg.has_output_sample_rate()) {
1642 output_sample_rate = msg.output_sample_rate();
1643 }
1644
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001645 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001646 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001647 reverse_sample_rate,
1648 msg.num_input_channels(),
1649 msg.num_output_channels(),
1650 msg.num_reverse_channels(),
1651 false);
1652 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001653 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001654 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001655 auto aec_dump =
1656 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1657 EXPECT_TRUE(aec_dump);
1658 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001659 first_init = false;
1660 }
1661
1662 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1663 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1664
1665 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001666 ASSERT_EQ(revframe_->num_channels_,
1667 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001668 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001669 memcpy(revfloat_cb_->channels()[i],
1670 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001671 msg.channel(i).size());
1672 }
1673 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001674 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001675 if (format == kFloatFormat) {
1676 // We're using an int16 input file; convert to float.
1677 ConvertToFloat(*revframe_, revfloat_cb_.get());
1678 }
1679 }
1680 AnalyzeReverseStreamChooser(format);
1681
1682 } else if (event_msg.type() == audioproc::Event::STREAM) {
1683 const audioproc::Stream msg = event_msg.stream();
1684 // ProcessStream could have changed this for the output frame.
1685 frame_->num_channels_ = apm_->num_input_channels();
1686
1687 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1688 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
Sergey Silkin271812a2018-09-13 14:55:17 +00001689 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001690 if (msg.has_keypress()) {
1691 apm_->set_stream_key_pressed(msg.keypress());
1692 } else {
1693 apm_->set_stream_key_pressed(true);
1694 }
1695
1696 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001697 ASSERT_EQ(frame_->num_channels_,
1698 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001699 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001700 memcpy(float_cb_->channels()[i],
1701 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001702 msg.input_channel(i).size());
1703 }
1704 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001705 memcpy(frame_->mutable_data(), msg.input_data().data(),
1706 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001707 if (format == kFloatFormat) {
1708 // We're using an int16 input file; convert to float.
1709 ConvertToFloat(*frame_, float_cb_.get());
1710 }
1711 }
1712 ProcessStreamChooser(format);
1713 }
1714 }
aleloif4dd1912017-06-15 01:55:38 -07001715 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001716 fclose(in_file);
1717}
1718
1719void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001720 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001721 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001722 std::string format_string;
1723 switch (format) {
1724 case kIntFormat:
1725 format_string = "_int";
1726 break;
1727 case kFloatFormat:
1728 format_string = "_float";
1729 break;
1730 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001731 const std::string ref_filename = test::TempFilename(
1732 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1733 const std::string out_filename = test::TempFilename(
1734 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001735 const std::string limited_filename = test::TempFilename(
1736 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1737 const size_t logging_limit_bytes = 100000;
1738 // We expect at least this many bytes in the created logfile.
1739 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001740 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001741 ProcessDebugDump(in_filename, ref_filename, format, -1);
1742 ProcessDebugDump(ref_filename, out_filename, format, -1);
1743 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001744
1745 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1746 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001747 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001748 ASSERT_TRUE(ref_file != NULL);
1749 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001750 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001751 std::unique_ptr<uint8_t[]> ref_bytes;
1752 std::unique_ptr<uint8_t[]> out_bytes;
1753 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001754
1755 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1756 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001757 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001758 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001759 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001760 while (ref_size > 0 && out_size > 0) {
1761 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001762 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001763 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001764 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001765 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001766 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001767 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1768 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001769 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001770 }
1771 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001772 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1773 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001774 EXPECT_NE(0, feof(ref_file));
1775 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001776 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001777 ASSERT_EQ(0, fclose(ref_file));
1778 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001779 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001780 remove(ref_filename.c_str());
1781 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001782 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001783}
1784
pbosc7a65692016-05-06 12:50:04 -07001785TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001786 VerifyDebugDumpTest(kIntFormat);
1787}
1788
pbosc7a65692016-05-06 12:50:04 -07001789TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001790 VerifyDebugDumpTest(kFloatFormat);
1791}
1792#endif
1793
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001794// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001795TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001796 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001797 const std::string filename =
1798 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001799 {
1800 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1801 EXPECT_FALSE(aec_dump);
1802 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001803
1804#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1805 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001806 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001807
aleloif4dd1912017-06-15 01:55:38 -07001808 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1809 EXPECT_TRUE(aec_dump);
1810 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001811 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001812 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001813 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001814
1815 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001816 FILE* fid = fopen(filename.c_str(), "r");
1817 ASSERT_TRUE(fid != NULL);
1818
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001819 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001820 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001821 ASSERT_EQ(0, remove(filename.c_str()));
1822#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001823 // Verify the file has NOT been written.
1824 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1825#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1826}
1827
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001828// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001829TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001830 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1831
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001832 const std::string filename =
1833 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001834 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001835 ASSERT_TRUE(fid);
1836
1837#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1838 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001839 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001840
aleloif4dd1912017-06-15 01:55:38 -07001841 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1842 EXPECT_TRUE(aec_dump);
1843 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001844 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001845 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001846 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001847
1848 // Verify the file has been written.
1849 fid = fopen(filename.c_str(), "r");
1850 ASSERT_TRUE(fid != NULL);
1851
1852 // Clean it up.
1853 ASSERT_EQ(0, fclose(fid));
1854 ASSERT_EQ(0, remove(filename.c_str()));
1855#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001856 ASSERT_EQ(0, fclose(fid));
1857#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1858}
1859
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001860TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001861 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001862 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001863
1864 Config config;
1865 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001866 std::unique_ptr<AudioProcessing> fapm(
1867 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001868 EnableAllComponents();
1869 EnableAllAPComponents(fapm.get());
1870 for (int i = 0; i < ref_data.test_size(); i++) {
1871 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1872
1873 audioproc::Test* test = ref_data.mutable_test(i);
1874 // TODO(ajm): Restore downmixing test cases.
1875 if (test->num_input_channels() != test->num_output_channels())
1876 continue;
1877
Peter Kasting69558702016-01-12 16:26:35 -08001878 const size_t num_render_channels =
1879 static_cast<size_t>(test->num_reverse_channels());
1880 const size_t num_input_channels =
1881 static_cast<size_t>(test->num_input_channels());
1882 const size_t num_output_channels =
1883 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001884 const size_t samples_per_channel = static_cast<size_t>(
1885 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001886
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001887 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1888 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001889 Init(fapm.get());
1890
1891 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001892 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1893 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001894
1895 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001896 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001897 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1898 ReadFrame(near_file_, frame_, float_cb_.get())) {
1899 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1900
aluebsb0319552016-03-17 20:39:53 -07001901 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001902 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1903 revfloat_cb_->channels(),
1904 samples_per_channel,
1905 test->sample_rate(),
1906 LayoutFromChannels(num_render_channels)));
1907
1908 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1909 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00001910 apm_->echo_cancellation()->set_stream_drift_samples(0);
1911 fapm->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001912 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1913 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1914
1915 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001916 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001917 output_int16.channels());
1918
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001919 EXPECT_NOERR(fapm->ProcessStream(
1920 float_cb_->channels(),
1921 samples_per_channel,
1922 test->sample_rate(),
1923 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001924 test->sample_rate(),
1925 LayoutFromChannels(num_output_channels),
1926 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001927 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001928 FloatToS16(float_cb_->channels()[j],
1929 samples_per_channel,
1930 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001931 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001932 float snr = ComputeSNR(output_int16.channels()[j],
1933 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001934 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001935
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001936 const float kVarianceThreshold = 20;
1937 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001938
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001939 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001940 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1941 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001942 }
1943 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001944
1945 analog_level = fapm->gain_control()->stream_analog_level();
1946 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1947 fapm->gain_control()->stream_analog_level());
Sergey Silkin271812a2018-09-13 14:55:17 +00001948 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
1949 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001950 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1951 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001952 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001953
1954 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001955 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001956 }
aluebs776593b2016-03-15 14:04:58 -07001957
1958#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1959 const size_t kMaxNumBadChunks = 0;
1960#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1961 // There are a few chunks in the fixed-point profile that give low SNR.
1962 // Listening confirmed the difference is acceptable.
1963 const size_t kMaxNumBadChunks = 60;
1964#endif
1965 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1966
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001967 rewind(far_file_);
1968 rewind(near_file_);
1969 }
1970}
1971
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001972// TODO(andrew): Add a test to process a few frames with different combinations
1973// of enabled components.
1974
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001975TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001976 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001977 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001978
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001979 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001980 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001981 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001982 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001983 for (size_t i = 0; i < arraysize(kChannels); i++) {
1984 for (size_t j = 0; j < arraysize(kChannels); j++) {
1985 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001986 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001987 test->set_num_reverse_channels(kChannels[i]);
1988 test->set_num_input_channels(kChannels[j]);
1989 test->set_num_output_channels(kChannels[j]);
1990 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001991 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001992 }
1993 }
1994 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001995#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1996 // To test the extended filter mode.
1997 audioproc::Test* test = ref_data.add_test();
1998 test->set_num_reverse_channels(2);
1999 test->set_num_input_channels(2);
2000 test->set_num_output_channels(2);
2001 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2002 test->set_use_aec_extended_filter(true);
2003#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002004 }
2005
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002006 for (int i = 0; i < ref_data.test_size(); i++) {
2007 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002008
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002009 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002010 // TODO(ajm): We no longer allow different input and output channels. Skip
2011 // these tests for now, but they should be removed from the set.
2012 if (test->num_input_channels() != test->num_output_channels())
2013 continue;
2014
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002015 Config config;
2016 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002017 config.Set<ExtendedFilter>(
2018 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01002019 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002020
2021 EnableAllComponents();
2022
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002023 Init(test->sample_rate(),
2024 test->sample_rate(),
2025 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002026 static_cast<size_t>(test->num_input_channels()),
2027 static_cast<size_t>(test->num_output_channels()),
2028 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002029 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002030
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002031 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002032 int has_voice_count = 0;
2033 int is_saturated_count = 0;
2034 int analog_level = 127;
2035 int analog_level_average = 0;
2036 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002037 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002038#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2039 int stats_index = 0;
2040#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002041
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002042 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002043 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002044
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002045 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2046
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002047 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00002048 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002049 EXPECT_EQ(apm_->kNoError,
2050 apm_->gain_control()->set_stream_analog_level(analog_level));
2051
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002052 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002053
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002054 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002055 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2056 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002057
2058 max_output_average += MaxAudioFrame(*frame_);
2059
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002060 analog_level = apm_->gain_control()->stream_analog_level();
2061 analog_level_average += analog_level;
2062 if (apm_->gain_control()->stream_is_saturated()) {
2063 is_saturated_count++;
2064 }
2065 if (apm_->voice_detection()->stream_has_voice()) {
2066 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002067 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002068 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002069 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002070 }
2071
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002072 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2073
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002074 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002075 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002076 sizeof(int16_t),
2077 frame_size,
2078 out_file_);
2079 ASSERT_EQ(frame_size, write_count);
2080
2081 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002082 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002083 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002084
2085#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2086 const int kStatsAggregationFrameNum = 100; // 1 second.
2087 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002088 // Get echo and delay metrics.
2089 AudioProcessingStats stats =
2090 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07002091
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002092 // Echo metrics.
2093 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
2094 const float echo_return_loss_enhancement =
2095 stats.echo_return_loss_enhancement.value_or(-1.0f);
2096 const float divergent_filter_fraction =
2097 stats.divergent_filter_fraction.value_or(-1.0f);
2098 const float residual_echo_likelihood =
2099 stats.residual_echo_likelihood.value_or(-1.0f);
2100 const float residual_echo_likelihood_recent_max =
2101 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
2102
2103 // Delay metrics.
2104 const int32_t delay_median_ms = stats.delay_median_ms.value_or(-1.0);
2105 const int32_t delay_standard_deviation_ms =
2106 stats.delay_standard_deviation_ms.value_or(-1.0);
minyue58530ed2016-05-24 05:50:12 -07002107
2108 // Get RMS.
2109 int rms_level = apm_->level_estimator()->RMS();
2110 EXPECT_LE(0, rms_level);
2111 EXPECT_GE(127, rms_level);
2112
2113 if (!write_ref_data) {
2114 const audioproc::Test::EchoMetrics& reference =
2115 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002116 constexpr float kEpsilon = 0.01;
2117 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
2118 EXPECT_NEAR(echo_return_loss_enhancement,
2119 reference.echo_return_loss_enhancement(), kEpsilon);
2120 EXPECT_NEAR(divergent_filter_fraction,
2121 reference.divergent_filter_fraction(), kEpsilon);
2122 EXPECT_NEAR(residual_echo_likelihood,
2123 reference.residual_echo_likelihood(), kEpsilon);
2124 EXPECT_NEAR(residual_echo_likelihood_recent_max,
2125 reference.residual_echo_likelihood_recent_max(),
2126 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07002127
2128 const audioproc::Test::DelayMetrics& reference_delay =
2129 test->delay_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002130 EXPECT_EQ(reference_delay.median(), delay_median_ms);
2131 EXPECT_EQ(reference_delay.std(), delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07002132
2133 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2134
2135 ++stats_index;
2136 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002137 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
2138 message_echo->set_echo_return_loss(echo_return_loss);
2139 message_echo->set_echo_return_loss_enhancement(
2140 echo_return_loss_enhancement);
2141 message_echo->set_divergent_filter_fraction(
2142 divergent_filter_fraction);
2143 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
2144 message_echo->set_residual_echo_likelihood_recent_max(
2145 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07002146 audioproc::Test::DelayMetrics* message_delay =
2147 test->add_delay_metrics();
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002148 message_delay->set_median(delay_median_ms);
2149 message_delay->set_std(delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07002150
2151 test->add_rms_level(rms_level);
2152 }
2153 }
2154#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002155 }
2156 max_output_average /= frame_count;
2157 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002158 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002159
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002160 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002161 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002162 // When running the test on a N7 we get a {2, 6} difference of
2163 // |has_voice_count| and |max_output_average| is up to 18 higher.
2164 // All numbers being consistently higher on N7 compare to ref_data.
2165 // TODO(bjornv): If we start getting more of these offsets on Android we
2166 // should consider a different approach. Either using one slack for all,
2167 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02002168#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002169 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002170 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002171 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002172 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002173#else
2174 const int kHasVoiceCountOffset = 0;
2175 const int kHasVoiceCountNear = kIntNear;
2176 const int kMaxOutputAverageOffset = 0;
2177 const int kMaxOutputAverageNear = kIntNear;
2178#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002179 EXPECT_NEAR(test->has_voice_count(),
2180 has_voice_count - kHasVoiceCountOffset,
2181 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002182 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002183
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002184 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002185 EXPECT_NEAR(test->max_output_average(),
2186 max_output_average - kMaxOutputAverageOffset,
2187 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002188#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002189 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002190 EXPECT_NEAR(test->ns_speech_probability_average(),
2191 ns_speech_prob_average,
2192 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002193#endif
2194 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002195 test->set_has_voice_count(has_voice_count);
2196 test->set_is_saturated_count(is_saturated_count);
2197
2198 test->set_analog_level_average(analog_level_average);
2199 test->set_max_output_average(max_output_average);
2200
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002201#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002202 EXPECT_LE(0.0f, ns_speech_prob_average);
2203 EXPECT_GE(1.0f, ns_speech_prob_average);
2204 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002205#endif
2206 }
2207
2208 rewind(far_file_);
2209 rewind(near_file_);
2210 }
2211
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002212 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002213 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002214 }
2215}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002216
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002217TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2218 struct ChannelFormat {
2219 AudioProcessing::ChannelLayout in_layout;
2220 AudioProcessing::ChannelLayout out_layout;
2221 };
2222 ChannelFormat cf[] = {
2223 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2224 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2225 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2226 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002227
Ivo Creusen62337e52018-01-09 14:17:33 +01002228 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002229 // Enable one component just to ensure some processing takes place.
2230 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002231 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002232 const int in_rate = 44100;
2233 const int out_rate = 48000;
2234 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2235 TotalChannelsFromLayout(cf[i].in_layout));
2236 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2237 ChannelsFromLayout(cf[i].out_layout));
2238
2239 // Run over a few chunks.
2240 for (int j = 0; j < 10; ++j) {
2241 EXPECT_NOERR(ap->ProcessStream(
2242 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002243 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002244 in_rate,
2245 cf[i].in_layout,
2246 out_rate,
2247 cf[i].out_layout,
2248 out_cb.channels()));
2249 }
2250 }
2251}
2252
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002253// Compares the reference and test arrays over a region around the expected
2254// delay. Finds the highest SNR in that region and adds the variance and squared
2255// error results to the supplied accumulators.
2256void UpdateBestSNR(const float* ref,
2257 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002258 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002259 int expected_delay,
2260 double* variance_acc,
2261 double* sq_error_acc) {
2262 double best_snr = std::numeric_limits<double>::min();
2263 double best_variance = 0;
2264 double best_sq_error = 0;
2265 // Search over a region of eight samples around the expected delay.
2266 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2267 ++delay) {
2268 double sq_error = 0;
2269 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002270 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002271 double error = test[i + delay] - ref[i];
2272 sq_error += error * error;
2273 variance += ref[i] * ref[i];
2274 }
2275
2276 if (sq_error == 0) {
2277 *variance_acc += variance;
2278 return;
2279 }
2280 double snr = variance / sq_error;
2281 if (snr > best_snr) {
2282 best_snr = snr;
2283 best_variance = variance;
2284 best_sq_error = sq_error;
2285 }
2286 }
2287
2288 *variance_acc += best_variance;
2289 *sq_error_acc += best_sq_error;
2290}
2291
2292// Used to test a multitude of sample rate and channel combinations. It works
2293// by first producing a set of reference files (in SetUpTestCase) that are
2294// assumed to be correct, as the used parameters are verified by other tests
2295// in this collection. Primarily the reference files are all produced at
2296// "native" rates which do not involve any resampling.
2297
2298// Each test pass produces an output file with a particular format. The output
2299// is matched against the reference file closest to its internal processing
2300// format. If necessary the output is resampled back to its process format.
2301// Due to the resampling distortion, we don't expect identical results, but
2302// enforce SNR thresholds which vary depending on the format. 0 is a special
2303// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002304typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002305class AudioProcessingTest
2306 : public testing::TestWithParam<AudioProcessingTestData> {
2307 public:
2308 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002309 : input_rate_(std::get<0>(GetParam())),
2310 output_rate_(std::get<1>(GetParam())),
2311 reverse_input_rate_(std::get<2>(GetParam())),
2312 reverse_output_rate_(std::get<3>(GetParam())),
2313 expected_snr_(std::get<4>(GetParam())),
2314 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002315
2316 virtual ~AudioProcessingTest() {}
2317
2318 static void SetUpTestCase() {
2319 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002320 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002321 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002322 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2323 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2324 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002325 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002326 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2327 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2328 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002329 }
2330 }
2331 }
2332 }
2333
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002334 void TearDown() {
2335 // Remove "out" files after each test.
2336 ClearTempOutFiles();
2337 }
2338
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002339 static void TearDownTestCase() {
2340 ClearTempFiles();
2341 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002342
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002343 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002344 // to a file specified with |output_file_prefix|. Both forward and reverse
2345 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002346 static void ProcessFormat(int input_rate,
2347 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002348 int reverse_input_rate,
2349 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002350 size_t num_input_channels,
2351 size_t num_output_channels,
2352 size_t num_reverse_input_channels,
2353 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002354 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002355 Config config;
2356 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002357 std::unique_ptr<AudioProcessing> ap(
2358 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002359 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002360
ekmeyerson60d9b332015-08-14 10:35:55 -07002361 ProcessingConfig processing_config = {
2362 {{input_rate, num_input_channels},
2363 {output_rate, num_output_channels},
2364 {reverse_input_rate, num_reverse_input_channels},
2365 {reverse_output_rate, num_reverse_output_channels}}};
2366 ap->Initialize(processing_config);
2367
2368 FILE* far_file =
2369 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002370 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002371 FILE* out_file =
2372 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2373 reverse_input_rate, reverse_output_rate,
2374 num_input_channels, num_output_channels,
2375 num_reverse_input_channels,
2376 num_reverse_output_channels, kForward).c_str(),
2377 "wb");
2378 FILE* rev_out_file =
2379 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2380 reverse_input_rate, reverse_output_rate,
2381 num_input_channels, num_output_channels,
2382 num_reverse_input_channels,
2383 num_reverse_output_channels, kReverse).c_str(),
2384 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002385 ASSERT_TRUE(far_file != NULL);
2386 ASSERT_TRUE(near_file != NULL);
2387 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002388 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002389
2390 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2391 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002392 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2393 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002394 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2395 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002396 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2397 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002398
2399 // Temporary buffers.
2400 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002401 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2402 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002403 std::unique_ptr<float[]> float_data(new float[max_length]);
2404 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002405
2406 int analog_level = 127;
2407 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2408 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002409 EXPECT_NOERR(ap->ProcessReverseStream(
2410 rev_cb.channels(), processing_config.reverse_input_stream(),
2411 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002412
2413 EXPECT_NOERR(ap->set_stream_delay_ms(0));
Sergey Silkin271812a2018-09-13 14:55:17 +00002414 ap->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002415 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2416
2417 EXPECT_NOERR(ap->ProcessStream(
2418 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002419 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002420 input_rate,
2421 LayoutFromChannels(num_input_channels),
2422 output_rate,
2423 LayoutFromChannels(num_output_channels),
2424 out_cb.channels()));
2425
ekmeyerson60d9b332015-08-14 10:35:55 -07002426 // Dump forward output to file.
2427 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002428 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002429 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002430
pkasting25702cb2016-01-08 13:50:27 -08002431 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002433 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002434
ekmeyerson60d9b332015-08-14 10:35:55 -07002435 // Dump reverse output to file.
2436 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2437 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002438 size_t rev_out_length =
2439 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002440
pkasting25702cb2016-01-08 13:50:27 -08002441 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002442 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2443 rev_out_file));
2444
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002445 analog_level = ap->gain_control()->stream_analog_level();
2446 }
2447 fclose(far_file);
2448 fclose(near_file);
2449 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002450 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002451 }
2452
2453 protected:
2454 int input_rate_;
2455 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002456 int reverse_input_rate_;
2457 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002458 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002459 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002460};
2461
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002462TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002463 struct ChannelFormat {
2464 int num_input;
2465 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002466 int num_reverse_input;
2467 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002468 };
2469 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002470 {1, 1, 1, 1},
2471 {1, 1, 2, 1},
2472 {2, 1, 1, 1},
2473 {2, 1, 2, 1},
2474 {2, 2, 1, 1},
2475 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002476 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002477
pkasting25702cb2016-01-08 13:50:27 -08002478 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002479 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2480 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2481 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002482
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 // Verify output for both directions.
2484 std::vector<StreamDirection> stream_directions;
2485 stream_directions.push_back(kForward);
2486 stream_directions.push_back(kReverse);
2487 for (StreamDirection file_direction : stream_directions) {
2488 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2489 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2490 const int out_num =
2491 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2492 const double expected_snr =
2493 file_direction ? expected_reverse_snr_ : expected_snr_;
2494
2495 const int min_ref_rate = std::min(in_rate, out_rate);
2496 int ref_rate;
2497
2498 if (min_ref_rate > 32000) {
2499 ref_rate = 48000;
2500 } else if (min_ref_rate > 16000) {
2501 ref_rate = 32000;
2502 } else if (min_ref_rate > 8000) {
2503 ref_rate = 16000;
2504 } else {
2505 ref_rate = 8000;
2506 }
aluebs776593b2016-03-15 14:04:58 -07002507#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002508 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002509 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002510 }
2511#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002512 FILE* out_file = fopen(
2513 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2514 reverse_output_rate_, cf[i].num_input,
2515 cf[i].num_output, cf[i].num_reverse_input,
2516 cf[i].num_reverse_output, file_direction).c_str(),
2517 "rb");
2518 // The reference files always have matching input and output channels.
2519 FILE* ref_file = fopen(
2520 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2521 cf[i].num_output, cf[i].num_output,
2522 cf[i].num_reverse_output, cf[i].num_reverse_output,
2523 file_direction).c_str(),
2524 "rb");
2525 ASSERT_TRUE(out_file != NULL);
2526 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002527
pkasting25702cb2016-01-08 13:50:27 -08002528 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2529 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002530 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002531 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002532 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002533 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002534 // Data from the resampled output, in case the reference and output rates
2535 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002536 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002537
ekmeyerson60d9b332015-08-14 10:35:55 -07002538 PushResampler<float> resampler;
2539 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002540
ekmeyerson60d9b332015-08-14 10:35:55 -07002541 // Compute the resampling delay of the output relative to the reference,
2542 // to find the region over which we should search for the best SNR.
2543 float expected_delay_sec = 0;
2544 if (in_rate != ref_rate) {
2545 // Input resampling delay.
2546 expected_delay_sec +=
2547 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2548 }
2549 if (out_rate != ref_rate) {
2550 // Output resampling delay.
2551 expected_delay_sec +=
2552 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2553 // Delay of converting the output back to its processing rate for
2554 // testing.
2555 expected_delay_sec +=
2556 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2557 }
2558 int expected_delay =
2559 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002560
ekmeyerson60d9b332015-08-14 10:35:55 -07002561 double variance = 0;
2562 double sq_error = 0;
2563 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2564 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2565 float* out_ptr = out_data.get();
2566 if (out_rate != ref_rate) {
2567 // Resample the output back to its internal processing rate if
2568 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002569 ASSERT_EQ(ref_length,
2570 static_cast<size_t>(resampler.Resample(
2571 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002572 out_ptr = cmp_data.get();
2573 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002574
ekmeyerson60d9b332015-08-14 10:35:55 -07002575 // Update the |sq_error| and |variance| accumulators with the highest
2576 // SNR of reference vs output.
2577 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2578 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002579 }
2580
ekmeyerson60d9b332015-08-14 10:35:55 -07002581 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2582 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2583 << cf[i].num_input << ", " << cf[i].num_output << ", "
2584 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2585 << ", " << file_direction << "): ";
2586 if (sq_error > 0) {
2587 double snr = 10 * log10(variance / sq_error);
2588 EXPECT_GE(snr, expected_snr);
2589 EXPECT_NE(0, expected_snr);
2590 std::cout << "SNR=" << snr << " dB" << std::endl;
2591 } else {
aluebs776593b2016-03-15 14:04:58 -07002592 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002593 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002594
ekmeyerson60d9b332015-08-14 10:35:55 -07002595 fclose(out_file);
2596 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002597 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002598 }
2599}
2600
2601#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2602INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002603 CommonFormats,
2604 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002605 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2606 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2607 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2608 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2609 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2610 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2611 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2612 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2613 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2614 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2615 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2616 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002617
Edward Lemurc5ee9872017-10-23 23:33:04 +02002618 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2619 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2620 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2621 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2622 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2623 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2624 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2625 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2626 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2627 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2628 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2629 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002630
Edward Lemurc5ee9872017-10-23 23:33:04 +02002631 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2632 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2633 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2634 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2635 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2636 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2637 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2638 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2639 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2640 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2641 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2642 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002643
Edward Lemurc5ee9872017-10-23 23:33:04 +02002644 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2645 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2646 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2647 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2648 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2649 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2650 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2651 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2652 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2653 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2654 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2655 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002656
2657#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2658INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002659 CommonFormats,
2660 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002661 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2662 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2663 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2664 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2665 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2666 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2667 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2668 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2669 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2670 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2671 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2672 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002673
Edward Lemurc5ee9872017-10-23 23:33:04 +02002674 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2675 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2676 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2677 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2678 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2679 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2680 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2681 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2682 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2683 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2684 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2685 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002686
Edward Lemurc5ee9872017-10-23 23:33:04 +02002687 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2688 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2689 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2690 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2691 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2692 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2693 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2694 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2695 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2696 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2697 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2698 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002699
Edward Lemurc5ee9872017-10-23 23:33:04 +02002700 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2701 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2702 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2703 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2704 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2705 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2706 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2707 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2708 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2709 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2710 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2711 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002712#endif
2713
niklase@google.com470e71d2011-07-07 08:21:25 +00002714} // namespace
peahc19f3122016-10-07 14:54:10 -07002715
Alessio Bazzicac054e782018-04-16 12:10:09 +02002716TEST(RuntimeSettingTest, TestDefaultCtor) {
2717 auto s = AudioProcessing::RuntimeSetting();
2718 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2719}
2720
2721TEST(RuntimeSettingTest, TestCapturePreGain) {
2722 using Type = AudioProcessing::RuntimeSetting::Type;
2723 {
2724 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2725 EXPECT_EQ(Type::kCapturePreGain, s.type());
2726 float v;
2727 s.GetFloat(&v);
2728 EXPECT_EQ(1.25f, v);
2729 }
2730
2731#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2732 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2733#endif
2734}
2735
2736TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2737 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2738 auto s = AudioProcessing::RuntimeSetting();
2739 ASSERT_TRUE(q.Insert(&s));
2740 ASSERT_TRUE(q.Remove(&s));
2741 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2742}
2743
Sam Zackrisson0beac582017-09-25 12:04:02 +02002744TEST(ApmConfiguration, EnablePostProcessing) {
2745 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002746 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002747 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002748 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002749 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002750 rtc::scoped_refptr<AudioProcessing> apm =
2751 AudioProcessingBuilder()
2752 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002753 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002754
2755 AudioFrame audio;
2756 audio.num_channels_ = 1;
2757 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2758
2759 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002760 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002761}
2762
Alex Loiko5825aa62017-12-18 16:02:40 +01002763TEST(ApmConfiguration, EnablePreProcessing) {
2764 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002765 auto mock_pre_processor_ptr =
2766 new testing::NiceMock<test::MockCustomProcessing>();
2767 auto mock_pre_processor =
2768 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002769 rtc::scoped_refptr<AudioProcessing> apm =
2770 AudioProcessingBuilder()
2771 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002772 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002773
2774 AudioFrame audio;
2775 audio.num_channels_ = 1;
2776 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2777
2778 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2779 apm->ProcessReverseStream(&audio);
2780}
2781
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002782TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2783 // Verify that apm uses a capture analyzer if one is provided.
2784 auto mock_capture_analyzer_ptr =
2785 new testing::NiceMock<test::MockCustomAudioAnalyzer>();
2786 auto mock_capture_analyzer =
2787 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2788 rtc::scoped_refptr<AudioProcessing> apm =
2789 AudioProcessingBuilder()
2790 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2791 .Create();
2792
2793 AudioFrame audio;
2794 audio.num_channels_ = 1;
2795 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2796
2797 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(testing::_)).Times(1);
2798 apm->ProcessStream(&audio);
2799}
2800
Alex Loiko73ec0192018-05-15 10:52:28 +02002801TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2802 auto mock_pre_processor_ptr =
2803 new testing::NiceMock<test::MockCustomProcessing>();
2804 auto mock_pre_processor =
2805 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2806 rtc::scoped_refptr<AudioProcessing> apm =
2807 AudioProcessingBuilder()
2808 .SetRenderPreProcessing(std::move(mock_pre_processor))
2809 .Create();
2810 apm->SetRuntimeSetting(
2811 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2812
2813 // RuntimeSettings forwarded during 'Process*Stream' calls.
2814 // Therefore we have to make one such call.
2815 AudioFrame audio;
2816 audio.num_channels_ = 1;
2817 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2818
2819 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2820 apm->ProcessReverseStream(&audio);
2821}
2822
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002823class MyEchoControlFactory : public EchoControlFactory {
2824 public:
2825 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2826 auto ec = new test::MockEchoControl();
2827 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2828 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2829 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2830 return std::unique_ptr<EchoControl>(ec);
2831 }
2832};
2833
2834TEST(ApmConfiguration, EchoControlInjection) {
2835 // Verify that apm uses an injected echo controller if one is provided.
2836 webrtc::Config webrtc_config;
2837 std::unique_ptr<EchoControlFactory> echo_control_factory(
2838 new MyEchoControlFactory());
2839
Alex Loiko5825aa62017-12-18 16:02:40 +01002840 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002841 AudioProcessingBuilder()
2842 .SetEchoControlFactory(std::move(echo_control_factory))
2843 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002844
2845 AudioFrame audio;
2846 audio.num_channels_ = 1;
2847 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2848 apm->ProcessStream(&audio);
2849 apm->ProcessReverseStream(&audio);
2850 apm->ProcessStream(&audio);
2851}
Ivo Creusenae026092017-11-20 13:07:16 +01002852
2853std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2854 Config old_config;
2855 if (use_AEC2) {
2856 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2857 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2858 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002859 std::unique_ptr<AudioProcessing> apm(
2860 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002861 if (!apm) {
2862 return apm;
2863 }
2864
2865 ProcessingConfig processing_config = {
2866 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2867
2868 if (apm->Initialize(processing_config) != 0) {
2869 return nullptr;
2870 }
2871
2872 // Disable all components except for an AEC and the residual echo detector.
Sergey Silkin271812a2018-09-13 14:55:17 +00002873 AudioProcessing::Config config;
2874 config.residual_echo_detector.enabled = true;
2875 config.high_pass_filter.enabled = false;
2876 config.gain_controller2.enabled = false;
2877 apm->ApplyConfig(config);
Ivo Creusenae026092017-11-20 13:07:16 +01002878 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2879 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2880 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2881 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Sergey Silkin271812a2018-09-13 14:55:17 +00002882
2883 if (use_AEC2) {
2884 EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
2885 EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
2886 EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
2887 EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
2888 } else {
2889 EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
2890 EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
2891 }
Ivo Creusenae026092017-11-20 13:07:16 +01002892 return apm;
2893}
2894
2895#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2896#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2897#else
2898#define MAYBE_ApmStatistics ApmStatistics
2899#endif
2900
2901TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2902 // Set up APM with AEC2 and process some audio.
2903 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2904 ASSERT_TRUE(apm);
2905
2906 // Set up an audioframe.
2907 AudioFrame frame;
2908 frame.num_channels_ = 1;
2909 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2910
2911 // Fill the audio frame with a sawtooth pattern.
2912 int16_t* ptr = frame.mutable_data();
2913 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2914 ptr[i] = 10000 * ((i % 3) - 1);
2915 }
2916
2917 // Do some processing.
2918 for (int i = 0; i < 200; i++) {
2919 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2920 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2921 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2922 }
2923
2924 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002925 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002926 // We expect all statistics to be set and have a sensible value.
2927 ASSERT_TRUE(stats.residual_echo_likelihood);
2928 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2929 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2930 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2931 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2932 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2933 ASSERT_TRUE(stats.echo_return_loss);
2934 EXPECT_NE(*stats.echo_return_loss, -100.0);
2935 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2936 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2937 ASSERT_TRUE(stats.divergent_filter_fraction);
2938 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2939 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2940 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2941 // We don't check stats.delay_median_ms since it takes too long to settle to a
2942 // value. At least 20 seconds of data need to be processed before it will get
2943 // a value, which would make this test take too much time.
2944
2945 // If there are no receive streams, we expect the stats not to be set. The
2946 // 'false' argument signals to APM that no receive streams are currently
2947 // active. In that situation the statistics would get stuck at their last
2948 // calculated value (AEC and echo detection need at least one stream in each
2949 // direction), so to avoid that, they should not be set by APM.
2950 stats = apm->GetStatistics(false);
2951 EXPECT_FALSE(stats.residual_echo_likelihood);
2952 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2953 EXPECT_FALSE(stats.echo_return_loss);
2954 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2955 EXPECT_FALSE(stats.divergent_filter_fraction);
2956 EXPECT_FALSE(stats.delay_median_ms);
2957 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2958}
2959
2960TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2961 // Set up APM with AECM and process some audio.
2962 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2963 ASSERT_TRUE(apm);
2964
2965 // Set up an audioframe.
2966 AudioFrame frame;
2967 frame.num_channels_ = 1;
2968 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2969
2970 // Fill the audio frame with a sawtooth pattern.
2971 int16_t* ptr = frame.mutable_data();
2972 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2973 ptr[i] = 10000 * ((i % 3) - 1);
2974 }
2975
2976 // Do some processing.
2977 for (int i = 0; i < 200; i++) {
2978 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2979 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2980 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2981 }
2982
2983 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002984 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002985 // We expect only the residual echo detector statistics to be set and have a
2986 // sensible value.
2987 EXPECT_TRUE(stats.residual_echo_likelihood);
2988 if (stats.residual_echo_likelihood) {
2989 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2990 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2991 }
2992 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2993 if (stats.residual_echo_likelihood_recent_max) {
2994 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2995 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2996 }
2997 EXPECT_FALSE(stats.echo_return_loss);
2998 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2999 EXPECT_FALSE(stats.divergent_filter_fraction);
3000 EXPECT_FALSE(stats.delay_median_ms);
3001 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3002
3003 // If there are no receive streams, we expect the stats not to be set.
3004 stats = apm->GetStatistics(false);
3005 EXPECT_FALSE(stats.residual_echo_likelihood);
3006 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
3007 EXPECT_FALSE(stats.echo_return_loss);
3008 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3009 EXPECT_FALSE(stats.divergent_filter_fraction);
3010 EXPECT_FALSE(stats.delay_median_ms);
3011 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3012}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003013} // namespace webrtc