blob: b0c652654baf736f0a881bf70c1c055dcc82c704 [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 */
10
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000011#include <math.h>
ajm@google.com59e41402011-07-28 17:34:04 +000012#include <stdio.h>
kwiberg62eaacf2016-02-17 06:39:05 -080013
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000014#include <algorithm>
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000015#include <limits>
kwiberg62eaacf2016-02-17 06:39:05 -080016#include <memory>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000017#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000018
pkasting25702cb2016-01-08 13:50:27 -080019#include "webrtc/base/arraysize.h"
kwiberg9e2be5f2016-09-14 05:23:22 -070020#include "webrtc/base/checks.h"
kwiberg77eab702016-09-28 17:42:01 -070021#include "webrtc/base/ignore_wundef.h"
andrew@webrtc.org27c69802014-02-18 20:24:56 +000022#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000023#include "webrtc/common_audio/resampler/include/push_resampler.h"
24#include "webrtc/common_audio/resampler/push_sinc_resampler.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000025#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000026#include "webrtc/modules/audio_processing/beamformer/mock_nonlinear_beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000027#include "webrtc/modules/audio_processing/common.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000028#include "webrtc/modules/audio_processing/include/audio_processing.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070029#include "webrtc/modules/audio_processing/test/protobuf_utils.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000030#include "webrtc/modules/audio_processing/test/test_utils.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010031#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010032#include "webrtc/system_wrappers/include/event_wrapper.h"
33#include "webrtc/system_wrappers/include/trace.h"
kwiberg77eab702016-09-28 17:42:01 -070034#include "webrtc/test/gtest.h"
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000035#include "webrtc/test/testsupport/fileutils.h"
kwiberg77eab702016-09-28 17:42:01 -070036
37RTC_PUSH_IGNORING_WUNDEF()
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000038#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000039#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000040#else
kjellander78ddd732016-02-09 08:13:06 -080041#include "webrtc/modules/audio_processing/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000042#endif
kwiberg77eab702016-09-28 17:42:01 -070043RTC_POP_IGNORING_WUNDEF()
niklase@google.com470e71d2011-07-07 08:21:25 +000044
andrew@webrtc.org27c69802014-02-18 20:24:56 +000045namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000046namespace {
andrew@webrtc.org17e40642014-03-04 20:58:13 +000047
ekmeyerson60d9b332015-08-14 10:35:55 -070048// TODO(ekmeyerson): Switch to using StreamConfig and ProcessingConfig where
49// applicable.
50
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +000051// TODO(bjornv): This is not feasible until the functionality has been
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +000052// re-implemented; see comment at the bottom of this file. For now, the user has
53// to hard code the |write_ref_data| value.
ajm@google.com59e41402011-07-28 17:34:04 +000054// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000055// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000056// be set to true with the command-line switch --write_ref_data.
57bool write_ref_data = false;
pkasting25702cb2016-01-08 13:50:27 -080058const google::protobuf::int32 kChannels[] = {1, 2};
Alejandro Luebs47748742015-05-22 12:00:21 -070059const int kSampleRates[] = {8000, 16000, 32000, 48000};
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000060
aluebseb3603b2016-04-20 15:27:58 -070061#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
62// Android doesn't support 48kHz.
63const int kProcessSampleRates[] = {8000, 16000, 32000};
64#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
Alejandro Luebs47748742015-05-22 12:00:21 -070065const int kProcessSampleRates[] = {8000, 16000, 32000, 48000};
aluebseb3603b2016-04-20 15:27:58 -070066#endif
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000067
ekmeyerson60d9b332015-08-14 10:35:55 -070068enum StreamDirection { kForward = 0, kReverse };
69
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000070void ConvertToFloat(const int16_t* int_data, ChannelBuffer<float>* cb) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000071 ChannelBuffer<int16_t> cb_int(cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000072 cb->num_channels());
73 Deinterleave(int_data,
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000074 cb->num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000075 cb->num_channels(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000076 cb_int.channels());
Peter Kasting69558702016-01-12 16:26:35 -080077 for (size_t i = 0; i < cb->num_channels(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000078 S16ToFloat(cb_int.channels()[i],
79 cb->num_frames(),
80 cb->channels()[i]);
81 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +000082}
andrew@webrtc.org17e40642014-03-04 20:58:13 +000083
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000084void ConvertToFloat(const AudioFrame& frame, ChannelBuffer<float>* cb) {
85 ConvertToFloat(frame.data_, cb);
86}
87
andrew@webrtc.org103657b2014-04-24 18:28:56 +000088// Number of channels including the keyboard channel.
Peter Kasting69558702016-01-12 16:26:35 -080089size_t TotalChannelsFromLayout(AudioProcessing::ChannelLayout layout) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +000090 switch (layout) {
91 case AudioProcessing::kMono:
92 return 1;
93 case AudioProcessing::kMonoAndKeyboard:
94 case AudioProcessing::kStereo:
95 return 2;
96 case AudioProcessing::kStereoAndKeyboard:
97 return 3;
98 }
kwiberg9e2be5f2016-09-14 05:23:22 -070099 RTC_NOTREACHED();
pkasting25702cb2016-01-08 13:50:27 -0800100 return 0;
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000101}
102
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000103int TruncateToMultipleOf10(int value) {
104 return (value / 10) * 10;
105}
106
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000107void MixStereoToMono(const float* stereo, float* mono,
pkasting25702cb2016-01-08 13:50:27 -0800108 size_t samples_per_channel) {
109 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000110 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) / 2;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000111}
112
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000113void MixStereoToMono(const int16_t* stereo, int16_t* mono,
pkasting25702cb2016-01-08 13:50:27 -0800114 size_t samples_per_channel) {
115 for (size_t i = 0; i < samples_per_channel; ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000116 mono[i] = (stereo[i * 2] + stereo[i * 2 + 1]) >> 1;
117}
118
pkasting25702cb2016-01-08 13:50:27 -0800119void CopyLeftToRightChannel(int16_t* stereo, size_t samples_per_channel) {
120 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000121 stereo[i * 2 + 1] = stereo[i * 2];
122 }
123}
124
pkasting25702cb2016-01-08 13:50:27 -0800125void VerifyChannelsAreEqual(int16_t* stereo, size_t samples_per_channel) {
126 for (size_t i = 0; i < samples_per_channel; i++) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000127 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
128 }
129}
130
131void SetFrameTo(AudioFrame* frame, int16_t value) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700132 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
133 ++i) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000134 frame->data_[i] = value;
135 }
136}
137
138void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
Peter Kasting69558702016-01-12 16:26:35 -0800139 ASSERT_EQ(2u, frame->num_channels_);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700140 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000141 frame->data_[i] = left;
142 frame->data_[i + 1] = right;
143 }
144}
145
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000146void ScaleFrame(AudioFrame* frame, float scale) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 for (size_t i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
148 ++i) {
andrew@webrtc.org4fc4add2014-10-30 03:40:10 +0000149 frame->data_[i] = FloatS16ToS16(frame->data_[i] * scale);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000150 }
151}
152
andrew@webrtc.org81865342012-10-27 00:28:27 +0000153bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000154 if (frame1.samples_per_channel_ != frame2.samples_per_channel_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000155 return false;
156 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000157 if (frame1.num_channels_ != frame2.num_channels_) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000158 return false;
159 }
160 if (memcmp(frame1.data_, frame2.data_,
161 frame1.samples_per_channel_ * frame1.num_channels_ *
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000162 sizeof(int16_t))) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000163 return false;
164 }
165 return true;
166}
167
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000168void EnableAllAPComponents(AudioProcessing* ap) {
169#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
170 EXPECT_NOERR(ap->echo_control_mobile()->Enable(true));
171
172 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveDigital));
173 EXPECT_NOERR(ap->gain_control()->Enable(true));
174#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
175 EXPECT_NOERR(ap->echo_cancellation()->enable_drift_compensation(true));
176 EXPECT_NOERR(ap->echo_cancellation()->enable_metrics(true));
177 EXPECT_NOERR(ap->echo_cancellation()->enable_delay_logging(true));
178 EXPECT_NOERR(ap->echo_cancellation()->Enable(true));
179
180 EXPECT_NOERR(ap->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
181 EXPECT_NOERR(ap->gain_control()->set_analog_level_limits(0, 255));
182 EXPECT_NOERR(ap->gain_control()->Enable(true));
183#endif
184
185 EXPECT_NOERR(ap->high_pass_filter()->Enable(true));
186 EXPECT_NOERR(ap->level_estimator()->Enable(true));
187 EXPECT_NOERR(ap->noise_suppression()->Enable(true));
188
189 EXPECT_NOERR(ap->voice_detection()->Enable(true));
190}
191
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +0000192// These functions are only used by ApmTest.Process.
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000193template <class T>
194T AbsValue(T a) {
195 return a > 0 ? a: -a;
196}
197
198int16_t MaxAudioFrame(const AudioFrame& frame) {
pkasting25702cb2016-01-08 13:50:27 -0800199 const size_t length = frame.samples_per_channel_ * frame.num_channels_;
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000200 int16_t max_data = AbsValue(frame.data_[0]);
pkasting25702cb2016-01-08 13:50:27 -0800201 for (size_t i = 1; i < length; i++) {
andrew@webrtc.orgd7696c42013-12-03 23:39:16 +0000202 max_data = std::max(max_data, AbsValue(frame.data_[i]));
203 }
204
205 return max_data;
206}
207
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000208#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org81865342012-10-27 00:28:27 +0000209void TestStats(const AudioProcessing::Statistic& test,
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000210 const audioproc::Test::Statistic& reference) {
minyue58530ed2016-05-24 05:50:12 -0700211 EXPECT_EQ(reference.instant(), test.instant);
212 EXPECT_EQ(reference.average(), test.average);
213 EXPECT_EQ(reference.maximum(), test.maximum);
214 EXPECT_EQ(reference.minimum(), test.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000215}
216
217void WriteStatsMessage(const AudioProcessing::Statistic& output,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000218 audioproc::Test::Statistic* msg) {
219 msg->set_instant(output.instant);
220 msg->set_average(output.average);
221 msg->set_maximum(output.maximum);
222 msg->set_minimum(output.minimum);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000223}
fischman@webrtc.orgf8be8df2013-12-17 23:46:39 +0000224#endif
andrew@webrtc.org81865342012-10-27 00:28:27 +0000225
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000226void OpenFileAndWriteMessage(const std::string filename,
227 const ::google::protobuf::MessageLite& msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000228 FILE* file = fopen(filename.c_str(), "wb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000229 ASSERT_TRUE(file != NULL);
230
231 int32_t size = msg.ByteSize();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232 ASSERT_GT(size, 0);
kwiberg62eaacf2016-02-17 06:39:05 -0800233 std::unique_ptr<uint8_t[]> array(new uint8_t[size]);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000234 ASSERT_TRUE(msg.SerializeToArray(array.get(), size));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000235
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000236 ASSERT_EQ(1u, fwrite(&size, sizeof(size), 1, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000237 ASSERT_EQ(static_cast<size_t>(size),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000238 fwrite(array.get(), sizeof(array[0]), size, file));
andrew@webrtc.org81865342012-10-27 00:28:27 +0000239 fclose(file);
240}
241
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000242std::string ResourceFilePath(std::string name, int sample_rate_hz) {
243 std::ostringstream ss;
244 // Resource files are all stereo.
245 ss << name << sample_rate_hz / 1000 << "_stereo";
246 return test::ResourcePath(ss.str(), "pcm");
247}
248
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000249// Temporary filenames unique to this process. Used to be able to run these
250// tests in parallel as each process needs to be running in isolation they can't
251// have competing filenames.
252std::map<std::string, std::string> temp_filenames;
253
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254std::string OutputFilePath(std::string name,
andrew@webrtc.orgf26c9e82014-04-24 03:46:46 +0000255 int input_rate,
256 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -0700257 int reverse_input_rate,
258 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800259 size_t num_input_channels,
260 size_t num_output_channels,
261 size_t num_reverse_input_channels,
262 size_t num_reverse_output_channels,
ekmeyerson60d9b332015-08-14 10:35:55 -0700263 StreamDirection file_direction) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000264 std::ostringstream ss;
ekmeyerson60d9b332015-08-14 10:35:55 -0700265 ss << name << "_i" << num_input_channels << "_" << input_rate / 1000 << "_ir"
266 << num_reverse_input_channels << "_" << reverse_input_rate / 1000 << "_";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000267 if (num_output_channels == 1) {
268 ss << "mono";
269 } else if (num_output_channels == 2) {
270 ss << "stereo";
271 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700272 RTC_NOTREACHED();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000273 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700274 ss << output_rate / 1000;
275 if (num_reverse_output_channels == 1) {
276 ss << "_rmono";
277 } else if (num_reverse_output_channels == 2) {
278 ss << "_rstereo";
279 } else {
kwiberg9e2be5f2016-09-14 05:23:22 -0700280 RTC_NOTREACHED();
ekmeyerson60d9b332015-08-14 10:35:55 -0700281 }
282 ss << reverse_output_rate / 1000;
283 ss << "_d" << file_direction << "_pcm";
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000284
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000285 std::string filename = ss.str();
pbosbb36fdf2015-07-09 07:48:14 -0700286 if (temp_filenames[filename].empty())
pbos@webrtc.orga525c982015-01-12 17:31:18 +0000287 temp_filenames[filename] = test::TempFilename(test::OutputPath(), filename);
288 return temp_filenames[filename];
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289}
290
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000291void ClearTempFiles() {
292 for (auto& kv : temp_filenames)
293 remove(kv.second.c_str());
294}
295
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000296void OpenFileAndReadMessage(const std::string filename,
297 ::google::protobuf::MessageLite* msg) {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000298 FILE* file = fopen(filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000299 ASSERT_TRUE(file != NULL);
300 ReadMessageFromFile(file, msg);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000301 fclose(file);
302}
303
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000304// Reads a 10 ms chunk of int16 interleaved audio from the given (assumed
305// stereo) file, converts to deinterleaved float (optionally downmixing) and
306// returns the result in |cb|. Returns false if the file ended (or on error) and
307// true otherwise.
308//
309// |int_data| and |float_data| are just temporary space that must be
310// sufficiently large to hold the 10 ms chunk.
311bool ReadChunk(FILE* file, int16_t* int_data, float* float_data,
312 ChannelBuffer<float>* cb) {
313 // The files always contain stereo audio.
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000314 size_t frame_size = cb->num_frames() * 2;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000315 size_t read_count = fread(int_data, sizeof(int16_t), frame_size, file);
316 if (read_count != frame_size) {
317 // Check that the file really ended.
kwiberg9e2be5f2016-09-14 05:23:22 -0700318 RTC_DCHECK(feof(file));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000319 return false; // This is expected.
320 }
321
322 S16ToFloat(int_data, frame_size, float_data);
323 if (cb->num_channels() == 1) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000324 MixStereoToMono(float_data, cb->channels()[0], cb->num_frames());
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000325 } else {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000326 Deinterleave(float_data, cb->num_frames(), 2,
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000327 cb->channels());
328 }
329
330 return true;
331}
332
niklase@google.com470e71d2011-07-07 08:21:25 +0000333class ApmTest : public ::testing::Test {
334 protected:
335 ApmTest();
336 virtual void SetUp();
337 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000338
339 static void SetUpTestCase() {
340 Trace::CreateTrace();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000341 }
342
343 static void TearDownTestCase() {
344 Trace::ReturnTrace();
pbos@webrtc.org200ac002015-02-03 14:14:01 +0000345 ClearTempFiles();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000346 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000347
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000348 // Used to select between int and float interface tests.
349 enum Format {
350 kIntFormat,
351 kFloatFormat
352 };
353
354 void Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000355 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000356 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800357 size_t num_input_channels,
358 size_t num_output_channels,
359 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000360 bool open_output_file);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000361 void Init(AudioProcessing* ap);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000362 void EnableAllComponents();
363 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000364 bool ReadFrame(FILE* file, AudioFrame* frame, ChannelBuffer<float>* cb);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000365 void ReadFrameWithRewind(FILE* file, AudioFrame* frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000366 void ReadFrameWithRewind(FILE* file, AudioFrame* frame,
367 ChannelBuffer<float>* cb);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000368 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000369 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
370 int delay_min, int delay_max);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700371 void TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800372 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700373 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800374 void TestChangingForwardChannels(size_t num_in_channels,
375 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700376 AudioProcessing::Error expected_return);
Peter Kasting69558702016-01-12 16:26:35 -0800377 void TestChangingReverseChannels(size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700378 AudioProcessing::Error expected_return);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000379 void RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate);
380 void RunManualVolumeChangeIsPossibleTest(int sample_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000381 void StreamParametersTest(Format format);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000382 int ProcessStreamChooser(Format format);
383 int AnalyzeReverseStreamChooser(Format format);
384 void ProcessDebugDump(const std::string& in_filename,
385 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -0800386 Format format,
387 int max_size_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000388 void VerifyDebugDumpTest(Format format);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000389
390 const std::string output_path_;
391 const std::string ref_path_;
392 const std::string ref_filename_;
kwiberg62eaacf2016-02-17 06:39:05 -0800393 std::unique_ptr<AudioProcessing> apm_;
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000394 AudioFrame* frame_;
395 AudioFrame* revframe_;
kwiberg62eaacf2016-02-17 06:39:05 -0800396 std::unique_ptr<ChannelBuffer<float> > float_cb_;
397 std::unique_ptr<ChannelBuffer<float> > revfloat_cb_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000398 int output_sample_rate_hz_;
Peter Kasting69558702016-01-12 16:26:35 -0800399 size_t num_output_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 FILE* far_file_;
401 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000402 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000403};
404
405ApmTest::ApmTest()
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000406 : output_path_(test::OutputPath()),
407 ref_path_(test::ProjectRootPath() + "data/audio_processing/"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000408#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000409 ref_filename_(ref_path_ + "output_data_fixed.pb"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000410#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000411#if defined(WEBRTC_MAC)
412 // A different file for Mac is needed because on this platform the AEC
413 // constant |kFixedDelayMs| value is 20 and not 50 as it is on the rest.
414 ref_filename_(ref_path_ + "output_data_mac.pb"),
415#else
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000416 ref_filename_(ref_path_ + "output_data_float.pb"),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000417#endif
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +0000418#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000419 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000420 revframe_(NULL),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000421 output_sample_rate_hz_(0),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000422 num_output_channels_(0),
ajm@google.com22e65152011-07-18 18:03:01 +0000423 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000424 near_file_(NULL),
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +0000425 out_file_(NULL) {
426 Config config;
427 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
428 apm_.reset(AudioProcessing::Create(config));
429}
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
431void ApmTest::SetUp() {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000432 ASSERT_TRUE(apm_.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
434 frame_ = new AudioFrame();
435 revframe_ = new AudioFrame();
436
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000437 Init(32000, 32000, 32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000438}
439
440void ApmTest::TearDown() {
441 if (frame_) {
442 delete frame_;
443 }
444 frame_ = NULL;
445
446 if (revframe_) {
447 delete revframe_;
448 }
449 revframe_ = NULL;
450
451 if (far_file_) {
452 ASSERT_EQ(0, fclose(far_file_));
453 }
454 far_file_ = NULL;
455
456 if (near_file_) {
457 ASSERT_EQ(0, fclose(near_file_));
458 }
459 near_file_ = NULL;
460
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000461 if (out_file_) {
462 ASSERT_EQ(0, fclose(out_file_));
463 }
464 out_file_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000467void ApmTest::Init(AudioProcessing* ap) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000468 ASSERT_EQ(kNoErr,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700469 ap->Initialize(
470 {{{frame_->sample_rate_hz_, frame_->num_channels_},
471 {output_sample_rate_hz_, num_output_channels_},
ekmeyerson60d9b332015-08-14 10:35:55 -0700472 {revframe_->sample_rate_hz_, revframe_->num_channels_},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700473 {revframe_->sample_rate_hz_, revframe_->num_channels_}}}));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000474}
475
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000476void ApmTest::Init(int sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000478 int reverse_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -0800479 size_t num_input_channels,
480 size_t num_output_channels,
481 size_t num_reverse_channels,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000482 bool open_output_file) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000483 SetContainerFormat(sample_rate_hz, num_input_channels, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000484 output_sample_rate_hz_ = output_sample_rate_hz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000485 num_output_channels_ = num_output_channels;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000486
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000487 SetContainerFormat(reverse_sample_rate_hz, num_reverse_channels, revframe_,
488 &revfloat_cb_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000489 Init(apm_.get());
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000490
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000491 if (far_file_) {
492 ASSERT_EQ(0, fclose(far_file_));
493 }
494 std::string filename = ResourceFilePath("far", sample_rate_hz);
495 far_file_ = fopen(filename.c_str(), "rb");
496 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
497 filename << "\n";
498
499 if (near_file_) {
500 ASSERT_EQ(0, fclose(near_file_));
501 }
502 filename = ResourceFilePath("near", sample_rate_hz);
503 near_file_ = fopen(filename.c_str(), "rb");
504 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
505 filename << "\n";
506
507 if (open_output_file) {
508 if (out_file_) {
509 ASSERT_EQ(0, fclose(out_file_));
510 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700511 filename = OutputFilePath(
512 "out", sample_rate_hz, output_sample_rate_hz, reverse_sample_rate_hz,
513 reverse_sample_rate_hz, num_input_channels, num_output_channels,
514 num_reverse_channels, num_reverse_channels, kForward);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000515 out_file_ = fopen(filename.c_str(), "wb");
516 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
517 filename << "\n";
518 }
519}
520
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000521void ApmTest::EnableAllComponents() {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000522 EnableAllAPComponents(apm_.get());
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000523}
524
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000525bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame,
526 ChannelBuffer<float>* cb) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000527 // The files always contain stereo audio.
528 size_t frame_size = frame->samples_per_channel_ * 2;
529 size_t read_count = fread(frame->data_,
530 sizeof(int16_t),
531 frame_size,
532 file);
533 if (read_count != frame_size) {
534 // Check that the file really ended.
535 EXPECT_NE(0, feof(file));
536 return false; // This is expected.
537 }
538
539 if (frame->num_channels_ == 1) {
540 MixStereoToMono(frame->data_, frame->data_,
541 frame->samples_per_channel_);
542 }
543
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000544 if (cb) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000545 ConvertToFloat(*frame, cb);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000546 }
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000547 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000548}
549
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000550bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
551 return ReadFrame(file, frame, NULL);
552}
553
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000554// If the end of the file has been reached, rewind it and attempt to read the
555// frame again.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame,
557 ChannelBuffer<float>* cb) {
558 if (!ReadFrame(near_file_, frame_, cb)) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000559 rewind(near_file_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560 ASSERT_TRUE(ReadFrame(near_file_, frame_, cb));
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000561 }
562}
563
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000564void ApmTest::ReadFrameWithRewind(FILE* file, AudioFrame* frame) {
565 ReadFrameWithRewind(file, frame, NULL);
566}
567
andrew@webrtc.org81865342012-10-27 00:28:27 +0000568void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
569 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000570 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000571 EXPECT_EQ(apm_->kNoError,
572 apm_->gain_control()->set_stream_analog_level(127));
573 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000574}
575
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000576int ApmTest::ProcessStreamChooser(Format format) {
577 if (format == kIntFormat) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000578 return apm_->ProcessStream(frame_);
579 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580 return apm_->ProcessStream(float_cb_->channels(),
581 frame_->samples_per_channel_,
582 frame_->sample_rate_hz_,
583 LayoutFromChannels(frame_->num_channels_),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000584 output_sample_rate_hz_,
585 LayoutFromChannels(num_output_channels_),
586 float_cb_->channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000587}
588
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000589int ApmTest::AnalyzeReverseStreamChooser(Format format) {
590 if (format == kIntFormat) {
aluebsb0319552016-03-17 20:39:53 -0700591 return apm_->ProcessReverseStream(revframe_);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 return apm_->AnalyzeReverseStream(
594 revfloat_cb_->channels(),
595 revframe_->samples_per_channel_,
596 revframe_->sample_rate_hz_,
597 LayoutFromChannels(revframe_->num_channels_));
598}
599
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000600void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
601 int delay_min, int delay_max) {
602 // The |revframe_| and |frame_| should include the proper frame information,
603 // hence can be used for extracting information.
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000604 AudioFrame tmp_frame;
605 std::queue<AudioFrame*> frame_queue;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000606 bool causal = true;
607
608 tmp_frame.CopyFrom(*revframe_);
609 SetFrameTo(&tmp_frame, 0);
610
611 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
612 // Initialize the |frame_queue| with empty frames.
613 int frame_delay = delay_ms / 10;
614 while (frame_delay < 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000615 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000616 frame->CopyFrom(tmp_frame);
617 frame_queue.push(frame);
618 frame_delay++;
619 causal = false;
620 }
621 while (frame_delay > 0) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000622 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000623 frame->CopyFrom(tmp_frame);
624 frame_queue.push(frame);
625 frame_delay--;
626 }
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000627 // Run for 4.5 seconds, skipping statistics from the first 2.5 seconds. We
628 // need enough frames with audio to have reliable estimates, but as few as
629 // possible to keep processing time down. 4.5 seconds seemed to be a good
630 // compromise for this recording.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000631 for (int frame_count = 0; frame_count < 450; ++frame_count) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000632 AudioFrame* frame = new AudioFrame();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000633 frame->CopyFrom(tmp_frame);
634 // Use the near end recording, since that has more speech in it.
635 ASSERT_TRUE(ReadFrame(near_file_, frame));
636 frame_queue.push(frame);
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000637 AudioFrame* reverse_frame = frame;
638 AudioFrame* process_frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000639 if (!causal) {
640 reverse_frame = frame_queue.front();
641 // When we call ProcessStream() the frame is modified, so we can't use the
642 // pointer directly when things are non-causal. Use an intermediate frame
643 // and copy the data.
644 process_frame = &tmp_frame;
645 process_frame->CopyFrom(*frame);
646 }
aluebsb0319552016-03-17 20:39:53 -0700647 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(reverse_frame));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000648 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
649 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
650 frame = frame_queue.front();
651 frame_queue.pop();
652 delete frame;
653
bjornv@webrtc.orgbbd47fc2014-01-13 08:54:34 +0000654 if (frame_count == 250) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000655 int median;
656 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000657 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000658 // Discard the first delay metrics to avoid convergence effects.
659 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000660 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
661 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000662 }
663 }
664
665 rewind(near_file_);
666 while (!frame_queue.empty()) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000667 AudioFrame* frame = frame_queue.front();
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000668 frame_queue.pop();
669 delete frame;
670 }
671 // Calculate expected delay estimate and acceptable regions. Further,
672 // limit them w.r.t. AEC delay estimation support.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700673 const size_t samples_per_ms =
674 std::min(static_cast<size_t>(16), frame_->samples_per_channel_ / 10);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000675 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
676 delay_min), delay_max);
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 int expected_median_high = std::min(
678 std::max(expected_median + static_cast<int>(96 / samples_per_ms),
679 delay_min),
680 delay_max);
681 int expected_median_low = std::min(
682 std::max(expected_median - static_cast<int>(96 / samples_per_ms),
683 delay_min),
684 delay_max);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000685 // Verify delay metrics.
686 int median;
687 int std;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000688 float poor_fraction;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000689 EXPECT_EQ(apm_->kNoError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000690 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
691 &poor_fraction));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000692 EXPECT_GE(expected_median_high, median);
693 EXPECT_LE(expected_median_low, median);
694}
695
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000696void ApmTest::StreamParametersTest(Format format) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000697 // No errors when the components are disabled.
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000698 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000699
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000700 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000701 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000702 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000703 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000704
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000705 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000706 EXPECT_EQ(apm_->kNoError,
707 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000708 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000709 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000710 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000711
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 // Other stream parameters set correctly.
713 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 EXPECT_EQ(apm_->kNoError,
715 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000716 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000717 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000718 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000720 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
721 EXPECT_EQ(apm_->kNoError,
722 apm_->echo_cancellation()->enable_drift_compensation(false));
723
724 // -- Missing delay --
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000725 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000726 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000727 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000728 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000729
730 // Resets after successful ProcessStream().
731 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000732 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000733 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000734 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000735
736 // Other stream parameters set correctly.
737 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
738 EXPECT_EQ(apm_->kNoError,
739 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000740 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000741 EXPECT_EQ(apm_->kNoError,
742 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000743 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000744 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000745 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
746
747 // -- Missing drift --
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000748 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000749 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000750
751 // Resets after successful ProcessStream().
752 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000753 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000754 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000755 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000756 ProcessStreamChooser(format));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000757
758 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000759 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
760 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
761 EXPECT_EQ(apm_->kNoError,
762 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000763 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000764 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000765
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000766 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000768 AnalyzeReverseStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 EXPECT_EQ(apm_->kStreamParameterNotSetError,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000770 ProcessStreamChooser(format));
niklase@google.com470e71d2011-07-07 08:21:25 +0000771
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000772 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000774 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000775 EXPECT_EQ(apm_->kNoError,
776 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000777 EXPECT_EQ(apm_->kNoError, ProcessStreamChooser(format));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000778}
779
780TEST_F(ApmTest, StreamParametersInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000781 StreamParametersTest(kIntFormat);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000782}
783
784TEST_F(ApmTest, StreamParametersFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000785 StreamParametersTest(kFloatFormat);
niklase@google.com470e71d2011-07-07 08:21:25 +0000786}
787
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000788TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
789 EXPECT_EQ(0, apm_->delay_offset_ms());
790 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
791 EXPECT_EQ(50, apm_->stream_delay_ms());
792}
793
794TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
795 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000796 apm_->set_delay_offset_ms(100);
797 EXPECT_EQ(100, apm_->delay_offset_ms());
798 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000799 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000800 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
801 EXPECT_EQ(200, apm_->stream_delay_ms());
802
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000803 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000804 apm_->set_delay_offset_ms(-50);
805 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000806 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
807 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000808 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
809 EXPECT_EQ(50, apm_->stream_delay_ms());
810}
811
Michael Graczyk86c6d332015-07-23 11:41:39 -0700812void ApmTest::TestChangingChannelsInt16Interface(
Peter Kasting69558702016-01-12 16:26:35 -0800813 size_t num_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814 AudioProcessing::Error expected_return) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000815 frame_->num_channels_ = num_channels;
816 EXPECT_EQ(expected_return, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -0700817 EXPECT_EQ(expected_return, apm_->ProcessReverseStream(frame_));
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000818}
819
Michael Graczyk86c6d332015-07-23 11:41:39 -0700820void ApmTest::TestChangingForwardChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800821 size_t num_in_channels,
822 size_t num_out_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700823 AudioProcessing::Error expected_return) {
824 const StreamConfig input_stream = {frame_->sample_rate_hz_, num_in_channels};
825 const StreamConfig output_stream = {output_sample_rate_hz_, num_out_channels};
826
827 EXPECT_EQ(expected_return,
828 apm_->ProcessStream(float_cb_->channels(), input_stream,
829 output_stream, float_cb_->channels()));
830}
831
832void ApmTest::TestChangingReverseChannels(
Peter Kasting69558702016-01-12 16:26:35 -0800833 size_t num_rev_channels,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700834 AudioProcessing::Error expected_return) {
835 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700836 {{frame_->sample_rate_hz_, apm_->num_input_channels()},
837 {output_sample_rate_hz_, apm_->num_output_channels()},
838 {frame_->sample_rate_hz_, num_rev_channels},
839 {frame_->sample_rate_hz_, num_rev_channels}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700840
ekmeyerson60d9b332015-08-14 10:35:55 -0700841 EXPECT_EQ(
842 expected_return,
843 apm_->ProcessReverseStream(
844 float_cb_->channels(), processing_config.reverse_input_stream(),
845 processing_config.reverse_output_stream(), float_cb_->channels()));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700846}
847
848TEST_F(ApmTest, ChannelsInt16Interface) {
849 // Testing number of invalid and valid channels.
850 Init(16000, 16000, 16000, 4, 4, 4, false);
851
852 TestChangingChannelsInt16Interface(0, apm_->kBadNumberChannelsError);
853
Peter Kasting69558702016-01-12 16:26:35 -0800854 for (size_t i = 1; i < 4; i++) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700855 TestChangingChannelsInt16Interface(i, kNoErr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000856 EXPECT_EQ(i, apm_->num_input_channels());
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000857 // We always force the number of reverse channels used for processing to 1.
Peter Kasting69558702016-01-12 16:26:35 -0800858 EXPECT_EQ(1u, apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000859 }
860}
861
Michael Graczyk86c6d332015-07-23 11:41:39 -0700862TEST_F(ApmTest, Channels) {
863 // Testing number of invalid and valid channels.
864 Init(16000, 16000, 16000, 4, 4, 4, false);
865
866 TestChangingForwardChannels(0, 1, apm_->kBadNumberChannelsError);
867 TestChangingReverseChannels(0, apm_->kBadNumberChannelsError);
868
Peter Kasting69558702016-01-12 16:26:35 -0800869 for (size_t i = 1; i < 4; ++i) {
870 for (size_t j = 0; j < 1; ++j) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700871 // Output channels much be one or match input channels.
872 if (j == 1 || i == j) {
873 TestChangingForwardChannels(i, j, kNoErr);
874 TestChangingReverseChannels(i, kNoErr);
875
876 EXPECT_EQ(i, apm_->num_input_channels());
877 EXPECT_EQ(j, apm_->num_output_channels());
878 // The number of reverse channels used for processing to is always 1.
Peter Kasting69558702016-01-12 16:26:35 -0800879 EXPECT_EQ(1u, apm_->num_reverse_channels());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700880 } else {
881 TestChangingForwardChannels(i, j,
882 AudioProcessing::kBadNumberChannelsError);
883 }
884 }
885 }
886}
887
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000888TEST_F(ApmTest, SampleRatesInt) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000889 // Testing invalid sample rates
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000890 SetContainerFormat(10000, 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000891 EXPECT_EQ(apm_->kBadSampleRateError, ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 // Testing valid sample rates
Alejandro Luebs47748742015-05-22 12:00:21 -0700893 int fs[] = {8000, 16000, 32000, 48000};
pkasting25702cb2016-01-08 13:50:27 -0800894 for (size_t i = 0; i < arraysize(fs); i++) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000895 SetContainerFormat(fs[i], 2, frame_, &float_cb_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000896 EXPECT_NOERR(ProcessStreamChooser(kIntFormat));
niklase@google.com470e71d2011-07-07 08:21:25 +0000897 }
898}
899
niklase@google.com470e71d2011-07-07 08:21:25 +0000900TEST_F(ApmTest, EchoCancellation) {
901 EXPECT_EQ(apm_->kNoError,
902 apm_->echo_cancellation()->enable_drift_compensation(true));
903 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
904 EXPECT_EQ(apm_->kNoError,
905 apm_->echo_cancellation()->enable_drift_compensation(false));
906 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
907
niklase@google.com470e71d2011-07-07 08:21:25 +0000908 EchoCancellation::SuppressionLevel level[] = {
909 EchoCancellation::kLowSuppression,
910 EchoCancellation::kModerateSuppression,
911 EchoCancellation::kHighSuppression,
912 };
pkasting25702cb2016-01-08 13:50:27 -0800913 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 EXPECT_EQ(apm_->kNoError,
915 apm_->echo_cancellation()->set_suppression_level(level[i]));
916 EXPECT_EQ(level[i],
917 apm_->echo_cancellation()->suppression_level());
918 }
919
920 EchoCancellation::Metrics metrics;
921 EXPECT_EQ(apm_->kNotEnabledError,
922 apm_->echo_cancellation()->GetMetrics(&metrics));
923
924 EXPECT_EQ(apm_->kNoError,
925 apm_->echo_cancellation()->enable_metrics(true));
926 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
927 EXPECT_EQ(apm_->kNoError,
928 apm_->echo_cancellation()->enable_metrics(false));
929 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
930
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000931 int median = 0;
932 int std = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000933 float poor_fraction = 0;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000934 EXPECT_EQ(apm_->kNotEnabledError,
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000935 apm_->echo_cancellation()->GetDelayMetrics(&median, &std,
936 &poor_fraction));
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000937
938 EXPECT_EQ(apm_->kNoError,
939 apm_->echo_cancellation()->enable_delay_logging(true));
940 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
941 EXPECT_EQ(apm_->kNoError,
942 apm_->echo_cancellation()->enable_delay_logging(false));
943 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
944
niklase@google.com470e71d2011-07-07 08:21:25 +0000945 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
946 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
947 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
948 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000949
950 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
951 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
952 EXPECT_TRUE(apm_->echo_cancellation()->aec_core() != NULL);
953 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
954 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
955 EXPECT_FALSE(apm_->echo_cancellation()->aec_core() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000956}
957
bjornv@webrtc.org84f8ec12014-06-19 12:14:33 +0000958TEST_F(ApmTest, DISABLED_EchoCancellationReportsCorrectDelays) {
bjornv@webrtc.orgbac00122015-01-02 09:23:49 +0000959 // TODO(bjornv): Fix this test to work with DA-AEC.
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000960 // Enable AEC only.
961 EXPECT_EQ(apm_->kNoError,
962 apm_->echo_cancellation()->enable_drift_compensation(false));
963 EXPECT_EQ(apm_->kNoError,
964 apm_->echo_cancellation()->enable_metrics(false));
965 EXPECT_EQ(apm_->kNoError,
966 apm_->echo_cancellation()->enable_delay_logging(true));
967 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000968 Config config;
henrik.lundin0f133b92015-07-02 00:17:55 -0700969 config.Set<DelayAgnostic>(new DelayAgnostic(false));
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000970 apm_->SetExtraOptions(config);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000971
972 // Internally in the AEC the amount of lookahead the delay estimation can
973 // handle is 15 blocks and the maximum delay is set to 60 blocks.
974 const int kLookaheadBlocks = 15;
975 const int kMaxDelayBlocks = 60;
976 // The AEC has a startup time before it actually starts to process. This
977 // procedure can flush the internal far-end buffer, which of course affects
978 // the delay estimation. Therefore, we set a system_delay high enough to
979 // avoid that. The smallest system_delay you can report without flushing the
980 // buffer is 66 ms in 8 kHz.
981 //
982 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
983 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
984 // delay estimation. This should be noted though. In case of test failure,
985 // this could be the cause.
986 const int kSystemDelayMs = 66;
987 // Test a couple of corner cases and verify that the estimated delay is
988 // within a valid region (set to +-1.5 blocks). Note that these cases are
989 // sampling frequency dependent.
pkasting25702cb2016-01-08 13:50:27 -0800990 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000991 Init(kProcessSampleRates[i],
992 kProcessSampleRates[i],
993 kProcessSampleRates[i],
994 2,
995 2,
996 2,
997 false);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000998 // Sampling frequency dependent variables.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700999 const int num_ms_per_block =
1000 std::max(4, static_cast<int>(640 / frame_->samples_per_channel_));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001001 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
1002 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
1003
1004 // 1) Verify correct delay estimate at lookahead boundary.
1005 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
1006 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1007 delay_max_ms);
1008 // 2) A delay less than maximum lookahead should give an delay estimate at
1009 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
1010 delay_ms -= 20;
1011 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1012 delay_max_ms);
1013 // 3) Three values around zero delay. Note that we need to compensate for
1014 // the fake system_delay.
1015 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
1016 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1017 delay_max_ms);
1018 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
1019 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1020 delay_max_ms);
1021 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
1022 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1023 delay_max_ms);
1024 // 4) Verify correct delay estimate at maximum delay boundary.
1025 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
1026 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1027 delay_max_ms);
1028 // 5) A delay above the maximum delay should give an estimate at the
1029 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
1030 delay_ms += 20;
1031 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
1032 delay_max_ms);
1033 }
1034}
1035
niklase@google.com470e71d2011-07-07 08:21:25 +00001036TEST_F(ApmTest, EchoControlMobile) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001037 // Turn AECM on (and AEC off)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001038 Init(16000, 16000, 16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001039 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
1040 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
1041
niklase@google.com470e71d2011-07-07 08:21:25 +00001042 // Toggle routing modes
1043 EchoControlMobile::RoutingMode mode[] = {
1044 EchoControlMobile::kQuietEarpieceOrHeadset,
1045 EchoControlMobile::kEarpiece,
1046 EchoControlMobile::kLoudEarpiece,
1047 EchoControlMobile::kSpeakerphone,
1048 EchoControlMobile::kLoudSpeakerphone,
1049 };
pkasting25702cb2016-01-08 13:50:27 -08001050 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 EXPECT_EQ(apm_->kNoError,
1052 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
1053 EXPECT_EQ(mode[i],
1054 apm_->echo_control_mobile()->routing_mode());
1055 }
1056 // Turn comfort noise off/on
1057 EXPECT_EQ(apm_->kNoError,
1058 apm_->echo_control_mobile()->enable_comfort_noise(false));
1059 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
1060 EXPECT_EQ(apm_->kNoError,
1061 apm_->echo_control_mobile()->enable_comfort_noise(true));
1062 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001063 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +00001064 const size_t echo_path_size =
1065 apm_->echo_control_mobile()->echo_path_size_bytes();
kwiberg62eaacf2016-02-17 06:39:05 -08001066 std::unique_ptr<char[]> echo_path_in(new char[echo_path_size]);
1067 std::unique_ptr<char[]> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001068 EXPECT_EQ(apm_->kNullPointerError,
1069 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
1070 EXPECT_EQ(apm_->kNullPointerError,
1071 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
1072 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001073 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001074 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001075 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001076 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001077 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001078 echo_path_in[i] = echo_path_out[i] + 1;
1079 }
1080 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001081 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001082 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001083 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
1084 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001085 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +00001086 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
1087 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +00001088 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +00001089 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
1090 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001091
1092 // Process a few frames with NS in the default disabled state. This exercises
1093 // a different codepath than with it enabled.
1094 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1095 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1096 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1097 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1098
niklase@google.com470e71d2011-07-07 08:21:25 +00001099 // Turn AECM off
1100 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
1101 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1102}
1103
aluebs@webrtc.orgc9ee4122014-02-03 14:41:57 +00001104TEST_F(ApmTest, GainControl) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001105 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +00001106 EXPECT_EQ(apm_->kNoError,
1107 apm_->gain_control()->set_mode(
1108 apm_->gain_control()->mode()));
1109
1110 GainControl::Mode mode[] = {
1111 GainControl::kAdaptiveAnalog,
1112 GainControl::kAdaptiveDigital,
1113 GainControl::kFixedDigital
1114 };
pkasting25702cb2016-01-08 13:50:27 -08001115 for (size_t i = 0; i < arraysize(mode); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001116 EXPECT_EQ(apm_->kNoError,
1117 apm_->gain_control()->set_mode(mode[i]));
1118 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
1119 }
1120 // Testing invalid target levels
1121 EXPECT_EQ(apm_->kBadParameterError,
1122 apm_->gain_control()->set_target_level_dbfs(-3));
1123 EXPECT_EQ(apm_->kBadParameterError,
1124 apm_->gain_control()->set_target_level_dbfs(-40));
1125 // Testing valid target levels
1126 EXPECT_EQ(apm_->kNoError,
1127 apm_->gain_control()->set_target_level_dbfs(
1128 apm_->gain_control()->target_level_dbfs()));
1129
1130 int level_dbfs[] = {0, 6, 31};
pkasting25702cb2016-01-08 13:50:27 -08001131 for (size_t i = 0; i < arraysize(level_dbfs); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001132 EXPECT_EQ(apm_->kNoError,
1133 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
1134 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
1135 }
1136
1137 // Testing invalid compression gains
1138 EXPECT_EQ(apm_->kBadParameterError,
1139 apm_->gain_control()->set_compression_gain_db(-1));
1140 EXPECT_EQ(apm_->kBadParameterError,
1141 apm_->gain_control()->set_compression_gain_db(100));
1142
1143 // Testing valid compression gains
1144 EXPECT_EQ(apm_->kNoError,
1145 apm_->gain_control()->set_compression_gain_db(
1146 apm_->gain_control()->compression_gain_db()));
1147
1148 int gain_db[] = {0, 10, 90};
pkasting25702cb2016-01-08 13:50:27 -08001149 for (size_t i = 0; i < arraysize(gain_db); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001150 EXPECT_EQ(apm_->kNoError,
1151 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1152 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1153 }
1154
1155 // Testing limiter off/on
1156 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1157 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1158 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1159 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1160
1161 // Testing invalid level limits
1162 EXPECT_EQ(apm_->kBadParameterError,
1163 apm_->gain_control()->set_analog_level_limits(-1, 512));
1164 EXPECT_EQ(apm_->kBadParameterError,
1165 apm_->gain_control()->set_analog_level_limits(100000, 512));
1166 EXPECT_EQ(apm_->kBadParameterError,
1167 apm_->gain_control()->set_analog_level_limits(512, -1));
1168 EXPECT_EQ(apm_->kBadParameterError,
1169 apm_->gain_control()->set_analog_level_limits(512, 100000));
1170 EXPECT_EQ(apm_->kBadParameterError,
1171 apm_->gain_control()->set_analog_level_limits(512, 255));
1172
1173 // Testing valid level limits
1174 EXPECT_EQ(apm_->kNoError,
1175 apm_->gain_control()->set_analog_level_limits(
1176 apm_->gain_control()->analog_level_minimum(),
1177 apm_->gain_control()->analog_level_maximum()));
1178
1179 int min_level[] = {0, 255, 1024};
pkasting25702cb2016-01-08 13:50:27 -08001180 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001181 EXPECT_EQ(apm_->kNoError,
1182 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1183 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1184 }
1185
1186 int max_level[] = {0, 1024, 65535};
pkasting25702cb2016-01-08 13:50:27 -08001187 for (size_t i = 0; i < arraysize(min_level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001188 EXPECT_EQ(apm_->kNoError,
1189 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1190 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1191 }
1192
1193 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1194
1195 // Turn AGC off
1196 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1197 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1198}
1199
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001200void ApmTest::RunQuantizedVolumeDoesNotGetStuckTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001201 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001202 EXPECT_EQ(apm_->kNoError,
1203 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1204 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1205
1206 int out_analog_level = 0;
1207 for (int i = 0; i < 2000; ++i) {
1208 ReadFrameWithRewind(near_file_, frame_);
1209 // Ensure the audio is at a low level, so the AGC will try to increase it.
1210 ScaleFrame(frame_, 0.25);
1211
1212 // Always pass in the same volume.
1213 EXPECT_EQ(apm_->kNoError,
1214 apm_->gain_control()->set_stream_analog_level(100));
1215 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1216 out_analog_level = apm_->gain_control()->stream_analog_level();
1217 }
1218
1219 // Ensure the AGC is still able to reach the maximum.
1220 EXPECT_EQ(255, out_analog_level);
1221}
1222
1223// Verifies that despite volume slider quantization, the AGC can continue to
1224// increase its volume.
1225TEST_F(ApmTest, QuantizedVolumeDoesNotGetStuck) {
pkasting25702cb2016-01-08 13:50:27 -08001226 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001227 RunQuantizedVolumeDoesNotGetStuckTest(kSampleRates[i]);
1228 }
1229}
1230
1231void ApmTest::RunManualVolumeChangeIsPossibleTest(int sample_rate) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001232 Init(sample_rate, sample_rate, sample_rate, 2, 2, 2, false);
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001233 EXPECT_EQ(apm_->kNoError,
1234 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
1235 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
1236
1237 int out_analog_level = 100;
1238 for (int i = 0; i < 1000; ++i) {
1239 ReadFrameWithRewind(near_file_, frame_);
1240 // Ensure the audio is at a low level, so the AGC will try to increase it.
1241 ScaleFrame(frame_, 0.25);
1242
1243 EXPECT_EQ(apm_->kNoError,
1244 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1245 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1246 out_analog_level = apm_->gain_control()->stream_analog_level();
1247 }
1248
1249 // Ensure the volume was raised.
1250 EXPECT_GT(out_analog_level, 100);
1251 int highest_level_reached = out_analog_level;
1252 // Simulate a user manual volume change.
1253 out_analog_level = 100;
1254
1255 for (int i = 0; i < 300; ++i) {
1256 ReadFrameWithRewind(near_file_, frame_);
1257 ScaleFrame(frame_, 0.25);
1258
1259 EXPECT_EQ(apm_->kNoError,
1260 apm_->gain_control()->set_stream_analog_level(out_analog_level));
1261 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1262 out_analog_level = apm_->gain_control()->stream_analog_level();
1263 // Check that AGC respected the manually adjusted volume.
1264 EXPECT_LT(out_analog_level, highest_level_reached);
1265 }
1266 // Check that the volume was still raised.
1267 EXPECT_GT(out_analog_level, 100);
1268}
1269
1270TEST_F(ApmTest, ManualVolumeChangeIsPossible) {
pkasting25702cb2016-01-08 13:50:27 -08001271 for (size_t i = 0; i < arraysize(kSampleRates); ++i) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00001272 RunManualVolumeChangeIsPossibleTest(kSampleRates[i]);
1273 }
1274}
1275
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001276#if !defined(WEBRTC_ANDROID) && !defined(WEBRTC_IOS)
1277TEST_F(ApmTest, AgcOnlyAdaptsWhenTargetSignalIsPresent) {
1278 const int kSampleRateHz = 16000;
pkasting25702cb2016-01-08 13:50:27 -08001279 const size_t kSamplesPerChannel =
1280 static_cast<size_t>(AudioProcessing::kChunkSizeMs * kSampleRateHz / 1000);
Peter Kasting69558702016-01-12 16:26:35 -08001281 const size_t kNumInputChannels = 2;
1282 const size_t kNumOutputChannels = 1;
pkasting25702cb2016-01-08 13:50:27 -08001283 const size_t kNumChunks = 700;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001284 const float kScaleFactor = 0.25f;
1285 Config config;
1286 std::vector<webrtc::Point> geometry;
1287 geometry.push_back(webrtc::Point(0.f, 0.f, 0.f));
1288 geometry.push_back(webrtc::Point(0.05f, 0.f, 0.f));
1289 config.Set<Beamforming>(new Beamforming(true, geometry));
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001290 testing::NiceMock<MockNonlinearBeamformer>* beamformer =
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001291 new testing::NiceMock<MockNonlinearBeamformer>(geometry, 1u);
kwiberg62eaacf2016-02-17 06:39:05 -08001292 std::unique_ptr<AudioProcessing> apm(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00001293 AudioProcessing::Create(config, beamformer));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001294 EXPECT_EQ(kNoErr, apm->gain_control()->Enable(true));
1295 ChannelBuffer<float> src_buf(kSamplesPerChannel, kNumInputChannels);
1296 ChannelBuffer<float> dest_buf(kSamplesPerChannel, kNumOutputChannels);
pkasting25702cb2016-01-08 13:50:27 -08001297 const size_t max_length = kSamplesPerChannel * std::max(kNumInputChannels,
1298 kNumOutputChannels);
kwiberg62eaacf2016-02-17 06:39:05 -08001299 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
1300 std::unique_ptr<float[]> float_data(new float[max_length]);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001301 std::string filename = ResourceFilePath("far", kSampleRateHz);
1302 FILE* far_file = fopen(filename.c_str(), "rb");
1303 ASSERT_TRUE(far_file != NULL) << "Could not open file " << filename << "\n";
1304 const int kDefaultVolume = apm->gain_control()->stream_analog_level();
1305 const int kDefaultCompressionGain =
1306 apm->gain_control()->compression_gain_db();
1307 bool is_target = false;
1308 EXPECT_CALL(*beamformer, is_target_present())
1309 .WillRepeatedly(testing::ReturnPointee(&is_target));
pkasting25702cb2016-01-08 13:50:27 -08001310 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001311 ASSERT_TRUE(ReadChunk(far_file,
1312 int_data.get(),
1313 float_data.get(),
1314 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001315 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001316 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001317 src_buf.channels()[j][k] *= kScaleFactor;
1318 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001319 }
1320 EXPECT_EQ(kNoErr,
1321 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001322 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001323 kSampleRateHz,
1324 LayoutFromChannels(src_buf.num_channels()),
1325 kSampleRateHz,
1326 LayoutFromChannels(dest_buf.num_channels()),
1327 dest_buf.channels()));
1328 }
1329 EXPECT_EQ(kDefaultVolume,
1330 apm->gain_control()->stream_analog_level());
1331 EXPECT_EQ(kDefaultCompressionGain,
1332 apm->gain_control()->compression_gain_db());
1333 rewind(far_file);
1334 is_target = true;
pkasting25702cb2016-01-08 13:50:27 -08001335 for (size_t i = 0; i < kNumChunks; ++i) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001336 ASSERT_TRUE(ReadChunk(far_file,
1337 int_data.get(),
1338 float_data.get(),
1339 &src_buf));
Peter Kasting69558702016-01-12 16:26:35 -08001340 for (size_t j = 0; j < kNumInputChannels; ++j) {
pkasting25702cb2016-01-08 13:50:27 -08001341 for (size_t k = 0; k < kSamplesPerChannel; ++k) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001342 src_buf.channels()[j][k] *= kScaleFactor;
1343 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001344 }
1345 EXPECT_EQ(kNoErr,
1346 apm->ProcessStream(src_buf.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001347 src_buf.num_frames(),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001348 kSampleRateHz,
1349 LayoutFromChannels(src_buf.num_channels()),
1350 kSampleRateHz,
1351 LayoutFromChannels(dest_buf.num_channels()),
1352 dest_buf.channels()));
1353 }
1354 EXPECT_LT(kDefaultVolume,
1355 apm->gain_control()->stream_analog_level());
1356 EXPECT_LT(kDefaultCompressionGain,
1357 apm->gain_control()->compression_gain_db());
1358 ASSERT_EQ(0, fclose(far_file));
1359}
1360#endif
1361
niklase@google.com470e71d2011-07-07 08:21:25 +00001362TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001363 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001364 NoiseSuppression::Level level[] = {
1365 NoiseSuppression::kLow,
1366 NoiseSuppression::kModerate,
1367 NoiseSuppression::kHigh,
1368 NoiseSuppression::kVeryHigh
1369 };
pkasting25702cb2016-01-08 13:50:27 -08001370 for (size_t i = 0; i < arraysize(level); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001371 EXPECT_EQ(apm_->kNoError,
1372 apm_->noise_suppression()->set_level(level[i]));
1373 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1374 }
1375
andrew@webrtc.org648af742012-02-08 01:57:29 +00001376 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001377 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1378 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1379 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1380 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1381}
1382
1383TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001384 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001385 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1386 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1387 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1388 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1389}
1390
1391TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001392 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001393 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001394 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001395
1396 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1397
1398 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1399 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1400
1401 // Run this test in wideband; in super-wb, the splitting filter distorts the
1402 // audio enough to cause deviation from the expectation for small values.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001403 frame_->samples_per_channel_ = 160;
1404 frame_->num_channels_ = 2;
1405 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001406
1407 // Min value if no frames have been processed.
1408 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1409
1410 // Min value on zero frames.
1411 SetFrameTo(frame_, 0);
1412 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1413 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1414 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1415
1416 // Try a few RMS values.
1417 // (These also test that the value resets after retrieving it.)
1418 SetFrameTo(frame_, 32767);
1419 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1420 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1421 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1422
1423 SetFrameTo(frame_, 30000);
1424 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1425 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1426 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1427
1428 SetFrameTo(frame_, 10000);
1429 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1430 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1431 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1432
1433 SetFrameTo(frame_, 10);
1434 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1435 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1436 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1437
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001438 // Verify reset after enable/disable.
1439 SetFrameTo(frame_, 32767);
1440 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1441 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1442 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1443 SetFrameTo(frame_, 1);
1444 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1445 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1446
1447 // Verify reset after initialize.
1448 SetFrameTo(frame_, 32767);
1449 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1450 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1451 SetFrameTo(frame_, 1);
1452 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1453 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001454}
1455
1456TEST_F(ApmTest, VoiceDetection) {
1457 // Test external VAD
1458 EXPECT_EQ(apm_->kNoError,
1459 apm_->voice_detection()->set_stream_has_voice(true));
1460 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1461 EXPECT_EQ(apm_->kNoError,
1462 apm_->voice_detection()->set_stream_has_voice(false));
1463 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1464
andrew@webrtc.org648af742012-02-08 01:57:29 +00001465 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001466 VoiceDetection::Likelihood likelihood[] = {
1467 VoiceDetection::kVeryLowLikelihood,
1468 VoiceDetection::kLowLikelihood,
1469 VoiceDetection::kModerateLikelihood,
1470 VoiceDetection::kHighLikelihood
1471 };
pkasting25702cb2016-01-08 13:50:27 -08001472 for (size_t i = 0; i < arraysize(likelihood); i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001473 EXPECT_EQ(apm_->kNoError,
1474 apm_->voice_detection()->set_likelihood(likelihood[i]));
1475 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1476 }
1477
1478 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001479 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001480 EXPECT_EQ(apm_->kBadParameterError,
1481 apm_->voice_detection()->set_frame_size_ms(12));
1482
andrew@webrtc.org648af742012-02-08 01:57:29 +00001483 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001484 for (int i = 10; i <= 30; i += 10) {
1485 EXPECT_EQ(apm_->kNoError,
1486 apm_->voice_detection()->set_frame_size_ms(i));
1487 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1488 }
1489 */
1490
andrew@webrtc.org648af742012-02-08 01:57:29 +00001491 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001492 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1493 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1494 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1495 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1496
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001497 // Test that AudioFrame activity is maintained when VAD is disabled.
1498 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1499 AudioFrame::VADActivity activity[] = {
1500 AudioFrame::kVadActive,
1501 AudioFrame::kVadPassive,
1502 AudioFrame::kVadUnknown
1503 };
pkasting25702cb2016-01-08 13:50:27 -08001504 for (size_t i = 0; i < arraysize(activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001505 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001506 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001507 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001508 }
1509
1510 // Test that AudioFrame activity is set when VAD is enabled.
1511 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001512 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001513 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001514 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001515
niklase@google.com470e71d2011-07-07 08:21:25 +00001516 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1517}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001518
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001519TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1520 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1521 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1522 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1523 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1524 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1525 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1526 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1527}
1528
1529TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
pkasting25702cb2016-01-08 13:50:27 -08001530 for (size_t i = 0; i < arraysize(kSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001531 Init(kSampleRates[i], kSampleRates[i], kSampleRates[i], 2, 2, 2, false);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001532 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001533 AudioFrame frame_copy;
1534 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001535 for (int j = 0; j < 1000; j++) {
1536 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1537 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
ekmeyerson60d9b332015-08-14 10:35:55 -07001538 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(frame_));
1539 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001540 }
1541 }
1542}
1543
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001544TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabledFloat) {
1545 // Test that ProcessStream copies input to output even with no processing.
1546 const size_t kSamples = 80;
1547 const int sample_rate = 8000;
1548 const float src[kSamples] = {
1549 -1.0f, 0.0f, 1.0f
1550 };
1551 float dest[kSamples] = {};
1552
1553 auto src_channels = &src[0];
1554 auto dest_channels = &dest[0];
1555
1556 apm_.reset(AudioProcessing::Create());
1557 EXPECT_NOERR(apm_->ProcessStream(
1558 &src_channels, kSamples, sample_rate, LayoutFromChannels(1),
1559 sample_rate, LayoutFromChannels(1), &dest_channels));
1560
1561 for (size_t i = 0; i < kSamples; ++i) {
1562 EXPECT_EQ(src[i], dest[i]);
1563 }
ekmeyerson60d9b332015-08-14 10:35:55 -07001564
1565 // Same for ProcessReverseStream.
1566 float rev_dest[kSamples] = {};
1567 auto rev_dest_channels = &rev_dest[0];
1568
1569 StreamConfig input_stream = {sample_rate, 1};
1570 StreamConfig output_stream = {sample_rate, 1};
1571 EXPECT_NOERR(apm_->ProcessReverseStream(&src_channels, input_stream,
1572 output_stream, &rev_dest_channels));
1573
1574 for (size_t i = 0; i < kSamples; ++i) {
1575 EXPECT_EQ(src[i], rev_dest[i]);
1576 }
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +00001577}
1578
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001579TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1580 EnableAllComponents();
1581
pkasting25702cb2016-01-08 13:50:27 -08001582 for (size_t i = 0; i < arraysize(kProcessSampleRates); i++) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001583 Init(kProcessSampleRates[i],
1584 kProcessSampleRates[i],
1585 kProcessSampleRates[i],
1586 2,
1587 2,
1588 2,
1589 false);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001590 int analog_level = 127;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001591 ASSERT_EQ(0, feof(far_file_));
1592 ASSERT_EQ(0, feof(near_file_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001593 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001594 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1595
aluebsb0319552016-03-17 20:39:53 -07001596 ASSERT_EQ(kNoErr, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001597
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001598 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1599 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1600
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001601 ASSERT_EQ(kNoErr, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001602 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001603 ASSERT_EQ(kNoErr,
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001604 apm_->gain_control()->set_stream_analog_level(analog_level));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001605 ASSERT_EQ(kNoErr, apm_->ProcessStream(frame_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001606 analog_level = apm_->gain_control()->stream_analog_level();
1607
1608 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1609 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001610 rewind(far_file_);
1611 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001612 }
1613}
1614
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001615TEST_F(ApmTest, SplittingFilter) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001616 // Verify the filter is not active through undistorted audio when:
1617 // 1. No components are enabled...
1618 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001619 AudioFrame frame_copy;
1620 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001621 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1622 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1623 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1624
1625 // 2. Only the level estimator is enabled...
1626 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001627 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001628 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1629 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1630 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1631 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1632 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1633
1634 // 3. Only VAD is enabled...
1635 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001636 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001637 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1638 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1640 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1641 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1642
1643 // 4. Both VAD and the level estimator are enabled...
1644 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001645 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001646 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1647 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1648 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1649 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1650 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1651 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1652 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1653
1654 // 5. Not using super-wb.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001655 frame_->samples_per_channel_ = 160;
1656 frame_->num_channels_ = 2;
1657 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001658 // Enable AEC, which would require the filter in super-wb. We rely on the
1659 // first few frames of data being unaffected by the AEC.
1660 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1661 // behavior of the AEC. Think of something more robust.
1662 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001663 // Make sure we have extended filter enabled. This makes sure nothing is
1664 // touched until we have a farend frame.
1665 Config config;
Henrik Lundin441f6342015-06-09 16:03:13 +02001666 config.Set<ExtendedFilter>(new ExtendedFilter(true));
bjornv@webrtc.orgcb0ea432014-06-09 08:21:52 +00001667 apm_->SetExtraOptions(config);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001668 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001669 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001670 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001671 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001672 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1673 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001674 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001675 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1676 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1677
1678 // Check the test is valid. We should have distortion from the filter
1679 // when AEC is enabled (which won't affect the audio).
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001680 frame_->samples_per_channel_ = 320;
1681 frame_->num_channels_ = 2;
1682 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001683 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001684 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001685 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001686 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001687 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1688 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1689}
1690
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001691#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1692void ApmTest::ProcessDebugDump(const std::string& in_filename,
1693 const std::string& out_filename,
ivocd66b44d2016-01-15 03:06:36 -08001694 Format format,
1695 int max_size_bytes) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001696 FILE* in_file = fopen(in_filename.c_str(), "rb");
1697 ASSERT_TRUE(in_file != NULL);
1698 audioproc::Event event_msg;
1699 bool first_init = true;
1700
1701 while (ReadMessageFromFile(in_file, &event_msg)) {
1702 if (event_msg.type() == audioproc::Event::INIT) {
1703 const audioproc::Init msg = event_msg.init();
1704 int reverse_sample_rate = msg.sample_rate();
1705 if (msg.has_reverse_sample_rate()) {
1706 reverse_sample_rate = msg.reverse_sample_rate();
1707 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001708 int output_sample_rate = msg.sample_rate();
1709 if (msg.has_output_sample_rate()) {
1710 output_sample_rate = msg.output_sample_rate();
1711 }
1712
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001713 Init(msg.sample_rate(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001714 output_sample_rate,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001715 reverse_sample_rate,
1716 msg.num_input_channels(),
1717 msg.num_output_channels(),
1718 msg.num_reverse_channels(),
1719 false);
1720 if (first_init) {
1721 // StartDebugRecording() writes an additional init message. Don't start
1722 // recording until after the first init to avoid the extra message.
ivocd66b44d2016-01-15 03:06:36 -08001723 EXPECT_NOERR(
1724 apm_->StartDebugRecording(out_filename.c_str(), max_size_bytes));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001725 first_init = false;
1726 }
1727
1728 } else if (event_msg.type() == audioproc::Event::REVERSE_STREAM) {
1729 const audioproc::ReverseStream msg = event_msg.reverse_stream();
1730
1731 if (msg.channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001732 ASSERT_EQ(revframe_->num_channels_,
1733 static_cast<size_t>(msg.channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001734 for (int i = 0; i < msg.channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001735 memcpy(revfloat_cb_->channels()[i],
1736 msg.channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001737 msg.channel(i).size());
1738 }
1739 } else {
1740 memcpy(revframe_->data_, msg.data().data(), msg.data().size());
1741 if (format == kFloatFormat) {
1742 // We're using an int16 input file; convert to float.
1743 ConvertToFloat(*revframe_, revfloat_cb_.get());
1744 }
1745 }
1746 AnalyzeReverseStreamChooser(format);
1747
1748 } else if (event_msg.type() == audioproc::Event::STREAM) {
1749 const audioproc::Stream msg = event_msg.stream();
1750 // ProcessStream could have changed this for the output frame.
1751 frame_->num_channels_ = apm_->num_input_channels();
1752
1753 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(msg.level()));
1754 EXPECT_NOERR(apm_->set_stream_delay_ms(msg.delay()));
1755 apm_->echo_cancellation()->set_stream_drift_samples(msg.drift());
1756 if (msg.has_keypress()) {
1757 apm_->set_stream_key_pressed(msg.keypress());
1758 } else {
1759 apm_->set_stream_key_pressed(true);
1760 }
1761
1762 if (msg.input_channel_size() > 0) {
Peter Kasting69558702016-01-12 16:26:35 -08001763 ASSERT_EQ(frame_->num_channels_,
1764 static_cast<size_t>(msg.input_channel_size()));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001765 for (int i = 0; i < msg.input_channel_size(); ++i) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001766 memcpy(float_cb_->channels()[i],
1767 msg.input_channel(i).data(),
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001768 msg.input_channel(i).size());
1769 }
1770 } else {
1771 memcpy(frame_->data_, msg.input_data().data(), msg.input_data().size());
1772 if (format == kFloatFormat) {
1773 // We're using an int16 input file; convert to float.
1774 ConvertToFloat(*frame_, float_cb_.get());
1775 }
1776 }
1777 ProcessStreamChooser(format);
1778 }
1779 }
1780 EXPECT_NOERR(apm_->StopDebugRecording());
1781 fclose(in_file);
1782}
1783
1784void ApmTest::VerifyDebugDumpTest(Format format) {
1785 const std::string in_filename = test::ResourcePath("ref03", "aecdump");
henrik.lundin@webrtc.org1092ea02014-04-02 07:46:49 +00001786 std::string format_string;
1787 switch (format) {
1788 case kIntFormat:
1789 format_string = "_int";
1790 break;
1791 case kFloatFormat:
1792 format_string = "_float";
1793 break;
1794 }
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001795 const std::string ref_filename = test::TempFilename(
1796 test::OutputPath(), std::string("ref") + format_string + "_aecdump");
1797 const std::string out_filename = test::TempFilename(
1798 test::OutputPath(), std::string("out") + format_string + "_aecdump");
ivocd66b44d2016-01-15 03:06:36 -08001799 const std::string limited_filename = test::TempFilename(
1800 test::OutputPath(), std::string("limited") + format_string + "_aecdump");
1801 const size_t logging_limit_bytes = 100000;
1802 // We expect at least this many bytes in the created logfile.
1803 const size_t logging_expected_bytes = 95000;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001804 EnableAllComponents();
ivocd66b44d2016-01-15 03:06:36 -08001805 ProcessDebugDump(in_filename, ref_filename, format, -1);
1806 ProcessDebugDump(ref_filename, out_filename, format, -1);
1807 ProcessDebugDump(ref_filename, limited_filename, format, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001808
1809 FILE* ref_file = fopen(ref_filename.c_str(), "rb");
1810 FILE* out_file = fopen(out_filename.c_str(), "rb");
ivocd66b44d2016-01-15 03:06:36 -08001811 FILE* limited_file = fopen(limited_filename.c_str(), "rb");
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001812 ASSERT_TRUE(ref_file != NULL);
1813 ASSERT_TRUE(out_file != NULL);
ivocd66b44d2016-01-15 03:06:36 -08001814 ASSERT_TRUE(limited_file != NULL);
kwiberg62eaacf2016-02-17 06:39:05 -08001815 std::unique_ptr<uint8_t[]> ref_bytes;
1816 std::unique_ptr<uint8_t[]> out_bytes;
1817 std::unique_ptr<uint8_t[]> limited_bytes;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001818
1819 size_t ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1820 size_t out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001821 size_t limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001822 size_t bytes_read = 0;
ivocd66b44d2016-01-15 03:06:36 -08001823 size_t bytes_read_limited = 0;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001824 while (ref_size > 0 && out_size > 0) {
1825 bytes_read += ref_size;
ivocd66b44d2016-01-15 03:06:36 -08001826 bytes_read_limited += limited_size;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001827 EXPECT_EQ(ref_size, out_size);
ivocd66b44d2016-01-15 03:06:36 -08001828 EXPECT_GE(ref_size, limited_size);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001829 EXPECT_EQ(0, memcmp(ref_bytes.get(), out_bytes.get(), ref_size));
ivocd66b44d2016-01-15 03:06:36 -08001830 EXPECT_EQ(0, memcmp(ref_bytes.get(), limited_bytes.get(), limited_size));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001831 ref_size = ReadMessageBytesFromFile(ref_file, &ref_bytes);
1832 out_size = ReadMessageBytesFromFile(out_file, &out_bytes);
ivocd66b44d2016-01-15 03:06:36 -08001833 limited_size = ReadMessageBytesFromFile(limited_file, &limited_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001834 }
1835 EXPECT_GT(bytes_read, 0u);
ivocd66b44d2016-01-15 03:06:36 -08001836 EXPECT_GT(bytes_read_limited, logging_expected_bytes);
1837 EXPECT_LE(bytes_read_limited, logging_limit_bytes);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001838 EXPECT_NE(0, feof(ref_file));
1839 EXPECT_NE(0, feof(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001840 EXPECT_NE(0, feof(limited_file));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001841 ASSERT_EQ(0, fclose(ref_file));
1842 ASSERT_EQ(0, fclose(out_file));
ivocd66b44d2016-01-15 03:06:36 -08001843 ASSERT_EQ(0, fclose(limited_file));
Peter Boströmfade1792015-05-12 10:44:11 +02001844 remove(ref_filename.c_str());
1845 remove(out_filename.c_str());
ivocd66b44d2016-01-15 03:06:36 -08001846 remove(limited_filename.c_str());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001847}
1848
pbosc7a65692016-05-06 12:50:04 -07001849TEST_F(ApmTest, VerifyDebugDumpInt) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001850 VerifyDebugDumpTest(kIntFormat);
1851}
1852
pbosc7a65692016-05-06 12:50:04 -07001853TEST_F(ApmTest, VerifyDebugDumpFloat) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001854 VerifyDebugDumpTest(kFloatFormat);
1855}
1856#endif
1857
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001858// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001859TEST_F(ApmTest, DebugDump) {
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001860 const std::string filename =
1861 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001862 EXPECT_EQ(apm_->kNullPointerError,
ivocd66b44d2016-01-15 03:06:36 -08001863 apm_->StartDebugRecording(static_cast<const char*>(NULL), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001864
1865#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1866 // Stopping without having started should be OK.
1867 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1868
ivocd66b44d2016-01-15 03:06:36 -08001869 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001870 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
aluebsb0319552016-03-17 20:39:53 -07001871 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001872 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1873
1874 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001875 FILE* fid = fopen(filename.c_str(), "r");
1876 ASSERT_TRUE(fid != NULL);
1877
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001878 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001879 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001880 ASSERT_EQ(0, remove(filename.c_str()));
1881#else
1882 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001883 apm_->StartDebugRecording(filename.c_str(), -1));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001884 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1885
1886 // Verify the file has NOT been written.
1887 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1888#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1889}
1890
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001891// TODO(andrew): expand test to verify output.
pbosc7a65692016-05-06 12:50:04 -07001892TEST_F(ApmTest, DebugDumpFromFileHandle) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001893 FILE* fid = NULL;
ivocd66b44d2016-01-15 03:06:36 -08001894 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(fid, -1));
pbos@webrtc.orga525c982015-01-12 17:31:18 +00001895 const std::string filename =
1896 test::TempFilename(test::OutputPath(), "debug_aec");
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001897 fid = fopen(filename.c_str(), "w");
1898 ASSERT_TRUE(fid);
1899
1900#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1901 // Stopping without having started should be OK.
1902 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1903
ivocd66b44d2016-01-15 03:06:36 -08001904 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(fid, -1));
aluebsb0319552016-03-17 20:39:53 -07001905 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001906 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1907 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1908
1909 // Verify the file has been written.
1910 fid = fopen(filename.c_str(), "r");
1911 ASSERT_TRUE(fid != NULL);
1912
1913 // Clean it up.
1914 ASSERT_EQ(0, fclose(fid));
1915 ASSERT_EQ(0, remove(filename.c_str()));
1916#else
1917 EXPECT_EQ(apm_->kUnsupportedFunctionError,
ivocd66b44d2016-01-15 03:06:36 -08001918 apm_->StartDebugRecording(fid, -1));
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001919 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1920
1921 ASSERT_EQ(0, fclose(fid));
1922#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1923}
1924
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001925TEST_F(ApmTest, FloatAndIntInterfacesGiveSimilarResults) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001926 audioproc::OutputData ref_data;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001927 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001928
1929 Config config;
1930 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08001931 std::unique_ptr<AudioProcessing> fapm(AudioProcessing::Create(config));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001932 EnableAllComponents();
1933 EnableAllAPComponents(fapm.get());
1934 for (int i = 0; i < ref_data.test_size(); i++) {
1935 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
1936
1937 audioproc::Test* test = ref_data.mutable_test(i);
1938 // TODO(ajm): Restore downmixing test cases.
1939 if (test->num_input_channels() != test->num_output_channels())
1940 continue;
1941
Peter Kasting69558702016-01-12 16:26:35 -08001942 const size_t num_render_channels =
1943 static_cast<size_t>(test->num_reverse_channels());
1944 const size_t num_input_channels =
1945 static_cast<size_t>(test->num_input_channels());
1946 const size_t num_output_channels =
1947 static_cast<size_t>(test->num_output_channels());
pkasting25702cb2016-01-08 13:50:27 -08001948 const size_t samples_per_channel = static_cast<size_t>(
1949 test->sample_rate() * AudioProcessing::kChunkSizeMs / 1000);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001950
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001951 Init(test->sample_rate(), test->sample_rate(), test->sample_rate(),
1952 num_input_channels, num_output_channels, num_render_channels, true);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001953 Init(fapm.get());
1954
1955 ChannelBuffer<int16_t> output_cb(samples_per_channel, num_input_channels);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001956 ChannelBuffer<int16_t> output_int16(samples_per_channel,
1957 num_input_channels);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001958
1959 int analog_level = 127;
aluebs776593b2016-03-15 14:04:58 -07001960 size_t num_bad_chunks = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001961 while (ReadFrame(far_file_, revframe_, revfloat_cb_.get()) &&
1962 ReadFrame(near_file_, frame_, float_cb_.get())) {
1963 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1964
aluebsb0319552016-03-17 20:39:53 -07001965 EXPECT_NOERR(apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001966 EXPECT_NOERR(fapm->AnalyzeReverseStream(
1967 revfloat_cb_->channels(),
1968 samples_per_channel,
1969 test->sample_rate(),
1970 LayoutFromChannels(num_render_channels)));
1971
1972 EXPECT_NOERR(apm_->set_stream_delay_ms(0));
1973 EXPECT_NOERR(fapm->set_stream_delay_ms(0));
1974 apm_->echo_cancellation()->set_stream_drift_samples(0);
1975 fapm->echo_cancellation()->set_stream_drift_samples(0);
1976 EXPECT_NOERR(apm_->gain_control()->set_stream_analog_level(analog_level));
1977 EXPECT_NOERR(fapm->gain_control()->set_stream_analog_level(analog_level));
1978
1979 EXPECT_NOERR(apm_->ProcessStream(frame_));
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001980 Deinterleave(frame_->data_, samples_per_channel, num_output_channels,
1981 output_int16.channels());
1982
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001983 EXPECT_NOERR(fapm->ProcessStream(
1984 float_cb_->channels(),
1985 samples_per_channel,
1986 test->sample_rate(),
1987 LayoutFromChannels(num_input_channels),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001988 test->sample_rate(),
1989 LayoutFromChannels(num_output_channels),
1990 float_cb_->channels()));
Peter Kasting69558702016-01-12 16:26:35 -08001991 for (size_t j = 0; j < num_output_channels; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001992 FloatToS16(float_cb_->channels()[j],
1993 samples_per_channel,
1994 output_cb.channels()[j]);
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001995 float variance = 0;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00001996 float snr = ComputeSNR(output_int16.channels()[j],
1997 output_cb.channels()[j],
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00001998 samples_per_channel, &variance);
aluebs776593b2016-03-15 14:04:58 -07001999
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002000 const float kVarianceThreshold = 20;
2001 const float kSNRThreshold = 20;
aluebs776593b2016-03-15 14:04:58 -07002002
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002003 // Skip frames with low energy.
aluebs776593b2016-03-15 14:04:58 -07002004 if (sqrt(variance) > kVarianceThreshold && snr < kSNRThreshold) {
2005 ++num_bad_chunks;
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002006 }
2007 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002008
2009 analog_level = fapm->gain_control()->stream_analog_level();
2010 EXPECT_EQ(apm_->gain_control()->stream_analog_level(),
2011 fapm->gain_control()->stream_analog_level());
2012 EXPECT_EQ(apm_->echo_cancellation()->stream_has_echo(),
2013 fapm->echo_cancellation()->stream_has_echo());
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002014 EXPECT_NEAR(apm_->noise_suppression()->speech_probability(),
2015 fapm->noise_suppression()->speech_probability(),
Alejandro Luebs47748742015-05-22 12:00:21 -07002016 0.01);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002017
2018 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002019 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002020 }
aluebs776593b2016-03-15 14:04:58 -07002021
2022#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2023 const size_t kMaxNumBadChunks = 0;
2024#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2025 // There are a few chunks in the fixed-point profile that give low SNR.
2026 // Listening confirmed the difference is acceptable.
2027 const size_t kMaxNumBadChunks = 60;
2028#endif
2029 EXPECT_LE(num_bad_chunks, kMaxNumBadChunks);
2030
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002031 rewind(far_file_);
2032 rewind(near_file_);
2033 }
2034}
2035
andrew@webrtc.org75f19482012-02-09 17:16:18 +00002036// TODO(andrew): Add a test to process a few frames with different combinations
2037// of enabled components.
2038
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002039TEST_F(ApmTest, Process) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002040 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002041 audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002042
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002043 if (!write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002044 OpenFileAndReadMessage(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002045 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002046 // Write the desired tests to the protobuf reference file.
pkasting25702cb2016-01-08 13:50:27 -08002047 for (size_t i = 0; i < arraysize(kChannels); i++) {
2048 for (size_t j = 0; j < arraysize(kChannels); j++) {
2049 for (size_t l = 0; l < arraysize(kProcessSampleRates); l++) {
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002050 audioproc::Test* test = ref_data.add_test();
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002051 test->set_num_reverse_channels(kChannels[i]);
2052 test->set_num_input_channels(kChannels[j]);
2053 test->set_num_output_channels(kChannels[j]);
2054 test->set_sample_rate(kProcessSampleRates[l]);
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002055 test->set_use_aec_extended_filter(false);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002056 }
2057 }
2058 }
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002059#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2060 // To test the extended filter mode.
2061 audioproc::Test* test = ref_data.add_test();
2062 test->set_num_reverse_channels(2);
2063 test->set_num_input_channels(2);
2064 test->set_num_output_channels(2);
2065 test->set_sample_rate(AudioProcessing::kSampleRate32kHz);
2066 test->set_use_aec_extended_filter(true);
2067#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002068 }
2069
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002070 for (int i = 0; i < ref_data.test_size(); i++) {
2071 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002072
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002073 audioproc::Test* test = ref_data.mutable_test(i);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00002074 // TODO(ajm): We no longer allow different input and output channels. Skip
2075 // these tests for now, but they should be removed from the set.
2076 if (test->num_input_channels() != test->num_output_channels())
2077 continue;
2078
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002079 Config config;
2080 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
Henrik Lundin441f6342015-06-09 16:03:13 +02002081 config.Set<ExtendedFilter>(
2082 new ExtendedFilter(test->use_aec_extended_filter()));
aluebs@webrtc.orgf17ee9c2015-01-29 00:03:53 +00002083 apm_.reset(AudioProcessing::Create(config));
2084
2085 EnableAllComponents();
2086
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002087 Init(test->sample_rate(),
2088 test->sample_rate(),
2089 test->sample_rate(),
Peter Kasting69558702016-01-12 16:26:35 -08002090 static_cast<size_t>(test->num_input_channels()),
2091 static_cast<size_t>(test->num_output_channels()),
2092 static_cast<size_t>(test->num_reverse_channels()),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002093 true);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002094
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002095 int frame_count = 0;
2096 int has_echo_count = 0;
2097 int has_voice_count = 0;
2098 int is_saturated_count = 0;
2099 int analog_level = 127;
2100 int analog_level_average = 0;
2101 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002102 float ns_speech_prob_average = 0.0f;
minyue58530ed2016-05-24 05:50:12 -07002103#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2104 int stats_index = 0;
2105#endif
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002106
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002107 while (ReadFrame(far_file_, revframe_) && ReadFrame(near_file_, frame_)) {
aluebsb0319552016-03-17 20:39:53 -07002108 EXPECT_EQ(apm_->kNoError, apm_->ProcessReverseStream(revframe_));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002109
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002110 frame_->vad_activity_ = AudioFrame::kVadUnknown;
2111
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002112 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00002113 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002114 EXPECT_EQ(apm_->kNoError,
2115 apm_->gain_control()->set_stream_analog_level(analog_level));
2116
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002117 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00002118
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002119 // Ensure the frame was downmixed properly.
Peter Kasting69558702016-01-12 16:26:35 -08002120 EXPECT_EQ(static_cast<size_t>(test->num_output_channels()),
2121 frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002122
2123 max_output_average += MaxAudioFrame(*frame_);
2124
2125 if (apm_->echo_cancellation()->stream_has_echo()) {
2126 has_echo_count++;
2127 }
2128
2129 analog_level = apm_->gain_control()->stream_analog_level();
2130 analog_level_average += analog_level;
2131 if (apm_->gain_control()->stream_is_saturated()) {
2132 is_saturated_count++;
2133 }
2134 if (apm_->voice_detection()->stream_has_voice()) {
2135 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002136 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002137 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002138 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002139 }
2140
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002141 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
2142
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00002143 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002144 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002145 sizeof(int16_t),
2146 frame_size,
2147 out_file_);
2148 ASSERT_EQ(frame_size, write_count);
2149
2150 // Reset in case of downmixing.
Peter Kasting69558702016-01-12 16:26:35 -08002151 frame_->num_channels_ = static_cast<size_t>(test->num_input_channels());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002152 frame_count++;
minyue58530ed2016-05-24 05:50:12 -07002153
2154#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2155 const int kStatsAggregationFrameNum = 100; // 1 second.
2156 if (frame_count % kStatsAggregationFrameNum == 0) {
2157 // Get echo metrics.
2158 EchoCancellation::Metrics echo_metrics;
2159 EXPECT_EQ(apm_->kNoError,
2160 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
2161
2162 // Get delay metrics.
2163 int median = 0;
2164 int std = 0;
2165 float fraction_poor_delays = 0;
2166 EXPECT_EQ(apm_->kNoError,
2167 apm_->echo_cancellation()->GetDelayMetrics(
2168 &median, &std, &fraction_poor_delays));
2169
2170 // Get RMS.
2171 int rms_level = apm_->level_estimator()->RMS();
2172 EXPECT_LE(0, rms_level);
2173 EXPECT_GE(127, rms_level);
2174
2175 if (!write_ref_data) {
2176 const audioproc::Test::EchoMetrics& reference =
2177 test->echo_metrics(stats_index);
2178 TestStats(echo_metrics.residual_echo_return_loss,
2179 reference.residual_echo_return_loss());
2180 TestStats(echo_metrics.echo_return_loss,
2181 reference.echo_return_loss());
2182 TestStats(echo_metrics.echo_return_loss_enhancement,
2183 reference.echo_return_loss_enhancement());
2184 TestStats(echo_metrics.a_nlp,
2185 reference.a_nlp());
2186 EXPECT_EQ(echo_metrics.divergent_filter_fraction,
2187 reference.divergent_filter_fraction());
2188
2189 const audioproc::Test::DelayMetrics& reference_delay =
2190 test->delay_metrics(stats_index);
2191 EXPECT_EQ(reference_delay.median(), median);
2192 EXPECT_EQ(reference_delay.std(), std);
2193 EXPECT_EQ(reference_delay.fraction_poor_delays(),
2194 fraction_poor_delays);
2195
2196 EXPECT_EQ(test->rms_level(stats_index), rms_level);
2197
2198 ++stats_index;
2199 } else {
2200 audioproc::Test::EchoMetrics* message =
2201 test->add_echo_metrics();
2202 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
2203 message->mutable_residual_echo_return_loss());
2204 WriteStatsMessage(echo_metrics.echo_return_loss,
2205 message->mutable_echo_return_loss());
2206 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
2207 message->mutable_echo_return_loss_enhancement());
2208 WriteStatsMessage(echo_metrics.a_nlp,
2209 message->mutable_a_nlp());
2210 message->set_divergent_filter_fraction(
2211 echo_metrics.divergent_filter_fraction);
2212
2213 audioproc::Test::DelayMetrics* message_delay =
2214 test->add_delay_metrics();
2215 message_delay->set_median(median);
2216 message_delay->set_std(std);
2217 message_delay->set_fraction_poor_delays(fraction_poor_delays);
2218
2219 test->add_rms_level(rms_level);
2220 }
2221 }
2222#endif // defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE).
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002223 }
2224 max_output_average /= frame_count;
2225 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002226 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002227
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002228 if (!write_ref_data) {
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002229 const int kIntNear = 1;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002230 // When running the test on a N7 we get a {2, 6} difference of
2231 // |has_voice_count| and |max_output_average| is up to 18 higher.
2232 // All numbers being consistently higher on N7 compare to ref_data.
2233 // TODO(bjornv): If we start getting more of these offsets on Android we
2234 // should consider a different approach. Either using one slack for all,
2235 // or generate a separate android reference.
2236#if defined(WEBRTC_ANDROID)
2237 const int kHasVoiceCountOffset = 3;
Alejandro Luebs2a5609d2016-04-05 18:16:54 -07002238 const int kHasVoiceCountNear = 4;
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002239 const int kMaxOutputAverageOffset = 9;
2240 const int kMaxOutputAverageNear = 9;
2241#else
2242 const int kHasVoiceCountOffset = 0;
2243 const int kHasVoiceCountNear = kIntNear;
2244 const int kMaxOutputAverageOffset = 0;
2245 const int kMaxOutputAverageNear = kIntNear;
2246#endif
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002247 EXPECT_NEAR(test->has_echo_count(), has_echo_count, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002248 EXPECT_NEAR(test->has_voice_count(),
2249 has_voice_count - kHasVoiceCountOffset,
2250 kHasVoiceCountNear);
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002251 EXPECT_NEAR(test->is_saturated_count(), is_saturated_count, kIntNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002252
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002253 EXPECT_NEAR(test->analog_level_average(), analog_level_average, kIntNear);
bjornv@webrtc.orgdc0b37d2014-09-23 05:03:44 +00002254 EXPECT_NEAR(test->max_output_average(),
2255 max_output_average - kMaxOutputAverageOffset,
2256 kMaxOutputAverageNear);
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002257#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002258 const double kFloatNear = 0.0005;
bjornv@webrtc.org8dd60cc2014-09-11 08:36:35 +00002259 EXPECT_NEAR(test->ns_speech_probability_average(),
2260 ns_speech_prob_average,
2261 kFloatNear);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002262#endif
2263 } else {
2264 test->set_has_echo_count(has_echo_count);
2265 test->set_has_voice_count(has_voice_count);
2266 test->set_is_saturated_count(is_saturated_count);
2267
2268 test->set_analog_level_average(analog_level_average);
2269 test->set_max_output_average(max_output_average);
2270
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00002271#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00002272 EXPECT_LE(0.0f, ns_speech_prob_average);
2273 EXPECT_GE(1.0f, ns_speech_prob_average);
2274 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002275#endif
2276 }
2277
2278 rewind(far_file_);
2279 rewind(near_file_);
2280 }
2281
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00002282 if (write_ref_data) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00002283 OpenFileAndWriteMessage(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00002284 }
2285}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002286
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002287TEST_F(ApmTest, NoErrorsWithKeyboardChannel) {
2288 struct ChannelFormat {
2289 AudioProcessing::ChannelLayout in_layout;
2290 AudioProcessing::ChannelLayout out_layout;
2291 };
2292 ChannelFormat cf[] = {
2293 {AudioProcessing::kMonoAndKeyboard, AudioProcessing::kMono},
2294 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kMono},
2295 {AudioProcessing::kStereoAndKeyboard, AudioProcessing::kStereo},
2296 };
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002297
kwiberg62eaacf2016-02-17 06:39:05 -08002298 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create());
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002299 // Enable one component just to ensure some processing takes place.
2300 ap->noise_suppression()->Enable(true);
pkasting25702cb2016-01-08 13:50:27 -08002301 for (size_t i = 0; i < arraysize(cf); ++i) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002302 const int in_rate = 44100;
2303 const int out_rate = 48000;
2304 ChannelBuffer<float> in_cb(SamplesFromRate(in_rate),
2305 TotalChannelsFromLayout(cf[i].in_layout));
2306 ChannelBuffer<float> out_cb(SamplesFromRate(out_rate),
2307 ChannelsFromLayout(cf[i].out_layout));
2308
2309 // Run over a few chunks.
2310 for (int j = 0; j < 10; ++j) {
2311 EXPECT_NOERR(ap->ProcessStream(
2312 in_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002313 in_cb.num_frames(),
andrew@webrtc.org103657b2014-04-24 18:28:56 +00002314 in_rate,
2315 cf[i].in_layout,
2316 out_rate,
2317 cf[i].out_layout,
2318 out_cb.channels()));
2319 }
2320 }
2321}
2322
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002323// Compares the reference and test arrays over a region around the expected
2324// delay. Finds the highest SNR in that region and adds the variance and squared
2325// error results to the supplied accumulators.
2326void UpdateBestSNR(const float* ref,
2327 const float* test,
pkasting25702cb2016-01-08 13:50:27 -08002328 size_t length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002329 int expected_delay,
2330 double* variance_acc,
2331 double* sq_error_acc) {
2332 double best_snr = std::numeric_limits<double>::min();
2333 double best_variance = 0;
2334 double best_sq_error = 0;
2335 // Search over a region of eight samples around the expected delay.
2336 for (int delay = std::max(expected_delay - 4, 0); delay <= expected_delay + 4;
2337 ++delay) {
2338 double sq_error = 0;
2339 double variance = 0;
pkasting25702cb2016-01-08 13:50:27 -08002340 for (size_t i = 0; i < length - delay; ++i) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002341 double error = test[i + delay] - ref[i];
2342 sq_error += error * error;
2343 variance += ref[i] * ref[i];
2344 }
2345
2346 if (sq_error == 0) {
2347 *variance_acc += variance;
2348 return;
2349 }
2350 double snr = variance / sq_error;
2351 if (snr > best_snr) {
2352 best_snr = snr;
2353 best_variance = variance;
2354 best_sq_error = sq_error;
2355 }
2356 }
2357
2358 *variance_acc += best_variance;
2359 *sq_error_acc += best_sq_error;
2360}
2361
2362// Used to test a multitude of sample rate and channel combinations. It works
2363// by first producing a set of reference files (in SetUpTestCase) that are
2364// assumed to be correct, as the used parameters are verified by other tests
2365// in this collection. Primarily the reference files are all produced at
2366// "native" rates which do not involve any resampling.
2367
2368// Each test pass produces an output file with a particular format. The output
2369// is matched against the reference file closest to its internal processing
2370// format. If necessary the output is resampled back to its process format.
2371// Due to the resampling distortion, we don't expect identical results, but
2372// enforce SNR thresholds which vary depending on the format. 0 is a special
2373// case SNR which corresponds to inf, or zero error.
ekmeyerson60d9b332015-08-14 10:35:55 -07002374typedef std::tr1::tuple<int, int, int, int, double, double>
2375 AudioProcessingTestData;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002376class AudioProcessingTest
2377 : public testing::TestWithParam<AudioProcessingTestData> {
2378 public:
2379 AudioProcessingTest()
2380 : input_rate_(std::tr1::get<0>(GetParam())),
2381 output_rate_(std::tr1::get<1>(GetParam())),
ekmeyerson60d9b332015-08-14 10:35:55 -07002382 reverse_input_rate_(std::tr1::get<2>(GetParam())),
2383 reverse_output_rate_(std::tr1::get<3>(GetParam())),
2384 expected_snr_(std::tr1::get<4>(GetParam())),
2385 expected_reverse_snr_(std::tr1::get<5>(GetParam())) {}
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002386
2387 virtual ~AudioProcessingTest() {}
2388
2389 static void SetUpTestCase() {
2390 // Create all needed output reference files.
Alejandro Luebs47748742015-05-22 12:00:21 -07002391 const int kNativeRates[] = {8000, 16000, 32000, 48000};
Peter Kasting69558702016-01-12 16:26:35 -08002392 const size_t kNumChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -08002393 for (size_t i = 0; i < arraysize(kNativeRates); ++i) {
2394 for (size_t j = 0; j < arraysize(kNumChannels); ++j) {
2395 for (size_t k = 0; k < arraysize(kNumChannels); ++k) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002396 // The reference files always have matching input and output channels.
ekmeyerson60d9b332015-08-14 10:35:55 -07002397 ProcessFormat(kNativeRates[i], kNativeRates[i], kNativeRates[i],
2398 kNativeRates[i], kNumChannels[j], kNumChannels[j],
2399 kNumChannels[k], kNumChannels[k], "ref");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002400 }
2401 }
2402 }
2403 }
2404
pbos@webrtc.org200ac002015-02-03 14:14:01 +00002405 static void TearDownTestCase() {
2406 ClearTempFiles();
2407 }
ekmeyerson60d9b332015-08-14 10:35:55 -07002408
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002409 // Runs a process pass on files with the given parameters and dumps the output
ekmeyerson60d9b332015-08-14 10:35:55 -07002410 // to a file specified with |output_file_prefix|. Both forward and reverse
2411 // output streams are dumped.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002412 static void ProcessFormat(int input_rate,
2413 int output_rate,
ekmeyerson60d9b332015-08-14 10:35:55 -07002414 int reverse_input_rate,
2415 int reverse_output_rate,
Peter Kasting69558702016-01-12 16:26:35 -08002416 size_t num_input_channels,
2417 size_t num_output_channels,
2418 size_t num_reverse_input_channels,
2419 size_t num_reverse_output_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002420 std::string output_file_prefix) {
andrew@webrtc.org8328e7c2014-10-31 04:58:14 +00002421 Config config;
2422 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
kwiberg62eaacf2016-02-17 06:39:05 -08002423 std::unique_ptr<AudioProcessing> ap(AudioProcessing::Create(config));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002424 EnableAllAPComponents(ap.get());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002425
ekmeyerson60d9b332015-08-14 10:35:55 -07002426 ProcessingConfig processing_config = {
2427 {{input_rate, num_input_channels},
2428 {output_rate, num_output_channels},
2429 {reverse_input_rate, num_reverse_input_channels},
2430 {reverse_output_rate, num_reverse_output_channels}}};
2431 ap->Initialize(processing_config);
2432
2433 FILE* far_file =
2434 fopen(ResourceFilePath("far", reverse_input_rate).c_str(), "rb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002435 FILE* near_file = fopen(ResourceFilePath("near", input_rate).c_str(), "rb");
ekmeyerson60d9b332015-08-14 10:35:55 -07002436 FILE* out_file =
2437 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2438 reverse_input_rate, reverse_output_rate,
2439 num_input_channels, num_output_channels,
2440 num_reverse_input_channels,
2441 num_reverse_output_channels, kForward).c_str(),
2442 "wb");
2443 FILE* rev_out_file =
2444 fopen(OutputFilePath(output_file_prefix, input_rate, output_rate,
2445 reverse_input_rate, reverse_output_rate,
2446 num_input_channels, num_output_channels,
2447 num_reverse_input_channels,
2448 num_reverse_output_channels, kReverse).c_str(),
2449 "wb");
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002450 ASSERT_TRUE(far_file != NULL);
2451 ASSERT_TRUE(near_file != NULL);
2452 ASSERT_TRUE(out_file != NULL);
ekmeyerson60d9b332015-08-14 10:35:55 -07002453 ASSERT_TRUE(rev_out_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002454
2455 ChannelBuffer<float> fwd_cb(SamplesFromRate(input_rate),
2456 num_input_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002457 ChannelBuffer<float> rev_cb(SamplesFromRate(reverse_input_rate),
2458 num_reverse_input_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002459 ChannelBuffer<float> out_cb(SamplesFromRate(output_rate),
2460 num_output_channels);
ekmeyerson60d9b332015-08-14 10:35:55 -07002461 ChannelBuffer<float> rev_out_cb(SamplesFromRate(reverse_output_rate),
2462 num_reverse_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002463
2464 // Temporary buffers.
2465 const int max_length =
ekmeyerson60d9b332015-08-14 10:35:55 -07002466 2 * std::max(std::max(out_cb.num_frames(), rev_out_cb.num_frames()),
2467 std::max(fwd_cb.num_frames(), rev_cb.num_frames()));
kwiberg62eaacf2016-02-17 06:39:05 -08002468 std::unique_ptr<float[]> float_data(new float[max_length]);
2469 std::unique_ptr<int16_t[]> int_data(new int16_t[max_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002470
2471 int analog_level = 127;
2472 while (ReadChunk(far_file, int_data.get(), float_data.get(), &rev_cb) &&
2473 ReadChunk(near_file, int_data.get(), float_data.get(), &fwd_cb)) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002474 EXPECT_NOERR(ap->ProcessReverseStream(
2475 rev_cb.channels(), processing_config.reverse_input_stream(),
2476 processing_config.reverse_output_stream(), rev_out_cb.channels()));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002477
2478 EXPECT_NOERR(ap->set_stream_delay_ms(0));
2479 ap->echo_cancellation()->set_stream_drift_samples(0);
2480 EXPECT_NOERR(ap->gain_control()->set_stream_analog_level(analog_level));
2481
2482 EXPECT_NOERR(ap->ProcessStream(
2483 fwd_cb.channels(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002484 fwd_cb.num_frames(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002485 input_rate,
2486 LayoutFromChannels(num_input_channels),
2487 output_rate,
2488 LayoutFromChannels(num_output_channels),
2489 out_cb.channels()));
2490
ekmeyerson60d9b332015-08-14 10:35:55 -07002491 // Dump forward output to file.
2492 Interleave(out_cb.channels(), out_cb.num_frames(), out_cb.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002493 float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002494 size_t out_length = out_cb.num_channels() * out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002495
pkasting25702cb2016-01-08 13:50:27 -08002496 ASSERT_EQ(out_length,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002497 fwrite(float_data.get(), sizeof(float_data[0]),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +00002498 out_length, out_file));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002499
ekmeyerson60d9b332015-08-14 10:35:55 -07002500 // Dump reverse output to file.
2501 Interleave(rev_out_cb.channels(), rev_out_cb.num_frames(),
2502 rev_out_cb.num_channels(), float_data.get());
pkasting25702cb2016-01-08 13:50:27 -08002503 size_t rev_out_length =
2504 rev_out_cb.num_channels() * rev_out_cb.num_frames();
ekmeyerson60d9b332015-08-14 10:35:55 -07002505
pkasting25702cb2016-01-08 13:50:27 -08002506 ASSERT_EQ(rev_out_length,
ekmeyerson60d9b332015-08-14 10:35:55 -07002507 fwrite(float_data.get(), sizeof(float_data[0]), rev_out_length,
2508 rev_out_file));
2509
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002510 analog_level = ap->gain_control()->stream_analog_level();
2511 }
2512 fclose(far_file);
2513 fclose(near_file);
2514 fclose(out_file);
ekmeyerson60d9b332015-08-14 10:35:55 -07002515 fclose(rev_out_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002516 }
2517
2518 protected:
2519 int input_rate_;
2520 int output_rate_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002521 int reverse_input_rate_;
2522 int reverse_output_rate_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002523 double expected_snr_;
ekmeyerson60d9b332015-08-14 10:35:55 -07002524 double expected_reverse_snr_;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002525};
2526
bjornv@webrtc.org2812b592014-06-02 11:27:29 +00002527TEST_P(AudioProcessingTest, Formats) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002528 struct ChannelFormat {
2529 int num_input;
2530 int num_output;
ekmeyerson60d9b332015-08-14 10:35:55 -07002531 int num_reverse_input;
2532 int num_reverse_output;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002533 };
2534 ChannelFormat cf[] = {
ekmeyerson60d9b332015-08-14 10:35:55 -07002535 {1, 1, 1, 1},
2536 {1, 1, 2, 1},
2537 {2, 1, 1, 1},
2538 {2, 1, 2, 1},
2539 {2, 2, 1, 1},
2540 {2, 2, 2, 2},
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002541 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002542
pkasting25702cb2016-01-08 13:50:27 -08002543 for (size_t i = 0; i < arraysize(cf); ++i) {
ekmeyerson60d9b332015-08-14 10:35:55 -07002544 ProcessFormat(input_rate_, output_rate_, reverse_input_rate_,
2545 reverse_output_rate_, cf[i].num_input, cf[i].num_output,
2546 cf[i].num_reverse_input, cf[i].num_reverse_output, "out");
Alejandro Luebs47748742015-05-22 12:00:21 -07002547
ekmeyerson60d9b332015-08-14 10:35:55 -07002548 // Verify output for both directions.
2549 std::vector<StreamDirection> stream_directions;
2550 stream_directions.push_back(kForward);
2551 stream_directions.push_back(kReverse);
2552 for (StreamDirection file_direction : stream_directions) {
2553 const int in_rate = file_direction ? reverse_input_rate_ : input_rate_;
2554 const int out_rate = file_direction ? reverse_output_rate_ : output_rate_;
2555 const int out_num =
2556 file_direction ? cf[i].num_reverse_output : cf[i].num_output;
2557 const double expected_snr =
2558 file_direction ? expected_reverse_snr_ : expected_snr_;
2559
2560 const int min_ref_rate = std::min(in_rate, out_rate);
2561 int ref_rate;
2562
2563 if (min_ref_rate > 32000) {
2564 ref_rate = 48000;
2565 } else if (min_ref_rate > 16000) {
2566 ref_rate = 32000;
2567 } else if (min_ref_rate > 8000) {
2568 ref_rate = 16000;
2569 } else {
2570 ref_rate = 8000;
2571 }
aluebs776593b2016-03-15 14:04:58 -07002572#ifdef WEBRTC_ARCH_ARM_FAMILY
perkjdfc28702016-03-09 16:23:23 -08002573 if (file_direction == kForward) {
aluebs776593b2016-03-15 14:04:58 -07002574 ref_rate = std::min(ref_rate, 32000);
perkjdfc28702016-03-09 16:23:23 -08002575 }
2576#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07002577 FILE* out_file = fopen(
2578 OutputFilePath("out", input_rate_, output_rate_, reverse_input_rate_,
2579 reverse_output_rate_, cf[i].num_input,
2580 cf[i].num_output, cf[i].num_reverse_input,
2581 cf[i].num_reverse_output, file_direction).c_str(),
2582 "rb");
2583 // The reference files always have matching input and output channels.
2584 FILE* ref_file = fopen(
2585 OutputFilePath("ref", ref_rate, ref_rate, ref_rate, ref_rate,
2586 cf[i].num_output, cf[i].num_output,
2587 cf[i].num_reverse_output, cf[i].num_reverse_output,
2588 file_direction).c_str(),
2589 "rb");
2590 ASSERT_TRUE(out_file != NULL);
2591 ASSERT_TRUE(ref_file != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002592
pkasting25702cb2016-01-08 13:50:27 -08002593 const size_t ref_length = SamplesFromRate(ref_rate) * out_num;
2594 const size_t out_length = SamplesFromRate(out_rate) * out_num;
ekmeyerson60d9b332015-08-14 10:35:55 -07002595 // Data from the reference file.
kwiberg62eaacf2016-02-17 06:39:05 -08002596 std::unique_ptr<float[]> ref_data(new float[ref_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002597 // Data from the output file.
kwiberg62eaacf2016-02-17 06:39:05 -08002598 std::unique_ptr<float[]> out_data(new float[out_length]);
ekmeyerson60d9b332015-08-14 10:35:55 -07002599 // Data from the resampled output, in case the reference and output rates
2600 // don't match.
kwiberg62eaacf2016-02-17 06:39:05 -08002601 std::unique_ptr<float[]> cmp_data(new float[ref_length]);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002602
ekmeyerson60d9b332015-08-14 10:35:55 -07002603 PushResampler<float> resampler;
2604 resampler.InitializeIfNeeded(out_rate, ref_rate, out_num);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002605
ekmeyerson60d9b332015-08-14 10:35:55 -07002606 // Compute the resampling delay of the output relative to the reference,
2607 // to find the region over which we should search for the best SNR.
2608 float expected_delay_sec = 0;
2609 if (in_rate != ref_rate) {
2610 // Input resampling delay.
2611 expected_delay_sec +=
2612 PushSincResampler::AlgorithmicDelaySeconds(in_rate);
2613 }
2614 if (out_rate != ref_rate) {
2615 // Output resampling delay.
2616 expected_delay_sec +=
2617 PushSincResampler::AlgorithmicDelaySeconds(ref_rate);
2618 // Delay of converting the output back to its processing rate for
2619 // testing.
2620 expected_delay_sec +=
2621 PushSincResampler::AlgorithmicDelaySeconds(out_rate);
2622 }
2623 int expected_delay =
2624 floor(expected_delay_sec * ref_rate + 0.5f) * out_num;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002625
ekmeyerson60d9b332015-08-14 10:35:55 -07002626 double variance = 0;
2627 double sq_error = 0;
2628 while (fread(out_data.get(), sizeof(out_data[0]), out_length, out_file) &&
2629 fread(ref_data.get(), sizeof(ref_data[0]), ref_length, ref_file)) {
2630 float* out_ptr = out_data.get();
2631 if (out_rate != ref_rate) {
2632 // Resample the output back to its internal processing rate if
2633 // necssary.
pkasting25702cb2016-01-08 13:50:27 -08002634 ASSERT_EQ(ref_length,
2635 static_cast<size_t>(resampler.Resample(
2636 out_ptr, out_length, cmp_data.get(), ref_length)));
ekmeyerson60d9b332015-08-14 10:35:55 -07002637 out_ptr = cmp_data.get();
2638 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002639
ekmeyerson60d9b332015-08-14 10:35:55 -07002640 // Update the |sq_error| and |variance| accumulators with the highest
2641 // SNR of reference vs output.
2642 UpdateBestSNR(ref_data.get(), out_ptr, ref_length, expected_delay,
2643 &variance, &sq_error);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002644 }
2645
ekmeyerson60d9b332015-08-14 10:35:55 -07002646 std::cout << "(" << input_rate_ << ", " << output_rate_ << ", "
2647 << reverse_input_rate_ << ", " << reverse_output_rate_ << ", "
2648 << cf[i].num_input << ", " << cf[i].num_output << ", "
2649 << cf[i].num_reverse_input << ", " << cf[i].num_reverse_output
2650 << ", " << file_direction << "): ";
2651 if (sq_error > 0) {
2652 double snr = 10 * log10(variance / sq_error);
2653 EXPECT_GE(snr, expected_snr);
2654 EXPECT_NE(0, expected_snr);
2655 std::cout << "SNR=" << snr << " dB" << std::endl;
2656 } else {
aluebs776593b2016-03-15 14:04:58 -07002657 std::cout << "SNR=inf dB" << std::endl;
ekmeyerson60d9b332015-08-14 10:35:55 -07002658 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002659
ekmeyerson60d9b332015-08-14 10:35:55 -07002660 fclose(out_file);
2661 fclose(ref_file);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002662 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002663 }
2664}
2665
2666#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
2667INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002668 CommonFormats,
2669 AudioProcessingTest,
2670 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 0, 0),
peah0bf612b2016-04-06 02:47:46 -07002671 std::tr1::make_tuple(48000, 48000, 32000, 48000, 40, 30),
2672 std::tr1::make_tuple(48000, 48000, 16000, 48000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002673 std::tr1::make_tuple(48000, 44100, 48000, 44100, 20, 20),
2674 std::tr1::make_tuple(48000, 44100, 32000, 44100, 20, 15),
2675 std::tr1::make_tuple(48000, 44100, 16000, 44100, 20, 15),
2676 std::tr1::make_tuple(48000, 32000, 48000, 32000, 30, 35),
2677 std::tr1::make_tuple(48000, 32000, 32000, 32000, 30, 0),
2678 std::tr1::make_tuple(48000, 32000, 16000, 32000, 30, 20),
2679 std::tr1::make_tuple(48000, 16000, 48000, 16000, 25, 20),
2680 std::tr1::make_tuple(48000, 16000, 32000, 16000, 25, 20),
2681 std::tr1::make_tuple(48000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002682
ekmeyerson60d9b332015-08-14 10:35:55 -07002683 std::tr1::make_tuple(44100, 48000, 48000, 48000, 30, 0),
2684 std::tr1::make_tuple(44100, 48000, 32000, 48000, 30, 30),
2685 std::tr1::make_tuple(44100, 48000, 16000, 48000, 30, 20),
2686 std::tr1::make_tuple(44100, 44100, 48000, 44100, 20, 20),
2687 std::tr1::make_tuple(44100, 44100, 32000, 44100, 20, 15),
2688 std::tr1::make_tuple(44100, 44100, 16000, 44100, 20, 15),
2689 std::tr1::make_tuple(44100, 32000, 48000, 32000, 30, 35),
2690 std::tr1::make_tuple(44100, 32000, 32000, 32000, 30, 0),
2691 std::tr1::make_tuple(44100, 32000, 16000, 32000, 30, 20),
2692 std::tr1::make_tuple(44100, 16000, 48000, 16000, 25, 20),
2693 std::tr1::make_tuple(44100, 16000, 32000, 16000, 25, 20),
2694 std::tr1::make_tuple(44100, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002695
ekmeyerson60d9b332015-08-14 10:35:55 -07002696 std::tr1::make_tuple(32000, 48000, 48000, 48000, 30, 0),
2697 std::tr1::make_tuple(32000, 48000, 32000, 48000, 35, 30),
2698 std::tr1::make_tuple(32000, 48000, 16000, 48000, 30, 20),
2699 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2700 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2701 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2702 std::tr1::make_tuple(32000, 32000, 48000, 32000, 40, 35),
2703 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2704 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
2705 std::tr1::make_tuple(32000, 16000, 48000, 16000, 25, 20),
2706 std::tr1::make_tuple(32000, 16000, 32000, 16000, 25, 20),
2707 std::tr1::make_tuple(32000, 16000, 16000, 16000, 25, 0),
Alejandro Luebs47748742015-05-22 12:00:21 -07002708
ekmeyerson60d9b332015-08-14 10:35:55 -07002709 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2710 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2711 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2712 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2713 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2714 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2715 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2716 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2717 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2718 std::tr1::make_tuple(16000, 16000, 48000, 16000, 40, 20),
aluebseb3603b2016-04-20 15:27:58 -07002719 std::tr1::make_tuple(16000, 16000, 32000, 16000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002720 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
Alejandro Luebs47748742015-05-22 12:00:21 -07002721
2722#elif defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
2723INSTANTIATE_TEST_CASE_P(
ekmeyerson60d9b332015-08-14 10:35:55 -07002724 CommonFormats,
2725 AudioProcessingTest,
perkjdfc28702016-03-09 16:23:23 -08002726 testing::Values(std::tr1::make_tuple(48000, 48000, 48000, 48000, 20, 0),
2727 std::tr1::make_tuple(48000, 48000, 32000, 48000, 20, 30),
2728 std::tr1::make_tuple(48000, 48000, 16000, 48000, 20, 20),
2729 std::tr1::make_tuple(48000, 44100, 48000, 44100, 15, 20),
2730 std::tr1::make_tuple(48000, 44100, 32000, 44100, 15, 15),
2731 std::tr1::make_tuple(48000, 44100, 16000, 44100, 15, 15),
ekmeyerson60d9b332015-08-14 10:35:55 -07002732 std::tr1::make_tuple(48000, 32000, 48000, 32000, 20, 35),
2733 std::tr1::make_tuple(48000, 32000, 32000, 32000, 20, 0),
2734 std::tr1::make_tuple(48000, 32000, 16000, 32000, 20, 20),
2735 std::tr1::make_tuple(48000, 16000, 48000, 16000, 20, 20),
2736 std::tr1::make_tuple(48000, 16000, 32000, 16000, 20, 20),
2737 std::tr1::make_tuple(48000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002738
aluebs776593b2016-03-15 14:04:58 -07002739 std::tr1::make_tuple(44100, 48000, 48000, 48000, 15, 0),
2740 std::tr1::make_tuple(44100, 48000, 32000, 48000, 15, 30),
2741 std::tr1::make_tuple(44100, 48000, 16000, 48000, 15, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002742 std::tr1::make_tuple(44100, 44100, 48000, 44100, 15, 20),
2743 std::tr1::make_tuple(44100, 44100, 32000, 44100, 15, 15),
2744 std::tr1::make_tuple(44100, 44100, 16000, 44100, 15, 15),
2745 std::tr1::make_tuple(44100, 32000, 48000, 32000, 20, 35),
2746 std::tr1::make_tuple(44100, 32000, 32000, 32000, 20, 0),
2747 std::tr1::make_tuple(44100, 32000, 16000, 32000, 20, 20),
2748 std::tr1::make_tuple(44100, 16000, 48000, 16000, 20, 20),
2749 std::tr1::make_tuple(44100, 16000, 32000, 16000, 20, 20),
2750 std::tr1::make_tuple(44100, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002751
aluebs776593b2016-03-15 14:04:58 -07002752 std::tr1::make_tuple(32000, 48000, 48000, 48000, 35, 0),
2753 std::tr1::make_tuple(32000, 48000, 32000, 48000, 65, 30),
2754 std::tr1::make_tuple(32000, 48000, 16000, 48000, 40, 20),
2755 std::tr1::make_tuple(32000, 44100, 48000, 44100, 20, 20),
2756 std::tr1::make_tuple(32000, 44100, 32000, 44100, 20, 15),
2757 std::tr1::make_tuple(32000, 44100, 16000, 44100, 20, 15),
2758 std::tr1::make_tuple(32000, 32000, 48000, 32000, 35, 35),
2759 std::tr1::make_tuple(32000, 32000, 32000, 32000, 0, 0),
2760 std::tr1::make_tuple(32000, 32000, 16000, 32000, 40, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002761 std::tr1::make_tuple(32000, 16000, 48000, 16000, 20, 20),
2762 std::tr1::make_tuple(32000, 16000, 32000, 16000, 20, 20),
2763 std::tr1::make_tuple(32000, 16000, 16000, 16000, 20, 0),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002764
ekmeyerson60d9b332015-08-14 10:35:55 -07002765 std::tr1::make_tuple(16000, 48000, 48000, 48000, 25, 0),
2766 std::tr1::make_tuple(16000, 48000, 32000, 48000, 25, 30),
2767 std::tr1::make_tuple(16000, 48000, 16000, 48000, 25, 20),
2768 std::tr1::make_tuple(16000, 44100, 48000, 44100, 15, 20),
2769 std::tr1::make_tuple(16000, 44100, 32000, 44100, 15, 15),
2770 std::tr1::make_tuple(16000, 44100, 16000, 44100, 15, 15),
2771 std::tr1::make_tuple(16000, 32000, 48000, 32000, 25, 35),
2772 std::tr1::make_tuple(16000, 32000, 32000, 32000, 25, 0),
2773 std::tr1::make_tuple(16000, 32000, 16000, 32000, 25, 20),
2774 std::tr1::make_tuple(16000, 16000, 48000, 16000, 35, 20),
aluebseb3603b2016-04-20 15:27:58 -07002775 std::tr1::make_tuple(16000, 16000, 32000, 16000, 35, 20),
ekmeyerson60d9b332015-08-14 10:35:55 -07002776 std::tr1::make_tuple(16000, 16000, 16000, 16000, 0, 0)));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002777#endif
2778
niklase@google.com470e71d2011-07-07 08:21:25 +00002779} // namespace
andrew@webrtc.org27c69802014-02-18 20:24:56 +00002780} // namespace webrtc