2 * Copyright (c) 2012 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.
11 // Unit tests for PacketBuffer class.
13 #include "webrtc/modules/audio_coding/neteq/packet_buffer.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/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"
20 using ::testing::Return;
25 // Helper class to generate packets. Packets must be deleted by the user.
26 class PacketGenerator {
28 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
29 virtual ~PacketGenerator() {}
30 Packet* NextPacket(int payload_size_bytes);
39 PacketGenerator::PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt,
44 frame_size_(frame_size) {
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];
64 void PacketGenerator::SkipPacket() {
70 // Start of test definitions.
72 TEST(PacketBuffer, CreateAndDestroy) {
73 PacketBuffer* buffer = new PacketBuffer(10); // 10 packets.
74 EXPECT_TRUE(buffer->Empty());
78 TEST(PacketBuffer, InsertPacket) {
79 PacketBuffer buffer(10); // 10 packets.
80 PacketGenerator gen(17u, 4711u, 0, 10);
82 const int payload_len = 100;
83 Packet* packet = gen.NextPacket(payload_len);
85 EXPECT_EQ(0, buffer.InsertPacket(packet));
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.
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.)
98 // Test to flush buffer.
99 TEST(PacketBuffer, FlushBuffer) {
100 PacketBuffer buffer(10); // 10 packets.
101 PacketGenerator gen(0, 0, 0, 10);
102 const int payload_len = 10;
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));
109 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
110 EXPECT_FALSE(buffer.Empty());
113 // Buffer should delete the payloads itself.
114 EXPECT_EQ(0, buffer.NumPacketsInBuffer());
115 EXPECT_TRUE(buffer.Empty());
118 // Test to fill the buffer over the limits, and verify that it flushes.
119 TEST(PacketBuffer, OverfillBuffer) {
120 PacketBuffer buffer(10); // 10 packets.
121 PacketGenerator gen(0, 0, 0, 10);
123 // Insert 10 small packets; should be ok.
124 const int payload_len = 10;
126 for (i = 0; i < 10; ++i) {
127 Packet* packet = gen.NextPacket(payload_len);
128 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
130 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
132 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
133 EXPECT_EQ(0u, next_ts); // Expect first inserted packet to be first in line.
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);
143 // Flush buffer to delete all packets.
147 // Test inserting a list of packets.
148 TEST(PacketBuffer, InsertPacketList) {
149 PacketBuffer buffer(10); // 10 packets.
150 PacketGenerator gen(0, 0, 0, 10);
152 const int payload_len = 10;
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);
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,
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.
176 buffer.Flush(); // Clean up.
178 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
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.
184 TEST(PacketBuffer, InsertPacketListChangePayloadType) {
185 PacketBuffer buffer(10); // 10 packets.
186 PacketGenerator gen(0, 0, 0, 10);
188 const int payload_len = 10;
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);
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);
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,
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.
217 buffer.Flush(); // Clean up.
219 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
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
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,
248 // Insert 9 small packets (skip one).
249 for (int i = 0; i < 10; ++i) {
250 const int payload_len = 10;
254 red_gen.SkipPacket();
258 Packet* packet = gen.NextPacket(payload_len);
259 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
261 // Secondary payload.
262 packet = red_gen.NextPacket(payload_len);
263 packet->primary = false;
264 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
267 EXPECT_EQ(17, buffer.NumPacketsInBuffer()); // 9 primary + 8 secondary
269 uint16_t current_seq_no = start_seq_no;
270 uint32_t current_ts = start_ts;
272 for (int i = 0; i < 10; ++i) {
275 Packet* packet = buffer.GetNextPacket(&drop_count);
276 ASSERT_FALSE(packet == NULL);
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);
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);
294 EXPECT_EQ(1, drop_count);
298 current_ts += ts_increment;
299 delete [] packet->payload;
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);
311 const int payload_len = 10;
313 // Insert 10 small packets.
314 for (int i = 0; i < 10; ++i) {
315 Packet* packet = gen.NextPacket(payload_len);
316 buffer.InsertPacket(packet);
318 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
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) {
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;
330 EXPECT_TRUE(buffer.Empty());
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;
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.
345 for (int i = 0; i < 10; ++i) {
346 Packet* packet = gen.NextPacket(payload_len);
348 list.push_front(packet);
350 list.push_back(packet);
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;
362 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
366 EXPECT_EQ(10, buffer.NumPacketsInBuffer());
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;
378 EXPECT_TRUE(buffer.Empty());
380 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
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);
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.
399 // Buffer should still be empty. Test all empty-checks.
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.
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));
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
420 buffer = new PacketBuffer(100); // 100 packets.
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,
440 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
441 EXPECT_EQ(1, buffer->NumPacketsInBuffer());
443 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
446 // Test packet comparison function.
447 // The function should return true if the first packet "goes before" the second.
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);
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);
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);
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);
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);
494 // Test equal timestamps and sequence numbers, but only 'b' is primary.
495 a->header.sequenceNumber = b->header.sequenceNumber;
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);
505 delete [] a->payload;
507 delete [] b->payload;
511 // Test the DeleteFirstPacket DeleteAllPackets methods.
512 TEST(PacketBuffer, DeleteAllPackets) {
513 PacketGenerator gen(0, 0, 0, 10);
515 const int payload_len = 10;
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);
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));
529 } // namespace webrtc