2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/modules/audio_coding/main/acm2/initial_delay_manager.h"
22 const uint8_t kAudioPayloadType = 0;
23 const uint8_t kCngPayloadType = 1;
24 const uint8_t kAvtPayloadType = 2;
26 const int kSamplingRateHz = 16000;
27 const int kInitDelayMs = 200;
28 const int kFrameSizeMs = 20;
29 const uint32_t kTimestampStep = kFrameSizeMs * kSamplingRateHz / 1000;
30 const int kLatePacketThreshold = 5;
32 void InitRtpInfo(WebRtcRTPHeader* rtp_info) {
33 memset(rtp_info, 0, sizeof(*rtp_info));
34 rtp_info->header.markerBit = false;
35 rtp_info->header.payloadType = kAudioPayloadType;
36 rtp_info->header.sequenceNumber = 1234;
37 rtp_info->header.timestamp = 0xFFFFFFFD; // Close to wrap around.
38 rtp_info->header.ssrc = 0x87654321; // Arbitrary.
39 rtp_info->header.numCSRCs = 0; // Arbitrary.
40 rtp_info->header.paddingLength = 0;
41 rtp_info->header.headerLength = sizeof(RTPHeader);
42 rtp_info->header.payload_type_frequency = kSamplingRateHz;
43 rtp_info->header.extension.absoluteSendTime = 0;
44 rtp_info->header.extension.transmissionTimeOffset = 0;
45 rtp_info->frameType = kAudioFrameSpeech;
48 void ForwardRtpHeader(int n,
49 WebRtcRTPHeader* rtp_info,
50 uint32_t* rtp_receive_timestamp) {
51 rtp_info->header.sequenceNumber += n;
52 rtp_info->header.timestamp += n * kTimestampStep;
53 *rtp_receive_timestamp += n * kTimestampStep;
56 void NextRtpHeader(WebRtcRTPHeader* rtp_info,
57 uint32_t* rtp_receive_timestamp) {
58 ForwardRtpHeader(1, rtp_info, rtp_receive_timestamp);
63 class InitialDelayManagerTest : public ::testing::Test {
65 InitialDelayManagerTest()
66 : manager_(new InitialDelayManager(kInitDelayMs, kLatePacketThreshold)),
67 rtp_receive_timestamp_(1111) { } // Arbitrary starting point.
69 virtual void SetUp() {
70 ASSERT_TRUE(manager_.get() != NULL);
71 InitRtpInfo(&rtp_info_);
74 void GetNextRtpHeader(WebRtcRTPHeader* rtp_info,
75 uint32_t* rtp_receive_timestamp) const {
76 memcpy(rtp_info, &rtp_info_, sizeof(*rtp_info));
77 *rtp_receive_timestamp = rtp_receive_timestamp_;
78 NextRtpHeader(rtp_info, rtp_receive_timestamp);
81 scoped_ptr<InitialDelayManager> manager_;
82 WebRtcRTPHeader rtp_info_;
83 uint32_t rtp_receive_timestamp_;
86 TEST_F(InitialDelayManagerTest, Init) {
87 EXPECT_TRUE(manager_->buffering());
88 EXPECT_FALSE(manager_->PacketBuffered());
89 manager_->DisableBuffering();
90 EXPECT_FALSE(manager_->buffering());
91 InitialDelayManager::SyncStream sync_stream;
93 // Call before any packet inserted.
94 manager_->LatePackets(0x6789ABCD, &sync_stream); // Arbitrary but large
96 EXPECT_EQ(0, sync_stream.num_sync_packets);
98 // Insert non-audio packets, a CNG and DTMF.
99 rtp_info_.header.payloadType = kCngPayloadType;
100 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
101 InitialDelayManager::kCngPacket, false,
102 kSamplingRateHz, &sync_stream);
103 EXPECT_EQ(0, sync_stream.num_sync_packets);
104 ForwardRtpHeader(5, &rtp_info_, &rtp_receive_timestamp_);
105 rtp_info_.header.payloadType = kAvtPayloadType;
106 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
107 InitialDelayManager::kAvtPacket, false,
108 kSamplingRateHz, &sync_stream);
109 // Gap in sequence numbers but no audio received, sync-stream should be empty.
110 EXPECT_EQ(0, sync_stream.num_sync_packets);
111 manager_->LatePackets(0x45678987, &sync_stream); // Large arbitrary receive
113 // |manager_| has no estimate of timestamp-step and has not received any
115 EXPECT_EQ(0, sync_stream.num_sync_packets);
118 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
119 rtp_info_.header.payloadType = kAudioPayloadType;
121 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
122 InitialDelayManager::kAudioPacket, true,
123 kSamplingRateHz, &sync_stream);
124 EXPECT_EQ(0, sync_stream.num_sync_packets);
126 // Call LatePAcket() after only one packet inserted.
127 manager_->LatePackets(0x6789ABCD, &sync_stream); // Arbitrary but large
128 // receive timestamp.
129 EXPECT_EQ(0, sync_stream.num_sync_packets);
131 // Gap in timestamp, but this packet is also flagged as "new," therefore,
132 // expecting empty sync-stream.
133 ForwardRtpHeader(5, &rtp_info_, &rtp_receive_timestamp_);
134 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
135 InitialDelayManager::kAudioPacket, true,
136 kSamplingRateHz, &sync_stream);
139 TEST_F(InitialDelayManagerTest, MissingPacket) {
140 InitialDelayManager::SyncStream sync_stream;
142 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
143 InitialDelayManager::kAudioPacket, true,
144 kSamplingRateHz, &sync_stream);
145 ASSERT_EQ(0, sync_stream.num_sync_packets);
148 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
149 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
150 InitialDelayManager::kAudioPacket, false,
151 kSamplingRateHz, &sync_stream);
152 ASSERT_EQ(0, sync_stream.num_sync_packets);
154 // Third packet, missing packets start from here.
155 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
157 // First sync-packet in sync-stream is one after the above packet.
158 WebRtcRTPHeader expected_rtp_info;
159 uint32_t expected_receive_timestamp;
160 GetNextRtpHeader(&expected_rtp_info, &expected_receive_timestamp);
162 const int kNumMissingPackets = 10;
163 ForwardRtpHeader(kNumMissingPackets, &rtp_info_, &rtp_receive_timestamp_);
164 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
165 InitialDelayManager::kAudioPacket, false,
166 kSamplingRateHz, &sync_stream);
167 EXPECT_EQ(kNumMissingPackets - 2, sync_stream.num_sync_packets);
168 EXPECT_EQ(0, memcmp(&expected_rtp_info, &sync_stream.rtp_info,
169 sizeof(expected_rtp_info)));
170 EXPECT_EQ(kTimestampStep, sync_stream.timestamp_step);
171 EXPECT_EQ(expected_receive_timestamp, sync_stream.receive_timestamp);
174 // There hasn't been any consecutive packets to estimate timestamp-step.
175 TEST_F(InitialDelayManagerTest, MissingPacketEstimateTimestamp) {
176 InitialDelayManager::SyncStream sync_stream;
178 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
179 InitialDelayManager::kAudioPacket, true,
180 kSamplingRateHz, &sync_stream);
181 ASSERT_EQ(0, sync_stream.num_sync_packets);
183 // Second packet, missing packets start here.
184 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
186 // First sync-packet in sync-stream is one after the above.
187 WebRtcRTPHeader expected_rtp_info;
188 uint32_t expected_receive_timestamp;
189 GetNextRtpHeader(&expected_rtp_info, &expected_receive_timestamp);
191 const int kNumMissingPackets = 10;
192 ForwardRtpHeader(kNumMissingPackets, &rtp_info_, &rtp_receive_timestamp_);
193 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
194 InitialDelayManager::kAudioPacket, false,
195 kSamplingRateHz, &sync_stream);
196 EXPECT_EQ(kNumMissingPackets - 2, sync_stream.num_sync_packets);
197 EXPECT_EQ(0, memcmp(&expected_rtp_info, &sync_stream.rtp_info,
198 sizeof(expected_rtp_info)));
201 TEST_F(InitialDelayManagerTest, MissingPacketWithCng) {
202 InitialDelayManager::SyncStream sync_stream;
205 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
206 InitialDelayManager::kAudioPacket, true,
207 kSamplingRateHz, &sync_stream);
208 ASSERT_EQ(0, sync_stream.num_sync_packets);
210 // Second packet as CNG.
211 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
212 rtp_info_.header.payloadType = kCngPayloadType;
213 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
214 InitialDelayManager::kCngPacket, false,
215 kSamplingRateHz, &sync_stream);
216 ASSERT_EQ(0, sync_stream.num_sync_packets);
218 // Audio packet after CNG. Missing packets start from this packet.
219 rtp_info_.header.payloadType = kAudioPayloadType;
220 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
222 // Timestamps are increased higher than regular packet.
223 const uint32_t kCngTimestampStep = 5 * kTimestampStep;
224 rtp_info_.header.timestamp += kCngTimestampStep;
225 rtp_receive_timestamp_ += kCngTimestampStep;
227 // First sync-packet in sync-stream is the one after the above packet.
228 WebRtcRTPHeader expected_rtp_info;
229 uint32_t expected_receive_timestamp;
230 GetNextRtpHeader(&expected_rtp_info, &expected_receive_timestamp);
232 const int kNumMissingPackets = 10;
233 ForwardRtpHeader(kNumMissingPackets, &rtp_info_, &rtp_receive_timestamp_);
234 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
235 InitialDelayManager::kAudioPacket, false,
236 kSamplingRateHz, &sync_stream);
237 EXPECT_EQ(kNumMissingPackets - 2, sync_stream.num_sync_packets);
238 EXPECT_EQ(0, memcmp(&expected_rtp_info, &sync_stream.rtp_info,
239 sizeof(expected_rtp_info)));
240 EXPECT_EQ(kTimestampStep, sync_stream.timestamp_step);
241 EXPECT_EQ(expected_receive_timestamp, sync_stream.receive_timestamp);
244 TEST_F(InitialDelayManagerTest, LatePacket) {
245 InitialDelayManager::SyncStream sync_stream;
247 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
248 InitialDelayManager::kAudioPacket, true,
249 kSamplingRateHz, &sync_stream);
250 ASSERT_EQ(0, sync_stream.num_sync_packets);
253 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
254 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
255 InitialDelayManager::kAudioPacket, false,
256 kSamplingRateHz, &sync_stream);
257 ASSERT_EQ(0, sync_stream.num_sync_packets);
259 // Timestamp increment for 10ms;
260 const uint32_t kTimestampStep10Ms = kSamplingRateHz / 100;
262 // 10 ms after the second packet is inserted.
263 uint32_t timestamp_now = rtp_receive_timestamp_ + kTimestampStep10Ms;
265 // Third packet, late packets start from this packet.
266 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
268 // First sync-packet in sync-stream, which is one after the above packet.
269 WebRtcRTPHeader expected_rtp_info;
270 uint32_t expected_receive_timestamp;
271 GetNextRtpHeader(&expected_rtp_info, &expected_receive_timestamp);
273 const int kLatePacketThreshold = 5;
275 int expected_num_late_packets = kLatePacketThreshold - 1;
276 for (int k = 0; k < 2; ++k) {
277 for (int n = 1; n < kLatePacketThreshold * kFrameSizeMs / 10; ++n) {
278 manager_->LatePackets(timestamp_now, &sync_stream);
279 EXPECT_EQ(0, sync_stream.num_sync_packets) <<
280 "try " << k << " loop number " << n;
281 timestamp_now += kTimestampStep10Ms;
283 manager_->LatePackets(timestamp_now, &sync_stream);
285 EXPECT_EQ(expected_num_late_packets, sync_stream.num_sync_packets) <<
287 EXPECT_EQ(kTimestampStep, sync_stream.timestamp_step) <<
289 EXPECT_EQ(expected_receive_timestamp, sync_stream.receive_timestamp) <<
291 EXPECT_EQ(0, memcmp(&expected_rtp_info, &sync_stream.rtp_info,
292 sizeof(expected_rtp_info)));
294 timestamp_now += kTimestampStep10Ms;
296 // |manger_| assumes the |sync_stream| obtained by LatePacket() is fully
297 // injected. The last injected packet is sync-packet, therefore, there will
298 // not be any gap between sync stream of this and the next iteration.
299 ForwardRtpHeader(sync_stream.num_sync_packets, &expected_rtp_info,
300 &expected_receive_timestamp);
301 expected_num_late_packets = kLatePacketThreshold;
304 // Test "no-gap" for missing packet after late packet.
305 // |expected_rtp_info| is the expected sync-packet if any packet is missing.
306 memcpy(&rtp_info_, &expected_rtp_info, sizeof(rtp_info_));
307 rtp_receive_timestamp_ = expected_receive_timestamp;
309 int kNumMissingPackets = 3; // Arbitrary.
310 ForwardRtpHeader(kNumMissingPackets, &rtp_info_, &rtp_receive_timestamp_);
311 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
312 InitialDelayManager::kAudioPacket, false,
313 kSamplingRateHz, &sync_stream);
315 // Note that there is one packet gap between the last sync-packet and the
316 // latest inserted packet.
317 EXPECT_EQ(kNumMissingPackets - 1, sync_stream.num_sync_packets);
318 EXPECT_EQ(kTimestampStep, sync_stream.timestamp_step);
319 EXPECT_EQ(expected_receive_timestamp, sync_stream.receive_timestamp);
320 EXPECT_EQ(0, memcmp(&expected_rtp_info, &sync_stream.rtp_info,
321 sizeof(expected_rtp_info)));
324 TEST_F(InitialDelayManagerTest, NoLatePacketAfterCng) {
325 InitialDelayManager::SyncStream sync_stream;
328 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
329 InitialDelayManager::kAudioPacket, true,
330 kSamplingRateHz, &sync_stream);
331 ASSERT_EQ(0, sync_stream.num_sync_packets);
333 // Second packet as CNG.
334 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
335 const uint8_t kCngPayloadType = 1; // Arbitrary.
336 rtp_info_.header.payloadType = kCngPayloadType;
337 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
338 InitialDelayManager::kCngPacket, false,
339 kSamplingRateHz, &sync_stream);
340 ASSERT_EQ(0, sync_stream.num_sync_packets);
342 // Forward the time more then |kLatePacketThreshold| packets.
343 uint32_t timestamp_now = rtp_receive_timestamp_ + kTimestampStep * (3 +
344 kLatePacketThreshold);
346 manager_->LatePackets(timestamp_now, &sync_stream);
347 EXPECT_EQ(0, sync_stream.num_sync_packets);
350 TEST_F(InitialDelayManagerTest, BufferingAudio) {
351 InitialDelayManager::SyncStream sync_stream;
353 // Very first packet is not counted in calculation of buffered audio.
354 for (int n = 0; n < kInitDelayMs / kFrameSizeMs; ++n) {
355 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
356 InitialDelayManager::kAudioPacket,
357 n == 0, kSamplingRateHz, &sync_stream);
358 EXPECT_EQ(0, sync_stream.num_sync_packets);
359 EXPECT_TRUE(manager_->buffering());
360 const uint32_t expected_playout_timestamp = rtp_info_.header.timestamp -
361 kInitDelayMs * kSamplingRateHz / 1000;
362 uint32_t actual_playout_timestamp = 0;
363 EXPECT_TRUE(manager_->GetPlayoutTimestamp(&actual_playout_timestamp));
364 EXPECT_EQ(expected_playout_timestamp, actual_playout_timestamp);
365 NextRtpHeader(&rtp_info_, &rtp_receive_timestamp_);
368 manager_->UpdateLastReceivedPacket(rtp_info_, rtp_receive_timestamp_,
369 InitialDelayManager::kAudioPacket,
370 false, kSamplingRateHz, &sync_stream);
371 EXPECT_EQ(0, sync_stream.num_sync_packets);
372 EXPECT_FALSE(manager_->buffering());
377 } // namespace webrtc