blob: a2cf80f27c129d7382db56ff8949a23f73ab2e1f [file] [log] [blame]
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:221/*
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 21:53:4611#include <memory>
12
Mirko Bonadei92ea95e2017-09-15 04:47:3113#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:2219
20using ::testing::_;
21using ::testing::AnyNumber;
22using ::testing::Return;
23using ::testing::Invoke;
24
25namespace webrtc {
26
minyue20c84cc2017-04-10 23:57:5727class TestDemuxer : public Demuxer {
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2228 public:
minyue20c84cc2017-04-10 23:57:5729 void IncomingPacket(NetworkPacket* packet) {
30 DeliverPacket(packet, PacketTime());
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2231 }
32
minyue20c84cc2017-04-10 23:57:5733 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:2237};
38
minyue20c84cc2017-04-10 23:57:5739class ReorderTestDemuxer : public TestDemuxer {
philipela2c55232016-01-26 16:41:5340 public:
minyue20c84cc2017-04-10 23:57:5741 void DeliverPacket(const NetworkPacket* packet,
42 const PacketTime& packet_time) override {
43 RTC_DCHECK_GE(packet->data_length(), sizeof(int));
philipela2c55232016-01-26 16:41:5344 int seq_num;
minyue20c84cc2017-04-10 23:57:5745 memcpy(&seq_num, packet->data(), sizeof(int));
philipela2c55232016-01-26 16:41:5346 delivered_sequence_numbers_.push_back(seq_num);
philipela2c55232016-01-26 16:41:5347 }
48 std::vector<int> delivered_sequence_numbers_;
49};
50
minyue20c84cc2017-04-10 23:57:5751class MockReceiver : public PacketReceiver {
52 public:
Danil Chapovalov292a73e2017-12-07 16:00:4053 MOCK_METHOD3(DeliverPacket,
54 DeliveryStatus(MediaType,
55 rtc::CopyOnWriteBuffer,
56 const PacketTime&));
minyue20c84cc2017-04-10 23:57:5757};
58
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2259class FakeNetworkPipeTest : public ::testing::Test {
Peter Boströmd3c94472015-12-09 10:20:5860 public:
61 FakeNetworkPipeTest() : fake_clock_(12345) {}
62
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2263 protected:
philipela2c55232016-01-26 16:41:5364 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) {
kwiberg352444f2016-11-28 23:58:5365 RTC_DCHECK_GE(packet_size, sizeof(int));
kwibergbfefb032016-05-01 21:53:4666 std::unique_ptr<uint8_t[]> packet(new uint8_t[packet_size]);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2267 for (int i = 0; i < number_packets; ++i) {
philipela2c55232016-01-26 16:41:5368 // Set a sequence number for the packets by
69 // using the first bytes in the packet.
70 memcpy(packet.get(), &i, sizeof(int));
71 pipe->SendPacket(packet.get(), packet_size);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2272 }
73 }
74
philipela2c55232016-01-26 16:41:5375 int PacketTimeMs(int capacity_kbps, int packet_size) const {
76 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2277 }
78
Peter Boströmd3c94472015-12-09 10:20:5879 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2280};
81
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2282// Test the capacity link and verify we get as many packets as we expect.
83TEST_F(FakeNetworkPipeTest, CapacityTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:2584 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:0685 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:1186 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 23:57:5787 TestDemuxer* demuxer = new TestDemuxer();
88 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
89 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2290
91 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
92 // get through the pipe.
93 const int kNumPackets = 10;
94 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:5795 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2296
97 // Time to get one packet through the link.
mflodman@webrtc.org7acb65a2012-12-13 15:53:1198 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
99 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22100
101 // Time haven't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 23:57:57102 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25103 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22104
105 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 10:20:58106 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
minyue20c84cc2017-04-10 23:57:57107 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25108 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22109
110 // Release all but one packet
Peter Boströmd3c94472015-12-09 10:20:58111 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
minyue20c84cc2017-04-10 23:57:57112 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25113 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22114
115 // And the last one.
Peter Boströmd3c94472015-12-09 10:20:58116 fake_clock_.AdvanceTimeMilliseconds(1);
minyue20c84cc2017-04-10 23:57:57117 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25118 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22119}
120
121// Test the extra network delay.
122TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25123 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06124 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11125 config.queue_delay_ms = 100;
126 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 23:57:57127 TestDemuxer* demuxer = new TestDemuxer();
128 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
129 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22130
131 const int kNumPackets = 2;
132 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:57133 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22134
135 // Time to get one packet through the link.
mflodman@webrtc.org7acb65a2012-12-13 15:53:11136 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
137 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22138
139 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 10:20:58140 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
minyue20c84cc2017-04-10 23:57:57141 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25142 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22143
144 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 10:20:58145 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
minyue20c84cc2017-04-10 23:57:57146 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25147 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22148
149 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 10:20:58150 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
minyue20c84cc2017-04-10 23:57:57151 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25152 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22153}
154
155// Test the number of buffers and packets are dropped when sending too many
156// packets too quickly.
157TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25158 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06159 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11160 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 23:57:57161 TestDemuxer* demuxer = new TestDemuxer();
162 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
163 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22164
165 const int kPacketSize = 1000;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11166 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
167 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22168
169 // Send three packets and verify only 2 are delivered.
170 SendPackets(pipe.get(), 3, kPacketSize);
171
172 // Increase time enough to deliver all three packets, verify only two are
173 // delivered.
Peter Boströmd3c94472015-12-09 10:20:58174 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
minyue20c84cc2017-04-10 23:57:57175 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25176 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22177}
178
179// Test we get statistics as expected.
180TEST_F(FakeNetworkPipeTest, StatisticsTest) {
stefan@webrtc.orgfaada6e2013-12-18 20:28:25181 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06182 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11183 config.queue_delay_ms = 20;
184 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 23:57:57185 TestDemuxer* demuxer = new TestDemuxer();
186 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
187 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22188
189 const int kPacketSize = 1000;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11190 const int kPacketTimeMs = PacketTimeMs(config.link_capacity_kbps,
191 kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22192
193 // Send three packets and verify only 2 are delivered.
194 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 10:20:58195 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
196 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22197
minyue20c84cc2017-04-10 23:57:57198 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25199 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22200
mflodman@webrtc.org7acb65a2012-12-13 15:53:11201 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
202 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22203 EXPECT_EQ(pipe->AverageDelay(), 170);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25204 EXPECT_EQ(pipe->sent_packets(), 2u);
205 EXPECT_EQ(pipe->dropped_packets(), 1u);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22206 EXPECT_EQ(pipe->PercentageLoss(), 1/3.f);
207}
208
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52209// Change the link capacity half-way through the test and verify that the
210// delivery times change accordingly.
211TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
212 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06213 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52214 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 23:57:57215 TestDemuxer* demuxer = new TestDemuxer();
216 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
217 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52218
219 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
220 // get through the pipe.
221 const int kNumPackets = 10;
222 const int kPacketSize = 1000;
223 SendPackets(pipe.get(), kNumPackets, kPacketSize);
224
225 // Time to get one packet through the link.
226 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
227
228 // Time hasn't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 23:57:57229 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52230 pipe->Process();
231
232 // Advance time in steps to release one packet at a time.
233 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58234 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
minyue20c84cc2017-04-10 23:57:57235 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52236 pipe->Process();
237 }
238
239 // Change the capacity.
240 config.link_capacity_kbps /= 2; // Reduce to 50%.
241 pipe->SetConfig(config);
242
243 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
244 // seconds to get them through the pipe.
245 SendPackets(pipe.get(), kNumPackets, kPacketSize);
246
247 // Time to get one packet through the link.
248 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
249
250 // Time hasn't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 23:57:57251 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52252 pipe->Process();
253
254 // Advance time in steps to release one packet at a time.
255 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58256 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
minyue20c84cc2017-04-10 23:57:57257 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52258 pipe->Process();
259 }
260
261 // Check that all the packets were sent.
262 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets());
Peter Boströmd3c94472015-12-09 10:20:58263 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
minyue20c84cc2017-04-10 23:57:57264 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52265 pipe->Process();
266}
267
268// Change the link capacity half-way through the test and verify that the
269// delivery times change accordingly.
270TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
271 FakeNetworkPipe::Config config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06272 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52273 config.link_capacity_kbps = 80;
minyue20c84cc2017-04-10 23:57:57274 TestDemuxer* demuxer = new TestDemuxer();
275 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
276 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52277
278 // Add 10 packets of 1000 bytes, = 80 kb.
279 const int kNumPackets = 10;
280 const int kPacketSize = 1000;
281 SendPackets(pipe.get(), kNumPackets, kPacketSize);
282
283 // Time to get one packet through the link at the initial speed.
284 int packet_time_1_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
285
286 // Change the capacity.
287 config.link_capacity_kbps *= 2; // Double the capacity.
288 pipe->SetConfig(config);
289
290 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
291 // seconds to get them through the pipe.
292 SendPackets(pipe.get(), kNumPackets, kPacketSize);
293
294 // Time to get one packet through the link at the new capacity.
295 int packet_time_2_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
296
297 // Time hasn't increased yet, so we souldn't get any packets.
minyue20c84cc2017-04-10 23:57:57298 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52299 pipe->Process();
300
301 // Advance time in steps to release one packet at a time.
302 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58303 fake_clock_.AdvanceTimeMilliseconds(packet_time_1_ms);
minyue20c84cc2017-04-10 23:57:57304 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52305 pipe->Process();
306 }
307
308 // Advance time in steps to release one packet at a time.
309 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58310 fake_clock_.AdvanceTimeMilliseconds(packet_time_2_ms);
minyue20c84cc2017-04-10 23:57:57311 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52312 pipe->Process();
313 }
314
315 // Check that all the packets were sent.
316 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->sent_packets());
Peter Boströmd3c94472015-12-09 10:20:58317 fake_clock_.AdvanceTimeMilliseconds(pipe->TimeUntilNextProcess());
minyue20c84cc2017-04-10 23:57:57318 EXPECT_CALL(*demuxer, DeliverPacket(_, _)).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52319 pipe->Process();
320}
philipela2c55232016-01-26 16:41:53321
322// At first disallow reordering and then allow reordering.
323TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
324 FakeNetworkPipe::Config config;
325 config.queue_length_packets = 1000;
326 config.link_capacity_kbps = 800;
327 config.queue_delay_ms = 100;
328 config.delay_standard_deviation_ms = 10;
minyue20c84cc2017-04-10 23:57:57329 ReorderTestDemuxer* demuxer = new ReorderTestDemuxer();
330 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
331 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
philipela2c55232016-01-26 16:41:53332
333 const uint32_t kNumPackets = 100;
334 const int kPacketSize = 10;
335 SendPackets(pipe.get(), kNumPackets, kPacketSize);
336 fake_clock_.AdvanceTimeMilliseconds(1000);
337 pipe->Process();
338
339 // Confirm that all packets have been delivered in order.
minyue20c84cc2017-04-10 23:57:57340 EXPECT_EQ(kNumPackets, demuxer->delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53341 int last_seq_num = -1;
minyue20c84cc2017-04-10 23:57:57342 for (int seq_num : demuxer->delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53343 EXPECT_GT(seq_num, last_seq_num);
344 last_seq_num = seq_num;
345 }
346
347 config.allow_reordering = true;
348 pipe->SetConfig(config);
349 SendPackets(pipe.get(), kNumPackets, kPacketSize);
350 fake_clock_.AdvanceTimeMilliseconds(1000);
minyue20c84cc2017-04-10 23:57:57351 demuxer->delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 16:41:53352 pipe->Process();
353
354 // Confirm that all packets have been delivered
355 // and that reordering has occured.
minyue20c84cc2017-04-10 23:57:57356 EXPECT_EQ(kNumPackets, demuxer->delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53357 bool reordering_has_occured = false;
358 last_seq_num = -1;
minyue20c84cc2017-04-10 23:57:57359 for (int seq_num : demuxer->delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53360 if (last_seq_num > seq_num) {
361 reordering_has_occured = true;
362 break;
363 }
364 last_seq_num = seq_num;
365 }
366 EXPECT_TRUE(reordering_has_occured);
367}
philipel536378b2016-05-31 10:20:23368
369TEST_F(FakeNetworkPipeTest, BurstLoss) {
370 const int kLossPercent = 5;
371 const int kAvgBurstLength = 3;
372 const int kNumPackets = 10000;
373 const int kPacketSize = 10;
374
375 FakeNetworkPipe::Config config;
376 config.queue_length_packets = kNumPackets;
377 config.loss_percent = kLossPercent;
378 config.avg_burst_loss_length = kAvgBurstLength;
minyue20c84cc2017-04-10 23:57:57379 ReorderTestDemuxer* demuxer = new ReorderTestDemuxer();
380 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
381 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
philipel536378b2016-05-31 10:20:23382
383 SendPackets(pipe.get(), kNumPackets, kPacketSize);
384 fake_clock_.AdvanceTimeMilliseconds(1000);
385 pipe->Process();
386
387 // Check that the average loss is |kLossPercent| percent.
minyue20c84cc2017-04-10 23:57:57388 int lost_packets = kNumPackets - demuxer->delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23389 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
390
391 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
392
393 // Find the number of bursts that has occurred.
minyue20c84cc2017-04-10 23:57:57394 size_t received_packets = demuxer->delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23395 int num_bursts = 0;
396 for (size_t i = 0; i < received_packets - 1; ++i) {
minyue20c84cc2017-04-10 23:57:57397 int diff = demuxer->delivered_sequence_numbers_[i + 1] -
398 demuxer->delivered_sequence_numbers_[i];
philipel536378b2016-05-31 10:20:23399 if (diff > 1)
400 ++num_bursts;
401 }
402
403 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
404
405 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
406}
minyue20c84cc2017-04-10 23:57:57407
408TEST_F(FakeNetworkPipeTest, SetReceiver) {
409 FakeNetworkPipe::Config config;
410 TestDemuxer* demuxer = new TestDemuxer();
411 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
412 &fake_clock_, config, std::unique_ptr<Demuxer>(demuxer)));
413 MockReceiver packet_receiver;
414 EXPECT_CALL(*demuxer, SetReceiver(&packet_receiver)).Times(1);
415 pipe->SetReceiver(&packet_receiver);
416}
417
418TEST(DemuxerImplTest, Demuxing) {
419 constexpr uint8_t kVideoPayloadType = 100;
420 constexpr uint8_t kAudioPayloadType = 101;
421 constexpr int64_t kTimeNow = 12345;
422 constexpr int64_t kArrivalTime = kTimeNow - 1;
423 constexpr size_t kPacketSize = 10;
424 DemuxerImpl demuxer({{kVideoPayloadType, MediaType::VIDEO},
425 {kAudioPayloadType, MediaType::AUDIO}});
426
427 MockReceiver mock_receiver;
428 demuxer.SetReceiver(&mock_receiver);
429
430 std::vector<uint8_t> data(kPacketSize);
431 data[1] = kVideoPayloadType;
432 std::unique_ptr<NetworkPacket> packet(
433 new NetworkPacket(&data[0], kPacketSize, kTimeNow, kArrivalTime));
Danil Chapovalov292a73e2017-12-07 16:00:40434 EXPECT_CALL(mock_receiver, DeliverPacket(MediaType::VIDEO, _, _))
minyue20c84cc2017-04-10 23:57:57435 .WillOnce(Return(PacketReceiver::DELIVERY_OK));
436 demuxer.DeliverPacket(packet.get(), PacketTime());
437
438 data[1] = kAudioPayloadType;
439 packet.reset(
440 new NetworkPacket(&data[0], kPacketSize, kTimeNow, kArrivalTime));
Danil Chapovalov292a73e2017-12-07 16:00:40441 EXPECT_CALL(mock_receiver, DeliverPacket(MediaType::AUDIO, _, _))
minyue20c84cc2017-04-10 23:57:57442 .WillOnce(Return(PacketReceiver::DELIVERY_OK));
443 demuxer.DeliverPacket(packet.get(), PacketTime());
444}
445
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22446} // namespace webrtc