blob: 6bdbc38cf14041fe0be9a977d012797ea727eabb [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
338} // namespace webrtc