blob: 3776474a661147eb0fd68e1bda86447406de05d1 [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"
Alessio Bazzicac054e782018-04-16 12:10:09 +020038#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020039#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/task_queue.h"
41#include "rtc_base/thread.h"
42#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/gtest.h"
44#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070045
46RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000048#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#endif
kwiberg77eab702016-09-28 17:42:01 -070052RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000053
andrew@webrtc.org27c69802014-02-18 20:24:56 +000054namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000055namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000056
ekmeyerson60d9b332015-08-14 10:35:55 -070057// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
58// applicable.
59
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000060// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000061// re-implemented; see comment at the bottom of this file. For now, the user has
62// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000063// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000064// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000065// be set to true with the command-line switch --write_ref_data.
66bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070067const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070068const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000069
aluebseb3603b2016-04-20 15:27:58 -070070#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
71// Android doesn't support 48kHz.
72const int kProcessSampleRates[] = {8000, 16000, 32000};
73#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070074const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070075#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000076
ekmeyerson60d9b332015-08-14 10:35:55 -070077enum StreamDirection { kForward = 0, kReverse };
78
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 cb->num_channels());
82 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000084 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080086 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 S16ToFloat(cb_int.channels()[i],
88 cb->num_frames(),
89 cb->channels()[i]);
90 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000091}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000092
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070094 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095}
96
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080098size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000099 switch (layout) {
100 case AudioProcessing::kMono:
101 return 1;
102 case AudioProcessing::kMonoAndKeyboard:
103 case AudioProcessing::kStereo:
104 return 2;
105 case AudioProcessing::kStereoAndKeyboard:
106 return 3;
107 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700108 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800109 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000110}
111
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000112int TruncateToMultipleOf10(int value) {
113 return (value / 10) * 10;
114}
115
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800117 size_t samples_per_channel) {
118 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000120}
121
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800123 size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000125 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
126}
127
pkasting25702cb2016-01-08 13:50:27 -0800128void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
129 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000130 stereo[i * 2 + 1] = stereo[i * 2];
131 }
132}
133
yujo36b1a5f2017-06-12 12:45:32 -0700134void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800135 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000136 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
137 }
138}
139
140void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700141 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
143 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700144 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000145 }
146}
147
148void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800149 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700150 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700152 frame_data[i] = left;
153 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000154 }
155}
156
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000157void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700158 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
160 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700161 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000162 }
163}
164
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
yujo36b1a5f2017-06-12 12:45:32 -0700172 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000173 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000174 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000175 return false;
176 }
177 return true;
178}
179
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000180void EnableAllAPComponents(AudioProcessing* ap) {
181#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000182 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000183
184 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
185 EXPECT_NOERR(ap->gain_control()->Enable(true));
186#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
187 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
188 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
189 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000190 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000191
192 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
193 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
194 EXPECT_NOERR(ap->gain_control()->Enable(true));
195#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000196
197 AudioProcessing::Config apm_config;
peah8271d042016-11-22 07:24:52 -0800198 apm_config.high_pass_filter.enabled = true;
199 ap->ApplyConfig(apm_config);
200
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000201 EXPECT_NOERR(ap->level_estimator()->Enable(true));
202 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
203
204 EXPECT_NOERR(ap->voice_detection()->Enable(true));
205}
206
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000207// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000208template <class T>
209T AbsValue(T a) {
210 return a > 0 ? a: -a;
211}
212
213int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800214 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700215 const int16_t* frame_data = frame.data();
216 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800217 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700218 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000219 }
220
221 return max_data;
222}
223
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000224#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000226 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700227 EXPECT_EQ(reference.instant(), test.instant);
228 EXPECT_EQ(reference.average(), test.average);
229 EXPECT_EQ(reference.maximum(), test.maximum);
230 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000231}
232
233void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 audioproc::Test::Statistic* msg) {
235 msg->set_instant(output.instant);
236 msg->set_average(output.average);
237 msg->set_maximum(output.maximum);
238 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000240#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241
Alex Loiko890988c2017-08-31 10:25:48 +0200242void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700243 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000245 ASSERT_TRUE(file != NULL);
246
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100247 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000248 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800249 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000250 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000251
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000253 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000254 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000255 fclose(file);
256}
257
Alex Loiko890988c2017-08-31 10:25:48 +0200258std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000259 std::ostringstream ss;
260 // Resource files are all stereo.
261 ss << name << sample_rate_hz / 1000 << "_stereo";
262 return test::ResourcePath(ss.str(), "pcm");
263}
264
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000265// Temporary filenames unique to this process. Used to be able to run these
266// tests in parallel as each process needs to be running in isolation they can't
267// have competing filenames.
268std::map<std::string, std::string> temp_filenames;
269
Alex Loiko890988c2017-08-31 10:25:48 +0200270std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000271 int input_rate,
272 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700273 int reverse_input_rate,
274 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800275 size_t num_input_channels,
276 size_t num_output_channels,
277 size_t num_reverse_input_channels,
278 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000280 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700281 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
282 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000283 if (num_output_channels == 1) {
284 ss << "mono";
285 } else if (num_output_channels == 2) {
286 ss << "stereo";
287 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700288 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700290 ss << output_rate / 1000;
291 if (num_reverse_output_channels == 1) {
292 ss << "_rmono";
293 } else if (num_reverse_output_channels == 2) {
294 ss << "_rstereo";
295 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700296 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700297 }
298 ss << reverse_output_rate / 1000;
299 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000300
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000301 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700302 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000303 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
304 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000305}
306
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000307void ClearTempFiles() {
308 for (auto& kv : temp_filenames)
309 remove(kv.second.c_str());
310}
311
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200312// Only remove "out" files. Keep "ref" files.
313void ClearTempOutFiles() {
314 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
315 const std::string& filename = it->first;
316 if (filename.substr(0, 3).compare("out") == 0) {
317 remove(it->second.c_str());
318 temp_filenames.erase(it++);
319 } else {
320 it++;
321 }
322 }
323}
324
Alex Loiko890988c2017-08-31 10:25:48 +0200325void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000326 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000327 ASSERT_TRUE(file != NULL);
328 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000329 fclose(file);
330}
331
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000332// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
333// stereo) file, converts to deinterleaved float (optionally downmixing) and
334// returns the result in |cb|. Returns false if the file ended (or on error) and
335// true otherwise.
336//
337// |int_data| and |float_data| are just temporary space that must be
338// sufficiently large to hold the 10 ms chunk.
339bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
340 ChannelBuffer<float>* cb) {
341 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000342 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000343 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
344 if (read_count != frame_size) {
345 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700346 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000347 return false; // This is expected.
348 }
349
350 S16ToFloat(int_data, frame_size, float_data);
351 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000352 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000353 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000354 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000355 cb->channels());
356 }
357
358 return true;
359}
360
niklase@google.com470e71d2011-07-07 08:21:25 +0000361class ApmTest : public ::testing::Test {
362 protected:
363 ApmTest();
364 virtual void SetUp();
365 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000366
367 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000368 }
369
370 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000371 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000372 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000373
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000374 // Used to select between int and float interface tests.
375 enum Format {
376 kIntFormat,
377 kFloatFormat
378 };
379
380 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000381 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000382 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800383 size_t num_input_channels,
384 size_t num_output_channels,
385 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000386 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000387 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000388 void EnableAllComponents();
389 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000390 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000391 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000392 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
393 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000394 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000395 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
396 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700397 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800398 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700399 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800400 void TestChangingForwardChannels(size_t num_in_channels,
401 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800403 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700404 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000405 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
406 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000407 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000408 int ProcessStreamChooser(Format format);
409 int AnalyzeReverseStreamChooser(Format format);
410 void ProcessDebugDump(const std::string& in_filename,
411 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800412 Format format,
413 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000414 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415
416 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000417 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800418 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000419 AudioFrame* frame_;
420 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800421 std::unique_ptr<ChannelBuffer<float> > float_cb_;
422 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000423 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800424 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000425 FILE* far_file_;
426 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000427 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428};
429
430ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000431 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000432#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800433 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
434 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000435#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000436#if defined(WEBRTC_MAC)
437 // A different file for Mac is needed because on this platform the AEC
438 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800439 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
440 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000441#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800442 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
443 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000444#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000445#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000447 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000448 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000449 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000450 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000451 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000452 out_file_(NULL) {
453 Config config;
454 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100455 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000456}
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
458void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000459 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
461 frame_ = new AudioFrame();
462 revframe_ = new AudioFrame();
463
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000464 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
467void ApmTest::TearDown() {
468 if (frame_) {
469 delete frame_;
470 }
471 frame_ = NULL;
472
473 if (revframe_) {
474 delete revframe_;
475 }
476 revframe_ = NULL;
477
478 if (far_file_) {
479 ASSERT_EQ(0, fclose(far_file_));
480 }
481 far_file_ = NULL;
482
483 if (near_file_) {
484 ASSERT_EQ(0, fclose(near_file_));
485 }
486 near_file_ = NULL;
487
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000488 if (out_file_) {
489 ASSERT_EQ(0, fclose(out_file_));
490 }
491 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492}
493
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000494void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000495 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700496 ap->Initialize(
497 {{{frame_->sample_rate_hz_, frame_->num_channels_},
498 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700499 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700500 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000501}
502
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000503void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000504 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000505 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800506 size_t num_input_channels,
507 size_t num_output_channels,
508 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000509 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000510 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000511 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000512 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000513
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000514 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
515 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000516 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000517
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000518 if (far_file_) {
519 ASSERT_EQ(0, fclose(far_file_));
520 }
521 std::string filename = ResourceFilePath("far", sample_rate_hz);
522 far_file_ = fopen(filename.c_str(), "rb");
523 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
524 filename << "\n";
525
526 if (near_file_) {
527 ASSERT_EQ(0, fclose(near_file_));
528 }
529 filename = ResourceFilePath("near", sample_rate_hz);
530 near_file_ = fopen(filename.c_str(), "rb");
531 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
532 filename << "\n";
533
534 if (open_output_file) {
535 if (out_file_) {
536 ASSERT_EQ(0, fclose(out_file_));
537 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700538 filename = OutputFilePath(
539 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
540 reverse_sample_rate_hz, num_input_channels, num_output_channels,
541 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000542 out_file_ = fopen(filename.c_str(), "wb");
543 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
544 filename << "\n";
545 }
546}
547
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000548void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000549 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000550}
551
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
553 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000554 // The files always contain stereo audio.
555 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700556 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000557 sizeof(int16_t),
558 frame_size,
559 file);
560 if (read_count != frame_size) {
561 // Check that the file really ended.
562 EXPECT_NE(0, feof(file));
563 return false; // This is expected.
564 }
565
566 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700567 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000568 frame->samples_per_channel_);
569 }
570
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000572 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000574 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000575}
576
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
578 return ReadFrame(file, frame, NULL);
579}
580
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000581// If the end of the file has been reached, rewind it and attempt to read the
582// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000583void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
584 ChannelBuffer<float>* cb) {
585 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000586 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000588 }
589}
590
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
592 ReadFrameWithRewind(file, frame, NULL);
593}
594
andrew@webrtc.org81865342012-10-27 00:28:27 +0000595void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
596 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000597 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000598 EXPECT_EQ(apm_->kNoError,
599 apm_->gain_control()->set_stream_analog_level(127));
600 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000601}
602
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000603int ApmTest::ProcessStreamChooser(Format format) {
604 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000605 return apm_->ProcessStream(frame_);
606 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000607 return apm_->ProcessStream(float_cb_->channels(),
608 frame_->samples_per_channel_,
609 frame_->sample_rate_hz_,
610 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000611 output_sample_rate_hz_,
612 LayoutFromChannels(num_output_channels_),
613 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000614}
615
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000616int ApmTest::AnalyzeReverseStreamChooser(Format format) {
617 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700618 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000619 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000620 return apm_->AnalyzeReverseStream(
621 revfloat_cb_->channels(),
622 revframe_->samples_per_channel_,
623 revframe_->sample_rate_hz_,
624 LayoutFromChannels(revframe_->num_channels_));
625}
626
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000627void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
628 int delay_min, int delay_max) {
629 // The |revframe_| and |frame_| should include the proper frame information,
630 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000631 AudioFrame tmp_frame;
632 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000633 bool causal = true;
634
635 tmp_frame.CopyFrom(*revframe_);
636 SetFrameTo(&tmp_frame, 0);
637
638 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
639 // Initialize the |frame_queue| with empty frames.
640 int frame_delay = delay_ms / 10;
641 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000642 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000643 frame->CopyFrom(tmp_frame);
644 frame_queue.push(frame);
645 frame_delay++;
646 causal = false;
647 }
648 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000649 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000650 frame->CopyFrom(tmp_frame);
651 frame_queue.push(frame);
652 frame_delay--;
653 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000654 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
655 // need enough frames with audio to have reliable estimates, but as few as
656 // possible to keep processing time down. 4.5 seconds seemed to be a good
657 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000659 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000660 frame->CopyFrom(tmp_frame);
661 // Use the near end recording, since that has more speech in it.
662 ASSERT_TRUE(ReadFrame(near_file_, frame));
663 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000664 AudioFrame* reverse_frame = frame;
665 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000666 if (!causal) {
667 reverse_frame = frame_queue.front();
668 // When we call ProcessStream() the frame is modified, so we can't use the
669 // pointer directly when things are non-causal. Use an intermediate frame
670 // and copy the data.
671 process_frame = &tmp_frame;
672 process_frame->CopyFrom(*frame);
673 }
aluebsb0319552016-03-17 20:39:53 -0700674 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000675 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
676 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
677 frame = frame_queue.front();
678 frame_queue.pop();
679 delete frame;
680
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000681 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000682 int median;
683 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000684 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000685 // Discard the first delay metrics to avoid convergence effects.
686 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000687 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
688 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 }
690 }
691
692 rewind(near_file_);
693 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000694 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000695 frame_queue.pop();
696 delete frame;
697 }
698 // Calculate expected delay estimate and acceptable regions. Further,
699 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700700 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700701 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700702 const int expected_median =
703 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
704 const int expected_median_high = rtc::SafeClamp<int>(
705 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700706 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700707 const int expected_median_low = rtc::SafeClamp<int>(
708 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700709 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000710 // Verify delay metrics.
711 int median;
712 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000713 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000714 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000715 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
716 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000717 EXPECT_GE(expected_median_high, median);
718 EXPECT_LE(expected_median_low, median);
719}
720
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000721void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000724
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000727 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000730 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 EXPECT_EQ(apm_->kNoError,
732 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000733 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000734 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000736
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000737 // Other stream parameters set correctly.
738 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 EXPECT_EQ(apm_->kNoError,
740 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000741 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000742 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000744 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000745 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
746 EXPECT_EQ(apm_->kNoError,
747 apm_->echo_cancellation()->enable_drift_compensation(false));
748
749 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000750 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000751 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000752 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000753 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000754
755 // Resets after successful ProcessStream().
756 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000758 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000759 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000760
761 // Other stream parameters set correctly.
762 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
763 EXPECT_EQ(apm_->kNoError,
764 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000765 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000766 EXPECT_EQ(apm_->kNoError,
767 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000768 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000769 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000770 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
771
772 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000773 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000774 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000775
776 // Resets after successful ProcessStream().
777 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000778 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000779 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000780 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000782
783 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000784 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
785 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
786 EXPECT_EQ(apm_->kNoError,
787 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000788 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000789 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000790
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000791 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000792 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000793 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000794 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000795 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000796
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000797 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000799 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800 EXPECT_EQ(apm_->kNoError,
801 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000802 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000803}
804
805TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000806 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000807}
808
809TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000810 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000811}
812
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000813TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
814 EXPECT_EQ(0, apm_->delay_offset_ms());
815 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
816 EXPECT_EQ(50, apm_->stream_delay_ms());
817}
818
819TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
820 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000821 apm_->set_delay_offset_ms(100);
822 EXPECT_EQ(100, apm_->delay_offset_ms());
823 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000824 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000825 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
826 EXPECT_EQ(200, apm_->stream_delay_ms());
827
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000828 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000829 apm_->set_delay_offset_ms(-50);
830 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000831 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
832 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000833 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
834 EXPECT_EQ(50, apm_->stream_delay_ms());
835}
836
Michael Graczyk86c6d332015-07-23 11:41:39 -0700837void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800838 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700839 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000840 frame_->num_channels_ = num_channels;
841 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700842 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000843}
844
Michael Graczyk86c6d332015-07-23 11:41:39 -0700845void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800846 size_t num_in_channels,
847 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848 AudioProcessing::Error expected_return) {
849 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
850 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
851
852 EXPECT_EQ(expected_return,
853 apm_->ProcessStream(float_cb_->channels(), input_stream,
854 output_stream, float_cb_->channels()));
855}
856
857void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800858 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700859 AudioProcessing::Error expected_return) {
860 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700861 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
862 {output_sample_rate_hz_, apm_->num_output_channels()},
863 {frame_->sample_rate_hz_, num_rev_channels},
864 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700865
ekmeyerson60d9b332015-08-14 10:35:55 -0700866 EXPECT_EQ(
867 expected_return,
868 apm_->ProcessReverseStream(
869 float_cb_->channels(), processing_config.reverse_input_stream(),
870 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700871}
872
873TEST_F(ApmTest, ChannelsInt16Interface) {
874 // Testing number of invalid and valid channels.
875 Init(16000, 16000, 16000, 4, 4, 4, false);
876
877 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
878
Peter Kasting69558702016-01-12 16:26:35 -0800879 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700880 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000881 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000882 }
883}
884
Michael Graczyk86c6d332015-07-23 11:41:39 -0700885TEST_F(ApmTest, Channels) {
886 // Testing number of invalid and valid channels.
887 Init(16000, 16000, 16000, 4, 4, 4, false);
888
889 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
890 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
891
Peter Kasting69558702016-01-12 16:26:35 -0800892 for (size_t i = 1; i < 4; ++i) {
893 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700894 // Output channels much be one or match input channels.
895 if (j == 1 || i == j) {
896 TestChangingForwardChannels(i, j, kNoErr);
897 TestChangingReverseChannels(i, kNoErr);
898
899 EXPECT_EQ(i, apm_->num_input_channels());
900 EXPECT_EQ(j, apm_->num_output_channels());
901 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800902 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700903 } else {
904 TestChangingForwardChannels(i, j,
905 AudioProcessing::kBadNumberChannelsError);
906 }
907 }
908 }
909}
910
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000911TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000912 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000913 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000914 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700916 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800917 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000918 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000919 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000920 }
921}
922
niklase@google.com470e71d2011-07-07 08:21:25 +0000923TEST_F(ApmTest, EchoCancellation) {
924 EXPECT_EQ(apm_->kNoError,
925 apm_->echo_cancellation()->enable_drift_compensation(true));
926 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
927 EXPECT_EQ(apm_->kNoError,
928 apm_->echo_cancellation()->enable_drift_compensation(false));
929 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
930
niklase@google.com470e71d2011-07-07 08:21:25 +0000931 EchoCancellation::SuppressionLevel level[] = {
932 EchoCancellation::kLowSuppression,
933 EchoCancellation::kModerateSuppression,
934 EchoCancellation::kHighSuppression,
935 };
pkasting25702cb2016-01-08 13:50:27 -0800936 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000937 EXPECT_EQ(apm_->kNoError,
938 apm_->echo_cancellation()->set_suppression_level(level[i]));
939 EXPECT_EQ(level[i],
940 apm_->echo_cancellation()->suppression_level());
941 }
942
943 EchoCancellation::Metrics metrics;
944 EXPECT_EQ(apm_->kNotEnabledError,
945 apm_->echo_cancellation()->GetMetrics(&metrics));
946
ivoc3e9a5372016-10-28 07:55:33 -0700947 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
948 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
949
niklase@google.com470e71d2011-07-07 08:21:25 +0000950 EXPECT_EQ(apm_->kNoError,
951 apm_->echo_cancellation()->enable_metrics(true));
952 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
953 EXPECT_EQ(apm_->kNoError,
954 apm_->echo_cancellation()->enable_metrics(false));
955 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
956
ivoc48dfab52016-10-28 03:29:31 -0700957 EXPECT_EQ(apm_->kNoError,
958 apm_->echo_cancellation()->enable_delay_logging(true));
959 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
960 EXPECT_EQ(apm_->kNoError,
961 apm_->echo_cancellation()->enable_delay_logging(false));
962 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
963
ivoc3e9a5372016-10-28 07:55:33 -0700964 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
965 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
966
967 int median = 0;
968 int std = 0;
969 float poor_fraction = 0;
970 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
971 &median, &std, &poor_fraction));
972
niklase@google.com470e71d2011-07-07 08:21:25 +0000973 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
974 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
975 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
976 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000977
978 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
979 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
980 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
981 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
982 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
983 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000984}
985
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000986TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000987 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000988 // Enable AEC only.
989 EXPECT_EQ(apm_->kNoError,
990 apm_->echo_cancellation()->enable_drift_compensation(false));
991 EXPECT_EQ(apm_->kNoError,
992 apm_->echo_cancellation()->enable_metrics(false));
993 EXPECT_EQ(apm_->kNoError,
994 apm_->echo_cancellation()->enable_delay_logging(true));
995 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000996 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700997 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000998 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000999
1000 // Internally in the AEC the amount of lookahead the delay estimation can
1001 // handle is 15 blocks and the maximum delay is set to 60 blocks.
1002 const int kLookaheadBlocks = 15;
1003 const int kMaxDelayBlocks = 60;
1004 // The AEC has a startup time before it actually starts to process. This
1005 // procedure can flush the internal far-end buffer, which of course affects
1006 // the delay estimation. Therefore, we set a system_delay high enough to
1007 // avoid that. The smallest system_delay you can report without flushing the
1008 // buffer is 66 ms in 8 kHz.
1009 //
1010 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1011 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1012 // delay estimation. This should be noted though. In case of test failure,
1013 // this could be the cause.
1014 const int kSystemDelayMs = 66;
1015 // Test a couple of corner cases and verify that the estimated delay is
1016 // within a valid region (set to +-1.5 blocks). Note that these cases are
1017 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001018 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001019 Init(kProcessSampleRates[i],
1020 kProcessSampleRates[i],
1021 kProcessSampleRates[i],
1022 2,
1023 2,
1024 2,
1025 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001026 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001027 const int num_ms_per_block =
1028 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001029 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1030 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1031
1032 // 1) Verify correct delay estimate at lookahead boundary.
1033 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1034 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1035 delay_max_ms);
1036 // 2) A delay less than maximum lookahead should give an delay estimate at
1037 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1038 delay_ms -= 20;
1039 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1040 delay_max_ms);
1041 // 3) Three values around zero delay. Note that we need to compensate for
1042 // the fake system_delay.
1043 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1044 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1045 delay_max_ms);
1046 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1047 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1048 delay_max_ms);
1049 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1050 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1051 delay_max_ms);
1052 // 4) Verify correct delay estimate at maximum delay boundary.
1053 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1054 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1055 delay_max_ms);
1056 // 5) A delay above the maximum delay should give an estimate at the
1057 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1058 delay_ms += 20;
1059 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1060 delay_max_ms);
1061 }
1062}
1063
niklase@google.com470e71d2011-07-07 08:21:25 +00001064TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001066 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1068 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1069
niklase@google.com470e71d2011-07-07 08:21:25 +00001070 // Toggle routing modes
1071 EchoControlMobile::RoutingMode mode[] = {
1072 EchoControlMobile::kQuietEarpieceOrHeadset,
1073 EchoControlMobile::kEarpiece,
1074 EchoControlMobile::kLoudEarpiece,
1075 EchoControlMobile::kSpeakerphone,
1076 EchoControlMobile::kLoudSpeakerphone,
1077 };
pkasting25702cb2016-01-08 13:50:27 -08001078 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001079 EXPECT_EQ(apm_->kNoError,
1080 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1081 EXPECT_EQ(mode[i],
1082 apm_->echo_control_mobile()->routing_mode());
1083 }
1084 // Turn comfort noise off/on
1085 EXPECT_EQ(apm_->kNoError,
1086 apm_->echo_control_mobile()->enable_comfort_noise(false));
1087 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1088 EXPECT_EQ(apm_->kNoError,
1089 apm_->echo_control_mobile()->enable_comfort_noise(true));
1090 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001091 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001092 const size_t echo_path_size =
1093 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001094 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1095 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001096 EXPECT_EQ(apm_->kNullPointerError,
1097 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1098 EXPECT_EQ(apm_->kNullPointerError,
1099 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1100 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001101 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001102 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001103 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001104 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001105 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001106 echo_path_in[i] = echo_path_out[i] + 1;
1107 }
1108 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001109 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001110 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001111 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1112 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001113 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001114 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1115 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001116 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001117 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1118 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001119
1120 // Process a few frames with NS in the default disabled state. This exercises
1121 // a different codepath than with it enabled.
1122 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1123 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1124 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1126
niklase@google.com470e71d2011-07-07 08:21:25 +00001127 // Turn AECM off
1128 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1129 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1130}
1131
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001132TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001134 EXPECT_EQ(apm_->kNoError,
1135 apm_->gain_control()->set_mode(
1136 apm_->gain_control()->mode()));
1137
1138 GainControl::Mode mode[] = {
1139 GainControl::kAdaptiveAnalog,
1140 GainControl::kAdaptiveDigital,
1141 GainControl::kFixedDigital
1142 };
pkasting25702cb2016-01-08 13:50:27 -08001143 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001144 EXPECT_EQ(apm_->kNoError,
1145 apm_->gain_control()->set_mode(mode[i]));
1146 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1147 }
1148 // Testing invalid target levels
1149 EXPECT_EQ(apm_->kBadParameterError,
1150 apm_->gain_control()->set_target_level_dbfs(-3));
1151 EXPECT_EQ(apm_->kBadParameterError,
1152 apm_->gain_control()->set_target_level_dbfs(-40));
1153 // Testing valid target levels
1154 EXPECT_EQ(apm_->kNoError,
1155 apm_->gain_control()->set_target_level_dbfs(
1156 apm_->gain_control()->target_level_dbfs()));
1157
1158 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001159 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001160 EXPECT_EQ(apm_->kNoError,
1161 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1162 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1163 }
1164
1165 // Testing invalid compression gains
1166 EXPECT_EQ(apm_->kBadParameterError,
1167 apm_->gain_control()->set_compression_gain_db(-1));
1168 EXPECT_EQ(apm_->kBadParameterError,
1169 apm_->gain_control()->set_compression_gain_db(100));
1170
1171 // Testing valid compression gains
1172 EXPECT_EQ(apm_->kNoError,
1173 apm_->gain_control()->set_compression_gain_db(
1174 apm_->gain_control()->compression_gain_db()));
1175
1176 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001177 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001178 EXPECT_EQ(apm_->kNoError,
1179 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1180 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1181 }
1182
1183 // Testing limiter off/on
1184 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1185 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1186 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1187 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1188
1189 // Testing invalid level limits
1190 EXPECT_EQ(apm_->kBadParameterError,
1191 apm_->gain_control()->set_analog_level_limits(-1, 512));
1192 EXPECT_EQ(apm_->kBadParameterError,
1193 apm_->gain_control()->set_analog_level_limits(100000, 512));
1194 EXPECT_EQ(apm_->kBadParameterError,
1195 apm_->gain_control()->set_analog_level_limits(512, -1));
1196 EXPECT_EQ(apm_->kBadParameterError,
1197 apm_->gain_control()->set_analog_level_limits(512, 100000));
1198 EXPECT_EQ(apm_->kBadParameterError,
1199 apm_->gain_control()->set_analog_level_limits(512, 255));
1200
1201 // Testing valid level limits
1202 EXPECT_EQ(apm_->kNoError,
1203 apm_->gain_control()->set_analog_level_limits(
1204 apm_->gain_control()->analog_level_minimum(),
1205 apm_->gain_control()->analog_level_maximum()));
1206
1207 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001208 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001209 EXPECT_EQ(apm_->kNoError,
1210 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1211 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1212 }
1213
1214 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001215 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001216 EXPECT_EQ(apm_->kNoError,
1217 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1218 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1219 }
1220
1221 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1222
1223 // Turn AGC off
1224 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1225 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1226}
1227
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001228void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001229 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001230 EXPECT_EQ(apm_->kNoError,
1231 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1232 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1233
1234 int out_analog_level = 0;
1235 for (int i = 0; i < 2000; ++i) {
1236 ReadFrameWithRewind(near_file_, frame_);
1237 // Ensure the audio is at a low level, so the AGC will try to increase it.
1238 ScaleFrame(frame_, 0.25);
1239
1240 // Always pass in the same volume.
1241 EXPECT_EQ(apm_->kNoError,
1242 apm_->gain_control()->set_stream_analog_level(100));
1243 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1244 out_analog_level = apm_->gain_control()->stream_analog_level();
1245 }
1246
1247 // Ensure the AGC is still able to reach the maximum.
1248 EXPECT_EQ(255, out_analog_level);
1249}
1250
1251// Verifies that despite volume slider quantization, the AGC can continue to
1252// increase its volume.
1253TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001254 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001255 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1256 }
1257}
1258
1259void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001260 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001261 EXPECT_EQ(apm_->kNoError,
1262 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1263 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1264
1265 int out_analog_level = 100;
1266 for (int i = 0; i < 1000; ++i) {
1267 ReadFrameWithRewind(near_file_, frame_);
1268 // Ensure the audio is at a low level, so the AGC will try to increase it.
1269 ScaleFrame(frame_, 0.25);
1270
1271 EXPECT_EQ(apm_->kNoError,
1272 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1273 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1274 out_analog_level = apm_->gain_control()->stream_analog_level();
1275 }
1276
1277 // Ensure the volume was raised.
1278 EXPECT_GT(out_analog_level, 100);
1279 int highest_level_reached = out_analog_level;
1280 // Simulate a user manual volume change.
1281 out_analog_level = 100;
1282
1283 for (int i = 0; i < 300; ++i) {
1284 ReadFrameWithRewind(near_file_, frame_);
1285 ScaleFrame(frame_, 0.25);
1286
1287 EXPECT_EQ(apm_->kNoError,
1288 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1289 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1290 out_analog_level = apm_->gain_control()->stream_analog_level();
1291 // Check that AGC respected the manually adjusted volume.
1292 EXPECT_LT(out_analog_level, highest_level_reached);
1293 }
1294 // Check that the volume was still raised.
1295 EXPECT_GT(out_analog_level, 100);
1296}
1297
1298TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001299 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001300 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1301 }
1302}
1303
niklase@google.com470e71d2011-07-07 08:21:25 +00001304TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001305 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001306 NoiseSuppression::Level level[] = {
1307 NoiseSuppression::kLow,
1308 NoiseSuppression::kModerate,
1309 NoiseSuppression::kHigh,
1310 NoiseSuppression::kVeryHigh
1311 };
pkasting25702cb2016-01-08 13:50:27 -08001312 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001313 EXPECT_EQ(apm_->kNoError,
1314 apm_->noise_suppression()->set_level(level[i]));
1315 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1316 }
1317
andrew@webrtc.org648af742012-02-08 01:57:29 +00001318 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001319 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1320 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1321 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1322 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1323}
1324
1325TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001326 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001327 AudioProcessing::Config apm_config;
1328 apm_config.high_pass_filter.enabled = true;
1329 apm_->ApplyConfig(apm_config);
1330 apm_config.high_pass_filter.enabled = false;
1331 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001332}
1333
1334TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001335 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001336 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001337 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001338
1339 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1340
1341 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1342 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1343
1344 // Run this test in wideband; in super-wb, the splitting filter distorts the
1345 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001346 frame_->samples_per_channel_ = 160;
1347 frame_->num_channels_ = 2;
1348 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001349
1350 // Min value if no frames have been processed.
1351 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1352
1353 // Min value on zero frames.
1354 SetFrameTo(frame_, 0);
1355 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1356 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1357 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1358
1359 // Try a few RMS values.
1360 // (These also test that the value resets after retrieving it.)
1361 SetFrameTo(frame_, 32767);
1362 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1363 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1364 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1365
1366 SetFrameTo(frame_, 30000);
1367 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1368 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1369 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1370
1371 SetFrameTo(frame_, 10000);
1372 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1373 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1374 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1375
1376 SetFrameTo(frame_, 10);
1377 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1378 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1379 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1380
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001381 // Verify reset after enable/disable.
1382 SetFrameTo(frame_, 32767);
1383 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1384 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1385 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1386 SetFrameTo(frame_, 1);
1387 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1388 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1389
1390 // Verify reset after initialize.
1391 SetFrameTo(frame_, 32767);
1392 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1393 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1394 SetFrameTo(frame_, 1);
1395 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1396 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001397}
1398
1399TEST_F(ApmTest, VoiceDetection) {
1400 // Test external VAD
1401 EXPECT_EQ(apm_->kNoError,
1402 apm_->voice_detection()->set_stream_has_voice(true));
1403 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1404 EXPECT_EQ(apm_->kNoError,
1405 apm_->voice_detection()->set_stream_has_voice(false));
1406 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1407
andrew@webrtc.org648af742012-02-08 01:57:29 +00001408 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 VoiceDetection::Likelihood likelihood[] = {
1410 VoiceDetection::kVeryLowLikelihood,
1411 VoiceDetection::kLowLikelihood,
1412 VoiceDetection::kModerateLikelihood,
1413 VoiceDetection::kHighLikelihood
1414 };
pkasting25702cb2016-01-08 13:50:27 -08001415 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 EXPECT_EQ(apm_->kNoError,
1417 apm_->voice_detection()->set_likelihood(likelihood[i]));
1418 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1419 }
1420
1421 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001422 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001423 EXPECT_EQ(apm_->kBadParameterError,
1424 apm_->voice_detection()->set_frame_size_ms(12));
1425
andrew@webrtc.org648af742012-02-08 01:57:29 +00001426 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001427 for (int i = 10; i <= 30; i += 10) {
1428 EXPECT_EQ(apm_->kNoError,
1429 apm_->voice_detection()->set_frame_size_ms(i));
1430 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1431 }
1432 */
1433
andrew@webrtc.org648af742012-02-08 01:57:29 +00001434 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001435 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1436 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1437 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1438 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1439
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001440 // Test that AudioFrame activity is maintained when VAD is disabled.
1441 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1442 AudioFrame::VADActivity activity[] = {
1443 AudioFrame::kVadActive,
1444 AudioFrame::kVadPassive,
1445 AudioFrame::kVadUnknown
1446 };
pkasting25702cb2016-01-08 13:50:27 -08001447 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001448 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001450 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001451 }
1452
1453 // Test that AudioFrame activity is set when VAD is enabled.
1454 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001455 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001456 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001457 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001458
niklase@google.com470e71d2011-07-07 08:21:25 +00001459 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1460}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001461
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001462TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1463 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1464 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1465 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1466 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1467 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1468 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1469 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1470}
1471
1472TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001473 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001474 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001475 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001476 AudioFrame frame_copy;
1477 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001478 for (int j = 0; j < 1000; j++) {
1479 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1480 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001481 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1482 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001483 }
1484 }
1485}
1486
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001487TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1488 // Test that ProcessStream copies input to output even with no processing.
1489 const size_t kSamples = 80;
1490 const int sample_rate = 8000;
1491 const float src[kSamples] = {
1492 -1.0f, 0.0f, 1.0f
1493 };
1494 float dest[kSamples] = {};
1495
1496 auto src_channels = &src[0];
1497 auto dest_channels = &dest[0];
1498
Ivo Creusen62337e52018-01-09 14:17:33 +01001499 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001500 EXPECT_NOERR(apm_->ProcessStream(
1501 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1502 sample_rate, LayoutFromChannels(1), &dest_channels));
1503
1504 for (size_t i = 0; i < kSamples; ++i) {
1505 EXPECT_EQ(src[i], dest[i]);
1506 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001507
1508 // Same for ProcessReverseStream.
1509 float rev_dest[kSamples] = {};
1510 auto rev_dest_channels = &rev_dest[0];
1511
1512 StreamConfig input_stream = {sample_rate, 1};
1513 StreamConfig output_stream = {sample_rate, 1};
1514 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1515 output_stream, &rev_dest_channels));
1516
1517 for (size_t i = 0; i < kSamples; ++i) {
1518 EXPECT_EQ(src[i], rev_dest[i]);
1519 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001520}
1521
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001522TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1523 EnableAllComponents();
1524
pkasting25702cb2016-01-08 13:50:27 -08001525 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001526 Init(kProcessSampleRates[i],
1527 kProcessSampleRates[i],
1528 kProcessSampleRates[i],
1529 2,
1530 2,
1531 2,
1532 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001533 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001534 ASSERT_EQ(0, feof(far_file_));
1535 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001536 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001537 CopyLeftToRightChannel(revframe_->mutable_data(),
1538 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001539
aluebsb0319552016-03-17 20:39:53 -07001540 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001541
yujo36b1a5f2017-06-12 12:45:32 -07001542 CopyLeftToRightChannel(frame_->mutable_data(),
1543 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001544 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1545
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001546 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001547 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001548 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001549 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001550 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001551 analog_level = apm_->gain_control()->stream_analog_level();
1552
yujo36b1a5f2017-06-12 12:45:32 -07001553 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001554 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001555 rewind(far_file_);
1556 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001557 }
1558}
1559
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001560TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001561 // Verify the filter is not active through undistorted audio when:
1562 // 1. No components are enabled...
1563 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001564 AudioFrame frame_copy;
1565 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001566 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1567 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1568 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1569
1570 // 2. Only the level estimator is enabled...
1571 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001572 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001573 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1574 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1575 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1576 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1577 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1578
1579 // 3. Only VAD is enabled...
1580 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001581 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001582 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1583 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1584 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1585 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1586 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1587
1588 // 4. Both VAD and the level estimator are enabled...
1589 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001590 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001591 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1592 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1593 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1594 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1595 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1596 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1597 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1598
1599 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001600 frame_->samples_per_channel_ = 160;
1601 frame_->num_channels_ = 2;
1602 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001603 // Enable AEC, which would require the filter in super-wb. We rely on the
1604 // first few frames of data being unaffected by the AEC.
1605 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1606 // behavior of the AEC. Think of something more robust.
1607 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001608 // Make sure we have extended filter enabled. This makes sure nothing is
1609 // touched until we have a farend frame.
1610 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001611 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001612 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001613 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001614 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001615 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001616 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1618 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001619 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001620 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1621 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1622
1623 // Check the test is valid. We should have distortion from the filter
1624 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001625 frame_->samples_per_channel_ = 320;
1626 frame_->num_channels_ = 2;
1627 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001628 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001629 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001630 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001631 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1633 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1634}
1635
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001636#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1637void ApmTest::ProcessDebugDump(const std::string& in_filename,
1638 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001639 Format format,
1640 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001641 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001642 FILE* in_file = fopen(in_filename.c_str(), "rb");
1643 ASSERT_TRUE(in_file != NULL);
1644 audioproc::Event event_msg;
1645 bool first_init = true;
1646
1647 while (ReadMessageFromFile(in_file, &event_msg)) {
1648 if (event_msg.type() == audioproc::Event::INIT) {
1649 const audioproc::Init msg = event_msg.init();
1650 int reverse_sample_rate = msg.sample_rate();
1651 if (msg.has_reverse_sample_rate()) {
1652 reverse_sample_rate = msg.reverse_sample_rate();
1653 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001654 int output_sample_rate = msg.sample_rate();
1655 if (msg.has_output_sample_rate()) {
1656 output_sample_rate = msg.output_sample_rate();
1657 }
1658
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001659 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001660 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001661 reverse_sample_rate,
1662 msg.num_input_channels(),
1663 msg.num_output_channels(),
1664 msg.num_reverse_channels(),
1665 false);
1666 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001667 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001668 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001669 auto aec_dump =
1670 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1671 EXPECT_TRUE(aec_dump);
1672 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001673 first_init = false;
1674 }
1675
1676 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1677 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1678
1679 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001680 ASSERT_EQ(revframe_->num_channels_,
1681 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001682 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001683 memcpy(revfloat_cb_->channels()[i],
1684 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001685 msg.channel(i).size());
1686 }
1687 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001688 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001689 if (format == kFloatFormat) {
1690 // We're using an int16 input file; convert to float.
1691 ConvertToFloat(*revframe_, revfloat_cb_.get());
1692 }
1693 }
1694 AnalyzeReverseStreamChooser(format);
1695
1696 } else if (event_msg.type() == audioproc::Event::STREAM) {
1697 const audioproc::Stream msg = event_msg.stream();
1698 // ProcessStream could have changed this for the output frame.
1699 frame_->num_channels_ = apm_->num_input_channels();
1700
1701 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1702 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1703 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1704 if (msg.has_keypress()) {
1705 apm_->set_stream_key_pressed(msg.keypress());
1706 } else {
1707 apm_->set_stream_key_pressed(true);
1708 }
1709
1710 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001711 ASSERT_EQ(frame_->num_channels_,
1712 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001713 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001714 memcpy(float_cb_->channels()[i],
1715 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001716 msg.input_channel(i).size());
1717 }
1718 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001719 memcpy(frame_->mutable_data(), msg.input_data().data(),
1720 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001721 if (format == kFloatFormat) {
1722 // We're using an int16 input file; convert to float.
1723 ConvertToFloat(*frame_, float_cb_.get());
1724 }
1725 }
1726 ProcessStreamChooser(format);
1727 }
1728 }
aleloif4dd1912017-06-15 01:55:38 -07001729 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001730 fclose(in_file);
1731}
1732
1733void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001734 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001735 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001736 std::string format_string;
1737 switch (format) {
1738 case kIntFormat:
1739 format_string = "_int";
1740 break;
1741 case kFloatFormat:
1742 format_string = "_float";
1743 break;
1744 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001745 const std::string ref_filename = test::TempFilename(
1746 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1747 const std::string out_filename = test::TempFilename(
1748 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001749 const std::string limited_filename = test::TempFilename(
1750 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1751 const size_t logging_limit_bytes = 100000;
1752 // We expect at least this many bytes in the created logfile.
1753 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001754 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001755 ProcessDebugDump(in_filename, ref_filename, format, -1);
1756 ProcessDebugDump(ref_filename, out_filename, format, -1);
1757 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001758
1759 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1760 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001761 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001762 ASSERT_TRUE(ref_file != NULL);
1763 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001764 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001765 std::unique_ptr<uint8_t[]> ref_bytes;
1766 std::unique_ptr<uint8_t[]> out_bytes;
1767 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001768
1769 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1770 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001771 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001772 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001773 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001774 while (ref_size > 0 && out_size > 0) {
1775 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001776 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001777 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001778 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001779 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001780 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001781 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1782 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001783 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001784 }
1785 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001786 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1787 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001788 EXPECT_NE(0, feof(ref_file));
1789 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001790 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001791 ASSERT_EQ(0, fclose(ref_file));
1792 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001793 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001794 remove(ref_filename.c_str());
1795 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001796 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001797}
1798
pbosc7a65692016-05-06 12:50:04 -07001799TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001800 VerifyDebugDumpTest(kIntFormat);
1801}
1802
pbosc7a65692016-05-06 12:50:04 -07001803TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001804 VerifyDebugDumpTest(kFloatFormat);
1805}
1806#endif
1807
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001808// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001809TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001810 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001811 const std::string filename =
1812 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001813 {
1814 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1815 EXPECT_FALSE(aec_dump);
1816 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001817
1818#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1819 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001820 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001821
aleloif4dd1912017-06-15 01:55:38 -07001822 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1823 EXPECT_TRUE(aec_dump);
1824 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001825 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001826 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001827 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001828
1829 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001830 FILE* fid = fopen(filename.c_str(), "r");
1831 ASSERT_TRUE(fid != NULL);
1832
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001833 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001834 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001835 ASSERT_EQ(0, remove(filename.c_str()));
1836#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001837 // Verify the file has NOT been written.
1838 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1839#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1840}
1841
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001842// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001843TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001844 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1845
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001846 const std::string filename =
1847 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001848 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001849 ASSERT_TRUE(fid);
1850
1851#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1852 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001853 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001854
aleloif4dd1912017-06-15 01:55:38 -07001855 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1856 EXPECT_TRUE(aec_dump);
1857 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001858 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001859 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001860 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001861
1862 // Verify the file has been written.
1863 fid = fopen(filename.c_str(), "r");
1864 ASSERT_TRUE(fid != NULL);
1865
1866 // Clean it up.
1867 ASSERT_EQ(0, fclose(fid));
1868 ASSERT_EQ(0, remove(filename.c_str()));
1869#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001870 ASSERT_EQ(0, fclose(fid));
1871#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1872}
1873
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001874TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001875 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001876 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001877
1878 Config config;
1879 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001880 std::unique_ptr<AudioProcessing> fapm(
1881 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001882 EnableAllComponents();
1883 EnableAllAPComponents(fapm.get());
1884 for (int i = 0; i < ref_data.test_size(); i++) {
1885 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1886
1887 audioproc::Test* test = ref_data.mutable_test(i);
1888 // TODO(ajm): Restore downmixing test cases.
1889 if (test->num_input_channels() != test->num_output_channels())
1890 continue;
1891
Peter Kasting69558702016-01-12 16:26:35 -08001892 const size_t num_render_channels =
1893 static_cast<size_t>(test->num_reverse_channels());
1894 const size_t num_input_channels =
1895 static_cast<size_t>(test->num_input_channels());
1896 const size_t num_output_channels =
1897 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001898 const size_t samples_per_channel = static_cast<size_t>(
1899 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001900
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001901 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1902 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001903 Init(fapm.get());
1904
1905 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001906 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1907 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001908
1909 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001910 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001911 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1912 ReadFrame(near_file_, frame_, float_cb_.get())) {
1913 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1914
aluebsb0319552016-03-17 20:39:53 -07001915 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001916 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1917 revfloat_cb_->channels(),
1918 samples_per_channel,
1919 test->sample_rate(),
1920 LayoutFromChannels(num_render_channels)));
1921
1922 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1923 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1924 apm_->echo_cancellation()->set_stream_drift_samples(0);
1925 fapm->echo_cancellation()->set_stream_drift_samples(0);
1926 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1927 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1928
1929 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001930 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001931 output_int16.channels());
1932
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001933 EXPECT_NOERR(fapm->ProcessStream(
1934 float_cb_->channels(),
1935 samples_per_channel,
1936 test->sample_rate(),
1937 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001938 test->sample_rate(),
1939 LayoutFromChannels(num_output_channels),
1940 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001941 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001942 FloatToS16(float_cb_->channels()[j],
1943 samples_per_channel,
1944 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001945 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001946 float snr = ComputeSNR(output_int16.channels()[j],
1947 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001948 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001949
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001950 const float kVarianceThreshold = 20;
1951 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001952
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001953 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001954 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1955 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001956 }
1957 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001958
1959 analog_level = fapm->gain_control()->stream_analog_level();
1960 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1961 fapm->gain_control()->stream_analog_level());
1962 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
1963 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001964 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1965 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001966 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001967
1968 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001969 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001970 }
aluebs776593b2016-03-15 14:04:58 -07001971
1972#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1973 const size_t kMaxNumBadChunks = 0;
1974#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1975 // There are a few chunks in the fixed-point profile that give low SNR.
1976 // Listening confirmed the difference is acceptable.
1977 const size_t kMaxNumBadChunks = 60;
1978#endif
1979 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1980
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001981 rewind(far_file_);
1982 rewind(near_file_);
1983 }
1984}
1985
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001986// TODO(andrew): Add a test to process a few frames with different combinations
1987// of enabled components.
1988
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001989TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001990 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001991 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001992
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001993 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001994 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001995 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001996 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001997 for (size_t i = 0; i < arraysize(kChannels); i++) {
1998 for (size_t j = 0; j < arraysize(kChannels); j++) {
1999 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002000 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002001 test->set_num_reverse_channels(kChannels[i]);
2002 test->set_num_input_channels(kChannels[j]);
2003 test->set_num_output_channels(kChannels[j]);
2004 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002005 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002006 }
2007 }
2008 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002009#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2010 // To test the extended filter mode.
2011 audioproc::Test* test = ref_data.add_test();
2012 test->set_num_reverse_channels(2);
2013 test->set_num_input_channels(2);
2014 test->set_num_output_channels(2);
2015 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2016 test->set_use_aec_extended_filter(true);
2017#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002018 }
2019
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002020 for (int i = 0; i < ref_data.test_size(); i++) {
2021 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002022
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002023 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002024 // TODO(ajm): We no longer allow different input and output channels. Skip
2025 // these tests for now, but they should be removed from the set.
2026 if (test->num_input_channels() != test->num_output_channels())
2027 continue;
2028
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002029 Config config;
2030 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002031 config.Set<ExtendedFilter>(
2032 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01002033 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002034
2035 EnableAllComponents();
2036
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002037 Init(test->sample_rate(),
2038 test->sample_rate(),
2039 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002040 static_cast<size_t>(test->num_input_channels()),
2041 static_cast<size_t>(test->num_output_channels()),
2042 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002043 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002044
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002045 int frame_count = 0;
2046 int has_echo_count = 0;
2047 int has_voice_count = 0;
2048 int is_saturated_count = 0;
2049 int analog_level = 127;
2050 int analog_level_average = 0;
2051 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002052 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002053#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2054 int stats_index = 0;
2055#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002056
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002057 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002058 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002059
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002060 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2061
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002062 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002063 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002064 EXPECT_EQ(apm_->kNoError,
2065 apm_->gain_control()->set_stream_analog_level(analog_level));
2066
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002067 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002068
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002069 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002070 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2071 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002072
2073 max_output_average += MaxAudioFrame(*frame_);
2074
2075 if (apm_->echo_cancellation()->stream_has_echo()) {
2076 has_echo_count++;
2077 }
2078
2079 analog_level = apm_->gain_control()->stream_analog_level();
2080 analog_level_average += analog_level;
2081 if (apm_->gain_control()->stream_is_saturated()) {
2082 is_saturated_count++;
2083 }
2084 if (apm_->voice_detection()->stream_has_voice()) {
2085 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002086 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002087 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002088 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002089 }
2090
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002091 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2092
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002093 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002094 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002095 sizeof(int16_t),
2096 frame_size,
2097 out_file_);
2098 ASSERT_EQ(frame_size, write_count);
2099
2100 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002101 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002102 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002103
2104#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2105 const int kStatsAggregationFrameNum = 100; // 1 second.
2106 if (frame_count % kStatsAggregationFrameNum == 0) {
2107 // Get echo metrics.
2108 EchoCancellation::Metrics echo_metrics;
2109 EXPECT_EQ(apm_->kNoError,
2110 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2111
2112 // Get delay metrics.
2113 int median = 0;
2114 int std = 0;
2115 float fraction_poor_delays = 0;
2116 EXPECT_EQ(apm_->kNoError,
2117 apm_->echo_cancellation()->GetDelayMetrics(
2118 &median, &std, &fraction_poor_delays));
2119
2120 // Get RMS.
2121 int rms_level = apm_->level_estimator()->RMS();
2122 EXPECT_LE(0, rms_level);
2123 EXPECT_GE(127, rms_level);
2124
2125 if (!write_ref_data) {
2126 const audioproc::Test::EchoMetrics& reference =
2127 test->echo_metrics(stats_index);
2128 TestStats(echo_metrics.residual_echo_return_loss,
2129 reference.residual_echo_return_loss());
2130 TestStats(echo_metrics.echo_return_loss,
2131 reference.echo_return_loss());
2132 TestStats(echo_metrics.echo_return_loss_enhancement,
2133 reference.echo_return_loss_enhancement());
2134 TestStats(echo_metrics.a_nlp,
2135 reference.a_nlp());
2136 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2137 reference.divergent_filter_fraction());
2138
2139 const audioproc::Test::DelayMetrics& reference_delay =
2140 test->delay_metrics(stats_index);
2141 EXPECT_EQ(reference_delay.median(), median);
2142 EXPECT_EQ(reference_delay.std(), std);
2143 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2144 fraction_poor_delays);
2145
2146 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2147
2148 ++stats_index;
2149 } else {
2150 audioproc::Test::EchoMetrics* message =
2151 test->add_echo_metrics();
2152 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2153 message->mutable_residual_echo_return_loss());
2154 WriteStatsMessage(echo_metrics.echo_return_loss,
2155 message->mutable_echo_return_loss());
2156 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2157 message->mutable_echo_return_loss_enhancement());
2158 WriteStatsMessage(echo_metrics.a_nlp,
2159 message->mutable_a_nlp());
2160 message->set_divergent_filter_fraction(
2161 echo_metrics.divergent_filter_fraction);
2162
2163 audioproc::Test::DelayMetrics* message_delay =
2164 test->add_delay_metrics();
2165 message_delay->set_median(median);
2166 message_delay->set_std(std);
2167 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2168
2169 test->add_rms_level(rms_level);
2170 }
2171 }
2172#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002173 }
2174 max_output_average /= frame_count;
2175 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002176 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002177
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002178 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002179 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002180 // When running the test on a N7 we get a {2, 6} difference of
2181 // |has_voice_count| and |max_output_average| is up to 18 higher.
2182 // All numbers being consistently higher on N7 compare to ref_data.
2183 // TODO(bjornv): If we start getting more of these offsets on Android we
2184 // should consider a different approach. Either using one slack for all,
2185 // or generate a separate android reference.
2186#if defined(WEBRTC_ANDROID)
2187 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002188 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002189 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002190 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002191#else
2192 const int kHasVoiceCountOffset = 0;
2193 const int kHasVoiceCountNear = kIntNear;
2194 const int kMaxOutputAverageOffset = 0;
2195 const int kMaxOutputAverageNear = kIntNear;
2196#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002197 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002198 EXPECT_NEAR(test->has_voice_count(),
2199 has_voice_count - kHasVoiceCountOffset,
2200 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002201 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002202
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002203 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002204 EXPECT_NEAR(test->max_output_average(),
2205 max_output_average - kMaxOutputAverageOffset,
2206 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002207#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002208 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002209 EXPECT_NEAR(test->ns_speech_probability_average(),
2210 ns_speech_prob_average,
2211 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002212#endif
2213 } else {
2214 test->set_has_echo_count(has_echo_count);
2215 test->set_has_voice_count(has_voice_count);
2216 test->set_is_saturated_count(is_saturated_count);
2217
2218 test->set_analog_level_average(analog_level_average);
2219 test->set_max_output_average(max_output_average);
2220
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002221#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002222 EXPECT_LE(0.0f, ns_speech_prob_average);
2223 EXPECT_GE(1.0f, ns_speech_prob_average);
2224 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002225#endif
2226 }
2227
2228 rewind(far_file_);
2229 rewind(near_file_);
2230 }
2231
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002232 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002233 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002234 }
2235}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002236
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002237TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2238 struct ChannelFormat {
2239 AudioProcessing::ChannelLayout in_layout;
2240 AudioProcessing::ChannelLayout out_layout;
2241 };
2242 ChannelFormat cf[] = {
2243 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2244 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2245 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2246 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002247
Ivo Creusen62337e52018-01-09 14:17:33 +01002248 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002249 // Enable one component just to ensure some processing takes place.
2250 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002251 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002252 const int in_rate = 44100;
2253 const int out_rate = 48000;
2254 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2255 TotalChannelsFromLayout(cf[i].in_layout));
2256 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2257 ChannelsFromLayout(cf[i].out_layout));
2258
2259 // Run over a few chunks.
2260 for (int j = 0; j < 10; ++j) {
2261 EXPECT_NOERR(ap->ProcessStream(
2262 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002263 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002264 in_rate,
2265 cf[i].in_layout,
2266 out_rate,
2267 cf[i].out_layout,
2268 out_cb.channels()));
2269 }
2270 }
2271}
2272
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002273// Compares the reference and test arrays over a region around the expected
2274// delay. Finds the highest SNR in that region and adds the variance and squared
2275// error results to the supplied accumulators.
2276void UpdateBestSNR(const float* ref,
2277 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002278 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002279 int expected_delay,
2280 double* variance_acc,
2281 double* sq_error_acc) {
2282 double best_snr = std::numeric_limits<double>::min();
2283 double best_variance = 0;
2284 double best_sq_error = 0;
2285 // Search over a region of eight samples around the expected delay.
2286 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2287 ++delay) {
2288 double sq_error = 0;
2289 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002290 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002291 double error = test[i + delay] - ref[i];
2292 sq_error += error * error;
2293 variance += ref[i] * ref[i];
2294 }
2295
2296 if (sq_error == 0) {
2297 *variance_acc += variance;
2298 return;
2299 }
2300 double snr = variance / sq_error;
2301 if (snr > best_snr) {
2302 best_snr = snr;
2303 best_variance = variance;
2304 best_sq_error = sq_error;
2305 }
2306 }
2307
2308 *variance_acc += best_variance;
2309 *sq_error_acc += best_sq_error;
2310}
2311
2312// Used to test a multitude of sample rate and channel combinations. It works
2313// by first producing a set of reference files (in SetUpTestCase) that are
2314// assumed to be correct, as the used parameters are verified by other tests
2315// in this collection. Primarily the reference files are all produced at
2316// "native" rates which do not involve any resampling.
2317
2318// Each test pass produces an output file with a particular format. The output
2319// is matched against the reference file closest to its internal processing
2320// format. If necessary the output is resampled back to its process format.
2321// Due to the resampling distortion, we don't expect identical results, but
2322// enforce SNR thresholds which vary depending on the format. 0 is a special
2323// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002324typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002325class AudioProcessingTest
2326 : public testing::TestWithParam<AudioProcessingTestData> {
2327 public:
2328 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002329 : input_rate_(std::get<0>(GetParam())),
2330 output_rate_(std::get<1>(GetParam())),
2331 reverse_input_rate_(std::get<2>(GetParam())),
2332 reverse_output_rate_(std::get<3>(GetParam())),
2333 expected_snr_(std::get<4>(GetParam())),
2334 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002335
2336 virtual ~AudioProcessingTest() {}
2337
2338 static void SetUpTestCase() {
2339 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002340 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002341 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002342 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2343 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2344 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002345 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002346 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2347 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2348 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002349 }
2350 }
2351 }
2352 }
2353
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002354 void TearDown() {
2355 // Remove "out" files after each test.
2356 ClearTempOutFiles();
2357 }
2358
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002359 static void TearDownTestCase() {
2360 ClearTempFiles();
2361 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002362
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002363 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002364 // to a file specified with |output_file_prefix|. Both forward and reverse
2365 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002366 static void ProcessFormat(int input_rate,
2367 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002368 int reverse_input_rate,
2369 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002370 size_t num_input_channels,
2371 size_t num_output_channels,
2372 size_t num_reverse_input_channels,
2373 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002374 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002375 Config config;
2376 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002377 std::unique_ptr<AudioProcessing> ap(
2378 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002379 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002380
ekmeyerson60d9b332015-08-14 10:35:55 -07002381 ProcessingConfig processing_config = {
2382 {{input_rate, num_input_channels},
2383 {output_rate, num_output_channels},
2384 {reverse_input_rate, num_reverse_input_channels},
2385 {reverse_output_rate, num_reverse_output_channels}}};
2386 ap->Initialize(processing_config);
2387
2388 FILE* far_file =
2389 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002390 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002391 FILE* out_file =
2392 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2393 reverse_input_rate, reverse_output_rate,
2394 num_input_channels, num_output_channels,
2395 num_reverse_input_channels,
2396 num_reverse_output_channels, kForward).c_str(),
2397 "wb");
2398 FILE* rev_out_file =
2399 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2400 reverse_input_rate, reverse_output_rate,
2401 num_input_channels, num_output_channels,
2402 num_reverse_input_channels,
2403 num_reverse_output_channels, kReverse).c_str(),
2404 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002405 ASSERT_TRUE(far_file != NULL);
2406 ASSERT_TRUE(near_file != NULL);
2407 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002408 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409
2410 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2411 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002412 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2413 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002414 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2415 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002416 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2417 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002418
2419 // Temporary buffers.
2420 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002421 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2422 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002423 std::unique_ptr<float[]> float_data(new float[max_length]);
2424 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002425
2426 int analog_level = 127;
2427 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2428 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002429 EXPECT_NOERR(ap->ProcessReverseStream(
2430 rev_cb.channels(), processing_config.reverse_input_stream(),
2431 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432
2433 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2434 ap->echo_cancellation()->set_stream_drift_samples(0);
2435 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2436
2437 EXPECT_NOERR(ap->ProcessStream(
2438 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002439 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002440 input_rate,
2441 LayoutFromChannels(num_input_channels),
2442 output_rate,
2443 LayoutFromChannels(num_output_channels),
2444 out_cb.channels()));
2445
ekmeyerson60d9b332015-08-14 10:35:55 -07002446 // Dump forward output to file.
2447 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002448 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002449 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002450
pkasting25702cb2016-01-08 13:50:27 -08002451 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002452 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002453 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002454
ekmeyerson60d9b332015-08-14 10:35:55 -07002455 // Dump reverse output to file.
2456 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2457 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002458 size_t rev_out_length =
2459 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002460
pkasting25702cb2016-01-08 13:50:27 -08002461 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002462 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2463 rev_out_file));
2464
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002465 analog_level = ap->gain_control()->stream_analog_level();
2466 }
2467 fclose(far_file);
2468 fclose(near_file);
2469 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002470 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002471 }
2472
2473 protected:
2474 int input_rate_;
2475 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002476 int reverse_input_rate_;
2477 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002478 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002479 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002480};
2481
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002482TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002483 struct ChannelFormat {
2484 int num_input;
2485 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002486 int num_reverse_input;
2487 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002488 };
2489 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002490 {1, 1, 1, 1},
2491 {1, 1, 2, 1},
2492 {2, 1, 1, 1},
2493 {2, 1, 2, 1},
2494 {2, 2, 1, 1},
2495 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002496 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002497
pkasting25702cb2016-01-08 13:50:27 -08002498 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002499 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2500 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2501 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002502
ekmeyerson60d9b332015-08-14 10:35:55 -07002503 // Verify output for both directions.
2504 std::vector<StreamDirection> stream_directions;
2505 stream_directions.push_back(kForward);
2506 stream_directions.push_back(kReverse);
2507 for (StreamDirection file_direction : stream_directions) {
2508 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2509 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2510 const int out_num =
2511 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2512 const double expected_snr =
2513 file_direction ? expected_reverse_snr_ : expected_snr_;
2514
2515 const int min_ref_rate = std::min(in_rate, out_rate);
2516 int ref_rate;
2517
2518 if (min_ref_rate > 32000) {
2519 ref_rate = 48000;
2520 } else if (min_ref_rate > 16000) {
2521 ref_rate = 32000;
2522 } else if (min_ref_rate > 8000) {
2523 ref_rate = 16000;
2524 } else {
2525 ref_rate = 8000;
2526 }
aluebs776593b2016-03-15 14:04:58 -07002527#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002528 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002529 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002530 }
2531#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002532 FILE* out_file = fopen(
2533 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2534 reverse_output_rate_, cf[i].num_input,
2535 cf[i].num_output, cf[i].num_reverse_input,
2536 cf[i].num_reverse_output, file_direction).c_str(),
2537 "rb");
2538 // The reference files always have matching input and output channels.
2539 FILE* ref_file = fopen(
2540 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2541 cf[i].num_output, cf[i].num_output,
2542 cf[i].num_reverse_output, cf[i].num_reverse_output,
2543 file_direction).c_str(),
2544 "rb");
2545 ASSERT_TRUE(out_file != NULL);
2546 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002547
pkasting25702cb2016-01-08 13:50:27 -08002548 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2549 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002550 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002551 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002552 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002553 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002554 // Data from the resampled output, in case the reference and output rates
2555 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002556 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002557
ekmeyerson60d9b332015-08-14 10:35:55 -07002558 PushResampler<float> resampler;
2559 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002560
ekmeyerson60d9b332015-08-14 10:35:55 -07002561 // Compute the resampling delay of the output relative to the reference,
2562 // to find the region over which we should search for the best SNR.
2563 float expected_delay_sec = 0;
2564 if (in_rate != ref_rate) {
2565 // Input resampling delay.
2566 expected_delay_sec +=
2567 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2568 }
2569 if (out_rate != ref_rate) {
2570 // Output resampling delay.
2571 expected_delay_sec +=
2572 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2573 // Delay of converting the output back to its processing rate for
2574 // testing.
2575 expected_delay_sec +=
2576 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2577 }
2578 int expected_delay =
2579 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002580
ekmeyerson60d9b332015-08-14 10:35:55 -07002581 double variance = 0;
2582 double sq_error = 0;
2583 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2584 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2585 float* out_ptr = out_data.get();
2586 if (out_rate != ref_rate) {
2587 // Resample the output back to its internal processing rate if
2588 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002589 ASSERT_EQ(ref_length,
2590 static_cast<size_t>(resampler.Resample(
2591 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002592 out_ptr = cmp_data.get();
2593 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002594
ekmeyerson60d9b332015-08-14 10:35:55 -07002595 // Update the |sq_error| and |variance| accumulators with the highest
2596 // SNR of reference vs output.
2597 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2598 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002599 }
2600
ekmeyerson60d9b332015-08-14 10:35:55 -07002601 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2602 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2603 << cf[i].num_input << ", " << cf[i].num_output << ", "
2604 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2605 << ", " << file_direction << "): ";
2606 if (sq_error > 0) {
2607 double snr = 10 * log10(variance / sq_error);
2608 EXPECT_GE(snr, expected_snr);
2609 EXPECT_NE(0, expected_snr);
2610 std::cout << "SNR=" << snr << " dB" << std::endl;
2611 } else {
aluebs776593b2016-03-15 14:04:58 -07002612 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002613 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002614
ekmeyerson60d9b332015-08-14 10:35:55 -07002615 fclose(out_file);
2616 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002617 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002618 }
2619}
2620
2621#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2622INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002623 CommonFormats,
2624 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002625 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2626 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2627 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2628 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2629 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2630 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2631 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2632 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2633 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2634 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2635 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2636 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002637
Edward Lemurc5ee9872017-10-23 23:33:04 +02002638 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2639 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2640 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2641 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2642 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2643 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2644 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2645 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2646 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2647 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2648 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2649 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002650
Edward Lemurc5ee9872017-10-23 23:33:04 +02002651 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2652 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2653 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2654 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2655 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2656 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2657 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2658 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2659 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2660 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2661 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2662 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002663
Edward Lemurc5ee9872017-10-23 23:33:04 +02002664 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2665 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2666 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2667 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2668 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2669 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2670 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2671 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2672 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2673 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2674 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2675 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002676
2677#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2678INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002679 CommonFormats,
2680 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002681 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2682 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2683 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2684 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2685 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2686 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2687 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2688 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2689 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2690 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2691 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2692 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002693
Edward Lemurc5ee9872017-10-23 23:33:04 +02002694 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2695 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2696 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2697 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2698 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2699 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2700 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2701 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2702 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2703 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2704 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2705 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002706
Edward Lemurc5ee9872017-10-23 23:33:04 +02002707 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2708 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2709 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2710 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2711 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2712 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2713 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2714 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2715 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2716 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2717 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2718 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002719
Edward Lemurc5ee9872017-10-23 23:33:04 +02002720 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2721 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2722 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2723 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2724 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2725 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2726 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2727 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2728 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2729 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2730 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2731 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002732#endif
2733
niklase@google.com470e71d2011-07-07 08:21:25 +00002734} // namespace
peahc19f3122016-10-07 14:54:10 -07002735
Alessio Bazzicac054e782018-04-16 12:10:09 +02002736TEST(RuntimeSettingTest, TestDefaultCtor) {
2737 auto s = AudioProcessing::RuntimeSetting();
2738 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2739}
2740
2741TEST(RuntimeSettingTest, TestCapturePreGain) {
2742 using Type = AudioProcessing::RuntimeSetting::Type;
2743 {
2744 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2745 EXPECT_EQ(Type::kCapturePreGain, s.type());
2746 float v;
2747 s.GetFloat(&v);
2748 EXPECT_EQ(1.25f, v);
2749 }
2750
2751#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2752 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2753#endif
2754}
2755
2756TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2757 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2758 auto s = AudioProcessing::RuntimeSetting();
2759 ASSERT_TRUE(q.Insert(&s));
2760 ASSERT_TRUE(q.Remove(&s));
2761 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2762}
2763
Sam Zackrisson0beac582017-09-25 12:04:02 +02002764TEST(ApmConfiguration, EnablePostProcessing) {
2765 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002766 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002767 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002768 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002769 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002770 rtc::scoped_refptr<AudioProcessing> apm =
2771 AudioProcessingBuilder()
2772 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002773 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002774
2775 AudioFrame audio;
2776 audio.num_channels_ = 1;
2777 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2778
2779 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002780 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002781}
2782
Alex Loiko5825aa62017-12-18 16:02:40 +01002783TEST(ApmConfiguration, EnablePreProcessing) {
2784 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002785 auto mock_pre_processor_ptr =
2786 new testing::NiceMock<test::MockCustomProcessing>();
2787 auto mock_pre_processor =
2788 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002789 rtc::scoped_refptr<AudioProcessing> apm =
2790 AudioProcessingBuilder()
2791 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002792 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002793
2794 AudioFrame audio;
2795 audio.num_channels_ = 1;
2796 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2797
2798 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2799 apm->ProcessReverseStream(&audio);
2800}
2801
Alex Loiko73ec0192018-05-15 10:52:28 +02002802TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2803 auto mock_pre_processor_ptr =
2804 new testing::NiceMock<test::MockCustomProcessing>();
2805 auto mock_pre_processor =
2806 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2807 rtc::scoped_refptr<AudioProcessing> apm =
2808 AudioProcessingBuilder()
2809 .SetRenderPreProcessing(std::move(mock_pre_processor))
2810 .Create();
2811 apm->SetRuntimeSetting(
2812 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2813
2814 // RuntimeSettings forwarded during 'Process*Stream' calls.
2815 // Therefore we have to make one such call.
2816 AudioFrame audio;
2817 audio.num_channels_ = 1;
2818 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2819
2820 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2821 apm->ProcessReverseStream(&audio);
2822}
2823
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002824class MyEchoControlFactory : public EchoControlFactory {
2825 public:
2826 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2827 auto ec = new test::MockEchoControl();
2828 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2829 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2830 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2831 return std::unique_ptr<EchoControl>(ec);
2832 }
2833};
2834
2835TEST(ApmConfiguration, EchoControlInjection) {
2836 // Verify that apm uses an injected echo controller if one is provided.
2837 webrtc::Config webrtc_config;
2838 std::unique_ptr<EchoControlFactory> echo_control_factory(
2839 new MyEchoControlFactory());
2840
Alex Loiko5825aa62017-12-18 16:02:40 +01002841 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002842 AudioProcessingBuilder()
2843 .SetEchoControlFactory(std::move(echo_control_factory))
2844 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002845
2846 AudioFrame audio;
2847 audio.num_channels_ = 1;
2848 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2849 apm->ProcessStream(&audio);
2850 apm->ProcessReverseStream(&audio);
2851 apm->ProcessStream(&audio);
2852}
Ivo Creusenae026092017-11-20 13:07:16 +01002853
2854std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2855 Config old_config;
2856 if (use_AEC2) {
2857 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2858 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2859 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002860 std::unique_ptr<AudioProcessing> apm(
2861 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002862 if (!apm) {
2863 return apm;
2864 }
2865
2866 ProcessingConfig processing_config = {
2867 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2868
2869 if (apm->Initialize(processing_config) != 0) {
2870 return nullptr;
2871 }
2872
2873 // Disable all components except for an AEC and the residual echo detector.
2874 AudioProcessing::Config config;
2875 config.residual_echo_detector.enabled = true;
Ivo Creusenae026092017-11-20 13:07:16 +01002876 config.high_pass_filter.enabled = false;
2877 config.gain_controller2.enabled = false;
Ivo Creusenae026092017-11-20 13:07:16 +01002878 apm->ApplyConfig(config);
2879 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2880 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2881 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2882 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
2883
2884 if (use_AEC2) {
2885 EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
2886 EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
2887 EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
2888 EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
2889 } else {
2890 EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
2891 EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
2892 }
2893 return apm;
2894}
2895
2896#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2897#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2898#else
2899#define MAYBE_ApmStatistics ApmStatistics
2900#endif
2901
2902TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2903 // Set up APM with AEC2 and process some audio.
2904 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2905 ASSERT_TRUE(apm);
2906
2907 // Set up an audioframe.
2908 AudioFrame frame;
2909 frame.num_channels_ = 1;
2910 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2911
2912 // Fill the audio frame with a sawtooth pattern.
2913 int16_t* ptr = frame.mutable_data();
2914 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2915 ptr[i] = 10000 * ((i % 3) - 1);
2916 }
2917
2918 // Do some processing.
2919 for (int i = 0; i < 200; i++) {
2920 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2921 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2922 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2923 }
2924
2925 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002926 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002927 // We expect all statistics to be set and have a sensible value.
2928 ASSERT_TRUE(stats.residual_echo_likelihood);
2929 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2930 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2931 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2932 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2933 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2934 ASSERT_TRUE(stats.echo_return_loss);
2935 EXPECT_NE(*stats.echo_return_loss, -100.0);
2936 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2937 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2938 ASSERT_TRUE(stats.divergent_filter_fraction);
2939 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2940 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2941 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2942 // We don't check stats.delay_median_ms since it takes too long to settle to a
2943 // value. At least 20 seconds of data need to be processed before it will get
2944 // a value, which would make this test take too much time.
2945
2946 // If there are no receive streams, we expect the stats not to be set. The
2947 // 'false' argument signals to APM that no receive streams are currently
2948 // active. In that situation the statistics would get stuck at their last
2949 // calculated value (AEC and echo detection need at least one stream in each
2950 // direction), so to avoid that, they should not be set by APM.
2951 stats = apm->GetStatistics(false);
2952 EXPECT_FALSE(stats.residual_echo_likelihood);
2953 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2954 EXPECT_FALSE(stats.echo_return_loss);
2955 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2956 EXPECT_FALSE(stats.divergent_filter_fraction);
2957 EXPECT_FALSE(stats.delay_median_ms);
2958 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2959}
2960
2961TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2962 // Set up APM with AECM and process some audio.
2963 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2964 ASSERT_TRUE(apm);
2965
2966 // Set up an audioframe.
2967 AudioFrame frame;
2968 frame.num_channels_ = 1;
2969 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2970
2971 // Fill the audio frame with a sawtooth pattern.
2972 int16_t* ptr = frame.mutable_data();
2973 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2974 ptr[i] = 10000 * ((i % 3) - 1);
2975 }
2976
2977 // Do some processing.
2978 for (int i = 0; i < 200; i++) {
2979 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2980 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2981 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2982 }
2983
2984 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002985 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002986 // We expect only the residual echo detector statistics to be set and have a
2987 // sensible value.
2988 EXPECT_TRUE(stats.residual_echo_likelihood);
2989 if (stats.residual_echo_likelihood) {
2990 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2991 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2992 }
2993 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2994 if (stats.residual_echo_likelihood_recent_max) {
2995 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2996 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2997 }
2998 EXPECT_FALSE(stats.echo_return_loss);
2999 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3000 EXPECT_FALSE(stats.divergent_filter_fraction);
3001 EXPECT_FALSE(stats.delay_median_ms);
3002 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3003
3004 // If there are no receive streams, we expect the stats not to be set.
3005 stats = apm->GetStatistics(false);
3006 EXPECT_FALSE(stats.residual_echo_likelihood);
3007 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
3008 EXPECT_FALSE(stats.echo_return_loss);
3009 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3010 EXPECT_FALSE(stats.divergent_filter_fraction);
3011 EXPECT_FALSE(stats.delay_median_ms);
3012 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3013}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003014} // namespace webrtc