blob: 9401ebdfa51f2c2ca9791a6e7cc1cb6339156138 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000010#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000014#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080015#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000016#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/include/audio_util.h"
19#include "common_audio/resampler/include/push_resampler.h"
20#include "common_audio/resampler/push_sinc_resampler.h"
21#include "common_audio/signal_processing/include/signal_processing_library.h"
22#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
23#include "modules/audio_processing/audio_processing_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/common.h"
25#include "modules/audio_processing/include/audio_processing.h"
Sam Zackrisson0beac582017-09-25 12:04:02 +020026#include "modules/audio_processing/include/mock_audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/test/protobuf_utils.h"
28#include "modules/audio_processing/test/test_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/arraysize.h"
30#include "rtc_base/checks.h"
Minyue Li656d6092018-08-10 15:38:52 +020031#include "rtc_base/fakeclock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/gtest_prod_util.h"
33#include "rtc_base/ignore_wundef.h"
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +010034#include "rtc_base/numerics/safe_conversions.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010035#include "rtc_base/numerics/safe_minmax.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/protobuf_utils.h"
Niels Möller84255bb2017-10-06 13:43:23 +020037#include "rtc_base/refcountedobject.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020038#include "rtc_base/strings/string_builder.h"
Alessio Bazzicac054e782018-04-16 12:10:09 +020039#include "rtc_base/swap_queue.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020040#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/task_queue.h"
42#include "rtc_base/thread.h"
43#include "system_wrappers/include/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/gtest.h"
45#include "test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070046
47RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000049#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000050#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#endif
kwiberg77eab702016-09-28 17:42:01 -070053RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000054
andrew@webrtc.org27c69802014-02-18 20:24:56 +000055namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000056namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000057
ekmeyerson60d9b332015-08-14 10:35:55 -070058// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
59// applicable.
60
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000061// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000062// re-implemented; see comment at the bottom of this file. For now, the user has
63// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000064// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000065// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000066// be set to true with the command-line switch --write_ref_data.
67bool write_ref_data = false;
mbonadei7c2c8432017-04-07 00:59:12 -070068const int32_t kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070069const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000070
aluebseb3603b2016-04-20 15:27:58 -070071#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
72// Android doesn't support 48kHz.
73const int kProcessSampleRates[] = {8000, 16000, 32000};
74#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070075const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070076#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000077
ekmeyerson60d9b332015-08-14 10:35:55 -070078enum StreamDirection { kForward = 0, kReverse };
79
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000080void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000082 cb->num_channels());
83 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000084 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000085 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000086 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080087 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000088 S16ToFloat(cb_int.channels()[i],
89 cb->num_frames(),
90 cb->channels()[i]);
91 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000092}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000093
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000094void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
yujo36b1a5f2017-06-12 12:45:32 -070095 ConvertToFloat(frame.data(), cb);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000096}
97
andrew@webrtc.org103657b2014-04-24 18:28:56 +000098// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080099size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000100 switch (layout) {
101 case AudioProcessing::kMono:
102 return 1;
103 case AudioProcessing::kMonoAndKeyboard:
104 case AudioProcessing::kStereo:
105 return 2;
106 case AudioProcessing::kStereoAndKeyboard:
107 return 3;
108 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700109 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800110 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000111}
112
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000113int TruncateToMultipleOf10(int value) {
114 return (value / 10) * 10;
115}
116
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000117void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800118 size_t samples_per_channel) {
119 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000120 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000121}
122
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000123void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800124 size_t samples_per_channel) {
125 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000126 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
127}
128
pkasting25702cb2016-01-08 13:50:27 -0800129void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
130 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000131 stereo[i * 2 + 1] = stereo[i * 2];
132 }
133}
134
yujo36b1a5f2017-06-12 12:45:32 -0700135void VerifyChannelsAreEqual(const int16_t* stereo, size_t samples_per_channel) {
pkasting25702cb2016-01-08 13:50:27 -0800136 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000137 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
138 }
139}
140
141void SetFrameTo(AudioFrame* frame, int16_t value) {
yujo36b1a5f2017-06-12 12:45:32 -0700142 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700143 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
144 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700145 frame_data[i] = value;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000146 }
147}
148
149void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800150 ASSERT_EQ(2u, frame->num_channels_);
yujo36b1a5f2017-06-12 12:45:32 -0700151 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 frame_data[i] = left;
154 frame_data[i + 1] = right;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000155 }
156}
157
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000158void ScaleFrame(AudioFrame* frame, float scale) {
yujo36b1a5f2017-06-12 12:45:32 -0700159 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
161 ++i) {
yujo36b1a5f2017-06-12 12:45:32 -0700162 frame_data[i] = FloatS16ToS16(frame_data[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000163 }
164}
165
andrew@webrtc.org81865342012-10-27 00:28:27 +0000166bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000167 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000168 return false;
169 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000170 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000171 return false;
172 }
yujo36b1a5f2017-06-12 12:45:32 -0700173 if (memcmp(frame1.data(), frame2.data(),
andrew@webrtc.org81865342012-10-27 00:28:27 +0000174 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000175 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000176 return false;
177 }
178 return true;
179}
180
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000181void EnableAllAPComponents(AudioProcessing* ap) {
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200182 AudioProcessing::Config apm_config = ap->GetConfig();
183 apm_config.echo_canceller.enabled = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000184#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200185 apm_config.echo_canceller.mobile_mode = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000186
187 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
188 EXPECT_NOERR(ap->gain_control()->Enable(true));
189#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200190 apm_config.echo_canceller.mobile_mode = false;
Sam Zackrisson1a039602018-09-13 13:37:48 +0200191 apm_config.echo_canceller.legacy_moderate_suppression_level = true;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000192
193 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
194 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
195 EXPECT_NOERR(ap->gain_control()->Enable(true));
196#endif
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000197
peah8271d042016-11-22 07:24:52 -0800198 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
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100247 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
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) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200259 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000260 // 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) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200280 rtc::StringBuilder 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
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200312// Only remove "out" files. Keep "ref" files.
313void ClearTempOutFiles() {
314 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
315 const std::string& filename = it->first;
316 if (filename.substr(0, 3).compare("out") == 0) {
317 remove(it->second.c_str());
318 temp_filenames.erase(it++);
319 } else {
320 it++;
321 }
322 }
323}
324
Alex Loiko890988c2017-08-31 10:25:48 +0200325void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000326 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000327 ASSERT_TRUE(file != NULL);
328 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000329 fclose(file);
330}
331
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000332// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
333// stereo) file, converts to deinterleaved float (optionally downmixing) and
334// returns the result in |cb|. Returns false if the file ended (or on error) and
335// true otherwise.
336//
337// |int_data| and |float_data| are just temporary space that must be
338// sufficiently large to hold the 10 ms chunk.
339bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
340 ChannelBuffer<float>* cb) {
341 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000342 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000343 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
344 if (read_count != frame_size) {
345 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700346 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000347 return false; // This is expected.
348 }
349
350 S16ToFloat(int_data, frame_size, float_data);
351 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000352 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000353 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000354 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000355 cb->channels());
356 }
357
358 return true;
359}
360
niklase@google.com470e71d2011-07-07 08:21:25 +0000361class ApmTest : public ::testing::Test {
362 protected:
363 ApmTest();
364 virtual void SetUp();
365 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000366
367 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000368 }
369
370 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000371 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000372 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000373
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000374 // Used to select between int and float interface tests.
375 enum Format {
376 kIntFormat,
377 kFloatFormat
378 };
379
380 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000381 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000382 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800383 size_t num_input_channels,
384 size_t num_output_channels,
385 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000386 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000387 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000388 void EnableAllComponents();
389 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000390 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000391 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000392 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
393 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000394 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000395 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
396 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700397 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800398 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700399 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800400 void TestChangingForwardChannels(size_t num_in_channels,
401 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800403 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700404 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000405 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
406 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000407 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000408 int ProcessStreamChooser(Format format);
409 int AnalyzeReverseStreamChooser(Format format);
410 void ProcessDebugDump(const std::string& in_filename,
411 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800412 Format format,
413 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000414 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000415
416 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000417 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800418 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000419 AudioFrame* frame_;
420 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800421 std::unique_ptr<ChannelBuffer<float> > float_cb_;
422 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000423 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800424 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000425 FILE* far_file_;
426 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000427 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428};
429
430ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000431 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000432#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800433 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
434 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000435#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000436#if defined(WEBRTC_MAC)
437 // A different file for Mac is needed because on this platform the AEC
438 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800439 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
440 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000441#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800442 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
443 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000444#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000445#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000447 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000448 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000449 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000450 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000451 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000452 out_file_(NULL) {
453 Config config;
454 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100455 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000456}
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
458void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000459 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
461 frame_ = new AudioFrame();
462 revframe_ = new AudioFrame();
463
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000464 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
467void ApmTest::TearDown() {
468 if (frame_) {
469 delete frame_;
470 }
471 frame_ = NULL;
472
473 if (revframe_) {
474 delete revframe_;
475 }
476 revframe_ = NULL;
477
478 if (far_file_) {
479 ASSERT_EQ(0, fclose(far_file_));
480 }
481 far_file_ = NULL;
482
483 if (near_file_) {
484 ASSERT_EQ(0, fclose(near_file_));
485 }
486 near_file_ = NULL;
487
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000488 if (out_file_) {
489 ASSERT_EQ(0, fclose(out_file_));
490 }
491 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000492}
493
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000494void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000495 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700496 ap->Initialize(
497 {{{frame_->sample_rate_hz_, frame_->num_channels_},
498 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700499 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700500 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000501}
502
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000503void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000504 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000505 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800506 size_t num_input_channels,
507 size_t num_output_channels,
508 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000509 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000510 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000511 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000512 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000513
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000514 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
515 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000516 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000517
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000518 if (far_file_) {
519 ASSERT_EQ(0, fclose(far_file_));
520 }
521 std::string filename = ResourceFilePath("far", sample_rate_hz);
522 far_file_ = fopen(filename.c_str(), "rb");
523 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
524 filename << "\n";
525
526 if (near_file_) {
527 ASSERT_EQ(0, fclose(near_file_));
528 }
529 filename = ResourceFilePath("near", sample_rate_hz);
530 near_file_ = fopen(filename.c_str(), "rb");
531 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
532 filename << "\n";
533
534 if (open_output_file) {
535 if (out_file_) {
536 ASSERT_EQ(0, fclose(out_file_));
537 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700538 filename = OutputFilePath(
539 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
540 reverse_sample_rate_hz, num_input_channels, num_output_channels,
541 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000542 out_file_ = fopen(filename.c_str(), "wb");
543 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
544 filename << "\n";
545 }
546}
547
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000548void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000549 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000550}
551
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
553 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000554 // The files always contain stereo audio.
555 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700556 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000557 sizeof(int16_t),
558 frame_size,
559 file);
560 if (read_count != frame_size) {
561 // Check that the file really ended.
562 EXPECT_NE(0, feof(file));
563 return false; // This is expected.
564 }
565
566 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700567 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000568 frame->samples_per_channel_);
569 }
570
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000572 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000574 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000575}
576
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
578 return ReadFrame(file, frame, NULL);
579}
580
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000581// If the end of the file has been reached, rewind it and attempt to read the
582// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000583void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
584 ChannelBuffer<float>* cb) {
585 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000586 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000588 }
589}
590
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
592 ReadFrameWithRewind(file, frame, NULL);
593}
594
andrew@webrtc.org81865342012-10-27 00:28:27 +0000595void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
596 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000597 EXPECT_EQ(apm_->kNoError,
598 apm_->gain_control()->set_stream_analog_level(127));
599 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000600}
601
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000602int ApmTest::ProcessStreamChooser(Format format) {
603 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000604 return apm_->ProcessStream(frame_);
605 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000606 return apm_->ProcessStream(float_cb_->channels(),
607 frame_->samples_per_channel_,
608 frame_->sample_rate_hz_,
609 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000610 output_sample_rate_hz_,
611 LayoutFromChannels(num_output_channels_),
612 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000613}
614
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000615int ApmTest::AnalyzeReverseStreamChooser(Format format) {
616 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700617 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000618 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000619 return apm_->AnalyzeReverseStream(
620 revfloat_cb_->channels(),
621 revframe_->samples_per_channel_,
622 revframe_->sample_rate_hz_,
623 LayoutFromChannels(revframe_->num_channels_));
624}
625
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000626void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
627 int delay_min, int delay_max) {
628 // The |revframe_| and |frame_| should include the proper frame information,
629 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000630 AudioFrame tmp_frame;
631 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000632 bool causal = true;
633
634 tmp_frame.CopyFrom(*revframe_);
635 SetFrameTo(&tmp_frame, 0);
636
637 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
638 // Initialize the |frame_queue| with empty frames.
639 int frame_delay = delay_ms / 10;
640 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000641 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000642 frame->CopyFrom(tmp_frame);
643 frame_queue.push(frame);
644 frame_delay++;
645 causal = false;
646 }
647 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000648 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000649 frame->CopyFrom(tmp_frame);
650 frame_queue.push(frame);
651 frame_delay--;
652 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000653 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
654 // need enough frames with audio to have reliable estimates, but as few as
655 // possible to keep processing time down. 4.5 seconds seemed to be a good
656 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000657 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000658 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000659 frame->CopyFrom(tmp_frame);
660 // Use the near end recording, since that has more speech in it.
661 ASSERT_TRUE(ReadFrame(near_file_, frame));
662 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000663 AudioFrame* reverse_frame = frame;
664 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000665 if (!causal) {
666 reverse_frame = frame_queue.front();
667 // When we call ProcessStream() the frame is modified, so we can't use the
668 // pointer directly when things are non-causal. Use an intermediate frame
669 // and copy the data.
670 process_frame = &tmp_frame;
671 process_frame->CopyFrom(*frame);
672 }
aluebsb0319552016-03-17 20:39:53 -0700673 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000674 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
675 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
676 frame = frame_queue.front();
677 frame_queue.pop();
678 delete frame;
679
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000680 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000681 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrisson1a039602018-09-13 13:37:48 +0200682 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000683 }
684 }
685
686 rewind(near_file_);
687 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000688 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 frame_queue.pop();
690 delete frame;
691 }
692 // Calculate expected delay estimate and acceptable regions. Further,
693 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700694 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700695 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700696 const int expected_median =
697 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
698 const int expected_median_high = rtc::SafeClamp<int>(
699 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700700 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700701 const int expected_median_low = rtc::SafeClamp<int>(
702 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700703 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000704 // Verify delay metrics.
Sam Zackrisson1a039602018-09-13 13:37:48 +0200705 AudioProcessingStats stats =
706 apm_->GetStatistics(true /* has_remote_tracks */);
707 ASSERT_TRUE(stats.delay_median_ms.has_value());
708 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000709 EXPECT_GE(expected_median_high, median);
710 EXPECT_LE(expected_median_low, median);
711}
712
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000713void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000715 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000716
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000717 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000719 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000720 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000721
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000722 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000723 EXPECT_EQ(apm_->kNoError,
724 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000725 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000726 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000727 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000728
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729 // Other stream parameters set correctly.
Sam Zackrisson1a039602018-09-13 13:37:48 +0200730 AudioProcessing::Config apm_config = apm_->GetConfig();
731 apm_config.echo_canceller.enabled = true;
732 apm_config.echo_canceller.mobile_mode = false;
733 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000734 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000736 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000737 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000738
739 // -- Missing delay --
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,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000753 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000754 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000755 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000756 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
757
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000758 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000759 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000760 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000761 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000762 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000763
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000764 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 EXPECT_EQ(apm_->kNoError,
767 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000768 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000769}
770
771TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000772 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000773}
774
775TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000776 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000779TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
780 EXPECT_EQ(0, apm_->delay_offset_ms());
781 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
782 EXPECT_EQ(50, apm_->stream_delay_ms());
783}
784
785TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
786 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000787 apm_->set_delay_offset_ms(100);
788 EXPECT_EQ(100, apm_->delay_offset_ms());
789 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000790 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000791 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
792 EXPECT_EQ(200, apm_->stream_delay_ms());
793
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000794 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000795 apm_->set_delay_offset_ms(-50);
796 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000797 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
798 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000799 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
800 EXPECT_EQ(50, apm_->stream_delay_ms());
801}
802
Michael Graczyk86c6d332015-07-23 11:41:39 -0700803void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800804 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700805 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000806 frame_->num_channels_ = num_channels;
807 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700808 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000809}
810
Michael Graczyk86c6d332015-07-23 11:41:39 -0700811void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800812 size_t num_in_channels,
813 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 AudioProcessing::Error expected_return) {
815 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
816 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
817
818 EXPECT_EQ(expected_return,
819 apm_->ProcessStream(float_cb_->channels(), input_stream,
820 output_stream, float_cb_->channels()));
821}
822
823void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800824 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 AudioProcessing::Error expected_return) {
826 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700827 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
828 {output_sample_rate_hz_, apm_->num_output_channels()},
829 {frame_->sample_rate_hz_, num_rev_channels},
830 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700831
ekmeyerson60d9b332015-08-14 10:35:55 -0700832 EXPECT_EQ(
833 expected_return,
834 apm_->ProcessReverseStream(
835 float_cb_->channels(), processing_config.reverse_input_stream(),
836 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700837}
838
839TEST_F(ApmTest, ChannelsInt16Interface) {
840 // Testing number of invalid and valid channels.
841 Init(16000, 16000, 16000, 4, 4, 4, false);
842
843 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
844
Peter Kasting69558702016-01-12 16:26:35 -0800845 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700846 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000847 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000848 }
849}
850
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851TEST_F(ApmTest, Channels) {
852 // Testing number of invalid and valid channels.
853 Init(16000, 16000, 16000, 4, 4, 4, false);
854
855 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
856 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
857
Peter Kasting69558702016-01-12 16:26:35 -0800858 for (size_t i = 1; i < 4; ++i) {
859 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700860 // Output channels much be one or match input channels.
861 if (j == 1 || i == j) {
862 TestChangingForwardChannels(i, j, kNoErr);
863 TestChangingReverseChannels(i, kNoErr);
864
865 EXPECT_EQ(i, apm_->num_input_channels());
866 EXPECT_EQ(j, apm_->num_output_channels());
867 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800868 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700869 } else {
870 TestChangingForwardChannels(i, j,
871 AudioProcessing::kBadNumberChannelsError);
872 }
873 }
874 }
875}
876
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000877TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000879 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000880 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000881 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700882 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800883 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000884 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000885 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 }
887}
888
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000889TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000890 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000891 // Enable AEC only.
Sam Zackrisson1a039602018-09-13 13:37:48 +0200892 AudioProcessing::Config apm_config = apm_->GetConfig();
893 apm_config.echo_canceller.enabled = true;
894 apm_config.echo_canceller.mobile_mode = false;
895 apm_->ApplyConfig(apm_config);
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000896 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700897 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000898 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000899
900 // Internally in the AEC the amount of lookahead the delay estimation can
901 // handle is 15 blocks and the maximum delay is set to 60 blocks.
902 const int kLookaheadBlocks = 15;
903 const int kMaxDelayBlocks = 60;
904 // The AEC has a startup time before it actually starts to process. This
905 // procedure can flush the internal far-end buffer, which of course affects
906 // the delay estimation. Therefore, we set a system_delay high enough to
907 // avoid that. The smallest system_delay you can report without flushing the
908 // buffer is 66 ms in 8 kHz.
909 //
910 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
911 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
912 // delay estimation. This should be noted though. In case of test failure,
913 // this could be the cause.
914 const int kSystemDelayMs = 66;
915 // Test a couple of corner cases and verify that the estimated delay is
916 // within a valid region (set to +-1.5 blocks). Note that these cases are
917 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800918 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000919 Init(kProcessSampleRates[i],
920 kProcessSampleRates[i],
921 kProcessSampleRates[i],
922 2,
923 2,
924 2,
925 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000926 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700927 const int num_ms_per_block =
928 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000929 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
930 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
931
932 // 1) Verify correct delay estimate at lookahead boundary.
933 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
934 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
935 delay_max_ms);
936 // 2) A delay less than maximum lookahead should give an delay estimate at
937 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
938 delay_ms -= 20;
939 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
940 delay_max_ms);
941 // 3) Three values around zero delay. Note that we need to compensate for
942 // the fake system_delay.
943 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
944 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
945 delay_max_ms);
946 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
947 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
948 delay_max_ms);
949 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
950 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
951 delay_max_ms);
952 // 4) Verify correct delay estimate at maximum delay boundary.
953 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
954 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
955 delay_max_ms);
956 // 5) A delay above the maximum delay should give an estimate at the
957 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
958 delay_ms += 20;
959 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
960 delay_max_ms);
961 }
962}
963
niklase@google.com470e71d2011-07-07 08:21:25 +0000964TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000965 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000966 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000967 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
968 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
969
niklase@google.com470e71d2011-07-07 08:21:25 +0000970 // Toggle routing modes
971 EchoControlMobile::RoutingMode mode[] = {
972 EchoControlMobile::kQuietEarpieceOrHeadset,
973 EchoControlMobile::kEarpiece,
974 EchoControlMobile::kLoudEarpiece,
975 EchoControlMobile::kSpeakerphone,
976 EchoControlMobile::kLoudSpeakerphone,
977 };
pkasting25702cb2016-01-08 13:50:27 -0800978 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000979 EXPECT_EQ(apm_->kNoError,
980 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
981 EXPECT_EQ(mode[i],
982 apm_->echo_control_mobile()->routing_mode());
983 }
984 // Turn comfort noise off/on
985 EXPECT_EQ(apm_->kNoError,
986 apm_->echo_control_mobile()->enable_comfort_noise(false));
987 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
988 EXPECT_EQ(apm_->kNoError,
989 apm_->echo_control_mobile()->enable_comfort_noise(true));
990 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000991 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +0000992 const size_t echo_path_size =
993 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -0800994 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
995 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000996 EXPECT_EQ(apm_->kNullPointerError,
997 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
998 EXPECT_EQ(apm_->kNullPointerError,
999 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1000 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001001 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001002 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001003 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001004 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001005 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001006 echo_path_in[i] = echo_path_out[i] + 1;
1007 }
1008 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001009 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001010 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001011 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1012 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001013 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001014 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1015 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001016 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001017 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1018 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001019
1020 // Process a few frames with NS in the default disabled state. This exercises
1021 // a different codepath than with it enabled.
1022 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1023 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1024 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1025 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1026
niklase@google.com470e71d2011-07-07 08:21:25 +00001027 // Turn AECM off
1028 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1029 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1030}
1031
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001032TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001033 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001034 EXPECT_EQ(apm_->kNoError,
1035 apm_->gain_control()->set_mode(
1036 apm_->gain_control()->mode()));
1037
1038 GainControl::Mode mode[] = {
1039 GainControl::kAdaptiveAnalog,
1040 GainControl::kAdaptiveDigital,
1041 GainControl::kFixedDigital
1042 };
pkasting25702cb2016-01-08 13:50:27 -08001043 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001044 EXPECT_EQ(apm_->kNoError,
1045 apm_->gain_control()->set_mode(mode[i]));
1046 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1047 }
1048 // Testing invalid target levels
1049 EXPECT_EQ(apm_->kBadParameterError,
1050 apm_->gain_control()->set_target_level_dbfs(-3));
1051 EXPECT_EQ(apm_->kBadParameterError,
1052 apm_->gain_control()->set_target_level_dbfs(-40));
1053 // Testing valid target levels
1054 EXPECT_EQ(apm_->kNoError,
1055 apm_->gain_control()->set_target_level_dbfs(
1056 apm_->gain_control()->target_level_dbfs()));
1057
1058 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001059 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 EXPECT_EQ(apm_->kNoError,
1061 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1062 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1063 }
1064
1065 // Testing invalid compression gains
1066 EXPECT_EQ(apm_->kBadParameterError,
1067 apm_->gain_control()->set_compression_gain_db(-1));
1068 EXPECT_EQ(apm_->kBadParameterError,
1069 apm_->gain_control()->set_compression_gain_db(100));
1070
1071 // Testing valid compression gains
1072 EXPECT_EQ(apm_->kNoError,
1073 apm_->gain_control()->set_compression_gain_db(
1074 apm_->gain_control()->compression_gain_db()));
1075
1076 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001077 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001078 EXPECT_EQ(apm_->kNoError,
1079 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1080 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1081 }
1082
1083 // Testing limiter off/on
1084 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1085 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1086 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1087 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1088
1089 // Testing invalid level limits
1090 EXPECT_EQ(apm_->kBadParameterError,
1091 apm_->gain_control()->set_analog_level_limits(-1, 512));
1092 EXPECT_EQ(apm_->kBadParameterError,
1093 apm_->gain_control()->set_analog_level_limits(100000, 512));
1094 EXPECT_EQ(apm_->kBadParameterError,
1095 apm_->gain_control()->set_analog_level_limits(512, -1));
1096 EXPECT_EQ(apm_->kBadParameterError,
1097 apm_->gain_control()->set_analog_level_limits(512, 100000));
1098 EXPECT_EQ(apm_->kBadParameterError,
1099 apm_->gain_control()->set_analog_level_limits(512, 255));
1100
1101 // Testing valid level limits
1102 EXPECT_EQ(apm_->kNoError,
1103 apm_->gain_control()->set_analog_level_limits(
1104 apm_->gain_control()->analog_level_minimum(),
1105 apm_->gain_control()->analog_level_maximum()));
1106
1107 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001108 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001109 EXPECT_EQ(apm_->kNoError,
1110 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1111 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1112 }
1113
1114 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001115 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001116 EXPECT_EQ(apm_->kNoError,
1117 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1118 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1119 }
1120
1121 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1122
1123 // Turn AGC off
1124 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1125 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1126}
1127
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001128void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001129 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001130 EXPECT_EQ(apm_->kNoError,
1131 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1132 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1133
1134 int out_analog_level = 0;
1135 for (int i = 0; i < 2000; ++i) {
1136 ReadFrameWithRewind(near_file_, frame_);
1137 // Ensure the audio is at a low level, so the AGC will try to increase it.
1138 ScaleFrame(frame_, 0.25);
1139
1140 // Always pass in the same volume.
1141 EXPECT_EQ(apm_->kNoError,
1142 apm_->gain_control()->set_stream_analog_level(100));
1143 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1144 out_analog_level = apm_->gain_control()->stream_analog_level();
1145 }
1146
1147 // Ensure the AGC is still able to reach the maximum.
1148 EXPECT_EQ(255, out_analog_level);
1149}
1150
1151// Verifies that despite volume slider quantization, the AGC can continue to
1152// increase its volume.
1153TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001154 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001155 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1156 }
1157}
1158
1159void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001160 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001161 EXPECT_EQ(apm_->kNoError,
1162 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1163 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1164
1165 int out_analog_level = 100;
1166 for (int i = 0; i < 1000; ++i) {
1167 ReadFrameWithRewind(near_file_, frame_);
1168 // Ensure the audio is at a low level, so the AGC will try to increase it.
1169 ScaleFrame(frame_, 0.25);
1170
1171 EXPECT_EQ(apm_->kNoError,
1172 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1173 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1174 out_analog_level = apm_->gain_control()->stream_analog_level();
1175 }
1176
1177 // Ensure the volume was raised.
1178 EXPECT_GT(out_analog_level, 100);
1179 int highest_level_reached = out_analog_level;
1180 // Simulate a user manual volume change.
1181 out_analog_level = 100;
1182
1183 for (int i = 0; i < 300; ++i) {
1184 ReadFrameWithRewind(near_file_, frame_);
1185 ScaleFrame(frame_, 0.25);
1186
1187 EXPECT_EQ(apm_->kNoError,
1188 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1189 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1190 out_analog_level = apm_->gain_control()->stream_analog_level();
1191 // Check that AGC respected the manually adjusted volume.
1192 EXPECT_LT(out_analog_level, highest_level_reached);
1193 }
1194 // Check that the volume was still raised.
1195 EXPECT_GT(out_analog_level, 100);
1196}
1197
1198TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001199 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001200 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1201 }
1202}
1203
niklase@google.com470e71d2011-07-07 08:21:25 +00001204TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001205 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001206 NoiseSuppression::Level level[] = {
1207 NoiseSuppression::kLow,
1208 NoiseSuppression::kModerate,
1209 NoiseSuppression::kHigh,
1210 NoiseSuppression::kVeryHigh
1211 };
pkasting25702cb2016-01-08 13:50:27 -08001212 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001213 EXPECT_EQ(apm_->kNoError,
1214 apm_->noise_suppression()->set_level(level[i]));
1215 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1216 }
1217
andrew@webrtc.org648af742012-02-08 01:57:29 +00001218 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001219 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1220 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1221 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1222 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1223}
1224
1225TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001226 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001227 AudioProcessing::Config apm_config;
1228 apm_config.high_pass_filter.enabled = true;
1229 apm_->ApplyConfig(apm_config);
1230 apm_config.high_pass_filter.enabled = false;
1231 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001232}
1233
1234TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001235 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001236 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001237 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001238
1239 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1240
1241 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1242 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1243
1244 // Run this test in wideband; in super-wb, the splitting filter distorts the
1245 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001246 frame_->samples_per_channel_ = 160;
1247 frame_->num_channels_ = 2;
1248 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001249
1250 // Min value if no frames have been processed.
1251 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1252
1253 // Min value on zero frames.
1254 SetFrameTo(frame_, 0);
1255 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1256 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1257 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1258
1259 // Try a few RMS values.
1260 // (These also test that the value resets after retrieving it.)
1261 SetFrameTo(frame_, 32767);
1262 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1263 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1264 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1265
1266 SetFrameTo(frame_, 30000);
1267 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1268 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1269 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1270
1271 SetFrameTo(frame_, 10000);
1272 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1273 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1274 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1275
1276 SetFrameTo(frame_, 10);
1277 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1278 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1279 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1280
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001281 // Verify reset after enable/disable.
1282 SetFrameTo(frame_, 32767);
1283 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1284 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1285 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1286 SetFrameTo(frame_, 1);
1287 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1288 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1289
1290 // Verify reset after initialize.
1291 SetFrameTo(frame_, 32767);
1292 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1293 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1294 SetFrameTo(frame_, 1);
1295 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1296 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001297}
1298
1299TEST_F(ApmTest, VoiceDetection) {
1300 // Test external VAD
1301 EXPECT_EQ(apm_->kNoError,
1302 apm_->voice_detection()->set_stream_has_voice(true));
1303 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1304 EXPECT_EQ(apm_->kNoError,
1305 apm_->voice_detection()->set_stream_has_voice(false));
1306 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1307
andrew@webrtc.org648af742012-02-08 01:57:29 +00001308 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 VoiceDetection::Likelihood likelihood[] = {
1310 VoiceDetection::kVeryLowLikelihood,
1311 VoiceDetection::kLowLikelihood,
1312 VoiceDetection::kModerateLikelihood,
1313 VoiceDetection::kHighLikelihood
1314 };
pkasting25702cb2016-01-08 13:50:27 -08001315 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001316 EXPECT_EQ(apm_->kNoError,
1317 apm_->voice_detection()->set_likelihood(likelihood[i]));
1318 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1319 }
1320
1321 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001322 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 EXPECT_EQ(apm_->kBadParameterError,
1324 apm_->voice_detection()->set_frame_size_ms(12));
1325
andrew@webrtc.org648af742012-02-08 01:57:29 +00001326 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001327 for (int i = 10; i <= 30; i += 10) {
1328 EXPECT_EQ(apm_->kNoError,
1329 apm_->voice_detection()->set_frame_size_ms(i));
1330 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1331 }
1332 */
1333
andrew@webrtc.org648af742012-02-08 01:57:29 +00001334 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001335 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1336 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1337 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1338 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1339
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001340 // Test that AudioFrame activity is maintained when VAD is disabled.
1341 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1342 AudioFrame::VADActivity activity[] = {
1343 AudioFrame::kVadActive,
1344 AudioFrame::kVadPassive,
1345 AudioFrame::kVadUnknown
1346 };
pkasting25702cb2016-01-08 13:50:27 -08001347 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001348 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001349 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001350 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001351 }
1352
1353 // Test that AudioFrame activity is set when VAD is enabled.
1354 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001355 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001356 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001357 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001358
niklase@google.com470e71d2011-07-07 08:21:25 +00001359 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1360}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001361
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001362TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrisson1a039602018-09-13 13:37:48 +02001363 AudioProcessing::Config config = apm_->GetConfig();
1364 EXPECT_FALSE(config.echo_canceller.enabled);
1365 EXPECT_FALSE(config.high_pass_filter.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001366 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1367 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1368 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1369 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1370 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1371}
1372
1373TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001374 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001375 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001376 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001377 AudioFrame frame_copy;
1378 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001379 for (int j = 0; j < 1000; j++) {
1380 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1381 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001382 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1383 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001384 }
1385 }
1386}
1387
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001388TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1389 // Test that ProcessStream copies input to output even with no processing.
1390 const size_t kSamples = 80;
1391 const int sample_rate = 8000;
1392 const float src[kSamples] = {
1393 -1.0f, 0.0f, 1.0f
1394 };
1395 float dest[kSamples] = {};
1396
1397 auto src_channels = &src[0];
1398 auto dest_channels = &dest[0];
1399
Ivo Creusen62337e52018-01-09 14:17:33 +01001400 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001401 EXPECT_NOERR(apm_->ProcessStream(
1402 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1403 sample_rate, LayoutFromChannels(1), &dest_channels));
1404
1405 for (size_t i = 0; i < kSamples; ++i) {
1406 EXPECT_EQ(src[i], dest[i]);
1407 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001408
1409 // Same for ProcessReverseStream.
1410 float rev_dest[kSamples] = {};
1411 auto rev_dest_channels = &rev_dest[0];
1412
1413 StreamConfig input_stream = {sample_rate, 1};
1414 StreamConfig output_stream = {sample_rate, 1};
1415 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1416 output_stream, &rev_dest_channels));
1417
1418 for (size_t i = 0; i < kSamples; ++i) {
1419 EXPECT_EQ(src[i], rev_dest[i]);
1420 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001421}
1422
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001423TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1424 EnableAllComponents();
1425
pkasting25702cb2016-01-08 13:50:27 -08001426 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001427 Init(kProcessSampleRates[i],
1428 kProcessSampleRates[i],
1429 kProcessSampleRates[i],
1430 2,
1431 2,
1432 2,
1433 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001434 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001435 ASSERT_EQ(0, feof(far_file_));
1436 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001437 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001438 CopyLeftToRightChannel(revframe_->mutable_data(),
1439 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001440
aluebsb0319552016-03-17 20:39:53 -07001441 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001442
yujo36b1a5f2017-06-12 12:45:32 -07001443 CopyLeftToRightChannel(frame_->mutable_data(),
1444 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001445 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1446
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001447 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001448 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001449 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001450 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001451 analog_level = apm_->gain_control()->stream_analog_level();
1452
yujo36b1a5f2017-06-12 12:45:32 -07001453 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001454 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001455 rewind(far_file_);
1456 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001457 }
1458}
1459
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001460TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001461 // Verify the filter is not active through undistorted audio when:
1462 // 1. No components are enabled...
1463 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001464 AudioFrame frame_copy;
1465 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001466 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1467 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1468 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1469
1470 // 2. Only the level estimator is enabled...
1471 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001472 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001473 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1474 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1475 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1476 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1477 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1478
1479 // 3. Only VAD is enabled...
1480 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001481 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001482 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1483 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1484 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1485 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1486 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1487
1488 // 4. Both VAD and the level estimator are enabled...
1489 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001490 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001491 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1492 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1493 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1494 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1495 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1496 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1497 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1498
1499 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001500 frame_->samples_per_channel_ = 160;
1501 frame_->num_channels_ = 2;
1502 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001503 // Enable AEC, which would require the filter in super-wb. We rely on the
1504 // first few frames of data being unaffected by the AEC.
1505 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1506 // behavior of the AEC. Think of something more robust.
Sam Zackrisson1a039602018-09-13 13:37:48 +02001507 AudioProcessing::Config apm_config = apm_->GetConfig();
1508 apm_config.echo_canceller.enabled = true;
1509 apm_config.echo_canceller.mobile_mode = false;
1510 apm_->ApplyConfig(apm_config);
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001511 // Make sure we have extended filter enabled. This makes sure nothing is
1512 // touched until we have a farend frame.
1513 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001514 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001515 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001516 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001517 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001518 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001519 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1520 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001521 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1522 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1523
1524 // Check the test is valid. We should have distortion from the filter
1525 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001526 frame_->samples_per_channel_ = 320;
1527 frame_->num_channels_ = 2;
1528 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001529 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001530 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001531 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001532 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1533 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1534}
1535
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001536#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1537void ApmTest::ProcessDebugDump(const std::string& in_filename,
1538 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001539 Format format,
1540 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001541 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001542 FILE* in_file = fopen(in_filename.c_str(), "rb");
1543 ASSERT_TRUE(in_file != NULL);
1544 audioproc::Event event_msg;
1545 bool first_init = true;
1546
1547 while (ReadMessageFromFile(in_file, &event_msg)) {
1548 if (event_msg.type() == audioproc::Event::INIT) {
1549 const audioproc::Init msg = event_msg.init();
1550 int reverse_sample_rate = msg.sample_rate();
1551 if (msg.has_reverse_sample_rate()) {
1552 reverse_sample_rate = msg.reverse_sample_rate();
1553 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001554 int output_sample_rate = msg.sample_rate();
1555 if (msg.has_output_sample_rate()) {
1556 output_sample_rate = msg.output_sample_rate();
1557 }
1558
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001559 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001560 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001561 reverse_sample_rate,
1562 msg.num_input_channels(),
1563 msg.num_output_channels(),
1564 msg.num_reverse_channels(),
1565 false);
1566 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001567 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001568 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001569 auto aec_dump =
1570 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1571 EXPECT_TRUE(aec_dump);
1572 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001573 first_init = false;
1574 }
1575
1576 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1577 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1578
1579 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001580 ASSERT_EQ(revframe_->num_channels_,
1581 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001582 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001583 memcpy(revfloat_cb_->channels()[i],
1584 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001585 msg.channel(i).size());
1586 }
1587 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001588 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001589 if (format == kFloatFormat) {
1590 // We're using an int16 input file; convert to float.
1591 ConvertToFloat(*revframe_, revfloat_cb_.get());
1592 }
1593 }
1594 AnalyzeReverseStreamChooser(format);
1595
1596 } else if (event_msg.type() == audioproc::Event::STREAM) {
1597 const audioproc::Stream msg = event_msg.stream();
1598 // ProcessStream could have changed this for the output frame.
1599 frame_->num_channels_ = apm_->num_input_channels();
1600
1601 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1602 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001603 if (msg.has_keypress()) {
1604 apm_->set_stream_key_pressed(msg.keypress());
1605 } else {
1606 apm_->set_stream_key_pressed(true);
1607 }
1608
1609 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001610 ASSERT_EQ(frame_->num_channels_,
1611 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001612 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001613 memcpy(float_cb_->channels()[i],
1614 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001615 msg.input_channel(i).size());
1616 }
1617 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001618 memcpy(frame_->mutable_data(), msg.input_data().data(),
1619 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001620 if (format == kFloatFormat) {
1621 // We're using an int16 input file; convert to float.
1622 ConvertToFloat(*frame_, float_cb_.get());
1623 }
1624 }
1625 ProcessStreamChooser(format);
1626 }
1627 }
aleloif4dd1912017-06-15 01:55:38 -07001628 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001629 fclose(in_file);
1630}
1631
1632void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001633 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001634 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001635 std::string format_string;
1636 switch (format) {
1637 case kIntFormat:
1638 format_string = "_int";
1639 break;
1640 case kFloatFormat:
1641 format_string = "_float";
1642 break;
1643 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001644 const std::string ref_filename = test::TempFilename(
1645 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1646 const std::string out_filename = test::TempFilename(
1647 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001648 const std::string limited_filename = test::TempFilename(
1649 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1650 const size_t logging_limit_bytes = 100000;
1651 // We expect at least this many bytes in the created logfile.
1652 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001653 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001654 ProcessDebugDump(in_filename, ref_filename, format, -1);
1655 ProcessDebugDump(ref_filename, out_filename, format, -1);
1656 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001657
1658 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1659 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001660 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001661 ASSERT_TRUE(ref_file != NULL);
1662 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001663 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001664 std::unique_ptr<uint8_t[]> ref_bytes;
1665 std::unique_ptr<uint8_t[]> out_bytes;
1666 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001667
1668 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1669 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001670 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001671 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001672 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001673 while (ref_size > 0 && out_size > 0) {
1674 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001675 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001676 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001677 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001678 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001679 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001680 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1681 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001682 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001683 }
1684 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001685 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1686 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001687 EXPECT_NE(0, feof(ref_file));
1688 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001689 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001690 ASSERT_EQ(0, fclose(ref_file));
1691 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001692 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001693 remove(ref_filename.c_str());
1694 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001695 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001696}
1697
pbosc7a65692016-05-06 12:50:04 -07001698TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001699 VerifyDebugDumpTest(kIntFormat);
1700}
1701
pbosc7a65692016-05-06 12:50:04 -07001702TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001703 VerifyDebugDumpTest(kFloatFormat);
1704}
1705#endif
1706
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001707// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001708TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001709 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001710 const std::string filename =
1711 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001712 {
1713 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1714 EXPECT_FALSE(aec_dump);
1715 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001716
1717#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1718 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001719 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001720
aleloif4dd1912017-06-15 01:55:38 -07001721 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1722 EXPECT_TRUE(aec_dump);
1723 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001724 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001725 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001726 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001727
1728 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001729 FILE* fid = fopen(filename.c_str(), "r");
1730 ASSERT_TRUE(fid != NULL);
1731
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001732 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001733 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001734 ASSERT_EQ(0, remove(filename.c_str()));
1735#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001736 // Verify the file has NOT been written.
1737 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1738#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1739}
1740
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001741// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001742TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001743 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1744
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001745 const std::string filename =
1746 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001747 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001748 ASSERT_TRUE(fid);
1749
1750#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1751 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001752 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001753
aleloif4dd1912017-06-15 01:55:38 -07001754 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1755 EXPECT_TRUE(aec_dump);
1756 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001757 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001758 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001759 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001760
1761 // Verify the file has been written.
1762 fid = fopen(filename.c_str(), "r");
1763 ASSERT_TRUE(fid != NULL);
1764
1765 // Clean it up.
1766 ASSERT_EQ(0, fclose(fid));
1767 ASSERT_EQ(0, remove(filename.c_str()));
1768#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001769 ASSERT_EQ(0, fclose(fid));
1770#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1771}
1772
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001773TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001774 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001775 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001776
1777 Config config;
1778 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001779 std::unique_ptr<AudioProcessing> fapm(
1780 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001781 EnableAllComponents();
1782 EnableAllAPComponents(fapm.get());
1783 for (int i = 0; i < ref_data.test_size(); i++) {
1784 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1785
1786 audioproc::Test* test = ref_data.mutable_test(i);
1787 // TODO(ajm): Restore downmixing test cases.
1788 if (test->num_input_channels() != test->num_output_channels())
1789 continue;
1790
Peter Kasting69558702016-01-12 16:26:35 -08001791 const size_t num_render_channels =
1792 static_cast<size_t>(test->num_reverse_channels());
1793 const size_t num_input_channels =
1794 static_cast<size_t>(test->num_input_channels());
1795 const size_t num_output_channels =
1796 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001797 const size_t samples_per_channel = static_cast<size_t>(
1798 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001799
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001800 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1801 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001802 Init(fapm.get());
1803
1804 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001805 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1806 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001807
1808 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001809 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001810 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1811 ReadFrame(near_file_, frame_, float_cb_.get())) {
1812 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1813
aluebsb0319552016-03-17 20:39:53 -07001814 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001815 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1816 revfloat_cb_->channels(),
1817 samples_per_channel,
1818 test->sample_rate(),
1819 LayoutFromChannels(num_render_channels)));
1820
1821 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1822 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001823 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1824 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1825
1826 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001827 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001828 output_int16.channels());
1829
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001830 EXPECT_NOERR(fapm->ProcessStream(
1831 float_cb_->channels(),
1832 samples_per_channel,
1833 test->sample_rate(),
1834 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001835 test->sample_rate(),
1836 LayoutFromChannels(num_output_channels),
1837 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001838 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001839 FloatToS16(float_cb_->channels()[j],
1840 samples_per_channel,
1841 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001842 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001843 float snr = ComputeSNR(output_int16.channels()[j],
1844 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001845 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001846
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001847 const float kVarianceThreshold = 20;
1848 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001849
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001850 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001851 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1852 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001853 }
1854 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001855
1856 analog_level = fapm->gain_control()->stream_analog_level();
1857 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1858 fapm->gain_control()->stream_analog_level());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001859 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1860 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001861 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001862
1863 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001864 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001865 }
aluebs776593b2016-03-15 14:04:58 -07001866
1867#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1868 const size_t kMaxNumBadChunks = 0;
1869#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1870 // There are a few chunks in the fixed-point profile that give low SNR.
1871 // Listening confirmed the difference is acceptable.
1872 const size_t kMaxNumBadChunks = 60;
1873#endif
1874 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1875
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001876 rewind(far_file_);
1877 rewind(near_file_);
1878 }
1879}
1880
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001881// TODO(andrew): Add a test to process a few frames with different combinations
1882// of enabled components.
1883
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001884TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001885 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001886 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001887
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001888 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001889 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001890 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001891 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001892 for (size_t i = 0; i < arraysize(kChannels); i++) {
1893 for (size_t j = 0; j < arraysize(kChannels); j++) {
1894 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001895 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001896 test->set_num_reverse_channels(kChannels[i]);
1897 test->set_num_input_channels(kChannels[j]);
1898 test->set_num_output_channels(kChannels[j]);
1899 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001900 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001901 }
1902 }
1903 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001904#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1905 // To test the extended filter mode.
1906 audioproc::Test* test = ref_data.add_test();
1907 test->set_num_reverse_channels(2);
1908 test->set_num_input_channels(2);
1909 test->set_num_output_channels(2);
1910 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1911 test->set_use_aec_extended_filter(true);
1912#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001913 }
1914
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001915 for (int i = 0; i < ref_data.test_size(); i++) {
1916 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001917
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001918 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001919 // TODO(ajm): We no longer allow different input and output channels. Skip
1920 // these tests for now, but they should be removed from the set.
1921 if (test->num_input_channels() != test->num_output_channels())
1922 continue;
1923
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001924 Config config;
1925 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001926 config.Set<ExtendedFilter>(
1927 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001928 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001929
1930 EnableAllComponents();
1931
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001932 Init(test->sample_rate(),
1933 test->sample_rate(),
1934 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001935 static_cast<size_t>(test->num_input_channels()),
1936 static_cast<size_t>(test->num_output_channels()),
1937 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001938 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001939
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001940 int frame_count = 0;
1941 int has_echo_count = 0;
1942 int has_voice_count = 0;
1943 int is_saturated_count = 0;
1944 int analog_level = 127;
1945 int analog_level_average = 0;
1946 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001947 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001948#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1949 int stats_index = 0;
1950#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001951
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001952 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001953 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001954
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001955 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1956
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001957 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001958 EXPECT_EQ(apm_->kNoError,
1959 apm_->gain_control()->set_stream_analog_level(analog_level));
1960
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001961 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001962
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001963 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001964 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1965 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001966
1967 max_output_average += MaxAudioFrame(*frame_);
1968
1969 if (apm_->echo_cancellation()->stream_has_echo()) {
1970 has_echo_count++;
1971 }
1972
1973 analog_level = apm_->gain_control()->stream_analog_level();
1974 analog_level_average += analog_level;
1975 if (apm_->gain_control()->stream_is_saturated()) {
1976 is_saturated_count++;
1977 }
1978 if (apm_->voice_detection()->stream_has_voice()) {
1979 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001980 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001981 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001982 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001983 }
1984
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001985 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
1986
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001987 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001988 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001989 sizeof(int16_t),
1990 frame_size,
1991 out_file_);
1992 ASSERT_EQ(frame_size, write_count);
1993
1994 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001995 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001996 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001997
1998#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1999 const int kStatsAggregationFrameNum = 100; // 1 second.
2000 if (frame_count % kStatsAggregationFrameNum == 0) {
2001 // Get echo metrics.
2002 EchoCancellation::Metrics echo_metrics;
2003 EXPECT_EQ(apm_->kNoError,
2004 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2005
2006 // Get delay metrics.
2007 int median = 0;
2008 int std = 0;
2009 float fraction_poor_delays = 0;
2010 EXPECT_EQ(apm_->kNoError,
2011 apm_->echo_cancellation()->GetDelayMetrics(
2012 &median, &std, &fraction_poor_delays));
2013
2014 // Get RMS.
2015 int rms_level = apm_->level_estimator()->RMS();
2016 EXPECT_LE(0, rms_level);
2017 EXPECT_GE(127, rms_level);
2018
2019 if (!write_ref_data) {
2020 const audioproc::Test::EchoMetrics& reference =
2021 test->echo_metrics(stats_index);
2022 TestStats(echo_metrics.residual_echo_return_loss,
2023 reference.residual_echo_return_loss());
2024 TestStats(echo_metrics.echo_return_loss,
2025 reference.echo_return_loss());
2026 TestStats(echo_metrics.echo_return_loss_enhancement,
2027 reference.echo_return_loss_enhancement());
2028 TestStats(echo_metrics.a_nlp,
2029 reference.a_nlp());
2030 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2031 reference.divergent_filter_fraction());
2032
2033 const audioproc::Test::DelayMetrics& reference_delay =
2034 test->delay_metrics(stats_index);
2035 EXPECT_EQ(reference_delay.median(), median);
2036 EXPECT_EQ(reference_delay.std(), std);
2037 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2038 fraction_poor_delays);
2039
2040 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2041
2042 ++stats_index;
2043 } else {
2044 audioproc::Test::EchoMetrics* message =
2045 test->add_echo_metrics();
2046 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2047 message->mutable_residual_echo_return_loss());
2048 WriteStatsMessage(echo_metrics.echo_return_loss,
2049 message->mutable_echo_return_loss());
2050 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2051 message->mutable_echo_return_loss_enhancement());
2052 WriteStatsMessage(echo_metrics.a_nlp,
2053 message->mutable_a_nlp());
2054 message->set_divergent_filter_fraction(
2055 echo_metrics.divergent_filter_fraction);
2056
2057 audioproc::Test::DelayMetrics* message_delay =
2058 test->add_delay_metrics();
2059 message_delay->set_median(median);
2060 message_delay->set_std(std);
2061 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2062
2063 test->add_rms_level(rms_level);
2064 }
2065 }
2066#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002067 }
2068 max_output_average /= frame_count;
2069 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002070 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002071
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002072 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002073 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002074 // When running the test on a N7 we get a {2, 6} difference of
2075 // |has_voice_count| and |max_output_average| is up to 18 higher.
2076 // All numbers being consistently higher on N7 compare to ref_data.
2077 // TODO(bjornv): If we start getting more of these offsets on Android we
2078 // should consider a different approach. Either using one slack for all,
2079 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02002080#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002081 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002082 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002083 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002084 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002085#else
2086 const int kHasVoiceCountOffset = 0;
2087 const int kHasVoiceCountNear = kIntNear;
2088 const int kMaxOutputAverageOffset = 0;
2089 const int kMaxOutputAverageNear = kIntNear;
2090#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002091 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002092 EXPECT_NEAR(test->has_voice_count(),
2093 has_voice_count - kHasVoiceCountOffset,
2094 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002095 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002096
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002097 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002098 EXPECT_NEAR(test->max_output_average(),
2099 max_output_average - kMaxOutputAverageOffset,
2100 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002101#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002102 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002103 EXPECT_NEAR(test->ns_speech_probability_average(),
2104 ns_speech_prob_average,
2105 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002106#endif
2107 } else {
2108 test->set_has_echo_count(has_echo_count);
2109 test->set_has_voice_count(has_voice_count);
2110 test->set_is_saturated_count(is_saturated_count);
2111
2112 test->set_analog_level_average(analog_level_average);
2113 test->set_max_output_average(max_output_average);
2114
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002115#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002116 EXPECT_LE(0.0f, ns_speech_prob_average);
2117 EXPECT_GE(1.0f, ns_speech_prob_average);
2118 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002119#endif
2120 }
2121
2122 rewind(far_file_);
2123 rewind(near_file_);
2124 }
2125
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002126 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002127 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002128 }
2129}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002130
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002131TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2132 struct ChannelFormat {
2133 AudioProcessing::ChannelLayout in_layout;
2134 AudioProcessing::ChannelLayout out_layout;
2135 };
2136 ChannelFormat cf[] = {
2137 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2138 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2139 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2140 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002141
Ivo Creusen62337e52018-01-09 14:17:33 +01002142 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002143 // Enable one component just to ensure some processing takes place.
2144 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002145 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002146 const int in_rate = 44100;
2147 const int out_rate = 48000;
2148 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2149 TotalChannelsFromLayout(cf[i].in_layout));
2150 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2151 ChannelsFromLayout(cf[i].out_layout));
2152
2153 // Run over a few chunks.
2154 for (int j = 0; j < 10; ++j) {
2155 EXPECT_NOERR(ap->ProcessStream(
2156 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002157 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002158 in_rate,
2159 cf[i].in_layout,
2160 out_rate,
2161 cf[i].out_layout,
2162 out_cb.channels()));
2163 }
2164 }
2165}
2166
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002167// Compares the reference and test arrays over a region around the expected
2168// delay. Finds the highest SNR in that region and adds the variance and squared
2169// error results to the supplied accumulators.
2170void UpdateBestSNR(const float* ref,
2171 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002172 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002173 int expected_delay,
2174 double* variance_acc,
2175 double* sq_error_acc) {
2176 double best_snr = std::numeric_limits<double>::min();
2177 double best_variance = 0;
2178 double best_sq_error = 0;
2179 // Search over a region of eight samples around the expected delay.
2180 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2181 ++delay) {
2182 double sq_error = 0;
2183 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002184 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002185 double error = test[i + delay] - ref[i];
2186 sq_error += error * error;
2187 variance += ref[i] * ref[i];
2188 }
2189
2190 if (sq_error == 0) {
2191 *variance_acc += variance;
2192 return;
2193 }
2194 double snr = variance / sq_error;
2195 if (snr > best_snr) {
2196 best_snr = snr;
2197 best_variance = variance;
2198 best_sq_error = sq_error;
2199 }
2200 }
2201
2202 *variance_acc += best_variance;
2203 *sq_error_acc += best_sq_error;
2204}
2205
2206// Used to test a multitude of sample rate and channel combinations. It works
2207// by first producing a set of reference files (in SetUpTestCase) that are
2208// assumed to be correct, as the used parameters are verified by other tests
2209// in this collection. Primarily the reference files are all produced at
2210// "native" rates which do not involve any resampling.
2211
2212// Each test pass produces an output file with a particular format. The output
2213// is matched against the reference file closest to its internal processing
2214// format. If necessary the output is resampled back to its process format.
2215// Due to the resampling distortion, we don't expect identical results, but
2216// enforce SNR thresholds which vary depending on the format. 0 is a special
2217// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002218typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002219class AudioProcessingTest
2220 : public testing::TestWithParam<AudioProcessingTestData> {
2221 public:
2222 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002223 : input_rate_(std::get<0>(GetParam())),
2224 output_rate_(std::get<1>(GetParam())),
2225 reverse_input_rate_(std::get<2>(GetParam())),
2226 reverse_output_rate_(std::get<3>(GetParam())),
2227 expected_snr_(std::get<4>(GetParam())),
2228 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002229
2230 virtual ~AudioProcessingTest() {}
2231
2232 static void SetUpTestCase() {
2233 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002234 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002235 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002236 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2237 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2238 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002239 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002240 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2241 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2242 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002243 }
2244 }
2245 }
2246 }
2247
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002248 void TearDown() {
2249 // Remove "out" files after each test.
2250 ClearTempOutFiles();
2251 }
2252
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002253 static void TearDownTestCase() {
2254 ClearTempFiles();
2255 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002256
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002257 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002258 // to a file specified with |output_file_prefix|. Both forward and reverse
2259 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002260 static void ProcessFormat(int input_rate,
2261 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002262 int reverse_input_rate,
2263 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002264 size_t num_input_channels,
2265 size_t num_output_channels,
2266 size_t num_reverse_input_channels,
2267 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002268 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002269 Config config;
2270 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002271 std::unique_ptr<AudioProcessing> ap(
2272 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002273 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002274
ekmeyerson60d9b332015-08-14 10:35:55 -07002275 ProcessingConfig processing_config = {
2276 {{input_rate, num_input_channels},
2277 {output_rate, num_output_channels},
2278 {reverse_input_rate, num_reverse_input_channels},
2279 {reverse_output_rate, num_reverse_output_channels}}};
2280 ap->Initialize(processing_config);
2281
2282 FILE* far_file =
2283 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002284 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002285 FILE* out_file =
2286 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2287 reverse_input_rate, reverse_output_rate,
2288 num_input_channels, num_output_channels,
2289 num_reverse_input_channels,
2290 num_reverse_output_channels, kForward).c_str(),
2291 "wb");
2292 FILE* rev_out_file =
2293 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2294 reverse_input_rate, reverse_output_rate,
2295 num_input_channels, num_output_channels,
2296 num_reverse_input_channels,
2297 num_reverse_output_channels, kReverse).c_str(),
2298 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002299 ASSERT_TRUE(far_file != NULL);
2300 ASSERT_TRUE(near_file != NULL);
2301 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002302 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002303
2304 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2305 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002306 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2307 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002308 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2309 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002310 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2311 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002312
2313 // Temporary buffers.
2314 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002315 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2316 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002317 std::unique_ptr<float[]> float_data(new float[max_length]);
2318 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002319
2320 int analog_level = 127;
2321 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2322 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002323 EXPECT_NOERR(ap->ProcessReverseStream(
2324 rev_cb.channels(), processing_config.reverse_input_stream(),
2325 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002326
2327 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002328 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2329
2330 EXPECT_NOERR(ap->ProcessStream(
2331 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002332 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002333 input_rate,
2334 LayoutFromChannels(num_input_channels),
2335 output_rate,
2336 LayoutFromChannels(num_output_channels),
2337 out_cb.channels()));
2338
ekmeyerson60d9b332015-08-14 10:35:55 -07002339 // Dump forward output to file.
2340 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002341 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002342 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002343
pkasting25702cb2016-01-08 13:50:27 -08002344 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002345 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002346 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002347
ekmeyerson60d9b332015-08-14 10:35:55 -07002348 // Dump reverse output to file.
2349 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2350 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002351 size_t rev_out_length =
2352 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002353
pkasting25702cb2016-01-08 13:50:27 -08002354 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002355 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2356 rev_out_file));
2357
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002358 analog_level = ap->gain_control()->stream_analog_level();
2359 }
2360 fclose(far_file);
2361 fclose(near_file);
2362 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002363 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002364 }
2365
2366 protected:
2367 int input_rate_;
2368 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002369 int reverse_input_rate_;
2370 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002371 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002372 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002373};
2374
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002375TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002376 struct ChannelFormat {
2377 int num_input;
2378 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002379 int num_reverse_input;
2380 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002381 };
2382 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002383 {1, 1, 1, 1},
2384 {1, 1, 2, 1},
2385 {2, 1, 1, 1},
2386 {2, 1, 2, 1},
2387 {2, 2, 1, 1},
2388 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002389 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002390
pkasting25702cb2016-01-08 13:50:27 -08002391 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002392 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2393 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2394 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002395
ekmeyerson60d9b332015-08-14 10:35:55 -07002396 // Verify output for both directions.
2397 std::vector<StreamDirection> stream_directions;
2398 stream_directions.push_back(kForward);
2399 stream_directions.push_back(kReverse);
2400 for (StreamDirection file_direction : stream_directions) {
2401 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2402 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2403 const int out_num =
2404 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2405 const double expected_snr =
2406 file_direction ? expected_reverse_snr_ : expected_snr_;
2407
2408 const int min_ref_rate = std::min(in_rate, out_rate);
2409 int ref_rate;
2410
2411 if (min_ref_rate > 32000) {
2412 ref_rate = 48000;
2413 } else if (min_ref_rate > 16000) {
2414 ref_rate = 32000;
2415 } else if (min_ref_rate > 8000) {
2416 ref_rate = 16000;
2417 } else {
2418 ref_rate = 8000;
2419 }
aluebs776593b2016-03-15 14:04:58 -07002420#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002421 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002422 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002423 }
2424#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002425 FILE* out_file = fopen(
2426 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2427 reverse_output_rate_, cf[i].num_input,
2428 cf[i].num_output, cf[i].num_reverse_input,
2429 cf[i].num_reverse_output, file_direction).c_str(),
2430 "rb");
2431 // The reference files always have matching input and output channels.
2432 FILE* ref_file = fopen(
2433 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2434 cf[i].num_output, cf[i].num_output,
2435 cf[i].num_reverse_output, cf[i].num_reverse_output,
2436 file_direction).c_str(),
2437 "rb");
2438 ASSERT_TRUE(out_file != NULL);
2439 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002440
pkasting25702cb2016-01-08 13:50:27 -08002441 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2442 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002443 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002444 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002445 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002446 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002447 // Data from the resampled output, in case the reference and output rates
2448 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002449 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002450
ekmeyerson60d9b332015-08-14 10:35:55 -07002451 PushResampler<float> resampler;
2452 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002453
ekmeyerson60d9b332015-08-14 10:35:55 -07002454 // Compute the resampling delay of the output relative to the reference,
2455 // to find the region over which we should search for the best SNR.
2456 float expected_delay_sec = 0;
2457 if (in_rate != ref_rate) {
2458 // Input resampling delay.
2459 expected_delay_sec +=
2460 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2461 }
2462 if (out_rate != ref_rate) {
2463 // Output resampling delay.
2464 expected_delay_sec +=
2465 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2466 // Delay of converting the output back to its processing rate for
2467 // testing.
2468 expected_delay_sec +=
2469 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2470 }
2471 int expected_delay =
2472 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002473
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 double variance = 0;
2475 double sq_error = 0;
2476 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2477 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2478 float* out_ptr = out_data.get();
2479 if (out_rate != ref_rate) {
2480 // Resample the output back to its internal processing rate if
2481 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002482 ASSERT_EQ(ref_length,
2483 static_cast<size_t>(resampler.Resample(
2484 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002485 out_ptr = cmp_data.get();
2486 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002487
ekmeyerson60d9b332015-08-14 10:35:55 -07002488 // Update the |sq_error| and |variance| accumulators with the highest
2489 // SNR of reference vs output.
2490 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2491 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002492 }
2493
ekmeyerson60d9b332015-08-14 10:35:55 -07002494 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2495 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2496 << cf[i].num_input << ", " << cf[i].num_output << ", "
2497 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2498 << ", " << file_direction << "): ";
2499 if (sq_error > 0) {
2500 double snr = 10 * log10(variance / sq_error);
2501 EXPECT_GE(snr, expected_snr);
2502 EXPECT_NE(0, expected_snr);
2503 std::cout << "SNR=" << snr << " dB" << std::endl;
2504 } else {
aluebs776593b2016-03-15 14:04:58 -07002505 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002506 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002507
ekmeyerson60d9b332015-08-14 10:35:55 -07002508 fclose(out_file);
2509 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002510 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002511 }
2512}
2513
2514#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2515INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002516 CommonFormats,
2517 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002518 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2519 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2520 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2521 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2522 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2523 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2524 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2525 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2526 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2527 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2528 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2529 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002530
Edward Lemurc5ee9872017-10-23 23:33:04 +02002531 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2532 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2533 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2534 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2535 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2536 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2537 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2538 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2539 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2540 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2541 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2542 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002543
Edward Lemurc5ee9872017-10-23 23:33:04 +02002544 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2545 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2546 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2547 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2548 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2549 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2550 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2551 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2552 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2553 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2554 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2555 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002556
Edward Lemurc5ee9872017-10-23 23:33:04 +02002557 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2558 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2559 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2560 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2561 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2562 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2563 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2564 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2565 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2566 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2567 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2568 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002569
2570#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2571INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002572 CommonFormats,
2573 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002574 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2575 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2576 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2577 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2578 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2579 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2580 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2581 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2582 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2583 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2584 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2585 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002586
Edward Lemurc5ee9872017-10-23 23:33:04 +02002587 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2588 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2589 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2590 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2591 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2592 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2593 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2594 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2595 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2596 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2597 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2598 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002599
Edward Lemurc5ee9872017-10-23 23:33:04 +02002600 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2601 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2602 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2603 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2604 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2605 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2606 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2607 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2608 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2609 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2610 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2611 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002612
Edward Lemurc5ee9872017-10-23 23:33:04 +02002613 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2614 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2615 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2616 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2617 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2618 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2619 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2620 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2621 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2622 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2623 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2624 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002625#endif
2626
niklase@google.com470e71d2011-07-07 08:21:25 +00002627} // namespace
peahc19f3122016-10-07 14:54:10 -07002628
Alessio Bazzicac054e782018-04-16 12:10:09 +02002629TEST(RuntimeSettingTest, TestDefaultCtor) {
2630 auto s = AudioProcessing::RuntimeSetting();
2631 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2632}
2633
2634TEST(RuntimeSettingTest, TestCapturePreGain) {
2635 using Type = AudioProcessing::RuntimeSetting::Type;
2636 {
2637 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2638 EXPECT_EQ(Type::kCapturePreGain, s.type());
2639 float v;
2640 s.GetFloat(&v);
2641 EXPECT_EQ(1.25f, v);
2642 }
2643
2644#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2645 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2646#endif
2647}
2648
2649TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2650 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2651 auto s = AudioProcessing::RuntimeSetting();
2652 ASSERT_TRUE(q.Insert(&s));
2653 ASSERT_TRUE(q.Remove(&s));
2654 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2655}
2656
Sam Zackrisson0beac582017-09-25 12:04:02 +02002657TEST(ApmConfiguration, EnablePostProcessing) {
2658 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002659 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002660 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002661 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002662 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002663 rtc::scoped_refptr<AudioProcessing> apm =
2664 AudioProcessingBuilder()
2665 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002666 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002667
2668 AudioFrame audio;
2669 audio.num_channels_ = 1;
2670 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2671
2672 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002673 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002674}
2675
Alex Loiko5825aa62017-12-18 16:02:40 +01002676TEST(ApmConfiguration, EnablePreProcessing) {
2677 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002678 auto mock_pre_processor_ptr =
2679 new testing::NiceMock<test::MockCustomProcessing>();
2680 auto mock_pre_processor =
2681 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002682 rtc::scoped_refptr<AudioProcessing> apm =
2683 AudioProcessingBuilder()
2684 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002685 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002686
2687 AudioFrame audio;
2688 audio.num_channels_ = 1;
2689 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2690
2691 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2692 apm->ProcessReverseStream(&audio);
2693}
2694
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002695TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2696 // Verify that apm uses a capture analyzer if one is provided.
2697 auto mock_capture_analyzer_ptr =
2698 new testing::NiceMock<test::MockCustomAudioAnalyzer>();
2699 auto mock_capture_analyzer =
2700 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2701 rtc::scoped_refptr<AudioProcessing> apm =
2702 AudioProcessingBuilder()
2703 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2704 .Create();
2705
2706 AudioFrame audio;
2707 audio.num_channels_ = 1;
2708 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2709
2710 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(testing::_)).Times(1);
2711 apm->ProcessStream(&audio);
2712}
2713
Alex Loiko73ec0192018-05-15 10:52:28 +02002714TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2715 auto mock_pre_processor_ptr =
2716 new testing::NiceMock<test::MockCustomProcessing>();
2717 auto mock_pre_processor =
2718 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2719 rtc::scoped_refptr<AudioProcessing> apm =
2720 AudioProcessingBuilder()
2721 .SetRenderPreProcessing(std::move(mock_pre_processor))
2722 .Create();
2723 apm->SetRuntimeSetting(
2724 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2725
2726 // RuntimeSettings forwarded during 'Process*Stream' calls.
2727 // Therefore we have to make one such call.
2728 AudioFrame audio;
2729 audio.num_channels_ = 1;
2730 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2731
2732 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2733 apm->ProcessReverseStream(&audio);
2734}
2735
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002736class MyEchoControlFactory : public EchoControlFactory {
2737 public:
2738 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2739 auto ec = new test::MockEchoControl();
2740 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2741 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2742 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2743 return std::unique_ptr<EchoControl>(ec);
2744 }
2745};
2746
2747TEST(ApmConfiguration, EchoControlInjection) {
2748 // Verify that apm uses an injected echo controller if one is provided.
2749 webrtc::Config webrtc_config;
2750 std::unique_ptr<EchoControlFactory> echo_control_factory(
2751 new MyEchoControlFactory());
2752
Alex Loiko5825aa62017-12-18 16:02:40 +01002753 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002754 AudioProcessingBuilder()
2755 .SetEchoControlFactory(std::move(echo_control_factory))
2756 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002757
2758 AudioFrame audio;
2759 audio.num_channels_ = 1;
2760 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2761 apm->ProcessStream(&audio);
2762 apm->ProcessReverseStream(&audio);
2763 apm->ProcessStream(&audio);
2764}
Ivo Creusenae026092017-11-20 13:07:16 +01002765
2766std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2767 Config old_config;
2768 if (use_AEC2) {
2769 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2770 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2771 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002772 std::unique_ptr<AudioProcessing> apm(
2773 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002774 if (!apm) {
2775 return apm;
2776 }
2777
2778 ProcessingConfig processing_config = {
2779 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2780
2781 if (apm->Initialize(processing_config) != 0) {
2782 return nullptr;
2783 }
2784
2785 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrisson1a039602018-09-13 13:37:48 +02002786 AudioProcessing::Config apm_config;
2787 apm_config.residual_echo_detector.enabled = true;
2788 apm_config.high_pass_filter.enabled = false;
2789 apm_config.gain_controller2.enabled = false;
2790 apm_config.echo_canceller.enabled = true;
2791 apm_config.echo_canceller.mobile_mode = !use_AEC2;
2792 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002793 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2794 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2795 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2796 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002797 return apm;
2798}
2799
2800#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2801#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2802#else
2803#define MAYBE_ApmStatistics ApmStatistics
2804#endif
2805
2806TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2807 // Set up APM with AEC2 and process some audio.
2808 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2809 ASSERT_TRUE(apm);
2810
2811 // Set up an audioframe.
2812 AudioFrame frame;
2813 frame.num_channels_ = 1;
2814 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2815
2816 // Fill the audio frame with a sawtooth pattern.
2817 int16_t* ptr = frame.mutable_data();
2818 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2819 ptr[i] = 10000 * ((i % 3) - 1);
2820 }
2821
2822 // Do some processing.
2823 for (int i = 0; i < 200; i++) {
2824 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2825 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2826 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2827 }
2828
2829 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002830 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002831 // We expect all statistics to be set and have a sensible value.
2832 ASSERT_TRUE(stats.residual_echo_likelihood);
2833 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2834 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2835 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2836 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2837 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2838 ASSERT_TRUE(stats.echo_return_loss);
2839 EXPECT_NE(*stats.echo_return_loss, -100.0);
2840 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2841 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2842 ASSERT_TRUE(stats.divergent_filter_fraction);
2843 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2844 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2845 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2846 // We don't check stats.delay_median_ms since it takes too long to settle to a
2847 // value. At least 20 seconds of data need to be processed before it will get
2848 // a value, which would make this test take too much time.
2849
2850 // If there are no receive streams, we expect the stats not to be set. The
2851 // 'false' argument signals to APM that no receive streams are currently
2852 // active. In that situation the statistics would get stuck at their last
2853 // calculated value (AEC and echo detection need at least one stream in each
2854 // direction), so to avoid that, they should not be set by APM.
2855 stats = apm->GetStatistics(false);
2856 EXPECT_FALSE(stats.residual_echo_likelihood);
2857 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2858 EXPECT_FALSE(stats.echo_return_loss);
2859 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2860 EXPECT_FALSE(stats.divergent_filter_fraction);
2861 EXPECT_FALSE(stats.delay_median_ms);
2862 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2863}
2864
2865TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2866 // Set up APM with AECM and process some audio.
2867 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2868 ASSERT_TRUE(apm);
2869
2870 // Set up an audioframe.
2871 AudioFrame frame;
2872 frame.num_channels_ = 1;
2873 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2874
2875 // Fill the audio frame with a sawtooth pattern.
2876 int16_t* ptr = frame.mutable_data();
2877 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2878 ptr[i] = 10000 * ((i % 3) - 1);
2879 }
2880
2881 // Do some processing.
2882 for (int i = 0; i < 200; i++) {
2883 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2884 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2885 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2886 }
2887
2888 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002889 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002890 // We expect only the residual echo detector statistics to be set and have a
2891 // sensible value.
2892 EXPECT_TRUE(stats.residual_echo_likelihood);
2893 if (stats.residual_echo_likelihood) {
2894 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2895 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2896 }
2897 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2898 if (stats.residual_echo_likelihood_recent_max) {
2899 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2900 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2901 }
2902 EXPECT_FALSE(stats.echo_return_loss);
2903 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2904 EXPECT_FALSE(stats.divergent_filter_fraction);
2905 EXPECT_FALSE(stats.delay_median_ms);
2906 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2907
2908 // If there are no receive streams, we expect the stats not to be set.
2909 stats = apm->GetStatistics(false);
2910 EXPECT_FALSE(stats.residual_echo_likelihood);
2911 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2912 EXPECT_FALSE(stats.echo_return_loss);
2913 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2914 EXPECT_FALSE(stats.divergent_filter_fraction);
2915 EXPECT_FALSE(stats.delay_median_ms);
2916 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2917}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002918} // namespace webrtc