blob: 4beead6729bf421ff28e7e56bc58b04ca27bd158 [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 PacketBuffer class.
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_coding/neteq/packet_buffer.h"
14#include "api/audio_codecs/builtin_audio_decoder_factory.h"
15#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
16#include "modules/audio_coding/neteq/mock/mock_statistics_calculator.h"
17#include "modules/audio_coding/neteq/packet.h"
18#include "modules/audio_coding/neteq/tick_timer.h"
19#include "test/gmock.h"
20#include "test/gtest.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000021
22using ::testing::Return;
minyue-webrtcfae474c2017-07-05 11:17:40 +020023using ::testing::StrictMock;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024using ::testing::_;
henrik.lundin63d146b2017-07-05 07:03:34 -070025using ::testing::InSequence;
26using ::testing::MockFunction;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027
28namespace webrtc {
29
30// Helper class to generate packets. Packets must be deleted by the user.
31class PacketGenerator {
32 public:
33 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
34 virtual ~PacketGenerator() {}
minyue@webrtc.orgc8039072014-10-09 10:49:54 +000035 void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
ossua73f6c92016-10-24 08:25:28 -070036 Packet NextPacket(int payload_size_bytes);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
38 uint16_t seq_no_;
39 uint32_t ts_;
40 uint8_t pt_;
41 int frame_size_;
42};
43
44PacketGenerator::PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt,
minyue@webrtc.orgc8039072014-10-09 10:49:54 +000045 int frame_size) {
46 Reset(seq_no, ts, pt, frame_size);
47}
48
49void PacketGenerator::Reset(uint16_t seq_no, uint32_t ts, uint8_t pt,
50 int frame_size) {
51 seq_no_ = seq_no;
52 ts_ = ts;
53 pt_ = pt;
54 frame_size_ = frame_size;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055}
56
ossua73f6c92016-10-24 08:25:28 -070057Packet PacketGenerator::NextPacket(int payload_size_bytes) {
58 Packet packet;
59 packet.sequence_number = seq_no_;
60 packet.timestamp = ts_;
61 packet.payload_type = pt_;
62 packet.payload.SetSize(payload_size_bytes);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063 ++seq_no_;
64 ts_ += frame_size_;
65 return packet;
66}
67
minyue@webrtc.orgc8039072014-10-09 10:49:54 +000068struct PacketsToInsert {
69 uint16_t sequence_number;
70 uint32_t timestamp;
71 uint8_t payload_type;
72 bool primary;
73 // Order of this packet to appear upon extraction, after inserting a series
74 // of packets. A negative number means that it should have been discarded
75 // before extraction.
76 int extract_order;
77};
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078
79// Start of test definitions.
80
81TEST(PacketBuffer, CreateAndDestroy) {
henrik.lundin84f8cd62016-04-26 07:45:16 -070082 TickTimer tick_timer;
83 PacketBuffer* buffer = new PacketBuffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 EXPECT_TRUE(buffer->Empty());
85 delete buffer;
86}
87
88TEST(PacketBuffer, InsertPacket) {
henrik.lundin84f8cd62016-04-26 07:45:16 -070089 TickTimer tick_timer;
90 PacketBuffer buffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091 PacketGenerator gen(17u, 4711u, 0, 10);
minyue-webrtc12d30842017-07-19 11:44:06 +020092 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093
94 const int payload_len = 100;
ossua73f6c92016-10-24 08:25:28 -070095 const Packet packet = gen.NextPacket(payload_len);
minyue-webrtc12d30842017-07-19 11:44:06 +020096 EXPECT_EQ(0, buffer.InsertPacket(packet.Clone(), &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000097 uint32_t next_ts;
98 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
99 EXPECT_EQ(4711u, next_ts);
100 EXPECT_FALSE(buffer.Empty());
Peter Kastingdce40cf2015-08-24 14:52:23 -0700101 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700102 const Packet* next_packet = buffer.PeekNextPacket();
ossua73f6c92016-10-24 08:25:28 -0700103 EXPECT_EQ(packet, *next_packet); // Compare contents.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104
105 // Do not explicitly flush buffer or delete packet to test that it is deleted
106 // with the buffer. (Tested with Valgrind or similar tool.)
107}
108
109// Test to flush buffer.
110TEST(PacketBuffer, FlushBuffer) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700111 TickTimer tick_timer;
112 PacketBuffer buffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 PacketGenerator gen(0, 0, 0, 10);
114 const int payload_len = 10;
minyue-webrtc12d30842017-07-19 11:44:06 +0200115 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000116
117 // Insert 10 small packets; should be ok.
118 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700119 EXPECT_EQ(PacketBuffer::kOK,
minyue-webrtc12d30842017-07-19 11:44:06 +0200120 buffer.InsertPacket(gen.NextPacket(payload_len), &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123 EXPECT_FALSE(buffer.Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124
125 buffer.Flush();
126 // Buffer should delete the payloads itself.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700127 EXPECT_EQ(0u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128 EXPECT_TRUE(buffer.Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129}
130
131// Test to fill the buffer over the limits, and verify that it flushes.
132TEST(PacketBuffer, OverfillBuffer) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700133 TickTimer tick_timer;
134 PacketBuffer buffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135 PacketGenerator gen(0, 0, 0, 10);
minyue-webrtc12d30842017-07-19 11:44:06 +0200136 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137
138 // Insert 10 small packets; should be ok.
139 const int payload_len = 10;
140 int i;
141 for (i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700142 EXPECT_EQ(PacketBuffer::kOK,
minyue-webrtc12d30842017-07-19 11:44:06 +0200143 buffer.InsertPacket(gen.NextPacket(payload_len), &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146 uint32_t next_ts;
147 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
148 EXPECT_EQ(0u, next_ts); // Expect first inserted packet to be first in line.
149
ossua73f6c92016-10-24 08:25:28 -0700150 const Packet packet = gen.NextPacket(payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000151 // Insert 11th packet; should flush the buffer and insert it after flushing.
minyue-webrtc12d30842017-07-19 11:44:06 +0200152 EXPECT_EQ(PacketBuffer::kFlushed,
153 buffer.InsertPacket(packet.Clone(), &mock_stats));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700154 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
156 // Expect last inserted packet to be first in line.
ossua73f6c92016-10-24 08:25:28 -0700157 EXPECT_EQ(packet.timestamp, next_ts);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158
henrik.lundin@webrtc.org116ed1d2014-04-28 08:20:04 +0000159 // Flush buffer to delete all packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000160 buffer.Flush();
161}
162
163// Test inserting a list of packets.
164TEST(PacketBuffer, InsertPacketList) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700165 TickTimer tick_timer;
166 PacketBuffer buffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 PacketGenerator gen(0, 0, 0, 10);
168 PacketList list;
169 const int payload_len = 10;
170
171 // Insert 10 small packets.
172 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700173 list.push_back(gen.NextPacket(payload_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000174 }
175
176 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700177 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700178 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700179 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
180 .WillRepeatedly(Return(&info));
minyue-webrtc12d30842017-07-19 11:44:06 +0200181
182 StrictMock<MockStatisticsCalculator> mock_stats;
183
henrik.lundinda8bbf62016-08-31 03:14:11 -0700184 rtc::Optional<uint8_t> current_pt;
185 rtc::Optional<uint8_t> current_cng_pt;
minyue-webrtc12d30842017-07-19 11:44:06 +0200186 EXPECT_EQ(PacketBuffer::kOK,
187 buffer.InsertPacketList(&list, decoder_database, &current_pt,
188 &current_cng_pt, &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700190 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundinda8bbf62016-08-31 03:14:11 -0700191 EXPECT_EQ(rtc::Optional<uint8_t>(0),
192 current_pt); // Current payload type changed to 0.
193 EXPECT_FALSE(current_cng_pt); // CNG payload type not changed.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194
195 buffer.Flush(); // Clean up.
196
197 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
198}
199
200// Test inserting a list of packets. Last packet is of a different payload type.
201// Expecting the buffer to flush.
202// TODO(hlundin): Remove this test when legacy operation is no longer needed.
203TEST(PacketBuffer, InsertPacketListChangePayloadType) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700204 TickTimer tick_timer;
205 PacketBuffer buffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206 PacketGenerator gen(0, 0, 0, 10);
207 PacketList list;
208 const int payload_len = 10;
209
210 // Insert 10 small packets.
211 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700212 list.push_back(gen.NextPacket(payload_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000213 }
214 // Insert 11th packet of another payload type (not CNG).
ossua73f6c92016-10-24 08:25:28 -0700215 {
216 Packet packet = gen.NextPacket(payload_len);
217 packet.payload_type = 1;
218 list.push_back(std::move(packet));
219 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000220
221 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700222 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700223 const DecoderDatabase::DecoderInfo info0(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700224 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
225 .WillRepeatedly(Return(&info0));
ossuf1b08da2016-09-23 02:19:43 -0700226 const DecoderDatabase::DecoderInfo info1(NetEqDecoder::kDecoderPCMa, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700227 EXPECT_CALL(decoder_database, GetDecoderInfo(1))
228 .WillRepeatedly(Return(&info1));
minyue-webrtc12d30842017-07-19 11:44:06 +0200229
230 StrictMock<MockStatisticsCalculator> mock_stats;
231
henrik.lundinda8bbf62016-08-31 03:14:11 -0700232 rtc::Optional<uint8_t> current_pt;
233 rtc::Optional<uint8_t> current_cng_pt;
minyue-webrtc12d30842017-07-19 11:44:06 +0200234 EXPECT_EQ(PacketBuffer::kFlushed,
235 buffer.InsertPacketList(&list, decoder_database, &current_pt,
236 &current_cng_pt, &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000237 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700238 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); // Only the last packet.
henrik.lundinda8bbf62016-08-31 03:14:11 -0700239 EXPECT_EQ(rtc::Optional<uint8_t>(1),
240 current_pt); // Current payload type changed to 1.
241 EXPECT_FALSE(current_cng_pt); // CNG payload type not changed.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000242
243 buffer.Flush(); // Clean up.
244
245 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
246}
247
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000248TEST(PacketBuffer, ExtractOrderRedundancy) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700249 TickTimer tick_timer;
250 PacketBuffer buffer(100, &tick_timer); // 100 packets.
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000251 const int kPackets = 18;
252 const int kFrameSize = 10;
253 const int kPayloadLength = 10;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000254
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000255 PacketsToInsert packet_facts[kPackets] = {
256 {0xFFFD, 0xFFFFFFD7, 0, true, 0},
257 {0xFFFE, 0xFFFFFFE1, 0, true, 1},
258 {0xFFFE, 0xFFFFFFD7, 1, false, -1},
259 {0xFFFF, 0xFFFFFFEB, 0, true, 2},
260 {0xFFFF, 0xFFFFFFE1, 1, false, -1},
261 {0x0000, 0xFFFFFFF5, 0, true, 3},
262 {0x0000, 0xFFFFFFEB, 1, false, -1},
263 {0x0001, 0xFFFFFFFF, 0, true, 4},
264 {0x0001, 0xFFFFFFF5, 1, false, -1},
265 {0x0002, 0x0000000A, 0, true, 5},
266 {0x0002, 0xFFFFFFFF, 1, false, -1},
267 {0x0003, 0x0000000A, 1, false, -1},
268 {0x0004, 0x0000001E, 0, true, 7},
269 {0x0004, 0x00000014, 1, false, 6},
270 {0x0005, 0x0000001E, 0, true, -1},
271 {0x0005, 0x00000014, 1, false, -1},
272 {0x0006, 0x00000028, 0, true, 8},
273 {0x0006, 0x0000001E, 1, false, -1},
274 };
275
Peter Kastingdce40cf2015-08-24 14:52:23 -0700276 const size_t kExpectPacketsInBuffer = 9;
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000277
ossua73f6c92016-10-24 08:25:28 -0700278 std::vector<Packet> expect_order(kExpectPacketsInBuffer);
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000279
280 PacketGenerator gen(0, 0, 0, kFrameSize);
281
minyue-webrtc12d30842017-07-19 11:44:06 +0200282 StrictMock<MockStatisticsCalculator> mock_stats;
283
284 // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
285 // check ensures that exactly one call to PacketsDiscarded happens in each
286 // DiscardNextPacket call.
287 InSequence s;
288 MockFunction<void(int check_point_id)> check;
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000289 for (int i = 0; i < kPackets; ++i) {
290 gen.Reset(packet_facts[i].sequence_number,
291 packet_facts[i].timestamp,
292 packet_facts[i].payload_type,
293 kFrameSize);
ossua73f6c92016-10-24 08:25:28 -0700294 Packet packet = gen.NextPacket(kPayloadLength);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200295 packet.priority.codec_level = packet_facts[i].primary ? 0 : 1;
minyue-webrtc12d30842017-07-19 11:44:06 +0200296 if (packet_facts[i].extract_order < 0) {
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200297 if (packet.priority.codec_level > 0) {
298 EXPECT_CALL(mock_stats, SecondaryPacketsDiscarded(1));
299 } else {
300 EXPECT_CALL(mock_stats, PacketsDiscarded(1));
301 }
minyue-webrtc12d30842017-07-19 11:44:06 +0200302 }
303 EXPECT_CALL(check, Call(i));
304 EXPECT_EQ(PacketBuffer::kOK,
305 buffer.InsertPacket(packet.Clone(), &mock_stats));
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000306 if (packet_facts[i].extract_order >= 0) {
ossua73f6c92016-10-24 08:25:28 -0700307 expect_order[packet_facts[i].extract_order] = std::move(packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 }
minyue-webrtc12d30842017-07-19 11:44:06 +0200309 check.Call(i);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000310 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000312 EXPECT_EQ(kExpectPacketsInBuffer, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313
Peter Kastingdce40cf2015-08-24 14:52:23 -0700314 for (size_t i = 0; i < kExpectPacketsInBuffer; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700315 const rtc::Optional<Packet> packet = buffer.GetNextPacket();
316 EXPECT_EQ(packet, expect_order[i]); // Compare contents.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317 }
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000318 EXPECT_TRUE(buffer.Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000319}
320
321TEST(PacketBuffer, DiscardPackets) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700322 TickTimer tick_timer;
323 PacketBuffer buffer(100, &tick_timer); // 100 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000324 const uint16_t start_seq_no = 17;
325 const uint32_t start_ts = 4711;
326 const uint32_t ts_increment = 10;
327 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
328 PacketList list;
329 const int payload_len = 10;
minyue-webrtc12d30842017-07-19 11:44:06 +0200330 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331
minyue-webrtcfae474c2017-07-05 11:17:40 +0200332 constexpr int kTotalPackets = 10;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 // Insert 10 small packets.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200334 for (int i = 0; i < kTotalPackets; ++i) {
minyue-webrtc12d30842017-07-19 11:44:06 +0200335 buffer.InsertPacket(gen.NextPacket(payload_len), &mock_stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700337 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000338
minyue-webrtcfae474c2017-07-05 11:17:40 +0200339 uint32_t current_ts = start_ts;
340
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341 // Discard them one by one and make sure that the right packets are at the
342 // front of the buffer.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200343 constexpr int kDiscardPackets = 5;
henrik.lundin63d146b2017-07-05 07:03:34 -0700344
345 // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
346 // check ensures that exactly one call to PacketsDiscarded happens in each
347 // DiscardNextPacket call.
348 InSequence s;
349 MockFunction<void(int check_point_id)> check;
minyue-webrtcfae474c2017-07-05 11:17:40 +0200350 for (int i = 0; i < kDiscardPackets; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000351 uint32_t ts;
352 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
353 EXPECT_EQ(current_ts, ts);
henrik.lundin63d146b2017-07-05 07:03:34 -0700354 EXPECT_CALL(mock_stats, PacketsDiscarded(1));
355 EXPECT_CALL(check, Call(i));
minyue-webrtcfae474c2017-07-05 11:17:40 +0200356 EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket(&mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 current_ts += ts_increment;
henrik.lundin63d146b2017-07-05 07:03:34 -0700358 check.Call(i);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000359 }
minyue-webrtcfae474c2017-07-05 11:17:40 +0200360
361 constexpr int kRemainingPackets = kTotalPackets - kDiscardPackets;
henrik.lundin63d146b2017-07-05 07:03:34 -0700362 // This will discard all remaining packets but one. The oldest packet is older
363 // than the indicated horizon_samples, and will thus be left in the buffer.
364 constexpr size_t kSkipPackets = 1;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200365 EXPECT_CALL(mock_stats, PacketsDiscarded(1))
366 .Times(kRemainingPackets - kSkipPackets);
henrik.lundin63d146b2017-07-05 07:03:34 -0700367 EXPECT_CALL(check, Call(17)); // Arbitrary id number.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200368 buffer.DiscardOldPackets(start_ts + kTotalPackets * ts_increment,
369 kRemainingPackets * ts_increment, &mock_stats);
henrik.lundin63d146b2017-07-05 07:03:34 -0700370 check.Call(17); // Same arbitrary id number.
371
372 EXPECT_EQ(kSkipPackets, buffer.NumPacketsInBuffer());
minyue-webrtcfae474c2017-07-05 11:17:40 +0200373 uint32_t ts;
374 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
375 EXPECT_EQ(current_ts, ts);
376
377 // Discard all remaining packets.
henrik.lundin63d146b2017-07-05 07:03:34 -0700378 EXPECT_CALL(mock_stats, PacketsDiscarded(kSkipPackets));
minyue-webrtcfae474c2017-07-05 11:17:40 +0200379 buffer.DiscardAllOldPackets(start_ts + kTotalPackets * ts_increment,
380 &mock_stats);
381
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000382 EXPECT_TRUE(buffer.Empty());
383}
384
385TEST(PacketBuffer, Reordering) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700386 TickTimer tick_timer;
387 PacketBuffer buffer(100, &tick_timer); // 100 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000388 const uint16_t start_seq_no = 17;
389 const uint32_t start_ts = 4711;
390 const uint32_t ts_increment = 10;
391 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
392 const int payload_len = 10;
393
394 // Generate 10 small packets and insert them into a PacketList. Insert every
395 // odd packet to the front, and every even packet to the back, thus creating
396 // a (rather strange) reordering.
397 PacketList list;
398 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700399 Packet packet = gen.NextPacket(payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400 if (i % 2) {
ossua73f6c92016-10-24 08:25:28 -0700401 list.push_front(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000402 } else {
ossua73f6c92016-10-24 08:25:28 -0700403 list.push_back(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000404 }
405 }
406
407 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700408 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700409 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700410 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
411 .WillRepeatedly(Return(&info));
henrik.lundinda8bbf62016-08-31 03:14:11 -0700412 rtc::Optional<uint8_t> current_pt;
413 rtc::Optional<uint8_t> current_cng_pt;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000414
minyue-webrtc12d30842017-07-19 11:44:06 +0200415 StrictMock<MockStatisticsCalculator> mock_stats;
416
417 EXPECT_EQ(PacketBuffer::kOK,
418 buffer.InsertPacketList(&list, decoder_database, &current_pt,
419 &current_cng_pt, &mock_stats));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700420 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000421
422 // Extract them and make sure that come out in the right order.
423 uint32_t current_ts = start_ts;
424 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700425 const rtc::Optional<Packet> packet = buffer.GetNextPacket();
426 ASSERT_TRUE(packet);
ossu7a377612016-10-18 04:06:13 -0700427 EXPECT_EQ(current_ts, packet->timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000428 current_ts += ts_increment;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000429 }
430 EXPECT_TRUE(buffer.Empty());
431
432 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
433}
434
henrik.lundin067d8552016-09-01 23:19:05 -0700435// The test first inserts a packet with narrow-band CNG, then a packet with
436// wide-band speech. The expected behavior of the packet buffer is to detect a
437// change in sample rate, even though no speech packet has been inserted before,
438// and flush out the CNG packet.
439TEST(PacketBuffer, CngFirstThenSpeechWithNewSampleRate) {
440 TickTimer tick_timer;
441 PacketBuffer buffer(10, &tick_timer); // 10 packets.
442 const uint8_t kCngPt = 13;
443 const int kPayloadLen = 10;
444 const uint8_t kSpeechPt = 100;
445
446 MockDecoderDatabase decoder_database;
447 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700448 const DecoderDatabase::DecoderInfo info_cng(NetEqDecoder::kDecoderCNGnb,
henrik.lundin067d8552016-09-01 23:19:05 -0700449 factory);
450 EXPECT_CALL(decoder_database, GetDecoderInfo(kCngPt))
451 .WillRepeatedly(Return(&info_cng));
452 const DecoderDatabase::DecoderInfo info_speech(NetEqDecoder::kDecoderPCM16Bwb,
ossuf1b08da2016-09-23 02:19:43 -0700453 factory);
henrik.lundin067d8552016-09-01 23:19:05 -0700454 EXPECT_CALL(decoder_database, GetDecoderInfo(kSpeechPt))
455 .WillRepeatedly(Return(&info_speech));
456
457 // Insert first packet, which is narrow-band CNG.
458 PacketGenerator gen(0, 0, kCngPt, 10);
459 PacketList list;
460 list.push_back(gen.NextPacket(kPayloadLen));
461 rtc::Optional<uint8_t> current_pt;
462 rtc::Optional<uint8_t> current_cng_pt;
minyue-webrtc12d30842017-07-19 11:44:06 +0200463
464 StrictMock<MockStatisticsCalculator> mock_stats;
465
henrik.lundin067d8552016-09-01 23:19:05 -0700466 EXPECT_EQ(PacketBuffer::kOK,
467 buffer.InsertPacketList(&list, decoder_database, &current_pt,
minyue-webrtc12d30842017-07-19 11:44:06 +0200468 &current_cng_pt, &mock_stats));
henrik.lundin067d8552016-09-01 23:19:05 -0700469 EXPECT_TRUE(list.empty());
470 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700471 ASSERT_TRUE(buffer.PeekNextPacket());
472 EXPECT_EQ(kCngPt, buffer.PeekNextPacket()->payload_type);
henrik.lundin067d8552016-09-01 23:19:05 -0700473 EXPECT_FALSE(current_pt); // Current payload type not set.
474 EXPECT_EQ(rtc::Optional<uint8_t>(kCngPt),
475 current_cng_pt); // CNG payload type set.
476
477 // Insert second packet, which is wide-band speech.
ossua73f6c92016-10-24 08:25:28 -0700478 {
479 Packet packet = gen.NextPacket(kPayloadLen);
480 packet.payload_type = kSpeechPt;
481 list.push_back(std::move(packet));
482 }
henrik.lundin067d8552016-09-01 23:19:05 -0700483 // Expect the buffer to flush out the CNG packet, since it does not match the
484 // new speech sample rate.
485 EXPECT_EQ(PacketBuffer::kFlushed,
486 buffer.InsertPacketList(&list, decoder_database, &current_pt,
minyue-webrtc12d30842017-07-19 11:44:06 +0200487 &current_cng_pt, &mock_stats));
henrik.lundin067d8552016-09-01 23:19:05 -0700488 EXPECT_TRUE(list.empty());
489 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700490 ASSERT_TRUE(buffer.PeekNextPacket());
491 EXPECT_EQ(kSpeechPt, buffer.PeekNextPacket()->payload_type);
henrik.lundin067d8552016-09-01 23:19:05 -0700492
493 EXPECT_EQ(rtc::Optional<uint8_t>(kSpeechPt),
494 current_pt); // Current payload type set.
495 EXPECT_FALSE(current_cng_pt); // CNG payload type reset.
496
497 buffer.Flush(); // Clean up.
498 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
499}
500
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000501TEST(PacketBuffer, Failures) {
502 const uint16_t start_seq_no = 17;
503 const uint32_t start_ts = 4711;
504 const uint32_t ts_increment = 10;
505 int payload_len = 100;
506 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
henrik.lundin84f8cd62016-04-26 07:45:16 -0700507 TickTimer tick_timer;
minyue-webrtc12d30842017-07-19 11:44:06 +0200508 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000509
henrik.lundin84f8cd62016-04-26 07:45:16 -0700510 PacketBuffer* buffer = new PacketBuffer(100, &tick_timer); // 100 packets.
ossua73f6c92016-10-24 08:25:28 -0700511 {
512 Packet packet = gen.NextPacket(payload_len);
513 packet.payload.Clear();
514 EXPECT_EQ(PacketBuffer::kInvalidPacket,
minyue-webrtc12d30842017-07-19 11:44:06 +0200515 buffer->InsertPacket(std::move(packet), &mock_stats));
ossua73f6c92016-10-24 08:25:28 -0700516 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000517 // Buffer should still be empty. Test all empty-checks.
518 uint32_t temp_ts;
519 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->NextTimestamp(&temp_ts));
520 EXPECT_EQ(PacketBuffer::kBufferEmpty,
521 buffer->NextHigherTimestamp(0, &temp_ts));
ossu7a377612016-10-18 04:06:13 -0700522 EXPECT_EQ(NULL, buffer->PeekNextPacket());
ossua73f6c92016-10-24 08:25:28 -0700523 EXPECT_FALSE(buffer->GetNextPacket());
minyue-webrtcfae474c2017-07-05 11:17:40 +0200524
minyue-webrtcfae474c2017-07-05 11:17:40 +0200525 // Discarding packets will not invoke mock_stats.PacketDiscarded() because the
526 // packet buffer is empty.
527 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->DiscardNextPacket(&mock_stats));
528 buffer->DiscardAllOldPackets(0, &mock_stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000529
530 // Insert one packet to make the buffer non-empty.
ossua73f6c92016-10-24 08:25:28 -0700531 EXPECT_EQ(PacketBuffer::kOK,
minyue-webrtc12d30842017-07-19 11:44:06 +0200532 buffer->InsertPacket(gen.NextPacket(payload_len), &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000533 EXPECT_EQ(PacketBuffer::kInvalidPointer, buffer->NextTimestamp(NULL));
534 EXPECT_EQ(PacketBuffer::kInvalidPointer,
535 buffer->NextHigherTimestamp(0, NULL));
536 delete buffer;
537
538 // Insert packet list of three packets, where the second packet has an invalid
539 // payload. Expect first packet to be inserted, and the remaining two to be
540 // discarded.
henrik.lundin84f8cd62016-04-26 07:45:16 -0700541 buffer = new PacketBuffer(100, &tick_timer); // 100 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000542 PacketList list;
543 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
ossua73f6c92016-10-24 08:25:28 -0700544 {
545 Packet packet = gen.NextPacket(payload_len);
546 packet.payload.Clear(); // Invalid.
547 list.push_back(std::move(packet));
548 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000549 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
550 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700551 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700552 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700553 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
554 .WillRepeatedly(Return(&info));
henrik.lundinda8bbf62016-08-31 03:14:11 -0700555 rtc::Optional<uint8_t> current_pt;
556 rtc::Optional<uint8_t> current_cng_pt;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000557 EXPECT_EQ(PacketBuffer::kInvalidPacket,
minyue-webrtc12d30842017-07-19 11:44:06 +0200558 buffer->InsertPacketList(&list, decoder_database, &current_pt,
559 &current_cng_pt, &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000560 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700561 EXPECT_EQ(1u, buffer->NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000562 delete buffer;
563 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
564}
565
566// Test packet comparison function.
567// The function should return true if the first packet "goes before" the second.
568TEST(PacketBuffer, ComparePackets) {
569 PacketGenerator gen(0, 0, 0, 10);
ossua73f6c92016-10-24 08:25:28 -0700570 Packet a(gen.NextPacket(10)); // SN = 0, TS = 0.
571 Packet b(gen.NextPacket(10)); // SN = 1, TS = 10.
572 EXPECT_FALSE(a == b);
573 EXPECT_TRUE(a != b);
574 EXPECT_TRUE(a < b);
575 EXPECT_FALSE(a > b);
576 EXPECT_TRUE(a <= b);
577 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000578
579 // Testing wrap-around case; 'a' is earlier but has a larger timestamp value.
ossua73f6c92016-10-24 08:25:28 -0700580 a.timestamp = 0xFFFFFFFF - 10;
581 EXPECT_FALSE(a == b);
582 EXPECT_TRUE(a != b);
583 EXPECT_TRUE(a < b);
584 EXPECT_FALSE(a > b);
585 EXPECT_TRUE(a <= b);
586 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000587
588 // Test equal packets.
ossua73f6c92016-10-24 08:25:28 -0700589 EXPECT_TRUE(a == a);
590 EXPECT_FALSE(a != a);
591 EXPECT_FALSE(a < a);
592 EXPECT_FALSE(a > a);
593 EXPECT_TRUE(a <= a);
594 EXPECT_TRUE(a >= a);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000595
596 // Test equal timestamps but different sequence numbers (0 and 1).
ossua73f6c92016-10-24 08:25:28 -0700597 a.timestamp = b.timestamp;
598 EXPECT_FALSE(a == b);
599 EXPECT_TRUE(a != b);
600 EXPECT_TRUE(a < b);
601 EXPECT_FALSE(a > b);
602 EXPECT_TRUE(a <= b);
603 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000604
605 // Test equal timestamps but different sequence numbers (32767 and 1).
ossua73f6c92016-10-24 08:25:28 -0700606 a.sequence_number = 0xFFFF;
607 EXPECT_FALSE(a == b);
608 EXPECT_TRUE(a != b);
609 EXPECT_TRUE(a < b);
610 EXPECT_FALSE(a > b);
611 EXPECT_TRUE(a <= b);
612 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000613
ossua70695a2016-09-22 02:06:28 -0700614 // Test equal timestamps and sequence numbers, but differing priorities.
ossua73f6c92016-10-24 08:25:28 -0700615 a.sequence_number = b.sequence_number;
616 a.priority = {1, 0};
617 b.priority = {0, 0};
ossua70695a2016-09-22 02:06:28 -0700618 // a after b
ossua73f6c92016-10-24 08:25:28 -0700619 EXPECT_FALSE(a == b);
620 EXPECT_TRUE(a != b);
621 EXPECT_FALSE(a < b);
622 EXPECT_TRUE(a > b);
623 EXPECT_FALSE(a <= b);
624 EXPECT_TRUE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000625
ossua73f6c92016-10-24 08:25:28 -0700626 Packet c(gen.NextPacket(0)); // SN = 2, TS = 20.
627 Packet d(gen.NextPacket(0)); // SN = 3, TS = 20.
628 c.timestamp = b.timestamp;
629 d.timestamp = b.timestamp;
630 c.sequence_number = b.sequence_number;
631 d.sequence_number = b.sequence_number;
632 c.priority = {1, 1};
633 d.priority = {0, 1};
ossua70695a2016-09-22 02:06:28 -0700634 // c after d
ossua73f6c92016-10-24 08:25:28 -0700635 EXPECT_FALSE(c == d);
636 EXPECT_TRUE(c != d);
637 EXPECT_FALSE(c < d);
638 EXPECT_TRUE(c > d);
639 EXPECT_FALSE(c <= d);
640 EXPECT_TRUE(c >= d);
ossua70695a2016-09-22 02:06:28 -0700641
642 // c after a
ossua73f6c92016-10-24 08:25:28 -0700643 EXPECT_FALSE(c == a);
644 EXPECT_TRUE(c != a);
645 EXPECT_FALSE(c < a);
646 EXPECT_TRUE(c > a);
647 EXPECT_FALSE(c <= a);
648 EXPECT_TRUE(c >= a);
ossua70695a2016-09-22 02:06:28 -0700649
650 // c after b
ossua73f6c92016-10-24 08:25:28 -0700651 EXPECT_FALSE(c == b);
652 EXPECT_TRUE(c != b);
653 EXPECT_FALSE(c < b);
654 EXPECT_TRUE(c > b);
655 EXPECT_FALSE(c <= b);
656 EXPECT_TRUE(c >= b);
ossua70695a2016-09-22 02:06:28 -0700657
658 // a after d
ossua73f6c92016-10-24 08:25:28 -0700659 EXPECT_FALSE(a == d);
660 EXPECT_TRUE(a != d);
661 EXPECT_FALSE(a < d);
662 EXPECT_TRUE(a > d);
663 EXPECT_FALSE(a <= d);
664 EXPECT_TRUE(a >= d);
ossua70695a2016-09-22 02:06:28 -0700665
666 // d after b
ossua73f6c92016-10-24 08:25:28 -0700667 EXPECT_FALSE(d == b);
668 EXPECT_TRUE(d != b);
669 EXPECT_FALSE(d < b);
670 EXPECT_TRUE(d > b);
671 EXPECT_FALSE(d <= b);
672 EXPECT_TRUE(d >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000673}
674
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000675namespace {
676void TestIsObsoleteTimestamp(uint32_t limit_timestamp) {
677 // Check with zero horizon, which implies that the horizon is at 2^31, i.e.,
678 // half the timestamp range.
679 static const uint32_t kZeroHorizon = 0;
680 static const uint32_t k2Pow31Minus1 = 0x7FFFFFFF;
681 // Timestamp on the limit is not old.
682 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
683 limit_timestamp, limit_timestamp, kZeroHorizon));
684 // 1 sample behind is old.
685 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
686 limit_timestamp - 1, limit_timestamp, kZeroHorizon));
687 // 2^31 - 1 samples behind is old.
688 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
689 limit_timestamp - k2Pow31Minus1, limit_timestamp, kZeroHorizon));
690 // 1 sample ahead is not old.
691 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
692 limit_timestamp + 1, limit_timestamp, kZeroHorizon));
Cesar Magalhaesf69f1fb2015-05-30 17:49:18 +0200693 // If |t1-t2|=2^31 and t1>t2, t2 is older than t1 but not the opposite.
694 uint32_t other_timestamp = limit_timestamp + (1 << 31);
695 uint32_t lowest_timestamp = std::min(limit_timestamp, other_timestamp);
696 uint32_t highest_timestamp = std::max(limit_timestamp, other_timestamp);
697 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
698 lowest_timestamp, highest_timestamp, kZeroHorizon));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000699 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
Cesar Magalhaesf69f1fb2015-05-30 17:49:18 +0200700 highest_timestamp, lowest_timestamp, kZeroHorizon));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000701
702 // Fixed horizon at 10 samples.
703 static const uint32_t kHorizon = 10;
704 // Timestamp on the limit is not old.
705 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
706 limit_timestamp, limit_timestamp, kHorizon));
707 // 1 sample behind is old.
708 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
709 limit_timestamp - 1, limit_timestamp, kHorizon));
710 // 9 samples behind is old.
711 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
712 limit_timestamp - 9, limit_timestamp, kHorizon));
713 // 10 samples behind is not old.
714 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
715 limit_timestamp - 10, limit_timestamp, kHorizon));
716 // 2^31 - 1 samples behind is not old.
717 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
718 limit_timestamp - k2Pow31Minus1, limit_timestamp, kHorizon));
719 // 1 sample ahead is not old.
720 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
721 limit_timestamp + 1, limit_timestamp, kHorizon));
722 // 2^31 samples ahead is not old.
723 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
724 limit_timestamp + (1 << 31), limit_timestamp, kHorizon));
725}
726} // namespace
727
728// Test the IsObsoleteTimestamp method with different limit timestamps.
729TEST(PacketBuffer, IsObsoleteTimestamp) {
730 TestIsObsoleteTimestamp(0);
731 TestIsObsoleteTimestamp(1);
732 TestIsObsoleteTimestamp(0xFFFFFFFF); // -1 in uint32_t.
733 TestIsObsoleteTimestamp(0x80000000); // 2^31.
734 TestIsObsoleteTimestamp(0x80000001); // 2^31 + 1.
735 TestIsObsoleteTimestamp(0x7FFFFFFF); // 2^31 - 1.
736}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000737} // namespace webrtc