• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #include <algorithm>
13 #include <vector>
14 
15 #include <memory>
16 #include "rtc_base/net_helper.h"
17 #include "rtc_base/numerics/safe_minmax.h"
18 
19 namespace webrtc {
20 namespace test {
21 namespace {
22 constexpr char kDummyTransportName[] = "dummy";
CreateSimulationConfig(NetworkSimulationConfig config)23 SimulatedNetwork::Config CreateSimulationConfig(
24     NetworkSimulationConfig config) {
25   SimulatedNetwork::Config sim_config;
26   sim_config.link_capacity_kbps = config.bandwidth.kbps_or(0);
27   sim_config.loss_percent = config.loss_rate * 100;
28   sim_config.queue_delay_ms = config.delay.ms();
29   sim_config.delay_standard_deviation_ms = config.delay_std_dev.ms();
30   sim_config.packet_overhead = config.packet_overhead.bytes<int>();
31   sim_config.queue_length_packets =
32       config.packet_queue_length_limit.value_or(0);
33   return sim_config;
34 }
35 }  // namespace
36 
SimulationNode(NetworkSimulationConfig config,SimulatedNetwork * behavior,EmulatedNetworkNode * network_node)37 SimulationNode::SimulationNode(NetworkSimulationConfig config,
38                                SimulatedNetwork* behavior,
39                                EmulatedNetworkNode* network_node)
40     : config_(config), simulation_(behavior), network_node_(network_node) {}
41 
CreateBehavior(NetworkSimulationConfig config)42 std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior(
43     NetworkSimulationConfig config) {
44   SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
45   return std::make_unique<SimulatedNetwork>(sim_config);
46 }
47 
UpdateConfig(std::function<void (NetworkSimulationConfig *)> modifier)48 void SimulationNode::UpdateConfig(
49     std::function<void(NetworkSimulationConfig*)> modifier) {
50   modifier(&config_);
51   SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
52   simulation_->SetConfig(sim_config);
53 }
54 
PauseTransmissionUntil(Timestamp until)55 void SimulationNode::PauseTransmissionUntil(Timestamp until) {
56   simulation_->PauseTransmissionUntil(until.us());
57 }
58 
ConfigPrinter() const59 ColumnPrinter SimulationNode::ConfigPrinter() const {
60   return ColumnPrinter::Lambda(
61       "propagation_delay capacity loss_rate",
62       [this](rtc::SimpleStringBuilder& sb) {
63         sb.AppendFormat("%.3lf %.0lf %.2lf", config_.delay.seconds<double>(),
64                         config_.bandwidth.bps() / 8.0, config_.loss_rate);
65       });
66 }
67 
NetworkNodeTransport(Clock * sender_clock,Call * sender_call)68 NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
69                                            Call* sender_call)
70     : sender_clock_(sender_clock), sender_call_(sender_call) {}
71 
72 NetworkNodeTransport::~NetworkNodeTransport() = default;
73 
SendRtp(const uint8_t * packet,size_t length,const PacketOptions & options)74 bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
75                                    size_t length,
76                                    const PacketOptions& options) {
77   int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
78   rtc::SentPacket sent_packet;
79   sent_packet.packet_id = options.packet_id;
80   sent_packet.info.included_in_feedback = options.included_in_feedback;
81   sent_packet.info.included_in_allocation = options.included_in_allocation;
82   sent_packet.send_time_ms = send_time_ms;
83   sent_packet.info.packet_size_bytes = length;
84   sent_packet.info.packet_type = rtc::PacketType::kData;
85   sender_call_->OnSentPacket(sent_packet);
86 
87   MutexLock lock(&mutex_);
88   if (!endpoint_)
89     return false;
90   rtc::CopyOnWriteBuffer buffer(packet, length);
91   endpoint_->SendPacket(local_address_, remote_address_, buffer,
92                         packet_overhead_.bytes());
93   return true;
94 }
95 
SendRtcp(const uint8_t * packet,size_t length)96 bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
97   rtc::CopyOnWriteBuffer buffer(packet, length);
98   MutexLock lock(&mutex_);
99   if (!endpoint_)
100     return false;
101   endpoint_->SendPacket(local_address_, remote_address_, buffer,
102                         packet_overhead_.bytes());
103   return true;
104 }
105 
Connect(EmulatedEndpoint * endpoint,const rtc::SocketAddress & receiver_address,DataSize packet_overhead)106 void NetworkNodeTransport::Connect(EmulatedEndpoint* endpoint,
107                                    const rtc::SocketAddress& receiver_address,
108                                    DataSize packet_overhead) {
109   rtc::NetworkRoute route;
110   route.connected = true;
111   // We assume that the address will be unique in the lower bytes.
112   route.local = rtc::RouteEndpoint::CreateWithNetworkId(static_cast<uint16_t>(
113       receiver_address.ipaddr().v4AddressAsHostOrderInteger()));
114   route.remote = rtc::RouteEndpoint::CreateWithNetworkId(static_cast<uint16_t>(
115       receiver_address.ipaddr().v4AddressAsHostOrderInteger()));
116   route.packet_overhead = packet_overhead.bytes() +
117                           receiver_address.ipaddr().overhead() +
118                           cricket::kUdpHeaderSize;
119   {
120     // Only IPv4 address is supported.
121     RTC_CHECK_EQ(receiver_address.family(), AF_INET);
122     MutexLock lock(&mutex_);
123     endpoint_ = endpoint;
124     local_address_ = rtc::SocketAddress(endpoint_->GetPeerLocalAddress(), 0);
125     remote_address_ = receiver_address;
126     packet_overhead_ = packet_overhead;
127     current_network_route_ = route;
128   }
129 
130   sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
131       kDummyTransportName, route);
132 }
133 
Disconnect()134 void NetworkNodeTransport::Disconnect() {
135   MutexLock lock(&mutex_);
136   current_network_route_.connected = false;
137   sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
138       kDummyTransportName, current_network_route_);
139   current_network_route_ = {};
140   endpoint_ = nullptr;
141 }
142 
143 }  // namespace test
144 }  // namespace webrtc
145