blob: 6afed661f916c1439c93191bf75463b162118ee7 [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"
18#include "test/gmock.h"
19#include "test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000020
21namespace webrtc {
22
23using ::testing::Return;
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000024using ::testing::_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025
26class DelayManagerTest : public ::testing::Test {
27 protected:
28 static const int kMaxNumberOfPackets = 240;
29 static const int kTimeStepMs = 10;
30 static const int kFs = 8000;
31 static const int kFrameSizeMs = 20;
32 static const int kTsIncrement = kFrameSizeMs * kFs / 1000;
33
34 DelayManagerTest();
35 virtual void SetUp();
36 virtual void TearDown();
37 void SetPacketAudioLength(int lengt_ms);
38 void InsertNextPacket();
39 void IncreaseTime(int inc_ms);
40
41 DelayManager* dm_;
henrik.lundinf3933702016-04-28 01:53:52 -070042 TickTimer tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043 MockDelayPeakDetector detector_;
44 uint16_t seq_no_;
45 uint32_t ts_;
46};
47
48DelayManagerTest::DelayManagerTest()
henrik.lundinf3933702016-04-28 01:53:52 -070049 : dm_(NULL), detector_(&tick_timer_), seq_no_(0x1234), ts_(0x12345678) {}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
51void DelayManagerTest::SetUp() {
Yves Gerey665174f2018-06-19 15:03:05 +020052 EXPECT_CALL(detector_, Reset()).Times(1);
henrik.lundin8f8c96d2016-04-28 23:19:20 -070053 dm_ = new DelayManager(kMaxNumberOfPackets, &detector_, &tick_timer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054}
55
56void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
57 EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms));
58 dm_->SetPacketAudioLength(lengt_ms);
59}
60
61void DelayManagerTest::InsertNextPacket() {
62 EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs));
63 seq_no_ += 1;
64 ts_ += kTsIncrement;
65}
66
67void DelayManagerTest::IncreaseTime(int inc_ms) {
68 for (int t = 0; t < inc_ms; t += kTimeStepMs) {
henrik.lundinf3933702016-04-28 01:53:52 -070069 tick_timer_.Increment();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070 }
71}
72void DelayManagerTest::TearDown() {
73 EXPECT_CALL(detector_, Die());
74 delete dm_;
75}
76
77TEST_F(DelayManagerTest, CreateAndDestroy) {
78 // Nothing to do here. The test fixture creates and destroys the DelayManager
79 // object.
80}
81
82TEST_F(DelayManagerTest, VectorInitialization) {
83 const DelayManager::IATVector& vec = dm_->iat_vector();
84 double sum = 0.0;
85 for (size_t i = 0; i < vec.size(); i++) {
Henrik Lundincb3e8fe2015-05-11 15:15:51 +020086 EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
87 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 sum += vec[i];
89 }
90 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
91}
92
93TEST_F(DelayManagerTest, SetPacketAudioLength) {
94 const int kLengthMs = 30;
95 // Expect DelayManager to pass on the new length to the detector object.
Yves Gerey665174f2018-06-19 15:03:05 +020096 EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs)).Times(1);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097 EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs));
98 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value.
99}
100
101TEST_F(DelayManagerTest, PeakFound) {
102 // Expect DelayManager to pass on the question to the detector.
103 // Call twice, and let the detector return true the first time and false the
104 // second time.
105 EXPECT_CALL(detector_, peak_found())
106 .WillOnce(Return(true))
107 .WillOnce(Return(false));
108 EXPECT_TRUE(dm_->PeakFound());
109 EXPECT_FALSE(dm_->PeakFound());
110}
111
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112TEST_F(DelayManagerTest, UpdateNormal) {
113 SetPacketAudioLength(kFrameSizeMs);
114 // First packet arrival.
115 InsertNextPacket();
116 // Advance time by one frame size.
117 IncreaseTime(kFrameSizeMs);
118 // Second packet arrival.
119 // Expect detector update method to be called once with inter-arrival time
120 // equal to 1 packet, and (base) target level equal to 1 as well.
121 // Return false to indicate no peaks found.
Yves Gerey665174f2018-06-19 15:03:05 +0200122 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123 InsertNextPacket();
124 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
125 EXPECT_EQ(1, dm_->base_target_level());
126 int lower, higher;
127 dm_->BufferLimits(&lower, &higher);
128 // Expect |lower| to be 75% of target level, and |higher| to be target level,
129 // but also at least 20 ms higher than |lower|, which is the limiting case
130 // here.
131 EXPECT_EQ((1 << 8) * 3 / 4, lower);
132 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
133}
134
135TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) {
136 SetPacketAudioLength(kFrameSizeMs);
137 // First packet arrival.
138 InsertNextPacket();
139 // Advance time by two frame size.
140 IncreaseTime(2 * kFrameSizeMs);
141 // Second packet arrival.
142 // Expect detector update method to be called once with inter-arrival time
143 // equal to 1 packet, and (base) target level equal to 1 as well.
144 // Return false to indicate no peaks found.
Yves Gerey665174f2018-06-19 15:03:05 +0200145 EXPECT_CALL(detector_, Update(2, 2)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146 InsertNextPacket();
147 EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8.
148 EXPECT_EQ(2, dm_->base_target_level());
149 int lower, higher;
150 dm_->BufferLimits(&lower, &higher);
151 // Expect |lower| to be 75% of target level, and |higher| to be target level,
152 // but also at least 20 ms higher than |lower|, which is the limiting case
153 // here.
154 EXPECT_EQ((2 << 8) * 3 / 4, lower);
155 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
156}
157
158TEST_F(DelayManagerTest, UpdatePeakFound) {
159 SetPacketAudioLength(kFrameSizeMs);
160 // First packet arrival.
161 InsertNextPacket();
162 // Advance time by one frame size.
163 IncreaseTime(kFrameSizeMs);
164 // Second packet arrival.
165 // Expect detector update method to be called once with inter-arrival time
166 // equal to 1 packet, and (base) target level equal to 1 as well.
167 // Return true to indicate that peaks are found. Let the peak height be 5.
Yves Gerey665174f2018-06-19 15:03:05 +0200168 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(true));
169 EXPECT_CALL(detector_, MaxPeakHeight()).WillOnce(Return(5));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 InsertNextPacket();
171 EXPECT_EQ(5 << 8, dm_->TargetLevel());
172 EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks.
173 int lower, higher;
174 dm_->BufferLimits(&lower, &higher);
175 // Expect |lower| to be 75% of target level, and |higher| to be target level.
176 EXPECT_EQ((5 << 8) * 3 / 4, lower);
177 EXPECT_EQ(5 << 8, higher);
178}
179
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000180TEST_F(DelayManagerTest, TargetDelay) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 SetPacketAudioLength(kFrameSizeMs);
182 // First packet arrival.
183 InsertNextPacket();
184 // Advance time by one frame size.
185 IncreaseTime(kFrameSizeMs);
186 // Second packet arrival.
187 // Expect detector update method to be called once with inter-arrival time
188 // equal to 1 packet, and (base) target level equal to 1 as well.
189 // Return false to indicate no peaks found.
Yves Gerey665174f2018-06-19 15:03:05 +0200190 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000192 const int kExpectedTarget = 1;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel()); // In Q8.
194 EXPECT_EQ(1, dm_->base_target_level());
195 int lower, higher;
196 dm_->BufferLimits(&lower, &higher);
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000197 // Expect |lower| to be 75% of base target level, and |higher| to be
198 // lower + 20 ms headroom.
199 EXPECT_EQ((1 << 8) * 3 / 4, lower);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000200 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
201}
202
Niels Möller18f1adc2018-08-23 08:40:41 +0200203TEST_F(DelayManagerTest, MaxDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000204 const int kExpectedTarget = 5;
205 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
206 SetPacketAudioLength(kFrameSizeMs);
207 // First packet arrival.
208 InsertNextPacket();
209 // Second packet arrival.
210 // Expect detector update method to be called once with inter-arrival time
211 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
212 EXPECT_CALL(detector_, Update(kExpectedTarget, _))
213 .WillRepeatedly(Return(true));
214 EXPECT_CALL(detector_, MaxPeakHeight())
215 .WillRepeatedly(Return(kExpectedTarget));
216 IncreaseTime(kTimeIncrement);
217 InsertNextPacket();
218
219 // No limit is set.
220 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
221
222 int kMaxDelayPackets = kExpectedTarget - 2;
223 int kMaxDelayMs = kMaxDelayPackets * kFrameSizeMs;
224 EXPECT_TRUE(dm_->SetMaximumDelay(kMaxDelayMs));
225 IncreaseTime(kTimeIncrement);
226 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000227 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
228
229 // Target level at least should be one packet.
230 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
231}
232
Niels Möller18f1adc2018-08-23 08:40:41 +0200233TEST_F(DelayManagerTest, MinDelay) {
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000234 const int kExpectedTarget = 5;
235 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
236 SetPacketAudioLength(kFrameSizeMs);
237 // First packet arrival.
238 InsertNextPacket();
239 // Second packet arrival.
240 // Expect detector update method to be called once with inter-arrival time
241 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
242 EXPECT_CALL(detector_, Update(kExpectedTarget, _))
243 .WillRepeatedly(Return(true));
244 EXPECT_CALL(detector_, MaxPeakHeight())
245 .WillRepeatedly(Return(kExpectedTarget));
246 IncreaseTime(kTimeIncrement);
247 InsertNextPacket();
248
249 // No limit is applied.
250 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
251
252 int kMinDelayPackets = kExpectedTarget + 2;
253 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
254 dm_->SetMinimumDelay(kMinDelayMs);
255 IncreaseTime(kTimeIncrement);
256 InsertNextPacket();
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000257 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
258}
259
henrik.lundinb8c55b12017-05-10 07:38:01 -0700260// Tests that skipped sequence numbers (simulating empty packets) are handled
261// correctly.
262TEST_F(DelayManagerTest, EmptyPacketsReported) {
263 SetPacketAudioLength(kFrameSizeMs);
264 // First packet arrival.
265 InsertNextPacket();
266
267 // Advance time by one frame size.
268 IncreaseTime(kFrameSizeMs);
269
270 // Advance the sequence number by 5, simulating that 5 empty packets were
271 // received, but never inserted.
272 seq_no_ += 10;
273 for (int j = 0; j < 10; ++j) {
274 dm_->RegisterEmptyPacket();
275 }
276
277 // Second packet arrival.
278 // Expect detector update method to be called once with inter-arrival time
279 // equal to 1 packet, and (base) target level equal to 1 as well.
280 // Return false to indicate no peaks found.
281 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
282 InsertNextPacket();
283
284 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
285}
286
287// Same as above, but do not call RegisterEmptyPacket. Observe the target level
288// increase dramatically.
289TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
290 SetPacketAudioLength(kFrameSizeMs);
291 // First packet arrival.
292 InsertNextPacket();
293
294 // Advance time by one frame size.
295 IncreaseTime(kFrameSizeMs);
296
297 // Advance the sequence number by 5, simulating that 5 empty packets were
298 // received, but never inserted.
299 seq_no_ += 10;
300
301 // Second packet arrival.
302 // Expect detector update method to be called once with inter-arrival time
303 // equal to 1 packet, and (base) target level equal to 1 as well.
304 // Return false to indicate no peaks found.
305 EXPECT_CALL(detector_, Update(10, 10)).WillOnce(Return(false));
306 InsertNextPacket();
307
308 // Note 10 times higher target value.
309 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
310}
311
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000312TEST_F(DelayManagerTest, Failures) {
313 // Wrong sample rate.
314 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
315 // Wrong packet size.
316 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
317 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000318
319 // Minimum delay higher than a maximum delay is not accepted.
320 EXPECT_TRUE(dm_->SetMaximumDelay(10));
321 EXPECT_FALSE(dm_->SetMinimumDelay(20));
322
323 // Maximum delay less than minimum delay is not accepted.
324 EXPECT_TRUE(dm_->SetMaximumDelay(100));
325 EXPECT_TRUE(dm_->SetMinimumDelay(80));
326 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000327}
328
Ivo Creusen385b10b2017-10-13 12:37:27 +0200329// Test if the histogram is stretched correctly if the packet size is decreased.
330TEST(DelayManagerIATScalingTest, StretchTest) {
331 using IATVector = DelayManager::IATVector;
332 // Test a straightforward 60ms to 20ms change.
333 IATVector iat = {12, 0, 0, 0, 0, 0};
334 IATVector expected_result = {4, 4, 4, 0, 0, 0};
335 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
336 EXPECT_EQ(stretched_iat, expected_result);
337
338 // Test an example where the last bin in the stretched histogram should
339 // contain the sum of the elements that don't fit into the new histogram.
340 iat = {18, 15, 12, 9, 6, 3, 0};
341 expected_result = {6, 6, 6, 5, 5, 5, 30};
342 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
343 EXPECT_EQ(stretched_iat, expected_result);
344
345 // Test a 120ms to 60ms change.
346 iat = {18, 16, 14, 4, 0};
347 expected_result = {9, 9, 8, 8, 18};
348 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
349 EXPECT_EQ(stretched_iat, expected_result);
350
351 // Test a 120ms to 20ms change.
352 iat = {19, 12, 0, 0, 0, 0, 0, 0};
353 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
354 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
355 EXPECT_EQ(stretched_iat, expected_result);
356
357 // Test a 70ms to 40ms change.
358 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
359 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
360 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
361 EXPECT_EQ(stretched_iat, expected_result);
362
363 // Test a 30ms to 20ms change.
364 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
365 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
366 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
367 EXPECT_EQ(stretched_iat, expected_result);
368}
369
370// Test if the histogram is compressed correctly if the packet size is
371// increased.
372TEST(DelayManagerIATScalingTest, CompressionTest) {
373 using IATVector = DelayManager::IATVector;
374 // Test a 20 to 60 ms change.
375 IATVector iat = {12, 11, 10, 3, 2, 1};
376 IATVector expected_result = {33, 6, 0, 0, 0, 0};
377 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
378 EXPECT_EQ(compressed_iat, expected_result);
379
380 // Test a 60ms to 120ms change.
381 iat = {18, 16, 14, 4, 1};
382 expected_result = {34, 18, 1, 0, 0};
383 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
384 EXPECT_EQ(compressed_iat, expected_result);
385
386 // Test a 20ms to 120ms change.
387 iat = {18, 12, 5, 4, 4, 3, 5, 1};
388 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
389 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
390 EXPECT_EQ(compressed_iat, expected_result);
391
392 // Test a 70ms to 80ms change.
393 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
394 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
395 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
396 EXPECT_EQ(compressed_iat, expected_result);
397
398 // Test a 50ms to 110ms change.
399 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
400 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
401 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
402 EXPECT_EQ(compressed_iat, expected_result);
403}
404
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100405// Test if the histogram scaling function handles overflows correctly.
406TEST(DelayManagerIATScalingTest, OverflowTest) {
407 using IATVector = DelayManager::IATVector;
408 // Test a compression operation that can cause overflow.
409 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
410 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
411 0, 0, 0, 0, 0, 0, 0};
412 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
413 EXPECT_EQ(scaled_iat, expected_result);
414
415 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
416 114119, 2072996, 0, 2149354, 0};
417 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
418 0, 0, 0, 0, 0};
419 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
420 EXPECT_EQ(scaled_iat, expected_result);
421
422 // In this test case we will not be able to add everything to the final bin in
423 // the scaled histogram. Check that the last bin doesn't overflow.
424 iat = {2000000000, 2000000000, 2000000000,
425 2000000000, 2000000000, 2000000000};
426 expected_result = {666666666, 666666666, 666666666,
427 666666667, 666666667, 2147483647};
428 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
429 EXPECT_EQ(scaled_iat, expected_result);
430
431 // In this test case we will not be able to add enough to each of the bins,
432 // so the values should be smeared out past the end of the normal range.
433 iat = {2000000000, 2000000000, 2000000000,
434 2000000000, 2000000000, 2000000000};
435 expected_result = {2147483647, 2147483647, 2147483647,
436 2147483647, 2147483647, 1262581765};
437 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
438 EXPECT_EQ(scaled_iat, expected_result);
439}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000440} // namespace webrtc