blob: 61d8cef3ed65399593b50cbb99e1c5aecd9e9684 [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
ajm@google.com59e41402011-07-28 17:34:04 +000011#include <stdio.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000012
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000013#include <algorithm>
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000014#include <queue>
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000015
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +000016#include "gtest/gtest.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000018#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
19#include "webrtc/modules/audio_processing/include/audio_processing.h"
20#include "webrtc/modules/interface/module_common_types.h"
21#include "webrtc/system_wrappers/interface/event_wrapper.h"
22#include "webrtc/system_wrappers/interface/scoped_ptr.h"
23#include "webrtc/system_wrappers/interface/thread_wrapper.h"
24#include "webrtc/system_wrappers/interface/trace.h"
25#include "webrtc/test/testsupport/fileutils.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000026#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000027#include "external/webrtc/webrtc/modules/audio_processing/test/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000028#else
ajm@google.com808e0e02011-08-03 21:08:51 +000029#include "webrtc/audio_processing/unittest.pb.h"
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000030#endif
niklase@google.com470e71d2011-07-07 08:21:25 +000031
andrew@webrtc.org293d22b2012-01-30 22:04:26 +000032#if (defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)) || \
33 (defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64) && !defined(NDEBUG))
34# define WEBRTC_AUDIOPROC_BIT_EXACT
35#endif
36
niklase@google.com470e71d2011-07-07 08:21:25 +000037using webrtc::AudioProcessing;
38using webrtc::AudioFrame;
39using webrtc::GainControl;
40using webrtc::NoiseSuppression;
41using webrtc::EchoCancellation;
42using webrtc::EventWrapper;
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +000043using webrtc::scoped_array;
niklase@google.com470e71d2011-07-07 08:21:25 +000044using webrtc::Trace;
45using webrtc::LevelEstimator;
46using webrtc::EchoCancellation;
47using webrtc::EchoControlMobile;
48using webrtc::VoiceDetection;
49
50namespace {
ajm@google.com59e41402011-07-28 17:34:04 +000051// When false, this will compare the output data with the results stored to
niklase@google.com470e71d2011-07-07 08:21:25 +000052// file. This is the typical case. When the file should be updated, it can
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +000053// be set to true with the command-line switch --write_ref_data.
54bool write_ref_data = false;
ajm@google.com59e41402011-07-28 17:34:04 +000055
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +000056const int kSampleRates[] = {8000, 16000, 32000};
57const size_t kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates);
58const int kChannels[] = {1, 2};
59const size_t kChannelsSize = sizeof(kChannels) / sizeof(*kChannels);
60
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +000061#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
62// AECM doesn't support super-wb.
63const int kProcessSampleRates[] = {8000, 16000};
64#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
65const int kProcessSampleRates[] = {8000, 16000, 32000};
66#endif
67const size_t kProcessSampleRatesSize = sizeof(kProcessSampleRates) /
68 sizeof(*kProcessSampleRates);
69
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000070int TruncateToMultipleOf10(int value) {
71 return (value / 10) * 10;
72}
73
andrew@webrtc.org81865342012-10-27 00:28:27 +000074// TODO(andrew): Use the MonoToStereo routine from AudioFrameOperations.
75void MixStereoToMono(const int16_t* stereo,
76 int16_t* mono,
77 int samples_per_channel) {
78 for (int i = 0; i < samples_per_channel; i++) {
bjornv@webrtc.org3e102492013-02-14 15:29:09 +000079 int32_t mono_s32 = (static_cast<int32_t>(stereo[i * 2]) +
80 static_cast<int32_t>(stereo[i * 2 + 1])) >> 1;
81 mono[i] = static_cast<int16_t>(mono_s32);
andrew@webrtc.org81865342012-10-27 00:28:27 +000082 }
83}
84
85void CopyLeftToRightChannel(int16_t* stereo, int samples_per_channel) {
86 for (int i = 0; i < samples_per_channel; i++) {
87 stereo[i * 2 + 1] = stereo[i * 2];
88 }
89}
90
91void VerifyChannelsAreEqual(int16_t* stereo, int samples_per_channel) {
92 for (int i = 0; i < samples_per_channel; i++) {
93 EXPECT_EQ(stereo[i * 2 + 1], stereo[i * 2]);
94 }
95}
96
97void SetFrameTo(AudioFrame* frame, int16_t value) {
98 for (int i = 0; i < frame->samples_per_channel_ * frame->num_channels_;
99 ++i) {
100 frame->data_[i] = value;
101 }
102}
103
104void SetFrameTo(AudioFrame* frame, int16_t left, int16_t right) {
105 ASSERT_EQ(2, frame->num_channels_);
106 for (int i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
107 frame->data_[i] = left;
108 frame->data_[i + 1] = right;
109 }
110}
111
112template <class T>
113T AbsValue(T a) {
114 return a > 0 ? a: -a;
115}
116
117int16_t MaxAudioFrame(const AudioFrame& frame) {
118 const int length = frame.samples_per_channel_ * frame.num_channels_;
119 int16_t max_data = AbsValue(frame.data_[0]);
120 for (int i = 1; i < length; i++) {
121 max_data = std::max(max_data, AbsValue(frame.data_[i]));
122 }
123
124 return max_data;
125}
126
127bool FrameDataAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
128 if (frame1.samples_per_channel_ !=
129 frame2.samples_per_channel_) {
130 return false;
131 }
132 if (frame1.num_channels_ !=
133 frame2.num_channels_) {
134 return false;
135 }
136 if (memcmp(frame1.data_, frame2.data_,
137 frame1.samples_per_channel_ * frame1.num_channels_ *
138 sizeof(int16_t))) {
139 return false;
140 }
141 return true;
142}
143
144void TestStats(const AudioProcessing::Statistic& test,
145 const webrtc::audioproc::Test::Statistic& reference) {
146 EXPECT_EQ(reference.instant(), test.instant);
147 EXPECT_EQ(reference.average(), test.average);
148 EXPECT_EQ(reference.maximum(), test.maximum);
149 EXPECT_EQ(reference.minimum(), test.minimum);
150}
151
152void WriteStatsMessage(const AudioProcessing::Statistic& output,
153 webrtc::audioproc::Test::Statistic* message) {
154 message->set_instant(output.instant);
155 message->set_average(output.average);
156 message->set_maximum(output.maximum);
157 message->set_minimum(output.minimum);
158}
159
160void WriteMessageLiteToFile(const std::string filename,
161 const ::google::protobuf::MessageLite& message) {
162 FILE* file = fopen(filename.c_str(), "wb");
163 ASSERT_TRUE(file != NULL) << "Could not open " << filename;
164 int size = message.ByteSize();
165 ASSERT_GT(size, 0);
166 unsigned char* array = new unsigned char[size];
167 ASSERT_TRUE(message.SerializeToArray(array, size));
168
169 ASSERT_EQ(1u, fwrite(&size, sizeof(int), 1, file));
170 ASSERT_EQ(static_cast<size_t>(size),
171 fwrite(array, sizeof(unsigned char), size, file));
172
173 delete [] array;
174 fclose(file);
175}
176
177void ReadMessageLiteFromFile(const std::string filename,
178 ::google::protobuf::MessageLite* message) {
179 assert(message != NULL);
180
181 FILE* file = fopen(filename.c_str(), "rb");
182 ASSERT_TRUE(file != NULL) << "Could not open " << filename;
183 int size = 0;
184 ASSERT_EQ(1u, fread(&size, sizeof(int), 1, file));
185 ASSERT_GT(size, 0);
186 unsigned char* array = new unsigned char[size];
187 ASSERT_EQ(static_cast<size_t>(size),
188 fread(array, sizeof(unsigned char), size, file));
189
190 ASSERT_TRUE(message->ParseFromArray(array, size));
191
192 delete [] array;
193 fclose(file);
194}
195
196struct ThreadData {
197 ThreadData(int thread_num_, AudioProcessing* ap_)
198 : thread_num(thread_num_),
199 error(false),
200 ap(ap_) {}
201 int thread_num;
202 bool error;
203 AudioProcessing* ap;
204};
205
niklase@google.com470e71d2011-07-07 08:21:25 +0000206class ApmTest : public ::testing::Test {
207 protected:
208 ApmTest();
209 virtual void SetUp();
210 virtual void TearDown();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000211
212 static void SetUpTestCase() {
213 Trace::CreateTrace();
214 std::string trace_filename = webrtc::test::OutputPath() +
andrew@webrtc.org81865342012-10-27 00:28:27 +0000215 "audioproc_trace.txt";
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000216 ASSERT_EQ(0, Trace::SetTraceFile(trace_filename.c_str()));
217 }
218
219 static void TearDownTestCase() {
220 Trace::ReturnTrace();
221 }
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000222
223 void Init(int sample_rate_hz, int num_reverse_channels,
224 int num_input_channels, int num_output_channels,
225 bool open_output_file);
226 std::string ResourceFilePath(std::string name, int sample_rate_hz);
227 std::string OutputFilePath(std::string name,
228 int sample_rate_hz,
229 int num_reverse_channels,
230 int num_input_channels,
231 int num_output_channels);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000232 void EnableAllComponents();
233 bool ReadFrame(FILE* file, AudioFrame* frame);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000234 void ProcessWithDefaultStreamParameters(AudioFrame* frame);
235 template <typename F>
236 void ChangeTriggersInit(F f, AudioProcessing* ap, int initial_value,
237 int changed_value);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000238 void ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
239 int delay_min, int delay_max);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000240
241 const std::string output_path_;
242 const std::string ref_path_;
243 const std::string ref_filename_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 webrtc::AudioProcessing* apm_;
245 webrtc::AudioFrame* frame_;
246 webrtc::AudioFrame* revframe_;
247 FILE* far_file_;
248 FILE* near_file_;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000249 FILE* out_file_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000250};
251
252ApmTest::ApmTest()
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000253 : output_path_(webrtc::test::OutputPath()),
254 ref_path_(webrtc::test::ProjectRootPath() +
andrew@webrtc.org9dc45da2012-05-23 15:39:01 +0000255 "data/audio_processing/"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000256#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000257 ref_filename_(ref_path_ + "output_data_fixed.pb"),
andrew@webrtc.org293d22b2012-01-30 22:04:26 +0000258#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000259 ref_filename_(ref_path_ + "output_data_float.pb"),
kjellander@webrtc.org61f07c32011-10-18 06:54:58 +0000260#endif
261 apm_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000262 frame_(NULL),
ajm@google.com22e65152011-07-18 18:03:01 +0000263 revframe_(NULL),
264 far_file_(NULL),
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000265 near_file_(NULL),
266 out_file_(NULL) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000267
268void ApmTest::SetUp() {
269 apm_ = AudioProcessing::Create(0);
270 ASSERT_TRUE(apm_ != NULL);
271
272 frame_ = new AudioFrame();
273 revframe_ = new AudioFrame();
274
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000275 Init(32000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276}
277
278void ApmTest::TearDown() {
279 if (frame_) {
280 delete frame_;
281 }
282 frame_ = NULL;
283
284 if (revframe_) {
285 delete revframe_;
286 }
287 revframe_ = NULL;
288
289 if (far_file_) {
290 ASSERT_EQ(0, fclose(far_file_));
291 }
292 far_file_ = NULL;
293
294 if (near_file_) {
295 ASSERT_EQ(0, fclose(near_file_));
296 }
297 near_file_ = NULL;
298
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000299 if (out_file_) {
300 ASSERT_EQ(0, fclose(out_file_));
301 }
302 out_file_ = NULL;
303
niklase@google.com470e71d2011-07-07 08:21:25 +0000304 if (apm_ != NULL) {
305 AudioProcessing::Destroy(apm_);
306 }
307 apm_ = NULL;
308}
309
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000310std::string ApmTest::ResourceFilePath(std::string name, int sample_rate_hz) {
311 std::ostringstream ss;
312 // Resource files are all stereo.
313 ss << name << sample_rate_hz / 1000 << "_stereo";
314 return webrtc::test::ResourcePath(ss.str(), "pcm");
315}
316
317std::string ApmTest::OutputFilePath(std::string name,
318 int sample_rate_hz,
319 int num_reverse_channels,
320 int num_input_channels,
321 int num_output_channels) {
322 std::ostringstream ss;
323 ss << name << sample_rate_hz / 1000 << "_" << num_reverse_channels << "r" <<
324 num_input_channels << "i" << "_";
325 if (num_output_channels == 1) {
326 ss << "mono";
327 } else if (num_output_channels == 2) {
328 ss << "stereo";
329 } else {
330 assert(false);
331 return "";
332 }
333 ss << ".pcm";
334
335 return output_path_ + ss.str();
336}
337
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000338void ApmTest::Init(int sample_rate_hz, int num_reverse_channels,
339 int num_input_channels, int num_output_channels,
340 bool open_output_file) {
341 ASSERT_EQ(apm_->kNoError, apm_->Initialize());
342
343 // Handles error checking of the parameters as well. No need to repeat it.
344 ASSERT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(sample_rate_hz));
345 ASSERT_EQ(apm_->kNoError, apm_->set_num_channels(num_input_channels,
346 num_output_channels));
347 ASSERT_EQ(apm_->kNoError,
348 apm_->set_num_reverse_channels(num_reverse_channels));
349
350 // We always use 10 ms frames.
351 const int samples_per_channel = sample_rate_hz / 100;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000352 frame_->samples_per_channel_ = samples_per_channel;
353 frame_->num_channels_ = num_input_channels;
354 frame_->sample_rate_hz_ = sample_rate_hz;
355 revframe_->samples_per_channel_ = samples_per_channel;
356 revframe_->num_channels_ = num_reverse_channels;
357 revframe_->sample_rate_hz_ = sample_rate_hz;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +0000358
359 if (far_file_) {
360 ASSERT_EQ(0, fclose(far_file_));
361 }
362 std::string filename = ResourceFilePath("far", sample_rate_hz);
363 far_file_ = fopen(filename.c_str(), "rb");
364 ASSERT_TRUE(far_file_ != NULL) << "Could not open file " <<
365 filename << "\n";
366
367 if (near_file_) {
368 ASSERT_EQ(0, fclose(near_file_));
369 }
370 filename = ResourceFilePath("near", sample_rate_hz);
371 near_file_ = fopen(filename.c_str(), "rb");
372 ASSERT_TRUE(near_file_ != NULL) << "Could not open file " <<
373 filename << "\n";
374
375 if (open_output_file) {
376 if (out_file_) {
377 ASSERT_EQ(0, fclose(out_file_));
378 }
379 filename = OutputFilePath("out", sample_rate_hz, num_reverse_channels,
380 num_input_channels, num_output_channels);
381 out_file_ = fopen(filename.c_str(), "wb");
382 ASSERT_TRUE(out_file_ != NULL) << "Could not open file " <<
383 filename << "\n";
384 }
385}
386
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +0000387void ApmTest::EnableAllComponents() {
388#if defined(WEBRTC_AUDIOPROC_FIXED_PROFILE)
389 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(16000));
390 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
391
392 EXPECT_EQ(apm_->kNoError,
393 apm_->gain_control()->set_mode(GainControl::kAdaptiveDigital));
394 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
395#elif defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
396 EXPECT_EQ(apm_->kNoError,
397 apm_->echo_cancellation()->enable_drift_compensation(true));
398 EXPECT_EQ(apm_->kNoError,
399 apm_->echo_cancellation()->enable_metrics(true));
400 EXPECT_EQ(apm_->kNoError,
401 apm_->echo_cancellation()->enable_delay_logging(true));
402 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
403
404 EXPECT_EQ(apm_->kNoError,
405 apm_->gain_control()->set_mode(GainControl::kAdaptiveAnalog));
406 EXPECT_EQ(apm_->kNoError,
407 apm_->gain_control()->set_analog_level_limits(0, 255));
408 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
409#endif
410
411 EXPECT_EQ(apm_->kNoError,
412 apm_->high_pass_filter()->Enable(true));
413
414 EXPECT_EQ(apm_->kNoError,
415 apm_->level_estimator()->Enable(true));
416
417 EXPECT_EQ(apm_->kNoError,
418 apm_->noise_suppression()->Enable(true));
419
420 EXPECT_EQ(apm_->kNoError,
421 apm_->voice_detection()->Enable(true));
422}
423
424bool ApmTest::ReadFrame(FILE* file, AudioFrame* frame) {
425 // The files always contain stereo audio.
426 size_t frame_size = frame->samples_per_channel_ * 2;
427 size_t read_count = fread(frame->data_,
428 sizeof(int16_t),
429 frame_size,
430 file);
431 if (read_count != frame_size) {
432 // Check that the file really ended.
433 EXPECT_NE(0, feof(file));
434 return false; // This is expected.
435 }
436
437 if (frame->num_channels_ == 1) {
438 MixStereoToMono(frame->data_, frame->data_,
439 frame->samples_per_channel_);
440 }
441
442 return true;
ajm@google.coma769fa52011-07-13 21:57:58 +0000443}
444
andrew@webrtc.org81865342012-10-27 00:28:27 +0000445void ApmTest::ProcessWithDefaultStreamParameters(AudioFrame* frame) {
446 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
447 EXPECT_EQ(apm_->kNoError,
448 apm_->echo_cancellation()->set_stream_drift_samples(0));
449 EXPECT_EQ(apm_->kNoError,
450 apm_->gain_control()->set_stream_analog_level(127));
451 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000452}
453
andrew@webrtc.org81865342012-10-27 00:28:27 +0000454template <typename F>
455void ApmTest::ChangeTriggersInit(F f, AudioProcessing* ap, int initial_value,
456 int changed_value) {
457 EnableAllComponents();
458 Init(16000, 2, 2, 2, false);
459 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000460 AudioFrame frame_copy;
461 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000462 ProcessWithDefaultStreamParameters(frame_);
463 // Verify the processing has actually changed the frame.
464 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
465
466 // Test that a change in value triggers an init.
467 f(apm_, changed_value);
468 f(apm_, initial_value);
469 ProcessWithDefaultStreamParameters(&frame_copy);
470 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
471
472 apm_->Initialize();
473 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000474 AudioFrame initial_frame;
475 initial_frame.CopyFrom(*frame_);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000476 ProcessWithDefaultStreamParameters(frame_);
477 ProcessWithDefaultStreamParameters(frame_);
478 // Verify the processing has actually changed the frame.
479 EXPECT_FALSE(FrameDataAreEqual(*frame_, initial_frame));
480
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000481 frame_copy.CopyFrom(initial_frame);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000482 apm_->Initialize();
483 ProcessWithDefaultStreamParameters(&frame_copy);
484 // Verify an init here would result in different output.
485 apm_->Initialize();
486 ProcessWithDefaultStreamParameters(&frame_copy);
487 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
488
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000489 frame_copy.CopyFrom(initial_frame);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000490 apm_->Initialize();
491 ProcessWithDefaultStreamParameters(&frame_copy);
492 // Test that the same value does not trigger an init.
493 f(apm_, initial_value);
494 ProcessWithDefaultStreamParameters(&frame_copy);
495 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +0000496}
497
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000498void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
499 int delay_min, int delay_max) {
500 // The |revframe_| and |frame_| should include the proper frame information,
501 // hence can be used for extracting information.
502 webrtc::AudioFrame tmp_frame;
503 std::queue<webrtc::AudioFrame*> frame_queue;
504 bool causal = true;
505
506 tmp_frame.CopyFrom(*revframe_);
507 SetFrameTo(&tmp_frame, 0);
508
509 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
510 // Initialize the |frame_queue| with empty frames.
511 int frame_delay = delay_ms / 10;
512 while (frame_delay < 0) {
513 webrtc::AudioFrame* frame = new AudioFrame();
514 frame->CopyFrom(tmp_frame);
515 frame_queue.push(frame);
516 frame_delay++;
517 causal = false;
518 }
519 while (frame_delay > 0) {
520 webrtc::AudioFrame* frame = new AudioFrame();
521 frame->CopyFrom(tmp_frame);
522 frame_queue.push(frame);
523 frame_delay--;
524 }
525 // Run for 4.5 seconds, skipping statistics from the first second. We need
526 // enough frames with audio to have reliable estimates, but as few as possible
527 // to keep processing time down. 4.5 seconds seemed to be a good compromise
528 // for this recording.
529 for (int frame_count = 0; frame_count < 450; ++frame_count) {
530 webrtc::AudioFrame* frame = new AudioFrame();
531 frame->CopyFrom(tmp_frame);
532 // Use the near end recording, since that has more speech in it.
533 ASSERT_TRUE(ReadFrame(near_file_, frame));
534 frame_queue.push(frame);
535 webrtc::AudioFrame* reverse_frame = frame;
536 webrtc::AudioFrame* process_frame = frame_queue.front();
537 if (!causal) {
538 reverse_frame = frame_queue.front();
539 // When we call ProcessStream() the frame is modified, so we can't use the
540 // pointer directly when things are non-causal. Use an intermediate frame
541 // and copy the data.
542 process_frame = &tmp_frame;
543 process_frame->CopyFrom(*frame);
544 }
545 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(reverse_frame));
546 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
547 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
548 frame = frame_queue.front();
549 frame_queue.pop();
550 delete frame;
551
552 if (frame_count == 100) {
553 int median;
554 int std;
555 // Discard the first delay metrics to avoid convergence effects.
556 EXPECT_EQ(apm_->kNoError,
557 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
558 }
559 }
560
561 rewind(near_file_);
562 while (!frame_queue.empty()) {
563 webrtc::AudioFrame* frame = frame_queue.front();
564 frame_queue.pop();
565 delete frame;
566 }
567 // Calculate expected delay estimate and acceptable regions. Further,
568 // limit them w.r.t. AEC delay estimation support.
569 const int samples_per_ms = std::min(16, frame_->samples_per_channel_ / 10);
570 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
571 delay_min), delay_max);
572 int expected_median_high = std::min(std::max(
573 expected_median + 96 / samples_per_ms, delay_min), delay_max);
574 int expected_median_low = std::min(std::max(
575 expected_median - 96 / samples_per_ms, delay_min), delay_max);
576 // Verify delay metrics.
577 int median;
578 int std;
579 EXPECT_EQ(apm_->kNoError,
580 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
581 EXPECT_GE(expected_median_high, median);
582 EXPECT_LE(expected_median_low, median);
583}
584
niklase@google.com470e71d2011-07-07 08:21:25 +0000585TEST_F(ApmTest, StreamParameters) {
586 // No errors when the components are disabled.
587 EXPECT_EQ(apm_->kNoError,
588 apm_->ProcessStream(frame_));
589
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000590 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
592 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000593 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000594
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000595 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000596 EXPECT_EQ(apm_->kNoError,
597 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000598 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
599 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000600
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000601 // Other stream parameters set correctly.
602 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000603 EXPECT_EQ(apm_->kNoError,
604 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000605 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
606 EXPECT_EQ(apm_->kNoError,
607 apm_->echo_cancellation()->set_stream_drift_samples(0));
niklase@google.com470e71d2011-07-07 08:21:25 +0000608 EXPECT_EQ(apm_->kStreamParameterNotSetError,
609 apm_->ProcessStream(frame_));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000610 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
611 EXPECT_EQ(apm_->kNoError,
612 apm_->echo_cancellation()->enable_drift_compensation(false));
613
614 // -- Missing delay --
615 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
616 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
617 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
618
619 // Resets after successful ProcessStream().
620 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
621 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
622 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
623
624 // Other stream parameters set correctly.
625 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
626 EXPECT_EQ(apm_->kNoError,
627 apm_->echo_cancellation()->enable_drift_compensation(true));
628 EXPECT_EQ(apm_->kNoError,
629 apm_->echo_cancellation()->set_stream_drift_samples(0));
630 EXPECT_EQ(apm_->kNoError,
631 apm_->gain_control()->set_stream_analog_level(127));
632 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
633 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
634
635 // -- Missing drift --
636 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
637 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
638
639 // Resets after successful ProcessStream().
640 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
641 EXPECT_EQ(apm_->kNoError,
642 apm_->echo_cancellation()->set_stream_drift_samples(0));
643 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
644 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
645
646 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000647 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
648 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
649 EXPECT_EQ(apm_->kNoError,
650 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000651 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000652
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000653 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000654 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
655 EXPECT_EQ(apm_->kNoError,
656 apm_->AnalyzeReverseStream(revframe_));
657 EXPECT_EQ(apm_->kStreamParameterNotSetError,
658 apm_->ProcessStream(frame_));
659
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000660 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000661 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
662 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
663 EXPECT_EQ(apm_->kNoError,
664 apm_->echo_cancellation()->set_stream_drift_samples(0));
665 EXPECT_EQ(apm_->kNoError,
666 apm_->gain_control()->set_stream_analog_level(127));
667 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
668}
669
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000670TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
671 EXPECT_EQ(0, apm_->delay_offset_ms());
672 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
673 EXPECT_EQ(50, apm_->stream_delay_ms());
674}
675
676TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
677 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000678 apm_->set_delay_offset_ms(100);
679 EXPECT_EQ(100, apm_->delay_offset_ms());
680 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000681 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000682 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
683 EXPECT_EQ(200, apm_->stream_delay_ms());
684
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000685 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000686 apm_->set_delay_offset_ms(-50);
687 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000688 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
689 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000690 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
691 EXPECT_EQ(50, apm_->stream_delay_ms());
692}
693
niklase@google.com470e71d2011-07-07 08:21:25 +0000694TEST_F(ApmTest, Channels) {
695 // Testing number of invalid channels
696 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(0, 1));
697 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(1, 0));
698 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(3, 1));
699 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(1, 3));
700 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_reverse_channels(0));
701 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_reverse_channels(3));
702 // Testing number of valid channels
703 for (int i = 1; i < 3; i++) {
704 for (int j = 1; j < 3; j++) {
705 if (j > i) {
706 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(i, j));
707 } else {
708 EXPECT_EQ(apm_->kNoError, apm_->set_num_channels(i, j));
709 EXPECT_EQ(j, apm_->num_output_channels());
710 }
711 }
712 EXPECT_EQ(i, apm_->num_input_channels());
713 EXPECT_EQ(apm_->kNoError, apm_->set_num_reverse_channels(i));
714 EXPECT_EQ(i, apm_->num_reverse_channels());
715 }
716}
717
718TEST_F(ApmTest, SampleRates) {
719 // Testing invalid sample rates
720 EXPECT_EQ(apm_->kBadParameterError, apm_->set_sample_rate_hz(10000));
721 // Testing valid sample rates
722 int fs[] = {8000, 16000, 32000};
723 for (size_t i = 0; i < sizeof(fs) / sizeof(*fs); i++) {
724 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(fs[i]));
725 EXPECT_EQ(fs[i], apm_->sample_rate_hz());
726 }
727}
728
andrew@webrtc.org81865342012-10-27 00:28:27 +0000729void SetSampleRate(AudioProcessing* ap, int value) {
730 EXPECT_EQ(ap->kNoError, ap->set_sample_rate_hz(value));
731}
732
733void SetNumReverseChannels(AudioProcessing* ap, int value) {
734 EXPECT_EQ(ap->kNoError, ap->set_num_reverse_channels(value));
735}
736
737void SetNumOutputChannels(AudioProcessing* ap, int value) {
738 EXPECT_EQ(ap->kNoError, ap->set_num_channels(2, value));
739}
740
741TEST_F(ApmTest, SampleRateChangeTriggersInit) {
742 ChangeTriggersInit(SetSampleRate, apm_, 16000, 8000);
743}
744
745TEST_F(ApmTest, ReverseChannelChangeTriggersInit) {
746 ChangeTriggersInit(SetNumReverseChannels, apm_, 2, 1);
747}
748
749TEST_F(ApmTest, ChannelChangeTriggersInit) {
750 ChangeTriggersInit(SetNumOutputChannels, apm_, 2, 1);
751}
niklase@google.com470e71d2011-07-07 08:21:25 +0000752
753TEST_F(ApmTest, EchoCancellation) {
754 EXPECT_EQ(apm_->kNoError,
755 apm_->echo_cancellation()->enable_drift_compensation(true));
756 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
757 EXPECT_EQ(apm_->kNoError,
758 apm_->echo_cancellation()->enable_drift_compensation(false));
759 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
760
761 EXPECT_EQ(apm_->kBadParameterError,
762 apm_->echo_cancellation()->set_device_sample_rate_hz(4000));
763 EXPECT_EQ(apm_->kBadParameterError,
764 apm_->echo_cancellation()->set_device_sample_rate_hz(100000));
765
766 int rate[] = {16000, 44100, 48000};
767 for (size_t i = 0; i < sizeof(rate)/sizeof(*rate); i++) {
768 EXPECT_EQ(apm_->kNoError,
769 apm_->echo_cancellation()->set_device_sample_rate_hz(rate[i]));
770 EXPECT_EQ(rate[i],
771 apm_->echo_cancellation()->device_sample_rate_hz());
772 }
773
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 EchoCancellation::SuppressionLevel level[] = {
775 EchoCancellation::kLowSuppression,
776 EchoCancellation::kModerateSuppression,
777 EchoCancellation::kHighSuppression,
778 };
779 for (size_t i = 0; i < sizeof(level)/sizeof(*level); i++) {
780 EXPECT_EQ(apm_->kNoError,
781 apm_->echo_cancellation()->set_suppression_level(level[i]));
782 EXPECT_EQ(level[i],
783 apm_->echo_cancellation()->suppression_level());
784 }
785
786 EchoCancellation::Metrics metrics;
787 EXPECT_EQ(apm_->kNotEnabledError,
788 apm_->echo_cancellation()->GetMetrics(&metrics));
789
790 EXPECT_EQ(apm_->kNoError,
791 apm_->echo_cancellation()->enable_metrics(true));
792 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
793 EXPECT_EQ(apm_->kNoError,
794 apm_->echo_cancellation()->enable_metrics(false));
795 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
796
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000797 int median = 0;
798 int std = 0;
799 EXPECT_EQ(apm_->kNotEnabledError,
800 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
801
802 EXPECT_EQ(apm_->kNoError,
803 apm_->echo_cancellation()->enable_delay_logging(true));
804 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
805 EXPECT_EQ(apm_->kNoError,
806 apm_->echo_cancellation()->enable_delay_logging(false));
807 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
808
niklase@google.com470e71d2011-07-07 08:21:25 +0000809 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
810 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
811 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
812 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
813}
814
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000815TEST_F(ApmTest, EchoCancellationReportsCorrectDelays) {
816 // Enable AEC only.
817 EXPECT_EQ(apm_->kNoError,
818 apm_->echo_cancellation()->enable_drift_compensation(false));
819 EXPECT_EQ(apm_->kNoError,
820 apm_->echo_cancellation()->enable_metrics(false));
821 EXPECT_EQ(apm_->kNoError,
822 apm_->echo_cancellation()->enable_delay_logging(true));
823 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
824
825 // Internally in the AEC the amount of lookahead the delay estimation can
826 // handle is 15 blocks and the maximum delay is set to 60 blocks.
827 const int kLookaheadBlocks = 15;
828 const int kMaxDelayBlocks = 60;
829 // The AEC has a startup time before it actually starts to process. This
830 // procedure can flush the internal far-end buffer, which of course affects
831 // the delay estimation. Therefore, we set a system_delay high enough to
832 // avoid that. The smallest system_delay you can report without flushing the
833 // buffer is 66 ms in 8 kHz.
834 //
835 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
836 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
837 // delay estimation. This should be noted though. In case of test failure,
838 // this could be the cause.
839 const int kSystemDelayMs = 66;
840 // Test a couple of corner cases and verify that the estimated delay is
841 // within a valid region (set to +-1.5 blocks). Note that these cases are
842 // sampling frequency dependent.
843 for (size_t i = 0; i < kProcessSampleRatesSize; i++) {
844 Init(kProcessSampleRates[i], 2, 2, 2, false);
845 // Sampling frequency dependent variables.
846 const int num_ms_per_block = std::max(4,
847 640 / frame_->samples_per_channel_);
848 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
849 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
850
851 // 1) Verify correct delay estimate at lookahead boundary.
852 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
853 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
854 delay_max_ms);
855 // 2) A delay less than maximum lookahead should give an delay estimate at
856 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
857 delay_ms -= 20;
858 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
859 delay_max_ms);
860 // 3) Three values around zero delay. Note that we need to compensate for
861 // the fake system_delay.
862 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
863 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
864 delay_max_ms);
865 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
866 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
867 delay_max_ms);
868 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
869 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
870 delay_max_ms);
871 // 4) Verify correct delay estimate at maximum delay boundary.
872 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
873 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
874 delay_max_ms);
875 // 5) A delay above the maximum delay should give an estimate at the
876 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
877 delay_ms += 20;
878 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
879 delay_max_ms);
880 }
881}
882
niklase@google.com470e71d2011-07-07 08:21:25 +0000883TEST_F(ApmTest, EchoControlMobile) {
884 // AECM won't use super-wideband.
885 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(32000));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000886 EXPECT_EQ(apm_->kBadSampleRateError,
887 apm_->echo_control_mobile()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000888 // Turn AECM on (and AEC off)
andrew@webrtc.org75f19482012-02-09 17:16:18 +0000889 Init(16000, 2, 2, 2, false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000890 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(true));
891 EXPECT_TRUE(apm_->echo_control_mobile()->is_enabled());
892
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 // Toggle routing modes
894 EchoControlMobile::RoutingMode mode[] = {
895 EchoControlMobile::kQuietEarpieceOrHeadset,
896 EchoControlMobile::kEarpiece,
897 EchoControlMobile::kLoudEarpiece,
898 EchoControlMobile::kSpeakerphone,
899 EchoControlMobile::kLoudSpeakerphone,
900 };
901 for (size_t i = 0; i < sizeof(mode)/sizeof(*mode); i++) {
902 EXPECT_EQ(apm_->kNoError,
903 apm_->echo_control_mobile()->set_routing_mode(mode[i]));
904 EXPECT_EQ(mode[i],
905 apm_->echo_control_mobile()->routing_mode());
906 }
907 // Turn comfort noise off/on
908 EXPECT_EQ(apm_->kNoError,
909 apm_->echo_control_mobile()->enable_comfort_noise(false));
910 EXPECT_FALSE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
911 EXPECT_EQ(apm_->kNoError,
912 apm_->echo_control_mobile()->enable_comfort_noise(true));
913 EXPECT_TRUE(apm_->echo_control_mobile()->is_comfort_noise_enabled());
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000914 // Set and get echo path
ajm@google.com22e65152011-07-18 18:03:01 +0000915 const size_t echo_path_size =
916 apm_->echo_control_mobile()->echo_path_size_bytes();
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000917 scoped_array<char> echo_path_in(new char[echo_path_size]);
918 scoped_array<char> echo_path_out(new char[echo_path_size]);
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000919 EXPECT_EQ(apm_->kNullPointerError,
920 apm_->echo_control_mobile()->SetEchoPath(NULL, echo_path_size));
921 EXPECT_EQ(apm_->kNullPointerError,
922 apm_->echo_control_mobile()->GetEchoPath(NULL, echo_path_size));
923 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000924 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000925 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000926 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000927 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +0000928 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000929 echo_path_in[i] = echo_path_out[i] + 1;
930 }
931 EXPECT_EQ(apm_->kBadParameterError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000932 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(), 1));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000933 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000934 apm_->echo_control_mobile()->SetEchoPath(echo_path_in.get(),
935 echo_path_size));
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000936 EXPECT_EQ(apm_->kNoError,
andrew@webrtc.org3119ecf2011-11-01 17:00:18 +0000937 apm_->echo_control_mobile()->GetEchoPath(echo_path_out.get(),
938 echo_path_size));
ajm@google.com22e65152011-07-18 18:03:01 +0000939 for (size_t i = 0; i < echo_path_size; i++) {
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000940 EXPECT_EQ(echo_path_in[i], echo_path_out[i]);
941 }
andrew@webrtc.org75f19482012-02-09 17:16:18 +0000942
943 // Process a few frames with NS in the default disabled state. This exercises
944 // a different codepath than with it enabled.
945 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
946 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
947 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
948 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
949
niklase@google.com470e71d2011-07-07 08:21:25 +0000950 // Turn AECM off
951 EXPECT_EQ(apm_->kNoError, apm_->echo_control_mobile()->Enable(false));
952 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
953}
954
955TEST_F(ApmTest, GainControl) {
956 // Testing gain modes
niklase@google.com470e71d2011-07-07 08:21:25 +0000957 EXPECT_EQ(apm_->kNoError,
958 apm_->gain_control()->set_mode(
959 apm_->gain_control()->mode()));
960
961 GainControl::Mode mode[] = {
962 GainControl::kAdaptiveAnalog,
963 GainControl::kAdaptiveDigital,
964 GainControl::kFixedDigital
965 };
966 for (size_t i = 0; i < sizeof(mode)/sizeof(*mode); i++) {
967 EXPECT_EQ(apm_->kNoError,
968 apm_->gain_control()->set_mode(mode[i]));
969 EXPECT_EQ(mode[i], apm_->gain_control()->mode());
970 }
971 // Testing invalid target levels
972 EXPECT_EQ(apm_->kBadParameterError,
973 apm_->gain_control()->set_target_level_dbfs(-3));
974 EXPECT_EQ(apm_->kBadParameterError,
975 apm_->gain_control()->set_target_level_dbfs(-40));
976 // Testing valid target levels
977 EXPECT_EQ(apm_->kNoError,
978 apm_->gain_control()->set_target_level_dbfs(
979 apm_->gain_control()->target_level_dbfs()));
980
981 int level_dbfs[] = {0, 6, 31};
982 for (size_t i = 0; i < sizeof(level_dbfs)/sizeof(*level_dbfs); i++) {
983 EXPECT_EQ(apm_->kNoError,
984 apm_->gain_control()->set_target_level_dbfs(level_dbfs[i]));
985 EXPECT_EQ(level_dbfs[i], apm_->gain_control()->target_level_dbfs());
986 }
987
988 // Testing invalid compression gains
989 EXPECT_EQ(apm_->kBadParameterError,
990 apm_->gain_control()->set_compression_gain_db(-1));
991 EXPECT_EQ(apm_->kBadParameterError,
992 apm_->gain_control()->set_compression_gain_db(100));
993
994 // Testing valid compression gains
995 EXPECT_EQ(apm_->kNoError,
996 apm_->gain_control()->set_compression_gain_db(
997 apm_->gain_control()->compression_gain_db()));
998
999 int gain_db[] = {0, 10, 90};
1000 for (size_t i = 0; i < sizeof(gain_db)/sizeof(*gain_db); i++) {
1001 EXPECT_EQ(apm_->kNoError,
1002 apm_->gain_control()->set_compression_gain_db(gain_db[i]));
1003 EXPECT_EQ(gain_db[i], apm_->gain_control()->compression_gain_db());
1004 }
1005
1006 // Testing limiter off/on
1007 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(false));
1008 EXPECT_FALSE(apm_->gain_control()->is_limiter_enabled());
1009 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->enable_limiter(true));
1010 EXPECT_TRUE(apm_->gain_control()->is_limiter_enabled());
1011
1012 // Testing invalid level limits
1013 EXPECT_EQ(apm_->kBadParameterError,
1014 apm_->gain_control()->set_analog_level_limits(-1, 512));
1015 EXPECT_EQ(apm_->kBadParameterError,
1016 apm_->gain_control()->set_analog_level_limits(100000, 512));
1017 EXPECT_EQ(apm_->kBadParameterError,
1018 apm_->gain_control()->set_analog_level_limits(512, -1));
1019 EXPECT_EQ(apm_->kBadParameterError,
1020 apm_->gain_control()->set_analog_level_limits(512, 100000));
1021 EXPECT_EQ(apm_->kBadParameterError,
1022 apm_->gain_control()->set_analog_level_limits(512, 255));
1023
1024 // Testing valid level limits
1025 EXPECT_EQ(apm_->kNoError,
1026 apm_->gain_control()->set_analog_level_limits(
1027 apm_->gain_control()->analog_level_minimum(),
1028 apm_->gain_control()->analog_level_maximum()));
1029
1030 int min_level[] = {0, 255, 1024};
1031 for (size_t i = 0; i < sizeof(min_level)/sizeof(*min_level); i++) {
1032 EXPECT_EQ(apm_->kNoError,
1033 apm_->gain_control()->set_analog_level_limits(min_level[i], 1024));
1034 EXPECT_EQ(min_level[i], apm_->gain_control()->analog_level_minimum());
1035 }
1036
1037 int max_level[] = {0, 1024, 65535};
1038 for (size_t i = 0; i < sizeof(min_level)/sizeof(*min_level); i++) {
1039 EXPECT_EQ(apm_->kNoError,
1040 apm_->gain_control()->set_analog_level_limits(0, max_level[i]));
1041 EXPECT_EQ(max_level[i], apm_->gain_control()->analog_level_maximum());
1042 }
1043
1044 // TODO(ajm): stream_is_saturated() and stream_analog_level()
1045
1046 // Turn AGC off
1047 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
1048 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1049}
1050
1051TEST_F(ApmTest, NoiseSuppression) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001052 // Test valid suppression levels.
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 NoiseSuppression::Level level[] = {
1054 NoiseSuppression::kLow,
1055 NoiseSuppression::kModerate,
1056 NoiseSuppression::kHigh,
1057 NoiseSuppression::kVeryHigh
1058 };
1059 for (size_t i = 0; i < sizeof(level)/sizeof(*level); i++) {
1060 EXPECT_EQ(apm_->kNoError,
1061 apm_->noise_suppression()->set_level(level[i]));
1062 EXPECT_EQ(level[i], apm_->noise_suppression()->level());
1063 }
1064
andrew@webrtc.org648af742012-02-08 01:57:29 +00001065 // Turn NS on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(true));
1067 EXPECT_TRUE(apm_->noise_suppression()->is_enabled());
1068 EXPECT_EQ(apm_->kNoError, apm_->noise_suppression()->Enable(false));
1069 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1070}
1071
1072TEST_F(ApmTest, HighPassFilter) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001073 // Turn HP filter on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001074 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(true));
1075 EXPECT_TRUE(apm_->high_pass_filter()->is_enabled());
1076 EXPECT_EQ(apm_->kNoError, apm_->high_pass_filter()->Enable(false));
1077 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1078}
1079
1080TEST_F(ApmTest, LevelEstimator) {
andrew@webrtc.org648af742012-02-08 01:57:29 +00001081 // Turn level estimator on/off
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001082 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001084
1085 EXPECT_EQ(apm_->kNotEnabledError, apm_->level_estimator()->RMS());
1086
1087 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1088 EXPECT_TRUE(apm_->level_estimator()->is_enabled());
1089
1090 // Run this test in wideband; in super-wb, the splitting filter distorts the
1091 // audio enough to cause deviation from the expectation for small values.
1092 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(16000));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001093 frame_->samples_per_channel_ = 160;
1094 frame_->num_channels_ = 2;
1095 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001096
1097 // Min value if no frames have been processed.
1098 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1099
1100 // Min value on zero frames.
1101 SetFrameTo(frame_, 0);
1102 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1103 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1104 EXPECT_EQ(127, apm_->level_estimator()->RMS());
1105
1106 // Try a few RMS values.
1107 // (These also test that the value resets after retrieving it.)
1108 SetFrameTo(frame_, 32767);
1109 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1110 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1111 EXPECT_EQ(0, apm_->level_estimator()->RMS());
1112
1113 SetFrameTo(frame_, 30000);
1114 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1115 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1116 EXPECT_EQ(1, apm_->level_estimator()->RMS());
1117
1118 SetFrameTo(frame_, 10000);
1119 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1120 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1121 EXPECT_EQ(10, apm_->level_estimator()->RMS());
1122
1123 SetFrameTo(frame_, 10);
1124 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1125 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1126 EXPECT_EQ(70, apm_->level_estimator()->RMS());
1127
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001128 // Min value if energy_ == 0.
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001129 SetFrameTo(frame_, 10000);
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001130 uint32_t energy = frame_->energy_; // Save default to restore below.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001131 frame_->energy_ = 0;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001132 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1133 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1134 EXPECT_EQ(127, apm_->level_estimator()->RMS());
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001135 frame_->energy_ = energy;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001136
1137 // Verify reset after enable/disable.
1138 SetFrameTo(frame_, 32767);
1139 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1140 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1141 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1142 SetFrameTo(frame_, 1);
1143 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1144 EXPECT_EQ(90, apm_->level_estimator()->RMS());
1145
1146 // Verify reset after initialize.
1147 SetFrameTo(frame_, 32767);
1148 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1149 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
1150 SetFrameTo(frame_, 1);
1151 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1152 EXPECT_EQ(90, apm_->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +00001153}
1154
1155TEST_F(ApmTest, VoiceDetection) {
1156 // Test external VAD
1157 EXPECT_EQ(apm_->kNoError,
1158 apm_->voice_detection()->set_stream_has_voice(true));
1159 EXPECT_TRUE(apm_->voice_detection()->stream_has_voice());
1160 EXPECT_EQ(apm_->kNoError,
1161 apm_->voice_detection()->set_stream_has_voice(false));
1162 EXPECT_FALSE(apm_->voice_detection()->stream_has_voice());
1163
andrew@webrtc.org648af742012-02-08 01:57:29 +00001164 // Test valid likelihoods
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 VoiceDetection::Likelihood likelihood[] = {
1166 VoiceDetection::kVeryLowLikelihood,
1167 VoiceDetection::kLowLikelihood,
1168 VoiceDetection::kModerateLikelihood,
1169 VoiceDetection::kHighLikelihood
1170 };
1171 for (size_t i = 0; i < sizeof(likelihood)/sizeof(*likelihood); i++) {
1172 EXPECT_EQ(apm_->kNoError,
1173 apm_->voice_detection()->set_likelihood(likelihood[i]));
1174 EXPECT_EQ(likelihood[i], apm_->voice_detection()->likelihood());
1175 }
1176
1177 /* TODO(bjornv): Enable once VAD supports other frame lengths than 10 ms
andrew@webrtc.org648af742012-02-08 01:57:29 +00001178 // Test invalid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001179 EXPECT_EQ(apm_->kBadParameterError,
1180 apm_->voice_detection()->set_frame_size_ms(12));
1181
andrew@webrtc.org648af742012-02-08 01:57:29 +00001182 // Test valid frame sizes
niklase@google.com470e71d2011-07-07 08:21:25 +00001183 for (int i = 10; i <= 30; i += 10) {
1184 EXPECT_EQ(apm_->kNoError,
1185 apm_->voice_detection()->set_frame_size_ms(i));
1186 EXPECT_EQ(i, apm_->voice_detection()->frame_size_ms());
1187 }
1188 */
1189
andrew@webrtc.org648af742012-02-08 01:57:29 +00001190 // Turn VAD on/off
niklase@google.com470e71d2011-07-07 08:21:25 +00001191 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1192 EXPECT_TRUE(apm_->voice_detection()->is_enabled());
1193 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1194 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1195
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001196 // Test that AudioFrame activity is maintained when VAD is disabled.
1197 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1198 AudioFrame::VADActivity activity[] = {
1199 AudioFrame::kVadActive,
1200 AudioFrame::kVadPassive,
1201 AudioFrame::kVadUnknown
1202 };
1203 for (size_t i = 0; i < sizeof(activity)/sizeof(*activity); i++) {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001204 frame_->vad_activity_ = activity[i];
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001205 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001206 EXPECT_EQ(activity[i], frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001207 }
1208
1209 // Test that AudioFrame activity is set when VAD is enabled.
1210 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001211 frame_->vad_activity_ = AudioFrame::kVadUnknown;
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001212 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001213 EXPECT_NE(AudioFrame::kVadUnknown, frame_->vad_activity_);
andrew@webrtc.orged083d42011-09-19 15:28:51 +00001214
niklase@google.com470e71d2011-07-07 08:21:25 +00001215 // TODO(bjornv): Add tests for streamed voice; stream_has_voice()
1216}
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001217
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001218TEST_F(ApmTest, VerifyDownMixing) {
1219 for (size_t i = 0; i < kSampleRatesSize; i++) {
1220 Init(kSampleRates[i], 2, 2, 1, false);
1221 SetFrameTo(frame_, 1000, 2000);
1222 AudioFrame mono_frame;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001223 mono_frame.samples_per_channel_ = frame_->samples_per_channel_;
1224 mono_frame.num_channels_ = 1;
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001225 SetFrameTo(&mono_frame, 1500);
1226 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1227 EXPECT_TRUE(FrameDataAreEqual(*frame_, mono_frame));
1228 }
1229}
1230
1231TEST_F(ApmTest, AllProcessingDisabledByDefault) {
1232 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
1233 EXPECT_FALSE(apm_->echo_control_mobile()->is_enabled());
1234 EXPECT_FALSE(apm_->gain_control()->is_enabled());
1235 EXPECT_FALSE(apm_->high_pass_filter()->is_enabled());
1236 EXPECT_FALSE(apm_->level_estimator()->is_enabled());
1237 EXPECT_FALSE(apm_->noise_suppression()->is_enabled());
1238 EXPECT_FALSE(apm_->voice_detection()->is_enabled());
1239}
1240
1241TEST_F(ApmTest, NoProcessingWhenAllComponentsDisabled) {
1242 for (size_t i = 0; i < kSampleRatesSize; i++) {
1243 Init(kSampleRates[i], 2, 2, 2, false);
1244 SetFrameTo(frame_, 1000, 2000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001245 AudioFrame frame_copy;
1246 frame_copy.CopyFrom(*frame_);
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001247 for (int j = 0; j < 1000; j++) {
1248 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1249 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1250 }
1251 }
1252}
1253
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001254TEST_F(ApmTest, IdenticalInputChannelsResultInIdenticalOutputChannels) {
1255 EnableAllComponents();
1256
1257 for (size_t i = 0; i < kProcessSampleRatesSize; i++) {
1258 Init(kProcessSampleRates[i], 2, 2, 2, false);
1259 int analog_level = 127;
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001260 EXPECT_EQ(0, feof(far_file_));
1261 EXPECT_EQ(0, feof(near_file_));
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001262 while (1) {
1263 if (!ReadFrame(far_file_, revframe_)) break;
1264 CopyLeftToRightChannel(revframe_->data_, revframe_->samples_per_channel_);
1265
1266 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(revframe_));
1267
1268 if (!ReadFrame(near_file_, frame_)) break;
1269 CopyLeftToRightChannel(frame_->data_, frame_->samples_per_channel_);
1270 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1271
1272 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1273 EXPECT_EQ(apm_->kNoError,
1274 apm_->echo_cancellation()->set_stream_drift_samples(0));
1275 EXPECT_EQ(apm_->kNoError,
1276 apm_->gain_control()->set_stream_analog_level(analog_level));
1277 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1278 analog_level = apm_->gain_control()->stream_analog_level();
1279
1280 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1281 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001282 rewind(far_file_);
1283 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001284 }
1285}
1286
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001287TEST_F(ApmTest, SplittingFilter) {
1288 // Verify the filter is not active through undistorted audio when:
1289 // 1. No components are enabled...
1290 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001291 AudioFrame frame_copy;
1292 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001293 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1294 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1295 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1296
1297 // 2. Only the level estimator is enabled...
1298 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001299 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001300 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1301 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1302 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1303 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1304 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1305
1306 // 3. Only VAD is enabled...
1307 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001308 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001309 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1310 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1311 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1312 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1313 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1314
1315 // 4. Both VAD and the level estimator are enabled...
1316 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001317 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001318 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1319 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1320 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1321 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1322 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1323 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1324 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1325
1326 // 5. Not using super-wb.
1327 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(16000));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001328 frame_->samples_per_channel_ = 160;
1329 frame_->num_channels_ = 2;
1330 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001331 // Enable AEC, which would require the filter in super-wb. We rely on the
1332 // first few frames of data being unaffected by the AEC.
1333 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1334 // behavior of the AEC. Think of something more robust.
1335 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
1336 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001337 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001338 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1339 EXPECT_EQ(apm_->kNoError,
1340 apm_->echo_cancellation()->set_stream_drift_samples(0));
1341 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1342 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1343 EXPECT_EQ(apm_->kNoError,
1344 apm_->echo_cancellation()->set_stream_drift_samples(0));
1345 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1346 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1347
1348 // Check the test is valid. We should have distortion from the filter
1349 // when AEC is enabled (which won't affect the audio).
1350 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(32000));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001351 frame_->samples_per_channel_ = 320;
1352 frame_->num_channels_ = 2;
1353 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001354 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001355 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001356 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1357 EXPECT_EQ(apm_->kNoError,
1358 apm_->echo_cancellation()->set_stream_drift_samples(0));
1359 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1360 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1361}
1362
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001363// TODO(andrew): expand test to verify output.
1364TEST_F(ApmTest, DebugDump) {
1365 const std::string filename = webrtc::test::OutputPath() + "debug.aec";
1366 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(NULL));
1367
1368#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1369 // Stopping without having started should be OK.
1370 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1371
1372 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str()));
1373 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(revframe_));
1374 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1375 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1376
1377 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001378 FILE* fid = fopen(filename.c_str(), "r");
1379 ASSERT_TRUE(fid != NULL);
1380
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001381 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001382 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001383 ASSERT_EQ(0, remove(filename.c_str()));
1384#else
1385 EXPECT_EQ(apm_->kUnsupportedFunctionError,
1386 apm_->StartDebugRecording(filename.c_str()));
1387 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1388
1389 // Verify the file has NOT been written.
1390 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1391#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1392}
1393
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001394// TODO(andrew): Add a test to process a few frames with different combinations
1395// of enabled components.
1396
andrew@webrtc.orge2ed5ba2012-01-20 19:06:38 +00001397// TODO(andrew): Make this test more robust such that it can be run on multiple
1398// platforms. It currently requires bit-exactness.
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001399#ifdef WEBRTC_AUDIOPROC_BIT_EXACT
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001400TEST_F(ApmTest, Process) {
1401 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001402 webrtc::audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001403
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001404 if (!write_ref_data) {
1405 ReadMessageLiteFromFile(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001406 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001407 // Write the desired tests to the protobuf reference file.
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001408 for (size_t i = 0; i < kChannelsSize; i++) {
1409 for (size_t j = 0; j < kChannelsSize; j++) {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001410 // We can't have more output than input channels.
1411 for (size_t k = 0; k <= j; k++) {
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001412 for (size_t l = 0; l < kProcessSampleRatesSize; l++) {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001413 webrtc::audioproc::Test* test = ref_data.add_test();
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001414 test->set_num_reverse_channels(kChannels[i]);
1415 test->set_num_input_channels(kChannels[j]);
1416 test->set_num_output_channels(kChannels[k]);
1417 test->set_sample_rate(kProcessSampleRates[l]);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001418 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001419 }
1420 }
1421 }
1422 }
1423
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001424 EnableAllComponents();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001425
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001426 for (int i = 0; i < ref_data.test_size(); i++) {
1427 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001428
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001429 webrtc::audioproc::Test* test = ref_data.mutable_test(i);
1430 Init(test->sample_rate(), test->num_reverse_channels(),
1431 test->num_input_channels(), test->num_output_channels(), true);
1432
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001433 int frame_count = 0;
1434 int has_echo_count = 0;
1435 int has_voice_count = 0;
1436 int is_saturated_count = 0;
1437 int analog_level = 127;
1438 int analog_level_average = 0;
1439 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001440 float ns_speech_prob_average = 0.0f;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001441
1442 while (1) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001443 if (!ReadFrame(far_file_, revframe_)) break;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001444 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(revframe_));
1445
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001446 if (!ReadFrame(near_file_, frame_)) break;
1447 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1448
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001449 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
1450 EXPECT_EQ(apm_->kNoError,
1451 apm_->echo_cancellation()->set_stream_drift_samples(0));
1452 EXPECT_EQ(apm_->kNoError,
1453 apm_->gain_control()->set_stream_analog_level(analog_level));
1454
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001455 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001456 // Ensure the frame was downmixed properly.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001457 EXPECT_EQ(test->num_output_channels(), frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001458
1459 max_output_average += MaxAudioFrame(*frame_);
1460
1461 if (apm_->echo_cancellation()->stream_has_echo()) {
1462 has_echo_count++;
1463 }
1464
1465 analog_level = apm_->gain_control()->stream_analog_level();
1466 analog_level_average += analog_level;
1467 if (apm_->gain_control()->stream_is_saturated()) {
1468 is_saturated_count++;
1469 }
1470 if (apm_->voice_detection()->stream_has_voice()) {
1471 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001472 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001473 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001474 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001475 }
1476
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001477 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
1478
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001479 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001480 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001481 sizeof(int16_t),
1482 frame_size,
1483 out_file_);
1484 ASSERT_EQ(frame_size, write_count);
1485
1486 // Reset in case of downmixing.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001487 frame_->num_channels_ = test->num_input_channels();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001488 frame_count++;
1489 }
1490 max_output_average /= frame_count;
1491 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001492 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001493
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001494#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001495 EchoCancellation::Metrics echo_metrics;
1496 EXPECT_EQ(apm_->kNoError,
1497 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
1498 int median = 0;
1499 int std = 0;
1500 EXPECT_EQ(apm_->kNoError,
1501 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
1502
1503 int rms_level = apm_->level_estimator()->RMS();
1504 EXPECT_LE(0, rms_level);
1505 EXPECT_GE(127, rms_level);
1506#endif
1507
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001508 if (!write_ref_data) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001509 EXPECT_EQ(test->has_echo_count(), has_echo_count);
1510 EXPECT_EQ(test->has_voice_count(), has_voice_count);
1511 EXPECT_EQ(test->is_saturated_count(), is_saturated_count);
1512
1513 EXPECT_EQ(test->analog_level_average(), analog_level_average);
1514 EXPECT_EQ(test->max_output_average(), max_output_average);
1515
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001516#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001517 webrtc::audioproc::Test::EchoMetrics reference =
1518 test->echo_metrics();
1519 TestStats(echo_metrics.residual_echo_return_loss,
1520 reference.residual_echo_return_loss());
1521 TestStats(echo_metrics.echo_return_loss,
1522 reference.echo_return_loss());
1523 TestStats(echo_metrics.echo_return_loss_enhancement,
1524 reference.echo_return_loss_enhancement());
1525 TestStats(echo_metrics.a_nlp,
1526 reference.a_nlp());
1527
1528 webrtc::audioproc::Test::DelayMetrics reference_delay =
1529 test->delay_metrics();
andrew@webrtc.org828af1b2011-11-22 22:40:27 +00001530 EXPECT_EQ(reference_delay.median(), median);
1531 EXPECT_EQ(reference_delay.std(), std);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001532
1533 EXPECT_EQ(test->rms_level(), rms_level);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001534
1535 EXPECT_FLOAT_EQ(test->ns_speech_probability_average(),
1536 ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001537#endif
1538 } else {
1539 test->set_has_echo_count(has_echo_count);
1540 test->set_has_voice_count(has_voice_count);
1541 test->set_is_saturated_count(is_saturated_count);
1542
1543 test->set_analog_level_average(analog_level_average);
1544 test->set_max_output_average(max_output_average);
1545
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001546#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001547 webrtc::audioproc::Test::EchoMetrics* message =
1548 test->mutable_echo_metrics();
1549 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
1550 message->mutable_residual_echo_return_loss());
1551 WriteStatsMessage(echo_metrics.echo_return_loss,
1552 message->mutable_echo_return_loss());
1553 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
1554 message->mutable_echo_return_loss_enhancement());
1555 WriteStatsMessage(echo_metrics.a_nlp,
1556 message->mutable_a_nlp());
1557
1558 webrtc::audioproc::Test::DelayMetrics* message_delay =
1559 test->mutable_delay_metrics();
1560 message_delay->set_median(median);
1561 message_delay->set_std(std);
1562
1563 test->set_rms_level(rms_level);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001564
1565 EXPECT_LE(0.0f, ns_speech_prob_average);
1566 EXPECT_GE(1.0f, ns_speech_prob_average);
1567 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001568#endif
1569 }
1570
1571 rewind(far_file_);
1572 rewind(near_file_);
1573 }
1574
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001575 if (write_ref_data) {
1576 WriteMessageLiteToFile(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001577 }
1578}
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001579#endif // WEBRTC_AUDIOPROC_BIT_EXACT
andrew@webrtc.orge2ed5ba2012-01-20 19:06:38 +00001580
niklase@google.com470e71d2011-07-07 08:21:25 +00001581} // namespace
1582
1583int main(int argc, char** argv) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001584 for (int i = 1; i < argc; i++) {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001585 if (strcmp(argv[i], "--write_ref_data") == 0) {
1586 write_ref_data = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00001587 }
1588 }
1589
andrew@webrtc.org28d01402012-10-18 00:42:32 +00001590 // We don't use TestSuite here because it would require the Android platform
1591 // build to depend on Gmock.
1592 webrtc::test::SetExecutablePath(argv[0]);
1593 testing::InitGoogleTest(&argc, argv);
1594 int result = RUN_ALL_TESTS();
andrew@webrtc.org64235092011-08-19 21:22:08 +00001595 // Optional, but removes memory leak noise from Valgrind.
1596 google::protobuf::ShutdownProtobufLibrary();
andrew@webrtc.org28d01402012-10-18 00:42:32 +00001597 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +00001598}