blob: e4e865fe46be30028032c7d546c93c82aadc1a10 [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;
30 static const int kTimeStepMs = 10;
31 static const int kFs = 8000;
32 static const int kFrameSizeMs = 20;
33 static const int kTsIncrement = kFrameSizeMs * kFs / 1000;
34
35 DelayManagerTest();
36 virtual void SetUp();
37 virtual void TearDown();
Minyue Li002fbb82018-10-04 11:31:03 +020038 void RecreateDelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 void SetPacketAudioLength(int lengt_ms);
40 void InsertNextPacket();
41 void IncreaseTime(int inc_ms);
42
Minyue Li002fbb82018-10-04 11:31:03 +020043 std::unique_ptr<DelayManager> dm_;
henrik.lundinf3933702016-04-28 01:53:52 -070044 TickTimer tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045 MockDelayPeakDetector detector_;
46 uint16_t seq_no_;
47 uint32_t ts_;
48};
49
50DelayManagerTest::DelayManagerTest()
Minyue Li002fbb82018-10-04 11:31:03 +020051 : dm_(nullptr), detector_(&tick_timer_), seq_no_(0x1234), ts_(0x12345678) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052
53void DelayManagerTest::SetUp() {
Minyue Li002fbb82018-10-04 11:31:03 +020054 RecreateDelayManager();
55}
56
57void DelayManagerTest::RecreateDelayManager() {
Yves Gerey665174f2018-06-19 15:03:05 +020058 EXPECT_CALL(detector_, Reset()).Times(1);
Minyue Li002fbb82018-10-04 11:31:03 +020059 dm_.reset(new DelayManager(kMaxNumberOfPackets, &detector_, &tick_timer_));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060}
61
62void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
63 EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms));
64 dm_->SetPacketAudioLength(lengt_ms);
65}
66
67void DelayManagerTest::InsertNextPacket() {
68 EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs));
69 seq_no_ += 1;
70 ts_ += kTsIncrement;
71}
72
73void DelayManagerTest::IncreaseTime(int inc_ms) {
74 for (int t = 0; t < inc_ms; t += kTimeStepMs) {
henrik.lundinf3933702016-04-28 01:53:52 -070075 tick_timer_.Increment();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076 }
77}
78void DelayManagerTest::TearDown() {
79 EXPECT_CALL(detector_, Die());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080}
81
82TEST_F(DelayManagerTest, CreateAndDestroy) {
83 // Nothing to do here. The test fixture creates and destroys the DelayManager
84 // object.
85}
86
87TEST_F(DelayManagerTest, VectorInitialization) {
88 const DelayManager::IATVector& vec = dm_->iat_vector();
89 double sum = 0.0;
90 for (size_t i = 0; i < vec.size(); i++) {
Henrik Lundincb3e8fe2015-05-11 15:15:51 +020091 EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
92 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 sum += vec[i];
94 }
95 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
96}
97
98TEST_F(DelayManagerTest, SetPacketAudioLength) {
99 const int kLengthMs = 30;
100 // Expect DelayManager to pass on the new length to the detector object.
Yves Gerey665174f2018-06-19 15:03:05 +0200101 EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs)).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102 EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs));
103 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value.
104}
105
106TEST_F(DelayManagerTest, PeakFound) {
107 // Expect DelayManager to pass on the question to the detector.
108 // Call twice, and let the detector return true the first time and false the
109 // second time.
110 EXPECT_CALL(detector_, peak_found())
111 .WillOnce(Return(true))
112 .WillOnce(Return(false));
113 EXPECT_TRUE(dm_->PeakFound());
114 EXPECT_FALSE(dm_->PeakFound());
115}
116
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117TEST_F(DelayManagerTest, UpdateNormal) {
118 SetPacketAudioLength(kFrameSizeMs);
119 // First packet arrival.
120 InsertNextPacket();
121 // Advance time by one frame size.
122 IncreaseTime(kFrameSizeMs);
123 // Second packet arrival.
124 // Expect detector update method to be called once with inter-arrival time
125 // equal to 1 packet, and (base) target level equal to 1 as well.
126 // Return false to indicate no peaks found.
Yves Gerey665174f2018-06-19 15:03:05 +0200127 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128 InsertNextPacket();
129 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
130 EXPECT_EQ(1, dm_->base_target_level());
131 int lower, higher;
132 dm_->BufferLimits(&lower, &higher);
133 // Expect |lower| to be 75% of target level, and |higher| to be target level,
134 // but also at least 20 ms higher than |lower|, which is the limiting case
135 // here.
136 EXPECT_EQ((1 << 8) * 3 / 4, lower);
137 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
138}
139
140TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) {
141 SetPacketAudioLength(kFrameSizeMs);
142 // First packet arrival.
143 InsertNextPacket();
144 // Advance time by two frame size.
145 IncreaseTime(2 * kFrameSizeMs);
146 // Second packet arrival.
147 // Expect detector update method to be called once with inter-arrival time
148 // equal to 1 packet, and (base) target level equal to 1 as well.
149 // Return false to indicate no peaks found.
Yves Gerey665174f2018-06-19 15:03:05 +0200150 EXPECT_CALL(detector_, Update(2, 2)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000151 InsertNextPacket();
152 EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8.
153 EXPECT_EQ(2, dm_->base_target_level());
154 int lower, higher;
155 dm_->BufferLimits(&lower, &higher);
156 // Expect |lower| to be 75% of target level, and |higher| to be target level,
157 // but also at least 20 ms higher than |lower|, which is the limiting case
158 // here.
159 EXPECT_EQ((2 << 8) * 3 / 4, lower);
160 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
161}
162
163TEST_F(DelayManagerTest, UpdatePeakFound) {
164 SetPacketAudioLength(kFrameSizeMs);
165 // First packet arrival.
166 InsertNextPacket();
167 // Advance time by one frame size.
168 IncreaseTime(kFrameSizeMs);
169 // Second packet arrival.
170 // Expect detector update method to be called once with inter-arrival time
171 // equal to 1 packet, and (base) target level equal to 1 as well.
172 // Return true to indicate that peaks are found. Let the peak height be 5.
Yves Gerey665174f2018-06-19 15:03:05 +0200173 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(true));
174 EXPECT_CALL(detector_, MaxPeakHeight()).WillOnce(Return(5));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000175 InsertNextPacket();
176 EXPECT_EQ(5 << 8, dm_->TargetLevel());
177 EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks.
178 int lower, higher;
179 dm_->BufferLimits(&lower, &higher);
180 // Expect |lower| to be 75% of target level, and |higher| to be target level.
181 EXPECT_EQ((5 << 8) * 3 / 4, lower);
182 EXPECT_EQ(5 << 8, higher);
183}
184
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000185TEST_F(DelayManagerTest, TargetDelay) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000186 SetPacketAudioLength(kFrameSizeMs);
187 // First packet arrival.
188 InsertNextPacket();
189 // Advance time by one frame size.
190 IncreaseTime(kFrameSizeMs);
191 // Second packet arrival.
192 // Expect detector update method to be called once with inter-arrival time
193 // equal to 1 packet, and (base) target level equal to 1 as well.
194 // Return false to indicate no peaks found.
Yves Gerey665174f2018-06-19 15:03:05 +0200195 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000197 const int kExpectedTarget = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000198 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
199 EXPECT_EQ(1, dm_->base_target_level());
200 int lower, higher;
201 dm_->BufferLimits(&lower, &higher);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000202 // Expect |lower| to be 75% of base target level, and |higher| to be
203 // lower + 20 ms headroom.
204 EXPECT_EQ((1 << 8) * 3 / 4, lower);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
206}
207
Niels Möller18f1adc2018-08-23 08:40:41 +0200208TEST_F(DelayManagerTest, MaxDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000209 const int kExpectedTarget = 5;
210 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
211 SetPacketAudioLength(kFrameSizeMs);
212 // First packet arrival.
213 InsertNextPacket();
214 // Second packet arrival.
215 // Expect detector update method to be called once with inter-arrival time
216 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
217 EXPECT_CALL(detector_, Update(kExpectedTarget, _))
218 .WillRepeatedly(Return(true));
219 EXPECT_CALL(detector_, MaxPeakHeight())
220 .WillRepeatedly(Return(kExpectedTarget));
221 IncreaseTime(kTimeIncrement);
222 InsertNextPacket();
223
224 // No limit is set.
225 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
226
227 int kMaxDelayPackets = kExpectedTarget - 2;
228 int kMaxDelayMs = kMaxDelayPackets * kFrameSizeMs;
229 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
230 IncreaseTime(kTimeIncrement);
231 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000232 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
233
234 // Target level at least should be one packet.
235 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
236}
237
Niels Möller18f1adc2018-08-23 08:40:41 +0200238TEST_F(DelayManagerTest, MinDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000239 const int kExpectedTarget = 5;
240 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
241 SetPacketAudioLength(kFrameSizeMs);
242 // First packet arrival.
243 InsertNextPacket();
244 // Second packet arrival.
245 // Expect detector update method to be called once with inter-arrival time
246 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
247 EXPECT_CALL(detector_, Update(kExpectedTarget, _))
248 .WillRepeatedly(Return(true));
249 EXPECT_CALL(detector_, MaxPeakHeight())
250 .WillRepeatedly(Return(kExpectedTarget));
251 IncreaseTime(kTimeIncrement);
252 InsertNextPacket();
253
254 // No limit is applied.
255 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
256
257 int kMinDelayPackets = kExpectedTarget + 2;
258 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
259 dm_->SetMinimumDelay(kMinDelayMs);
260 IncreaseTime(kTimeIncrement);
261 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000262 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
263}
264
henrik.lundinb8c55b12017-05-10 07:38:01 -0700265// Tests that skipped sequence numbers (simulating empty packets) are handled
266// correctly.
267TEST_F(DelayManagerTest, EmptyPacketsReported) {
268 SetPacketAudioLength(kFrameSizeMs);
269 // First packet arrival.
270 InsertNextPacket();
271
272 // Advance time by one frame size.
273 IncreaseTime(kFrameSizeMs);
274
275 // Advance the sequence number by 5, simulating that 5 empty packets were
276 // received, but never inserted.
277 seq_no_ += 10;
278 for (int j = 0; j < 10; ++j) {
279 dm_->RegisterEmptyPacket();
280 }
281
282 // Second packet arrival.
283 // Expect detector update method to be called once with inter-arrival time
284 // equal to 1 packet, and (base) target level equal to 1 as well.
285 // Return false to indicate no peaks found.
286 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
287 InsertNextPacket();
288
289 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
290}
291
292// Same as above, but do not call RegisterEmptyPacket. Observe the target level
293// increase dramatically.
294TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
295 SetPacketAudioLength(kFrameSizeMs);
296 // First packet arrival.
297 InsertNextPacket();
298
299 // Advance time by one frame size.
300 IncreaseTime(kFrameSizeMs);
301
302 // Advance the sequence number by 5, simulating that 5 empty packets were
303 // received, but never inserted.
304 seq_no_ += 10;
305
306 // Second packet arrival.
307 // Expect detector update method to be called once with inter-arrival time
308 // equal to 1 packet, and (base) target level equal to 1 as well.
309 // Return false to indicate no peaks found.
310 EXPECT_CALL(detector_, Update(10, 10)).WillOnce(Return(false));
311 InsertNextPacket();
312
313 // Note 10 times higher target value.
314 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
315}
316
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317TEST_F(DelayManagerTest, Failures) {
318 // Wrong sample rate.
319 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
320 // Wrong packet size.
321 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
322 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000323
324 // Minimum delay higher than a maximum delay is not accepted.
325 EXPECT_TRUE(dm_->SetMaximumDelay(10));
326 EXPECT_FALSE(dm_->SetMinimumDelay(20));
327
328 // Maximum delay less than minimum delay is not accepted.
329 EXPECT_TRUE(dm_->SetMaximumDelay(100));
330 EXPECT_TRUE(dm_->SetMinimumDelay(80));
331 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332}
333
Minyue Li002fbb82018-10-04 11:31:03 +0200334TEST_F(DelayManagerTest, TargetDelayGreaterThanOne) {
335 test::ScopedFieldTrials field_trial(
336 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-0/");
337 RecreateDelayManager();
338 EXPECT_EQ(absl::make_optional<int>(1 << 30),
339 dm_->forced_limit_probability_for_test());
340
341 SetPacketAudioLength(kFrameSizeMs);
342 // First packet arrival.
343 InsertNextPacket();
344 // Advance time by one frame size.
345 IncreaseTime(kFrameSizeMs);
346 // Second packet arrival.
347 // Expect detector update method to be called once with inter-arrival time
348 // equal to 1 packet.
349 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
350 InsertNextPacket();
351 constexpr int kExpectedTarget = 1;
352 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
353}
354
355TEST_F(DelayManagerTest, ForcedTargetDelayPercentile) {
356 {
357 test::ScopedFieldTrials field_trial(
358 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-95/");
359 RecreateDelayManager();
360 EXPECT_EQ(absl::make_optional<int>(53687091),
361 dm_->forced_limit_probability_for_test()); // 1/20 in Q30
362 }
363 {
364 test::ScopedFieldTrials field_trial(
365 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-99.95/");
366 RecreateDelayManager();
367 EXPECT_EQ(absl::make_optional<int>(536871),
368 dm_->forced_limit_probability_for_test()); // 1/2000 in Q30
369 }
370 {
371 test::ScopedFieldTrials field_trial(
372 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Disabled/");
373 RecreateDelayManager();
374 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
375 }
376 {
377 test::ScopedFieldTrials field_trial(
378 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled--1/");
379 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
380 }
381 {
382 test::ScopedFieldTrials field_trial(
383 "WebRTC-Audio-NetEqForceTargetDelayPercentile/Enabled-100.1/");
384 RecreateDelayManager();
385 EXPECT_EQ(absl::nullopt, dm_->forced_limit_probability_for_test());
386 }
387}
388
Ivo Creusen385b10b2017-10-13 12:37:27 +0200389// Test if the histogram is stretched correctly if the packet size is decreased.
390TEST(DelayManagerIATScalingTest, StretchTest) {
391 using IATVector = DelayManager::IATVector;
392 // Test a straightforward 60ms to 20ms change.
393 IATVector iat = {12, 0, 0, 0, 0, 0};
394 IATVector expected_result = {4, 4, 4, 0, 0, 0};
395 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
396 EXPECT_EQ(stretched_iat, expected_result);
397
398 // Test an example where the last bin in the stretched histogram should
399 // contain the sum of the elements that don't fit into the new histogram.
400 iat = {18, 15, 12, 9, 6, 3, 0};
401 expected_result = {6, 6, 6, 5, 5, 5, 30};
402 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
403 EXPECT_EQ(stretched_iat, expected_result);
404
405 // Test a 120ms to 60ms change.
406 iat = {18, 16, 14, 4, 0};
407 expected_result = {9, 9, 8, 8, 18};
408 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
409 EXPECT_EQ(stretched_iat, expected_result);
410
411 // Test a 120ms to 20ms change.
412 iat = {19, 12, 0, 0, 0, 0, 0, 0};
413 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
414 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
415 EXPECT_EQ(stretched_iat, expected_result);
416
417 // Test a 70ms to 40ms change.
418 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
419 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
420 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
421 EXPECT_EQ(stretched_iat, expected_result);
422
423 // Test a 30ms to 20ms change.
424 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
425 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
426 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
427 EXPECT_EQ(stretched_iat, expected_result);
428}
429
430// Test if the histogram is compressed correctly if the packet size is
431// increased.
432TEST(DelayManagerIATScalingTest, CompressionTest) {
433 using IATVector = DelayManager::IATVector;
434 // Test a 20 to 60 ms change.
435 IATVector iat = {12, 11, 10, 3, 2, 1};
436 IATVector expected_result = {33, 6, 0, 0, 0, 0};
437 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
438 EXPECT_EQ(compressed_iat, expected_result);
439
440 // Test a 60ms to 120ms change.
441 iat = {18, 16, 14, 4, 1};
442 expected_result = {34, 18, 1, 0, 0};
443 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
444 EXPECT_EQ(compressed_iat, expected_result);
445
446 // Test a 20ms to 120ms change.
447 iat = {18, 12, 5, 4, 4, 3, 5, 1};
448 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
449 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
450 EXPECT_EQ(compressed_iat, expected_result);
451
452 // Test a 70ms to 80ms change.
453 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
454 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
455 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
456 EXPECT_EQ(compressed_iat, expected_result);
457
458 // Test a 50ms to 110ms change.
459 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
460 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
461 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
462 EXPECT_EQ(compressed_iat, expected_result);
463}
464
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100465// Test if the histogram scaling function handles overflows correctly.
466TEST(DelayManagerIATScalingTest, OverflowTest) {
467 using IATVector = DelayManager::IATVector;
468 // Test a compression operation that can cause overflow.
469 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
470 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
471 0, 0, 0, 0, 0, 0, 0};
472 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
473 EXPECT_EQ(scaled_iat, expected_result);
474
475 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
476 114119, 2072996, 0, 2149354, 0};
477 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
478 0, 0, 0, 0, 0};
479 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
480 EXPECT_EQ(scaled_iat, expected_result);
481
482 // In this test case we will not be able to add everything to the final bin in
483 // the scaled histogram. Check that the last bin doesn't overflow.
484 iat = {2000000000, 2000000000, 2000000000,
485 2000000000, 2000000000, 2000000000};
486 expected_result = {666666666, 666666666, 666666666,
487 666666667, 666666667, 2147483647};
488 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
489 EXPECT_EQ(scaled_iat, expected_result);
490
491 // In this test case we will not be able to add enough to each of the bins,
492 // so the values should be smeared out past the end of the normal range.
493 iat = {2000000000, 2000000000, 2000000000,
494 2000000000, 2000000000, 2000000000};
495 expected_result = {2147483647, 2147483647, 2147483647,
496 2147483647, 2147483647, 1262581765};
497 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
498 EXPECT_EQ(scaled_iat, expected_result);
499}
Minyue Li002fbb82018-10-04 11:31:03 +0200500
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000501} // namespace webrtc