Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: webrtc/modules/audio_coding/neteq/packet_buffer_unittest.cc

Issue 2425223002: NetEq now works with packets as values, rather than pointers. (Closed)
Patch Set: Compare packets better in test. One more const. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 12 matching lines...) Expand all
23 using ::testing::_; 23 using ::testing::_;
24 24
25 namespace webrtc { 25 namespace webrtc {
26 26
27 // Helper class to generate packets. Packets must be deleted by the user. 27 // Helper class to generate packets. Packets must be deleted by the user.
28 class PacketGenerator { 28 class PacketGenerator {
29 public: 29 public:
30 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size); 30 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
31 virtual ~PacketGenerator() {} 31 virtual ~PacketGenerator() {}
32 void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size); 32 void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size);
33 Packet* NextPacket(int payload_size_bytes); 33 Packet NextPacket(int payload_size_bytes);
34 34
35 uint16_t seq_no_; 35 uint16_t seq_no_;
36 uint32_t ts_; 36 uint32_t ts_;
37 uint8_t pt_; 37 uint8_t pt_;
38 int frame_size_; 38 int frame_size_;
39 }; 39 };
40 40
41 PacketGenerator::PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, 41 PacketGenerator::PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt,
42 int frame_size) { 42 int frame_size) {
43 Reset(seq_no, ts, pt, frame_size); 43 Reset(seq_no, ts, pt, frame_size);
44 } 44 }
45 45
46 void PacketGenerator::Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, 46 void PacketGenerator::Reset(uint16_t seq_no, uint32_t ts, uint8_t pt,
47 int frame_size) { 47 int frame_size) {
48 seq_no_ = seq_no; 48 seq_no_ = seq_no;
49 ts_ = ts; 49 ts_ = ts;
50 pt_ = pt; 50 pt_ = pt;
51 frame_size_ = frame_size; 51 frame_size_ = frame_size;
52 } 52 }
53 53
54 Packet* PacketGenerator::NextPacket(int payload_size_bytes) { 54 Packet PacketGenerator::NextPacket(int payload_size_bytes) {
55 Packet* packet = new Packet; 55 Packet packet;
56 packet->sequence_number = seq_no_; 56 packet.sequence_number = seq_no_;
57 packet->timestamp = ts_; 57 packet.timestamp = ts_;
58 packet->payload_type = pt_; 58 packet.payload_type = pt_;
59 packet->payload.SetSize(payload_size_bytes); 59 packet.payload.SetSize(payload_size_bytes);
60 ++seq_no_; 60 ++seq_no_;
61 ts_ += frame_size_; 61 ts_ += frame_size_;
62 return packet; 62 return packet;
63 } 63 }
64 64
65 struct PacketsToInsert { 65 struct PacketsToInsert {
66 uint16_t sequence_number; 66 uint16_t sequence_number;
67 uint32_t timestamp; 67 uint32_t timestamp;
68 uint8_t payload_type; 68 uint8_t payload_type;
69 bool primary; 69 bool primary;
(...skipping 11 matching lines...) Expand all
81 EXPECT_TRUE(buffer->Empty()); 81 EXPECT_TRUE(buffer->Empty());
82 delete buffer; 82 delete buffer;
83 } 83 }
84 84
85 TEST(PacketBuffer, InsertPacket) { 85 TEST(PacketBuffer, InsertPacket) {
86 TickTimer tick_timer; 86 TickTimer tick_timer;
87 PacketBuffer buffer(10, &tick_timer); // 10 packets. 87 PacketBuffer buffer(10, &tick_timer); // 10 packets.
88 PacketGenerator gen(17u, 4711u, 0, 10); 88 PacketGenerator gen(17u, 4711u, 0, 10);
89 89
90 const int payload_len = 100; 90 const int payload_len = 100;
91 Packet* packet = gen.NextPacket(payload_len); 91 const Packet packet = gen.NextPacket(payload_len);
92 92 EXPECT_EQ(0, buffer.InsertPacket(packet.Clone()));
93 EXPECT_EQ(0, buffer.InsertPacket(packet));
94 uint32_t next_ts; 93 uint32_t next_ts;
95 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts)); 94 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
96 EXPECT_EQ(4711u, next_ts); 95 EXPECT_EQ(4711u, next_ts);
97 EXPECT_FALSE(buffer.Empty()); 96 EXPECT_FALSE(buffer.Empty());
98 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); 97 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
99 const Packet* next_packet = buffer.PeekNextPacket(); 98 const Packet* next_packet = buffer.PeekNextPacket();
100 EXPECT_EQ(packet, next_packet); // Compare pointer addresses. 99 EXPECT_EQ(packet, *next_packet); // Compare contents.
101 100
102 // Do not explicitly flush buffer or delete packet to test that it is deleted 101 // Do not explicitly flush buffer or delete packet to test that it is deleted
103 // with the buffer. (Tested with Valgrind or similar tool.) 102 // with the buffer. (Tested with Valgrind or similar tool.)
104 } 103 }
105 104
106 // Test to flush buffer. 105 // Test to flush buffer.
107 TEST(PacketBuffer, FlushBuffer) { 106 TEST(PacketBuffer, FlushBuffer) {
108 TickTimer tick_timer; 107 TickTimer tick_timer;
109 PacketBuffer buffer(10, &tick_timer); // 10 packets. 108 PacketBuffer buffer(10, &tick_timer); // 10 packets.
110 PacketGenerator gen(0, 0, 0, 10); 109 PacketGenerator gen(0, 0, 0, 10);
111 const int payload_len = 10; 110 const int payload_len = 10;
112 111
113 // Insert 10 small packets; should be ok. 112 // Insert 10 small packets; should be ok.
114 for (int i = 0; i < 10; ++i) { 113 for (int i = 0; i < 10; ++i) {
115 Packet* packet = gen.NextPacket(payload_len); 114 EXPECT_EQ(PacketBuffer::kOK,
116 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet)); 115 buffer.InsertPacket(gen.NextPacket(payload_len)));
117 } 116 }
118 EXPECT_EQ(10u, buffer.NumPacketsInBuffer()); 117 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
119 EXPECT_FALSE(buffer.Empty()); 118 EXPECT_FALSE(buffer.Empty());
120 119
121 buffer.Flush(); 120 buffer.Flush();
122 // Buffer should delete the payloads itself. 121 // Buffer should delete the payloads itself.
123 EXPECT_EQ(0u, buffer.NumPacketsInBuffer()); 122 EXPECT_EQ(0u, buffer.NumPacketsInBuffer());
124 EXPECT_TRUE(buffer.Empty()); 123 EXPECT_TRUE(buffer.Empty());
125 } 124 }
126 125
127 // Test to fill the buffer over the limits, and verify that it flushes. 126 // Test to fill the buffer over the limits, and verify that it flushes.
128 TEST(PacketBuffer, OverfillBuffer) { 127 TEST(PacketBuffer, OverfillBuffer) {
129 TickTimer tick_timer; 128 TickTimer tick_timer;
130 PacketBuffer buffer(10, &tick_timer); // 10 packets. 129 PacketBuffer buffer(10, &tick_timer); // 10 packets.
131 PacketGenerator gen(0, 0, 0, 10); 130 PacketGenerator gen(0, 0, 0, 10);
132 131
133 // Insert 10 small packets; should be ok. 132 // Insert 10 small packets; should be ok.
134 const int payload_len = 10; 133 const int payload_len = 10;
135 int i; 134 int i;
136 for (i = 0; i < 10; ++i) { 135 for (i = 0; i < 10; ++i) {
137 Packet* packet = gen.NextPacket(payload_len); 136 EXPECT_EQ(PacketBuffer::kOK,
138 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet)); 137 buffer.InsertPacket(gen.NextPacket(payload_len)));
139 } 138 }
140 EXPECT_EQ(10u, buffer.NumPacketsInBuffer()); 139 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
141 uint32_t next_ts; 140 uint32_t next_ts;
142 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts)); 141 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
143 EXPECT_EQ(0u, next_ts); // Expect first inserted packet to be first in line. 142 EXPECT_EQ(0u, next_ts); // Expect first inserted packet to be first in line.
144 143
144 const Packet packet = gen.NextPacket(payload_len);
145 // Insert 11th packet; should flush the buffer and insert it after flushing. 145 // Insert 11th packet; should flush the buffer and insert it after flushing.
146 Packet* packet = gen.NextPacket(payload_len); 146 EXPECT_EQ(PacketBuffer::kFlushed, buffer.InsertPacket(packet.Clone()));
147 EXPECT_EQ(PacketBuffer::kFlushed, buffer.InsertPacket(packet));
148 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); 147 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
149 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts)); 148 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&next_ts));
150 // Expect last inserted packet to be first in line. 149 // Expect last inserted packet to be first in line.
151 EXPECT_EQ(packet->timestamp, next_ts); 150 EXPECT_EQ(packet.timestamp, next_ts);
152 151
153 // Flush buffer to delete all packets. 152 // Flush buffer to delete all packets.
154 buffer.Flush(); 153 buffer.Flush();
155 } 154 }
156 155
157 // Test inserting a list of packets. 156 // Test inserting a list of packets.
158 TEST(PacketBuffer, InsertPacketList) { 157 TEST(PacketBuffer, InsertPacketList) {
159 TickTimer tick_timer; 158 TickTimer tick_timer;
160 PacketBuffer buffer(10, &tick_timer); // 10 packets. 159 PacketBuffer buffer(10, &tick_timer); // 10 packets.
161 PacketGenerator gen(0, 0, 0, 10); 160 PacketGenerator gen(0, 0, 0, 10);
162 PacketList list; 161 PacketList list;
163 const int payload_len = 10; 162 const int payload_len = 10;
164 163
165 // Insert 10 small packets. 164 // Insert 10 small packets.
166 for (int i = 0; i < 10; ++i) { 165 for (int i = 0; i < 10; ++i) {
167 Packet* packet = gen.NextPacket(payload_len); 166 list.push_back(gen.NextPacket(payload_len));
168 list.push_back(packet);
169 } 167 }
170 168
171 MockDecoderDatabase decoder_database; 169 MockDecoderDatabase decoder_database;
172 auto factory = CreateBuiltinAudioDecoderFactory(); 170 auto factory = CreateBuiltinAudioDecoderFactory();
173 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory); 171 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
174 EXPECT_CALL(decoder_database, GetDecoderInfo(0)) 172 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
175 .WillRepeatedly(Return(&info)); 173 .WillRepeatedly(Return(&info));
176 rtc::Optional<uint8_t> current_pt; 174 rtc::Optional<uint8_t> current_pt;
177 rtc::Optional<uint8_t> current_cng_pt; 175 rtc::Optional<uint8_t> current_cng_pt;
178 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list, 176 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
(...skipping 16 matching lines...) Expand all
195 // TODO(hlundin): Remove this test when legacy operation is no longer needed. 193 // TODO(hlundin): Remove this test when legacy operation is no longer needed.
196 TEST(PacketBuffer, InsertPacketListChangePayloadType) { 194 TEST(PacketBuffer, InsertPacketListChangePayloadType) {
197 TickTimer tick_timer; 195 TickTimer tick_timer;
198 PacketBuffer buffer(10, &tick_timer); // 10 packets. 196 PacketBuffer buffer(10, &tick_timer); // 10 packets.
199 PacketGenerator gen(0, 0, 0, 10); 197 PacketGenerator gen(0, 0, 0, 10);
200 PacketList list; 198 PacketList list;
201 const int payload_len = 10; 199 const int payload_len = 10;
202 200
203 // Insert 10 small packets. 201 // Insert 10 small packets.
204 for (int i = 0; i < 10; ++i) { 202 for (int i = 0; i < 10; ++i) {
205 Packet* packet = gen.NextPacket(payload_len); 203 list.push_back(gen.NextPacket(payload_len));
206 list.push_back(packet);
207 } 204 }
208 // Insert 11th packet of another payload type (not CNG). 205 // Insert 11th packet of another payload type (not CNG).
209 Packet* packet = gen.NextPacket(payload_len); 206 {
210 packet->payload_type = 1; 207 Packet packet = gen.NextPacket(payload_len);
211 list.push_back(packet); 208 packet.payload_type = 1;
212 209 list.push_back(std::move(packet));
210 }
213 211
214 MockDecoderDatabase decoder_database; 212 MockDecoderDatabase decoder_database;
215 auto factory = CreateBuiltinAudioDecoderFactory(); 213 auto factory = CreateBuiltinAudioDecoderFactory();
216 const DecoderDatabase::DecoderInfo info0(NetEqDecoder::kDecoderPCMu, factory); 214 const DecoderDatabase::DecoderInfo info0(NetEqDecoder::kDecoderPCMu, factory);
217 EXPECT_CALL(decoder_database, GetDecoderInfo(0)) 215 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
218 .WillRepeatedly(Return(&info0)); 216 .WillRepeatedly(Return(&info0));
219 const DecoderDatabase::DecoderInfo info1(NetEqDecoder::kDecoderPCMa, factory); 217 const DecoderDatabase::DecoderInfo info1(NetEqDecoder::kDecoderPCMa, factory);
220 EXPECT_CALL(decoder_database, GetDecoderInfo(1)) 218 EXPECT_CALL(decoder_database, GetDecoderInfo(1))
221 .WillRepeatedly(Return(&info1)); 219 .WillRepeatedly(Return(&info1));
222 rtc::Optional<uint8_t> current_pt; 220 rtc::Optional<uint8_t> current_pt;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 {0x0004, 0x0000001E, 0, true, 7}, 257 {0x0004, 0x0000001E, 0, true, 7},
260 {0x0004, 0x00000014, 1, false, 6}, 258 {0x0004, 0x00000014, 1, false, 6},
261 {0x0005, 0x0000001E, 0, true, -1}, 259 {0x0005, 0x0000001E, 0, true, -1},
262 {0x0005, 0x00000014, 1, false, -1}, 260 {0x0005, 0x00000014, 1, false, -1},
263 {0x0006, 0x00000028, 0, true, 8}, 261 {0x0006, 0x00000028, 0, true, 8},
264 {0x0006, 0x0000001E, 1, false, -1}, 262 {0x0006, 0x0000001E, 1, false, -1},
265 }; 263 };
266 264
267 const size_t kExpectPacketsInBuffer = 9; 265 const size_t kExpectPacketsInBuffer = 9;
268 266
269 std::vector<Packet*> expect_order(kExpectPacketsInBuffer); 267 std::vector<Packet> expect_order(kExpectPacketsInBuffer);
270 268
271 PacketGenerator gen(0, 0, 0, kFrameSize); 269 PacketGenerator gen(0, 0, 0, kFrameSize);
272 270
273 for (int i = 0; i < kPackets; ++i) { 271 for (int i = 0; i < kPackets; ++i) {
274 gen.Reset(packet_facts[i].sequence_number, 272 gen.Reset(packet_facts[i].sequence_number,
275 packet_facts[i].timestamp, 273 packet_facts[i].timestamp,
276 packet_facts[i].payload_type, 274 packet_facts[i].payload_type,
277 kFrameSize); 275 kFrameSize);
278 Packet* packet = gen.NextPacket(kPayloadLength); 276 Packet packet = gen.NextPacket(kPayloadLength);
279 packet->priority.red_level = packet_facts[i].primary ? 0 : 1; 277 packet.priority.red_level = packet_facts[i].primary ? 0 : 1;
280 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet)); 278 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacket(packet.Clone()));
281 if (packet_facts[i].extract_order >= 0) { 279 if (packet_facts[i].extract_order >= 0) {
282 expect_order[packet_facts[i].extract_order] = packet; 280 expect_order[packet_facts[i].extract_order] = std::move(packet);
283 } 281 }
284 } 282 }
285 283
286 EXPECT_EQ(kExpectPacketsInBuffer, buffer.NumPacketsInBuffer()); 284 EXPECT_EQ(kExpectPacketsInBuffer, buffer.NumPacketsInBuffer());
287 285
288 size_t drop_count;
289 for (size_t i = 0; i < kExpectPacketsInBuffer; ++i) { 286 for (size_t i = 0; i < kExpectPacketsInBuffer; ++i) {
290 Packet* packet = buffer.GetNextPacket(&drop_count); 287 const rtc::Optional<Packet> packet = buffer.GetNextPacket();
291 EXPECT_EQ(0u, drop_count); 288 EXPECT_EQ(packet, expect_order[i]); // Compare contents.
292 EXPECT_EQ(packet, expect_order[i]); // Compare pointer addresses.
293 delete packet;
294 } 289 }
295 EXPECT_TRUE(buffer.Empty()); 290 EXPECT_TRUE(buffer.Empty());
296 } 291 }
297 292
298 TEST(PacketBuffer, DiscardPackets) { 293 TEST(PacketBuffer, DiscardPackets) {
299 TickTimer tick_timer; 294 TickTimer tick_timer;
300 PacketBuffer buffer(100, &tick_timer); // 100 packets. 295 PacketBuffer buffer(100, &tick_timer); // 100 packets.
301 const uint16_t start_seq_no = 17; 296 const uint16_t start_seq_no = 17;
302 const uint32_t start_ts = 4711; 297 const uint32_t start_ts = 4711;
303 const uint32_t ts_increment = 10; 298 const uint32_t ts_increment = 10;
304 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment); 299 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
305 PacketList list; 300 PacketList list;
306 const int payload_len = 10; 301 const int payload_len = 10;
307 302
308 // Insert 10 small packets. 303 // Insert 10 small packets.
309 for (int i = 0; i < 10; ++i) { 304 for (int i = 0; i < 10; ++i) {
310 Packet* packet = gen.NextPacket(payload_len); 305 buffer.InsertPacket(gen.NextPacket(payload_len));
311 buffer.InsertPacket(packet);
312 } 306 }
313 EXPECT_EQ(10u, buffer.NumPacketsInBuffer()); 307 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
314 308
315 // Discard them one by one and make sure that the right packets are at the 309 // Discard them one by one and make sure that the right packets are at the
316 // front of the buffer. 310 // front of the buffer.
317 uint32_t current_ts = start_ts; 311 uint32_t current_ts = start_ts;
318 for (int i = 0; i < 10; ++i) { 312 for (int i = 0; i < 10; ++i) {
319 uint32_t ts; 313 uint32_t ts;
320 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts)); 314 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts));
321 EXPECT_EQ(current_ts, ts); 315 EXPECT_EQ(current_ts, ts);
(...skipping 10 matching lines...) Expand all
332 const uint32_t start_ts = 4711; 326 const uint32_t start_ts = 4711;
333 const uint32_t ts_increment = 10; 327 const uint32_t ts_increment = 10;
334 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment); 328 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
335 const int payload_len = 10; 329 const int payload_len = 10;
336 330
337 // Generate 10 small packets and insert them into a PacketList. Insert every 331 // Generate 10 small packets and insert them into a PacketList. Insert every
338 // odd packet to the front, and every even packet to the back, thus creating 332 // odd packet to the front, and every even packet to the back, thus creating
339 // a (rather strange) reordering. 333 // a (rather strange) reordering.
340 PacketList list; 334 PacketList list;
341 for (int i = 0; i < 10; ++i) { 335 for (int i = 0; i < 10; ++i) {
342 Packet* packet = gen.NextPacket(payload_len); 336 Packet packet = gen.NextPacket(payload_len);
343 if (i % 2) { 337 if (i % 2) {
344 list.push_front(packet); 338 list.push_front(std::move(packet));
345 } else { 339 } else {
346 list.push_back(packet); 340 list.push_back(std::move(packet));
347 } 341 }
348 } 342 }
349 343
350 MockDecoderDatabase decoder_database; 344 MockDecoderDatabase decoder_database;
351 auto factory = CreateBuiltinAudioDecoderFactory(); 345 auto factory = CreateBuiltinAudioDecoderFactory();
352 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory); 346 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
353 EXPECT_CALL(decoder_database, GetDecoderInfo(0)) 347 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
354 .WillRepeatedly(Return(&info)); 348 .WillRepeatedly(Return(&info));
355 rtc::Optional<uint8_t> current_pt; 349 rtc::Optional<uint8_t> current_pt;
356 rtc::Optional<uint8_t> current_cng_pt; 350 rtc::Optional<uint8_t> current_cng_pt;
357 351
358 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list, 352 EXPECT_EQ(PacketBuffer::kOK, buffer.InsertPacketList(&list,
359 decoder_database, 353 decoder_database,
360 &current_pt, 354 &current_pt,
361 &current_cng_pt)); 355 &current_cng_pt));
362 EXPECT_EQ(10u, buffer.NumPacketsInBuffer()); 356 EXPECT_EQ(10u, buffer.NumPacketsInBuffer());
363 357
364 // Extract them and make sure that come out in the right order. 358 // Extract them and make sure that come out in the right order.
365 uint32_t current_ts = start_ts; 359 uint32_t current_ts = start_ts;
366 for (int i = 0; i < 10; ++i) { 360 for (int i = 0; i < 10; ++i) {
367 Packet* packet = buffer.GetNextPacket(NULL); 361 const rtc::Optional<Packet> packet = buffer.GetNextPacket();
368 ASSERT_FALSE(packet == NULL); 362 ASSERT_TRUE(packet);
369 EXPECT_EQ(current_ts, packet->timestamp); 363 EXPECT_EQ(current_ts, packet->timestamp);
370 current_ts += ts_increment; 364 current_ts += ts_increment;
371 delete packet;
372 } 365 }
373 EXPECT_TRUE(buffer.Empty()); 366 EXPECT_TRUE(buffer.Empty());
374 367
375 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted. 368 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
376 } 369 }
377 370
378 // The test first inserts a packet with narrow-band CNG, then a packet with 371 // The test first inserts a packet with narrow-band CNG, then a packet with
379 // wide-band speech. The expected behavior of the packet buffer is to detect a 372 // wide-band speech. The expected behavior of the packet buffer is to detect a
380 // change in sample rate, even though no speech packet has been inserted before, 373 // change in sample rate, even though no speech packet has been inserted before,
381 // and flush out the CNG packet. 374 // and flush out the CNG packet.
(...skipping 26 matching lines...) Expand all
408 &current_cng_pt)); 401 &current_cng_pt));
409 EXPECT_TRUE(list.empty()); 402 EXPECT_TRUE(list.empty());
410 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); 403 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
411 ASSERT_TRUE(buffer.PeekNextPacket()); 404 ASSERT_TRUE(buffer.PeekNextPacket());
412 EXPECT_EQ(kCngPt, buffer.PeekNextPacket()->payload_type); 405 EXPECT_EQ(kCngPt, buffer.PeekNextPacket()->payload_type);
413 EXPECT_FALSE(current_pt); // Current payload type not set. 406 EXPECT_FALSE(current_pt); // Current payload type not set.
414 EXPECT_EQ(rtc::Optional<uint8_t>(kCngPt), 407 EXPECT_EQ(rtc::Optional<uint8_t>(kCngPt),
415 current_cng_pt); // CNG payload type set. 408 current_cng_pt); // CNG payload type set.
416 409
417 // Insert second packet, which is wide-band speech. 410 // Insert second packet, which is wide-band speech.
418 Packet* packet = gen.NextPacket(kPayloadLen); 411 {
419 packet->payload_type = kSpeechPt; 412 Packet packet = gen.NextPacket(kPayloadLen);
420 list.push_back(packet); 413 packet.payload_type = kSpeechPt;
414 list.push_back(std::move(packet));
415 }
421 // Expect the buffer to flush out the CNG packet, since it does not match the 416 // Expect the buffer to flush out the CNG packet, since it does not match the
422 // new speech sample rate. 417 // new speech sample rate.
423 EXPECT_EQ(PacketBuffer::kFlushed, 418 EXPECT_EQ(PacketBuffer::kFlushed,
424 buffer.InsertPacketList(&list, decoder_database, &current_pt, 419 buffer.InsertPacketList(&list, decoder_database, &current_pt,
425 &current_cng_pt)); 420 &current_cng_pt));
426 EXPECT_TRUE(list.empty()); 421 EXPECT_TRUE(list.empty());
427 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); 422 EXPECT_EQ(1u, buffer.NumPacketsInBuffer());
428 ASSERT_TRUE(buffer.PeekNextPacket()); 423 ASSERT_TRUE(buffer.PeekNextPacket());
429 EXPECT_EQ(kSpeechPt, buffer.PeekNextPacket()->payload_type); 424 EXPECT_EQ(kSpeechPt, buffer.PeekNextPacket()->payload_type);
430 425
431 EXPECT_EQ(rtc::Optional<uint8_t>(kSpeechPt), 426 EXPECT_EQ(rtc::Optional<uint8_t>(kSpeechPt),
432 current_pt); // Current payload type set. 427 current_pt); // Current payload type set.
433 EXPECT_FALSE(current_cng_pt); // CNG payload type reset. 428 EXPECT_FALSE(current_cng_pt); // CNG payload type reset.
434 429
435 buffer.Flush(); // Clean up. 430 buffer.Flush(); // Clean up.
436 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted. 431 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
437 } 432 }
438 433
439 TEST(PacketBuffer, Failures) { 434 TEST(PacketBuffer, Failures) {
440 const uint16_t start_seq_no = 17; 435 const uint16_t start_seq_no = 17;
441 const uint32_t start_ts = 4711; 436 const uint32_t start_ts = 4711;
442 const uint32_t ts_increment = 10; 437 const uint32_t ts_increment = 10;
443 int payload_len = 100; 438 int payload_len = 100;
444 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment); 439 PacketGenerator gen(start_seq_no, start_ts, 0, ts_increment);
445 TickTimer tick_timer; 440 TickTimer tick_timer;
446 441
447 PacketBuffer* buffer = new PacketBuffer(100, &tick_timer); // 100 packets. 442 PacketBuffer* buffer = new PacketBuffer(100, &tick_timer); // 100 packets.
448 Packet* packet = NULL; 443 {
449 EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet)); 444 Packet packet = gen.NextPacket(payload_len);
450 packet = gen.NextPacket(payload_len); 445 packet.payload.Clear();
451 packet->payload.Clear(); 446 EXPECT_EQ(PacketBuffer::kInvalidPacket,
452 EXPECT_EQ(PacketBuffer::kInvalidPacket, buffer->InsertPacket(packet)); 447 buffer->InsertPacket(std::move(packet)));
453 // Packet is deleted by the PacketBuffer. 448 }
454
455 // Buffer should still be empty. Test all empty-checks. 449 // Buffer should still be empty. Test all empty-checks.
456 uint32_t temp_ts; 450 uint32_t temp_ts;
457 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->NextTimestamp(&temp_ts)); 451 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->NextTimestamp(&temp_ts));
458 EXPECT_EQ(PacketBuffer::kBufferEmpty, 452 EXPECT_EQ(PacketBuffer::kBufferEmpty,
459 buffer->NextHigherTimestamp(0, &temp_ts)); 453 buffer->NextHigherTimestamp(0, &temp_ts));
460 EXPECT_EQ(NULL, buffer->PeekNextPacket()); 454 EXPECT_EQ(NULL, buffer->PeekNextPacket());
461 EXPECT_EQ(NULL, buffer->GetNextPacket(NULL)); 455 EXPECT_FALSE(buffer->GetNextPacket());
462 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->DiscardNextPacket()); 456 EXPECT_EQ(PacketBuffer::kBufferEmpty, buffer->DiscardNextPacket());
463 EXPECT_EQ(0, buffer->DiscardAllOldPackets(0)); // 0 packets discarded. 457 EXPECT_EQ(0, buffer->DiscardAllOldPackets(0)); // 0 packets discarded.
464 458
465 // Insert one packet to make the buffer non-empty. 459 // Insert one packet to make the buffer non-empty.
466 packet = gen.NextPacket(payload_len); 460 EXPECT_EQ(PacketBuffer::kOK,
467 EXPECT_EQ(PacketBuffer::kOK, buffer->InsertPacket(packet)); 461 buffer->InsertPacket(gen.NextPacket(payload_len)));
468 EXPECT_EQ(PacketBuffer::kInvalidPointer, buffer->NextTimestamp(NULL)); 462 EXPECT_EQ(PacketBuffer::kInvalidPointer, buffer->NextTimestamp(NULL));
469 EXPECT_EQ(PacketBuffer::kInvalidPointer, 463 EXPECT_EQ(PacketBuffer::kInvalidPointer,
470 buffer->NextHigherTimestamp(0, NULL)); 464 buffer->NextHigherTimestamp(0, NULL));
471 delete buffer; 465 delete buffer;
472 466
473 // Insert packet list of three packets, where the second packet has an invalid 467 // Insert packet list of three packets, where the second packet has an invalid
474 // payload. Expect first packet to be inserted, and the remaining two to be 468 // payload. Expect first packet to be inserted, and the remaining two to be
475 // discarded. 469 // discarded.
476 buffer = new PacketBuffer(100, &tick_timer); // 100 packets. 470 buffer = new PacketBuffer(100, &tick_timer); // 100 packets.
477 PacketList list; 471 PacketList list;
478 list.push_back(gen.NextPacket(payload_len)); // Valid packet. 472 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
479 packet = gen.NextPacket(payload_len); 473 {
480 packet->payload.Clear(); // Invalid. 474 Packet packet = gen.NextPacket(payload_len);
481 list.push_back(packet); 475 packet.payload.Clear(); // Invalid.
476 list.push_back(std::move(packet));
477 }
482 list.push_back(gen.NextPacket(payload_len)); // Valid packet. 478 list.push_back(gen.NextPacket(payload_len)); // Valid packet.
483 MockDecoderDatabase decoder_database; 479 MockDecoderDatabase decoder_database;
484 auto factory = CreateBuiltinAudioDecoderFactory(); 480 auto factory = CreateBuiltinAudioDecoderFactory();
485 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory); 481 const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory);
486 EXPECT_CALL(decoder_database, GetDecoderInfo(0)) 482 EXPECT_CALL(decoder_database, GetDecoderInfo(0))
487 .WillRepeatedly(Return(&info)); 483 .WillRepeatedly(Return(&info));
488 rtc::Optional<uint8_t> current_pt; 484 rtc::Optional<uint8_t> current_pt;
489 rtc::Optional<uint8_t> current_cng_pt; 485 rtc::Optional<uint8_t> current_cng_pt;
490 EXPECT_EQ(PacketBuffer::kInvalidPacket, 486 EXPECT_EQ(PacketBuffer::kInvalidPacket,
491 buffer->InsertPacketList(&list, 487 buffer->InsertPacketList(&list,
492 decoder_database, 488 decoder_database,
493 &current_pt, 489 &current_pt,
494 &current_cng_pt)); 490 &current_cng_pt));
495 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list. 491 EXPECT_TRUE(list.empty()); // The PacketBuffer should have depleted the list.
496 EXPECT_EQ(1u, buffer->NumPacketsInBuffer()); 492 EXPECT_EQ(1u, buffer->NumPacketsInBuffer());
497 delete buffer; 493 delete buffer;
498 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted. 494 EXPECT_CALL(decoder_database, Die()); // Called when object is deleted.
499 } 495 }
500 496
501 // Test packet comparison function. 497 // Test packet comparison function.
502 // The function should return true if the first packet "goes before" the second. 498 // The function should return true if the first packet "goes before" the second.
503 TEST(PacketBuffer, ComparePackets) { 499 TEST(PacketBuffer, ComparePackets) {
504 PacketGenerator gen(0, 0, 0, 10); 500 PacketGenerator gen(0, 0, 0, 10);
505 std::unique_ptr<Packet> a(gen.NextPacket(10)); // SN = 0, TS = 0. 501 Packet a(gen.NextPacket(10)); // SN = 0, TS = 0.
506 std::unique_ptr<Packet> b(gen.NextPacket(10)); // SN = 1, TS = 10. 502 Packet b(gen.NextPacket(10)); // SN = 1, TS = 10.
507 EXPECT_FALSE(*a == *b); 503 EXPECT_FALSE(a == b);
508 EXPECT_TRUE(*a != *b); 504 EXPECT_TRUE(a != b);
509 EXPECT_TRUE(*a < *b); 505 EXPECT_TRUE(a < b);
510 EXPECT_FALSE(*a > *b); 506 EXPECT_FALSE(a > b);
511 EXPECT_TRUE(*a <= *b); 507 EXPECT_TRUE(a <= b);
512 EXPECT_FALSE(*a >= *b); 508 EXPECT_FALSE(a >= b);
513 509
514 // Testing wrap-around case; 'a' is earlier but has a larger timestamp value. 510 // Testing wrap-around case; 'a' is earlier but has a larger timestamp value.
515 a->timestamp = 0xFFFFFFFF - 10; 511 a.timestamp = 0xFFFFFFFF - 10;
516 EXPECT_FALSE(*a == *b); 512 EXPECT_FALSE(a == b);
517 EXPECT_TRUE(*a != *b); 513 EXPECT_TRUE(a != b);
518 EXPECT_TRUE(*a < *b); 514 EXPECT_TRUE(a < b);
519 EXPECT_FALSE(*a > *b); 515 EXPECT_FALSE(a > b);
520 EXPECT_TRUE(*a <= *b); 516 EXPECT_TRUE(a <= b);
521 EXPECT_FALSE(*a >= *b); 517 EXPECT_FALSE(a >= b);
522 518
523 // Test equal packets. 519 // Test equal packets.
524 EXPECT_TRUE(*a == *a); 520 EXPECT_TRUE(a == a);
525 EXPECT_FALSE(*a != *a); 521 EXPECT_FALSE(a != a);
526 EXPECT_FALSE(*a < *a); 522 EXPECT_FALSE(a < a);
527 EXPECT_FALSE(*a > *a); 523 EXPECT_FALSE(a > a);
528 EXPECT_TRUE(*a <= *a); 524 EXPECT_TRUE(a <= a);
529 EXPECT_TRUE(*a >= *a); 525 EXPECT_TRUE(a >= a);
530 526
531 // Test equal timestamps but different sequence numbers (0 and 1). 527 // Test equal timestamps but different sequence numbers (0 and 1).
532 a->timestamp = b->timestamp; 528 a.timestamp = b.timestamp;
533 EXPECT_FALSE(*a == *b); 529 EXPECT_FALSE(a == b);
534 EXPECT_TRUE(*a != *b); 530 EXPECT_TRUE(a != b);
535 EXPECT_TRUE(*a < *b); 531 EXPECT_TRUE(a < b);
536 EXPECT_FALSE(*a > *b); 532 EXPECT_FALSE(a > b);
537 EXPECT_TRUE(*a <= *b); 533 EXPECT_TRUE(a <= b);
538 EXPECT_FALSE(*a >= *b); 534 EXPECT_FALSE(a >= b);
539 535
540 // Test equal timestamps but different sequence numbers (32767 and 1). 536 // Test equal timestamps but different sequence numbers (32767 and 1).
541 a->sequence_number = 0xFFFF; 537 a.sequence_number = 0xFFFF;
542 EXPECT_FALSE(*a == *b); 538 EXPECT_FALSE(a == b);
543 EXPECT_TRUE(*a != *b); 539 EXPECT_TRUE(a != b);
544 EXPECT_TRUE(*a < *b); 540 EXPECT_TRUE(a < b);
545 EXPECT_FALSE(*a > *b); 541 EXPECT_FALSE(a > b);
546 EXPECT_TRUE(*a <= *b); 542 EXPECT_TRUE(a <= b);
547 EXPECT_FALSE(*a >= *b); 543 EXPECT_FALSE(a >= b);
548 544
549 // Test equal timestamps and sequence numbers, but differing priorities. 545 // Test equal timestamps and sequence numbers, but differing priorities.
550 a->sequence_number = b->sequence_number; 546 a.sequence_number = b.sequence_number;
551 a->priority = {1, 0}; 547 a.priority = {1, 0};
552 b->priority = {0, 0}; 548 b.priority = {0, 0};
553 // a after b 549 // a after b
554 EXPECT_FALSE(*a == *b); 550 EXPECT_FALSE(a == b);
555 EXPECT_TRUE(*a != *b); 551 EXPECT_TRUE(a != b);
556 EXPECT_FALSE(*a < *b); 552 EXPECT_FALSE(a < b);
557 EXPECT_TRUE(*a > *b); 553 EXPECT_TRUE(a > b);
558 EXPECT_FALSE(*a <= *b); 554 EXPECT_FALSE(a <= b);
559 EXPECT_TRUE(*a >= *b); 555 EXPECT_TRUE(a >= b);
560 556
561 std::unique_ptr<Packet> c(gen.NextPacket(0)); // SN = 2, TS = 20. 557 Packet c(gen.NextPacket(0)); // SN = 2, TS = 20.
562 std::unique_ptr<Packet> d(gen.NextPacket(0)); // SN = 3, TS = 20. 558 Packet d(gen.NextPacket(0)); // SN = 3, TS = 20.
563 c->timestamp = b->timestamp; 559 c.timestamp = b.timestamp;
564 d->timestamp = b->timestamp; 560 d.timestamp = b.timestamp;
565 c->sequence_number = b->sequence_number; 561 c.sequence_number = b.sequence_number;
566 d->sequence_number = b->sequence_number; 562 d.sequence_number = b.sequence_number;
567 c->priority = {1, 1}; 563 c.priority = {1, 1};
568 d->priority = {0, 1}; 564 d.priority = {0, 1};
569 // c after d 565 // c after d
570 EXPECT_FALSE(*c == *d); 566 EXPECT_FALSE(c == d);
571 EXPECT_TRUE(*c != *d); 567 EXPECT_TRUE(c != d);
572 EXPECT_FALSE(*c < *d); 568 EXPECT_FALSE(c < d);
573 EXPECT_TRUE(*c > *d); 569 EXPECT_TRUE(c > d);
574 EXPECT_FALSE(*c <= *d); 570 EXPECT_FALSE(c <= d);
575 EXPECT_TRUE(*c >= *d); 571 EXPECT_TRUE(c >= d);
576 572
577 // c after a 573 // c after a
578 EXPECT_FALSE(*c == *a); 574 EXPECT_FALSE(c == a);
579 EXPECT_TRUE(*c != *a); 575 EXPECT_TRUE(c != a);
580 EXPECT_FALSE(*c < *a); 576 EXPECT_FALSE(c < a);
581 EXPECT_TRUE(*c > *a); 577 EXPECT_TRUE(c > a);
582 EXPECT_FALSE(*c <= *a); 578 EXPECT_FALSE(c <= a);
583 EXPECT_TRUE(*c >= *a); 579 EXPECT_TRUE(c >= a);
584 580
585 // c after b 581 // c after b
586 EXPECT_FALSE(*c == *b); 582 EXPECT_FALSE(c == b);
587 EXPECT_TRUE(*c != *b); 583 EXPECT_TRUE(c != b);
588 EXPECT_FALSE(*c < *b); 584 EXPECT_FALSE(c < b);
589 EXPECT_TRUE(*c > *b); 585 EXPECT_TRUE(c > b);
590 EXPECT_FALSE(*c <= *b); 586 EXPECT_FALSE(c <= b);
591 EXPECT_TRUE(*c >= *b); 587 EXPECT_TRUE(c >= b);
592 588
593 // a after d 589 // a after d
594 EXPECT_FALSE(*a == *d); 590 EXPECT_FALSE(a == d);
595 EXPECT_TRUE(*a != *d); 591 EXPECT_TRUE(a != d);
596 EXPECT_FALSE(*a < *d); 592 EXPECT_FALSE(a < d);
597 EXPECT_TRUE(*a > *d); 593 EXPECT_TRUE(a > d);
598 EXPECT_FALSE(*a <= *d); 594 EXPECT_FALSE(a <= d);
599 EXPECT_TRUE(*a >= *d); 595 EXPECT_TRUE(a >= d);
600 596
601 // d after b 597 // d after b
602 EXPECT_FALSE(*d == *b); 598 EXPECT_FALSE(d == b);
603 EXPECT_TRUE(*d != *b); 599 EXPECT_TRUE(d != b);
604 EXPECT_FALSE(*d < *b); 600 EXPECT_FALSE(d < b);
605 EXPECT_TRUE(*d > *b); 601 EXPECT_TRUE(d > b);
606 EXPECT_FALSE(*d <= *b); 602 EXPECT_FALSE(d <= b);
607 EXPECT_TRUE(*d >= *b); 603 EXPECT_TRUE(d >= b);
608 }
609
610 // Test the DeleteFirstPacket DeleteAllPackets methods.
611 TEST(PacketBuffer, DeleteAllPackets) {
612 PacketGenerator gen(0, 0, 0, 10);
613 PacketList list;
614 const int payload_len = 10;
615
616 // Insert 10 small packets.
617 for (int i = 0; i < 10; ++i) {
618 Packet* packet = gen.NextPacket(payload_len);
619 list.push_back(packet);
620 }
621 EXPECT_TRUE(PacketBuffer::DeleteFirstPacket(&list));
622 EXPECT_EQ(9u, list.size());
623 PacketBuffer::DeleteAllPackets(&list);
624 EXPECT_TRUE(list.empty());
625 EXPECT_FALSE(PacketBuffer::DeleteFirstPacket(&list));
626 } 604 }
627 605
628 namespace { 606 namespace {
629 void TestIsObsoleteTimestamp(uint32_t limit_timestamp) { 607 void TestIsObsoleteTimestamp(uint32_t limit_timestamp) {
630 // Check with zero horizon, which implies that the horizon is at 2^31, i.e., 608 // Check with zero horizon, which implies that the horizon is at 2^31, i.e.,
631 // half the timestamp range. 609 // half the timestamp range.
632 static const uint32_t kZeroHorizon = 0; 610 static const uint32_t kZeroHorizon = 0;
633 static const uint32_t k2Pow31Minus1 = 0x7FFFFFFF; 611 static const uint32_t k2Pow31Minus1 = 0x7FFFFFFF;
634 // Timestamp on the limit is not old. 612 // Timestamp on the limit is not old.
635 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp( 613 EXPECT_FALSE(PacketBuffer::IsObsoleteTimestamp(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
681 // Test the IsObsoleteTimestamp method with different limit timestamps. 659 // Test the IsObsoleteTimestamp method with different limit timestamps.
682 TEST(PacketBuffer, IsObsoleteTimestamp) { 660 TEST(PacketBuffer, IsObsoleteTimestamp) {
683 TestIsObsoleteTimestamp(0); 661 TestIsObsoleteTimestamp(0);
684 TestIsObsoleteTimestamp(1); 662 TestIsObsoleteTimestamp(1);
685 TestIsObsoleteTimestamp(0xFFFFFFFF); // -1 in uint32_t. 663 TestIsObsoleteTimestamp(0xFFFFFFFF); // -1 in uint32_t.
686 TestIsObsoleteTimestamp(0x80000000); // 2^31. 664 TestIsObsoleteTimestamp(0x80000000); // 2^31.
687 TestIsObsoleteTimestamp(0x80000001); // 2^31 + 1. 665 TestIsObsoleteTimestamp(0x80000001); // 2^31 + 1.
688 TestIsObsoleteTimestamp(0x7FFFFFFF); // 2^31 - 1. 666 TestIsObsoleteTimestamp(0x7FFFFFFF); // 2^31 - 1.
689 } 667 }
690 } // namespace webrtc 668 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_coding/neteq/packet_buffer.cc ('k') | webrtc/modules/audio_coding/neteq/red_payload_splitter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698