blob: d22669a745ffcf0ffbbc96ae53bec812c41a1165 [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 Zackrissoncdf0e6d2018-09-17 11:05:17 +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
Alex Loiko890988c2017-08-31 10:25:48 +0200224void OpenFileAndWriteMessage(const std::string& filename,
mbonadei7c2c8432017-04-07 00:59:12 -0700225 const MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000226 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000227 ASSERT_TRUE(file != NULL);
228
Mirko Bonadei5b86f0a2017-11-29 15:20:26 +0100229 int32_t size = rtc::checked_cast<int32_t>(msg.ByteSizeLong());
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800231 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000232 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000233
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237 fclose(file);
238}
239
Alex Loiko890988c2017-08-31 10:25:48 +0200240std::string ResourceFilePath(const std::string& name, int sample_rate_hz) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200241 rtc::StringBuilder ss;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000242 // Resource files are all stereo.
243 ss << name << sample_rate_hz / 1000 << "_stereo";
244 return test::ResourcePath(ss.str(), "pcm");
245}
246
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000247// Temporary filenames unique to this process. Used to be able to run these
248// tests in parallel as each process needs to be running in isolation they can't
249// have competing filenames.
250std::map<std::string, std::string> temp_filenames;
251
Alex Loiko890988c2017-08-31 10:25:48 +0200252std::string OutputFilePath(const std::string& name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000253 int input_rate,
254 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700255 int reverse_input_rate,
256 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800257 size_t num_input_channels,
258 size_t num_output_channels,
259 size_t num_reverse_input_channels,
260 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700261 StreamDirection file_direction) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200262 rtc::StringBuilder ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700263 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
264 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000265 if (num_output_channels == 1) {
266 ss << "mono";
267 } else if (num_output_channels == 2) {
268 ss << "stereo";
269 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700270 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000271 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700272 ss << output_rate / 1000;
273 if (num_reverse_output_channels == 1) {
274 ss << "_rmono";
275 } else if (num_reverse_output_channels == 2) {
276 ss << "_rstereo";
277 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700278 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700279 }
280 ss << reverse_output_rate / 1000;
281 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000283 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700284 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000285 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
286 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000287}
288
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000289void ClearTempFiles() {
290 for (auto& kv : temp_filenames)
291 remove(kv.second.c_str());
292}
293
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +0200294// Only remove "out" files. Keep "ref" files.
295void ClearTempOutFiles() {
296 for (auto it = temp_filenames.begin(); it != temp_filenames.end();) {
297 const std::string& filename = it->first;
298 if (filename.substr(0, 3).compare("out") == 0) {
299 remove(it->second.c_str());
300 temp_filenames.erase(it++);
301 } else {
302 it++;
303 }
304 }
305}
306
Alex Loiko890988c2017-08-31 10:25:48 +0200307void OpenFileAndReadMessage(const std::string& filename, MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000308 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000309 ASSERT_TRUE(file != NULL);
310 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000311 fclose(file);
312}
313
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000314// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
315// stereo) file, converts to deinterleaved float (optionally downmixing) and
316// returns the result in |cb|. Returns false if the file ended (or on error) and
317// true otherwise.
318//
319// |int_data| and |float_data| are just temporary space that must be
320// sufficiently large to hold the 10 ms chunk.
321bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
322 ChannelBuffer<float>* cb) {
323 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000324 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000325 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
326 if (read_count != frame_size) {
327 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700328 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000329 return false; // This is expected.
330 }
331
332 S16ToFloat(int_data, frame_size, float_data);
333 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000334 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000335 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000336 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000337 cb->channels());
338 }
339
340 return true;
341}
342
niklase@google.com470e71d2011-07-07 08:21:25 +0000343class ApmTest : public ::testing::Test {
344 protected:
345 ApmTest();
346 virtual void SetUp();
347 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000348
349 static void SetUpTestCase() {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000350 }
351
352 static void TearDownTestCase() {
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000353 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000354 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000355
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000356 // Used to select between int and float interface tests.
357 enum Format {
358 kIntFormat,
359 kFloatFormat
360 };
361
362 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000363 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000364 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800365 size_t num_input_channels,
366 size_t num_output_channels,
367 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000368 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000369 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000370 void EnableAllComponents();
371 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000372 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000373 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000374 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
375 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000376 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000377 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
378 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700379 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800380 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700381 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800382 void TestChangingForwardChannels(size_t num_in_channels,
383 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700384 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800385 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700386 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000387 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
388 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000390 int ProcessStreamChooser(Format format);
391 int AnalyzeReverseStreamChooser(Format format);
392 void ProcessDebugDump(const std::string& in_filename,
393 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800394 Format format,
395 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000397
398 const std::string output_path_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000399 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800400 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000401 AudioFrame* frame_;
402 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800403 std::unique_ptr<ChannelBuffer<float> > float_cb_;
404 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000405 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800406 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407 FILE* far_file_;
408 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000409 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410};
411
412ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000413 : output_path_(test::OutputPath()),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000414#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800415 ref_filename_(test::ResourcePath("audio_processing/output_data_fixed",
416 "pb")),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000417#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000418#if defined(WEBRTC_MAC)
419 // A different file for Mac is needed because on this platform the AEC
420 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800421 ref_filename_(test::ResourcePath("audio_processing/output_data_mac",
422 "pb")),
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000423#else
ehmaldonadodedaf1c2016-11-18 04:52:22 -0800424 ref_filename_(test::ResourcePath("audio_processing/output_data_float",
425 "pb")),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000426#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000427#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000428 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000429 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000430 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000431 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000432 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000433 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000434 out_file_(NULL) {
435 Config config;
436 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +0100437 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000438}
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
440void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000441 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
443 frame_ = new AudioFrame();
444 revframe_ = new AudioFrame();
445
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000446 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
449void ApmTest::TearDown() {
450 if (frame_) {
451 delete frame_;
452 }
453 frame_ = NULL;
454
455 if (revframe_) {
456 delete revframe_;
457 }
458 revframe_ = NULL;
459
460 if (far_file_) {
461 ASSERT_EQ(0, fclose(far_file_));
462 }
463 far_file_ = NULL;
464
465 if (near_file_) {
466 ASSERT_EQ(0, fclose(near_file_));
467 }
468 near_file_ = NULL;
469
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000470 if (out_file_) {
471 ASSERT_EQ(0, fclose(out_file_));
472 }
473 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000474}
475
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000476void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700478 ap->Initialize(
479 {{{frame_->sample_rate_hz_, frame_->num_channels_},
480 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700481 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700482 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000483}
484
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000485void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000486 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000487 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800488 size_t num_input_channels,
489 size_t num_output_channels,
490 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000491 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000492 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000493 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000494 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000495
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000496 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
497 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000498 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000499
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000500 if (far_file_) {
501 ASSERT_EQ(0, fclose(far_file_));
502 }
503 std::string filename = ResourceFilePath("far", sample_rate_hz);
504 far_file_ = fopen(filename.c_str(), "rb");
505 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
506 filename << "\n";
507
508 if (near_file_) {
509 ASSERT_EQ(0, fclose(near_file_));
510 }
511 filename = ResourceFilePath("near", sample_rate_hz);
512 near_file_ = fopen(filename.c_str(), "rb");
513 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
514 filename << "\n";
515
516 if (open_output_file) {
517 if (out_file_) {
518 ASSERT_EQ(0, fclose(out_file_));
519 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700520 filename = OutputFilePath(
521 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
522 reverse_sample_rate_hz, num_input_channels, num_output_channels,
523 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000524 out_file_ = fopen(filename.c_str(), "wb");
525 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
526 filename << "\n";
527 }
528}
529
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000530void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000531 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000532}
533
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
535 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000536 // The files always contain stereo audio.
537 size_t frame_size = frame->samples_per_channel_ * 2;
yujo36b1a5f2017-06-12 12:45:32 -0700538 size_t read_count = fread(frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000539 sizeof(int16_t),
540 frame_size,
541 file);
542 if (read_count != frame_size) {
543 // Check that the file really ended.
544 EXPECT_NE(0, feof(file));
545 return false; // This is expected.
546 }
547
548 if (frame->num_channels_ == 1) {
yujo36b1a5f2017-06-12 12:45:32 -0700549 MixStereoToMono(frame->data(), frame->mutable_data(),
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000550 frame->samples_per_channel_);
551 }
552
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000553 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000554 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000556 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000557}
558
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
560 return ReadFrame(file, frame, NULL);
561}
562
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000563// If the end of the file has been reached, rewind it and attempt to read the
564// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
566 ChannelBuffer<float>* cb) {
567 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000568 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000570 }
571}
572
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
574 ReadFrameWithRewind(file, frame, NULL);
575}
576
andrew@webrtc.org81865342012-10-27 00:28:27 +0000577void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
578 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000579 EXPECT_EQ(apm_->kNoError,
580 apm_->gain_control()->set_stream_analog_level(127));
581 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000582}
583
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000584int ApmTest::ProcessStreamChooser(Format format) {
585 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586 return apm_->ProcessStream(frame_);
587 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000588 return apm_->ProcessStream(float_cb_->channels(),
589 frame_->samples_per_channel_,
590 frame_->sample_rate_hz_,
591 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000592 output_sample_rate_hz_,
593 LayoutFromChannels(num_output_channels_),
594 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000595}
596
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000597int ApmTest::AnalyzeReverseStreamChooser(Format format) {
598 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700599 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000600 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000601 return apm_->AnalyzeReverseStream(
602 revfloat_cb_->channels(),
603 revframe_->samples_per_channel_,
604 revframe_->sample_rate_hz_,
605 LayoutFromChannels(revframe_->num_channels_));
606}
607
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000608void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
609 int delay_min, int delay_max) {
610 // The |revframe_| and |frame_| should include the proper frame information,
611 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000612 AudioFrame tmp_frame;
613 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000614 bool causal = true;
615
616 tmp_frame.CopyFrom(*revframe_);
617 SetFrameTo(&tmp_frame, 0);
618
619 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
620 // Initialize the |frame_queue| with empty frames.
621 int frame_delay = delay_ms / 10;
622 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000623 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000624 frame->CopyFrom(tmp_frame);
625 frame_queue.push(frame);
626 frame_delay++;
627 causal = false;
628 }
629 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000630 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 frame->CopyFrom(tmp_frame);
632 frame_queue.push(frame);
633 frame_delay--;
634 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000635 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
636 // need enough frames with audio to have reliable estimates, but as few as
637 // possible to keep processing time down. 4.5 seconds seemed to be a good
638 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000640 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000641 frame->CopyFrom(tmp_frame);
642 // Use the near end recording, since that has more speech in it.
643 ASSERT_TRUE(ReadFrame(near_file_, frame));
644 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000645 AudioFrame* reverse_frame = frame;
646 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000647 if (!causal) {
648 reverse_frame = frame_queue.front();
649 // When we call ProcessStream() the frame is modified, so we can't use the
650 // pointer directly when things are non-causal. Use an intermediate frame
651 // and copy the data.
652 process_frame = &tmp_frame;
653 process_frame->CopyFrom(*frame);
654 }
aluebsb0319552016-03-17 20:39:53 -0700655 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000656 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
657 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
658 frame = frame_queue.front();
659 frame_queue.pop();
660 delete frame;
661
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000662 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000663 // Discard the first delay metrics to avoid convergence effects.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200664 static_cast<void>(apm_->GetStatistics(true /* has_remote_tracks */));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000665 }
666 }
667
668 rewind(near_file_);
669 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000670 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000671 frame_queue.pop();
672 delete frame;
673 }
674 // Calculate expected delay estimate and acceptable regions. Further,
675 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700676 const size_t samples_per_ms =
kwiberg7885d3f2017-04-25 12:35:07 -0700677 rtc::SafeMin<size_t>(16u, frame_->samples_per_channel_ / 10);
kwiberg07038562017-06-12 11:40:47 -0700678 const int expected_median =
679 rtc::SafeClamp<int>(delay_ms - system_delay_ms, delay_min, delay_max);
680 const int expected_median_high = rtc::SafeClamp<int>(
681 expected_median + rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700682 delay_max);
kwiberg07038562017-06-12 11:40:47 -0700683 const int expected_median_low = rtc::SafeClamp<int>(
684 expected_median - rtc::dchecked_cast<int>(96 / samples_per_ms), delay_min,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700685 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000686 // Verify delay metrics.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200687 AudioProcessingStats stats =
688 apm_->GetStatistics(true /* has_remote_tracks */);
689 ASSERT_TRUE(stats.delay_median_ms.has_value());
690 int32_t median = *stats.delay_median_ms;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000691 EXPECT_GE(expected_median_high, median);
692 EXPECT_LE(expected_median_low, median);
693}
694
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000695void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000696 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000697 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000698
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000699 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000700 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000701 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000702 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000703
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000704 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 EXPECT_EQ(apm_->kNoError,
706 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000707 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000708 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000709 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000710
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000711 // Other stream parameters set correctly.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200712 AudioProcessing::Config apm_config = apm_->GetConfig();
713 apm_config.echo_canceller.enabled = true;
714 apm_config.echo_canceller.mobile_mode = false;
715 apm_->ApplyConfig(apm_config);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000718 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000719 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720
721 // -- Missing delay --
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000722 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000723 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725
726 // Resets after successful ProcessStream().
727 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000730 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000731
732 // Other stream parameters set correctly.
733 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
734 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000735 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000737 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000738 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
739
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000740 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000741 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000742 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000744 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000745
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000746 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 EXPECT_EQ(apm_->kNoError,
749 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000750 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000751}
752
753TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000754 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000755}
756
757TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000758 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000761TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
762 EXPECT_EQ(0, apm_->delay_offset_ms());
763 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
764 EXPECT_EQ(50, apm_->stream_delay_ms());
765}
766
767TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
768 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000769 apm_->set_delay_offset_ms(100);
770 EXPECT_EQ(100, apm_->delay_offset_ms());
771 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000772 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000773 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
774 EXPECT_EQ(200, apm_->stream_delay_ms());
775
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000776 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000777 apm_->set_delay_offset_ms(-50);
778 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000779 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
780 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000781 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
782 EXPECT_EQ(50, apm_->stream_delay_ms());
783}
784
Michael Graczyk86c6d332015-07-23 11:41:39 -0700785void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800786 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700787 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000788 frame_->num_channels_ = num_channels;
789 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700790 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000791}
792
Michael Graczyk86c6d332015-07-23 11:41:39 -0700793void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800794 size_t num_in_channels,
795 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700796 AudioProcessing::Error expected_return) {
797 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
798 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
799
800 EXPECT_EQ(expected_return,
801 apm_->ProcessStream(float_cb_->channels(), input_stream,
802 output_stream, float_cb_->channels()));
803}
804
805void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800806 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700807 AudioProcessing::Error expected_return) {
808 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700809 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
810 {output_sample_rate_hz_, apm_->num_output_channels()},
811 {frame_->sample_rate_hz_, num_rev_channels},
812 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700813
ekmeyerson60d9b332015-08-14 10:35:55 -0700814 EXPECT_EQ(
815 expected_return,
816 apm_->ProcessReverseStream(
817 float_cb_->channels(), processing_config.reverse_input_stream(),
818 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700819}
820
821TEST_F(ApmTest, ChannelsInt16Interface) {
822 // Testing number of invalid and valid channels.
823 Init(16000, 16000, 16000, 4, 4, 4, false);
824
825 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
826
Peter Kasting69558702016-01-12 16:26:35 -0800827 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700828 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000829 EXPECT_EQ(i, apm_->num_input_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000830 }
831}
832
Michael Graczyk86c6d332015-07-23 11:41:39 -0700833TEST_F(ApmTest, Channels) {
834 // Testing number of invalid and valid channels.
835 Init(16000, 16000, 16000, 4, 4, 4, false);
836
837 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
838 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
839
Peter Kasting69558702016-01-12 16:26:35 -0800840 for (size_t i = 1; i < 4; ++i) {
841 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700842 // Output channels much be one or match input channels.
843 if (j == 1 || i == j) {
844 TestChangingForwardChannels(i, j, kNoErr);
845 TestChangingReverseChannels(i, kNoErr);
846
847 EXPECT_EQ(i, apm_->num_input_channels());
848 EXPECT_EQ(j, apm_->num_output_channels());
849 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800850 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851 } else {
852 TestChangingForwardChannels(i, j,
853 AudioProcessing::kBadNumberChannelsError);
854 }
855 }
856 }
857}
858
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000859TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000860 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000861 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000862 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000863 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700864 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800865 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000866 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000867 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 }
869}
870
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000871TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000872 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000873 // Enable AEC only.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200874 AudioProcessing::Config apm_config = apm_->GetConfig();
875 apm_config.echo_canceller.enabled = true;
876 apm_config.echo_canceller.mobile_mode = false;
877 apm_->ApplyConfig(apm_config);
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000878 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700879 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000880 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000881
882 // Internally in the AEC the amount of lookahead the delay estimation can
883 // handle is 15 blocks and the maximum delay is set to 60 blocks.
884 const int kLookaheadBlocks = 15;
885 const int kMaxDelayBlocks = 60;
886 // The AEC has a startup time before it actually starts to process. This
887 // procedure can flush the internal far-end buffer, which of course affects
888 // the delay estimation. Therefore, we set a system_delay high enough to
889 // avoid that. The smallest system_delay you can report without flushing the
890 // buffer is 66 ms in 8 kHz.
891 //
892 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
893 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
894 // delay estimation. This should be noted though. In case of test failure,
895 // this could be the cause.
896 const int kSystemDelayMs = 66;
897 // Test a couple of corner cases and verify that the estimated delay is
898 // within a valid region (set to +-1.5 blocks). Note that these cases are
899 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800900 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000901 Init(kProcessSampleRates[i],
902 kProcessSampleRates[i],
903 kProcessSampleRates[i],
904 2,
905 2,
906 2,
907 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000908 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700909 const int num_ms_per_block =
910 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000911 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
912 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
913
914 // 1) Verify correct delay estimate at lookahead boundary.
915 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
916 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
917 delay_max_ms);
918 // 2) A delay less than maximum lookahead should give an delay estimate at
919 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
920 delay_ms -= 20;
921 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
922 delay_max_ms);
923 // 3) Three values around zero delay. Note that we need to compensate for
924 // the fake system_delay.
925 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
926 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
927 delay_max_ms);
928 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
929 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
930 delay_max_ms);
931 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
932 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
933 delay_max_ms);
934 // 4) Verify correct delay estimate at maximum delay boundary.
935 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
936 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
937 delay_max_ms);
938 // 5) A delay above the maximum delay should give an estimate at the
939 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
940 delay_ms += 20;
941 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
942 delay_max_ms);
943 }
944}
945
niklase@google.com470e71d2011-07-07 08:21:25 +0000946TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000947 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000948 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000949 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
950 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
951
niklase@google.com470e71d2011-07-07 08:21:25 +0000952 // Toggle routing modes
953 EchoControlMobile::RoutingMode mode[] = {
954 EchoControlMobile::kQuietEarpieceOrHeadset,
955 EchoControlMobile::kEarpiece,
956 EchoControlMobile::kLoudEarpiece,
957 EchoControlMobile::kSpeakerphone,
958 EchoControlMobile::kLoudSpeakerphone,
959 };
pkasting25702cb2016-01-08 13:50:27 -0800960 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000961 EXPECT_EQ(apm_->kNoError,
962 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
963 EXPECT_EQ(mode[i],
964 apm_->echo_control_mobile()->routing_mode());
965 }
966 // Turn comfort noise off/on
967 EXPECT_EQ(apm_->kNoError,
968 apm_->echo_control_mobile()->enable_comfort_noise(false));
969 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
970 EXPECT_EQ(apm_->kNoError,
971 apm_->echo_control_mobile()->enable_comfort_noise(true));
972 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000973 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +0000974 const size_t echo_path_size =
975 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -0800976 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
977 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000978 EXPECT_EQ(apm_->kNullPointerError,
979 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
980 EXPECT_EQ(apm_->kNullPointerError,
981 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
982 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000983 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000984 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000985 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000986 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +0000987 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000988 echo_path_in[i] = echo_path_out[i] + 1;
989 }
990 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000991 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000992 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000993 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
994 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000995 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000996 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
997 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +0000998 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000999 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1000 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001001
1002 // Process a few frames with NS in the default disabled state. This exercises
1003 // a different codepath than with it enabled.
1004 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1005 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1006 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1007 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1008
niklase@google.com470e71d2011-07-07 08:21:25 +00001009 // Turn AECM off
1010 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1011 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1012}
1013
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001014TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001015 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001016 EXPECT_EQ(apm_->kNoError,
1017 apm_->gain_control()->set_mode(
1018 apm_->gain_control()->mode()));
1019
1020 GainControl::Mode mode[] = {
1021 GainControl::kAdaptiveAnalog,
1022 GainControl::kAdaptiveDigital,
1023 GainControl::kFixedDigital
1024 };
pkasting25702cb2016-01-08 13:50:27 -08001025 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001026 EXPECT_EQ(apm_->kNoError,
1027 apm_->gain_control()->set_mode(mode[i]));
1028 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1029 }
1030 // Testing invalid target levels
1031 EXPECT_EQ(apm_->kBadParameterError,
1032 apm_->gain_control()->set_target_level_dbfs(-3));
1033 EXPECT_EQ(apm_->kBadParameterError,
1034 apm_->gain_control()->set_target_level_dbfs(-40));
1035 // Testing valid target levels
1036 EXPECT_EQ(apm_->kNoError,
1037 apm_->gain_control()->set_target_level_dbfs(
1038 apm_->gain_control()->target_level_dbfs()));
1039
1040 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001041 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001042 EXPECT_EQ(apm_->kNoError,
1043 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1044 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1045 }
1046
1047 // Testing invalid compression gains
1048 EXPECT_EQ(apm_->kBadParameterError,
1049 apm_->gain_control()->set_compression_gain_db(-1));
1050 EXPECT_EQ(apm_->kBadParameterError,
1051 apm_->gain_control()->set_compression_gain_db(100));
1052
1053 // Testing valid compression gains
1054 EXPECT_EQ(apm_->kNoError,
1055 apm_->gain_control()->set_compression_gain_db(
1056 apm_->gain_control()->compression_gain_db()));
1057
1058 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001059 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 EXPECT_EQ(apm_->kNoError,
1061 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1062 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1063 }
1064
1065 // Testing limiter off/on
1066 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1067 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1068 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1069 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1070
1071 // Testing invalid level limits
1072 EXPECT_EQ(apm_->kBadParameterError,
1073 apm_->gain_control()->set_analog_level_limits(-1, 512));
1074 EXPECT_EQ(apm_->kBadParameterError,
1075 apm_->gain_control()->set_analog_level_limits(100000, 512));
1076 EXPECT_EQ(apm_->kBadParameterError,
1077 apm_->gain_control()->set_analog_level_limits(512, -1));
1078 EXPECT_EQ(apm_->kBadParameterError,
1079 apm_->gain_control()->set_analog_level_limits(512, 100000));
1080 EXPECT_EQ(apm_->kBadParameterError,
1081 apm_->gain_control()->set_analog_level_limits(512, 255));
1082
1083 // Testing valid level limits
1084 EXPECT_EQ(apm_->kNoError,
1085 apm_->gain_control()->set_analog_level_limits(
1086 apm_->gain_control()->analog_level_minimum(),
1087 apm_->gain_control()->analog_level_maximum()));
1088
1089 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001090 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001091 EXPECT_EQ(apm_->kNoError,
1092 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1093 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1094 }
1095
1096 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001097 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001098 EXPECT_EQ(apm_->kNoError,
1099 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1100 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1101 }
1102
1103 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1104
1105 // Turn AGC off
1106 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1107 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1108}
1109
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001110void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001111 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001112 EXPECT_EQ(apm_->kNoError,
1113 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1114 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1115
1116 int out_analog_level = 0;
1117 for (int i = 0; i < 2000; ++i) {
1118 ReadFrameWithRewind(near_file_, frame_);
1119 // Ensure the audio is at a low level, so the AGC will try to increase it.
1120 ScaleFrame(frame_, 0.25);
1121
1122 // Always pass in the same volume.
1123 EXPECT_EQ(apm_->kNoError,
1124 apm_->gain_control()->set_stream_analog_level(100));
1125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1126 out_analog_level = apm_->gain_control()->stream_analog_level();
1127 }
1128
1129 // Ensure the AGC is still able to reach the maximum.
1130 EXPECT_EQ(255, out_analog_level);
1131}
1132
1133// Verifies that despite volume slider quantization, the AGC can continue to
1134// increase its volume.
1135TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001136 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001137 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1138 }
1139}
1140
1141void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001142 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001143 EXPECT_EQ(apm_->kNoError,
1144 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1145 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1146
1147 int out_analog_level = 100;
1148 for (int i = 0; i < 1000; ++i) {
1149 ReadFrameWithRewind(near_file_, frame_);
1150 // Ensure the audio is at a low level, so the AGC will try to increase it.
1151 ScaleFrame(frame_, 0.25);
1152
1153 EXPECT_EQ(apm_->kNoError,
1154 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1155 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1156 out_analog_level = apm_->gain_control()->stream_analog_level();
1157 }
1158
1159 // Ensure the volume was raised.
1160 EXPECT_GT(out_analog_level, 100);
1161 int highest_level_reached = out_analog_level;
1162 // Simulate a user manual volume change.
1163 out_analog_level = 100;
1164
1165 for (int i = 0; i < 300; ++i) {
1166 ReadFrameWithRewind(near_file_, frame_);
1167 ScaleFrame(frame_, 0.25);
1168
1169 EXPECT_EQ(apm_->kNoError,
1170 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1171 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1172 out_analog_level = apm_->gain_control()->stream_analog_level();
1173 // Check that AGC respected the manually adjusted volume.
1174 EXPECT_LT(out_analog_level, highest_level_reached);
1175 }
1176 // Check that the volume was still raised.
1177 EXPECT_GT(out_analog_level, 100);
1178}
1179
1180TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001181 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001182 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1183 }
1184}
1185
niklase@google.com470e71d2011-07-07 08:21:25 +00001186TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001187 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001188 NoiseSuppression::Level level[] = {
1189 NoiseSuppression::kLow,
1190 NoiseSuppression::kModerate,
1191 NoiseSuppression::kHigh,
1192 NoiseSuppression::kVeryHigh
1193 };
pkasting25702cb2016-01-08 13:50:27 -08001194 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001195 EXPECT_EQ(apm_->kNoError,
1196 apm_->noise_suppression()->set_level(level[i]));
1197 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1198 }
1199
andrew@webrtc.org648af742012-02-08 01:57:29 +00001200 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1202 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1203 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1204 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1205}
1206
1207TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001208 // Turn HP filter on/off
peah8271d042016-11-22 07:24:52 -08001209 AudioProcessing::Config apm_config;
1210 apm_config.high_pass_filter.enabled = true;
1211 apm_->ApplyConfig(apm_config);
1212 apm_config.high_pass_filter.enabled = false;
1213 apm_->ApplyConfig(apm_config);
niklase@google.com470e71d2011-07-07 08:21:25 +00001214}
1215
1216TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001217 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001218 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001219 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001220
1221 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1222
1223 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1224 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1225
1226 // Run this test in wideband; in super-wb, the splitting filter distorts the
1227 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001228 frame_->samples_per_channel_ = 160;
1229 frame_->num_channels_ = 2;
1230 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001231
1232 // Min value if no frames have been processed.
1233 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1234
1235 // Min value on zero frames.
1236 SetFrameTo(frame_, 0);
1237 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1238 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1239 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1240
1241 // Try a few RMS values.
1242 // (These also test that the value resets after retrieving it.)
1243 SetFrameTo(frame_, 32767);
1244 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1245 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1246 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1247
1248 SetFrameTo(frame_, 30000);
1249 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1250 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1251 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1252
1253 SetFrameTo(frame_, 10000);
1254 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1255 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1256 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1257
1258 SetFrameTo(frame_, 10);
1259 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1260 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1261 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1262
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001263 // Verify reset after enable/disable.
1264 SetFrameTo(frame_, 32767);
1265 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1266 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1267 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1268 SetFrameTo(frame_, 1);
1269 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1270 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1271
1272 // Verify reset after initialize.
1273 SetFrameTo(frame_, 32767);
1274 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1275 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1276 SetFrameTo(frame_, 1);
1277 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1278 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001279}
1280
1281TEST_F(ApmTest, VoiceDetection) {
1282 // Test external VAD
1283 EXPECT_EQ(apm_->kNoError,
1284 apm_->voice_detection()->set_stream_has_voice(true));
1285 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1286 EXPECT_EQ(apm_->kNoError,
1287 apm_->voice_detection()->set_stream_has_voice(false));
1288 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1289
andrew@webrtc.org648af742012-02-08 01:57:29 +00001290 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001291 VoiceDetection::Likelihood likelihood[] = {
1292 VoiceDetection::kVeryLowLikelihood,
1293 VoiceDetection::kLowLikelihood,
1294 VoiceDetection::kModerateLikelihood,
1295 VoiceDetection::kHighLikelihood
1296 };
pkasting25702cb2016-01-08 13:50:27 -08001297 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001298 EXPECT_EQ(apm_->kNoError,
1299 apm_->voice_detection()->set_likelihood(likelihood[i]));
1300 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1301 }
1302
1303 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001304 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 EXPECT_EQ(apm_->kBadParameterError,
1306 apm_->voice_detection()->set_frame_size_ms(12));
1307
andrew@webrtc.org648af742012-02-08 01:57:29 +00001308 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 for (int i = 10; i <= 30; i += 10) {
1310 EXPECT_EQ(apm_->kNoError,
1311 apm_->voice_detection()->set_frame_size_ms(i));
1312 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1313 }
1314 */
1315
andrew@webrtc.org648af742012-02-08 01:57:29 +00001316 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001317 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1318 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1319 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1320 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1321
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001322 // Test that AudioFrame activity is maintained when VAD is disabled.
1323 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1324 AudioFrame::VADActivity activity[] = {
1325 AudioFrame::kVadActive,
1326 AudioFrame::kVadPassive,
1327 AudioFrame::kVadUnknown
1328 };
pkasting25702cb2016-01-08 13:50:27 -08001329 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001330 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001331 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001332 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001333 }
1334
1335 // Test that AudioFrame activity is set when VAD is enabled.
1336 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001337 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001338 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001339 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001340
niklase@google.com470e71d2011-07-07 08:21:25 +00001341 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1342}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001343
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001344TEST_F(ApmTest, AllProcessingDisabledByDefault) {
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001345 AudioProcessing::Config config = apm_->GetConfig();
1346 EXPECT_FALSE(config.echo_canceller.enabled);
1347 EXPECT_FALSE(config.high_pass_filter.enabled);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001348 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1349 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1350 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1351 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1352 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1353}
1354
1355TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001356 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001357 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001358 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001359 AudioFrame frame_copy;
1360 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001361 for (int j = 0; j < 1000; j++) {
1362 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1363 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001364 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1365 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001366 }
1367 }
1368}
1369
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001370TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1371 // Test that ProcessStream copies input to output even with no processing.
1372 const size_t kSamples = 80;
1373 const int sample_rate = 8000;
1374 const float src[kSamples] = {
1375 -1.0f, 0.0f, 1.0f
1376 };
1377 float dest[kSamples] = {};
1378
1379 auto src_channels = &src[0];
1380 auto dest_channels = &dest[0];
1381
Ivo Creusen62337e52018-01-09 14:17:33 +01001382 apm_.reset(AudioProcessingBuilder().Create());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001383 EXPECT_NOERR(apm_->ProcessStream(
1384 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1385 sample_rate, LayoutFromChannels(1), &dest_channels));
1386
1387 for (size_t i = 0; i < kSamples; ++i) {
1388 EXPECT_EQ(src[i], dest[i]);
1389 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001390
1391 // Same for ProcessReverseStream.
1392 float rev_dest[kSamples] = {};
1393 auto rev_dest_channels = &rev_dest[0];
1394
1395 StreamConfig input_stream = {sample_rate, 1};
1396 StreamConfig output_stream = {sample_rate, 1};
1397 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1398 output_stream, &rev_dest_channels));
1399
1400 for (size_t i = 0; i < kSamples; ++i) {
1401 EXPECT_EQ(src[i], rev_dest[i]);
1402 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001403}
1404
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001405TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1406 EnableAllComponents();
1407
pkasting25702cb2016-01-08 13:50:27 -08001408 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001409 Init(kProcessSampleRates[i],
1410 kProcessSampleRates[i],
1411 kProcessSampleRates[i],
1412 2,
1413 2,
1414 2,
1415 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001416 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001417 ASSERT_EQ(0, feof(far_file_));
1418 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001419 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
yujo36b1a5f2017-06-12 12:45:32 -07001420 CopyLeftToRightChannel(revframe_->mutable_data(),
1421 revframe_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001422
aluebsb0319552016-03-17 20:39:53 -07001423 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001424
yujo36b1a5f2017-06-12 12:45:32 -07001425 CopyLeftToRightChannel(frame_->mutable_data(),
1426 frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001427 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1428
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001429 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001430 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001431 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001432 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001433 analog_level = apm_->gain_control()->stream_analog_level();
1434
yujo36b1a5f2017-06-12 12:45:32 -07001435 VerifyChannelsAreEqual(frame_->data(), frame_->samples_per_channel_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001436 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001437 rewind(far_file_);
1438 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001439 }
1440}
1441
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001442TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001443 // Verify the filter is not active through undistorted audio when:
1444 // 1. No components are enabled...
1445 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001446 AudioFrame frame_copy;
1447 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001448 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1450 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1451
1452 // 2. Only the level estimator is enabled...
1453 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001454 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001455 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1456 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1457 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1458 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1459 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1460
1461 // 3. Only VAD is enabled...
1462 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001463 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001464 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1465 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1466 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1467 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1468 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1469
1470 // 4. Both VAD and the level estimator are 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_->voice_detection()->Enable(true));
1475 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1476 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1477 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1478 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1479 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1480
1481 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001482 frame_->samples_per_channel_ = 160;
1483 frame_->num_channels_ = 2;
1484 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001485 // Enable AEC, which would require the filter in super-wb. We rely on the
1486 // first few frames of data being unaffected by the AEC.
1487 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1488 // behavior of the AEC. Think of something more robust.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001489 AudioProcessing::Config apm_config = apm_->GetConfig();
1490 apm_config.echo_canceller.enabled = true;
1491 apm_config.echo_canceller.mobile_mode = false;
1492 apm_->ApplyConfig(apm_config);
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001493 // Make sure we have extended filter enabled. This makes sure nothing is
1494 // touched until we have a farend frame.
1495 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001496 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001497 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001498 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001499 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001500 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001501 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1502 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001503 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1504 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1505
1506 // Check the test is valid. We should have distortion from the filter
1507 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001508 frame_->samples_per_channel_ = 320;
1509 frame_->num_channels_ = 2;
1510 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001511 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001512 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001513 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001514 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1515 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1516}
1517
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001518#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1519void ApmTest::ProcessDebugDump(const std::string& in_filename,
1520 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001521 Format format,
1522 int max_size_bytes) {
aleloif4dd1912017-06-15 01:55:38 -07001523 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001524 FILE* in_file = fopen(in_filename.c_str(), "rb");
1525 ASSERT_TRUE(in_file != NULL);
1526 audioproc::Event event_msg;
1527 bool first_init = true;
1528
1529 while (ReadMessageFromFile(in_file, &event_msg)) {
1530 if (event_msg.type() == audioproc::Event::INIT) {
1531 const audioproc::Init msg = event_msg.init();
1532 int reverse_sample_rate = msg.sample_rate();
1533 if (msg.has_reverse_sample_rate()) {
1534 reverse_sample_rate = msg.reverse_sample_rate();
1535 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001536 int output_sample_rate = msg.sample_rate();
1537 if (msg.has_output_sample_rate()) {
1538 output_sample_rate = msg.output_sample_rate();
1539 }
1540
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001541 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001542 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001543 reverse_sample_rate,
1544 msg.num_input_channels(),
1545 msg.num_output_channels(),
1546 msg.num_reverse_channels(),
1547 false);
1548 if (first_init) {
aleloif4dd1912017-06-15 01:55:38 -07001549 // AttachAecDump() writes an additional init message. Don't start
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001550 // recording until after the first init to avoid the extra message.
aleloif4dd1912017-06-15 01:55:38 -07001551 auto aec_dump =
1552 AecDumpFactory::Create(out_filename, max_size_bytes, &worker_queue);
1553 EXPECT_TRUE(aec_dump);
1554 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001555 first_init = false;
1556 }
1557
1558 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1559 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1560
1561 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001562 ASSERT_EQ(revframe_->num_channels_,
1563 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001564 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001565 memcpy(revfloat_cb_->channels()[i],
1566 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001567 msg.channel(i).size());
1568 }
1569 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001570 memcpy(revframe_->mutable_data(), msg.data().data(), msg.data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001571 if (format == kFloatFormat) {
1572 // We're using an int16 input file; convert to float.
1573 ConvertToFloat(*revframe_, revfloat_cb_.get());
1574 }
1575 }
1576 AnalyzeReverseStreamChooser(format);
1577
1578 } else if (event_msg.type() == audioproc::Event::STREAM) {
1579 const audioproc::Stream msg = event_msg.stream();
1580 // ProcessStream could have changed this for the output frame.
1581 frame_->num_channels_ = apm_->num_input_channels();
1582
1583 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1584 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001585 if (msg.has_keypress()) {
1586 apm_->set_stream_key_pressed(msg.keypress());
1587 } else {
1588 apm_->set_stream_key_pressed(true);
1589 }
1590
1591 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001592 ASSERT_EQ(frame_->num_channels_,
1593 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001594 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001595 memcpy(float_cb_->channels()[i],
1596 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001597 msg.input_channel(i).size());
1598 }
1599 } else {
yujo36b1a5f2017-06-12 12:45:32 -07001600 memcpy(frame_->mutable_data(), msg.input_data().data(),
1601 msg.input_data().size());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001602 if (format == kFloatFormat) {
1603 // We're using an int16 input file; convert to float.
1604 ConvertToFloat(*frame_, float_cb_.get());
1605 }
1606 }
1607 ProcessStreamChooser(format);
1608 }
1609 }
aleloif4dd1912017-06-15 01:55:38 -07001610 apm_->DetachAecDump();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001611 fclose(in_file);
1612}
1613
1614void ApmTest::VerifyDebugDumpTest(Format format) {
Minyue Li656d6092018-08-10 15:38:52 +02001615 rtc::ScopedFakeClock fake_clock;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001616 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001617 std::string format_string;
1618 switch (format) {
1619 case kIntFormat:
1620 format_string = "_int";
1621 break;
1622 case kFloatFormat:
1623 format_string = "_float";
1624 break;
1625 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001626 const std::string ref_filename = test::TempFilename(
1627 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1628 const std::string out_filename = test::TempFilename(
1629 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001630 const std::string limited_filename = test::TempFilename(
1631 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1632 const size_t logging_limit_bytes = 100000;
1633 // We expect at least this many bytes in the created logfile.
1634 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001635 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001636 ProcessDebugDump(in_filename, ref_filename, format, -1);
1637 ProcessDebugDump(ref_filename, out_filename, format, -1);
1638 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001639
1640 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1641 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001642 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001643 ASSERT_TRUE(ref_file != NULL);
1644 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001645 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001646 std::unique_ptr<uint8_t[]> ref_bytes;
1647 std::unique_ptr<uint8_t[]> out_bytes;
1648 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001649
1650 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1651 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001652 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001653 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001654 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001655 while (ref_size > 0 && out_size > 0) {
1656 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001657 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001658 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001659 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001660 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001661 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001662 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1663 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001664 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001665 }
1666 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001667 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1668 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001669 EXPECT_NE(0, feof(ref_file));
1670 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001671 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001672 ASSERT_EQ(0, fclose(ref_file));
1673 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001674 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001675 remove(ref_filename.c_str());
1676 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001677 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001678}
1679
pbosc7a65692016-05-06 12:50:04 -07001680TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001681 VerifyDebugDumpTest(kIntFormat);
1682}
1683
pbosc7a65692016-05-06 12:50:04 -07001684TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001685 VerifyDebugDumpTest(kFloatFormat);
1686}
1687#endif
1688
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001689// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001690TEST_F(ApmTest, DebugDump) {
aleloif4dd1912017-06-15 01:55:38 -07001691 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001692 const std::string filename =
1693 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001694 {
1695 auto aec_dump = AecDumpFactory::Create("", -1, &worker_queue);
1696 EXPECT_FALSE(aec_dump);
1697 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001698
1699#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1700 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001701 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001702
aleloif4dd1912017-06-15 01:55:38 -07001703 auto aec_dump = AecDumpFactory::Create(filename, -1, &worker_queue);
1704 EXPECT_TRUE(aec_dump);
1705 apm_->AttachAecDump(std::move(aec_dump));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001706 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001707 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
aleloif4dd1912017-06-15 01:55:38 -07001708 apm_->DetachAecDump();
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001709
1710 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001711 FILE* fid = fopen(filename.c_str(), "r");
1712 ASSERT_TRUE(fid != NULL);
1713
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001714 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001715 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001716 ASSERT_EQ(0, remove(filename.c_str()));
1717#else
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001718 // Verify the file has NOT been written.
1719 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1720#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1721}
1722
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001723// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001724TEST_F(ApmTest, DebugDumpFromFileHandle) {
aleloif4dd1912017-06-15 01:55:38 -07001725 rtc::TaskQueue worker_queue("ApmTest_worker_queue");
1726
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001727 const std::string filename =
1728 test::TempFilename(test::OutputPath(), "debug_aec");
aleloif4dd1912017-06-15 01:55:38 -07001729 FILE* fid = fopen(filename.c_str(), "w");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001730 ASSERT_TRUE(fid);
1731
1732#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1733 // Stopping without having started should be OK.
aleloif4dd1912017-06-15 01:55:38 -07001734 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001735
aleloif4dd1912017-06-15 01:55:38 -07001736 auto aec_dump = AecDumpFactory::Create(fid, -1, &worker_queue);
1737 EXPECT_TRUE(aec_dump);
1738 apm_->AttachAecDump(std::move(aec_dump));
aluebsb0319552016-03-17 20:39:53 -07001739 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001740 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aleloif4dd1912017-06-15 01:55:38 -07001741 apm_->DetachAecDump();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001742
1743 // Verify the file has been written.
1744 fid = fopen(filename.c_str(), "r");
1745 ASSERT_TRUE(fid != NULL);
1746
1747 // Clean it up.
1748 ASSERT_EQ(0, fclose(fid));
1749 ASSERT_EQ(0, remove(filename.c_str()));
1750#else
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001751 ASSERT_EQ(0, fclose(fid));
1752#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1753}
1754
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001755TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001756 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001757 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001758
1759 Config config;
1760 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01001761 std::unique_ptr<AudioProcessing> fapm(
1762 AudioProcessingBuilder().Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001763 EnableAllComponents();
1764 EnableAllAPComponents(fapm.get());
1765 for (int i = 0; i < ref_data.test_size(); i++) {
1766 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1767
1768 audioproc::Test* test = ref_data.mutable_test(i);
1769 // TODO(ajm): Restore downmixing test cases.
1770 if (test->num_input_channels() != test->num_output_channels())
1771 continue;
1772
Peter Kasting69558702016-01-12 16:26:35 -08001773 const size_t num_render_channels =
1774 static_cast<size_t>(test->num_reverse_channels());
1775 const size_t num_input_channels =
1776 static_cast<size_t>(test->num_input_channels());
1777 const size_t num_output_channels =
1778 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001779 const size_t samples_per_channel = static_cast<size_t>(
1780 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001781
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001782 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1783 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001784 Init(fapm.get());
1785
1786 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001787 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1788 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001789
1790 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001791 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001792 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1793 ReadFrame(near_file_, frame_, float_cb_.get())) {
1794 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1795
aluebsb0319552016-03-17 20:39:53 -07001796 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001797 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1798 revfloat_cb_->channels(),
1799 samples_per_channel,
1800 test->sample_rate(),
1801 LayoutFromChannels(num_render_channels)));
1802
1803 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1804 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001805 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1806 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1807
1808 EXPECT_NOERR(apm_->ProcessStream(frame_));
yujo36b1a5f2017-06-12 12:45:32 -07001809 Deinterleave(frame_->data(), samples_per_channel, num_output_channels,
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001810 output_int16.channels());
1811
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001812 EXPECT_NOERR(fapm->ProcessStream(
1813 float_cb_->channels(),
1814 samples_per_channel,
1815 test->sample_rate(),
1816 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001817 test->sample_rate(),
1818 LayoutFromChannels(num_output_channels),
1819 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001820 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001821 FloatToS16(float_cb_->channels()[j],
1822 samples_per_channel,
1823 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001824 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001825 float snr = ComputeSNR(output_int16.channels()[j],
1826 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001827 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001828
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001829 const float kVarianceThreshold = 20;
1830 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07001831
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001832 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07001833 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
1834 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001835 }
1836 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001837
1838 analog_level = fapm->gain_control()->stream_analog_level();
1839 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
1840 fapm->gain_control()->stream_analog_level());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001841 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
1842 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07001843 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001844
1845 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001846 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001847 }
aluebs776593b2016-03-15 14:04:58 -07001848
1849#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1850 const size_t kMaxNumBadChunks = 0;
1851#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
1852 // There are a few chunks in the fixed-point profile that give low SNR.
1853 // Listening confirmed the difference is acceptable.
1854 const size_t kMaxNumBadChunks = 60;
1855#endif
1856 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
1857
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001858 rewind(far_file_);
1859 rewind(near_file_);
1860 }
1861}
1862
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001863// TODO(andrew): Add a test to process a few frames with different combinations
1864// of enabled components.
1865
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00001866TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001867 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001868 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001869
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001870 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001871 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001872 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001873 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08001874 for (size_t i = 0; i < arraysize(kChannels); i++) {
1875 for (size_t j = 0; j < arraysize(kChannels); j++) {
1876 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001877 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001878 test->set_num_reverse_channels(kChannels[i]);
1879 test->set_num_input_channels(kChannels[j]);
1880 test->set_num_output_channels(kChannels[j]);
1881 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001882 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001883 }
1884 }
1885 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001886#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1887 // To test the extended filter mode.
1888 audioproc::Test* test = ref_data.add_test();
1889 test->set_num_reverse_channels(2);
1890 test->set_num_input_channels(2);
1891 test->set_num_output_channels(2);
1892 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
1893 test->set_use_aec_extended_filter(true);
1894#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001895 }
1896
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001897 for (int i = 0; i < ref_data.test_size(); i++) {
1898 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001899
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001900 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00001901 // TODO(ajm): We no longer allow different input and output channels. Skip
1902 // these tests for now, but they should be removed from the set.
1903 if (test->num_input_channels() != test->num_output_channels())
1904 continue;
1905
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001906 Config config;
1907 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02001908 config.Set<ExtendedFilter>(
1909 new ExtendedFilter(test->use_aec_extended_filter()));
Ivo Creusen62337e52018-01-09 14:17:33 +01001910 apm_.reset(AudioProcessingBuilder().Create(config));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00001911
1912 EnableAllComponents();
1913
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001914 Init(test->sample_rate(),
1915 test->sample_rate(),
1916 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08001917 static_cast<size_t>(test->num_input_channels()),
1918 static_cast<size_t>(test->num_output_channels()),
1919 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001920 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001921
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001922 int frame_count = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001923 int has_voice_count = 0;
1924 int is_saturated_count = 0;
1925 int analog_level = 127;
1926 int analog_level_average = 0;
1927 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001928 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07001929#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1930 int stats_index = 0;
1931#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001932
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001933 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07001934 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001935
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001936 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1937
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001938 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001939 EXPECT_EQ(apm_->kNoError,
1940 apm_->gain_control()->set_stream_analog_level(analog_level));
1941
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001942 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001943
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001944 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08001945 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
1946 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001947
1948 max_output_average += MaxAudioFrame(*frame_);
1949
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001950 analog_level = apm_->gain_control()->stream_analog_level();
1951 analog_level_average += analog_level;
1952 if (apm_->gain_control()->stream_is_saturated()) {
1953 is_saturated_count++;
1954 }
1955 if (apm_->voice_detection()->stream_has_voice()) {
1956 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001957 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001958 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001959 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001960 }
1961
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001962 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
1963
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001964 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
yujo36b1a5f2017-06-12 12:45:32 -07001965 size_t write_count = fwrite(frame_->data(),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001966 sizeof(int16_t),
1967 frame_size,
1968 out_file_);
1969 ASSERT_EQ(frame_size, write_count);
1970
1971 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08001972 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001973 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07001974
1975#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
1976 const int kStatsAggregationFrameNum = 100; // 1 second.
1977 if (frame_count % kStatsAggregationFrameNum == 0) {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001978 // Get echo and delay metrics.
1979 AudioProcessingStats stats =
1980 apm_->GetStatistics(true /* has_remote_tracks */);
minyue58530ed2016-05-24 05:50:12 -07001981
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02001982 // Echo metrics.
1983 const float echo_return_loss = stats.echo_return_loss.value_or(-1.0f);
1984 const float echo_return_loss_enhancement =
1985 stats.echo_return_loss_enhancement.value_or(-1.0f);
1986 const float divergent_filter_fraction =
1987 stats.divergent_filter_fraction.value_or(-1.0f);
1988 const float residual_echo_likelihood =
1989 stats.residual_echo_likelihood.value_or(-1.0f);
1990 const float residual_echo_likelihood_recent_max =
1991 stats.residual_echo_likelihood_recent_max.value_or(-1.0f);
1992
1993 // Delay metrics.
1994 const int32_t delay_median_ms = stats.delay_median_ms.value_or(-1.0);
1995 const int32_t delay_standard_deviation_ms =
1996 stats.delay_standard_deviation_ms.value_or(-1.0);
minyue58530ed2016-05-24 05:50:12 -07001997
1998 // Get RMS.
1999 int rms_level = apm_->level_estimator()->RMS();
2000 EXPECT_LE(0, rms_level);
2001 EXPECT_GE(127, rms_level);
2002
2003 if (!write_ref_data) {
2004 const audioproc::Test::EchoMetrics& reference =
2005 test->echo_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002006 constexpr float kEpsilon = 0.01;
2007 EXPECT_NEAR(echo_return_loss, reference.echo_return_loss(), kEpsilon);
2008 EXPECT_NEAR(echo_return_loss_enhancement,
2009 reference.echo_return_loss_enhancement(), kEpsilon);
2010 EXPECT_NEAR(divergent_filter_fraction,
2011 reference.divergent_filter_fraction(), kEpsilon);
2012 EXPECT_NEAR(residual_echo_likelihood,
2013 reference.residual_echo_likelihood(), kEpsilon);
2014 EXPECT_NEAR(residual_echo_likelihood_recent_max,
2015 reference.residual_echo_likelihood_recent_max(),
2016 kEpsilon);
minyue58530ed2016-05-24 05:50:12 -07002017
2018 const audioproc::Test::DelayMetrics& reference_delay =
2019 test->delay_metrics(stats_index);
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002020 EXPECT_EQ(reference_delay.median(), delay_median_ms);
2021 EXPECT_EQ(reference_delay.std(), delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07002022
2023 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2024
2025 ++stats_index;
2026 } else {
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002027 audioproc::Test::EchoMetrics* message_echo = test->add_echo_metrics();
2028 message_echo->set_echo_return_loss(echo_return_loss);
2029 message_echo->set_echo_return_loss_enhancement(
2030 echo_return_loss_enhancement);
2031 message_echo->set_divergent_filter_fraction(
2032 divergent_filter_fraction);
2033 message_echo->set_residual_echo_likelihood(residual_echo_likelihood);
2034 message_echo->set_residual_echo_likelihood_recent_max(
2035 residual_echo_likelihood_recent_max);
minyue58530ed2016-05-24 05:50:12 -07002036 audioproc::Test::DelayMetrics* message_delay =
2037 test->add_delay_metrics();
Sam Zackrissonaf6c1392018-09-13 12:59:09 +02002038 message_delay->set_median(delay_median_ms);
2039 message_delay->set_std(delay_standard_deviation_ms);
minyue58530ed2016-05-24 05:50:12 -07002040
2041 test->add_rms_level(rms_level);
2042 }
2043 }
2044#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002045 }
2046 max_output_average /= frame_count;
2047 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002048 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002049
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002050 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002051 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002052 // When running the test on a N7 we get a {2, 6} difference of
2053 // |has_voice_count| and |max_output_average| is up to 18 higher.
2054 // All numbers being consistently higher on N7 compare to ref_data.
2055 // TODO(bjornv): If we start getting more of these offsets on Android we
2056 // should consider a different approach. Either using one slack for all,
2057 // or generate a separate android reference.
Kári Tristan Helgason640106e2018-09-06 15:29:45 +02002058#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002059 const int kHasVoiceCountOffset = 3;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002060 const int kHasVoiceCountNear = 8;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002061 const int kMaxOutputAverageOffset = 9;
Sam Zackrissone507b0c2018-07-20 15:22:50 +02002062 const int kMaxOutputAverageNear = 26;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002063#else
2064 const int kHasVoiceCountOffset = 0;
2065 const int kHasVoiceCountNear = kIntNear;
2066 const int kMaxOutputAverageOffset = 0;
2067 const int kMaxOutputAverageNear = kIntNear;
2068#endif
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002069 EXPECT_NEAR(test->has_voice_count(),
2070 has_voice_count - kHasVoiceCountOffset,
2071 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002072 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002073
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002074 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002075 EXPECT_NEAR(test->max_output_average(),
2076 max_output_average - kMaxOutputAverageOffset,
2077 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002078#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002079 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002080 EXPECT_NEAR(test->ns_speech_probability_average(),
2081 ns_speech_prob_average,
2082 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002083#endif
2084 } else {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002085 test->set_has_voice_count(has_voice_count);
2086 test->set_is_saturated_count(is_saturated_count);
2087
2088 test->set_analog_level_average(analog_level_average);
2089 test->set_max_output_average(max_output_average);
2090
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002091#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002092 EXPECT_LE(0.0f, ns_speech_prob_average);
2093 EXPECT_GE(1.0f, ns_speech_prob_average);
2094 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002095#endif
2096 }
2097
2098 rewind(far_file_);
2099 rewind(near_file_);
2100 }
2101
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002102 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002103 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002104 }
2105}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002106
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002107TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2108 struct ChannelFormat {
2109 AudioProcessing::ChannelLayout in_layout;
2110 AudioProcessing::ChannelLayout out_layout;
2111 };
2112 ChannelFormat cf[] = {
2113 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2114 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2115 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2116 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002117
Ivo Creusen62337e52018-01-09 14:17:33 +01002118 std::unique_ptr<AudioProcessing> ap(AudioProcessingBuilder().Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002119 // Enable one component just to ensure some processing takes place.
2120 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002121 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002122 const int in_rate = 44100;
2123 const int out_rate = 48000;
2124 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2125 TotalChannelsFromLayout(cf[i].in_layout));
2126 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2127 ChannelsFromLayout(cf[i].out_layout));
2128
2129 // Run over a few chunks.
2130 for (int j = 0; j < 10; ++j) {
2131 EXPECT_NOERR(ap->ProcessStream(
2132 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002133 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002134 in_rate,
2135 cf[i].in_layout,
2136 out_rate,
2137 cf[i].out_layout,
2138 out_cb.channels()));
2139 }
2140 }
2141}
2142
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002143// Compares the reference and test arrays over a region around the expected
2144// delay. Finds the highest SNR in that region and adds the variance and squared
2145// error results to the supplied accumulators.
2146void UpdateBestSNR(const float* ref,
2147 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002148 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002149 int expected_delay,
2150 double* variance_acc,
2151 double* sq_error_acc) {
2152 double best_snr = std::numeric_limits<double>::min();
2153 double best_variance = 0;
2154 double best_sq_error = 0;
2155 // Search over a region of eight samples around the expected delay.
2156 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2157 ++delay) {
2158 double sq_error = 0;
2159 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002160 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002161 double error = test[i + delay] - ref[i];
2162 sq_error += error * error;
2163 variance += ref[i] * ref[i];
2164 }
2165
2166 if (sq_error == 0) {
2167 *variance_acc += variance;
2168 return;
2169 }
2170 double snr = variance / sq_error;
2171 if (snr > best_snr) {
2172 best_snr = snr;
2173 best_variance = variance;
2174 best_sq_error = sq_error;
2175 }
2176 }
2177
2178 *variance_acc += best_variance;
2179 *sq_error_acc += best_sq_error;
2180}
2181
2182// Used to test a multitude of sample rate and channel combinations. It works
2183// by first producing a set of reference files (in SetUpTestCase) that are
2184// assumed to be correct, as the used parameters are verified by other tests
2185// in this collection. Primarily the reference files are all produced at
2186// "native" rates which do not involve any resampling.
2187
2188// Each test pass produces an output file with a particular format. The output
2189// is matched against the reference file closest to its internal processing
2190// format. If necessary the output is resampled back to its process format.
2191// Due to the resampling distortion, we don't expect identical results, but
2192// enforce SNR thresholds which vary depending on the format. 0 is a special
2193// case SNR which corresponds to inf, or zero error.
Edward Lemurc5ee9872017-10-23 23:33:04 +02002194typedef std::tuple<int, int, int, int, double, double> AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002195class AudioProcessingTest
2196 : public testing::TestWithParam<AudioProcessingTestData> {
2197 public:
2198 AudioProcessingTest()
Edward Lemurc5ee9872017-10-23 23:33:04 +02002199 : input_rate_(std::get<0>(GetParam())),
2200 output_rate_(std::get<1>(GetParam())),
2201 reverse_input_rate_(std::get<2>(GetParam())),
2202 reverse_output_rate_(std::get<3>(GetParam())),
2203 expected_snr_(std::get<4>(GetParam())),
2204 expected_reverse_snr_(std::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002205
2206 virtual ~AudioProcessingTest() {}
2207
2208 static void SetUpTestCase() {
2209 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002210 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002211 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002212 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2213 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2214 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002215 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002216 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2217 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2218 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002219 }
2220 }
2221 }
2222 }
2223
Gustaf Ullberg8ffeeb22017-10-11 11:42:38 +02002224 void TearDown() {
2225 // Remove "out" files after each test.
2226 ClearTempOutFiles();
2227 }
2228
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002229 static void TearDownTestCase() {
2230 ClearTempFiles();
2231 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002232
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002233 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002234 // to a file specified with |output_file_prefix|. Both forward and reverse
2235 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002236 static void ProcessFormat(int input_rate,
2237 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002238 int reverse_input_rate,
2239 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002240 size_t num_input_channels,
2241 size_t num_output_channels,
2242 size_t num_reverse_input_channels,
2243 size_t num_reverse_output_channels,
Alex Loiko890988c2017-08-31 10:25:48 +02002244 const std::string& output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002245 Config config;
2246 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Ivo Creusen62337e52018-01-09 14:17:33 +01002247 std::unique_ptr<AudioProcessing> ap(
2248 AudioProcessingBuilder().Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002249 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002250
ekmeyerson60d9b332015-08-14 10:35:55 -07002251 ProcessingConfig processing_config = {
2252 {{input_rate, num_input_channels},
2253 {output_rate, num_output_channels},
2254 {reverse_input_rate, num_reverse_input_channels},
2255 {reverse_output_rate, num_reverse_output_channels}}};
2256 ap->Initialize(processing_config);
2257
2258 FILE* far_file =
2259 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002260 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002261 FILE* out_file =
2262 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2263 reverse_input_rate, reverse_output_rate,
2264 num_input_channels, num_output_channels,
2265 num_reverse_input_channels,
2266 num_reverse_output_channels, kForward).c_str(),
2267 "wb");
2268 FILE* rev_out_file =
2269 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2270 reverse_input_rate, reverse_output_rate,
2271 num_input_channels, num_output_channels,
2272 num_reverse_input_channels,
2273 num_reverse_output_channels, kReverse).c_str(),
2274 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002275 ASSERT_TRUE(far_file != NULL);
2276 ASSERT_TRUE(near_file != NULL);
2277 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002278 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002279
2280 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2281 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002282 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2283 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002284 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2285 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002286 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2287 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002288
2289 // Temporary buffers.
2290 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002291 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2292 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002293 std::unique_ptr<float[]> float_data(new float[max_length]);
2294 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002295
2296 int analog_level = 127;
2297 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2298 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002299 EXPECT_NOERR(ap->ProcessReverseStream(
2300 rev_cb.channels(), processing_config.reverse_input_stream(),
2301 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002302
2303 EXPECT_NOERR(ap->set_stream_delay_ms(0));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002304 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2305
2306 EXPECT_NOERR(ap->ProcessStream(
2307 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002308 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002309 input_rate,
2310 LayoutFromChannels(num_input_channels),
2311 output_rate,
2312 LayoutFromChannels(num_output_channels),
2313 out_cb.channels()));
2314
ekmeyerson60d9b332015-08-14 10:35:55 -07002315 // Dump forward output to file.
2316 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002317 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002318 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002319
pkasting25702cb2016-01-08 13:50:27 -08002320 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002321 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002322 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002323
ekmeyerson60d9b332015-08-14 10:35:55 -07002324 // Dump reverse output to file.
2325 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2326 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002327 size_t rev_out_length =
2328 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002329
pkasting25702cb2016-01-08 13:50:27 -08002330 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002331 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2332 rev_out_file));
2333
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002334 analog_level = ap->gain_control()->stream_analog_level();
2335 }
2336 fclose(far_file);
2337 fclose(near_file);
2338 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002339 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002340 }
2341
2342 protected:
2343 int input_rate_;
2344 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002345 int reverse_input_rate_;
2346 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002347 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002348 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002349};
2350
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002351TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002352 struct ChannelFormat {
2353 int num_input;
2354 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002355 int num_reverse_input;
2356 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002357 };
2358 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002359 {1, 1, 1, 1},
2360 {1, 1, 2, 1},
2361 {2, 1, 1, 1},
2362 {2, 1, 2, 1},
2363 {2, 2, 1, 1},
2364 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002365 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002366
pkasting25702cb2016-01-08 13:50:27 -08002367 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002368 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2369 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2370 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002371
ekmeyerson60d9b332015-08-14 10:35:55 -07002372 // Verify output for both directions.
2373 std::vector<StreamDirection> stream_directions;
2374 stream_directions.push_back(kForward);
2375 stream_directions.push_back(kReverse);
2376 for (StreamDirection file_direction : stream_directions) {
2377 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2378 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2379 const int out_num =
2380 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2381 const double expected_snr =
2382 file_direction ? expected_reverse_snr_ : expected_snr_;
2383
2384 const int min_ref_rate = std::min(in_rate, out_rate);
2385 int ref_rate;
2386
2387 if (min_ref_rate > 32000) {
2388 ref_rate = 48000;
2389 } else if (min_ref_rate > 16000) {
2390 ref_rate = 32000;
2391 } else if (min_ref_rate > 8000) {
2392 ref_rate = 16000;
2393 } else {
2394 ref_rate = 8000;
2395 }
aluebs776593b2016-03-15 14:04:58 -07002396#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002397 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002398 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002399 }
2400#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002401 FILE* out_file = fopen(
2402 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2403 reverse_output_rate_, cf[i].num_input,
2404 cf[i].num_output, cf[i].num_reverse_input,
2405 cf[i].num_reverse_output, file_direction).c_str(),
2406 "rb");
2407 // The reference files always have matching input and output channels.
2408 FILE* ref_file = fopen(
2409 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2410 cf[i].num_output, cf[i].num_output,
2411 cf[i].num_reverse_output, cf[i].num_reverse_output,
2412 file_direction).c_str(),
2413 "rb");
2414 ASSERT_TRUE(out_file != NULL);
2415 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002416
pkasting25702cb2016-01-08 13:50:27 -08002417 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2418 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002419 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002420 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002421 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002422 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002423 // Data from the resampled output, in case the reference and output rates
2424 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002425 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002426
ekmeyerson60d9b332015-08-14 10:35:55 -07002427 PushResampler<float> resampler;
2428 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002429
ekmeyerson60d9b332015-08-14 10:35:55 -07002430 // Compute the resampling delay of the output relative to the reference,
2431 // to find the region over which we should search for the best SNR.
2432 float expected_delay_sec = 0;
2433 if (in_rate != ref_rate) {
2434 // Input resampling delay.
2435 expected_delay_sec +=
2436 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2437 }
2438 if (out_rate != ref_rate) {
2439 // Output resampling delay.
2440 expected_delay_sec +=
2441 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2442 // Delay of converting the output back to its processing rate for
2443 // testing.
2444 expected_delay_sec +=
2445 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2446 }
2447 int expected_delay =
2448 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002449
ekmeyerson60d9b332015-08-14 10:35:55 -07002450 double variance = 0;
2451 double sq_error = 0;
2452 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2453 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2454 float* out_ptr = out_data.get();
2455 if (out_rate != ref_rate) {
2456 // Resample the output back to its internal processing rate if
2457 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002458 ASSERT_EQ(ref_length,
2459 static_cast<size_t>(resampler.Resample(
2460 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002461 out_ptr = cmp_data.get();
2462 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002463
ekmeyerson60d9b332015-08-14 10:35:55 -07002464 // Update the |sq_error| and |variance| accumulators with the highest
2465 // SNR of reference vs output.
2466 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2467 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002468 }
2469
ekmeyerson60d9b332015-08-14 10:35:55 -07002470 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2471 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2472 << cf[i].num_input << ", " << cf[i].num_output << ", "
2473 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2474 << ", " << file_direction << "): ";
2475 if (sq_error > 0) {
2476 double snr = 10 * log10(variance / sq_error);
2477 EXPECT_GE(snr, expected_snr);
2478 EXPECT_NE(0, expected_snr);
2479 std::cout << "SNR=" << snr << " dB" << std::endl;
2480 } else {
aluebs776593b2016-03-15 14:04:58 -07002481 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002482 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002483
ekmeyerson60d9b332015-08-14 10:35:55 -07002484 fclose(out_file);
2485 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002486 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002487 }
2488}
2489
2490#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2491INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002492 CommonFormats,
2493 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002494 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 0, 0),
2495 std::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2496 std::make_tuple(48000, 48000, 16000, 48000, 40, 20),
2497 std::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2498 std::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2499 std::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2500 std::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2501 std::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2502 std::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2503 std::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2504 std::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2505 std::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002506
Edward Lemurc5ee9872017-10-23 23:33:04 +02002507 std::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2508 std::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2509 std::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2510 std::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2511 std::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2512 std::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2513 std::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2514 std::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2515 std::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2516 std::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2517 std::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2518 std::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002519
Edward Lemurc5ee9872017-10-23 23:33:04 +02002520 std::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2521 std::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2522 std::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2523 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2524 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2525 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2526 std::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2527 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2528 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2529 std::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2530 std::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2531 std::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002532
Edward Lemurc5ee9872017-10-23 23:33:04 +02002533 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2534 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2535 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2536 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2537 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2538 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2539 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2540 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2541 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2542 std::make_tuple(16000, 16000, 48000, 16000, 40, 20),
2543 std::make_tuple(16000, 16000, 32000, 16000, 40, 20),
2544 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002545
2546#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2547INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002548 CommonFormats,
2549 AudioProcessingTest,
Edward Lemurc5ee9872017-10-23 23:33:04 +02002550 testing::Values(std::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2551 std::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2552 std::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2553 std::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2554 std::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2555 std::make_tuple(48000, 44100, 16000, 44100, 15, 15),
2556 std::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2557 std::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2558 std::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2559 std::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2560 std::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2561 std::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002562
Edward Lemurc5ee9872017-10-23 23:33:04 +02002563 std::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2564 std::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2565 std::make_tuple(44100, 48000, 16000, 48000, 15, 20),
2566 std::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2567 std::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2568 std::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2569 std::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2570 std::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2571 std::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2572 std::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2573 std::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2574 std::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002575
Edward Lemurc5ee9872017-10-23 23:33:04 +02002576 std::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2577 std::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2578 std::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2579 std::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2580 std::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2581 std::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2582 std::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2583 std::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2584 std::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2585 std::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2586 std::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2587 std::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002588
Edward Lemurc5ee9872017-10-23 23:33:04 +02002589 std::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2590 std::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2591 std::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2592 std::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2593 std::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2594 std::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2595 std::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2596 std::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2597 std::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2598 std::make_tuple(16000, 16000, 48000, 16000, 35, 20),
2599 std::make_tuple(16000, 16000, 32000, 16000, 35, 20),
2600 std::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002601#endif
2602
niklase@google.com470e71d2011-07-07 08:21:25 +00002603} // namespace
peahc19f3122016-10-07 14:54:10 -07002604
Alessio Bazzicac054e782018-04-16 12:10:09 +02002605TEST(RuntimeSettingTest, TestDefaultCtor) {
2606 auto s = AudioProcessing::RuntimeSetting();
2607 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2608}
2609
2610TEST(RuntimeSettingTest, TestCapturePreGain) {
2611 using Type = AudioProcessing::RuntimeSetting::Type;
2612 {
2613 auto s = AudioProcessing::RuntimeSetting::CreateCapturePreGain(1.25f);
2614 EXPECT_EQ(Type::kCapturePreGain, s.type());
2615 float v;
2616 s.GetFloat(&v);
2617 EXPECT_EQ(1.25f, v);
2618 }
2619
2620#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
2621 EXPECT_DEATH(AudioProcessing::RuntimeSetting::CreateCapturePreGain(0.1f), "");
2622#endif
2623}
2624
2625TEST(RuntimeSettingTest, TestUsageWithSwapQueue) {
2626 SwapQueue<AudioProcessing::RuntimeSetting> q(1);
2627 auto s = AudioProcessing::RuntimeSetting();
2628 ASSERT_TRUE(q.Insert(&s));
2629 ASSERT_TRUE(q.Remove(&s));
2630 EXPECT_EQ(AudioProcessing::RuntimeSetting::Type::kNotSpecified, s.type());
2631}
2632
Sam Zackrisson0beac582017-09-25 12:04:02 +02002633TEST(ApmConfiguration, EnablePostProcessing) {
2634 // Verify that apm uses a capture post processing module if one is provided.
Sam Zackrisson0beac582017-09-25 12:04:02 +02002635 auto mock_post_processor_ptr =
Alex Loiko5825aa62017-12-18 16:02:40 +01002636 new testing::NiceMock<test::MockCustomProcessing>();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002637 auto mock_post_processor =
Alex Loiko5825aa62017-12-18 16:02:40 +01002638 std::unique_ptr<CustomProcessing>(mock_post_processor_ptr);
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002639 rtc::scoped_refptr<AudioProcessing> apm =
2640 AudioProcessingBuilder()
2641 .SetCapturePostProcessing(std::move(mock_post_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002642 .Create();
Sam Zackrisson0beac582017-09-25 12:04:02 +02002643
2644 AudioFrame audio;
2645 audio.num_channels_ = 1;
2646 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2647
2648 EXPECT_CALL(*mock_post_processor_ptr, Process(testing::_)).Times(1);
Gustaf Ullbergd8579e02017-10-11 16:29:02 +02002649 apm->ProcessStream(&audio);
Sam Zackrisson0beac582017-09-25 12:04:02 +02002650}
2651
Alex Loiko5825aa62017-12-18 16:02:40 +01002652TEST(ApmConfiguration, EnablePreProcessing) {
2653 // Verify that apm uses a capture post processing module if one is provided.
Alex Loiko5825aa62017-12-18 16:02:40 +01002654 auto mock_pre_processor_ptr =
2655 new testing::NiceMock<test::MockCustomProcessing>();
2656 auto mock_pre_processor =
2657 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
Ivo Creusen62337e52018-01-09 14:17:33 +01002658 rtc::scoped_refptr<AudioProcessing> apm =
2659 AudioProcessingBuilder()
2660 .SetRenderPreProcessing(std::move(mock_pre_processor))
Alex Loiko73ec0192018-05-15 10:52:28 +02002661 .Create();
Alex Loiko5825aa62017-12-18 16:02:40 +01002662
2663 AudioFrame audio;
2664 audio.num_channels_ = 1;
2665 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2666
2667 EXPECT_CALL(*mock_pre_processor_ptr, Process(testing::_)).Times(1);
2668 apm->ProcessReverseStream(&audio);
2669}
2670
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002671TEST(ApmConfiguration, EnableCaptureAnalyzer) {
2672 // Verify that apm uses a capture analyzer if one is provided.
2673 auto mock_capture_analyzer_ptr =
2674 new testing::NiceMock<test::MockCustomAudioAnalyzer>();
2675 auto mock_capture_analyzer =
2676 std::unique_ptr<CustomAudioAnalyzer>(mock_capture_analyzer_ptr);
2677 rtc::scoped_refptr<AudioProcessing> apm =
2678 AudioProcessingBuilder()
2679 .SetCaptureAnalyzer(std::move(mock_capture_analyzer))
2680 .Create();
2681
2682 AudioFrame audio;
2683 audio.num_channels_ = 1;
2684 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2685
2686 EXPECT_CALL(*mock_capture_analyzer_ptr, Analyze(testing::_)).Times(1);
2687 apm->ProcessStream(&audio);
2688}
2689
Alex Loiko73ec0192018-05-15 10:52:28 +02002690TEST(ApmConfiguration, PreProcessingReceivesRuntimeSettings) {
2691 auto mock_pre_processor_ptr =
2692 new testing::NiceMock<test::MockCustomProcessing>();
2693 auto mock_pre_processor =
2694 std::unique_ptr<CustomProcessing>(mock_pre_processor_ptr);
2695 rtc::scoped_refptr<AudioProcessing> apm =
2696 AudioProcessingBuilder()
2697 .SetRenderPreProcessing(std::move(mock_pre_processor))
2698 .Create();
2699 apm->SetRuntimeSetting(
2700 AudioProcessing::RuntimeSetting::CreateCustomRenderSetting(0));
2701
2702 // RuntimeSettings forwarded during 'Process*Stream' calls.
2703 // Therefore we have to make one such call.
2704 AudioFrame audio;
2705 audio.num_channels_ = 1;
2706 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2707
2708 EXPECT_CALL(*mock_pre_processor_ptr, SetRuntimeSetting(testing::_)).Times(1);
2709 apm->ProcessReverseStream(&audio);
2710}
2711
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002712class MyEchoControlFactory : public EchoControlFactory {
2713 public:
2714 std::unique_ptr<EchoControl> Create(int sample_rate_hz) {
2715 auto ec = new test::MockEchoControl();
2716 EXPECT_CALL(*ec, AnalyzeRender(testing::_)).Times(1);
2717 EXPECT_CALL(*ec, AnalyzeCapture(testing::_)).Times(2);
2718 EXPECT_CALL(*ec, ProcessCapture(testing::_, testing::_)).Times(2);
2719 return std::unique_ptr<EchoControl>(ec);
2720 }
2721};
2722
2723TEST(ApmConfiguration, EchoControlInjection) {
2724 // Verify that apm uses an injected echo controller if one is provided.
2725 webrtc::Config webrtc_config;
2726 std::unique_ptr<EchoControlFactory> echo_control_factory(
2727 new MyEchoControlFactory());
2728
Alex Loiko5825aa62017-12-18 16:02:40 +01002729 rtc::scoped_refptr<AudioProcessing> apm =
Ivo Creusen5ec7e122017-12-22 11:35:59 +01002730 AudioProcessingBuilder()
2731 .SetEchoControlFactory(std::move(echo_control_factory))
2732 .Create(webrtc_config);
Gustaf Ullberg002ef282017-10-12 15:13:17 +02002733
2734 AudioFrame audio;
2735 audio.num_channels_ = 1;
2736 SetFrameSampleRate(&audio, AudioProcessing::NativeRate::kSampleRate16kHz);
2737 apm->ProcessStream(&audio);
2738 apm->ProcessReverseStream(&audio);
2739 apm->ProcessStream(&audio);
2740}
Ivo Creusenae026092017-11-20 13:07:16 +01002741
2742std::unique_ptr<AudioProcessing> CreateApm(bool use_AEC2) {
2743 Config old_config;
2744 if (use_AEC2) {
2745 old_config.Set<ExtendedFilter>(new ExtendedFilter(true));
2746 old_config.Set<DelayAgnostic>(new DelayAgnostic(true));
2747 }
Ivo Creusen62337e52018-01-09 14:17:33 +01002748 std::unique_ptr<AudioProcessing> apm(
2749 AudioProcessingBuilder().Create(old_config));
Ivo Creusenae026092017-11-20 13:07:16 +01002750 if (!apm) {
2751 return apm;
2752 }
2753
2754 ProcessingConfig processing_config = {
2755 {{32000, 1}, {32000, 1}, {32000, 1}, {32000, 1}}};
2756
2757 if (apm->Initialize(processing_config) != 0) {
2758 return nullptr;
2759 }
2760
2761 // Disable all components except for an AEC and the residual echo detector.
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02002762 AudioProcessing::Config apm_config;
2763 apm_config.residual_echo_detector.enabled = true;
2764 apm_config.high_pass_filter.enabled = false;
2765 apm_config.gain_controller2.enabled = false;
2766 apm_config.echo_canceller.enabled = true;
2767 apm_config.echo_canceller.mobile_mode = !use_AEC2;
2768 apm->ApplyConfig(apm_config);
Ivo Creusenae026092017-11-20 13:07:16 +01002769 EXPECT_EQ(apm->gain_control()->Enable(false), 0);
2770 EXPECT_EQ(apm->level_estimator()->Enable(false), 0);
2771 EXPECT_EQ(apm->noise_suppression()->Enable(false), 0);
2772 EXPECT_EQ(apm->voice_detection()->Enable(false), 0);
Ivo Creusenae026092017-11-20 13:07:16 +01002773 return apm;
2774}
2775
2776#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_MAC)
2777#define MAYBE_ApmStatistics DISABLED_ApmStatistics
2778#else
2779#define MAYBE_ApmStatistics ApmStatistics
2780#endif
2781
2782TEST(MAYBE_ApmStatistics, AEC2EnabledTest) {
2783 // Set up APM with AEC2 and process some audio.
2784 std::unique_ptr<AudioProcessing> apm = CreateApm(true);
2785 ASSERT_TRUE(apm);
2786
2787 // Set up an audioframe.
2788 AudioFrame frame;
2789 frame.num_channels_ = 1;
2790 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2791
2792 // Fill the audio frame with a sawtooth pattern.
2793 int16_t* ptr = frame.mutable_data();
2794 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2795 ptr[i] = 10000 * ((i % 3) - 1);
2796 }
2797
2798 // Do some processing.
2799 for (int i = 0; i < 200; i++) {
2800 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2801 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2802 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2803 }
2804
2805 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002806 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002807 // We expect all statistics to be set and have a sensible value.
2808 ASSERT_TRUE(stats.residual_echo_likelihood);
2809 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2810 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2811 ASSERT_TRUE(stats.residual_echo_likelihood_recent_max);
2812 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2813 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2814 ASSERT_TRUE(stats.echo_return_loss);
2815 EXPECT_NE(*stats.echo_return_loss, -100.0);
2816 ASSERT_TRUE(stats.echo_return_loss_enhancement);
2817 EXPECT_NE(*stats.echo_return_loss_enhancement, -100.0);
2818 ASSERT_TRUE(stats.divergent_filter_fraction);
2819 EXPECT_NE(*stats.divergent_filter_fraction, -1.0);
2820 ASSERT_TRUE(stats.delay_standard_deviation_ms);
2821 EXPECT_GE(*stats.delay_standard_deviation_ms, 0);
2822 // We don't check stats.delay_median_ms since it takes too long to settle to a
2823 // value. At least 20 seconds of data need to be processed before it will get
2824 // a value, which would make this test take too much time.
2825
2826 // If there are no receive streams, we expect the stats not to be set. The
2827 // 'false' argument signals to APM that no receive streams are currently
2828 // active. In that situation the statistics would get stuck at their last
2829 // calculated value (AEC and echo detection need at least one stream in each
2830 // direction), so to avoid that, they should not be set by APM.
2831 stats = apm->GetStatistics(false);
2832 EXPECT_FALSE(stats.residual_echo_likelihood);
2833 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2834 EXPECT_FALSE(stats.echo_return_loss);
2835 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2836 EXPECT_FALSE(stats.divergent_filter_fraction);
2837 EXPECT_FALSE(stats.delay_median_ms);
2838 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2839}
2840
2841TEST(MAYBE_ApmStatistics, AECMEnabledTest) {
2842 // Set up APM with AECM and process some audio.
2843 std::unique_ptr<AudioProcessing> apm = CreateApm(false);
2844 ASSERT_TRUE(apm);
2845
2846 // Set up an audioframe.
2847 AudioFrame frame;
2848 frame.num_channels_ = 1;
2849 SetFrameSampleRate(&frame, AudioProcessing::NativeRate::kSampleRate48kHz);
2850
2851 // Fill the audio frame with a sawtooth pattern.
2852 int16_t* ptr = frame.mutable_data();
2853 for (size_t i = 0; i < frame.kMaxDataSizeSamples; i++) {
2854 ptr[i] = 10000 * ((i % 3) - 1);
2855 }
2856
2857 // Do some processing.
2858 for (int i = 0; i < 200; i++) {
2859 EXPECT_EQ(apm->ProcessReverseStream(&frame), 0);
2860 EXPECT_EQ(apm->set_stream_delay_ms(0), 0);
2861 EXPECT_EQ(apm->ProcessStream(&frame), 0);
2862 }
2863
2864 // Test statistics interface.
Ivo Creusen56d46092017-11-24 17:29:59 +01002865 AudioProcessingStats stats = apm->GetStatistics(true);
Ivo Creusenae026092017-11-20 13:07:16 +01002866 // We expect only the residual echo detector statistics to be set and have a
2867 // sensible value.
2868 EXPECT_TRUE(stats.residual_echo_likelihood);
2869 if (stats.residual_echo_likelihood) {
2870 EXPECT_GE(*stats.residual_echo_likelihood, 0.0);
2871 EXPECT_LE(*stats.residual_echo_likelihood, 1.0);
2872 }
2873 EXPECT_TRUE(stats.residual_echo_likelihood_recent_max);
2874 if (stats.residual_echo_likelihood_recent_max) {
2875 EXPECT_GE(*stats.residual_echo_likelihood_recent_max, 0.0);
2876 EXPECT_LE(*stats.residual_echo_likelihood_recent_max, 1.0);
2877 }
2878 EXPECT_FALSE(stats.echo_return_loss);
2879 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2880 EXPECT_FALSE(stats.divergent_filter_fraction);
2881 EXPECT_FALSE(stats.delay_median_ms);
2882 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2883
2884 // If there are no receive streams, we expect the stats not to be set.
2885 stats = apm->GetStatistics(false);
2886 EXPECT_FALSE(stats.residual_echo_likelihood);
2887 EXPECT_FALSE(stats.residual_echo_likelihood_recent_max);
2888 EXPECT_FALSE(stats.echo_return_loss);
2889 EXPECT_FALSE(stats.echo_return_loss_enhancement);
2890 EXPECT_FALSE(stats.divergent_filter_fraction);
2891 EXPECT_FALSE(stats.delay_median_ms);
2892 EXPECT_FALSE(stats.delay_standard_deviation_ms);
2893}
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002894} // namespace webrtc