blob: 2b1393fad495b27dbe6e3d89061f43d677da3df5 [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öllerb7239a92017-10-03 10:42:04 +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"
42#include "system_wrappers/include/trace.h"
43#include "test/gtest.h"
44#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070045
46RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000048#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000049#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000051#endif
kwiberg77eab702016-09-28 17:42:01 -070052RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000053
andrew@webrtc.org27c69802014-02-18 20:24:56 +000054namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000055namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000056
ekmeyerson60d9b332015-08-14 10:35:55 -070057// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
58// applicable.
59
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000060// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000061// re-implemented; see comment at the bottom of this file. For now, the user has
62// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000063// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000064// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000065// be set to true with the command-line switch --write_ref_data.
66bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070067const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070068const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000069
aluebseb3603b2016-04-20 15:27:58 -070070#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
71// Android doesn't support 48kHz.
72const int kProcessSampleRates[] = {8000, 16000, 32000};
73#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070074const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070075#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000076
ekmeyerson60d9b332015-08-14 10:35:55 -070077enum StreamDirection { kForward = 0, kReverse };
78
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000079void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000080 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000081 cb->num_channels());
82 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000083 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000084 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000085 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080086 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000087 S16ToFloat(cb_int.channels()[i],
88 cb->num_frames(),
89 cb->channels()[i]);
90 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000091}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000092
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000093void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070094 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000095}
96
andrew@webrtc.org103657b2014-04-24 18:28:56 +000097// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080098size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000099 switch (layout) {
100 case AudioProcessing::kMono:
101 return 1;
102 case AudioProcessing::kMonoAndKeyboard:
103 case AudioProcessing::kStereo:
104 return 2;
105 case AudioProcessing::kStereoAndKeyboard:
106 return 3;
107 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700108 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800109 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000110}
111
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000112int TruncateToMultipleOf10(int value) {
113 return (value / 10) * 10;
114}
115
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800117 size_t samples_per_channel) {
118 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000119 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000120}
121
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000122void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800123 size_t samples_per_channel) {
124 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000125 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
126}
127
pkasting25702cb2016-01-08 13:50:27 -0800128void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
129 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000130 stereo[i * 2 + 1] = stereo[i * 2];
131 }
132}
133
yujo36b1a5f2017-06-12 12:45:32 -0700134void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800135 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000136 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
137 }
138}
139
140void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700141 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
143 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700144 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000145 }
146}
147
148void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800149 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700150 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700152 frame_data[i] = left;
153 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000154 }
155}
156
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000157void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700158 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
160 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700161 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000162 }
163}
164
andrew@webrtc.org81865342012-10-27 00:28:27 +0000165bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000166 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000167 return false;
168 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000169 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000170 return false;
171 }
yujo36b1a5f2017-06-12 12:45:32 -0700172 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000173 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000174 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000175 return false;
176 }
177 return true;
178}
179
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000180void EnableAllAPComponents(AudioProcessing* ap) {
181#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
182 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
183
184 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
185 EXPECT_NOERR(ap->gain_control()->Enable(true));
186#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
187 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
188 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
189 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
190 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
191
192 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
193 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
194 EXPECT_NOERR(ap->gain_control()->Enable(true));
195#endif
196
peah8271d042016-11-22 07:24:52 -0800197 AudioProcessing::Config apm_config;
198 apm_config.high_pass_filter.enabled = true;
199 ap->ApplyConfig(apm_config);
200
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000201 EXPECT_NOERR(ap->level_estimator()->Enable(true));
202 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
203
204 EXPECT_NOERR(ap->voice_detection()->Enable(true));
205}
206
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000207// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000208template <class T>
209T AbsValue(T a) {
210 return a > 0 ? a: -a;
211}
212
213int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800214 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -0700215 const int16_t* frame_data = frame.data();
216 int16_t max_data = AbsValue(frame_data[0]);
pkasting25702cb2016-01-08 13:50:27 -0800217 for (size_t i = 1; i < length; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700218 max_data = std::max(max_data, AbsValue(frame_data[i]));
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000219 }
220
221 return max_data;
222}
223
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000224#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000226 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700227 EXPECT_EQ(reference.instant(), test.instant);
228 EXPECT_EQ(reference.average(), test.average);
229 EXPECT_EQ(reference.maximum(), test.maximum);
230 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000231}
232
233void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 audioproc::Test::Statistic* msg) {
235 msg->set_instant(output.instant);
236 msg->set_average(output.average);
237 msg->set_maximum(output.maximum);
238 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000240#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241
Alex Loiko890988c2017-08-31 10:25:48 +0200242void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700243 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000244 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000245 ASSERT_TRUE(file != NULL);
246
247 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000248 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800249 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000250 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000251
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000252 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000253 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000254 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000255 fclose(file);
256}
257
Alex Loiko890988c2017-08-31 10:25:48 +0200258std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000259 std::ostringstream ss;
260 // Resource files are all stereo.
261 ss << name << sample_rate_hz / 1000 << "_stereo";
262 return test::ResourcePath(ss.str(), "pcm");
263}
264
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000265// Temporary filenames unique to this process. Used to be able to run these
266// tests in parallel as each process needs to be running in isolation they can't
267// have competing filenames.
268std::map<std::string, std::string> temp_filenames;
269
Alex Loiko890988c2017-08-31 10:25:48 +0200270std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000271 int input_rate,
272 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700273 int reverse_input_rate,
274 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800275 size_t num_input_channels,
276 size_t num_output_channels,
277 size_t num_reverse_input_channels,
278 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000280 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700281 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
282 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000283 if (num_output_channels == 1) {
284 ss << "mono";
285 } else if (num_output_channels == 2) {
286 ss << "stereo";
287 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700288 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700290 ss << output_rate / 1000;
291 if (num_reverse_output_channels == 1) {
292 ss << "_rmono";
293 } else if (num_reverse_output_channels == 2) {
294 ss << "_rstereo";
295 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700296 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700297 }
298 ss << reverse_output_rate / 1000;
299 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000300
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000301 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700302 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000303 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
304 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000305}
306
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000307void ClearTempFiles() {
308 for (auto& kv : temp_filenames)
309 remove(kv.second.c_str());
310}
311
Alex Loiko890988c2017-08-31 10:25:48 +0200312void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000313 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000314 ASSERT_TRUE(file != NULL);
315 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000316 fclose(file);
317}
318
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000319// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
320// stereo) file, converts to deinterleaved float (optionally downmixing) and
321// returns the result in |cb|. Returns false if the file ended (or on error) and
322// true otherwise.
323//
324// |int_data| and |float_data| are just temporary space that must be
325// sufficiently large to hold the 10 ms chunk.
326bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
327 ChannelBuffer<float>* cb) {
328 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000329 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000330 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
331 if (read_count != frame_size) {
332 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700333 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000334 return false; // This is expected.
335 }
336
337 S16ToFloat(int_data, frame_size, float_data);
338 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000339 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000340 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000341 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000342 cb->channels());
343 }
344
345 return true;
346}
347
niklase@google.com470e71d2011-07-07 08:21:25 +0000348class ApmTest : public ::testing::Test {
349 protected:
350 ApmTest();
351 virtual void SetUp();
352 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000353
354 static void SetUpTestCase() {
355 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000356 }
357
358 static void TearDownTestCase() {
359 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000360 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000361 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000362
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363 // Used to select between int and float interface tests.
364 enum Format {
365 kIntFormat,
366 kFloatFormat
367 };
368
369 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000370 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000371 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800372 size_t num_input_channels,
373 size_t num_output_channels,
374 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000375 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000376 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000377 void EnableAllComponents();
378 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000379 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000380 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000381 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
382 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000383 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000384 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
385 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700386 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800387 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700388 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800389 void TestChangingForwardChannels(size_t num_in_channels,
390 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700391 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800392 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700393 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000394 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
395 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000397 int ProcessStreamChooser(Format format);
398 int AnalyzeReverseStreamChooser(Format format);
399 void ProcessDebugDump(const std::string& in_filename,
400 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800401 Format format,
402 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000403 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000404
405 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000406 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800407 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000408 AudioFrame* frame_;
409 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800410 std::unique_ptr<ChannelBuffer<float> > float_cb_;
411 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000412 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800413 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414 FILE* far_file_;
415 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000416 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000417};
418
419ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000420 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000421#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800422 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
423 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000424#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000425#if defined(WEBRTC_MAC)
426 // A different file for Mac is needed because on this platform the AEC
427 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800428 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
429 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000430#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800431 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
432 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000433#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000434#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000435 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000436 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000437 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000438 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000439 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000440 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000441 out_file_(NULL) {
442 Config config;
443 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
444 apm_.reset(AudioProcessing::Create(config));
445}
niklase@google.com470e71d2011-07-07 08:21:25 +0000446
447void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000448 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
450 frame_ = new AudioFrame();
451 revframe_ = new AudioFrame();
452
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000453 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
456void ApmTest::TearDown() {
457 if (frame_) {
458 delete frame_;
459 }
460 frame_ = NULL;
461
462 if (revframe_) {
463 delete revframe_;
464 }
465 revframe_ = NULL;
466
467 if (far_file_) {
468 ASSERT_EQ(0, fclose(far_file_));
469 }
470 far_file_ = NULL;
471
472 if (near_file_) {
473 ASSERT_EQ(0, fclose(near_file_));
474 }
475 near_file_ = NULL;
476
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000477 if (out_file_) {
478 ASSERT_EQ(0, fclose(out_file_));
479 }
480 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000483void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000484 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700485 ap->Initialize(
486 {{{frame_->sample_rate_hz_, frame_->num_channels_},
487 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700488 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700489 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000490}
491
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000492void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000493 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000494 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800495 size_t num_input_channels,
496 size_t num_output_channels,
497 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000498 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000499 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000500 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000501 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000502
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000503 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
504 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000505 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000506
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000507 if (far_file_) {
508 ASSERT_EQ(0, fclose(far_file_));
509 }
510 std::string filename = ResourceFilePath("far", sample_rate_hz);
511 far_file_ = fopen(filename.c_str(), "rb");
512 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
513 filename << "\n";
514
515 if (near_file_) {
516 ASSERT_EQ(0, fclose(near_file_));
517 }
518 filename = ResourceFilePath("near", sample_rate_hz);
519 near_file_ = fopen(filename.c_str(), "rb");
520 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
521 filename << "\n";
522
523 if (open_output_file) {
524 if (out_file_) {
525 ASSERT_EQ(0, fclose(out_file_));
526 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700527 filename = OutputFilePath(
528 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
529 reverse_sample_rate_hz, num_input_channels, num_output_channels,
530 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000531 out_file_ = fopen(filename.c_str(), "wb");
532 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
533 filename << "\n";
534 }
535}
536
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000537void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000538 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000539}
540
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000541bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
542 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000543 // The files always contain stereo audio.
544 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700545 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000546 sizeof(int16_t),
547 frame_size,
548 file);
549 if (read_count != frame_size) {
550 // Check that the file really ended.
551 EXPECT_NE(0, feof(file));
552 return false; // This is expected.
553 }
554
555 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700556 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000557 frame->samples_per_channel_);
558 }
559
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000561 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000562 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000563 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000564}
565
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000566bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
567 return ReadFrame(file, frame, NULL);
568}
569
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000570// If the end of the file has been reached, rewind it and attempt to read the
571// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
573 ChannelBuffer<float>* cb) {
574 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000575 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000576 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000577 }
578}
579
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
581 ReadFrameWithRewind(file, frame, NULL);
582}
583
andrew@webrtc.org81865342012-10-27 00:28:27 +0000584void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
585 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000586 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000587 EXPECT_EQ(apm_->kNoError,
588 apm_->gain_control()->set_stream_analog_level(127));
589 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000590}
591
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000592int ApmTest::ProcessStreamChooser(Format format) {
593 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000594 return apm_->ProcessStream(frame_);
595 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000596 return apm_->ProcessStream(float_cb_->channels(),
597 frame_->samples_per_channel_,
598 frame_->sample_rate_hz_,
599 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000600 output_sample_rate_hz_,
601 LayoutFromChannels(num_output_channels_),
602 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000603}
604
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000605int ApmTest::AnalyzeReverseStreamChooser(Format format) {
606 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700607 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000608 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000609 return apm_->AnalyzeReverseStream(
610 revfloat_cb_->channels(),
611 revframe_->samples_per_channel_,
612 revframe_->sample_rate_hz_,
613 LayoutFromChannels(revframe_->num_channels_));
614}
615
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000616void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
617 int delay_min, int delay_max) {
618 // The |revframe_| and |frame_| should include the proper frame information,
619 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000620 AudioFrame tmp_frame;
621 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000622 bool causal = true;
623
624 tmp_frame.CopyFrom(*revframe_);
625 SetFrameTo(&tmp_frame, 0);
626
627 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
628 // Initialize the |frame_queue| with empty frames.
629 int frame_delay = delay_ms / 10;
630 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000631 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000632 frame->CopyFrom(tmp_frame);
633 frame_queue.push(frame);
634 frame_delay++;
635 causal = false;
636 }
637 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000638 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 frame->CopyFrom(tmp_frame);
640 frame_queue.push(frame);
641 frame_delay--;
642 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000643 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
644 // need enough frames with audio to have reliable estimates, but as few as
645 // possible to keep processing time down. 4.5 seconds seemed to be a good
646 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 for (int frame_count = 0; frame_count < 450; ++frame_count) {
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 // Use the near end recording, since that has more speech in it.
651 ASSERT_TRUE(ReadFrame(near_file_, frame));
652 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000653 AudioFrame* reverse_frame = frame;
654 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000655 if (!causal) {
656 reverse_frame = frame_queue.front();
657 // When we call ProcessStream() the frame is modified, so we can't use the
658 // pointer directly when things are non-causal. Use an intermediate frame
659 // and copy the data.
660 process_frame = &tmp_frame;
661 process_frame->CopyFrom(*frame);
662 }
aluebsb0319552016-03-17 20:39:53 -0700663 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000664 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
665 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
666 frame = frame_queue.front();
667 frame_queue.pop();
668 delete frame;
669
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000670 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000671 int median;
672 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000673 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000674 // Discard the first delay metrics to avoid convergence effects.
675 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000676 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
677 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000678 }
679 }
680
681 rewind(near_file_);
682 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000683 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000684 frame_queue.pop();
685 delete frame;
686 }
687 // Calculate expected delay estimate and acceptable regions. Further,
688 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700689 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700690 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700691 const int expected_median =
692 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
693 const int expected_median_high = rtc::SafeClamp<int>(
694 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700695 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700696 const int expected_median_low = rtc::SafeClamp<int>(
697 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700698 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000699 // Verify delay metrics.
700 int median;
701 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000702 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000703 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000704 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
705 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000706 EXPECT_GE(expected_median_high, median);
707 EXPECT_LE(expected_median_low, median);
708}
709
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000710void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000712 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000713
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000714 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000716 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000718
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 EXPECT_EQ(apm_->kNoError,
721 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000723 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000725
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000726 // Other stream parameters set correctly.
727 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 EXPECT_EQ(apm_->kNoError,
729 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000730 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000731 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000732 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000733 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
735 EXPECT_EQ(apm_->kNoError,
736 apm_->echo_cancellation()->enable_drift_compensation(false));
737
738 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000739 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000740 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000741 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000743
744 // Resets after successful ProcessStream().
745 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000746 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000747 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000748 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000749
750 // Other stream parameters set correctly.
751 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
752 EXPECT_EQ(apm_->kNoError,
753 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000754 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000755 EXPECT_EQ(apm_->kNoError,
756 apm_->gain_control()->set_stream_analog_level(127));
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 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
760
761 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000762 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000763 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000764
765 // Resets after successful ProcessStream().
766 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000767 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000768 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000769 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000770 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000771
772 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
774 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
775 EXPECT_EQ(apm_->kNoError,
776 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000777 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000778 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000779
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000780 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000781 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000782 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000784 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000785
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000786 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000787 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000788 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000789 EXPECT_EQ(apm_->kNoError,
790 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000791 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000792}
793
794TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000795 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000796}
797
798TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000799 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000800}
801
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000802TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
803 EXPECT_EQ(0, apm_->delay_offset_ms());
804 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
805 EXPECT_EQ(50, apm_->stream_delay_ms());
806}
807
808TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
809 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000810 apm_->set_delay_offset_ms(100);
811 EXPECT_EQ(100, apm_->delay_offset_ms());
812 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000813 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000814 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
815 EXPECT_EQ(200, apm_->stream_delay_ms());
816
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000817 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000818 apm_->set_delay_offset_ms(-50);
819 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000820 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
821 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000822 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
823 EXPECT_EQ(50, apm_->stream_delay_ms());
824}
825
Michael Graczyk86c6d332015-07-23 11:41:39 -0700826void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800827 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700828 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000829 frame_->num_channels_ = num_channels;
830 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700831 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000832}
833
Michael Graczyk86c6d332015-07-23 11:41:39 -0700834void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800835 size_t num_in_channels,
836 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700837 AudioProcessing::Error expected_return) {
838 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
839 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
840
841 EXPECT_EQ(expected_return,
842 apm_->ProcessStream(float_cb_->channels(), input_stream,
843 output_stream, float_cb_->channels()));
844}
845
846void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800847 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848 AudioProcessing::Error expected_return) {
849 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700850 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
851 {output_sample_rate_hz_, apm_->num_output_channels()},
852 {frame_->sample_rate_hz_, num_rev_channels},
853 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700854
ekmeyerson60d9b332015-08-14 10:35:55 -0700855 EXPECT_EQ(
856 expected_return,
857 apm_->ProcessReverseStream(
858 float_cb_->channels(), processing_config.reverse_input_stream(),
859 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700860}
861
862TEST_F(ApmTest, ChannelsInt16Interface) {
863 // Testing number of invalid and valid channels.
864 Init(16000, 16000, 16000, 4, 4, 4, false);
865
866 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
867
Peter Kasting69558702016-01-12 16:26:35 -0800868 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700869 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000870 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 }
872}
873
Michael Graczyk86c6d332015-07-23 11:41:39 -0700874TEST_F(ApmTest, Channels) {
875 // Testing number of invalid and valid channels.
876 Init(16000, 16000, 16000, 4, 4, 4, false);
877
878 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
879 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
880
Peter Kasting69558702016-01-12 16:26:35 -0800881 for (size_t i = 1; i < 4; ++i) {
882 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700883 // Output channels much be one or match input channels.
884 if (j == 1 || i == j) {
885 TestChangingForwardChannels(i, j, kNoErr);
886 TestChangingReverseChannels(i, kNoErr);
887
888 EXPECT_EQ(i, apm_->num_input_channels());
889 EXPECT_EQ(j, apm_->num_output_channels());
890 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800891 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700892 } else {
893 TestChangingForwardChannels(i, j,
894 AudioProcessing::kBadNumberChannelsError);
895 }
896 }
897 }
898}
899
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000900TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000902 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000903 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000904 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700905 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800906 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000907 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000908 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000909 }
910}
911
niklase@google.com470e71d2011-07-07 08:21:25 +0000912TEST_F(ApmTest, EchoCancellation) {
913 EXPECT_EQ(apm_->kNoError,
914 apm_->echo_cancellation()->enable_drift_compensation(true));
915 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
916 EXPECT_EQ(apm_->kNoError,
917 apm_->echo_cancellation()->enable_drift_compensation(false));
918 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
919
niklase@google.com470e71d2011-07-07 08:21:25 +0000920 EchoCancellation::SuppressionLevel level[] = {
921 EchoCancellation::kLowSuppression,
922 EchoCancellation::kModerateSuppression,
923 EchoCancellation::kHighSuppression,
924 };
pkasting25702cb2016-01-08 13:50:27 -0800925 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 EXPECT_EQ(apm_->kNoError,
927 apm_->echo_cancellation()->set_suppression_level(level[i]));
928 EXPECT_EQ(level[i],
929 apm_->echo_cancellation()->suppression_level());
930 }
931
932 EchoCancellation::Metrics metrics;
933 EXPECT_EQ(apm_->kNotEnabledError,
934 apm_->echo_cancellation()->GetMetrics(&metrics));
935
ivoc3e9a5372016-10-28 07:55:33 -0700936 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
937 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
938
niklase@google.com470e71d2011-07-07 08:21:25 +0000939 EXPECT_EQ(apm_->kNoError,
940 apm_->echo_cancellation()->enable_metrics(true));
941 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
942 EXPECT_EQ(apm_->kNoError,
943 apm_->echo_cancellation()->enable_metrics(false));
944 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
945
ivoc48dfab52016-10-28 03:29:31 -0700946 EXPECT_EQ(apm_->kNoError,
947 apm_->echo_cancellation()->enable_delay_logging(true));
948 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
949 EXPECT_EQ(apm_->kNoError,
950 apm_->echo_cancellation()->enable_delay_logging(false));
951 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
952
ivoc3e9a5372016-10-28 07:55:33 -0700953 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
954 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
955
956 int median = 0;
957 int std = 0;
958 float poor_fraction = 0;
959 EXPECT_EQ(apm_->kNotEnabledError, apm_->echo_cancellation()->GetDelayMetrics(
960 &median, &std, &poor_fraction));
961
niklase@google.com470e71d2011-07-07 08:21:25 +0000962 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
963 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
964 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
965 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000966
967 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
968 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
969 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
970 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
971 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
972 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000973}
974
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000975TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000976 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000977 // Enable AEC only.
978 EXPECT_EQ(apm_->kNoError,
979 apm_->echo_cancellation()->enable_drift_compensation(false));
980 EXPECT_EQ(apm_->kNoError,
981 apm_->echo_cancellation()->enable_metrics(false));
982 EXPECT_EQ(apm_->kNoError,
983 apm_->echo_cancellation()->enable_delay_logging(true));
984 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000985 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700986 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000987 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000988
989 // Internally in the AEC the amount of lookahead the delay estimation can
990 // handle is 15 blocks and the maximum delay is set to 60 blocks.
991 const int kLookaheadBlocks = 15;
992 const int kMaxDelayBlocks = 60;
993 // The AEC has a startup time before it actually starts to process. This
994 // procedure can flush the internal far-end buffer, which of course affects
995 // the delay estimation. Therefore, we set a system_delay high enough to
996 // avoid that. The smallest system_delay you can report without flushing the
997 // buffer is 66 ms in 8 kHz.
998 //
999 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
1000 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
1001 // delay estimation. This should be noted though. In case of test failure,
1002 // this could be the cause.
1003 const int kSystemDelayMs = 66;
1004 // Test a couple of corner cases and verify that the estimated delay is
1005 // within a valid region (set to +-1.5 blocks). Note that these cases are
1006 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -08001007 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001008 Init(kProcessSampleRates[i],
1009 kProcessSampleRates[i],
1010 kProcessSampleRates[i],
1011 2,
1012 2,
1013 2,
1014 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001015 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -07001016 const int num_ms_per_block =
1017 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001018 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1019 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1020
1021 // 1) Verify correct delay estimate at lookahead boundary.
1022 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1023 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1024 delay_max_ms);
1025 // 2) A delay less than maximum lookahead should give an delay estimate at
1026 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1027 delay_ms -= 20;
1028 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1029 delay_max_ms);
1030 // 3) Three values around zero delay. Note that we need to compensate for
1031 // the fake system_delay.
1032 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1033 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1034 delay_max_ms);
1035 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1036 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1037 delay_max_ms);
1038 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1039 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1040 delay_max_ms);
1041 // 4) Verify correct delay estimate at maximum delay boundary.
1042 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1043 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1044 delay_max_ms);
1045 // 5) A delay above the maximum delay should give an estimate at the
1046 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1047 delay_ms += 20;
1048 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1049 delay_max_ms);
1050 }
1051}
1052
niklase@google.com470e71d2011-07-07 08:21:25 +00001053TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001054 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001055 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001056 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1057 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1058
niklase@google.com470e71d2011-07-07 08:21:25 +00001059 // Toggle routing modes
1060 EchoControlMobile::RoutingMode mode[] = {
1061 EchoControlMobile::kQuietEarpieceOrHeadset,
1062 EchoControlMobile::kEarpiece,
1063 EchoControlMobile::kLoudEarpiece,
1064 EchoControlMobile::kSpeakerphone,
1065 EchoControlMobile::kLoudSpeakerphone,
1066 };
pkasting25702cb2016-01-08 13:50:27 -08001067 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001068 EXPECT_EQ(apm_->kNoError,
1069 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1070 EXPECT_EQ(mode[i],
1071 apm_->echo_control_mobile()->routing_mode());
1072 }
1073 // Turn comfort noise off/on
1074 EXPECT_EQ(apm_->kNoError,
1075 apm_->echo_control_mobile()->enable_comfort_noise(false));
1076 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1077 EXPECT_EQ(apm_->kNoError,
1078 apm_->echo_control_mobile()->enable_comfort_noise(true));
1079 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001080 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001081 const size_t echo_path_size =
1082 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001083 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1084 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001085 EXPECT_EQ(apm_->kNullPointerError,
1086 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1087 EXPECT_EQ(apm_->kNullPointerError,
1088 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1089 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001090 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001091 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001092 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001093 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001094 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001095 echo_path_in[i] = echo_path_out[i] + 1;
1096 }
1097 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001098 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001099 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001100 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1101 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001102 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001103 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1104 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001105 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001106 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1107 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001108
1109 // Process a few frames with NS in the default disabled state. This exercises
1110 // a different codepath than with it enabled.
1111 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1112 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1113 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1114 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1115
niklase@google.com470e71d2011-07-07 08:21:25 +00001116 // Turn AECM off
1117 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1118 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1119}
1120
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001121TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001122 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 EXPECT_EQ(apm_->kNoError,
1124 apm_->gain_control()->set_mode(
1125 apm_->gain_control()->mode()));
1126
1127 GainControl::Mode mode[] = {
1128 GainControl::kAdaptiveAnalog,
1129 GainControl::kAdaptiveDigital,
1130 GainControl::kFixedDigital
1131 };
pkasting25702cb2016-01-08 13:50:27 -08001132 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 EXPECT_EQ(apm_->kNoError,
1134 apm_->gain_control()->set_mode(mode[i]));
1135 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1136 }
1137 // Testing invalid target levels
1138 EXPECT_EQ(apm_->kBadParameterError,
1139 apm_->gain_control()->set_target_level_dbfs(-3));
1140 EXPECT_EQ(apm_->kBadParameterError,
1141 apm_->gain_control()->set_target_level_dbfs(-40));
1142 // Testing valid target levels
1143 EXPECT_EQ(apm_->kNoError,
1144 apm_->gain_control()->set_target_level_dbfs(
1145 apm_->gain_control()->target_level_dbfs()));
1146
1147 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001148 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001149 EXPECT_EQ(apm_->kNoError,
1150 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1151 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1152 }
1153
1154 // Testing invalid compression gains
1155 EXPECT_EQ(apm_->kBadParameterError,
1156 apm_->gain_control()->set_compression_gain_db(-1));
1157 EXPECT_EQ(apm_->kBadParameterError,
1158 apm_->gain_control()->set_compression_gain_db(100));
1159
1160 // Testing valid compression gains
1161 EXPECT_EQ(apm_->kNoError,
1162 apm_->gain_control()->set_compression_gain_db(
1163 apm_->gain_control()->compression_gain_db()));
1164
1165 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001166 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001167 EXPECT_EQ(apm_->kNoError,
1168 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1169 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1170 }
1171
1172 // Testing limiter off/on
1173 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1174 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1175 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1176 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1177
1178 // Testing invalid level limits
1179 EXPECT_EQ(apm_->kBadParameterError,
1180 apm_->gain_control()->set_analog_level_limits(-1, 512));
1181 EXPECT_EQ(apm_->kBadParameterError,
1182 apm_->gain_control()->set_analog_level_limits(100000, 512));
1183 EXPECT_EQ(apm_->kBadParameterError,
1184 apm_->gain_control()->set_analog_level_limits(512, -1));
1185 EXPECT_EQ(apm_->kBadParameterError,
1186 apm_->gain_control()->set_analog_level_limits(512, 100000));
1187 EXPECT_EQ(apm_->kBadParameterError,
1188 apm_->gain_control()->set_analog_level_limits(512, 255));
1189
1190 // Testing valid level limits
1191 EXPECT_EQ(apm_->kNoError,
1192 apm_->gain_control()->set_analog_level_limits(
1193 apm_->gain_control()->analog_level_minimum(),
1194 apm_->gain_control()->analog_level_maximum()));
1195
1196 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001197 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 EXPECT_EQ(apm_->kNoError,
1199 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1200 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1201 }
1202
1203 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001204 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001205 EXPECT_EQ(apm_->kNoError,
1206 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1207 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1208 }
1209
1210 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1211
1212 // Turn AGC off
1213 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1214 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1215}
1216
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001217void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001218 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001219 EXPECT_EQ(apm_->kNoError,
1220 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1221 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1222
1223 int out_analog_level = 0;
1224 for (int i = 0; i < 2000; ++i) {
1225 ReadFrameWithRewind(near_file_, frame_);
1226 // Ensure the audio is at a low level, so the AGC will try to increase it.
1227 ScaleFrame(frame_, 0.25);
1228
1229 // Always pass in the same volume.
1230 EXPECT_EQ(apm_->kNoError,
1231 apm_->gain_control()->set_stream_analog_level(100));
1232 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1233 out_analog_level = apm_->gain_control()->stream_analog_level();
1234 }
1235
1236 // Ensure the AGC is still able to reach the maximum.
1237 EXPECT_EQ(255, out_analog_level);
1238}
1239
1240// Verifies that despite volume slider quantization, the AGC can continue to
1241// increase its volume.
1242TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001243 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001244 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1245 }
1246}
1247
1248void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001249 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001250 EXPECT_EQ(apm_->kNoError,
1251 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1252 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1253
1254 int out_analog_level = 100;
1255 for (int i = 0; i < 1000; ++i) {
1256 ReadFrameWithRewind(near_file_, frame_);
1257 // Ensure the audio is at a low level, so the AGC will try to increase it.
1258 ScaleFrame(frame_, 0.25);
1259
1260 EXPECT_EQ(apm_->kNoError,
1261 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1262 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1263 out_analog_level = apm_->gain_control()->stream_analog_level();
1264 }
1265
1266 // Ensure the volume was raised.
1267 EXPECT_GT(out_analog_level, 100);
1268 int highest_level_reached = out_analog_level;
1269 // Simulate a user manual volume change.
1270 out_analog_level = 100;
1271
1272 for (int i = 0; i < 300; ++i) {
1273 ReadFrameWithRewind(near_file_, frame_);
1274 ScaleFrame(frame_, 0.25);
1275
1276 EXPECT_EQ(apm_->kNoError,
1277 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1278 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1279 out_analog_level = apm_->gain_control()->stream_analog_level();
1280 // Check that AGC respected the manually adjusted volume.
1281 EXPECT_LT(out_analog_level, highest_level_reached);
1282 }
1283 // Check that the volume was still raised.
1284 EXPECT_GT(out_analog_level, 100);
1285}
1286
1287TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001288 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001289 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1290 }
1291}
1292
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001293#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1294TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1295 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001296 const size_t kSamplesPerChannel =
1297 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001298 const size_t kNumInputChannels = 2;
1299 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001300 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001301 const float kScaleFactor = 0.25f;
1302 Config config;
1303 std::vector<webrtc::Point> geometry;
1304 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1305 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1306 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001307 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001308 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001309 std::unique_ptr<AudioProcessing> apm(
Sam Zackrisson0beac582017-09-25 12:04:02 +02001310 AudioProcessing::Create(config, nullptr, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001311 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1312 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1313 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001314 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1315 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001316 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1317 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001318 std::string filename = ResourceFilePath("far", kSampleRateHz);
1319 FILE* far_file = fopen(filename.c_str(), "rb");
1320 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1321 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1322 const int kDefaultCompressionGain =
1323 apm->gain_control()->compression_gain_db();
1324 bool is_target = false;
1325 EXPECT_CALL(*beamformer, is_target_present())
1326 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001327 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001328 ASSERT_TRUE(ReadChunk(far_file,
1329 int_data.get(),
1330 float_data.get(),
1331 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001332 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001333 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001334 src_buf.channels()[j][k] *= kScaleFactor;
1335 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001336 }
1337 EXPECT_EQ(kNoErr,
1338 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001339 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001340 kSampleRateHz,
1341 LayoutFromChannels(src_buf.num_channels()),
1342 kSampleRateHz,
1343 LayoutFromChannels(dest_buf.num_channels()),
1344 dest_buf.channels()));
1345 }
1346 EXPECT_EQ(kDefaultVolume,
1347 apm->gain_control()->stream_analog_level());
1348 EXPECT_EQ(kDefaultCompressionGain,
1349 apm->gain_control()->compression_gain_db());
1350 rewind(far_file);
1351 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001352 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001353 ASSERT_TRUE(ReadChunk(far_file,
1354 int_data.get(),
1355 float_data.get(),
1356 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001357 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001358 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001359 src_buf.channels()[j][k] *= kScaleFactor;
1360 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001361 }
1362 EXPECT_EQ(kNoErr,
1363 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001364 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001365 kSampleRateHz,
1366 LayoutFromChannels(src_buf.num_channels()),
1367 kSampleRateHz,
1368 LayoutFromChannels(dest_buf.num_channels()),
1369 dest_buf.channels()));
1370 }
1371 EXPECT_LT(kDefaultVolume,
1372 apm->gain_control()->stream_analog_level());
1373 EXPECT_LT(kDefaultCompressionGain,
1374 apm->gain_control()->compression_gain_db());
1375 ASSERT_EQ(0, fclose(far_file));
1376}
1377#endif
1378
niklase@google.com470e71d2011-07-07 08:21:25 +00001379TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001380 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001381 NoiseSuppression::Level level[] = {
1382 NoiseSuppression::kLow,
1383 NoiseSuppression::kModerate,
1384 NoiseSuppression::kHigh,
1385 NoiseSuppression::kVeryHigh
1386 };
pkasting25702cb2016-01-08 13:50:27 -08001387 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001388 EXPECT_EQ(apm_->kNoError,
1389 apm_->noise_suppression()->set_level(level[i]));
1390 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1391 }
1392
andrew@webrtc.org648af742012-02-08 01:57:29 +00001393 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001394 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1395 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1396 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1397 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1398}
1399
1400TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001401 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001402 AudioProcessing::Config apm_config;
1403 apm_config.high_pass_filter.enabled = true;
1404 apm_->ApplyConfig(apm_config);
1405 apm_config.high_pass_filter.enabled = false;
1406 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001407}
1408
1409TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001410 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001411 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001412 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001413
1414 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1415
1416 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1417 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1418
1419 // Run this test in wideband; in super-wb, the splitting filter distorts the
1420 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001421 frame_->samples_per_channel_ = 160;
1422 frame_->num_channels_ = 2;
1423 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001424
1425 // Min value if no frames have been processed.
1426 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1427
1428 // Min value on zero frames.
1429 SetFrameTo(frame_, 0);
1430 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1431 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1432 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1433
1434 // Try a few RMS values.
1435 // (These also test that the value resets after retrieving it.)
1436 SetFrameTo(frame_, 32767);
1437 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1438 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1439 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1440
1441 SetFrameTo(frame_, 30000);
1442 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1443 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1444 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1445
1446 SetFrameTo(frame_, 10000);
1447 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1450
1451 SetFrameTo(frame_, 10);
1452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1453 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1454 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1455
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001456 // Verify reset after enable/disable.
1457 SetFrameTo(frame_, 32767);
1458 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1459 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1460 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1461 SetFrameTo(frame_, 1);
1462 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1463 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1464
1465 // Verify reset after initialize.
1466 SetFrameTo(frame_, 32767);
1467 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1468 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1469 SetFrameTo(frame_, 1);
1470 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1471 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001472}
1473
1474TEST_F(ApmTest, VoiceDetection) {
1475 // Test external VAD
1476 EXPECT_EQ(apm_->kNoError,
1477 apm_->voice_detection()->set_stream_has_voice(true));
1478 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1479 EXPECT_EQ(apm_->kNoError,
1480 apm_->voice_detection()->set_stream_has_voice(false));
1481 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1482
andrew@webrtc.org648af742012-02-08 01:57:29 +00001483 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001484 VoiceDetection::Likelihood likelihood[] = {
1485 VoiceDetection::kVeryLowLikelihood,
1486 VoiceDetection::kLowLikelihood,
1487 VoiceDetection::kModerateLikelihood,
1488 VoiceDetection::kHighLikelihood
1489 };
pkasting25702cb2016-01-08 13:50:27 -08001490 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001491 EXPECT_EQ(apm_->kNoError,
1492 apm_->voice_detection()->set_likelihood(likelihood[i]));
1493 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1494 }
1495
1496 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001497 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 EXPECT_EQ(apm_->kBadParameterError,
1499 apm_->voice_detection()->set_frame_size_ms(12));
1500
andrew@webrtc.org648af742012-02-08 01:57:29 +00001501 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001502 for (int i = 10; i <= 30; i += 10) {
1503 EXPECT_EQ(apm_->kNoError,
1504 apm_->voice_detection()->set_frame_size_ms(i));
1505 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1506 }
1507 */
1508
andrew@webrtc.org648af742012-02-08 01:57:29 +00001509 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001510 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1511 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1512 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1513 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1514
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001515 // Test that AudioFrame activity is maintained when VAD is disabled.
1516 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1517 AudioFrame::VADActivity activity[] = {
1518 AudioFrame::kVadActive,
1519 AudioFrame::kVadPassive,
1520 AudioFrame::kVadUnknown
1521 };
pkasting25702cb2016-01-08 13:50:27 -08001522 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001523 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001524 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001525 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001526 }
1527
1528 // Test that AudioFrame activity is set when VAD is enabled.
1529 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001530 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001531 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001532 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001533
niklase@google.com470e71d2011-07-07 08:21:25 +00001534 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1535}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001536
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001537TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1538 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1539 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1540 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1541 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1542 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1543 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1544 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1545}
1546
1547TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001548 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001549 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001550 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001551 AudioFrame frame_copy;
1552 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001553 for (int j = 0; j < 1000; j++) {
1554 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1555 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001556 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1557 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001558 }
1559 }
1560}
1561
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001562TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1563 // Test that ProcessStream copies input to output even with no processing.
1564 const size_t kSamples = 80;
1565 const int sample_rate = 8000;
1566 const float src[kSamples] = {
1567 -1.0f, 0.0f, 1.0f
1568 };
1569 float dest[kSamples] = {};
1570
1571 auto src_channels = &src[0];
1572 auto dest_channels = &dest[0];
1573
1574 apm_.reset(AudioProcessing::Create());
1575 EXPECT_NOERR(apm_->ProcessStream(
1576 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1577 sample_rate, LayoutFromChannels(1), &dest_channels));
1578
1579 for (size_t i = 0; i < kSamples; ++i) {
1580 EXPECT_EQ(src[i], dest[i]);
1581 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001582
1583 // Same for ProcessReverseStream.
1584 float rev_dest[kSamples] = {};
1585 auto rev_dest_channels = &rev_dest[0];
1586
1587 StreamConfig input_stream = {sample_rate, 1};
1588 StreamConfig output_stream = {sample_rate, 1};
1589 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1590 output_stream, &rev_dest_channels));
1591
1592 for (size_t i = 0; i < kSamples; ++i) {
1593 EXPECT_EQ(src[i], rev_dest[i]);
1594 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001595}
1596
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001597TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1598 EnableAllComponents();
1599
pkasting25702cb2016-01-08 13:50:27 -08001600 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001601 Init(kProcessSampleRates[i],
1602 kProcessSampleRates[i],
1603 kProcessSampleRates[i],
1604 2,
1605 2,
1606 2,
1607 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001608 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001609 ASSERT_EQ(0, feof(far_file_));
1610 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001611 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001612 CopyLeftToRightChannel(revframe_->mutable_data(),
1613 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001614
aluebsb0319552016-03-17 20:39:53 -07001615 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001616
yujo36b1a5f2017-06-12 12:45:32 -07001617 CopyLeftToRightChannel(frame_->mutable_data(),
1618 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001619 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1620
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001621 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001622 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001623 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001624 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001625 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001626 analog_level = apm_->gain_control()->stream_analog_level();
1627
yujo36b1a5f2017-06-12 12:45:32 -07001628 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001629 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001630 rewind(far_file_);
1631 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001632 }
1633}
1634
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001635TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001636 // Verify the filter is not active through undistorted audio when:
1637 // 1. No components are enabled...
1638 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001639 AudioFrame frame_copy;
1640 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001641 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1642 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1643 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1644
1645 // 2. Only the level estimator is enabled...
1646 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001647 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001648 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1649 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1650 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1651 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1652 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1653
1654 // 3. Only VAD is enabled...
1655 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001656 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001657 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1658 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1659 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1660 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1661 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1662
1663 // 4. Both VAD and the level estimator are enabled...
1664 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001665 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001666 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1667 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_->level_estimator()->Enable(false));
1672 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1673
1674 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001675 frame_->samples_per_channel_ = 160;
1676 frame_->num_channels_ = 2;
1677 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001678 // Enable AEC, which would require the filter in super-wb. We rely on the
1679 // first few frames of data being unaffected by the AEC.
1680 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1681 // behavior of the AEC. Think of something more robust.
1682 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001683 // Make sure we have extended filter enabled. This makes sure nothing is
1684 // touched until we have a farend frame.
1685 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001686 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001687 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001688 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001689 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001690 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001691 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001692 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1693 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001694 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001695 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1696 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1697
1698 // Check the test is valid. We should have distortion from the filter
1699 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001700 frame_->samples_per_channel_ = 320;
1701 frame_->num_channels_ = 2;
1702 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001703 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001704 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001705 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001706 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001707 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1708 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1709}
1710
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001711#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1712void ApmTest::ProcessDebugDump(const std::string& in_filename,
1713 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001714 Format format,
1715 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001716 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001717 FILE* in_file = fopen(in_filename.c_str(), "rb");
1718 ASSERT_TRUE(in_file != NULL);
1719 audioproc::Event event_msg;
1720 bool first_init = true;
1721
1722 while (ReadMessageFromFile(in_file, &event_msg)) {
1723 if (event_msg.type() == audioproc::Event::INIT) {
1724 const audioproc::Init msg = event_msg.init();
1725 int reverse_sample_rate = msg.sample_rate();
1726 if (msg.has_reverse_sample_rate()) {
1727 reverse_sample_rate = msg.reverse_sample_rate();
1728 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001729 int output_sample_rate = msg.sample_rate();
1730 if (msg.has_output_sample_rate()) {
1731 output_sample_rate = msg.output_sample_rate();
1732 }
1733
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001734 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001735 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001736 reverse_sample_rate,
1737 msg.num_input_channels(),
1738 msg.num_output_channels(),
1739 msg.num_reverse_channels(),
1740 false);
1741 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001742 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001743 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001744 auto aec_dump =
1745 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1746 EXPECT_TRUE(aec_dump);
1747 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001748 first_init = false;
1749 }
1750
1751 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1752 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1753
1754 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001755 ASSERT_EQ(revframe_->num_channels_,
1756 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001757 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001758 memcpy(revfloat_cb_->channels()[i],
1759 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001760 msg.channel(i).size());
1761 }
1762 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001763 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001764 if (format == kFloatFormat) {
1765 // We're using an int16 input file; convert to float.
1766 ConvertToFloat(*revframe_, revfloat_cb_.get());
1767 }
1768 }
1769 AnalyzeReverseStreamChooser(format);
1770
1771 } else if (event_msg.type() == audioproc::Event::STREAM) {
1772 const audioproc::Stream msg = event_msg.stream();
1773 // ProcessStream could have changed this for the output frame.
1774 frame_->num_channels_ = apm_->num_input_channels();
1775
1776 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1777 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1778 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1779 if (msg.has_keypress()) {
1780 apm_->set_stream_key_pressed(msg.keypress());
1781 } else {
1782 apm_->set_stream_key_pressed(true);
1783 }
1784
1785 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001786 ASSERT_EQ(frame_->num_channels_,
1787 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001788 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001789 memcpy(float_cb_->channels()[i],
1790 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001791 msg.input_channel(i).size());
1792 }
1793 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001794 memcpy(frame_->mutable_data(), msg.input_data().data(),
1795 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001796 if (format == kFloatFormat) {
1797 // We're using an int16 input file; convert to float.
1798 ConvertToFloat(*frame_, float_cb_.get());
1799 }
1800 }
1801 ProcessStreamChooser(format);
1802 }
1803 }
aleloif4dd1912017-06-15 01:55:38 -07001804 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001805 fclose(in_file);
1806}
1807
1808void ApmTest::VerifyDebugDumpTest(Format format) {
1809 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001810 std::string format_string;
1811 switch (format) {
1812 case kIntFormat:
1813 format_string = "_int";
1814 break;
1815 case kFloatFormat:
1816 format_string = "_float";
1817 break;
1818 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001819 const std::string ref_filename = test::TempFilename(
1820 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1821 const std::string out_filename = test::TempFilename(
1822 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001823 const std::string limited_filename = test::TempFilename(
1824 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1825 const size_t logging_limit_bytes = 100000;
1826 // We expect at least this many bytes in the created logfile.
1827 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001828 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001829 ProcessDebugDump(in_filename, ref_filename, format, -1);
1830 ProcessDebugDump(ref_filename, out_filename, format, -1);
1831 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001832
1833 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1834 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001835 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001836 ASSERT_TRUE(ref_file != NULL);
1837 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001838 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001839 std::unique_ptr<uint8_t[]> ref_bytes;
1840 std::unique_ptr<uint8_t[]> out_bytes;
1841 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001842
1843 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1844 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001845 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001846 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001847 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001848 while (ref_size > 0 && out_size > 0) {
1849 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001850 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001851 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001852 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001853 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001854 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001855 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1856 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001857 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001858 }
1859 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001860 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1861 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001862 EXPECT_NE(0, feof(ref_file));
1863 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001864 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001865 ASSERT_EQ(0, fclose(ref_file));
1866 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001867 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001868 remove(ref_filename.c_str());
1869 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001870 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001871}
1872
pbosc7a65692016-05-06 12:50:04 -07001873TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001874 VerifyDebugDumpTest(kIntFormat);
1875}
1876
pbosc7a65692016-05-06 12:50:04 -07001877TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001878 VerifyDebugDumpTest(kFloatFormat);
1879}
1880#endif
1881
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001882// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001883TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001884 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001885 const std::string filename =
1886 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001887 {
1888 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1889 EXPECT_FALSE(aec_dump);
1890 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001891
1892#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1893 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001894 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001895
aleloif4dd1912017-06-15 01:55:38 -07001896 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1897 EXPECT_TRUE(aec_dump);
1898 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001899 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001900 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001901 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001902
1903 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001904 FILE* fid = fopen(filename.c_str(), "r");
1905 ASSERT_TRUE(fid != NULL);
1906
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001907 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001908 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001909 ASSERT_EQ(0, remove(filename.c_str()));
1910#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001911 // Verify the file has NOT been written.
1912 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1913#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1914}
1915
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001916// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001917TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001918 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1919
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001920 const std::string filename =
1921 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001922 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001923 ASSERT_TRUE(fid);
1924
1925#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1926 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001927 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001928
aleloif4dd1912017-06-15 01:55:38 -07001929 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1930 EXPECT_TRUE(aec_dump);
1931 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001932 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001933 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001934 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001935
1936 // Verify the file has been written.
1937 fid = fopen(filename.c_str(), "r");
1938 ASSERT_TRUE(fid != NULL);
1939
1940 // Clean it up.
1941 ASSERT_EQ(0, fclose(fid));
1942 ASSERT_EQ(0, remove(filename.c_str()));
1943#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001944 ASSERT_EQ(0, fclose(fid));
1945#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1946}
1947
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001948TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001949 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001950 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001951
1952 Config config;
1953 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001954 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001955 EnableAllComponents();
1956 EnableAllAPComponents(fapm.get());
1957 for (int i = 0; i < ref_data.test_size(); i++) {
1958 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1959
1960 audioproc::Test* test = ref_data.mutable_test(i);
1961 // TODO(ajm): Restore downmixing test cases.
1962 if (test->num_input_channels() != test->num_output_channels())
1963 continue;
1964
Peter Kasting69558702016-01-12 16:26:35 -08001965 const size_t num_render_channels =
1966 static_cast<size_t>(test->num_reverse_channels());
1967 const size_t num_input_channels =
1968 static_cast<size_t>(test->num_input_channels());
1969 const size_t num_output_channels =
1970 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001971 const size_t samples_per_channel = static_cast<size_t>(
1972 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001973
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001974 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1975 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001976 Init(fapm.get());
1977
1978 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001979 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1980 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001981
1982 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001983 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001984 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1985 ReadFrame(near_file_, frame_, float_cb_.get())) {
1986 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1987
aluebsb0319552016-03-17 20:39:53 -07001988 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001989 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1990 revfloat_cb_->channels(),
1991 samples_per_channel,
1992 test->sample_rate(),
1993 LayoutFromChannels(num_render_channels)));
1994
1995 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1996 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1997 apm_->echo_cancellation()->set_stream_drift_samples(0);
1998 fapm->echo_cancellation()->set_stream_drift_samples(0);
1999 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
2000 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
2001
2002 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07002003 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002004 output_int16.channels());
2005
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002006 EXPECT_NOERR(fapm->ProcessStream(
2007 float_cb_->channels(),
2008 samples_per_channel,
2009 test->sample_rate(),
2010 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002011 test->sample_rate(),
2012 LayoutFromChannels(num_output_channels),
2013 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08002014 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002015 FloatToS16(float_cb_->channels()[j],
2016 samples_per_channel,
2017 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002018 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002019 float snr = ComputeSNR(output_int16.channels()[j],
2020 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002021 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07002022
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002023 const float kVarianceThreshold = 20;
2024 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002025
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002026 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002027 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2028 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002029 }
2030 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002031
2032 analog_level = fapm->gain_control()->stream_analog_level();
2033 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2034 fapm->gain_control()->stream_analog_level());
2035 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2036 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002037 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2038 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002039 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002040
2041 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002042 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002043 }
aluebs776593b2016-03-15 14:04:58 -07002044
2045#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2046 const size_t kMaxNumBadChunks = 0;
2047#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2048 // There are a few chunks in the fixed-point profile that give low SNR.
2049 // Listening confirmed the difference is acceptable.
2050 const size_t kMaxNumBadChunks = 60;
2051#endif
2052 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2053
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002054 rewind(far_file_);
2055 rewind(near_file_);
2056 }
2057}
2058
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002059// TODO(andrew): Add a test to process a few frames with different combinations
2060// of enabled components.
2061
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002062TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002063 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002064 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002065
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002066 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002067 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002068 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002069 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002070 for (size_t i = 0; i < arraysize(kChannels); i++) {
2071 for (size_t j = 0; j < arraysize(kChannels); j++) {
2072 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002073 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002074 test->set_num_reverse_channels(kChannels[i]);
2075 test->set_num_input_channels(kChannels[j]);
2076 test->set_num_output_channels(kChannels[j]);
2077 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002078 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002079 }
2080 }
2081 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002082#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2083 // To test the extended filter mode.
2084 audioproc::Test* test = ref_data.add_test();
2085 test->set_num_reverse_channels(2);
2086 test->set_num_input_channels(2);
2087 test->set_num_output_channels(2);
2088 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2089 test->set_use_aec_extended_filter(true);
2090#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002091 }
2092
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002093 for (int i = 0; i < ref_data.test_size(); i++) {
2094 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002095
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002096 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002097 // TODO(ajm): We no longer allow different input and output channels. Skip
2098 // these tests for now, but they should be removed from the set.
2099 if (test->num_input_channels() != test->num_output_channels())
2100 continue;
2101
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002102 Config config;
2103 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002104 config.Set<ExtendedFilter>(
2105 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002106 apm_.reset(AudioProcessing::Create(config));
2107
2108 EnableAllComponents();
2109
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002110 Init(test->sample_rate(),
2111 test->sample_rate(),
2112 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002113 static_cast<size_t>(test->num_input_channels()),
2114 static_cast<size_t>(test->num_output_channels()),
2115 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002116 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002117
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002118 int frame_count = 0;
2119 int has_echo_count = 0;
2120 int has_voice_count = 0;
2121 int is_saturated_count = 0;
2122 int analog_level = 127;
2123 int analog_level_average = 0;
2124 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002125 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002126#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2127 int stats_index = 0;
2128#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002129
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002130 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002131 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002132
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002133 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2134
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002135 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002136 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002137 EXPECT_EQ(apm_->kNoError,
2138 apm_->gain_control()->set_stream_analog_level(analog_level));
2139
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002140 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002141
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002142 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002143 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2144 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002145
2146 max_output_average += MaxAudioFrame(*frame_);
2147
2148 if (apm_->echo_cancellation()->stream_has_echo()) {
2149 has_echo_count++;
2150 }
2151
2152 analog_level = apm_->gain_control()->stream_analog_level();
2153 analog_level_average += analog_level;
2154 if (apm_->gain_control()->stream_is_saturated()) {
2155 is_saturated_count++;
2156 }
2157 if (apm_->voice_detection()->stream_has_voice()) {
2158 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002159 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002160 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002161 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002162 }
2163
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002164 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2165
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002166 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07002167 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002168 sizeof(int16_t),
2169 frame_size,
2170 out_file_);
2171 ASSERT_EQ(frame_size, write_count);
2172
2173 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002174 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002175 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002176
2177#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2178 const int kStatsAggregationFrameNum = 100; // 1 second.
2179 if (frame_count % kStatsAggregationFrameNum == 0) {
2180 // Get echo metrics.
2181 EchoCancellation::Metrics echo_metrics;
2182 EXPECT_EQ(apm_->kNoError,
2183 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2184
2185 // Get delay metrics.
2186 int median = 0;
2187 int std = 0;
2188 float fraction_poor_delays = 0;
2189 EXPECT_EQ(apm_->kNoError,
2190 apm_->echo_cancellation()->GetDelayMetrics(
2191 &median, &std, &fraction_poor_delays));
2192
2193 // Get RMS.
2194 int rms_level = apm_->level_estimator()->RMS();
2195 EXPECT_LE(0, rms_level);
2196 EXPECT_GE(127, rms_level);
2197
2198 if (!write_ref_data) {
2199 const audioproc::Test::EchoMetrics& reference =
2200 test->echo_metrics(stats_index);
2201 TestStats(echo_metrics.residual_echo_return_loss,
2202 reference.residual_echo_return_loss());
2203 TestStats(echo_metrics.echo_return_loss,
2204 reference.echo_return_loss());
2205 TestStats(echo_metrics.echo_return_loss_enhancement,
2206 reference.echo_return_loss_enhancement());
2207 TestStats(echo_metrics.a_nlp,
2208 reference.a_nlp());
2209 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2210 reference.divergent_filter_fraction());
2211
2212 const audioproc::Test::DelayMetrics& reference_delay =
2213 test->delay_metrics(stats_index);
2214 EXPECT_EQ(reference_delay.median(), median);
2215 EXPECT_EQ(reference_delay.std(), std);
2216 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2217 fraction_poor_delays);
2218
2219 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2220
2221 ++stats_index;
2222 } else {
2223 audioproc::Test::EchoMetrics* message =
2224 test->add_echo_metrics();
2225 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2226 message->mutable_residual_echo_return_loss());
2227 WriteStatsMessage(echo_metrics.echo_return_loss,
2228 message->mutable_echo_return_loss());
2229 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2230 message->mutable_echo_return_loss_enhancement());
2231 WriteStatsMessage(echo_metrics.a_nlp,
2232 message->mutable_a_nlp());
2233 message->set_divergent_filter_fraction(
2234 echo_metrics.divergent_filter_fraction);
2235
2236 audioproc::Test::DelayMetrics* message_delay =
2237 test->add_delay_metrics();
2238 message_delay->set_median(median);
2239 message_delay->set_std(std);
2240 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2241
2242 test->add_rms_level(rms_level);
2243 }
2244 }
2245#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002246 }
2247 max_output_average /= frame_count;
2248 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002249 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002250
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002251 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002252 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002253 // When running the test on a N7 we get a {2, 6} difference of
2254 // |has_voice_count| and |max_output_average| is up to 18 higher.
2255 // All numbers being consistently higher on N7 compare to ref_data.
2256 // TODO(bjornv): If we start getting more of these offsets on Android we
2257 // should consider a different approach. Either using one slack for all,
2258 // or generate a separate android reference.
2259#if defined(WEBRTC_ANDROID)
2260 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002261 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002262 const int kMaxOutputAverageOffset = 9;
2263 const int kMaxOutputAverageNear = 9;
2264#else
2265 const int kHasVoiceCountOffset = 0;
2266 const int kHasVoiceCountNear = kIntNear;
2267 const int kMaxOutputAverageOffset = 0;
2268 const int kMaxOutputAverageNear = kIntNear;
2269#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002270 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002271 EXPECT_NEAR(test->has_voice_count(),
2272 has_voice_count - kHasVoiceCountOffset,
2273 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002274 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002275
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002276 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002277 EXPECT_NEAR(test->max_output_average(),
2278 max_output_average - kMaxOutputAverageOffset,
2279 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002280#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002281 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002282 EXPECT_NEAR(test->ns_speech_probability_average(),
2283 ns_speech_prob_average,
2284 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002285#endif
2286 } else {
2287 test->set_has_echo_count(has_echo_count);
2288 test->set_has_voice_count(has_voice_count);
2289 test->set_is_saturated_count(is_saturated_count);
2290
2291 test->set_analog_level_average(analog_level_average);
2292 test->set_max_output_average(max_output_average);
2293
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002294#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002295 EXPECT_LE(0.0f, ns_speech_prob_average);
2296 EXPECT_GE(1.0f, ns_speech_prob_average);
2297 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002298#endif
2299 }
2300
2301 rewind(far_file_);
2302 rewind(near_file_);
2303 }
2304
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002305 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002306 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002307 }
2308}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002309
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002310TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2311 struct ChannelFormat {
2312 AudioProcessing::ChannelLayout in_layout;
2313 AudioProcessing::ChannelLayout out_layout;
2314 };
2315 ChannelFormat cf[] = {
2316 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2317 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2318 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2319 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002320
kwiberg62eaacf2016-02-17 06:39:05 -08002321 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002322 // Enable one component just to ensure some processing takes place.
2323 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002324 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002325 const int in_rate = 44100;
2326 const int out_rate = 48000;
2327 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2328 TotalChannelsFromLayout(cf[i].in_layout));
2329 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2330 ChannelsFromLayout(cf[i].out_layout));
2331
2332 // Run over a few chunks.
2333 for (int j = 0; j < 10; ++j) {
2334 EXPECT_NOERR(ap->ProcessStream(
2335 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002336 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002337 in_rate,
2338 cf[i].in_layout,
2339 out_rate,
2340 cf[i].out_layout,
2341 out_cb.channels()));
2342 }
2343 }
2344}
2345
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002346// Compares the reference and test arrays over a region around the expected
2347// delay. Finds the highest SNR in that region and adds the variance and squared
2348// error results to the supplied accumulators.
2349void UpdateBestSNR(const float* ref,
2350 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002351 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002352 int expected_delay,
2353 double* variance_acc,
2354 double* sq_error_acc) {
2355 double best_snr = std::numeric_limits<double>::min();
2356 double best_variance = 0;
2357 double best_sq_error = 0;
2358 // Search over a region of eight samples around the expected delay.
2359 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2360 ++delay) {
2361 double sq_error = 0;
2362 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002363 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002364 double error = test[i + delay] - ref[i];
2365 sq_error += error * error;
2366 variance += ref[i] * ref[i];
2367 }
2368
2369 if (sq_error == 0) {
2370 *variance_acc += variance;
2371 return;
2372 }
2373 double snr = variance / sq_error;
2374 if (snr > best_snr) {
2375 best_snr = snr;
2376 best_variance = variance;
2377 best_sq_error = sq_error;
2378 }
2379 }
2380
2381 *variance_acc += best_variance;
2382 *sq_error_acc += best_sq_error;
2383}
2384
2385// Used to test a multitude of sample rate and channel combinations. It works
2386// by first producing a set of reference files (in SetUpTestCase) that are
2387// assumed to be correct, as the used parameters are verified by other tests
2388// in this collection. Primarily the reference files are all produced at
2389// "native" rates which do not involve any resampling.
2390
2391// Each test pass produces an output file with a particular format. The output
2392// is matched against the reference file closest to its internal processing
2393// format. If necessary the output is resampled back to its process format.
2394// Due to the resampling distortion, we don't expect identical results, but
2395// enforce SNR thresholds which vary depending on the format. 0 is a special
2396// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002397typedef std::tr1::tuple<int, int, int, int, double, double>
2398 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002399class AudioProcessingTest
2400 : public testing::TestWithParam<AudioProcessingTestData> {
2401 public:
2402 AudioProcessingTest()
2403 : input_rate_(std::tr1::get<0>(GetParam())),
2404 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002405 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2406 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2407 expected_snr_(std::tr1::get<4>(GetParam())),
2408 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409
2410 virtual ~AudioProcessingTest() {}
2411
2412 static void SetUpTestCase() {
2413 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002414 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002415 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002416 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2417 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2418 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002419 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002420 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2421 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2422 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002423 }
2424 }
2425 }
2426 }
2427
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002428 static void TearDownTestCase() {
2429 ClearTempFiles();
2430 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002431
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002432 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002433 // to a file specified with |output_file_prefix|. Both forward and reverse
2434 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002435 static void ProcessFormat(int input_rate,
2436 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002437 int reverse_input_rate,
2438 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002439 size_t num_input_channels,
2440 size_t num_output_channels,
2441 size_t num_reverse_input_channels,
2442 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002443 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002444 Config config;
2445 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002446 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002447 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002448
ekmeyerson60d9b332015-08-14 10:35:55 -07002449 ProcessingConfig processing_config = {
2450 {{input_rate, num_input_channels},
2451 {output_rate, num_output_channels},
2452 {reverse_input_rate, num_reverse_input_channels},
2453 {reverse_output_rate, num_reverse_output_channels}}};
2454 ap->Initialize(processing_config);
2455
2456 FILE* far_file =
2457 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002458 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002459 FILE* out_file =
2460 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2461 reverse_input_rate, reverse_output_rate,
2462 num_input_channels, num_output_channels,
2463 num_reverse_input_channels,
2464 num_reverse_output_channels, kForward).c_str(),
2465 "wb");
2466 FILE* rev_out_file =
2467 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2468 reverse_input_rate, reverse_output_rate,
2469 num_input_channels, num_output_channels,
2470 num_reverse_input_channels,
2471 num_reverse_output_channels, kReverse).c_str(),
2472 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002473 ASSERT_TRUE(far_file != NULL);
2474 ASSERT_TRUE(near_file != NULL);
2475 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002476 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002477
2478 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2479 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002480 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2481 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002482 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2483 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002484 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2485 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002486
2487 // Temporary buffers.
2488 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002489 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2490 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002491 std::unique_ptr<float[]> float_data(new float[max_length]);
2492 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002493
2494 int analog_level = 127;
2495 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2496 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002497 EXPECT_NOERR(ap->ProcessReverseStream(
2498 rev_cb.channels(), processing_config.reverse_input_stream(),
2499 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002500
2501 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2502 ap->echo_cancellation()->set_stream_drift_samples(0);
2503 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2504
2505 EXPECT_NOERR(ap->ProcessStream(
2506 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002507 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002508 input_rate,
2509 LayoutFromChannels(num_input_channels),
2510 output_rate,
2511 LayoutFromChannels(num_output_channels),
2512 out_cb.channels()));
2513
ekmeyerson60d9b332015-08-14 10:35:55 -07002514 // Dump forward output to file.
2515 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002516 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002517 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002518
pkasting25702cb2016-01-08 13:50:27 -08002519 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002520 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002521 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002522
ekmeyerson60d9b332015-08-14 10:35:55 -07002523 // Dump reverse output to file.
2524 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2525 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002526 size_t rev_out_length =
2527 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002528
pkasting25702cb2016-01-08 13:50:27 -08002529 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002530 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2531 rev_out_file));
2532
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002533 analog_level = ap->gain_control()->stream_analog_level();
2534 }
2535 fclose(far_file);
2536 fclose(near_file);
2537 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002538 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002539 }
2540
2541 protected:
2542 int input_rate_;
2543 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002544 int reverse_input_rate_;
2545 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002546 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002547 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002548};
2549
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002550TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002551 struct ChannelFormat {
2552 int num_input;
2553 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002554 int num_reverse_input;
2555 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002556 };
2557 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002558 {1, 1, 1, 1},
2559 {1, 1, 2, 1},
2560 {2, 1, 1, 1},
2561 {2, 1, 2, 1},
2562 {2, 2, 1, 1},
2563 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002564 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002565
pkasting25702cb2016-01-08 13:50:27 -08002566 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002567 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2568 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2569 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002570
ekmeyerson60d9b332015-08-14 10:35:55 -07002571 // Verify output for both directions.
2572 std::vector<StreamDirection> stream_directions;
2573 stream_directions.push_back(kForward);
2574 stream_directions.push_back(kReverse);
2575 for (StreamDirection file_direction : stream_directions) {
2576 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2577 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2578 const int out_num =
2579 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2580 const double expected_snr =
2581 file_direction ? expected_reverse_snr_ : expected_snr_;
2582
2583 const int min_ref_rate = std::min(in_rate, out_rate);
2584 int ref_rate;
2585
2586 if (min_ref_rate > 32000) {
2587 ref_rate = 48000;
2588 } else if (min_ref_rate > 16000) {
2589 ref_rate = 32000;
2590 } else if (min_ref_rate > 8000) {
2591 ref_rate = 16000;
2592 } else {
2593 ref_rate = 8000;
2594 }
aluebs776593b2016-03-15 14:04:58 -07002595#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002596 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002597 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002598 }
2599#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002600 FILE* out_file = fopen(
2601 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2602 reverse_output_rate_, cf[i].num_input,
2603 cf[i].num_output, cf[i].num_reverse_input,
2604 cf[i].num_reverse_output, file_direction).c_str(),
2605 "rb");
2606 // The reference files always have matching input and output channels.
2607 FILE* ref_file = fopen(
2608 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2609 cf[i].num_output, cf[i].num_output,
2610 cf[i].num_reverse_output, cf[i].num_reverse_output,
2611 file_direction).c_str(),
2612 "rb");
2613 ASSERT_TRUE(out_file != NULL);
2614 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002615
pkasting25702cb2016-01-08 13:50:27 -08002616 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2617 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002618 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002619 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002620 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002621 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002622 // Data from the resampled output, in case the reference and output rates
2623 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002624 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002625
ekmeyerson60d9b332015-08-14 10:35:55 -07002626 PushResampler<float> resampler;
2627 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002628
ekmeyerson60d9b332015-08-14 10:35:55 -07002629 // Compute the resampling delay of the output relative to the reference,
2630 // to find the region over which we should search for the best SNR.
2631 float expected_delay_sec = 0;
2632 if (in_rate != ref_rate) {
2633 // Input resampling delay.
2634 expected_delay_sec +=
2635 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2636 }
2637 if (out_rate != ref_rate) {
2638 // Output resampling delay.
2639 expected_delay_sec +=
2640 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2641 // Delay of converting the output back to its processing rate for
2642 // testing.
2643 expected_delay_sec +=
2644 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2645 }
2646 int expected_delay =
2647 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002648
ekmeyerson60d9b332015-08-14 10:35:55 -07002649 double variance = 0;
2650 double sq_error = 0;
2651 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2652 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2653 float* out_ptr = out_data.get();
2654 if (out_rate != ref_rate) {
2655 // Resample the output back to its internal processing rate if
2656 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002657 ASSERT_EQ(ref_length,
2658 static_cast<size_t>(resampler.Resample(
2659 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002660 out_ptr = cmp_data.get();
2661 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002662
ekmeyerson60d9b332015-08-14 10:35:55 -07002663 // Update the |sq_error| and |variance| accumulators with the highest
2664 // SNR of reference vs output.
2665 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2666 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002667 }
2668
ekmeyerson60d9b332015-08-14 10:35:55 -07002669 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2670 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2671 << cf[i].num_input << ", " << cf[i].num_output << ", "
2672 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2673 << ", " << file_direction << "): ";
2674 if (sq_error > 0) {
2675 double snr = 10 * log10(variance / sq_error);
2676 EXPECT_GE(snr, expected_snr);
2677 EXPECT_NE(0, expected_snr);
2678 std::cout << "SNR=" << snr << " dB" << std::endl;
2679 } else {
aluebs776593b2016-03-15 14:04:58 -07002680 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002681 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002682
ekmeyerson60d9b332015-08-14 10:35:55 -07002683 fclose(out_file);
2684 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002685 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002686 }
2687}
2688
2689#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2690INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002691 CommonFormats,
2692 AudioProcessingTest,
2693 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002694 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2695 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002696 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2697 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2698 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2699 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2700 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2701 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2702 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2703 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2704 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002705
ekmeyerson60d9b332015-08-14 10:35:55 -07002706 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2707 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2708 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2709 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2710 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2711 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2712 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2713 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2714 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2715 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2716 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2717 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002718
ekmeyerson60d9b332015-08-14 10:35:55 -07002719 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2720 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2721 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2722 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2723 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2724 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2725 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2726 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2727 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2728 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2729 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2730 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002731
ekmeyerson60d9b332015-08-14 10:35:55 -07002732 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2733 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2734 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2735 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2736 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2737 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2738 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2739 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2740 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2741 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002742 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002743 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002744
2745#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2746INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002747 CommonFormats,
2748 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002749 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2750 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2751 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2752 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2753 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2754 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002755 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2756 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2757 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2758 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2759 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2760 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002761
aluebs776593b2016-03-15 14:04:58 -07002762 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2763 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2764 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002765 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2766 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2767 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2768 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2769 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2770 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2771 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2772 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2773 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002774
aluebs776593b2016-03-15 14:04:58 -07002775 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2776 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2777 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2778 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2779 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2780 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2781 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2782 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2783 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002784 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2785 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2786 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002787
ekmeyerson60d9b332015-08-14 10:35:55 -07002788 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2789 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2790 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2791 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2792 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2793 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2794 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2795 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2796 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2797 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002798 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002799 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002800#endif
2801
niklase@google.com470e71d2011-07-07 08:21:25 +00002802} // namespace
peahc19f3122016-10-07 14:54:10 -07002803
2804TEST(ApmConfiguration, DefaultBehavior) {
2805 // Verify that the level controller is default off, it can be activated using
2806 // the config, and that the default initial level is maintained after the
2807 // config has been applied.
2808 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002809 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002810 AudioProcessing::Config config;
2811 EXPECT_FALSE(apm->config_.level_controller.enabled);
2812 // TODO(peah): Add test for the existence of the level controller object once
2813 // that is created only when that is specified in the config.
2814 // TODO(peah): Remove the testing for
2815 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2816 // is instead used to activate the level controller.
2817 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2818 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2819 apm->config_.level_controller.initial_peak_level_dbfs,
2820 std::numeric_limits<float>::epsilon());
2821 config.level_controller.enabled = true;
2822 apm->ApplyConfig(config);
2823 EXPECT_TRUE(apm->config_.level_controller.enabled);
2824 // TODO(peah): Add test for the existence of the level controller object once
2825 // that is created only when the that is specified in the config.
2826 // TODO(peah): Remove the testing for
2827 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2828 // is instead used to activate the level controller.
2829 EXPECT_TRUE(apm->capture_nonlocked_.level_controller_enabled);
2830 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2831 apm->config_.level_controller.initial_peak_level_dbfs,
2832 std::numeric_limits<float>::epsilon());
2833}
2834
2835TEST(ApmConfiguration, ValidConfigBehavior) {
2836 // Verify that the initial level can be specified and is retained after the
2837 // config has been applied.
2838 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002839 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002840 AudioProcessing::Config config;
2841 config.level_controller.initial_peak_level_dbfs = -50.f;
2842 apm->ApplyConfig(config);
2843 EXPECT_FALSE(apm->config_.level_controller.enabled);
2844 // TODO(peah): Add test for the existence of the level controller object once
2845 // that is created only when the that is specified in the config.
2846 // TODO(peah): Remove the testing for
2847 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2848 // is instead used to activate the level controller.
2849 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2850 EXPECT_NEAR(-50.f, apm->config_.level_controller.initial_peak_level_dbfs,
2851 std::numeric_limits<float>::epsilon());
2852}
2853
2854TEST(ApmConfiguration, InValidConfigBehavior) {
2855 // Verify that the config is properly reset when nonproper values are applied
2856 // for the initial level.
2857
2858 // Verify that the config is properly reset when the specified initial peak
2859 // level is too low.
2860 std::unique_ptr<AudioProcessingImpl> apm(
peaha9cc40b2017-06-29 08:32:09 -07002861 new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002862 AudioProcessing::Config config;
2863 config.level_controller.enabled = true;
2864 config.level_controller.initial_peak_level_dbfs = -101.f;
2865 apm->ApplyConfig(config);
2866 EXPECT_FALSE(apm->config_.level_controller.enabled);
2867 // TODO(peah): Add test for the existence of the level controller object once
2868 // that is created only when the that is specified in the config.
2869 // TODO(peah): Remove the testing for
2870 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2871 // is instead used to activate the level controller.
2872 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2873 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2874 apm->config_.level_controller.initial_peak_level_dbfs,
2875 std::numeric_limits<float>::epsilon());
2876
2877 // Verify that the config is properly reset when the specified initial peak
2878 // level is too high.
peaha9cc40b2017-06-29 08:32:09 -07002879 apm.reset(new rtc::RefCountedObject<AudioProcessingImpl>(webrtc::Config()));
peahc19f3122016-10-07 14:54:10 -07002880 config = AudioProcessing::Config();
2881 config.level_controller.enabled = true;
2882 config.level_controller.initial_peak_level_dbfs = 1.f;
2883 apm->ApplyConfig(config);
2884 EXPECT_FALSE(apm->config_.level_controller.enabled);
2885 // TODO(peah): Add test for the existence of the level controller object once
2886 // that is created only when that is specified in the config.
2887 // TODO(peah): Remove the testing for
2888 // apm->capture_nonlocked_.level_controller_enabled once the value in config_
2889 // is instead used to activate the level controller.
2890 EXPECT_FALSE(apm->capture_nonlocked_.level_controller_enabled);
2891 EXPECT_NEAR(kTargetLcPeakLeveldBFS,
2892 apm->config_.level_controller.initial_peak_level_dbfs,
2893 std::numeric_limits<float>::epsilon());
2894}
2895
Sam Zackrisson0beac582017-09-25 12:04:02 +02002896TEST(ApmConfiguration, EnablePostProcessing) {
2897 // Verify that apm uses a capture post processing module if one is provided.
2898 webrtc::Config webrtc_config;
2899 auto mock_post_processor_ptr =
2900 new testing::NiceMock<test::MockPostProcessing>();
2901 auto mock_post_processor =
2902 std::unique_ptr<PostProcessing>(mock_post_processor_ptr);
2903 rtc::scoped_refptr<AudioProcessing> apm = AudioProcessing::Create(
2904 webrtc_config, std::move(mock_post_processor), nullptr);
2905
2906 AudioFrame audio;
2907 audio.num_channels_ = 1;
2908 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2909
2910 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
2911 std::cout << apm->ProcessStream(&audio) << std::endl;
2912}
2913
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002914} // namespace webrtc