• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
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   void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
31   Packet* NextPacket(int payload_size_bytes);
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   Reset(seq_no, ts, pt, frame_size);
42 }
43 
Reset(uint16_t seq_no,uint32_t ts,uint8_t pt,int frame_size)44 void PacketGenerator::Reset(uint16_t seq_no, uint32_t ts, uint8_t pt,
45                             int frame_size) {
46   seq_no_ = seq_no;
47   ts_ = ts;
48   pt_ = pt;
49   frame_size_ = frame_size;
50 }
51 
NextPacket(int payload_size_bytes)52 Packet* PacketGenerator::NextPacket(int payload_size_bytes) {
53   Packet* packet = new Packet;
54   packet->header.sequenceNumber = seq_no_;
55   packet->header.timestamp = ts_;
56   packet->header.payloadType = pt_;
57   packet->header.markerBit = false;
58   packet->header.ssrc = 0x12345678;
59   packet->header.numCSRCs = 0;
60   packet->header.paddingLength = 0;
61   packet->payload_length = payload_size_bytes;
62   packet->primary = true;
63   packet->payload = new uint8_t[payload_size_bytes];
64   ++seq_no_;
65   ts_ += frame_size_;
66   return packet;
67 }
68 
69 struct PacketsToInsert {
70   uint16_t sequence_number;
71   uint32_t timestamp;
72   uint8_t payload_type;
73   bool primary;
74   // Order of this packet to appear upon extraction, after inserting a series
75   // of packets. A negative number means that it should have been discarded
76   // before extraction.
77   int extract_order;
78 };
79 
80 // Start of test definitions.
81 
TEST(PacketBuffer,CreateAndDestroy)82 TEST(PacketBuffer, CreateAndDestroy) {
83   PacketBuffer* buffer = new PacketBuffer(10);  // 10 packets.
84   EXPECT_TRUE(buffer->Empty());
85   delete buffer;
86 }
87 
TEST(PacketBuffer,InsertPacket)88 TEST(PacketBuffer, InsertPacket) {
89   PacketBuffer buffer(10);  // 10 packets.
90   PacketGenerator gen(17u, 4711u, 0, 10);
91 
92   const int payload_len = 100;
93   Packet* packet = gen.NextPacket(payload_len);
94 
95   EXPECT_EQ(0, buffer.InsertPacket(packet));
96   uint32_t next_ts;
97   EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
98   EXPECT_EQ(4711u, next_ts);
99   EXPECT_FALSE(buffer.Empty());
100   EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
101   const RTPHeader* hdr = buffer.NextRtpHeader();
102   EXPECT_EQ(&(packet->header), hdr);  // Compare pointer addresses.
103 
104   // Do not explicitly flush buffer or delete packet to test that it is deleted
105   // with the buffer. (Tested with Valgrind or similar tool.)
106 }
107 
108 // Test to flush buffer.
TEST(PacketBuffer,FlushBuffer)109 TEST(PacketBuffer, FlushBuffer) {
110   PacketBuffer buffer(10);  // 10 packets.
111   PacketGenerator gen(0, 0, 0, 10);
112   const int payload_len = 10;
113 
114   // Insert 10 small packets; should be ok.
115   for (int i = 0; i < 10; ++i) {
116     Packet* packet = gen.NextPacket(payload_len);
117     EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
118   }
119   EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
120   EXPECT_FALSE(buffer.Empty());
121 
122   buffer.Flush();
123   // Buffer should delete the payloads itself.
124   EXPECT_EQ(0u, buffer.NumPacketsInBuffer());
125   EXPECT_TRUE(buffer.Empty());
126 }
127 
128 // Test to fill the buffer over the limits, and verify that it flushes.
TEST(PacketBuffer,OverfillBuffer)129 TEST(PacketBuffer, OverfillBuffer) {
130   PacketBuffer buffer(10);  // 10 packets.
131   PacketGenerator gen(0, 0, 0, 10);
132 
133   // Insert 10 small packets; should be ok.
134   const int payload_len = 10;
135   int i;
136   for (i = 0; i < 10; ++i) {
137     Packet* packet = gen.NextPacket(payload_len);
138     EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
139   }
140   EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
141   uint32_t next_ts;
142   EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
143   EXPECT_EQ(0u, next_ts);  // Expect first inserted packet to be first in line.
144 
145   // Insert 11th packet; should flush the buffer and insert it after flushing.
146   Packet* packet = gen.NextPacket(payload_len);
147   EXPECT_EQ(PacketBuffer::kFlushed, buffer.InsertPacket(packet));
148   EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
149   EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
150   // Expect last inserted packet to be first in line.
151   EXPECT_EQ(packet->header.timestamp, next_ts);
152 
153   // Flush buffer to delete all packets.
154   buffer.Flush();
155 }
156 
157 // Test inserting a list of packets.
TEST(PacketBuffer,InsertPacketList)158 TEST(PacketBuffer, InsertPacketList) {
159   PacketBuffer buffer(10);  // 10 packets.
160   PacketGenerator gen(0, 0, 0, 10);
161   PacketList list;
162   const int payload_len = 10;
163 
164   // Insert 10 small packets.
165   for (int i = 0; i < 10; ++i) {
166     Packet* packet = gen.NextPacket(payload_len);
167     list.push_back(packet);
168   }
169 
170   MockDecoderDatabase decoder_database;
171   EXPECT_CALL(decoder_database, IsComfortNoise(0))
172       .WillRepeatedly(Return(false));
173   EXPECT_CALL(decoder_database, IsDtmf(0))
174       .WillRepeatedly(Return(false));
175   uint8_t current_pt = 0xFF;
176   uint8_t current_cng_pt = 0xFF;
177   EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
178                                                        decoder_database,
179                                                        &current_pt,
180                                                        &current_cng_pt));
181   EXPECT_TRUE(list.empty());  // The PacketBuffer should have depleted the list.
182   EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
183   EXPECT_EQ(0, current_pt);  // Current payload type changed to 0.
184   EXPECT_EQ(0xFF, current_cng_pt);  // CNG payload type not changed.
185 
186   buffer.Flush();  // Clean up.
187 
188   EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
189 }
190 
191 // Test inserting a list of packets. Last packet is of a different payload type.
192 // Expecting the buffer to flush.
193 // TODO(hlundin): Remove this test when legacy operation is no longer needed.
TEST(PacketBuffer,InsertPacketListChangePayloadType)194 TEST(PacketBuffer, InsertPacketListChangePayloadType) {
195   PacketBuffer buffer(10);  // 10 packets.
196   PacketGenerator gen(0, 0, 0, 10);
197   PacketList list;
198   const int payload_len = 10;
199 
200   // Insert 10 small packets.
201   for (int i = 0; i < 10; ++i) {
202     Packet* packet = gen.NextPacket(payload_len);
203     list.push_back(packet);
204   }
205   // Insert 11th packet of another payload type (not CNG).
206   Packet* packet = gen.NextPacket(payload_len);
207   packet->header.payloadType = 1;
208   list.push_back(packet);
209 
210 
211   MockDecoderDatabase decoder_database;
212   EXPECT_CALL(decoder_database, IsComfortNoise(_))
213       .WillRepeatedly(Return(false));
214   EXPECT_CALL(decoder_database, IsDtmf(_))
215       .WillRepeatedly(Return(false));
216   uint8_t current_pt = 0xFF;
217   uint8_t current_cng_pt = 0xFF;
218   EXPECT_EQ(PacketBuffer::kFlushed, buffer.InsertPacketList(&list,
219                                                             decoder_database,
220                                                             &current_pt,
221                                                             &current_cng_pt));
222   EXPECT_TRUE(list.empty());  // The PacketBuffer should have depleted the list.
223   EXPECT_EQ(1u, buffer.NumPacketsInBuffer());  // Only the last packet.
224   EXPECT_EQ(1, current_pt);  // Current payload type changed to 0.
225   EXPECT_EQ(0xFF, current_cng_pt);  // CNG payload type not changed.
226 
227   buffer.Flush();  // Clean up.
228 
229   EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
230 }
231 
TEST(PacketBuffer,ExtractOrderRedundancy)232 TEST(PacketBuffer, ExtractOrderRedundancy) {
233   PacketBuffer buffer(100);  // 100 packets.
234   const int kPackets = 18;
235   const int kFrameSize = 10;
236   const int kPayloadLength = 10;
237 
238   PacketsToInsert packet_facts[kPackets] = {
239     {0xFFFD, 0xFFFFFFD7, 0, true, 0},
240     {0xFFFE, 0xFFFFFFE1, 0, true, 1},
241     {0xFFFE, 0xFFFFFFD7, 1, false, -1},
242     {0xFFFF, 0xFFFFFFEB, 0, true, 2},
243     {0xFFFF, 0xFFFFFFE1, 1, false, -1},
244     {0x0000, 0xFFFFFFF5, 0, true, 3},
245     {0x0000, 0xFFFFFFEB, 1, false, -1},
246     {0x0001, 0xFFFFFFFF, 0, true, 4},
247     {0x0001, 0xFFFFFFF5, 1, false, -1},
248     {0x0002, 0x0000000A, 0, true, 5},
249     {0x0002, 0xFFFFFFFF, 1, false, -1},
250     {0x0003, 0x0000000A, 1, false, -1},
251     {0x0004, 0x0000001E, 0, true, 7},
252     {0x0004, 0x00000014, 1, false, 6},
253     {0x0005, 0x0000001E, 0, true, -1},
254     {0x0005, 0x00000014, 1, false, -1},
255     {0x0006, 0x00000028, 0, true, 8},
256     {0x0006, 0x0000001E, 1, false, -1},
257   };
258 
259   const size_t kExpectPacketsInBuffer = 9;
260 
261   std::vector<Packet*> expect_order(kExpectPacketsInBuffer);
262 
263   PacketGenerator gen(0, 0, 0, kFrameSize);
264 
265   for (int i = 0; i < kPackets; ++i) {
266     gen.Reset(packet_facts[i].sequence_number,
267               packet_facts[i].timestamp,
268               packet_facts[i].payload_type,
269               kFrameSize);
270     Packet* packet = gen.NextPacket(kPayloadLength);
271     packet->primary = packet_facts[i].primary;
272     EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet));
273     if (packet_facts[i].extract_order >= 0) {
274       expect_order[packet_facts[i].extract_order] = packet;
275     }
276   }
277 
278   EXPECT_EQ(kExpectPacketsInBuffer, buffer.NumPacketsInBuffer());
279 
280   size_t drop_count;
281   for (size_t i = 0; i < kExpectPacketsInBuffer; ++i) {
282     Packet* packet = buffer.GetNextPacket(&drop_count);
283     EXPECT_EQ(0u, drop_count);
284     EXPECT_EQ(packet, expect_order[i]);  // Compare pointer addresses.
285     delete[] packet->payload;
286     delete packet;
287   }
288   EXPECT_TRUE(buffer.Empty());
289 }
290 
TEST(PacketBuffer,DiscardPackets)291 TEST(PacketBuffer, DiscardPackets) {
292   PacketBuffer buffer(100);  // 100 packets.
293   const uint16_t start_seq_no = 17;
294   const uint32_t start_ts = 4711;
295   const uint32_t ts_increment = 10;
296   PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
297   PacketList list;
298   const int payload_len = 10;
299 
300   // Insert 10 small packets.
301   for (int i = 0; i < 10; ++i) {
302     Packet* packet = gen.NextPacket(payload_len);
303     buffer.InsertPacket(packet);
304   }
305   EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
306 
307   // Discard them one by one and make sure that the right packets are at the
308   // front of the buffer.
309   uint32_t current_ts = start_ts;
310   for (int i = 0; i < 10; ++i) {
311     uint32_t ts;
312     EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
313     EXPECT_EQ(current_ts, ts);
314     EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket());
315     current_ts += ts_increment;
316   }
317   EXPECT_TRUE(buffer.Empty());
318 }
319 
TEST(PacketBuffer,Reordering)320 TEST(PacketBuffer, Reordering) {
321   PacketBuffer buffer(100);  // 100 packets.
322   const uint16_t start_seq_no = 17;
323   const uint32_t start_ts = 4711;
324   const uint32_t ts_increment = 10;
325   PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
326   const int payload_len = 10;
327 
328   // Generate 10 small packets and insert them into a PacketList. Insert every
329   // odd packet to the front, and every even packet to the back, thus creating
330   // a (rather strange) reordering.
331   PacketList list;
332   for (int i = 0; i < 10; ++i) {
333     Packet* packet = gen.NextPacket(payload_len);
334     if (i % 2) {
335       list.push_front(packet);
336     } else {
337       list.push_back(packet);
338     }
339   }
340 
341   MockDecoderDatabase decoder_database;
342   EXPECT_CALL(decoder_database, IsComfortNoise(0))
343       .WillRepeatedly(Return(false));
344   EXPECT_CALL(decoder_database, IsDtmf(0))
345       .WillRepeatedly(Return(false));
346   uint8_t current_pt = 0xFF;
347   uint8_t current_cng_pt = 0xFF;
348 
349   EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
350                                                        decoder_database,
351                                                        &current_pt,
352                                                        &current_cng_pt));
353   EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
354 
355   // Extract them and make sure that come out in the right order.
356   uint32_t current_ts = start_ts;
357   for (int i = 0; i < 10; ++i) {
358     Packet* packet = buffer.GetNextPacket(NULL);
359     ASSERT_FALSE(packet == NULL);
360     EXPECT_EQ(current_ts, packet->header.timestamp);
361     current_ts += ts_increment;
362     delete [] packet->payload;
363     delete packet;
364   }
365   EXPECT_TRUE(buffer.Empty());
366 
367   EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
368 }
369 
TEST(PacketBuffer,Failures)370 TEST(PacketBuffer, Failures) {
371   const uint16_t start_seq_no = 17;
372   const uint32_t start_ts = 4711;
373   const uint32_t ts_increment = 10;
374   int payload_len = 100;
375   PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
376 
377   PacketBuffer* buffer = new PacketBuffer(100);  // 100 packets.
378   Packet* packet = NULL;
379   EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet));
380   packet = gen.NextPacket(payload_len);
381   delete [] packet->payload;
382   packet->payload = NULL;
383   EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet));
384   // Packet is deleted by the PacketBuffer.
385 
386   // Buffer should still be empty. Test all empty-checks.
387   uint32_t temp_ts;
388   EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->NextTimestamp(&temp_ts));
389   EXPECT_EQ(PacketBuffer::kBufferEmpty,
390             buffer->NextHigherTimestamp(0, &temp_ts));
391   EXPECT_EQ(NULL, buffer->NextRtpHeader());
392   EXPECT_EQ(NULL, buffer->GetNextPacket(NULL));
393   EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->DiscardNextPacket());
394   EXPECT_EQ(0, buffer->DiscardAllOldPackets(0));  // 0 packets discarded.
395 
396   // Insert one packet to make the buffer non-empty.
397   packet = gen.NextPacket(payload_len);
398   EXPECT_EQ(PacketBuffer::kOK, buffer->InsertPacket(packet));
399   EXPECT_EQ(PacketBuffer::kInvalidPointer, buffer->NextTimestamp(NULL));
400   EXPECT_EQ(PacketBuffer::kInvalidPointer,
401             buffer->NextHigherTimestamp(0, NULL));
402   delete buffer;
403 
404   // Insert packet list of three packets, where the second packet has an invalid
405   // payload.  Expect first packet to be inserted, and the remaining two to be
406   // discarded.
407   buffer = new PacketBuffer(100);  // 100 packets.
408   PacketList list;
409   list.push_back(gen.NextPacket(payload_len));  // Valid packet.
410   packet = gen.NextPacket(payload_len);
411   delete [] packet->payload;
412   packet->payload = NULL;  // Invalid.
413   list.push_back(packet);
414   list.push_back(gen.NextPacket(payload_len));  // Valid packet.
415   MockDecoderDatabase decoder_database;
416   EXPECT_CALL(decoder_database, IsComfortNoise(0))
417       .WillRepeatedly(Return(false));
418   EXPECT_CALL(decoder_database, IsDtmf(0))
419       .WillRepeatedly(Return(false));
420   uint8_t current_pt = 0xFF;
421   uint8_t current_cng_pt = 0xFF;
422   EXPECT_EQ(PacketBuffer::kInvalidPacket,
423             buffer->InsertPacketList(&list,
424                                      decoder_database,
425                                      &current_pt,
426                                      &current_cng_pt));
427   EXPECT_TRUE(list.empty());  // The PacketBuffer should have depleted the list.
428   EXPECT_EQ(1u, buffer->NumPacketsInBuffer());
429   delete buffer;
430   EXPECT_CALL(decoder_database, Die());  // Called when object is deleted.
431 }
432 
433 // Test packet comparison function.
434 // The function should return true if the first packet "goes before" the second.
TEST(PacketBuffer,ComparePackets)435 TEST(PacketBuffer, ComparePackets) {
436   PacketGenerator gen(0, 0, 0, 10);
437   Packet* a = gen.NextPacket(10);  // SN = 0, TS = 0.
438   Packet* b = gen.NextPacket(10);  // SN = 1, TS = 10.
439   EXPECT_FALSE(*a == *b);
440   EXPECT_TRUE(*a != *b);
441   EXPECT_TRUE(*a < *b);
442   EXPECT_FALSE(*a > *b);
443   EXPECT_TRUE(*a <= *b);
444   EXPECT_FALSE(*a >= *b);
445 
446   // Testing wrap-around case; 'a' is earlier but has a larger timestamp value.
447   a->header.timestamp = 0xFFFFFFFF - 10;
448   EXPECT_FALSE(*a == *b);
449   EXPECT_TRUE(*a != *b);
450   EXPECT_TRUE(*a < *b);
451   EXPECT_FALSE(*a > *b);
452   EXPECT_TRUE(*a <= *b);
453   EXPECT_FALSE(*a >= *b);
454 
455   // Test equal packets.
456   EXPECT_TRUE(*a == *a);
457   EXPECT_FALSE(*a != *a);
458   EXPECT_FALSE(*a < *a);
459   EXPECT_FALSE(*a > *a);
460   EXPECT_TRUE(*a <= *a);
461   EXPECT_TRUE(*a >= *a);
462 
463   // Test equal timestamps but different sequence numbers (0 and 1).
464   a->header.timestamp = b->header.timestamp;
465   EXPECT_FALSE(*a == *b);
466   EXPECT_TRUE(*a != *b);
467   EXPECT_TRUE(*a < *b);
468   EXPECT_FALSE(*a > *b);
469   EXPECT_TRUE(*a <= *b);
470   EXPECT_FALSE(*a >= *b);
471 
472   // Test equal timestamps but different sequence numbers (32767 and 1).
473   a->header.sequenceNumber = 0xFFFF;
474   EXPECT_FALSE(*a == *b);
475   EXPECT_TRUE(*a != *b);
476   EXPECT_TRUE(*a < *b);
477   EXPECT_FALSE(*a > *b);
478   EXPECT_TRUE(*a <= *b);
479   EXPECT_FALSE(*a >= *b);
480 
481   // Test equal timestamps and sequence numbers, but only 'b' is primary.
482   a->header.sequenceNumber = b->header.sequenceNumber;
483   a->primary = false;
484   b->primary = true;
485   EXPECT_FALSE(*a == *b);
486   EXPECT_TRUE(*a != *b);
487   EXPECT_FALSE(*a < *b);
488   EXPECT_TRUE(*a > *b);
489   EXPECT_FALSE(*a <= *b);
490   EXPECT_TRUE(*a >= *b);
491 
492   delete [] a->payload;
493   delete a;
494   delete [] b->payload;
495   delete b;
496 }
497 
498 // Test the DeleteFirstPacket DeleteAllPackets methods.
TEST(PacketBuffer,DeleteAllPackets)499 TEST(PacketBuffer, DeleteAllPackets) {
500   PacketGenerator gen(0, 0, 0, 10);
501   PacketList list;
502   const int payload_len = 10;
503 
504   // Insert 10 small packets.
505   for (int i = 0; i < 10; ++i) {
506     Packet* packet = gen.NextPacket(payload_len);
507     list.push_back(packet);
508   }
509   EXPECT_TRUE(PacketBuffer::DeleteFirstPacket(&list));
510   EXPECT_EQ(9u, list.size());
511   PacketBuffer::DeleteAllPackets(&list);
512   EXPECT_TRUE(list.empty());
513   EXPECT_FALSE(PacketBuffer::DeleteFirstPacket(&list));
514 }
515 
516 namespace {
TestIsObsoleteTimestamp(uint32_t limit_timestamp)517 void TestIsObsoleteTimestamp(uint32_t limit_timestamp) {
518   // Check with zero horizon, which implies that the horizon is at 2^31, i.e.,
519   // half the timestamp range.
520   static const uint32_t kZeroHorizon = 0;
521   static const uint32_t k2Pow31Minus1 = 0x7FFFFFFF;
522   // Timestamp on the limit is not old.
523   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
524       limit_timestamp, limit_timestamp, kZeroHorizon));
525   // 1 sample behind is old.
526   EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
527       limit_timestamp - 1, limit_timestamp, kZeroHorizon));
528   // 2^31 - 1 samples behind is old.
529   EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
530       limit_timestamp - k2Pow31Minus1, limit_timestamp, kZeroHorizon));
531   // 1 sample ahead is not old.
532   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
533       limit_timestamp + 1, limit_timestamp, kZeroHorizon));
534   // If |t1-t2|=2^31 and t1>t2, t2 is older than t1 but not the opposite.
535   uint32_t other_timestamp = limit_timestamp + (1 << 31);
536   uint32_t lowest_timestamp = std::min(limit_timestamp, other_timestamp);
537   uint32_t highest_timestamp = std::max(limit_timestamp, other_timestamp);
538   EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
539       lowest_timestamp, highest_timestamp, kZeroHorizon));
540   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
541       highest_timestamp, lowest_timestamp, kZeroHorizon));
542 
543   // Fixed horizon at 10 samples.
544   static const uint32_t kHorizon = 10;
545   // Timestamp on the limit is not old.
546   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
547       limit_timestamp, limit_timestamp, kHorizon));
548   // 1 sample behind is old.
549   EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
550       limit_timestamp - 1, limit_timestamp, kHorizon));
551   // 9 samples behind is old.
552   EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
553       limit_timestamp - 9, limit_timestamp, kHorizon));
554   // 10 samples behind is not old.
555   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
556       limit_timestamp - 10, limit_timestamp, kHorizon));
557   // 2^31 - 1 samples behind is not old.
558   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
559       limit_timestamp - k2Pow31Minus1, limit_timestamp, kHorizon));
560   // 1 sample ahead is not old.
561   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
562       limit_timestamp + 1, limit_timestamp, kHorizon));
563   // 2^31 samples ahead is not old.
564   EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
565       limit_timestamp + (1 << 31), limit_timestamp, kHorizon));
566 }
567 }  // namespace
568 
569 // Test the IsObsoleteTimestamp method with different limit timestamps.
TEST(PacketBuffer,IsObsoleteTimestamp)570 TEST(PacketBuffer, IsObsoleteTimestamp) {
571   TestIsObsoleteTimestamp(0);
572   TestIsObsoleteTimestamp(1);
573   TestIsObsoleteTimestamp(0xFFFFFFFF);  // -1 in uint32_t.
574   TestIsObsoleteTimestamp(0x80000000);  // 2^31.
575   TestIsObsoleteTimestamp(0x80000001);  // 2^31 + 1.
576   TestIsObsoleteTimestamp(0x7FFFFFFF);  // 2^31 - 1.
577 }
578 }  // namespace webrtc
579