blob: 0ddeb8a58540d8ed4c6674d1c4cf651c556ce3e1 [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());
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100191 EXPECT_EQ(0, current_pt); // Current payload type changed to 0.
192 EXPECT_EQ(rtc::nullopt, current_cng_pt); // CNG payload type not changed.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193
194 buffer.Flush(); // Clean up.
195
196 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
197}
198
199// Test inserting a list of packets. Last packet is of a different payload type.
200// Expecting the buffer to flush.
201// TODO(hlundin): Remove this test when legacy operation is no longer needed.
202TEST(PacketBuffer, InsertPacketListChangePayloadType) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700203 TickTimer tick_timer;
204 PacketBuffer buffer(10, &tick_timer); // 10 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205 PacketGenerator gen(0, 0, 0, 10);
206 PacketList list;
207 const int payload_len = 10;
208
209 // Insert 10 small packets.
210 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700211 list.push_back(gen.NextPacket(payload_len));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000212 }
213 // Insert 11th packet of another payload type (not CNG).
ossua73f6c92016-10-24 08:25:28 -0700214 {
215 Packet packet = gen.NextPacket(payload_len);
216 packet.payload_type = 1;
217 list.push_back(std::move(packet));
218 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000219
220 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700221 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700222 const DecoderDatabase::DecoderInfo info0(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700223 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
224 .WillRepeatedly(Return(&info0));
ossuf1b08da2016-09-23 02:19:43 -0700225 const DecoderDatabase::DecoderInfo info1(NetEqDecoder::kDecoderPCMa, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700226 EXPECT_CALL(decoder_database, GetDecoderInfo(1))
227 .WillRepeatedly(Return(&info1));
minyue-webrtc12d30842017-07-19 11:44:06 +0200228
229 StrictMock<MockStatisticsCalculator> mock_stats;
230
henrik.lundinda8bbf62016-08-31 03:14:11 -0700231 rtc::Optional<uint8_t> current_pt;
232 rtc::Optional<uint8_t> current_cng_pt;
minyue-webrtc12d30842017-07-19 11:44:06 +0200233 EXPECT_EQ(PacketBuffer::kFlushed,
234 buffer.InsertPacketList(&list, decoder_database, &current_pt,
235 &current_cng_pt, &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000236 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700237 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); // Only the last packet.
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100238 EXPECT_EQ(1, current_pt); // Current payload type changed to 1.
239 EXPECT_EQ(rtc::nullopt, current_cng_pt); // CNG payload type not changed.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000240
241 buffer.Flush(); // Clean up.
242
243 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
244}
245
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000246TEST(PacketBuffer, ExtractOrderRedundancy) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700247 TickTimer tick_timer;
248 PacketBuffer buffer(100, &tick_timer); // 100 packets.
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000249 const int kPackets = 18;
250 const int kFrameSize = 10;
251 const int kPayloadLength = 10;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000252
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000253 PacketsToInsert packet_facts[kPackets] = {
254 {0xFFFD, 0xFFFFFFD7, 0, true, 0},
255 {0xFFFE, 0xFFFFFFE1, 0, true, 1},
256 {0xFFFE, 0xFFFFFFD7, 1, false, -1},
257 {0xFFFF, 0xFFFFFFEB, 0, true, 2},
258 {0xFFFF, 0xFFFFFFE1, 1, false, -1},
259 {0x0000, 0xFFFFFFF5, 0, true, 3},
260 {0x0000, 0xFFFFFFEB, 1, false, -1},
261 {0x0001, 0xFFFFFFFF, 0, true, 4},
262 {0x0001, 0xFFFFFFF5, 1, false, -1},
263 {0x0002, 0x0000000A, 0, true, 5},
264 {0x0002, 0xFFFFFFFF, 1, false, -1},
265 {0x0003, 0x0000000A, 1, false, -1},
266 {0x0004, 0x0000001E, 0, true, 7},
267 {0x0004, 0x00000014, 1, false, 6},
268 {0x0005, 0x0000001E, 0, true, -1},
269 {0x0005, 0x00000014, 1, false, -1},
270 {0x0006, 0x00000028, 0, true, 8},
271 {0x0006, 0x0000001E, 1, false, -1},
272 };
273
Peter Kastingdce40cf2015-08-24 14:52:23 -0700274 const size_t kExpectPacketsInBuffer = 9;
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000275
ossua73f6c92016-10-24 08:25:28 -0700276 std::vector<Packet> expect_order(kExpectPacketsInBuffer);
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000277
278 PacketGenerator gen(0, 0, 0, kFrameSize);
279
minyue-webrtc12d30842017-07-19 11:44:06 +0200280 StrictMock<MockStatisticsCalculator> mock_stats;
281
282 // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
283 // check ensures that exactly one call to PacketsDiscarded happens in each
284 // DiscardNextPacket call.
285 InSequence s;
286 MockFunction<void(int check_point_id)> check;
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000287 for (int i = 0; i < kPackets; ++i) {
288 gen.Reset(packet_facts[i].sequence_number,
289 packet_facts[i].timestamp,
290 packet_facts[i].payload_type,
291 kFrameSize);
ossua73f6c92016-10-24 08:25:28 -0700292 Packet packet = gen.NextPacket(kPayloadLength);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200293 packet.priority.codec_level = packet_facts[i].primary ? 0 : 1;
minyue-webrtc12d30842017-07-19 11:44:06 +0200294 if (packet_facts[i].extract_order < 0) {
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200295 if (packet.priority.codec_level > 0) {
296 EXPECT_CALL(mock_stats, SecondaryPacketsDiscarded(1));
297 } else {
298 EXPECT_CALL(mock_stats, PacketsDiscarded(1));
299 }
minyue-webrtc12d30842017-07-19 11:44:06 +0200300 }
301 EXPECT_CALL(check, Call(i));
302 EXPECT_EQ(PacketBuffer::kOK,
303 buffer.InsertPacket(packet.Clone(), &mock_stats));
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000304 if (packet_facts[i].extract_order >= 0) {
ossua73f6c92016-10-24 08:25:28 -0700305 expect_order[packet_facts[i].extract_order] = std::move(packet);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000306 }
minyue-webrtc12d30842017-07-19 11:44:06 +0200307 check.Call(i);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000309
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000310 EXPECT_EQ(kExpectPacketsInBuffer, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311
Peter Kastingdce40cf2015-08-24 14:52:23 -0700312 for (size_t i = 0; i < kExpectPacketsInBuffer; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700313 const rtc::Optional<Packet> packet = buffer.GetNextPacket();
314 EXPECT_EQ(packet, expect_order[i]); // Compare contents.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000315 }
minyue@webrtc.orgc8039072014-10-09 10:49:54 +0000316 EXPECT_TRUE(buffer.Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000317}
318
319TEST(PacketBuffer, DiscardPackets) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700320 TickTimer tick_timer;
321 PacketBuffer buffer(100, &tick_timer); // 100 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322 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 PacketList list;
327 const int payload_len = 10;
minyue-webrtc12d30842017-07-19 11:44:06 +0200328 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000329
minyue-webrtcfae474c2017-07-05 11:17:40 +0200330 constexpr int kTotalPackets = 10;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000331 // Insert 10 small packets.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200332 for (int i = 0; i < kTotalPackets; ++i) {
minyue-webrtc12d30842017-07-19 11:44:06 +0200333 buffer.InsertPacket(gen.NextPacket(payload_len), &mock_stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700335 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000336
minyue-webrtcfae474c2017-07-05 11:17:40 +0200337 uint32_t current_ts = start_ts;
338
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000339 // Discard them one by one and make sure that the right packets are at the
340 // front of the buffer.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200341 constexpr int kDiscardPackets = 5;
henrik.lundin63d146b2017-07-05 07:03:34 -0700342
343 // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
344 // check ensures that exactly one call to PacketsDiscarded happens in each
345 // DiscardNextPacket call.
346 InSequence s;
347 MockFunction<void(int check_point_id)> check;
minyue-webrtcfae474c2017-07-05 11:17:40 +0200348 for (int i = 0; i < kDiscardPackets; ++i) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349 uint32_t ts;
350 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
351 EXPECT_EQ(current_ts, ts);
henrik.lundin63d146b2017-07-05 07:03:34 -0700352 EXPECT_CALL(mock_stats, PacketsDiscarded(1));
353 EXPECT_CALL(check, Call(i));
minyue-webrtcfae474c2017-07-05 11:17:40 +0200354 EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket(&mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000355 current_ts += ts_increment;
henrik.lundin63d146b2017-07-05 07:03:34 -0700356 check.Call(i);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000357 }
minyue-webrtcfae474c2017-07-05 11:17:40 +0200358
359 constexpr int kRemainingPackets = kTotalPackets - kDiscardPackets;
henrik.lundin63d146b2017-07-05 07:03:34 -0700360 // This will discard all remaining packets but one. The oldest packet is older
361 // than the indicated horizon_samples, and will thus be left in the buffer.
362 constexpr size_t kSkipPackets = 1;
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200363 EXPECT_CALL(mock_stats, PacketsDiscarded(1))
364 .Times(kRemainingPackets - kSkipPackets);
henrik.lundin63d146b2017-07-05 07:03:34 -0700365 EXPECT_CALL(check, Call(17)); // Arbitrary id number.
minyue-webrtcfae474c2017-07-05 11:17:40 +0200366 buffer.DiscardOldPackets(start_ts + kTotalPackets * ts_increment,
367 kRemainingPackets * ts_increment, &mock_stats);
henrik.lundin63d146b2017-07-05 07:03:34 -0700368 check.Call(17); // Same arbitrary id number.
369
370 EXPECT_EQ(kSkipPackets, buffer.NumPacketsInBuffer());
minyue-webrtcfae474c2017-07-05 11:17:40 +0200371 uint32_t ts;
372 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
373 EXPECT_EQ(current_ts, ts);
374
375 // Discard all remaining packets.
henrik.lundin63d146b2017-07-05 07:03:34 -0700376 EXPECT_CALL(mock_stats, PacketsDiscarded(kSkipPackets));
minyue-webrtcfae474c2017-07-05 11:17:40 +0200377 buffer.DiscardAllOldPackets(start_ts + kTotalPackets * ts_increment,
378 &mock_stats);
379
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000380 EXPECT_TRUE(buffer.Empty());
381}
382
383TEST(PacketBuffer, Reordering) {
henrik.lundin84f8cd62016-04-26 07:45:16 -0700384 TickTimer tick_timer;
385 PacketBuffer buffer(100, &tick_timer); // 100 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000386 const uint16_t start_seq_no = 17;
387 const uint32_t start_ts = 4711;
388 const uint32_t ts_increment = 10;
389 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
390 const int payload_len = 10;
391
392 // Generate 10 small packets and insert them into a PacketList. Insert every
393 // odd packet to the front, and every even packet to the back, thus creating
394 // a (rather strange) reordering.
395 PacketList list;
396 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700397 Packet packet = gen.NextPacket(payload_len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000398 if (i % 2) {
ossua73f6c92016-10-24 08:25:28 -0700399 list.push_front(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000400 } else {
ossua73f6c92016-10-24 08:25:28 -0700401 list.push_back(std::move(packet));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000402 }
403 }
404
405 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700406 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700407 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700408 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
409 .WillRepeatedly(Return(&info));
henrik.lundinda8bbf62016-08-31 03:14:11 -0700410 rtc::Optional<uint8_t> current_pt;
411 rtc::Optional<uint8_t> current_cng_pt;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000412
minyue-webrtc12d30842017-07-19 11:44:06 +0200413 StrictMock<MockStatisticsCalculator> mock_stats;
414
415 EXPECT_EQ(PacketBuffer::kOK,
416 buffer.InsertPacketList(&list, decoder_database, &current_pt,
417 &current_cng_pt, &mock_stats));
Peter Kastingdce40cf2015-08-24 14:52:23 -0700418 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000419
420 // Extract them and make sure that come out in the right order.
421 uint32_t current_ts = start_ts;
422 for (int i = 0; i < 10; ++i) {
ossua73f6c92016-10-24 08:25:28 -0700423 const rtc::Optional<Packet> packet = buffer.GetNextPacket();
424 ASSERT_TRUE(packet);
ossu7a377612016-10-18 04:06:13 -0700425 EXPECT_EQ(current_ts, packet->timestamp);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000426 current_ts += ts_increment;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000427 }
428 EXPECT_TRUE(buffer.Empty());
429
430 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
431}
432
henrik.lundin067d8552016-09-01 23:19:05 -0700433// The test first inserts a packet with narrow-band CNG, then a packet with
434// wide-band speech. The expected behavior of the packet buffer is to detect a
435// change in sample rate, even though no speech packet has been inserted before,
436// and flush out the CNG packet.
437TEST(PacketBuffer, CngFirstThenSpeechWithNewSampleRate) {
438 TickTimer tick_timer;
439 PacketBuffer buffer(10, &tick_timer); // 10 packets.
440 const uint8_t kCngPt = 13;
441 const int kPayloadLen = 10;
442 const uint8_t kSpeechPt = 100;
443
444 MockDecoderDatabase decoder_database;
445 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700446 const DecoderDatabase::DecoderInfo info_cng(NetEqDecoder::kDecoderCNGnb,
henrik.lundin067d8552016-09-01 23:19:05 -0700447 factory);
448 EXPECT_CALL(decoder_database, GetDecoderInfo(kCngPt))
449 .WillRepeatedly(Return(&info_cng));
450 const DecoderDatabase::DecoderInfo info_speech(NetEqDecoder::kDecoderPCM16Bwb,
ossuf1b08da2016-09-23 02:19:43 -0700451 factory);
henrik.lundin067d8552016-09-01 23:19:05 -0700452 EXPECT_CALL(decoder_database, GetDecoderInfo(kSpeechPt))
453 .WillRepeatedly(Return(&info_speech));
454
455 // Insert first packet, which is narrow-band CNG.
456 PacketGenerator gen(0, 0, kCngPt, 10);
457 PacketList list;
458 list.push_back(gen.NextPacket(kPayloadLen));
459 rtc::Optional<uint8_t> current_pt;
460 rtc::Optional<uint8_t> current_cng_pt;
minyue-webrtc12d30842017-07-19 11:44:06 +0200461
462 StrictMock<MockStatisticsCalculator> mock_stats;
463
henrik.lundin067d8552016-09-01 23:19:05 -0700464 EXPECT_EQ(PacketBuffer::kOK,
465 buffer.InsertPacketList(&list, decoder_database, &current_pt,
minyue-webrtc12d30842017-07-19 11:44:06 +0200466 &current_cng_pt, &mock_stats));
henrik.lundin067d8552016-09-01 23:19:05 -0700467 EXPECT_TRUE(list.empty());
468 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700469 ASSERT_TRUE(buffer.PeekNextPacket());
470 EXPECT_EQ(kCngPt, buffer.PeekNextPacket()->payload_type);
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100471 EXPECT_EQ(current_pt, rtc::nullopt); // Current payload type not set.
472 EXPECT_EQ(kCngPt, current_cng_pt); // CNG payload type set.
henrik.lundin067d8552016-09-01 23:19:05 -0700473
474 // Insert second packet, which is wide-band speech.
ossua73f6c92016-10-24 08:25:28 -0700475 {
476 Packet packet = gen.NextPacket(kPayloadLen);
477 packet.payload_type = kSpeechPt;
478 list.push_back(std::move(packet));
479 }
henrik.lundin067d8552016-09-01 23:19:05 -0700480 // Expect the buffer to flush out the CNG packet, since it does not match the
481 // new speech sample rate.
482 EXPECT_EQ(PacketBuffer::kFlushed,
483 buffer.InsertPacketList(&list, decoder_database, &current_pt,
minyue-webrtc12d30842017-07-19 11:44:06 +0200484 &current_cng_pt, &mock_stats));
henrik.lundin067d8552016-09-01 23:19:05 -0700485 EXPECT_TRUE(list.empty());
486 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
ossu7a377612016-10-18 04:06:13 -0700487 ASSERT_TRUE(buffer.PeekNextPacket());
488 EXPECT_EQ(kSpeechPt, buffer.PeekNextPacket()->payload_type);
henrik.lundin067d8552016-09-01 23:19:05 -0700489
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100490 EXPECT_EQ(kSpeechPt, current_pt); // Current payload type set.
491 EXPECT_EQ(rtc::nullopt, current_cng_pt); // CNG payload type reset.
henrik.lundin067d8552016-09-01 23:19:05 -0700492
493 buffer.Flush(); // Clean up.
494 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
495}
496
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000497TEST(PacketBuffer, Failures) {
498 const uint16_t start_seq_no = 17;
499 const uint32_t start_ts = 4711;
500 const uint32_t ts_increment = 10;
501 int payload_len = 100;
502 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
henrik.lundin84f8cd62016-04-26 07:45:16 -0700503 TickTimer tick_timer;
minyue-webrtc12d30842017-07-19 11:44:06 +0200504 StrictMock<MockStatisticsCalculator> mock_stats;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000505
henrik.lundin84f8cd62016-04-26 07:45:16 -0700506 PacketBuffer* buffer = new PacketBuffer(100, &tick_timer); // 100 packets.
ossua73f6c92016-10-24 08:25:28 -0700507 {
508 Packet packet = gen.NextPacket(payload_len);
509 packet.payload.Clear();
510 EXPECT_EQ(PacketBuffer::kInvalidPacket,
minyue-webrtc12d30842017-07-19 11:44:06 +0200511 buffer->InsertPacket(std::move(packet), &mock_stats));
ossua73f6c92016-10-24 08:25:28 -0700512 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000513 // Buffer should still be empty. Test all empty-checks.
514 uint32_t temp_ts;
515 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->NextTimestamp(&temp_ts));
516 EXPECT_EQ(PacketBuffer::kBufferEmpty,
517 buffer->NextHigherTimestamp(0, &temp_ts));
ossu7a377612016-10-18 04:06:13 -0700518 EXPECT_EQ(NULL, buffer->PeekNextPacket());
ossua73f6c92016-10-24 08:25:28 -0700519 EXPECT_FALSE(buffer->GetNextPacket());
minyue-webrtcfae474c2017-07-05 11:17:40 +0200520
minyue-webrtcfae474c2017-07-05 11:17:40 +0200521 // Discarding packets will not invoke mock_stats.PacketDiscarded() because the
522 // packet buffer is empty.
523 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->DiscardNextPacket(&mock_stats));
524 buffer->DiscardAllOldPackets(0, &mock_stats);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000525
526 // Insert one packet to make the buffer non-empty.
ossua73f6c92016-10-24 08:25:28 -0700527 EXPECT_EQ(PacketBuffer::kOK,
minyue-webrtc12d30842017-07-19 11:44:06 +0200528 buffer->InsertPacket(gen.NextPacket(payload_len), &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000529 EXPECT_EQ(PacketBuffer::kInvalidPointer, buffer->NextTimestamp(NULL));
530 EXPECT_EQ(PacketBuffer::kInvalidPointer,
531 buffer->NextHigherTimestamp(0, NULL));
532 delete buffer;
533
534 // Insert packet list of three packets, where the second packet has an invalid
535 // payload. Expect first packet to be inserted, and the remaining two to be
536 // discarded.
henrik.lundin84f8cd62016-04-26 07:45:16 -0700537 buffer = new PacketBuffer(100, &tick_timer); // 100 packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000538 PacketList list;
539 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
ossua73f6c92016-10-24 08:25:28 -0700540 {
541 Packet packet = gen.NextPacket(payload_len);
542 packet.payload.Clear(); // Invalid.
543 list.push_back(std::move(packet));
544 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000545 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
546 MockDecoderDatabase decoder_database;
henrik.lundinebd9fd72016-08-31 13:03:36 -0700547 auto factory = CreateBuiltinAudioDecoderFactory();
ossuf1b08da2016-09-23 02:19:43 -0700548 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
henrik.lundinebd9fd72016-08-31 13:03:36 -0700549 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
550 .WillRepeatedly(Return(&info));
henrik.lundinda8bbf62016-08-31 03:14:11 -0700551 rtc::Optional<uint8_t> current_pt;
552 rtc::Optional<uint8_t> current_cng_pt;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000553 EXPECT_EQ(PacketBuffer::kInvalidPacket,
minyue-webrtc12d30842017-07-19 11:44:06 +0200554 buffer->InsertPacketList(&list, decoder_database, &current_pt,
555 &current_cng_pt, &mock_stats));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000556 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700557 EXPECT_EQ(1u, buffer->NumPacketsInBuffer());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000558 delete buffer;
559 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
560}
561
562// Test packet comparison function.
563// The function should return true if the first packet "goes before" the second.
564TEST(PacketBuffer, ComparePackets) {
565 PacketGenerator gen(0, 0, 0, 10);
ossua73f6c92016-10-24 08:25:28 -0700566 Packet a(gen.NextPacket(10)); // SN = 0, TS = 0.
567 Packet b(gen.NextPacket(10)); // SN = 1, TS = 10.
568 EXPECT_FALSE(a == b);
569 EXPECT_TRUE(a != b);
570 EXPECT_TRUE(a < b);
571 EXPECT_FALSE(a > b);
572 EXPECT_TRUE(a <= b);
573 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000574
575 // Testing wrap-around case; 'a' is earlier but has a larger timestamp value.
ossua73f6c92016-10-24 08:25:28 -0700576 a.timestamp = 0xFFFFFFFF - 10;
577 EXPECT_FALSE(a == b);
578 EXPECT_TRUE(a != b);
579 EXPECT_TRUE(a < b);
580 EXPECT_FALSE(a > b);
581 EXPECT_TRUE(a <= b);
582 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000583
584 // Test equal packets.
ossua73f6c92016-10-24 08:25:28 -0700585 EXPECT_TRUE(a == a);
586 EXPECT_FALSE(a != a);
587 EXPECT_FALSE(a < a);
588 EXPECT_FALSE(a > a);
589 EXPECT_TRUE(a <= a);
590 EXPECT_TRUE(a >= a);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000591
592 // Test equal timestamps but different sequence numbers (0 and 1).
ossua73f6c92016-10-24 08:25:28 -0700593 a.timestamp = b.timestamp;
594 EXPECT_FALSE(a == b);
595 EXPECT_TRUE(a != b);
596 EXPECT_TRUE(a < b);
597 EXPECT_FALSE(a > b);
598 EXPECT_TRUE(a <= b);
599 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000600
601 // Test equal timestamps but different sequence numbers (32767 and 1).
ossua73f6c92016-10-24 08:25:28 -0700602 a.sequence_number = 0xFFFF;
603 EXPECT_FALSE(a == b);
604 EXPECT_TRUE(a != b);
605 EXPECT_TRUE(a < b);
606 EXPECT_FALSE(a > b);
607 EXPECT_TRUE(a <= b);
608 EXPECT_FALSE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000609
ossua70695a2016-09-22 02:06:28 -0700610 // Test equal timestamps and sequence numbers, but differing priorities.
ossua73f6c92016-10-24 08:25:28 -0700611 a.sequence_number = b.sequence_number;
612 a.priority = {1, 0};
613 b.priority = {0, 0};
ossua70695a2016-09-22 02:06:28 -0700614 // a after b
ossua73f6c92016-10-24 08:25:28 -0700615 EXPECT_FALSE(a == b);
616 EXPECT_TRUE(a != b);
617 EXPECT_FALSE(a < b);
618 EXPECT_TRUE(a > b);
619 EXPECT_FALSE(a <= b);
620 EXPECT_TRUE(a >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000621
ossua73f6c92016-10-24 08:25:28 -0700622 Packet c(gen.NextPacket(0)); // SN = 2, TS = 20.
623 Packet d(gen.NextPacket(0)); // SN = 3, TS = 20.
624 c.timestamp = b.timestamp;
625 d.timestamp = b.timestamp;
626 c.sequence_number = b.sequence_number;
627 d.sequence_number = b.sequence_number;
628 c.priority = {1, 1};
629 d.priority = {0, 1};
ossua70695a2016-09-22 02:06:28 -0700630 // c after d
ossua73f6c92016-10-24 08:25:28 -0700631 EXPECT_FALSE(c == d);
632 EXPECT_TRUE(c != d);
633 EXPECT_FALSE(c < d);
634 EXPECT_TRUE(c > d);
635 EXPECT_FALSE(c <= d);
636 EXPECT_TRUE(c >= d);
ossua70695a2016-09-22 02:06:28 -0700637
638 // c after a
ossua73f6c92016-10-24 08:25:28 -0700639 EXPECT_FALSE(c == a);
640 EXPECT_TRUE(c != a);
641 EXPECT_FALSE(c < a);
642 EXPECT_TRUE(c > a);
643 EXPECT_FALSE(c <= a);
644 EXPECT_TRUE(c >= a);
ossua70695a2016-09-22 02:06:28 -0700645
646 // c after b
ossua73f6c92016-10-24 08:25:28 -0700647 EXPECT_FALSE(c == b);
648 EXPECT_TRUE(c != b);
649 EXPECT_FALSE(c < b);
650 EXPECT_TRUE(c > b);
651 EXPECT_FALSE(c <= b);
652 EXPECT_TRUE(c >= b);
ossua70695a2016-09-22 02:06:28 -0700653
654 // a after d
ossua73f6c92016-10-24 08:25:28 -0700655 EXPECT_FALSE(a == d);
656 EXPECT_TRUE(a != d);
657 EXPECT_FALSE(a < d);
658 EXPECT_TRUE(a > d);
659 EXPECT_FALSE(a <= d);
660 EXPECT_TRUE(a >= d);
ossua70695a2016-09-22 02:06:28 -0700661
662 // d after b
ossua73f6c92016-10-24 08:25:28 -0700663 EXPECT_FALSE(d == b);
664 EXPECT_TRUE(d != b);
665 EXPECT_FALSE(d < b);
666 EXPECT_TRUE(d > b);
667 EXPECT_FALSE(d <= b);
668 EXPECT_TRUE(d >= b);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000669}
670
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000671namespace {
672void TestIsObsoleteTimestamp(uint32_t limit_timestamp) {
673 // Check with zero horizon, which implies that the horizon is at 2^31, i.e.,
674 // half the timestamp range.
675 static const uint32_t kZeroHorizon = 0;
676 static const uint32_t k2Pow31Minus1 = 0x7FFFFFFF;
677 // Timestamp on the limit is not old.
678 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
679 limit_timestamp, limit_timestamp, kZeroHorizon));
680 // 1 sample behind is old.
681 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
682 limit_timestamp - 1, limit_timestamp, kZeroHorizon));
683 // 2^31 - 1 samples behind is old.
684 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
685 limit_timestamp - k2Pow31Minus1, limit_timestamp, kZeroHorizon));
686 // 1 sample ahead is not old.
687 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
688 limit_timestamp + 1, limit_timestamp, kZeroHorizon));
Cesar Magalhaesf69f1fb2015-05-30 17:49:18 +0200689 // If |t1-t2|=2^31 and t1>t2, t2 is older than t1 but not the opposite.
690 uint32_t other_timestamp = limit_timestamp + (1 << 31);
691 uint32_t lowest_timestamp = std::min(limit_timestamp, other_timestamp);
692 uint32_t highest_timestamp = std::max(limit_timestamp, other_timestamp);
693 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
694 lowest_timestamp, highest_timestamp, kZeroHorizon));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000695 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
Cesar Magalhaesf69f1fb2015-05-30 17:49:18 +0200696 highest_timestamp, lowest_timestamp, kZeroHorizon));
henrik.lundin@webrtc.org52b42cb2014-11-04 14:03:58 +0000697
698 // Fixed horizon at 10 samples.
699 static const uint32_t kHorizon = 10;
700 // Timestamp on the limit is not old.
701 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
702 limit_timestamp, limit_timestamp, kHorizon));
703 // 1 sample behind is old.
704 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
705 limit_timestamp - 1, limit_timestamp, kHorizon));
706 // 9 samples behind is old.
707 EXPECT_TRUE(PacketBuffer::IsObsoleteTimestamp(
708 limit_timestamp - 9, limit_timestamp, kHorizon));
709 // 10 samples behind is not old.
710 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
711 limit_timestamp - 10, limit_timestamp, kHorizon));
712 // 2^31 - 1 samples behind is not old.
713 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
714 limit_timestamp - k2Pow31Minus1, limit_timestamp, kHorizon));
715 // 1 sample ahead is not old.
716 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
717 limit_timestamp + 1, limit_timestamp, kHorizon));
718 // 2^31 samples ahead is not old.
719 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
720 limit_timestamp + (1 << 31), limit_timestamp, kHorizon));
721}
722} // namespace
723
724// Test the IsObsoleteTimestamp method with different limit timestamps.
725TEST(PacketBuffer, IsObsoleteTimestamp) {
726 TestIsObsoleteTimestamp(0);
727 TestIsObsoleteTimestamp(1);
728 TestIsObsoleteTimestamp(0xFFFFFFFF); // -1 in uint32_t.
729 TestIsObsoleteTimestamp(0x80000000); // 2^31.
730 TestIsObsoleteTimestamp(0x80000001); // 2^31 + 1.
731 TestIsObsoleteTimestamp(0x7FFFFFFF); // 2^31 - 1.
732}
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000733} // namespace webrtc