blob: ff138939abb1efd51c1ae4a58ba179500dddbaca [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
15namespace webrtc {
16namespace test {
17namespace {
18SimulatedNetwork::Config CreateSimulationConfig(NetworkNodeConfig config) {
19 SimulatedNetwork::Config sim_config;
20 sim_config.link_capacity_kbps = config.simulation.bandwidth.kbps_or(0);
21 sim_config.loss_percent = config.simulation.loss_rate * 100;
22 sim_config.queue_delay_ms = config.simulation.delay.ms();
23 sim_config.delay_standard_deviation_ms = config.simulation.delay_std_dev.ms();
24 return sim_config;
25}
26} // namespace
27
28bool NullReceiver::TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
29 uint64_t receiver,
30 Timestamp at_time) {
31 return true;
32}
33
34ActionReceiver::ActionReceiver(std::function<void()> action)
35 : action_(action) {}
36
37bool ActionReceiver::TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
38 uint64_t receiver,
39 Timestamp at_time) {
40 action_();
41 return true;
42}
43
44NetworkNode::~NetworkNode() = default;
45
46NetworkNode::NetworkNode(NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +020047 std::unique_ptr<NetworkBehaviorInterface> behavior)
Sebastian Jansson98b07e92018-09-27 13:47:01 +020048 : packet_overhead_(config.packet_overhead.bytes()),
Artem Titov8ea1e9d2018-10-04 14:46:31 +020049 behavior_(std::move(behavior)) {}
Sebastian Jansson98b07e92018-09-27 13:47:01 +020050
51void NetworkNode::SetRoute(uint64_t receiver, NetworkReceiverInterface* node) {
52 rtc::CritScope crit(&crit_sect_);
53 routing_[receiver] = node;
54}
55
56void NetworkNode::ClearRoute(uint64_t receiver_id) {
57 rtc::CritScope crit(&crit_sect_);
58 auto it = routing_.find(receiver_id);
59 routing_.erase(it);
60}
61
62bool NetworkNode::TryDeliverPacket(rtc::CopyOnWriteBuffer packet,
63 uint64_t receiver,
64 Timestamp at_time) {
65 rtc::CritScope crit(&crit_sect_);
66 if (routing_.find(receiver) == routing_.end())
67 return false;
68 uint64_t packet_id = next_packet_id_++;
Artem Titov8ea1e9d2018-10-04 14:46:31 +020069 bool sent = behavior_->EnqueuePacket(PacketInFlightInfo(
Sebastian Jansson98b07e92018-09-27 13:47:01 +020070 packet.size() + packet_overhead_, at_time.us(), packet_id));
71 if (sent) {
72 packets_.emplace_back(StoredPacket{packet, receiver, packet_id, false});
73 }
74 return sent;
75}
76
77void NetworkNode::Process(Timestamp at_time) {
78 std::vector<PacketDeliveryInfo> delivery_infos;
79 {
80 rtc::CritScope crit(&crit_sect_);
Artem Titov8ea1e9d2018-10-04 14:46:31 +020081 absl::optional<int64_t> delivery_us = behavior_->NextDeliveryTimeUs();
Sebastian Jansson98b07e92018-09-27 13:47:01 +020082 if (delivery_us && *delivery_us > at_time.us())
83 return;
84
Artem Titov8ea1e9d2018-10-04 14:46:31 +020085 delivery_infos = behavior_->DequeueDeliverablePackets(at_time.us());
Sebastian Jansson98b07e92018-09-27 13:47:01 +020086 }
87 for (PacketDeliveryInfo& delivery_info : delivery_infos) {
88 StoredPacket* packet = nullptr;
89 NetworkReceiverInterface* receiver = nullptr;
90 {
91 rtc::CritScope crit(&crit_sect_);
92 for (StoredPacket& stored_packet : packets_) {
93 if (stored_packet.id == delivery_info.packet_id) {
94 packet = &stored_packet;
95 break;
96 }
97 }
98 RTC_CHECK(packet);
99 RTC_DCHECK(!packet->removed);
100 receiver = routing_[packet->receiver_id];
101 packet->removed = true;
102 }
103 // We don't want to keep the lock here. Otherwise we would get a deadlock if
104 // the receiver tries to push a new packet.
105 receiver->TryDeliverPacket(packet->packet_data, packet->receiver_id,
106 at_time);
107 {
108 rtc::CritScope crit(&crit_sect_);
109 while (!packets_.empty() && packets_.front().removed) {
110 packets_.pop_front();
111 }
112 }
113 }
114}
115
116void NetworkNode::Route(int64_t receiver_id,
117 std::vector<NetworkNode*> nodes,
118 NetworkReceiverInterface* receiver) {
119 RTC_CHECK(!nodes.empty());
120 for (size_t i = 0; i + 1 < nodes.size(); ++i)
121 nodes[i]->SetRoute(receiver_id, nodes[i + 1]);
122 nodes.back()->SetRoute(receiver_id, receiver);
123}
124
125void NetworkNode::ClearRoute(int64_t receiver_id,
126 std::vector<NetworkNode*> nodes) {
127 for (NetworkNode* node : nodes)
128 node->ClearRoute(receiver_id);
129}
130
131std::unique_ptr<SimulationNode> SimulationNode::Create(
132 NetworkNodeConfig config) {
133 RTC_DCHECK(config.mode == NetworkNodeConfig::TrafficMode::kSimulation);
134 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config);
135 auto network = absl::make_unique<SimulatedNetwork>(sim_config);
136 SimulatedNetwork* simulation_ptr = network.get();
137 return std::unique_ptr<SimulationNode>(
138 new SimulationNode(config, std::move(network), simulation_ptr));
139}
140
141void SimulationNode::UpdateConfig(
142 std::function<void(NetworkNodeConfig*)> modifier) {
143 modifier(&config_);
144 SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_);
145 simulated_network_->SetConfig(sim_config);
146}
147
148void SimulationNode::PauseTransmissionUntil(Timestamp until) {
149 simulated_network_->PauseTransmissionUntil(until.us());
150}
151
152ColumnPrinter SimulationNode::ConfigPrinter() const {
153 return ColumnPrinter::Lambda("propagation_delay capacity loss_rate",
154 [this](rtc::SimpleStringBuilder& sb) {
155 sb.AppendFormat(
156 "%.3lf %.0lf %.2lf",
157 config_.simulation.delay.seconds<double>(),
158 config_.simulation.bandwidth.bps() / 8.0,
159 config_.simulation.loss_rate);
160 });
161}
162
163SimulationNode::SimulationNode(
164 NetworkNodeConfig config,
Artem Titov8ea1e9d2018-10-04 14:46:31 +0200165 std::unique_ptr<NetworkBehaviorInterface> behavior,
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200166 SimulatedNetwork* simulation)
167 : NetworkNode(config, std::move(behavior)),
168 simulated_network_(simulation),
169 config_(config) {}
170
171NetworkNodeTransport::NetworkNodeTransport(CallClient* sender,
172 NetworkNode* send_net,
173 uint64_t receiver,
174 DataSize packet_overhead)
175 : sender_(sender),
176 send_net_(send_net),
177 receiver_id_(receiver),
178 packet_overhead_(packet_overhead) {}
179
180NetworkNodeTransport::~NetworkNodeTransport() = default;
181
182bool NetworkNodeTransport::SendRtp(const uint8_t* packet,
183 size_t length,
184 const PacketOptions& options) {
Sebastian Jansson156d11d2018-09-28 17:21:34 +0200185 int64_t send_time_ms = sender_->clock_->TimeInMilliseconds();
186 rtc::SentPacket sent_packet;
187 sent_packet.packet_id = options.packet_id;
188 sent_packet.send_time_ms = send_time_ms;
189 sent_packet.info.packet_size_bytes = length;
190 sent_packet.info.packet_type = rtc::PacketType::kData;
191 sender_->call_->OnSentPacket(sent_packet);
192
193 Timestamp send_time = Timestamp::ms(send_time_ms);
Sebastian Jansson98b07e92018-09-27 13:47:01 +0200194 rtc::CopyOnWriteBuffer buffer(packet, length,
195 length + packet_overhead_.bytes());
196 buffer.SetSize(length + packet_overhead_.bytes());
197 return send_net_->TryDeliverPacket(buffer, receiver_id_, send_time);
198}
199
200bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
201 rtc::CopyOnWriteBuffer buffer(packet, length);
202 Timestamp send_time = Timestamp::ms(sender_->clock_->TimeInMilliseconds());
203 buffer.SetSize(length + packet_overhead_.bytes());
204 return send_net_->TryDeliverPacket(buffer, receiver_id_, send_time);
205}
206
207uint64_t NetworkNodeTransport::ReceiverId() const {
208 return receiver_id_;
209}
210
211CrossTrafficSource::CrossTrafficSource(NetworkReceiverInterface* target,
212 uint64_t receiver_id,
213 CrossTrafficConfig config)
214 : target_(target),
215 receiver_id_(receiver_id),
216 config_(config),
217 random_(config.random_seed) {}
218
219CrossTrafficSource::~CrossTrafficSource() = default;
220
221DataRate CrossTrafficSource::TrafficRate() const {
222 return config_.peak_rate * intensity_;
223}
224
225void CrossTrafficSource::Process(Timestamp at_time, TimeDelta delta) {
226 time_since_update_ += delta;
227 if (config_.mode == CrossTrafficConfig::Mode::kRandomWalk) {
228 if (time_since_update_ >= config_.random_walk.update_interval) {
229 intensity_ += random_.Gaussian(config_.random_walk.bias,
230 config_.random_walk.variance) *
231 time_since_update_.seconds<double>();
232 intensity_ = rtc::SafeClamp(intensity_, 0.0, 1.0);
233 time_since_update_ = TimeDelta::Zero();
234 }
235 } else if (config_.mode == CrossTrafficConfig::Mode::kPulsedPeaks) {
236 if (intensity_ == 0 && time_since_update_ >= config_.pulsed.hold_duration) {
237 intensity_ = 1;
238 time_since_update_ = TimeDelta::Zero();
239 } else if (intensity_ == 1 &&
240 time_since_update_ >= config_.pulsed.send_duration) {
241 intensity_ = 0;
242 time_since_update_ = TimeDelta::Zero();
243 }
244 }
245 pending_size_ += TrafficRate() * delta;
246 if (pending_size_ > config_.min_packet_size) {
247 target_->TryDeliverPacket(rtc::CopyOnWriteBuffer(pending_size_.bytes()),
248 receiver_id_, at_time);
249 pending_size_ = DataSize::Zero();
250 }
251}
252
253ColumnPrinter CrossTrafficSource::StatsPrinter() {
254 return ColumnPrinter::Lambda("cross_traffic_rate",
255 [this](rtc::SimpleStringBuilder& sb) {
256 sb.AppendFormat("%.0lf",
257 TrafficRate().bps() / 8.0);
258 },
259 32);
260}
261
262} // namespace test
263} // namespace webrtc