1 /*
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 PacketBuffer class.
12
13 #include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
14
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 #include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h"
18 #include "webrtc/modules/audio_coding/neteq/packet.h"
19
20 using ::testing::Return;
21 using ::testing::_;
22
23 namespace webrtc {
24
25 // Helper class to generate packets. Packets must be deleted by the user.
26 class PacketGenerator {
27 public:
28 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
~PacketGenerator()29 virtual ~PacketGenerator() {}
30 Packet* NextPacket(int payload_size_bytes);
31 void SkipPacket();
32
33 uint16_t seq_no_;
34 uint32_t ts_;
35 uint8_t pt_;
36 int frame_size_;
37 };
38
PacketGenerator(uint16_t seq_no,uint32_t ts,uint8_t pt,int frame_size)39 PacketGenerator::PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt,
40 int frame_size)
41 : seq_no_(seq_no),
42 ts_(ts),
43 pt_(pt),
44 frame_size_(frame_size) {
45 }
46
NextPacket(int payload_size_bytes)47 Packet* PacketGenerator::NextPacket(int payload_size_bytes) {
48 Packet* packet = new Packet;
49 packet->header.sequenceNumber = seq_no_;
50 packet->header.timestamp = ts_;
51 packet->header.payloadType = pt_;
52 packet->header.markerBit = false;
53 packet->header.ssrc = 0x12345678;
54 packet->header.numCSRCs = 0;
55 packet->header.paddingLength = 0;
56 packet->payload_length = payload_size_bytes;
57 packet->primary = true;
58 packet->payload = new uint8_t[payload_size_bytes];
59 ++seq_no_;
60 ts_ += frame_size_;
61 return packet;
62 }
63
SkipPacket()64 void PacketGenerator::SkipPacket() {
65 ++seq_no_;
66 ts_ += frame_size_;
67 }
68
69
70 // Start of test definitions.
71
TEST(PacketBuffer,CreateAndDestroy)72 TEST(PacketBuffer, CreateAndDestroy) {
73 PacketBuffer* buffer = new PacketBuffer(10); // 10 packets.
74 EXPECT_TRUE(buffer->Empty());
75 delete buffer;
76 }
77
TEST(PacketBuffer,InsertPacket)78 TEST(PacketBuffer, InsertPacket) {
79 PacketBuffer buffer(10); // 10 packets.
80 PacketGenerator gen(17u, 4711u, 0, 10);
81
82 const int payload_len = 100;
83 Packet* packet = gen.NextPacket(payload_len);
84
85 EXPECT_EQ(0, buffer.InsertPacket(packet));
86 uint32_t next_ts;
87 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
88 EXPECT_EQ(4711u, next_ts);
89 EXPECT_FALSE(buffer.Empty());
90 EXPECT_EQ(1, buffer.NumPacketsInBuffer());
91 const RTPHeader* hdr = buffer.NextRtpHeader();
92 EXPECT_EQ(&(packet->header), hdr); // Compare pointer addresses.
93
94 // Do not explicitly flush buffer or delete packet to test that it is deleted
95 // with the buffer. (Tested with Valgrind or similar tool.)
96 }
97
98 // Test to flush buffer.
TEST(PacketBuffer,FlushBuffer)99 TEST(PacketBuffer, FlushBuffer) {
100 PacketBuffer buffer(10); // 10 packets.
101 PacketGenerator gen(0, 0, 0, 10);
102 const int payload_len = 10;
103
104 // Insert 10 small packets; should be ok.
105 for (int i = 0; i < 10; ++i) {
106 Packet* packet = gen.NextPacket(payload_len);
107 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
108 }
109 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
110 EXPECT_FALSE(buffer.Empty());
111
112 buffer.Flush();
113 // Buffer should delete the payloads itself.
114 EXPECT_EQ(0, buffer.NumPacketsInBuffer());
115 EXPECT_TRUE(buffer.Empty());
116 }
117
118 // Test to fill the buffer over the limits, and verify that it flushes.
TEST(PacketBuffer,OverfillBuffer)119 TEST(PacketBuffer, OverfillBuffer) {
120 PacketBuffer buffer(10); // 10 packets.
121 PacketGenerator gen(0, 0, 0, 10);
122
123 // Insert 10 small packets; should be ok.
124 const int payload_len = 10;
125 int i;
126 for (i = 0; i < 10; ++i) {
127 Packet* packet = gen.NextPacket(payload_len);
128 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
129 }
130 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
131 uint32_t next_ts;
132 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
133 EXPECT_EQ(0u, next_ts); // Expect first inserted packet to be first in line.
134
135 // Insert 11th packet; should flush the buffer and insert it after flushing.
136 Packet* packet = gen.NextPacket(payload_len);
137 EXPECT_EQ(PacketBuffer::kFlushed, buffer.InsertPacket(packet));
138 EXPECT_EQ(1, buffer.NumPacketsInBuffer());
139 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
140 // Expect last inserted packet to be first in line.
141 EXPECT_EQ(packet->header.timestamp, next_ts);
142
143 // Flush buffer to delete all packets.
144 buffer.Flush();
145 }
146
147 // Test inserting a list of packets.
TEST(PacketBuffer,InsertPacketList)148 TEST(PacketBuffer, InsertPacketList) {
149 PacketBuffer buffer(10); // 10 packets.
150 PacketGenerator gen(0, 0, 0, 10);
151 PacketList list;
152 const int payload_len = 10;
153
154 // Insert 10 small packets.
155 for (int i = 0; i < 10; ++i) {
156 Packet* packet = gen.NextPacket(payload_len);
157 list.push_back(packet);
158 }
159
160 MockDecoderDatabase decoder_database;
161 EXPECT_CALL(decoder_database, IsComfortNoise(0))
162 .WillRepeatedly(Return(false));
163 EXPECT_CALL(decoder_database, IsDtmf(0))
164 .WillRepeatedly(Return(false));
165 uint8_t current_pt = 0xFF;
166 uint8_t current_cng_pt = 0xFF;
167 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
168 decoder_database,
169 ¤t_pt,
170 ¤t_cng_pt));
171 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
172 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
173 EXPECT_EQ(0, current_pt); // Current payload type changed to 0.
174 EXPECT_EQ(0xFF, current_cng_pt); // CNG payload type not changed.
175
176 buffer.Flush(); // Clean up.
177
178 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
179 }
180
181 // Test inserting a list of packets. Last packet is of a different payload type.
182 // Expecting the buffer to flush.
183 // TODO(hlundin): Remove this test when legacy operation is no longer needed.
TEST(PacketBuffer,InsertPacketListChangePayloadType)184 TEST(PacketBuffer, InsertPacketListChangePayloadType) {
185 PacketBuffer buffer(10); // 10 packets.
186 PacketGenerator gen(0, 0, 0, 10);
187 PacketList list;
188 const int payload_len = 10;
189
190 // Insert 10 small packets.
191 for (int i = 0; i < 10; ++i) {
192 Packet* packet = gen.NextPacket(payload_len);
193 list.push_back(packet);
194 }
195 // Insert 11th packet of another payload type (not CNG).
196 Packet* packet = gen.NextPacket(payload_len);
197 packet->header.payloadType = 1;
198 list.push_back(packet);
199
200
201 MockDecoderDatabase decoder_database;
202 EXPECT_CALL(decoder_database, IsComfortNoise(_))
203 .WillRepeatedly(Return(false));
204 EXPECT_CALL(decoder_database, IsDtmf(_))
205 .WillRepeatedly(Return(false));
206 uint8_t current_pt = 0xFF;
207 uint8_t current_cng_pt = 0xFF;
208 EXPECT_EQ(PacketBuffer::kFlushed, buffer.InsertPacketList(&list,
209 decoder_database,
210 ¤t_pt,
211 ¤t_cng_pt));
212 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
213 EXPECT_EQ(1, buffer.NumPacketsInBuffer()); // Only the last packet.
214 EXPECT_EQ(1, current_pt); // Current payload type changed to 0.
215 EXPECT_EQ(0xFF, current_cng_pt); // CNG payload type not changed.
216
217 buffer.Flush(); // Clean up.
218
219 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
220 }
221
222 // Test inserting a number of packets, and verifying correct extraction order.
223 // The packets inserted are as follows:
224 // Packet no. Seq. no. Primary TS Secondary TS
225 // 0 0xFFFD 0xFFFFFFD7 -
226 // 1 0xFFFE 0xFFFFFFE1 0xFFFFFFD7
227 // 2 0xFFFF 0xFFFFFFEB 0xFFFFFFE1
228 // 3 0x0000 0xFFFFFFF5 0xFFFFFFEB
229 // 4 0x0001 0xFFFFFFFF 0xFFFFFFF5
230 // 5 0x0002 0x0000000A 0xFFFFFFFF
231 // 6 MISSING--0x0003------0x00000014----0x0000000A--MISSING
232 // 7 0x0004 0x0000001E 0x00000014
233 // 8 0x0005 0x00000028 0x0000001E
234 // 9 0x0006 0x00000032 0x00000028
TEST(PacketBuffer,ExtractOrderRedundancy)235 TEST(PacketBuffer, ExtractOrderRedundancy) {
236 PacketBuffer buffer(100); // 100 packets.
237 const uint32_t ts_increment = 10; // Samples per packet.
238 const uint16_t start_seq_no = 0xFFFF - 2; // Wraps after 3 packets.
239 const uint32_t start_ts = 0xFFFFFFFF -
240 4 * ts_increment; // Wraps after 5 packets.
241 const uint8_t primary_pt = 0;
242 const uint8_t secondary_pt = 1;
243 PacketGenerator gen(start_seq_no, start_ts, primary_pt, ts_increment);
244 // Insert secondary payloads too. (Simulating RED.)
245 PacketGenerator red_gen(start_seq_no + 1, start_ts, secondary_pt,
246 ts_increment);
247
248 // Insert 9 small packets (skip one).
249 for (int i = 0; i < 10; ++i) {
250 const int payload_len = 10;
251 if (i == 6) {
252 // Skip this packet.
253 gen.SkipPacket();
254 red_gen.SkipPacket();
255 continue;
256 }
257 // Primary payload.
258 Packet* packet = gen.NextPacket(payload_len);
259 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
260 if (i >= 1) {
261 // Secondary payload.
262 packet = red_gen.NextPacket(payload_len);
263 packet->primary = false;
264 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
265 }
266 }
267 EXPECT_EQ(17, buffer.NumPacketsInBuffer()); // 9 primary + 8 secondary
268
269 uint16_t current_seq_no = start_seq_no;
270 uint32_t current_ts = start_ts;
271
272 for (int i = 0; i < 10; ++i) {
273 // Extract packets.
274 int drop_count = 0;
275 Packet* packet = buffer.GetNextPacket(&drop_count);
276 ASSERT_FALSE(packet == NULL);
277 if (i == 6) {
278 // Special case for the dropped primary payload.
279 // Expect secondary payload, and one step higher sequence number.
280 EXPECT_EQ(current_seq_no + 1, packet->header.sequenceNumber);
281 EXPECT_EQ(current_ts, packet->header.timestamp);
282 EXPECT_FALSE(packet->primary);
283 EXPECT_EQ(1, packet->header.payloadType);
284 EXPECT_EQ(0, drop_count);
285 } else {
286 EXPECT_EQ(current_seq_no, packet->header.sequenceNumber);
287 EXPECT_EQ(current_ts, packet->header.timestamp);
288 EXPECT_TRUE(packet->primary);
289 EXPECT_EQ(0, packet->header.payloadType);
290 if (i == 5 || i == 9) {
291 // No duplicate TS for dropped packet or for last primary payload.
292 EXPECT_EQ(0, drop_count);
293 } else {
294 EXPECT_EQ(1, drop_count);
295 }
296 }
297 ++current_seq_no;
298 current_ts += ts_increment;
299 delete [] packet->payload;
300 delete packet;
301 }
302 }
303
TEST(PacketBuffer,DiscardPackets)304 TEST(PacketBuffer, DiscardPackets) {
305 PacketBuffer buffer(100); // 100 packets.
306 const uint16_t start_seq_no = 17;
307 const uint32_t start_ts = 4711;
308 const uint32_t ts_increment = 10;
309 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
310 PacketList list;
311 const int payload_len = 10;
312
313 // Insert 10 small packets.
314 for (int i = 0; i < 10; ++i) {
315 Packet* packet = gen.NextPacket(payload_len);
316 buffer.InsertPacket(packet);
317 }
318 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
319
320 // Discard them one by one and make sure that the right packets are at the
321 // front of the buffer.
322 uint32_t current_ts = start_ts;
323 for (int i = 0; i < 10; ++i) {
324 uint32_t ts;
325 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
326 EXPECT_EQ(current_ts, ts);
327 EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket());
328 current_ts += ts_increment;
329 }
330 EXPECT_TRUE(buffer.Empty());
331 }
332
TEST(PacketBuffer,Reordering)333 TEST(PacketBuffer, Reordering) {
334 PacketBuffer buffer(100); // 100 packets.
335 const uint16_t start_seq_no = 17;
336 const uint32_t start_ts = 4711;
337 const uint32_t ts_increment = 10;
338 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
339 const int payload_len = 10;
340
341 // Generate 10 small packets and insert them into a PacketList. Insert every
342 // odd packet to the front, and every even packet to the back, thus creating
343 // a (rather strange) reordering.
344 PacketList list;
345 for (int i = 0; i < 10; ++i) {
346 Packet* packet = gen.NextPacket(payload_len);
347 if (i % 2) {
348 list.push_front(packet);
349 } else {
350 list.push_back(packet);
351 }
352 }
353
354 MockDecoderDatabase decoder_database;
355 EXPECT_CALL(decoder_database, IsComfortNoise(0))
356 .WillRepeatedly(Return(false));
357 EXPECT_CALL(decoder_database, IsDtmf(0))
358 .WillRepeatedly(Return(false));
359 uint8_t current_pt = 0xFF;
360 uint8_t current_cng_pt = 0xFF;
361
362 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
363 decoder_database,
364 ¤t_pt,
365 ¤t_cng_pt));
366 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
367
368 // Extract them and make sure that come out in the right order.
369 uint32_t current_ts = start_ts;
370 for (int i = 0; i < 10; ++i) {
371 Packet* packet = buffer.GetNextPacket(NULL);
372 ASSERT_FALSE(packet == NULL);
373 EXPECT_EQ(current_ts, packet->header.timestamp);
374 current_ts += ts_increment;
375 delete [] packet->payload;
376 delete packet;
377 }
378 EXPECT_TRUE(buffer.Empty());
379
380 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
381 }
382
TEST(PacketBuffer,Failures)383 TEST(PacketBuffer, Failures) {
384 const uint16_t start_seq_no = 17;
385 const uint32_t start_ts = 4711;
386 const uint32_t ts_increment = 10;
387 int payload_len = 100;
388 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
389
390 PacketBuffer* buffer = new PacketBuffer(100); // 100 packets.
391 Packet* packet = NULL;
392 EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet));
393 packet = gen.NextPacket(payload_len);
394 delete [] packet->payload;
395 packet->payload = NULL;
396 EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet));
397 // Packet is deleted by the PacketBuffer.
398
399 // Buffer should still be empty. Test all empty-checks.
400 uint32_t temp_ts;
401 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->NextTimestamp(&temp_ts));
402 EXPECT_EQ(PacketBuffer::kBufferEmpty,
403 buffer->NextHigherTimestamp(0, &temp_ts));
404 EXPECT_EQ(NULL, buffer->NextRtpHeader());
405 EXPECT_EQ(NULL, buffer->GetNextPacket(NULL));
406 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->DiscardNextPacket());
407 EXPECT_EQ(0, buffer->DiscardOldPackets(0)); // 0 packets discarded.
408
409 // Insert one packet to make the buffer non-empty.
410 packet = gen.NextPacket(payload_len);
411 EXPECT_EQ(PacketBuffer::kOK, buffer->InsertPacket(packet));
412 EXPECT_EQ(PacketBuffer::kInvalidPointer, buffer->NextTimestamp(NULL));
413 EXPECT_EQ(PacketBuffer::kInvalidPointer,
414 buffer->NextHigherTimestamp(0, NULL));
415 delete buffer;
416
417 // Insert packet list of three packets, where the second packet has an invalid
418 // payload. Expect first packet to be inserted, and the remaining two to be
419 // discarded.
420 buffer = new PacketBuffer(100); // 100 packets.
421 PacketList list;
422 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
423 packet = gen.NextPacket(payload_len);
424 delete [] packet->payload;
425 packet->payload = NULL; // Invalid.
426 list.push_back(packet);
427 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
428 MockDecoderDatabase decoder_database;
429 EXPECT_CALL(decoder_database, IsComfortNoise(0))
430 .WillRepeatedly(Return(false));
431 EXPECT_CALL(decoder_database, IsDtmf(0))
432 .WillRepeatedly(Return(false));
433 uint8_t current_pt = 0xFF;
434 uint8_t current_cng_pt = 0xFF;
435 EXPECT_EQ(PacketBuffer::kInvalidPacket,
436 buffer->InsertPacketList(&list,
437 decoder_database,
438 ¤t_pt,
439 ¤t_cng_pt));
440 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
441 EXPECT_EQ(1, buffer->NumPacketsInBuffer());
442 delete buffer;
443 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
444 }
445
446 // Test packet comparison function.
447 // The function should return true if the first packet "goes before" the second.
TEST(PacketBuffer,ComparePackets)448 TEST(PacketBuffer, ComparePackets) {
449 PacketGenerator gen(0, 0, 0, 10);
450 Packet* a = gen.NextPacket(10); // SN = 0, TS = 0.
451 Packet* b = gen.NextPacket(10); // SN = 1, TS = 10.
452 EXPECT_FALSE(*a == *b);
453 EXPECT_TRUE(*a != *b);
454 EXPECT_TRUE(*a < *b);
455 EXPECT_FALSE(*a > *b);
456 EXPECT_TRUE(*a <= *b);
457 EXPECT_FALSE(*a >= *b);
458
459 // Testing wrap-around case; 'a' is earlier but has a larger timestamp value.
460 a->header.timestamp = 0xFFFFFFFF - 10;
461 EXPECT_FALSE(*a == *b);
462 EXPECT_TRUE(*a != *b);
463 EXPECT_TRUE(*a < *b);
464 EXPECT_FALSE(*a > *b);
465 EXPECT_TRUE(*a <= *b);
466 EXPECT_FALSE(*a >= *b);
467
468 // Test equal packets.
469 EXPECT_TRUE(*a == *a);
470 EXPECT_FALSE(*a != *a);
471 EXPECT_FALSE(*a < *a);
472 EXPECT_FALSE(*a > *a);
473 EXPECT_TRUE(*a <= *a);
474 EXPECT_TRUE(*a >= *a);
475
476 // Test equal timestamps but different sequence numbers (0 and 1).
477 a->header.timestamp = b->header.timestamp;
478 EXPECT_FALSE(*a == *b);
479 EXPECT_TRUE(*a != *b);
480 EXPECT_TRUE(*a < *b);
481 EXPECT_FALSE(*a > *b);
482 EXPECT_TRUE(*a <= *b);
483 EXPECT_FALSE(*a >= *b);
484
485 // Test equal timestamps but different sequence numbers (32767 and 1).
486 a->header.sequenceNumber = 0xFFFF;
487 EXPECT_FALSE(*a == *b);
488 EXPECT_TRUE(*a != *b);
489 EXPECT_TRUE(*a < *b);
490 EXPECT_FALSE(*a > *b);
491 EXPECT_TRUE(*a <= *b);
492 EXPECT_FALSE(*a >= *b);
493
494 // Test equal timestamps and sequence numbers, but only 'b' is primary.
495 a->header.sequenceNumber = b->header.sequenceNumber;
496 a->primary = false;
497 b->primary = true;
498 EXPECT_FALSE(*a == *b);
499 EXPECT_TRUE(*a != *b);
500 EXPECT_FALSE(*a < *b);
501 EXPECT_TRUE(*a > *b);
502 EXPECT_FALSE(*a <= *b);
503 EXPECT_TRUE(*a >= *b);
504
505 delete [] a->payload;
506 delete a;
507 delete [] b->payload;
508 delete b;
509 }
510
511 // Test the DeleteFirstPacket DeleteAllPackets methods.
TEST(PacketBuffer,DeleteAllPackets)512 TEST(PacketBuffer, DeleteAllPackets) {
513 PacketGenerator gen(0, 0, 0, 10);
514 PacketList list;
515 const int payload_len = 10;
516
517 // Insert 10 small packets.
518 for (int i = 0; i < 10; ++i) {
519 Packet* packet = gen.NextPacket(payload_len);
520 list.push_back(packet);
521 }
522 EXPECT_TRUE(PacketBuffer::DeleteFirstPacket(&list));
523 EXPECT_EQ(9u, list.size());
524 PacketBuffer::DeleteAllPackets(&list);
525 EXPECT_TRUE(list.empty());
526 EXPECT_FALSE(PacketBuffer::DeleteFirstPacket(&list));
527 }
528
529 } // namespace webrtc
530