blob: b96c3ecbbee5a87bb150f9c3c5887a66375f28ec [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/common.h"
25#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020026#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/test/protobuf_utils.h"
28#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/arraysize.h"
30#include "rtc_base/checks.h"
Minyue Li656d6092018-08-10 15:38:52 +020031#include "rtc_base/fakeclock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/gtest_prod_util.h"
33#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Niels Möller84255bb2017-10-06 13:43:23 +020037#include "rtc_base/refcountedobject.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020038#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020039#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020040#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/task_queue.h"
42#include "rtc_base/thread.h"
43#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gtest.h"
45#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070046
47RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000049#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#endif
kwiberg77eab702016-09-28 17:42:01 -070053RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000054
andrew@webrtc.org27c69802014-02-18 20:24:56 +000055namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000056namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000057
ekmeyerson60d9b332015-08-14 10:35:55 -070058// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
59// applicable.
60
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000061// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000062// re-implemented; see comment at the bottom of this file. For now, the user has
63// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000064// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000065// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000066// be set to true with the command-line switch --write_ref_data.
67bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070068const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070069const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000070
aluebseb3603b2016-04-20 15:27:58 -070071#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
72// Android doesn't support 48kHz.
73const int kProcessSampleRates[] = {8000, 16000, 32000};
74#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070075const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070076#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000077
ekmeyerson60d9b332015-08-14 10:35:55 -070078enum StreamDirection { kForward = 0, kReverse };
79
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082 cb->num_channels());
83 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000085 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080087 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 S16ToFloat(cb_int.channels()[i],
89 cb->num_frames(),
90 cb->channels()[i]);
91 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000092}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000093
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070095 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000096}
97
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080099size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000100 switch (layout) {
101 case AudioProcessing::kMono:
102 return 1;
103 case AudioProcessing::kMonoAndKeyboard:
104 case AudioProcessing::kStereo:
105 return 2;
106 case AudioProcessing::kStereoAndKeyboard:
107 return 3;
108 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700109 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800110 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000111}
112
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000113int TruncateToMultipleOf10(int value) {
114 return (value / 10) * 10;
115}
116
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000121}
122
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800124 size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000126 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
127}
128
pkasting25702cb2016-01-08 13:50:27 -0800129void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 stereo[i * 2 + 1] = stereo[i * 2];
132 }
133}
134
yujo36b1a5f2017-06-12 12:45:32 -0700135void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800136 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000137 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
138 }
139}
140
141void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700142 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
144 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700145 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000146 }
147}
148
149void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800150 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700151 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 frame_data[i] = left;
154 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000155 }
156}
157
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700159 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
161 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700162 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000163 }
164}
165
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 return false;
172 }
yujo36b1a5f2017-06-12 12:45:32 -0700173 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000175 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000176 return false;
177 }
178 return true;
179}
180
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000181void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200182 AudioProcessing::Config apm_config = ap->GetConfig();
183 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200185 apm_config.echo_canceller.mobile_mode = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186
187 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
188 EXPECT_NOERR(ap->gain_control()->Enable(true));
189#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200190 apm_config.echo_canceller.mobile_mode = false;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000191 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
192 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
193 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
Sam Zackrissonc4deaaa2018-08-16 11:42:27 +0200194 EXPECT_NOERR(ap->echo_cancellation()->set_suppression_level(
195 EchoCancellation::SuppressionLevel::kModerateSuppression));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000196
197 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
198 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
199 EXPECT_NOERR(ap->gain_control()->Enable(true));
200#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000201
peah8271d042016-11-22 07:24:52 -0800202 apm_config.high_pass_filter.enabled = true;
203 ap->ApplyConfig(apm_config);
204
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000205 EXPECT_NOERR(ap->level_estimator()->Enable(true));
206 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
207
208 EXPECT_NOERR(ap->voice_detection()->Enable(true));
209}
210
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000211// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000212template <class T>
213T AbsValue(T a) {
214 return a > 0 ? a: -a;
215}
216
217int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800218 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700219 const int16_t* frame_data = frame.data();
220 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800221 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700222 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000223 }
224
225 return max_data;
226}
227
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000228#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000230 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700231 EXPECT_EQ(reference.instant(), test.instant);
232 EXPECT_EQ(reference.average(), test.average);
233 EXPECT_EQ(reference.maximum(), test.maximum);
234 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235}
236
237void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000238 audioproc::Test::Statistic* msg) {
239 msg->set_instant(output.instant);
240 msg->set_average(output.average);
241 msg->set_maximum(output.maximum);
242 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000244#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000245
Alex Loiko890988c2017-08-31 10:25:48 +0200246void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700247 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000248 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000249 ASSERT_TRUE(file != NULL);
250
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100251 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000252 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800253 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000254 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000255
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000256 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000257 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000258 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000259 fclose(file);
260}
261
Alex Loiko890988c2017-08-31 10:25:48 +0200262std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200263 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000264 // Resource files are all stereo.
265 ss << name << sample_rate_hz / 1000 << "_stereo";
266 return test::ResourcePath(ss.str(), "pcm");
267}
268
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000269// Temporary filenames unique to this process. Used to be able to run these
270// tests in parallel as each process needs to be running in isolation they can't
271// have competing filenames.
272std::map<std::string, std::string> temp_filenames;
273
Alex Loiko890988c2017-08-31 10:25:48 +0200274std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000275 int input_rate,
276 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700277 int reverse_input_rate,
278 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800279 size_t num_input_channels,
280 size_t num_output_channels,
281 size_t num_reverse_input_channels,
282 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700283 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200284 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700285 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
286 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000287 if (num_output_channels == 1) {
288 ss << "mono";
289 } else if (num_output_channels == 2) {
290 ss << "stereo";
291 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700292 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000293 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700294 ss << output_rate / 1000;
295 if (num_reverse_output_channels == 1) {
296 ss << "_rmono";
297 } else if (num_reverse_output_channels == 2) {
298 ss << "_rstereo";
299 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700300 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700301 }
302 ss << reverse_output_rate / 1000;
303 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000304
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000305 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700306 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000307 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
308 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000309}
310
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000311void ClearTempFiles() {
312 for (auto& kv : temp_filenames)
313 remove(kv.second.c_str());
314}
315
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200316// Only remove "out" files. Keep "ref" files.
317void ClearTempOutFiles() {
318 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
319 const std::string& filename = it->first;
320 if (filename.substr(0, 3).compare("out") == 0) {
321 remove(it->second.c_str());
322 temp_filenames.erase(it++);
323 } else {
324 it++;
325 }
326 }
327}
328
Alex Loiko890988c2017-08-31 10:25:48 +0200329void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000330 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000331 ASSERT_TRUE(file != NULL);
332 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000333 fclose(file);
334}
335
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000336// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
337// stereo) file, converts to deinterleaved float (optionally downmixing) and
338// returns the result in |cb|. Returns false if the file ended (or on error) and
339// true otherwise.
340//
341// |int_data| and |float_data| are just temporary space that must be
342// sufficiently large to hold the 10 ms chunk.
343bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
344 ChannelBuffer<float>* cb) {
345 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000346 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000347 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
348 if (read_count != frame_size) {
349 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700350 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000351 return false; // This is expected.
352 }
353
354 S16ToFloat(int_data, frame_size, float_data);
355 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000356 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000357 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000358 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000359 cb->channels());
360 }
361
362 return true;
363}
364
niklase@google.com470e71d2011-07-07 08:21:25 +0000365class ApmTest : public ::testing::Test {
366 protected:
367 ApmTest();
368 virtual void SetUp();
369 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000370
371 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000372 }
373
374 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000375 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000376 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000377
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000378 // Used to select between int and float interface tests.
379 enum Format {
380 kIntFormat,
381 kFloatFormat
382 };
383
384 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000385 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000386 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800387 size_t num_input_channels,
388 size_t num_output_channels,
389 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000390 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000391 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000392 void EnableAllComponents();
393 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000394 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000395 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000396 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
397 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000398 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000399 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
400 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700401 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800402 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700403 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800404 void TestChangingForwardChannels(size_t num_in_channels,
405 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700406 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800407 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700408 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000409 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
410 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000411 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000412 int ProcessStreamChooser(Format format);
413 int AnalyzeReverseStreamChooser(Format format);
414 void ProcessDebugDump(const std::string& in_filename,
415 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800416 Format format,
417 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000418 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000419
420 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000421 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800422 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000423 AudioFrame* frame_;
424 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800425 std::unique_ptr<ChannelBuffer<float> > float_cb_;
426 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000427 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800428 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429 FILE* far_file_;
430 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000431 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000432};
433
434ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000435 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000436#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800437 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
438 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000439#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000440#if defined(WEBRTC_MAC)
441 // A different file for Mac is needed because on this platform the AEC
442 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800443 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
444 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000445#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800446 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
447 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000448#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000449#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000451 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000452 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000453 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000454 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000455 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000456 out_file_(NULL) {
457 Config config;
458 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100459 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000460}
niklase@google.com470e71d2011-07-07 08:21:25 +0000461
462void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000463 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000464
465 frame_ = new AudioFrame();
466 revframe_ = new AudioFrame();
467
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000468 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000469}
470
471void ApmTest::TearDown() {
472 if (frame_) {
473 delete frame_;
474 }
475 frame_ = NULL;
476
477 if (revframe_) {
478 delete revframe_;
479 }
480 revframe_ = NULL;
481
482 if (far_file_) {
483 ASSERT_EQ(0, fclose(far_file_));
484 }
485 far_file_ = NULL;
486
487 if (near_file_) {
488 ASSERT_EQ(0, fclose(near_file_));
489 }
490 near_file_ = NULL;
491
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000492 if (out_file_) {
493 ASSERT_EQ(0, fclose(out_file_));
494 }
495 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000496}
497
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000498void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000499 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700500 ap->Initialize(
501 {{{frame_->sample_rate_hz_, frame_->num_channels_},
502 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700503 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700504 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000505}
506
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000507void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000508 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000509 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800510 size_t num_input_channels,
511 size_t num_output_channels,
512 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000513 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000514 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000515 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000516 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000517
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000518 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
519 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000520 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000521
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000522 if (far_file_) {
523 ASSERT_EQ(0, fclose(far_file_));
524 }
525 std::string filename = ResourceFilePath("far", sample_rate_hz);
526 far_file_ = fopen(filename.c_str(), "rb");
527 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
528 filename << "\n";
529
530 if (near_file_) {
531 ASSERT_EQ(0, fclose(near_file_));
532 }
533 filename = ResourceFilePath("near", sample_rate_hz);
534 near_file_ = fopen(filename.c_str(), "rb");
535 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
536 filename << "\n";
537
538 if (open_output_file) {
539 if (out_file_) {
540 ASSERT_EQ(0, fclose(out_file_));
541 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700542 filename = OutputFilePath(
543 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
544 reverse_sample_rate_hz, num_input_channels, num_output_channels,
545 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000546 out_file_ = fopen(filename.c_str(), "wb");
547 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
548 filename << "\n";
549 }
550}
551
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000552void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000553 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000554}
555
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
557 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000558 // The files always contain stereo audio.
559 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700560 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000561 sizeof(int16_t),
562 frame_size,
563 file);
564 if (read_count != frame_size) {
565 // Check that the file really ended.
566 EXPECT_NE(0, feof(file));
567 return false; // This is expected.
568 }
569
570 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700571 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000572 frame->samples_per_channel_);
573 }
574
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000576 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000578 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000579}
580
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
582 return ReadFrame(file, frame, NULL);
583}
584
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000585// If the end of the file has been reached, rewind it and attempt to read the
586// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
588 ChannelBuffer<float>* cb) {
589 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000590 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000592 }
593}
594
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000595void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
596 ReadFrameWithRewind(file, frame, NULL);
597}
598
andrew@webrtc.org81865342012-10-27 00:28:27 +0000599void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
600 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000601 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000602 EXPECT_EQ(apm_->kNoError,
603 apm_->gain_control()->set_stream_analog_level(127));
604 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000605}
606
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000607int ApmTest::ProcessStreamChooser(Format format) {
608 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000609 return apm_->ProcessStream(frame_);
610 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000611 return apm_->ProcessStream(float_cb_->channels(),
612 frame_->samples_per_channel_,
613 frame_->sample_rate_hz_,
614 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000615 output_sample_rate_hz_,
616 LayoutFromChannels(num_output_channels_),
617 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000618}
619
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000620int ApmTest::AnalyzeReverseStreamChooser(Format format) {
621 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700622 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000623 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000624 return apm_->AnalyzeReverseStream(
625 revfloat_cb_->channels(),
626 revframe_->samples_per_channel_,
627 revframe_->sample_rate_hz_,
628 LayoutFromChannels(revframe_->num_channels_));
629}
630
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
632 int delay_min, int delay_max) {
633 // The |revframe_| and |frame_| should include the proper frame information,
634 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000635 AudioFrame tmp_frame;
636 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000637 bool causal = true;
638
639 tmp_frame.CopyFrom(*revframe_);
640 SetFrameTo(&tmp_frame, 0);
641
642 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
643 // Initialize the |frame_queue| with empty frames.
644 int frame_delay = delay_ms / 10;
645 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000646 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 frame->CopyFrom(tmp_frame);
648 frame_queue.push(frame);
649 frame_delay++;
650 causal = false;
651 }
652 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000653 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000654 frame->CopyFrom(tmp_frame);
655 frame_queue.push(frame);
656 frame_delay--;
657 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000658 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
659 // need enough frames with audio to have reliable estimates, but as few as
660 // possible to keep processing time down. 4.5 seconds seemed to be a good
661 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000663 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 frame->CopyFrom(tmp_frame);
665 // Use the near end recording, since that has more speech in it.
666 ASSERT_TRUE(ReadFrame(near_file_, frame));
667 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000668 AudioFrame* reverse_frame = frame;
669 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000670 if (!causal) {
671 reverse_frame = frame_queue.front();
672 // When we call ProcessStream() the frame is modified, so we can't use the
673 // pointer directly when things are non-causal. Use an intermediate frame
674 // and copy the data.
675 process_frame = &tmp_frame;
676 process_frame->CopyFrom(*frame);
677 }
aluebsb0319552016-03-17 20:39:53 -0700678 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000679 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
680 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
681 frame = frame_queue.front();
682 frame_queue.pop();
683 delete frame;
684
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000685 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000686 int median;
687 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000688 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 // Discard the first delay metrics to avoid convergence effects.
690 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000691 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
692 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000693 }
694 }
695
696 rewind(near_file_);
697 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000698 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000699 frame_queue.pop();
700 delete frame;
701 }
702 // Calculate expected delay estimate and acceptable regions. Further,
703 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700704 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700705 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700706 const int expected_median =
707 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
708 const int expected_median_high = rtc::SafeClamp<int>(
709 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700710 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700711 const int expected_median_low = rtc::SafeClamp<int>(
712 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700713 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000714 // Verify delay metrics.
715 int median;
716 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000717 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000718 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000719 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
720 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000721 EXPECT_GE(expected_median_high, median);
722 EXPECT_LE(expected_median_low, median);
723}
724
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000725void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000726 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000727 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000728
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000731 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000733
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 EXPECT_EQ(apm_->kNoError,
736 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000737 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000738 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000739 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000740
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000741 // Other stream parameters set correctly.
742 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 EXPECT_EQ(apm_->kNoError,
744 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000745 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000746 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000748 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000749 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
750 EXPECT_EQ(apm_->kNoError,
751 apm_->echo_cancellation()->enable_drift_compensation(false));
752
753 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000754 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758
759 // Resets after successful ProcessStream().
760 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000761 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000762 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000763 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000764
765 // Other stream parameters set correctly.
766 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
767 EXPECT_EQ(apm_->kNoError,
768 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000769 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000770 EXPECT_EQ(apm_->kNoError,
771 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000772 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000773 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000774 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
775
776 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000777 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000779
780 // Resets after successful ProcessStream().
781 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000782 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000783 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000784 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000785 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000786
787 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000788 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
789 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
790 EXPECT_EQ(apm_->kNoError,
791 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000792 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000793 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000794
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000795 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000796 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000797 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000799 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000800
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000801 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000803 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000804 EXPECT_EQ(apm_->kNoError,
805 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000806 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000807}
808
809TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000810 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000811}
812
813TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000814 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000815}
816
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000817TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
818 EXPECT_EQ(0, apm_->delay_offset_ms());
819 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
820 EXPECT_EQ(50, apm_->stream_delay_ms());
821}
822
823TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
824 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000825 apm_->set_delay_offset_ms(100);
826 EXPECT_EQ(100, apm_->delay_offset_ms());
827 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000828 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000829 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
830 EXPECT_EQ(200, apm_->stream_delay_ms());
831
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000832 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000833 apm_->set_delay_offset_ms(-50);
834 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000835 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
836 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000837 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
838 EXPECT_EQ(50, apm_->stream_delay_ms());
839}
840
Michael Graczyk86c6d332015-07-23 11:41:39 -0700841void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800842 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700843 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000844 frame_->num_channels_ = num_channels;
845 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700846 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000847}
848
Michael Graczyk86c6d332015-07-23 11:41:39 -0700849void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800850 size_t num_in_channels,
851 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700852 AudioProcessing::Error expected_return) {
853 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
854 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
855
856 EXPECT_EQ(expected_return,
857 apm_->ProcessStream(float_cb_->channels(), input_stream,
858 output_stream, float_cb_->channels()));
859}
860
861void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800862 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700863 AudioProcessing::Error expected_return) {
864 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700865 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
866 {output_sample_rate_hz_, apm_->num_output_channels()},
867 {frame_->sample_rate_hz_, num_rev_channels},
868 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700869
ekmeyerson60d9b332015-08-14 10:35:55 -0700870 EXPECT_EQ(
871 expected_return,
872 apm_->ProcessReverseStream(
873 float_cb_->channels(), processing_config.reverse_input_stream(),
874 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700875}
876
877TEST_F(ApmTest, ChannelsInt16Interface) {
878 // Testing number of invalid and valid channels.
879 Init(16000, 16000, 16000, 4, 4, 4, false);
880
881 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
882
Peter Kasting69558702016-01-12 16:26:35 -0800883 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700884 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 }
887}
888
Michael Graczyk86c6d332015-07-23 11:41:39 -0700889TEST_F(ApmTest, Channels) {
890 // Testing number of invalid and valid channels.
891 Init(16000, 16000, 16000, 4, 4, 4, false);
892
893 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
894 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
895
Peter Kasting69558702016-01-12 16:26:35 -0800896 for (size_t i = 1; i < 4; ++i) {
897 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700898 // Output channels much be one or match input channels.
899 if (j == 1 || i == j) {
900 TestChangingForwardChannels(i, j, kNoErr);
901 TestChangingReverseChannels(i, kNoErr);
902
903 EXPECT_EQ(i, apm_->num_input_channels());
904 EXPECT_EQ(j, apm_->num_output_channels());
905 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800906 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700907 } else {
908 TestChangingForwardChannels(i, j,
909 AudioProcessing::kBadNumberChannelsError);
910 }
911 }
912 }
913}
914
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000915TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000916 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000917 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000918 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000919 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700920 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800921 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000922 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000923 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 }
925}
926
niklase@google.com470e71d2011-07-07 08:21:25 +0000927TEST_F(ApmTest, EchoCancellation) {
928 EXPECT_EQ(apm_->kNoError,
929 apm_->echo_cancellation()->enable_drift_compensation(true));
930 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
931 EXPECT_EQ(apm_->kNoError,
932 apm_->echo_cancellation()->enable_drift_compensation(false));
933 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
934
niklase@google.com470e71d2011-07-07 08:21:25 +0000935 EchoCancellation::SuppressionLevel level[] = {
936 EchoCancellation::kLowSuppression,
937 EchoCancellation::kModerateSuppression,
938 EchoCancellation::kHighSuppression,
939 };
pkasting25702cb2016-01-08 13:50:27 -0800940 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000941 EXPECT_EQ(apm_->kNoError,
942 apm_->echo_cancellation()->set_suppression_level(level[i]));
943 EXPECT_EQ(level[i],
944 apm_->echo_cancellation()->suppression_level());
945 }
946
947 EchoCancellation::Metrics metrics;
948 EXPECT_EQ(apm_->kNotEnabledError,
949 apm_->echo_cancellation()->GetMetrics(&metrics));
950
ivoc3e9a5372016-10-28 07:55:33 -0700951 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
952 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
953
niklase@google.com470e71d2011-07-07 08:21:25 +0000954 EXPECT_EQ(apm_->kNoError,
955 apm_->echo_cancellation()->enable_metrics(true));
956 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
957 EXPECT_EQ(apm_->kNoError,
958 apm_->echo_cancellation()->enable_metrics(false));
959 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
960
ivoc48dfab52016-10-28 03:29:31 -0700961 EXPECT_EQ(apm_->kNoError,
962 apm_->echo_cancellation()->enable_delay_logging(true));
963 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
964 EXPECT_EQ(apm_->kNoError,
965 apm_->echo_cancellation()->enable_delay_logging(false));
966 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
967
ivoc3e9a5372016-10-28 07:55:33 -0700968 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
969 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
970
971 int median = 0;
972 int std = 0;
973 float poor_fraction = 0;
974 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
975 &median, &std, &poor_fraction));
976
niklase@google.com470e71d2011-07-07 08:21:25 +0000977 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
978 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
979 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
980 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000981
982 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
983 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
984 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
985 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
986 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
987 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000988}
989
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000990TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000991 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000992 // Enable AEC only.
993 EXPECT_EQ(apm_->kNoError,
994 apm_->echo_cancellation()->enable_drift_compensation(false));
995 EXPECT_EQ(apm_->kNoError,
996 apm_->echo_cancellation()->enable_metrics(false));
997 EXPECT_EQ(apm_->kNoError,
998 apm_->echo_cancellation()->enable_delay_logging(true));
999 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +00001000 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -07001001 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +00001002 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001003
1004 // Internally in the AEC the amount of lookahead the delay estimation can
1005 // handle is 15 blocks and the maximum delay is set to 60 blocks.
1006 const int kLookaheadBlocks = 15;
1007 const int kMaxDelayBlocks = 60;
1008 // The AEC has a startup time before it actually starts to process. This
1009 // procedure can flush the internal far-end buffer, which of course affects
1010 // the delay estimation. Therefore, we set a system_delay high enough to
1011 // avoid that. The smallest system_delay you can report without flushing the
1012 // buffer is 66 ms in 8 kHz.
1013 //
1014 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1015 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1016 // delay estimation. This should be noted though. In case of test failure,
1017 // this could be the cause.
1018 const int kSystemDelayMs = 66;
1019 // Test a couple of corner cases and verify that the estimated delay is
1020 // within a valid region (set to +-1.5 blocks). Note that these cases are
1021 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001022 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001023 Init(kProcessSampleRates[i],
1024 kProcessSampleRates[i],
1025 kProcessSampleRates[i],
1026 2,
1027 2,
1028 2,
1029 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001030 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001031 const int num_ms_per_block =
1032 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001033 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1034 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1035
1036 // 1) Verify correct delay estimate at lookahead boundary.
1037 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1038 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1039 delay_max_ms);
1040 // 2) A delay less than maximum lookahead should give an delay estimate at
1041 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1042 delay_ms -= 20;
1043 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1044 delay_max_ms);
1045 // 3) Three values around zero delay. Note that we need to compensate for
1046 // the fake system_delay.
1047 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1048 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1049 delay_max_ms);
1050 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1051 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1052 delay_max_ms);
1053 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1054 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1055 delay_max_ms);
1056 // 4) Verify correct delay estimate at maximum delay boundary.
1057 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1058 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1059 delay_max_ms);
1060 // 5) A delay above the maximum delay should give an estimate at the
1061 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1062 delay_ms += 20;
1063 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1064 delay_max_ms);
1065 }
1066}
1067
niklase@google.com470e71d2011-07-07 08:21:25 +00001068TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001069 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001070 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1072 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1073
niklase@google.com470e71d2011-07-07 08:21:25 +00001074 // Toggle routing modes
1075 EchoControlMobile::RoutingMode mode[] = {
1076 EchoControlMobile::kQuietEarpieceOrHeadset,
1077 EchoControlMobile::kEarpiece,
1078 EchoControlMobile::kLoudEarpiece,
1079 EchoControlMobile::kSpeakerphone,
1080 EchoControlMobile::kLoudSpeakerphone,
1081 };
pkasting25702cb2016-01-08 13:50:27 -08001082 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 EXPECT_EQ(apm_->kNoError,
1084 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1085 EXPECT_EQ(mode[i],
1086 apm_->echo_control_mobile()->routing_mode());
1087 }
1088 // Turn comfort noise off/on
1089 EXPECT_EQ(apm_->kNoError,
1090 apm_->echo_control_mobile()->enable_comfort_noise(false));
1091 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1092 EXPECT_EQ(apm_->kNoError,
1093 apm_->echo_control_mobile()->enable_comfort_noise(true));
1094 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001095 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001096 const size_t echo_path_size =
1097 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001098 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1099 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001100 EXPECT_EQ(apm_->kNullPointerError,
1101 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1102 EXPECT_EQ(apm_->kNullPointerError,
1103 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1104 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001105 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001106 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001107 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001108 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001109 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001110 echo_path_in[i] = echo_path_out[i] + 1;
1111 }
1112 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001113 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001114 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001115 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1116 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001117 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001118 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1119 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001120 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001121 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1122 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001123
1124 // Process a few frames with NS in the default disabled state. This exercises
1125 // a different codepath than with it enabled.
1126 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1127 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1128 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1129 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1130
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 // Turn AECM off
1132 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1133 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1134}
1135
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001136TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001137 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001138 EXPECT_EQ(apm_->kNoError,
1139 apm_->gain_control()->set_mode(
1140 apm_->gain_control()->mode()));
1141
1142 GainControl::Mode mode[] = {
1143 GainControl::kAdaptiveAnalog,
1144 GainControl::kAdaptiveDigital,
1145 GainControl::kFixedDigital
1146 };
pkasting25702cb2016-01-08 13:50:27 -08001147 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001148 EXPECT_EQ(apm_->kNoError,
1149 apm_->gain_control()->set_mode(mode[i]));
1150 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1151 }
1152 // Testing invalid target levels
1153 EXPECT_EQ(apm_->kBadParameterError,
1154 apm_->gain_control()->set_target_level_dbfs(-3));
1155 EXPECT_EQ(apm_->kBadParameterError,
1156 apm_->gain_control()->set_target_level_dbfs(-40));
1157 // Testing valid target levels
1158 EXPECT_EQ(apm_->kNoError,
1159 apm_->gain_control()->set_target_level_dbfs(
1160 apm_->gain_control()->target_level_dbfs()));
1161
1162 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001163 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001164 EXPECT_EQ(apm_->kNoError,
1165 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1166 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1167 }
1168
1169 // Testing invalid compression gains
1170 EXPECT_EQ(apm_->kBadParameterError,
1171 apm_->gain_control()->set_compression_gain_db(-1));
1172 EXPECT_EQ(apm_->kBadParameterError,
1173 apm_->gain_control()->set_compression_gain_db(100));
1174
1175 // Testing valid compression gains
1176 EXPECT_EQ(apm_->kNoError,
1177 apm_->gain_control()->set_compression_gain_db(
1178 apm_->gain_control()->compression_gain_db()));
1179
1180 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001181 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001182 EXPECT_EQ(apm_->kNoError,
1183 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1184 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1185 }
1186
1187 // Testing limiter off/on
1188 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1189 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1190 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1191 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1192
1193 // Testing invalid level limits
1194 EXPECT_EQ(apm_->kBadParameterError,
1195 apm_->gain_control()->set_analog_level_limits(-1, 512));
1196 EXPECT_EQ(apm_->kBadParameterError,
1197 apm_->gain_control()->set_analog_level_limits(100000, 512));
1198 EXPECT_EQ(apm_->kBadParameterError,
1199 apm_->gain_control()->set_analog_level_limits(512, -1));
1200 EXPECT_EQ(apm_->kBadParameterError,
1201 apm_->gain_control()->set_analog_level_limits(512, 100000));
1202 EXPECT_EQ(apm_->kBadParameterError,
1203 apm_->gain_control()->set_analog_level_limits(512, 255));
1204
1205 // Testing valid level limits
1206 EXPECT_EQ(apm_->kNoError,
1207 apm_->gain_control()->set_analog_level_limits(
1208 apm_->gain_control()->analog_level_minimum(),
1209 apm_->gain_control()->analog_level_maximum()));
1210
1211 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001212 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001213 EXPECT_EQ(apm_->kNoError,
1214 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1215 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1216 }
1217
1218 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001219 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001220 EXPECT_EQ(apm_->kNoError,
1221 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1222 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1223 }
1224
1225 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1226
1227 // Turn AGC off
1228 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1229 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1230}
1231
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001232void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001233 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001234 EXPECT_EQ(apm_->kNoError,
1235 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1236 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1237
1238 int out_analog_level = 0;
1239 for (int i = 0; i < 2000; ++i) {
1240 ReadFrameWithRewind(near_file_, frame_);
1241 // Ensure the audio is at a low level, so the AGC will try to increase it.
1242 ScaleFrame(frame_, 0.25);
1243
1244 // Always pass in the same volume.
1245 EXPECT_EQ(apm_->kNoError,
1246 apm_->gain_control()->set_stream_analog_level(100));
1247 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1248 out_analog_level = apm_->gain_control()->stream_analog_level();
1249 }
1250
1251 // Ensure the AGC is still able to reach the maximum.
1252 EXPECT_EQ(255, out_analog_level);
1253}
1254
1255// Verifies that despite volume slider quantization, the AGC can continue to
1256// increase its volume.
1257TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001258 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001259 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1260 }
1261}
1262
1263void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001264 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001265 EXPECT_EQ(apm_->kNoError,
1266 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1267 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1268
1269 int out_analog_level = 100;
1270 for (int i = 0; i < 1000; ++i) {
1271 ReadFrameWithRewind(near_file_, frame_);
1272 // Ensure the audio is at a low level, so the AGC will try to increase it.
1273 ScaleFrame(frame_, 0.25);
1274
1275 EXPECT_EQ(apm_->kNoError,
1276 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1277 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1278 out_analog_level = apm_->gain_control()->stream_analog_level();
1279 }
1280
1281 // Ensure the volume was raised.
1282 EXPECT_GT(out_analog_level, 100);
1283 int highest_level_reached = out_analog_level;
1284 // Simulate a user manual volume change.
1285 out_analog_level = 100;
1286
1287 for (int i = 0; i < 300; ++i) {
1288 ReadFrameWithRewind(near_file_, frame_);
1289 ScaleFrame(frame_, 0.25);
1290
1291 EXPECT_EQ(apm_->kNoError,
1292 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1293 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1294 out_analog_level = apm_->gain_control()->stream_analog_level();
1295 // Check that AGC respected the manually adjusted volume.
1296 EXPECT_LT(out_analog_level, highest_level_reached);
1297 }
1298 // Check that the volume was still raised.
1299 EXPECT_GT(out_analog_level, 100);
1300}
1301
1302TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001303 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001304 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1305 }
1306}
1307
niklase@google.com470e71d2011-07-07 08:21:25 +00001308TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001309 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001310 NoiseSuppression::Level level[] = {
1311 NoiseSuppression::kLow,
1312 NoiseSuppression::kModerate,
1313 NoiseSuppression::kHigh,
1314 NoiseSuppression::kVeryHigh
1315 };
pkasting25702cb2016-01-08 13:50:27 -08001316 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001317 EXPECT_EQ(apm_->kNoError,
1318 apm_->noise_suppression()->set_level(level[i]));
1319 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1320 }
1321
andrew@webrtc.org648af742012-02-08 01:57:29 +00001322 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1324 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1325 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1326 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1327}
1328
1329TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001330 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001331 AudioProcessing::Config apm_config;
1332 apm_config.high_pass_filter.enabled = true;
1333 apm_->ApplyConfig(apm_config);
1334 apm_config.high_pass_filter.enabled = false;
1335 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001336}
1337
1338TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001339 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001340 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001341 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001342
1343 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1344
1345 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1346 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1347
1348 // Run this test in wideband; in super-wb, the splitting filter distorts the
1349 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001350 frame_->samples_per_channel_ = 160;
1351 frame_->num_channels_ = 2;
1352 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001353
1354 // Min value if no frames have been processed.
1355 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1356
1357 // Min value on zero frames.
1358 SetFrameTo(frame_, 0);
1359 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1360 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1361 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1362
1363 // Try a few RMS values.
1364 // (These also test that the value resets after retrieving it.)
1365 SetFrameTo(frame_, 32767);
1366 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1367 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1368 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1369
1370 SetFrameTo(frame_, 30000);
1371 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1372 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1373 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1374
1375 SetFrameTo(frame_, 10000);
1376 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1377 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1378 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1379
1380 SetFrameTo(frame_, 10);
1381 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1382 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1383 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1384
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001385 // Verify reset after enable/disable.
1386 SetFrameTo(frame_, 32767);
1387 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1388 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1389 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1390 SetFrameTo(frame_, 1);
1391 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1392 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1393
1394 // Verify reset after initialize.
1395 SetFrameTo(frame_, 32767);
1396 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1397 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1398 SetFrameTo(frame_, 1);
1399 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1400 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001401}
1402
1403TEST_F(ApmTest, VoiceDetection) {
1404 // Test external VAD
1405 EXPECT_EQ(apm_->kNoError,
1406 apm_->voice_detection()->set_stream_has_voice(true));
1407 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1408 EXPECT_EQ(apm_->kNoError,
1409 apm_->voice_detection()->set_stream_has_voice(false));
1410 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1411
andrew@webrtc.org648af742012-02-08 01:57:29 +00001412 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001413 VoiceDetection::Likelihood likelihood[] = {
1414 VoiceDetection::kVeryLowLikelihood,
1415 VoiceDetection::kLowLikelihood,
1416 VoiceDetection::kModerateLikelihood,
1417 VoiceDetection::kHighLikelihood
1418 };
pkasting25702cb2016-01-08 13:50:27 -08001419 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001420 EXPECT_EQ(apm_->kNoError,
1421 apm_->voice_detection()->set_likelihood(likelihood[i]));
1422 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1423 }
1424
1425 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001426 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001427 EXPECT_EQ(apm_->kBadParameterError,
1428 apm_->voice_detection()->set_frame_size_ms(12));
1429
andrew@webrtc.org648af742012-02-08 01:57:29 +00001430 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 for (int i = 10; i <= 30; i += 10) {
1432 EXPECT_EQ(apm_->kNoError,
1433 apm_->voice_detection()->set_frame_size_ms(i));
1434 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1435 }
1436 */
1437
andrew@webrtc.org648af742012-02-08 01:57:29 +00001438 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001439 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1440 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1441 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1442 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1443
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001444 // Test that AudioFrame activity is maintained when VAD is disabled.
1445 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1446 AudioFrame::VADActivity activity[] = {
1447 AudioFrame::kVadActive,
1448 AudioFrame::kVadPassive,
1449 AudioFrame::kVadUnknown
1450 };
pkasting25702cb2016-01-08 13:50:27 -08001451 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001452 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001453 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001454 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001455 }
1456
1457 // Test that AudioFrame activity is set when VAD is enabled.
1458 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001459 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001460 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001461 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001462
niklase@google.com470e71d2011-07-07 08:21:25 +00001463 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1464}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001465
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001466TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1467 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1468 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1469 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1470 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1471 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1472 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1473 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1474}
1475
1476TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001477 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001478 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001479 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001480 AudioFrame frame_copy;
1481 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001482 for (int j = 0; j < 1000; j++) {
1483 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1484 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001485 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1486 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001487 }
1488 }
1489}
1490
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001491TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1492 // Test that ProcessStream copies input to output even with no processing.
1493 const size_t kSamples = 80;
1494 const int sample_rate = 8000;
1495 const float src[kSamples] = {
1496 -1.0f, 0.0f, 1.0f
1497 };
1498 float dest[kSamples] = {};
1499
1500 auto src_channels = &src[0];
1501 auto dest_channels = &dest[0];
1502
Ivo Creusen62337e52018-01-09 14:17:33 +01001503 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001504 EXPECT_NOERR(apm_->ProcessStream(
1505 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1506 sample_rate, LayoutFromChannels(1), &dest_channels));
1507
1508 for (size_t i = 0; i < kSamples; ++i) {
1509 EXPECT_EQ(src[i], dest[i]);
1510 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001511
1512 // Same for ProcessReverseStream.
1513 float rev_dest[kSamples] = {};
1514 auto rev_dest_channels = &rev_dest[0];
1515
1516 StreamConfig input_stream = {sample_rate, 1};
1517 StreamConfig output_stream = {sample_rate, 1};
1518 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1519 output_stream, &rev_dest_channels));
1520
1521 for (size_t i = 0; i < kSamples; ++i) {
1522 EXPECT_EQ(src[i], rev_dest[i]);
1523 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001524}
1525
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001526TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1527 EnableAllComponents();
1528
pkasting25702cb2016-01-08 13:50:27 -08001529 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001530 Init(kProcessSampleRates[i],
1531 kProcessSampleRates[i],
1532 kProcessSampleRates[i],
1533 2,
1534 2,
1535 2,
1536 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001537 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001538 ASSERT_EQ(0, feof(far_file_));
1539 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001540 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001541 CopyLeftToRightChannel(revframe_->mutable_data(),
1542 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001543
aluebsb0319552016-03-17 20:39:53 -07001544 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001545
yujo36b1a5f2017-06-12 12:45:32 -07001546 CopyLeftToRightChannel(frame_->mutable_data(),
1547 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001548 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1549
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001550 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001551 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001552 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001553 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001554 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001555 analog_level = apm_->gain_control()->stream_analog_level();
1556
yujo36b1a5f2017-06-12 12:45:32 -07001557 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001558 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001559 rewind(far_file_);
1560 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001561 }
1562}
1563
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001564TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001565 // Verify the filter is not active through undistorted audio when:
1566 // 1. No components are enabled...
1567 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001568 AudioFrame frame_copy;
1569 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001570 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1571 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1572 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1573
1574 // 2. Only the level estimator is enabled...
1575 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001576 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001577 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1578 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1579 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1580 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1581 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1582
1583 // 3. Only VAD is enabled...
1584 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001585 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001586 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1587 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1588 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1589 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1590 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1591
1592 // 4. Both VAD and the level estimator are enabled...
1593 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001594 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001595 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1596 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1597 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1598 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1599 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1600 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1601 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1602
1603 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001604 frame_->samples_per_channel_ = 160;
1605 frame_->num_channels_ = 2;
1606 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001607 // Enable AEC, which would require the filter in super-wb. We rely on the
1608 // first few frames of data being unaffected by the AEC.
1609 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1610 // behavior of the AEC. Think of something more robust.
1611 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001612 // Make sure we have extended filter enabled. This makes sure nothing is
1613 // touched until we have a farend frame.
1614 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001615 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001616 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001618 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001619 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001620 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001621 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1622 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001623 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001624 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1625 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1626
1627 // Check the test is valid. We should have distortion from the filter
1628 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001629 frame_->samples_per_channel_ = 320;
1630 frame_->num_channels_ = 2;
1631 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001633 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001635 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001636 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1637 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1638}
1639
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001640#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1641void ApmTest::ProcessDebugDump(const std::string& in_filename,
1642 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001643 Format format,
1644 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001645 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001646 FILE* in_file = fopen(in_filename.c_str(), "rb");
1647 ASSERT_TRUE(in_file != NULL);
1648 audioproc::Event event_msg;
1649 bool first_init = true;
1650
1651 while (ReadMessageFromFile(in_file, &event_msg)) {
1652 if (event_msg.type() == audioproc::Event::INIT) {
1653 const audioproc::Init msg = event_msg.init();
1654 int reverse_sample_rate = msg.sample_rate();
1655 if (msg.has_reverse_sample_rate()) {
1656 reverse_sample_rate = msg.reverse_sample_rate();
1657 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001658 int output_sample_rate = msg.sample_rate();
1659 if (msg.has_output_sample_rate()) {
1660 output_sample_rate = msg.output_sample_rate();
1661 }
1662
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001663 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001664 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001665 reverse_sample_rate,
1666 msg.num_input_channels(),
1667 msg.num_output_channels(),
1668 msg.num_reverse_channels(),
1669 false);
1670 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001671 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001672 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001673 auto aec_dump =
1674 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1675 EXPECT_TRUE(aec_dump);
1676 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001677 first_init = false;
1678 }
1679
1680 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1681 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1682
1683 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001684 ASSERT_EQ(revframe_->num_channels_,
1685 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001686 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001687 memcpy(revfloat_cb_->channels()[i],
1688 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001689 msg.channel(i).size());
1690 }
1691 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001692 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001693 if (format == kFloatFormat) {
1694 // We're using an int16 input file; convert to float.
1695 ConvertToFloat(*revframe_, revfloat_cb_.get());
1696 }
1697 }
1698 AnalyzeReverseStreamChooser(format);
1699
1700 } else if (event_msg.type() == audioproc::Event::STREAM) {
1701 const audioproc::Stream msg = event_msg.stream();
1702 // ProcessStream could have changed this for the output frame.
1703 frame_->num_channels_ = apm_->num_input_channels();
1704
1705 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1706 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1707 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1708 if (msg.has_keypress()) {
1709 apm_->set_stream_key_pressed(msg.keypress());
1710 } else {
1711 apm_->set_stream_key_pressed(true);
1712 }
1713
1714 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001715 ASSERT_EQ(frame_->num_channels_,
1716 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001717 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001718 memcpy(float_cb_->channels()[i],
1719 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001720 msg.input_channel(i).size());
1721 }
1722 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001723 memcpy(frame_->mutable_data(), msg.input_data().data(),
1724 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001725 if (format == kFloatFormat) {
1726 // We're using an int16 input file; convert to float.
1727 ConvertToFloat(*frame_, float_cb_.get());
1728 }
1729 }
1730 ProcessStreamChooser(format);
1731 }
1732 }
aleloif4dd1912017-06-15 01:55:38 -07001733 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001734 fclose(in_file);
1735}
1736
1737void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001738 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001739 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001740 std::string format_string;
1741 switch (format) {
1742 case kIntFormat:
1743 format_string = "_int";
1744 break;
1745 case kFloatFormat:
1746 format_string = "_float";
1747 break;
1748 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001749 const std::string ref_filename = test::TempFilename(
1750 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1751 const std::string out_filename = test::TempFilename(
1752 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001753 const std::string limited_filename = test::TempFilename(
1754 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1755 const size_t logging_limit_bytes = 100000;
1756 // We expect at least this many bytes in the created logfile.
1757 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001758 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001759 ProcessDebugDump(in_filename, ref_filename, format, -1);
1760 ProcessDebugDump(ref_filename, out_filename, format, -1);
1761 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001762
1763 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1764 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001765 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001766 ASSERT_TRUE(ref_file != NULL);
1767 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001768 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001769 std::unique_ptr<uint8_t[]> ref_bytes;
1770 std::unique_ptr<uint8_t[]> out_bytes;
1771 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001772
1773 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1774 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001775 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001776 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001777 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001778 while (ref_size > 0 && out_size > 0) {
1779 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001780 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001781 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001782 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001783 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001784 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001785 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1786 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001787 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001788 }
1789 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001790 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1791 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001792 EXPECT_NE(0, feof(ref_file));
1793 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001794 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001795 ASSERT_EQ(0, fclose(ref_file));
1796 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001797 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001798 remove(ref_filename.c_str());
1799 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001800 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001801}
1802
pbosc7a65692016-05-06 12:50:04 -07001803TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001804 VerifyDebugDumpTest(kIntFormat);
1805}
1806
pbosc7a65692016-05-06 12:50:04 -07001807TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001808 VerifyDebugDumpTest(kFloatFormat);
1809}
1810#endif
1811
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001812// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001813TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001814 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001815 const std::string filename =
1816 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001817 {
1818 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1819 EXPECT_FALSE(aec_dump);
1820 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001821
1822#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1823 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001824 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001825
aleloif4dd1912017-06-15 01:55:38 -07001826 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1827 EXPECT_TRUE(aec_dump);
1828 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001829 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001830 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001831 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001832
1833 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001834 FILE* fid = fopen(filename.c_str(), "r");
1835 ASSERT_TRUE(fid != NULL);
1836
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001837 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001838 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001839 ASSERT_EQ(0, remove(filename.c_str()));
1840#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001841 // Verify the file has NOT been written.
1842 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1843#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1844}
1845
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001846// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001847TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001848 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1849
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001850 const std::string filename =
1851 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001852 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001853 ASSERT_TRUE(fid);
1854
1855#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1856 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001857 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001858
aleloif4dd1912017-06-15 01:55:38 -07001859 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1860 EXPECT_TRUE(aec_dump);
1861 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001862 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001863 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001864 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001865
1866 // Verify the file has been written.
1867 fid = fopen(filename.c_str(), "r");
1868 ASSERT_TRUE(fid != NULL);
1869
1870 // Clean it up.
1871 ASSERT_EQ(0, fclose(fid));
1872 ASSERT_EQ(0, remove(filename.c_str()));
1873#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001874 ASSERT_EQ(0, fclose(fid));
1875#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1876}
1877
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001878TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001879 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001880 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001881
1882 Config config;
1883 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001884 std::unique_ptr<AudioProcessing> fapm(
1885 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001886 EnableAllComponents();
1887 EnableAllAPComponents(fapm.get());
1888 for (int i = 0; i < ref_data.test_size(); i++) {
1889 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1890
1891 audioproc::Test* test = ref_data.mutable_test(i);
1892 // TODO(ajm): Restore downmixing test cases.
1893 if (test->num_input_channels() != test->num_output_channels())
1894 continue;
1895
Peter Kasting69558702016-01-12 16:26:35 -08001896 const size_t num_render_channels =
1897 static_cast<size_t>(test->num_reverse_channels());
1898 const size_t num_input_channels =
1899 static_cast<size_t>(test->num_input_channels());
1900 const size_t num_output_channels =
1901 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001902 const size_t samples_per_channel = static_cast<size_t>(
1903 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001904
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001905 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1906 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001907 Init(fapm.get());
1908
1909 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001910 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1911 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001912
1913 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001914 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001915 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1916 ReadFrame(near_file_, frame_, float_cb_.get())) {
1917 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1918
aluebsb0319552016-03-17 20:39:53 -07001919 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001920 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1921 revfloat_cb_->channels(),
1922 samples_per_channel,
1923 test->sample_rate(),
1924 LayoutFromChannels(num_render_channels)));
1925
1926 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1927 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1928 apm_->echo_cancellation()->set_stream_drift_samples(0);
1929 fapm->echo_cancellation()->set_stream_drift_samples(0);
1930 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1931 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1932
1933 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001934 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001935 output_int16.channels());
1936
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001937 EXPECT_NOERR(fapm->ProcessStream(
1938 float_cb_->channels(),
1939 samples_per_channel,
1940 test->sample_rate(),
1941 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001942 test->sample_rate(),
1943 LayoutFromChannels(num_output_channels),
1944 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001945 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001946 FloatToS16(float_cb_->channels()[j],
1947 samples_per_channel,
1948 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001949 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001950 float snr = ComputeSNR(output_int16.channels()[j],
1951 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001952 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001953
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001954 const float kVarianceThreshold = 20;
1955 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001956
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001957 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001958 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1959 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001960 }
1961 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001962
1963 analog_level = fapm->gain_control()->stream_analog_level();
1964 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1965 fapm->gain_control()->stream_analog_level());
1966 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
1967 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001968 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1969 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001970 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001971
1972 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001973 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001974 }
aluebs776593b2016-03-15 14:04:58 -07001975
1976#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1977 const size_t kMaxNumBadChunks = 0;
1978#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1979 // There are a few chunks in the fixed-point profile that give low SNR.
1980 // Listening confirmed the difference is acceptable.
1981 const size_t kMaxNumBadChunks = 60;
1982#endif
1983 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1984
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001985 rewind(far_file_);
1986 rewind(near_file_);
1987 }
1988}
1989
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001990// TODO(andrew): Add a test to process a few frames with different combinations
1991// of enabled components.
1992
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001993TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001994 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001995 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001996
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001997 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001998 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001999 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002000 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002001 for (size_t i = 0; i < arraysize(kChannels); i++) {
2002 for (size_t j = 0; j < arraysize(kChannels); j++) {
2003 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002004 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002005 test->set_num_reverse_channels(kChannels[i]);
2006 test->set_num_input_channels(kChannels[j]);
2007 test->set_num_output_channels(kChannels[j]);
2008 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002009 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002010 }
2011 }
2012 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002013#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2014 // To test the extended filter mode.
2015 audioproc::Test* test = ref_data.add_test();
2016 test->set_num_reverse_channels(2);
2017 test->set_num_input_channels(2);
2018 test->set_num_output_channels(2);
2019 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2020 test->set_use_aec_extended_filter(true);
2021#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002022 }
2023
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002024 for (int i = 0; i < ref_data.test_size(); i++) {
2025 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002026
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002027 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002028 // TODO(ajm): We no longer allow different input and output channels. Skip
2029 // these tests for now, but they should be removed from the set.
2030 if (test->num_input_channels() != test->num_output_channels())
2031 continue;
2032
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002033 Config config;
2034 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002035 config.Set<ExtendedFilter>(
2036 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01002037 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002038
2039 EnableAllComponents();
2040
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002041 Init(test->sample_rate(),
2042 test->sample_rate(),
2043 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002044 static_cast<size_t>(test->num_input_channels()),
2045 static_cast<size_t>(test->num_output_channels()),
2046 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002047 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002048
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002049 int frame_count = 0;
2050 int has_echo_count = 0;
2051 int has_voice_count = 0;
2052 int is_saturated_count = 0;
2053 int analog_level = 127;
2054 int analog_level_average = 0;
2055 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002056 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002057#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2058 int stats_index = 0;
2059#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002060
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002061 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002062 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002063
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002064 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2065
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002066 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002067 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002068 EXPECT_EQ(apm_->kNoError,
2069 apm_->gain_control()->set_stream_analog_level(analog_level));
2070
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002071 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002072
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002073 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002074 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2075 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002076
2077 max_output_average += MaxAudioFrame(*frame_);
2078
2079 if (apm_->echo_cancellation()->stream_has_echo()) {
2080 has_echo_count++;
2081 }
2082
2083 analog_level = apm_->gain_control()->stream_analog_level();
2084 analog_level_average += analog_level;
2085 if (apm_->gain_control()->stream_is_saturated()) {
2086 is_saturated_count++;
2087 }
2088 if (apm_->voice_detection()->stream_has_voice()) {
2089 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002090 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002091 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002092 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002093 }
2094
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002095 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2096
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002097 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002098 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002099 sizeof(int16_t),
2100 frame_size,
2101 out_file_);
2102 ASSERT_EQ(frame_size, write_count);
2103
2104 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002105 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002106 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002107
2108#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2109 const int kStatsAggregationFrameNum = 100; // 1 second.
2110 if (frame_count % kStatsAggregationFrameNum == 0) {
2111 // Get echo metrics.
2112 EchoCancellation::Metrics echo_metrics;
2113 EXPECT_EQ(apm_->kNoError,
2114 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2115
2116 // Get delay metrics.
2117 int median = 0;
2118 int std = 0;
2119 float fraction_poor_delays = 0;
2120 EXPECT_EQ(apm_->kNoError,
2121 apm_->echo_cancellation()->GetDelayMetrics(
2122 &median, &std, &fraction_poor_delays));
2123
2124 // Get RMS.
2125 int rms_level = apm_->level_estimator()->RMS();
2126 EXPECT_LE(0, rms_level);
2127 EXPECT_GE(127, rms_level);
2128
2129 if (!write_ref_data) {
2130 const audioproc::Test::EchoMetrics& reference =
2131 test->echo_metrics(stats_index);
2132 TestStats(echo_metrics.residual_echo_return_loss,
2133 reference.residual_echo_return_loss());
2134 TestStats(echo_metrics.echo_return_loss,
2135 reference.echo_return_loss());
2136 TestStats(echo_metrics.echo_return_loss_enhancement,
2137 reference.echo_return_loss_enhancement());
2138 TestStats(echo_metrics.a_nlp,
2139 reference.a_nlp());
2140 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2141 reference.divergent_filter_fraction());
2142
2143 const audioproc::Test::DelayMetrics& reference_delay =
2144 test->delay_metrics(stats_index);
2145 EXPECT_EQ(reference_delay.median(), median);
2146 EXPECT_EQ(reference_delay.std(), std);
2147 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2148 fraction_poor_delays);
2149
2150 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2151
2152 ++stats_index;
2153 } else {
2154 audioproc::Test::EchoMetrics* message =
2155 test->add_echo_metrics();
2156 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2157 message->mutable_residual_echo_return_loss());
2158 WriteStatsMessage(echo_metrics.echo_return_loss,
2159 message->mutable_echo_return_loss());
2160 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2161 message->mutable_echo_return_loss_enhancement());
2162 WriteStatsMessage(echo_metrics.a_nlp,
2163 message->mutable_a_nlp());
2164 message->set_divergent_filter_fraction(
2165 echo_metrics.divergent_filter_fraction);
2166
2167 audioproc::Test::DelayMetrics* message_delay =
2168 test->add_delay_metrics();
2169 message_delay->set_median(median);
2170 message_delay->set_std(std);
2171 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2172
2173 test->add_rms_level(rms_level);
2174 }
2175 }
2176#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002177 }
2178 max_output_average /= frame_count;
2179 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002180 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002181
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002182 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002183 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002184 // When running the test on a N7 we get a {2, 6} difference of
2185 // |has_voice_count| and |max_output_average| is up to 18 higher.
2186 // All numbers being consistently higher on N7 compare to ref_data.
2187 // TODO(bjornv): If we start getting more of these offsets on Android we
2188 // should consider a different approach. Either using one slack for all,
2189 // or generate a separate android reference.
2190#if defined(WEBRTC_ANDROID)
2191 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002192 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002193 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002194 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002195#else
2196 const int kHasVoiceCountOffset = 0;
2197 const int kHasVoiceCountNear = kIntNear;
2198 const int kMaxOutputAverageOffset = 0;
2199 const int kMaxOutputAverageNear = kIntNear;
2200#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002201 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002202 EXPECT_NEAR(test->has_voice_count(),
2203 has_voice_count - kHasVoiceCountOffset,
2204 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002205 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002206
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002207 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002208 EXPECT_NEAR(test->max_output_average(),
2209 max_output_average - kMaxOutputAverageOffset,
2210 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002211#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002212 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002213 EXPECT_NEAR(test->ns_speech_probability_average(),
2214 ns_speech_prob_average,
2215 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002216#endif
2217 } else {
2218 test->set_has_echo_count(has_echo_count);
2219 test->set_has_voice_count(has_voice_count);
2220 test->set_is_saturated_count(is_saturated_count);
2221
2222 test->set_analog_level_average(analog_level_average);
2223 test->set_max_output_average(max_output_average);
2224
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002225#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002226 EXPECT_LE(0.0f, ns_speech_prob_average);
2227 EXPECT_GE(1.0f, ns_speech_prob_average);
2228 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002229#endif
2230 }
2231
2232 rewind(far_file_);
2233 rewind(near_file_);
2234 }
2235
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002236 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002237 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002238 }
2239}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002240
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002241TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2242 struct ChannelFormat {
2243 AudioProcessing::ChannelLayout in_layout;
2244 AudioProcessing::ChannelLayout out_layout;
2245 };
2246 ChannelFormat cf[] = {
2247 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2248 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2249 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2250 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002251
Ivo Creusen62337e52018-01-09 14:17:33 +01002252 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002253 // Enable one component just to ensure some processing takes place.
2254 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002255 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002256 const int in_rate = 44100;
2257 const int out_rate = 48000;
2258 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2259 TotalChannelsFromLayout(cf[i].in_layout));
2260 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2261 ChannelsFromLayout(cf[i].out_layout));
2262
2263 // Run over a few chunks.
2264 for (int j = 0; j < 10; ++j) {
2265 EXPECT_NOERR(ap->ProcessStream(
2266 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002267 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002268 in_rate,
2269 cf[i].in_layout,
2270 out_rate,
2271 cf[i].out_layout,
2272 out_cb.channels()));
2273 }
2274 }
2275}
2276
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002277// Compares the reference and test arrays over a region around the expected
2278// delay. Finds the highest SNR in that region and adds the variance and squared
2279// error results to the supplied accumulators.
2280void UpdateBestSNR(const float* ref,
2281 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002282 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002283 int expected_delay,
2284 double* variance_acc,
2285 double* sq_error_acc) {
2286 double best_snr = std::numeric_limits<double>::min();
2287 double best_variance = 0;
2288 double best_sq_error = 0;
2289 // Search over a region of eight samples around the expected delay.
2290 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2291 ++delay) {
2292 double sq_error = 0;
2293 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002294 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002295 double error = test[i + delay] - ref[i];
2296 sq_error += error * error;
2297 variance += ref[i] * ref[i];
2298 }
2299
2300 if (sq_error == 0) {
2301 *variance_acc += variance;
2302 return;
2303 }
2304 double snr = variance / sq_error;
2305 if (snr > best_snr) {
2306 best_snr = snr;
2307 best_variance = variance;
2308 best_sq_error = sq_error;
2309 }
2310 }
2311
2312 *variance_acc += best_variance;
2313 *sq_error_acc += best_sq_error;
2314}
2315
2316// Used to test a multitude of sample rate and channel combinations. It works
2317// by first producing a set of reference files (in SetUpTestCase) that are
2318// assumed to be correct, as the used parameters are verified by other tests
2319// in this collection. Primarily the reference files are all produced at
2320// "native" rates which do not involve any resampling.
2321
2322// Each test pass produces an output file with a particular format. The output
2323// is matched against the reference file closest to its internal processing
2324// format. If necessary the output is resampled back to its process format.
2325// Due to the resampling distortion, we don't expect identical results, but
2326// enforce SNR thresholds which vary depending on the format. 0 is a special
2327// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002328typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002329class AudioProcessingTest
2330 : public testing::TestWithParam<AudioProcessingTestData> {
2331 public:
2332 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002333 : input_rate_(std::get<0>(GetParam())),
2334 output_rate_(std::get<1>(GetParam())),
2335 reverse_input_rate_(std::get<2>(GetParam())),
2336 reverse_output_rate_(std::get<3>(GetParam())),
2337 expected_snr_(std::get<4>(GetParam())),
2338 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002339
2340 virtual ~AudioProcessingTest() {}
2341
2342 static void SetUpTestCase() {
2343 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002344 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002345 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002346 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2347 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2348 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002349 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002350 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2351 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2352 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002353 }
2354 }
2355 }
2356 }
2357
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002358 void TearDown() {
2359 // Remove "out" files after each test.
2360 ClearTempOutFiles();
2361 }
2362
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002363 static void TearDownTestCase() {
2364 ClearTempFiles();
2365 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002366
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002367 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002368 // to a file specified with |output_file_prefix|. Both forward and reverse
2369 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002370 static void ProcessFormat(int input_rate,
2371 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002372 int reverse_input_rate,
2373 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002374 size_t num_input_channels,
2375 size_t num_output_channels,
2376 size_t num_reverse_input_channels,
2377 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002378 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002379 Config config;
2380 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002381 std::unique_ptr<AudioProcessing> ap(
2382 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002383 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002384
ekmeyerson60d9b332015-08-14 10:35:55 -07002385 ProcessingConfig processing_config = {
2386 {{input_rate, num_input_channels},
2387 {output_rate, num_output_channels},
2388 {reverse_input_rate, num_reverse_input_channels},
2389 {reverse_output_rate, num_reverse_output_channels}}};
2390 ap->Initialize(processing_config);
2391
2392 FILE* far_file =
2393 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002394 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002395 FILE* out_file =
2396 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2397 reverse_input_rate, reverse_output_rate,
2398 num_input_channels, num_output_channels,
2399 num_reverse_input_channels,
2400 num_reverse_output_channels, kForward).c_str(),
2401 "wb");
2402 FILE* rev_out_file =
2403 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2404 reverse_input_rate, reverse_output_rate,
2405 num_input_channels, num_output_channels,
2406 num_reverse_input_channels,
2407 num_reverse_output_channels, kReverse).c_str(),
2408 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409 ASSERT_TRUE(far_file != NULL);
2410 ASSERT_TRUE(near_file != NULL);
2411 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002412 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002413
2414 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2415 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002416 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2417 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002418 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2419 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002420 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2421 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002422
2423 // Temporary buffers.
2424 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002425 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2426 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002427 std::unique_ptr<float[]> float_data(new float[max_length]);
2428 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429
2430 int analog_level = 127;
2431 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2432 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002433 EXPECT_NOERR(ap->ProcessReverseStream(
2434 rev_cb.channels(), processing_config.reverse_input_stream(),
2435 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002436
2437 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2438 ap->echo_cancellation()->set_stream_drift_samples(0);
2439 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2440
2441 EXPECT_NOERR(ap->ProcessStream(
2442 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002443 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002444 input_rate,
2445 LayoutFromChannels(num_input_channels),
2446 output_rate,
2447 LayoutFromChannels(num_output_channels),
2448 out_cb.channels()));
2449
ekmeyerson60d9b332015-08-14 10:35:55 -07002450 // Dump forward output to file.
2451 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002452 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002453 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002454
pkasting25702cb2016-01-08 13:50:27 -08002455 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002456 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002457 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002458
ekmeyerson60d9b332015-08-14 10:35:55 -07002459 // Dump reverse output to file.
2460 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2461 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002462 size_t rev_out_length =
2463 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002464
pkasting25702cb2016-01-08 13:50:27 -08002465 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002466 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2467 rev_out_file));
2468
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002469 analog_level = ap->gain_control()->stream_analog_level();
2470 }
2471 fclose(far_file);
2472 fclose(near_file);
2473 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002475 }
2476
2477 protected:
2478 int input_rate_;
2479 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002480 int reverse_input_rate_;
2481 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002482 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002484};
2485
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002486TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002487 struct ChannelFormat {
2488 int num_input;
2489 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002490 int num_reverse_input;
2491 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002492 };
2493 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002494 {1, 1, 1, 1},
2495 {1, 1, 2, 1},
2496 {2, 1, 1, 1},
2497 {2, 1, 2, 1},
2498 {2, 2, 1, 1},
2499 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002500 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002501
pkasting25702cb2016-01-08 13:50:27 -08002502 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002503 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2504 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2505 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002506
ekmeyerson60d9b332015-08-14 10:35:55 -07002507 // Verify output for both directions.
2508 std::vector<StreamDirection> stream_directions;
2509 stream_directions.push_back(kForward);
2510 stream_directions.push_back(kReverse);
2511 for (StreamDirection file_direction : stream_directions) {
2512 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2513 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2514 const int out_num =
2515 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2516 const double expected_snr =
2517 file_direction ? expected_reverse_snr_ : expected_snr_;
2518
2519 const int min_ref_rate = std::min(in_rate, out_rate);
2520 int ref_rate;
2521
2522 if (min_ref_rate > 32000) {
2523 ref_rate = 48000;
2524 } else if (min_ref_rate > 16000) {
2525 ref_rate = 32000;
2526 } else if (min_ref_rate > 8000) {
2527 ref_rate = 16000;
2528 } else {
2529 ref_rate = 8000;
2530 }
aluebs776593b2016-03-15 14:04:58 -07002531#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002532 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002533 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002534 }
2535#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002536 FILE* out_file = fopen(
2537 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2538 reverse_output_rate_, cf[i].num_input,
2539 cf[i].num_output, cf[i].num_reverse_input,
2540 cf[i].num_reverse_output, file_direction).c_str(),
2541 "rb");
2542 // The reference files always have matching input and output channels.
2543 FILE* ref_file = fopen(
2544 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2545 cf[i].num_output, cf[i].num_output,
2546 cf[i].num_reverse_output, cf[i].num_reverse_output,
2547 file_direction).c_str(),
2548 "rb");
2549 ASSERT_TRUE(out_file != NULL);
2550 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002551
pkasting25702cb2016-01-08 13:50:27 -08002552 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2553 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002554 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002555 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002556 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002557 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002558 // Data from the resampled output, in case the reference and output rates
2559 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002560 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002561
ekmeyerson60d9b332015-08-14 10:35:55 -07002562 PushResampler<float> resampler;
2563 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002564
ekmeyerson60d9b332015-08-14 10:35:55 -07002565 // Compute the resampling delay of the output relative to the reference,
2566 // to find the region over which we should search for the best SNR.
2567 float expected_delay_sec = 0;
2568 if (in_rate != ref_rate) {
2569 // Input resampling delay.
2570 expected_delay_sec +=
2571 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2572 }
2573 if (out_rate != ref_rate) {
2574 // Output resampling delay.
2575 expected_delay_sec +=
2576 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2577 // Delay of converting the output back to its processing rate for
2578 // testing.
2579 expected_delay_sec +=
2580 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2581 }
2582 int expected_delay =
2583 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002584
ekmeyerson60d9b332015-08-14 10:35:55 -07002585 double variance = 0;
2586 double sq_error = 0;
2587 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2588 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2589 float* out_ptr = out_data.get();
2590 if (out_rate != ref_rate) {
2591 // Resample the output back to its internal processing rate if
2592 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002593 ASSERT_EQ(ref_length,
2594 static_cast<size_t>(resampler.Resample(
2595 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002596 out_ptr = cmp_data.get();
2597 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002598
ekmeyerson60d9b332015-08-14 10:35:55 -07002599 // Update the |sq_error| and |variance| accumulators with the highest
2600 // SNR of reference vs output.
2601 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2602 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002603 }
2604
ekmeyerson60d9b332015-08-14 10:35:55 -07002605 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2606 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2607 << cf[i].num_input << ", " << cf[i].num_output << ", "
2608 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2609 << ", " << file_direction << "): ";
2610 if (sq_error > 0) {
2611 double snr = 10 * log10(variance / sq_error);
2612 EXPECT_GE(snr, expected_snr);
2613 EXPECT_NE(0, expected_snr);
2614 std::cout << "SNR=" << snr << " dB" << std::endl;
2615 } else {
aluebs776593b2016-03-15 14:04:58 -07002616 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002617 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002618
ekmeyerson60d9b332015-08-14 10:35:55 -07002619 fclose(out_file);
2620 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002621 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002622 }
2623}
2624
2625#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2626INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002627 CommonFormats,
2628 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002629 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2630 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2631 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2632 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2633 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2634 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2635 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2636 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2637 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2638 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2639 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2640 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002641
Edward Lemurc5ee9872017-10-23 23:33:04 +02002642 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2643 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2644 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2645 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2646 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2647 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2648 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2649 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2650 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2651 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2652 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2653 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002654
Edward Lemurc5ee9872017-10-23 23:33:04 +02002655 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2656 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2657 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2658 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2659 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2660 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2661 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2662 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2663 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2664 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2665 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2666 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002667
Edward Lemurc5ee9872017-10-23 23:33:04 +02002668 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2669 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2670 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2671 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2672 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2673 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2674 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2675 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2676 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2677 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2678 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2679 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002680
2681#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2682INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002683 CommonFormats,
2684 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002685 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2686 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2687 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2688 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2689 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2690 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2691 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2692 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2693 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2694 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2695 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2696 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002697
Edward Lemurc5ee9872017-10-23 23:33:04 +02002698 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2699 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2700 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2701 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2702 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2703 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2704 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2705 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2706 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2707 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2708 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2709 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002710
Edward Lemurc5ee9872017-10-23 23:33:04 +02002711 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2712 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2713 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2714 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2715 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2716 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2717 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2718 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2719 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2720 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2721 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2722 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002723
Edward Lemurc5ee9872017-10-23 23:33:04 +02002724 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2725 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2726 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2727 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2728 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2729 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2730 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2731 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2732 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2733 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2734 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2735 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002736#endif
2737
niklase@google.com470e71d2011-07-07 08:21:25 +00002738} // namespace
peahc19f3122016-10-07 14:54:10 -07002739
Alessio Bazzicac054e782018-04-16 12:10:09 +02002740TEST(RuntimeSettingTest, TestDefaultCtor) {
2741 auto s = AudioProcessing::RuntimeSetting();
2742 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2743}
2744
2745TEST(RuntimeSettingTest, TestCapturePreGain) {
2746 using Type = AudioProcessing::RuntimeSetting::Type;
2747 {
2748 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2749 EXPECT_EQ(Type::kCapturePreGain, s.type());
2750 float v;
2751 s.GetFloat(&v);
2752 EXPECT_EQ(1.25f, v);
2753 }
2754
2755#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2756 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2757#endif
2758}
2759
2760TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2761 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2762 auto s = AudioProcessing::RuntimeSetting();
2763 ASSERT_TRUE(q.Insert(&s));
2764 ASSERT_TRUE(q.Remove(&s));
2765 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2766}
2767
Sam Zackrisson0beac582017-09-25 12:04:02 +02002768TEST(ApmConfiguration, EnablePostProcessing) {
2769 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002770 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002771 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002772 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002773 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002774 rtc::scoped_refptr<AudioProcessing> apm =
2775 AudioProcessingBuilder()
2776 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002777 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002778
2779 AudioFrame audio;
2780 audio.num_channels_ = 1;
2781 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2782
2783 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002784 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002785}
2786
Alex Loiko5825aa62017-12-18 16:02:40 +01002787TEST(ApmConfiguration, EnablePreProcessing) {
2788 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002789 auto mock_pre_processor_ptr =
2790 new testing::NiceMock<test::MockCustomProcessing>();
2791 auto mock_pre_processor =
2792 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002793 rtc::scoped_refptr<AudioProcessing> apm =
2794 AudioProcessingBuilder()
2795 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002796 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002797
2798 AudioFrame audio;
2799 audio.num_channels_ = 1;
2800 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2801
2802 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2803 apm->ProcessReverseStream(&audio);
2804}
2805
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002806TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2807 // Verify that apm uses a capture analyzer if one is provided.
2808 auto mock_capture_analyzer_ptr =
2809 new testing::NiceMock<test::MockCustomAudioAnalyzer>();
2810 auto mock_capture_analyzer =
2811 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2812 rtc::scoped_refptr<AudioProcessing> apm =
2813 AudioProcessingBuilder()
2814 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2815 .Create();
2816
2817 AudioFrame audio;
2818 audio.num_channels_ = 1;
2819 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2820
2821 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(testing::_)).Times(1);
2822 apm->ProcessStream(&audio);
2823}
2824
Alex Loiko73ec0192018-05-15 10:52:28 +02002825TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2826 auto mock_pre_processor_ptr =
2827 new testing::NiceMock<test::MockCustomProcessing>();
2828 auto mock_pre_processor =
2829 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2830 rtc::scoped_refptr<AudioProcessing> apm =
2831 AudioProcessingBuilder()
2832 .SetRenderPreProcessing(std::move(mock_pre_processor))
2833 .Create();
2834 apm->SetRuntimeSetting(
2835 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2836
2837 // RuntimeSettings forwarded during 'Process*Stream' calls.
2838 // Therefore we have to make one such call.
2839 AudioFrame audio;
2840 audio.num_channels_ = 1;
2841 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2842
2843 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2844 apm->ProcessReverseStream(&audio);
2845}
2846
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002847class MyEchoControlFactory : public EchoControlFactory {
2848 public:
2849 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2850 auto ec = new test::MockEchoControl();
2851 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2852 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2853 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2854 return std::unique_ptr<EchoControl>(ec);
2855 }
2856};
2857
2858TEST(ApmConfiguration, EchoControlInjection) {
2859 // Verify that apm uses an injected echo controller if one is provided.
2860 webrtc::Config webrtc_config;
2861 std::unique_ptr<EchoControlFactory> echo_control_factory(
2862 new MyEchoControlFactory());
2863
Alex Loiko5825aa62017-12-18 16:02:40 +01002864 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002865 AudioProcessingBuilder()
2866 .SetEchoControlFactory(std::move(echo_control_factory))
2867 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002868
2869 AudioFrame audio;
2870 audio.num_channels_ = 1;
2871 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2872 apm->ProcessStream(&audio);
2873 apm->ProcessReverseStream(&audio);
2874 apm->ProcessStream(&audio);
2875}
Ivo Creusenae026092017-11-20 13:07:16 +01002876
2877std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2878 Config old_config;
2879 if (use_AEC2) {
2880 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2881 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2882 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002883 std::unique_ptr<AudioProcessing> apm(
2884 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002885 if (!apm) {
2886 return apm;
2887 }
2888
2889 ProcessingConfig processing_config = {
2890 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2891
2892 if (apm->Initialize(processing_config) != 0) {
2893 return nullptr;
2894 }
2895
2896 // Disable all components except for an AEC and the residual echo detector.
2897 AudioProcessing::Config config;
2898 config.residual_echo_detector.enabled = true;
Ivo Creusenae026092017-11-20 13:07:16 +01002899 config.high_pass_filter.enabled = false;
2900 config.gain_controller2.enabled = false;
Ivo Creusenae026092017-11-20 13:07:16 +01002901 apm->ApplyConfig(config);
2902 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2903 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2904 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2905 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
2906
2907 if (use_AEC2) {
2908 EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
2909 EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
2910 EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
2911 EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
2912 } else {
2913 EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
2914 EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
2915 }
2916 return apm;
2917}
2918
2919#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2920#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2921#else
2922#define MAYBE_ApmStatistics ApmStatistics
2923#endif
2924
2925TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2926 // Set up APM with AEC2 and process some audio.
2927 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2928 ASSERT_TRUE(apm);
2929
2930 // Set up an audioframe.
2931 AudioFrame frame;
2932 frame.num_channels_ = 1;
2933 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2934
2935 // Fill the audio frame with a sawtooth pattern.
2936 int16_t* ptr = frame.mutable_data();
2937 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2938 ptr[i] = 10000 * ((i % 3) - 1);
2939 }
2940
2941 // Do some processing.
2942 for (int i = 0; i < 200; i++) {
2943 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2944 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2945 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2946 }
2947
2948 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002949 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002950 // We expect all statistics to be set and have a sensible value.
2951 ASSERT_TRUE(stats.residual_echo_likelihood);
2952 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2953 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2954 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2955 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2956 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2957 ASSERT_TRUE(stats.echo_return_loss);
2958 EXPECT_NE(*stats.echo_return_loss, -100.0);
2959 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2960 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2961 ASSERT_TRUE(stats.divergent_filter_fraction);
2962 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2963 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2964 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2965 // We don't check stats.delay_median_ms since it takes too long to settle to a
2966 // value. At least 20 seconds of data need to be processed before it will get
2967 // a value, which would make this test take too much time.
2968
2969 // If there are no receive streams, we expect the stats not to be set. The
2970 // 'false' argument signals to APM that no receive streams are currently
2971 // active. In that situation the statistics would get stuck at their last
2972 // calculated value (AEC and echo detection need at least one stream in each
2973 // direction), so to avoid that, they should not be set by APM.
2974 stats = apm->GetStatistics(false);
2975 EXPECT_FALSE(stats.residual_echo_likelihood);
2976 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2977 EXPECT_FALSE(stats.echo_return_loss);
2978 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2979 EXPECT_FALSE(stats.divergent_filter_fraction);
2980 EXPECT_FALSE(stats.delay_median_ms);
2981 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2982}
2983
2984TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2985 // Set up APM with AECM and process some audio.
2986 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2987 ASSERT_TRUE(apm);
2988
2989 // Set up an audioframe.
2990 AudioFrame frame;
2991 frame.num_channels_ = 1;
2992 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2993
2994 // Fill the audio frame with a sawtooth pattern.
2995 int16_t* ptr = frame.mutable_data();
2996 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2997 ptr[i] = 10000 * ((i % 3) - 1);
2998 }
2999
3000 // Do some processing.
3001 for (int i = 0; i < 200; i++) {
3002 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
3003 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
3004 EXPECT_EQ(apm->ProcessStream(&frame), 0);
3005 }
3006
3007 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01003008 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01003009 // We expect only the residual echo detector statistics to be set and have a
3010 // sensible value.
3011 EXPECT_TRUE(stats.residual_echo_likelihood);
3012 if (stats.residual_echo_likelihood) {
3013 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
3014 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
3015 }
3016 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
3017 if (stats.residual_echo_likelihood_recent_max) {
3018 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
3019 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
3020 }
3021 EXPECT_FALSE(stats.echo_return_loss);
3022 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3023 EXPECT_FALSE(stats.divergent_filter_fraction);
3024 EXPECT_FALSE(stats.delay_median_ms);
3025 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3026
3027 // If there are no receive streams, we expect the stats not to be set.
3028 stats = apm->GetStatistics(false);
3029 EXPECT_FALSE(stats.residual_echo_likelihood);
3030 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
3031 EXPECT_FALSE(stats.echo_return_loss);
3032 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3033 EXPECT_FALSE(stats.divergent_filter_fraction);
3034 EXPECT_FALSE(stats.delay_median_ms);
3035 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3036}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003037} // namespace webrtc