blob: b936fcb60dec838349e93d90b39cad1a0d549e66 [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 Jansson98b07e92018-09-27 13:47:01 +020032 return sim_config;
33}
34} // namespace
35
Sebastian Janssona4c22b92019-04-15 21:10:00 +020036SimulationNode::SimulationNode(NetworkSimulationConfig config,
37 SimulatedNetwork* behavior,
38 EmulatedNetworkNode* network_node)
39 : config_(config), simulation_(behavior), network_node_(network_node) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020040
Sebastian Janssona4c22b92019-04-15 21:10:00 +020041std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior(
Sebastian Janssonef86d142019-04-15 14:42:42 +020042 NetworkSimulationConfig config) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020043 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
Mirko Bonadei317a1f02019-09-17 17:06:18 +020044 return std::make_unique<SimulatedNetwork>(sim_config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020045}
46
47void SimulationNode::UpdateConfig(
Sebastian Janssonef86d142019-04-15 14:42:42 +020048 std::function<void(NetworkSimulationConfig*)> modifier) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020049 modifier(&config_);
50 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
Sebastian Janssona4c22b92019-04-15 21:10:00 +020051 simulation_->SetConfig(sim_config);
Sebastian Jansson98b07e92018-09-27 13:47:01 +020052}
53
54void SimulationNode::PauseTransmissionUntil(Timestamp until) {
Sebastian Janssona4c22b92019-04-15 21:10:00 +020055 simulation_->PauseTransmissionUntil(until.us());
Sebastian Jansson98b07e92018-09-27 13:47:01 +020056}
57
58ColumnPrinter SimulationNode::ConfigPrinter() const {
Sebastian Janssonef86d142019-04-15 14:42:42 +020059 return ColumnPrinter::Lambda(
60 "propagation_delay capacity loss_rate",
61 [this](rtc::SimpleStringBuilder& sb) {
62 sb.AppendFormat("%.3lf %.0lf %.2lf", config_.delay.seconds<double>(),
63 config_.bandwidth.bps() / 8.0, config_.loss_rate);
64 });
Sebastian Jansson98b07e92018-09-27 13:47:01 +020065}
66
Sebastian Janssonaa01f272019-01-30 11:28:59 +010067NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
Sebastian Jansson800e1212018-10-22 11:49:03 +020068 Call* sender_call)
69 : sender_clock_(sender_clock), sender_call_(sender_call) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020070
71NetworkNodeTransport::~NetworkNodeTransport() = default;
72
73bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
74 size_t length,
75 const PacketOptions& options) {
Sebastian Jansson800e1212018-10-22 11:49:03 +020076 int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
Sebastian Jansson156d11d2018-09-28 17:21:34 +020077 rtc::SentPacket sent_packet;
78 sent_packet.packet_id = options.packet_id;
Sebastian Jansson03789972018-10-09 18:27:57 +020079 sent_packet.info.included_in_feedback = options.included_in_feedback;
80 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 17:21:34 +020081 sent_packet.send_time_ms = send_time_ms;
82 sent_packet.info.packet_size_bytes = length;
83 sent_packet.info.packet_type = rtc::PacketType::kData;
Sebastian Jansson800e1212018-10-22 11:49:03 +020084 sender_call_->OnSentPacket(sent_packet);
Sebastian Jansson156d11d2018-09-28 17:21:34 +020085
86 Timestamp send_time = Timestamp::ms(send_time_ms);
Sebastian Jansson800e1212018-10-22 11:49:03 +020087 rtc::CritScope crit(&crit_sect_);
88 if (!send_net_)
89 return false;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020090 rtc::CopyOnWriteBuffer buffer(packet, length,
91 length + packet_overhead_.bytes());
92 buffer.SetSize(length + packet_overhead_.bytes());
Artem Titov4cd433e2019-04-01 11:01:16 +020093 send_net_->OnPacketReceived(
94 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +010095 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020096}
97
98bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
99 rtc::CopyOnWriteBuffer buffer(packet, length);
Sebastian Janssonb64ad0e2019-06-19 09:39:34 +0200100 Timestamp send_time = sender_clock_->CurrentTime();
Sebastian Jansson800e1212018-10-22 11:49:03 +0200101 rtc::CritScope crit(&crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200102 buffer.SetSize(length + packet_overhead_.bytes());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200103 if (!send_net_)
104 return false;
Artem Titov4cd433e2019-04-01 11:01:16 +0200105 send_net_->OnPacketReceived(
106 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100107 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200108}
109
Artem Titov37d18482019-01-08 15:41:45 +0100110void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node,
Artem Titov4cd433e2019-04-01 11:01:16 +0200111 rtc::IPAddress receiver_ip,
Sebastian Jansson800e1212018-10-22 11:49:03 +0200112 DataSize packet_overhead) {
Sebastian Jansson800e1212018-10-22 11:49:03 +0200113 rtc::NetworkRoute route;
114 route.connected = true;
Artem Titov4cd433e2019-04-01 11:01:16 +0200115 route.local_network_id =
116 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
117 route.remote_network_id =
118 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200119 {
120 // Only IPv4 address is supported. We don't use full range of IPs in
121 // scenario framework and also we need a simple way to convert IP into
122 // network_id to signal network route.
123 RTC_CHECK_EQ(receiver_ip.family(), AF_INET);
124 RTC_CHECK_LE(receiver_ip.v4AddressAsHostOrderInteger(),
125 std::numeric_limits<uint16_t>::max());
126 rtc::CritScope crit(&crit_sect_);
127 send_net_ = send_node;
128 receiver_address_ = rtc::SocketAddress(receiver_ip, 0);
129 packet_overhead_ = packet_overhead;
130 current_network_route_ = route;
131 }
132
Sebastian Jansson800e1212018-10-22 11:49:03 +0200133 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200134 kDummyTransportName, route);
135}
136
137void NetworkNodeTransport::Disconnect() {
138 rtc::CritScope crit(&crit_sect_);
139 current_network_route_.connected = false;
140 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
141 kDummyTransportName, current_network_route_);
142 current_network_route_ = {};
143 send_net_ = nullptr;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200144}
145
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200146} // namespace test
147} // namespace webrtc