You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
5.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 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. #include "test/scenario/network_node.h"
  11. #include <algorithm>
  12. #include <vector>
  13. #include <memory>
  14. #include "rtc_base/numerics/safe_minmax.h"
  15. namespace webrtc {
  16. namespace test {
  17. namespace {
  18. constexpr char kDummyTransportName[] = "dummy";
  19. SimulatedNetwork::Config CreateSimulationConfig(
  20. NetworkSimulationConfig config) {
  21. SimulatedNetwork::Config sim_config;
  22. sim_config.link_capacity_kbps = config.bandwidth.kbps_or(0);
  23. sim_config.loss_percent = config.loss_rate * 100;
  24. sim_config.queue_delay_ms = config.delay.ms();
  25. sim_config.delay_standard_deviation_ms = config.delay_std_dev.ms();
  26. sim_config.packet_overhead = config.packet_overhead.bytes<int>();
  27. sim_config.codel_active_queue_management =
  28. config.codel_active_queue_management;
  29. sim_config.queue_length_packets =
  30. config.packet_queue_length_limit.value_or(0);
  31. return sim_config;
  32. }
  33. } // namespace
  34. SimulationNode::SimulationNode(NetworkSimulationConfig config,
  35. SimulatedNetwork* behavior,
  36. EmulatedNetworkNode* network_node)
  37. : config_(config), simulation_(behavior), network_node_(network_node) {}
  38. std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior(
  39. NetworkSimulationConfig config) {
  40. SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
  41. return std::make_unique<SimulatedNetwork>(sim_config);
  42. }
  43. void SimulationNode::UpdateConfig(
  44. std::function<void(NetworkSimulationConfig*)> modifier) {
  45. modifier(&config_);
  46. SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
  47. simulation_->SetConfig(sim_config);
  48. }
  49. void SimulationNode::PauseTransmissionUntil(Timestamp until) {
  50. simulation_->PauseTransmissionUntil(until.us());
  51. }
  52. ColumnPrinter SimulationNode::ConfigPrinter() const {
  53. return ColumnPrinter::Lambda(
  54. "propagation_delay capacity loss_rate",
  55. [this](rtc::SimpleStringBuilder& sb) {
  56. sb.AppendFormat("%.3lf %.0lf %.2lf", config_.delay.seconds<double>(),
  57. config_.bandwidth.bps() / 8.0, config_.loss_rate);
  58. });
  59. }
  60. NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
  61. Call* sender_call)
  62. : sender_clock_(sender_clock), sender_call_(sender_call) {}
  63. NetworkNodeTransport::~NetworkNodeTransport() = default;
  64. bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
  65. size_t length,
  66. const PacketOptions& options) {
  67. int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
  68. rtc::SentPacket sent_packet;
  69. sent_packet.packet_id = options.packet_id;
  70. sent_packet.info.included_in_feedback = options.included_in_feedback;
  71. sent_packet.info.included_in_allocation = options.included_in_allocation;
  72. sent_packet.send_time_ms = send_time_ms;
  73. sent_packet.info.packet_size_bytes = length;
  74. sent_packet.info.packet_type = rtc::PacketType::kData;
  75. sender_call_->OnSentPacket(sent_packet);
  76. Timestamp send_time = Timestamp::ms(send_time_ms);
  77. rtc::CritScope crit(&crit_sect_);
  78. if (!send_net_)
  79. return false;
  80. rtc::CopyOnWriteBuffer buffer(packet, length,
  81. length + packet_overhead_.bytes());
  82. buffer.SetSize(length + packet_overhead_.bytes());
  83. send_net_->OnPacketReceived(
  84. EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
  85. return true;
  86. }
  87. bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
  88. rtc::CopyOnWriteBuffer buffer(packet, length);
  89. Timestamp send_time = sender_clock_->CurrentTime();
  90. rtc::CritScope crit(&crit_sect_);
  91. buffer.SetSize(length + packet_overhead_.bytes());
  92. if (!send_net_)
  93. return false;
  94. send_net_->OnPacketReceived(
  95. EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
  96. return true;
  97. }
  98. void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node,
  99. rtc::IPAddress receiver_ip,
  100. DataSize packet_overhead) {
  101. rtc::NetworkRoute route;
  102. route.connected = true;
  103. route.local_network_id =
  104. static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
  105. route.remote_network_id =
  106. static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
  107. {
  108. // Only IPv4 address is supported. We don't use full range of IPs in
  109. // scenario framework and also we need a simple way to convert IP into
  110. // network_id to signal network route.
  111. RTC_CHECK_EQ(receiver_ip.family(), AF_INET);
  112. RTC_CHECK_LE(receiver_ip.v4AddressAsHostOrderInteger(),
  113. std::numeric_limits<uint16_t>::max());
  114. rtc::CritScope crit(&crit_sect_);
  115. send_net_ = send_node;
  116. receiver_address_ = rtc::SocketAddress(receiver_ip, 0);
  117. packet_overhead_ = packet_overhead;
  118. current_network_route_ = route;
  119. }
  120. sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
  121. kDummyTransportName, route);
  122. }
  123. void NetworkNodeTransport::Disconnect() {
  124. rtc::CritScope crit(&crit_sect_);
  125. current_network_route_.connected = false;
  126. sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
  127. kDummyTransportName, current_network_route_);
  128. current_network_route_ = {};
  129. send_net_ = nullptr;
  130. }
  131. } // namespace test
  132. } // namespace webrtc