Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [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> |
Evan Shrubsole | 02bdf66 | 2023-03-02 12:52:50 | [diff] [blame] | 13 | #include <memory> |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Evan Shrubsole | 02bdf66 | 2023-03-02 12:52:50 | [diff] [blame] | 16 | #include "absl/cleanup/cleanup.h" |
Sebastian Jansson | f52d3ed | 2020-02-28 15:22:09 | [diff] [blame] | 17 | #include "rtc_base/net_helper.h" |
Yves Gerey | 2e00abc | 2018-10-05 13:39:24 | [diff] [blame] | 18 | #include "rtc_base/numerics/safe_minmax.h" |
| 19 | |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | namespace test { |
| 22 | namespace { |
Sebastian Jansson | 4124dab | 2019-04-01 12:33:53 | [diff] [blame] | 23 | constexpr char kDummyTransportName[] = "dummy"; |
Sebastian Jansson | ef86d14 | 2019-04-15 12:42:42 | [diff] [blame] | 24 | SimulatedNetwork::Config CreateSimulationConfig( |
| 25 | NetworkSimulationConfig config) { |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 26 | SimulatedNetwork::Config sim_config; |
Sebastian Jansson | ef86d14 | 2019-04-15 12:42:42 | [diff] [blame] | 27 | sim_config.link_capacity_kbps = config.bandwidth.kbps_or(0); |
| 28 | sim_config.loss_percent = config.loss_rate * 100; |
| 29 | sim_config.queue_delay_ms = config.delay.ms(); |
| 30 | sim_config.delay_standard_deviation_ms = config.delay_std_dev.ms(); |
Sebastian Jansson | 8c8feb9 | 2019-01-29 14:59:17 | [diff] [blame] | 31 | sim_config.packet_overhead = config.packet_overhead.bytes<int>(); |
Sebastian Jansson | 24c678f | 2019-10-14 13:07:58 | [diff] [blame] | 32 | sim_config.queue_length_packets = |
| 33 | config.packet_queue_length_limit.value_or(0); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 34 | return sim_config; |
| 35 | } |
| 36 | } // namespace |
| 37 | |
Sebastian Jansson | a4c22b9 | 2019-04-15 19:10:00 | [diff] [blame] | 38 | SimulationNode::SimulationNode(NetworkSimulationConfig config, |
| 39 | SimulatedNetwork* behavior, |
| 40 | EmulatedNetworkNode* network_node) |
| 41 | : config_(config), simulation_(behavior), network_node_(network_node) {} |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 42 | |
Sebastian Jansson | a4c22b9 | 2019-04-15 19:10:00 | [diff] [blame] | 43 | std::unique_ptr<SimulatedNetwork> SimulationNode::CreateBehavior( |
Sebastian Jansson | ef86d14 | 2019-04-15 12:42:42 | [diff] [blame] | 44 | NetworkSimulationConfig config) { |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 45 | SimulatedNetwork::Config sim_config = CreateSimulationConfig(config); |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 46 | return std::make_unique<SimulatedNetwork>(sim_config); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void SimulationNode::UpdateConfig( |
Sebastian Jansson | ef86d14 | 2019-04-15 12:42:42 | [diff] [blame] | 50 | std::function<void(NetworkSimulationConfig*)> modifier) { |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 51 | modifier(&config_); |
| 52 | SimulatedNetwork::Config sim_config = CreateSimulationConfig(config_); |
Sebastian Jansson | a4c22b9 | 2019-04-15 19:10:00 | [diff] [blame] | 53 | simulation_->SetConfig(sim_config); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SimulationNode::PauseTransmissionUntil(Timestamp until) { |
Sebastian Jansson | a4c22b9 | 2019-04-15 19:10:00 | [diff] [blame] | 57 | simulation_->PauseTransmissionUntil(until.us()); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ColumnPrinter SimulationNode::ConfigPrinter() const { |
Sebastian Jansson | ef86d14 | 2019-04-15 12:42:42 | [diff] [blame] | 61 | return ColumnPrinter::Lambda( |
| 62 | "propagation_delay capacity loss_rate", |
| 63 | [this](rtc::SimpleStringBuilder& sb) { |
| 64 | sb.AppendFormat("%.3lf %.0lf %.2lf", config_.delay.seconds<double>(), |
| 65 | config_.bandwidth.bps() / 8.0, config_.loss_rate); |
| 66 | }); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 67 | } |
| 68 | |
Sebastian Jansson | aa01f27 | 2019-01-30 10:28:59 | [diff] [blame] | 69 | NetworkNodeTransport::NetworkNodeTransport(Clock* sender_clock, |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 70 | Call* sender_call) |
| 71 | : sender_clock_(sender_clock), sender_call_(sender_call) {} |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 72 | |
| 73 | NetworkNodeTransport::~NetworkNodeTransport() = default; |
| 74 | |
Harald Alvestrand | d43af91 | 2023-08-15 11:41:45 | [diff] [blame] | 75 | bool NetworkNodeTransport::SendRtp(rtc::ArrayView<const uint8_t> packet, |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 76 | const PacketOptions& options) { |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 77 | int64_t send_time_ms = sender_clock_->TimeInMilliseconds(); |
Sebastian Jansson | 156d11d | 2018-09-28 15:21:34 | [diff] [blame] | 78 | rtc::SentPacket sent_packet; |
| 79 | sent_packet.packet_id = options.packet_id; |
Sebastian Jansson | 0378997 | 2018-10-09 16:27:57 | [diff] [blame] | 80 | sent_packet.info.included_in_feedback = options.included_in_feedback; |
| 81 | sent_packet.info.included_in_allocation = options.included_in_allocation; |
Sebastian Jansson | 156d11d | 2018-09-28 15:21:34 | [diff] [blame] | 82 | sent_packet.send_time_ms = send_time_ms; |
Harald Alvestrand | d43af91 | 2023-08-15 11:41:45 | [diff] [blame] | 83 | sent_packet.info.packet_size_bytes = packet.size(); |
Sebastian Jansson | 156d11d | 2018-09-28 15:21:34 | [diff] [blame] | 84 | sent_packet.info.packet_type = rtc::PacketType::kData; |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 85 | sender_call_->OnSentPacket(sent_packet); |
Sebastian Jansson | 156d11d | 2018-09-28 15:21:34 | [diff] [blame] | 86 | |
Markus Handell | a5a4be1 | 2020-07-08 14:09:21 | [diff] [blame] | 87 | MutexLock lock(&mutex_); |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 88 | if (!endpoint_) |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 89 | return false; |
Harald Alvestrand | d43af91 | 2023-08-15 11:41:45 | [diff] [blame] | 90 | rtc::CopyOnWriteBuffer buffer(packet); |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 91 | endpoint_->SendPacket(local_address_, remote_address_, buffer, |
| 92 | packet_overhead_.bytes()); |
Sebastian Jansson | f65309c | 2018-12-20 09:26:00 | [diff] [blame] | 93 | return true; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 94 | } |
| 95 | |
Harald Alvestrand | d43af91 | 2023-08-15 11:41:45 | [diff] [blame] | 96 | bool NetworkNodeTransport::SendRtcp(rtc::ArrayView<const uint8_t> packet) { |
| 97 | rtc::CopyOnWriteBuffer buffer(packet); |
Markus Handell | a5a4be1 | 2020-07-08 14:09:21 | [diff] [blame] | 98 | MutexLock lock(&mutex_); |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 99 | if (!endpoint_) |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 100 | return false; |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 101 | endpoint_->SendPacket(local_address_, remote_address_, buffer, |
| 102 | packet_overhead_.bytes()); |
Sebastian Jansson | f65309c | 2018-12-20 09:26:00 | [diff] [blame] | 103 | return true; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 104 | } |
| 105 | |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 106 | void NetworkNodeTransport::Connect(EmulatedEndpoint* endpoint, |
| 107 | const rtc::SocketAddress& receiver_address, |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 108 | DataSize packet_overhead) { |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 109 | rtc::NetworkRoute route; |
| 110 | route.connected = true; |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 111 | // We assume that the address will be unique in the lower bytes. |
Jonas Oreland | 71fda36 | 2020-03-20 15:11:56 | [diff] [blame] | 112 | route.local = rtc::RouteEndpoint::CreateWithNetworkId(static_cast<uint16_t>( |
| 113 | receiver_address.ipaddr().v4AddressAsHostOrderInteger())); |
| 114 | route.remote = rtc::RouteEndpoint::CreateWithNetworkId(static_cast<uint16_t>( |
| 115 | receiver_address.ipaddr().v4AddressAsHostOrderInteger())); |
Sebastian Jansson | f52d3ed | 2020-02-28 15:22:09 | [diff] [blame] | 116 | route.packet_overhead = packet_overhead.bytes() + |
| 117 | receiver_address.ipaddr().overhead() + |
| 118 | cricket::kUdpHeaderSize; |
Sebastian Jansson | 4124dab | 2019-04-01 12:33:53 | [diff] [blame] | 119 | { |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 120 | // Only IPv4 address is supported. |
| 121 | RTC_CHECK_EQ(receiver_address.family(), AF_INET); |
Markus Handell | a5a4be1 | 2020-07-08 14:09:21 | [diff] [blame] | 122 | MutexLock lock(&mutex_); |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 123 | endpoint_ = endpoint; |
| 124 | local_address_ = rtc::SocketAddress(endpoint_->GetPeerLocalAddress(), 0); |
| 125 | remote_address_ = receiver_address; |
Sebastian Jansson | 4124dab | 2019-04-01 12:33:53 | [diff] [blame] | 126 | packet_overhead_ = packet_overhead; |
| 127 | current_network_route_ = route; |
| 128 | } |
| 129 | |
Evan Shrubsole | 02bdf66 | 2023-03-02 12:52:50 | [diff] [blame] | 130 | // Must be called from the worker thread. |
| 131 | rtc::Event event; |
| 132 | auto cleanup = absl::MakeCleanup([&event] { event.Set(); }); |
| 133 | auto&& task = [this, &route, cleanup = std::move(cleanup)] { |
| 134 | sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged( |
| 135 | kDummyTransportName, route); |
| 136 | }; |
| 137 | if (!sender_call_->worker_thread()->IsCurrent()) { |
| 138 | sender_call_->worker_thread()->PostTask(std::move(task)); |
| 139 | } else { |
| 140 | std::move(task)(); |
| 141 | } |
| 142 | event.Wait(TimeDelta::Seconds(1)); |
Sebastian Jansson | 4124dab | 2019-04-01 12:33:53 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void NetworkNodeTransport::Disconnect() { |
Markus Handell | a5a4be1 | 2020-07-08 14:09:21 | [diff] [blame] | 146 | MutexLock lock(&mutex_); |
Sebastian Jansson | 4124dab | 2019-04-01 12:33:53 | [diff] [blame] | 147 | current_network_route_.connected = false; |
Evan Shrubsole | 02bdf66 | 2023-03-02 12:52:50 | [diff] [blame] | 148 | |
Sebastian Jansson | 4124dab | 2019-04-01 12:33:53 | [diff] [blame] | 149 | sender_call_->GetTransportControllerSend()->OnNetworkRouteChanged( |
| 150 | kDummyTransportName, current_network_route_); |
| 151 | current_network_route_ = {}; |
Sebastian Jansson | 77bd385 | 2020-01-17 12:05:54 | [diff] [blame] | 152 | endpoint_ = nullptr; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 153 | } |
| 154 | |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 155 | } // namespace test |
| 156 | } // namespace webrtc |