blob: c21de9998098f6bd8d18ee37281b3ca6bbcf1c3d [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"
31#include "rtc_base/gtest_prod_util.h"
32#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010033#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010034#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/protobuf_utils.h"
Niels Möller84255bb2017-10-06 13:43:23 +020036#include "rtc_base/refcountedobject.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020037#include "rtc_base/swap_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/task_queue.h"
39#include "rtc_base/thread.h"
40#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "test/gtest.h"
42#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070043
44RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000045#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000046#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#endif
kwiberg77eab702016-09-28 17:42:01 -070050RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000051
andrew@webrtc.org27c69802014-02-18 20:24:56 +000052namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000053namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000054
ekmeyerson60d9b332015-08-14 10:35:55 -070055// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
56// applicable.
57
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000058// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000059// re-implemented; see comment at the bottom of this file. For now, the user has
60// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000061// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000062// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000063// be set to true with the command-line switch --write_ref_data.
64bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070065const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070066const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000067
aluebseb3603b2016-04-20 15:27:58 -070068#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
69// Android doesn't support 48kHz.
70const int kProcessSampleRates[] = {8000, 16000, 32000};
71#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070072const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070073#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000074
ekmeyerson60d9b332015-08-14 10:35:55 -070075enum StreamDirection { kForward = 0, kReverse };
76
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000077void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000078 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079 cb->num_channels());
80 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000083 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080084 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000085 S16ToFloat(cb_int.channels()[i],
86 cb->num_frames(),
87 cb->channels()[i]);
88 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000089}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000090
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000091void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070092 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093}
94
andrew@webrtc.org103657b2014-04-24 18:28:56 +000095// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080096size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097 switch (layout) {
98 case AudioProcessing::kMono:
99 return 1;
100 case AudioProcessing::kMonoAndKeyboard:
101 case AudioProcessing::kStereo:
102 return 2;
103 case AudioProcessing::kStereoAndKeyboard:
104 return 3;
105 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700106 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800107 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000108}
109
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000110int TruncateToMultipleOf10(int value) {
111 return (value / 10) * 10;
112}
113
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000114void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800115 size_t samples_per_channel) {
116 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000118}
119
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800121 size_t samples_per_channel) {
122 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
124}
125
pkasting25702cb2016-01-08 13:50:27 -0800126void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
127 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000128 stereo[i * 2 + 1] = stereo[i * 2];
129 }
130}
131
yujo36b1a5f2017-06-12 12:45:32 -0700132void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800133 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000134 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
135 }
136}
137
138void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700139 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700140 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
141 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700142 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000143 }
144}
145
146void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800147 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700148 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700149 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700150 frame_data[i] = left;
151 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000152 }
153}
154
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000155void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700156 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700157 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
158 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700159 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000160 }
161}
162
andrew@webrtc.org81865342012-10-27 00:28:27 +0000163bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000164 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165 return false;
166 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
yujo36b1a5f2017-06-12 12:45:32 -0700170 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000172 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000173 return false;
174 }
175 return true;
176}
177
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000178void EnableAllAPComponents(AudioProcessing* ap) {
179#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000180 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000181
182 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
183 EXPECT_NOERR(ap->gain_control()->Enable(true));
184#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
185 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
186 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
187 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000188 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000189
190 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
191 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
192 EXPECT_NOERR(ap->gain_control()->Enable(true));
193#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000194
195 AudioProcessing::Config apm_config;
peah8271d042016-11-22 07:24:52 -0800196 apm_config.high_pass_filter.enabled = true;
197 ap->ApplyConfig(apm_config);
198
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000199 EXPECT_NOERR(ap->level_estimator()->Enable(true));
200 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
201
202 EXPECT_NOERR(ap->voice_detection()->Enable(true));
203}
204
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000205// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000206template <class T>
207T AbsValue(T a) {
208 return a > 0 ? a: -a;
209}
210
211int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800212 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700213 const int16_t* frame_data = frame.data();
214 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800215 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700216 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000217 }
218
219 return max_data;
220}
221
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000222#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000223void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000224 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700225 EXPECT_EQ(reference.instant(), test.instant);
226 EXPECT_EQ(reference.average(), test.average);
227 EXPECT_EQ(reference.maximum(), test.maximum);
228 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000229}
230
231void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000232 audioproc::Test::Statistic* msg) {
233 msg->set_instant(output.instant);
234 msg->set_average(output.average);
235 msg->set_maximum(output.maximum);
236 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000238#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239
Alex Loiko890988c2017-08-31 10:25:48 +0200240void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700241 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000243 ASSERT_TRUE(file != NULL);
244
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100245 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000246 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800247 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000248 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000249
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000250 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000251 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000253 fclose(file);
254}
255
Alex Loiko890988c2017-08-31 10:25:48 +0200256std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000257 std::ostringstream ss;
258 // Resource files are all stereo.
259 ss << name << sample_rate_hz / 1000 << "_stereo";
260 return test::ResourcePath(ss.str(), "pcm");
261}
262
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000263// Temporary filenames unique to this process. Used to be able to run these
264// tests in parallel as each process needs to be running in isolation they can't
265// have competing filenames.
266std::map<std::string, std::string> temp_filenames;
267
Alex Loiko890988c2017-08-31 10:25:48 +0200268std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000269 int input_rate,
270 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700271 int reverse_input_rate,
272 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800273 size_t num_input_channels,
274 size_t num_output_channels,
275 size_t num_reverse_input_channels,
276 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700277 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000278 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
280 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000281 if (num_output_channels == 1) {
282 ss << "mono";
283 } else if (num_output_channels == 2) {
284 ss << "stereo";
285 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700286 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000287 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700288 ss << output_rate / 1000;
289 if (num_reverse_output_channels == 1) {
290 ss << "_rmono";
291 } else if (num_reverse_output_channels == 2) {
292 ss << "_rstereo";
293 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700294 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700295 }
296 ss << reverse_output_rate / 1000;
297 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000298
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000299 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700300 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000301 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
302 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000303}
304
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000305void ClearTempFiles() {
306 for (auto& kv : temp_filenames)
307 remove(kv.second.c_str());
308}
309
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200310// Only remove "out" files. Keep "ref" files.
311void ClearTempOutFiles() {
312 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
313 const std::string& filename = it->first;
314 if (filename.substr(0, 3).compare("out") == 0) {
315 remove(it->second.c_str());
316 temp_filenames.erase(it++);
317 } else {
318 it++;
319 }
320 }
321}
322
Alex Loiko890988c2017-08-31 10:25:48 +0200323void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000324 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000325 ASSERT_TRUE(file != NULL);
326 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000327 fclose(file);
328}
329
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000330// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
331// stereo) file, converts to deinterleaved float (optionally downmixing) and
332// returns the result in |cb|. Returns false if the file ended (or on error) and
333// true otherwise.
334//
335// |int_data| and |float_data| are just temporary space that must be
336// sufficiently large to hold the 10 ms chunk.
337bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
338 ChannelBuffer<float>* cb) {
339 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000340 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000341 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
342 if (read_count != frame_size) {
343 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700344 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000345 return false; // This is expected.
346 }
347
348 S16ToFloat(int_data, frame_size, float_data);
349 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000350 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000351 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000352 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000353 cb->channels());
354 }
355
356 return true;
357}
358
niklase@google.com470e71d2011-07-07 08:21:25 +0000359class ApmTest : public ::testing::Test {
360 protected:
361 ApmTest();
362 virtual void SetUp();
363 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000364
365 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000366 }
367
368 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000369 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000370 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000371
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000372 // Used to select between int and float interface tests.
373 enum Format {
374 kIntFormat,
375 kFloatFormat
376 };
377
378 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000379 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000380 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800381 size_t num_input_channels,
382 size_t num_output_channels,
383 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000384 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000385 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000386 void EnableAllComponents();
387 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000388 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000389 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000390 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
391 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000392 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000393 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
394 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700395 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800396 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700397 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800398 void TestChangingForwardChannels(size_t num_in_channels,
399 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700400 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800401 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000403 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
404 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000405 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000406 int ProcessStreamChooser(Format format);
407 int AnalyzeReverseStreamChooser(Format format);
408 void ProcessDebugDump(const std::string& in_filename,
409 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800410 Format format,
411 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000412 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000413
414 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800416 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000417 AudioFrame* frame_;
418 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800419 std::unique_ptr<ChannelBuffer<float> > float_cb_;
420 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000421 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800422 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 FILE* far_file_;
424 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000425 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000426};
427
428ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000429 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000430#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800431 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
432 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000433#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000434#if defined(WEBRTC_MAC)
435 // A different file for Mac is needed because on this platform the AEC
436 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800437 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
438 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000439#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800440 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
441 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000442#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000443#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000445 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000446 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000447 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000448 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000449 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000450 out_file_(NULL) {
451 Config config;
452 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100453 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000454}
niklase@google.com470e71d2011-07-07 08:21:25 +0000455
456void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000457 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000458
459 frame_ = new AudioFrame();
460 revframe_ = new AudioFrame();
461
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000462 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
465void ApmTest::TearDown() {
466 if (frame_) {
467 delete frame_;
468 }
469 frame_ = NULL;
470
471 if (revframe_) {
472 delete revframe_;
473 }
474 revframe_ = NULL;
475
476 if (far_file_) {
477 ASSERT_EQ(0, fclose(far_file_));
478 }
479 far_file_ = NULL;
480
481 if (near_file_) {
482 ASSERT_EQ(0, fclose(near_file_));
483 }
484 near_file_ = NULL;
485
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000486 if (out_file_) {
487 ASSERT_EQ(0, fclose(out_file_));
488 }
489 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000490}
491
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000492void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000493 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700494 ap->Initialize(
495 {{{frame_->sample_rate_hz_, frame_->num_channels_},
496 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700497 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700498 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000499}
500
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000501void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000502 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000503 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800504 size_t num_input_channels,
505 size_t num_output_channels,
506 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000507 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000508 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000509 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000510 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000511
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000512 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
513 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000514 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000515
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000516 if (far_file_) {
517 ASSERT_EQ(0, fclose(far_file_));
518 }
519 std::string filename = ResourceFilePath("far", sample_rate_hz);
520 far_file_ = fopen(filename.c_str(), "rb");
521 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
522 filename << "\n";
523
524 if (near_file_) {
525 ASSERT_EQ(0, fclose(near_file_));
526 }
527 filename = ResourceFilePath("near", sample_rate_hz);
528 near_file_ = fopen(filename.c_str(), "rb");
529 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
530 filename << "\n";
531
532 if (open_output_file) {
533 if (out_file_) {
534 ASSERT_EQ(0, fclose(out_file_));
535 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700536 filename = OutputFilePath(
537 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
538 reverse_sample_rate_hz, num_input_channels, num_output_channels,
539 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000540 out_file_ = fopen(filename.c_str(), "wb");
541 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
542 filename << "\n";
543 }
544}
545
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000546void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000548}
549
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000550bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
551 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000552 // The files always contain stereo audio.
553 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700554 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000555 sizeof(int16_t),
556 frame_size,
557 file);
558 if (read_count != frame_size) {
559 // Check that the file really ended.
560 EXPECT_NE(0, feof(file));
561 return false; // This is expected.
562 }
563
564 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700565 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000566 frame->samples_per_channel_);
567 }
568
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000570 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000572 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000573}
574
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000575bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
576 return ReadFrame(file, frame, NULL);
577}
578
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000579// If the end of the file has been reached, rewind it and attempt to read the
580// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
582 ChannelBuffer<float>* cb) {
583 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000584 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000585 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000586 }
587}
588
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000589void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
590 ReadFrameWithRewind(file, frame, NULL);
591}
592
andrew@webrtc.org81865342012-10-27 00:28:27 +0000593void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
594 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000595 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000596 EXPECT_EQ(apm_->kNoError,
597 apm_->gain_control()->set_stream_analog_level(127));
598 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000599}
600
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000601int ApmTest::ProcessStreamChooser(Format format) {
602 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000603 return apm_->ProcessStream(frame_);
604 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000605 return apm_->ProcessStream(float_cb_->channels(),
606 frame_->samples_per_channel_,
607 frame_->sample_rate_hz_,
608 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000609 output_sample_rate_hz_,
610 LayoutFromChannels(num_output_channels_),
611 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000612}
613
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000614int ApmTest::AnalyzeReverseStreamChooser(Format format) {
615 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700616 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000617 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000618 return apm_->AnalyzeReverseStream(
619 revfloat_cb_->channels(),
620 revframe_->samples_per_channel_,
621 revframe_->sample_rate_hz_,
622 LayoutFromChannels(revframe_->num_channels_));
623}
624
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000625void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
626 int delay_min, int delay_max) {
627 // The |revframe_| and |frame_| should include the proper frame information,
628 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000629 AudioFrame tmp_frame;
630 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 bool causal = true;
632
633 tmp_frame.CopyFrom(*revframe_);
634 SetFrameTo(&tmp_frame, 0);
635
636 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
637 // Initialize the |frame_queue| with empty frames.
638 int frame_delay = delay_ms / 10;
639 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000640 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000641 frame->CopyFrom(tmp_frame);
642 frame_queue.push(frame);
643 frame_delay++;
644 causal = false;
645 }
646 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000647 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000648 frame->CopyFrom(tmp_frame);
649 frame_queue.push(frame);
650 frame_delay--;
651 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000652 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
653 // need enough frames with audio to have reliable estimates, but as few as
654 // possible to keep processing time down. 4.5 seconds seemed to be a good
655 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000657 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 frame->CopyFrom(tmp_frame);
659 // Use the near end recording, since that has more speech in it.
660 ASSERT_TRUE(ReadFrame(near_file_, frame));
661 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000662 AudioFrame* reverse_frame = frame;
663 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 if (!causal) {
665 reverse_frame = frame_queue.front();
666 // When we call ProcessStream() the frame is modified, so we can't use the
667 // pointer directly when things are non-causal. Use an intermediate frame
668 // and copy the data.
669 process_frame = &tmp_frame;
670 process_frame->CopyFrom(*frame);
671 }
aluebsb0319552016-03-17 20:39:53 -0700672 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000673 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
674 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
675 frame = frame_queue.front();
676 frame_queue.pop();
677 delete frame;
678
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000679 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000680 int median;
681 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000682 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 // Discard the first delay metrics to avoid convergence effects.
684 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000685 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
686 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000687 }
688 }
689
690 rewind(near_file_);
691 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000692 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000693 frame_queue.pop();
694 delete frame;
695 }
696 // Calculate expected delay estimate and acceptable regions. Further,
697 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700698 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700699 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700700 const int expected_median =
701 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
702 const int expected_median_high = rtc::SafeClamp<int>(
703 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700704 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700705 const int expected_median_low = rtc::SafeClamp<int>(
706 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700707 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000708 // Verify delay metrics.
709 int median;
710 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000711 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000712 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000713 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
714 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000715 EXPECT_GE(expected_median_high, median);
716 EXPECT_LE(expected_median_low, median);
717}
718
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000721 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000723 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000725 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000726 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000728 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 EXPECT_EQ(apm_->kNoError,
730 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000731 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000732 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000733 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000734
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000735 // Other stream parameters set correctly.
736 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000737 EXPECT_EQ(apm_->kNoError,
738 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000739 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000740 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
744 EXPECT_EQ(apm_->kNoError,
745 apm_->echo_cancellation()->enable_drift_compensation(false));
746
747 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000748 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000749 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000750 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000751 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000752
753 // Resets after successful ProcessStream().
754 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000757 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758
759 // Other stream parameters set correctly.
760 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
761 EXPECT_EQ(apm_->kNoError,
762 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000763 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000764 EXPECT_EQ(apm_->kNoError,
765 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000766 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000767 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000768 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
769
770 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000771 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000772 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000773
774 // Resets after successful ProcessStream().
775 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000776 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000777 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000778 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000779 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000780
781 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
783 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
784 EXPECT_EQ(apm_->kNoError,
785 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000786 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000787 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000788
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000789 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000790 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000791 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000792 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000793 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000794
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000795 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000796 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000797 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 EXPECT_EQ(apm_->kNoError,
799 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000800 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000801}
802
803TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000804 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000805}
806
807TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000808 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000811TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
812 EXPECT_EQ(0, apm_->delay_offset_ms());
813 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
814 EXPECT_EQ(50, apm_->stream_delay_ms());
815}
816
817TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
818 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000819 apm_->set_delay_offset_ms(100);
820 EXPECT_EQ(100, apm_->delay_offset_ms());
821 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000822 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000823 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
824 EXPECT_EQ(200, apm_->stream_delay_ms());
825
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000826 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000827 apm_->set_delay_offset_ms(-50);
828 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000829 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
830 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000831 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
832 EXPECT_EQ(50, apm_->stream_delay_ms());
833}
834
Michael Graczyk86c6d332015-07-23 11:41:39 -0700835void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800836 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700837 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000838 frame_->num_channels_ = num_channels;
839 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700840 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000841}
842
Michael Graczyk86c6d332015-07-23 11:41:39 -0700843void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800844 size_t num_in_channels,
845 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700846 AudioProcessing::Error expected_return) {
847 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
848 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
849
850 EXPECT_EQ(expected_return,
851 apm_->ProcessStream(float_cb_->channels(), input_stream,
852 output_stream, float_cb_->channels()));
853}
854
855void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800856 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700857 AudioProcessing::Error expected_return) {
858 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700859 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
860 {output_sample_rate_hz_, apm_->num_output_channels()},
861 {frame_->sample_rate_hz_, num_rev_channels},
862 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700863
ekmeyerson60d9b332015-08-14 10:35:55 -0700864 EXPECT_EQ(
865 expected_return,
866 apm_->ProcessReverseStream(
867 float_cb_->channels(), processing_config.reverse_input_stream(),
868 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700869}
870
871TEST_F(ApmTest, ChannelsInt16Interface) {
872 // Testing number of invalid and valid channels.
873 Init(16000, 16000, 16000, 4, 4, 4, false);
874
875 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
876
Peter Kasting69558702016-01-12 16:26:35 -0800877 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700878 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000879 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000880 }
881}
882
Michael Graczyk86c6d332015-07-23 11:41:39 -0700883TEST_F(ApmTest, Channels) {
884 // Testing number of invalid and valid channels.
885 Init(16000, 16000, 16000, 4, 4, 4, false);
886
887 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
888 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
889
Peter Kasting69558702016-01-12 16:26:35 -0800890 for (size_t i = 1; i < 4; ++i) {
891 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700892 // Output channels much be one or match input channels.
893 if (j == 1 || i == j) {
894 TestChangingForwardChannels(i, j, kNoErr);
895 TestChangingReverseChannels(i, kNoErr);
896
897 EXPECT_EQ(i, apm_->num_input_channels());
898 EXPECT_EQ(j, apm_->num_output_channels());
899 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800900 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700901 } else {
902 TestChangingForwardChannels(i, j,
903 AudioProcessing::kBadNumberChannelsError);
904 }
905 }
906 }
907}
908
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000909TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000910 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000911 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000912 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000913 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700914 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800915 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000916 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000917 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 }
919}
920
niklase@google.com470e71d2011-07-07 08:21:25 +0000921TEST_F(ApmTest, EchoCancellation) {
922 EXPECT_EQ(apm_->kNoError,
923 apm_->echo_cancellation()->enable_drift_compensation(true));
924 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
925 EXPECT_EQ(apm_->kNoError,
926 apm_->echo_cancellation()->enable_drift_compensation(false));
927 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
928
niklase@google.com470e71d2011-07-07 08:21:25 +0000929 EchoCancellation::SuppressionLevel level[] = {
930 EchoCancellation::kLowSuppression,
931 EchoCancellation::kModerateSuppression,
932 EchoCancellation::kHighSuppression,
933 };
pkasting25702cb2016-01-08 13:50:27 -0800934 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000935 EXPECT_EQ(apm_->kNoError,
936 apm_->echo_cancellation()->set_suppression_level(level[i]));
937 EXPECT_EQ(level[i],
938 apm_->echo_cancellation()->suppression_level());
939 }
940
941 EchoCancellation::Metrics metrics;
942 EXPECT_EQ(apm_->kNotEnabledError,
943 apm_->echo_cancellation()->GetMetrics(&metrics));
944
ivoc3e9a5372016-10-28 07:55:33 -0700945 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
946 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
947
niklase@google.com470e71d2011-07-07 08:21:25 +0000948 EXPECT_EQ(apm_->kNoError,
949 apm_->echo_cancellation()->enable_metrics(true));
950 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
951 EXPECT_EQ(apm_->kNoError,
952 apm_->echo_cancellation()->enable_metrics(false));
953 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
954
ivoc48dfab52016-10-28 03:29:31 -0700955 EXPECT_EQ(apm_->kNoError,
956 apm_->echo_cancellation()->enable_delay_logging(true));
957 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
958 EXPECT_EQ(apm_->kNoError,
959 apm_->echo_cancellation()->enable_delay_logging(false));
960 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
961
ivoc3e9a5372016-10-28 07:55:33 -0700962 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
963 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
964
965 int median = 0;
966 int std = 0;
967 float poor_fraction = 0;
968 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
969 &median, &std, &poor_fraction));
970
niklase@google.com470e71d2011-07-07 08:21:25 +0000971 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
972 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
973 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
974 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000975
976 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
977 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
978 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
979 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
980 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
981 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000982}
983
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000984TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000985 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000986 // Enable AEC only.
987 EXPECT_EQ(apm_->kNoError,
988 apm_->echo_cancellation()->enable_drift_compensation(false));
989 EXPECT_EQ(apm_->kNoError,
990 apm_->echo_cancellation()->enable_metrics(false));
991 EXPECT_EQ(apm_->kNoError,
992 apm_->echo_cancellation()->enable_delay_logging(true));
993 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000994 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700995 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000996 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000997
998 // Internally in the AEC the amount of lookahead the delay estimation can
999 // handle is 15 blocks and the maximum delay is set to 60 blocks.
1000 const int kLookaheadBlocks = 15;
1001 const int kMaxDelayBlocks = 60;
1002 // The AEC has a startup time before it actually starts to process. This
1003 // procedure can flush the internal far-end buffer, which of course affects
1004 // the delay estimation. Therefore, we set a system_delay high enough to
1005 // avoid that. The smallest system_delay you can report without flushing the
1006 // buffer is 66 ms in 8 kHz.
1007 //
1008 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1009 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1010 // delay estimation. This should be noted though. In case of test failure,
1011 // this could be the cause.
1012 const int kSystemDelayMs = 66;
1013 // Test a couple of corner cases and verify that the estimated delay is
1014 // within a valid region (set to +-1.5 blocks). Note that these cases are
1015 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001016 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001017 Init(kProcessSampleRates[i],
1018 kProcessSampleRates[i],
1019 kProcessSampleRates[i],
1020 2,
1021 2,
1022 2,
1023 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001024 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001025 const int num_ms_per_block =
1026 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001027 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1028 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1029
1030 // 1) Verify correct delay estimate at lookahead boundary.
1031 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1032 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1033 delay_max_ms);
1034 // 2) A delay less than maximum lookahead should give an delay estimate at
1035 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1036 delay_ms -= 20;
1037 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1038 delay_max_ms);
1039 // 3) Three values around zero delay. Note that we need to compensate for
1040 // the fake system_delay.
1041 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1042 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1043 delay_max_ms);
1044 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1045 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1046 delay_max_ms);
1047 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1048 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1049 delay_max_ms);
1050 // 4) Verify correct delay estimate at maximum delay boundary.
1051 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1052 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1053 delay_max_ms);
1054 // 5) A delay above the maximum delay should give an estimate at the
1055 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1056 delay_ms += 20;
1057 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1058 delay_max_ms);
1059 }
1060}
1061
niklase@google.com470e71d2011-07-07 08:21:25 +00001062TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001063 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001064 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1066 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1067
niklase@google.com470e71d2011-07-07 08:21:25 +00001068 // Toggle routing modes
1069 EchoControlMobile::RoutingMode mode[] = {
1070 EchoControlMobile::kQuietEarpieceOrHeadset,
1071 EchoControlMobile::kEarpiece,
1072 EchoControlMobile::kLoudEarpiece,
1073 EchoControlMobile::kSpeakerphone,
1074 EchoControlMobile::kLoudSpeakerphone,
1075 };
pkasting25702cb2016-01-08 13:50:27 -08001076 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001077 EXPECT_EQ(apm_->kNoError,
1078 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1079 EXPECT_EQ(mode[i],
1080 apm_->echo_control_mobile()->routing_mode());
1081 }
1082 // Turn comfort noise off/on
1083 EXPECT_EQ(apm_->kNoError,
1084 apm_->echo_control_mobile()->enable_comfort_noise(false));
1085 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1086 EXPECT_EQ(apm_->kNoError,
1087 apm_->echo_control_mobile()->enable_comfort_noise(true));
1088 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001089 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001090 const size_t echo_path_size =
1091 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001092 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1093 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001094 EXPECT_EQ(apm_->kNullPointerError,
1095 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1096 EXPECT_EQ(apm_->kNullPointerError,
1097 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1098 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001099 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001100 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001101 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001102 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001103 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001104 echo_path_in[i] = echo_path_out[i] + 1;
1105 }
1106 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001107 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001108 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001109 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1110 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001111 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001112 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1113 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001114 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001115 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1116 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001117
1118 // Process a few frames with NS in the default disabled state. This exercises
1119 // a different codepath than with it enabled.
1120 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1121 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1122 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1123 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1124
niklase@google.com470e71d2011-07-07 08:21:25 +00001125 // Turn AECM off
1126 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1127 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1128}
1129
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001130TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001132 EXPECT_EQ(apm_->kNoError,
1133 apm_->gain_control()->set_mode(
1134 apm_->gain_control()->mode()));
1135
1136 GainControl::Mode mode[] = {
1137 GainControl::kAdaptiveAnalog,
1138 GainControl::kAdaptiveDigital,
1139 GainControl::kFixedDigital
1140 };
pkasting25702cb2016-01-08 13:50:27 -08001141 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 EXPECT_EQ(apm_->kNoError,
1143 apm_->gain_control()->set_mode(mode[i]));
1144 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1145 }
1146 // Testing invalid target levels
1147 EXPECT_EQ(apm_->kBadParameterError,
1148 apm_->gain_control()->set_target_level_dbfs(-3));
1149 EXPECT_EQ(apm_->kBadParameterError,
1150 apm_->gain_control()->set_target_level_dbfs(-40));
1151 // Testing valid target levels
1152 EXPECT_EQ(apm_->kNoError,
1153 apm_->gain_control()->set_target_level_dbfs(
1154 apm_->gain_control()->target_level_dbfs()));
1155
1156 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001157 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 EXPECT_EQ(apm_->kNoError,
1159 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1160 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1161 }
1162
1163 // Testing invalid compression gains
1164 EXPECT_EQ(apm_->kBadParameterError,
1165 apm_->gain_control()->set_compression_gain_db(-1));
1166 EXPECT_EQ(apm_->kBadParameterError,
1167 apm_->gain_control()->set_compression_gain_db(100));
1168
1169 // Testing valid compression gains
1170 EXPECT_EQ(apm_->kNoError,
1171 apm_->gain_control()->set_compression_gain_db(
1172 apm_->gain_control()->compression_gain_db()));
1173
1174 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001175 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001176 EXPECT_EQ(apm_->kNoError,
1177 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1178 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1179 }
1180
1181 // Testing limiter off/on
1182 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1183 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1184 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1185 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1186
1187 // Testing invalid level limits
1188 EXPECT_EQ(apm_->kBadParameterError,
1189 apm_->gain_control()->set_analog_level_limits(-1, 512));
1190 EXPECT_EQ(apm_->kBadParameterError,
1191 apm_->gain_control()->set_analog_level_limits(100000, 512));
1192 EXPECT_EQ(apm_->kBadParameterError,
1193 apm_->gain_control()->set_analog_level_limits(512, -1));
1194 EXPECT_EQ(apm_->kBadParameterError,
1195 apm_->gain_control()->set_analog_level_limits(512, 100000));
1196 EXPECT_EQ(apm_->kBadParameterError,
1197 apm_->gain_control()->set_analog_level_limits(512, 255));
1198
1199 // Testing valid level limits
1200 EXPECT_EQ(apm_->kNoError,
1201 apm_->gain_control()->set_analog_level_limits(
1202 apm_->gain_control()->analog_level_minimum(),
1203 apm_->gain_control()->analog_level_maximum()));
1204
1205 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001206 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001207 EXPECT_EQ(apm_->kNoError,
1208 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1209 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1210 }
1211
1212 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001213 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001214 EXPECT_EQ(apm_->kNoError,
1215 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1216 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1217 }
1218
1219 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1220
1221 // Turn AGC off
1222 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1223 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1224}
1225
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001226void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001227 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001228 EXPECT_EQ(apm_->kNoError,
1229 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1230 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1231
1232 int out_analog_level = 0;
1233 for (int i = 0; i < 2000; ++i) {
1234 ReadFrameWithRewind(near_file_, frame_);
1235 // Ensure the audio is at a low level, so the AGC will try to increase it.
1236 ScaleFrame(frame_, 0.25);
1237
1238 // Always pass in the same volume.
1239 EXPECT_EQ(apm_->kNoError,
1240 apm_->gain_control()->set_stream_analog_level(100));
1241 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1242 out_analog_level = apm_->gain_control()->stream_analog_level();
1243 }
1244
1245 // Ensure the AGC is still able to reach the maximum.
1246 EXPECT_EQ(255, out_analog_level);
1247}
1248
1249// Verifies that despite volume slider quantization, the AGC can continue to
1250// increase its volume.
1251TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001252 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001253 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1254 }
1255}
1256
1257void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001258 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001259 EXPECT_EQ(apm_->kNoError,
1260 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1261 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1262
1263 int out_analog_level = 100;
1264 for (int i = 0; i < 1000; ++i) {
1265 ReadFrameWithRewind(near_file_, frame_);
1266 // Ensure the audio is at a low level, so the AGC will try to increase it.
1267 ScaleFrame(frame_, 0.25);
1268
1269 EXPECT_EQ(apm_->kNoError,
1270 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1271 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1272 out_analog_level = apm_->gain_control()->stream_analog_level();
1273 }
1274
1275 // Ensure the volume was raised.
1276 EXPECT_GT(out_analog_level, 100);
1277 int highest_level_reached = out_analog_level;
1278 // Simulate a user manual volume change.
1279 out_analog_level = 100;
1280
1281 for (int i = 0; i < 300; ++i) {
1282 ReadFrameWithRewind(near_file_, frame_);
1283 ScaleFrame(frame_, 0.25);
1284
1285 EXPECT_EQ(apm_->kNoError,
1286 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1287 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1288 out_analog_level = apm_->gain_control()->stream_analog_level();
1289 // Check that AGC respected the manually adjusted volume.
1290 EXPECT_LT(out_analog_level, highest_level_reached);
1291 }
1292 // Check that the volume was still raised.
1293 EXPECT_GT(out_analog_level, 100);
1294}
1295
1296TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001297 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001298 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1299 }
1300}
1301
niklase@google.com470e71d2011-07-07 08:21:25 +00001302TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001303 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001304 NoiseSuppression::Level level[] = {
1305 NoiseSuppression::kLow,
1306 NoiseSuppression::kModerate,
1307 NoiseSuppression::kHigh,
1308 NoiseSuppression::kVeryHigh
1309 };
pkasting25702cb2016-01-08 13:50:27 -08001310 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001311 EXPECT_EQ(apm_->kNoError,
1312 apm_->noise_suppression()->set_level(level[i]));
1313 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1314 }
1315
andrew@webrtc.org648af742012-02-08 01:57:29 +00001316 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001317 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1318 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1319 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1320 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1321}
1322
1323TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001324 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001325 AudioProcessing::Config apm_config;
1326 apm_config.high_pass_filter.enabled = true;
1327 apm_->ApplyConfig(apm_config);
1328 apm_config.high_pass_filter.enabled = false;
1329 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001330}
1331
1332TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001333 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001334 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001335 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001336
1337 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1338
1339 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1340 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1341
1342 // Run this test in wideband; in super-wb, the splitting filter distorts the
1343 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001344 frame_->samples_per_channel_ = 160;
1345 frame_->num_channels_ = 2;
1346 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001347
1348 // Min value if no frames have been processed.
1349 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1350
1351 // Min value on zero frames.
1352 SetFrameTo(frame_, 0);
1353 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1354 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1355 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1356
1357 // Try a few RMS values.
1358 // (These also test that the value resets after retrieving it.)
1359 SetFrameTo(frame_, 32767);
1360 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1361 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1362 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1363
1364 SetFrameTo(frame_, 30000);
1365 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1366 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1367 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1368
1369 SetFrameTo(frame_, 10000);
1370 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1371 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1372 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1373
1374 SetFrameTo(frame_, 10);
1375 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1376 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1377 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1378
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001379 // Verify reset after enable/disable.
1380 SetFrameTo(frame_, 32767);
1381 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1382 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1383 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1384 SetFrameTo(frame_, 1);
1385 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1386 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1387
1388 // Verify reset after initialize.
1389 SetFrameTo(frame_, 32767);
1390 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1391 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1392 SetFrameTo(frame_, 1);
1393 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1394 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001395}
1396
1397TEST_F(ApmTest, VoiceDetection) {
1398 // Test external VAD
1399 EXPECT_EQ(apm_->kNoError,
1400 apm_->voice_detection()->set_stream_has_voice(true));
1401 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1402 EXPECT_EQ(apm_->kNoError,
1403 apm_->voice_detection()->set_stream_has_voice(false));
1404 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1405
andrew@webrtc.org648af742012-02-08 01:57:29 +00001406 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 VoiceDetection::Likelihood likelihood[] = {
1408 VoiceDetection::kVeryLowLikelihood,
1409 VoiceDetection::kLowLikelihood,
1410 VoiceDetection::kModerateLikelihood,
1411 VoiceDetection::kHighLikelihood
1412 };
pkasting25702cb2016-01-08 13:50:27 -08001413 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001414 EXPECT_EQ(apm_->kNoError,
1415 apm_->voice_detection()->set_likelihood(likelihood[i]));
1416 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1417 }
1418
1419 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001420 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001421 EXPECT_EQ(apm_->kBadParameterError,
1422 apm_->voice_detection()->set_frame_size_ms(12));
1423
andrew@webrtc.org648af742012-02-08 01:57:29 +00001424 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 for (int i = 10; i <= 30; i += 10) {
1426 EXPECT_EQ(apm_->kNoError,
1427 apm_->voice_detection()->set_frame_size_ms(i));
1428 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1429 }
1430 */
1431
andrew@webrtc.org648af742012-02-08 01:57:29 +00001432 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001433 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1434 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1435 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1436 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1437
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001438 // Test that AudioFrame activity is maintained when VAD is disabled.
1439 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1440 AudioFrame::VADActivity activity[] = {
1441 AudioFrame::kVadActive,
1442 AudioFrame::kVadPassive,
1443 AudioFrame::kVadUnknown
1444 };
pkasting25702cb2016-01-08 13:50:27 -08001445 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001446 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001448 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001449 }
1450
1451 // Test that AudioFrame activity is set when VAD is enabled.
1452 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001453 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001454 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001455 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001456
niklase@google.com470e71d2011-07-07 08:21:25 +00001457 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1458}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001459
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001460TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1461 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1462 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1463 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1464 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1465 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1466 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1467 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1468}
1469
1470TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001471 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001472 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001473 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001474 AudioFrame frame_copy;
1475 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001476 for (int j = 0; j < 1000; j++) {
1477 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1478 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001479 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1480 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001481 }
1482 }
1483}
1484
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001485TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1486 // Test that ProcessStream copies input to output even with no processing.
1487 const size_t kSamples = 80;
1488 const int sample_rate = 8000;
1489 const float src[kSamples] = {
1490 -1.0f, 0.0f, 1.0f
1491 };
1492 float dest[kSamples] = {};
1493
1494 auto src_channels = &src[0];
1495 auto dest_channels = &dest[0];
1496
Ivo Creusen62337e52018-01-09 14:17:33 +01001497 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001498 EXPECT_NOERR(apm_->ProcessStream(
1499 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1500 sample_rate, LayoutFromChannels(1), &dest_channels));
1501
1502 for (size_t i = 0; i < kSamples; ++i) {
1503 EXPECT_EQ(src[i], dest[i]);
1504 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001505
1506 // Same for ProcessReverseStream.
1507 float rev_dest[kSamples] = {};
1508 auto rev_dest_channels = &rev_dest[0];
1509
1510 StreamConfig input_stream = {sample_rate, 1};
1511 StreamConfig output_stream = {sample_rate, 1};
1512 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1513 output_stream, &rev_dest_channels));
1514
1515 for (size_t i = 0; i < kSamples; ++i) {
1516 EXPECT_EQ(src[i], rev_dest[i]);
1517 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001518}
1519
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001520TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1521 EnableAllComponents();
1522
pkasting25702cb2016-01-08 13:50:27 -08001523 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001524 Init(kProcessSampleRates[i],
1525 kProcessSampleRates[i],
1526 kProcessSampleRates[i],
1527 2,
1528 2,
1529 2,
1530 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001531 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001532 ASSERT_EQ(0, feof(far_file_));
1533 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001534 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001535 CopyLeftToRightChannel(revframe_->mutable_data(),
1536 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001537
aluebsb0319552016-03-17 20:39:53 -07001538 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001539
yujo36b1a5f2017-06-12 12:45:32 -07001540 CopyLeftToRightChannel(frame_->mutable_data(),
1541 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001542 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1543
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001544 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001545 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001546 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001547 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001548 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001549 analog_level = apm_->gain_control()->stream_analog_level();
1550
yujo36b1a5f2017-06-12 12:45:32 -07001551 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001552 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001553 rewind(far_file_);
1554 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001555 }
1556}
1557
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001558TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001559 // Verify the filter is not active through undistorted audio when:
1560 // 1. No components are enabled...
1561 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001562 AudioFrame frame_copy;
1563 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001564 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1565 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1566 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1567
1568 // 2. Only the level estimator is enabled...
1569 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001570 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001571 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1572 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1573 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1574 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1575 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1576
1577 // 3. Only VAD is enabled...
1578 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001579 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001580 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1581 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1582 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1583 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1584 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1585
1586 // 4. Both VAD and the level estimator are enabled...
1587 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001588 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001589 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1590 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1591 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1592 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1593 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1594 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1595 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1596
1597 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001598 frame_->samples_per_channel_ = 160;
1599 frame_->num_channels_ = 2;
1600 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001601 // Enable AEC, which would require the filter in super-wb. We rely on the
1602 // first few frames of data being unaffected by the AEC.
1603 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1604 // behavior of the AEC. Think of something more robust.
1605 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001606 // Make sure we have extended filter enabled. This makes sure nothing is
1607 // touched until we have a farend frame.
1608 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001609 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001610 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001611 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001612 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001613 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001614 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001615 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1616 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001617 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001618 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1619 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1620
1621 // Check the test is valid. We should have distortion from the filter
1622 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001623 frame_->samples_per_channel_ = 320;
1624 frame_->num_channels_ = 2;
1625 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001626 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001627 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001628 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001629 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001630 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1631 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1632}
1633
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001634#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1635void ApmTest::ProcessDebugDump(const std::string& in_filename,
1636 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001637 Format format,
1638 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001639 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001640 FILE* in_file = fopen(in_filename.c_str(), "rb");
1641 ASSERT_TRUE(in_file != NULL);
1642 audioproc::Event event_msg;
1643 bool first_init = true;
1644
1645 while (ReadMessageFromFile(in_file, &event_msg)) {
1646 if (event_msg.type() == audioproc::Event::INIT) {
1647 const audioproc::Init msg = event_msg.init();
1648 int reverse_sample_rate = msg.sample_rate();
1649 if (msg.has_reverse_sample_rate()) {
1650 reverse_sample_rate = msg.reverse_sample_rate();
1651 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001652 int output_sample_rate = msg.sample_rate();
1653 if (msg.has_output_sample_rate()) {
1654 output_sample_rate = msg.output_sample_rate();
1655 }
1656
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001657 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001658 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001659 reverse_sample_rate,
1660 msg.num_input_channels(),
1661 msg.num_output_channels(),
1662 msg.num_reverse_channels(),
1663 false);
1664 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001665 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001666 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001667 auto aec_dump =
1668 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1669 EXPECT_TRUE(aec_dump);
1670 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001671 first_init = false;
1672 }
1673
1674 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1675 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1676
1677 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001678 ASSERT_EQ(revframe_->num_channels_,
1679 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001680 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001681 memcpy(revfloat_cb_->channels()[i],
1682 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001683 msg.channel(i).size());
1684 }
1685 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001686 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001687 if (format == kFloatFormat) {
1688 // We're using an int16 input file; convert to float.
1689 ConvertToFloat(*revframe_, revfloat_cb_.get());
1690 }
1691 }
1692 AnalyzeReverseStreamChooser(format);
1693
1694 } else if (event_msg.type() == audioproc::Event::STREAM) {
1695 const audioproc::Stream msg = event_msg.stream();
1696 // ProcessStream could have changed this for the output frame.
1697 frame_->num_channels_ = apm_->num_input_channels();
1698
1699 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1700 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1701 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1702 if (msg.has_keypress()) {
1703 apm_->set_stream_key_pressed(msg.keypress());
1704 } else {
1705 apm_->set_stream_key_pressed(true);
1706 }
1707
1708 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001709 ASSERT_EQ(frame_->num_channels_,
1710 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001711 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001712 memcpy(float_cb_->channels()[i],
1713 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001714 msg.input_channel(i).size());
1715 }
1716 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001717 memcpy(frame_->mutable_data(), msg.input_data().data(),
1718 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001719 if (format == kFloatFormat) {
1720 // We're using an int16 input file; convert to float.
1721 ConvertToFloat(*frame_, float_cb_.get());
1722 }
1723 }
1724 ProcessStreamChooser(format);
1725 }
1726 }
aleloif4dd1912017-06-15 01:55:38 -07001727 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001728 fclose(in_file);
1729}
1730
1731void ApmTest::VerifyDebugDumpTest(Format format) {
1732 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001733 std::string format_string;
1734 switch (format) {
1735 case kIntFormat:
1736 format_string = "_int";
1737 break;
1738 case kFloatFormat:
1739 format_string = "_float";
1740 break;
1741 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001742 const std::string ref_filename = test::TempFilename(
1743 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1744 const std::string out_filename = test::TempFilename(
1745 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001746 const std::string limited_filename = test::TempFilename(
1747 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1748 const size_t logging_limit_bytes = 100000;
1749 // We expect at least this many bytes in the created logfile.
1750 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001751 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001752 ProcessDebugDump(in_filename, ref_filename, format, -1);
1753 ProcessDebugDump(ref_filename, out_filename, format, -1);
1754 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001755
1756 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1757 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001758 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001759 ASSERT_TRUE(ref_file != NULL);
1760 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001761 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001762 std::unique_ptr<uint8_t[]> ref_bytes;
1763 std::unique_ptr<uint8_t[]> out_bytes;
1764 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001765
1766 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1767 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001768 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001769 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001770 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001771 while (ref_size > 0 && out_size > 0) {
1772 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001773 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001774 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001775 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001776 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001777 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001778 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1779 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001780 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001781 }
1782 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001783 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1784 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001785 EXPECT_NE(0, feof(ref_file));
1786 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001787 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001788 ASSERT_EQ(0, fclose(ref_file));
1789 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001790 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001791 remove(ref_filename.c_str());
1792 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001793 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001794}
1795
pbosc7a65692016-05-06 12:50:04 -07001796TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001797 VerifyDebugDumpTest(kIntFormat);
1798}
1799
pbosc7a65692016-05-06 12:50:04 -07001800TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001801 VerifyDebugDumpTest(kFloatFormat);
1802}
1803#endif
1804
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001805// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001806TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001807 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001808 const std::string filename =
1809 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001810 {
1811 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1812 EXPECT_FALSE(aec_dump);
1813 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001814
1815#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1816 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001817 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001818
aleloif4dd1912017-06-15 01:55:38 -07001819 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1820 EXPECT_TRUE(aec_dump);
1821 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001822 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001823 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001824 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001825
1826 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001827 FILE* fid = fopen(filename.c_str(), "r");
1828 ASSERT_TRUE(fid != NULL);
1829
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001830 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001831 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001832 ASSERT_EQ(0, remove(filename.c_str()));
1833#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001834 // Verify the file has NOT been written.
1835 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1836#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1837}
1838
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001839// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001840TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001841 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1842
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001843 const std::string filename =
1844 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001845 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001846 ASSERT_TRUE(fid);
1847
1848#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1849 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001850 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001851
aleloif4dd1912017-06-15 01:55:38 -07001852 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1853 EXPECT_TRUE(aec_dump);
1854 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001855 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001856 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001857 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001858
1859 // Verify the file has been written.
1860 fid = fopen(filename.c_str(), "r");
1861 ASSERT_TRUE(fid != NULL);
1862
1863 // Clean it up.
1864 ASSERT_EQ(0, fclose(fid));
1865 ASSERT_EQ(0, remove(filename.c_str()));
1866#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001867 ASSERT_EQ(0, fclose(fid));
1868#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1869}
1870
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001871TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001872 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001873 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001874
1875 Config config;
1876 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001877 std::unique_ptr<AudioProcessing> fapm(
1878 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001879 EnableAllComponents();
1880 EnableAllAPComponents(fapm.get());
1881 for (int i = 0; i < ref_data.test_size(); i++) {
1882 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1883
1884 audioproc::Test* test = ref_data.mutable_test(i);
1885 // TODO(ajm): Restore downmixing test cases.
1886 if (test->num_input_channels() != test->num_output_channels())
1887 continue;
1888
Peter Kasting69558702016-01-12 16:26:35 -08001889 const size_t num_render_channels =
1890 static_cast<size_t>(test->num_reverse_channels());
1891 const size_t num_input_channels =
1892 static_cast<size_t>(test->num_input_channels());
1893 const size_t num_output_channels =
1894 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001895 const size_t samples_per_channel = static_cast<size_t>(
1896 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001897
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001898 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1899 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001900 Init(fapm.get());
1901
1902 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001903 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1904 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001905
1906 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001907 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001908 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1909 ReadFrame(near_file_, frame_, float_cb_.get())) {
1910 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1911
aluebsb0319552016-03-17 20:39:53 -07001912 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001913 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1914 revfloat_cb_->channels(),
1915 samples_per_channel,
1916 test->sample_rate(),
1917 LayoutFromChannels(num_render_channels)));
1918
1919 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1920 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1921 apm_->echo_cancellation()->set_stream_drift_samples(0);
1922 fapm->echo_cancellation()->set_stream_drift_samples(0);
1923 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1924 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1925
1926 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001927 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001928 output_int16.channels());
1929
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001930 EXPECT_NOERR(fapm->ProcessStream(
1931 float_cb_->channels(),
1932 samples_per_channel,
1933 test->sample_rate(),
1934 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001935 test->sample_rate(),
1936 LayoutFromChannels(num_output_channels),
1937 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001938 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001939 FloatToS16(float_cb_->channels()[j],
1940 samples_per_channel,
1941 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001942 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001943 float snr = ComputeSNR(output_int16.channels()[j],
1944 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001945 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001946
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001947 const float kVarianceThreshold = 20;
1948 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001949
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001950 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001951 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1952 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001953 }
1954 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001955
1956 analog_level = fapm->gain_control()->stream_analog_level();
1957 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1958 fapm->gain_control()->stream_analog_level());
1959 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
1960 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001961 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1962 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001963 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001964
1965 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001966 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001967 }
aluebs776593b2016-03-15 14:04:58 -07001968
1969#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1970 const size_t kMaxNumBadChunks = 0;
1971#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1972 // There are a few chunks in the fixed-point profile that give low SNR.
1973 // Listening confirmed the difference is acceptable.
1974 const size_t kMaxNumBadChunks = 60;
1975#endif
1976 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1977
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001978 rewind(far_file_);
1979 rewind(near_file_);
1980 }
1981}
1982
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001983// TODO(andrew): Add a test to process a few frames with different combinations
1984// of enabled components.
1985
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001986TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001987 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001988 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001989
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001990 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001991 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001992 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001993 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001994 for (size_t i = 0; i < arraysize(kChannels); i++) {
1995 for (size_t j = 0; j < arraysize(kChannels); j++) {
1996 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001997 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001998 test->set_num_reverse_channels(kChannels[i]);
1999 test->set_num_input_channels(kChannels[j]);
2000 test->set_num_output_channels(kChannels[j]);
2001 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002002 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002003 }
2004 }
2005 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002006#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2007 // To test the extended filter mode.
2008 audioproc::Test* test = ref_data.add_test();
2009 test->set_num_reverse_channels(2);
2010 test->set_num_input_channels(2);
2011 test->set_num_output_channels(2);
2012 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2013 test->set_use_aec_extended_filter(true);
2014#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002015 }
2016
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002017 for (int i = 0; i < ref_data.test_size(); i++) {
2018 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002019
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002020 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002021 // TODO(ajm): We no longer allow different input and output channels. Skip
2022 // these tests for now, but they should be removed from the set.
2023 if (test->num_input_channels() != test->num_output_channels())
2024 continue;
2025
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002026 Config config;
2027 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002028 config.Set<ExtendedFilter>(
2029 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01002030 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002031
2032 EnableAllComponents();
2033
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002034 Init(test->sample_rate(),
2035 test->sample_rate(),
2036 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002037 static_cast<size_t>(test->num_input_channels()),
2038 static_cast<size_t>(test->num_output_channels()),
2039 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002040 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002041
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002042 int frame_count = 0;
2043 int has_echo_count = 0;
2044 int has_voice_count = 0;
2045 int is_saturated_count = 0;
2046 int analog_level = 127;
2047 int analog_level_average = 0;
2048 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002049 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002050#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2051 int stats_index = 0;
2052#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002053
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002054 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002055 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002056
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002057 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2058
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002059 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002060 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002061 EXPECT_EQ(apm_->kNoError,
2062 apm_->gain_control()->set_stream_analog_level(analog_level));
2063
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002064 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002065
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002066 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002067 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2068 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002069
2070 max_output_average += MaxAudioFrame(*frame_);
2071
2072 if (apm_->echo_cancellation()->stream_has_echo()) {
2073 has_echo_count++;
2074 }
2075
2076 analog_level = apm_->gain_control()->stream_analog_level();
2077 analog_level_average += analog_level;
2078 if (apm_->gain_control()->stream_is_saturated()) {
2079 is_saturated_count++;
2080 }
2081 if (apm_->voice_detection()->stream_has_voice()) {
2082 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002083 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002084 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002085 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002086 }
2087
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002088 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2089
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002090 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002091 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002092 sizeof(int16_t),
2093 frame_size,
2094 out_file_);
2095 ASSERT_EQ(frame_size, write_count);
2096
2097 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002098 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002099 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002100
2101#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2102 const int kStatsAggregationFrameNum = 100; // 1 second.
2103 if (frame_count % kStatsAggregationFrameNum == 0) {
2104 // Get echo metrics.
2105 EchoCancellation::Metrics echo_metrics;
2106 EXPECT_EQ(apm_->kNoError,
2107 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2108
2109 // Get delay metrics.
2110 int median = 0;
2111 int std = 0;
2112 float fraction_poor_delays = 0;
2113 EXPECT_EQ(apm_->kNoError,
2114 apm_->echo_cancellation()->GetDelayMetrics(
2115 &median, &std, &fraction_poor_delays));
2116
2117 // Get RMS.
2118 int rms_level = apm_->level_estimator()->RMS();
2119 EXPECT_LE(0, rms_level);
2120 EXPECT_GE(127, rms_level);
2121
2122 if (!write_ref_data) {
2123 const audioproc::Test::EchoMetrics& reference =
2124 test->echo_metrics(stats_index);
2125 TestStats(echo_metrics.residual_echo_return_loss,
2126 reference.residual_echo_return_loss());
2127 TestStats(echo_metrics.echo_return_loss,
2128 reference.echo_return_loss());
2129 TestStats(echo_metrics.echo_return_loss_enhancement,
2130 reference.echo_return_loss_enhancement());
2131 TestStats(echo_metrics.a_nlp,
2132 reference.a_nlp());
2133 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2134 reference.divergent_filter_fraction());
2135
2136 const audioproc::Test::DelayMetrics& reference_delay =
2137 test->delay_metrics(stats_index);
2138 EXPECT_EQ(reference_delay.median(), median);
2139 EXPECT_EQ(reference_delay.std(), std);
2140 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2141 fraction_poor_delays);
2142
2143 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2144
2145 ++stats_index;
2146 } else {
2147 audioproc::Test::EchoMetrics* message =
2148 test->add_echo_metrics();
2149 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2150 message->mutable_residual_echo_return_loss());
2151 WriteStatsMessage(echo_metrics.echo_return_loss,
2152 message->mutable_echo_return_loss());
2153 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2154 message->mutable_echo_return_loss_enhancement());
2155 WriteStatsMessage(echo_metrics.a_nlp,
2156 message->mutable_a_nlp());
2157 message->set_divergent_filter_fraction(
2158 echo_metrics.divergent_filter_fraction);
2159
2160 audioproc::Test::DelayMetrics* message_delay =
2161 test->add_delay_metrics();
2162 message_delay->set_median(median);
2163 message_delay->set_std(std);
2164 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2165
2166 test->add_rms_level(rms_level);
2167 }
2168 }
2169#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002170 }
2171 max_output_average /= frame_count;
2172 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002173 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002174
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002175 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002176 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002177 // When running the test on a N7 we get a {2, 6} difference of
2178 // |has_voice_count| and |max_output_average| is up to 18 higher.
2179 // All numbers being consistently higher on N7 compare to ref_data.
2180 // TODO(bjornv): If we start getting more of these offsets on Android we
2181 // should consider a different approach. Either using one slack for all,
2182 // or generate a separate android reference.
2183#if defined(WEBRTC_ANDROID)
2184 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002185 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002186 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002187 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002188#else
2189 const int kHasVoiceCountOffset = 0;
2190 const int kHasVoiceCountNear = kIntNear;
2191 const int kMaxOutputAverageOffset = 0;
2192 const int kMaxOutputAverageNear = kIntNear;
2193#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002194 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002195 EXPECT_NEAR(test->has_voice_count(),
2196 has_voice_count - kHasVoiceCountOffset,
2197 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002198 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002199
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002200 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002201 EXPECT_NEAR(test->max_output_average(),
2202 max_output_average - kMaxOutputAverageOffset,
2203 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002204#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002205 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002206 EXPECT_NEAR(test->ns_speech_probability_average(),
2207 ns_speech_prob_average,
2208 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002209#endif
2210 } else {
2211 test->set_has_echo_count(has_echo_count);
2212 test->set_has_voice_count(has_voice_count);
2213 test->set_is_saturated_count(is_saturated_count);
2214
2215 test->set_analog_level_average(analog_level_average);
2216 test->set_max_output_average(max_output_average);
2217
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002218#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002219 EXPECT_LE(0.0f, ns_speech_prob_average);
2220 EXPECT_GE(1.0f, ns_speech_prob_average);
2221 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002222#endif
2223 }
2224
2225 rewind(far_file_);
2226 rewind(near_file_);
2227 }
2228
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002229 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002230 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002231 }
2232}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002233
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002234TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2235 struct ChannelFormat {
2236 AudioProcessing::ChannelLayout in_layout;
2237 AudioProcessing::ChannelLayout out_layout;
2238 };
2239 ChannelFormat cf[] = {
2240 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2241 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2242 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2243 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002244
Ivo Creusen62337e52018-01-09 14:17:33 +01002245 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002246 // Enable one component just to ensure some processing takes place.
2247 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002248 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002249 const int in_rate = 44100;
2250 const int out_rate = 48000;
2251 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2252 TotalChannelsFromLayout(cf[i].in_layout));
2253 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2254 ChannelsFromLayout(cf[i].out_layout));
2255
2256 // Run over a few chunks.
2257 for (int j = 0; j < 10; ++j) {
2258 EXPECT_NOERR(ap->ProcessStream(
2259 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002260 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002261 in_rate,
2262 cf[i].in_layout,
2263 out_rate,
2264 cf[i].out_layout,
2265 out_cb.channels()));
2266 }
2267 }
2268}
2269
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002270// Compares the reference and test arrays over a region around the expected
2271// delay. Finds the highest SNR in that region and adds the variance and squared
2272// error results to the supplied accumulators.
2273void UpdateBestSNR(const float* ref,
2274 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002275 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002276 int expected_delay,
2277 double* variance_acc,
2278 double* sq_error_acc) {
2279 double best_snr = std::numeric_limits<double>::min();
2280 double best_variance = 0;
2281 double best_sq_error = 0;
2282 // Search over a region of eight samples around the expected delay.
2283 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2284 ++delay) {
2285 double sq_error = 0;
2286 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002287 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002288 double error = test[i + delay] - ref[i];
2289 sq_error += error * error;
2290 variance += ref[i] * ref[i];
2291 }
2292
2293 if (sq_error == 0) {
2294 *variance_acc += variance;
2295 return;
2296 }
2297 double snr = variance / sq_error;
2298 if (snr > best_snr) {
2299 best_snr = snr;
2300 best_variance = variance;
2301 best_sq_error = sq_error;
2302 }
2303 }
2304
2305 *variance_acc += best_variance;
2306 *sq_error_acc += best_sq_error;
2307}
2308
2309// Used to test a multitude of sample rate and channel combinations. It works
2310// by first producing a set of reference files (in SetUpTestCase) that are
2311// assumed to be correct, as the used parameters are verified by other tests
2312// in this collection. Primarily the reference files are all produced at
2313// "native" rates which do not involve any resampling.
2314
2315// Each test pass produces an output file with a particular format. The output
2316// is matched against the reference file closest to its internal processing
2317// format. If necessary the output is resampled back to its process format.
2318// Due to the resampling distortion, we don't expect identical results, but
2319// enforce SNR thresholds which vary depending on the format. 0 is a special
2320// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002321typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002322class AudioProcessingTest
2323 : public testing::TestWithParam<AudioProcessingTestData> {
2324 public:
2325 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002326 : input_rate_(std::get<0>(GetParam())),
2327 output_rate_(std::get<1>(GetParam())),
2328 reverse_input_rate_(std::get<2>(GetParam())),
2329 reverse_output_rate_(std::get<3>(GetParam())),
2330 expected_snr_(std::get<4>(GetParam())),
2331 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002332
2333 virtual ~AudioProcessingTest() {}
2334
2335 static void SetUpTestCase() {
2336 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002337 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002338 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002339 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2340 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2341 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002342 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002343 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2344 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2345 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002346 }
2347 }
2348 }
2349 }
2350
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002351 void TearDown() {
2352 // Remove "out" files after each test.
2353 ClearTempOutFiles();
2354 }
2355
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002356 static void TearDownTestCase() {
2357 ClearTempFiles();
2358 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002359
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002360 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002361 // to a file specified with |output_file_prefix|. Both forward and reverse
2362 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002363 static void ProcessFormat(int input_rate,
2364 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002365 int reverse_input_rate,
2366 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002367 size_t num_input_channels,
2368 size_t num_output_channels,
2369 size_t num_reverse_input_channels,
2370 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002371 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002372 Config config;
2373 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002374 std::unique_ptr<AudioProcessing> ap(
2375 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002376 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002377
ekmeyerson60d9b332015-08-14 10:35:55 -07002378 ProcessingConfig processing_config = {
2379 {{input_rate, num_input_channels},
2380 {output_rate, num_output_channels},
2381 {reverse_input_rate, num_reverse_input_channels},
2382 {reverse_output_rate, num_reverse_output_channels}}};
2383 ap->Initialize(processing_config);
2384
2385 FILE* far_file =
2386 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002387 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002388 FILE* out_file =
2389 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2390 reverse_input_rate, reverse_output_rate,
2391 num_input_channels, num_output_channels,
2392 num_reverse_input_channels,
2393 num_reverse_output_channels, kForward).c_str(),
2394 "wb");
2395 FILE* rev_out_file =
2396 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2397 reverse_input_rate, reverse_output_rate,
2398 num_input_channels, num_output_channels,
2399 num_reverse_input_channels,
2400 num_reverse_output_channels, kReverse).c_str(),
2401 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002402 ASSERT_TRUE(far_file != NULL);
2403 ASSERT_TRUE(near_file != NULL);
2404 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002405 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002406
2407 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2408 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002409 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2410 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002411 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2412 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002413 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2414 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002415
2416 // Temporary buffers.
2417 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002418 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2419 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002420 std::unique_ptr<float[]> float_data(new float[max_length]);
2421 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002422
2423 int analog_level = 127;
2424 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2425 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002426 EXPECT_NOERR(ap->ProcessReverseStream(
2427 rev_cb.channels(), processing_config.reverse_input_stream(),
2428 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429
2430 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2431 ap->echo_cancellation()->set_stream_drift_samples(0);
2432 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2433
2434 EXPECT_NOERR(ap->ProcessStream(
2435 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002436 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002437 input_rate,
2438 LayoutFromChannels(num_input_channels),
2439 output_rate,
2440 LayoutFromChannels(num_output_channels),
2441 out_cb.channels()));
2442
ekmeyerson60d9b332015-08-14 10:35:55 -07002443 // Dump forward output to file.
2444 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002445 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002446 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002447
pkasting25702cb2016-01-08 13:50:27 -08002448 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002449 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002450 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002451
ekmeyerson60d9b332015-08-14 10:35:55 -07002452 // Dump reverse output to file.
2453 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2454 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002455 size_t rev_out_length =
2456 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002457
pkasting25702cb2016-01-08 13:50:27 -08002458 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002459 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2460 rev_out_file));
2461
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002462 analog_level = ap->gain_control()->stream_analog_level();
2463 }
2464 fclose(far_file);
2465 fclose(near_file);
2466 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002467 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002468 }
2469
2470 protected:
2471 int input_rate_;
2472 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002473 int reverse_input_rate_;
2474 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002475 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002476 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002477};
2478
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002479TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002480 struct ChannelFormat {
2481 int num_input;
2482 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002483 int num_reverse_input;
2484 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002485 };
2486 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002487 {1, 1, 1, 1},
2488 {1, 1, 2, 1},
2489 {2, 1, 1, 1},
2490 {2, 1, 2, 1},
2491 {2, 2, 1, 1},
2492 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002493 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002494
pkasting25702cb2016-01-08 13:50:27 -08002495 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002496 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2497 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2498 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002499
ekmeyerson60d9b332015-08-14 10:35:55 -07002500 // Verify output for both directions.
2501 std::vector<StreamDirection> stream_directions;
2502 stream_directions.push_back(kForward);
2503 stream_directions.push_back(kReverse);
2504 for (StreamDirection file_direction : stream_directions) {
2505 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2506 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2507 const int out_num =
2508 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2509 const double expected_snr =
2510 file_direction ? expected_reverse_snr_ : expected_snr_;
2511
2512 const int min_ref_rate = std::min(in_rate, out_rate);
2513 int ref_rate;
2514
2515 if (min_ref_rate > 32000) {
2516 ref_rate = 48000;
2517 } else if (min_ref_rate > 16000) {
2518 ref_rate = 32000;
2519 } else if (min_ref_rate > 8000) {
2520 ref_rate = 16000;
2521 } else {
2522 ref_rate = 8000;
2523 }
aluebs776593b2016-03-15 14:04:58 -07002524#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002525 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002526 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002527 }
2528#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002529 FILE* out_file = fopen(
2530 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2531 reverse_output_rate_, cf[i].num_input,
2532 cf[i].num_output, cf[i].num_reverse_input,
2533 cf[i].num_reverse_output, file_direction).c_str(),
2534 "rb");
2535 // The reference files always have matching input and output channels.
2536 FILE* ref_file = fopen(
2537 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2538 cf[i].num_output, cf[i].num_output,
2539 cf[i].num_reverse_output, cf[i].num_reverse_output,
2540 file_direction).c_str(),
2541 "rb");
2542 ASSERT_TRUE(out_file != NULL);
2543 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002544
pkasting25702cb2016-01-08 13:50:27 -08002545 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2546 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002547 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002548 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002549 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002550 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002551 // Data from the resampled output, in case the reference and output rates
2552 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002553 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002554
ekmeyerson60d9b332015-08-14 10:35:55 -07002555 PushResampler<float> resampler;
2556 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002557
ekmeyerson60d9b332015-08-14 10:35:55 -07002558 // Compute the resampling delay of the output relative to the reference,
2559 // to find the region over which we should search for the best SNR.
2560 float expected_delay_sec = 0;
2561 if (in_rate != ref_rate) {
2562 // Input resampling delay.
2563 expected_delay_sec +=
2564 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2565 }
2566 if (out_rate != ref_rate) {
2567 // Output resampling delay.
2568 expected_delay_sec +=
2569 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2570 // Delay of converting the output back to its processing rate for
2571 // testing.
2572 expected_delay_sec +=
2573 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2574 }
2575 int expected_delay =
2576 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002577
ekmeyerson60d9b332015-08-14 10:35:55 -07002578 double variance = 0;
2579 double sq_error = 0;
2580 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2581 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2582 float* out_ptr = out_data.get();
2583 if (out_rate != ref_rate) {
2584 // Resample the output back to its internal processing rate if
2585 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002586 ASSERT_EQ(ref_length,
2587 static_cast<size_t>(resampler.Resample(
2588 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002589 out_ptr = cmp_data.get();
2590 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002591
ekmeyerson60d9b332015-08-14 10:35:55 -07002592 // Update the |sq_error| and |variance| accumulators with the highest
2593 // SNR of reference vs output.
2594 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2595 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002596 }
2597
ekmeyerson60d9b332015-08-14 10:35:55 -07002598 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2599 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2600 << cf[i].num_input << ", " << cf[i].num_output << ", "
2601 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2602 << ", " << file_direction << "): ";
2603 if (sq_error > 0) {
2604 double snr = 10 * log10(variance / sq_error);
2605 EXPECT_GE(snr, expected_snr);
2606 EXPECT_NE(0, expected_snr);
2607 std::cout << "SNR=" << snr << " dB" << std::endl;
2608 } else {
aluebs776593b2016-03-15 14:04:58 -07002609 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002610 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002611
ekmeyerson60d9b332015-08-14 10:35:55 -07002612 fclose(out_file);
2613 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002614 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002615 }
2616}
2617
2618#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2619INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002620 CommonFormats,
2621 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002622 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2623 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2624 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2625 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2626 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2627 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2628 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2629 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2630 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2631 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2632 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2633 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002634
Edward Lemurc5ee9872017-10-23 23:33:04 +02002635 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2636 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2637 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2638 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2639 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2640 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2641 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2642 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2643 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2644 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2645 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2646 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002647
Edward Lemurc5ee9872017-10-23 23:33:04 +02002648 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2649 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2650 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2651 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2652 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2653 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2654 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2655 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2656 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2657 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2658 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2659 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002660
Edward Lemurc5ee9872017-10-23 23:33:04 +02002661 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2662 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2663 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2664 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2665 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2666 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2667 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2668 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2669 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2670 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2671 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2672 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002673
2674#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2675INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002676 CommonFormats,
2677 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002678 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2679 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2680 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2681 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2682 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2683 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2684 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2685 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2686 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2687 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2688 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2689 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002690
Edward Lemurc5ee9872017-10-23 23:33:04 +02002691 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2692 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2693 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2694 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2695 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2696 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2697 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2698 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2699 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2700 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2701 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2702 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002703
Edward Lemurc5ee9872017-10-23 23:33:04 +02002704 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2705 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2706 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2707 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2708 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2709 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2710 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2711 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2712 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2713 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2714 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2715 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002716
Edward Lemurc5ee9872017-10-23 23:33:04 +02002717 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2718 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2719 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2720 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2721 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2722 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2723 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2724 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2725 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2726 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2727 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2728 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002729#endif
2730
niklase@google.com470e71d2011-07-07 08:21:25 +00002731} // namespace
peahc19f3122016-10-07 14:54:10 -07002732
Alessio Bazzicac054e782018-04-16 12:10:09 +02002733TEST(RuntimeSettingTest, TestDefaultCtor) {
2734 auto s = AudioProcessing::RuntimeSetting();
2735 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2736}
2737
2738TEST(RuntimeSettingTest, TestCapturePreGain) {
2739 using Type = AudioProcessing::RuntimeSetting::Type;
2740 {
2741 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2742 EXPECT_EQ(Type::kCapturePreGain, s.type());
2743 float v;
2744 s.GetFloat(&v);
2745 EXPECT_EQ(1.25f, v);
2746 }
2747
2748#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2749 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2750#endif
2751}
2752
2753TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2754 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2755 auto s = AudioProcessing::RuntimeSetting();
2756 ASSERT_TRUE(q.Insert(&s));
2757 ASSERT_TRUE(q.Remove(&s));
2758 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2759}
2760
Sam Zackrisson0beac582017-09-25 12:04:02 +02002761TEST(ApmConfiguration, EnablePostProcessing) {
2762 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002763 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002764 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002765 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002766 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002767 rtc::scoped_refptr<AudioProcessing> apm =
2768 AudioProcessingBuilder()
2769 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002770 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002771
2772 AudioFrame audio;
2773 audio.num_channels_ = 1;
2774 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2775
2776 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002777 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002778}
2779
Alex Loiko5825aa62017-12-18 16:02:40 +01002780TEST(ApmConfiguration, EnablePreProcessing) {
2781 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002782 auto mock_pre_processor_ptr =
2783 new testing::NiceMock<test::MockCustomProcessing>();
2784 auto mock_pre_processor =
2785 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002786 rtc::scoped_refptr<AudioProcessing> apm =
2787 AudioProcessingBuilder()
2788 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002789 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002790
2791 AudioFrame audio;
2792 audio.num_channels_ = 1;
2793 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2794
2795 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2796 apm->ProcessReverseStream(&audio);
2797}
2798
Alex Loiko73ec0192018-05-15 10:52:28 +02002799TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2800 auto mock_pre_processor_ptr =
2801 new testing::NiceMock<test::MockCustomProcessing>();
2802 auto mock_pre_processor =
2803 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2804 rtc::scoped_refptr<AudioProcessing> apm =
2805 AudioProcessingBuilder()
2806 .SetRenderPreProcessing(std::move(mock_pre_processor))
2807 .Create();
2808 apm->SetRuntimeSetting(
2809 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2810
2811 // RuntimeSettings forwarded during 'Process*Stream' calls.
2812 // Therefore we have to make one such call.
2813 AudioFrame audio;
2814 audio.num_channels_ = 1;
2815 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2816
2817 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2818 apm->ProcessReverseStream(&audio);
2819}
2820
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002821class MyEchoControlFactory : public EchoControlFactory {
2822 public:
2823 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2824 auto ec = new test::MockEchoControl();
2825 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2826 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2827 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2828 return std::unique_ptr<EchoControl>(ec);
2829 }
2830};
2831
2832TEST(ApmConfiguration, EchoControlInjection) {
2833 // Verify that apm uses an injected echo controller if one is provided.
2834 webrtc::Config webrtc_config;
2835 std::unique_ptr<EchoControlFactory> echo_control_factory(
2836 new MyEchoControlFactory());
2837
Alex Loiko5825aa62017-12-18 16:02:40 +01002838 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002839 AudioProcessingBuilder()
2840 .SetEchoControlFactory(std::move(echo_control_factory))
2841 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002842
2843 AudioFrame audio;
2844 audio.num_channels_ = 1;
2845 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2846 apm->ProcessStream(&audio);
2847 apm->ProcessReverseStream(&audio);
2848 apm->ProcessStream(&audio);
2849}
Ivo Creusenae026092017-11-20 13:07:16 +01002850
2851std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2852 Config old_config;
2853 if (use_AEC2) {
2854 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2855 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2856 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002857 std::unique_ptr<AudioProcessing> apm(
2858 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002859 if (!apm) {
2860 return apm;
2861 }
2862
2863 ProcessingConfig processing_config = {
2864 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2865
2866 if (apm->Initialize(processing_config) != 0) {
2867 return nullptr;
2868 }
2869
2870 // Disable all components except for an AEC and the residual echo detector.
2871 AudioProcessing::Config config;
2872 config.residual_echo_detector.enabled = true;
Ivo Creusenae026092017-11-20 13:07:16 +01002873 config.high_pass_filter.enabled = false;
2874 config.gain_controller2.enabled = false;
Ivo Creusenae026092017-11-20 13:07:16 +01002875 apm->ApplyConfig(config);
2876 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2877 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2878 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2879 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
2880
2881 if (use_AEC2) {
2882 EXPECT_EQ(apm->echo_control_mobile()->Enable(false), 0);
2883 EXPECT_EQ(apm->echo_cancellation()->enable_metrics(true), 0);
2884 EXPECT_EQ(apm->echo_cancellation()->enable_delay_logging(true), 0);
2885 EXPECT_EQ(apm->echo_cancellation()->Enable(true), 0);
2886 } else {
2887 EXPECT_EQ(apm->echo_cancellation()->Enable(false), 0);
2888 EXPECT_EQ(apm->echo_control_mobile()->Enable(true), 0);
2889 }
2890 return apm;
2891}
2892
2893#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2894#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2895#else
2896#define MAYBE_ApmStatistics ApmStatistics
2897#endif
2898
2899TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2900 // Set up APM with AEC2 and process some audio.
2901 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2902 ASSERT_TRUE(apm);
2903
2904 // Set up an audioframe.
2905 AudioFrame frame;
2906 frame.num_channels_ = 1;
2907 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2908
2909 // Fill the audio frame with a sawtooth pattern.
2910 int16_t* ptr = frame.mutable_data();
2911 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2912 ptr[i] = 10000 * ((i % 3) - 1);
2913 }
2914
2915 // Do some processing.
2916 for (int i = 0; i < 200; i++) {
2917 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2918 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2919 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2920 }
2921
2922 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002923 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002924 // We expect all statistics to be set and have a sensible value.
2925 ASSERT_TRUE(stats.residual_echo_likelihood);
2926 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2927 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2928 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2929 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2930 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2931 ASSERT_TRUE(stats.echo_return_loss);
2932 EXPECT_NE(*stats.echo_return_loss, -100.0);
2933 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2934 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2935 ASSERT_TRUE(stats.divergent_filter_fraction);
2936 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2937 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2938 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2939 // We don't check stats.delay_median_ms since it takes too long to settle to a
2940 // value. At least 20 seconds of data need to be processed before it will get
2941 // a value, which would make this test take too much time.
2942
2943 // If there are no receive streams, we expect the stats not to be set. The
2944 // 'false' argument signals to APM that no receive streams are currently
2945 // active. In that situation the statistics would get stuck at their last
2946 // calculated value (AEC and echo detection need at least one stream in each
2947 // direction), so to avoid that, they should not be set by APM.
2948 stats = apm->GetStatistics(false);
2949 EXPECT_FALSE(stats.residual_echo_likelihood);
2950 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2951 EXPECT_FALSE(stats.echo_return_loss);
2952 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2953 EXPECT_FALSE(stats.divergent_filter_fraction);
2954 EXPECT_FALSE(stats.delay_median_ms);
2955 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2956}
2957
2958TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2959 // Set up APM with AECM and process some audio.
2960 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2961 ASSERT_TRUE(apm);
2962
2963 // Set up an audioframe.
2964 AudioFrame frame;
2965 frame.num_channels_ = 1;
2966 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2967
2968 // Fill the audio frame with a sawtooth pattern.
2969 int16_t* ptr = frame.mutable_data();
2970 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2971 ptr[i] = 10000 * ((i % 3) - 1);
2972 }
2973
2974 // Do some processing.
2975 for (int i = 0; i < 200; i++) {
2976 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2977 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2978 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2979 }
2980
2981 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002982 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002983 // We expect only the residual echo detector statistics to be set and have a
2984 // sensible value.
2985 EXPECT_TRUE(stats.residual_echo_likelihood);
2986 if (stats.residual_echo_likelihood) {
2987 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2988 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2989 }
2990 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2991 if (stats.residual_echo_likelihood_recent_max) {
2992 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2993 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2994 }
2995 EXPECT_FALSE(stats.echo_return_loss);
2996 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2997 EXPECT_FALSE(stats.divergent_filter_fraction);
2998 EXPECT_FALSE(stats.delay_median_ms);
2999 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3000
3001 // If there are no receive streams, we expect the stats not to be set.
3002 stats = apm->GetStatistics(false);
3003 EXPECT_FALSE(stats.residual_echo_likelihood);
3004 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
3005 EXPECT_FALSE(stats.echo_return_loss);
3006 EXPECT_FALSE(stats.echo_return_loss_enhancement);
3007 EXPECT_FALSE(stats.divergent_filter_fraction);
3008 EXPECT_FALSE(stats.delay_median_ms);
3009 EXPECT_FALSE(stats.delay_standard_deviation_ms);
3010}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00003011} // namespace webrtc