blob: b0e23a979cef45c21e7a94b5af7e2e1119c34ace [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"
Minyue Li002fbb82018-10-04 11:31:03 +020018#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "test/gmock.h"
20#include "test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021
22namespace webrtc {
23
24using ::testing::Return;
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000025using ::testing::_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026
27class DelayManagerTest : public ::testing::Test {
28 protected:
29 static const int kMaxNumberOfPackets = 240;
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010030 static const int kMinDelayMs = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031 static const int kTimeStepMs = 10;
32 static const int kFs = 8000;
33 static const int kFrameSizeMs = 20;
34 static const int kTsIncrement = kFrameSizeMs * kFs / 1000;
35
36 DelayManagerTest();
37 virtual void SetUp();
38 virtual void TearDown();
Minyue Li002fbb82018-10-04 11:31:03 +020039 void RecreateDelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000040 void SetPacketAudioLength(int lengt_ms);
41 void InsertNextPacket();
42 void IncreaseTime(int inc_ms);
43
Minyue Li002fbb82018-10-04 11:31:03 +020044 std::unique_ptr<DelayManager> dm_;
henrik.lundinf3933702016-04-28 01:53:52 -070045 TickTimer tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046 MockDelayPeakDetector detector_;
47 uint16_t seq_no_;
48 uint32_t ts_;
49};
50
51DelayManagerTest::DelayManagerTest()
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010052 : dm_(nullptr),
53 detector_(&tick_timer_, false),
54 seq_no_(0x1234),
55 ts_(0x12345678) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
57void DelayManagerTest::SetUp() {
Minyue Li002fbb82018-10-04 11:31:03 +020058 RecreateDelayManager();
59}
60
61void DelayManagerTest::RecreateDelayManager() {
Yves Gerey665174f2018-06-19 15:03:05 +020062 EXPECT_CALL(detector_, Reset()).Times(1);
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010063 dm_.reset(new DelayManager(kMaxNumberOfPackets, kMinDelayMs, &detector_,
64 &tick_timer_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065}
66
67void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
68 EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms));
69 dm_->SetPacketAudioLength(lengt_ms);
70}
71
72void DelayManagerTest::InsertNextPacket() {
73 EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs));
74 seq_no_ += 1;
75 ts_ += kTsIncrement;
76}
77
78void DelayManagerTest::IncreaseTime(int inc_ms) {
79 for (int t = 0; t < inc_ms; t += kTimeStepMs) {
henrik.lundinf3933702016-04-28 01:53:52 -070080 tick_timer_.Increment();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081 }
82}
83void DelayManagerTest::TearDown() {
84 EXPECT_CALL(detector_, Die());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085}
86
87TEST_F(DelayManagerTest, CreateAndDestroy) {
88 // Nothing to do here. The test fixture creates and destroys the DelayManager
89 // object.
90}
91
92TEST_F(DelayManagerTest, VectorInitialization) {
93 const DelayManager::IATVector& vec = dm_->iat_vector();
94 double sum = 0.0;
95 for (size_t i = 0; i < vec.size(); i++) {
Henrik Lundincb3e8fe2015-05-11 15:15:51 +020096 EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
97 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098 sum += vec[i];
99 }
100 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
101}
102
103TEST_F(DelayManagerTest, SetPacketAudioLength) {
104 const int kLengthMs = 30;
105 // Expect DelayManager to pass on the new length to the detector object.
Yves Gerey665174f2018-06-19 15:03:05 +0200106 EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs)).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs));
108 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value.
109}
110
111TEST_F(DelayManagerTest, PeakFound) {
112 // Expect DelayManager to pass on the question to the detector.
113 // Call twice, and let the detector return true the first time and false the
114 // second time.
115 EXPECT_CALL(detector_, peak_found())
116 .WillOnce(Return(true))
117 .WillOnce(Return(false));
118 EXPECT_TRUE(dm_->PeakFound());
119 EXPECT_FALSE(dm_->PeakFound());
120}
121
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000122TEST_F(DelayManagerTest, UpdateNormal) {
123 SetPacketAudioLength(kFrameSizeMs);
124 // First packet arrival.
125 InsertNextPacket();
126 // Advance time by one frame size.
127 IncreaseTime(kFrameSizeMs);
128 // Second packet arrival.
129 // Expect detector update method to be called once with inter-arrival time
130 // equal to 1 packet, and (base) target level equal to 1 as well.
131 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100132 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000133 InsertNextPacket();
134 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
135 EXPECT_EQ(1, dm_->base_target_level());
136 int lower, higher;
137 dm_->BufferLimits(&lower, &higher);
138 // Expect |lower| to be 75% of target level, and |higher| to be target level,
139 // but also at least 20 ms higher than |lower|, which is the limiting case
140 // here.
141 EXPECT_EQ((1 << 8) * 3 / 4, lower);
142 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
143}
144
145TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) {
146 SetPacketAudioLength(kFrameSizeMs);
147 // First packet arrival.
148 InsertNextPacket();
149 // Advance time by two frame size.
150 IncreaseTime(2 * kFrameSizeMs);
151 // Second packet arrival.
152 // Expect detector update method to be called once with inter-arrival time
153 // equal to 1 packet, and (base) target level equal to 1 as well.
154 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100155 EXPECT_CALL(detector_, Update(2, false, 2)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 InsertNextPacket();
157 EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8.
158 EXPECT_EQ(2, dm_->base_target_level());
159 int lower, higher;
160 dm_->BufferLimits(&lower, &higher);
161 // Expect |lower| to be 75% of target level, and |higher| to be target level,
162 // but also at least 20 ms higher than |lower|, which is the limiting case
163 // here.
164 EXPECT_EQ((2 << 8) * 3 / 4, lower);
165 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
166}
167
168TEST_F(DelayManagerTest, UpdatePeakFound) {
169 SetPacketAudioLength(kFrameSizeMs);
170 // First packet arrival.
171 InsertNextPacket();
172 // Advance time by one frame size.
173 IncreaseTime(kFrameSizeMs);
174 // Second packet arrival.
175 // Expect detector update method to be called once with inter-arrival time
176 // equal to 1 packet, and (base) target level equal to 1 as well.
177 // Return true to indicate that peaks are found. Let the peak height be 5.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100178 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(true));
Yves Gerey665174f2018-06-19 15:03:05 +0200179 EXPECT_CALL(detector_, MaxPeakHeight()).WillOnce(Return(5));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000180 InsertNextPacket();
181 EXPECT_EQ(5 << 8, dm_->TargetLevel());
182 EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks.
183 int lower, higher;
184 dm_->BufferLimits(&lower, &higher);
185 // Expect |lower| to be 75% of target level, and |higher| to be target level.
186 EXPECT_EQ((5 << 8) * 3 / 4, lower);
187 EXPECT_EQ(5 << 8, higher);
188}
189
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000190TEST_F(DelayManagerTest, TargetDelay) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191 SetPacketAudioLength(kFrameSizeMs);
192 // First packet arrival.
193 InsertNextPacket();
194 // Advance time by one frame size.
195 IncreaseTime(kFrameSizeMs);
196 // Second packet arrival.
197 // Expect detector update method to be called once with inter-arrival time
198 // equal to 1 packet, and (base) target level equal to 1 as well.
199 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100200 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000202 const int kExpectedTarget = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
204 EXPECT_EQ(1, dm_->base_target_level());
205 int lower, higher;
206 dm_->BufferLimits(&lower, &higher);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000207 // Expect |lower| to be 75% of base target level, and |higher| to be
208 // lower + 20 ms headroom.
209 EXPECT_EQ((1 << 8) * 3 / 4, lower);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000210 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
211}
212
Niels Möller18f1adc2018-08-23 08:40:41 +0200213TEST_F(DelayManagerTest, MaxDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000214 const int kExpectedTarget = 5;
215 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
216 SetPacketAudioLength(kFrameSizeMs);
217 // First packet arrival.
218 InsertNextPacket();
219 // Second packet arrival.
220 // Expect detector update method to be called once with inter-arrival time
221 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100222 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000223 .WillRepeatedly(Return(true));
224 EXPECT_CALL(detector_, MaxPeakHeight())
225 .WillRepeatedly(Return(kExpectedTarget));
226 IncreaseTime(kTimeIncrement);
227 InsertNextPacket();
228
229 // No limit is set.
230 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
231
232 int kMaxDelayPackets = kExpectedTarget - 2;
233 int kMaxDelayMs = kMaxDelayPackets * kFrameSizeMs;
234 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
235 IncreaseTime(kTimeIncrement);
236 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000237 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
238
239 // Target level at least should be one packet.
240 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
241}
242
Niels Möller18f1adc2018-08-23 08:40:41 +0200243TEST_F(DelayManagerTest, MinDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000244 const int kExpectedTarget = 5;
245 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
246 SetPacketAudioLength(kFrameSizeMs);
247 // First packet arrival.
248 InsertNextPacket();
249 // Second packet arrival.
250 // Expect detector update method to be called once with inter-arrival time
251 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100252 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000253 .WillRepeatedly(Return(true));
254 EXPECT_CALL(detector_, MaxPeakHeight())
255 .WillRepeatedly(Return(kExpectedTarget));
256 IncreaseTime(kTimeIncrement);
257 InsertNextPacket();
258
259 // No limit is applied.
260 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
261
262 int kMinDelayPackets = kExpectedTarget + 2;
263 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
264 dm_->SetMinimumDelay(kMinDelayMs);
265 IncreaseTime(kTimeIncrement);
266 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000267 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
268}
269
Ruslan Burakovedbea462019-02-04 16:17:31 +0100270TEST_F(DelayManagerTest, BaseMinDelay) {
271 const int kExpectedTarget = 5;
272 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
273 SetPacketAudioLength(kFrameSizeMs);
274 // First packet arrival.
275 InsertNextPacket();
276 // Second packet arrival.
277 // Expect detector update method to be called once with inter-arrival time
278 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
279 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
280 .WillRepeatedly(Return(true));
281 EXPECT_CALL(detector_, MaxPeakHeight())
282 .WillRepeatedly(Return(kExpectedTarget));
283 IncreaseTime(kTimeIncrement);
284 InsertNextPacket();
285
286 // No limit is applied.
287 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
288
289 int kBaseMinDelayPackets = kExpectedTarget + 2;
290 int kBaseMinDelayMs = kBaseMinDelayPackets * kFrameSizeMs;
291 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinDelayMs));
292 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinDelayMs);
293
294 IncreaseTime(kTimeIncrement);
295 InsertNextPacket();
296 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinDelayMs);
297 EXPECT_EQ(kBaseMinDelayPackets << 8, dm_->TargetLevel());
298}
299
300TEST_F(DelayManagerTest, BaseMinDelayGreaterThanMaxDelayIsInvalid) {
301 int kMaxDelayMs = 2 * kFrameSizeMs;
302 int kBaseMinDelayMs = 4 * kFrameSizeMs;
303 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
304 EXPECT_FALSE(dm_->SetBaseMinimumDelay(kBaseMinDelayMs));
305}
306
307TEST_F(DelayManagerTest, BaseMinDelayGreaterThanQ75MaxPacketsIsInvalid) {
308 // .75 of |max_packets_in_buffer|, + 1 to ensure that |kBaseMinDelayMs| is
309 // greater.
310 int kBaseMinDelayMs = (3 * kMaxNumberOfPackets * kFrameSizeMs / 4) + 1;
311 EXPECT_FALSE(dm_->SetBaseMinimumDelay(kBaseMinDelayMs));
312}
313
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100314TEST_F(DelayManagerTest, UpdateReorderedPacket) {
315 SetPacketAudioLength(kFrameSizeMs);
316 InsertNextPacket();
317
318 // Insert packet that was sent before the previous packet.
319 EXPECT_CALL(detector_, Update(_, true, _));
320 EXPECT_EQ(0, dm_->Update(seq_no_ - 1, ts_ - kFrameSizeMs, kFs));
321}
322
henrik.lundinb8c55b12017-05-10 07:38:01 -0700323// Tests that skipped sequence numbers (simulating empty packets) are handled
324// correctly.
325TEST_F(DelayManagerTest, EmptyPacketsReported) {
326 SetPacketAudioLength(kFrameSizeMs);
327 // First packet arrival.
328 InsertNextPacket();
329
330 // Advance time by one frame size.
331 IncreaseTime(kFrameSizeMs);
332
333 // Advance the sequence number by 5, simulating that 5 empty packets were
334 // received, but never inserted.
335 seq_no_ += 10;
336 for (int j = 0; j < 10; ++j) {
337 dm_->RegisterEmptyPacket();
338 }
339
340 // Second packet arrival.
341 // Expect detector update method to be called once with inter-arrival time
342 // equal to 1 packet, and (base) target level equal to 1 as well.
343 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100344 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700345 InsertNextPacket();
346
347 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
348}
349
350// Same as above, but do not call RegisterEmptyPacket. Observe the target level
351// increase dramatically.
352TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
353 SetPacketAudioLength(kFrameSizeMs);
354 // First packet arrival.
355 InsertNextPacket();
356
357 // Advance time by one frame size.
358 IncreaseTime(kFrameSizeMs);
359
360 // Advance the sequence number by 5, simulating that 5 empty packets were
361 // received, but never inserted.
362 seq_no_ += 10;
363
364 // Second packet arrival.
365 // Expect detector update method to be called once with inter-arrival time
366 // equal to 1 packet, and (base) target level equal to 1 as well.
367 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100368 EXPECT_CALL(detector_, Update(10, false, 10)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700369 InsertNextPacket();
370
371 // Note 10 times higher target value.
372 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
373}
374
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000375TEST_F(DelayManagerTest, Failures) {
376 // Wrong sample rate.
377 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
378 // Wrong packet size.
379 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
380 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000381
382 // Minimum delay higher than a maximum delay is not accepted.
383 EXPECT_TRUE(dm_->SetMaximumDelay(10));
384 EXPECT_FALSE(dm_->SetMinimumDelay(20));
385
386 // Maximum delay less than minimum delay is not accepted.
387 EXPECT_TRUE(dm_->SetMaximumDelay(100));
388 EXPECT_TRUE(dm_->SetMinimumDelay(80));
389 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000390}
391
Minyue Li002fbb82018-10-04 11:31:03 +0200392TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
393 test::ScopedFieldTrials field_trial(
394 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
395 RecreateDelayManager();
396 EXPECT_EQ(absl::make_optional<int>(1 << 30),
397 dm_->forced_limit_probability_for_test());
398
399 SetPacketAudioLength(kFrameSizeMs);
400 // First packet arrival.
401 InsertNextPacket();
402 // Advance time by one frame size.
403 IncreaseTime(kFrameSizeMs);
404 // Second packet arrival.
405 // Expect detector update method to be called once with inter-arrival time
406 // equal to 1 packet.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100407 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
Minyue Li002fbb82018-10-04 11:31:03 +0200408 InsertNextPacket();
409 constexpr int kExpectedTarget = 1;
410 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
411}
412
413TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
414 {
415 test::ScopedFieldTrials field_trial(
416 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
417 RecreateDelayManager();
418 EXPECT_EQ(absl::make_optional<int>(53687091),
419 dm_->forced_limit_probability_for_test()); // 1/20 in Q30
420 }
421 {
422 test::ScopedFieldTrials field_trial(
423 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
424 RecreateDelayManager();
425 EXPECT_EQ(absl::make_optional<int>(536871),
426 dm_->forced_limit_probability_for_test()); // 1/2000 in Q30
427 }
428 {
429 test::ScopedFieldTrials field_trial(
430 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
431 RecreateDelayManager();
432 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
433 }
434 {
435 test::ScopedFieldTrials field_trial(
436 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
437 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
438 }
439 {
440 test::ScopedFieldTrials field_trial(
441 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
442 RecreateDelayManager();
443 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
444 }
445}
446
Ivo Creusen385b10b2017-10-13 12:37:27 +0200447// Test if the histogram is stretched correctly if the packet size is decreased.
448TEST(DelayManagerIATScalingTest, StretchTest) {
449 using IATVector = DelayManager::IATVector;
450 // Test a straightforward 60ms to 20ms change.
451 IATVector iat = {12, 0, 0, 0, 0, 0};
452 IATVector expected_result = {4, 4, 4, 0, 0, 0};
453 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
454 EXPECT_EQ(stretched_iat, expected_result);
455
456 // Test an example where the last bin in the stretched histogram should
457 // contain the sum of the elements that don't fit into the new histogram.
458 iat = {18, 15, 12, 9, 6, 3, 0};
459 expected_result = {6, 6, 6, 5, 5, 5, 30};
460 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
461 EXPECT_EQ(stretched_iat, expected_result);
462
463 // Test a 120ms to 60ms change.
464 iat = {18, 16, 14, 4, 0};
465 expected_result = {9, 9, 8, 8, 18};
466 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
467 EXPECT_EQ(stretched_iat, expected_result);
468
469 // Test a 120ms to 20ms change.
470 iat = {19, 12, 0, 0, 0, 0, 0, 0};
471 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
472 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
473 EXPECT_EQ(stretched_iat, expected_result);
474
475 // Test a 70ms to 40ms change.
476 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
477 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
478 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
479 EXPECT_EQ(stretched_iat, expected_result);
480
481 // Test a 30ms to 20ms change.
482 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
483 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
484 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
485 EXPECT_EQ(stretched_iat, expected_result);
486}
487
488// Test if the histogram is compressed correctly if the packet size is
489// increased.
490TEST(DelayManagerIATScalingTest, CompressionTest) {
491 using IATVector = DelayManager::IATVector;
492 // Test a 20 to 60 ms change.
493 IATVector iat = {12, 11, 10, 3, 2, 1};
494 IATVector expected_result = {33, 6, 0, 0, 0, 0};
495 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
496 EXPECT_EQ(compressed_iat, expected_result);
497
498 // Test a 60ms to 120ms change.
499 iat = {18, 16, 14, 4, 1};
500 expected_result = {34, 18, 1, 0, 0};
501 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
502 EXPECT_EQ(compressed_iat, expected_result);
503
504 // Test a 20ms to 120ms change.
505 iat = {18, 12, 5, 4, 4, 3, 5, 1};
506 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
507 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
508 EXPECT_EQ(compressed_iat, expected_result);
509
510 // Test a 70ms to 80ms change.
511 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
512 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
513 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
514 EXPECT_EQ(compressed_iat, expected_result);
515
516 // Test a 50ms to 110ms change.
517 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
518 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
519 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
520 EXPECT_EQ(compressed_iat, expected_result);
521}
522
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100523// Test if the histogram scaling function handles overflows correctly.
524TEST(DelayManagerIATScalingTest, OverflowTest) {
525 using IATVector = DelayManager::IATVector;
526 // Test a compression operation that can cause overflow.
527 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
528 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
529 0, 0, 0, 0, 0, 0, 0};
530 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
531 EXPECT_EQ(scaled_iat, expected_result);
532
533 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
534 114119, 2072996, 0, 2149354, 0};
535 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
536 0, 0, 0, 0, 0};
537 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
538 EXPECT_EQ(scaled_iat, expected_result);
539
540 // In this test case we will not be able to add everything to the final bin in
541 // the scaled histogram. Check that the last bin doesn't overflow.
542 iat = {2000000000, 2000000000, 2000000000,
543 2000000000, 2000000000, 2000000000};
544 expected_result = {666666666, 666666666, 666666666,
545 666666667, 666666667, 2147483647};
546 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
547 EXPECT_EQ(scaled_iat, expected_result);
548
549 // In this test case we will not be able to add enough to each of the bins,
550 // so the values should be smeared out past the end of the normal range.
551 iat = {2000000000, 2000000000, 2000000000,
552 2000000000, 2000000000, 2000000000};
553 expected_result = {2147483647, 2147483647, 2147483647,
554 2147483647, 2147483647, 1262581765};
555 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
556 EXPECT_EQ(scaled_iat, expected_result);
557}
Minyue Li002fbb82018-10-04 11:31:03 +0200558
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000559} // namespace webrtc