blob: e0f8f8ecad6e93715389012cd44ed510fb7f00e5 [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 {
21SimulatedNetwork::Config CreateSimulationConfig(NetworkNodeConfig config) {
22 SimulatedNetwork::Config sim_config;
23 sim_config.link_capacity_kbps = config.simulation.bandwidth.kbps_or(0);
24 sim_config.loss_percent = config.simulation.loss_rate * 100;
25 sim_config.queue_delay_ms = config.simulation.delay.ms();
26 sim_config.delay_standard_deviation_ms = config.simulation.delay_std_dev.ms();
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010027 sim_config.packet_overhead = config.packet_overhead.bytes<int>();
Sebastian Jansson2b08e312019-02-25 10:24:46 +010028 sim_config.codel_active_queue_management =
29 config.simulation.codel_active_queue_management;
Sebastian Jansson98b07e92018-09-27 13:47:01 +020030 return sim_config;
31}
32} // namespace
33
Artem Titov40f51152019-01-04 15:45:01 +010034void NullReceiver::OnPacketReceived(EmulatedIpPacket packet) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020035
36ActionReceiver::ActionReceiver(std::function<void()> action)
37 : action_(action) {}
38
Artem Titov40f51152019-01-04 15:45:01 +010039void ActionReceiver::OnPacketReceived(EmulatedIpPacket packet) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020040 action_();
Sebastian Jansson98b07e92018-09-27 13:47:01 +020041}
42
Sebastian Jansson98b07e92018-09-27 13:47:01 +020043std::unique_ptr<SimulationNode> SimulationNode::Create(
44 NetworkNodeConfig config) {
45 RTC_DCHECK(config.mode == NetworkNodeConfig::TrafficMode::kSimulation);
46 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
47 auto network = absl::make_unique<SimulatedNetwork>(sim_config);
48 SimulatedNetwork* simulation_ptr = network.get();
49 return std::unique_ptr<SimulationNode>(
50 new SimulationNode(config, std::move(network), simulation_ptr));
51}
52
53void SimulationNode::UpdateConfig(
54 std::function<void(NetworkNodeConfig*)> modifier) {
55 modifier(&config_);
56 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
57 simulated_network_->SetConfig(sim_config);
58}
59
60void SimulationNode::PauseTransmissionUntil(Timestamp until) {
61 simulated_network_->PauseTransmissionUntil(until.us());
62}
63
64ColumnPrinter SimulationNode::ConfigPrinter() const {
65 return ColumnPrinter::Lambda("propagation_delay capacity loss_rate",
66 [this](rtc::SimpleStringBuilder& sb) {
67 sb.AppendFormat(
68 "%.3lf %.0lf %.2lf",
69 config_.simulation.delay.seconds<double>(),
70 config_.simulation.bandwidth.bps() / 8.0,
71 config_.simulation.loss_rate);
72 });
73}
74
75SimulationNode::SimulationNode(
76 NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020077 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020078 SimulatedNetwork* simulation)
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010079 : EmulatedNetworkNode(std::move(behavior)),
Sebastian Jansson98b07e92018-09-27 13:47:01 +020080 simulated_network_(simulation),
81 config_(config) {}
82
Sebastian Janssonaa01f272019-01-30 11:28:59 +010083NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock,
Sebastian Jansson800e1212018-10-22 11:49:03 +020084 Call* sender_call)
85 : sender_clock_(sender_clock), sender_call_(sender_call) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020086
87NetworkNodeTransport::~NetworkNodeTransport() = default;
88
89bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
90 size_t length,
91 const PacketOptions& options) {
Sebastian Jansson800e1212018-10-22 11:49:03 +020092 int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
Sebastian Jansson156d11d2018-09-28 17:21:34 +020093 rtc::SentPacket sent_packet;
94 sent_packet.packet_id = options.packet_id;
Sebastian Jansson03789972018-10-09 18:27:57 +020095 sent_packet.info.included_in_feedback = options.included_in_feedback;
96 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 17:21:34 +020097 sent_packet.send_time_ms = send_time_ms;
98 sent_packet.info.packet_size_bytes = length;
99 sent_packet.info.packet_type = rtc::PacketType::kData;
Sebastian Jansson800e1212018-10-22 11:49:03 +0200100 sender_call_->OnSentPacket(sent_packet);
Sebastian Jansson156d11d2018-09-28 17:21:34 +0200101
102 Timestamp send_time = Timestamp::ms(send_time_ms);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200103 rtc::CritScope crit(&crit_sect_);
104 if (!send_net_)
105 return false;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200106 rtc::CopyOnWriteBuffer buffer(packet, length,
107 length + packet_overhead_.bytes());
108 buffer.SetSize(length + packet_overhead_.bytes());
Artem Titov4cd433e2019-04-01 11:01:16 +0200109 send_net_->OnPacketReceived(
110 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100111 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200112}
113
114bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
115 rtc::CopyOnWriteBuffer buffer(packet, length);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200116 Timestamp send_time = Timestamp::ms(sender_clock_->TimeInMilliseconds());
117 rtc::CritScope crit(&crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200118 buffer.SetSize(length + packet_overhead_.bytes());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200119 if (!send_net_)
120 return false;
Artem Titov4cd433e2019-04-01 11:01:16 +0200121 send_net_->OnPacketReceived(
122 EmulatedIpPacket(local_address_, receiver_address_, buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100123 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200124}
125
Artem Titov37d18482019-01-08 15:41:45 +0100126void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node,
Artem Titov4cd433e2019-04-01 11:01:16 +0200127 rtc::IPAddress receiver_ip,
Sebastian Jansson800e1212018-10-22 11:49:03 +0200128 DataSize packet_overhead) {
Artem Titov4cd433e2019-04-01 11:01:16 +0200129 // Only IPv4 address is supported. We don't use full range of IPs in scenario
130 // framework and also we need a simple way to convert IP into network_id
131 // to signal network route.
132 RTC_CHECK_EQ(receiver_ip.family(), AF_INET);
133 RTC_CHECK_LE(receiver_ip.v4AddressAsHostOrderInteger(),
134 std::numeric_limits<uint16_t>::max());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200135 rtc::CritScope crit(&crit_sect_);
136 send_net_ = send_node;
Artem Titov4cd433e2019-04-01 11:01:16 +0200137 receiver_address_ = rtc::SocketAddress(receiver_ip, 0);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200138 packet_overhead_ = packet_overhead;
139
140 rtc::NetworkRoute route;
141 route.connected = true;
Artem Titov4cd433e2019-04-01 11:01:16 +0200142 route.local_network_id =
143 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
144 route.remote_network_id =
145 static_cast<uint16_t>(receiver_ip.v4AddressAsHostOrderInteger());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200146 std::string transport_name = "dummy";
147 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
148 transport_name, route);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200149}
150
Artem Titov40f51152019-01-04 15:45:01 +0100151CrossTrafficSource::CrossTrafficSource(EmulatedNetworkReceiverInterface* target,
Artem Titov4cd433e2019-04-01 11:01:16 +0200152 rtc::IPAddress receiver_ip,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200153 CrossTrafficConfig config)
154 : target_(target),
Artem Titov4cd433e2019-04-01 11:01:16 +0200155 receiver_address_(receiver_ip, 0),
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200156 config_(config),
157 random_(config.random_seed) {}
158
159CrossTrafficSource::~CrossTrafficSource() = default;
160
161DataRate CrossTrafficSource::TrafficRate() const {
162 return config_.peak_rate * intensity_;
163}
164
165void CrossTrafficSource::Process(Timestamp at_time, TimeDelta delta) {
166 time_since_update_ += delta;
167 if (config_.mode == CrossTrafficConfig::Mode::kRandomWalk) {
168 if (time_since_update_ >= config_.random_walk.update_interval) {
169 intensity_ += random_.Gaussian(config_.random_walk.bias,
170 config_.random_walk.variance) *
171 time_since_update_.seconds<double>();
172 intensity_ = rtc::SafeClamp(intensity_, 0.0, 1.0);
173 time_since_update_ = TimeDelta::Zero();
174 }
175 } else if (config_.mode == CrossTrafficConfig::Mode::kPulsedPeaks) {
176 if (intensity_ == 0 && time_since_update_ >= config_.pulsed.hold_duration) {
177 intensity_ = 1;
178 time_since_update_ = TimeDelta::Zero();
179 } else if (intensity_ == 1 &&
180 time_since_update_ >= config_.pulsed.send_duration) {
181 intensity_ = 0;
182 time_since_update_ = TimeDelta::Zero();
183 }
184 }
185 pending_size_ += TrafficRate() * delta;
186 if (pending_size_ > config_.min_packet_size) {
Artem Titov40f51152019-01-04 15:45:01 +0100187 target_->OnPacketReceived(EmulatedIpPacket(
Artem Titov4cd433e2019-04-01 11:01:16 +0200188 rtc::SocketAddress() /*from*/, receiver_address_,
189 rtc::CopyOnWriteBuffer(pending_size_.bytes()), at_time));
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200190 pending_size_ = DataSize::Zero();
191 }
192}
193
194ColumnPrinter CrossTrafficSource::StatsPrinter() {
195 return ColumnPrinter::Lambda("cross_traffic_rate",
196 [this](rtc::SimpleStringBuilder& sb) {
197 sb.AppendFormat("%.0lf",
198 TrafficRate().bps() / 8.0);
199 },
200 32);
201}
202
203} // namespace test
204} // namespace webrtc