blob: 953bc6b4002301db63eaa4f5e5d8fe8ca7ab810d [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() {
52 EXPECT_CALL(detector_, Reset())
53 .Times(1);
henrik.lundin8f8c96d2016-04-28 23:19:20 -070054 dm_ = new DelayManager(kMaxNumberOfPackets, &detector_, &tick_timer_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055}
56
57void DelayManagerTest::SetPacketAudioLength(int lengt_ms) {
58 EXPECT_CALL(detector_, SetPacketAudioLength(lengt_ms));
59 dm_->SetPacketAudioLength(lengt_ms);
60}
61
62void DelayManagerTest::InsertNextPacket() {
63 EXPECT_EQ(0, dm_->Update(seq_no_, ts_, kFs));
64 seq_no_ += 1;
65 ts_ += kTsIncrement;
66}
67
68void DelayManagerTest::IncreaseTime(int inc_ms) {
69 for (int t = 0; t < inc_ms; t += kTimeStepMs) {
henrik.lundinf3933702016-04-28 01:53:52 -070070 tick_timer_.Increment();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071 }
72}
73void DelayManagerTest::TearDown() {
74 EXPECT_CALL(detector_, Die());
75 delete dm_;
76}
77
78TEST_F(DelayManagerTest, CreateAndDestroy) {
79 // Nothing to do here. The test fixture creates and destroys the DelayManager
80 // object.
81}
82
83TEST_F(DelayManagerTest, VectorInitialization) {
84 const DelayManager::IATVector& vec = dm_->iat_vector();
85 double sum = 0.0;
86 for (size_t i = 0; i < vec.size(); i++) {
Henrik Lundincb3e8fe2015-05-11 15:15:51 +020087 EXPECT_NEAR(ldexp(pow(0.5, static_cast<int>(i + 1)), 30), vec[i], 65537);
88 // Tolerance 65537 in Q30 corresponds to a delta of approximately 0.00006.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 sum += vec[i];
90 }
91 EXPECT_EQ(1 << 30, static_cast<int>(sum)); // Should be 1 in Q30.
92}
93
94TEST_F(DelayManagerTest, SetPacketAudioLength) {
95 const int kLengthMs = 30;
96 // Expect DelayManager to pass on the new length to the detector object.
97 EXPECT_CALL(detector_, SetPacketAudioLength(kLengthMs))
98 .Times(1);
99 EXPECT_EQ(0, dm_->SetPacketAudioLength(kLengthMs));
100 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1)); // Illegal parameter value.
101}
102
103TEST_F(DelayManagerTest, PeakFound) {
104 // Expect DelayManager to pass on the question to the detector.
105 // Call twice, and let the detector return true the first time and false the
106 // second time.
107 EXPECT_CALL(detector_, peak_found())
108 .WillOnce(Return(true))
109 .WillOnce(Return(false));
110 EXPECT_TRUE(dm_->PeakFound());
111 EXPECT_FALSE(dm_->PeakFound());
112}
113
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000114TEST_F(DelayManagerTest, UpdateNormal) {
115 SetPacketAudioLength(kFrameSizeMs);
116 // First packet arrival.
117 InsertNextPacket();
118 // Advance time by one frame size.
119 IncreaseTime(kFrameSizeMs);
120 // Second packet arrival.
121 // Expect detector update method to be called once with inter-arrival time
122 // equal to 1 packet, and (base) target level equal to 1 as well.
123 // Return false to indicate no peaks found.
124 EXPECT_CALL(detector_, Update(1, 1))
125 .WillOnce(Return(false));
126 InsertNextPacket();
127 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
128 EXPECT_EQ(1, dm_->base_target_level());
129 int lower, higher;
130 dm_->BufferLimits(&lower, &higher);
131 // Expect |lower| to be 75% of target level, and |higher| to be target level,
132 // but also at least 20 ms higher than |lower|, which is the limiting case
133 // here.
134 EXPECT_EQ((1 << 8) * 3 / 4, lower);
135 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
136}
137
138TEST_F(DelayManagerTest, UpdateLongInterArrivalTime) {
139 SetPacketAudioLength(kFrameSizeMs);
140 // First packet arrival.
141 InsertNextPacket();
142 // Advance time by two frame size.
143 IncreaseTime(2 * kFrameSizeMs);
144 // Second packet arrival.
145 // Expect detector update method to be called once with inter-arrival time
146 // equal to 1 packet, and (base) target level equal to 1 as well.
147 // Return false to indicate no peaks found.
148 EXPECT_CALL(detector_, Update(2, 2))
149 .WillOnce(Return(false));
150 InsertNextPacket();
151 EXPECT_EQ(2 << 8, dm_->TargetLevel()); // In Q8.
152 EXPECT_EQ(2, dm_->base_target_level());
153 int lower, higher;
154 dm_->BufferLimits(&lower, &higher);
155 // Expect |lower| to be 75% of target level, and |higher| to be target level,
156 // but also at least 20 ms higher than |lower|, which is the limiting case
157 // here.
158 EXPECT_EQ((2 << 8) * 3 / 4, lower);
159 EXPECT_EQ(lower + (20 << 8) / kFrameSizeMs, higher);
160}
161
162TEST_F(DelayManagerTest, UpdatePeakFound) {
163 SetPacketAudioLength(kFrameSizeMs);
164 // First packet arrival.
165 InsertNextPacket();
166 // Advance time by one frame size.
167 IncreaseTime(kFrameSizeMs);
168 // Second packet arrival.
169 // Expect detector update method to be called once with inter-arrival time
170 // equal to 1 packet, and (base) target level equal to 1 as well.
171 // Return true to indicate that peaks are found. Let the peak height be 5.
172 EXPECT_CALL(detector_, Update(1, 1))
173 .WillOnce(Return(true));
174 EXPECT_CALL(detector_, MaxPeakHeight())
175 .WillOnce(Return(5));
176 InsertNextPacket();
177 EXPECT_EQ(5 << 8, dm_->TargetLevel());
178 EXPECT_EQ(1, dm_->base_target_level()); // Base target level is w/o peaks.
179 int lower, higher;
180 dm_->BufferLimits(&lower, &higher);
181 // Expect |lower| to be 75% of target level, and |higher| to be target level.
182 EXPECT_EQ((5 << 8) * 3 / 4, lower);
183 EXPECT_EQ(5 << 8, higher);
184}
185
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000186TEST_F(DelayManagerTest, TargetDelay) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 SetPacketAudioLength(kFrameSizeMs);
188 // First packet arrival.
189 InsertNextPacket();
190 // Advance time by one frame size.
191 IncreaseTime(kFrameSizeMs);
192 // Second packet arrival.
193 // Expect detector update method to be called once with inter-arrival time
194 // equal to 1 packet, and (base) target level equal to 1 as well.
195 // Return false to indicate no peaks found.
196 EXPECT_CALL(detector_, Update(1, 1))
197 .WillOnce(Return(false));
198 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
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000210TEST_F(DelayManagerTest, MaxAndRequiredDelay) {
211 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();
234 EXPECT_EQ(kExpectedTarget * kFrameSizeMs, dm_->least_required_delay_ms());
235 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
236
237 // Target level at least should be one packet.
238 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
239}
240
241TEST_F(DelayManagerTest, MinAndRequiredDelay) {
242 const int kExpectedTarget = 5;
243 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
244 SetPacketAudioLength(kFrameSizeMs);
245 // First packet arrival.
246 InsertNextPacket();
247 // Second packet arrival.
248 // Expect detector update method to be called once with inter-arrival time
249 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
250 EXPECT_CALL(detector_, Update(kExpectedTarget, _))
251 .WillRepeatedly(Return(true));
252 EXPECT_CALL(detector_, MaxPeakHeight())
253 .WillRepeatedly(Return(kExpectedTarget));
254 IncreaseTime(kTimeIncrement);
255 InsertNextPacket();
256
257 // No limit is applied.
258 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
259
260 int kMinDelayPackets = kExpectedTarget + 2;
261 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
262 dm_->SetMinimumDelay(kMinDelayMs);
263 IncreaseTime(kTimeIncrement);
264 InsertNextPacket();
265 EXPECT_EQ(kExpectedTarget * kFrameSizeMs, dm_->least_required_delay_ms());
266 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
267}
268
henrik.lundinb8c55b12017-05-10 07:38:01 -0700269// Tests that skipped sequence numbers (simulating empty packets) are handled
270// correctly.
271TEST_F(DelayManagerTest, EmptyPacketsReported) {
272 SetPacketAudioLength(kFrameSizeMs);
273 // First packet arrival.
274 InsertNextPacket();
275
276 // Advance time by one frame size.
277 IncreaseTime(kFrameSizeMs);
278
279 // Advance the sequence number by 5, simulating that 5 empty packets were
280 // received, but never inserted.
281 seq_no_ += 10;
282 for (int j = 0; j < 10; ++j) {
283 dm_->RegisterEmptyPacket();
284 }
285
286 // Second packet arrival.
287 // Expect detector update method to be called once with inter-arrival time
288 // equal to 1 packet, and (base) target level equal to 1 as well.
289 // Return false to indicate no peaks found.
290 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
291 InsertNextPacket();
292
293 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
294}
295
296// Same as above, but do not call RegisterEmptyPacket. Observe the target level
297// increase dramatically.
298TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
299 SetPacketAudioLength(kFrameSizeMs);
300 // First packet arrival.
301 InsertNextPacket();
302
303 // Advance time by one frame size.
304 IncreaseTime(kFrameSizeMs);
305
306 // Advance the sequence number by 5, simulating that 5 empty packets were
307 // received, but never inserted.
308 seq_no_ += 10;
309
310 // Second packet arrival.
311 // Expect detector update method to be called once with inter-arrival time
312 // equal to 1 packet, and (base) target level equal to 1 as well.
313 // Return false to indicate no peaks found.
314 EXPECT_CALL(detector_, Update(10, 10)).WillOnce(Return(false));
315 InsertNextPacket();
316
317 // Note 10 times higher target value.
318 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
319}
320
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321TEST_F(DelayManagerTest, Failures) {
322 // Wrong sample rate.
323 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
324 // Wrong packet size.
325 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
326 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000327
328 // Minimum delay higher than a maximum delay is not accepted.
329 EXPECT_TRUE(dm_->SetMaximumDelay(10));
330 EXPECT_FALSE(dm_->SetMinimumDelay(20));
331
332 // Maximum delay less than minimum delay is not accepted.
333 EXPECT_TRUE(dm_->SetMaximumDelay(100));
334 EXPECT_TRUE(dm_->SetMinimumDelay(80));
335 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336}
337
Ivo Creusen385b10b2017-10-13 12:37:27 +0200338// Test if the histogram is stretched correctly if the packet size is decreased.
339TEST(DelayManagerIATScalingTest, StretchTest) {
340 using IATVector = DelayManager::IATVector;
341 // Test a straightforward 60ms to 20ms change.
342 IATVector iat = {12, 0, 0, 0, 0, 0};
343 IATVector expected_result = {4, 4, 4, 0, 0, 0};
344 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
345 EXPECT_EQ(stretched_iat, expected_result);
346
347 // Test an example where the last bin in the stretched histogram should
348 // contain the sum of the elements that don't fit into the new histogram.
349 iat = {18, 15, 12, 9, 6, 3, 0};
350 expected_result = {6, 6, 6, 5, 5, 5, 30};
351 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
352 EXPECT_EQ(stretched_iat, expected_result);
353
354 // Test a 120ms to 60ms change.
355 iat = {18, 16, 14, 4, 0};
356 expected_result = {9, 9, 8, 8, 18};
357 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
358 EXPECT_EQ(stretched_iat, expected_result);
359
360 // Test a 120ms to 20ms change.
361 iat = {19, 12, 0, 0, 0, 0, 0, 0};
362 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
363 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
364 EXPECT_EQ(stretched_iat, expected_result);
365
366 // Test a 70ms to 40ms change.
367 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
368 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
369 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
370 EXPECT_EQ(stretched_iat, expected_result);
371
372 // Test a 30ms to 20ms change.
373 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
374 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
375 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
376 EXPECT_EQ(stretched_iat, expected_result);
377}
378
379// Test if the histogram is compressed correctly if the packet size is
380// increased.
381TEST(DelayManagerIATScalingTest, CompressionTest) {
382 using IATVector = DelayManager::IATVector;
383 // Test a 20 to 60 ms change.
384 IATVector iat = {12, 11, 10, 3, 2, 1};
385 IATVector expected_result = {33, 6, 0, 0, 0, 0};
386 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
387 EXPECT_EQ(compressed_iat, expected_result);
388
389 // Test a 60ms to 120ms change.
390 iat = {18, 16, 14, 4, 1};
391 expected_result = {34, 18, 1, 0, 0};
392 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
393 EXPECT_EQ(compressed_iat, expected_result);
394
395 // Test a 20ms to 120ms change.
396 iat = {18, 12, 5, 4, 4, 3, 5, 1};
397 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
398 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
399 EXPECT_EQ(compressed_iat, expected_result);
400
401 // Test a 70ms to 80ms change.
402 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
403 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
404 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
405 EXPECT_EQ(compressed_iat, expected_result);
406
407 // Test a 50ms to 110ms change.
408 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
409 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
410 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
411 EXPECT_EQ(compressed_iat, expected_result);
412}
413
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100414// Test if the histogram scaling function handles overflows correctly.
415TEST(DelayManagerIATScalingTest, OverflowTest) {
416 using IATVector = DelayManager::IATVector;
417 // Test a compression operation that can cause overflow.
418 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
419 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
420 0, 0, 0, 0, 0, 0, 0};
421 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
422 EXPECT_EQ(scaled_iat, expected_result);
423
424 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
425 114119, 2072996, 0, 2149354, 0};
426 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
427 0, 0, 0, 0, 0};
428 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
429 EXPECT_EQ(scaled_iat, expected_result);
430
431 // In this test case we will not be able to add everything to the final bin in
432 // the scaled histogram. Check that the last bin doesn't overflow.
433 iat = {2000000000, 2000000000, 2000000000,
434 2000000000, 2000000000, 2000000000};
435 expected_result = {666666666, 666666666, 666666666,
436 666666667, 666666667, 2147483647};
437 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
438 EXPECT_EQ(scaled_iat, expected_result);
439
440 // In this test case we will not be able to add enough to each of the bins,
441 // so the values should be smeared out past the end of the normal range.
442 iat = {2000000000, 2000000000, 2000000000,
443 2000000000, 2000000000, 2000000000};
444 expected_result = {2147483647, 2147483647, 2147483647,
445 2147483647, 2147483647, 1262581765};
446 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
447 EXPECT_EQ(scaled_iat, expected_result);
448}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000449} // namespace webrtc