OLD | NEW |
---|---|
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 |
11 // Unit tests for PacketBuffer class. | 11 // Unit tests for PacketBuffer class. |
12 | 12 |
13 #include "webrtc/modules/audio_coding/neteq/packet_buffer.h" | 13 #include "webrtc/modules/audio_coding/neteq/packet_buffer.h" |
14 #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h" | 14 #include "webrtc/api/audio_codecs/builtin_audio_decoder_factory.h" |
15 #include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h" | 15 #include "webrtc/modules/audio_coding/neteq/mock/mock_decoder_database.h" |
16 #include "webrtc/modules/audio_coding/neteq/mock/mock_statistics_calculator.h" | 16 #include "webrtc/modules/audio_coding/neteq/mock/mock_statistics_calculator.h" |
17 #include "webrtc/modules/audio_coding/neteq/packet.h" | 17 #include "webrtc/modules/audio_coding/neteq/packet.h" |
18 #include "webrtc/modules/audio_coding/neteq/tick_timer.h" | 18 #include "webrtc/modules/audio_coding/neteq/tick_timer.h" |
19 #include "webrtc/test/gmock.h" | 19 #include "webrtc/test/gmock.h" |
20 #include "webrtc/test/gtest.h" | 20 #include "webrtc/test/gtest.h" |
21 | 21 |
22 using ::testing::Return; | 22 using ::testing::Return; |
23 using ::testing::StrictMock; | 23 using ::testing::StrictMock; |
24 using ::testing::_; | 24 using ::testing::_; |
25 using ::testing::InSequence; | |
26 using ::testing::MockFunction; | |
25 | 27 |
26 namespace webrtc { | 28 namespace webrtc { |
27 | 29 |
28 // Helper class to generate packets. Packets must be deleted by the user. | 30 // Helper class to generate packets. Packets must be deleted by the user. |
29 class PacketGenerator { | 31 class PacketGenerator { |
30 public: | 32 public: |
31 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size); | 33 PacketGenerator(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size); |
32 virtual ~PacketGenerator() {} | 34 virtual ~PacketGenerator() {} |
33 void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size); | 35 void Reset(uint16_t seq_no, uint32_t ts, uint8_t pt, int frame_size); |
34 Packet NextPacket(int payload_size_bytes); | 36 Packet NextPacket(int payload_size_bytes); |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 buffer.InsertPacket(gen.NextPacket(payload_len)); | 309 buffer.InsertPacket(gen.NextPacket(payload_len)); |
308 } | 310 } |
309 EXPECT_EQ(10u, buffer.NumPacketsInBuffer()); | 311 EXPECT_EQ(10u, buffer.NumPacketsInBuffer()); |
310 | 312 |
311 StrictMock<MockStatisticsCalculator> mock_stats; | 313 StrictMock<MockStatisticsCalculator> mock_stats; |
312 uint32_t current_ts = start_ts; | 314 uint32_t current_ts = start_ts; |
313 | 315 |
314 // Discard them one by one and make sure that the right packets are at the | 316 // Discard them one by one and make sure that the right packets are at the |
315 // front of the buffer. | 317 // front of the buffer. |
316 constexpr int kDiscardPackets = 5; | 318 constexpr int kDiscardPackets = 5; |
319 | |
320 // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction | |
321 // check ensures that exactly one call to PacketsDiscarded happens in each | |
322 // DiscardNextPacket call. | |
minyue-webrtc
2017/07/05 12:11:08
nice, another way would be avoid the for-loop. It
hlundin-webrtc
2017/07/05 13:42:35
True. But I still think I would like to have the I
| |
323 InSequence s; | |
324 MockFunction<void(int check_point_id)> check; | |
317 for (int i = 0; i < kDiscardPackets; ++i) { | 325 for (int i = 0; i < kDiscardPackets; ++i) { |
318 uint32_t ts; | 326 uint32_t ts; |
319 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts)); | 327 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts)); |
320 EXPECT_EQ(current_ts, ts); | 328 EXPECT_EQ(current_ts, ts); |
321 EXPECT_CALL(mock_stats, PacketsDiscarded(1)).Times(1); | 329 EXPECT_CALL(mock_stats, PacketsDiscarded(1)); |
330 EXPECT_CALL(check, Call(i)); | |
minyue-webrtc
2017/07/05 12:11:08
why not arbitrary number
hlundin-webrtc
2017/07/05 13:42:35
Indexing the calls to the check function makes the
| |
322 EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket(&mock_stats)); | 331 EXPECT_EQ(PacketBuffer::kOK, buffer.DiscardNextPacket(&mock_stats)); |
323 current_ts += ts_increment; | 332 current_ts += ts_increment; |
333 check.Call(i); | |
324 } | 334 } |
325 | 335 |
326 constexpr int kRemainingPackets = kTotalPackets - kDiscardPackets; | 336 constexpr int kRemainingPackets = kTotalPackets - kDiscardPackets; |
327 // This will not discard any packets because the oldest packet is newer than | 337 // This will all remaining packets but one. The oldest packet is older than |
minyue-webrtc
2017/07/05 12:11:08
This will "discard" all remaining ...
hlundin-webrtc
2017/07/05 13:42:35
Done.
| |
328 // the indicated horizon_samples. | 338 // the indicated horizon_samples, and will thus be left in the buffer. |
339 EXPECT_CALL(mock_stats, PacketsDiscarded(kRemainingPackets - 1)); | |
minyue-webrtc
2017/07/05 12:11:08
define 1 as kSkipPackets
hlundin-webrtc
2017/07/05 13:42:35
Done.
| |
340 EXPECT_CALL(check, Call(17)); // Arbitrary id number. | |
329 buffer.DiscardOldPackets(start_ts + kTotalPackets * ts_increment, | 341 buffer.DiscardOldPackets(start_ts + kTotalPackets * ts_increment, |
330 kRemainingPackets * ts_increment, &mock_stats); | 342 kRemainingPackets * ts_increment, &mock_stats); |
343 check.Call(17); // Same arbitrary id number. | |
344 | |
345 EXPECT_EQ(1u, buffer.NumPacketsInBuffer()); | |
331 uint32_t ts; | 346 uint32_t ts; |
332 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts)); | 347 EXPECT_EQ(PacketBuffer::kOK, buffer.NextTimestamp(&ts)); |
333 EXPECT_EQ(current_ts, ts); | 348 EXPECT_EQ(current_ts, ts); |
334 | 349 |
335 // Discard all remaining packets. | 350 // Discard all remaining packets. |
336 EXPECT_CALL(mock_stats, PacketsDiscarded(1)).Times(kRemainingPackets); | 351 EXPECT_CALL(mock_stats, PacketsDiscarded(1)); |
337 buffer.DiscardAllOldPackets(start_ts + kTotalPackets * ts_increment, | 352 buffer.DiscardAllOldPackets(start_ts + kTotalPackets * ts_increment, |
338 &mock_stats); | 353 &mock_stats); |
339 | 354 |
340 EXPECT_TRUE(buffer.Empty()); | 355 EXPECT_TRUE(buffer.Empty()); |
341 } | 356 } |
342 | 357 |
343 TEST(PacketBuffer, Reordering) { | 358 TEST(PacketBuffer, Reordering) { |
344 TickTimer tick_timer; | 359 TickTimer tick_timer; |
345 PacketBuffer buffer(100, &tick_timer); // 100 packets. | 360 PacketBuffer buffer(100, &tick_timer); // 100 packets. |
346 const uint16_t start_seq_no = 17; | 361 const uint16_t start_seq_no = 17; |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
684 // Test the IsObsoleteTimestamp method with different limit timestamps. | 699 // Test the IsObsoleteTimestamp method with different limit timestamps. |
685 TEST(PacketBuffer, IsObsoleteTimestamp) { | 700 TEST(PacketBuffer, IsObsoleteTimestamp) { |
686 TestIsObsoleteTimestamp(0); | 701 TestIsObsoleteTimestamp(0); |
687 TestIsObsoleteTimestamp(1); | 702 TestIsObsoleteTimestamp(1); |
688 TestIsObsoleteTimestamp(0xFFFFFFFF); // -1 in uint32_t. | 703 TestIsObsoleteTimestamp(0xFFFFFFFF); // -1 in uint32_t. |
689 TestIsObsoleteTimestamp(0x80000000); // 2^31. | 704 TestIsObsoleteTimestamp(0x80000000); // 2^31. |
690 TestIsObsoleteTimestamp(0x80000001); // 2^31 + 1. | 705 TestIsObsoleteTimestamp(0x80000001); // 2^31 + 1. |
691 TestIsObsoleteTimestamp(0x7FFFFFFF); // 2^31 - 1. | 706 TestIsObsoleteTimestamp(0x7FFFFFFF); // 2^31 - 1. |
692 } | 707 } |
693 } // namespace webrtc | 708 } // namespace webrtc |
OLD | NEW |