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