fake_network_pipe_unittest.cc (18959B)
1 /* 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 11 #include "call/fake_network_pipe.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 #include <cstring> 16 #include <memory> 17 #include <utility> 18 #include <vector> 19 20 #include "api/test/simulated_network.h" 21 #include "api/units/data_rate.h" 22 #include "api/units/time_delta.h" 23 #include "api/units/timestamp.h" 24 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h" 25 #include "modules/rtp_rtcp/source/rtp_header_extensions.h" 26 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 27 #include "rtc_base/checks.h" 28 #include "rtc_base/copy_on_write_buffer.h" 29 #include "system_wrappers/include/clock.h" 30 #include "test/gmock.h" 31 #include "test/gtest.h" 32 #include "test/network/simulated_network.h" 33 34 using ::testing::_; 35 using ::testing::Property; 36 using ::testing::WithArg; 37 38 namespace webrtc { 39 class MockReceiver : public PacketReceiver { 40 public: 41 MOCK_METHOD(void, DeliverRtcpPacket, (CopyOnWriteBuffer packet), (override)); 42 MOCK_METHOD(void, 43 DeliverRtpPacket, 44 (MediaType media_type, 45 RtpPacketReceived packet, 46 OnUndemuxablePacketHandler undemuxable_packet_handler), 47 (override)); 48 ~MockReceiver() override = default; 49 }; 50 51 class ReorderTestReceiver : public MockReceiver { 52 public: 53 void DeliverRtpPacket( 54 MediaType /* media_type */, 55 RtpPacketReceived packet, 56 OnUndemuxablePacketHandler /* undemuxable_packet_handler */) override { 57 RTC_DCHECK_GE(packet.size(), sizeof(int)); 58 delivered_sequence_numbers_.push_back(packet.SequenceNumber()); 59 } 60 std::vector<int> delivered_sequence_numbers_; 61 }; 62 63 class FakeNetworkPipeTest : public ::testing::Test { 64 public: 65 FakeNetworkPipeTest() : fake_clock_(12345) {} 66 67 protected: 68 void SendPackets(FakeNetworkPipe* pipe, int number_packets, int packet_size) { 69 RTC_DCHECK_GE(packet_size, sizeof(int)); 70 for (int i = 0; i < number_packets; ++i) { 71 RtpPacketReceived packet; 72 constexpr size_t kFixedHeaderSize = 12; 73 packet.AllocatePayload(packet_size - kFixedHeaderSize); 74 packet.SetSequenceNumber(i); 75 packet.set_arrival_time(fake_clock_.CurrentTime()); 76 RTC_DCHECK_EQ(packet.Buffer().size(), packet_size); 77 pipe->DeliverRtpPacket(MediaType::ANY, std::move(packet), 78 [](const RtpPacketReceived&) { return false; }); 79 } 80 } 81 82 int PacketTimeMs(DataRate capacity, int packet_size) const { 83 return 8 * packet_size / capacity.kbps(); 84 } 85 86 SimulatedClock fake_clock_; 87 }; 88 89 // Test the capacity link and verify we get as many packets as we expect. 90 TEST_F(FakeNetworkPipeTest, CapacityTest) { 91 BuiltInNetworkBehaviorConfig config; 92 config.queue_length_packets = 20; 93 config.link_capacity = DataRate::KilobitsPerSec(80); 94 MockReceiver receiver; 95 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 96 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 97 &fake_clock_, std::move(simulated_network), &receiver)); 98 99 // Add 10 packets of 1000 bytes, = 80 kb, and verify it takes one second to 100 // get through the pipe. 101 const int kNumPackets = 10; 102 const int kPacketSize = 1000; 103 SendPackets(pipe.get(), kNumPackets, kPacketSize); 104 105 // Time to get one packet through the link. 106 const int kPacketTimeMs = PacketTimeMs(config.link_capacity, kPacketSize); 107 108 // Time haven't increased yet, so we souldn't get any packets. 109 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 110 pipe->Process(); 111 112 // Advance enough time to release one packet. 113 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 114 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 115 pipe->Process(); 116 117 // Release all but one packet 118 fake_clock_.AdvanceTimeMilliseconds(9 * kPacketTimeMs - 1); 119 EXPECT_CALL(receiver, DeliverRtpPacket).Times(8); 120 pipe->Process(); 121 122 // And the last one. 123 fake_clock_.AdvanceTimeMilliseconds(1); 124 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 125 pipe->Process(); 126 } 127 128 // Test the extra network delay. 129 TEST_F(FakeNetworkPipeTest, ExtraDelayTest) { 130 BuiltInNetworkBehaviorConfig config; 131 config.queue_length_packets = 20; 132 config.queue_delay_ms = 100; 133 config.link_capacity = DataRate::KilobitsPerSec(80); 134 MockReceiver receiver; 135 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 136 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 137 &fake_clock_, std::move(simulated_network), &receiver)); 138 139 const int kNumPackets = 2; 140 const int kPacketSize = 1000; 141 SendPackets(pipe.get(), kNumPackets, kPacketSize); 142 143 // Time to get one packet through the link. 144 const int kPacketTimeMs = PacketTimeMs(config.link_capacity, kPacketSize); 145 146 // Increase more than kPacketTimeMs, but not more than the extra delay. 147 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 148 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 149 pipe->Process(); 150 151 // Advance the network delay to get the first packet. 152 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms); 153 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 154 pipe->Process(); 155 156 // Advance one more kPacketTimeMs to get the last packet. 157 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 158 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 159 pipe->Process(); 160 } 161 162 // Test the number of buffers and packets are dropped when sending too many 163 // packets too quickly. 164 TEST_F(FakeNetworkPipeTest, QueueLengthTest) { 165 BuiltInNetworkBehaviorConfig config; 166 config.queue_length_packets = 2; 167 config.link_capacity = DataRate::KilobitsPerSec(80); 168 MockReceiver receiver; 169 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 170 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 171 &fake_clock_, std::move(simulated_network), &receiver)); 172 173 const int kPacketSize = 1000; 174 const int kPacketTimeMs = PacketTimeMs(config.link_capacity, kPacketSize); 175 176 // Send three packets and verify only 2 are delivered. 177 SendPackets(pipe.get(), 3, kPacketSize); 178 179 // Increase time enough to deliver all three packets, verify only two are 180 // delivered. 181 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs); 182 EXPECT_CALL(receiver, DeliverRtpPacket).Times(2); 183 pipe->Process(); 184 } 185 186 // Test we get statistics as expected. 187 TEST_F(FakeNetworkPipeTest, StatisticsTest) { 188 BuiltInNetworkBehaviorConfig config; 189 config.queue_length_packets = 2; 190 config.queue_delay_ms = 20; 191 config.link_capacity = DataRate::KilobitsPerSec(80); 192 MockReceiver receiver; 193 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 194 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 195 &fake_clock_, std::move(simulated_network), &receiver)); 196 197 const int kPacketSize = 1000; 198 const int kPacketTimeMs = PacketTimeMs(config.link_capacity, kPacketSize); 199 200 // Send three packets and verify only 2 are delivered. 201 SendPackets(pipe.get(), 3, kPacketSize); 202 fake_clock_.AdvanceTimeMilliseconds(3 * kPacketTimeMs + 203 config.queue_delay_ms); 204 205 EXPECT_CALL(receiver, DeliverRtpPacket).Times(2); 206 pipe->Process(); 207 208 // Packet 1: kPacketTimeMs + config.queue_delay_ms, 209 // packet 2: 2 * kPacketTimeMs + config.queue_delay_ms => 170 ms average. 210 EXPECT_EQ(pipe->AverageDelay(), 170); 211 EXPECT_EQ(pipe->SentPackets(), 2u); 212 EXPECT_EQ(pipe->DroppedPackets(), 1u); 213 EXPECT_EQ(pipe->PercentageLoss(), 1 / 3.f); 214 } 215 216 // Change the link capacity half-way through the test and verify that the 217 // delivery times change accordingly. 218 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithEmptyPipeTest) { 219 BuiltInNetworkBehaviorConfig config; 220 config.queue_length_packets = 20; 221 config.link_capacity = DataRate::KilobitsPerSec(80); 222 MockReceiver receiver; 223 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config)); 224 SimulatedNetwork* simulated_network = network.get(); 225 std::unique_ptr<FakeNetworkPipe> pipe( 226 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver)); 227 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, kPacketSize); 236 237 // Time hasn't increased yet, so we souldn't get any packets. 238 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 239 pipe->Process(); 240 241 // Advance time in steps to release one packet at a time. 242 for (int i = 0; i < kNumPackets; ++i) { 243 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms); 244 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 245 pipe->Process(); 246 } 247 248 // Change the capacity. 249 config.link_capacity = config.link_capacity / 2; // Reduce to 50%. 250 simulated_network->SetConfig(config); 251 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, kPacketSize); 258 259 // Time hasn't increased yet, so we souldn't get any packets. 260 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 261 pipe->Process(); 262 263 // Advance time in steps to release one packet at a time. 264 for (int i = 0; i < kNumPackets; ++i) { 265 fake_clock_.AdvanceTimeMilliseconds(packet_time_ms); 266 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 267 pipe->Process(); 268 } 269 270 // Check that all the packets were sent. 271 EXPECT_EQ(static_cast<size_t>(2 * kNumPackets), pipe->SentPackets()); 272 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value()); 273 fake_clock_.AdvanceTimeMilliseconds(1000); 274 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 275 pipe->Process(); 276 } 277 278 // Change the link capacity half-way through the test and verify that the 279 // delivery times change accordingly. 280 TEST_F(FakeNetworkPipeTest, ChangingCapacityWithPacketsInPipeTest) { 281 BuiltInNetworkBehaviorConfig config; 282 config.queue_length_packets = 20; 283 config.link_capacity = DataRate::KilobitsPerSec(80); 284 MockReceiver receiver; 285 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config)); 286 SimulatedNetwork* simulated_network = network.get(); 287 std::unique_ptr<FakeNetworkPipe> pipe( 288 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver)); 289 290 // Add 20 packets of 1000 bytes, = 160 kb. 291 const int kNumPackets = 20; 292 const int kPacketSize = 1000; 293 SendPackets(pipe.get(), kNumPackets, kPacketSize); 294 295 // Time hasn't increased yet, so we souldn't get any packets. 296 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 297 pipe->Process(); 298 299 // Advance time in steps to release half of the packets one at a time. 300 int step_ms = PacketTimeMs(config.link_capacity, kPacketSize); 301 for (int i = 0; i < kNumPackets / 2; ++i) { 302 fake_clock_.AdvanceTimeMilliseconds(step_ms); 303 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 304 pipe->Process(); 305 } 306 307 // Change the capacity. 308 config.link_capacity = 2 * config.link_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, kPacketSize); 313 for (int i = 0; i < kNumPackets / 2; ++i) { 314 fake_clock_.AdvanceTimeMilliseconds(step_ms); 315 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 316 pipe->Process(); 317 } 318 319 // Check that all the packets were sent. 320 EXPECT_EQ(static_cast<size_t>(kNumPackets), pipe->SentPackets()); 321 EXPECT_FALSE(pipe->TimeUntilNextProcess().has_value()); 322 fake_clock_.AdvanceTimeMilliseconds(1000); 323 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 324 pipe->Process(); 325 } 326 327 // At first disallow reordering and then allow reordering. 328 TEST_F(FakeNetworkPipeTest, DisallowReorderingThenAllowReordering) { 329 BuiltInNetworkBehaviorConfig config; 330 config.queue_length_packets = 1000; 331 config.link_capacity = DataRate::KilobitsPerSec(80); 332 config.queue_delay_ms = 100; 333 config.delay_standard_deviation_ms = 10; 334 ReorderTestReceiver receiver; 335 std::unique_ptr<SimulatedNetwork> network(new SimulatedNetwork(config)); 336 SimulatedNetwork* simulated_network = network.get(); 337 std::unique_ptr<FakeNetworkPipe> pipe( 338 new FakeNetworkPipe(&fake_clock_, std::move(network), &receiver)); 339 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. 347 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size()); 348 int last_seq_num = -1; 349 for (int seq_num : receiver.delivered_sequence_numbers_) { 350 EXPECT_GT(seq_num, last_seq_num); 351 last_seq_num = seq_num; 352 } 353 354 config.allow_reordering = true; 355 simulated_network->SetConfig(config); 356 SendPackets(pipe.get(), kNumPackets, kPacketSize); 357 fake_clock_.AdvanceTimeMilliseconds(1000); 358 receiver.delivered_sequence_numbers_.clear(); 359 pipe->Process(); 360 361 // Confirm that all packets have been delivered 362 // and that reordering has occured. 363 EXPECT_EQ(kNumPackets, receiver.delivered_sequence_numbers_.size()); 364 bool reordering_has_occured = false; 365 last_seq_num = -1; 366 for (int seq_num : receiver.delivered_sequence_numbers_) { 367 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 } 375 376 TEST_F(FakeNetworkPipeTest, BurstLoss) { 377 const int kLossPercent = 5; 378 const int kAvgBurstLength = 3; 379 const int kNumPackets = 10000; 380 const int kPacketSize = 10; 381 382 BuiltInNetworkBehaviorConfig config; 383 config.queue_length_packets = kNumPackets; 384 config.loss_percent = kLossPercent; 385 config.avg_burst_loss_length = kAvgBurstLength; 386 ReorderTestReceiver receiver; 387 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 388 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 389 &fake_clock_, std::move(simulated_network), &receiver)); 390 391 SendPackets(pipe.get(), kNumPackets, kPacketSize); 392 fake_clock_.AdvanceTimeMilliseconds(1000); 393 pipe->Process(); 394 395 // Check that the average loss is `kLossPercent` percent. 396 int lost_packets = kNumPackets - receiver.delivered_sequence_numbers_.size(); 397 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. 402 size_t received_packets = receiver.delivered_sequence_numbers_.size(); 403 int num_bursts = 0; 404 for (size_t i = 0; i < received_packets - 1; ++i) { 405 int diff = receiver.delivered_sequence_numbers_[i + 1] - 406 receiver.delivered_sequence_numbers_[i]; 407 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 } 415 416 TEST_F(FakeNetworkPipeTest, SetReceiver) { 417 BuiltInNetworkBehaviorConfig config; 418 config.link_capacity = DataRate::KilobitsPerSec(800); 419 MockReceiver receiver; 420 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 421 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 422 &fake_clock_, std::move(simulated_network), &receiver)); 423 424 const int kPacketSize = 1000; 425 const int kPacketTimeMs = PacketTimeMs(config.link_capacity, kPacketSize); 426 SendPackets(pipe.get(), 1, kPacketSize); 427 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 428 EXPECT_CALL(receiver, DeliverRtpPacket).Times(1); 429 pipe->Process(); 430 431 MockReceiver new_receiver; 432 pipe->SetReceiver(&new_receiver); 433 434 SendPackets(pipe.get(), 1, kPacketSize); 435 fake_clock_.AdvanceTimeMilliseconds(kPacketTimeMs); 436 EXPECT_CALL(receiver, DeliverRtpPacket).Times(0); 437 EXPECT_CALL(new_receiver, DeliverRtpPacket).Times(1); 438 pipe->Process(); 439 } 440 441 TEST_F(FakeNetworkPipeTest, DeliverRtpPacketSetsCorrectArrivalTime) { 442 BuiltInNetworkBehaviorConfig config; 443 config.queue_delay_ms = 100; 444 MockReceiver receiver; 445 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 446 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 447 &fake_clock_, std::move(simulated_network), &receiver)); 448 449 Timestamp send_time = fake_clock_.CurrentTime(); 450 RtpPacketReceived packet(nullptr, send_time); 451 packet.SetExtension<TransportSequenceNumber>(123); 452 pipe->DeliverRtpPacket(MediaType::VIDEO, std::move(packet), 453 [](const RtpPacketReceived&) { return false; }); 454 455 // Advance the network delay to get the first packet. 456 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms); 457 EXPECT_CALL(receiver, DeliverRtpPacket(MediaType::VIDEO, _, _)) 458 .WillOnce(WithArg<1>([&](RtpPacketReceived packet) { 459 EXPECT_EQ(packet.arrival_time(), 460 send_time + TimeDelta::Millis(config.queue_delay_ms)); 461 })); 462 pipe->Process(); 463 } 464 465 TEST_F(FakeNetworkPipeTest, DeliverRtpPacketPropagatesExtensions) { 466 BuiltInNetworkBehaviorConfig config; 467 config.queue_delay_ms = 100; 468 MockReceiver receiver; 469 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 470 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 471 &fake_clock_, std::move(simulated_network), &receiver)); 472 RtpHeaderExtensionMap extension_map; 473 extension_map.Register<TransportSequenceNumber>(/*id=*/7); 474 475 RtpPacketReceived packet(&extension_map, fake_clock_.CurrentTime()); 476 packet.SetExtension<TransportSequenceNumber>(123); 477 pipe->DeliverRtpPacket(MediaType::VIDEO, std::move(packet), 478 [](const RtpPacketReceived&) { return false; }); 479 480 // Advance the network delay to get the first packet. 481 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms); 482 EXPECT_CALL(receiver, DeliverRtpPacket(MediaType::VIDEO, _, _)) 483 .WillOnce(WithArg<1>([](RtpPacketReceived packet) { 484 EXPECT_EQ(packet.GetExtension<TransportSequenceNumber>(), 123); 485 })); 486 pipe->Process(); 487 } 488 489 TEST_F(FakeNetworkPipeTest, DeliverRtcpPacket) { 490 BuiltInNetworkBehaviorConfig config; 491 config.queue_delay_ms = 100; 492 MockReceiver receiver; 493 auto simulated_network = std::make_unique<SimulatedNetwork>(config); 494 std::unique_ptr<FakeNetworkPipe> pipe(new FakeNetworkPipe( 495 &fake_clock_, std::move(simulated_network), &receiver)); 496 497 CopyOnWriteBuffer buffer(100); 498 memset(buffer.MutableData(), 0, 100); 499 pipe->DeliverRtcpPacket(std::move(buffer)); 500 501 // Advance the network delay to get the first packet. 502 fake_clock_.AdvanceTimeMilliseconds(config.queue_delay_ms); 503 EXPECT_CALL(receiver, 504 DeliverRtcpPacket(Property(&CopyOnWriteBuffer::size, 100))); 505 pipe->Process(); 506 } 507 508 } // namespace webrtc