blob: db73d7e18377a1e1a78e254e9ee8dfd78c1eefbd [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"
13#include "test/gtest.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020014#include "typedefs.h" // NOLINT(build/include)
peah50e21bd2016-03-05 08:39:21 -080015namespace webrtc {
bjornv@webrtc.org70569082012-04-12 12:13:50 +000016namespace {
17
18class SystemDelayTest : public ::testing::Test {
19 protected:
20 SystemDelayTest();
21 virtual void SetUp();
22 virtual void TearDown();
23
24 // Initialization of AEC handle with respect to |sample_rate_hz|. Since the
25 // device sample rate is unimportant we set that value to 48000 Hz.
26 void Init(int sample_rate_hz);
27
28 // Makes one render call and one capture call in that specific order.
29 void RenderAndCapture(int device_buffer_ms);
30
31 // Fills up the far-end buffer with respect to the default device buffer size.
Peter Kastingdce40cf2015-08-24 14:52:23 -070032 size_t BufferFillUp();
bjornv@webrtc.org70569082012-04-12 12:13:50 +000033
34 // Runs and verifies the behavior in a stable startup procedure.
35 void RunStableStartup();
36
37 // Maps buffer size in ms into samples, taking the unprocessed frame into
38 // account.
Bjorn Volcker71012692015-06-18 11:04:56 +020039 int MapBufferSizeToSamples(int size_in_ms, bool extended_filter);
bjornv@webrtc.org70569082012-04-12 12:13:50 +000040
41 void* handle_;
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000042 Aec* self_;
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 size_t samples_per_frame_;
bjornv@webrtc.org70569082012-04-12 12:13:50 +000044 // Dummy input/output speech data.
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000045 static const int kSamplesPerChunk = 160;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +000046 float far_[kSamplesPerChunk];
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000047 float near_[kSamplesPerChunk];
48 float out_[kSamplesPerChunk];
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +000049 const float* near_ptr_;
50 float* out_ptr_;
bjornv@webrtc.org70569082012-04-12 12:13:50 +000051};
52
53SystemDelayTest::SystemDelayTest()
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000054 : handle_(NULL), self_(NULL), samples_per_frame_(0) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +000055 // Dummy input data are set with more or less arbitrary non-zero values.
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +000056 for (int i = 0; i < kSamplesPerChunk; i++) {
57 far_[i] = 257.0;
mflodman@webrtc.orgd5da2502014-05-15 11:17:21 +000058 near_[i] = 514.0;
kwiberg@webrtc.org38214d52014-07-03 09:47:33 +000059 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +000060 memset(out_, 0, sizeof(out_));
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +000061 near_ptr_ = near_;
62 out_ptr_ = out_;
bjornv@webrtc.org70569082012-04-12 12:13:50 +000063}
64
65void SystemDelayTest::SetUp() {
Bjorn Volcker9345e862015-06-10 21:43:36 +020066 handle_ = WebRtcAec_Create();
67 ASSERT_TRUE(handle_);
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000068 self_ = reinterpret_cast<Aec*>(handle_);
bjornv@webrtc.org70569082012-04-12 12:13:50 +000069}
70
71void SystemDelayTest::TearDown() {
72 // Free AEC
Bjorn Volckerf6a99e62015-04-10 07:56:57 +020073 WebRtcAec_Free(handle_);
bjornv@webrtc.org70569082012-04-12 12:13:50 +000074 handle_ = NULL;
75}
76
77// In SWB mode nothing is added to the buffer handling with respect to
78// functionality compared to WB. We therefore only verify behavior in NB and WB.
andrew@webrtc.org13b2d462013-10-08 23:41:42 +000079static const int kSampleRateHz[] = {8000, 16000};
bjornv@webrtc.org70569082012-04-12 12:13:50 +000080static const size_t kNumSampleRates =
81 sizeof(kSampleRateHz) / sizeof(*kSampleRateHz);
82
83// Default audio device buffer size used.
84static const int kDeviceBufMs = 100;
85
86// Requirement for a stable device convergence time in ms. Should converge in
87// less than |kStableConvergenceMs|.
88static const int kStableConvergenceMs = 100;
89
90// Maximum convergence time in ms. This means that we should leave the startup
91// phase after |kMaxConvergenceMs| independent of device buffer stability
92// conditions.
93static const int kMaxConvergenceMs = 500;
94
95void SystemDelayTest::Init(int sample_rate_hz) {
96 // Initialize AEC
97 EXPECT_EQ(0, WebRtcAec_Init(handle_, sample_rate_hz, 48000));
Bjorn Volcker71012692015-06-18 11:04:56 +020098 EXPECT_EQ(0, WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +000099
100 // One frame equals 10 ms of data.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700101 samples_per_frame_ = static_cast<size_t>(sample_rate_hz / 100);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000102}
103
104void SystemDelayTest::RenderAndCapture(int device_buffer_ms) {
105 EXPECT_EQ(0, WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000106 EXPECT_EQ(0,
107 WebRtcAec_Process(handle_,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000108 &near_ptr_,
109 1,
110 &out_ptr_,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000111 samples_per_frame_,
112 device_buffer_ms,
113 0));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000114}
115
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116size_t SystemDelayTest::BufferFillUp() {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000117 // To make sure we have a full buffer when we verify stability we first fill
118 // up the far-end buffer with the same amount as we will report in through
119 // Process().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700120 size_t buffer_size = 0;
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000121 for (int i = 0; i < kDeviceBufMs / 10; i++) {
122 EXPECT_EQ(0, WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
123 buffer_size += samples_per_frame_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700124 EXPECT_EQ(static_cast<int>(buffer_size),
125 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000126 }
127 return buffer_size;
128}
129
130void SystemDelayTest::RunStableStartup() {
131 // To make sure we have a full buffer when we verify stability we first fill
132 // up the far-end buffer with the same amount as we will report in through
133 // Process().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700134 size_t buffer_size = BufferFillUp();
Bjorn Volcker71012692015-06-18 11:04:56 +0200135
henrik.lundin0f133b92015-07-02 00:17:55 -0700136 if (WebRtcAec_delay_agnostic_enabled(self_->aec) == 1) {
Bjorn Volcker71012692015-06-18 11:04:56 +0200137 // In extended_filter mode we set the buffer size after the first processed
138 // 10 ms chunk. Hence, we don't need to wait for the reported system delay
139 // values to become stable.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000140 RenderAndCapture(kDeviceBufMs);
141 buffer_size += samples_per_frame_;
Bjorn Volcker71012692015-06-18 11:04:56 +0200142 EXPECT_EQ(0, self_->startup_phase);
143 } else {
144 // A stable device should be accepted and put in a regular process mode
145 // within |kStableConvergenceMs|.
146 int process_time_ms = 0;
147 for (; process_time_ms < kStableConvergenceMs; process_time_ms += 10) {
148 RenderAndCapture(kDeviceBufMs);
149 buffer_size += samples_per_frame_;
150 if (self_->startup_phase == 0) {
151 // We have left the startup phase.
152 break;
153 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000154 }
Bjorn Volcker71012692015-06-18 11:04:56 +0200155 // Verify convergence time.
156 EXPECT_GT(kStableConvergenceMs, process_time_ms);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000157 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000158 // Verify that the buffer has been flushed.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 EXPECT_GE(static_cast<int>(buffer_size),
160 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000161}
162
Bjorn Volcker71012692015-06-18 11:04:56 +0200163 int SystemDelayTest::MapBufferSizeToSamples(int size_in_ms,
164 bool extended_filter) {
165 // If extended_filter is disabled we add an extra 10 ms for the unprocessed
166 // frame. That is simply how the algorithm is constructed.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700167 return static_cast<int>(
168 (size_in_ms + (extended_filter ? 0 : 10)) * samples_per_frame_ / 10);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000169}
170
171// The tests should meet basic requirements and not be adjusted to what is
172// actually implemented. If we don't get good code coverage this way we either
173// lack in tests or have unnecessary code.
174// General requirements:
175// 1) If we add far-end data the system delay should be increased with the same
176// amount we add.
177// 2) If the far-end buffer is full we should flush the oldest data to make room
178// for the new. In this case the system delay is unaffected.
179// 3) There should exist a startup phase in which the buffer size is to be
180// determined. In this phase no cancellation should be performed.
181// 4) Under stable conditions (small variations in device buffer sizes) the AEC
182// should determine an appropriate local buffer size within
183// |kStableConvergenceMs| ms.
184// 5) Under unstable conditions the AEC should make a decision within
185// |kMaxConvergenceMs| ms.
186// 6) If the local buffer runs out of data we should stuff the buffer with older
187// frames.
188// 7) The system delay should within |kMaxConvergenceMs| ms heal from
189// disturbances like drift, data glitches, toggling events and outliers.
190// 8) The system delay should never become negative.
191
192TEST_F(SystemDelayTest, CorrectIncreaseWhenBufferFarend) {
193 // When we add data to the AEC buffer the internal system delay should be
194 // incremented with the same amount as the size of data.
Bjorn Volcker71012692015-06-18 11:04:56 +0200195 // This process should be independent of DA-AEC and extended_filter mode.
196 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
197 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
198 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
199 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700200 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
201 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200202 for (size_t i = 0; i < kNumSampleRates; i++) {
203 Init(kSampleRateHz[i]);
204 // Loop through a couple of calls to make sure the system delay
205 // increments correctly.
206 for (int j = 1; j <= 5; j++) {
207 EXPECT_EQ(0,
208 WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700209 EXPECT_EQ(static_cast<int>(j * samples_per_frame_),
210 WebRtcAec_system_delay(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200211 }
212 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000213 }
214 }
215}
216
217// TODO(bjornv): Add a test to verify behavior if the far-end buffer is full
218// when adding new data.
219
220TEST_F(SystemDelayTest, CorrectDelayAfterStableStartup) {
221 // We run the system in a stable startup. After that we verify that the system
222 // delay meets the requirements.
Bjorn Volcker71012692015-06-18 11:04:56 +0200223 // This process should be independent of DA-AEC and extended_filter mode.
224 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
225 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
226 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
227 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700228 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
229 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200230 for (size_t i = 0; i < kNumSampleRates; i++) {
231 Init(kSampleRateHz[i]);
232 RunStableStartup();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000233
Bjorn Volcker71012692015-06-18 11:04:56 +0200234 // Verify system delay with respect to requirements, i.e., the
235 // |system_delay| is in the interval [75%, 100%] of what's reported on
236 // the average.
237 // In extended_filter mode we target 50% and measure after one processed
238 // 10 ms chunk.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700239 int average_reported_delay =
240 static_cast<int>(kDeviceBufMs * samples_per_frame_ / 10);
Bjorn Volcker71012692015-06-18 11:04:56 +0200241 EXPECT_GE(average_reported_delay, WebRtcAec_system_delay(self_->aec));
242 int lower_bound = WebRtcAec_extended_filter_enabled(self_->aec)
243 ? average_reported_delay / 2 - samples_per_frame_
244 : average_reported_delay * 3 / 4;
245 EXPECT_LE(lower_bound, WebRtcAec_system_delay(self_->aec));
246 }
247 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000248 }
249}
250
251TEST_F(SystemDelayTest, CorrectDelayAfterUnstableStartup) {
Bjorn Volcker71012692015-06-18 11:04:56 +0200252 // This test does not apply in extended_filter mode, since we only use the
253 // the first 10 ms chunk to determine a reasonable buffer size. Neither does
254 // it apply if DA-AEC is on because that overrides the startup procedure.
255 WebRtcAec_enable_extended_filter(self_->aec, 0);
256 EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(self_->aec));
henrik.lundin0f133b92015-07-02 00:17:55 -0700257 WebRtcAec_enable_delay_agnostic(self_->aec, 0);
258 EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200259
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000260 // In an unstable system we would start processing after |kMaxConvergenceMs|.
261 // On the last frame the AEC buffer is adjusted to 60% of the last reported
262 // device buffer size.
263 // We construct an unstable system by altering the device buffer size between
264 // two values |kDeviceBufMs| +- 25 ms.
265 for (size_t i = 0; i < kNumSampleRates; i++) {
266 Init(kSampleRateHz[i]);
267
268 // To make sure we have a full buffer when we verify stability we first fill
269 // up the far-end buffer with the same amount as we will report in on the
270 // average through Process().
Peter Kastingdce40cf2015-08-24 14:52:23 -0700271 size_t buffer_size = BufferFillUp();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000272
273 int buffer_offset_ms = 25;
274 int reported_delay_ms = 0;
275 int process_time_ms = 0;
276 for (; process_time_ms <= kMaxConvergenceMs; process_time_ms += 10) {
277 reported_delay_ms = kDeviceBufMs + buffer_offset_ms;
278 RenderAndCapture(reported_delay_ms);
279 buffer_size += samples_per_frame_;
280 buffer_offset_ms = -buffer_offset_ms;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000281 if (self_->startup_phase == 0) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000282 // We have left the startup phase.
283 break;
284 }
285 }
286 // Verify convergence time.
287 EXPECT_GE(kMaxConvergenceMs, process_time_ms);
288 // Verify that the buffer has been flushed.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700289 EXPECT_GE(static_cast<int>(buffer_size),
290 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000291
292 // Verify system delay with respect to requirements, i.e., the
293 // |system_delay| is in the interval [60%, 100%] of what's last reported.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700294 EXPECT_GE(static_cast<int>(reported_delay_ms * samples_per_frame_ / 10),
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000295 WebRtcAec_system_delay(self_->aec));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700296 EXPECT_LE(
297 static_cast<int>(reported_delay_ms * samples_per_frame_ / 10 * 3 / 5),
298 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000299 }
300}
301
Bjorn Volcker71012692015-06-18 11:04:56 +0200302TEST_F(SystemDelayTest, CorrectDelayAfterStableBufferBuildUp) {
303 // This test does not apply in extended_filter mode, since we only use the
304 // the first 10 ms chunk to determine a reasonable buffer size. Neither does
305 // it apply if DA-AEC is on because that overrides the startup procedure.
306 WebRtcAec_enable_extended_filter(self_->aec, 0);
307 EXPECT_EQ(0, WebRtcAec_extended_filter_enabled(self_->aec));
henrik.lundin0f133b92015-07-02 00:17:55 -0700308 WebRtcAec_enable_delay_agnostic(self_->aec, 0);
309 EXPECT_EQ(0, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200310
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000311 // In this test we start by establishing the device buffer size during stable
312 // conditions, but with an empty internal far-end buffer. Once that is done we
313 // verify that the system delay is increased correctly until we have reach an
314 // internal buffer size of 75% of what's been reported.
315 for (size_t i = 0; i < kNumSampleRates; i++) {
316 Init(kSampleRateHz[i]);
317
318 // We assume that running |kStableConvergenceMs| calls will put the
319 // algorithm in a state where the device buffer size has been determined. We
320 // can make that assumption since we have a separate stability test.
321 int process_time_ms = 0;
322 for (; process_time_ms < kStableConvergenceMs; process_time_ms += 10) {
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000323 EXPECT_EQ(0,
324 WebRtcAec_Process(handle_,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000325 &near_ptr_,
326 1,
327 &out_ptr_,
andrew@webrtc.org13b2d462013-10-08 23:41:42 +0000328 samples_per_frame_,
329 kDeviceBufMs,
330 0));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000331 }
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000332 // Verify that a buffer size has been established.
333 EXPECT_EQ(0, self_->checkBuffSize);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000334
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000335 // We now have established the required buffer size. Let us verify that we
336 // fill up before leaving the startup phase for normal processing.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700337 size_t buffer_size = 0;
338 size_t target_buffer_size = kDeviceBufMs * samples_per_frame_ / 10 * 3 / 4;
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000339 process_time_ms = 0;
340 for (; process_time_ms <= kMaxConvergenceMs; process_time_ms += 10) {
341 RenderAndCapture(kDeviceBufMs);
342 buffer_size += samples_per_frame_;
343 if (self_->startup_phase == 0) {
344 // We have left the startup phase.
345 break;
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000346 }
347 }
bjornv@webrtc.orgb51d3ea2014-06-05 13:41:33 +0000348 // Verify convergence time.
349 EXPECT_GT(kMaxConvergenceMs, process_time_ms);
350 // Verify that the buffer has reached the desired size.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700351 EXPECT_LE(static_cast<int>(target_buffer_size),
352 WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000353
354 // Verify normal behavior (system delay is kept constant) after startup by
355 // running a couple of calls to BufferFarend() and Process().
356 for (int j = 0; j < 6; j++) {
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000357 int system_delay_before_calls = WebRtcAec_system_delay(self_->aec);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000358 RenderAndCapture(kDeviceBufMs);
bjornv@webrtc.org4d1cfae2013-02-20 17:31:38 +0000359 EXPECT_EQ(system_delay_before_calls, WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000360 }
361 }
362}
363
364TEST_F(SystemDelayTest, CorrectDelayWhenBufferUnderrun) {
365 // Here we test a buffer under run scenario. If we keep on calling
366 // WebRtcAec_Process() we will finally run out of data, but should
367 // automatically stuff the buffer. We verify this behavior by checking if the
368 // system delay goes negative.
Bjorn Volcker71012692015-06-18 11:04:56 +0200369 // This process should be independent of DA-AEC and extended_filter mode.
370 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
371 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
372 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
373 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700374 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
375 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200376 for (size_t i = 0; i < kNumSampleRates; i++) {
377 Init(kSampleRateHz[i]);
378 RunStableStartup();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000379
Bjorn Volcker71012692015-06-18 11:04:56 +0200380 // The AEC has now left the Startup phase. We now have at most
381 // |kStableConvergenceMs| in the buffer. Keep on calling Process() until
382 // we run out of data and verify that the system delay is non-negative.
383 for (int j = 0; j <= kStableConvergenceMs; j += 10) {
384 EXPECT_EQ(0, WebRtcAec_Process(handle_, &near_ptr_, 1, &out_ptr_,
385 samples_per_frame_, kDeviceBufMs, 0));
386 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
387 }
388 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000389 }
390 }
391}
392
Bjorn Volcker71012692015-06-18 11:04:56 +0200393TEST_F(SystemDelayTest, CorrectDelayDuringDrift) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000394 // This drift test should verify that the system delay is never exceeding the
395 // device buffer. The drift is simulated by decreasing the reported device
396 // buffer size by 1 ms every 100 ms. If the device buffer size goes below 30
397 // ms we jump (add) 10 ms to give a repeated pattern.
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000398
Bjorn Volcker71012692015-06-18 11:04:56 +0200399 // This process should be independent of DA-AEC and extended_filter mode.
400 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
401 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
402 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
403 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700404 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
405 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200406 for (size_t i = 0; i < kNumSampleRates; i++) {
407 Init(kSampleRateHz[i]);
408 RunStableStartup();
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000409
Bjorn Volcker71012692015-06-18 11:04:56 +0200410 // We have left the startup phase and proceed with normal processing.
411 int jump = 0;
412 for (int j = 0; j < 1000; j++) {
413 // Drift = -1 ms per 100 ms of data.
414 int device_buf_ms = kDeviceBufMs - (j / 10) + jump;
415 int device_buf = MapBufferSizeToSamples(device_buf_ms,
416 extended_filter == 1);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000417
Bjorn Volcker71012692015-06-18 11:04:56 +0200418 if (device_buf_ms < 30) {
419 // Add 10 ms data, taking affect next frame.
420 jump += 10;
421 }
422 RenderAndCapture(device_buf_ms);
423
424 // Verify that the system delay does not exceed the device buffer.
425 EXPECT_GE(device_buf, WebRtcAec_system_delay(self_->aec));
426
427 // Verify that the system delay is non-negative.
428 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
429 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000430 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000431 }
432 }
433}
434
Bjorn Volcker71012692015-06-18 11:04:56 +0200435TEST_F(SystemDelayTest, ShouldRecoverAfterGlitch) {
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000436 // This glitch test should verify that the system delay recovers if there is
437 // a glitch in data. The data glitch is constructed as 200 ms of buffering
438 // after which the stable procedure continues. The glitch is never reported by
439 // the device.
440 // The system is said to be in a non-causal state if the difference between
441 // the device buffer and system delay is less than a block (64 samples).
bjornv@webrtc.org5c3f4e32014-06-19 09:51:29 +0000442
Bjorn Volcker71012692015-06-18 11:04:56 +0200443 // This process should be independent of DA-AEC and extended_filter mode.
444 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
445 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
446 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
447 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700448 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
449 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200450 for (size_t i = 0; i < kNumSampleRates; i++) {
451 Init(kSampleRateHz[i]);
452 RunStableStartup();
453 int device_buf = MapBufferSizeToSamples(kDeviceBufMs,
454 extended_filter == 1);
455 // Glitch state.
456 for (int j = 0; j < 20; j++) {
457 EXPECT_EQ(0,
458 WebRtcAec_BufferFarend(handle_, far_, samples_per_frame_));
459 // No need to verify system delay, since that is done in a separate
460 // test.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000461 }
Bjorn Volcker71012692015-06-18 11:04:56 +0200462 // Verify that we are in a non-causal state, i.e.,
463 // |system_delay| > |device_buf|.
464 EXPECT_LT(device_buf, WebRtcAec_system_delay(self_->aec));
465
466 // Recover state. Should recover at least 4 ms of data per 10 ms, hence
467 // a glitch of 200 ms will take at most 200 * 10 / 4 = 500 ms to recover
468 // from.
469 bool non_causal = true; // We are currently in a non-causal state.
470 for (int j = 0; j < 50; j++) {
471 int system_delay_before = WebRtcAec_system_delay(self_->aec);
472 RenderAndCapture(kDeviceBufMs);
473 int system_delay_after = WebRtcAec_system_delay(self_->aec);
474 // We have recovered if
475 // |device_buf| - |system_delay_after| >= PART_LEN (1 block).
476 // During recovery, |system_delay_after| < |system_delay_before|,
477 // otherwise they are equal.
478 if (non_causal) {
479 EXPECT_LT(system_delay_after, system_delay_before);
480 if (device_buf - system_delay_after >= PART_LEN) {
481 non_causal = false;
482 }
483 } else {
484 EXPECT_EQ(system_delay_before, system_delay_after);
485 }
486 // Verify that the system delay is non-negative.
487 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
488 }
489 // Check that we have recovered.
490 EXPECT_FALSE(non_causal);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000491 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000492 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000493 }
494}
495
496TEST_F(SystemDelayTest, UnaffectedWhenSpuriousDeviceBufferValues) {
Bjorn Volcker71012692015-06-18 11:04:56 +0200497 // This test does not apply in extended_filter mode, since we only use the
498 // the first 10 ms chunk to determine a reasonable buffer size.
499 const int extended_filter = 0;
500 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
501 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000502
Bjorn Volcker71012692015-06-18 11:04:56 +0200503 // Should be DA-AEC independent.
504 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700505 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
506 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200507 // This spurious device buffer data test aims at verifying that the system
508 // delay is unaffected by large outliers.
509 // The system is said to be in a non-causal state if the difference between
510 // the device buffer and system delay is less than a block (64 samples).
511 for (size_t i = 0; i < kNumSampleRates; i++) {
512 Init(kSampleRateHz[i]);
513 RunStableStartup();
514 int device_buf = MapBufferSizeToSamples(kDeviceBufMs,
515 extended_filter == 1);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000516
Bjorn Volcker71012692015-06-18 11:04:56 +0200517 // Normal state. We are currently not in a non-causal state.
518 bool non_causal = false;
519
520 // Run 1 s and replace device buffer size with 500 ms every 100 ms.
521 for (int j = 0; j < 100; j++) {
522 int system_delay_before_calls = WebRtcAec_system_delay(self_->aec);
523 int device_buf_ms = j % 10 == 0 ? 500 : kDeviceBufMs;
524 RenderAndCapture(device_buf_ms);
525
526 // Check for non-causality.
527 if (device_buf - WebRtcAec_system_delay(self_->aec) < PART_LEN) {
528 non_causal = true;
529 }
530 EXPECT_FALSE(non_causal);
531 EXPECT_EQ(system_delay_before_calls,
532 WebRtcAec_system_delay(self_->aec));
533
534 // Verify that the system delay is non-negative.
535 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000536 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000537 }
538 }
539}
540
541TEST_F(SystemDelayTest, CorrectImpactWhenTogglingDeviceBufferValues) {
542 // This test aims at verifying that the system delay is "unaffected" by
543 // toggling values reported by the device.
544 // The test is constructed such that every other device buffer value is zero
545 // and then 2 * |kDeviceBufMs|, hence the size is constant on the average. The
546 // zero values will force us into a non-causal state and thereby lowering the
Bjorn Volcker71012692015-06-18 11:04:56 +0200547 // system delay until we basically run out of data. Once that happens the
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000548 // buffer will be stuffed.
549 // TODO(bjornv): This test will have a better impact if we verified that the
Bjorn Volcker71012692015-06-18 11:04:56 +0200550 // delay estimate goes up when the system delay goes down to meet the average
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000551 // device buffer size.
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000552
Bjorn Volcker71012692015-06-18 11:04:56 +0200553 // This test does not apply if DA-AEC is enabled and extended_filter mode
554 // disabled.
555 for (int extended_filter = 0; extended_filter <= 1; ++extended_filter) {
556 WebRtcAec_enable_extended_filter(self_->aec, extended_filter);
557 EXPECT_EQ(extended_filter, WebRtcAec_extended_filter_enabled(self_->aec));
558 for (int da_aec = 0; da_aec <= 1; ++da_aec) {
henrik.lundin0f133b92015-07-02 00:17:55 -0700559 WebRtcAec_enable_delay_agnostic(self_->aec, da_aec);
560 EXPECT_EQ(da_aec, WebRtcAec_delay_agnostic_enabled(self_->aec));
Bjorn Volcker71012692015-06-18 11:04:56 +0200561 if (extended_filter == 0 && da_aec == 1) {
562 continue;
563 }
564 for (size_t i = 0; i < kNumSampleRates; i++) {
565 Init(kSampleRateHz[i]);
566 RunStableStartup();
567 const int device_buf = MapBufferSizeToSamples(kDeviceBufMs,
568 extended_filter == 1);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000569
Bjorn Volcker71012692015-06-18 11:04:56 +0200570 // Normal state. We are currently not in a non-causal state.
571 bool non_causal = false;
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000572
Bjorn Volcker71012692015-06-18 11:04:56 +0200573 // Loop through 100 frames (both render and capture), which equals 1 s
574 // of data. Every odd frame we set the device buffer size to
575 // 2 * |kDeviceBufMs| and even frames we set the device buffer size to
576 // zero.
577 for (int j = 0; j < 100; j++) {
578 int system_delay_before_calls = WebRtcAec_system_delay(self_->aec);
579 int device_buf_ms = 2 * (j % 2) * kDeviceBufMs;
580 RenderAndCapture(device_buf_ms);
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000581
Bjorn Volcker71012692015-06-18 11:04:56 +0200582 // Check for non-causality, compared with the average device buffer
583 // size.
584 non_causal |= (device_buf - WebRtcAec_system_delay(self_->aec) < 64);
585 EXPECT_GE(system_delay_before_calls,
586 WebRtcAec_system_delay(self_->aec));
587
588 // Verify that the system delay is non-negative.
589 EXPECT_LE(0, WebRtcAec_system_delay(self_->aec));
590 }
591 // Verify we are not in a non-causal state.
592 EXPECT_FALSE(non_causal);
593 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000594 }
bjornv@webrtc.org70569082012-04-12 12:13:50 +0000595 }
596}
597
598} // namespace
peah50e21bd2016-03-05 08:39:21 -0800599} // namespace webrtc