blob: 346c2a2c40faa5edc19d7fd9852e30a00060b5ab [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/common.h"
25#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020026#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/test/protobuf_utils.h"
28#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/arraysize.h"
30#include "rtc_base/checks.h"
Minyue Li656d6092018-08-10 15:38:52 +020031#include "rtc_base/fakeclock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/gtest_prod_util.h"
33#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Niels Möller84255bb2017-10-06 13:43:23 +020037#include "rtc_base/refcountedobject.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020038#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020039#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/task_queue.h"
41#include "rtc_base/thread.h"
42#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "test/gtest.h"
44#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070045
46RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000048#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#endif
kwiberg77eab702016-09-28 17:42:01 -070052RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000053
andrew@webrtc.org27c69802014-02-18 20:24:56 +000054namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000055namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000056
ekmeyerson60d9b332015-08-14 10:35:55 -070057// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
58// applicable.
59
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000060// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000061// re-implemented; see comment at the bottom of this file. For now, the user has
62// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000063// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000064// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000065// be set to true with the command-line switch --write_ref_data.
66bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070067const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070068const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000069
aluebseb3603b2016-04-20 15:27:58 -070070#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
71// Android doesn't support 48kHz.
72const int kProcessSampleRates[] = {8000, 16000, 32000};
73#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070074const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070075#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000076
ekmeyerson60d9b332015-08-14 10:35:55 -070077enum StreamDirection { kForward = 0, kReverse };
78
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 cb->num_channels());
82 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000084 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080086 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 S16ToFloat(cb_int.channels()[i],
88 cb->num_frames(),
89 cb->channels()[i]);
90 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000091}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000092
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070094 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095}
96
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080098size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000099 switch (layout) {
100 case AudioProcessing::kMono:
101 return 1;
102 case AudioProcessing::kMonoAndKeyboard:
103 case AudioProcessing::kStereo:
104 return 2;
105 case AudioProcessing::kStereoAndKeyboard:
106 return 3;
107 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700108 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800109 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000110}
111
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000112int TruncateToMultipleOf10(int value) {
113 return (value / 10) * 10;
114}
115
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800117 size_t samples_per_channel) {
118 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000120}
121
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800123 size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000125 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
126}
127
pkasting25702cb2016-01-08 13:50:27 -0800128void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
129 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000130 stereo[i * 2 + 1] = stereo[i * 2];
131 }
132}
133
yujo36b1a5f2017-06-12 12:45:32 -0700134void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800135 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000136 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
137 }
138}
139
140void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700141 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
143 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700144 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000145 }
146}
147
148void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800149 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700150 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700152 frame_data[i] = left;
153 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000154 }
155}
156
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000157void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700158 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
160 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700161 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000162 }
163}
164
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
yujo36b1a5f2017-06-12 12:45:32 -0700172 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000173 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000174 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000175 return false;
176 }
177 return true;
178}
179
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000180void EnableAllAPComponents(AudioProcessing* ap) {
181#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000182 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000183
184 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
185 EXPECT_NOERR(ap->gain_control()->Enable(true));
186#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
187 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
188 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
189 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
Sam Zackrissonc4deaaa2018-08-16 11:42:27 +0200190 EXPECT_NOERR(ap->echo_cancellation()->set_suppression_level(
191 EchoCancellation::SuppressionLevel::kModerateSuppression));
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000192 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000193
194 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
195 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
196 EXPECT_NOERR(ap->gain_control()->Enable(true));
197#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000198
199 AudioProcessing::Config apm_config;
peah8271d042016-11-22 07:24:52 -0800200 apm_config.high_pass_filter.enabled = true;
201 ap->ApplyConfig(apm_config);
202
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000203 EXPECT_NOERR(ap->level_estimator()->Enable(true));
204 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
205
206 EXPECT_NOERR(ap->voice_detection()->Enable(true));
207}
208
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000209// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000210template <class T>
211T AbsValue(T a) {
212 return a > 0 ? a: -a;
213}
214
215int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800216 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700217 const int16_t* frame_data = frame.data();
218 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800219 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700220 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000221 }
222
223 return max_data;
224}
225
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000226#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000227void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000228 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700229 EXPECT_EQ(reference.instant(), test.instant);
230 EXPECT_EQ(reference.average(), test.average);
231 EXPECT_EQ(reference.maximum(), test.maximum);
232 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000233}
234
235void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 audioproc::Test::Statistic* msg) {
237 msg->set_instant(output.instant);
238 msg->set_average(output.average);
239 msg->set_maximum(output.maximum);
240 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000242#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243
Alex Loiko890988c2017-08-31 10:25:48 +0200244void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700245 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000246 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000247 ASSERT_TRUE(file != NULL);
248
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100249 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000250 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800251 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000253
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000254 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000255 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000256 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000257 fclose(file);
258}
259
Alex Loiko890988c2017-08-31 10:25:48 +0200260std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000261 std::ostringstream ss;
262 // Resource files are all stereo.
263 ss << name << sample_rate_hz / 1000 << "_stereo";
264 return test::ResourcePath(ss.str(), "pcm");
265}
266
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000267// Temporary filenames unique to this process. Used to be able to run these
268// tests in parallel as each process needs to be running in isolation they can't
269// have competing filenames.
270std::map<std::string, std::string> temp_filenames;
271
Alex Loiko890988c2017-08-31 10:25:48 +0200272std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000273 int input_rate,
274 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700275 int reverse_input_rate,
276 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800277 size_t num_input_channels,
278 size_t num_output_channels,
279 size_t num_reverse_input_channels,
280 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700281 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700283 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
284 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000285 if (num_output_channels == 1) {
286 ss << "mono";
287 } else if (num_output_channels == 2) {
288 ss << "stereo";
289 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700290 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000291 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700292 ss << output_rate / 1000;
293 if (num_reverse_output_channels == 1) {
294 ss << "_rmono";
295 } else if (num_reverse_output_channels == 2) {
296 ss << "_rstereo";
297 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700298 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700299 }
300 ss << reverse_output_rate / 1000;
301 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000302
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000303 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700304 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000305 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
306 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000307}
308
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000309void ClearTempFiles() {
310 for (auto& kv : temp_filenames)
311 remove(kv.second.c_str());
312}
313
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200314// Only remove "out" files. Keep "ref" files.
315void ClearTempOutFiles() {
316 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
317 const std::string& filename = it->first;
318 if (filename.substr(0, 3).compare("out") == 0) {
319 remove(it->second.c_str());
320 temp_filenames.erase(it++);
321 } else {
322 it++;
323 }
324 }
325}
326
Alex Loiko890988c2017-08-31 10:25:48 +0200327void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000328 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000329 ASSERT_TRUE(file != NULL);
330 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000331 fclose(file);
332}
333
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
335// stereo) file, converts to deinterleaved float (optionally downmixing) and
336// returns the result in |cb|. Returns false if the file ended (or on error) and
337// true otherwise.
338//
339// |int_data| and |float_data| are just temporary space that must be
340// sufficiently large to hold the 10 ms chunk.
341bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
342 ChannelBuffer<float>* cb) {
343 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000344 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000345 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
346 if (read_count != frame_size) {
347 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700348 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000349 return false; // This is expected.
350 }
351
352 S16ToFloat(int_data, frame_size, float_data);
353 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000354 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000355 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000356 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000357 cb->channels());
358 }
359
360 return true;
361}
362
niklase@google.com470e71d2011-07-07 08:21:25 +0000363class ApmTest : public ::testing::Test {
364 protected:
365 ApmTest();
366 virtual void SetUp();
367 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000368
369 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000370 }
371
372 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000373 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000374 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000375
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000376 // Used to select between int and float interface tests.
377 enum Format {
378 kIntFormat,
379 kFloatFormat
380 };
381
382 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000383 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000384 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800385 size_t num_input_channels,
386 size_t num_output_channels,
387 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000388 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000389 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000390 void EnableAllComponents();
391 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000392 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000393 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000394 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
395 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000396 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000397 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
398 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700399 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800400 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700401 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800402 void TestChangingForwardChannels(size_t num_in_channels,
403 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700404 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800405 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700406 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000407 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
408 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000409 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000410 int ProcessStreamChooser(Format format);
411 int AnalyzeReverseStreamChooser(Format format);
412 void ProcessDebugDump(const std::string& in_filename,
413 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800414 Format format,
415 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000416 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000417
418 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000419 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800420 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000421 AudioFrame* frame_;
422 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800423 std::unique_ptr<ChannelBuffer<float> > float_cb_;
424 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000425 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800426 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 FILE* far_file_;
428 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000429 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000430};
431
432ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000433 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000434#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800435 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
436 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000437#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000438#if defined(WEBRTC_MAC)
439 // A different file for Mac is needed because on this platform the AEC
440 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800441 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
442 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000443#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800444 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
445 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000446#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000447#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000448 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000449 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000450 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000451 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000452 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000453 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000454 out_file_(NULL) {
455 Config config;
456 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100457 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000458}
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
460void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000461 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
463 frame_ = new AudioFrame();
464 revframe_ = new AudioFrame();
465
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000466 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000467}
468
469void ApmTest::TearDown() {
470 if (frame_) {
471 delete frame_;
472 }
473 frame_ = NULL;
474
475 if (revframe_) {
476 delete revframe_;
477 }
478 revframe_ = NULL;
479
480 if (far_file_) {
481 ASSERT_EQ(0, fclose(far_file_));
482 }
483 far_file_ = NULL;
484
485 if (near_file_) {
486 ASSERT_EQ(0, fclose(near_file_));
487 }
488 near_file_ = NULL;
489
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000490 if (out_file_) {
491 ASSERT_EQ(0, fclose(out_file_));
492 }
493 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494}
495
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000496void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000497 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700498 ap->Initialize(
499 {{{frame_->sample_rate_hz_, frame_->num_channels_},
500 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700501 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700502 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000503}
504
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000505void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000506 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000507 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800508 size_t num_input_channels,
509 size_t num_output_channels,
510 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000511 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000512 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000513 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000514 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000515
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000516 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
517 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000519
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000520 if (far_file_) {
521 ASSERT_EQ(0, fclose(far_file_));
522 }
523 std::string filename = ResourceFilePath("far", sample_rate_hz);
524 far_file_ = fopen(filename.c_str(), "rb");
525 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
526 filename << "\n";
527
528 if (near_file_) {
529 ASSERT_EQ(0, fclose(near_file_));
530 }
531 filename = ResourceFilePath("near", sample_rate_hz);
532 near_file_ = fopen(filename.c_str(), "rb");
533 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
534 filename << "\n";
535
536 if (open_output_file) {
537 if (out_file_) {
538 ASSERT_EQ(0, fclose(out_file_));
539 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700540 filename = OutputFilePath(
541 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
542 reverse_sample_rate_hz, num_input_channels, num_output_channels,
543 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000544 out_file_ = fopen(filename.c_str(), "wb");
545 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
546 filename << "\n";
547 }
548}
549
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000550void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000552}
553
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
555 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000556 // The files always contain stereo audio.
557 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700558 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000559 sizeof(int16_t),
560 frame_size,
561 file);
562 if (read_count != frame_size) {
563 // Check that the file really ended.
564 EXPECT_NE(0, feof(file));
565 return false; // This is expected.
566 }
567
568 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700569 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000570 frame->samples_per_channel_);
571 }
572
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000574 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000576 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000577}
578
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000579bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
580 return ReadFrame(file, frame, NULL);
581}
582
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000583// If the end of the file has been reached, rewind it and attempt to read the
584// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000585void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
586 ChannelBuffer<float>* cb) {
587 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000588 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000589 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000590 }
591}
592
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
594 ReadFrameWithRewind(file, frame, NULL);
595}
596
andrew@webrtc.org81865342012-10-27 00:28:27 +0000597void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
598 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000599 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000600 EXPECT_EQ(apm_->kNoError,
601 apm_->gain_control()->set_stream_analog_level(127));
602 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000603}
604
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000605int ApmTest::ProcessStreamChooser(Format format) {
606 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000607 return apm_->ProcessStream(frame_);
608 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000609 return apm_->ProcessStream(float_cb_->channels(),
610 frame_->samples_per_channel_,
611 frame_->sample_rate_hz_,
612 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000613 output_sample_rate_hz_,
614 LayoutFromChannels(num_output_channels_),
615 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000616}
617
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000618int ApmTest::AnalyzeReverseStreamChooser(Format format) {
619 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700620 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000621 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000622 return apm_->AnalyzeReverseStream(
623 revfloat_cb_->channels(),
624 revframe_->samples_per_channel_,
625 revframe_->sample_rate_hz_,
626 LayoutFromChannels(revframe_->num_channels_));
627}
628
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000629void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
630 int delay_min, int delay_max) {
631 // The |revframe_| and |frame_| should include the proper frame information,
632 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000633 AudioFrame tmp_frame;
634 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000635 bool causal = true;
636
637 tmp_frame.CopyFrom(*revframe_);
638 SetFrameTo(&tmp_frame, 0);
639
640 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
641 // Initialize the |frame_queue| with empty frames.
642 int frame_delay = delay_ms / 10;
643 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000644 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000645 frame->CopyFrom(tmp_frame);
646 frame_queue.push(frame);
647 frame_delay++;
648 causal = false;
649 }
650 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000651 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000652 frame->CopyFrom(tmp_frame);
653 frame_queue.push(frame);
654 frame_delay--;
655 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000656 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
657 // need enough frames with audio to have reliable estimates, but as few as
658 // possible to keep processing time down. 4.5 seconds seemed to be a good
659 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000660 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000661 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 frame->CopyFrom(tmp_frame);
663 // Use the near end recording, since that has more speech in it.
664 ASSERT_TRUE(ReadFrame(near_file_, frame));
665 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000666 AudioFrame* reverse_frame = frame;
667 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000668 if (!causal) {
669 reverse_frame = frame_queue.front();
670 // When we call ProcessStream() the frame is modified, so we can't use the
671 // pointer directly when things are non-causal. Use an intermediate frame
672 // and copy the data.
673 process_frame = &tmp_frame;
674 process_frame->CopyFrom(*frame);
675 }
aluebsb0319552016-03-17 20:39:53 -0700676 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000677 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
678 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
679 frame = frame_queue.front();
680 frame_queue.pop();
681 delete frame;
682
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000683 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000684 int median;
685 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000686 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000687 // Discard the first delay metrics to avoid convergence effects.
688 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000689 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
690 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000691 }
692 }
693
694 rewind(near_file_);
695 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000696 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000697 frame_queue.pop();
698 delete frame;
699 }
700 // Calculate expected delay estimate and acceptable regions. Further,
701 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700702 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700703 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700704 const int expected_median =
705 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
706 const int expected_median_high = rtc::SafeClamp<int>(
707 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700708 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700709 const int expected_median_low = rtc::SafeClamp<int>(
710 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700711 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000712 // Verify delay metrics.
713 int median;
714 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000715 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000716 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000717 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
718 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000719 EXPECT_GE(expected_median_high, median);
720 EXPECT_LE(expected_median_low, median);
721}
722
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000725 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000727 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000731
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000732 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 EXPECT_EQ(apm_->kNoError,
734 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000735 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000737 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000738
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000739 // Other stream parameters set correctly.
740 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 EXPECT_EQ(apm_->kNoError,
742 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000744 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000745 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000746 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000747 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
748 EXPECT_EQ(apm_->kNoError,
749 apm_->echo_cancellation()->enable_drift_compensation(false));
750
751 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000752 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000753 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000754 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000756
757 // Resets after successful ProcessStream().
758 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000759 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000760 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000761 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000762
763 // Other stream parameters set correctly.
764 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
765 EXPECT_EQ(apm_->kNoError,
766 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000767 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000768 EXPECT_EQ(apm_->kNoError,
769 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000770 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000771 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000772 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
773
774 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000775 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000776 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000777
778 // Resets after successful ProcessStream().
779 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000780 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000782 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000783 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000784
785 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
787 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
788 EXPECT_EQ(apm_->kNoError,
789 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000790 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000791 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000792
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000793 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000794 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000795 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000796 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000797 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000798
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000799 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000800 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000801 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 EXPECT_EQ(apm_->kNoError,
803 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000804 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000805}
806
807TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000808 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000809}
810
811TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000812 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000813}
814
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000815TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
816 EXPECT_EQ(0, apm_->delay_offset_ms());
817 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
818 EXPECT_EQ(50, apm_->stream_delay_ms());
819}
820
821TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
822 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000823 apm_->set_delay_offset_ms(100);
824 EXPECT_EQ(100, apm_->delay_offset_ms());
825 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000826 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000827 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
828 EXPECT_EQ(200, apm_->stream_delay_ms());
829
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000830 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000831 apm_->set_delay_offset_ms(-50);
832 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000833 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
834 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000835 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
836 EXPECT_EQ(50, apm_->stream_delay_ms());
837}
838
Michael Graczyk86c6d332015-07-23 11:41:39 -0700839void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800840 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700841 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000842 frame_->num_channels_ = num_channels;
843 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700844 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000845}
846
Michael Graczyk86c6d332015-07-23 11:41:39 -0700847void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800848 size_t num_in_channels,
849 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700850 AudioProcessing::Error expected_return) {
851 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
852 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
853
854 EXPECT_EQ(expected_return,
855 apm_->ProcessStream(float_cb_->channels(), input_stream,
856 output_stream, float_cb_->channels()));
857}
858
859void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800860 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700861 AudioProcessing::Error expected_return) {
862 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700863 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
864 {output_sample_rate_hz_, apm_->num_output_channels()},
865 {frame_->sample_rate_hz_, num_rev_channels},
866 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700867
ekmeyerson60d9b332015-08-14 10:35:55 -0700868 EXPECT_EQ(
869 expected_return,
870 apm_->ProcessReverseStream(
871 float_cb_->channels(), processing_config.reverse_input_stream(),
872 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700873}
874
875TEST_F(ApmTest, ChannelsInt16Interface) {
876 // Testing number of invalid and valid channels.
877 Init(16000, 16000, 16000, 4, 4, 4, false);
878
879 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
880
Peter Kasting69558702016-01-12 16:26:35 -0800881 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700882 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000883 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 }
885}
886
Michael Graczyk86c6d332015-07-23 11:41:39 -0700887TEST_F(ApmTest, Channels) {
888 // Testing number of invalid and valid channels.
889 Init(16000, 16000, 16000, 4, 4, 4, false);
890
891 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
892 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
893
Peter Kasting69558702016-01-12 16:26:35 -0800894 for (size_t i = 1; i < 4; ++i) {
895 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700896 // Output channels much be one or match input channels.
897 if (j == 1 || i == j) {
898 TestChangingForwardChannels(i, j, kNoErr);
899 TestChangingReverseChannels(i, kNoErr);
900
901 EXPECT_EQ(i, apm_->num_input_channels());
902 EXPECT_EQ(j, apm_->num_output_channels());
903 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800904 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700905 } else {
906 TestChangingForwardChannels(i, j,
907 AudioProcessing::kBadNumberChannelsError);
908 }
909 }
910 }
911}
912
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000913TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000915 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000916 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700918 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800919 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000920 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000921 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000922 }
923}
924
niklase@google.com470e71d2011-07-07 08:21:25 +0000925TEST_F(ApmTest, EchoCancellation) {
926 EXPECT_EQ(apm_->kNoError,
927 apm_->echo_cancellation()->enable_drift_compensation(true));
928 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
929 EXPECT_EQ(apm_->kNoError,
930 apm_->echo_cancellation()->enable_drift_compensation(false));
931 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
932
niklase@google.com470e71d2011-07-07 08:21:25 +0000933 EchoCancellation::SuppressionLevel level[] = {
934 EchoCancellation::kLowSuppression,
935 EchoCancellation::kModerateSuppression,
936 EchoCancellation::kHighSuppression,
937 };
pkasting25702cb2016-01-08 13:50:27 -0800938 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000939 EXPECT_EQ(apm_->kNoError,
940 apm_->echo_cancellation()->set_suppression_level(level[i]));
941 EXPECT_EQ(level[i],
942 apm_->echo_cancellation()->suppression_level());
943 }
944
945 EchoCancellation::Metrics metrics;
946 EXPECT_EQ(apm_->kNotEnabledError,
947 apm_->echo_cancellation()->GetMetrics(&metrics));
948
ivoc3e9a5372016-10-28 07:55:33 -0700949 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
950 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
951
niklase@google.com470e71d2011-07-07 08:21:25 +0000952 EXPECT_EQ(apm_->kNoError,
953 apm_->echo_cancellation()->enable_metrics(true));
954 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
955 EXPECT_EQ(apm_->kNoError,
956 apm_->echo_cancellation()->enable_metrics(false));
957 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
958
ivoc48dfab52016-10-28 03:29:31 -0700959 EXPECT_EQ(apm_->kNoError,
960 apm_->echo_cancellation()->enable_delay_logging(true));
961 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
962 EXPECT_EQ(apm_->kNoError,
963 apm_->echo_cancellation()->enable_delay_logging(false));
964 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
965
ivoc3e9a5372016-10-28 07:55:33 -0700966 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
967 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
968
969 int median = 0;
970 int std = 0;
971 float poor_fraction = 0;
972 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
973 &median, &std, &poor_fraction));
974
niklase@google.com470e71d2011-07-07 08:21:25 +0000975 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
976 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
977 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
978 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000979
980 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
981 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
982 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
983 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
984 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
985 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000986}
987
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000988TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000989 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000990 // Enable AEC only.
991 EXPECT_EQ(apm_->kNoError,
992 apm_->echo_cancellation()->enable_drift_compensation(false));
993 EXPECT_EQ(apm_->kNoError,
994 apm_->echo_cancellation()->enable_metrics(false));
995 EXPECT_EQ(apm_->kNoError,
996 apm_->echo_cancellation()->enable_delay_logging(true));
997 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000998 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700999 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +00001000 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001001
1002 // Internally in the AEC the amount of lookahead the delay estimation can
1003 // handle is 15 blocks and the maximum delay is set to 60 blocks.
1004 const int kLookaheadBlocks = 15;
1005 const int kMaxDelayBlocks = 60;
1006 // The AEC has a startup time before it actually starts to process. This
1007 // procedure can flush the internal far-end buffer, which of course affects
1008 // the delay estimation. Therefore, we set a system_delay high enough to
1009 // avoid that. The smallest system_delay you can report without flushing the
1010 // buffer is 66 ms in 8 kHz.
1011 //
1012 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1013 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1014 // delay estimation. This should be noted though. In case of test failure,
1015 // this could be the cause.
1016 const int kSystemDelayMs = 66;
1017 // Test a couple of corner cases and verify that the estimated delay is
1018 // within a valid region (set to +-1.5 blocks). Note that these cases are
1019 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001020 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001021 Init(kProcessSampleRates[i],
1022 kProcessSampleRates[i],
1023 kProcessSampleRates[i],
1024 2,
1025 2,
1026 2,
1027 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001028 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001029 const int num_ms_per_block =
1030 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001031 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1032 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1033
1034 // 1) Verify correct delay estimate at lookahead boundary.
1035 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1036 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1037 delay_max_ms);
1038 // 2) A delay less than maximum lookahead should give an delay estimate at
1039 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1040 delay_ms -= 20;
1041 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1042 delay_max_ms);
1043 // 3) Three values around zero delay. Note that we need to compensate for
1044 // the fake system_delay.
1045 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1046 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1047 delay_max_ms);
1048 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1049 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1050 delay_max_ms);
1051 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1052 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1053 delay_max_ms);
1054 // 4) Verify correct delay estimate at maximum delay boundary.
1055 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1056 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1057 delay_max_ms);
1058 // 5) A delay above the maximum delay should give an estimate at the
1059 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1060 delay_ms += 20;
1061 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1062 delay_max_ms);
1063 }
1064}
1065
niklase@google.com470e71d2011-07-07 08:21:25 +00001066TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001068 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001069 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1070 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1071
niklase@google.com470e71d2011-07-07 08:21:25 +00001072 // Toggle routing modes
1073 EchoControlMobile::RoutingMode mode[] = {
1074 EchoControlMobile::kQuietEarpieceOrHeadset,
1075 EchoControlMobile::kEarpiece,
1076 EchoControlMobile::kLoudEarpiece,
1077 EchoControlMobile::kSpeakerphone,
1078 EchoControlMobile::kLoudSpeakerphone,
1079 };
pkasting25702cb2016-01-08 13:50:27 -08001080 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001081 EXPECT_EQ(apm_->kNoError,
1082 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1083 EXPECT_EQ(mode[i],
1084 apm_->echo_control_mobile()->routing_mode());
1085 }
1086 // Turn comfort noise off/on
1087 EXPECT_EQ(apm_->kNoError,
1088 apm_->echo_control_mobile()->enable_comfort_noise(false));
1089 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1090 EXPECT_EQ(apm_->kNoError,
1091 apm_->echo_control_mobile()->enable_comfort_noise(true));
1092 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001093 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001094 const size_t echo_path_size =
1095 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001096 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1097 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001098 EXPECT_EQ(apm_->kNullPointerError,
1099 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1100 EXPECT_EQ(apm_->kNullPointerError,
1101 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1102 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001103 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001104 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001105 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001106 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001107 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001108 echo_path_in[i] = echo_path_out[i] + 1;
1109 }
1110 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001111 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001112 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001113 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1114 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001115 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001116 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1117 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001118 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001119 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1120 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001121
1122 // Process a few frames with NS in the default disabled state. This exercises
1123 // a different codepath than with it enabled.
1124 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1126 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1127 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1128
niklase@google.com470e71d2011-07-07 08:21:25 +00001129 // Turn AECM off
1130 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1131 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1132}
1133
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001134TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001135 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001136 EXPECT_EQ(apm_->kNoError,
1137 apm_->gain_control()->set_mode(
1138 apm_->gain_control()->mode()));
1139
1140 GainControl::Mode mode[] = {
1141 GainControl::kAdaptiveAnalog,
1142 GainControl::kAdaptiveDigital,
1143 GainControl::kFixedDigital
1144 };
pkasting25702cb2016-01-08 13:50:27 -08001145 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001146 EXPECT_EQ(apm_->kNoError,
1147 apm_->gain_control()->set_mode(mode[i]));
1148 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1149 }
1150 // Testing invalid target levels
1151 EXPECT_EQ(apm_->kBadParameterError,
1152 apm_->gain_control()->set_target_level_dbfs(-3));
1153 EXPECT_EQ(apm_->kBadParameterError,
1154 apm_->gain_control()->set_target_level_dbfs(-40));
1155 // Testing valid target levels
1156 EXPECT_EQ(apm_->kNoError,
1157 apm_->gain_control()->set_target_level_dbfs(
1158 apm_->gain_control()->target_level_dbfs()));
1159
1160 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001161 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 EXPECT_EQ(apm_->kNoError,
1163 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1164 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1165 }
1166
1167 // Testing invalid compression gains
1168 EXPECT_EQ(apm_->kBadParameterError,
1169 apm_->gain_control()->set_compression_gain_db(-1));
1170 EXPECT_EQ(apm_->kBadParameterError,
1171 apm_->gain_control()->set_compression_gain_db(100));
1172
1173 // Testing valid compression gains
1174 EXPECT_EQ(apm_->kNoError,
1175 apm_->gain_control()->set_compression_gain_db(
1176 apm_->gain_control()->compression_gain_db()));
1177
1178 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001179 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001180 EXPECT_EQ(apm_->kNoError,
1181 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1182 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1183 }
1184
1185 // Testing limiter off/on
1186 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1187 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1188 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1189 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1190
1191 // Testing invalid level limits
1192 EXPECT_EQ(apm_->kBadParameterError,
1193 apm_->gain_control()->set_analog_level_limits(-1, 512));
1194 EXPECT_EQ(apm_->kBadParameterError,
1195 apm_->gain_control()->set_analog_level_limits(100000, 512));
1196 EXPECT_EQ(apm_->kBadParameterError,
1197 apm_->gain_control()->set_analog_level_limits(512, -1));
1198 EXPECT_EQ(apm_->kBadParameterError,
1199 apm_->gain_control()->set_analog_level_limits(512, 100000));
1200 EXPECT_EQ(apm_->kBadParameterError,
1201 apm_->gain_control()->set_analog_level_limits(512, 255));
1202
1203 // Testing valid level limits
1204 EXPECT_EQ(apm_->kNoError,
1205 apm_->gain_control()->set_analog_level_limits(
1206 apm_->gain_control()->analog_level_minimum(),
1207 apm_->gain_control()->analog_level_maximum()));
1208
1209 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001210 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001211 EXPECT_EQ(apm_->kNoError,
1212 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1213 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1214 }
1215
1216 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001217 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001218 EXPECT_EQ(apm_->kNoError,
1219 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1220 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1221 }
1222
1223 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1224
1225 // Turn AGC off
1226 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1227 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1228}
1229
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001230void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001231 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001232 EXPECT_EQ(apm_->kNoError,
1233 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1234 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1235
1236 int out_analog_level = 0;
1237 for (int i = 0; i < 2000; ++i) {
1238 ReadFrameWithRewind(near_file_, frame_);
1239 // Ensure the audio is at a low level, so the AGC will try to increase it.
1240 ScaleFrame(frame_, 0.25);
1241
1242 // Always pass in the same volume.
1243 EXPECT_EQ(apm_->kNoError,
1244 apm_->gain_control()->set_stream_analog_level(100));
1245 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1246 out_analog_level = apm_->gain_control()->stream_analog_level();
1247 }
1248
1249 // Ensure the AGC is still able to reach the maximum.
1250 EXPECT_EQ(255, out_analog_level);
1251}
1252
1253// Verifies that despite volume slider quantization, the AGC can continue to
1254// increase its volume.
1255TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001256 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001257 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1258 }
1259}
1260
1261void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001262 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001263 EXPECT_EQ(apm_->kNoError,
1264 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1265 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1266
1267 int out_analog_level = 100;
1268 for (int i = 0; i < 1000; ++i) {
1269 ReadFrameWithRewind(near_file_, frame_);
1270 // Ensure the audio is at a low level, so the AGC will try to increase it.
1271 ScaleFrame(frame_, 0.25);
1272
1273 EXPECT_EQ(apm_->kNoError,
1274 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1275 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1276 out_analog_level = apm_->gain_control()->stream_analog_level();
1277 }
1278
1279 // Ensure the volume was raised.
1280 EXPECT_GT(out_analog_level, 100);
1281 int highest_level_reached = out_analog_level;
1282 // Simulate a user manual volume change.
1283 out_analog_level = 100;
1284
1285 for (int i = 0; i < 300; ++i) {
1286 ReadFrameWithRewind(near_file_, frame_);
1287 ScaleFrame(frame_, 0.25);
1288
1289 EXPECT_EQ(apm_->kNoError,
1290 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1291 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1292 out_analog_level = apm_->gain_control()->stream_analog_level();
1293 // Check that AGC respected the manually adjusted volume.
1294 EXPECT_LT(out_analog_level, highest_level_reached);
1295 }
1296 // Check that the volume was still raised.
1297 EXPECT_GT(out_analog_level, 100);
1298}
1299
1300TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001301 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001302 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1303 }
1304}
1305
niklase@google.com470e71d2011-07-07 08:21:25 +00001306TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001307 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001308 NoiseSuppression::Level level[] = {
1309 NoiseSuppression::kLow,
1310 NoiseSuppression::kModerate,
1311 NoiseSuppression::kHigh,
1312 NoiseSuppression::kVeryHigh
1313 };
pkasting25702cb2016-01-08 13:50:27 -08001314 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001315 EXPECT_EQ(apm_->kNoError,
1316 apm_->noise_suppression()->set_level(level[i]));
1317 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1318 }
1319
andrew@webrtc.org648af742012-02-08 01:57:29 +00001320 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001321 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1322 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1323 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1324 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1325}
1326
1327TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001328 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001329 AudioProcessing::Config apm_config;
1330 apm_config.high_pass_filter.enabled = true;
1331 apm_->ApplyConfig(apm_config);
1332 apm_config.high_pass_filter.enabled = false;
1333 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001334}
1335
1336TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001337 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001338 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001339 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001340
1341 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1342
1343 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1344 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1345
1346 // Run this test in wideband; in super-wb, the splitting filter distorts the
1347 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001348 frame_->samples_per_channel_ = 160;
1349 frame_->num_channels_ = 2;
1350 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001351
1352 // Min value if no frames have been processed.
1353 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1354
1355 // Min value on zero frames.
1356 SetFrameTo(frame_, 0);
1357 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1358 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1359 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1360
1361 // Try a few RMS values.
1362 // (These also test that the value resets after retrieving it.)
1363 SetFrameTo(frame_, 32767);
1364 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1365 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1366 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1367
1368 SetFrameTo(frame_, 30000);
1369 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1370 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1371 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1372
1373 SetFrameTo(frame_, 10000);
1374 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1375 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1376 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1377
1378 SetFrameTo(frame_, 10);
1379 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1380 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1381 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1382
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001383 // Verify reset after enable/disable.
1384 SetFrameTo(frame_, 32767);
1385 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1386 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1387 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1388 SetFrameTo(frame_, 1);
1389 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1390 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1391
1392 // Verify reset after initialize.
1393 SetFrameTo(frame_, 32767);
1394 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1395 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1396 SetFrameTo(frame_, 1);
1397 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1398 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001399}
1400
1401TEST_F(ApmTest, VoiceDetection) {
1402 // Test external VAD
1403 EXPECT_EQ(apm_->kNoError,
1404 apm_->voice_detection()->set_stream_has_voice(true));
1405 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1406 EXPECT_EQ(apm_->kNoError,
1407 apm_->voice_detection()->set_stream_has_voice(false));
1408 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1409
andrew@webrtc.org648af742012-02-08 01:57:29 +00001410 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001411 VoiceDetection::Likelihood likelihood[] = {
1412 VoiceDetection::kVeryLowLikelihood,
1413 VoiceDetection::kLowLikelihood,
1414 VoiceDetection::kModerateLikelihood,
1415 VoiceDetection::kHighLikelihood
1416 };
pkasting25702cb2016-01-08 13:50:27 -08001417 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001418 EXPECT_EQ(apm_->kNoError,
1419 apm_->voice_detection()->set_likelihood(likelihood[i]));
1420 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1421 }
1422
1423 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001424 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 EXPECT_EQ(apm_->kBadParameterError,
1426 apm_->voice_detection()->set_frame_size_ms(12));
1427
andrew@webrtc.org648af742012-02-08 01:57:29 +00001428 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001429 for (int i = 10; i <= 30; i += 10) {
1430 EXPECT_EQ(apm_->kNoError,
1431 apm_->voice_detection()->set_frame_size_ms(i));
1432 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1433 }
1434 */
1435
andrew@webrtc.org648af742012-02-08 01:57:29 +00001436 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001437 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1438 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1439 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1440 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1441
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001442 // Test that AudioFrame activity is maintained when VAD is disabled.
1443 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1444 AudioFrame::VADActivity activity[] = {
1445 AudioFrame::kVadActive,
1446 AudioFrame::kVadPassive,
1447 AudioFrame::kVadUnknown
1448 };
pkasting25702cb2016-01-08 13:50:27 -08001449 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001450 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001451 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001452 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001453 }
1454
1455 // Test that AudioFrame activity is set when VAD is enabled.
1456 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001457 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001458 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001459 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001460
niklase@google.com470e71d2011-07-07 08:21:25 +00001461 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1462}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001463
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001464TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1465 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1466 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1467 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1468 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1469 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1470 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1471 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1472}
1473
1474TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001475 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001476 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001477 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001478 AudioFrame frame_copy;
1479 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001480 for (int j = 0; j < 1000; j++) {
1481 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1482 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001483 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1484 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001485 }
1486 }
1487}
1488
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001489TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1490 // Test that ProcessStream copies input to output even with no processing.
1491 const size_t kSamples = 80;
1492 const int sample_rate = 8000;
1493 const float src[kSamples] = {
1494 -1.0f, 0.0f, 1.0f
1495 };
1496 float dest[kSamples] = {};
1497
1498 auto src_channels = &src[0];
1499 auto dest_channels = &dest[0];
1500
Ivo Creusen62337e52018-01-09 14:17:33 +01001501 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001502 EXPECT_NOERR(apm_->ProcessStream(
1503 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1504 sample_rate, LayoutFromChannels(1), &dest_channels));
1505
1506 for (size_t i = 0; i < kSamples; ++i) {
1507 EXPECT_EQ(src[i], dest[i]);
1508 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001509
1510 // Same for ProcessReverseStream.
1511 float rev_dest[kSamples] = {};
1512 auto rev_dest_channels = &rev_dest[0];
1513
1514 StreamConfig input_stream = {sample_rate, 1};
1515 StreamConfig output_stream = {sample_rate, 1};
1516 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1517 output_stream, &rev_dest_channels));
1518
1519 for (size_t i = 0; i < kSamples; ++i) {
1520 EXPECT_EQ(src[i], rev_dest[i]);
1521 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001522}
1523
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001524TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1525 EnableAllComponents();
1526
pkasting25702cb2016-01-08 13:50:27 -08001527 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001528 Init(kProcessSampleRates[i],
1529 kProcessSampleRates[i],
1530 kProcessSampleRates[i],
1531 2,
1532 2,
1533 2,
1534 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001535 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001536 ASSERT_EQ(0, feof(far_file_));
1537 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001538 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001539 CopyLeftToRightChannel(revframe_->mutable_data(),
1540 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001541
aluebsb0319552016-03-17 20:39:53 -07001542 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001543
yujo36b1a5f2017-06-12 12:45:32 -07001544 CopyLeftToRightChannel(frame_->mutable_data(),
1545 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001546 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1547
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001548 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001549 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001550 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001551 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001552 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001553 analog_level = apm_->gain_control()->stream_analog_level();
1554
yujo36b1a5f2017-06-12 12:45:32 -07001555 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001556 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001557 rewind(far_file_);
1558 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001559 }
1560}
1561
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001562TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001563 // Verify the filter is not active through undistorted audio when:
1564 // 1. No components are enabled...
1565 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001566 AudioFrame frame_copy;
1567 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001568 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1569 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1570 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1571
1572 // 2. Only the level estimator is enabled...
1573 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001574 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001575 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1576 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1577 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1578 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1579 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1580
1581 // 3. Only VAD is enabled...
1582 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001583 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001584 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1585 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1586 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1587 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1588 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1589
1590 // 4. Both VAD and the level estimator are enabled...
1591 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001592 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001593 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1594 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1595 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1596 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1597 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1598 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1599 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1600
1601 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001602 frame_->samples_per_channel_ = 160;
1603 frame_->num_channels_ = 2;
1604 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001605 // Enable AEC, which would require the filter in super-wb. We rely on the
1606 // first few frames of data being unaffected by the AEC.
1607 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1608 // behavior of the AEC. Think of something more robust.
1609 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001610 // Make sure we have extended filter enabled. This makes sure nothing is
1611 // touched until we have a farend frame.
1612 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001613 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001614 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001615 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001616 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001617 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001618 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001619 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1620 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001621 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001622 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1623 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1624
1625 // Check the test is valid. We should have distortion from the filter
1626 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001627 frame_->samples_per_channel_ = 320;
1628 frame_->num_channels_ = 2;
1629 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001630 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001631 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001632 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001633 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001634 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1635 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1636}
1637
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001638#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1639void ApmTest::ProcessDebugDump(const std::string& in_filename,
1640 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001641 Format format,
1642 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001643 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001644 FILE* in_file = fopen(in_filename.c_str(), "rb");
1645 ASSERT_TRUE(in_file != NULL);
1646 audioproc::Event event_msg;
1647 bool first_init = true;
1648
1649 while (ReadMessageFromFile(in_file, &event_msg)) {
1650 if (event_msg.type() == audioproc::Event::INIT) {
1651 const audioproc::Init msg = event_msg.init();
1652 int reverse_sample_rate = msg.sample_rate();
1653 if (msg.has_reverse_sample_rate()) {
1654 reverse_sample_rate = msg.reverse_sample_rate();
1655 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001656 int output_sample_rate = msg.sample_rate();
1657 if (msg.has_output_sample_rate()) {
1658 output_sample_rate = msg.output_sample_rate();
1659 }
1660
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001661 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001662 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001663 reverse_sample_rate,
1664 msg.num_input_channels(),
1665 msg.num_output_channels(),
1666 msg.num_reverse_channels(),
1667 false);
1668 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001669 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001670 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001671 auto aec_dump =
1672 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1673 EXPECT_TRUE(aec_dump);
1674 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001675 first_init = false;
1676 }
1677
1678 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1679 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1680
1681 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001682 ASSERT_EQ(revframe_->num_channels_,
1683 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001684 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001685 memcpy(revfloat_cb_->channels()[i],
1686 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001687 msg.channel(i).size());
1688 }
1689 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001690 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001691 if (format == kFloatFormat) {
1692 // We're using an int16 input file; convert to float.
1693 ConvertToFloat(*revframe_, revfloat_cb_.get());
1694 }
1695 }
1696 AnalyzeReverseStreamChooser(format);
1697
1698 } else if (event_msg.type() == audioproc::Event::STREAM) {
1699 const audioproc::Stream msg = event_msg.stream();
1700 // ProcessStream could have changed this for the output frame.
1701 frame_->num_channels_ = apm_->num_input_channels();
1702
1703 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1704 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1705 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1706 if (msg.has_keypress()) {
1707 apm_->set_stream_key_pressed(msg.keypress());
1708 } else {
1709 apm_->set_stream_key_pressed(true);
1710 }
1711
1712 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001713 ASSERT_EQ(frame_->num_channels_,
1714 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001715 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001716 memcpy(float_cb_->channels()[i],
1717 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001718 msg.input_channel(i).size());
1719 }
1720 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001721 memcpy(frame_->mutable_data(), msg.input_data().data(),
1722 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001723 if (format == kFloatFormat) {
1724 // We're using an int16 input file; convert to float.
1725 ConvertToFloat(*frame_, float_cb_.get());
1726 }
1727 }
1728 ProcessStreamChooser(format);
1729 }
1730 }
aleloif4dd1912017-06-15 01:55:38 -07001731 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001732 fclose(in_file);
1733}
1734
1735void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001736 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001737 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001738 std::string format_string;
1739 switch (format) {
1740 case kIntFormat:
1741 format_string = "_int";
1742 break;
1743 case kFloatFormat:
1744 format_string = "_float";
1745 break;
1746 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001747 const std::string ref_filename = test::TempFilename(
1748 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1749 const std::string out_filename = test::TempFilename(
1750 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001751 const std::string limited_filename = test::TempFilename(
1752 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1753 const size_t logging_limit_bytes = 100000;
1754 // We expect at least this many bytes in the created logfile.
1755 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001756 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001757 ProcessDebugDump(in_filename, ref_filename, format, -1);
1758 ProcessDebugDump(ref_filename, out_filename, format, -1);
1759 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001760
1761 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1762 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001763 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001764 ASSERT_TRUE(ref_file != NULL);
1765 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001766 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001767 std::unique_ptr<uint8_t[]> ref_bytes;
1768 std::unique_ptr<uint8_t[]> out_bytes;
1769 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001770
1771 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1772 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001773 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001774 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001775 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001776 while (ref_size > 0 && out_size > 0) {
1777 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001778 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001779 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001780 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001781 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001782 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001783 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1784 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001785 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001786 }
1787 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001788 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1789 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001790 EXPECT_NE(0, feof(ref_file));
1791 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001792 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001793 ASSERT_EQ(0, fclose(ref_file));
1794 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001795 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001796 remove(ref_filename.c_str());
1797 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001798 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001799}
1800
pbosc7a65692016-05-06 12:50:04 -07001801TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001802 VerifyDebugDumpTest(kIntFormat);
1803}
1804
pbosc7a65692016-05-06 12:50:04 -07001805TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001806 VerifyDebugDumpTest(kFloatFormat);
1807}
1808#endif
1809
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001810// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001811TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001812 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001813 const std::string filename =
1814 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001815 {
1816 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1817 EXPECT_FALSE(aec_dump);
1818 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001819
1820#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1821 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001822 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001823
aleloif4dd1912017-06-15 01:55:38 -07001824 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1825 EXPECT_TRUE(aec_dump);
1826 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001827 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001828 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001829 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001830
1831 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001832 FILE* fid = fopen(filename.c_str(), "r");
1833 ASSERT_TRUE(fid != NULL);
1834
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001835 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001836 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001837 ASSERT_EQ(0, remove(filename.c_str()));
1838#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001839 // Verify the file has NOT been written.
1840 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1841#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1842}
1843
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001844// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001845TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001846 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1847
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001848 const std::string filename =
1849 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001850 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001851 ASSERT_TRUE(fid);
1852
1853#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1854 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001855 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001856
aleloif4dd1912017-06-15 01:55:38 -07001857 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1858 EXPECT_TRUE(aec_dump);
1859 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001860 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001861 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001862 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001863
1864 // Verify the file has been written.
1865 fid = fopen(filename.c_str(), "r");
1866 ASSERT_TRUE(fid != NULL);
1867
1868 // Clean it up.
1869 ASSERT_EQ(0, fclose(fid));
1870 ASSERT_EQ(0, remove(filename.c_str()));
1871#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001872 ASSERT_EQ(0, fclose(fid));
1873#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1874}
1875
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001876TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001877 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001878 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001879
1880 Config config;
1881 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001882 std::unique_ptr<AudioProcessing> fapm(
1883 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001884 EnableAllComponents();
1885 EnableAllAPComponents(fapm.get());
1886 for (int i = 0; i < ref_data.test_size(); i++) {
1887 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1888
1889 audioproc::Test* test = ref_data.mutable_test(i);
1890 // TODO(ajm): Restore downmixing test cases.
1891 if (test->num_input_channels() != test->num_output_channels())
1892 continue;
1893
Peter Kasting69558702016-01-12 16:26:35 -08001894 const size_t num_render_channels =
1895 static_cast<size_t>(test->num_reverse_channels());
1896 const size_t num_input_channels =
1897 static_cast<size_t>(test->num_input_channels());
1898 const size_t num_output_channels =
1899 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001900 const size_t samples_per_channel = static_cast<size_t>(
1901 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001902
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001903 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1904 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001905 Init(fapm.get());
1906
1907 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001908 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1909 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001910
1911 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001912 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001913 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1914 ReadFrame(near_file_, frame_, float_cb_.get())) {
1915 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1916
aluebsb0319552016-03-17 20:39:53 -07001917 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001918 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1919 revfloat_cb_->channels(),
1920 samples_per_channel,
1921 test->sample_rate(),
1922 LayoutFromChannels(num_render_channels)));
1923
1924 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1925 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1926 apm_->echo_cancellation()->set_stream_drift_samples(0);
1927 fapm->echo_cancellation()->set_stream_drift_samples(0);
1928 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1929 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1930
1931 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001932 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001933 output_int16.channels());
1934
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001935 EXPECT_NOERR(fapm->ProcessStream(
1936 float_cb_->channels(),
1937 samples_per_channel,
1938 test->sample_rate(),
1939 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001940 test->sample_rate(),
1941 LayoutFromChannels(num_output_channels),
1942 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001943 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001944 FloatToS16(float_cb_->channels()[j],
1945 samples_per_channel,
1946 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001947 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001948 float snr = ComputeSNR(output_int16.channels()[j],
1949 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001950 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001951
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001952 const float kVarianceThreshold = 20;
1953 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001954
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001955 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001956 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1957 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001958 }
1959 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001960
1961 analog_level = fapm->gain_control()->stream_analog_level();
1962 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1963 fapm->gain_control()->stream_analog_level());
1964 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
1965 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001966 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1967 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001968 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001969
1970 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001971 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001972 }
aluebs776593b2016-03-15 14:04:58 -07001973
1974#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1975 const size_t kMaxNumBadChunks = 0;
1976#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1977 // There are a few chunks in the fixed-point profile that give low SNR.
1978 // Listening confirmed the difference is acceptable.
1979 const size_t kMaxNumBadChunks = 60;
1980#endif
1981 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1982
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001983 rewind(far_file_);
1984 rewind(near_file_);
1985 }
1986}
1987
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001988// TODO(andrew): Add a test to process a few frames with different combinations
1989// of enabled components.
1990
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001991TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001992 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001993 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001994
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001995 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001996 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001997 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001998 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001999 for (size_t i = 0; i < arraysize(kChannels); i++) {
2000 for (size_t j = 0; j < arraysize(kChannels); j++) {
2001 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002002 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002003 test->set_num_reverse_channels(kChannels[i]);
2004 test->set_num_input_channels(kChannels[j]);
2005 test->set_num_output_channels(kChannels[j]);
2006 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002007 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002008 }
2009 }
2010 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002011#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2012 // To test the extended filter mode.
2013 audioproc::Test* test = ref_data.add_test();
2014 test->set_num_reverse_channels(2);
2015 test->set_num_input_channels(2);
2016 test->set_num_output_channels(2);
2017 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2018 test->set_use_aec_extended_filter(true);
2019#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002020 }
2021
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002022 for (int i = 0; i < ref_data.test_size(); i++) {
2023 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002024
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002025 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002026 // TODO(ajm): We no longer allow different input and output channels. Skip
2027 // these tests for now, but they should be removed from the set.
2028 if (test->num_input_channels() != test->num_output_channels())
2029 continue;
2030
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002031 Config config;
2032 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002033 config.Set<ExtendedFilter>(
2034 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01002035 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002036
2037 EnableAllComponents();
2038
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002039 Init(test->sample_rate(),
2040 test->sample_rate(),
2041 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002042 static_cast<size_t>(test->num_input_channels()),
2043 static_cast<size_t>(test->num_output_channels()),
2044 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002045 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002046
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002047 int frame_count = 0;
2048 int has_echo_count = 0;
2049 int has_voice_count = 0;
2050 int is_saturated_count = 0;
2051 int analog_level = 127;
2052 int analog_level_average = 0;
2053 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002054 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002055#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2056 int stats_index = 0;
2057#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002058
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002059 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002060 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002061
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002062 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2063
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002064 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002065 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002066 EXPECT_EQ(apm_->kNoError,
2067 apm_->gain_control()->set_stream_analog_level(analog_level));
2068
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002069 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002070
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002071 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002072 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2073 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002074
2075 max_output_average += MaxAudioFrame(*frame_);
2076
2077 if (apm_->echo_cancellation()->stream_has_echo()) {
2078 has_echo_count++;
2079 }
2080
2081 analog_level = apm_->gain_control()->stream_analog_level();
2082 analog_level_average += analog_level;
2083 if (apm_->gain_control()->stream_is_saturated()) {
2084 is_saturated_count++;
2085 }
2086 if (apm_->voice_detection()->stream_has_voice()) {
2087 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002088 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002089 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002090 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002091 }
2092
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002093 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2094
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002095 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002096 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002097 sizeof(int16_t),
2098 frame_size,
2099 out_file_);
2100 ASSERT_EQ(frame_size, write_count);
2101
2102 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002103 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002104 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002105
2106#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2107 const int kStatsAggregationFrameNum = 100; // 1 second.
2108 if (frame_count % kStatsAggregationFrameNum == 0) {
2109 // Get echo metrics.
2110 EchoCancellation::Metrics echo_metrics;
2111 EXPECT_EQ(apm_->kNoError,
2112 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2113
2114 // Get delay metrics.
2115 int median = 0;
2116 int std = 0;
2117 float fraction_poor_delays = 0;
2118 EXPECT_EQ(apm_->kNoError,
2119 apm_->echo_cancellation()->GetDelayMetrics(
2120 &median, &std, &fraction_poor_delays));
2121
2122 // Get RMS.
2123 int rms_level = apm_->level_estimator()->RMS();
2124 EXPECT_LE(0, rms_level);
2125 EXPECT_GE(127, rms_level);
2126
2127 if (!write_ref_data) {
2128 const audioproc::Test::EchoMetrics& reference =
2129 test->echo_metrics(stats_index);
2130 TestStats(echo_metrics.residual_echo_return_loss,
2131 reference.residual_echo_return_loss());
2132 TestStats(echo_metrics.echo_return_loss,
2133 reference.echo_return_loss());
2134 TestStats(echo_metrics.echo_return_loss_enhancement,
2135 reference.echo_return_loss_enhancement());
2136 TestStats(echo_metrics.a_nlp,
2137 reference.a_nlp());
2138 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2139 reference.divergent_filter_fraction());
2140
2141 const audioproc::Test::DelayMetrics& reference_delay =
2142 test->delay_metrics(stats_index);
2143 EXPECT_EQ(reference_delay.median(), median);
2144 EXPECT_EQ(reference_delay.std(), std);
2145 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2146 fraction_poor_delays);
2147
2148 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2149
2150 ++stats_index;
2151 } else {
2152 audioproc::Test::EchoMetrics* message =
2153 test->add_echo_metrics();
2154 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2155 message->mutable_residual_echo_return_loss());
2156 WriteStatsMessage(echo_metrics.echo_return_loss,
2157 message->mutable_echo_return_loss());
2158 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2159 message->mutable_echo_return_loss_enhancement());
2160 WriteStatsMessage(echo_metrics.a_nlp,
2161 message->mutable_a_nlp());
2162 message->set_divergent_filter_fraction(
2163 echo_metrics.divergent_filter_fraction);
2164
2165 audioproc::Test::DelayMetrics* message_delay =
2166 test->add_delay_metrics();
2167 message_delay->set_median(median);
2168 message_delay->set_std(std);
2169 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2170
2171 test->add_rms_level(rms_level);
2172 }
2173 }
2174#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002175 }
2176 max_output_average /= frame_count;
2177 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002178 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002179
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002180 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002181 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002182 // When running the test on a N7 we get a {2, 6} difference of
2183 // |has_voice_count| and |max_output_average| is up to 18 higher.
2184 // All numbers being consistently higher on N7 compare to ref_data.
2185 // TODO(bjornv): If we start getting more of these offsets on Android we
2186 // should consider a different approach. Either using one slack for all,
2187 // or generate a separate android reference.
2188#if defined(WEBRTC_ANDROID)
2189 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002190 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002191 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002192 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002193#else
2194 const int kHasVoiceCountOffset = 0;
2195 const int kHasVoiceCountNear = kIntNear;
2196 const int kMaxOutputAverageOffset = 0;
2197 const int kMaxOutputAverageNear = kIntNear;
2198#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002199 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002200 EXPECT_NEAR(test->has_voice_count(),
2201 has_voice_count - kHasVoiceCountOffset,
2202 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002203 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002204
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002205 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002206 EXPECT_NEAR(test->max_output_average(),
2207 max_output_average - kMaxOutputAverageOffset,
2208 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002209#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002210 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002211 EXPECT_NEAR(test->ns_speech_probability_average(),
2212 ns_speech_prob_average,
2213 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002214#endif
2215 } else {
2216 test->set_has_echo_count(has_echo_count);
2217 test->set_has_voice_count(has_voice_count);
2218 test->set_is_saturated_count(is_saturated_count);
2219
2220 test->set_analog_level_average(analog_level_average);
2221 test->set_max_output_average(max_output_average);
2222
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002223#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002224 EXPECT_LE(0.0f, ns_speech_prob_average);
2225 EXPECT_GE(1.0f, ns_speech_prob_average);
2226 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002227#endif
2228 }
2229
2230 rewind(far_file_);
2231 rewind(near_file_);
2232 }
2233
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002234 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002235 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002236 }
2237}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002238
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002239TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2240 struct ChannelFormat {
2241 AudioProcessing::ChannelLayout in_layout;
2242 AudioProcessing::ChannelLayout out_layout;
2243 };
2244 ChannelFormat cf[] = {
2245 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2246 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2247 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2248 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002249
Ivo Creusen62337e52018-01-09 14:17:33 +01002250 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002251 // Enable one component just to ensure some processing takes place.
2252 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002253 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002254 const int in_rate = 44100;
2255 const int out_rate = 48000;
2256 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2257 TotalChannelsFromLayout(cf[i].in_layout));
2258 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2259 ChannelsFromLayout(cf[i].out_layout));
2260
2261 // Run over a few chunks.
2262 for (int j = 0; j < 10; ++j) {
2263 EXPECT_NOERR(ap->ProcessStream(
2264 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002265 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002266 in_rate,
2267 cf[i].in_layout,
2268 out_rate,
2269 cf[i].out_layout,
2270 out_cb.channels()));
2271 }
2272 }
2273}
2274
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002275// Compares the reference and test arrays over a region around the expected
2276// delay. Finds the highest SNR in that region and adds the variance and squared
2277// error results to the supplied accumulators.
2278void UpdateBestSNR(const float* ref,
2279 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002280 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002281 int expected_delay,
2282 double* variance_acc,
2283 double* sq_error_acc) {
2284 double best_snr = std::numeric_limits<double>::min();
2285 double best_variance = 0;
2286 double best_sq_error = 0;
2287 // Search over a region of eight samples around the expected delay.
2288 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2289 ++delay) {
2290 double sq_error = 0;
2291 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002292 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002293 double error = test[i + delay] - ref[i];
2294 sq_error += error * error;
2295 variance += ref[i] * ref[i];
2296 }
2297
2298 if (sq_error == 0) {
2299 *variance_acc += variance;
2300 return;
2301 }
2302 double snr = variance / sq_error;
2303 if (snr > best_snr) {
2304 best_snr = snr;
2305 best_variance = variance;
2306 best_sq_error = sq_error;
2307 }
2308 }
2309
2310 *variance_acc += best_variance;
2311 *sq_error_acc += best_sq_error;
2312}
2313
2314// Used to test a multitude of sample rate and channel combinations. It works
2315// by first producing a set of reference files (in SetUpTestCase) that are
2316// assumed to be correct, as the used parameters are verified by other tests
2317// in this collection. Primarily the reference files are all produced at
2318// "native" rates which do not involve any resampling.
2319
2320// Each test pass produces an output file with a particular format. The output
2321// is matched against the reference file closest to its internal processing
2322// format. If necessary the output is resampled back to its process format.
2323// Due to the resampling distortion, we don't expect identical results, but
2324// enforce SNR thresholds which vary depending on the format. 0 is a special
2325// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002326typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002327class AudioProcessingTest
2328 : public testing::TestWithParam<AudioProcessingTestData> {
2329 public:
2330 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002331 : input_rate_(std::get<0>(GetParam())),
2332 output_rate_(std::get<1>(GetParam())),
2333 reverse_input_rate_(std::get<2>(GetParam())),
2334 reverse_output_rate_(std::get<3>(GetParam())),
2335 expected_snr_(std::get<4>(GetParam())),
2336 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002337
2338 virtual ~AudioProcessingTest() {}
2339
2340 static void SetUpTestCase() {
2341 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002342 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002343 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002344 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2345 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2346 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002347 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002348 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2349 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2350 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002351 }
2352 }
2353 }
2354 }
2355
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002356 void TearDown() {
2357 // Remove "out" files after each test.
2358 ClearTempOutFiles();
2359 }
2360
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002361 static void TearDownTestCase() {
2362 ClearTempFiles();
2363 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002364
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002365 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002366 // to a file specified with |output_file_prefix|. Both forward and reverse
2367 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002368 static void ProcessFormat(int input_rate,
2369 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002370 int reverse_input_rate,
2371 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002372 size_t num_input_channels,
2373 size_t num_output_channels,
2374 size_t num_reverse_input_channels,
2375 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002376 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002377 Config config;
2378 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002379 std::unique_ptr<AudioProcessing> ap(
2380 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002381 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002382
ekmeyerson60d9b332015-08-14 10:35:55 -07002383 ProcessingConfig processing_config = {
2384 {{input_rate, num_input_channels},
2385 {output_rate, num_output_channels},
2386 {reverse_input_rate, num_reverse_input_channels},
2387 {reverse_output_rate, num_reverse_output_channels}}};
2388 ap->Initialize(processing_config);
2389
2390 FILE* far_file =
2391 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002392 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002393 FILE* out_file =
2394 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2395 reverse_input_rate, reverse_output_rate,
2396 num_input_channels, num_output_channels,
2397 num_reverse_input_channels,
2398 num_reverse_output_channels, kForward).c_str(),
2399 "wb");
2400 FILE* rev_out_file =
2401 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2402 reverse_input_rate, reverse_output_rate,
2403 num_input_channels, num_output_channels,
2404 num_reverse_input_channels,
2405 num_reverse_output_channels, kReverse).c_str(),
2406 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002407 ASSERT_TRUE(far_file != NULL);
2408 ASSERT_TRUE(near_file != NULL);
2409 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002410 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002411
2412 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2413 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002414 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2415 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002416 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2417 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002418 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2419 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002420
2421 // Temporary buffers.
2422 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002423 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2424 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002425 std::unique_ptr<float[]> float_data(new float[max_length]);
2426 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002427
2428 int analog_level = 127;
2429 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2430 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002431 EXPECT_NOERR(ap->ProcessReverseStream(
2432 rev_cb.channels(), processing_config.reverse_input_stream(),
2433 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002434
2435 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2436 ap->echo_cancellation()->set_stream_drift_samples(0);
2437 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2438
2439 EXPECT_NOERR(ap->ProcessStream(
2440 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002441 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002442 input_rate,
2443 LayoutFromChannels(num_input_channels),
2444 output_rate,
2445 LayoutFromChannels(num_output_channels),
2446 out_cb.channels()));
2447
ekmeyerson60d9b332015-08-14 10:35:55 -07002448 // Dump forward output to file.
2449 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002450 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002451 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002452
pkasting25702cb2016-01-08 13:50:27 -08002453 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002454 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002455 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002456
ekmeyerson60d9b332015-08-14 10:35:55 -07002457 // Dump reverse output to file.
2458 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2459 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002460 size_t rev_out_length =
2461 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002462
pkasting25702cb2016-01-08 13:50:27 -08002463 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002464 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2465 rev_out_file));
2466
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002467 analog_level = ap->gain_control()->stream_analog_level();
2468 }
2469 fclose(far_file);
2470 fclose(near_file);
2471 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002472 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002473 }
2474
2475 protected:
2476 int input_rate_;
2477 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002478 int reverse_input_rate_;
2479 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002480 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002481 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002482};
2483
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002484TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002485 struct ChannelFormat {
2486 int num_input;
2487 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002488 int num_reverse_input;
2489 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002490 };
2491 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002492 {1, 1, 1, 1},
2493 {1, 1, 2, 1},
2494 {2, 1, 1, 1},
2495 {2, 1, 2, 1},
2496 {2, 2, 1, 1},
2497 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002498 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002499
pkasting25702cb2016-01-08 13:50:27 -08002500 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002501 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2502 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2503 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002504
ekmeyerson60d9b332015-08-14 10:35:55 -07002505 // Verify output for both directions.
2506 std::vector<StreamDirection> stream_directions;
2507 stream_directions.push_back(kForward);
2508 stream_directions.push_back(kReverse);
2509 for (StreamDirection file_direction : stream_directions) {
2510 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2511 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2512 const int out_num =
2513 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2514 const double expected_snr =
2515 file_direction ? expected_reverse_snr_ : expected_snr_;
2516
2517 const int min_ref_rate = std::min(in_rate, out_rate);
2518 int ref_rate;
2519
2520 if (min_ref_rate > 32000) {
2521 ref_rate = 48000;
2522 } else if (min_ref_rate > 16000) {
2523 ref_rate = 32000;
2524 } else if (min_ref_rate > 8000) {
2525 ref_rate = 16000;
2526 } else {
2527 ref_rate = 8000;
2528 }
aluebs776593b2016-03-15 14:04:58 -07002529#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002530 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002531 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002532 }
2533#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002534 FILE* out_file = fopen(
2535 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2536 reverse_output_rate_, cf[i].num_input,
2537 cf[i].num_output, cf[i].num_reverse_input,
2538 cf[i].num_reverse_output, file_direction).c_str(),
2539 "rb");
2540 // The reference files always have matching input and output channels.
2541 FILE* ref_file = fopen(
2542 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2543 cf[i].num_output, cf[i].num_output,
2544 cf[i].num_reverse_output, cf[i].num_reverse_output,
2545 file_direction).c_str(),
2546 "rb");
2547 ASSERT_TRUE(out_file != NULL);
2548 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002549
pkasting25702cb2016-01-08 13:50:27 -08002550 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2551 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002552 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002553 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002554 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002555 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002556 // Data from the resampled output, in case the reference and output rates
2557 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002558 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002559
ekmeyerson60d9b332015-08-14 10:35:55 -07002560 PushResampler<float> resampler;
2561 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002562
ekmeyerson60d9b332015-08-14 10:35:55 -07002563 // Compute the resampling delay of the output relative to the reference,
2564 // to find the region over which we should search for the best SNR.
2565 float expected_delay_sec = 0;
2566 if (in_rate != ref_rate) {
2567 // Input resampling delay.
2568 expected_delay_sec +=
2569 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2570 }
2571 if (out_rate != ref_rate) {
2572 // Output resampling delay.
2573 expected_delay_sec +=
2574 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2575 // Delay of converting the output back to its processing rate for
2576 // testing.
2577 expected_delay_sec +=
2578 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2579 }
2580 int expected_delay =
2581 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002582
ekmeyerson60d9b332015-08-14 10:35:55 -07002583 double variance = 0;
2584 double sq_error = 0;
2585 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2586 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2587 float* out_ptr = out_data.get();
2588 if (out_rate != ref_rate) {
2589 // Resample the output back to its internal processing rate if
2590 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002591 ASSERT_EQ(ref_length,
2592 static_cast<size_t>(resampler.Resample(
2593 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002594 out_ptr = cmp_data.get();
2595 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002596
ekmeyerson60d9b332015-08-14 10:35:55 -07002597 // Update the |sq_error| and |variance| accumulators with the highest
2598 // SNR of reference vs output.
2599 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2600 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002601 }
2602
ekmeyerson60d9b332015-08-14 10:35:55 -07002603 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2604 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2605 << cf[i].num_input << ", " << cf[i].num_output << ", "
2606 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2607 << ", " << file_direction << "): ";
2608 if (sq_error > 0) {
2609 double snr = 10 * log10(variance / sq_error);
2610 EXPECT_GE(snr, expected_snr);
2611 EXPECT_NE(0, expected_snr);
2612 std::cout << "SNR=" << snr << " dB" << std::endl;
2613 } else {
aluebs776593b2016-03-15 14:04:58 -07002614 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002615 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002616
ekmeyerson60d9b332015-08-14 10:35:55 -07002617 fclose(out_file);
2618 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002619 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002620 }
2621}
2622
2623#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2624INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002625 CommonFormats,
2626 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002627 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2628 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2629 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2630 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2631 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2632 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2633 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2634 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2635 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2636 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2637 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2638 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002639
Edward Lemurc5ee9872017-10-23 23:33:04 +02002640 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2641 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2642 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2643 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2644 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2645 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2646 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2647 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2648 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2649 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2650 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2651 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002652
Edward Lemurc5ee9872017-10-23 23:33:04 +02002653 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2654 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2655 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2656 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2657 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2658 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2659 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2660 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2661 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2662 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2663 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2664 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002665
Edward Lemurc5ee9872017-10-23 23:33:04 +02002666 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2667 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2668 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2669 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2670 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2671 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2672 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2673 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2674 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2675 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2676 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2677 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002678
2679#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2680INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002681 CommonFormats,
2682 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002683 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2684 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2685 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2686 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2687 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2688 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2689 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2690 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2691 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2692 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2693 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2694 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002695
Edward Lemurc5ee9872017-10-23 23:33:04 +02002696 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2697 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2698 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2699 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2700 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2701 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2702 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2703 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2704 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2705 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2706 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2707 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002708
Edward Lemurc5ee9872017-10-23 23:33:04 +02002709 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2710 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2711 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2712 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2713 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2714 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2715 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2716 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2717 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2718 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2719 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2720 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002721
Edward Lemurc5ee9872017-10-23 23:33:04 +02002722 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2723 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2724 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2725 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2726 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2727 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2728 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2729 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2730 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2731 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2732 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2733 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002734#endif
2735
niklase@google.com470e71d2011-07-07 08:21:25 +00002736} // namespace
peahc19f3122016-10-07 14:54:10 -07002737
Alessio Bazzicac054e782018-04-16 12:10:09 +02002738TEST(RuntimeSettingTest, TestDefaultCtor) {
2739 auto s = AudioProcessing::RuntimeSetting();
2740 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2741}
2742
2743TEST(RuntimeSettingTest, TestCapturePreGain) {
2744 using Type = AudioProcessing::RuntimeSetting::Type;
2745 {
2746 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2747 EXPECT_EQ(Type::kCapturePreGain, s.type());
2748 float v;
2749 s.GetFloat(&v);
2750 EXPECT_EQ(1.25f, v);
2751 }
2752
2753#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2754 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2755#endif
2756}
2757
2758TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2759 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2760 auto s = AudioProcessing::RuntimeSetting();
2761 ASSERT_TRUE(q.Insert(&s));
2762 ASSERT_TRUE(q.Remove(&s));
2763 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2764}
2765
Sam Zackrisson0beac582017-09-25 12:04:02 +02002766TEST(ApmConfiguration, EnablePostProcessing) {
2767 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002768 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002769 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002770 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002771 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002772 rtc::scoped_refptr<AudioProcessing> apm =
2773 AudioProcessingBuilder()
2774 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002775 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002776
2777 AudioFrame audio;
2778 audio.num_channels_ = 1;
2779 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2780
2781 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002782 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002783}
2784
Alex Loiko5825aa62017-12-18 16:02:40 +01002785TEST(ApmConfiguration, EnablePreProcessing) {
2786 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002787 auto mock_pre_processor_ptr =
2788 new testing::NiceMock<test::MockCustomProcessing>();
2789 auto mock_pre_processor =
2790 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002791 rtc::scoped_refptr<AudioProcessing> apm =
2792 AudioProcessingBuilder()
2793 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002794 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002795
2796 AudioFrame audio;
2797 audio.num_channels_ = 1;
2798 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2799
2800 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2801 apm->ProcessReverseStream(&audio);
2802}
2803
Alex Loiko73ec0192018-05-15 10:52:28 +02002804TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2805 auto mock_pre_processor_ptr =
2806 new testing::NiceMock<test::MockCustomProcessing>();
2807 auto mock_pre_processor =
2808 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2809 rtc::scoped_refptr<AudioProcessing> apm =
2810 AudioProcessingBuilder()
2811 .SetRenderPreProcessing(std::move(mock_pre_processor))
2812 .Create();
2813 apm->SetRuntimeSetting(
2814 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2815
2816 // RuntimeSettings forwarded during 'Process*Stream' calls.
2817 // Therefore we have to make one such call.
2818 AudioFrame audio;
2819 audio.num_channels_ = 1;
2820 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2821
2822 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2823 apm->ProcessReverseStream(&audio);
2824}
2825
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002826class MyEchoControlFactory : public EchoControlFactory {
2827 public:
2828 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2829 auto ec = new test::MockEchoControl();
2830 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2831 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2832 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2833 return std::unique_ptr<EchoControl>(ec);
2834 }
2835};
2836
2837TEST(ApmConfiguration, EchoControlInjection) {
2838 // Verify that apm uses an injected echo controller if one is provided.
2839 webrtc::Config webrtc_config;
2840 std::unique_ptr<EchoControlFactory> echo_control_factory(
2841 new MyEchoControlFactory());
2842
Alex Loiko5825aa62017-12-18 16:02:40 +01002843 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002844 AudioProcessingBuilder()
2845 .SetEchoControlFactory(std::move(echo_control_factory))
2846 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002847
2848 AudioFrame audio;
2849 audio.num_channels_ = 1;
2850 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2851 apm->ProcessStream(&audio);
2852 apm->ProcessReverseStream(&audio);
2853 apm->ProcessStream(&audio);
2854}
Ivo Creusenae026092017-11-20 13:07:16 +01002855
2856std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2857 Config old_config;
2858 if (use_AEC2) {
2859 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2860 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2861 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002862 std::unique_ptr<AudioProcessing> apm(
2863 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002864 if (!apm) {
2865 return apm;
2866 }
2867
2868 ProcessingConfig processing_config = {
2869 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2870
2871 if (apm->Initialize(processing_config) != 0) {
2872 return nullptr;
2873 }
2874
2875 // Disable all components except for an AEC and the residual echo detector.
2876 AudioProcessing::Config config;
2877 config.residual_echo_detector.enabled = true;
Ivo Creusenae026092017-11-20 13:07:16 +01002878 config.high_pass_filter.enabled = false;
2879 config.gain_controller2.enabled = false;
Ivo Creusenae026092017-11-20 13:07:16 +01002880 apm->ApplyConfig(config);
2881 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2882 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2883 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2884 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
2885
2886 if (use_AEC2) {
2887 EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
2888 EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
2889 EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
2890 EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
2891 } else {
2892 EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
2893 EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
2894 }
2895 return apm;
2896}
2897
2898#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2899#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2900#else
2901#define MAYBE_ApmStatistics ApmStatistics
2902#endif
2903
2904TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2905 // Set up APM with AEC2 and process some audio.
2906 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2907 ASSERT_TRUE(apm);
2908
2909 // Set up an audioframe.
2910 AudioFrame frame;
2911 frame.num_channels_ = 1;
2912 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2913
2914 // Fill the audio frame with a sawtooth pattern.
2915 int16_t* ptr = frame.mutable_data();
2916 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2917 ptr[i] = 10000 * ((i % 3) - 1);
2918 }
2919
2920 // Do some processing.
2921 for (int i = 0; i < 200; i++) {
2922 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2923 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2924 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2925 }
2926
2927 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002928 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002929 // We expect all statistics to be set and have a sensible value.
2930 ASSERT_TRUE(stats.residual_echo_likelihood);
2931 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2932 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2933 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2934 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2935 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2936 ASSERT_TRUE(stats.echo_return_loss);
2937 EXPECT_NE(*stats.echo_return_loss, -100.0);
2938 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2939 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2940 ASSERT_TRUE(stats.divergent_filter_fraction);
2941 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2942 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2943 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2944 // We don't check stats.delay_median_ms since it takes too long to settle to a
2945 // value. At least 20 seconds of data need to be processed before it will get
2946 // a value, which would make this test take too much time.
2947
2948 // If there are no receive streams, we expect the stats not to be set. The
2949 // 'false' argument signals to APM that no receive streams are currently
2950 // active. In that situation the statistics would get stuck at their last
2951 // calculated value (AEC and echo detection need at least one stream in each
2952 // direction), so to avoid that, they should not be set by APM.
2953 stats = apm->GetStatistics(false);
2954 EXPECT_FALSE(stats.residual_echo_likelihood);
2955 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2956 EXPECT_FALSE(stats.echo_return_loss);
2957 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2958 EXPECT_FALSE(stats.divergent_filter_fraction);
2959 EXPECT_FALSE(stats.delay_median_ms);
2960 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2961}
2962
2963TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2964 // Set up APM with AECM and process some audio.
2965 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2966 ASSERT_TRUE(apm);
2967
2968 // Set up an audioframe.
2969 AudioFrame frame;
2970 frame.num_channels_ = 1;
2971 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2972
2973 // Fill the audio frame with a sawtooth pattern.
2974 int16_t* ptr = frame.mutable_data();
2975 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2976 ptr[i] = 10000 * ((i % 3) - 1);
2977 }
2978
2979 // Do some processing.
2980 for (int i = 0; i < 200; i++) {
2981 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2982 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2983 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2984 }
2985
2986 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002987 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002988 // We expect only the residual echo detector statistics to be set and have a
2989 // sensible value.
2990 EXPECT_TRUE(stats.residual_echo_likelihood);
2991 if (stats.residual_echo_likelihood) {
2992 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2993 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2994 }
2995 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2996 if (stats.residual_echo_likelihood_recent_max) {
2997 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2998 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2999 }
3000 EXPECT_FALSE(stats.echo_return_loss);
3001 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3002 EXPECT_FALSE(stats.divergent_filter_fraction);
3003 EXPECT_FALSE(stats.delay_median_ms);
3004 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3005
3006 // If there are no receive streams, we expect the stats not to be set.
3007 stats = apm->GetStatistics(false);
3008 EXPECT_FALSE(stats.residual_echo_likelihood);
3009 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
3010 EXPECT_FALSE(stats.echo_return_loss);
3011 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3012 EXPECT_FALSE(stats.divergent_filter_fraction);
3013 EXPECT_FALSE(stats.delay_median_ms);
3014 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3015}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003016} // namespace webrtc