blob: f9c56800178fa91d739e740d7b5f397a4f82be96 [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
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000203TEST_F(DelayManagerTest, MaxAndRequiredDelay) {
204 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();
227 EXPECT_EQ(kExpectedTarget * kFrameSizeMs, dm_->least_required_delay_ms());
228 EXPECT_EQ(kMaxDelayPackets << 8, dm_->TargetLevel());
229
230 // Target level at least should be one packet.
231 EXPECT_FALSE(dm_->SetMaximumDelay(kFrameSizeMs - 1));
232}
233
234TEST_F(DelayManagerTest, MinAndRequiredDelay) {
235 const int kExpectedTarget = 5;
236 const int kTimeIncrement = kExpectedTarget * kFrameSizeMs;
237 SetPacketAudioLength(kFrameSizeMs);
238 // First packet arrival.
239 InsertNextPacket();
240 // Second packet arrival.
241 // Expect detector update method to be called once with inter-arrival time
242 // equal to |kExpectedTarget| packet. Return true to indicate peaks found.
243 EXPECT_CALL(detector_, Update(kExpectedTarget, _))
244 .WillRepeatedly(Return(true));
245 EXPECT_CALL(detector_, MaxPeakHeight())
246 .WillRepeatedly(Return(kExpectedTarget));
247 IncreaseTime(kTimeIncrement);
248 InsertNextPacket();
249
250 // No limit is applied.
251 EXPECT_EQ(kExpectedTarget << 8, dm_->TargetLevel());
252
253 int kMinDelayPackets = kExpectedTarget + 2;
254 int kMinDelayMs = kMinDelayPackets * kFrameSizeMs;
255 dm_->SetMinimumDelay(kMinDelayMs);
256 IncreaseTime(kTimeIncrement);
257 InsertNextPacket();
258 EXPECT_EQ(kExpectedTarget * kFrameSizeMs, dm_->least_required_delay_ms());
259 EXPECT_EQ(kMinDelayPackets << 8, dm_->TargetLevel());
260}
261
henrik.lundinb8c55b12017-05-10 07:38:01 -0700262// Tests that skipped sequence numbers (simulating empty packets) are handled
263// correctly.
264TEST_F(DelayManagerTest, EmptyPacketsReported) {
265 SetPacketAudioLength(kFrameSizeMs);
266 // First packet arrival.
267 InsertNextPacket();
268
269 // Advance time by one frame size.
270 IncreaseTime(kFrameSizeMs);
271
272 // Advance the sequence number by 5, simulating that 5 empty packets were
273 // received, but never inserted.
274 seq_no_ += 10;
275 for (int j = 0; j < 10; ++j) {
276 dm_->RegisterEmptyPacket();
277 }
278
279 // Second packet arrival.
280 // Expect detector update method to be called once with inter-arrival time
281 // equal to 1 packet, and (base) target level equal to 1 as well.
282 // Return false to indicate no peaks found.
283 EXPECT_CALL(detector_, Update(1, 1)).WillOnce(Return(false));
284 InsertNextPacket();
285
286 EXPECT_EQ(1 << 8, dm_->TargetLevel()); // In Q8.
287}
288
289// Same as above, but do not call RegisterEmptyPacket. Observe the target level
290// increase dramatically.
291TEST_F(DelayManagerTest, EmptyPacketsNotReported) {
292 SetPacketAudioLength(kFrameSizeMs);
293 // First packet arrival.
294 InsertNextPacket();
295
296 // Advance time by one frame size.
297 IncreaseTime(kFrameSizeMs);
298
299 // Advance the sequence number by 5, simulating that 5 empty packets were
300 // received, but never inserted.
301 seq_no_ += 10;
302
303 // Second packet arrival.
304 // Expect detector update method to be called once with inter-arrival time
305 // equal to 1 packet, and (base) target level equal to 1 as well.
306 // Return false to indicate no peaks found.
307 EXPECT_CALL(detector_, Update(10, 10)).WillOnce(Return(false));
308 InsertNextPacket();
309
310 // Note 10 times higher target value.
311 EXPECT_EQ(10 * 1 << 8, dm_->TargetLevel()); // In Q8.
312}
313
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000314TEST_F(DelayManagerTest, Failures) {
315 // Wrong sample rate.
316 EXPECT_EQ(-1, dm_->Update(0, 0, -1));
317 // Wrong packet size.
318 EXPECT_EQ(-1, dm_->SetPacketAudioLength(0));
319 EXPECT_EQ(-1, dm_->SetPacketAudioLength(-1));
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000320
321 // Minimum delay higher than a maximum delay is not accepted.
322 EXPECT_TRUE(dm_->SetMaximumDelay(10));
323 EXPECT_FALSE(dm_->SetMinimumDelay(20));
324
325 // Maximum delay less than minimum delay is not accepted.
326 EXPECT_TRUE(dm_->SetMaximumDelay(100));
327 EXPECT_TRUE(dm_->SetMinimumDelay(80));
328 EXPECT_FALSE(dm_->SetMaximumDelay(60));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329}
330
Ivo Creusen385b10b2017-10-13 12:37:27 +0200331// Test if the histogram is stretched correctly if the packet size is decreased.
332TEST(DelayManagerIATScalingTest, StretchTest) {
333 using IATVector = DelayManager::IATVector;
334 // Test a straightforward 60ms to 20ms change.
335 IATVector iat = {12, 0, 0, 0, 0, 0};
336 IATVector expected_result = {4, 4, 4, 0, 0, 0};
337 IATVector stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
338 EXPECT_EQ(stretched_iat, expected_result);
339
340 // Test an example where the last bin in the stretched histogram should
341 // contain the sum of the elements that don't fit into the new histogram.
342 iat = {18, 15, 12, 9, 6, 3, 0};
343 expected_result = {6, 6, 6, 5, 5, 5, 30};
344 stretched_iat = DelayManager::ScaleHistogram(iat, 60, 20);
345 EXPECT_EQ(stretched_iat, expected_result);
346
347 // Test a 120ms to 60ms change.
348 iat = {18, 16, 14, 4, 0};
349 expected_result = {9, 9, 8, 8, 18};
350 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 60);
351 EXPECT_EQ(stretched_iat, expected_result);
352
353 // Test a 120ms to 20ms change.
354 iat = {19, 12, 0, 0, 0, 0, 0, 0};
355 expected_result = {3, 3, 3, 3, 3, 3, 2, 11};
356 stretched_iat = DelayManager::ScaleHistogram(iat, 120, 20);
357 EXPECT_EQ(stretched_iat, expected_result);
358
359 // Test a 70ms to 40ms change.
360 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
361 expected_result = {7, 5, 5, 3, 3, 2, 2, 1, 2, 2, 6, 22};
362 stretched_iat = DelayManager::ScaleHistogram(iat, 70, 40);
363 EXPECT_EQ(stretched_iat, expected_result);
364
365 // Test a 30ms to 20ms change.
366 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3, 0, 0, 0};
367 expected_result = {8, 6, 6, 3, 2, 2, 1, 3, 3, 8, 7, 11};
368 stretched_iat = DelayManager::ScaleHistogram(iat, 30, 20);
369 EXPECT_EQ(stretched_iat, expected_result);
370}
371
372// Test if the histogram is compressed correctly if the packet size is
373// increased.
374TEST(DelayManagerIATScalingTest, CompressionTest) {
375 using IATVector = DelayManager::IATVector;
376 // Test a 20 to 60 ms change.
377 IATVector iat = {12, 11, 10, 3, 2, 1};
378 IATVector expected_result = {33, 6, 0, 0, 0, 0};
379 IATVector compressed_iat = DelayManager::ScaleHistogram(iat, 20, 60);
380 EXPECT_EQ(compressed_iat, expected_result);
381
382 // Test a 60ms to 120ms change.
383 iat = {18, 16, 14, 4, 1};
384 expected_result = {34, 18, 1, 0, 0};
385 compressed_iat = DelayManager::ScaleHistogram(iat, 60, 120);
386 EXPECT_EQ(compressed_iat, expected_result);
387
388 // Test a 20ms to 120ms change.
389 iat = {18, 12, 5, 4, 4, 3, 5, 1};
390 expected_result = {46, 6, 0, 0, 0, 0, 0, 0};
391 compressed_iat = DelayManager::ScaleHistogram(iat, 20, 120);
392 EXPECT_EQ(compressed_iat, expected_result);
393
394 // Test a 70ms to 80ms change.
395 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
396 expected_result = {11, 8, 6, 2, 5, 12, 13, 3, 0};
397 compressed_iat = DelayManager::ScaleHistogram(iat, 70, 80);
398 EXPECT_EQ(compressed_iat, expected_result);
399
400 // Test a 50ms to 110ms change.
401 iat = {13, 7, 5, 3, 1, 5, 12, 11, 3};
402 expected_result = {18, 8, 16, 16, 2, 0, 0, 0, 0};
403 compressed_iat = DelayManager::ScaleHistogram(iat, 50, 110);
404 EXPECT_EQ(compressed_iat, expected_result);
405}
406
Ivo Creusend95a7dd2017-12-11 16:47:48 +0100407// Test if the histogram scaling function handles overflows correctly.
408TEST(DelayManagerIATScalingTest, OverflowTest) {
409 using IATVector = DelayManager::IATVector;
410 // Test a compression operation that can cause overflow.
411 IATVector iat = {733544448, 0, 0, 0, 0, 0, 0, 340197376, 0, 0, 0, 0, 0, 0};
412 IATVector expected_result = {733544448, 340197376, 0, 0, 0, 0, 0,
413 0, 0, 0, 0, 0, 0, 0};
414 IATVector scaled_iat = DelayManager::ScaleHistogram(iat, 10, 60);
415 EXPECT_EQ(scaled_iat, expected_result);
416
417 iat = {655591163, 39962288, 360736736, 1930514, 4003853, 1782764,
418 114119, 2072996, 0, 2149354, 0};
419 expected_result = {1056290187, 7717131, 2187115, 2149354, 0, 0,
420 0, 0, 0, 0, 0};
421 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
422 EXPECT_EQ(scaled_iat, expected_result);
423
424 // In this test case we will not be able to add everything to the final bin in
425 // the scaled histogram. Check that the last bin doesn't overflow.
426 iat = {2000000000, 2000000000, 2000000000,
427 2000000000, 2000000000, 2000000000};
428 expected_result = {666666666, 666666666, 666666666,
429 666666667, 666666667, 2147483647};
430 scaled_iat = DelayManager::ScaleHistogram(iat, 60, 20);
431 EXPECT_EQ(scaled_iat, expected_result);
432
433 // In this test case we will not be able to add enough to each of the bins,
434 // so the values should be smeared out past the end of the normal range.
435 iat = {2000000000, 2000000000, 2000000000,
436 2000000000, 2000000000, 2000000000};
437 expected_result = {2147483647, 2147483647, 2147483647,
438 2147483647, 2147483647, 1262581765};
439 scaled_iat = DelayManager::ScaleHistogram(iat, 20, 60);
440 EXPECT_EQ(scaled_iat, expected_result);
441}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000442} // namespace webrtc