blob: 759003b294a05fd34972dd0155bfd3d7de200a00 [file] [log] [blame]
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +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
kwibergbfefb032016-05-01 14:53:46 -070011#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "call/call.h"
14#include "modules/rtp_rtcp/include/rtp_header_parser.h"
15#include "system_wrappers/include/clock.h"
16#include "test/fake_network_pipe.h"
17#include "test/gmock.h"
18#include "test/gtest.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000019
20using ::testing::_;
21using ::testing::AnyNumber;
22using ::testing::Return;
23using ::testing::Invoke;
24
25namespace webrtc {
26
minyue20c84cc2017-04-10 16:57:57 -070027class TestDemuxer : public Demuxer {
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000028 public:
minyue20c84cc2017-04-10 16:57:57 -070029 void IncomingPacket(NetworkPacket* packet) {
30 DeliverPacket(packet, PacketTime());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000031 }
32
minyue20c84cc2017-04-10 16:57:57 -070033 MOCK_METHOD1(SetReceiver, void(PacketReceiver* receiver));
34 MOCK_METHOD2(DeliverPacket,
35 void(const NetworkPacket* packet,
36 const PacketTime& packet_time));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000037};
38
minyue20c84cc2017-04-10 16:57:57 -070039class ReorderTestDemuxer : public TestDemuxer {
philipela2c55232016-01-26 08:41:53 -080040 public:
minyue20c84cc2017-04-10 16:57:57 -070041 void DeliverPacket(const NetworkPacket* packet,
42 const PacketTime& packet_time) override {
43 RTC_DCHECK_GE(packet->data_length(), sizeof(int));
philipela2c55232016-01-26 08:41:53 -080044 int seq_num;
minyue20c84cc2017-04-10 16:57:57 -070045 memcpy(&seq_num, packet->data(), sizeof(int));
philipela2c55232016-01-26 08:41:53 -080046 delivered_sequence_numbers_.push_back(seq_num);
philipela2c55232016-01-26 08:41:53 -080047 }
48 std::vector<int> delivered_sequence_numbers_;
49};
50
minyue20c84cc2017-04-10 16:57:57 -070051class MockReceiver : public PacketReceiver {
52 public:
53 MOCK_METHOD4(
54 DeliverPacket,
55 DeliveryStatus(MediaType, const uint8_t*, size_t, const PacketTime&));
56};
57
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000058class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 11:20:58 +010059 public:
60 FakeNetworkPipeTest() : fake_clock_(12345) {}
61
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000062 protected:
philipela2c55232016-01-26 08:41:53 -080063 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
kwiberg352444f2016-11-28 15:58:53 -080064 RTC_DCHECK_GE(packet_size, sizeof(int));
kwibergbfefb032016-05-01 14:53:46 -070065 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000066 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 08:41:53 -080067 // Set a sequence number for the packets by
68 // using the first bytes in the packet.
69 memcpy(packet.get(), &i, sizeof(int));
70 pipe->SendPacket(packet.get(), packet_size);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000071 }
72 }
73
philipela2c55232016-01-26 08:41:53 -080074 int PacketTimeMs(int capacity_kbps, int packet_size) const {
75 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000076 }
77
Peter Boströmd3c94472015-12-09 11:20:58 +010078 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000079};
80
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000081// Test the capacity link and verify we get as many packets as we expect.
82TEST_F(FakeNetworkPipeTest, CapacityTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +000083 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +000084 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000085 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 16:57:57 -070086 TestDemuxer* demuxer = new TestDemuxer();
87 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
88 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000089
90 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
91 // get through the pipe.
92 const int kNumPackets = 10;
93 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 16:57:57 -070094 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000095
96 // Time to get one packet through the link.
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +000097 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
98 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +000099
100 // Time haven't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 16:57:57 -0700101 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000102 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000103
104 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100105 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
minyue20c84cc2017-04-10 16:57:57 -0700106 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000107 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000108
109 // Release all but one packet
Peter Boströmd3c94472015-12-09 11:20:58 +0100110 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
minyue20c84cc2017-04-10 16:57:57 -0700111 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000112 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000113
114 // And the last one.
Peter Boströmd3c94472015-12-09 11:20:58 +0100115 fake_clock_.AdvanceTimeMilliseconds(1);
minyue20c84cc2017-04-10 16:57:57 -0700116 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000117 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000118}
119
120// Test the extra network delay.
121TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000122 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000123 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000124 config.queue_delay_ms = 100;
125 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 16:57:57 -0700126 TestDemuxer* demuxer = new TestDemuxer();
127 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
128 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000129
130 const int kNumPackets = 2;
131 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 16:57:57 -0700132 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000133
134 // Time to get one packet through the link.
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000135 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
136 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000137
138 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 11:20:58 +0100139 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
minyue20c84cc2017-04-10 16:57:57 -0700140 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000141 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000142
143 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100144 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
minyue20c84cc2017-04-10 16:57:57 -0700145 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000146 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000147
148 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 11:20:58 +0100149 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
minyue20c84cc2017-04-10 16:57:57 -0700150 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000151 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000152}
153
154// Test the number of buffers and packets are dropped when sending too many
155// packets too quickly.
156TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000157 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000158 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000159 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 16:57:57 -0700160 TestDemuxer* demuxer = new TestDemuxer();
161 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
162 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000163
164 const int kPacketSize = 1000;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000165 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
166 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000167
168 // Send three packets and verify only 2 are delivered.
169 SendPackets(pipe.get(), 3, kPacketSize);
170
171 // Increase time enough to deliver all three packets, verify only two are
172 // delivered.
Peter Boströmd3c94472015-12-09 11:20:58 +0100173 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
minyue20c84cc2017-04-10 16:57:57 -0700174 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000175 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000176}
177
178// Test we get statistics as expected.
179TEST_F(FakeNetworkPipeTest, StatisticsTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000180 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000181 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000182 config.queue_delay_ms = 20;
183 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 16:57:57 -0700184 TestDemuxer* demuxer = new TestDemuxer();
185 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
186 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000187
188 const int kPacketSize = 1000;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000189 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
190 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000191
192 // Send three packets and verify only 2 are delivered.
193 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 11:20:58 +0100194 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
195 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000196
minyue20c84cc2017-04-10 16:57:57 -0700197 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000198 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000199
mflodman@webrtc.org7acb65a2012-12-13 15:53:11 +0000200 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
201 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000202 EXPECT_EQ(pipe->AverageDelay(), 170);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25 +0000203 EXPECT_EQ(pipe->sent_packets(), 2u);
204 EXPECT_EQ(pipe->dropped_packets(), 1u);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000205 EXPECT_EQ(pipe->PercentageLoss(), 1/3.f);
206}
207
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000208// Change the link capacity half-way through the test and verify that the
209// delivery times change accordingly.
210TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
211 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000212 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000213 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 16:57:57 -0700214 TestDemuxer* demuxer = new TestDemuxer();
215 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
216 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000217
218 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
219 // get through the pipe.
220 const int kNumPackets = 10;
221 const int kPacketSize = 1000;
222 SendPackets(pipe.get(), kNumPackets, kPacketSize);
223
224 // Time to get one packet through the link.
225 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
226
227 // Time hasn't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 16:57:57 -0700228 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000229 pipe->Process();
230
231 // Advance time in steps to release one packet at a time.
232 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100233 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
minyue20c84cc2017-04-10 16:57:57 -0700234 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000235 pipe->Process();
236 }
237
238 // Change the capacity.
239 config.link_capacity_kbps /= 2; // Reduce to 50%.
240 pipe->SetConfig(config);
241
242 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
243 // seconds to get them through the pipe.
244 SendPackets(pipe.get(), kNumPackets, kPacketSize);
245
246 // Time to get one packet through the link.
247 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
248
249 // Time hasn't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 16:57:57 -0700250 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000251 pipe->Process();
252
253 // Advance time in steps to release one packet at a time.
254 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100255 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
minyue20c84cc2017-04-10 16:57:57 -0700256 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000257 pipe->Process();
258 }
259
260 // Check that all the packets were sent.
261 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets());
Peter Boströmd3c94472015-12-09 11:20:58 +0100262 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
minyue20c84cc2017-04-10 16:57:57 -0700263 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000264 pipe->Process();
265}
266
267// Change the link capacity half-way through the test and verify that the
268// delivery times change accordingly.
269TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
270 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06 +0000271 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000272 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 16:57:57 -0700273 TestDemuxer* demuxer = new TestDemuxer();
274 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
275 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000276
277 // Add 10 packets of 1000 bytes, = 80 kb.
278 const int kNumPackets = 10;
279 const int kPacketSize = 1000;
280 SendPackets(pipe.get(), kNumPackets, kPacketSize);
281
282 // Time to get one packet through the link at the initial speed.
283 int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
284
285 // Change the capacity.
286 config.link_capacity_kbps *= 2; // Double the capacity.
287 pipe->SetConfig(config);
288
289 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
290 // seconds to get them through the pipe.
291 SendPackets(pipe.get(), kNumPackets, kPacketSize);
292
293 // Time to get one packet through the link at the new capacity.
294 int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
295
296 // Time hasn't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 16:57:57 -0700297 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000298 pipe->Process();
299
300 // Advance time in steps to release one packet at a time.
301 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100302 fake_clock_.AdvanceTimeMilliseconds(packet_time_1_ms);
minyue20c84cc2017-04-10 16:57:57 -0700303 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000304 pipe->Process();
305 }
306
307 // Advance time in steps to release one packet at a time.
308 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 11:20:58 +0100309 fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms);
minyue20c84cc2017-04-10 16:57:57 -0700310 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000311 pipe->Process();
312 }
313
314 // Check that all the packets were sent.
315 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets());
Peter Boströmd3c94472015-12-09 11:20:58 +0100316 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
minyue20c84cc2017-04-10 16:57:57 -0700317 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52 +0000318 pipe->Process();
319}
philipela2c55232016-01-26 08:41:53 -0800320
321// At first disallow reordering and then allow reordering.
322TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
323 FakeNetworkPipe::Config config;
324 config.queue_length_packets = 1000;
325 config.link_capacity_kbps = 800;
326 config.queue_delay_ms = 100;
327 config.delay_standard_deviation_ms = 10;
minyue20c84cc2017-04-10 16:57:57 -0700328 ReorderTestDemuxer* demuxer = new ReorderTestDemuxer();
329 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
330 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
philipela2c55232016-01-26 08:41:53 -0800331
332 const uint32_t kNumPackets = 100;
333 const int kPacketSize = 10;
334 SendPackets(pipe.get(), kNumPackets, kPacketSize);
335 fake_clock_.AdvanceTimeMilliseconds(1000);
336 pipe->Process();
337
338 // Confirm that all packets have been delivered in order.
minyue20c84cc2017-04-10 16:57:57 -0700339 EXPECT_EQ(kNumPackets, demuxer->delivered_sequence_numbers_.size());
philipela2c55232016-01-26 08:41:53 -0800340 int last_seq_num = -1;
minyue20c84cc2017-04-10 16:57:57 -0700341 for (int seq_num : demuxer->delivered_sequence_numbers_) {
philipela2c55232016-01-26 08:41:53 -0800342 EXPECT_GT(seq_num, last_seq_num);
343 last_seq_num = seq_num;
344 }
345
346 config.allow_reordering = true;
347 pipe->SetConfig(config);
348 SendPackets(pipe.get(), kNumPackets, kPacketSize);
349 fake_clock_.AdvanceTimeMilliseconds(1000);
minyue20c84cc2017-04-10 16:57:57 -0700350 demuxer->delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 08:41:53 -0800351 pipe->Process();
352
353 // Confirm that all packets have been delivered
354 // and that reordering has occured.
minyue20c84cc2017-04-10 16:57:57 -0700355 EXPECT_EQ(kNumPackets, demuxer->delivered_sequence_numbers_.size());
philipela2c55232016-01-26 08:41:53 -0800356 bool reordering_has_occured = false;
357 last_seq_num = -1;
minyue20c84cc2017-04-10 16:57:57 -0700358 for (int seq_num : demuxer->delivered_sequence_numbers_) {
philipela2c55232016-01-26 08:41:53 -0800359 if (last_seq_num > seq_num) {
360 reordering_has_occured = true;
361 break;
362 }
363 last_seq_num = seq_num;
364 }
365 EXPECT_TRUE(reordering_has_occured);
366}
philipel536378b2016-05-31 03:20:23 -0700367
368TEST_F(FakeNetworkPipeTest, BurstLoss) {
369 const int kLossPercent = 5;
370 const int kAvgBurstLength = 3;
371 const int kNumPackets = 10000;
372 const int kPacketSize = 10;
373
374 FakeNetworkPipe::Config config;
375 config.queue_length_packets = kNumPackets;
376 config.loss_percent = kLossPercent;
377 config.avg_burst_loss_length = kAvgBurstLength;
minyue20c84cc2017-04-10 16:57:57 -0700378 ReorderTestDemuxer* demuxer = new ReorderTestDemuxer();
379 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
380 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
philipel536378b2016-05-31 03:20:23 -0700381
382 SendPackets(pipe.get(), kNumPackets, kPacketSize);
383 fake_clock_.AdvanceTimeMilliseconds(1000);
384 pipe->Process();
385
386 // Check that the average loss is |kLossPercent| percent.
minyue20c84cc2017-04-10 16:57:57 -0700387 int lost_packets = kNumPackets - demuxer->delivered_sequence_numbers_.size();
philipel536378b2016-05-31 03:20:23 -0700388 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
389
390 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
391
392 // Find the number of bursts that has occurred.
minyue20c84cc2017-04-10 16:57:57 -0700393 size_t received_packets = demuxer->delivered_sequence_numbers_.size();
philipel536378b2016-05-31 03:20:23 -0700394 int num_bursts = 0;
395 for (size_t i = 0; i < received_packets - 1; ++i) {
minyue20c84cc2017-04-10 16:57:57 -0700396 int diff = demuxer->delivered_sequence_numbers_[i + 1] -
397 demuxer->delivered_sequence_numbers_[i];
philipel536378b2016-05-31 03:20:23 -0700398 if (diff > 1)
399 ++num_bursts;
400 }
401
402 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
403
404 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
405}
minyue20c84cc2017-04-10 16:57:57 -0700406
407TEST_F(FakeNetworkPipeTest, SetReceiver) {
408 FakeNetworkPipe::Config config;
409 TestDemuxer* demuxer = new TestDemuxer();
410 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
411 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
412 MockReceiver packet_receiver;
413 EXPECT_CALL(*demuxer, SetReceiver(&packet_receiver)).Times(1);
414 pipe->SetReceiver(&packet_receiver);
415}
416
417TEST(DemuxerImplTest, Demuxing) {
418 constexpr uint8_t kVideoPayloadType = 100;
419 constexpr uint8_t kAudioPayloadType = 101;
420 constexpr int64_t kTimeNow = 12345;
421 constexpr int64_t kArrivalTime = kTimeNow - 1;
422 constexpr size_t kPacketSize = 10;
423 DemuxerImpl demuxer({{kVideoPayloadType, MediaType::VIDEO},
424 {kAudioPayloadType, MediaType::AUDIO}});
425
426 MockReceiver mock_receiver;
427 demuxer.SetReceiver(&mock_receiver);
428
429 std::vector<uint8_t> data(kPacketSize);
430 data[1] = kVideoPayloadType;
431 std::unique_ptr<NetworkPacket> packet(
432 new NetworkPacket(&data[0], kPacketSize, kTimeNow, kArrivalTime));
433 EXPECT_CALL(mock_receiver, DeliverPacket(MediaType::VIDEO, _, _, _))
434 .WillOnce(Return(PacketReceiver::DELIVERY_OK));
435 demuxer.DeliverPacket(packet.get(), PacketTime());
436
437 data[1] = kAudioPayloadType;
438 packet.reset(
439 new NetworkPacket(&data[0], kPacketSize, kTimeNow, kArrivalTime));
440 EXPECT_CALL(mock_receiver, DeliverPacket(MediaType::AUDIO, _, _, _))
441 .WillOnce(Return(PacketReceiver::DELIVERY_OK));
442 demuxer.DeliverPacket(packet.get(), PacketTime());
443}
444
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22 +0000445} // namespace webrtc