blob: 5866a5fd020994c432250b553cf3e4722915afb5 [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));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000447 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000448 EXPECT_EQ(apm_->kNoError,
449 apm_->gain_control()->set_stream_analog_level(127));
450 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame));
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000451}
452
andrew@webrtc.org81865342012-10-27 00:28:27 +0000453template <typename F>
454void ApmTest::ChangeTriggersInit(F f, AudioProcessing* ap, int initial_value,
455 int changed_value) {
456 EnableAllComponents();
457 Init(16000, 2, 2, 2, false);
458 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000459 AudioFrame frame_copy;
460 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000461 ProcessWithDefaultStreamParameters(frame_);
462 // Verify the processing has actually changed the frame.
463 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
464
465 // Test that a change in value triggers an init.
466 f(apm_, changed_value);
467 f(apm_, initial_value);
468 ProcessWithDefaultStreamParameters(&frame_copy);
469 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
470
471 apm_->Initialize();
472 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000473 AudioFrame initial_frame;
474 initial_frame.CopyFrom(*frame_);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000475 ProcessWithDefaultStreamParameters(frame_);
476 ProcessWithDefaultStreamParameters(frame_);
477 // Verify the processing has actually changed the frame.
478 EXPECT_FALSE(FrameDataAreEqual(*frame_, initial_frame));
479
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000480 frame_copy.CopyFrom(initial_frame);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000481 apm_->Initialize();
482 ProcessWithDefaultStreamParameters(&frame_copy);
483 // Verify an init here would result in different output.
484 apm_->Initialize();
485 ProcessWithDefaultStreamParameters(&frame_copy);
486 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
487
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000488 frame_copy.CopyFrom(initial_frame);
andrew@webrtc.org81865342012-10-27 00:28:27 +0000489 apm_->Initialize();
490 ProcessWithDefaultStreamParameters(&frame_copy);
491 // Test that the same value does not trigger an init.
492 f(apm_, initial_value);
493 ProcessWithDefaultStreamParameters(&frame_copy);
494 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +0000495}
496
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000497void ApmTest::ProcessDelayVerificationTest(int delay_ms, int system_delay_ms,
498 int delay_min, int delay_max) {
499 // The |revframe_| and |frame_| should include the proper frame information,
500 // hence can be used for extracting information.
501 webrtc::AudioFrame tmp_frame;
502 std::queue<webrtc::AudioFrame*> frame_queue;
503 bool causal = true;
504
505 tmp_frame.CopyFrom(*revframe_);
506 SetFrameTo(&tmp_frame, 0);
507
508 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
509 // Initialize the |frame_queue| with empty frames.
510 int frame_delay = delay_ms / 10;
511 while (frame_delay < 0) {
512 webrtc::AudioFrame* frame = new AudioFrame();
513 frame->CopyFrom(tmp_frame);
514 frame_queue.push(frame);
515 frame_delay++;
516 causal = false;
517 }
518 while (frame_delay > 0) {
519 webrtc::AudioFrame* frame = new AudioFrame();
520 frame->CopyFrom(tmp_frame);
521 frame_queue.push(frame);
522 frame_delay--;
523 }
524 // Run for 4.5 seconds, skipping statistics from the first second. We need
525 // enough frames with audio to have reliable estimates, but as few as possible
526 // to keep processing time down. 4.5 seconds seemed to be a good compromise
527 // for this recording.
528 for (int frame_count = 0; frame_count < 450; ++frame_count) {
529 webrtc::AudioFrame* frame = new AudioFrame();
530 frame->CopyFrom(tmp_frame);
531 // Use the near end recording, since that has more speech in it.
532 ASSERT_TRUE(ReadFrame(near_file_, frame));
533 frame_queue.push(frame);
534 webrtc::AudioFrame* reverse_frame = frame;
535 webrtc::AudioFrame* process_frame = frame_queue.front();
536 if (!causal) {
537 reverse_frame = frame_queue.front();
538 // When we call ProcessStream() the frame is modified, so we can't use the
539 // pointer directly when things are non-causal. Use an intermediate frame
540 // and copy the data.
541 process_frame = &tmp_frame;
542 process_frame->CopyFrom(*frame);
543 }
544 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(reverse_frame));
545 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(system_delay_ms));
546 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(process_frame));
547 frame = frame_queue.front();
548 frame_queue.pop();
549 delete frame;
550
551 if (frame_count == 100) {
552 int median;
553 int std;
554 // Discard the first delay metrics to avoid convergence effects.
555 EXPECT_EQ(apm_->kNoError,
556 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
557 }
558 }
559
560 rewind(near_file_);
561 while (!frame_queue.empty()) {
562 webrtc::AudioFrame* frame = frame_queue.front();
563 frame_queue.pop();
564 delete frame;
565 }
566 // Calculate expected delay estimate and acceptable regions. Further,
567 // limit them w.r.t. AEC delay estimation support.
568 const int samples_per_ms = std::min(16, frame_->samples_per_channel_ / 10);
569 int expected_median = std::min(std::max(delay_ms - system_delay_ms,
570 delay_min), delay_max);
571 int expected_median_high = std::min(std::max(
572 expected_median + 96 / samples_per_ms, delay_min), delay_max);
573 int expected_median_low = std::min(std::max(
574 expected_median - 96 / samples_per_ms, delay_min), delay_max);
575 // Verify delay metrics.
576 int median;
577 int std;
578 EXPECT_EQ(apm_->kNoError,
579 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
580 EXPECT_GE(expected_median_high, median);
581 EXPECT_LE(expected_median_low, median);
582}
583
niklase@google.com470e71d2011-07-07 08:21:25 +0000584TEST_F(ApmTest, StreamParameters) {
585 // No errors when the components are disabled.
586 EXPECT_EQ(apm_->kNoError,
587 apm_->ProcessStream(frame_));
588
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000589 // -- Missing AGC level --
niklase@google.com470e71d2011-07-07 08:21:25 +0000590 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
591 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000592 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000594 // Resets after successful ProcessStream().
niklase@google.com470e71d2011-07-07 08:21:25 +0000595 EXPECT_EQ(apm_->kNoError,
596 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000597 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
598 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000599
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000600 // Other stream parameters set correctly.
601 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
niklase@google.com470e71d2011-07-07 08:21:25 +0000602 EXPECT_EQ(apm_->kNoError,
603 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000604 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000605 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000606 EXPECT_EQ(apm_->kStreamParameterNotSetError,
607 apm_->ProcessStream(frame_));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000608 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
609 EXPECT_EQ(apm_->kNoError,
610 apm_->echo_cancellation()->enable_drift_compensation(false));
611
612 // -- Missing delay --
613 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
614 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
615 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
616
617 // Resets after successful ProcessStream().
618 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
619 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
620 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
621
622 // Other stream parameters set correctly.
623 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
624 EXPECT_EQ(apm_->kNoError,
625 apm_->echo_cancellation()->enable_drift_compensation(true));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000626 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000627 EXPECT_EQ(apm_->kNoError,
628 apm_->gain_control()->set_stream_analog_level(127));
629 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
630 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(false));
631
632 // -- Missing drift --
633 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
634 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
635
636 // Resets after successful ProcessStream().
637 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000638 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000639 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
640 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
641
642 // Other stream parameters set correctly.
niklase@google.com470e71d2011-07-07 08:21:25 +0000643 EXPECT_EQ(apm_->kNoError, apm_->gain_control()->Enable(true));
644 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
645 EXPECT_EQ(apm_->kNoError,
646 apm_->gain_control()->set_stream_analog_level(127));
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000647 EXPECT_EQ(apm_->kStreamParameterNotSetError, apm_->ProcessStream(frame_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000648
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000649 // -- No stream parameters --
niklase@google.com470e71d2011-07-07 08:21:25 +0000650 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
651 EXPECT_EQ(apm_->kNoError,
652 apm_->AnalyzeReverseStream(revframe_));
653 EXPECT_EQ(apm_->kStreamParameterNotSetError,
654 apm_->ProcessStream(frame_));
655
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000656 // -- All there --
niklase@google.com470e71d2011-07-07 08:21:25 +0000657 EXPECT_EQ(apm_->kNoError, apm_->Initialize());
658 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000659 apm_->echo_cancellation()->set_stream_drift_samples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000660 EXPECT_EQ(apm_->kNoError,
661 apm_->gain_control()->set_stream_analog_level(127));
662 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
663}
664
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000665TEST_F(ApmTest, DefaultDelayOffsetIsZero) {
666 EXPECT_EQ(0, apm_->delay_offset_ms());
667 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(50));
668 EXPECT_EQ(50, apm_->stream_delay_ms());
669}
670
671TEST_F(ApmTest, DelayOffsetWithLimitsIsSetProperly) {
672 // High limit of 500 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000673 apm_->set_delay_offset_ms(100);
674 EXPECT_EQ(100, apm_->delay_offset_ms());
675 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(450));
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000676 EXPECT_EQ(500, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000677 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
678 EXPECT_EQ(200, apm_->stream_delay_ms());
679
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000680 // Low limit of 0 ms.
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000681 apm_->set_delay_offset_ms(-50);
682 EXPECT_EQ(-50, apm_->delay_offset_ms());
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000683 EXPECT_EQ(apm_->kBadStreamParameterWarning, apm_->set_stream_delay_ms(20));
684 EXPECT_EQ(0, apm_->stream_delay_ms());
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000685 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(100));
686 EXPECT_EQ(50, apm_->stream_delay_ms());
687}
688
niklase@google.com470e71d2011-07-07 08:21:25 +0000689TEST_F(ApmTest, Channels) {
690 // Testing number of invalid channels
691 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(0, 1));
692 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(1, 0));
693 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(3, 1));
694 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(1, 3));
695 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_reverse_channels(0));
696 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_reverse_channels(3));
697 // Testing number of valid channels
698 for (int i = 1; i < 3; i++) {
699 for (int j = 1; j < 3; j++) {
700 if (j > i) {
701 EXPECT_EQ(apm_->kBadParameterError, apm_->set_num_channels(i, j));
702 } else {
703 EXPECT_EQ(apm_->kNoError, apm_->set_num_channels(i, j));
704 EXPECT_EQ(j, apm_->num_output_channels());
705 }
706 }
707 EXPECT_EQ(i, apm_->num_input_channels());
708 EXPECT_EQ(apm_->kNoError, apm_->set_num_reverse_channels(i));
709 EXPECT_EQ(i, apm_->num_reverse_channels());
710 }
711}
712
713TEST_F(ApmTest, SampleRates) {
714 // Testing invalid sample rates
715 EXPECT_EQ(apm_->kBadParameterError, apm_->set_sample_rate_hz(10000));
716 // Testing valid sample rates
717 int fs[] = {8000, 16000, 32000};
718 for (size_t i = 0; i < sizeof(fs) / sizeof(*fs); i++) {
719 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(fs[i]));
720 EXPECT_EQ(fs[i], apm_->sample_rate_hz());
721 }
722}
723
andrew@webrtc.org81865342012-10-27 00:28:27 +0000724void SetSampleRate(AudioProcessing* ap, int value) {
725 EXPECT_EQ(ap->kNoError, ap->set_sample_rate_hz(value));
726}
727
728void SetNumReverseChannels(AudioProcessing* ap, int value) {
729 EXPECT_EQ(ap->kNoError, ap->set_num_reverse_channels(value));
730}
731
732void SetNumOutputChannels(AudioProcessing* ap, int value) {
733 EXPECT_EQ(ap->kNoError, ap->set_num_channels(2, value));
734}
735
736TEST_F(ApmTest, SampleRateChangeTriggersInit) {
737 ChangeTriggersInit(SetSampleRate, apm_, 16000, 8000);
738}
739
740TEST_F(ApmTest, ReverseChannelChangeTriggersInit) {
741 ChangeTriggersInit(SetNumReverseChannels, apm_, 2, 1);
742}
743
744TEST_F(ApmTest, ChannelChangeTriggersInit) {
745 ChangeTriggersInit(SetNumOutputChannels, apm_, 2, 1);
746}
niklase@google.com470e71d2011-07-07 08:21:25 +0000747
748TEST_F(ApmTest, EchoCancellation) {
749 EXPECT_EQ(apm_->kNoError,
750 apm_->echo_cancellation()->enable_drift_compensation(true));
751 EXPECT_TRUE(apm_->echo_cancellation()->is_drift_compensation_enabled());
752 EXPECT_EQ(apm_->kNoError,
753 apm_->echo_cancellation()->enable_drift_compensation(false));
754 EXPECT_FALSE(apm_->echo_cancellation()->is_drift_compensation_enabled());
755
756 EXPECT_EQ(apm_->kBadParameterError,
757 apm_->echo_cancellation()->set_device_sample_rate_hz(4000));
758 EXPECT_EQ(apm_->kBadParameterError,
759 apm_->echo_cancellation()->set_device_sample_rate_hz(100000));
760
761 int rate[] = {16000, 44100, 48000};
762 for (size_t i = 0; i < sizeof(rate)/sizeof(*rate); i++) {
763 EXPECT_EQ(apm_->kNoError,
764 apm_->echo_cancellation()->set_device_sample_rate_hz(rate[i]));
765 EXPECT_EQ(rate[i],
766 apm_->echo_cancellation()->device_sample_rate_hz());
767 }
768
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 EchoCancellation::SuppressionLevel level[] = {
770 EchoCancellation::kLowSuppression,
771 EchoCancellation::kModerateSuppression,
772 EchoCancellation::kHighSuppression,
773 };
774 for (size_t i = 0; i < sizeof(level)/sizeof(*level); i++) {
775 EXPECT_EQ(apm_->kNoError,
776 apm_->echo_cancellation()->set_suppression_level(level[i]));
777 EXPECT_EQ(level[i],
778 apm_->echo_cancellation()->suppression_level());
779 }
780
781 EchoCancellation::Metrics metrics;
782 EXPECT_EQ(apm_->kNotEnabledError,
783 apm_->echo_cancellation()->GetMetrics(&metrics));
784
785 EXPECT_EQ(apm_->kNoError,
786 apm_->echo_cancellation()->enable_metrics(true));
787 EXPECT_TRUE(apm_->echo_cancellation()->are_metrics_enabled());
788 EXPECT_EQ(apm_->kNoError,
789 apm_->echo_cancellation()->enable_metrics(false));
790 EXPECT_FALSE(apm_->echo_cancellation()->are_metrics_enabled());
791
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000792 int median = 0;
793 int std = 0;
794 EXPECT_EQ(apm_->kNotEnabledError,
795 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
796
797 EXPECT_EQ(apm_->kNoError,
798 apm_->echo_cancellation()->enable_delay_logging(true));
799 EXPECT_TRUE(apm_->echo_cancellation()->is_delay_logging_enabled());
800 EXPECT_EQ(apm_->kNoError,
801 apm_->echo_cancellation()->enable_delay_logging(false));
802 EXPECT_FALSE(apm_->echo_cancellation()->is_delay_logging_enabled());
803
niklase@google.com470e71d2011-07-07 08:21:25 +0000804 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
805 EXPECT_TRUE(apm_->echo_cancellation()->is_enabled());
806 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(false));
807 EXPECT_FALSE(apm_->echo_cancellation()->is_enabled());
808}
809
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000810TEST_F(ApmTest, EchoCancellationReportsCorrectDelays) {
811 // Enable AEC only.
812 EXPECT_EQ(apm_->kNoError,
813 apm_->echo_cancellation()->enable_drift_compensation(false));
814 EXPECT_EQ(apm_->kNoError,
815 apm_->echo_cancellation()->enable_metrics(false));
816 EXPECT_EQ(apm_->kNoError,
817 apm_->echo_cancellation()->enable_delay_logging(true));
818 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
819
820 // Internally in the AEC the amount of lookahead the delay estimation can
821 // handle is 15 blocks and the maximum delay is set to 60 blocks.
822 const int kLookaheadBlocks = 15;
823 const int kMaxDelayBlocks = 60;
824 // The AEC has a startup time before it actually starts to process. This
825 // procedure can flush the internal far-end buffer, which of course affects
826 // the delay estimation. Therefore, we set a system_delay high enough to
827 // avoid that. The smallest system_delay you can report without flushing the
828 // buffer is 66 ms in 8 kHz.
829 //
830 // It is known that for 16 kHz (and 32 kHz) sampling frequency there is an
831 // additional stuffing of 8 ms on the fly, but it seems to have no impact on
832 // delay estimation. This should be noted though. In case of test failure,
833 // this could be the cause.
834 const int kSystemDelayMs = 66;
835 // Test a couple of corner cases and verify that the estimated delay is
836 // within a valid region (set to +-1.5 blocks). Note that these cases are
837 // sampling frequency dependent.
838 for (size_t i = 0; i < kProcessSampleRatesSize; i++) {
839 Init(kProcessSampleRates[i], 2, 2, 2, false);
840 // Sampling frequency dependent variables.
841 const int num_ms_per_block = std::max(4,
842 640 / frame_->samples_per_channel_);
843 const int delay_min_ms = -kLookaheadBlocks * num_ms_per_block;
844 const int delay_max_ms = (kMaxDelayBlocks - 1) * num_ms_per_block;
845
846 // 1) Verify correct delay estimate at lookahead boundary.
847 int delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_min_ms);
848 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
849 delay_max_ms);
850 // 2) A delay less than maximum lookahead should give an delay estimate at
851 // the boundary (= -kLookaheadBlocks * num_ms_per_block).
852 delay_ms -= 20;
853 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
854 delay_max_ms);
855 // 3) Three values around zero delay. Note that we need to compensate for
856 // the fake system_delay.
857 delay_ms = TruncateToMultipleOf10(kSystemDelayMs - 10);
858 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
859 delay_max_ms);
860 delay_ms = TruncateToMultipleOf10(kSystemDelayMs);
861 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
862 delay_max_ms);
863 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + 10);
864 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
865 delay_max_ms);
866 // 4) Verify correct delay estimate at maximum delay boundary.
867 delay_ms = TruncateToMultipleOf10(kSystemDelayMs + delay_max_ms);
868 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
869 delay_max_ms);
870 // 5) A delay above the maximum delay should give an estimate at the
871 // boundary (= (kMaxDelayBlocks - 1) * num_ms_per_block).
872 delay_ms += 20;
873 ProcessDelayVerificationTest(delay_ms, kSystemDelayMs, delay_min_ms,
874 delay_max_ms);
875 }
876}
877
niklase@google.com470e71d2011-07-07 08:21:25 +0000878TEST_F(ApmTest, EchoControlMobile) {
879 // AECM won't use super-wideband.
880 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(32000));
bjornv@webrtc.org3e102492013-02-14 15:29:09 +0000881 EXPECT_EQ(apm_->kBadSampleRateError,
882 apm_->echo_control_mobile()->Enable(true));
andrew@webrtc.org78693fe2013-03-01 16:36:19 +0000883 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(16000));
884 EXPECT_EQ(apm_->kNoError,
885 apm_->echo_control_mobile()->Enable(true));
886 EXPECT_EQ(apm_->kUnsupportedComponentError, apm_->set_sample_rate_hz(32000));
887
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));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001273 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001274 EXPECT_EQ(apm_->kNoError,
1275 apm_->gain_control()->set_stream_analog_level(analog_level));
1276 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1277 analog_level = apm_->gain_control()->stream_analog_level();
1278
1279 VerifyChannelsAreEqual(frame_->data_, frame_->samples_per_channel_);
1280 }
bjornv@webrtc.org3e102492013-02-14 15:29:09 +00001281 rewind(far_file_);
1282 rewind(near_file_);
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001283 }
1284}
1285
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001286TEST_F(ApmTest, SplittingFilter) {
1287 // Verify the filter is not active through undistorted audio when:
1288 // 1. No components are enabled...
1289 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001290 AudioFrame frame_copy;
1291 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001292 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1293 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1294 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1295
1296 // 2. Only the level estimator is enabled...
1297 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001298 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001299 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1300 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1301 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1302 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1303 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1304
1305 // 3. Only VAD is enabled...
1306 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001307 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001308 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1309 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1310 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1311 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1312 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1313
1314 // 4. Both VAD and the level estimator are enabled...
1315 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001316 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001317 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(true));
1318 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(true));
1319 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1320 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1321 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1322 EXPECT_EQ(apm_->kNoError, apm_->level_estimator()->Enable(false));
1323 EXPECT_EQ(apm_->kNoError, apm_->voice_detection()->Enable(false));
1324
1325 // 5. Not using super-wb.
1326 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(16000));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001327 frame_->samples_per_channel_ = 160;
1328 frame_->num_channels_ = 2;
1329 frame_->sample_rate_hz_ = 16000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001330 // Enable AEC, which would require the filter in super-wb. We rely on the
1331 // first few frames of data being unaffected by the AEC.
1332 // TODO(andrew): This test, and the one below, rely rather tenuously on the
1333 // behavior of the AEC. Think of something more robust.
1334 EXPECT_EQ(apm_->kNoError, apm_->echo_cancellation()->Enable(true));
1335 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001336 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001337 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001338 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001339 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1340 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001341 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001342 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1343 EXPECT_TRUE(FrameDataAreEqual(*frame_, frame_copy));
1344
1345 // Check the test is valid. We should have distortion from the filter
1346 // when AEC is enabled (which won't affect the audio).
1347 EXPECT_EQ(apm_->kNoError, apm_->set_sample_rate_hz(32000));
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001348 frame_->samples_per_channel_ = 320;
1349 frame_->num_channels_ = 2;
1350 frame_->sample_rate_hz_ = 32000;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001351 SetFrameTo(frame_, 1000);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00001352 frame_copy.CopyFrom(*frame_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001353 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001354 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001355 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1356 EXPECT_FALSE(FrameDataAreEqual(*frame_, frame_copy));
1357}
1358
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001359// TODO(andrew): expand test to verify output.
1360TEST_F(ApmTest, DebugDump) {
1361 const std::string filename = webrtc::test::OutputPath() + "debug.aec";
1362 EXPECT_EQ(apm_->kNullPointerError, apm_->StartDebugRecording(NULL));
1363
1364#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
1365 // Stopping without having started should be OK.
1366 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1367
1368 EXPECT_EQ(apm_->kNoError, apm_->StartDebugRecording(filename.c_str()));
1369 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(revframe_));
1370 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
1371 EXPECT_EQ(apm_->kNoError, apm_->StopDebugRecording());
1372
1373 // Verify the file has been written.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001374 FILE* fid = fopen(filename.c_str(), "r");
1375 ASSERT_TRUE(fid != NULL);
1376
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001377 // Clean it up.
andrew@webrtc.orgf5d8c3b2012-01-24 21:35:39 +00001378 ASSERT_EQ(0, fclose(fid));
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001379 ASSERT_EQ(0, remove(filename.c_str()));
1380#else
1381 EXPECT_EQ(apm_->kUnsupportedFunctionError,
1382 apm_->StartDebugRecording(filename.c_str()));
1383 EXPECT_EQ(apm_->kUnsupportedFunctionError, apm_->StopDebugRecording());
1384
1385 // Verify the file has NOT been written.
1386 ASSERT_TRUE(fopen(filename.c_str(), "r") == NULL);
1387#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1388}
1389
andrew@webrtc.org75f19482012-02-09 17:16:18 +00001390// TODO(andrew): Add a test to process a few frames with different combinations
1391// of enabled components.
1392
andrew@webrtc.orge2ed5ba2012-01-20 19:06:38 +00001393// TODO(andrew): Make this test more robust such that it can be run on multiple
1394// platforms. It currently requires bit-exactness.
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001395#ifdef WEBRTC_AUDIOPROC_BIT_EXACT
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001396TEST_F(ApmTest, Process) {
1397 GOOGLE_PROTOBUF_VERIFY_VERSION;
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001398 webrtc::audioproc::OutputData ref_data;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001399
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001400 if (!write_ref_data) {
1401 ReadMessageLiteFromFile(ref_filename_, &ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001402 } else {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001403 // Write the desired tests to the protobuf reference file.
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001404 for (size_t i = 0; i < kChannelsSize; i++) {
1405 for (size_t j = 0; j < kChannelsSize; j++) {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001406 // We can't have more output than input channels.
1407 for (size_t k = 0; k <= j; k++) {
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001408 for (size_t l = 0; l < kProcessSampleRatesSize; l++) {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001409 webrtc::audioproc::Test* test = ref_data.add_test();
andrew@webrtc.orgecac9b72012-05-02 00:04:10 +00001410 test->set_num_reverse_channels(kChannels[i]);
1411 test->set_num_input_channels(kChannels[j]);
1412 test->set_num_output_channels(kChannels[k]);
1413 test->set_sample_rate(kProcessSampleRates[l]);
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001414 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001415 }
1416 }
1417 }
1418 }
1419
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001420 EnableAllComponents();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001421
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001422 for (int i = 0; i < ref_data.test_size(); i++) {
1423 printf("Running test %d of %d...\n", i + 1, ref_data.test_size());
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001424
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001425 webrtc::audioproc::Test* test = ref_data.mutable_test(i);
1426 Init(test->sample_rate(), test->num_reverse_channels(),
1427 test->num_input_channels(), test->num_output_channels(), true);
1428
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001429 int frame_count = 0;
1430 int has_echo_count = 0;
1431 int has_voice_count = 0;
1432 int is_saturated_count = 0;
1433 int analog_level = 127;
1434 int analog_level_average = 0;
1435 int max_output_average = 0;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001436 float ns_speech_prob_average = 0.0f;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001437
1438 while (1) {
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001439 if (!ReadFrame(far_file_, revframe_)) break;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001440 EXPECT_EQ(apm_->kNoError, apm_->AnalyzeReverseStream(revframe_));
1441
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001442 if (!ReadFrame(near_file_, frame_)) break;
1443 frame_->vad_activity_ = AudioFrame::kVadUnknown;
1444
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001445 EXPECT_EQ(apm_->kNoError, apm_->set_stream_delay_ms(0));
andrew@webrtc.org6be1e932013-03-01 18:47:28 +00001446 apm_->echo_cancellation()->set_stream_drift_samples(0);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001447 EXPECT_EQ(apm_->kNoError,
1448 apm_->gain_control()->set_stream_analog_level(analog_level));
1449
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001450 EXPECT_EQ(apm_->kNoError, apm_->ProcessStream(frame_));
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001451 // Ensure the frame was downmixed properly.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001452 EXPECT_EQ(test->num_output_channels(), frame_->num_channels_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001453
1454 max_output_average += MaxAudioFrame(*frame_);
1455
1456 if (apm_->echo_cancellation()->stream_has_echo()) {
1457 has_echo_count++;
1458 }
1459
1460 analog_level = apm_->gain_control()->stream_analog_level();
1461 analog_level_average += analog_level;
1462 if (apm_->gain_control()->stream_is_saturated()) {
1463 is_saturated_count++;
1464 }
1465 if (apm_->voice_detection()->stream_has_voice()) {
1466 has_voice_count++;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001467 EXPECT_EQ(AudioFrame::kVadActive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001468 } else {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001469 EXPECT_EQ(AudioFrame::kVadPassive, frame_->vad_activity_);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001470 }
1471
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001472 ns_speech_prob_average += apm_->noise_suppression()->speech_probability();
1473
andrew@webrtc.org07bf9a02012-05-05 00:32:00 +00001474 size_t frame_size = frame_->samples_per_channel_ * frame_->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001475 size_t write_count = fwrite(frame_->data_,
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001476 sizeof(int16_t),
1477 frame_size,
1478 out_file_);
1479 ASSERT_EQ(frame_size, write_count);
1480
1481 // Reset in case of downmixing.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001482 frame_->num_channels_ = test->num_input_channels();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001483 frame_count++;
1484 }
1485 max_output_average /= frame_count;
1486 analog_level_average /= frame_count;
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001487 ns_speech_prob_average /= frame_count;
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001488
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001489#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001490 EchoCancellation::Metrics echo_metrics;
1491 EXPECT_EQ(apm_->kNoError,
1492 apm_->echo_cancellation()->GetMetrics(&echo_metrics));
1493 int median = 0;
1494 int std = 0;
1495 EXPECT_EQ(apm_->kNoError,
1496 apm_->echo_cancellation()->GetDelayMetrics(&median, &std));
1497
1498 int rms_level = apm_->level_estimator()->RMS();
1499 EXPECT_LE(0, rms_level);
1500 EXPECT_GE(127, rms_level);
1501#endif
1502
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001503 if (!write_ref_data) {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001504 EXPECT_EQ(test->has_echo_count(), has_echo_count);
1505 EXPECT_EQ(test->has_voice_count(), has_voice_count);
1506 EXPECT_EQ(test->is_saturated_count(), is_saturated_count);
1507
1508 EXPECT_EQ(test->analog_level_average(), analog_level_average);
1509 EXPECT_EQ(test->max_output_average(), max_output_average);
1510
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001511#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001512 webrtc::audioproc::Test::EchoMetrics reference =
1513 test->echo_metrics();
1514 TestStats(echo_metrics.residual_echo_return_loss,
1515 reference.residual_echo_return_loss());
1516 TestStats(echo_metrics.echo_return_loss,
1517 reference.echo_return_loss());
1518 TestStats(echo_metrics.echo_return_loss_enhancement,
1519 reference.echo_return_loss_enhancement());
1520 TestStats(echo_metrics.a_nlp,
1521 reference.a_nlp());
1522
1523 webrtc::audioproc::Test::DelayMetrics reference_delay =
1524 test->delay_metrics();
andrew@webrtc.org828af1b2011-11-22 22:40:27 +00001525 EXPECT_EQ(reference_delay.median(), median);
1526 EXPECT_EQ(reference_delay.std(), std);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001527
1528 EXPECT_EQ(test->rms_level(), rms_level);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001529
1530 EXPECT_FLOAT_EQ(test->ns_speech_probability_average(),
1531 ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001532#endif
1533 } else {
1534 test->set_has_echo_count(has_echo_count);
1535 test->set_has_voice_count(has_voice_count);
1536 test->set_is_saturated_count(is_saturated_count);
1537
1538 test->set_analog_level_average(analog_level_average);
1539 test->set_max_output_average(max_output_average);
1540
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001541#if defined(WEBRTC_AUDIOPROC_FLOAT_PROFILE)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001542 webrtc::audioproc::Test::EchoMetrics* message =
1543 test->mutable_echo_metrics();
1544 WriteStatsMessage(echo_metrics.residual_echo_return_loss,
1545 message->mutable_residual_echo_return_loss());
1546 WriteStatsMessage(echo_metrics.echo_return_loss,
1547 message->mutable_echo_return_loss());
1548 WriteStatsMessage(echo_metrics.echo_return_loss_enhancement,
1549 message->mutable_echo_return_loss_enhancement());
1550 WriteStatsMessage(echo_metrics.a_nlp,
1551 message->mutable_a_nlp());
1552
1553 webrtc::audioproc::Test::DelayMetrics* message_delay =
1554 test->mutable_delay_metrics();
1555 message_delay->set_median(median);
1556 message_delay->set_std(std);
1557
1558 test->set_rms_level(rms_level);
bjornv@webrtc.org08329f42012-07-12 21:00:43 +00001559
1560 EXPECT_LE(0.0f, ns_speech_prob_average);
1561 EXPECT_GE(1.0f, ns_speech_prob_average);
1562 test->set_ns_speech_probability_average(ns_speech_prob_average);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001563#endif
1564 }
1565
1566 rewind(far_file_);
1567 rewind(near_file_);
1568 }
1569
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001570 if (write_ref_data) {
1571 WriteMessageLiteToFile(ref_filename_, ref_data);
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001572 }
1573}
andrew@webrtc.org293d22b2012-01-30 22:04:26 +00001574#endif // WEBRTC_AUDIOPROC_BIT_EXACT
andrew@webrtc.orge2ed5ba2012-01-20 19:06:38 +00001575
niklase@google.com470e71d2011-07-07 08:21:25 +00001576} // namespace
1577
1578int main(int argc, char** argv) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001579 for (int i = 1; i < argc; i++) {
andrew@webrtc.orgdaacee82012-02-07 00:01:04 +00001580 if (strcmp(argv[i], "--write_ref_data") == 0) {
1581 write_ref_data = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00001582 }
1583 }
1584
andrew@webrtc.org28d01402012-10-18 00:42:32 +00001585 // We don't use TestSuite here because it would require the Android platform
1586 // build to depend on Gmock.
1587 webrtc::test::SetExecutablePath(argv[0]);
1588 testing::InitGoogleTest(&argc, argv);
1589 int result = RUN_ALL_TESTS();
andrew@webrtc.org64235092011-08-19 21:22:08 +00001590 // Optional, but removes memory leak noise from Valgrind.
1591 google::protobuf::ShutdownProtobufLibrary();
andrew@webrtc.org28d01402012-10-18 00:42:32 +00001592 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +00001593}