blob: 626545426368fc3808462dad9b73bd15ba67d821 [file] [log] [blame]
Sebastian Jansson98b07e912018-09-27 11:47:011/*
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>
Evan Shrubsole02bdf662023-03-02 12:52:5013#include <memory>
Sebastian Jansson98b07e912018-09-27 11:47:0114#include <vector>
15
Evan Shrubsole02bdf662023-03-02 12:52:5016#include "absl/cleanup/cleanup.h"
Sebastian Janssonf52d3ed2020-02-28 15:22:0917#include "rtc_base/net_helper.h"
Yves Gerey2e00abc2018-10-05 13:39:2418#include "rtc_base/numerics/safe_minmax.h"
19
Sebastian Jansson98b07e912018-09-27 11:47:0120namespace webrtc {
21namespace test {
22namespace {
Sebastian Jansson4124dab2019-04-01 12:33:5323constexpr char kDummyTransportName[] = "dummy";
Sebastian Janssonef86d142019-04-15 12:42:4224SimulatedNetwork::Config CreateSimulationConfig(
25 NetworkSimulationConfig config) {
Sebastian Jansson98b07e912018-09-27 11:47:0126 SimulatedNetwork::Config sim_config;
Sebastian Janssonef86d142019-04-15 12:42:4227 sim_config.link_capacity_kbps = config.bandwidth.kbps_or(0);
28 sim_config.loss_percent = config.loss_rate * 100;
29 sim_config.queue_delay_ms = config.delay.ms();
30 sim_config.delay_standard_deviation_ms = config.delay_std_dev.ms();
Sebastian Jansson8c8feb92019-01-29 14:59:1731 sim_config.packet_overhead = config.packet_overhead.bytes<int>();
Sebastian Jansson24c678f2019-10-14 13:07:5832 sim_config.queue_length_packets =
33 config.packet_queue_length_limit.value_or(0);
Sebastian Jansson98b07e912018-09-27 11:47:0134 return sim_config;
35}
36} // namespace
37
Sebastian Janssona4c22b92019-04-15 19:10:0038SimulationNode::SimulationNode(NetworkSimulationConfig config,
39 SimulatedNetwork* behavior,
40 EmulatedNetworkNode* network_node)
41 : config_(config), simulation_(behavior), network_node_(network_node) {}
Sebastian Jansson98b07e912018-09-27 11:47:0142
Sebastian Janssona4c22b92019-04-15 19:10:0043std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior(
Sebastian Janssonef86d142019-04-15 12:42:4244 NetworkSimulationConfig config) {
Sebastian Jansson98b07e912018-09-27 11:47:0145 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
Mirko Bonadei317a1f02019-09-17 15:06:1846 return std::make_unique<SimulatedNetwork>(sim_config);
Sebastian Jansson98b07e912018-09-27 11:47:0147}
48
49void SimulationNode::UpdateConfig(
Sebastian Janssonef86d142019-04-15 12:42:4250 std::function<void(NetworkSimulationConfig*)> modifier) {
Sebastian Jansson98b07e912018-09-27 11:47:0151 modifier(&config_);
52 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
Sebastian Janssona4c22b92019-04-15 19:10:0053 simulation_->SetConfig(sim_config);
Sebastian Jansson98b07e912018-09-27 11:47:0154}
55
56void SimulationNode::PauseTransmissionUntil(Timestamp until) {
Sebastian Janssona4c22b92019-04-15 19:10:0057 simulation_->PauseTransmissionUntil(until.us());
Sebastian Jansson98b07e912018-09-27 11:47:0158}
59
60ColumnPrinter SimulationNode::ConfigPrinter() const {
Sebastian Janssonef86d142019-04-15 12:42:4261 return ColumnPrinter::Lambda(
62 "propagation_delay capacity loss_rate",
63 [this](rtc::SimpleStringBuilder& sb) {
64 sb.AppendFormat("%.3lf %.0lf %.2lf", config_.delay.seconds<double>(),
65 config_.bandwidth.bps() / 8.0, config_.loss_rate);
66 });
Sebastian Jansson98b07e912018-09-27 11:47:0167}
68
Sebastian Janssonaa01f272019-01-30 10:28:5969NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
Sebastian Jansson800e1212018-10-22 09:49:0370 Call* sender_call)
71 : sender_clock_(sender_clock), sender_call_(sender_call) {}
Sebastian Jansson98b07e912018-09-27 11:47:0172
73NetworkNodeTransport::~NetworkNodeTransport() = default;
74
Harald Alvestrandd43af912023-08-15 11:41:4575bool NetworkNodeTransport::SendRtp(rtc::ArrayView<const uint8_t> packet,
Sebastian Jansson98b07e912018-09-27 11:47:0176 const PacketOptions& options) {
Sebastian Jansson800e1212018-10-22 09:49:0377 int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
Sebastian Jansson156d11d2018-09-28 15:21:3478 rtc::SentPacket sent_packet;
79 sent_packet.packet_id = options.packet_id;
Sebastian Jansson03789972018-10-09 16:27:5780 sent_packet.info.included_in_feedback = options.included_in_feedback;
81 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 15:21:3482 sent_packet.send_time_ms = send_time_ms;
Harald Alvestrandd43af912023-08-15 11:41:4583 sent_packet.info.packet_size_bytes = packet.size();
Sebastian Jansson156d11d2018-09-28 15:21:3484 sent_packet.info.packet_type = rtc::PacketType::kData;
Sebastian Jansson800e1212018-10-22 09:49:0385 sender_call_->OnSentPacket(sent_packet);
Sebastian Jansson156d11d2018-09-28 15:21:3486
Markus Handella5a4be12020-07-08 14:09:2187 MutexLock lock(&mutex_);
Sebastian Jansson77bd3852020-01-17 12:05:5488 if (!endpoint_)
Sebastian Jansson800e1212018-10-22 09:49:0389 return false;
Harald Alvestrandd43af912023-08-15 11:41:4590 rtc::CopyOnWriteBuffer buffer(packet);
Sebastian Jansson77bd3852020-01-17 12:05:5491 endpoint_->SendPacket(local_address_, remote_address_, buffer,
92 packet_overhead_.bytes());
Sebastian Janssonf65309c2018-12-20 09:26:0093 return true;
Sebastian Jansson98b07e912018-09-27 11:47:0194}
95
Harald Alvestrandd43af912023-08-15 11:41:4596bool NetworkNodeTransport::SendRtcp(rtc::ArrayView<const uint8_t> packet) {
97 rtc::CopyOnWriteBuffer buffer(packet);
Markus Handella5a4be12020-07-08 14:09:2198 MutexLock lock(&mutex_);
Sebastian Jansson77bd3852020-01-17 12:05:5499 if (!endpoint_)
Sebastian Jansson800e1212018-10-22 09:49:03100 return false;
Sebastian Jansson77bd3852020-01-17 12:05:54101 endpoint_->SendPacket(local_address_, remote_address_, buffer,
102 packet_overhead_.bytes());
Sebastian Janssonf65309c2018-12-20 09:26:00103 return true;
Sebastian Jansson98b07e912018-09-27 11:47:01104}
105
Sebastian Jansson77bd3852020-01-17 12:05:54106void NetworkNodeTransport::Connect(EmulatedEndpoint* endpoint,
107 const rtc::SocketAddress& receiver_address,
Sebastian Jansson800e1212018-10-22 09:49:03108 DataSize packet_overhead) {
Sebastian Jansson800e1212018-10-22 09:49:03109 rtc::NetworkRoute route;
110 route.connected = true;
Sebastian Jansson77bd3852020-01-17 12:05:54111 // We assume that the address will be unique in the lower bytes.
Jonas Oreland71fda362020-03-20 15:11:56112 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()));
Sebastian Janssonf52d3ed2020-02-28 15:22:09116 route.packet_overhead = packet_overhead.bytes() +
117 receiver_address.ipaddr().overhead() +
118 cricket::kUdpHeaderSize;
Sebastian Jansson4124dab2019-04-01 12:33:53119 {
Sebastian Jansson77bd3852020-01-17 12:05:54120 // Only IPv4 address is supported.
121 RTC_CHECK_EQ(receiver_address.family(), AF_INET);
Markus Handella5a4be12020-07-08 14:09:21122 MutexLock lock(&mutex_);
Sebastian Jansson77bd3852020-01-17 12:05:54123 endpoint_ = endpoint;
124 local_address_ = rtc::SocketAddress(endpoint_->GetPeerLocalAddress(), 0);
125 remote_address_ = receiver_address;
Sebastian Jansson4124dab2019-04-01 12:33:53126 packet_overhead_ = packet_overhead;
127 current_network_route_ = route;
128 }
129
Evan Shrubsole02bdf662023-03-02 12:52:50130 // Must be called from the worker thread.
131 rtc::Event event;
132 auto cleanup = absl::MakeCleanup([&event] { event.Set(); });
133 auto&& task = [this, &route, cleanup = std::move(cleanup)] {
134 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
135 kDummyTransportName, route);
136 };
137 if (!sender_call_->worker_thread()->IsCurrent()) {
138 sender_call_->worker_thread()->PostTask(std::move(task));
139 } else {
140 std::move(task)();
141 }
142 event.Wait(TimeDelta::Seconds(1));
Sebastian Jansson4124dab2019-04-01 12:33:53143}
144
145void NetworkNodeTransport::Disconnect() {
Markus Handella5a4be12020-07-08 14:09:21146 MutexLock lock(&mutex_);
Sebastian Jansson4124dab2019-04-01 12:33:53147 current_network_route_.connected = false;
Evan Shrubsole02bdf662023-03-02 12:52:50148
Sebastian Jansson4124dab2019-04-01 12:33:53149 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
150 kDummyTransportName, current_network_route_);
151 current_network_route_ = {};
Sebastian Jansson77bd3852020-01-17 12:05:54152 endpoint_ = nullptr;
Sebastian Jansson98b07e912018-09-27 11:47:01153}
154
Sebastian Jansson98b07e912018-09-27 11:47:01155} // namespace test
156} // namespace webrtc