blob: 31f97fc85c736b82f0afaedf7531ea13b83abbef [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
Erik Språng09708512018-03-14 14:16:5011#include "call/fake_network_pipe.h"
12
kwibergbfefb032016-05-01 21:53:4613#include <memory>
Sebastian Jansson487c09b2019-02-21 15:21:5114#include <utility>
kwibergbfefb032016-05-01 21:53:4615
Per K4abca662023-01-19 14:11:0716#include "api/units/time_delta.h"
17#include "api/units/timestamp.h"
Artem Titove23b8a92018-08-16 13:51:0718#include "call/simulated_network.h"
Per Kbc319022023-01-09 20:44:5619#include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
20#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
21#include "modules/rtp_rtcp/source/rtp_packet_received.h"
22#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3124#include "test/gmock.h"
25#include "test/gtest.h"
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2226
27using ::testing::_;
Per Kbc319022023-01-09 20:44:5628using ::testing::Property;
29using ::testing::WithArg;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2230
31namespace webrtc {
minyue20c84cc2017-04-10 23:57:5732class MockReceiver : public PacketReceiver {
33 public:
Per Kbc319022023-01-09 20:44:5634 MOCK_METHOD(void,
35 DeliverRtcpPacket,
36 (rtc::CopyOnWriteBuffer packet),
37 (override));
38 MOCK_METHOD(void,
39 DeliverRtpPacket,
40 (MediaType media_type,
41 RtpPacketReceived packet,
42 OnUndemuxablePacketHandler undemuxable_packet_handler),
43 (override));
Sebastian Jansson09408112018-04-24 12:41:2244 virtual ~MockReceiver() = default;
45};
46
47class ReorderTestReceiver : public MockReceiver {
48 public:
Per Kbc319022023-01-09 20:44:5649 void DeliverRtpPacket(
50 MediaType media_type,
51 RtpPacketReceived packet,
52 OnUndemuxablePacketHandler undemuxable_packet_handler) override {
Sebastian Jansson09408112018-04-24 12:41:2253 RTC_DCHECK_GE(packet.size(), sizeof(int));
Per Kbc319022023-01-09 20:44:5654 delivered_sequence_numbers_.push_back(packet.SequenceNumber());
Sebastian Jansson09408112018-04-24 12:41:2255 }
56 std::vector<int> delivered_sequence_numbers_;
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));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2266 for (int i = 0; i < number_packets; ++i) {
Per Kbc319022023-01-09 20:44:5667 RtpPacketReceived packet;
68 constexpr size_t kFixedHeaderSize = 12;
69 packet.AllocatePayload(packet_size - kFixedHeaderSize);
70 packet.SetSequenceNumber(i);
71 packet.set_arrival_time(fake_clock_.CurrentTime());
72 RTC_DCHECK_EQ(packet.Buffer().size(), packet_size);
73 pipe->DeliverRtpPacket(MediaType::ANY, std::move(packet),
74 [](const RtpPacketReceived&) { return false; });
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2275 }
76 }
77
philipela2c55232016-01-26 16:41:5378 int PacketTimeMs(int capacity_kbps, int packet_size) const {
79 return 8 * packet_size / capacity_kbps;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2280 }
81
Peter Boströmd3c94472015-12-09 10:20:5882 SimulatedClock fake_clock_;
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2283};
84
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2285// Test the capacity link and verify we get as many packets as we expect.
86TEST_F(FakeNetworkPipeTest, CapacityTest) {
Artem Titov75e36472018-10-08 10:28:5687 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:0688 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:1189 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:2290 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:1891 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:5492 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
93 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:2294
95 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
96 // get through the pipe.
97 const int kNumPackets = 10;
98 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:5799 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22100
101 // Time to get one packet through the link.
Erik Språng09708512018-03-14 14:16:50102 const int kPacketTimeMs =
103 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22104
105 // Time haven't increased yet, so we souldn't get any packets.
Per Kbc319022023-01-09 20:44:56106 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25107 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22108
109 // Advance enough time to release one packet.
Peter Boströmd3c94472015-12-09 10:20:58110 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Per Kbc319022023-01-09 20:44:56111 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25112 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22113
114 // Release all but one packet
Peter Boströmd3c94472015-12-09 10:20:58115 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1);
Per Kbc319022023-01-09 20:44:56116 EXPECT_CALL(receiver, DeliverRtpPacket).Times(8);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25117 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22118
119 // And the last one.
Peter Boströmd3c94472015-12-09 10:20:58120 fake_clock_.AdvanceTimeMilliseconds(1);
Per Kbc319022023-01-09 20:44:56121 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25122 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22123}
124
125// Test the extra network delay.
126TEST_F(FakeNetworkPipeTest, ExtraDelayTest) {
Artem Titov75e36472018-10-08 10:28:56127 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06128 config.queue_length_packets = 20;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11129 config.queue_delay_ms = 100;
130 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22131 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18132 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54133 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
134 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22135
136 const int kNumPackets = 2;
137 const int kPacketSize = 1000;
minyue20c84cc2017-04-10 23:57:57138 SendPackets(pipe.get(), kNumPackets, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22139
140 // Time to get one packet through the link.
Erik Språng09708512018-03-14 14:16:50141 const int kPacketTimeMs =
142 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22143
144 // Increase more than kPacketTimeMs, but not more than the extra delay.
Peter Boströmd3c94472015-12-09 10:20:58145 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Per Kbc319022023-01-09 20:44:56146 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25147 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22148
149 // Advance the network delay to get the first packet.
Peter Boströmd3c94472015-12-09 10:20:58150 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
Per Kbc319022023-01-09 20:44:56151 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25152 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22153
154 // Advance one more kPacketTimeMs to get the last packet.
Peter Boströmd3c94472015-12-09 10:20:58155 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Per Kbc319022023-01-09 20:44:56156 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25157 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22158}
159
160// Test the number of buffers and packets are dropped when sending too many
161// packets too quickly.
162TEST_F(FakeNetworkPipeTest, QueueLengthTest) {
Artem Titov75e36472018-10-08 10:28:56163 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06164 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11165 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22166 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18167 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54168 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
169 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22170
171 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 14:16:50172 const int kPacketTimeMs =
173 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22174
175 // Send three packets and verify only 2 are delivered.
176 SendPackets(pipe.get(), 3, kPacketSize);
177
178 // Increase time enough to deliver all three packets, verify only two are
179 // delivered.
Peter Boströmd3c94472015-12-09 10:20:58180 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs);
Per Kbc319022023-01-09 20:44:56181 EXPECT_CALL(receiver, DeliverRtpPacket).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25182 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22183}
184
185// Test we get statistics as expected.
186TEST_F(FakeNetworkPipeTest, StatisticsTest) {
Artem Titov75e36472018-10-08 10:28:56187 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06188 config.queue_length_packets = 2;
mflodman@webrtc.org7acb65a2012-12-13 15:53:11189 config.queue_delay_ms = 20;
190 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22191 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18192 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54193 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
194 &fake_clock_, std::move(simulated_network), &receiver));
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22195
196 const int kPacketSize = 1000;
Erik Språng09708512018-03-14 14:16:50197 const int kPacketTimeMs =
198 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22199
200 // Send three packets and verify only 2 are delivered.
201 SendPackets(pipe.get(), 3, kPacketSize);
Peter Boströmd3c94472015-12-09 10:20:58202 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs +
203 config.queue_delay_ms);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22204
Per Kbc319022023-01-09 20:44:56205 EXPECT_CALL(receiver, DeliverRtpPacket).Times(2);
stefan@webrtc.orgfaada6e2013-12-18 20:28:25206 pipe->Process();
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22207
mflodman@webrtc.org7acb65a2012-12-13 15:53:11208 // Packet 1: kPacketTimeMs + config.queue_delay_ms,
209 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average.
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22210 EXPECT_EQ(pipe->AverageDelay(), 170);
Erik Språng09708512018-03-14 14:16:50211 EXPECT_EQ(pipe->SentPackets(), 2u);
212 EXPECT_EQ(pipe->DroppedPackets(), 1u);
213 EXPECT_EQ(pipe->PercentageLoss(), 1 / 3.f);
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22214}
215
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52216// Change the link capacity half-way through the test and verify that the
217// delivery times change accordingly.
218TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) {
Artem Titov75e36472018-10-08 10:28:56219 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06220 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52221 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22222 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07223 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
224 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22225 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20226 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52227
228 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to
229 // get through the pipe.
230 const int kNumPackets = 10;
231 const int kPacketSize = 1000;
232 SendPackets(pipe.get(), kNumPackets, kPacketSize);
233
234 // Time to get one packet through the link.
235 int packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
236
237 // Time hasn't increased yet, so we souldn't get any packets.
Per Kbc319022023-01-09 20:44:56238 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52239 pipe->Process();
240
241 // Advance time in steps to release one packet at a time.
242 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58243 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Per Kbc319022023-01-09 20:44:56244 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52245 pipe->Process();
246 }
247
248 // Change the capacity.
249 config.link_capacity_kbps /= 2; // Reduce to 50%.
Artem Titove23b8a92018-08-16 13:51:07250 simulated_network->SetConfig(config);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52251
252 // Add another 10 packets of 1000 bytes, = 80 kb, and verify it takes two
253 // seconds to get them through the pipe.
254 SendPackets(pipe.get(), kNumPackets, kPacketSize);
255
256 // Time to get one packet through the link.
257 packet_time_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
258
259 // Time hasn't increased yet, so we souldn't get any packets.
Per Kbc319022023-01-09 20:44:56260 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52261 pipe->Process();
262
263 // Advance time in steps to release one packet at a time.
264 for (int i = 0; i < kNumPackets; ++i) {
Peter Boströmd3c94472015-12-09 10:20:58265 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms);
Per Kbc319022023-01-09 20:44:56266 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52267 pipe->Process();
268 }
269
270 // Check that all the packets were sent.
Erik Språng09708512018-03-14 14:16:50271 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 15:21:51272 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
273 fake_clock_.AdvanceTimeMilliseconds(1000);
Per Kbc319022023-01-09 20:44:56274 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52275 pipe->Process();
276}
277
278// Change the link capacity half-way through the test and verify that the
279// delivery times change accordingly.
280TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) {
Artem Titov75e36472018-10-08 10:28:56281 BuiltInNetworkBehaviorConfig config;
stefan@webrtc.orgb8e9e442014-07-09 11:29:06282 config.queue_length_packets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52283 config.link_capacity_kbps = 80;
Sebastian Jansson09408112018-04-24 12:41:22284 MockReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07285 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
286 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22287 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20288 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52289
Mirko Bonadei248fdb12022-10-13 13:06:08290 // Add 20 packets of 1000 bytes, = 160 kb.
Christoffer Rodbro813c79b2019-01-31 08:25:12291 const int kNumPackets = 20;
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52292 const int kPacketSize = 1000;
293 SendPackets(pipe.get(), kNumPackets, kPacketSize);
294
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52295 // Time hasn't increased yet, so we souldn't get any packets.
Per Kbc319022023-01-09 20:44:56296 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52297 pipe->Process();
298
Christoffer Rodbro813c79b2019-01-31 08:25:12299 // Advance time in steps to release half of the packets one at a time.
300 int step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
301 for (int i = 0; i < kNumPackets / 2; ++i) {
302 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Per Kbc319022023-01-09 20:44:56303 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52304 pipe->Process();
305 }
306
Christoffer Rodbro813c79b2019-01-31 08:25:12307 // Change the capacity.
308 config.link_capacity_kbps *= 2; // Double the capacity.
309 simulated_network->SetConfig(config);
310
311 // Advance time in steps to release remaining packets one at a time.
312 step_ms = PacketTimeMs(config.link_capacity_kbps, kPacketSize);
313 for (int i = 0; i < kNumPackets / 2; ++i) {
314 fake_clock_.AdvanceTimeMilliseconds(step_ms);
Per Kbc319022023-01-09 20:44:56315 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52316 pipe->Process();
317 }
318
319 // Check that all the packets were sent.
Christoffer Rodbro813c79b2019-01-31 08:25:12320 EXPECT_EQ(static_cast<size_t>(kNumPackets), pipe->SentPackets());
Sebastian Jansson487c09b2019-02-21 15:21:51321 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value());
322 fake_clock_.AdvanceTimeMilliseconds(1000);
Per Kbc319022023-01-09 20:44:56323 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
henrik.lundin@webrtc.orgc0e9aeb2014-02-26 13:34:52324 pipe->Process();
325}
philipela2c55232016-01-26 16:41:53326
327// At first disallow reordering and then allow reordering.
328TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) {
Artem Titov75e36472018-10-08 10:28:56329 BuiltInNetworkBehaviorConfig config;
philipela2c55232016-01-26 16:41:53330 config.queue_length_packets = 1000;
331 config.link_capacity_kbps = 800;
332 config.queue_delay_ms = 100;
333 config.delay_standard_deviation_ms = 10;
Sebastian Jansson09408112018-04-24 12:41:22334 ReorderTestReceiver receiver;
Artem Titove23b8a92018-08-16 13:51:07335 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config));
336 SimulatedNetwork* simulated_network = network.get();
Sebastian Jansson09408112018-04-24 12:41:22337 std::unique_ptr<FakeNetworkPipe> pipe(
Artem Titovb0050872018-08-16 15:02:20338 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver));
philipela2c55232016-01-26 16:41:53339
340 const uint32_t kNumPackets = 100;
341 const int kPacketSize = 10;
342 SendPackets(pipe.get(), kNumPackets, kPacketSize);
343 fake_clock_.AdvanceTimeMilliseconds(1000);
344 pipe->Process();
345
346 // Confirm that all packets have been delivered in order.
Sebastian Jansson09408112018-04-24 12:41:22347 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53348 int last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22349 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53350 EXPECT_GT(seq_num, last_seq_num);
351 last_seq_num = seq_num;
352 }
353
354 config.allow_reordering = true;
Artem Titove23b8a92018-08-16 13:51:07355 simulated_network->SetConfig(config);
philipela2c55232016-01-26 16:41:53356 SendPackets(pipe.get(), kNumPackets, kPacketSize);
357 fake_clock_.AdvanceTimeMilliseconds(1000);
Sebastian Jansson09408112018-04-24 12:41:22358 receiver.delivered_sequence_numbers_.clear();
philipela2c55232016-01-26 16:41:53359 pipe->Process();
360
361 // Confirm that all packets have been delivered
362 // and that reordering has occured.
Sebastian Jansson09408112018-04-24 12:41:22363 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size());
philipela2c55232016-01-26 16:41:53364 bool reordering_has_occured = false;
365 last_seq_num = -1;
Sebastian Jansson09408112018-04-24 12:41:22366 for (int seq_num : receiver.delivered_sequence_numbers_) {
philipela2c55232016-01-26 16:41:53367 if (last_seq_num > seq_num) {
368 reordering_has_occured = true;
369 break;
370 }
371 last_seq_num = seq_num;
372 }
373 EXPECT_TRUE(reordering_has_occured);
374}
philipel536378b2016-05-31 10:20:23375
376TEST_F(FakeNetworkPipeTest, BurstLoss) {
377 const int kLossPercent = 5;
378 const int kAvgBurstLength = 3;
379 const int kNumPackets = 10000;
380 const int kPacketSize = 10;
381
Artem Titov75e36472018-10-08 10:28:56382 BuiltInNetworkBehaviorConfig config;
philipel536378b2016-05-31 10:20:23383 config.queue_length_packets = kNumPackets;
384 config.loss_percent = kLossPercent;
385 config.avg_burst_loss_length = kAvgBurstLength;
Sebastian Jansson09408112018-04-24 12:41:22386 ReorderTestReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18387 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titovc7ea8522018-08-29 07:07:27388 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
389 &fake_clock_, std::move(simulated_network), &receiver));
philipel536378b2016-05-31 10:20:23390
391 SendPackets(pipe.get(), kNumPackets, kPacketSize);
392 fake_clock_.AdvanceTimeMilliseconds(1000);
393 pipe->Process();
394
Artem Titovea240272021-07-26 10:40:21395 // Check that the average loss is `kLossPercent` percent.
Sebastian Jansson09408112018-04-24 12:41:22396 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23397 double loss_fraction = lost_packets / static_cast<double>(kNumPackets);
398
399 EXPECT_NEAR(kLossPercent / 100.0, loss_fraction, 0.05);
400
401 // Find the number of bursts that has occurred.
Sebastian Jansson09408112018-04-24 12:41:22402 size_t received_packets = receiver.delivered_sequence_numbers_.size();
philipel536378b2016-05-31 10:20:23403 int num_bursts = 0;
404 for (size_t i = 0; i < received_packets - 1; ++i) {
Sebastian Jansson09408112018-04-24 12:41:22405 int diff = receiver.delivered_sequence_numbers_[i + 1] -
406 receiver.delivered_sequence_numbers_[i];
philipel536378b2016-05-31 10:20:23407 if (diff > 1)
408 ++num_bursts;
409 }
410
411 double average_burst_length = static_cast<double>(lost_packets) / num_bursts;
412
413 EXPECT_NEAR(kAvgBurstLength, average_burst_length, 0.3);
414}
minyue20c84cc2017-04-10 23:57:57415
416TEST_F(FakeNetworkPipeTest, SetReceiver) {
Artem Titov75e36472018-10-08 10:28:56417 BuiltInNetworkBehaviorConfig config;
Sebastian Jansson09408112018-04-24 12:41:22418 config.link_capacity_kbps = 800;
419 MockReceiver receiver;
Mirko Bonadei317a1f02019-09-17 15:06:18420 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
Artem Titov3229d652018-08-17 11:00:54421 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
422 &fake_clock_, std::move(simulated_network), &receiver));
minyue20c84cc2017-04-10 23:57:57423
Sebastian Jansson09408112018-04-24 12:41:22424 const int kPacketSize = 1000;
425 const int kPacketTimeMs =
426 PacketTimeMs(config.link_capacity_kbps, kPacketSize);
427 SendPackets(pipe.get(), 1, kPacketSize);
428 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Per Kbc319022023-01-09 20:44:56429 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1);
Sebastian Jansson09408112018-04-24 12:41:22430 pipe->Process();
minyue20c84cc2017-04-10 23:57:57431
Sebastian Jansson09408112018-04-24 12:41:22432 MockReceiver new_receiver;
433 pipe->SetReceiver(&new_receiver);
minyue20c84cc2017-04-10 23:57:57434
Sebastian Jansson09408112018-04-24 12:41:22435 SendPackets(pipe.get(), 1, kPacketSize);
436 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs);
Per Kbc319022023-01-09 20:44:56437 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0);
438 EXPECT_CALL(new_receiver, DeliverRtpPacket).Times(1);
439 pipe->Process();
440}
441
Per K4abca662023-01-19 14:11:07442TEST_F(FakeNetworkPipeTest, DeliverRtpPacketSetsCorrectArrivalTime) {
443 BuiltInNetworkBehaviorConfig config;
444 config.queue_delay_ms = 100;
445 MockReceiver receiver;
446 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
447 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
448 &fake_clock_, std::move(simulated_network), &receiver));
449
450 Timestamp send_time = fake_clock_.CurrentTime();
451 RtpPacketReceived packet(nullptr, send_time);
452 packet.SetExtension<TransportSequenceNumber>(123);
453 pipe->DeliverRtpPacket(MediaType::VIDEO, std::move(packet),
454 [](const RtpPacketReceived&) { return false; });
455
456 // Advance the network delay to get the first packet.
457 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
458 EXPECT_CALL(receiver, DeliverRtpPacket(MediaType::VIDEO, _, _))
459 .WillOnce(WithArg<1>([&](RtpPacketReceived packet) {
460 EXPECT_EQ(packet.arrival_time(),
461 send_time + TimeDelta::Millis(config.queue_delay_ms));
462 }));
463 pipe->Process();
464}
465
Per Kbc319022023-01-09 20:44:56466TEST_F(FakeNetworkPipeTest, DeliverRtpPacketPropagatesExtensions) {
467 BuiltInNetworkBehaviorConfig config;
468 config.queue_delay_ms = 100;
469 MockReceiver receiver;
470 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
471 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
472 &fake_clock_, std::move(simulated_network), &receiver));
473 RtpHeaderExtensionMap extension_map;
474 extension_map.Register<TransportSequenceNumber>(/*id=*/7);
475
476 RtpPacketReceived packet(&extension_map, fake_clock_.CurrentTime());
477 packet.SetExtension<TransportSequenceNumber>(123);
478 pipe->DeliverRtpPacket(MediaType::VIDEO, std::move(packet),
479 [](const RtpPacketReceived&) { return false; });
480
481 // Advance the network delay to get the first packet.
482 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
483 EXPECT_CALL(receiver, DeliverRtpPacket(MediaType::VIDEO, _, _))
484 .WillOnce(WithArg<1>([](RtpPacketReceived packet) {
485 EXPECT_EQ(packet.GetExtension<TransportSequenceNumber>(), 123);
486 }));
487 pipe->Process();
488}
489
490TEST_F(FakeNetworkPipeTest, DeliverRtcpPacket) {
491 BuiltInNetworkBehaviorConfig config;
492 config.queue_delay_ms = 100;
493 MockReceiver receiver;
494 auto simulated_network = std::make_unique<SimulatedNetwork>(config);
495 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe(
496 &fake_clock_, std::move(simulated_network), &receiver));
497
498 rtc::CopyOnWriteBuffer buffer(100);
499 memset(buffer.MutableData(), 0, 100);
500 pipe->DeliverRtcpPacket(std::move(buffer));
501
502 // Advance the network delay to get the first packet.
503 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms);
504 EXPECT_CALL(receiver,
505 DeliverRtcpPacket(Property(&rtc::CopyOnWriteBuffer::size, 100)));
Sebastian Jansson09408112018-04-24 12:41:22506 pipe->Process();
minyue20c84cc2017-04-10 23:57:57507}
508
mflodman@webrtc.orgeaf7cf22012-12-11 11:47:22509} // namespace webrtc