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