blob: f2656b817ead8fc3b74a68804b760c83c94ee523 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +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
11// Unit tests for DelayManager class.
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/delay_manager.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000014
15#include <math.h>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/mock/mock_delay_peak_detector.h"
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010018#include "rtc_base/checks.h"
Minyue Li002fbb82018-10-04 11:31:03 +020019#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "test/gmock.h"
21#include "test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000022
23namespace webrtc {
24
25using ::testing::Return;
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000026using ::testing::_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027
28class DelayManagerTest : public ::testing::Test {
29 protected:
30 static const int kMaxNumberOfPackets = 240;
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010031 static const int kMinDelayMs = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 static const int kTimeStepMs = 10;
33 static const int kFs = 8000;
34 static const int kFrameSizeMs = 20;
35 static const int kTsIncrement = kFrameSizeMs * kFs / 1000;
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010036 static const int kMaxBufferSizeMs = kMaxNumberOfPackets * kFrameSizeMs;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
38 DelayManagerTest();
39 virtual void SetUp();
40 virtual void TearDown();
Minyue Li002fbb82018-10-04 11:31:03 +020041 void RecreateDelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000042 void SetPacketAudioLength(int lengt_ms);
43 void InsertNextPacket();
44 void IncreaseTime(int inc_ms);
45
Minyue Li002fbb82018-10-04 11:31:03 +020046 std::unique_ptr<DelayManager> dm_;
henrik.lundinf3933702016-04-28 01:53:52 -070047 TickTimer tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048 MockDelayPeakDetector detector_;
49 uint16_t seq_no_;
50 uint32_t ts_;
Jakob Ivarssone98954c2019-02-06 15:37:50 +010051 bool enable_rtx_handling_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052};
53
54DelayManagerTest::DelayManagerTest()
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010055 : dm_(nullptr),
56 detector_(&tick_timer_, false),
57 seq_no_(0x1234),
58 ts_(0x12345678) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
60void DelayManagerTest::SetUp() {
Minyue Li002fbb82018-10-04 11:31:03 +020061 RecreateDelayManager();
62}
63
64void DelayManagerTest::RecreateDelayManager() {
Yves Gerey665174f2018-06-19 15:03:05 +020065 EXPECT_CALL(detector_, Reset()).Times(1);
Jakob Ivarssone98954c2019-02-06 15:37:50 +010066 dm_.reset(new DelayManager(kMaxNumberOfPackets, kMinDelayMs,
67 enable_rtx_handling_, &detector_, &tick_timer_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068}
69
70void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
71 EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms));
72 dm_->SetPacketAudioLength(lengt_ms);
73}
74
75void DelayManagerTest::InsertNextPacket() {
76 EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs));
77 seq_no_ += 1;
78 ts_ += kTsIncrement;
79}
80
81void DelayManagerTest::IncreaseTime(int inc_ms) {
82 for (int t = 0; t < inc_ms; t += kTimeStepMs) {
henrik.lundinf3933702016-04-28 01:53:52 -070083 tick_timer_.Increment();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 }
85}
86void DelayManagerTest::TearDown() {
87 EXPECT_CALL(detector_, Die());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088}
89
90TEST_F(DelayManagerTest, CreateAndDestroy) {
91 // Nothing to do here. The test fixture creates and destroys the DelayManager
92 // object.
93}
94
95TEST_F(DelayManagerTest, VectorInitialization) {
96 const DelayManager::IATVector& vec = dm_->iat_vector();
97 double sum = 0.0;
98 for (size_t i = 0; i < vec.size(); i++) {
Henrik Lundincb3e8fe2015-05-11 15:15:51 +020099 EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
100 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101 sum += vec[i];
102 }
103 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
104}
105
106TEST_F(DelayManagerTest, SetPacketAudioLength) {
107 const int kLengthMs = 30;
108 // Expect DelayManager to pass on the new length to the detector object.
Yves Gerey665174f2018-06-19 15:03:05 +0200109 EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs)).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000110 EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs));
111 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value.
112}
113
114TEST_F(DelayManagerTest, PeakFound) {
115 // Expect DelayManager to pass on the question to the detector.
116 // Call twice, and let the detector return true the first time and false the
117 // second time.
118 EXPECT_CALL(detector_, peak_found())
119 .WillOnce(Return(true))
120 .WillOnce(Return(false));
121 EXPECT_TRUE(dm_->PeakFound());
122 EXPECT_FALSE(dm_->PeakFound());
123}
124
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000125TEST_F(DelayManagerTest, UpdateNormal) {
126 SetPacketAudioLength(kFrameSizeMs);
127 // First packet arrival.
128 InsertNextPacket();
129 // Advance time by one frame size.
130 IncreaseTime(kFrameSizeMs);
131 // Second packet arrival.
132 // Expect detector update method to be called once with inter-arrival time
133 // equal to 1 packet, and (base) target level equal to 1 as well.
134 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100135 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136 InsertNextPacket();
137 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
138 EXPECT_EQ(1, dm_->base_target_level());
139 int lower, higher;
140 dm_->BufferLimits(&lower, &higher);
141 // Expect |lower| to be 75% of target level, and |higher| to be target level,
142 // but also at least 20 ms higher than |lower|, which is the limiting case
143 // here.
144 EXPECT_EQ((1 << 8) * 3 / 4, lower);
145 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
146}
147
148TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) {
149 SetPacketAudioLength(kFrameSizeMs);
150 // First packet arrival.
151 InsertNextPacket();
152 // Advance time by two frame size.
153 IncreaseTime(2 * kFrameSizeMs);
154 // Second packet arrival.
155 // Expect detector update method to be called once with inter-arrival time
156 // equal to 1 packet, and (base) target level equal to 1 as well.
157 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100158 EXPECT_CALL(detector_, Update(2, false, 2)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159 InsertNextPacket();
160 EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8.
161 EXPECT_EQ(2, dm_->base_target_level());
162 int lower, higher;
163 dm_->BufferLimits(&lower, &higher);
164 // Expect |lower| to be 75% of target level, and |higher| to be target level,
165 // but also at least 20 ms higher than |lower|, which is the limiting case
166 // here.
167 EXPECT_EQ((2 << 8) * 3 / 4, lower);
168 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
169}
170
171TEST_F(DelayManagerTest, UpdatePeakFound) {
172 SetPacketAudioLength(kFrameSizeMs);
173 // First packet arrival.
174 InsertNextPacket();
175 // Advance time by one frame size.
176 IncreaseTime(kFrameSizeMs);
177 // Second packet arrival.
178 // Expect detector update method to be called once with inter-arrival time
179 // equal to 1 packet, and (base) target level equal to 1 as well.
180 // Return true to indicate that peaks are found. Let the peak height be 5.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100181 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(true));
Yves Gerey665174f2018-06-19 15:03:05 +0200182 EXPECT_CALL(detector_, MaxPeakHeight()).WillOnce(Return(5));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000183 InsertNextPacket();
184 EXPECT_EQ(5 << 8, dm_->TargetLevel());
185 EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks.
186 int lower, higher;
187 dm_->BufferLimits(&lower, &higher);
188 // Expect |lower| to be 75% of target level, and |higher| to be target level.
189 EXPECT_EQ((5 << 8) * 3 / 4, lower);
190 EXPECT_EQ(5 << 8, higher);
191}
192
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000193TEST_F(DelayManagerTest, TargetDelay) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194 SetPacketAudioLength(kFrameSizeMs);
195 // First packet arrival.
196 InsertNextPacket();
197 // Advance time by one frame size.
198 IncreaseTime(kFrameSizeMs);
199 // Second packet arrival.
200 // Expect detector update method to be called once with inter-arrival time
201 // equal to 1 packet, and (base) target level equal to 1 as well.
202 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100203 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000205 const int kExpectedTarget = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
207 EXPECT_EQ(1, dm_->base_target_level());
208 int lower, higher;
209 dm_->BufferLimits(&lower, &higher);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000210 // Expect |lower| to be 75% of base target level, and |higher| to be
211 // lower + 20 ms headroom.
212 EXPECT_EQ((1 << 8) * 3 / 4, lower);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
214}
215
Niels Möller18f1adc2018-08-23 08:40:41 +0200216TEST_F(DelayManagerTest, MaxDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000217 const int kExpectedTarget = 5;
218 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
219 SetPacketAudioLength(kFrameSizeMs);
220 // First packet arrival.
221 InsertNextPacket();
222 // Second packet arrival.
223 // Expect detector update method to be called once with inter-arrival time
224 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100225 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000226 .WillRepeatedly(Return(true));
227 EXPECT_CALL(detector_, MaxPeakHeight())
228 .WillRepeatedly(Return(kExpectedTarget));
229 IncreaseTime(kTimeIncrement);
230 InsertNextPacket();
231
232 // No limit is set.
233 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
234
235 int kMaxDelayPackets = kExpectedTarget - 2;
236 int kMaxDelayMs = kMaxDelayPackets * kFrameSizeMs;
237 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
238 IncreaseTime(kTimeIncrement);
239 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000240 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
241
242 // Target level at least should be one packet.
243 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
244}
245
Niels Möller18f1adc2018-08-23 08:40:41 +0200246TEST_F(DelayManagerTest, MinDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000247 const int kExpectedTarget = 5;
248 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
249 SetPacketAudioLength(kFrameSizeMs);
250 // First packet arrival.
251 InsertNextPacket();
252 // Second packet arrival.
253 // Expect detector update method to be called once with inter-arrival time
254 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100255 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000256 .WillRepeatedly(Return(true));
257 EXPECT_CALL(detector_, MaxPeakHeight())
258 .WillRepeatedly(Return(kExpectedTarget));
259 IncreaseTime(kTimeIncrement);
260 InsertNextPacket();
261
262 // No limit is applied.
263 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
264
265 int kMinDelayPackets = kExpectedTarget + 2;
266 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
267 dm_->SetMinimumDelay(kMinDelayMs);
268 IncreaseTime(kTimeIncrement);
269 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000270 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
271}
272
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100273TEST_F(DelayManagerTest, BaseMinimumDelayCheckValidRange) {
274 SetPacketAudioLength(kFrameSizeMs);
275
276 // Base minimum delay should be between [0, 10000] milliseconds.
277 EXPECT_FALSE(dm_->SetBaseMinimumDelay(-1));
278 EXPECT_FALSE(dm_->SetBaseMinimumDelay(10001));
279 EXPECT_EQ(dm_->GetBaseMinimumDelay(), 0);
280
281 EXPECT_TRUE(dm_->SetBaseMinimumDelay(7999));
282 EXPECT_EQ(dm_->GetBaseMinimumDelay(), 7999);
283}
284
285TEST_F(DelayManagerTest, BaseMinimumDelayLowerThanMinimumDelay) {
286 SetPacketAudioLength(kFrameSizeMs);
287 constexpr int kBaseMinimumDelayMs = 100;
288 constexpr int kMinimumDelayMs = 200;
289
290 // Base minimum delay sets lower bound on minimum. That is why when base
291 // minimum delay is lower than minimum delay we use minimum delay.
292 RTC_DCHECK_LT(kBaseMinimumDelayMs, kMinimumDelayMs);
293
294 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
295 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
296 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMinimumDelayMs);
297}
298
299TEST_F(DelayManagerTest, BaseMinimumDelayGreaterThanMinimumDelay) {
300 SetPacketAudioLength(kFrameSizeMs);
301 constexpr int kBaseMinimumDelayMs = 70;
302 constexpr int kMinimumDelayMs = 30;
303
304 // Base minimum delay sets lower bound on minimum. That is why when base
305 // minimum delay is greater than minimum delay we use base minimum delay.
306 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
307
308 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
309 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
310 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kBaseMinimumDelayMs);
311}
312
313TEST_F(DelayManagerTest, BaseMinimumDelayGreaterThanBufferSize) {
314 SetPacketAudioLength(kFrameSizeMs);
315 constexpr int kBaseMinimumDelayMs = kMaxBufferSizeMs + 1;
316 constexpr int kMinimumDelayMs = 12;
317 constexpr int kMaxBufferSizeMsQ75 = 3 * kMaxBufferSizeMs / 4;
318
319 // Base minimum delay is greater than minimum delay, that is why we clamp
320 // it to current the highest possible value which is maximum delay.
321 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
322 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMaxBufferSizeMs);
323
324 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
325 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
326
327 // Unset maximum value.
328 EXPECT_TRUE(dm_->SetMaximumDelay(0));
329
330 // With maximum value unset, the highest possible value now is 75% of
331 // currently possible maximum buffer size.
332 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMaxBufferSizeMsQ75);
333}
334
335TEST_F(DelayManagerTest, BaseMinimumDelayGreaterThanMaximumDelay) {
336 SetPacketAudioLength(kFrameSizeMs);
337 constexpr int kMaximumDelayMs = 400;
338 constexpr int kBaseMinimumDelayMs = kMaximumDelayMs + 1;
339 constexpr int kMinimumDelayMs = 20;
340
341 // Base minimum delay is greater than minimum delay, that is why we clamp
342 // it to current the highest possible value which is kMaximumDelayMs.
343 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
344 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMaximumDelayMs);
345 RTC_DCHECK_LT(kMaximumDelayMs, kMaxBufferSizeMs);
346
347 EXPECT_TRUE(dm_->SetMaximumDelay(kMaximumDelayMs));
348 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
349 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
350 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMaximumDelayMs);
351}
352
353TEST_F(DelayManagerTest, BaseMinimumDelayLowerThanMaxSize) {
354 SetPacketAudioLength(kFrameSizeMs);
355 constexpr int kMaximumDelayMs = 400;
356 constexpr int kBaseMinimumDelayMs = kMaximumDelayMs - 1;
357 constexpr int kMinimumDelayMs = 20;
358
359 // Base minimum delay is greater than minimum delay, and lower than maximum
360 // delays that is why it is used.
361 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
362 RTC_DCHECK_LT(kBaseMinimumDelayMs, kMaximumDelayMs);
363
364 EXPECT_TRUE(dm_->SetMaximumDelay(kMaximumDelayMs));
365 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
366 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
367 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kBaseMinimumDelayMs);
368}
369
370TEST_F(DelayManagerTest, MinimumDelayMemorization) {
371 // Check that when we increase base minimum delay to value higher than
372 // minimum delay then minimum delay is still memorized. This allows to
373 // restore effective minimum delay to memorized minimum delay value when we
374 // decrease base minimum delay.
375 SetPacketAudioLength(kFrameSizeMs);
376
377 constexpr int kBaseMinimumDelayMsLow = 10;
378 constexpr int kMinimumDelayMs = 20;
379 constexpr int kBaseMinimumDelayMsHigh = 30;
380
381 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMsLow));
382 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
383 // Minimum delay is used as it is higher than base minimum delay.
384 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMinimumDelayMs);
385
386 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMsHigh));
387 // Base minimum delay is used as it is now higher than minimum delay.
388 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(),
389 kBaseMinimumDelayMsHigh);
390
391 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMsLow));
392 // Check that minimum delay is memorized and is used again.
393 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMinimumDelayMs);
394}
395
396TEST_F(DelayManagerTest, BaseMinimumDelay) {
Ruslan Burakovedbea462019-02-04 16:17:31 +0100397 const int kExpectedTarget = 5;
398 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
399 SetPacketAudioLength(kFrameSizeMs);
400 // First packet arrival.
401 InsertNextPacket();
402 // Second packet arrival.
403 // Expect detector update method to be called once with inter-arrival time
404 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
405 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
406 .WillRepeatedly(Return(true));
407 EXPECT_CALL(detector_, MaxPeakHeight())
408 .WillRepeatedly(Return(kExpectedTarget));
409 IncreaseTime(kTimeIncrement);
410 InsertNextPacket();
411
412 // No limit is applied.
413 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
414
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100415 constexpr int kBaseMinimumDelayPackets = kExpectedTarget + 2;
416 constexpr int kBaseMinimumDelayMs = kBaseMinimumDelayPackets * kFrameSizeMs;
417 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
418 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
Ruslan Burakovedbea462019-02-04 16:17:31 +0100419
420 IncreaseTime(kTimeIncrement);
421 InsertNextPacket();
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100422 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
423 EXPECT_EQ(kBaseMinimumDelayPackets << 8, dm_->TargetLevel());
Ruslan Burakovedbea462019-02-04 16:17:31 +0100424}
425
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100426TEST_F(DelayManagerTest, BaseMinimumDealyAffectTargetLevel) {
427 const int kExpectedTarget = 5;
428 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
429 SetPacketAudioLength(kFrameSizeMs);
430 // First packet arrival.
431 InsertNextPacket();
432 // Second packet arrival.
433 // Expect detector update method to be called once with inter-arrival time
434 // equal to |kExpectedTarget|. Return true to indicate peaks found.
435 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
436 .WillRepeatedly(Return(true));
437 EXPECT_CALL(detector_, MaxPeakHeight())
438 .WillRepeatedly(Return(kExpectedTarget));
439 IncreaseTime(kTimeIncrement);
440 InsertNextPacket();
Ruslan Burakovedbea462019-02-04 16:17:31 +0100441
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100442 // No limit is applied.
443 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
444
445 // Minimum delay is lower than base minimum delay, that is why base minimum
446 // delay is used to calculate target level.
447 constexpr int kMinimumDelayPackets = kExpectedTarget + 1;
448 constexpr int kBaseMinimumDelayPackets = kExpectedTarget + 2;
449
450 constexpr int kMinimumDelayMs = kMinimumDelayPackets * kFrameSizeMs;
451 constexpr int kBaseMinimumDelayMs = kBaseMinimumDelayPackets * kFrameSizeMs;
452
453 EXPECT_TRUE(kMinimumDelayMs < kBaseMinimumDelayMs);
454 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
455 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
456 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
457
458 IncreaseTime(kTimeIncrement);
459 InsertNextPacket();
460 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
461 EXPECT_EQ(kBaseMinimumDelayPackets << 8, dm_->TargetLevel());
Ruslan Burakovedbea462019-02-04 16:17:31 +0100462}
463
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100464TEST_F(DelayManagerTest, UpdateReorderedPacket) {
465 SetPacketAudioLength(kFrameSizeMs);
466 InsertNextPacket();
467
468 // Insert packet that was sent before the previous packet.
469 EXPECT_CALL(detector_, Update(_, true, _));
470 EXPECT_EQ(0, dm_->Update(seq_no_ - 1, ts_ - kFrameSizeMs, kFs));
471}
472
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100473TEST_F(DelayManagerTest, EnableRtxHandling) {
474 enable_rtx_handling_ = true;
475 RecreateDelayManager();
476
477 // Insert first packet.
478 SetPacketAudioLength(kFrameSizeMs);
479 InsertNextPacket();
480
481 // Insert reordered packet.
482 // TODO(jakobi): Test estimated inter-arrival time by mocking the histogram
483 // instead of checking the call to the peak detector.
484 EXPECT_CALL(detector_, Update(3, true, _));
485 EXPECT_EQ(0, dm_->Update(seq_no_ - 3, ts_ - 3 * kFrameSizeMs, kFs));
486
487 // Insert another reordered packet.
488 EXPECT_CALL(detector_, Update(2, true, _));
489 EXPECT_EQ(0, dm_->Update(seq_no_ - 2, ts_ - 2 * kFrameSizeMs, kFs));
490
491 // Insert the next packet in order and verify that the inter-arrival time is
492 // estimated correctly.
493 IncreaseTime(kFrameSizeMs);
494 EXPECT_CALL(detector_, Update(1, false, _));
495 InsertNextPacket();
496}
497
henrik.lundinb8c55b12017-05-10 07:38:01 -0700498// Tests that skipped sequence numbers (simulating empty packets) are handled
499// correctly.
500TEST_F(DelayManagerTest, EmptyPacketsReported) {
501 SetPacketAudioLength(kFrameSizeMs);
502 // First packet arrival.
503 InsertNextPacket();
504
505 // Advance time by one frame size.
506 IncreaseTime(kFrameSizeMs);
507
508 // Advance the sequence number by 5, simulating that 5 empty packets were
509 // received, but never inserted.
510 seq_no_ += 10;
511 for (int j = 0; j < 10; ++j) {
512 dm_->RegisterEmptyPacket();
513 }
514
515 // Second packet arrival.
516 // Expect detector update method to be called once with inter-arrival time
517 // equal to 1 packet, and (base) target level equal to 1 as well.
518 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100519 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700520 InsertNextPacket();
521
522 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
523}
524
525// Same as above, but do not call RegisterEmptyPacket. Observe the target level
526// increase dramatically.
527TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
528 SetPacketAudioLength(kFrameSizeMs);
529 // First packet arrival.
530 InsertNextPacket();
531
532 // Advance time by one frame size.
533 IncreaseTime(kFrameSizeMs);
534
535 // Advance the sequence number by 5, simulating that 5 empty packets were
536 // received, but never inserted.
537 seq_no_ += 10;
538
539 // Second packet arrival.
540 // Expect detector update method to be called once with inter-arrival time
541 // equal to 1 packet, and (base) target level equal to 1 as well.
542 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100543 EXPECT_CALL(detector_, Update(10, false, 10)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700544 InsertNextPacket();
545
546 // Note 10 times higher target value.
547 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
548}
549
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000550TEST_F(DelayManagerTest, Failures) {
551 // Wrong sample rate.
552 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
553 // Wrong packet size.
554 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
555 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000556
557 // Minimum delay higher than a maximum delay is not accepted.
558 EXPECT_TRUE(dm_->SetMaximumDelay(10));
559 EXPECT_FALSE(dm_->SetMinimumDelay(20));
560
561 // Maximum delay less than minimum delay is not accepted.
562 EXPECT_TRUE(dm_->SetMaximumDelay(100));
563 EXPECT_TRUE(dm_->SetMinimumDelay(80));
564 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000565}
566
Minyue Li002fbb82018-10-04 11:31:03 +0200567TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
568 test::ScopedFieldTrials field_trial(
569 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
570 RecreateDelayManager();
571 EXPECT_EQ(absl::make_optional<int>(1 << 30),
572 dm_->forced_limit_probability_for_test());
573
574 SetPacketAudioLength(kFrameSizeMs);
575 // First packet arrival.
576 InsertNextPacket();
577 // Advance time by one frame size.
578 IncreaseTime(kFrameSizeMs);
579 // Second packet arrival.
580 // Expect detector update method to be called once with inter-arrival time
581 // equal to 1 packet.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100582 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
Minyue Li002fbb82018-10-04 11:31:03 +0200583 InsertNextPacket();
584 constexpr int kExpectedTarget = 1;
585 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
586}
587
588TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
589 {
590 test::ScopedFieldTrials field_trial(
591 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
592 RecreateDelayManager();
593 EXPECT_EQ(absl::make_optional<int>(53687091),
594 dm_->forced_limit_probability_for_test()); // 1/20 in Q30
595 }
596 {
597 test::ScopedFieldTrials field_trial(
598 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
599 RecreateDelayManager();
600 EXPECT_EQ(absl::make_optional<int>(536871),
601 dm_->forced_limit_probability_for_test()); // 1/2000 in Q30
602 }
603 {
604 test::ScopedFieldTrials field_trial(
605 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
606 RecreateDelayManager();
607 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
608 }
609 {
610 test::ScopedFieldTrials field_trial(
611 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
612 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
613 }
614 {
615 test::ScopedFieldTrials field_trial(
616 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
617 RecreateDelayManager();
618 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
619 }
620}
621
Ivo Creusen385b10b2017-10-13 12:37:27 +0200622// Test if the histogram is stretched correctly if the packet size is decreased.
623TEST(DelayManagerIATScalingTest, StretchTest) {
624 using IATVector = DelayManager::IATVector;
625 // Test a straightforward 60ms to 20ms change.
626 IATVector iat = {12, 0, 0, 0, 0, 0};
627 IATVector expected_result = {4, 4, 4, 0, 0, 0};
628 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
629 EXPECT_EQ(stretched_iat, expected_result);
630
631 // Test an example where the last bin in the stretched histogram should
632 // contain the sum of the elements that don't fit into the new histogram.
633 iat = {18, 15, 12, 9, 6, 3, 0};
634 expected_result = {6, 6, 6, 5, 5, 5, 30};
635 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
636 EXPECT_EQ(stretched_iat, expected_result);
637
638 // Test a 120ms to 60ms change.
639 iat = {18, 16, 14, 4, 0};
640 expected_result = {9, 9, 8, 8, 18};
641 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
642 EXPECT_EQ(stretched_iat, expected_result);
643
644 // Test a 120ms to 20ms change.
645 iat = {19, 12, 0, 0, 0, 0, 0, 0};
646 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
647 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
648 EXPECT_EQ(stretched_iat, expected_result);
649
650 // Test a 70ms to 40ms change.
651 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
652 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
653 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
654 EXPECT_EQ(stretched_iat, expected_result);
655
656 // Test a 30ms to 20ms change.
657 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
658 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
659 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
660 EXPECT_EQ(stretched_iat, expected_result);
661}
662
663// Test if the histogram is compressed correctly if the packet size is
664// increased.
665TEST(DelayManagerIATScalingTest, CompressionTest) {
666 using IATVector = DelayManager::IATVector;
667 // Test a 20 to 60 ms change.
668 IATVector iat = {12, 11, 10, 3, 2, 1};
669 IATVector expected_result = {33, 6, 0, 0, 0, 0};
670 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
671 EXPECT_EQ(compressed_iat, expected_result);
672
673 // Test a 60ms to 120ms change.
674 iat = {18, 16, 14, 4, 1};
675 expected_result = {34, 18, 1, 0, 0};
676 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
677 EXPECT_EQ(compressed_iat, expected_result);
678
679 // Test a 20ms to 120ms change.
680 iat = {18, 12, 5, 4, 4, 3, 5, 1};
681 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
682 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
683 EXPECT_EQ(compressed_iat, expected_result);
684
685 // Test a 70ms to 80ms change.
686 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
687 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
688 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
689 EXPECT_EQ(compressed_iat, expected_result);
690
691 // Test a 50ms to 110ms change.
692 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
693 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
694 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
695 EXPECT_EQ(compressed_iat, expected_result);
696}
697
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100698// Test if the histogram scaling function handles overflows correctly.
699TEST(DelayManagerIATScalingTest, OverflowTest) {
700 using IATVector = DelayManager::IATVector;
701 // Test a compression operation that can cause overflow.
702 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
703 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
704 0, 0, 0, 0, 0, 0, 0};
705 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
706 EXPECT_EQ(scaled_iat, expected_result);
707
708 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
709 114119, 2072996, 0, 2149354, 0};
710 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
711 0, 0, 0, 0, 0};
712 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
713 EXPECT_EQ(scaled_iat, expected_result);
714
715 // In this test case we will not be able to add everything to the final bin in
716 // the scaled histogram. Check that the last bin doesn't overflow.
717 iat = {2000000000, 2000000000, 2000000000,
718 2000000000, 2000000000, 2000000000};
719 expected_result = {666666666, 666666666, 666666666,
720 666666667, 666666667, 2147483647};
721 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
722 EXPECT_EQ(scaled_iat, expected_result);
723
724 // In this test case we will not be able to add enough to each of the bins,
725 // so the values should be smeared out past the end of the normal range.
726 iat = {2000000000, 2000000000, 2000000000,
727 2000000000, 2000000000, 2000000000};
728 expected_result = {2147483647, 2147483647, 2147483647,
729 2147483647, 2147483647, 1262581765};
730 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
731 EXPECT_EQ(scaled_iat, expected_result);
732}
Minyue Li002fbb82018-10-04 11:31:03 +0200733
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000734} // namespace webrtc