blob: 198c66d1d86229a1f5fe79866ec1b8e614f7df2c [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) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200181 AudioProcessing::Config apm_config = ap->GetConfig();
182 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000183#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200184 apm_config.echo_canceller.mobile_mode = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000185
186 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
187 EXPECT_NOERR(ap->gain_control()->Enable(true));
188#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200189 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000190 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
191 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
192 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
Sam Zackrissonc4deaaa2018-08-16 11:42:27 +0200193 EXPECT_NOERR(ap->echo_cancellation()->set_suppression_level(
194 EchoCancellation::SuppressionLevel::kModerateSuppression));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000195
196 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
197 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
198 EXPECT_NOERR(ap->gain_control()->Enable(true));
199#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000200
peah8271d042016-11-22 07:24:52 -0800201 apm_config.high_pass_filter.enabled = true;
202 ap->ApplyConfig(apm_config);
203
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000204 EXPECT_NOERR(ap->level_estimator()->Enable(true));
205 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
206
207 EXPECT_NOERR(ap->voice_detection()->Enable(true));
208}
209
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000210// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000211template <class T>
212T AbsValue(T a) {
213 return a > 0 ? a: -a;
214}
215
216int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800217 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700218 const int16_t* frame_data = frame.data();
219 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800220 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700221 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000222 }
223
224 return max_data;
225}
226
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000227#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000228void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000229 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700230 EXPECT_EQ(reference.instant(), test.instant);
231 EXPECT_EQ(reference.average(), test.average);
232 EXPECT_EQ(reference.maximum(), test.maximum);
233 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234}
235
236void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000237 audioproc::Test::Statistic* msg) {
238 msg->set_instant(output.instant);
239 msg->set_average(output.average);
240 msg->set_maximum(output.maximum);
241 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000243#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244
Alex Loiko890988c2017-08-31 10:25:48 +0200245void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700246 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000248 ASSERT_TRUE(file != NULL);
249
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100250 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000251 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800252 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000253 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000254
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000255 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000256 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000257 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000258 fclose(file);
259}
260
Alex Loiko890988c2017-08-31 10:25:48 +0200261std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000262 std::ostringstream ss;
263 // Resource files are all stereo.
264 ss << name << sample_rate_hz / 1000 << "_stereo";
265 return test::ResourcePath(ss.str(), "pcm");
266}
267
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000268// Temporary filenames unique to this process. Used to be able to run these
269// tests in parallel as each process needs to be running in isolation they can't
270// have competing filenames.
271std::map<std::string, std::string> temp_filenames;
272
Alex Loiko890988c2017-08-31 10:25:48 +0200273std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000274 int input_rate,
275 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700276 int reverse_input_rate,
277 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800278 size_t num_input_channels,
279 size_t num_output_channels,
280 size_t num_reverse_input_channels,
281 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700282 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000283 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700284 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
285 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000286 if (num_output_channels == 1) {
287 ss << "mono";
288 } else if (num_output_channels == 2) {
289 ss << "stereo";
290 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700291 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000292 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700293 ss << output_rate / 1000;
294 if (num_reverse_output_channels == 1) {
295 ss << "_rmono";
296 } else if (num_reverse_output_channels == 2) {
297 ss << "_rstereo";
298 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700299 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700300 }
301 ss << reverse_output_rate / 1000;
302 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000303
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000304 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700305 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000306 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
307 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000308}
309
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000310void ClearTempFiles() {
311 for (auto& kv : temp_filenames)
312 remove(kv.second.c_str());
313}
314
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200315// Only remove "out" files. Keep "ref" files.
316void ClearTempOutFiles() {
317 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
318 const std::string& filename = it->first;
319 if (filename.substr(0, 3).compare("out") == 0) {
320 remove(it->second.c_str());
321 temp_filenames.erase(it++);
322 } else {
323 it++;
324 }
325 }
326}
327
Alex Loiko890988c2017-08-31 10:25:48 +0200328void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000329 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000330 ASSERT_TRUE(file != NULL);
331 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000332 fclose(file);
333}
334
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000335// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
336// stereo) file, converts to deinterleaved float (optionally downmixing) and
337// returns the result in |cb|. Returns false if the file ended (or on error) and
338// true otherwise.
339//
340// |int_data| and |float_data| are just temporary space that must be
341// sufficiently large to hold the 10 ms chunk.
342bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
343 ChannelBuffer<float>* cb) {
344 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000345 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000346 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
347 if (read_count != frame_size) {
348 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700349 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000350 return false; // This is expected.
351 }
352
353 S16ToFloat(int_data, frame_size, float_data);
354 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000355 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000356 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000357 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000358 cb->channels());
359 }
360
361 return true;
362}
363
niklase@google.com470e71d2011-07-07 08:21:25 +0000364class ApmTest : public ::testing::Test {
365 protected:
366 ApmTest();
367 virtual void SetUp();
368 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000369
370 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000371 }
372
373 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000374 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000375 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000376
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000377 // Used to select between int and float interface tests.
378 enum Format {
379 kIntFormat,
380 kFloatFormat
381 };
382
383 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000384 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000385 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800386 size_t num_input_channels,
387 size_t num_output_channels,
388 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000389 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000390 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000391 void EnableAllComponents();
392 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000393 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000394 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000395 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
396 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000397 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000398 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
399 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700400 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800401 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800403 void TestChangingForwardChannels(size_t num_in_channels,
404 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700405 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800406 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700407 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000408 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
409 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000410 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000411 int ProcessStreamChooser(Format format);
412 int AnalyzeReverseStreamChooser(Format format);
413 void ProcessDebugDump(const std::string& in_filename,
414 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800415 Format format,
416 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000417 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000418
419 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000420 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800421 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000422 AudioFrame* frame_;
423 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800424 std::unique_ptr<ChannelBuffer<float> > float_cb_;
425 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000426 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800427 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428 FILE* far_file_;
429 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000430 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000431};
432
433ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000434 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000435#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800436 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
437 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000438#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000439#if defined(WEBRTC_MAC)
440 // A different file for Mac is needed because on this platform the AEC
441 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800442 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
443 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000444#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800445 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
446 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000447#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000448#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000449 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000450 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000451 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000452 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000453 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000454 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000455 out_file_(NULL) {
456 Config config;
457 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100458 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000459}
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
461void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000462 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000463
464 frame_ = new AudioFrame();
465 revframe_ = new AudioFrame();
466
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000467 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468}
469
470void ApmTest::TearDown() {
471 if (frame_) {
472 delete frame_;
473 }
474 frame_ = NULL;
475
476 if (revframe_) {
477 delete revframe_;
478 }
479 revframe_ = NULL;
480
481 if (far_file_) {
482 ASSERT_EQ(0, fclose(far_file_));
483 }
484 far_file_ = NULL;
485
486 if (near_file_) {
487 ASSERT_EQ(0, fclose(near_file_));
488 }
489 near_file_ = NULL;
490
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000491 if (out_file_) {
492 ASSERT_EQ(0, fclose(out_file_));
493 }
494 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000495}
496
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000497void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000498 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700499 ap->Initialize(
500 {{{frame_->sample_rate_hz_, frame_->num_channels_},
501 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700502 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700503 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000504}
505
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000506void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000507 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000508 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800509 size_t num_input_channels,
510 size_t num_output_channels,
511 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000512 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000513 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000514 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000515 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000516
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000517 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
518 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000519 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000520
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000521 if (far_file_) {
522 ASSERT_EQ(0, fclose(far_file_));
523 }
524 std::string filename = ResourceFilePath("far", sample_rate_hz);
525 far_file_ = fopen(filename.c_str(), "rb");
526 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
527 filename << "\n";
528
529 if (near_file_) {
530 ASSERT_EQ(0, fclose(near_file_));
531 }
532 filename = ResourceFilePath("near", sample_rate_hz);
533 near_file_ = fopen(filename.c_str(), "rb");
534 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
535 filename << "\n";
536
537 if (open_output_file) {
538 if (out_file_) {
539 ASSERT_EQ(0, fclose(out_file_));
540 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700541 filename = OutputFilePath(
542 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
543 reverse_sample_rate_hz, num_input_channels, num_output_channels,
544 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000545 out_file_ = fopen(filename.c_str(), "wb");
546 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
547 filename << "\n";
548 }
549}
550
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000551void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000553}
554
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
556 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000557 // The files always contain stereo audio.
558 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700559 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000560 sizeof(int16_t),
561 frame_size,
562 file);
563 if (read_count != frame_size) {
564 // Check that the file really ended.
565 EXPECT_NE(0, feof(file));
566 return false; // This is expected.
567 }
568
569 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700570 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000571 frame->samples_per_channel_);
572 }
573
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000574 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000575 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000577 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000578}
579
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
581 return ReadFrame(file, frame, NULL);
582}
583
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000584// If the end of the file has been reached, rewind it and attempt to read the
585// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
587 ChannelBuffer<float>* cb) {
588 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000589 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000591 }
592}
593
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000594void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
595 ReadFrameWithRewind(file, frame, NULL);
596}
597
andrew@webrtc.org81865342012-10-27 00:28:27 +0000598void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
599 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000600 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000601 EXPECT_EQ(apm_->kNoError,
602 apm_->gain_control()->set_stream_analog_level(127));
603 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000604}
605
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000606int ApmTest::ProcessStreamChooser(Format format) {
607 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000608 return apm_->ProcessStream(frame_);
609 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000610 return apm_->ProcessStream(float_cb_->channels(),
611 frame_->samples_per_channel_,
612 frame_->sample_rate_hz_,
613 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000614 output_sample_rate_hz_,
615 LayoutFromChannels(num_output_channels_),
616 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000617}
618
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000619int ApmTest::AnalyzeReverseStreamChooser(Format format) {
620 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700621 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000622 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000623 return apm_->AnalyzeReverseStream(
624 revfloat_cb_->channels(),
625 revframe_->samples_per_channel_,
626 revframe_->sample_rate_hz_,
627 LayoutFromChannels(revframe_->num_channels_));
628}
629
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000630void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
631 int delay_min, int delay_max) {
632 // The |revframe_| and |frame_| should include the proper frame information,
633 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000634 AudioFrame tmp_frame;
635 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000636 bool causal = true;
637
638 tmp_frame.CopyFrom(*revframe_);
639 SetFrameTo(&tmp_frame, 0);
640
641 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
642 // Initialize the |frame_queue| with empty frames.
643 int frame_delay = delay_ms / 10;
644 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000645 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000646 frame->CopyFrom(tmp_frame);
647 frame_queue.push(frame);
648 frame_delay++;
649 causal = false;
650 }
651 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000652 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000653 frame->CopyFrom(tmp_frame);
654 frame_queue.push(frame);
655 frame_delay--;
656 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000657 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
658 // need enough frames with audio to have reliable estimates, but as few as
659 // possible to keep processing time down. 4.5 seconds seemed to be a good
660 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000661 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000662 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 frame->CopyFrom(tmp_frame);
664 // Use the near end recording, since that has more speech in it.
665 ASSERT_TRUE(ReadFrame(near_file_, frame));
666 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000667 AudioFrame* reverse_frame = frame;
668 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000669 if (!causal) {
670 reverse_frame = frame_queue.front();
671 // When we call ProcessStream() the frame is modified, so we can't use the
672 // pointer directly when things are non-causal. Use an intermediate frame
673 // and copy the data.
674 process_frame = &tmp_frame;
675 process_frame->CopyFrom(*frame);
676 }
aluebsb0319552016-03-17 20:39:53 -0700677 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000678 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
679 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
680 frame = frame_queue.front();
681 frame_queue.pop();
682 delete frame;
683
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000684 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000685 int median;
686 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000687 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000688 // Discard the first delay metrics to avoid convergence effects.
689 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000690 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
691 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000692 }
693 }
694
695 rewind(near_file_);
696 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000697 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000698 frame_queue.pop();
699 delete frame;
700 }
701 // Calculate expected delay estimate and acceptable regions. Further,
702 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700704 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700705 const int expected_median =
706 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
707 const int expected_median_high = 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);
kwiberg07038562017-06-12 11:40:47 -0700710 const int expected_median_low = rtc::SafeClamp<int>(
711 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700712 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000713 // Verify delay metrics.
714 int median;
715 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000716 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000717 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000718 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
719 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000720 EXPECT_GE(expected_median_high, median);
721 EXPECT_LE(expected_median_low, median);
722}
723
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000726 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000730 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000731 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000732
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000733 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 EXPECT_EQ(apm_->kNoError,
735 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000736 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000737 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000738 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000739
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000740 // Other stream parameters set correctly.
741 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000742 EXPECT_EQ(apm_->kNoError,
743 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000744 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000745 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000746 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000747 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000748 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
749 EXPECT_EQ(apm_->kNoError,
750 apm_->echo_cancellation()->enable_drift_compensation(false));
751
752 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000754 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000755 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000756 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000757
758 // Resets after successful ProcessStream().
759 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000760 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000761 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000762 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000763
764 // Other stream parameters set correctly.
765 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
766 EXPECT_EQ(apm_->kNoError,
767 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000768 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000769 EXPECT_EQ(apm_->kNoError,
770 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000771 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000772 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000773 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
774
775 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000776 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000777 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000778
779 // Resets after successful ProcessStream().
780 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000781 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000782 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000783 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000784 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000785
786 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000787 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
788 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
789 EXPECT_EQ(apm_->kNoError,
790 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000791 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000792 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000793
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000794 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000795 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000796 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000797 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000798 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000799
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000800 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000802 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000803 EXPECT_EQ(apm_->kNoError,
804 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000805 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000806}
807
808TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000809 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000810}
811
812TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000813 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000814}
815
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000816TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
817 EXPECT_EQ(0, apm_->delay_offset_ms());
818 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
819 EXPECT_EQ(50, apm_->stream_delay_ms());
820}
821
822TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
823 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000824 apm_->set_delay_offset_ms(100);
825 EXPECT_EQ(100, apm_->delay_offset_ms());
826 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000827 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000828 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
829 EXPECT_EQ(200, apm_->stream_delay_ms());
830
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000831 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000832 apm_->set_delay_offset_ms(-50);
833 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000834 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
835 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000836 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
837 EXPECT_EQ(50, apm_->stream_delay_ms());
838}
839
Michael Graczyk86c6d332015-07-23 11:41:39 -0700840void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800841 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700842 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000843 frame_->num_channels_ = num_channels;
844 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700845 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000846}
847
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800849 size_t num_in_channels,
850 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851 AudioProcessing::Error expected_return) {
852 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
853 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
854
855 EXPECT_EQ(expected_return,
856 apm_->ProcessStream(float_cb_->channels(), input_stream,
857 output_stream, float_cb_->channels()));
858}
859
860void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800861 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700862 AudioProcessing::Error expected_return) {
863 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700864 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
865 {output_sample_rate_hz_, apm_->num_output_channels()},
866 {frame_->sample_rate_hz_, num_rev_channels},
867 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700868
ekmeyerson60d9b332015-08-14 10:35:55 -0700869 EXPECT_EQ(
870 expected_return,
871 apm_->ProcessReverseStream(
872 float_cb_->channels(), processing_config.reverse_input_stream(),
873 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700874}
875
876TEST_F(ApmTest, ChannelsInt16Interface) {
877 // Testing number of invalid and valid channels.
878 Init(16000, 16000, 16000, 4, 4, 4, false);
879
880 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
881
Peter Kasting69558702016-01-12 16:26:35 -0800882 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700883 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 }
886}
887
Michael Graczyk86c6d332015-07-23 11:41:39 -0700888TEST_F(ApmTest, Channels) {
889 // Testing number of invalid and valid channels.
890 Init(16000, 16000, 16000, 4, 4, 4, false);
891
892 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
893 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
894
Peter Kasting69558702016-01-12 16:26:35 -0800895 for (size_t i = 1; i < 4; ++i) {
896 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700897 // Output channels much be one or match input channels.
898 if (j == 1 || i == j) {
899 TestChangingForwardChannels(i, j, kNoErr);
900 TestChangingReverseChannels(i, kNoErr);
901
902 EXPECT_EQ(i, apm_->num_input_channels());
903 EXPECT_EQ(j, apm_->num_output_channels());
904 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800905 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700906 } else {
907 TestChangingForwardChannels(i, j,
908 AudioProcessing::kBadNumberChannelsError);
909 }
910 }
911 }
912}
913
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000914TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000916 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000917 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700919 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800920 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000921 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000922 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000923 }
924}
925
niklase@google.com470e71d2011-07-07 08:21:25 +0000926TEST_F(ApmTest, EchoCancellation) {
927 EXPECT_EQ(apm_->kNoError,
928 apm_->echo_cancellation()->enable_drift_compensation(true));
929 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
930 EXPECT_EQ(apm_->kNoError,
931 apm_->echo_cancellation()->enable_drift_compensation(false));
932 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
933
niklase@google.com470e71d2011-07-07 08:21:25 +0000934 EchoCancellation::SuppressionLevel level[] = {
935 EchoCancellation::kLowSuppression,
936 EchoCancellation::kModerateSuppression,
937 EchoCancellation::kHighSuppression,
938 };
pkasting25702cb2016-01-08 13:50:27 -0800939 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000940 EXPECT_EQ(apm_->kNoError,
941 apm_->echo_cancellation()->set_suppression_level(level[i]));
942 EXPECT_EQ(level[i],
943 apm_->echo_cancellation()->suppression_level());
944 }
945
946 EchoCancellation::Metrics metrics;
947 EXPECT_EQ(apm_->kNotEnabledError,
948 apm_->echo_cancellation()->GetMetrics(&metrics));
949
ivoc3e9a5372016-10-28 07:55:33 -0700950 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
951 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
952
niklase@google.com470e71d2011-07-07 08:21:25 +0000953 EXPECT_EQ(apm_->kNoError,
954 apm_->echo_cancellation()->enable_metrics(true));
955 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
956 EXPECT_EQ(apm_->kNoError,
957 apm_->echo_cancellation()->enable_metrics(false));
958 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
959
ivoc48dfab52016-10-28 03:29:31 -0700960 EXPECT_EQ(apm_->kNoError,
961 apm_->echo_cancellation()->enable_delay_logging(true));
962 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
963 EXPECT_EQ(apm_->kNoError,
964 apm_->echo_cancellation()->enable_delay_logging(false));
965 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
966
ivoc3e9a5372016-10-28 07:55:33 -0700967 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
968 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
969
970 int median = 0;
971 int std = 0;
972 float poor_fraction = 0;
973 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
974 &median, &std, &poor_fraction));
975
niklase@google.com470e71d2011-07-07 08:21:25 +0000976 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
977 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
978 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
979 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000980
981 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
982 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
983 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
984 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
985 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
986 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000987}
988
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000989TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000990 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000991 // Enable AEC only.
992 EXPECT_EQ(apm_->kNoError,
993 apm_->echo_cancellation()->enable_drift_compensation(false));
994 EXPECT_EQ(apm_->kNoError,
995 apm_->echo_cancellation()->enable_metrics(false));
996 EXPECT_EQ(apm_->kNoError,
997 apm_->echo_cancellation()->enable_delay_logging(true));
998 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000999 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -07001000 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +00001001 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001002
1003 // Internally in the AEC the amount of lookahead the delay estimation can
1004 // handle is 15 blocks and the maximum delay is set to 60 blocks.
1005 const int kLookaheadBlocks = 15;
1006 const int kMaxDelayBlocks = 60;
1007 // The AEC has a startup time before it actually starts to process. This
1008 // procedure can flush the internal far-end buffer, which of course affects
1009 // the delay estimation. Therefore, we set a system_delay high enough to
1010 // avoid that. The smallest system_delay you can report without flushing the
1011 // buffer is 66 ms in 8 kHz.
1012 //
1013 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1014 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1015 // delay estimation. This should be noted though. In case of test failure,
1016 // this could be the cause.
1017 const int kSystemDelayMs = 66;
1018 // Test a couple of corner cases and verify that the estimated delay is
1019 // within a valid region (set to +-1.5 blocks). Note that these cases are
1020 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001021 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001022 Init(kProcessSampleRates[i],
1023 kProcessSampleRates[i],
1024 kProcessSampleRates[i],
1025 2,
1026 2,
1027 2,
1028 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001029 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001030 const int num_ms_per_block =
1031 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001032 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1033 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1034
1035 // 1) Verify correct delay estimate at lookahead boundary.
1036 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1037 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1038 delay_max_ms);
1039 // 2) A delay less than maximum lookahead should give an delay estimate at
1040 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1041 delay_ms -= 20;
1042 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1043 delay_max_ms);
1044 // 3) Three values around zero delay. Note that we need to compensate for
1045 // the fake system_delay.
1046 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1047 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1048 delay_max_ms);
1049 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1050 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1051 delay_max_ms);
1052 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1053 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1054 delay_max_ms);
1055 // 4) Verify correct delay estimate at maximum delay boundary.
1056 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1057 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1058 delay_max_ms);
1059 // 5) A delay above the maximum delay should give an estimate at the
1060 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1061 delay_ms += 20;
1062 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1063 delay_max_ms);
1064 }
1065}
1066
niklase@google.com470e71d2011-07-07 08:21:25 +00001067TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001068 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001069 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001070 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1071 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1072
niklase@google.com470e71d2011-07-07 08:21:25 +00001073 // Toggle routing modes
1074 EchoControlMobile::RoutingMode mode[] = {
1075 EchoControlMobile::kQuietEarpieceOrHeadset,
1076 EchoControlMobile::kEarpiece,
1077 EchoControlMobile::kLoudEarpiece,
1078 EchoControlMobile::kSpeakerphone,
1079 EchoControlMobile::kLoudSpeakerphone,
1080 };
pkasting25702cb2016-01-08 13:50:27 -08001081 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001082 EXPECT_EQ(apm_->kNoError,
1083 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1084 EXPECT_EQ(mode[i],
1085 apm_->echo_control_mobile()->routing_mode());
1086 }
1087 // Turn comfort noise off/on
1088 EXPECT_EQ(apm_->kNoError,
1089 apm_->echo_control_mobile()->enable_comfort_noise(false));
1090 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1091 EXPECT_EQ(apm_->kNoError,
1092 apm_->echo_control_mobile()->enable_comfort_noise(true));
1093 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001094 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001095 const size_t echo_path_size =
1096 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001097 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1098 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001099 EXPECT_EQ(apm_->kNullPointerError,
1100 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1101 EXPECT_EQ(apm_->kNullPointerError,
1102 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1103 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001104 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001105 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001106 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001107 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001108 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001109 echo_path_in[i] = echo_path_out[i] + 1;
1110 }
1111 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001112 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
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()->SetEchoPath(echo_path_in.get(),
1115 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001116 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001117 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1118 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001119 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001120 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1121 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001122
1123 // Process a few frames with NS in the default disabled state. This exercises
1124 // a different codepath than with it enabled.
1125 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1126 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1127 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1128 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1129
niklase@google.com470e71d2011-07-07 08:21:25 +00001130 // Turn AECM off
1131 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1132 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1133}
1134
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001135TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001136 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001137 EXPECT_EQ(apm_->kNoError,
1138 apm_->gain_control()->set_mode(
1139 apm_->gain_control()->mode()));
1140
1141 GainControl::Mode mode[] = {
1142 GainControl::kAdaptiveAnalog,
1143 GainControl::kAdaptiveDigital,
1144 GainControl::kFixedDigital
1145 };
pkasting25702cb2016-01-08 13:50:27 -08001146 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001147 EXPECT_EQ(apm_->kNoError,
1148 apm_->gain_control()->set_mode(mode[i]));
1149 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1150 }
1151 // Testing invalid target levels
1152 EXPECT_EQ(apm_->kBadParameterError,
1153 apm_->gain_control()->set_target_level_dbfs(-3));
1154 EXPECT_EQ(apm_->kBadParameterError,
1155 apm_->gain_control()->set_target_level_dbfs(-40));
1156 // Testing valid target levels
1157 EXPECT_EQ(apm_->kNoError,
1158 apm_->gain_control()->set_target_level_dbfs(
1159 apm_->gain_control()->target_level_dbfs()));
1160
1161 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001162 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001163 EXPECT_EQ(apm_->kNoError,
1164 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1165 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1166 }
1167
1168 // Testing invalid compression gains
1169 EXPECT_EQ(apm_->kBadParameterError,
1170 apm_->gain_control()->set_compression_gain_db(-1));
1171 EXPECT_EQ(apm_->kBadParameterError,
1172 apm_->gain_control()->set_compression_gain_db(100));
1173
1174 // Testing valid compression gains
1175 EXPECT_EQ(apm_->kNoError,
1176 apm_->gain_control()->set_compression_gain_db(
1177 apm_->gain_control()->compression_gain_db()));
1178
1179 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001180 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001181 EXPECT_EQ(apm_->kNoError,
1182 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1183 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1184 }
1185
1186 // Testing limiter off/on
1187 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1188 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1189 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1190 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1191
1192 // Testing invalid level limits
1193 EXPECT_EQ(apm_->kBadParameterError,
1194 apm_->gain_control()->set_analog_level_limits(-1, 512));
1195 EXPECT_EQ(apm_->kBadParameterError,
1196 apm_->gain_control()->set_analog_level_limits(100000, 512));
1197 EXPECT_EQ(apm_->kBadParameterError,
1198 apm_->gain_control()->set_analog_level_limits(512, -1));
1199 EXPECT_EQ(apm_->kBadParameterError,
1200 apm_->gain_control()->set_analog_level_limits(512, 100000));
1201 EXPECT_EQ(apm_->kBadParameterError,
1202 apm_->gain_control()->set_analog_level_limits(512, 255));
1203
1204 // Testing valid level limits
1205 EXPECT_EQ(apm_->kNoError,
1206 apm_->gain_control()->set_analog_level_limits(
1207 apm_->gain_control()->analog_level_minimum(),
1208 apm_->gain_control()->analog_level_maximum()));
1209
1210 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001211 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001212 EXPECT_EQ(apm_->kNoError,
1213 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1214 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1215 }
1216
1217 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001218 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001219 EXPECT_EQ(apm_->kNoError,
1220 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1221 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1222 }
1223
1224 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1225
1226 // Turn AGC off
1227 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1228 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1229}
1230
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001231void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001232 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001233 EXPECT_EQ(apm_->kNoError,
1234 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1235 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1236
1237 int out_analog_level = 0;
1238 for (int i = 0; i < 2000; ++i) {
1239 ReadFrameWithRewind(near_file_, frame_);
1240 // Ensure the audio is at a low level, so the AGC will try to increase it.
1241 ScaleFrame(frame_, 0.25);
1242
1243 // Always pass in the same volume.
1244 EXPECT_EQ(apm_->kNoError,
1245 apm_->gain_control()->set_stream_analog_level(100));
1246 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1247 out_analog_level = apm_->gain_control()->stream_analog_level();
1248 }
1249
1250 // Ensure the AGC is still able to reach the maximum.
1251 EXPECT_EQ(255, out_analog_level);
1252}
1253
1254// Verifies that despite volume slider quantization, the AGC can continue to
1255// increase its volume.
1256TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001257 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001258 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1259 }
1260}
1261
1262void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001263 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001264 EXPECT_EQ(apm_->kNoError,
1265 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1266 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1267
1268 int out_analog_level = 100;
1269 for (int i = 0; i < 1000; ++i) {
1270 ReadFrameWithRewind(near_file_, frame_);
1271 // Ensure the audio is at a low level, so the AGC will try to increase it.
1272 ScaleFrame(frame_, 0.25);
1273
1274 EXPECT_EQ(apm_->kNoError,
1275 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1276 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1277 out_analog_level = apm_->gain_control()->stream_analog_level();
1278 }
1279
1280 // Ensure the volume was raised.
1281 EXPECT_GT(out_analog_level, 100);
1282 int highest_level_reached = out_analog_level;
1283 // Simulate a user manual volume change.
1284 out_analog_level = 100;
1285
1286 for (int i = 0; i < 300; ++i) {
1287 ReadFrameWithRewind(near_file_, frame_);
1288 ScaleFrame(frame_, 0.25);
1289
1290 EXPECT_EQ(apm_->kNoError,
1291 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1292 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1293 out_analog_level = apm_->gain_control()->stream_analog_level();
1294 // Check that AGC respected the manually adjusted volume.
1295 EXPECT_LT(out_analog_level, highest_level_reached);
1296 }
1297 // Check that the volume was still raised.
1298 EXPECT_GT(out_analog_level, 100);
1299}
1300
1301TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001302 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001303 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1304 }
1305}
1306
niklase@google.com470e71d2011-07-07 08:21:25 +00001307TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001308 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 NoiseSuppression::Level level[] = {
1310 NoiseSuppression::kLow,
1311 NoiseSuppression::kModerate,
1312 NoiseSuppression::kHigh,
1313 NoiseSuppression::kVeryHigh
1314 };
pkasting25702cb2016-01-08 13:50:27 -08001315 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001316 EXPECT_EQ(apm_->kNoError,
1317 apm_->noise_suppression()->set_level(level[i]));
1318 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1319 }
1320
andrew@webrtc.org648af742012-02-08 01:57:29 +00001321 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001322 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1323 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1324 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1325 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1326}
1327
1328TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001329 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001330 AudioProcessing::Config apm_config;
1331 apm_config.high_pass_filter.enabled = true;
1332 apm_->ApplyConfig(apm_config);
1333 apm_config.high_pass_filter.enabled = false;
1334 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001335}
1336
1337TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001338 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001339 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001340 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001341
1342 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1343
1344 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1345 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1346
1347 // Run this test in wideband; in super-wb, the splitting filter distorts the
1348 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001349 frame_->samples_per_channel_ = 160;
1350 frame_->num_channels_ = 2;
1351 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001352
1353 // Min value if no frames have been processed.
1354 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1355
1356 // Min value on zero frames.
1357 SetFrameTo(frame_, 0);
1358 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1359 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1360 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1361
1362 // Try a few RMS values.
1363 // (These also test that the value resets after retrieving it.)
1364 SetFrameTo(frame_, 32767);
1365 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1366 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1367 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1368
1369 SetFrameTo(frame_, 30000);
1370 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1371 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1372 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1373
1374 SetFrameTo(frame_, 10000);
1375 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1376 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1377 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1378
1379 SetFrameTo(frame_, 10);
1380 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1381 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1382 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1383
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001384 // Verify reset after enable/disable.
1385 SetFrameTo(frame_, 32767);
1386 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1387 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1388 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1389 SetFrameTo(frame_, 1);
1390 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1391 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1392
1393 // Verify reset after initialize.
1394 SetFrameTo(frame_, 32767);
1395 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1396 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1397 SetFrameTo(frame_, 1);
1398 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1399 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001400}
1401
1402TEST_F(ApmTest, VoiceDetection) {
1403 // Test external VAD
1404 EXPECT_EQ(apm_->kNoError,
1405 apm_->voice_detection()->set_stream_has_voice(true));
1406 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1407 EXPECT_EQ(apm_->kNoError,
1408 apm_->voice_detection()->set_stream_has_voice(false));
1409 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1410
andrew@webrtc.org648af742012-02-08 01:57:29 +00001411 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001412 VoiceDetection::Likelihood likelihood[] = {
1413 VoiceDetection::kVeryLowLikelihood,
1414 VoiceDetection::kLowLikelihood,
1415 VoiceDetection::kModerateLikelihood,
1416 VoiceDetection::kHighLikelihood
1417 };
pkasting25702cb2016-01-08 13:50:27 -08001418 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 EXPECT_EQ(apm_->kNoError,
1420 apm_->voice_detection()->set_likelihood(likelihood[i]));
1421 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1422 }
1423
1424 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001425 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001426 EXPECT_EQ(apm_->kBadParameterError,
1427 apm_->voice_detection()->set_frame_size_ms(12));
1428
andrew@webrtc.org648af742012-02-08 01:57:29 +00001429 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001430 for (int i = 10; i <= 30; i += 10) {
1431 EXPECT_EQ(apm_->kNoError,
1432 apm_->voice_detection()->set_frame_size_ms(i));
1433 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1434 }
1435 */
1436
andrew@webrtc.org648af742012-02-08 01:57:29 +00001437 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001438 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1439 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1440 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1441 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1442
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001443 // Test that AudioFrame activity is maintained when VAD is disabled.
1444 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1445 AudioFrame::VADActivity activity[] = {
1446 AudioFrame::kVadActive,
1447 AudioFrame::kVadPassive,
1448 AudioFrame::kVadUnknown
1449 };
pkasting25702cb2016-01-08 13:50:27 -08001450 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001451 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001453 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001454 }
1455
1456 // Test that AudioFrame activity is set when VAD is enabled.
1457 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001458 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001459 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001460 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001461
niklase@google.com470e71d2011-07-07 08:21:25 +00001462 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1463}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001464
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001465TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1466 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1467 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1468 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1469 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1470 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1471 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1472 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1473}
1474
1475TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001476 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001477 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001478 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001479 AudioFrame frame_copy;
1480 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001481 for (int j = 0; j < 1000; j++) {
1482 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1483 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001484 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1485 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001486 }
1487 }
1488}
1489
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001490TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1491 // Test that ProcessStream copies input to output even with no processing.
1492 const size_t kSamples = 80;
1493 const int sample_rate = 8000;
1494 const float src[kSamples] = {
1495 -1.0f, 0.0f, 1.0f
1496 };
1497 float dest[kSamples] = {};
1498
1499 auto src_channels = &src[0];
1500 auto dest_channels = &dest[0];
1501
Ivo Creusen62337e52018-01-09 14:17:33 +01001502 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001503 EXPECT_NOERR(apm_->ProcessStream(
1504 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1505 sample_rate, LayoutFromChannels(1), &dest_channels));
1506
1507 for (size_t i = 0; i < kSamples; ++i) {
1508 EXPECT_EQ(src[i], dest[i]);
1509 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001510
1511 // Same for ProcessReverseStream.
1512 float rev_dest[kSamples] = {};
1513 auto rev_dest_channels = &rev_dest[0];
1514
1515 StreamConfig input_stream = {sample_rate, 1};
1516 StreamConfig output_stream = {sample_rate, 1};
1517 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1518 output_stream, &rev_dest_channels));
1519
1520 for (size_t i = 0; i < kSamples; ++i) {
1521 EXPECT_EQ(src[i], rev_dest[i]);
1522 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001523}
1524
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001525TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1526 EnableAllComponents();
1527
pkasting25702cb2016-01-08 13:50:27 -08001528 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001529 Init(kProcessSampleRates[i],
1530 kProcessSampleRates[i],
1531 kProcessSampleRates[i],
1532 2,
1533 2,
1534 2,
1535 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001536 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001537 ASSERT_EQ(0, feof(far_file_));
1538 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001539 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001540 CopyLeftToRightChannel(revframe_->mutable_data(),
1541 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001542
aluebsb0319552016-03-17 20:39:53 -07001543 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001544
yujo36b1a5f2017-06-12 12:45:32 -07001545 CopyLeftToRightChannel(frame_->mutable_data(),
1546 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001547 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1548
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001549 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001550 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001551 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001552 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001553 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001554 analog_level = apm_->gain_control()->stream_analog_level();
1555
yujo36b1a5f2017-06-12 12:45:32 -07001556 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001557 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001558 rewind(far_file_);
1559 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001560 }
1561}
1562
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001563TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001564 // Verify the filter is not active through undistorted audio when:
1565 // 1. No components are enabled...
1566 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001567 AudioFrame frame_copy;
1568 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001569 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1570 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1571 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1572
1573 // 2. Only the level estimator is enabled...
1574 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001575 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001576 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1577 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1578 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1579 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1580 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1581
1582 // 3. Only VAD is enabled...
1583 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001584 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001585 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1586 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1587 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1588 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1589 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1590
1591 // 4. Both VAD and the level estimator are enabled...
1592 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001593 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001594 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1595 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1596 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1597 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1598 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1599 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1600 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1601
1602 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001603 frame_->samples_per_channel_ = 160;
1604 frame_->num_channels_ = 2;
1605 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001606 // Enable AEC, which would require the filter in super-wb. We rely on the
1607 // first few frames of data being unaffected by the AEC.
1608 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1609 // behavior of the AEC. Think of something more robust.
1610 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001611 // Make sure we have extended filter enabled. This makes sure nothing is
1612 // touched until we have a farend frame.
1613 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001614 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001615 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001616 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001617 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001618 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_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001622 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001623 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1624 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1625
1626 // Check the test is valid. We should have distortion from the filter
1627 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001628 frame_->samples_per_channel_ = 320;
1629 frame_->num_channels_ = 2;
1630 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001631 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001632 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001633 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001634 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001635 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1636 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1637}
1638
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001639#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1640void ApmTest::ProcessDebugDump(const std::string& in_filename,
1641 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001642 Format format,
1643 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001644 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001645 FILE* in_file = fopen(in_filename.c_str(), "rb");
1646 ASSERT_TRUE(in_file != NULL);
1647 audioproc::Event event_msg;
1648 bool first_init = true;
1649
1650 while (ReadMessageFromFile(in_file, &event_msg)) {
1651 if (event_msg.type() == audioproc::Event::INIT) {
1652 const audioproc::Init msg = event_msg.init();
1653 int reverse_sample_rate = msg.sample_rate();
1654 if (msg.has_reverse_sample_rate()) {
1655 reverse_sample_rate = msg.reverse_sample_rate();
1656 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001657 int output_sample_rate = msg.sample_rate();
1658 if (msg.has_output_sample_rate()) {
1659 output_sample_rate = msg.output_sample_rate();
1660 }
1661
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001662 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001663 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001664 reverse_sample_rate,
1665 msg.num_input_channels(),
1666 msg.num_output_channels(),
1667 msg.num_reverse_channels(),
1668 false);
1669 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001670 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001671 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001672 auto aec_dump =
1673 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1674 EXPECT_TRUE(aec_dump);
1675 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001676 first_init = false;
1677 }
1678
1679 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1680 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1681
1682 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001683 ASSERT_EQ(revframe_->num_channels_,
1684 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001685 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001686 memcpy(revfloat_cb_->channels()[i],
1687 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001688 msg.channel(i).size());
1689 }
1690 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001691 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001692 if (format == kFloatFormat) {
1693 // We're using an int16 input file; convert to float.
1694 ConvertToFloat(*revframe_, revfloat_cb_.get());
1695 }
1696 }
1697 AnalyzeReverseStreamChooser(format);
1698
1699 } else if (event_msg.type() == audioproc::Event::STREAM) {
1700 const audioproc::Stream msg = event_msg.stream();
1701 // ProcessStream could have changed this for the output frame.
1702 frame_->num_channels_ = apm_->num_input_channels();
1703
1704 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1705 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1706 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1707 if (msg.has_keypress()) {
1708 apm_->set_stream_key_pressed(msg.keypress());
1709 } else {
1710 apm_->set_stream_key_pressed(true);
1711 }
1712
1713 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001714 ASSERT_EQ(frame_->num_channels_,
1715 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001716 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001717 memcpy(float_cb_->channels()[i],
1718 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001719 msg.input_channel(i).size());
1720 }
1721 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001722 memcpy(frame_->mutable_data(), msg.input_data().data(),
1723 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001724 if (format == kFloatFormat) {
1725 // We're using an int16 input file; convert to float.
1726 ConvertToFloat(*frame_, float_cb_.get());
1727 }
1728 }
1729 ProcessStreamChooser(format);
1730 }
1731 }
aleloif4dd1912017-06-15 01:55:38 -07001732 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001733 fclose(in_file);
1734}
1735
1736void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001737 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001738 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001739 std::string format_string;
1740 switch (format) {
1741 case kIntFormat:
1742 format_string = "_int";
1743 break;
1744 case kFloatFormat:
1745 format_string = "_float";
1746 break;
1747 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001748 const std::string ref_filename = test::TempFilename(
1749 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1750 const std::string out_filename = test::TempFilename(
1751 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001752 const std::string limited_filename = test::TempFilename(
1753 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1754 const size_t logging_limit_bytes = 100000;
1755 // We expect at least this many bytes in the created logfile.
1756 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001757 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001758 ProcessDebugDump(in_filename, ref_filename, format, -1);
1759 ProcessDebugDump(ref_filename, out_filename, format, -1);
1760 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001761
1762 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1763 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001764 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001765 ASSERT_TRUE(ref_file != NULL);
1766 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001767 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001768 std::unique_ptr<uint8_t[]> ref_bytes;
1769 std::unique_ptr<uint8_t[]> out_bytes;
1770 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001771
1772 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1773 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001774 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001775 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001776 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001777 while (ref_size > 0 && out_size > 0) {
1778 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001779 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001780 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001781 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001782 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001783 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001784 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1785 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001786 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001787 }
1788 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001789 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1790 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001791 EXPECT_NE(0, feof(ref_file));
1792 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001793 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001794 ASSERT_EQ(0, fclose(ref_file));
1795 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001796 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001797 remove(ref_filename.c_str());
1798 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001799 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001800}
1801
pbosc7a65692016-05-06 12:50:04 -07001802TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001803 VerifyDebugDumpTest(kIntFormat);
1804}
1805
pbosc7a65692016-05-06 12:50:04 -07001806TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001807 VerifyDebugDumpTest(kFloatFormat);
1808}
1809#endif
1810
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001811// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001812TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001813 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001814 const std::string filename =
1815 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001816 {
1817 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1818 EXPECT_FALSE(aec_dump);
1819 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001820
1821#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1822 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001823 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001824
aleloif4dd1912017-06-15 01:55:38 -07001825 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1826 EXPECT_TRUE(aec_dump);
1827 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001828 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001829 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001830 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001831
1832 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001833 FILE* fid = fopen(filename.c_str(), "r");
1834 ASSERT_TRUE(fid != NULL);
1835
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001836 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001837 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001838 ASSERT_EQ(0, remove(filename.c_str()));
1839#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001840 // Verify the file has NOT been written.
1841 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1842#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1843}
1844
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001845// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001846TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001847 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1848
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001849 const std::string filename =
1850 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001851 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001852 ASSERT_TRUE(fid);
1853
1854#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1855 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001856 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001857
aleloif4dd1912017-06-15 01:55:38 -07001858 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1859 EXPECT_TRUE(aec_dump);
1860 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001861 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001862 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001863 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001864
1865 // Verify the file has been written.
1866 fid = fopen(filename.c_str(), "r");
1867 ASSERT_TRUE(fid != NULL);
1868
1869 // Clean it up.
1870 ASSERT_EQ(0, fclose(fid));
1871 ASSERT_EQ(0, remove(filename.c_str()));
1872#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001873 ASSERT_EQ(0, fclose(fid));
1874#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1875}
1876
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001877TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001878 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001879 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001880
1881 Config config;
1882 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001883 std::unique_ptr<AudioProcessing> fapm(
1884 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001885 EnableAllComponents();
1886 EnableAllAPComponents(fapm.get());
1887 for (int i = 0; i < ref_data.test_size(); i++) {
1888 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1889
1890 audioproc::Test* test = ref_data.mutable_test(i);
1891 // TODO(ajm): Restore downmixing test cases.
1892 if (test->num_input_channels() != test->num_output_channels())
1893 continue;
1894
Peter Kasting69558702016-01-12 16:26:35 -08001895 const size_t num_render_channels =
1896 static_cast<size_t>(test->num_reverse_channels());
1897 const size_t num_input_channels =
1898 static_cast<size_t>(test->num_input_channels());
1899 const size_t num_output_channels =
1900 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001901 const size_t samples_per_channel = static_cast<size_t>(
1902 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001903
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001904 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1905 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001906 Init(fapm.get());
1907
1908 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001909 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1910 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001911
1912 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001913 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001914 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1915 ReadFrame(near_file_, frame_, float_cb_.get())) {
1916 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1917
aluebsb0319552016-03-17 20:39:53 -07001918 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001919 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1920 revfloat_cb_->channels(),
1921 samples_per_channel,
1922 test->sample_rate(),
1923 LayoutFromChannels(num_render_channels)));
1924
1925 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1926 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1927 apm_->echo_cancellation()->set_stream_drift_samples(0);
1928 fapm->echo_cancellation()->set_stream_drift_samples(0);
1929 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1930 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1931
1932 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001933 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001934 output_int16.channels());
1935
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001936 EXPECT_NOERR(fapm->ProcessStream(
1937 float_cb_->channels(),
1938 samples_per_channel,
1939 test->sample_rate(),
1940 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001941 test->sample_rate(),
1942 LayoutFromChannels(num_output_channels),
1943 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001944 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001945 FloatToS16(float_cb_->channels()[j],
1946 samples_per_channel,
1947 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001948 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001949 float snr = ComputeSNR(output_int16.channels()[j],
1950 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001951 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001952
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001953 const float kVarianceThreshold = 20;
1954 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001955
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001956 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001957 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1958 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001959 }
1960 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001961
1962 analog_level = fapm->gain_control()->stream_analog_level();
1963 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1964 fapm->gain_control()->stream_analog_level());
1965 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
1966 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001967 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1968 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001969 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001970
1971 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001972 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001973 }
aluebs776593b2016-03-15 14:04:58 -07001974
1975#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1976 const size_t kMaxNumBadChunks = 0;
1977#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1978 // There are a few chunks in the fixed-point profile that give low SNR.
1979 // Listening confirmed the difference is acceptable.
1980 const size_t kMaxNumBadChunks = 60;
1981#endif
1982 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1983
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001984 rewind(far_file_);
1985 rewind(near_file_);
1986 }
1987}
1988
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001989// TODO(andrew): Add a test to process a few frames with different combinations
1990// of enabled components.
1991
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001992TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001993 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001994 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001995
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001996 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001997 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001998 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001999 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002000 for (size_t i = 0; i < arraysize(kChannels); i++) {
2001 for (size_t j = 0; j < arraysize(kChannels); j++) {
2002 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002003 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002004 test->set_num_reverse_channels(kChannels[i]);
2005 test->set_num_input_channels(kChannels[j]);
2006 test->set_num_output_channels(kChannels[j]);
2007 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002008 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002009 }
2010 }
2011 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002012#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2013 // To test the extended filter mode.
2014 audioproc::Test* test = ref_data.add_test();
2015 test->set_num_reverse_channels(2);
2016 test->set_num_input_channels(2);
2017 test->set_num_output_channels(2);
2018 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2019 test->set_use_aec_extended_filter(true);
2020#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002021 }
2022
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002023 for (int i = 0; i < ref_data.test_size(); i++) {
2024 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002025
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002026 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002027 // TODO(ajm): We no longer allow different input and output channels. Skip
2028 // these tests for now, but they should be removed from the set.
2029 if (test->num_input_channels() != test->num_output_channels())
2030 continue;
2031
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002032 Config config;
2033 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002034 config.Set<ExtendedFilter>(
2035 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01002036 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002037
2038 EnableAllComponents();
2039
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002040 Init(test->sample_rate(),
2041 test->sample_rate(),
2042 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002043 static_cast<size_t>(test->num_input_channels()),
2044 static_cast<size_t>(test->num_output_channels()),
2045 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002046 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002047
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002048 int frame_count = 0;
2049 int has_echo_count = 0;
2050 int has_voice_count = 0;
2051 int is_saturated_count = 0;
2052 int analog_level = 127;
2053 int analog_level_average = 0;
2054 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002055 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002056#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2057 int stats_index = 0;
2058#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002059
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002060 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002061 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002062
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002063 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2064
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002065 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002066 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002067 EXPECT_EQ(apm_->kNoError,
2068 apm_->gain_control()->set_stream_analog_level(analog_level));
2069
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002070 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002071
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002072 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002073 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2074 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002075
2076 max_output_average += MaxAudioFrame(*frame_);
2077
2078 if (apm_->echo_cancellation()->stream_has_echo()) {
2079 has_echo_count++;
2080 }
2081
2082 analog_level = apm_->gain_control()->stream_analog_level();
2083 analog_level_average += analog_level;
2084 if (apm_->gain_control()->stream_is_saturated()) {
2085 is_saturated_count++;
2086 }
2087 if (apm_->voice_detection()->stream_has_voice()) {
2088 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002089 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002090 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002091 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002092 }
2093
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002094 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2095
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002096 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002097 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002098 sizeof(int16_t),
2099 frame_size,
2100 out_file_);
2101 ASSERT_EQ(frame_size, write_count);
2102
2103 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002104 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002105 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002106
2107#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2108 const int kStatsAggregationFrameNum = 100; // 1 second.
2109 if (frame_count % kStatsAggregationFrameNum == 0) {
2110 // Get echo metrics.
2111 EchoCancellation::Metrics echo_metrics;
2112 EXPECT_EQ(apm_->kNoError,
2113 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2114
2115 // Get delay metrics.
2116 int median = 0;
2117 int std = 0;
2118 float fraction_poor_delays = 0;
2119 EXPECT_EQ(apm_->kNoError,
2120 apm_->echo_cancellation()->GetDelayMetrics(
2121 &median, &std, &fraction_poor_delays));
2122
2123 // Get RMS.
2124 int rms_level = apm_->level_estimator()->RMS();
2125 EXPECT_LE(0, rms_level);
2126 EXPECT_GE(127, rms_level);
2127
2128 if (!write_ref_data) {
2129 const audioproc::Test::EchoMetrics& reference =
2130 test->echo_metrics(stats_index);
2131 TestStats(echo_metrics.residual_echo_return_loss,
2132 reference.residual_echo_return_loss());
2133 TestStats(echo_metrics.echo_return_loss,
2134 reference.echo_return_loss());
2135 TestStats(echo_metrics.echo_return_loss_enhancement,
2136 reference.echo_return_loss_enhancement());
2137 TestStats(echo_metrics.a_nlp,
2138 reference.a_nlp());
2139 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2140 reference.divergent_filter_fraction());
2141
2142 const audioproc::Test::DelayMetrics& reference_delay =
2143 test->delay_metrics(stats_index);
2144 EXPECT_EQ(reference_delay.median(), median);
2145 EXPECT_EQ(reference_delay.std(), std);
2146 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2147 fraction_poor_delays);
2148
2149 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2150
2151 ++stats_index;
2152 } else {
2153 audioproc::Test::EchoMetrics* message =
2154 test->add_echo_metrics();
2155 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2156 message->mutable_residual_echo_return_loss());
2157 WriteStatsMessage(echo_metrics.echo_return_loss,
2158 message->mutable_echo_return_loss());
2159 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2160 message->mutable_echo_return_loss_enhancement());
2161 WriteStatsMessage(echo_metrics.a_nlp,
2162 message->mutable_a_nlp());
2163 message->set_divergent_filter_fraction(
2164 echo_metrics.divergent_filter_fraction);
2165
2166 audioproc::Test::DelayMetrics* message_delay =
2167 test->add_delay_metrics();
2168 message_delay->set_median(median);
2169 message_delay->set_std(std);
2170 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2171
2172 test->add_rms_level(rms_level);
2173 }
2174 }
2175#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002176 }
2177 max_output_average /= frame_count;
2178 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002179 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002180
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002181 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002182 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002183 // When running the test on a N7 we get a {2, 6} difference of
2184 // |has_voice_count| and |max_output_average| is up to 18 higher.
2185 // All numbers being consistently higher on N7 compare to ref_data.
2186 // TODO(bjornv): If we start getting more of these offsets on Android we
2187 // should consider a different approach. Either using one slack for all,
2188 // or generate a separate android reference.
2189#if defined(WEBRTC_ANDROID)
2190 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002191 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002192 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002193 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002194#else
2195 const int kHasVoiceCountOffset = 0;
2196 const int kHasVoiceCountNear = kIntNear;
2197 const int kMaxOutputAverageOffset = 0;
2198 const int kMaxOutputAverageNear = kIntNear;
2199#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002200 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002201 EXPECT_NEAR(test->has_voice_count(),
2202 has_voice_count - kHasVoiceCountOffset,
2203 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002204 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002205
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002206 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002207 EXPECT_NEAR(test->max_output_average(),
2208 max_output_average - kMaxOutputAverageOffset,
2209 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002210#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002211 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002212 EXPECT_NEAR(test->ns_speech_probability_average(),
2213 ns_speech_prob_average,
2214 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002215#endif
2216 } else {
2217 test->set_has_echo_count(has_echo_count);
2218 test->set_has_voice_count(has_voice_count);
2219 test->set_is_saturated_count(is_saturated_count);
2220
2221 test->set_analog_level_average(analog_level_average);
2222 test->set_max_output_average(max_output_average);
2223
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002224#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002225 EXPECT_LE(0.0f, ns_speech_prob_average);
2226 EXPECT_GE(1.0f, ns_speech_prob_average);
2227 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002228#endif
2229 }
2230
2231 rewind(far_file_);
2232 rewind(near_file_);
2233 }
2234
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002235 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002236 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002237 }
2238}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002239
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002240TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2241 struct ChannelFormat {
2242 AudioProcessing::ChannelLayout in_layout;
2243 AudioProcessing::ChannelLayout out_layout;
2244 };
2245 ChannelFormat cf[] = {
2246 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2247 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2248 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2249 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002250
Ivo Creusen62337e52018-01-09 14:17:33 +01002251 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002252 // Enable one component just to ensure some processing takes place.
2253 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002254 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002255 const int in_rate = 44100;
2256 const int out_rate = 48000;
2257 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2258 TotalChannelsFromLayout(cf[i].in_layout));
2259 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2260 ChannelsFromLayout(cf[i].out_layout));
2261
2262 // Run over a few chunks.
2263 for (int j = 0; j < 10; ++j) {
2264 EXPECT_NOERR(ap->ProcessStream(
2265 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002266 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002267 in_rate,
2268 cf[i].in_layout,
2269 out_rate,
2270 cf[i].out_layout,
2271 out_cb.channels()));
2272 }
2273 }
2274}
2275
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002276// Compares the reference and test arrays over a region around the expected
2277// delay. Finds the highest SNR in that region and adds the variance and squared
2278// error results to the supplied accumulators.
2279void UpdateBestSNR(const float* ref,
2280 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002281 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002282 int expected_delay,
2283 double* variance_acc,
2284 double* sq_error_acc) {
2285 double best_snr = std::numeric_limits<double>::min();
2286 double best_variance = 0;
2287 double best_sq_error = 0;
2288 // Search over a region of eight samples around the expected delay.
2289 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2290 ++delay) {
2291 double sq_error = 0;
2292 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002293 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002294 double error = test[i + delay] - ref[i];
2295 sq_error += error * error;
2296 variance += ref[i] * ref[i];
2297 }
2298
2299 if (sq_error == 0) {
2300 *variance_acc += variance;
2301 return;
2302 }
2303 double snr = variance / sq_error;
2304 if (snr > best_snr) {
2305 best_snr = snr;
2306 best_variance = variance;
2307 best_sq_error = sq_error;
2308 }
2309 }
2310
2311 *variance_acc += best_variance;
2312 *sq_error_acc += best_sq_error;
2313}
2314
2315// Used to test a multitude of sample rate and channel combinations. It works
2316// by first producing a set of reference files (in SetUpTestCase) that are
2317// assumed to be correct, as the used parameters are verified by other tests
2318// in this collection. Primarily the reference files are all produced at
2319// "native" rates which do not involve any resampling.
2320
2321// Each test pass produces an output file with a particular format. The output
2322// is matched against the reference file closest to its internal processing
2323// format. If necessary the output is resampled back to its process format.
2324// Due to the resampling distortion, we don't expect identical results, but
2325// enforce SNR thresholds which vary depending on the format. 0 is a special
2326// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002327typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002328class AudioProcessingTest
2329 : public testing::TestWithParam<AudioProcessingTestData> {
2330 public:
2331 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002332 : input_rate_(std::get<0>(GetParam())),
2333 output_rate_(std::get<1>(GetParam())),
2334 reverse_input_rate_(std::get<2>(GetParam())),
2335 reverse_output_rate_(std::get<3>(GetParam())),
2336 expected_snr_(std::get<4>(GetParam())),
2337 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002338
2339 virtual ~AudioProcessingTest() {}
2340
2341 static void SetUpTestCase() {
2342 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002343 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002344 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002345 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2346 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2347 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002348 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002349 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2350 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2351 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002352 }
2353 }
2354 }
2355 }
2356
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002357 void TearDown() {
2358 // Remove "out" files after each test.
2359 ClearTempOutFiles();
2360 }
2361
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002362 static void TearDownTestCase() {
2363 ClearTempFiles();
2364 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002365
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002366 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002367 // to a file specified with |output_file_prefix|. Both forward and reverse
2368 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002369 static void ProcessFormat(int input_rate,
2370 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002371 int reverse_input_rate,
2372 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002373 size_t num_input_channels,
2374 size_t num_output_channels,
2375 size_t num_reverse_input_channels,
2376 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002377 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002378 Config config;
2379 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002380 std::unique_ptr<AudioProcessing> ap(
2381 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002382 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002383
ekmeyerson60d9b332015-08-14 10:35:55 -07002384 ProcessingConfig processing_config = {
2385 {{input_rate, num_input_channels},
2386 {output_rate, num_output_channels},
2387 {reverse_input_rate, num_reverse_input_channels},
2388 {reverse_output_rate, num_reverse_output_channels}}};
2389 ap->Initialize(processing_config);
2390
2391 FILE* far_file =
2392 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002393 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002394 FILE* out_file =
2395 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2396 reverse_input_rate, reverse_output_rate,
2397 num_input_channels, num_output_channels,
2398 num_reverse_input_channels,
2399 num_reverse_output_channels, kForward).c_str(),
2400 "wb");
2401 FILE* rev_out_file =
2402 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2403 reverse_input_rate, reverse_output_rate,
2404 num_input_channels, num_output_channels,
2405 num_reverse_input_channels,
2406 num_reverse_output_channels, kReverse).c_str(),
2407 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002408 ASSERT_TRUE(far_file != NULL);
2409 ASSERT_TRUE(near_file != NULL);
2410 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002411 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002412
2413 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2414 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002415 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2416 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002417 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2418 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002419 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2420 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002421
2422 // Temporary buffers.
2423 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002424 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2425 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002426 std::unique_ptr<float[]> float_data(new float[max_length]);
2427 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002428
2429 int analog_level = 127;
2430 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2431 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002432 EXPECT_NOERR(ap->ProcessReverseStream(
2433 rev_cb.channels(), processing_config.reverse_input_stream(),
2434 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002435
2436 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2437 ap->echo_cancellation()->set_stream_drift_samples(0);
2438 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2439
2440 EXPECT_NOERR(ap->ProcessStream(
2441 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002442 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002443 input_rate,
2444 LayoutFromChannels(num_input_channels),
2445 output_rate,
2446 LayoutFromChannels(num_output_channels),
2447 out_cb.channels()));
2448
ekmeyerson60d9b332015-08-14 10:35:55 -07002449 // Dump forward output to file.
2450 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002451 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002452 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002453
pkasting25702cb2016-01-08 13:50:27 -08002454 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002455 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002456 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002457
ekmeyerson60d9b332015-08-14 10:35:55 -07002458 // Dump reverse output to file.
2459 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2460 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002461 size_t rev_out_length =
2462 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002463
pkasting25702cb2016-01-08 13:50:27 -08002464 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002465 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2466 rev_out_file));
2467
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002468 analog_level = ap->gain_control()->stream_analog_level();
2469 }
2470 fclose(far_file);
2471 fclose(near_file);
2472 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002473 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002474 }
2475
2476 protected:
2477 int input_rate_;
2478 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002479 int reverse_input_rate_;
2480 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002481 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002482 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002483};
2484
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002485TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002486 struct ChannelFormat {
2487 int num_input;
2488 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002489 int num_reverse_input;
2490 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002491 };
2492 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002493 {1, 1, 1, 1},
2494 {1, 1, 2, 1},
2495 {2, 1, 1, 1},
2496 {2, 1, 2, 1},
2497 {2, 2, 1, 1},
2498 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002499 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002500
pkasting25702cb2016-01-08 13:50:27 -08002501 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002502 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2503 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2504 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002505
ekmeyerson60d9b332015-08-14 10:35:55 -07002506 // Verify output for both directions.
2507 std::vector<StreamDirection> stream_directions;
2508 stream_directions.push_back(kForward);
2509 stream_directions.push_back(kReverse);
2510 for (StreamDirection file_direction : stream_directions) {
2511 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2512 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2513 const int out_num =
2514 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2515 const double expected_snr =
2516 file_direction ? expected_reverse_snr_ : expected_snr_;
2517
2518 const int min_ref_rate = std::min(in_rate, out_rate);
2519 int ref_rate;
2520
2521 if (min_ref_rate > 32000) {
2522 ref_rate = 48000;
2523 } else if (min_ref_rate > 16000) {
2524 ref_rate = 32000;
2525 } else if (min_ref_rate > 8000) {
2526 ref_rate = 16000;
2527 } else {
2528 ref_rate = 8000;
2529 }
aluebs776593b2016-03-15 14:04:58 -07002530#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002531 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002532 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002533 }
2534#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002535 FILE* out_file = fopen(
2536 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2537 reverse_output_rate_, cf[i].num_input,
2538 cf[i].num_output, cf[i].num_reverse_input,
2539 cf[i].num_reverse_output, file_direction).c_str(),
2540 "rb");
2541 // The reference files always have matching input and output channels.
2542 FILE* ref_file = fopen(
2543 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2544 cf[i].num_output, cf[i].num_output,
2545 cf[i].num_reverse_output, cf[i].num_reverse_output,
2546 file_direction).c_str(),
2547 "rb");
2548 ASSERT_TRUE(out_file != NULL);
2549 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002550
pkasting25702cb2016-01-08 13:50:27 -08002551 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2552 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002553 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002554 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002555 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002556 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002557 // Data from the resampled output, in case the reference and output rates
2558 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002559 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002560
ekmeyerson60d9b332015-08-14 10:35:55 -07002561 PushResampler<float> resampler;
2562 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002563
ekmeyerson60d9b332015-08-14 10:35:55 -07002564 // Compute the resampling delay of the output relative to the reference,
2565 // to find the region over which we should search for the best SNR.
2566 float expected_delay_sec = 0;
2567 if (in_rate != ref_rate) {
2568 // Input resampling delay.
2569 expected_delay_sec +=
2570 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2571 }
2572 if (out_rate != ref_rate) {
2573 // Output resampling delay.
2574 expected_delay_sec +=
2575 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2576 // Delay of converting the output back to its processing rate for
2577 // testing.
2578 expected_delay_sec +=
2579 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2580 }
2581 int expected_delay =
2582 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002583
ekmeyerson60d9b332015-08-14 10:35:55 -07002584 double variance = 0;
2585 double sq_error = 0;
2586 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2587 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2588 float* out_ptr = out_data.get();
2589 if (out_rate != ref_rate) {
2590 // Resample the output back to its internal processing rate if
2591 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002592 ASSERT_EQ(ref_length,
2593 static_cast<size_t>(resampler.Resample(
2594 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002595 out_ptr = cmp_data.get();
2596 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002597
ekmeyerson60d9b332015-08-14 10:35:55 -07002598 // Update the |sq_error| and |variance| accumulators with the highest
2599 // SNR of reference vs output.
2600 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2601 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002602 }
2603
ekmeyerson60d9b332015-08-14 10:35:55 -07002604 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2605 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2606 << cf[i].num_input << ", " << cf[i].num_output << ", "
2607 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2608 << ", " << file_direction << "): ";
2609 if (sq_error > 0) {
2610 double snr = 10 * log10(variance / sq_error);
2611 EXPECT_GE(snr, expected_snr);
2612 EXPECT_NE(0, expected_snr);
2613 std::cout << "SNR=" << snr << " dB" << std::endl;
2614 } else {
aluebs776593b2016-03-15 14:04:58 -07002615 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002616 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002617
ekmeyerson60d9b332015-08-14 10:35:55 -07002618 fclose(out_file);
2619 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002620 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002621 }
2622}
2623
2624#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2625INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002626 CommonFormats,
2627 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002628 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2629 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2630 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2631 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2632 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2633 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2634 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2635 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2636 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2637 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2638 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2639 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002640
Edward Lemurc5ee9872017-10-23 23:33:04 +02002641 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2642 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2643 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2644 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2645 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2646 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2647 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2648 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2649 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2650 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2651 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2652 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002653
Edward Lemurc5ee9872017-10-23 23:33:04 +02002654 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2655 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2656 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2657 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2658 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2659 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2660 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2661 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2662 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2663 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2664 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2665 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002666
Edward Lemurc5ee9872017-10-23 23:33:04 +02002667 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2668 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2669 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2670 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2671 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2672 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2673 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2674 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2675 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2676 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2677 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2678 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002679
2680#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2681INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002682 CommonFormats,
2683 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002684 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2685 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2686 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2687 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2688 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2689 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2690 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2691 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2692 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2693 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2694 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2695 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002696
Edward Lemurc5ee9872017-10-23 23:33:04 +02002697 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2698 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2699 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2700 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2701 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2702 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2703 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2704 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2705 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2706 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2707 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2708 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002709
Edward Lemurc5ee9872017-10-23 23:33:04 +02002710 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2711 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2712 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2713 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2714 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2715 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2716 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2717 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2718 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2719 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2720 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2721 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002722
Edward Lemurc5ee9872017-10-23 23:33:04 +02002723 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2724 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2725 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2726 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2727 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2728 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2729 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2730 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2731 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2732 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2733 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2734 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002735#endif
2736
niklase@google.com470e71d2011-07-07 08:21:25 +00002737} // namespace
peahc19f3122016-10-07 14:54:10 -07002738
Alessio Bazzicac054e782018-04-16 12:10:09 +02002739TEST(RuntimeSettingTest, TestDefaultCtor) {
2740 auto s = AudioProcessing::RuntimeSetting();
2741 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2742}
2743
2744TEST(RuntimeSettingTest, TestCapturePreGain) {
2745 using Type = AudioProcessing::RuntimeSetting::Type;
2746 {
2747 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2748 EXPECT_EQ(Type::kCapturePreGain, s.type());
2749 float v;
2750 s.GetFloat(&v);
2751 EXPECT_EQ(1.25f, v);
2752 }
2753
2754#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2755 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2756#endif
2757}
2758
2759TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2760 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2761 auto s = AudioProcessing::RuntimeSetting();
2762 ASSERT_TRUE(q.Insert(&s));
2763 ASSERT_TRUE(q.Remove(&s));
2764 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2765}
2766
Sam Zackrisson0beac582017-09-25 12:04:02 +02002767TEST(ApmConfiguration, EnablePostProcessing) {
2768 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002769 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002770 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002771 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002772 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002773 rtc::scoped_refptr<AudioProcessing> apm =
2774 AudioProcessingBuilder()
2775 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002776 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002777
2778 AudioFrame audio;
2779 audio.num_channels_ = 1;
2780 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2781
2782 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002783 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002784}
2785
Alex Loiko5825aa62017-12-18 16:02:40 +01002786TEST(ApmConfiguration, EnablePreProcessing) {
2787 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002788 auto mock_pre_processor_ptr =
2789 new testing::NiceMock<test::MockCustomProcessing>();
2790 auto mock_pre_processor =
2791 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002792 rtc::scoped_refptr<AudioProcessing> apm =
2793 AudioProcessingBuilder()
2794 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002795 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002796
2797 AudioFrame audio;
2798 audio.num_channels_ = 1;
2799 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2800
2801 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2802 apm->ProcessReverseStream(&audio);
2803}
2804
Alex Loiko73ec0192018-05-15 10:52:28 +02002805TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2806 auto mock_pre_processor_ptr =
2807 new testing::NiceMock<test::MockCustomProcessing>();
2808 auto mock_pre_processor =
2809 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2810 rtc::scoped_refptr<AudioProcessing> apm =
2811 AudioProcessingBuilder()
2812 .SetRenderPreProcessing(std::move(mock_pre_processor))
2813 .Create();
2814 apm->SetRuntimeSetting(
2815 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2816
2817 // RuntimeSettings forwarded during 'Process*Stream' calls.
2818 // Therefore we have to make one such call.
2819 AudioFrame audio;
2820 audio.num_channels_ = 1;
2821 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2822
2823 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2824 apm->ProcessReverseStream(&audio);
2825}
2826
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002827class MyEchoControlFactory : public EchoControlFactory {
2828 public:
2829 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2830 auto ec = new test::MockEchoControl();
2831 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2832 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2833 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2834 return std::unique_ptr<EchoControl>(ec);
2835 }
2836};
2837
2838TEST(ApmConfiguration, EchoControlInjection) {
2839 // Verify that apm uses an injected echo controller if one is provided.
2840 webrtc::Config webrtc_config;
2841 std::unique_ptr<EchoControlFactory> echo_control_factory(
2842 new MyEchoControlFactory());
2843
Alex Loiko5825aa62017-12-18 16:02:40 +01002844 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002845 AudioProcessingBuilder()
2846 .SetEchoControlFactory(std::move(echo_control_factory))
2847 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002848
2849 AudioFrame audio;
2850 audio.num_channels_ = 1;
2851 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2852 apm->ProcessStream(&audio);
2853 apm->ProcessReverseStream(&audio);
2854 apm->ProcessStream(&audio);
2855}
Ivo Creusenae026092017-11-20 13:07:16 +01002856
2857std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2858 Config old_config;
2859 if (use_AEC2) {
2860 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2861 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2862 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002863 std::unique_ptr<AudioProcessing> apm(
2864 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002865 if (!apm) {
2866 return apm;
2867 }
2868
2869 ProcessingConfig processing_config = {
2870 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2871
2872 if (apm->Initialize(processing_config) != 0) {
2873 return nullptr;
2874 }
2875
2876 // Disable all components except for an AEC and the residual echo detector.
2877 AudioProcessing::Config config;
2878 config.residual_echo_detector.enabled = true;
Ivo Creusenae026092017-11-20 13:07:16 +01002879 config.high_pass_filter.enabled = false;
2880 config.gain_controller2.enabled = false;
Ivo Creusenae026092017-11-20 13:07:16 +01002881 apm->ApplyConfig(config);
2882 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2883 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2884 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2885 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
2886
2887 if (use_AEC2) {
2888 EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
2889 EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
2890 EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
2891 EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
2892 } else {
2893 EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
2894 EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
2895 }
2896 return apm;
2897}
2898
2899#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2900#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2901#else
2902#define MAYBE_ApmStatistics ApmStatistics
2903#endif
2904
2905TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2906 // Set up APM with AEC2 and process some audio.
2907 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2908 ASSERT_TRUE(apm);
2909
2910 // Set up an audioframe.
2911 AudioFrame frame;
2912 frame.num_channels_ = 1;
2913 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2914
2915 // Fill the audio frame with a sawtooth pattern.
2916 int16_t* ptr = frame.mutable_data();
2917 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2918 ptr[i] = 10000 * ((i % 3) - 1);
2919 }
2920
2921 // Do some processing.
2922 for (int i = 0; i < 200; i++) {
2923 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2924 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2925 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2926 }
2927
2928 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002929 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002930 // We expect all statistics to be set and have a sensible value.
2931 ASSERT_TRUE(stats.residual_echo_likelihood);
2932 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2933 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2934 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2935 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2936 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2937 ASSERT_TRUE(stats.echo_return_loss);
2938 EXPECT_NE(*stats.echo_return_loss, -100.0);
2939 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2940 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2941 ASSERT_TRUE(stats.divergent_filter_fraction);
2942 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2943 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2944 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2945 // We don't check stats.delay_median_ms since it takes too long to settle to a
2946 // value. At least 20 seconds of data need to be processed before it will get
2947 // a value, which would make this test take too much time.
2948
2949 // If there are no receive streams, we expect the stats not to be set. The
2950 // 'false' argument signals to APM that no receive streams are currently
2951 // active. In that situation the statistics would get stuck at their last
2952 // calculated value (AEC and echo detection need at least one stream in each
2953 // direction), so to avoid that, they should not be set by APM.
2954 stats = apm->GetStatistics(false);
2955 EXPECT_FALSE(stats.residual_echo_likelihood);
2956 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2957 EXPECT_FALSE(stats.echo_return_loss);
2958 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2959 EXPECT_FALSE(stats.divergent_filter_fraction);
2960 EXPECT_FALSE(stats.delay_median_ms);
2961 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2962}
2963
2964TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2965 // Set up APM with AECM and process some audio.
2966 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2967 ASSERT_TRUE(apm);
2968
2969 // Set up an audioframe.
2970 AudioFrame frame;
2971 frame.num_channels_ = 1;
2972 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2973
2974 // Fill the audio frame with a sawtooth pattern.
2975 int16_t* ptr = frame.mutable_data();
2976 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2977 ptr[i] = 10000 * ((i % 3) - 1);
2978 }
2979
2980 // Do some processing.
2981 for (int i = 0; i < 200; i++) {
2982 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2983 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2984 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2985 }
2986
2987 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002988 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002989 // We expect only the residual echo detector statistics to be set and have a
2990 // sensible value.
2991 EXPECT_TRUE(stats.residual_echo_likelihood);
2992 if (stats.residual_echo_likelihood) {
2993 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2994 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2995 }
2996 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2997 if (stats.residual_echo_likelihood_recent_max) {
2998 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2999 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
3000 }
3001 EXPECT_FALSE(stats.echo_return_loss);
3002 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3003 EXPECT_FALSE(stats.divergent_filter_fraction);
3004 EXPECT_FALSE(stats.delay_median_ms);
3005 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3006
3007 // If there are no receive streams, we expect the stats not to be set.
3008 stats = apm->GetStatistics(false);
3009 EXPECT_FALSE(stats.residual_echo_likelihood);
3010 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
3011 EXPECT_FALSE(stats.echo_return_loss);
3012 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3013 EXPECT_FALSE(stats.divergent_filter_fraction);
3014 EXPECT_FALSE(stats.delay_median_ms);
3015 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3016}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003017} // namespace webrtc