blob: eb215aa63c6e526e648766354cef66a789e9dc4b [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,
47 std::unique_ptr<NetworkSimulationInterface> simulation)
48 : packet_overhead_(config.packet_overhead.bytes()),
49 simulation_(std::move(simulation)) {}
50
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_++;
69 bool sent = simulation_->EnqueuePacket(PacketInFlightInfo(
70 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_);
81 absl::optional<int64_t> delivery_us = simulation_->NextDeliveryTimeUs();
82 if (delivery_us && *delivery_us > at_time.us())
83 return;
84
85 delivery_infos = simulation_->DequeueDeliverablePackets(at_time.us());
86 }
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,
165 std::unique_ptr<NetworkSimulationInterface> behavior,
166 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) {
185 sender_->call_->OnSentPacket(rtc::SentPacket(
186 options.packet_id, sender_->clock_->TimeInMilliseconds()));
187 Timestamp send_time = Timestamp::ms(sender_->clock_->TimeInMilliseconds());
188 rtc::CopyOnWriteBuffer buffer(packet, length,
189 length + packet_overhead_.bytes());
190 buffer.SetSize(length + packet_overhead_.bytes());
191 return send_net_->TryDeliverPacket(buffer, receiver_id_, send_time);
192}
193
194bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) {
195 rtc::CopyOnWriteBuffer buffer(packet, length);
196 Timestamp send_time = Timestamp::ms(sender_->clock_->TimeInMilliseconds());
197 buffer.SetSize(length + packet_overhead_.bytes());
198 return send_net_->TryDeliverPacket(buffer, receiver_id_, send_time);
199}
200
201uint64_t NetworkNodeTransport::ReceiverId() const {
202 return receiver_id_;
203}
204
205CrossTrafficSource::CrossTrafficSource(NetworkReceiverInterface* target,
206 uint64_t receiver_id,
207 CrossTrafficConfig config)
208 : target_(target),
209 receiver_id_(receiver_id),
210 config_(config),
211 random_(config.random_seed) {}
212
213CrossTrafficSource::~CrossTrafficSource() = default;
214
215DataRate CrossTrafficSource::TrafficRate() const {
216 return config_.peak_rate * intensity_;
217}
218
219void CrossTrafficSource::Process(Timestamp at_time, TimeDelta delta) {
220 time_since_update_ += delta;
221 if (config_.mode == CrossTrafficConfig::Mode::kRandomWalk) {
222 if (time_since_update_ >= config_.random_walk.update_interval) {
223 intensity_ += random_.Gaussian(config_.random_walk.bias,
224 config_.random_walk.variance) *
225 time_since_update_.seconds<double>();
226 intensity_ = rtc::SafeClamp(intensity_, 0.0, 1.0);
227 time_since_update_ = TimeDelta::Zero();
228 }
229 } else if (config_.mode == CrossTrafficConfig::Mode::kPulsedPeaks) {
230 if (intensity_ == 0 && time_since_update_ >= config_.pulsed.hold_duration) {
231 intensity_ = 1;
232 time_since_update_ = TimeDelta::Zero();
233 } else if (intensity_ == 1 &&
234 time_since_update_ >= config_.pulsed.send_duration) {
235 intensity_ = 0;
236 time_since_update_ = TimeDelta::Zero();
237 }
238 }
239 pending_size_ += TrafficRate() * delta;
240 if (pending_size_ > config_.min_packet_size) {
241 target_->TryDeliverPacket(rtc::CopyOnWriteBuffer(pending_size_.bytes()),
242 receiver_id_, at_time);
243 pending_size_ = DataSize::Zero();
244 }
245}
246
247ColumnPrinter CrossTrafficSource::StatsPrinter() {
248 return ColumnPrinter::Lambda("cross_traffic_rate",
249 [this](rtc::SimpleStringBuilder& sb) {
250 sb.AppendFormat("%.0lf",
251 TrafficRate().bps() / 8.0);
252 },
253 32);
254}
255
256} // namespace test
257} // namespace webrtc