blob: 19b99af77f70fd84f30158dc8c67f541b6dfcbe4 [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;
Ruslan Burakovb35bacc2019-02-20 13:41:59 +0100317 constexpr int kMaximumDelayMs = 20;
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100318 constexpr int kMaxBufferSizeMsQ75 = 3 * kMaxBufferSizeMs / 4;
319
Ruslan Burakovb35bacc2019-02-20 13:41:59 +0100320 EXPECT_TRUE(dm_->SetMaximumDelay(kMaximumDelayMs));
321
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100322 // Base minimum delay is greater than minimum delay, that is why we clamp
323 // it to current the highest possible value which is maximum delay.
324 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
325 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMaxBufferSizeMs);
Ruslan Burakovb35bacc2019-02-20 13:41:59 +0100326 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMaximumDelayMs);
327 RTC_DCHECK_LT(kMaximumDelayMs, kMaxBufferSizeMsQ75);
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100328
329 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
330 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
331
332 // Unset maximum value.
333 EXPECT_TRUE(dm_->SetMaximumDelay(0));
334
335 // With maximum value unset, the highest possible value now is 75% of
336 // currently possible maximum buffer size.
337 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMaxBufferSizeMsQ75);
338}
339
340TEST_F(DelayManagerTest, BaseMinimumDelayGreaterThanMaximumDelay) {
341 SetPacketAudioLength(kFrameSizeMs);
342 constexpr int kMaximumDelayMs = 400;
343 constexpr int kBaseMinimumDelayMs = kMaximumDelayMs + 1;
344 constexpr int kMinimumDelayMs = 20;
345
346 // Base minimum delay is greater than minimum delay, that is why we clamp
347 // it to current the highest possible value which is kMaximumDelayMs.
348 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
349 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMaximumDelayMs);
350 RTC_DCHECK_LT(kMaximumDelayMs, kMaxBufferSizeMs);
351
352 EXPECT_TRUE(dm_->SetMaximumDelay(kMaximumDelayMs));
353 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
354 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
355 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMaximumDelayMs);
356}
357
358TEST_F(DelayManagerTest, BaseMinimumDelayLowerThanMaxSize) {
359 SetPacketAudioLength(kFrameSizeMs);
360 constexpr int kMaximumDelayMs = 400;
361 constexpr int kBaseMinimumDelayMs = kMaximumDelayMs - 1;
362 constexpr int kMinimumDelayMs = 20;
363
364 // Base minimum delay is greater than minimum delay, and lower than maximum
365 // delays that is why it is used.
366 RTC_DCHECK_GT(kBaseMinimumDelayMs, kMinimumDelayMs);
367 RTC_DCHECK_LT(kBaseMinimumDelayMs, kMaximumDelayMs);
368
369 EXPECT_TRUE(dm_->SetMaximumDelay(kMaximumDelayMs));
370 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
371 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
372 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kBaseMinimumDelayMs);
373}
374
375TEST_F(DelayManagerTest, MinimumDelayMemorization) {
376 // Check that when we increase base minimum delay to value higher than
377 // minimum delay then minimum delay is still memorized. This allows to
378 // restore effective minimum delay to memorized minimum delay value when we
379 // decrease base minimum delay.
380 SetPacketAudioLength(kFrameSizeMs);
381
382 constexpr int kBaseMinimumDelayMsLow = 10;
383 constexpr int kMinimumDelayMs = 20;
384 constexpr int kBaseMinimumDelayMsHigh = 30;
385
386 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMsLow));
387 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
388 // Minimum delay is used as it is higher than base minimum delay.
389 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMinimumDelayMs);
390
391 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMsHigh));
392 // Base minimum delay is used as it is now higher than minimum delay.
393 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(),
394 kBaseMinimumDelayMsHigh);
395
396 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMsLow));
397 // Check that minimum delay is memorized and is used again.
398 EXPECT_EQ(dm_->effective_minimum_delay_ms_for_test(), kMinimumDelayMs);
399}
400
401TEST_F(DelayManagerTest, BaseMinimumDelay) {
Ruslan Burakovedbea462019-02-04 16:17:31 +0100402 const int kExpectedTarget = 5;
403 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
404 SetPacketAudioLength(kFrameSizeMs);
405 // First packet arrival.
406 InsertNextPacket();
407 // Second packet arrival.
408 // Expect detector update method to be called once with inter-arrival time
409 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
410 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
411 .WillRepeatedly(Return(true));
412 EXPECT_CALL(detector_, MaxPeakHeight())
413 .WillRepeatedly(Return(kExpectedTarget));
414 IncreaseTime(kTimeIncrement);
415 InsertNextPacket();
416
417 // No limit is applied.
418 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
419
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100420 constexpr int kBaseMinimumDelayPackets = kExpectedTarget + 2;
421 constexpr int kBaseMinimumDelayMs = kBaseMinimumDelayPackets * kFrameSizeMs;
422 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
423 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
Ruslan Burakovedbea462019-02-04 16:17:31 +0100424
425 IncreaseTime(kTimeIncrement);
426 InsertNextPacket();
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100427 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
428 EXPECT_EQ(kBaseMinimumDelayPackets << 8, dm_->TargetLevel());
Ruslan Burakovedbea462019-02-04 16:17:31 +0100429}
430
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100431TEST_F(DelayManagerTest, BaseMinimumDealyAffectTargetLevel) {
432 const int kExpectedTarget = 5;
433 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
434 SetPacketAudioLength(kFrameSizeMs);
435 // First packet arrival.
436 InsertNextPacket();
437 // Second packet arrival.
438 // Expect detector update method to be called once with inter-arrival time
439 // equal to |kExpectedTarget|. Return true to indicate peaks found.
440 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
441 .WillRepeatedly(Return(true));
442 EXPECT_CALL(detector_, MaxPeakHeight())
443 .WillRepeatedly(Return(kExpectedTarget));
444 IncreaseTime(kTimeIncrement);
445 InsertNextPacket();
Ruslan Burakovedbea462019-02-04 16:17:31 +0100446
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100447 // No limit is applied.
448 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
449
450 // Minimum delay is lower than base minimum delay, that is why base minimum
451 // delay is used to calculate target level.
452 constexpr int kMinimumDelayPackets = kExpectedTarget + 1;
453 constexpr int kBaseMinimumDelayPackets = kExpectedTarget + 2;
454
455 constexpr int kMinimumDelayMs = kMinimumDelayPackets * kFrameSizeMs;
456 constexpr int kBaseMinimumDelayMs = kBaseMinimumDelayPackets * kFrameSizeMs;
457
458 EXPECT_TRUE(kMinimumDelayMs < kBaseMinimumDelayMs);
459 EXPECT_TRUE(dm_->SetMinimumDelay(kMinimumDelayMs));
460 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinimumDelayMs));
461 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
462
463 IncreaseTime(kTimeIncrement);
464 InsertNextPacket();
465 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinimumDelayMs);
466 EXPECT_EQ(kBaseMinimumDelayPackets << 8, dm_->TargetLevel());
Ruslan Burakovedbea462019-02-04 16:17:31 +0100467}
468
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100469TEST_F(DelayManagerTest, UpdateReorderedPacket) {
470 SetPacketAudioLength(kFrameSizeMs);
471 InsertNextPacket();
472
473 // Insert packet that was sent before the previous packet.
474 EXPECT_CALL(detector_, Update(_, true, _));
475 EXPECT_EQ(0, dm_->Update(seq_no_ - 1, ts_ - kFrameSizeMs, kFs));
476}
477
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100478TEST_F(DelayManagerTest, EnableRtxHandling) {
479 enable_rtx_handling_ = true;
480 RecreateDelayManager();
481
482 // Insert first packet.
483 SetPacketAudioLength(kFrameSizeMs);
484 InsertNextPacket();
485
486 // Insert reordered packet.
487 // TODO(jakobi): Test estimated inter-arrival time by mocking the histogram
488 // instead of checking the call to the peak detector.
489 EXPECT_CALL(detector_, Update(3, true, _));
490 EXPECT_EQ(0, dm_->Update(seq_no_ - 3, ts_ - 3 * kFrameSizeMs, kFs));
491
492 // Insert another reordered packet.
493 EXPECT_CALL(detector_, Update(2, true, _));
494 EXPECT_EQ(0, dm_->Update(seq_no_ - 2, ts_ - 2 * kFrameSizeMs, kFs));
495
496 // Insert the next packet in order and verify that the inter-arrival time is
497 // estimated correctly.
498 IncreaseTime(kFrameSizeMs);
499 EXPECT_CALL(detector_, Update(1, false, _));
500 InsertNextPacket();
501}
502
henrik.lundinb8c55b12017-05-10 07:38:01 -0700503// Tests that skipped sequence numbers (simulating empty packets) are handled
504// correctly.
505TEST_F(DelayManagerTest, EmptyPacketsReported) {
506 SetPacketAudioLength(kFrameSizeMs);
507 // First packet arrival.
508 InsertNextPacket();
509
510 // Advance time by one frame size.
511 IncreaseTime(kFrameSizeMs);
512
513 // Advance the sequence number by 5, simulating that 5 empty packets were
514 // received, but never inserted.
515 seq_no_ += 10;
516 for (int j = 0; j < 10; ++j) {
517 dm_->RegisterEmptyPacket();
518 }
519
520 // Second packet arrival.
521 // Expect detector update method to be called once with inter-arrival time
522 // equal to 1 packet, and (base) target level equal to 1 as well.
523 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100524 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700525 InsertNextPacket();
526
527 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
528}
529
530// Same as above, but do not call RegisterEmptyPacket. Observe the target level
531// increase dramatically.
532TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
533 SetPacketAudioLength(kFrameSizeMs);
534 // First packet arrival.
535 InsertNextPacket();
536
537 // Advance time by one frame size.
538 IncreaseTime(kFrameSizeMs);
539
540 // Advance the sequence number by 5, simulating that 5 empty packets were
541 // received, but never inserted.
542 seq_no_ += 10;
543
544 // Second packet arrival.
545 // Expect detector update method to be called once with inter-arrival time
546 // equal to 1 packet, and (base) target level equal to 1 as well.
547 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100548 EXPECT_CALL(detector_, Update(10, false, 10)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700549 InsertNextPacket();
550
551 // Note 10 times higher target value.
552 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
553}
554
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000555TEST_F(DelayManagerTest, Failures) {
556 // Wrong sample rate.
557 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
558 // Wrong packet size.
559 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
560 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000561
562 // Minimum delay higher than a maximum delay is not accepted.
563 EXPECT_TRUE(dm_->SetMaximumDelay(10));
564 EXPECT_FALSE(dm_->SetMinimumDelay(20));
565
566 // Maximum delay less than minimum delay is not accepted.
567 EXPECT_TRUE(dm_->SetMaximumDelay(100));
568 EXPECT_TRUE(dm_->SetMinimumDelay(80));
569 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000570}
571
Minyue Li002fbb82018-10-04 11:31:03 +0200572TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
573 test::ScopedFieldTrials field_trial(
574 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
575 RecreateDelayManager();
576 EXPECT_EQ(absl::make_optional<int>(1 << 30),
577 dm_->forced_limit_probability_for_test());
578
579 SetPacketAudioLength(kFrameSizeMs);
580 // First packet arrival.
581 InsertNextPacket();
582 // Advance time by one frame size.
583 IncreaseTime(kFrameSizeMs);
584 // Second packet arrival.
585 // Expect detector update method to be called once with inter-arrival time
586 // equal to 1 packet.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100587 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
Minyue Li002fbb82018-10-04 11:31:03 +0200588 InsertNextPacket();
589 constexpr int kExpectedTarget = 1;
590 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
591}
592
593TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
594 {
595 test::ScopedFieldTrials field_trial(
596 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
597 RecreateDelayManager();
598 EXPECT_EQ(absl::make_optional<int>(53687091),
599 dm_->forced_limit_probability_for_test()); // 1/20 in Q30
600 }
601 {
602 test::ScopedFieldTrials field_trial(
603 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
604 RecreateDelayManager();
605 EXPECT_EQ(absl::make_optional<int>(536871),
606 dm_->forced_limit_probability_for_test()); // 1/2000 in Q30
607 }
608 {
609 test::ScopedFieldTrials field_trial(
610 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
611 RecreateDelayManager();
612 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
613 }
614 {
615 test::ScopedFieldTrials field_trial(
616 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
617 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
618 }
619 {
620 test::ScopedFieldTrials field_trial(
621 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
622 RecreateDelayManager();
623 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
624 }
625}
626
Ivo Creusen385b10b2017-10-13 12:37:27 +0200627// Test if the histogram is stretched correctly if the packet size is decreased.
628TEST(DelayManagerIATScalingTest, StretchTest) {
629 using IATVector = DelayManager::IATVector;
630 // Test a straightforward 60ms to 20ms change.
631 IATVector iat = {12, 0, 0, 0, 0, 0};
632 IATVector expected_result = {4, 4, 4, 0, 0, 0};
633 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
634 EXPECT_EQ(stretched_iat, expected_result);
635
636 // Test an example where the last bin in the stretched histogram should
637 // contain the sum of the elements that don't fit into the new histogram.
638 iat = {18, 15, 12, 9, 6, 3, 0};
639 expected_result = {6, 6, 6, 5, 5, 5, 30};
640 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
641 EXPECT_EQ(stretched_iat, expected_result);
642
643 // Test a 120ms to 60ms change.
644 iat = {18, 16, 14, 4, 0};
645 expected_result = {9, 9, 8, 8, 18};
646 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
647 EXPECT_EQ(stretched_iat, expected_result);
648
649 // Test a 120ms to 20ms change.
650 iat = {19, 12, 0, 0, 0, 0, 0, 0};
651 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
652 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
653 EXPECT_EQ(stretched_iat, expected_result);
654
655 // Test a 70ms to 40ms change.
656 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
657 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
658 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
659 EXPECT_EQ(stretched_iat, expected_result);
660
661 // Test a 30ms to 20ms change.
662 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
663 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
664 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
665 EXPECT_EQ(stretched_iat, expected_result);
666}
667
668// Test if the histogram is compressed correctly if the packet size is
669// increased.
670TEST(DelayManagerIATScalingTest, CompressionTest) {
671 using IATVector = DelayManager::IATVector;
672 // Test a 20 to 60 ms change.
673 IATVector iat = {12, 11, 10, 3, 2, 1};
674 IATVector expected_result = {33, 6, 0, 0, 0, 0};
675 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
676 EXPECT_EQ(compressed_iat, expected_result);
677
678 // Test a 60ms to 120ms change.
679 iat = {18, 16, 14, 4, 1};
680 expected_result = {34, 18, 1, 0, 0};
681 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
682 EXPECT_EQ(compressed_iat, expected_result);
683
684 // Test a 20ms to 120ms change.
685 iat = {18, 12, 5, 4, 4, 3, 5, 1};
686 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
687 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
688 EXPECT_EQ(compressed_iat, expected_result);
689
690 // Test a 70ms to 80ms change.
691 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
692 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
693 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
694 EXPECT_EQ(compressed_iat, expected_result);
695
696 // Test a 50ms to 110ms change.
697 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
698 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
699 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
700 EXPECT_EQ(compressed_iat, expected_result);
701}
702
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100703// Test if the histogram scaling function handles overflows correctly.
704TEST(DelayManagerIATScalingTest, OverflowTest) {
705 using IATVector = DelayManager::IATVector;
706 // Test a compression operation that can cause overflow.
707 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
708 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
709 0, 0, 0, 0, 0, 0, 0};
710 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
711 EXPECT_EQ(scaled_iat, expected_result);
712
713 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
714 114119, 2072996, 0, 2149354, 0};
715 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
716 0, 0, 0, 0, 0};
717 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
718 EXPECT_EQ(scaled_iat, expected_result);
719
720 // In this test case we will not be able to add everything to the final bin in
721 // the scaled histogram. Check that the last bin doesn't overflow.
722 iat = {2000000000, 2000000000, 2000000000,
723 2000000000, 2000000000, 2000000000};
724 expected_result = {666666666, 666666666, 666666666,
725 666666667, 666666667, 2147483647};
726 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
727 EXPECT_EQ(scaled_iat, expected_result);
728
729 // In this test case we will not be able to add enough to each of the bins,
730 // so the values should be smeared out past the end of the normal range.
731 iat = {2000000000, 2000000000, 2000000000,
732 2000000000, 2000000000, 2000000000};
733 expected_result = {2147483647, 2147483647, 2147483647,
734 2147483647, 2147483647, 1262581765};
735 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
736 EXPECT_EQ(scaled_iat, expected_result);
737}
Minyue Li002fbb82018-10-04 11:31:03 +0200738
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000739} // namespace webrtc