blob: 1fc9655fa4d0906223e5f2678b073ae959419278 [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
Steve Anton40d55332019-01-07 10:21:47 -080015#include "absl/memory/memory.h"
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);
Sebastian Janssona4c22b92019-04-15 21:10:00 +020044 return absl::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 Jansson98b07e92018-09-27 13:47:01 +020067
Sebastian Janssonaa01f272019-01-30 11:28:59 +010068NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
Sebastian Jansson800e1212018-10-22 11:49:03 +020069 Call* sender_call)
70 : sender_clock_(sender_clock), sender_call_(sender_call) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020071
72NetworkNodeTransport::~NetworkNodeTransport() = default;
73
74bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
75 size_t length,
76 const PacketOptions& options) {
Sebastian Jansson800e1212018-10-22 11:49:03 +020077 int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
Sebastian Jansson156d11d2018-09-28 17:21:34 +020078 rtc::SentPacket sent_packet;
79 sent_packet.packet_id = options.packet_id;
Sebastian Jansson03789972018-10-09 18:27:57 +020080 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 17:21:34 +020082 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;
Sebastian Jansson800e1212018-10-22 11:49:03 +020085 sender_call_->OnSentPacket(sent_packet);
Sebastian Jansson156d11d2018-09-28 17:21:34 +020086
87 Timestamp send_time = Timestamp::ms(send_time_ms);
Sebastian Jansson800e1212018-10-22 11:49:03 +020088 rtc::CritScope crit(&crit_sect_);
89 if (!send_net_)
90 return false;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020091 rtc::CopyOnWriteBuffer buffer(packet, length,
92 length + packet_overhead_.bytes());
93 buffer.SetSize(length + packet_overhead_.bytes());
Artem Titov4cd433e2019-04-01 11:01:16 +020094 send_net_->OnPacketReceived(
95 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +010096 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020097}
98
99bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
100 rtc::CopyOnWriteBuffer buffer(packet, length);
Sebastian Janssonb64ad0e2019-06-19 09:39:34 +0200101 Timestamp send_time = sender_clock_->CurrentTime();
Sebastian Jansson800e1212018-10-22 11:49:03 +0200102 rtc::CritScope crit(&crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200103 buffer.SetSize(length + packet_overhead_.bytes());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200104 if (!send_net_)
105 return false;
Artem Titov4cd433e2019-04-01 11:01:16 +0200106 send_net_->OnPacketReceived(
107 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100108 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200109}
110
Artem Titov37d18482019-01-08 15:41:45 +0100111void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node,
Artem Titov4cd433e2019-04-01 11:01:16 +0200112 rtc::IPAddress receiver_ip,
Sebastian Jansson800e1212018-10-22 11:49:03 +0200113 DataSize packet_overhead) {
Sebastian Jansson800e1212018-10-22 11:49:03 +0200114 rtc::NetworkRoute route;
115 route.connected = true;
Artem Titov4cd433e2019-04-01 11:01:16 +0200116 route.local_network_id =
117 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
118 route.remote_network_id =
119 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200120 {
121 // Only IPv4 address is supported. We don't use full range of IPs in
122 // scenario framework and also we need a simple way to convert IP into
123 // network_id to signal network route.
124 RTC_CHECK_EQ(receiver_ip.family(), AF_INET);
125 RTC_CHECK_LE(receiver_ip.v4AddressAsHostOrderInteger(),
126 std::numeric_limits<uint16_t>::max());
127 rtc::CritScope crit(&crit_sect_);
128 send_net_ = send_node;
129 receiver_address_ = rtc::SocketAddress(receiver_ip, 0);
130 packet_overhead_ = packet_overhead;
131 current_network_route_ = route;
132 }
133
Sebastian Jansson800e1212018-10-22 11:49:03 +0200134 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
Sebastian Jansson4124dab2019-04-01 14:33:53 +0200135 kDummyTransportName, route);
136}
137
138void NetworkNodeTransport::Disconnect() {
139 rtc::CritScope crit(&crit_sect_);
140 current_network_route_.connected = false;
141 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
142 kDummyTransportName, current_network_route_);
143 current_network_route_ = {};
144 send_net_ = nullptr;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200145}
146
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200147} // namespace test
148} // namespace webrtc