Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 1 | /* |
| 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 Anton | 40d5533 | 2019-01-07 10:21:47 -0800 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 16 | #include "rtc_base/numerics/safe_minmax.h" |
| 17 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | namespace { |
| 21 | SimulatedNetwork::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 Jansson | 8c8feb9 | 2019-01-29 15:59:17 +0100 | [diff] [blame] | 27 | sim_config.packet_overhead = config.packet_overhead.bytes<int>(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 28 | return sim_config; |
| 29 | } |
| 30 | } // namespace |
| 31 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 32 | void NullReceiver::OnPacketReceived(EmulatedIpPacket packet) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 33 | |
| 34 | ActionReceiver::ActionReceiver(std::function<void()> action) |
| 35 | : action_(action) {} |
| 36 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 37 | void ActionReceiver::OnPacketReceived(EmulatedIpPacket packet) { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 38 | action_(); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 41 | std::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 | |
| 51 | void 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 | |
| 58 | void SimulationNode::PauseTransmissionUntil(Timestamp until) { |
| 59 | simulated_network_->PauseTransmissionUntil(until.us()); |
| 60 | } |
| 61 | |
| 62 | ColumnPrinter 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 | |
| 73 | SimulationNode::SimulationNode( |
| 74 | NetworkNodeConfig config, |
Artem Titov | 8ea1e9d | 2018-10-04 14:46:31 +0200 | [diff] [blame] | 75 | std::unique_ptr<NetworkBehaviorInterface> behavior, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 76 | SimulatedNetwork* simulation) |
Sebastian Jansson | 8c8feb9 | 2019-01-29 15:59:17 +0100 | [diff] [blame] | 77 | : EmulatedNetworkNode(std::move(behavior)), |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 78 | simulated_network_(simulation), |
| 79 | config_(config) {} |
| 80 | |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 81 | NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 82 | Call* sender_call) |
| 83 | : sender_clock_(sender_clock), sender_call_(sender_call) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 84 | |
| 85 | NetworkNodeTransport::~NetworkNodeTransport() = default; |
| 86 | |
| 87 | bool NetworkNodeTransport::SendRtp(const uint8_t* packet, |
| 88 | size_t length, |
| 89 | const PacketOptions& options) { |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 90 | int64_t send_time_ms = sender_clock_->TimeInMilliseconds(); |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 91 | rtc::SentPacket sent_packet; |
| 92 | sent_packet.packet_id = options.packet_id; |
Sebastian Jansson | 0378997 | 2018-10-09 18:27:57 +0200 | [diff] [blame] | 93 | sent_packet.info.included_in_feedback = options.included_in_feedback; |
| 94 | sent_packet.info.included_in_allocation = options.included_in_allocation; |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 95 | 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 Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 98 | sender_call_->OnSentPacket(sent_packet); |
Sebastian Jansson | 156d11d | 2018-09-28 17:21:34 +0200 | [diff] [blame] | 99 | |
| 100 | Timestamp send_time = Timestamp::ms(send_time_ms); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 101 | rtc::CritScope crit(&crit_sect_); |
| 102 | if (!send_net_) |
| 103 | return false; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 104 | rtc::CopyOnWriteBuffer buffer(packet, length, |
| 105 | length + packet_overhead_.bytes()); |
| 106 | buffer.SetSize(length + packet_overhead_.bytes()); |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 107 | send_net_->OnPacketReceived(EmulatedIpPacket( |
| 108 | rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, receiver_id_, |
| 109 | buffer, send_time)); |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 110 | return true; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool NetworkNodeTransport::SendRtcp(const uint8_t* packet, size_t length) { |
| 114 | rtc::CopyOnWriteBuffer buffer(packet, length); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 115 | Timestamp send_time = Timestamp::ms(sender_clock_->TimeInMilliseconds()); |
| 116 | rtc::CritScope crit(&crit_sect_); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 117 | buffer.SetSize(length + packet_overhead_.bytes()); |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 118 | if (!send_net_) |
| 119 | return false; |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 120 | send_net_->OnPacketReceived(EmulatedIpPacket( |
| 121 | rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, receiver_id_, |
| 122 | buffer, send_time)); |
Sebastian Jansson | f65309c | 2018-12-20 10:26:00 +0100 | [diff] [blame] | 123 | return true; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Artem Titov | 37d1848 | 2019-01-08 15:41:45 +0100 | [diff] [blame] | 126 | void NetworkNodeTransport::Connect(EmulatedNetworkNode* send_node, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 127 | 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 Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Artem Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 143 | CrossTrafficSource::CrossTrafficSource(EmulatedNetworkReceiverInterface* target, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 144 | uint64_t receiver_id, |
| 145 | CrossTrafficConfig config) |
| 146 | : target_(target), |
| 147 | receiver_id_(receiver_id), |
| 148 | config_(config), |
| 149 | random_(config.random_seed) {} |
| 150 | |
| 151 | CrossTrafficSource::~CrossTrafficSource() = default; |
| 152 | |
| 153 | DataRate CrossTrafficSource::TrafficRate() const { |
| 154 | return config_.peak_rate * intensity_; |
| 155 | } |
| 156 | |
| 157 | void 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 Titov | 40f5115 | 2019-01-04 15:45:01 +0100 | [diff] [blame] | 179 | target_->OnPacketReceived(EmulatedIpPacket( |
| 180 | rtc::SocketAddress() /*from*/, rtc::SocketAddress() /*to*/, |
| 181 | receiver_id_, rtc::CopyOnWriteBuffer(pending_size_.bytes()), at_time)); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 182 | pending_size_ = DataSize::Zero(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | ColumnPrinter 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 |