blob: e1cca89ffc2ed39e45f6535a445cabb2c7a1addf [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
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100270TEST_F(DelayManagerTest, UpdateReorderedPacket) {
271 SetPacketAudioLength(kFrameSizeMs);
272 InsertNextPacket();
273
274 // Insert packet that was sent before the previous packet.
275 EXPECT_CALL(detector_, Update(_, true, _));
276 EXPECT_EQ(0, dm_->Update(seq_no_ - 1, ts_ - kFrameSizeMs, kFs));
277}
278
henrik.lundinb8c55b12017-05-10 07:38:01 -0700279// Tests that skipped sequence numbers (simulating empty packets) are handled
280// correctly.
281TEST_F(DelayManagerTest, EmptyPacketsReported) {
282 SetPacketAudioLength(kFrameSizeMs);
283 // First packet arrival.
284 InsertNextPacket();
285
286 // Advance time by one frame size.
287 IncreaseTime(kFrameSizeMs);
288
289 // Advance the sequence number by 5, simulating that 5 empty packets were
290 // received, but never inserted.
291 seq_no_ += 10;
292 for (int j = 0; j < 10; ++j) {
293 dm_->RegisterEmptyPacket();
294 }
295
296 // Second packet arrival.
297 // Expect detector update method to be called once with inter-arrival time
298 // equal to 1 packet, and (base) target level equal to 1 as well.
299 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100300 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700301 InsertNextPacket();
302
303 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
304}
305
306// Same as above, but do not call RegisterEmptyPacket. Observe the target level
307// increase dramatically.
308TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
309 SetPacketAudioLength(kFrameSizeMs);
310 // First packet arrival.
311 InsertNextPacket();
312
313 // Advance time by one frame size.
314 IncreaseTime(kFrameSizeMs);
315
316 // Advance the sequence number by 5, simulating that 5 empty packets were
317 // received, but never inserted.
318 seq_no_ += 10;
319
320 // Second packet arrival.
321 // Expect detector update method to be called once with inter-arrival time
322 // equal to 1 packet, and (base) target level equal to 1 as well.
323 // Return false to indicate no peaks found.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100324 EXPECT_CALL(detector_, Update(10, false, 10)).WillOnce(Return(false));
henrik.lundinb8c55b12017-05-10 07:38:01 -0700325 InsertNextPacket();
326
327 // Note 10 times higher target value.
328 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
329}
330
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331TEST_F(DelayManagerTest, Failures) {
332 // Wrong sample rate.
333 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
334 // Wrong packet size.
335 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
336 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000337
338 // Minimum delay higher than a maximum delay is not accepted.
339 EXPECT_TRUE(dm_->SetMaximumDelay(10));
340 EXPECT_FALSE(dm_->SetMinimumDelay(20));
341
342 // Maximum delay less than minimum delay is not accepted.
343 EXPECT_TRUE(dm_->SetMaximumDelay(100));
344 EXPECT_TRUE(dm_->SetMinimumDelay(80));
345 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000346}
347
Minyue Li002fbb82018-10-04 11:31:03 +0200348TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
349 test::ScopedFieldTrials field_trial(
350 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
351 RecreateDelayManager();
352 EXPECT_EQ(absl::make_optional<int>(1 << 30),
353 dm_->forced_limit_probability_for_test());
354
355 SetPacketAudioLength(kFrameSizeMs);
356 // First packet arrival.
357 InsertNextPacket();
358 // Advance time by one frame size.
359 IncreaseTime(kFrameSizeMs);
360 // Second packet arrival.
361 // Expect detector update method to be called once with inter-arrival time
362 // equal to 1 packet.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +0100363 EXPECT_CALL(detector_, Update(1, false, 1)).WillOnce(Return(false));
Minyue Li002fbb82018-10-04 11:31:03 +0200364 InsertNextPacket();
365 constexpr int kExpectedTarget = 1;
366 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
367}
368
369TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
370 {
371 test::ScopedFieldTrials field_trial(
372 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
373 RecreateDelayManager();
374 EXPECT_EQ(absl::make_optional<int>(53687091),
375 dm_->forced_limit_probability_for_test()); // 1/20 in Q30
376 }
377 {
378 test::ScopedFieldTrials field_trial(
379 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
380 RecreateDelayManager();
381 EXPECT_EQ(absl::make_optional<int>(536871),
382 dm_->forced_limit_probability_for_test()); // 1/2000 in Q30
383 }
384 {
385 test::ScopedFieldTrials field_trial(
386 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
387 RecreateDelayManager();
388 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
389 }
390 {
391 test::ScopedFieldTrials field_trial(
392 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
393 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
394 }
395 {
396 test::ScopedFieldTrials field_trial(
397 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
398 RecreateDelayManager();
399 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
400 }
401}
402
Ivo Creusen385b10b2017-10-13 12:37:27 +0200403// Test if the histogram is stretched correctly if the packet size is decreased.
404TEST(DelayManagerIATScalingTest, StretchTest) {
405 using IATVector = DelayManager::IATVector;
406 // Test a straightforward 60ms to 20ms change.
407 IATVector iat = {12, 0, 0, 0, 0, 0};
408 IATVector expected_result = {4, 4, 4, 0, 0, 0};
409 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
410 EXPECT_EQ(stretched_iat, expected_result);
411
412 // Test an example where the last bin in the stretched histogram should
413 // contain the sum of the elements that don't fit into the new histogram.
414 iat = {18, 15, 12, 9, 6, 3, 0};
415 expected_result = {6, 6, 6, 5, 5, 5, 30};
416 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
417 EXPECT_EQ(stretched_iat, expected_result);
418
419 // Test a 120ms to 60ms change.
420 iat = {18, 16, 14, 4, 0};
421 expected_result = {9, 9, 8, 8, 18};
422 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
423 EXPECT_EQ(stretched_iat, expected_result);
424
425 // Test a 120ms to 20ms change.
426 iat = {19, 12, 0, 0, 0, 0, 0, 0};
427 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
428 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
429 EXPECT_EQ(stretched_iat, expected_result);
430
431 // Test a 70ms to 40ms change.
432 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
433 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
434 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
435 EXPECT_EQ(stretched_iat, expected_result);
436
437 // Test a 30ms to 20ms change.
438 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
439 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
440 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
441 EXPECT_EQ(stretched_iat, expected_result);
442}
443
444// Test if the histogram is compressed correctly if the packet size is
445// increased.
446TEST(DelayManagerIATScalingTest, CompressionTest) {
447 using IATVector = DelayManager::IATVector;
448 // Test a 20 to 60 ms change.
449 IATVector iat = {12, 11, 10, 3, 2, 1};
450 IATVector expected_result = {33, 6, 0, 0, 0, 0};
451 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
452 EXPECT_EQ(compressed_iat, expected_result);
453
454 // Test a 60ms to 120ms change.
455 iat = {18, 16, 14, 4, 1};
456 expected_result = {34, 18, 1, 0, 0};
457 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
458 EXPECT_EQ(compressed_iat, expected_result);
459
460 // Test a 20ms to 120ms change.
461 iat = {18, 12, 5, 4, 4, 3, 5, 1};
462 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
463 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
464 EXPECT_EQ(compressed_iat, expected_result);
465
466 // Test a 70ms to 80ms change.
467 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
468 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
469 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
470 EXPECT_EQ(compressed_iat, expected_result);
471
472 // Test a 50ms to 110ms change.
473 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
474 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
475 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
476 EXPECT_EQ(compressed_iat, expected_result);
477}
478
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100479// Test if the histogram scaling function handles overflows correctly.
480TEST(DelayManagerIATScalingTest, OverflowTest) {
481 using IATVector = DelayManager::IATVector;
482 // Test a compression operation that can cause overflow.
483 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
484 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
485 0, 0, 0, 0, 0, 0, 0};
486 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
487 EXPECT_EQ(scaled_iat, expected_result);
488
489 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
490 114119, 2072996, 0, 2149354, 0};
491 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
492 0, 0, 0, 0, 0};
493 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
494 EXPECT_EQ(scaled_iat, expected_result);
495
496 // In this test case we will not be able to add everything to the final bin in
497 // the scaled histogram. Check that the last bin doesn't overflow.
498 iat = {2000000000, 2000000000, 2000000000,
499 2000000000, 2000000000, 2000000000};
500 expected_result = {666666666, 666666666, 666666666,
501 666666667, 666666667, 2147483647};
502 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
503 EXPECT_EQ(scaled_iat, expected_result);
504
505 // In this test case we will not be able to add enough to each of the bins,
506 // so the values should be smeared out past the end of the normal range.
507 iat = {2000000000, 2000000000, 2000000000,
508 2000000000, 2000000000, 2000000000};
509 expected_result = {2147483647, 2147483647, 2147483647,
510 2147483647, 2147483647, 1262581765};
511 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
512 EXPECT_EQ(scaled_iat, expected_result);
513}
Minyue Li002fbb82018-10-04 11:31:03 +0200514
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000515} // namespace webrtc