blob: 316a46bff677ab80145da1ebaf485c0798a87a4c [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 Jansson98b07e92018-09-27 13:47:01 +020028 return sim_config;
29}
30} // namespace
31
Artem Titov40f51152019-01-04 15:45:01 +010032void NullReceiver::OnPacketReceived(EmulatedIpPacket packet) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020033
34ActionReceiver::ActionReceiver(std::function<void()> action)
35 : action_(action) {}
36
Artem Titov40f51152019-01-04 15:45:01 +010037void ActionReceiver::OnPacketReceived(EmulatedIpPacket packet) {
Sebastian Jansson98b07e92018-09-27 13:47:01 +020038 action_();
Sebastian Jansson98b07e92018-09-27 13:47:01 +020039}
40
Sebastian Jansson98b07e92018-09-27 13:47:01 +020041std::unique_ptr<SimulationNode> SimulationNode::Create(
42 NetworkNodeConfig config) {
43 RTC_DCHECK(config.mode == NetworkNodeConfig::TrafficMode::kSimulation);
44 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
45 auto network = absl::make_unique<SimulatedNetwork>(sim_config);
46 SimulatedNetwork* simulation_ptr = network.get();
47 return std::unique_ptr<SimulationNode>(
48 new SimulationNode(config, std::move(network), simulation_ptr));
49}
50
51void SimulationNode::UpdateConfig(
52 std::function<void(NetworkNodeConfig*)> modifier) {
53 modifier(&config_);
54 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
55 simulated_network_->SetConfig(sim_config);
56}
57
58void SimulationNode::PauseTransmissionUntil(Timestamp until) {
59 simulated_network_->PauseTransmissionUntil(until.us());
60}
61
62ColumnPrinter SimulationNode::ConfigPrinter() const {
63 return ColumnPrinter::Lambda("propagation_delay capacity loss_rate",
64 [this](rtc::SimpleStringBuilder& sb) {
65 sb.AppendFormat(
66 "%.3lf %.0lf %.2lf",
67 config_.simulation.delay.seconds<double>(),
68 config_.simulation.bandwidth.bps() / 8.0,
69 config_.simulation.loss_rate);
70 });
71}
72
73SimulationNode::SimulationNode(
74 NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020075 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +020076 SimulatedNetwork* simulation)
Sebastian Jansson8c8feb92019-01-29 15:59:17 +010077 : EmulatedNetworkNode(std::move(behavior)),
Sebastian Jansson98b07e92018-09-27 13:47:01 +020078 simulated_network_(simulation),
79 config_(config) {}
80
Sebastian Jansson800e1212018-10-22 11:49:03 +020081NetworkNodeTransport::NetworkNodeTransport(const Clock* sender_clock,
82 Call* sender_call)
83 : sender_clock_(sender_clock), sender_call_(sender_call) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020084
85NetworkNodeTransport::~NetworkNodeTransport() = default;
86
87bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
88 size_t length,
89 const PacketOptions& options) {
Sebastian Jansson800e1212018-10-22 11:49:03 +020090 int64_t send_time_ms = sender_clock_->TimeInMilliseconds();
Sebastian Jansson156d11d2018-09-28 17:21:34 +020091 rtc::SentPacket sent_packet;
92 sent_packet.packet_id = options.packet_id;
Sebastian Jansson03789972018-10-09 18:27:57 +020093 sent_packet.info.included_in_feedback = options.included_in_feedback;
94 sent_packet.info.included_in_allocation = options.included_in_allocation;
Sebastian Jansson156d11d2018-09-28 17:21:34 +020095 sent_packet.send_time_ms = send_time_ms;
96 sent_packet.info.packet_size_bytes = length;
97 sent_packet.info.packet_type = rtc::PacketType::kData;
Sebastian Jansson800e1212018-10-22 11:49:03 +020098 sender_call_->OnSentPacket(sent_packet);
Sebastian Jansson156d11d2018-09-28 17:21:34 +020099
100 Timestamp send_time = Timestamp::ms(send_time_ms);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200101 rtc::CritScope crit(&crit_sect_);
102 if (!send_net_)
103 return false;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200104 rtc::CopyOnWriteBuffer buffer(packet, length,
105 length + packet_overhead_.bytes());
106 buffer.SetSize(length + packet_overhead_.bytes());
Artem Titov40f51152019-01-04 15:45:01 +0100107 send_net_->OnPacketReceived(EmulatedIpPacket(
108 rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, receiver_id_,
109 buffer, send_time));
Sebastian Janssonf65309c2018-12-20 10:26:00 +0100110 return true;
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200111}
112
113bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
114 rtc::CopyOnWriteBuffer buffer(packet, length);
Sebastian Jansson800e1212018-10-22 11:49:03 +0200115 Timestamp send_time = Timestamp::ms(sender_clock_->TimeInMilliseconds());
116 rtc::CritScope crit(&crit_sect_);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200117 buffer.SetSize(length + packet_overhead_.bytes());
Sebastian Jansson800e1212018-10-22 11:49:03 +0200118 if (!send_net_)
119 return false;
Artem Titov40f51152019-01-04 15:45:01 +0100120 send_net_->OnPacketReceived(EmulatedIpPacket(
121 rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, receiver_id_,
122 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,
Sebastian Jansson800e1212018-10-22 11:49:03 +0200127 uint64_t receiver_id,
128 DataSize packet_overhead) {
129 rtc::CritScope crit(&crit_sect_);
130 send_net_ = send_node;
131 receiver_id_ = receiver_id;
132 packet_overhead_ = packet_overhead;
133
134 rtc::NetworkRoute route;
135 route.connected = true;
136 route.local_network_id = receiver_id;
137 route.remote_network_id = receiver_id;
138 std::string transport_name = "dummy";
139 sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged(
140 transport_name, route);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200141}
142
Artem Titov40f51152019-01-04 15:45:01 +0100143CrossTrafficSource::CrossTrafficSource(EmulatedNetworkReceiverInterface* target,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200144 uint64_t receiver_id,
145 CrossTrafficConfig config)
146 : target_(target),
147 receiver_id_(receiver_id),
148 config_(config),
149 random_(config.random_seed) {}
150
151CrossTrafficSource::~CrossTrafficSource() = default;
152
153DataRate CrossTrafficSource::TrafficRate() const {
154 return config_.peak_rate * intensity_;
155}
156
157void CrossTrafficSource::Process(Timestamp at_time, TimeDelta delta) {
158 time_since_update_ += delta;
159 if (config_.mode == CrossTrafficConfig::Mode::kRandomWalk) {
160 if (time_since_update_ >= config_.random_walk.update_interval) {
161 intensity_ += random_.Gaussian(config_.random_walk.bias,
162 config_.random_walk.variance) *
163 time_since_update_.seconds<double>();
164 intensity_ = rtc::SafeClamp(intensity_, 0.0, 1.0);
165 time_since_update_ = TimeDelta::Zero();
166 }
167 } else if (config_.mode == CrossTrafficConfig::Mode::kPulsedPeaks) {
168 if (intensity_ == 0 && time_since_update_ >= config_.pulsed.hold_duration) {
169 intensity_ = 1;
170 time_since_update_ = TimeDelta::Zero();
171 } else if (intensity_ == 1 &&
172 time_since_update_ >= config_.pulsed.send_duration) {
173 intensity_ = 0;
174 time_since_update_ = TimeDelta::Zero();
175 }
176 }
177 pending_size_ += TrafficRate() * delta;
178 if (pending_size_ > config_.min_packet_size) {
Artem Titov40f51152019-01-04 15:45:01 +0100179 target_->OnPacketReceived(EmulatedIpPacket(
180 rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/,
181 receiver_id_, rtc::CopyOnWriteBuffer(pending_size_.bytes()), at_time));
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200182 pending_size_ = DataSize::Zero();
183 }
184}
185
186ColumnPrinter CrossTrafficSource::StatsPrinter() {
187 return ColumnPrinter::Lambda("cross_traffic_rate",
188 [this](rtc::SimpleStringBuilder& sb) {
189 sb.AppendFormat("%.0lf",
190 TrafficRate().bps() / 8.0);
191 },
192 32);
193}
194
195} // namespace test
196} // namespace webrtc