blob: 87b76afcd2eb451a634ce6c27926f5870ff395f2 [file] [log] [blame]
bjornv@webrtc.org70569082012-04-12 12:13:50 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/aec/aec_core.h"
12#include "modules/audio_processing/aec/echo_cancellation.h"
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +020013#include "rtc_base/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "test/gtest.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020015#include "typedefs.h" // NOLINT(build/include)
peah50e21bd2016-03-05 08:39:21 -080016namespace webrtc {
bjornv@webrtc.org70569082012-04-12 12:13:50 +000017namespace {
18
19class SystemDelayTest : public ::testing::Test {
20 protected:
21 SystemDelayTest();
22 virtual void SetUp();
23 virtual void TearDown();
24
25 // Initialization of AEC handle with respect to |sample_rate_hz|. Since the
26 // device sample rate is unimportant we set that value to 48000 Hz.
27 void Init(int sample_rate_hz);
28
29 // Makes one render call and one capture call in that specific order.
30 void RenderAndCapture(int device_buffer_ms);
31
32 // Fills up the far-end buffer with respect to the default device buffer size.
Peter Kastingdce40cf2015-08-24 14:52:23 -070033 size_t BufferFillUp();
bjornv@webrtc.org70569082012-04-12 12:13:50 +000034
35 // Runs and verifies the behavior in a stable startup procedure.
36 void RunStableStartup();
37
38 // Maps buffer size in ms into samples, taking the unprocessed frame into
39 // account.
Bjorn Volcker71012692015-06-18 11:04:56 +020040 int MapBufferSizeToSamples(int size_in_ms, bool extended_filter);
bjornv@webrtc.org70569082012-04-12 12:13:50 +000041
42 void* handle_;
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000043 Aec* self_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070044 size_t samples_per_frame_;
bjornv@webrtc.org70569082012-04-12 12:13:50 +000045 // Dummy input/output speech data.
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000046 static const int kSamplesPerChunk = 160;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +000047 float far_[kSamplesPerChunk];
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000048 float near_[kSamplesPerChunk];
49 float out_[kSamplesPerChunk];
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +000050 const float* near_ptr_;
51 float* out_ptr_;
bjornv@webrtc.org70569082012-04-12 12:13:50 +000052};
53
54SystemDelayTest::SystemDelayTest()
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000055 : handle_(NULL), self_(NULL), samples_per_frame_(0) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +000056 // Dummy input data are set with more or less arbitrary non-zero values.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +000057 for (int i = 0; i < kSamplesPerChunk; i++) {
58 far_[i] = 257.0;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000059 near_[i] = 514.0;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +000060 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +000061 memset(out_, 0, sizeof(out_));
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +000062 near_ptr_ = near_;
63 out_ptr_ = out_;
bjornv@webrtc.org70569082012-04-12 12:13:50 +000064}
65
66void SystemDelayTest::SetUp() {
Bjorn Volcker9345e862015-06-10 21:43:36 +020067 handle_ = WebRtcAec_Create();
68 ASSERT_TRUE(handle_);
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000069 self_ = reinterpret_cast<Aec*>(handle_);
bjornv@webrtc.org70569082012-04-12 12:13:50 +000070}
71
72void SystemDelayTest::TearDown() {
73 // Free AEC
Bjorn Volckerf6a99e62015-04-10 07:56:57 +020074 WebRtcAec_Free(handle_);
bjornv@webrtc.org70569082012-04-12 12:13:50 +000075 handle_ = NULL;
76}
77
78// In SWB mode nothing is added to the buffer handling with respect to
79// functionality compared to WB. We therefore only verify behavior in NB and WB.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000080static const int kSampleRateHz[] = {8000, 16000};
bjornv@webrtc.org70569082012-04-12 12:13:50 +000081static const size_t kNumSampleRates =
82 sizeof(kSampleRateHz) / sizeof(*kSampleRateHz);
83
84// Default audio device buffer size used.
85static const int kDeviceBufMs = 100;
86
87// Requirement for a stable device convergence time in ms. Should converge in
88// less than |kStableConvergenceMs|.
89static const int kStableConvergenceMs = 100;
90
91// Maximum convergence time in ms. This means that we should leave the startup
92// phase after |kMaxConvergenceMs| independent of device buffer stability
93// conditions.
94static const int kMaxConvergenceMs = 500;
95
96void SystemDelayTest::Init(int sample_rate_hz) {
97 // Initialize AEC
98 EXPECT_EQ(0, WebRtcAec_Init(handle_, sample_rate_hz, 48000));
Bjorn Volcker71012692015-06-18 11:04:56 +020099 EXPECT_EQ(0, WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000100
101 // One frame equals 10 ms of data.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700102 samples_per_frame_ = static_cast<size_t>(sample_rate_hz / 100);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000103}
104
105void SystemDelayTest::RenderAndCapture(int device_buffer_ms) {
106 EXPECT_EQ(0, WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000107 EXPECT_EQ(0,
108 WebRtcAec_Process(handle_,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000109 &near_ptr_,
110 1,
111 &out_ptr_,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000112 samples_per_frame_,
113 device_buffer_ms,
114 0));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000115}
116
Peter Kastingdce40cf2015-08-24 14:52:23 -0700117size_t SystemDelayTest::BufferFillUp() {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000118 // To make sure we have a full buffer when we verify stability we first fill
119 // up the far-end buffer with the same amount as we will report in through
120 // Process().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700121 size_t buffer_size = 0;
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000122 for (int i = 0; i < kDeviceBufMs / 10; i++) {
123 EXPECT_EQ(0, WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
124 buffer_size += samples_per_frame_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700125 EXPECT_EQ(static_cast<int>(buffer_size),
126 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000127 }
128 return buffer_size;
129}
130
131void SystemDelayTest::RunStableStartup() {
132 // To make sure we have a full buffer when we verify stability we first fill
133 // up the far-end buffer with the same amount as we will report in through
134 // Process().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700135 size_t buffer_size = BufferFillUp();
Bjorn Volcker71012692015-06-18 11:04:56 +0200136
henrik.lundin0f133b92015-07-02 00:17:55 -0700137 if (WebRtcAec_delay_agnostic_enabled(self_->aec) == 1) {
Bjorn Volcker71012692015-06-18 11:04:56 +0200138 // In extended_filter mode we set the buffer size after the first processed
139 // 10 ms chunk. Hence, we don't need to wait for the reported system delay
140 // values to become stable.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000141 RenderAndCapture(kDeviceBufMs);
142 buffer_size += samples_per_frame_;
Bjorn Volcker71012692015-06-18 11:04:56 +0200143 EXPECT_EQ(0, self_->startup_phase);
144 } else {
145 // A stable device should be accepted and put in a regular process mode
146 // within |kStableConvergenceMs|.
147 int process_time_ms = 0;
148 for (; process_time_ms < kStableConvergenceMs; process_time_ms += 10) {
149 RenderAndCapture(kDeviceBufMs);
150 buffer_size += samples_per_frame_;
151 if (self_->startup_phase == 0) {
152 // We have left the startup phase.
153 break;
154 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000155 }
Bjorn Volcker71012692015-06-18 11:04:56 +0200156 // Verify convergence time.
157 EXPECT_GT(kStableConvergenceMs, process_time_ms);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000158 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000159 // Verify that the buffer has been flushed.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 EXPECT_GE(static_cast<int>(buffer_size),
161 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000162}
163
Bjorn Volcker71012692015-06-18 11:04:56 +0200164 int SystemDelayTest::MapBufferSizeToSamples(int size_in_ms,
165 bool extended_filter) {
166 // If extended_filter is disabled we add an extra 10 ms for the unprocessed
167 // frame. That is simply how the algorithm is constructed.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700168 return static_cast<int>(
169 (size_in_ms + (extended_filter ? 0 : 10)) * samples_per_frame_ / 10);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000170}
171
172// The tests should meet basic requirements and not be adjusted to what is
173// actually implemented. If we don't get good code coverage this way we either
174// lack in tests or have unnecessary code.
175// General requirements:
176// 1) If we add far-end data the system delay should be increased with the same
177// amount we add.
178// 2) If the far-end buffer is full we should flush the oldest data to make room
179// for the new. In this case the system delay is unaffected.
180// 3) There should exist a startup phase in which the buffer size is to be
181// determined. In this phase no cancellation should be performed.
182// 4) Under stable conditions (small variations in device buffer sizes) the AEC
183// should determine an appropriate local buffer size within
184// |kStableConvergenceMs| ms.
185// 5) Under unstable conditions the AEC should make a decision within
186// |kMaxConvergenceMs| ms.
187// 6) If the local buffer runs out of data we should stuff the buffer with older
188// frames.
189// 7) The system delay should within |kMaxConvergenceMs| ms heal from
190// disturbances like drift, data glitches, toggling events and outliers.
191// 8) The system delay should never become negative.
192
193TEST_F(SystemDelayTest, CorrectIncreaseWhenBufferFarend) {
194 // When we add data to the AEC buffer the internal system delay should be
195 // incremented with the same amount as the size of data.
Bjorn Volcker71012692015-06-18 11:04:56 +0200196 // This process should be independent of DA-AEC and extended_filter mode.
197 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
198 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
199 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
200 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700201 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
202 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200203 for (size_t i = 0; i < kNumSampleRates; i++) {
204 Init(kSampleRateHz[i]);
205 // Loop through a couple of calls to make sure the system delay
206 // increments correctly.
207 for (int j = 1; j <= 5; j++) {
208 EXPECT_EQ(0,
209 WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700210 EXPECT_EQ(static_cast<int>(j * samples_per_frame_),
211 WebRtcAec_system_delay(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200212 }
213 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000214 }
215 }
216}
217
218// TODO(bjornv): Add a test to verify behavior if the far-end buffer is full
219// when adding new data.
220
221TEST_F(SystemDelayTest, CorrectDelayAfterStableStartup) {
222 // We run the system in a stable startup. After that we verify that the system
223 // delay meets the requirements.
Bjorn Volcker71012692015-06-18 11:04:56 +0200224 // This process should be independent of DA-AEC and extended_filter mode.
225 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
226 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
227 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
228 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700229 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
230 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200231 for (size_t i = 0; i < kNumSampleRates; i++) {
232 Init(kSampleRateHz[i]);
233 RunStableStartup();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000234
Bjorn Volcker71012692015-06-18 11:04:56 +0200235 // Verify system delay with respect to requirements, i.e., the
236 // |system_delay| is in the interval [75%, 100%] of what's reported on
237 // the average.
238 // In extended_filter mode we target 50% and measure after one processed
239 // 10 ms chunk.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700240 int average_reported_delay =
241 static_cast<int>(kDeviceBufMs * samples_per_frame_ / 10);
Bjorn Volcker71012692015-06-18 11:04:56 +0200242 EXPECT_GE(average_reported_delay, WebRtcAec_system_delay(self_->aec));
243 int lower_bound = WebRtcAec_extended_filter_enabled(self_->aec)
Mirko Bonadeiea7a3f82017-10-19 11:40:55 +0200244 ? (average_reported_delay / 2 -
245 rtc::checked_cast<int>(samples_per_frame_))
246 : average_reported_delay * 3 / 4;
Bjorn Volcker71012692015-06-18 11:04:56 +0200247 EXPECT_LE(lower_bound, WebRtcAec_system_delay(self_->aec));
248 }
249 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000250 }
251}
252
253TEST_F(SystemDelayTest, CorrectDelayAfterUnstableStartup) {
Bjorn Volcker71012692015-06-18 11:04:56 +0200254 // This test does not apply in extended_filter mode, since we only use the
255 // the first 10 ms chunk to determine a reasonable buffer size. Neither does
256 // it apply if DA-AEC is on because that overrides the startup procedure.
257 WebRtcAec_enable_extended_filter(self_->aec, 0);
258 EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(self_->aec));
henrik.lundin0f133b92015-07-02 00:17:55 -0700259 WebRtcAec_enable_delay_agnostic(self_->aec, 0);
260 EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200261
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000262 // In an unstable system we would start processing after |kMaxConvergenceMs|.
263 // On the last frame the AEC buffer is adjusted to 60% of the last reported
264 // device buffer size.
265 // We construct an unstable system by altering the device buffer size between
266 // two values |kDeviceBufMs| +- 25 ms.
267 for (size_t i = 0; i < kNumSampleRates; i++) {
268 Init(kSampleRateHz[i]);
269
270 // To make sure we have a full buffer when we verify stability we first fill
271 // up the far-end buffer with the same amount as we will report in on the
272 // average through Process().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700273 size_t buffer_size = BufferFillUp();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000274
275 int buffer_offset_ms = 25;
276 int reported_delay_ms = 0;
277 int process_time_ms = 0;
278 for (; process_time_ms <= kMaxConvergenceMs; process_time_ms += 10) {
279 reported_delay_ms = kDeviceBufMs + buffer_offset_ms;
280 RenderAndCapture(reported_delay_ms);
281 buffer_size += samples_per_frame_;
282 buffer_offset_ms = -buffer_offset_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000283 if (self_->startup_phase == 0) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000284 // We have left the startup phase.
285 break;
286 }
287 }
288 // Verify convergence time.
289 EXPECT_GE(kMaxConvergenceMs, process_time_ms);
290 // Verify that the buffer has been flushed.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700291 EXPECT_GE(static_cast<int>(buffer_size),
292 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000293
294 // Verify system delay with respect to requirements, i.e., the
295 // |system_delay| is in the interval [60%, 100%] of what's last reported.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700296 EXPECT_GE(static_cast<int>(reported_delay_ms * samples_per_frame_ / 10),
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000297 WebRtcAec_system_delay(self_->aec));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700298 EXPECT_LE(
299 static_cast<int>(reported_delay_ms * samples_per_frame_ / 10 * 3 / 5),
300 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000301 }
302}
303
Bjorn Volcker71012692015-06-18 11:04:56 +0200304TEST_F(SystemDelayTest, CorrectDelayAfterStableBufferBuildUp) {
305 // This test does not apply in extended_filter mode, since we only use the
306 // the first 10 ms chunk to determine a reasonable buffer size. Neither does
307 // it apply if DA-AEC is on because that overrides the startup procedure.
308 WebRtcAec_enable_extended_filter(self_->aec, 0);
309 EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(self_->aec));
henrik.lundin0f133b92015-07-02 00:17:55 -0700310 WebRtcAec_enable_delay_agnostic(self_->aec, 0);
311 EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200312
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000313 // In this test we start by establishing the device buffer size during stable
314 // conditions, but with an empty internal far-end buffer. Once that is done we
315 // verify that the system delay is increased correctly until we have reach an
316 // internal buffer size of 75% of what's been reported.
317 for (size_t i = 0; i < kNumSampleRates; i++) {
318 Init(kSampleRateHz[i]);
319
320 // We assume that running |kStableConvergenceMs| calls will put the
321 // algorithm in a state where the device buffer size has been determined. We
322 // can make that assumption since we have a separate stability test.
323 int process_time_ms = 0;
324 for (; process_time_ms < kStableConvergenceMs; process_time_ms += 10) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000325 EXPECT_EQ(0,
326 WebRtcAec_Process(handle_,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000327 &near_ptr_,
328 1,
329 &out_ptr_,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000330 samples_per_frame_,
331 kDeviceBufMs,
332 0));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000333 }
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000334 // Verify that a buffer size has been established.
335 EXPECT_EQ(0, self_->checkBuffSize);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000336
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000337 // We now have established the required buffer size. Let us verify that we
338 // fill up before leaving the startup phase for normal processing.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700339 size_t buffer_size = 0;
340 size_t target_buffer_size = kDeviceBufMs * samples_per_frame_ / 10 * 3 / 4;
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000341 process_time_ms = 0;
342 for (; process_time_ms <= kMaxConvergenceMs; process_time_ms += 10) {
343 RenderAndCapture(kDeviceBufMs);
344 buffer_size += samples_per_frame_;
345 if (self_->startup_phase == 0) {
346 // We have left the startup phase.
347 break;
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000348 }
349 }
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000350 // Verify convergence time.
351 EXPECT_GT(kMaxConvergenceMs, process_time_ms);
352 // Verify that the buffer has reached the desired size.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700353 EXPECT_LE(static_cast<int>(target_buffer_size),
354 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000355
356 // Verify normal behavior (system delay is kept constant) after startup by
357 // running a couple of calls to BufferFarend() and Process().
358 for (int j = 0; j < 6; j++) {
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000359 int system_delay_before_calls = WebRtcAec_system_delay(self_->aec);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000360 RenderAndCapture(kDeviceBufMs);
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000361 EXPECT_EQ(system_delay_before_calls, WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000362 }
363 }
364}
365
366TEST_F(SystemDelayTest, CorrectDelayWhenBufferUnderrun) {
367 // Here we test a buffer under run scenario. If we keep on calling
368 // WebRtcAec_Process() we will finally run out of data, but should
369 // automatically stuff the buffer. We verify this behavior by checking if the
370 // system delay goes negative.
Bjorn Volcker71012692015-06-18 11:04:56 +0200371 // This process should be independent of DA-AEC and extended_filter mode.
372 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
373 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
374 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
375 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700376 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
377 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200378 for (size_t i = 0; i < kNumSampleRates; i++) {
379 Init(kSampleRateHz[i]);
380 RunStableStartup();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000381
Bjorn Volcker71012692015-06-18 11:04:56 +0200382 // The AEC has now left the Startup phase. We now have at most
383 // |kStableConvergenceMs| in the buffer. Keep on calling Process() until
384 // we run out of data and verify that the system delay is non-negative.
385 for (int j = 0; j <= kStableConvergenceMs; j += 10) {
386 EXPECT_EQ(0, WebRtcAec_Process(handle_, &near_ptr_, 1, &out_ptr_,
387 samples_per_frame_, kDeviceBufMs, 0));
388 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
389 }
390 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000391 }
392 }
393}
394
Bjorn Volcker71012692015-06-18 11:04:56 +0200395TEST_F(SystemDelayTest, CorrectDelayDuringDrift) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000396 // This drift test should verify that the system delay is never exceeding the
397 // device buffer. The drift is simulated by decreasing the reported device
398 // buffer size by 1 ms every 100 ms. If the device buffer size goes below 30
399 // ms we jump (add) 10 ms to give a repeated pattern.
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000400
Bjorn Volcker71012692015-06-18 11:04:56 +0200401 // This process should be independent of DA-AEC and extended_filter mode.
402 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
403 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
404 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
405 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700406 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
407 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200408 for (size_t i = 0; i < kNumSampleRates; i++) {
409 Init(kSampleRateHz[i]);
410 RunStableStartup();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000411
Bjorn Volcker71012692015-06-18 11:04:56 +0200412 // We have left the startup phase and proceed with normal processing.
413 int jump = 0;
414 for (int j = 0; j < 1000; j++) {
415 // Drift = -1 ms per 100 ms of data.
416 int device_buf_ms = kDeviceBufMs - (j / 10) + jump;
417 int device_buf = MapBufferSizeToSamples(device_buf_ms,
418 extended_filter == 1);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000419
Bjorn Volcker71012692015-06-18 11:04:56 +0200420 if (device_buf_ms < 30) {
421 // Add 10 ms data, taking affect next frame.
422 jump += 10;
423 }
424 RenderAndCapture(device_buf_ms);
425
426 // Verify that the system delay does not exceed the device buffer.
427 EXPECT_GE(device_buf, WebRtcAec_system_delay(self_->aec));
428
429 // Verify that the system delay is non-negative.
430 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
431 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000432 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000433 }
434 }
435}
436
Bjorn Volcker71012692015-06-18 11:04:56 +0200437TEST_F(SystemDelayTest, ShouldRecoverAfterGlitch) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000438 // This glitch test should verify that the system delay recovers if there is
439 // a glitch in data. The data glitch is constructed as 200 ms of buffering
440 // after which the stable procedure continues. The glitch is never reported by
441 // the device.
442 // The system is said to be in a non-causal state if the difference between
443 // the device buffer and system delay is less than a block (64 samples).
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000444
Bjorn Volcker71012692015-06-18 11:04:56 +0200445 // This process should be independent of DA-AEC and extended_filter mode.
446 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
447 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
448 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
449 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700450 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
451 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200452 for (size_t i = 0; i < kNumSampleRates; i++) {
453 Init(kSampleRateHz[i]);
454 RunStableStartup();
455 int device_buf = MapBufferSizeToSamples(kDeviceBufMs,
456 extended_filter == 1);
457 // Glitch state.
458 for (int j = 0; j < 20; j++) {
459 EXPECT_EQ(0,
460 WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
461 // No need to verify system delay, since that is done in a separate
462 // test.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000463 }
Bjorn Volcker71012692015-06-18 11:04:56 +0200464 // Verify that we are in a non-causal state, i.e.,
465 // |system_delay| > |device_buf|.
466 EXPECT_LT(device_buf, WebRtcAec_system_delay(self_->aec));
467
468 // Recover state. Should recover at least 4 ms of data per 10 ms, hence
469 // a glitch of 200 ms will take at most 200 * 10 / 4 = 500 ms to recover
470 // from.
471 bool non_causal = true; // We are currently in a non-causal state.
472 for (int j = 0; j < 50; j++) {
473 int system_delay_before = WebRtcAec_system_delay(self_->aec);
474 RenderAndCapture(kDeviceBufMs);
475 int system_delay_after = WebRtcAec_system_delay(self_->aec);
476 // We have recovered if
477 // |device_buf| - |system_delay_after| >= PART_LEN (1 block).
478 // During recovery, |system_delay_after| < |system_delay_before|,
479 // otherwise they are equal.
480 if (non_causal) {
481 EXPECT_LT(system_delay_after, system_delay_before);
482 if (device_buf - system_delay_after >= PART_LEN) {
483 non_causal = false;
484 }
485 } else {
486 EXPECT_EQ(system_delay_before, system_delay_after);
487 }
488 // Verify that the system delay is non-negative.
489 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
490 }
491 // Check that we have recovered.
492 EXPECT_FALSE(non_causal);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000493 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000494 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000495 }
496}
497
498TEST_F(SystemDelayTest, UnaffectedWhenSpuriousDeviceBufferValues) {
Bjorn Volcker71012692015-06-18 11:04:56 +0200499 // This test does not apply in extended_filter mode, since we only use the
500 // the first 10 ms chunk to determine a reasonable buffer size.
501 const int extended_filter = 0;
502 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
503 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000504
Bjorn Volcker71012692015-06-18 11:04:56 +0200505 // Should be DA-AEC independent.
506 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700507 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
508 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200509 // This spurious device buffer data test aims at verifying that the system
510 // delay is unaffected by large outliers.
511 // The system is said to be in a non-causal state if the difference between
512 // the device buffer and system delay is less than a block (64 samples).
513 for (size_t i = 0; i < kNumSampleRates; i++) {
514 Init(kSampleRateHz[i]);
515 RunStableStartup();
516 int device_buf = MapBufferSizeToSamples(kDeviceBufMs,
517 extended_filter == 1);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000518
Bjorn Volcker71012692015-06-18 11:04:56 +0200519 // Normal state. We are currently not in a non-causal state.
520 bool non_causal = false;
521
522 // Run 1 s and replace device buffer size with 500 ms every 100 ms.
523 for (int j = 0; j < 100; j++) {
524 int system_delay_before_calls = WebRtcAec_system_delay(self_->aec);
525 int device_buf_ms = j % 10 == 0 ? 500 : kDeviceBufMs;
526 RenderAndCapture(device_buf_ms);
527
528 // Check for non-causality.
529 if (device_buf - WebRtcAec_system_delay(self_->aec) < PART_LEN) {
530 non_causal = true;
531 }
532 EXPECT_FALSE(non_causal);
533 EXPECT_EQ(system_delay_before_calls,
534 WebRtcAec_system_delay(self_->aec));
535
536 // Verify that the system delay is non-negative.
537 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000538 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000539 }
540 }
541}
542
543TEST_F(SystemDelayTest, CorrectImpactWhenTogglingDeviceBufferValues) {
544 // This test aims at verifying that the system delay is "unaffected" by
545 // toggling values reported by the device.
546 // The test is constructed such that every other device buffer value is zero
547 // and then 2 * |kDeviceBufMs|, hence the size is constant on the average. The
548 // zero values will force us into a non-causal state and thereby lowering the
Bjorn Volcker71012692015-06-18 11:04:56 +0200549 // system delay until we basically run out of data. Once that happens the
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000550 // buffer will be stuffed.
551 // TODO(bjornv): This test will have a better impact if we verified that the
Bjorn Volcker71012692015-06-18 11:04:56 +0200552 // delay estimate goes up when the system delay goes down to meet the average
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000553 // device buffer size.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000554
Bjorn Volcker71012692015-06-18 11:04:56 +0200555 // This test does not apply if DA-AEC is enabled and extended_filter mode
556 // disabled.
557 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
558 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
559 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
560 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700561 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
562 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200563 if (extended_filter == 0 && da_aec == 1) {
564 continue;
565 }
566 for (size_t i = 0; i < kNumSampleRates; i++) {
567 Init(kSampleRateHz[i]);
568 RunStableStartup();
569 const int device_buf = MapBufferSizeToSamples(kDeviceBufMs,
570 extended_filter == 1);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000571
Bjorn Volcker71012692015-06-18 11:04:56 +0200572 // Normal state. We are currently not in a non-causal state.
573 bool non_causal = false;
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000574
Bjorn Volcker71012692015-06-18 11:04:56 +0200575 // Loop through 100 frames (both render and capture), which equals 1 s
576 // of data. Every odd frame we set the device buffer size to
577 // 2 * |kDeviceBufMs| and even frames we set the device buffer size to
578 // zero.
579 for (int j = 0; j < 100; j++) {
580 int system_delay_before_calls = WebRtcAec_system_delay(self_->aec);
581 int device_buf_ms = 2 * (j % 2) * kDeviceBufMs;
582 RenderAndCapture(device_buf_ms);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000583
Bjorn Volcker71012692015-06-18 11:04:56 +0200584 // Check for non-causality, compared with the average device buffer
585 // size.
586 non_causal |= (device_buf - WebRtcAec_system_delay(self_->aec) < 64);
587 EXPECT_GE(system_delay_before_calls,
588 WebRtcAec_system_delay(self_->aec));
589
590 // Verify that the system delay is non-negative.
591 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
592 }
593 // Verify we are not in a non-causal state.
594 EXPECT_FALSE(non_causal);
595 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000596 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000597 }
598}
599
600} // namespace
peah50e21bd2016-03-05 08:39:21 -0800601} // namespace webrtc