blob: d381152934d636adb102821a1c52e30f19e2379d [file] [log] [blame]
Sebastian Jansson98b07e92018-09-27 13:47:01 +02001/*
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
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
Yves Gerey2e00abc2018-10-05 15:39:24 +020016#include "rtc_base/numerics/safe_minmax.h"
17
Sebastian Jansson98b07e92018-09-27 13:47:01 +020018namespace webrtc {
19namespace test {
20namespace {
Sebastian Jansson4124dab2019-04-01 14:33:53 +020021constexpr char kDummyTransportName[] = "dummy";
Sebastian Janssonef86d142019-04-15 14:42:42 +020022SimulatedNetwork::Config CreateSimulationConfig(
23 NetworkSimulationConfig config) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020024 SimulatedNetwork::Config sim_config;
Sebastian Janssonef86d142019-04-15 14:42:42 +020025 sim_config.link_capacity_kbps = config.bandwidth.kbps_or(0);
26 sim_config.loss_percent = config.loss_rate * 100;
27 sim_config.queue_delay_ms = config.delay.ms();
28 sim_config.delay_standard_deviation_ms = config.delay_std_dev.ms();
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010029 sim_config.packet_overhead = config.packet_overhead.bytes<int>();
Sebastian Jansson2b08e312019-02-25 10:24:46 +010030 sim_config.codel_active_queue_management =
Sebastian Janssonef86d142019-04-15 14:42:42 +020031 config.codel_active_queue_management;
Sebastian Jansson24c678f2019-10-14 15:07:58 +020032 sim_config.queue_length_packets =
33 config.packet_queue_length_limit.value_or(0);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020034 return sim_config;
35}
36} // namespace
37
Sebastian Janssona4c22b92019-04-15 21:10:00 +020038SimulationNode::SimulationNode(NetworkSimulationConfig config,
39 SimulatedNetwork* behavior,
40 EmulatedNetworkNode* network_node)
41 : config_(config), simulation_(behavior), network_node_(network_node) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020042
Sebastian Janssona4c22b92019-04-15 21:10:00 +020043std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior(
Sebastian Janssonef86d142019-04-15 14:42:42 +020044 NetworkSimulationConfig config) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020045 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020046 return std::make_unique<SimulatedNetwork>(sim_config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020047}
48
49void SimulationNode::UpdateConfig(
Sebastian Janssonef86d142019-04-15 14:42:42 +020050 std::function<void(NetworkSimulationConfig*)> modifier) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020051 modifier(&config_);
52 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
Sebastian Janssona4c22b92019-04-15 21:10:00 +020053 simulation_->SetConfig(sim_config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020054}
55
56void SimulationNode::PauseTransmissionUntil(Timestamp until) {
Sebastian Janssona4c22b92019-04-15 21:10:00 +020057 simulation_->PauseTransmissionUntil(until.us());
Sebastian Jansson98b07e92018-09-27 13:47:01 +020058}
59
60ColumnPrinter SimulationNode::ConfigPrinter() const {
Sebastian Janssonef86d142019-04-15 14:42:42 +020061 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 Jansson98b07e92018-09-27 13:47:01 +020067}
68
Sebastian Janssonaa01f272019-01-30 11:28:59 +010069NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
Sebastian Jansson800e1212018-10-22 11:49:03 +020070 Call* sender_call)
71 : sender_clock_(sender_clock), sender_call_(sender_call) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020072
73NetworkNodeTransport::~NetworkNodeTransport() = default;
74
75bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
76 size_t length,
77 const PacketOptions& options) {
Sebastian Jansson800e1212018-10-22 11:49:03 +020078 int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
Sebastian Jansson156d11d2018-09-28 17:21:34 +020079 rtc::SentPacket sent_packet;
80 sent_packet.packet_id = options.packet_id;
Sebastian Jansson03789972018-10-09 18:27:57 +020081 sent_packet.info.included_in_feedback = options.included_in_feedback;
82 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 17:21:34 +020083 sent_packet.send_time_ms = send_time_ms;
84 sent_packet.info.packet_size_bytes = length;
85 sent_packet.info.packet_type = rtc::PacketType::kData;
Sebastian Jansson800e1212018-10-22 11:49:03 +020086 sender_call_->OnSentPacket(sent_packet);
Sebastian Jansson156d11d2018-09-28 17:21:34 +020087
88 Timestamp send_time = Timestamp::ms(send_time_ms);
Sebastian Jansson800e1212018-10-22 11:49:03 +020089 rtc::CritScope crit(&crit_sect_);
90 if (!send_net_)
91 return false;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020092 rtc::CopyOnWriteBuffer buffer(packet, length,
93 length + packet_overhead_.bytes());
94 buffer.SetSize(length + packet_overhead_.bytes());
Artem Titov4cd433e2019-04-01 11:01:16 +020095 send_net_->OnPacketReceived(
96 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +010097 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020098}
99
100bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
101 rtc::CopyOnWriteBuffer buffer(packet, length);
Sebastian Janssonb64ad0e2019-06-19 09:39:34 +0200102 Timestamp send_time = sender_clock_->CurrentTime();
Sebastian Jansson800e1212018-10-22 11:49:03 +0200103 rtc::CritScope crit(&crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200104 buffer.SetSize(length + packet_overhead_.bytes());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200105 if (!send_net_)
106 return false;
Artem Titov4cd433e2019-04-01 11:01:16 +0200107 send_net_->OnPacketReceived(
108 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100109 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200110}
111
Artem Titov37d18482019-01-08 15:41:45 +0100112void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node,
Artem Titov4cd433e2019-04-01 11:01:16 +0200113 rtc::IPAddress receiver_ip,
Sebastian Jansson800e1212018-10-22 11:49:03 +0200114 DataSize packet_overhead) {
Sebastian Jansson800e1212018-10-22 11:49:03 +0200115 rtc::NetworkRoute route;
116 route.connected = true;
Artem Titov4cd433e2019-04-01 11:01:16 +0200117 route.local_network_id =
118 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
119 route.remote_network_id =
120 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200121 {
122 // Only IPv4 address is supported. We don't use full range of IPs in
123 // scenario framework and also we need a simple way to convert IP into
124 // network_id to signal network route.
125 RTC_CHECK_EQ(receiver_ip.family(), AF_INET);
126 RTC_CHECK_LE(receiver_ip.v4AddressAsHostOrderInteger(),
127 std::numeric_limits<uint16_t>::max());
128 rtc::CritScope crit(&crit_sect_);
129 send_net_ = send_node;
130 receiver_address_ = rtc::SocketAddress(receiver_ip, 0);
131 packet_overhead_ = packet_overhead;
132 current_network_route_ = route;
133 }
134
Sebastian Jansson800e1212018-10-22 11:49:03 +0200135 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200136 kDummyTransportName, route);
137}
138
139void NetworkNodeTransport::Disconnect() {
140 rtc::CritScope crit(&crit_sect_);
141 current_network_route_.connected = false;
142 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
143 kDummyTransportName, current_network_route_);
144 current_network_route_ = {};
145 send_net_ = nullptr;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200146}
147
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200148} // namespace test
149} // namespace webrtc