blob: e33b2aa89419f22182255f54da8bb7290605ff99 [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_;
Jakob Ivarssone98954c2019-02-06 15:37:50 +010049 bool enable_rtx_handling_ = false;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050};
51
52DelayManagerTest::DelayManagerTest()
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010053 : dm_(nullptr),
54 detector_(&tick_timer_, false),
55 seq_no_(0x1234),
56 ts_(0x12345678) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000057
58void DelayManagerTest::SetUp() {
Minyue Li002fbb82018-10-04 11:31:03 +020059 RecreateDelayManager();
60}
61
62void DelayManagerTest::RecreateDelayManager() {
Yves Gerey665174f2018-06-19 15:03:05 +020063 EXPECT_CALL(detector_, Reset()).Times(1);
Jakob Ivarssone98954c2019-02-06 15:37:50 +010064 dm_.reset(new DelayManager(kMaxNumberOfPackets, kMinDelayMs,
65 enable_rtx_handling_, &detector_, &tick_timer_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066}
67
68void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
69 EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms));
70 dm_->SetPacketAudioLength(lengt_ms);
71}
72
73void DelayManagerTest::InsertNextPacket() {
74 EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs));
75 seq_no_ += 1;
76 ts_ += kTsIncrement;
77}
78
79void DelayManagerTest::IncreaseTime(int inc_ms) {
80 for (int t = 0; t < inc_ms; t += kTimeStepMs) {
henrik.lundinf3933702016-04-28 01:53:52 -070081 tick_timer_.Increment();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 }
83}
84void DelayManagerTest::TearDown() {
85 EXPECT_CALL(detector_, Die());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086}
87
88TEST_F(DelayManagerTest, CreateAndDestroy) {
89 // Nothing to do here. The test fixture creates and destroys the DelayManager
90 // object.
91}
92
93TEST_F(DelayManagerTest, VectorInitialization) {
94 const DelayManager::IATVector& vec = dm_->iat_vector();
95 double sum = 0.0;
96 for (size_t i = 0; i < vec.size(); i++) {
Henrik Lundincb3e8fe2015-05-11 15:15:51 +020097 EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
98 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099 sum += vec[i];
100 }
101 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
102}
103
104TEST_F(DelayManagerTest, SetPacketAudioLength) {
105 const int kLengthMs = 30;
106 // Expect DelayManager to pass on the new length to the detector object.
Yves Gerey665174f2018-06-19 15:03:05 +0200107 EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs)).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs));
109 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value.
110}
111
112TEST_F(DelayManagerTest, PeakFound) {
113 // Expect DelayManager to pass on the question to the detector.
114 // Call twice, and let the detector return true the first time and false the
115 // second time.
116 EXPECT_CALL(detector_, peak_found())
117 .WillOnce(Return(true))
118 .WillOnce(Return(false));
119 EXPECT_TRUE(dm_->PeakFound());
120 EXPECT_FALSE(dm_->PeakFound());
121}
122
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123TEST_F(DelayManagerTest, UpdateNormal) {
124 SetPacketAudioLength(kFrameSizeMs);
125 // First packet arrival.
126 InsertNextPacket();
127 // Advance time by one frame size.
128 IncreaseTime(kFrameSizeMs);
129 // Second packet arrival.
130 // Expect detector update method to be called once with inter-arrival time
131 // equal to 1 packet, and (base) target level equal to 1 as well.
132 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100133 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134 InsertNextPacket();
135 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
136 EXPECT_EQ(1, dm_->base_target_level());
137 int lower, higher;
138 dm_->BufferLimits(&lower, &higher);
139 // Expect |lower| to be 75% of target level, and |higher| to be target level,
140 // but also at least 20 ms higher than |lower|, which is the limiting case
141 // here.
142 EXPECT_EQ((1 << 8) * 3 / 4, lower);
143 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
144}
145
146TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) {
147 SetPacketAudioLength(kFrameSizeMs);
148 // First packet arrival.
149 InsertNextPacket();
150 // Advance time by two frame size.
151 IncreaseTime(2 * kFrameSizeMs);
152 // Second packet arrival.
153 // Expect detector update method to be called once with inter-arrival time
154 // equal to 1 packet, and (base) target level equal to 1 as well.
155 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100156 EXPECT_CALL(detector_, Update(2, false, 2)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000157 InsertNextPacket();
158 EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8.
159 EXPECT_EQ(2, dm_->base_target_level());
160 int lower, higher;
161 dm_->BufferLimits(&lower, &higher);
162 // Expect |lower| to be 75% of target level, and |higher| to be target level,
163 // but also at least 20 ms higher than |lower|, which is the limiting case
164 // here.
165 EXPECT_EQ((2 << 8) * 3 / 4, lower);
166 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
167}
168
169TEST_F(DelayManagerTest, UpdatePeakFound) {
170 SetPacketAudioLength(kFrameSizeMs);
171 // First packet arrival.
172 InsertNextPacket();
173 // Advance time by one frame size.
174 IncreaseTime(kFrameSizeMs);
175 // Second packet arrival.
176 // Expect detector update method to be called once with inter-arrival time
177 // equal to 1 packet, and (base) target level equal to 1 as well.
178 // Return true to indicate that peaks are found. Let the peak height be 5.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100179 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(true));
Yves Gerey665174f2018-06-19 15:03:05 +0200180 EXPECT_CALL(detector_, MaxPeakHeight()).WillOnce(Return(5));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 InsertNextPacket();
182 EXPECT_EQ(5 << 8, dm_->TargetLevel());
183 EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks.
184 int lower, higher;
185 dm_->BufferLimits(&lower, &higher);
186 // Expect |lower| to be 75% of target level, and |higher| to be target level.
187 EXPECT_EQ((5 << 8) * 3 / 4, lower);
188 EXPECT_EQ(5 << 8, higher);
189}
190
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000191TEST_F(DelayManagerTest, TargetDelay) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000192 SetPacketAudioLength(kFrameSizeMs);
193 // First packet arrival.
194 InsertNextPacket();
195 // Advance time by one frame size.
196 IncreaseTime(kFrameSizeMs);
197 // Second packet arrival.
198 // Expect detector update method to be called once with inter-arrival time
199 // equal to 1 packet, and (base) target level equal to 1 as well.
200 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100201 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000202 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000203 const int kExpectedTarget = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000204 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
205 EXPECT_EQ(1, dm_->base_target_level());
206 int lower, higher;
207 dm_->BufferLimits(&lower, &higher);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000208 // Expect |lower| to be 75% of base target level, and |higher| to be
209 // lower + 20 ms headroom.
210 EXPECT_EQ((1 << 8) * 3 / 4, lower);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000211 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
212}
213
Niels Möller18f1adc2018-08-23 08:40:41 +0200214TEST_F(DelayManagerTest, MaxDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000215 const int kExpectedTarget = 5;
216 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
217 SetPacketAudioLength(kFrameSizeMs);
218 // First packet arrival.
219 InsertNextPacket();
220 // Second packet arrival.
221 // Expect detector update method to be called once with inter-arrival time
222 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100223 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000224 .WillRepeatedly(Return(true));
225 EXPECT_CALL(detector_, MaxPeakHeight())
226 .WillRepeatedly(Return(kExpectedTarget));
227 IncreaseTime(kTimeIncrement);
228 InsertNextPacket();
229
230 // No limit is set.
231 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
232
233 int kMaxDelayPackets = kExpectedTarget - 2;
234 int kMaxDelayMs = kMaxDelayPackets * kFrameSizeMs;
235 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
236 IncreaseTime(kTimeIncrement);
237 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000238 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
239
240 // Target level at least should be one packet.
241 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
242}
243
Niels Möller18f1adc2018-08-23 08:40:41 +0200244TEST_F(DelayManagerTest, MinDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000245 const int kExpectedTarget = 5;
246 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
247 SetPacketAudioLength(kFrameSizeMs);
248 // First packet arrival.
249 InsertNextPacket();
250 // Second packet arrival.
251 // Expect detector update method to be called once with inter-arrival time
252 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100253 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000254 .WillRepeatedly(Return(true));
255 EXPECT_CALL(detector_, MaxPeakHeight())
256 .WillRepeatedly(Return(kExpectedTarget));
257 IncreaseTime(kTimeIncrement);
258 InsertNextPacket();
259
260 // No limit is applied.
261 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
262
263 int kMinDelayPackets = kExpectedTarget + 2;
264 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
265 dm_->SetMinimumDelay(kMinDelayMs);
266 IncreaseTime(kTimeIncrement);
267 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000268 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
269}
270
Ruslan Burakovedbea462019-02-04 16:17:31 +0100271TEST_F(DelayManagerTest, BaseMinDelay) {
272 const int kExpectedTarget = 5;
273 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
274 SetPacketAudioLength(kFrameSizeMs);
275 // First packet arrival.
276 InsertNextPacket();
277 // Second packet arrival.
278 // Expect detector update method to be called once with inter-arrival time
279 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
280 EXPECT_CALL(detector_, Update(kExpectedTarget, false, _))
281 .WillRepeatedly(Return(true));
282 EXPECT_CALL(detector_, MaxPeakHeight())
283 .WillRepeatedly(Return(kExpectedTarget));
284 IncreaseTime(kTimeIncrement);
285 InsertNextPacket();
286
287 // No limit is applied.
288 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
289
290 int kBaseMinDelayPackets = kExpectedTarget + 2;
291 int kBaseMinDelayMs = kBaseMinDelayPackets * kFrameSizeMs;
292 EXPECT_TRUE(dm_->SetBaseMinimumDelay(kBaseMinDelayMs));
293 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinDelayMs);
294
295 IncreaseTime(kTimeIncrement);
296 InsertNextPacket();
297 EXPECT_EQ(dm_->GetBaseMinimumDelay(), kBaseMinDelayMs);
298 EXPECT_EQ(kBaseMinDelayPackets << 8, dm_->TargetLevel());
299}
300
301TEST_F(DelayManagerTest, BaseMinDelayGreaterThanMaxDelayIsInvalid) {
302 int kMaxDelayMs = 2 * kFrameSizeMs;
303 int kBaseMinDelayMs = 4 * kFrameSizeMs;
304 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
305 EXPECT_FALSE(dm_->SetBaseMinimumDelay(kBaseMinDelayMs));
306}
307
308TEST_F(DelayManagerTest, BaseMinDelayGreaterThanQ75MaxPacketsIsInvalid) {
309 // .75 of |max_packets_in_buffer|, + 1 to ensure that |kBaseMinDelayMs| is
310 // greater.
311 int kBaseMinDelayMs = (3 * kMaxNumberOfPackets * kFrameSizeMs / 4) + 1;
312 EXPECT_FALSE(dm_->SetBaseMinimumDelay(kBaseMinDelayMs));
313}
314
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100315TEST_F(DelayManagerTest, UpdateReorderedPacket) {
316 SetPacketAudioLength(kFrameSizeMs);
317 InsertNextPacket();
318
319 // Insert packet that was sent before the previous packet.
320 EXPECT_CALL(detector_, Update(_, true, _));
321 EXPECT_EQ(0, dm_->Update(seq_no_ - 1, ts_ - kFrameSizeMs, kFs));
322}
323
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100324TEST_F(DelayManagerTest, EnableRtxHandling) {
325 enable_rtx_handling_ = true;
326 RecreateDelayManager();
327
328 // Insert first packet.
329 SetPacketAudioLength(kFrameSizeMs);
330 InsertNextPacket();
331
332 // Insert reordered packet.
333 // TODO(jakobi): Test estimated inter-arrival time by mocking the histogram
334 // instead of checking the call to the peak detector.
335 EXPECT_CALL(detector_, Update(3, true, _));
336 EXPECT_EQ(0, dm_->Update(seq_no_ - 3, ts_ - 3 * kFrameSizeMs, kFs));
337
338 // Insert another reordered packet.
339 EXPECT_CALL(detector_, Update(2, true, _));
340 EXPECT_EQ(0, dm_->Update(seq_no_ - 2, ts_ - 2 * kFrameSizeMs, kFs));
341
342 // Insert the next packet in order and verify that the inter-arrival time is
343 // estimated correctly.
344 IncreaseTime(kFrameSizeMs);
345 EXPECT_CALL(detector_, Update(1, false, _));
346 InsertNextPacket();
347}
348
henrik.lundinb8c55b12017-05-10 07:38:01 -0700349// Tests that skipped sequence numbers (simulating empty packets) are handled
350// correctly.
351TEST_F(DelayManagerTest, EmptyPacketsReported) {
352 SetPacketAudioLength(kFrameSizeMs);
353 // First packet arrival.
354 InsertNextPacket();
355
356 // Advance time by one frame size.
357 IncreaseTime(kFrameSizeMs);
358
359 // Advance the sequence number by 5, simulating that 5 empty packets were
360 // received, but never inserted.
361 seq_no_ += 10;
362 for (int j = 0; j < 10; ++j) {
363 dm_->RegisterEmptyPacket();
364 }
365
366 // Second packet arrival.
367 // Expect detector update method to be called once with inter-arrival time
368 // equal to 1 packet, and (base) target level equal to 1 as well.
369 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100370 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700371 InsertNextPacket();
372
373 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
374}
375
376// Same as above, but do not call RegisterEmptyPacket. Observe the target level
377// increase dramatically.
378TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
379 SetPacketAudioLength(kFrameSizeMs);
380 // First packet arrival.
381 InsertNextPacket();
382
383 // Advance time by one frame size.
384 IncreaseTime(kFrameSizeMs);
385
386 // Advance the sequence number by 5, simulating that 5 empty packets were
387 // received, but never inserted.
388 seq_no_ += 10;
389
390 // Second packet arrival.
391 // Expect detector update method to be called once with inter-arrival time
392 // equal to 1 packet, and (base) target level equal to 1 as well.
393 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100394 EXPECT_CALL(detector_, Update(10, false, 10)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700395 InsertNextPacket();
396
397 // Note 10 times higher target value.
398 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
399}
400
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000401TEST_F(DelayManagerTest, Failures) {
402 // Wrong sample rate.
403 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
404 // Wrong packet size.
405 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
406 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000407
408 // Minimum delay higher than a maximum delay is not accepted.
409 EXPECT_TRUE(dm_->SetMaximumDelay(10));
410 EXPECT_FALSE(dm_->SetMinimumDelay(20));
411
412 // Maximum delay less than minimum delay is not accepted.
413 EXPECT_TRUE(dm_->SetMaximumDelay(100));
414 EXPECT_TRUE(dm_->SetMinimumDelay(80));
415 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000416}
417
Minyue Li002fbb82018-10-04 11:31:03 +0200418TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
419 test::ScopedFieldTrials field_trial(
420 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
421 RecreateDelayManager();
422 EXPECT_EQ(absl::make_optional<int>(1 << 30),
423 dm_->forced_limit_probability_for_test());
424
425 SetPacketAudioLength(kFrameSizeMs);
426 // First packet arrival.
427 InsertNextPacket();
428 // Advance time by one frame size.
429 IncreaseTime(kFrameSizeMs);
430 // Second packet arrival.
431 // Expect detector update method to be called once with inter-arrival time
432 // equal to 1 packet.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100433 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
Minyue Li002fbb82018-10-04 11:31:03 +0200434 InsertNextPacket();
435 constexpr int kExpectedTarget = 1;
436 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
437}
438
439TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
440 {
441 test::ScopedFieldTrials field_trial(
442 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
443 RecreateDelayManager();
444 EXPECT_EQ(absl::make_optional<int>(53687091),
445 dm_->forced_limit_probability_for_test()); // 1/20 in Q30
446 }
447 {
448 test::ScopedFieldTrials field_trial(
449 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
450 RecreateDelayManager();
451 EXPECT_EQ(absl::make_optional<int>(536871),
452 dm_->forced_limit_probability_for_test()); // 1/2000 in Q30
453 }
454 {
455 test::ScopedFieldTrials field_trial(
456 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
457 RecreateDelayManager();
458 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
459 }
460 {
461 test::ScopedFieldTrials field_trial(
462 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
463 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
464 }
465 {
466 test::ScopedFieldTrials field_trial(
467 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
468 RecreateDelayManager();
469 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
470 }
471}
472
Ivo Creusen385b10b2017-10-13 12:37:27 +0200473// Test if the histogram is stretched correctly if the packet size is decreased.
474TEST(DelayManagerIATScalingTest, StretchTest) {
475 using IATVector = DelayManager::IATVector;
476 // Test a straightforward 60ms to 20ms change.
477 IATVector iat = {12, 0, 0, 0, 0, 0};
478 IATVector expected_result = {4, 4, 4, 0, 0, 0};
479 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
480 EXPECT_EQ(stretched_iat, expected_result);
481
482 // Test an example where the last bin in the stretched histogram should
483 // contain the sum of the elements that don't fit into the new histogram.
484 iat = {18, 15, 12, 9, 6, 3, 0};
485 expected_result = {6, 6, 6, 5, 5, 5, 30};
486 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
487 EXPECT_EQ(stretched_iat, expected_result);
488
489 // Test a 120ms to 60ms change.
490 iat = {18, 16, 14, 4, 0};
491 expected_result = {9, 9, 8, 8, 18};
492 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
493 EXPECT_EQ(stretched_iat, expected_result);
494
495 // Test a 120ms to 20ms change.
496 iat = {19, 12, 0, 0, 0, 0, 0, 0};
497 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
498 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
499 EXPECT_EQ(stretched_iat, expected_result);
500
501 // Test a 70ms to 40ms change.
502 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
503 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
504 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
505 EXPECT_EQ(stretched_iat, expected_result);
506
507 // Test a 30ms to 20ms change.
508 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
509 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
510 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
511 EXPECT_EQ(stretched_iat, expected_result);
512}
513
514// Test if the histogram is compressed correctly if the packet size is
515// increased.
516TEST(DelayManagerIATScalingTest, CompressionTest) {
517 using IATVector = DelayManager::IATVector;
518 // Test a 20 to 60 ms change.
519 IATVector iat = {12, 11, 10, 3, 2, 1};
520 IATVector expected_result = {33, 6, 0, 0, 0, 0};
521 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
522 EXPECT_EQ(compressed_iat, expected_result);
523
524 // Test a 60ms to 120ms change.
525 iat = {18, 16, 14, 4, 1};
526 expected_result = {34, 18, 1, 0, 0};
527 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
528 EXPECT_EQ(compressed_iat, expected_result);
529
530 // Test a 20ms to 120ms change.
531 iat = {18, 12, 5, 4, 4, 3, 5, 1};
532 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
533 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
534 EXPECT_EQ(compressed_iat, expected_result);
535
536 // Test a 70ms to 80ms change.
537 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
538 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
539 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
540 EXPECT_EQ(compressed_iat, expected_result);
541
542 // Test a 50ms to 110ms change.
543 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
544 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
545 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
546 EXPECT_EQ(compressed_iat, expected_result);
547}
548
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100549// Test if the histogram scaling function handles overflows correctly.
550TEST(DelayManagerIATScalingTest, OverflowTest) {
551 using IATVector = DelayManager::IATVector;
552 // Test a compression operation that can cause overflow.
553 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
554 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
555 0, 0, 0, 0, 0, 0, 0};
556 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
557 EXPECT_EQ(scaled_iat, expected_result);
558
559 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
560 114119, 2072996, 0, 2149354, 0};
561 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
562 0, 0, 0, 0, 0};
563 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
564 EXPECT_EQ(scaled_iat, expected_result);
565
566 // In this test case we will not be able to add everything to the final bin in
567 // the scaled histogram. Check that the last bin doesn't overflow.
568 iat = {2000000000, 2000000000, 2000000000,
569 2000000000, 2000000000, 2000000000};
570 expected_result = {666666666, 666666666, 666666666,
571 666666667, 666666667, 2147483647};
572 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
573 EXPECT_EQ(scaled_iat, expected_result);
574
575 // In this test case we will not be able to add enough to each of the bins,
576 // so the values should be smeared out past the end of the normal range.
577 iat = {2000000000, 2000000000, 2000000000,
578 2000000000, 2000000000, 2000000000};
579 expected_result = {2147483647, 2147483647, 2147483647,
580 2147483647, 2147483647, 1262581765};
581 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
582 EXPECT_EQ(scaled_iat, expected_result);
583}
Minyue Li002fbb82018-10-04 11:31:03 +0200584
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000585} // namespace webrtc