blob: a206a5f6efbcb9d05582185a4e47f7df782ac0fd [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 Titov40f51152019-01-04 15:45:01 +0100109 send_net_->OnPacketReceived(EmulatedIpPacket(
110 rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, receiver_id_,
111 buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100112 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200113}
114
115bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
116 rtc::CopyOnWriteBuffer buffer(packet, length);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200117 Timestamp send_time = Timestamp::ms(sender_clock_->TimeInMilliseconds());
118 rtc::CritScope crit(&crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200119 buffer.SetSize(length + packet_overhead_.bytes());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200120 if (!send_net_)
121 return false;
Artem Titov40f51152019-01-04 15:45:01 +0100122 send_net_->OnPacketReceived(EmulatedIpPacket(
123 rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, receiver_id_,
124 buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100125 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200126}
127
Artem Titov37d18482019-01-08 15:41:45 +0100128void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node,
Sebastian Jansson800e1212018-10-22 11:49:03 +0200129 uint64_t receiver_id,
130 DataSize packet_overhead) {
131 rtc::CritScope crit(&crit_sect_);
132 send_net_ = send_node;
133 receiver_id_ = receiver_id;
134 packet_overhead_ = packet_overhead;
135
136 rtc::NetworkRoute route;
137 route.connected = true;
138 route.local_network_id = receiver_id;
139 route.remote_network_id = receiver_id;
140 std::string transport_name = "dummy";
141 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
142 transport_name, route);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200143}
144
Artem Titov40f51152019-01-04 15:45:01 +0100145CrossTrafficSource::CrossTrafficSource(EmulatedNetworkReceiverInterface* target,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200146 uint64_t receiver_id,
147 CrossTrafficConfig config)
148 : target_(target),
149 receiver_id_(receiver_id),
150 config_(config),
151 random_(config.random_seed) {}
152
153CrossTrafficSource::~CrossTrafficSource() = default;
154
155DataRate CrossTrafficSource::TrafficRate() const {
156 return config_.peak_rate * intensity_;
157}
158
159void CrossTrafficSource::Process(Timestamp at_time, TimeDelta delta) {
160 time_since_update_ += delta;
161 if (config_.mode == CrossTrafficConfig::Mode::kRandomWalk) {
162 if (time_since_update_ >= config_.random_walk.update_interval) {
163 intensity_ += random_.Gaussian(config_.random_walk.bias,
164 config_.random_walk.variance) *
165 time_since_update_.seconds<double>();
166 intensity_ = rtc::SafeClamp(intensity_, 0.0, 1.0);
167 time_since_update_ = TimeDelta::Zero();
168 }
169 } else if (config_.mode == CrossTrafficConfig::Mode::kPulsedPeaks) {
170 if (intensity_ == 0 && time_since_update_ >= config_.pulsed.hold_duration) {
171 intensity_ = 1;
172 time_since_update_ = TimeDelta::Zero();
173 } else if (intensity_ == 1 &&
174 time_since_update_ >= config_.pulsed.send_duration) {
175 intensity_ = 0;
176 time_since_update_ = TimeDelta::Zero();
177 }
178 }
179 pending_size_ += TrafficRate() * delta;
180 if (pending_size_ > config_.min_packet_size) {
Artem Titov40f51152019-01-04 15:45:01 +0100181 target_->OnPacketReceived(EmulatedIpPacket(
182 rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/,
183 receiver_id_, rtc::CopyOnWriteBuffer(pending_size_.bytes()), at_time));
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200184 pending_size_ = DataSize::Zero();
185 }
186}
187
188ColumnPrinter CrossTrafficSource::StatsPrinter() {
189 return ColumnPrinter::Lambda("cross_traffic_rate",
190 [this](rtc::SimpleStringBuilder& sb) {
191 sb.AppendFormat("%.0lf",
192 TrafficRate().bps() / 8.0);
193 },
194 32);
195}
196
197} // namespace test
198} // namespace webrtc