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