blob: 4540a6a3a3ff45b20ac24cec700f37246c86ce1e [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"
24#include "modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
25#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020027#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/level_controller/level_controller_constants.h"
29#include "modules/audio_processing/test/protobuf_utils.h"
30#include "modules/audio_processing/test/test_utils.h"
31#include "modules/include/module_common_types.h"
32#include "rtc_base/arraysize.h"
33#include "rtc_base/checks.h"
34#include "rtc_base/gtest_prod_util.h"
35#include "rtc_base/ignore_wundef.h"
36#include "rtc_base/protobuf_utils.h"
Niels Möller84255bb2017-10-06 13:43:23 +020037#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/safe_minmax.h"
39#include "rtc_base/task_queue.h"
40#include "rtc_base/thread.h"
41#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "test/gtest.h"
43#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070044
45RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000046#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000047#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#endif
kwiberg77eab702016-09-28 17:42:01 -070051RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000052
andrew@webrtc.org27c69802014-02-18 20:24:56 +000053namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000054namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000055
ekmeyerson60d9b332015-08-14 10:35:55 -070056// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
57// applicable.
58
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000059// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000060// re-implemented; see comment at the bottom of this file. For now, the user has
61// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000062// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000063// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000064// be set to true with the command-line switch --write_ref_data.
65bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070066const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070067const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000068
aluebseb3603b2016-04-20 15:27:58 -070069#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
70// Android doesn't support 48kHz.
71const int kProcessSampleRates[] = {8000, 16000, 32000};
72#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070073const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070074#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000075
ekmeyerson60d9b332015-08-14 10:35:55 -070076enum StreamDirection { kForward = 0, kReverse };
77
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000078void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000079 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080 cb->num_channels());
81 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000082 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000083 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000084 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080085 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000086 S16ToFloat(cb_int.channels()[i],
87 cb->num_frames(),
88 cb->channels()[i]);
89 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000090}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000091
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070093 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094}
95
andrew@webrtc.org103657b2014-04-24 18:28:56 +000096// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080097size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098 switch (layout) {
99 case AudioProcessing::kMono:
100 return 1;
101 case AudioProcessing::kMonoAndKeyboard:
102 case AudioProcessing::kStereo:
103 return 2;
104 case AudioProcessing::kStereoAndKeyboard:
105 return 3;
106 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700107 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800108 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000109}
110
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000111int TruncateToMultipleOf10(int value) {
112 return (value / 10) * 10;
113}
114
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000115void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800116 size_t samples_per_channel) {
117 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000118 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000119}
120
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000121void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800122 size_t samples_per_channel) {
123 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000124 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
125}
126
pkasting25702cb2016-01-08 13:50:27 -0800127void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
128 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000129 stereo[i * 2 + 1] = stereo[i * 2];
130 }
131}
132
yujo36b1a5f2017-06-12 12:45:32 -0700133void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800134 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000135 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
136 }
137}
138
139void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700140 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700141 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
142 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700143 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000144 }
145}
146
147void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800148 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700149 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700150 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700151 frame_data[i] = left;
152 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000153 }
154}
155
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000156void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700157 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700158 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
159 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700160 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000161 }
162}
163
andrew@webrtc.org81865342012-10-27 00:28:27 +0000164bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000165 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166 return false;
167 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000168 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000169 return false;
170 }
yujo36b1a5f2017-06-12 12:45:32 -0700171 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000172 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000173 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 return false;
175 }
176 return true;
177}
178
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000179void EnableAllAPComponents(AudioProcessing* ap) {
180#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
181 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
182
183 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
184 EXPECT_NOERR(ap->gain_control()->Enable(true));
185#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
186 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
187 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
188 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
189 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
190
191 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
192 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
193 EXPECT_NOERR(ap->gain_control()->Enable(true));
194#endif
195
peah8271d042016-11-22 07:24:52 -0800196 AudioProcessing::Config apm_config;
197 apm_config.high_pass_filter.enabled = true;
198 ap->ApplyConfig(apm_config);
199
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000200 EXPECT_NOERR(ap->level_estimator()->Enable(true));
201 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
202
203 EXPECT_NOERR(ap->voice_detection()->Enable(true));
204}
205
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000206// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000207template <class T>
208T AbsValue(T a) {
209 return a > 0 ? a: -a;
210}
211
212int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800213 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700214 const int16_t* frame_data = frame.data();
215 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800216 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700217 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000218 }
219
220 return max_data;
221}
222
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000223#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000224void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000225 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700226 EXPECT_EQ(reference.instant(), test.instant);
227 EXPECT_EQ(reference.average(), test.average);
228 EXPECT_EQ(reference.maximum(), test.maximum);
229 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230}
231
232void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000233 audioproc::Test::Statistic* msg) {
234 msg->set_instant(output.instant);
235 msg->set_average(output.average);
236 msg->set_maximum(output.maximum);
237 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000239#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240
Alex Loiko890988c2017-08-31 10:25:48 +0200241void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700242 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000243 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000244 ASSERT_TRUE(file != NULL);
245
246 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800248 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000249 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000250
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000251 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000252 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000253 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000254 fclose(file);
255}
256
Alex Loiko890988c2017-08-31 10:25:48 +0200257std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000258 std::ostringstream ss;
259 // Resource files are all stereo.
260 ss << name << sample_rate_hz / 1000 << "_stereo";
261 return test::ResourcePath(ss.str(), "pcm");
262}
263
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000264// Temporary filenames unique to this process. Used to be able to run these
265// tests in parallel as each process needs to be running in isolation they can't
266// have competing filenames.
267std::map<std::string, std::string> temp_filenames;
268
Alex Loiko890988c2017-08-31 10:25:48 +0200269std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000270 int input_rate,
271 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700272 int reverse_input_rate,
273 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800274 size_t num_input_channels,
275 size_t num_output_channels,
276 size_t num_reverse_input_channels,
277 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700278 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000279 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700280 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
281 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282 if (num_output_channels == 1) {
283 ss << "mono";
284 } else if (num_output_channels == 2) {
285 ss << "stereo";
286 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700287 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000288 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700289 ss << output_rate / 1000;
290 if (num_reverse_output_channels == 1) {
291 ss << "_rmono";
292 } else if (num_reverse_output_channels == 2) {
293 ss << "_rstereo";
294 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700295 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700296 }
297 ss << reverse_output_rate / 1000;
298 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000299
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000300 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700301 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000302 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
303 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000304}
305
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000306void ClearTempFiles() {
307 for (auto& kv : temp_filenames)
308 remove(kv.second.c_str());
309}
310
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200311// Only remove "out" files. Keep "ref" files.
312void ClearTempOutFiles() {
313 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
314 const std::string& filename = it->first;
315 if (filename.substr(0, 3).compare("out") == 0) {
316 remove(it->second.c_str());
317 temp_filenames.erase(it++);
318 } else {
319 it++;
320 }
321 }
322}
323
Alex Loiko890988c2017-08-31 10:25:48 +0200324void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000325 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000326 ASSERT_TRUE(file != NULL);
327 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000328 fclose(file);
329}
330
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000331// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
332// stereo) file, converts to deinterleaved float (optionally downmixing) and
333// returns the result in |cb|. Returns false if the file ended (or on error) and
334// true otherwise.
335//
336// |int_data| and |float_data| are just temporary space that must be
337// sufficiently large to hold the 10 ms chunk.
338bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
339 ChannelBuffer<float>* cb) {
340 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000341 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000342 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
343 if (read_count != frame_size) {
344 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700345 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000346 return false; // This is expected.
347 }
348
349 S16ToFloat(int_data, frame_size, float_data);
350 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000351 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000352 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000353 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000354 cb->channels());
355 }
356
357 return true;
358}
359
niklase@google.com470e71d2011-07-07 08:21:25 +0000360class ApmTest : public ::testing::Test {
361 protected:
362 ApmTest();
363 virtual void SetUp();
364 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000365
366 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000367 }
368
369 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000370 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000371 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000372
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000373 // Used to select between int and float interface tests.
374 enum Format {
375 kIntFormat,
376 kFloatFormat
377 };
378
379 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000380 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000381 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800382 size_t num_input_channels,
383 size_t num_output_channels,
384 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000385 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000386 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000387 void EnableAllComponents();
388 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000389 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000390 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000391 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
392 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000393 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000394 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
395 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700396 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800397 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700398 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800399 void TestChangingForwardChannels(size_t num_in_channels,
400 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700401 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800402 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700403 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000404 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
405 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000406 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000407 int ProcessStreamChooser(Format format);
408 int AnalyzeReverseStreamChooser(Format format);
409 void ProcessDebugDump(const std::string& in_filename,
410 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800411 Format format,
412 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000413 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000414
415 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000416 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800417 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000418 AudioFrame* frame_;
419 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800420 std::unique_ptr<ChannelBuffer<float> > float_cb_;
421 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000422 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800423 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000424 FILE* far_file_;
425 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000426 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427};
428
429ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000430 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000431#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800432 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
433 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000434#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000435#if defined(WEBRTC_MAC)
436 // A different file for Mac is needed because on this platform the AEC
437 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800438 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
439 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000440#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800441 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
442 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000443#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000444#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000445 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000446 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000447 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000448 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000449 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000450 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000451 out_file_(NULL) {
452 Config config;
453 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
454 apm_.reset(AudioProcessing::Create(config));
455}
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
457void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000458 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
460 frame_ = new AudioFrame();
461 revframe_ = new AudioFrame();
462
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000463 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000464}
465
466void ApmTest::TearDown() {
467 if (frame_) {
468 delete frame_;
469 }
470 frame_ = NULL;
471
472 if (revframe_) {
473 delete revframe_;
474 }
475 revframe_ = NULL;
476
477 if (far_file_) {
478 ASSERT_EQ(0, fclose(far_file_));
479 }
480 far_file_ = NULL;
481
482 if (near_file_) {
483 ASSERT_EQ(0, fclose(near_file_));
484 }
485 near_file_ = NULL;
486
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000487 if (out_file_) {
488 ASSERT_EQ(0, fclose(out_file_));
489 }
490 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491}
492
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000493void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000494 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700495 ap->Initialize(
496 {{{frame_->sample_rate_hz_, frame_->num_channels_},
497 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700498 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700499 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000500}
501
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000502void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000503 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000504 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800505 size_t num_input_channels,
506 size_t num_output_channels,
507 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000508 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000509 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000510 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000511 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000512
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000513 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
514 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000515 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000516
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000517 if (far_file_) {
518 ASSERT_EQ(0, fclose(far_file_));
519 }
520 std::string filename = ResourceFilePath("far", sample_rate_hz);
521 far_file_ = fopen(filename.c_str(), "rb");
522 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
523 filename << "\n";
524
525 if (near_file_) {
526 ASSERT_EQ(0, fclose(near_file_));
527 }
528 filename = ResourceFilePath("near", sample_rate_hz);
529 near_file_ = fopen(filename.c_str(), "rb");
530 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
531 filename << "\n";
532
533 if (open_output_file) {
534 if (out_file_) {
535 ASSERT_EQ(0, fclose(out_file_));
536 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700537 filename = OutputFilePath(
538 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
539 reverse_sample_rate_hz, num_input_channels, num_output_channels,
540 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000541 out_file_ = fopen(filename.c_str(), "wb");
542 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
543 filename << "\n";
544 }
545}
546
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000547void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000549}
550
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000551bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
552 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000553 // The files always contain stereo audio.
554 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700555 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000556 sizeof(int16_t),
557 frame_size,
558 file);
559 if (read_count != frame_size) {
560 // Check that the file really ended.
561 EXPECT_NE(0, feof(file));
562 return false; // This is expected.
563 }
564
565 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700566 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000567 frame->samples_per_channel_);
568 }
569
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000570 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000571 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000573 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000574}
575
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
577 return ReadFrame(file, frame, NULL);
578}
579
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000580// If the end of the file has been reached, rewind it and attempt to read the
581// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
583 ChannelBuffer<float>* cb) {
584 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000585 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000587 }
588}
589
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
591 ReadFrameWithRewind(file, frame, NULL);
592}
593
andrew@webrtc.org81865342012-10-27 00:28:27 +0000594void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
595 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000596 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000597 EXPECT_EQ(apm_->kNoError,
598 apm_->gain_control()->set_stream_analog_level(127));
599 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000600}
601
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000602int ApmTest::ProcessStreamChooser(Format format) {
603 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000604 return apm_->ProcessStream(frame_);
605 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000606 return apm_->ProcessStream(float_cb_->channels(),
607 frame_->samples_per_channel_,
608 frame_->sample_rate_hz_,
609 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000610 output_sample_rate_hz_,
611 LayoutFromChannels(num_output_channels_),
612 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000613}
614
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000615int ApmTest::AnalyzeReverseStreamChooser(Format format) {
616 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700617 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000618 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000619 return apm_->AnalyzeReverseStream(
620 revfloat_cb_->channels(),
621 revframe_->samples_per_channel_,
622 revframe_->sample_rate_hz_,
623 LayoutFromChannels(revframe_->num_channels_));
624}
625
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000626void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
627 int delay_min, int delay_max) {
628 // The |revframe_| and |frame_| should include the proper frame information,
629 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000630 AudioFrame tmp_frame;
631 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000632 bool causal = true;
633
634 tmp_frame.CopyFrom(*revframe_);
635 SetFrameTo(&tmp_frame, 0);
636
637 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
638 // Initialize the |frame_queue| with empty frames.
639 int frame_delay = delay_ms / 10;
640 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000641 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000642 frame->CopyFrom(tmp_frame);
643 frame_queue.push(frame);
644 frame_delay++;
645 causal = false;
646 }
647 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000648 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000649 frame->CopyFrom(tmp_frame);
650 frame_queue.push(frame);
651 frame_delay--;
652 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000653 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
654 // need enough frames with audio to have reliable estimates, but as few as
655 // possible to keep processing time down. 4.5 seconds seemed to be a good
656 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000657 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000658 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000659 frame->CopyFrom(tmp_frame);
660 // Use the near end recording, since that has more speech in it.
661 ASSERT_TRUE(ReadFrame(near_file_, frame));
662 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000663 AudioFrame* reverse_frame = frame;
664 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000665 if (!causal) {
666 reverse_frame = frame_queue.front();
667 // When we call ProcessStream() the frame is modified, so we can't use the
668 // pointer directly when things are non-causal. Use an intermediate frame
669 // and copy the data.
670 process_frame = &tmp_frame;
671 process_frame->CopyFrom(*frame);
672 }
aluebsb0319552016-03-17 20:39:53 -0700673 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000674 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
675 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
676 frame = frame_queue.front();
677 frame_queue.pop();
678 delete frame;
679
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000680 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000681 int median;
682 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000683 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000684 // Discard the first delay metrics to avoid convergence effects.
685 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000686 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
687 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000688 }
689 }
690
691 rewind(near_file_);
692 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000693 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000694 frame_queue.pop();
695 delete frame;
696 }
697 // Calculate expected delay estimate and acceptable regions. Further,
698 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700699 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700700 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700701 const int expected_median =
702 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
703 const int expected_median_high = rtc::SafeClamp<int>(
704 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700705 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700706 const int expected_median_low = rtc::SafeClamp<int>(
707 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700708 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000709 // Verify delay metrics.
710 int median;
711 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000712 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000713 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000714 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
715 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000716 EXPECT_GE(expected_median_high, median);
717 EXPECT_LE(expected_median_low, median);
718}
719
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000720void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000721 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000723
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000724 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000726 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000727 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000728
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 EXPECT_EQ(apm_->kNoError,
731 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000733 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000734 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000736 // Other stream parameters set correctly.
737 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000738 EXPECT_EQ(apm_->kNoError,
739 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000740 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000741 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000742 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000743 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000744 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
745 EXPECT_EQ(apm_->kNoError,
746 apm_->echo_cancellation()->enable_drift_compensation(false));
747
748 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000749 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000750 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000751 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000752 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753
754 // Resets after successful ProcessStream().
755 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000756 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000757 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000758 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000759
760 // Other stream parameters set correctly.
761 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
762 EXPECT_EQ(apm_->kNoError,
763 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000764 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000765 EXPECT_EQ(apm_->kNoError,
766 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000767 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000768 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000769 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
770
771 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000772 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000773 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000774
775 // Resets after successful ProcessStream().
776 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000777 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000779 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000780 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000781
782 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
784 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
785 EXPECT_EQ(apm_->kNoError,
786 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000787 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000788 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000790 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000791 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000792 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000793 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000794 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000795
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000796 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000797 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000798 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000799 EXPECT_EQ(apm_->kNoError,
800 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000801 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000802}
803
804TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000805 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000806}
807
808TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000809 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000812TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
813 EXPECT_EQ(0, apm_->delay_offset_ms());
814 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
815 EXPECT_EQ(50, apm_->stream_delay_ms());
816}
817
818TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
819 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000820 apm_->set_delay_offset_ms(100);
821 EXPECT_EQ(100, apm_->delay_offset_ms());
822 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000823 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000824 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
825 EXPECT_EQ(200, apm_->stream_delay_ms());
826
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000827 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000828 apm_->set_delay_offset_ms(-50);
829 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000830 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
831 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000832 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
833 EXPECT_EQ(50, apm_->stream_delay_ms());
834}
835
Michael Graczyk86c6d332015-07-23 11:41:39 -0700836void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800837 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700838 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000839 frame_->num_channels_ = num_channels;
840 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700841 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000842}
843
Michael Graczyk86c6d332015-07-23 11:41:39 -0700844void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800845 size_t num_in_channels,
846 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700847 AudioProcessing::Error expected_return) {
848 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
849 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
850
851 EXPECT_EQ(expected_return,
852 apm_->ProcessStream(float_cb_->channels(), input_stream,
853 output_stream, float_cb_->channels()));
854}
855
856void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800857 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700858 AudioProcessing::Error expected_return) {
859 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700860 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
861 {output_sample_rate_hz_, apm_->num_output_channels()},
862 {frame_->sample_rate_hz_, num_rev_channels},
863 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700864
ekmeyerson60d9b332015-08-14 10:35:55 -0700865 EXPECT_EQ(
866 expected_return,
867 apm_->ProcessReverseStream(
868 float_cb_->channels(), processing_config.reverse_input_stream(),
869 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700870}
871
872TEST_F(ApmTest, ChannelsInt16Interface) {
873 // Testing number of invalid and valid channels.
874 Init(16000, 16000, 16000, 4, 4, 4, false);
875
876 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
877
Peter Kasting69558702016-01-12 16:26:35 -0800878 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700879 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000880 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000881 }
882}
883
Michael Graczyk86c6d332015-07-23 11:41:39 -0700884TEST_F(ApmTest, Channels) {
885 // Testing number of invalid and valid channels.
886 Init(16000, 16000, 16000, 4, 4, 4, false);
887
888 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
889 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
890
Peter Kasting69558702016-01-12 16:26:35 -0800891 for (size_t i = 1; i < 4; ++i) {
892 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700893 // Output channels much be one or match input channels.
894 if (j == 1 || i == j) {
895 TestChangingForwardChannels(i, j, kNoErr);
896 TestChangingReverseChannels(i, kNoErr);
897
898 EXPECT_EQ(i, apm_->num_input_channels());
899 EXPECT_EQ(j, apm_->num_output_channels());
900 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800901 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700902 } else {
903 TestChangingForwardChannels(i, j,
904 AudioProcessing::kBadNumberChannelsError);
905 }
906 }
907 }
908}
909
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000910TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000911 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000912 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000913 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700915 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800916 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000917 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000918 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000919 }
920}
921
niklase@google.com470e71d2011-07-07 08:21:25 +0000922TEST_F(ApmTest, EchoCancellation) {
923 EXPECT_EQ(apm_->kNoError,
924 apm_->echo_cancellation()->enable_drift_compensation(true));
925 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
926 EXPECT_EQ(apm_->kNoError,
927 apm_->echo_cancellation()->enable_drift_compensation(false));
928 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
929
niklase@google.com470e71d2011-07-07 08:21:25 +0000930 EchoCancellation::SuppressionLevel level[] = {
931 EchoCancellation::kLowSuppression,
932 EchoCancellation::kModerateSuppression,
933 EchoCancellation::kHighSuppression,
934 };
pkasting25702cb2016-01-08 13:50:27 -0800935 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000936 EXPECT_EQ(apm_->kNoError,
937 apm_->echo_cancellation()->set_suppression_level(level[i]));
938 EXPECT_EQ(level[i],
939 apm_->echo_cancellation()->suppression_level());
940 }
941
942 EchoCancellation::Metrics metrics;
943 EXPECT_EQ(apm_->kNotEnabledError,
944 apm_->echo_cancellation()->GetMetrics(&metrics));
945
ivoc3e9a5372016-10-28 07:55:33 -0700946 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
947 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
948
niklase@google.com470e71d2011-07-07 08:21:25 +0000949 EXPECT_EQ(apm_->kNoError,
950 apm_->echo_cancellation()->enable_metrics(true));
951 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
952 EXPECT_EQ(apm_->kNoError,
953 apm_->echo_cancellation()->enable_metrics(false));
954 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
955
ivoc48dfab52016-10-28 03:29:31 -0700956 EXPECT_EQ(apm_->kNoError,
957 apm_->echo_cancellation()->enable_delay_logging(true));
958 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
959 EXPECT_EQ(apm_->kNoError,
960 apm_->echo_cancellation()->enable_delay_logging(false));
961 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
962
ivoc3e9a5372016-10-28 07:55:33 -0700963 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
964 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
965
966 int median = 0;
967 int std = 0;
968 float poor_fraction = 0;
969 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
970 &median, &std, &poor_fraction));
971
niklase@google.com470e71d2011-07-07 08:21:25 +0000972 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
973 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
974 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
975 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000976
977 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
978 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
979 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
980 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
981 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
982 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000983}
984
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000985TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000986 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000987 // Enable AEC only.
988 EXPECT_EQ(apm_->kNoError,
989 apm_->echo_cancellation()->enable_drift_compensation(false));
990 EXPECT_EQ(apm_->kNoError,
991 apm_->echo_cancellation()->enable_metrics(false));
992 EXPECT_EQ(apm_->kNoError,
993 apm_->echo_cancellation()->enable_delay_logging(true));
994 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000995 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700996 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000997 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000998
999 // Internally in the AEC the amount of lookahead the delay estimation can
1000 // handle is 15 blocks and the maximum delay is set to 60 blocks.
1001 const int kLookaheadBlocks = 15;
1002 const int kMaxDelayBlocks = 60;
1003 // The AEC has a startup time before it actually starts to process. This
1004 // procedure can flush the internal far-end buffer, which of course affects
1005 // the delay estimation. Therefore, we set a system_delay high enough to
1006 // avoid that. The smallest system_delay you can report without flushing the
1007 // buffer is 66 ms in 8 kHz.
1008 //
1009 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1010 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1011 // delay estimation. This should be noted though. In case of test failure,
1012 // this could be the cause.
1013 const int kSystemDelayMs = 66;
1014 // Test a couple of corner cases and verify that the estimated delay is
1015 // within a valid region (set to +-1.5 blocks). Note that these cases are
1016 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001017 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001018 Init(kProcessSampleRates[i],
1019 kProcessSampleRates[i],
1020 kProcessSampleRates[i],
1021 2,
1022 2,
1023 2,
1024 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001025 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001026 const int num_ms_per_block =
1027 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001028 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1029 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1030
1031 // 1) Verify correct delay estimate at lookahead boundary.
1032 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1033 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1034 delay_max_ms);
1035 // 2) A delay less than maximum lookahead should give an delay estimate at
1036 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1037 delay_ms -= 20;
1038 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1039 delay_max_ms);
1040 // 3) Three values around zero delay. Note that we need to compensate for
1041 // the fake system_delay.
1042 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1043 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1044 delay_max_ms);
1045 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1046 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1047 delay_max_ms);
1048 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1049 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1050 delay_max_ms);
1051 // 4) Verify correct delay estimate at maximum delay boundary.
1052 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1053 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1054 delay_max_ms);
1055 // 5) A delay above the maximum delay should give an estimate at the
1056 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1057 delay_ms += 20;
1058 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1059 delay_max_ms);
1060 }
1061}
1062
niklase@google.com470e71d2011-07-07 08:21:25 +00001063TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001064 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001065 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1067 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1068
niklase@google.com470e71d2011-07-07 08:21:25 +00001069 // Toggle routing modes
1070 EchoControlMobile::RoutingMode mode[] = {
1071 EchoControlMobile::kQuietEarpieceOrHeadset,
1072 EchoControlMobile::kEarpiece,
1073 EchoControlMobile::kLoudEarpiece,
1074 EchoControlMobile::kSpeakerphone,
1075 EchoControlMobile::kLoudSpeakerphone,
1076 };
pkasting25702cb2016-01-08 13:50:27 -08001077 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001078 EXPECT_EQ(apm_->kNoError,
1079 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1080 EXPECT_EQ(mode[i],
1081 apm_->echo_control_mobile()->routing_mode());
1082 }
1083 // Turn comfort noise off/on
1084 EXPECT_EQ(apm_->kNoError,
1085 apm_->echo_control_mobile()->enable_comfort_noise(false));
1086 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1087 EXPECT_EQ(apm_->kNoError,
1088 apm_->echo_control_mobile()->enable_comfort_noise(true));
1089 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001090 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001091 const size_t echo_path_size =
1092 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001093 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1094 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001095 EXPECT_EQ(apm_->kNullPointerError,
1096 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1097 EXPECT_EQ(apm_->kNullPointerError,
1098 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1099 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001100 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001101 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001102 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001103 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001104 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001105 echo_path_in[i] = echo_path_out[i] + 1;
1106 }
1107 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001108 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001109 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001110 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1111 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001112 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001113 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1114 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001115 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001116 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1117 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001118
1119 // Process a few frames with NS in the default disabled state. This exercises
1120 // a different codepath than with it enabled.
1121 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1122 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1123 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1124 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1125
niklase@google.com470e71d2011-07-07 08:21:25 +00001126 // Turn AECM off
1127 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1128 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1129}
1130
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001131TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001132 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 EXPECT_EQ(apm_->kNoError,
1134 apm_->gain_control()->set_mode(
1135 apm_->gain_control()->mode()));
1136
1137 GainControl::Mode mode[] = {
1138 GainControl::kAdaptiveAnalog,
1139 GainControl::kAdaptiveDigital,
1140 GainControl::kFixedDigital
1141 };
pkasting25702cb2016-01-08 13:50:27 -08001142 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001143 EXPECT_EQ(apm_->kNoError,
1144 apm_->gain_control()->set_mode(mode[i]));
1145 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1146 }
1147 // Testing invalid target levels
1148 EXPECT_EQ(apm_->kBadParameterError,
1149 apm_->gain_control()->set_target_level_dbfs(-3));
1150 EXPECT_EQ(apm_->kBadParameterError,
1151 apm_->gain_control()->set_target_level_dbfs(-40));
1152 // Testing valid target levels
1153 EXPECT_EQ(apm_->kNoError,
1154 apm_->gain_control()->set_target_level_dbfs(
1155 apm_->gain_control()->target_level_dbfs()));
1156
1157 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001158 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001159 EXPECT_EQ(apm_->kNoError,
1160 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1161 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1162 }
1163
1164 // Testing invalid compression gains
1165 EXPECT_EQ(apm_->kBadParameterError,
1166 apm_->gain_control()->set_compression_gain_db(-1));
1167 EXPECT_EQ(apm_->kBadParameterError,
1168 apm_->gain_control()->set_compression_gain_db(100));
1169
1170 // Testing valid compression gains
1171 EXPECT_EQ(apm_->kNoError,
1172 apm_->gain_control()->set_compression_gain_db(
1173 apm_->gain_control()->compression_gain_db()));
1174
1175 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001176 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 EXPECT_EQ(apm_->kNoError,
1178 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1179 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1180 }
1181
1182 // Testing limiter off/on
1183 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1184 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1185 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1186 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1187
1188 // Testing invalid level limits
1189 EXPECT_EQ(apm_->kBadParameterError,
1190 apm_->gain_control()->set_analog_level_limits(-1, 512));
1191 EXPECT_EQ(apm_->kBadParameterError,
1192 apm_->gain_control()->set_analog_level_limits(100000, 512));
1193 EXPECT_EQ(apm_->kBadParameterError,
1194 apm_->gain_control()->set_analog_level_limits(512, -1));
1195 EXPECT_EQ(apm_->kBadParameterError,
1196 apm_->gain_control()->set_analog_level_limits(512, 100000));
1197 EXPECT_EQ(apm_->kBadParameterError,
1198 apm_->gain_control()->set_analog_level_limits(512, 255));
1199
1200 // Testing valid level limits
1201 EXPECT_EQ(apm_->kNoError,
1202 apm_->gain_control()->set_analog_level_limits(
1203 apm_->gain_control()->analog_level_minimum(),
1204 apm_->gain_control()->analog_level_maximum()));
1205
1206 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001207 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001208 EXPECT_EQ(apm_->kNoError,
1209 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1210 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1211 }
1212
1213 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001214 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001215 EXPECT_EQ(apm_->kNoError,
1216 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1217 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1218 }
1219
1220 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1221
1222 // Turn AGC off
1223 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1224 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1225}
1226
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001227void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001228 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001229 EXPECT_EQ(apm_->kNoError,
1230 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1231 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1232
1233 int out_analog_level = 0;
1234 for (int i = 0; i < 2000; ++i) {
1235 ReadFrameWithRewind(near_file_, frame_);
1236 // Ensure the audio is at a low level, so the AGC will try to increase it.
1237 ScaleFrame(frame_, 0.25);
1238
1239 // Always pass in the same volume.
1240 EXPECT_EQ(apm_->kNoError,
1241 apm_->gain_control()->set_stream_analog_level(100));
1242 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1243 out_analog_level = apm_->gain_control()->stream_analog_level();
1244 }
1245
1246 // Ensure the AGC is still able to reach the maximum.
1247 EXPECT_EQ(255, out_analog_level);
1248}
1249
1250// Verifies that despite volume slider quantization, the AGC can continue to
1251// increase its volume.
1252TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001253 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001254 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1255 }
1256}
1257
1258void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001259 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001260 EXPECT_EQ(apm_->kNoError,
1261 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1262 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1263
1264 int out_analog_level = 100;
1265 for (int i = 0; i < 1000; ++i) {
1266 ReadFrameWithRewind(near_file_, frame_);
1267 // Ensure the audio is at a low level, so the AGC will try to increase it.
1268 ScaleFrame(frame_, 0.25);
1269
1270 EXPECT_EQ(apm_->kNoError,
1271 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1272 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1273 out_analog_level = apm_->gain_control()->stream_analog_level();
1274 }
1275
1276 // Ensure the volume was raised.
1277 EXPECT_GT(out_analog_level, 100);
1278 int highest_level_reached = out_analog_level;
1279 // Simulate a user manual volume change.
1280 out_analog_level = 100;
1281
1282 for (int i = 0; i < 300; ++i) {
1283 ReadFrameWithRewind(near_file_, frame_);
1284 ScaleFrame(frame_, 0.25);
1285
1286 EXPECT_EQ(apm_->kNoError,
1287 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1288 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1289 out_analog_level = apm_->gain_control()->stream_analog_level();
1290 // Check that AGC respected the manually adjusted volume.
1291 EXPECT_LT(out_analog_level, highest_level_reached);
1292 }
1293 // Check that the volume was still raised.
1294 EXPECT_GT(out_analog_level, 100);
1295}
1296
1297TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001298 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001299 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1300 }
1301}
1302
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001303#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1304TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1305 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001306 const size_t kSamplesPerChannel =
1307 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001308 const size_t kNumInputChannels = 2;
1309 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001310 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001311 const float kScaleFactor = 0.25f;
1312 Config config;
1313 std::vector<webrtc::Point> geometry;
1314 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1315 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1316 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001317 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001318 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02001319 std::unique_ptr<AudioProcessing> apm(AudioProcessing::Create(
1320 config, nullptr, rtc::Callback1<EchoControl*, int>(), beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001321 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1322 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1323 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001324 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1325 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001326 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1327 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001328 std::string filename = ResourceFilePath("far", kSampleRateHz);
1329 FILE* far_file = fopen(filename.c_str(), "rb");
1330 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1331 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1332 const int kDefaultCompressionGain =
1333 apm->gain_control()->compression_gain_db();
1334 bool is_target = false;
1335 EXPECT_CALL(*beamformer, is_target_present())
1336 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001337 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001338 ASSERT_TRUE(ReadChunk(far_file,
1339 int_data.get(),
1340 float_data.get(),
1341 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001342 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001343 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001344 src_buf.channels()[j][k] *= kScaleFactor;
1345 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001346 }
1347 EXPECT_EQ(kNoErr,
1348 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001349 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001350 kSampleRateHz,
1351 LayoutFromChannels(src_buf.num_channels()),
1352 kSampleRateHz,
1353 LayoutFromChannels(dest_buf.num_channels()),
1354 dest_buf.channels()));
1355 }
1356 EXPECT_EQ(kDefaultVolume,
1357 apm->gain_control()->stream_analog_level());
1358 EXPECT_EQ(kDefaultCompressionGain,
1359 apm->gain_control()->compression_gain_db());
1360 rewind(far_file);
1361 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001362 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001363 ASSERT_TRUE(ReadChunk(far_file,
1364 int_data.get(),
1365 float_data.get(),
1366 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001367 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001368 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001369 src_buf.channels()[j][k] *= kScaleFactor;
1370 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001371 }
1372 EXPECT_EQ(kNoErr,
1373 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001374 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001375 kSampleRateHz,
1376 LayoutFromChannels(src_buf.num_channels()),
1377 kSampleRateHz,
1378 LayoutFromChannels(dest_buf.num_channels()),
1379 dest_buf.channels()));
1380 }
1381 EXPECT_LT(kDefaultVolume,
1382 apm->gain_control()->stream_analog_level());
1383 EXPECT_LT(kDefaultCompressionGain,
1384 apm->gain_control()->compression_gain_db());
1385 ASSERT_EQ(0, fclose(far_file));
1386}
1387#endif
1388
niklase@google.com470e71d2011-07-07 08:21:25 +00001389TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001390 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 NoiseSuppression::Level level[] = {
1392 NoiseSuppression::kLow,
1393 NoiseSuppression::kModerate,
1394 NoiseSuppression::kHigh,
1395 NoiseSuppression::kVeryHigh
1396 };
pkasting25702cb2016-01-08 13:50:27 -08001397 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001398 EXPECT_EQ(apm_->kNoError,
1399 apm_->noise_suppression()->set_level(level[i]));
1400 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1401 }
1402
andrew@webrtc.org648af742012-02-08 01:57:29 +00001403 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1405 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1406 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1407 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1408}
1409
1410TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001411 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001412 AudioProcessing::Config apm_config;
1413 apm_config.high_pass_filter.enabled = true;
1414 apm_->ApplyConfig(apm_config);
1415 apm_config.high_pass_filter.enabled = false;
1416 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001417}
1418
1419TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001420 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001421 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001422 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001423
1424 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1425
1426 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1427 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1428
1429 // Run this test in wideband; in super-wb, the splitting filter distorts the
1430 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001431 frame_->samples_per_channel_ = 160;
1432 frame_->num_channels_ = 2;
1433 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001434
1435 // Min value if no frames have been processed.
1436 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1437
1438 // Min value on zero frames.
1439 SetFrameTo(frame_, 0);
1440 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1441 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1442 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1443
1444 // Try a few RMS values.
1445 // (These also test that the value resets after retrieving it.)
1446 SetFrameTo(frame_, 32767);
1447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1450
1451 SetFrameTo(frame_, 30000);
1452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1453 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1454 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1455
1456 SetFrameTo(frame_, 10000);
1457 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1458 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1459 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1460
1461 SetFrameTo(frame_, 10);
1462 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1463 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1464 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1465
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001466 // Verify reset after enable/disable.
1467 SetFrameTo(frame_, 32767);
1468 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1469 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1470 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1471 SetFrameTo(frame_, 1);
1472 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1473 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1474
1475 // Verify reset after initialize.
1476 SetFrameTo(frame_, 32767);
1477 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1478 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1479 SetFrameTo(frame_, 1);
1480 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1481 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001482}
1483
1484TEST_F(ApmTest, VoiceDetection) {
1485 // Test external VAD
1486 EXPECT_EQ(apm_->kNoError,
1487 apm_->voice_detection()->set_stream_has_voice(true));
1488 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1489 EXPECT_EQ(apm_->kNoError,
1490 apm_->voice_detection()->set_stream_has_voice(false));
1491 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1492
andrew@webrtc.org648af742012-02-08 01:57:29 +00001493 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 VoiceDetection::Likelihood likelihood[] = {
1495 VoiceDetection::kVeryLowLikelihood,
1496 VoiceDetection::kLowLikelihood,
1497 VoiceDetection::kModerateLikelihood,
1498 VoiceDetection::kHighLikelihood
1499 };
pkasting25702cb2016-01-08 13:50:27 -08001500 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 EXPECT_EQ(apm_->kNoError,
1502 apm_->voice_detection()->set_likelihood(likelihood[i]));
1503 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1504 }
1505
1506 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001507 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001508 EXPECT_EQ(apm_->kBadParameterError,
1509 apm_->voice_detection()->set_frame_size_ms(12));
1510
andrew@webrtc.org648af742012-02-08 01:57:29 +00001511 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001512 for (int i = 10; i <= 30; i += 10) {
1513 EXPECT_EQ(apm_->kNoError,
1514 apm_->voice_detection()->set_frame_size_ms(i));
1515 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1516 }
1517 */
1518
andrew@webrtc.org648af742012-02-08 01:57:29 +00001519 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001520 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1521 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1522 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1523 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1524
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001525 // Test that AudioFrame activity is maintained when VAD is disabled.
1526 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1527 AudioFrame::VADActivity activity[] = {
1528 AudioFrame::kVadActive,
1529 AudioFrame::kVadPassive,
1530 AudioFrame::kVadUnknown
1531 };
pkasting25702cb2016-01-08 13:50:27 -08001532 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001533 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001534 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001535 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001536 }
1537
1538 // Test that AudioFrame activity is set when VAD is enabled.
1539 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001540 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001541 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001542 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001543
niklase@google.com470e71d2011-07-07 08:21:25 +00001544 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1545}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001546
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001547TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1548 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1549 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1550 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1551 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1552 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1553 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1554 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1555}
1556
1557TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001558 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001559 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001560 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001561 AudioFrame frame_copy;
1562 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001563 for (int j = 0; j < 1000; j++) {
1564 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1565 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001566 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1567 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001568 }
1569 }
1570}
1571
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001572TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1573 // Test that ProcessStream copies input to output even with no processing.
1574 const size_t kSamples = 80;
1575 const int sample_rate = 8000;
1576 const float src[kSamples] = {
1577 -1.0f, 0.0f, 1.0f
1578 };
1579 float dest[kSamples] = {};
1580
1581 auto src_channels = &src[0];
1582 auto dest_channels = &dest[0];
1583
1584 apm_.reset(AudioProcessing::Create());
1585 EXPECT_NOERR(apm_->ProcessStream(
1586 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1587 sample_rate, LayoutFromChannels(1), &dest_channels));
1588
1589 for (size_t i = 0; i < kSamples; ++i) {
1590 EXPECT_EQ(src[i], dest[i]);
1591 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001592
1593 // Same for ProcessReverseStream.
1594 float rev_dest[kSamples] = {};
1595 auto rev_dest_channels = &rev_dest[0];
1596
1597 StreamConfig input_stream = {sample_rate, 1};
1598 StreamConfig output_stream = {sample_rate, 1};
1599 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1600 output_stream, &rev_dest_channels));
1601
1602 for (size_t i = 0; i < kSamples; ++i) {
1603 EXPECT_EQ(src[i], rev_dest[i]);
1604 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001605}
1606
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001607TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1608 EnableAllComponents();
1609
pkasting25702cb2016-01-08 13:50:27 -08001610 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001611 Init(kProcessSampleRates[i],
1612 kProcessSampleRates[i],
1613 kProcessSampleRates[i],
1614 2,
1615 2,
1616 2,
1617 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001618 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001619 ASSERT_EQ(0, feof(far_file_));
1620 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001621 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001622 CopyLeftToRightChannel(revframe_->mutable_data(),
1623 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001624
aluebsb0319552016-03-17 20:39:53 -07001625 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001626
yujo36b1a5f2017-06-12 12:45:32 -07001627 CopyLeftToRightChannel(frame_->mutable_data(),
1628 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001629 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1630
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001631 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001632 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001633 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001634 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001635 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001636 analog_level = apm_->gain_control()->stream_analog_level();
1637
yujo36b1a5f2017-06-12 12:45:32 -07001638 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001639 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001640 rewind(far_file_);
1641 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001642 }
1643}
1644
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001645TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001646 // Verify the filter is not active through undistorted audio when:
1647 // 1. No components are enabled...
1648 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001649 AudioFrame frame_copy;
1650 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001651 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1652 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1653 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1654
1655 // 2. Only the level estimator is enabled...
1656 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001657 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001658 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1659 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1660 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1661 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1662 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1663
1664 // 3. Only VAD is enabled...
1665 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001666 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001667 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1668 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1669 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1670 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1671 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1672
1673 // 4. Both VAD and the level estimator are enabled...
1674 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001675 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001676 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1677 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1678 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1679 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1680 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1681 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1682 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1683
1684 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001685 frame_->samples_per_channel_ = 160;
1686 frame_->num_channels_ = 2;
1687 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 // Enable AEC, which would require the filter in super-wb. We rely on the
1689 // first few frames of data being unaffected by the AEC.
1690 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1691 // behavior of the AEC. Think of something more robust.
1692 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001693 // Make sure we have extended filter enabled. This makes sure nothing is
1694 // touched until we have a farend frame.
1695 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001696 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001697 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001698 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001699 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001700 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001701 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001702 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1703 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001704 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001705 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1706 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1707
1708 // Check the test is valid. We should have distortion from the filter
1709 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001710 frame_->samples_per_channel_ = 320;
1711 frame_->num_channels_ = 2;
1712 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001713 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001714 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001715 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001716 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001717 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1718 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1719}
1720
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001721#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1722void ApmTest::ProcessDebugDump(const std::string& in_filename,
1723 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001724 Format format,
1725 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001726 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001727 FILE* in_file = fopen(in_filename.c_str(), "rb");
1728 ASSERT_TRUE(in_file != NULL);
1729 audioproc::Event event_msg;
1730 bool first_init = true;
1731
1732 while (ReadMessageFromFile(in_file, &event_msg)) {
1733 if (event_msg.type() == audioproc::Event::INIT) {
1734 const audioproc::Init msg = event_msg.init();
1735 int reverse_sample_rate = msg.sample_rate();
1736 if (msg.has_reverse_sample_rate()) {
1737 reverse_sample_rate = msg.reverse_sample_rate();
1738 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001739 int output_sample_rate = msg.sample_rate();
1740 if (msg.has_output_sample_rate()) {
1741 output_sample_rate = msg.output_sample_rate();
1742 }
1743
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001744 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001745 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001746 reverse_sample_rate,
1747 msg.num_input_channels(),
1748 msg.num_output_channels(),
1749 msg.num_reverse_channels(),
1750 false);
1751 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001752 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001753 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001754 auto aec_dump =
1755 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1756 EXPECT_TRUE(aec_dump);
1757 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001758 first_init = false;
1759 }
1760
1761 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1762 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1763
1764 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001765 ASSERT_EQ(revframe_->num_channels_,
1766 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001767 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001768 memcpy(revfloat_cb_->channels()[i],
1769 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001770 msg.channel(i).size());
1771 }
1772 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001773 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001774 if (format == kFloatFormat) {
1775 // We're using an int16 input file; convert to float.
1776 ConvertToFloat(*revframe_, revfloat_cb_.get());
1777 }
1778 }
1779 AnalyzeReverseStreamChooser(format);
1780
1781 } else if (event_msg.type() == audioproc::Event::STREAM) {
1782 const audioproc::Stream msg = event_msg.stream();
1783 // ProcessStream could have changed this for the output frame.
1784 frame_->num_channels_ = apm_->num_input_channels();
1785
1786 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1787 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1788 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1789 if (msg.has_keypress()) {
1790 apm_->set_stream_key_pressed(msg.keypress());
1791 } else {
1792 apm_->set_stream_key_pressed(true);
1793 }
1794
1795 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001796 ASSERT_EQ(frame_->num_channels_,
1797 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001798 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001799 memcpy(float_cb_->channels()[i],
1800 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001801 msg.input_channel(i).size());
1802 }
1803 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001804 memcpy(frame_->mutable_data(), msg.input_data().data(),
1805 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001806 if (format == kFloatFormat) {
1807 // We're using an int16 input file; convert to float.
1808 ConvertToFloat(*frame_, float_cb_.get());
1809 }
1810 }
1811 ProcessStreamChooser(format);
1812 }
1813 }
aleloif4dd1912017-06-15 01:55:38 -07001814 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001815 fclose(in_file);
1816}
1817
1818void ApmTest::VerifyDebugDumpTest(Format format) {
1819 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001820 std::string format_string;
1821 switch (format) {
1822 case kIntFormat:
1823 format_string = "_int";
1824 break;
1825 case kFloatFormat:
1826 format_string = "_float";
1827 break;
1828 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001829 const std::string ref_filename = test::TempFilename(
1830 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1831 const std::string out_filename = test::TempFilename(
1832 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001833 const std::string limited_filename = test::TempFilename(
1834 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1835 const size_t logging_limit_bytes = 100000;
1836 // We expect at least this many bytes in the created logfile.
1837 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001838 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001839 ProcessDebugDump(in_filename, ref_filename, format, -1);
1840 ProcessDebugDump(ref_filename, out_filename, format, -1);
1841 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001842
1843 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1844 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001845 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001846 ASSERT_TRUE(ref_file != NULL);
1847 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001848 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001849 std::unique_ptr<uint8_t[]> ref_bytes;
1850 std::unique_ptr<uint8_t[]> out_bytes;
1851 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001852
1853 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1854 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001855 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001856 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001857 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001858 while (ref_size > 0 && out_size > 0) {
1859 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001860 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001861 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001862 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001863 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001864 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001865 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1866 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001867 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001868 }
1869 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001870 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1871 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001872 EXPECT_NE(0, feof(ref_file));
1873 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001874 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001875 ASSERT_EQ(0, fclose(ref_file));
1876 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001877 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001878 remove(ref_filename.c_str());
1879 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001880 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001881}
1882
pbosc7a65692016-05-06 12:50:04 -07001883TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001884 VerifyDebugDumpTest(kIntFormat);
1885}
1886
pbosc7a65692016-05-06 12:50:04 -07001887TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001888 VerifyDebugDumpTest(kFloatFormat);
1889}
1890#endif
1891
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001892// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001893TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001894 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001895 const std::string filename =
1896 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001897 {
1898 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1899 EXPECT_FALSE(aec_dump);
1900 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001901
1902#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1903 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001904 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001905
aleloif4dd1912017-06-15 01:55:38 -07001906 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1907 EXPECT_TRUE(aec_dump);
1908 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001909 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001910 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001911 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001912
1913 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001914 FILE* fid = fopen(filename.c_str(), "r");
1915 ASSERT_TRUE(fid != NULL);
1916
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001917 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001918 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001919 ASSERT_EQ(0, remove(filename.c_str()));
1920#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001921 // Verify the file has NOT been written.
1922 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1923#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1924}
1925
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001926// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001927TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001928 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1929
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001930 const std::string filename =
1931 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001932 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001933 ASSERT_TRUE(fid);
1934
1935#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1936 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001937 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001938
aleloif4dd1912017-06-15 01:55:38 -07001939 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1940 EXPECT_TRUE(aec_dump);
1941 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001942 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001943 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001944 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001945
1946 // Verify the file has been written.
1947 fid = fopen(filename.c_str(), "r");
1948 ASSERT_TRUE(fid != NULL);
1949
1950 // Clean it up.
1951 ASSERT_EQ(0, fclose(fid));
1952 ASSERT_EQ(0, remove(filename.c_str()));
1953#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001954 ASSERT_EQ(0, fclose(fid));
1955#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1956}
1957
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001958TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001959 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001960 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001961
1962 Config config;
1963 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001964 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001965 EnableAllComponents();
1966 EnableAllAPComponents(fapm.get());
1967 for (int i = 0; i < ref_data.test_size(); i++) {
1968 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1969
1970 audioproc::Test* test = ref_data.mutable_test(i);
1971 // TODO(ajm): Restore downmixing test cases.
1972 if (test->num_input_channels() != test->num_output_channels())
1973 continue;
1974
Peter Kasting69558702016-01-12 16:26:35 -08001975 const size_t num_render_channels =
1976 static_cast<size_t>(test->num_reverse_channels());
1977 const size_t num_input_channels =
1978 static_cast<size_t>(test->num_input_channels());
1979 const size_t num_output_channels =
1980 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001981 const size_t samples_per_channel = static_cast<size_t>(
1982 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001983
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001984 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1985 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001986 Init(fapm.get());
1987
1988 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001989 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1990 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001991
1992 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001993 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001994 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1995 ReadFrame(near_file_, frame_, float_cb_.get())) {
1996 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1997
aluebsb0319552016-03-17 20:39:53 -07001998 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001999 EXPECT_NOERR(fapm->AnalyzeReverseStream(
2000 revfloat_cb_->channels(),
2001 samples_per_channel,
2002 test->sample_rate(),
2003 LayoutFromChannels(num_render_channels)));
2004
2005 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
2006 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
2007 apm_->echo_cancellation()->set_stream_drift_samples(0);
2008 fapm->echo_cancellation()->set_stream_drift_samples(0);
2009 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
2010 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
2011
2012 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07002013 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002014 output_int16.channels());
2015
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002016 EXPECT_NOERR(fapm->ProcessStream(
2017 float_cb_->channels(),
2018 samples_per_channel,
2019 test->sample_rate(),
2020 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002021 test->sample_rate(),
2022 LayoutFromChannels(num_output_channels),
2023 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002024 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002025 FloatToS16(float_cb_->channels()[j],
2026 samples_per_channel,
2027 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002028 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002029 float snr = ComputeSNR(output_int16.channels()[j],
2030 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002031 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002032
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002033 const float kVarianceThreshold = 20;
2034 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002035
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002036 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002037 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2038 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002039 }
2040 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002041
2042 analog_level = fapm->gain_control()->stream_analog_level();
2043 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2044 fapm->gain_control()->stream_analog_level());
2045 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2046 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002047 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2048 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002049 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002050
2051 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002052 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002053 }
aluebs776593b2016-03-15 14:04:58 -07002054
2055#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2056 const size_t kMaxNumBadChunks = 0;
2057#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2058 // There are a few chunks in the fixed-point profile that give low SNR.
2059 // Listening confirmed the difference is acceptable.
2060 const size_t kMaxNumBadChunks = 60;
2061#endif
2062 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2063
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002064 rewind(far_file_);
2065 rewind(near_file_);
2066 }
2067}
2068
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002069// TODO(andrew): Add a test to process a few frames with different combinations
2070// of enabled components.
2071
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002072TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002073 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002074 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002075
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002076 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002077 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002078 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002079 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002080 for (size_t i = 0; i < arraysize(kChannels); i++) {
2081 for (size_t j = 0; j < arraysize(kChannels); j++) {
2082 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002083 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002084 test->set_num_reverse_channels(kChannels[i]);
2085 test->set_num_input_channels(kChannels[j]);
2086 test->set_num_output_channels(kChannels[j]);
2087 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002088 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002089 }
2090 }
2091 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002092#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2093 // To test the extended filter mode.
2094 audioproc::Test* test = ref_data.add_test();
2095 test->set_num_reverse_channels(2);
2096 test->set_num_input_channels(2);
2097 test->set_num_output_channels(2);
2098 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2099 test->set_use_aec_extended_filter(true);
2100#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002101 }
2102
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002103 for (int i = 0; i < ref_data.test_size(); i++) {
2104 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002105
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002106 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002107 // TODO(ajm): We no longer allow different input and output channels. Skip
2108 // these tests for now, but they should be removed from the set.
2109 if (test->num_input_channels() != test->num_output_channels())
2110 continue;
2111
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002112 Config config;
2113 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002114 config.Set<ExtendedFilter>(
2115 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002116 apm_.reset(AudioProcessing::Create(config));
2117
2118 EnableAllComponents();
2119
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002120 Init(test->sample_rate(),
2121 test->sample_rate(),
2122 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002123 static_cast<size_t>(test->num_input_channels()),
2124 static_cast<size_t>(test->num_output_channels()),
2125 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002126 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002127
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002128 int frame_count = 0;
2129 int has_echo_count = 0;
2130 int has_voice_count = 0;
2131 int is_saturated_count = 0;
2132 int analog_level = 127;
2133 int analog_level_average = 0;
2134 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002135 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002136#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2137 int stats_index = 0;
2138#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002139
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002140 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002141 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002142
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002143 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2144
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002145 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002146 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002147 EXPECT_EQ(apm_->kNoError,
2148 apm_->gain_control()->set_stream_analog_level(analog_level));
2149
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002150 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002151
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002152 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002153 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2154 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002155
2156 max_output_average += MaxAudioFrame(*frame_);
2157
2158 if (apm_->echo_cancellation()->stream_has_echo()) {
2159 has_echo_count++;
2160 }
2161
2162 analog_level = apm_->gain_control()->stream_analog_level();
2163 analog_level_average += analog_level;
2164 if (apm_->gain_control()->stream_is_saturated()) {
2165 is_saturated_count++;
2166 }
2167 if (apm_->voice_detection()->stream_has_voice()) {
2168 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002169 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002170 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002171 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002172 }
2173
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002174 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2175
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002176 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002177 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002178 sizeof(int16_t),
2179 frame_size,
2180 out_file_);
2181 ASSERT_EQ(frame_size, write_count);
2182
2183 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002184 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002185 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002186
2187#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2188 const int kStatsAggregationFrameNum = 100; // 1 second.
2189 if (frame_count % kStatsAggregationFrameNum == 0) {
2190 // Get echo metrics.
2191 EchoCancellation::Metrics echo_metrics;
2192 EXPECT_EQ(apm_->kNoError,
2193 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2194
2195 // Get delay metrics.
2196 int median = 0;
2197 int std = 0;
2198 float fraction_poor_delays = 0;
2199 EXPECT_EQ(apm_->kNoError,
2200 apm_->echo_cancellation()->GetDelayMetrics(
2201 &median, &std, &fraction_poor_delays));
2202
2203 // Get RMS.
2204 int rms_level = apm_->level_estimator()->RMS();
2205 EXPECT_LE(0, rms_level);
2206 EXPECT_GE(127, rms_level);
2207
2208 if (!write_ref_data) {
2209 const audioproc::Test::EchoMetrics& reference =
2210 test->echo_metrics(stats_index);
2211 TestStats(echo_metrics.residual_echo_return_loss,
2212 reference.residual_echo_return_loss());
2213 TestStats(echo_metrics.echo_return_loss,
2214 reference.echo_return_loss());
2215 TestStats(echo_metrics.echo_return_loss_enhancement,
2216 reference.echo_return_loss_enhancement());
2217 TestStats(echo_metrics.a_nlp,
2218 reference.a_nlp());
2219 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2220 reference.divergent_filter_fraction());
2221
2222 const audioproc::Test::DelayMetrics& reference_delay =
2223 test->delay_metrics(stats_index);
2224 EXPECT_EQ(reference_delay.median(), median);
2225 EXPECT_EQ(reference_delay.std(), std);
2226 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2227 fraction_poor_delays);
2228
2229 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2230
2231 ++stats_index;
2232 } else {
2233 audioproc::Test::EchoMetrics* message =
2234 test->add_echo_metrics();
2235 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2236 message->mutable_residual_echo_return_loss());
2237 WriteStatsMessage(echo_metrics.echo_return_loss,
2238 message->mutable_echo_return_loss());
2239 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2240 message->mutable_echo_return_loss_enhancement());
2241 WriteStatsMessage(echo_metrics.a_nlp,
2242 message->mutable_a_nlp());
2243 message->set_divergent_filter_fraction(
2244 echo_metrics.divergent_filter_fraction);
2245
2246 audioproc::Test::DelayMetrics* message_delay =
2247 test->add_delay_metrics();
2248 message_delay->set_median(median);
2249 message_delay->set_std(std);
2250 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2251
2252 test->add_rms_level(rms_level);
2253 }
2254 }
2255#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002256 }
2257 max_output_average /= frame_count;
2258 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002259 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002260
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002261 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002262 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002263 // When running the test on a N7 we get a {2, 6} difference of
2264 // |has_voice_count| and |max_output_average| is up to 18 higher.
2265 // All numbers being consistently higher on N7 compare to ref_data.
2266 // TODO(bjornv): If we start getting more of these offsets on Android we
2267 // should consider a different approach. Either using one slack for all,
2268 // or generate a separate android reference.
2269#if defined(WEBRTC_ANDROID)
2270 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002271 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002272 const int kMaxOutputAverageOffset = 9;
2273 const int kMaxOutputAverageNear = 9;
2274#else
2275 const int kHasVoiceCountOffset = 0;
2276 const int kHasVoiceCountNear = kIntNear;
2277 const int kMaxOutputAverageOffset = 0;
2278 const int kMaxOutputAverageNear = kIntNear;
2279#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002280 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002281 EXPECT_NEAR(test->has_voice_count(),
2282 has_voice_count - kHasVoiceCountOffset,
2283 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002284 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002285
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002286 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002287 EXPECT_NEAR(test->max_output_average(),
2288 max_output_average - kMaxOutputAverageOffset,
2289 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002290#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002291 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002292 EXPECT_NEAR(test->ns_speech_probability_average(),
2293 ns_speech_prob_average,
2294 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002295#endif
2296 } else {
2297 test->set_has_echo_count(has_echo_count);
2298 test->set_has_voice_count(has_voice_count);
2299 test->set_is_saturated_count(is_saturated_count);
2300
2301 test->set_analog_level_average(analog_level_average);
2302 test->set_max_output_average(max_output_average);
2303
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002304#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002305 EXPECT_LE(0.0f, ns_speech_prob_average);
2306 EXPECT_GE(1.0f, ns_speech_prob_average);
2307 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002308#endif
2309 }
2310
2311 rewind(far_file_);
2312 rewind(near_file_);
2313 }
2314
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002315 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002316 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002317 }
2318}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002319
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002320TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2321 struct ChannelFormat {
2322 AudioProcessing::ChannelLayout in_layout;
2323 AudioProcessing::ChannelLayout out_layout;
2324 };
2325 ChannelFormat cf[] = {
2326 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2327 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2328 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2329 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002330
kwiberg62eaacf2016-02-17 06:39:05 -08002331 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002332 // Enable one component just to ensure some processing takes place.
2333 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002334 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002335 const int in_rate = 44100;
2336 const int out_rate = 48000;
2337 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2338 TotalChannelsFromLayout(cf[i].in_layout));
2339 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2340 ChannelsFromLayout(cf[i].out_layout));
2341
2342 // Run over a few chunks.
2343 for (int j = 0; j < 10; ++j) {
2344 EXPECT_NOERR(ap->ProcessStream(
2345 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002346 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002347 in_rate,
2348 cf[i].in_layout,
2349 out_rate,
2350 cf[i].out_layout,
2351 out_cb.channels()));
2352 }
2353 }
2354}
2355
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002356// Compares the reference and test arrays over a region around the expected
2357// delay. Finds the highest SNR in that region and adds the variance and squared
2358// error results to the supplied accumulators.
2359void UpdateBestSNR(const float* ref,
2360 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002361 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002362 int expected_delay,
2363 double* variance_acc,
2364 double* sq_error_acc) {
2365 double best_snr = std::numeric_limits<double>::min();
2366 double best_variance = 0;
2367 double best_sq_error = 0;
2368 // Search over a region of eight samples around the expected delay.
2369 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2370 ++delay) {
2371 double sq_error = 0;
2372 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002373 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002374 double error = test[i + delay] - ref[i];
2375 sq_error += error * error;
2376 variance += ref[i] * ref[i];
2377 }
2378
2379 if (sq_error == 0) {
2380 *variance_acc += variance;
2381 return;
2382 }
2383 double snr = variance / sq_error;
2384 if (snr > best_snr) {
2385 best_snr = snr;
2386 best_variance = variance;
2387 best_sq_error = sq_error;
2388 }
2389 }
2390
2391 *variance_acc += best_variance;
2392 *sq_error_acc += best_sq_error;
2393}
2394
2395// Used to test a multitude of sample rate and channel combinations. It works
2396// by first producing a set of reference files (in SetUpTestCase) that are
2397// assumed to be correct, as the used parameters are verified by other tests
2398// in this collection. Primarily the reference files are all produced at
2399// "native" rates which do not involve any resampling.
2400
2401// Each test pass produces an output file with a particular format. The output
2402// is matched against the reference file closest to its internal processing
2403// format. If necessary the output is resampled back to its process format.
2404// Due to the resampling distortion, we don't expect identical results, but
2405// enforce SNR thresholds which vary depending on the format. 0 is a special
2406// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002407typedef std::tr1::tuple<int, int, int, int, double, double>
2408 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409class AudioProcessingTest
2410 : public testing::TestWithParam<AudioProcessingTestData> {
2411 public:
2412 AudioProcessingTest()
2413 : input_rate_(std::tr1::get<0>(GetParam())),
2414 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002415 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2416 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2417 expected_snr_(std::tr1::get<4>(GetParam())),
2418 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002419
2420 virtual ~AudioProcessingTest() {}
2421
2422 static void SetUpTestCase() {
2423 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002424 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002425 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002426 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2427 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2428 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002430 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2431 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2432 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002433 }
2434 }
2435 }
2436 }
2437
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002438 void TearDown() {
2439 // Remove "out" files after each test.
2440 ClearTempOutFiles();
2441 }
2442
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002443 static void TearDownTestCase() {
2444 ClearTempFiles();
2445 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002446
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002447 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002448 // to a file specified with |output_file_prefix|. Both forward and reverse
2449 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002450 static void ProcessFormat(int input_rate,
2451 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002452 int reverse_input_rate,
2453 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002454 size_t num_input_channels,
2455 size_t num_output_channels,
2456 size_t num_reverse_input_channels,
2457 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002458 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002459 Config config;
2460 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002461 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002462 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002463
ekmeyerson60d9b332015-08-14 10:35:55 -07002464 ProcessingConfig processing_config = {
2465 {{input_rate, num_input_channels},
2466 {output_rate, num_output_channels},
2467 {reverse_input_rate, num_reverse_input_channels},
2468 {reverse_output_rate, num_reverse_output_channels}}};
2469 ap->Initialize(processing_config);
2470
2471 FILE* far_file =
2472 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002473 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 FILE* out_file =
2475 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2476 reverse_input_rate, reverse_output_rate,
2477 num_input_channels, num_output_channels,
2478 num_reverse_input_channels,
2479 num_reverse_output_channels, kForward).c_str(),
2480 "wb");
2481 FILE* rev_out_file =
2482 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2483 reverse_input_rate, reverse_output_rate,
2484 num_input_channels, num_output_channels,
2485 num_reverse_input_channels,
2486 num_reverse_output_channels, kReverse).c_str(),
2487 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002488 ASSERT_TRUE(far_file != NULL);
2489 ASSERT_TRUE(near_file != NULL);
2490 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002491 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002492
2493 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2494 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002495 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2496 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002497 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2498 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002499 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2500 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002501
2502 // Temporary buffers.
2503 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002504 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2505 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002506 std::unique_ptr<float[]> float_data(new float[max_length]);
2507 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002508
2509 int analog_level = 127;
2510 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2511 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002512 EXPECT_NOERR(ap->ProcessReverseStream(
2513 rev_cb.channels(), processing_config.reverse_input_stream(),
2514 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002515
2516 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2517 ap->echo_cancellation()->set_stream_drift_samples(0);
2518 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2519
2520 EXPECT_NOERR(ap->ProcessStream(
2521 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002522 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002523 input_rate,
2524 LayoutFromChannels(num_input_channels),
2525 output_rate,
2526 LayoutFromChannels(num_output_channels),
2527 out_cb.channels()));
2528
ekmeyerson60d9b332015-08-14 10:35:55 -07002529 // Dump forward output to file.
2530 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002531 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002532 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002533
pkasting25702cb2016-01-08 13:50:27 -08002534 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002535 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002536 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002537
ekmeyerson60d9b332015-08-14 10:35:55 -07002538 // Dump reverse output to file.
2539 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2540 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002541 size_t rev_out_length =
2542 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002543
pkasting25702cb2016-01-08 13:50:27 -08002544 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002545 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2546 rev_out_file));
2547
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002548 analog_level = ap->gain_control()->stream_analog_level();
2549 }
2550 fclose(far_file);
2551 fclose(near_file);
2552 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002553 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002554 }
2555
2556 protected:
2557 int input_rate_;
2558 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002559 int reverse_input_rate_;
2560 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002561 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002562 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002563};
2564
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002565TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002566 struct ChannelFormat {
2567 int num_input;
2568 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002569 int num_reverse_input;
2570 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002571 };
2572 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002573 {1, 1, 1, 1},
2574 {1, 1, 2, 1},
2575 {2, 1, 1, 1},
2576 {2, 1, 2, 1},
2577 {2, 2, 1, 1},
2578 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002579 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002580
pkasting25702cb2016-01-08 13:50:27 -08002581 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002582 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2583 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2584 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002585
ekmeyerson60d9b332015-08-14 10:35:55 -07002586 // Verify output for both directions.
2587 std::vector<StreamDirection> stream_directions;
2588 stream_directions.push_back(kForward);
2589 stream_directions.push_back(kReverse);
2590 for (StreamDirection file_direction : stream_directions) {
2591 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2592 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2593 const int out_num =
2594 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2595 const double expected_snr =
2596 file_direction ? expected_reverse_snr_ : expected_snr_;
2597
2598 const int min_ref_rate = std::min(in_rate, out_rate);
2599 int ref_rate;
2600
2601 if (min_ref_rate > 32000) {
2602 ref_rate = 48000;
2603 } else if (min_ref_rate > 16000) {
2604 ref_rate = 32000;
2605 } else if (min_ref_rate > 8000) {
2606 ref_rate = 16000;
2607 } else {
2608 ref_rate = 8000;
2609 }
aluebs776593b2016-03-15 14:04:58 -07002610#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002611 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002612 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002613 }
2614#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002615 FILE* out_file = fopen(
2616 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2617 reverse_output_rate_, cf[i].num_input,
2618 cf[i].num_output, cf[i].num_reverse_input,
2619 cf[i].num_reverse_output, file_direction).c_str(),
2620 "rb");
2621 // The reference files always have matching input and output channels.
2622 FILE* ref_file = fopen(
2623 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2624 cf[i].num_output, cf[i].num_output,
2625 cf[i].num_reverse_output, cf[i].num_reverse_output,
2626 file_direction).c_str(),
2627 "rb");
2628 ASSERT_TRUE(out_file != NULL);
2629 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002630
pkasting25702cb2016-01-08 13:50:27 -08002631 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2632 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002633 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002634 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002635 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002636 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002637 // Data from the resampled output, in case the reference and output rates
2638 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002639 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002640
ekmeyerson60d9b332015-08-14 10:35:55 -07002641 PushResampler<float> resampler;
2642 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002643
ekmeyerson60d9b332015-08-14 10:35:55 -07002644 // Compute the resampling delay of the output relative to the reference,
2645 // to find the region over which we should search for the best SNR.
2646 float expected_delay_sec = 0;
2647 if (in_rate != ref_rate) {
2648 // Input resampling delay.
2649 expected_delay_sec +=
2650 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2651 }
2652 if (out_rate != ref_rate) {
2653 // Output resampling delay.
2654 expected_delay_sec +=
2655 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2656 // Delay of converting the output back to its processing rate for
2657 // testing.
2658 expected_delay_sec +=
2659 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2660 }
2661 int expected_delay =
2662 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002663
ekmeyerson60d9b332015-08-14 10:35:55 -07002664 double variance = 0;
2665 double sq_error = 0;
2666 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2667 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2668 float* out_ptr = out_data.get();
2669 if (out_rate != ref_rate) {
2670 // Resample the output back to its internal processing rate if
2671 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002672 ASSERT_EQ(ref_length,
2673 static_cast<size_t>(resampler.Resample(
2674 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002675 out_ptr = cmp_data.get();
2676 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002677
ekmeyerson60d9b332015-08-14 10:35:55 -07002678 // Update the |sq_error| and |variance| accumulators with the highest
2679 // SNR of reference vs output.
2680 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2681 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002682 }
2683
ekmeyerson60d9b332015-08-14 10:35:55 -07002684 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2685 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2686 << cf[i].num_input << ", " << cf[i].num_output << ", "
2687 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2688 << ", " << file_direction << "): ";
2689 if (sq_error > 0) {
2690 double snr = 10 * log10(variance / sq_error);
2691 EXPECT_GE(snr, expected_snr);
2692 EXPECT_NE(0, expected_snr);
2693 std::cout << "SNR=" << snr << " dB" << std::endl;
2694 } else {
aluebs776593b2016-03-15 14:04:58 -07002695 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002696 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002697
ekmeyerson60d9b332015-08-14 10:35:55 -07002698 fclose(out_file);
2699 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002700 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002701 }
2702}
2703
2704#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2705INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002706 CommonFormats,
2707 AudioProcessingTest,
2708 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002709 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2710 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002711 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2712 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2713 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2714 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2715 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2716 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2717 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2718 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2719 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002720
ekmeyerson60d9b332015-08-14 10:35:55 -07002721 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2722 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2723 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2724 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2725 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2726 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2727 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2728 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2729 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2730 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2731 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2732 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002733
ekmeyerson60d9b332015-08-14 10:35:55 -07002734 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2735 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2736 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2737 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2738 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2739 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2740 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2741 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2742 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2743 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2744 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2745 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002746
ekmeyerson60d9b332015-08-14 10:35:55 -07002747 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2748 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2749 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2750 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2751 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2752 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2753 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2754 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2755 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2756 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002757 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002758 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002759
2760#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2761INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002762 CommonFormats,
2763 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002764 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2765 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2766 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2767 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2768 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2769 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002770 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2771 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2772 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2773 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2774 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2775 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002776
aluebs776593b2016-03-15 14:04:58 -07002777 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2778 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2779 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002780 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2781 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2782 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2783 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2784 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2785 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2786 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2787 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2788 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002789
aluebs776593b2016-03-15 14:04:58 -07002790 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2791 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2792 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2793 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2794 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2795 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2796 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2797 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2798 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002799 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2800 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2801 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002802
ekmeyerson60d9b332015-08-14 10:35:55 -07002803 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2804 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2805 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2806 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2807 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2808 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2809 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2810 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2811 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2812 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002813 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002814 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002815#endif
2816
niklase@google.com470e71d2011-07-07 08:21:25 +00002817} // namespace
peahc19f3122016-10-07 14:54:10 -07002818
2819TEST(ApmConfiguration, DefaultBehavior) {
2820 // Verify that the level controller is default off, it can be activated using
2821 // the config, and that the default initial level is maintained after the
2822 // config has been applied.
2823 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002824 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002825 AudioProcessing::Config config;
2826 EXPECT_FALSE(apm->config_.level_controller.enabled);
2827 // TODO(peah): Add test for the existence of the level controller object once
2828 // that is created only when that is specified in the config.
2829 // TODO(peah): Remove the testing for
2830 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2831 // is instead used to activate the level controller.
2832 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2833 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2834 apm->config_.level_controller.initial_peak_level_dbfs,
2835 std::numeric_limits<float>::epsilon());
2836 config.level_controller.enabled = true;
2837 apm->ApplyConfig(config);
2838 EXPECT_TRUE(apm->config_.level_controller.enabled);
2839 // TODO(peah): Add test for the existence of the level controller object once
2840 // that is created only when the that is specified in the config.
2841 // TODO(peah): Remove the testing for
2842 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2843 // is instead used to activate the level controller.
2844 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2845 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2846 apm->config_.level_controller.initial_peak_level_dbfs,
2847 std::numeric_limits<float>::epsilon());
2848}
2849
2850TEST(ApmConfiguration, ValidConfigBehavior) {
2851 // Verify that the initial level can be specified and is retained after the
2852 // config has been applied.
2853 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002854 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002855 AudioProcessing::Config config;
2856 config.level_controller.initial_peak_level_dbfs = -50.f;
2857 apm->ApplyConfig(config);
2858 EXPECT_FALSE(apm->config_.level_controller.enabled);
2859 // TODO(peah): Add test for the existence of the level controller object once
2860 // that is created only when the that is specified in the config.
2861 // TODO(peah): Remove the testing for
2862 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2863 // is instead used to activate the level controller.
2864 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2865 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2866 std::numeric_limits<float>::epsilon());
2867}
2868
2869TEST(ApmConfiguration, InValidConfigBehavior) {
2870 // Verify that the config is properly reset when nonproper values are applied
2871 // for the initial level.
2872
2873 // Verify that the config is properly reset when the specified initial peak
2874 // level is too low.
2875 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002876 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002877 AudioProcessing::Config config;
2878 config.level_controller.enabled = true;
2879 config.level_controller.initial_peak_level_dbfs = -101.f;
2880 apm->ApplyConfig(config);
2881 EXPECT_FALSE(apm->config_.level_controller.enabled);
2882 // TODO(peah): Add test for the existence of the level controller object once
2883 // that is created only when the that is specified in the config.
2884 // TODO(peah): Remove the testing for
2885 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2886 // is instead used to activate the level controller.
2887 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2888 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2889 apm->config_.level_controller.initial_peak_level_dbfs,
2890 std::numeric_limits<float>::epsilon());
2891
2892 // Verify that the config is properly reset when the specified initial peak
2893 // level is too high.
peaha9cc40b2017-06-29 08:32:09 -07002894 apm.reset(new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002895 config = AudioProcessing::Config();
2896 config.level_controller.enabled = true;
2897 config.level_controller.initial_peak_level_dbfs = 1.f;
2898 apm->ApplyConfig(config);
2899 EXPECT_FALSE(apm->config_.level_controller.enabled);
2900 // TODO(peah): Add test for the existence of the level controller object once
2901 // that is created only when that is specified in the config.
2902 // TODO(peah): Remove the testing for
2903 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2904 // is instead used to activate the level controller.
2905 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2906 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2907 apm->config_.level_controller.initial_peak_level_dbfs,
2908 std::numeric_limits<float>::epsilon());
2909}
2910
Sam Zackrisson0beac582017-09-25 12:04:02 +02002911TEST(ApmConfiguration, EnablePostProcessing) {
2912 // Verify that apm uses a capture post processing module if one is provided.
2913 webrtc::Config webrtc_config;
2914 auto mock_post_processor_ptr =
2915 new testing::NiceMock<test::MockPostProcessing>();
2916 auto mock_post_processor =
2917 std::unique_ptr<PostProcessing>(mock_post_processor_ptr);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002918 rtc::scoped_refptr<AudioProcessing> apm =
2919 AudioProcessing::Create(webrtc_config, std::move(mock_post_processor),
2920 rtc::Callback1<EchoControl*, int>(), nullptr);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002921
2922 AudioFrame audio;
2923 audio.num_channels_ = 1;
2924 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2925
2926 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002927 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002928}
2929
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002930} // namespace webrtc