Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
| 11 | #include <assert.h> |
| 12 | #include <math.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <cmath> |
| 17 | #include <utility> |
| 18 | |
| 19 | #include "call/call.h" |
| 20 | #include "call/fake_network_pipe.h" |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 22 | #include "rtc_base/ptr_util.h" |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 23 | #include "system_wrappers/include/clock.h" |
| 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | namespace { |
| 28 | constexpr int64_t kDefaultProcessIntervalMs = 5; |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 29 | constexpr int64_t kLogIntervalMs = 5000; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 30 | } // namespace |
| 31 | |
| 32 | NetworkPacket::NetworkPacket(rtc::CopyOnWriteBuffer packet, |
| 33 | int64_t send_time, |
| 34 | int64_t arrival_time, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 35 | absl::optional<PacketOptions> packet_options, |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 36 | bool is_rtcp, |
| 37 | MediaType media_type, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 38 | absl::optional<PacketTime> packet_time) |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 39 | : packet_(std::move(packet)), |
| 40 | send_time_(send_time), |
| 41 | arrival_time_(arrival_time), |
| 42 | packet_options_(packet_options), |
| 43 | is_rtcp_(is_rtcp), |
| 44 | media_type_(media_type), |
| 45 | packet_time_(packet_time) {} |
| 46 | |
| 47 | NetworkPacket::NetworkPacket(NetworkPacket&& o) |
| 48 | : packet_(std::move(o.packet_)), |
| 49 | send_time_(o.send_time_), |
| 50 | arrival_time_(o.arrival_time_), |
| 51 | packet_options_(o.packet_options_), |
| 52 | is_rtcp_(o.is_rtcp_), |
| 53 | media_type_(o.media_type_), |
| 54 | packet_time_(o.packet_time_) {} |
| 55 | |
| 56 | NetworkPacket& NetworkPacket::operator=(NetworkPacket&& o) { |
| 57 | packet_ = std::move(o.packet_); |
| 58 | send_time_ = o.send_time_; |
| 59 | arrival_time_ = o.arrival_time_; |
| 60 | packet_options_ = o.packet_options_; |
| 61 | is_rtcp_ = o.is_rtcp_; |
| 62 | media_type_ = o.media_type_; |
| 63 | packet_time_ = o.packet_time_; |
| 64 | |
| 65 | return *this; |
| 66 | } |
| 67 | |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 68 | FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
| 69 | const FakeNetworkPipe::Config& config) |
| 70 | : FakeNetworkPipe(clock, config, nullptr, 1) {} |
| 71 | |
| 72 | FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
| 73 | const FakeNetworkPipe::Config& config, |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 74 | PacketReceiver* receiver) |
| 75 | : FakeNetworkPipe(clock, config, receiver, 1) {} |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 76 | |
| 77 | FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
| 78 | const FakeNetworkPipe::Config& config, |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 79 | PacketReceiver* receiver, |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 80 | uint64_t seed) |
| 81 | : clock_(clock), |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 82 | network_simulation_(rtc::MakeUnique<SimulatedNetwork>(config, seed)), |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 83 | receiver_(receiver), |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 84 | transport_(nullptr), |
Sebastian Jansson | 7e85d67 | 2018-04-06 09:56:21 +0200 | [diff] [blame] | 85 | clock_offset_ms_(0), |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 86 | dropped_packets_(0), |
| 87 | sent_packets_(0), |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 88 | total_packet_delay_us_(0), |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 89 | next_process_time_us_(clock_->TimeInMicroseconds()), |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 90 | last_log_time_us_(clock_->TimeInMicroseconds()) {} |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 91 | |
| 92 | FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
| 93 | const FakeNetworkPipe::Config& config, |
| 94 | Transport* transport) |
| 95 | : clock_(clock), |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 96 | network_simulation_(rtc::MakeUnique<SimulatedNetwork>(config, 1)), |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 97 | receiver_(nullptr), |
| 98 | transport_(transport), |
Sebastian Jansson | 7e85d67 | 2018-04-06 09:56:21 +0200 | [diff] [blame] | 99 | clock_offset_ms_(0), |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 100 | dropped_packets_(0), |
| 101 | sent_packets_(0), |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 102 | total_packet_delay_us_(0), |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 103 | next_process_time_us_(clock_->TimeInMicroseconds()), |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 104 | last_log_time_us_(clock_->TimeInMicroseconds()) {} |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 105 | |
| 106 | FakeNetworkPipe::~FakeNetworkPipe() = default; |
| 107 | |
| 108 | void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { |
| 109 | rtc::CritScope crit(&config_lock_); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 110 | receiver_ = receiver; |
| 111 | } |
| 112 | |
| 113 | bool FakeNetworkPipe::SendRtp(const uint8_t* packet, |
| 114 | size_t length, |
| 115 | const PacketOptions& options) { |
| 116 | RTC_DCHECK(HasTransport()); |
| 117 | EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), options, false, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 118 | MediaType::ANY, absl::nullopt); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool FakeNetworkPipe::SendRtcp(const uint8_t* packet, size_t length) { |
| 123 | RTC_DCHECK(HasTransport()); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 124 | EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), absl::nullopt, true, |
| 125 | MediaType::ANY, absl::nullopt); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | PacketReceiver::DeliveryStatus FakeNetworkPipe::DeliverPacket( |
| 130 | MediaType media_type, |
| 131 | rtc::CopyOnWriteBuffer packet, |
| 132 | const PacketTime& packet_time) { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 133 | return EnqueuePacket(std::move(packet), absl::nullopt, false, media_type, |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 134 | packet_time) |
| 135 | ? PacketReceiver::DELIVERY_OK |
| 136 | : PacketReceiver::DELIVERY_PACKET_ERROR; |
| 137 | } |
| 138 | |
Sebastian Jansson | 7e85d67 | 2018-04-06 09:56:21 +0200 | [diff] [blame] | 139 | void FakeNetworkPipe::SetClockOffset(int64_t offset_ms) { |
| 140 | rtc::CritScope crit(&config_lock_); |
| 141 | clock_offset_ms_ = offset_ms; |
| 142 | } |
| 143 | |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 144 | SimulatedNetwork::SimulatedNetwork(SimulatedNetwork::Config config, |
| 145 | uint64_t random_seed) |
| 146 | : random_(random_seed), bursting_(false) { |
| 147 | SetConfig(config); |
| 148 | } |
| 149 | |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 150 | void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 151 | network_simulation_->SetConfig(config); |
| 152 | } |
| 153 | |
| 154 | void SimulatedNetwork::SetConfig(const SimulatedNetwork::Config& config) { |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 155 | rtc::CritScope crit(&config_lock_); |
| 156 | config_ = config; // Shallow copy of the struct. |
| 157 | double prob_loss = config.loss_percent / 100.0; |
| 158 | if (config_.avg_burst_loss_length == -1) { |
| 159 | // Uniform loss |
| 160 | prob_loss_bursting_ = prob_loss; |
| 161 | prob_start_bursting_ = prob_loss; |
| 162 | } else { |
| 163 | // Lose packets according to a gilbert-elliot model. |
| 164 | int avg_burst_loss_length = config.avg_burst_loss_length; |
| 165 | int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
| 166 | |
| 167 | RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
| 168 | << "For a total packet loss of " << config.loss_percent << "%% then" |
| 169 | << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 |
| 170 | << " or higher."; |
| 171 | |
| 172 | prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); |
| 173 | prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
| 174 | } |
| 175 | } |
| 176 | |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 177 | bool SimulatedNetwork::EnqueuePacket(PacketInFlightInfo packet) { |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 178 | Config config; |
| 179 | { |
| 180 | rtc::CritScope crit(&config_lock_); |
| 181 | config = config_; |
| 182 | } |
| 183 | rtc::CritScope crit(&process_lock_); |
| 184 | if (config.queue_length_packets > 0 && |
| 185 | capacity_link_.size() >= config.queue_length_packets) { |
| 186 | // Too many packet on the link, drop this one. |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 190 | // Delay introduced by the link capacity. |
| 191 | int64_t capacity_delay_ms = 0; |
| 192 | if (config.link_capacity_kbps > 0) { |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 193 | // Using bytes per millisecond to avoid losing precision. |
| 194 | const int64_t bytes_per_millisecond = config.link_capacity_kbps / 8; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 195 | // To round to the closest millisecond we add half a milliseconds worth of |
| 196 | // bytes to the delay calculation. |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 197 | capacity_delay_ms = (packet.size + capacity_delay_error_bytes_ + |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 198 | bytes_per_millisecond / 2) / |
| 199 | bytes_per_millisecond; |
| 200 | capacity_delay_error_bytes_ += |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 201 | packet.size - capacity_delay_ms * bytes_per_millisecond; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 202 | } |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 203 | int64_t network_start_time_us = packet.send_time_us; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 204 | |
| 205 | // Check if there already are packets on the link and change network start |
| 206 | // time forward if there is. |
| 207 | if (!capacity_link_.empty() && |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 208 | network_start_time_us < capacity_link_.back().arrival_time_us) |
| 209 | network_start_time_us = capacity_link_.back().arrival_time_us; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 210 | |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 211 | int64_t arrival_time_us = network_start_time_us + capacity_delay_ms * 1000; |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 212 | capacity_link_.push({packet, arrival_time_us}); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 213 | return true; |
| 214 | } |
| 215 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 216 | absl::optional<int64_t> SimulatedNetwork::NextDeliveryTimeUs() const { |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 217 | if (!delay_link_.empty()) |
| 218 | return delay_link_.begin()->arrival_time_us; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 219 | return absl::nullopt; |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | FakeNetworkPipe::StoredPacket::StoredPacket(NetworkPacket&& packet) |
| 223 | : packet(std::move(packet)) {} |
| 224 | |
| 225 | bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 226 | absl::optional<PacketOptions> options, |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 227 | bool is_rtcp, |
| 228 | MediaType media_type, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 229 | absl::optional<PacketTime> packet_time) { |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 230 | int64_t time_now_us = clock_->TimeInMicroseconds(); |
| 231 | rtc::CritScope crit(&process_lock_); |
| 232 | size_t packet_size = packet.size(); |
| 233 | NetworkPacket net_packet(std::move(packet), time_now_us, time_now_us, options, |
| 234 | is_rtcp, media_type, packet_time); |
| 235 | |
| 236 | packets_in_flight_.emplace_back(StoredPacket(std::move(net_packet))); |
| 237 | int64_t packet_id = reinterpret_cast<uint64_t>(&packets_in_flight_.back()); |
| 238 | bool sent = network_simulation_->EnqueuePacket( |
| 239 | PacketInFlightInfo(packet_size, time_now_us, packet_id)); |
| 240 | |
| 241 | if (!sent) { |
| 242 | packets_in_flight_.pop_back(); |
| 243 | ++dropped_packets_; |
| 244 | } |
| 245 | return sent; |
| 246 | } |
| 247 | |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 248 | float FakeNetworkPipe::PercentageLoss() { |
| 249 | rtc::CritScope crit(&process_lock_); |
| 250 | if (sent_packets_ == 0) |
| 251 | return 0; |
| 252 | |
| 253 | return static_cast<float>(dropped_packets_) / |
| 254 | (sent_packets_ + dropped_packets_); |
| 255 | } |
| 256 | |
| 257 | int FakeNetworkPipe::AverageDelay() { |
| 258 | rtc::CritScope crit(&process_lock_); |
| 259 | if (sent_packets_ == 0) |
| 260 | return 0; |
| 261 | |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 262 | return static_cast<int>(total_packet_delay_us_ / |
| 263 | (1000 * static_cast<int64_t>(sent_packets_))); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | size_t FakeNetworkPipe::DroppedPackets() { |
| 267 | rtc::CritScope crit(&process_lock_); |
| 268 | return dropped_packets_; |
| 269 | } |
| 270 | |
| 271 | size_t FakeNetworkPipe::SentPackets() { |
| 272 | rtc::CritScope crit(&process_lock_); |
| 273 | return sent_packets_; |
| 274 | } |
| 275 | |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 276 | std::vector<PacketDeliveryInfo> SimulatedNetwork::DequeueDeliverablePackets( |
| 277 | int64_t receive_time_us) { |
| 278 | int64_t time_now_us = receive_time_us; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 279 | Config config; |
| 280 | double prob_loss_bursting; |
| 281 | double prob_start_bursting; |
| 282 | { |
| 283 | rtc::CritScope crit(&config_lock_); |
| 284 | config = config_; |
| 285 | prob_loss_bursting = prob_loss_bursting_; |
| 286 | prob_start_bursting = prob_start_bursting_; |
| 287 | } |
| 288 | { |
| 289 | rtc::CritScope crit(&process_lock_); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 290 | // Check the capacity link first. |
| 291 | if (!capacity_link_.empty()) { |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 292 | int64_t last_arrival_time_us = |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 293 | delay_link_.empty() ? -1 : delay_link_.back().arrival_time_us; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 294 | bool needs_sort = false; |
| 295 | while (!capacity_link_.empty() && |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 296 | time_now_us >= capacity_link_.front().arrival_time_us) { |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 297 | // Time to get this packet. |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 298 | PacketInfo packet = std::move(capacity_link_.front()); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 299 | capacity_link_.pop(); |
| 300 | |
| 301 | // Drop packets at an average rate of |config_.loss_percent| with |
| 302 | // and average loss burst length of |config_.avg_burst_loss_length|. |
| 303 | if ((bursting_ && random_.Rand<double>() < prob_loss_bursting) || |
| 304 | (!bursting_ && random_.Rand<double>() < prob_start_bursting)) { |
| 305 | bursting_ = true; |
| 306 | continue; |
| 307 | } else { |
| 308 | bursting_ = false; |
| 309 | } |
| 310 | |
Sebastian Jansson | e6c0964 | 2018-05-11 10:53:02 +0200 | [diff] [blame] | 311 | int64_t arrival_time_jitter_us = std::max( |
| 312 | random_.Gaussian(config.queue_delay_ms * 1000, |
| 313 | config.delay_standard_deviation_ms * 1000), |
| 314 | 0.0); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 315 | |
| 316 | // If reordering is not allowed then adjust arrival_time_jitter |
| 317 | // to make sure all packets are sent in order. |
| 318 | if (!config.allow_reordering && !delay_link_.empty() && |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 319 | packet.arrival_time_us + arrival_time_jitter_us < |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 320 | last_arrival_time_us) { |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 321 | arrival_time_jitter_us = |
| 322 | last_arrival_time_us - packet.arrival_time_us; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 323 | } |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 324 | packet.arrival_time_us += arrival_time_jitter_us; |
| 325 | if (packet.arrival_time_us >= last_arrival_time_us) { |
| 326 | last_arrival_time_us = packet.arrival_time_us; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 327 | } else { |
| 328 | needs_sort = true; |
| 329 | } |
| 330 | delay_link_.emplace_back(std::move(packet)); |
| 331 | } |
| 332 | |
| 333 | if (needs_sort) { |
| 334 | // Packet(s) arrived out of order, make sure list is sorted. |
| 335 | std::sort(delay_link_.begin(), delay_link_.end(), |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 336 | [](const PacketInfo& p1, const PacketInfo& p2) { |
| 337 | return p1.arrival_time_us < p2.arrival_time_us; |
| 338 | }); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 342 | std::vector<PacketDeliveryInfo> packets_to_deliver; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 343 | // Check the extra delay queue. |
| 344 | while (!delay_link_.empty() && |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 345 | time_now_us >= delay_link_.front().arrival_time_us) { |
| 346 | PacketInfo packet_info = delay_link_.front(); |
| 347 | packets_to_deliver.emplace_back( |
| 348 | PacketDeliveryInfo(packet_info.packet, packet_info.arrival_time_us)); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 349 | delay_link_.pop_front(); |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 350 | } |
| 351 | return packets_to_deliver; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void FakeNetworkPipe::Process() { |
| 356 | int64_t time_now_us = clock_->TimeInMicroseconds(); |
| 357 | std::queue<NetworkPacket> packets_to_deliver; |
| 358 | { |
| 359 | rtc::CritScope crit(&process_lock_); |
| 360 | if (time_now_us - last_log_time_us_ > kLogIntervalMs * 1000) { |
| 361 | int64_t queueing_delay_us = 0; |
| 362 | if (!packets_in_flight_.empty()) |
| 363 | queueing_delay_us = |
| 364 | time_now_us - packets_in_flight_.front().packet.send_time(); |
| 365 | |
| 366 | RTC_LOG(LS_INFO) << "Network queue: " << queueing_delay_us / 1000 |
| 367 | << " ms."; |
| 368 | last_log_time_us_ = time_now_us; |
| 369 | } |
| 370 | |
| 371 | std::vector<PacketDeliveryInfo> delivery_infos = |
| 372 | network_simulation_->DequeueDeliverablePackets(time_now_us); |
| 373 | for (auto& delivery_info : delivery_infos) { |
| 374 | // In the common case where no reordering happens, find will return early |
| 375 | // as the first packet will be a match. |
| 376 | auto packet_it = |
| 377 | std::find_if(packets_in_flight_.begin(), packets_in_flight_.end(), |
| 378 | [&delivery_info](StoredPacket& packet_ref) { |
| 379 | return reinterpret_cast<uint64_t>(&packet_ref) == |
| 380 | delivery_info.packet_id; |
| 381 | }); |
| 382 | // Check that the packet is in the deque of packets in flight. |
| 383 | RTC_CHECK(packet_it != packets_in_flight_.end()); |
| 384 | // Check that the packet is not already removed. |
| 385 | RTC_DCHECK(!packet_it->removed); |
| 386 | |
| 387 | NetworkPacket packet = std::move(packet_it->packet); |
| 388 | packet_it->removed = true; |
| 389 | |
| 390 | // Cleanup of removed packets at the beginning of the deque. |
| 391 | while (!packets_in_flight_.empty() && |
| 392 | packets_in_flight_.front().removed) { |
| 393 | packets_in_flight_.pop_front(); |
| 394 | } |
| 395 | |
| 396 | if (delivery_info.receive_time_us != PacketDeliveryInfo::kNotReceived) { |
| 397 | int64_t added_delay_us = |
| 398 | delivery_info.receive_time_us - packet.send_time(); |
| 399 | packet.IncrementArrivalTime(added_delay_us); |
| 400 | packets_to_deliver.emplace(std::move(packet)); |
| 401 | // |time_now_us| might be later than when the packet should have |
| 402 | // arrived, due to NetworkProcess being called too late. For stats, use |
| 403 | // the time it should have been on the link. |
| 404 | total_packet_delay_us_ += added_delay_us; |
| 405 | } |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 406 | } |
| 407 | sent_packets_ += packets_to_deliver.size(); |
| 408 | } |
| 409 | |
| 410 | rtc::CritScope crit(&config_lock_); |
| 411 | while (!packets_to_deliver.empty()) { |
| 412 | NetworkPacket packet = std::move(packets_to_deliver.front()); |
| 413 | packets_to_deliver.pop(); |
| 414 | DeliverPacket(&packet); |
| 415 | } |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame^] | 416 | absl::optional<int64_t> delivery_us = |
Sebastian Jansson | 7ee2e25 | 2018-05-07 14:49:39 +0200 | [diff] [blame] | 417 | network_simulation_->NextDeliveryTimeUs(); |
| 418 | next_process_time_us_ = delivery_us |
| 419 | ? *delivery_us |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 420 | : time_now_us + kDefaultProcessIntervalMs * 1000; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void FakeNetworkPipe::DeliverPacket(NetworkPacket* packet) { |
Sebastian Jansson | a44ab18 | 2018-04-06 12:59:14 +0200 | [diff] [blame] | 424 | if (transport_) { |
| 425 | RTC_DCHECK(!receiver_); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 426 | if (packet->is_rtcp()) { |
| 427 | transport_->SendRtcp(packet->data(), packet->data_length()); |
| 428 | } else { |
| 429 | transport_->SendRtp(packet->data(), packet->data_length(), |
| 430 | packet->packet_options()); |
| 431 | } |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 432 | } else if (receiver_) { |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 433 | PacketTime packet_time = packet->packet_time(); |
| 434 | if (packet_time.timestamp != -1) { |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 435 | int64_t queue_time_us = packet->arrival_time() - packet->send_time(); |
| 436 | RTC_CHECK(queue_time_us >= 0); |
| 437 | packet_time.timestamp += queue_time_us; |
Sebastian Jansson | 7e85d67 | 2018-04-06 09:56:21 +0200 | [diff] [blame] | 438 | packet_time.timestamp += (clock_offset_ms_ * 1000); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 439 | } |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 440 | receiver_->DeliverPacket(packet->media_type(), |
| 441 | std::move(*packet->raw_packet()), packet_time); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
| 445 | int64_t FakeNetworkPipe::TimeUntilNextProcess() { |
| 446 | rtc::CritScope crit(&process_lock_); |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 447 | int64_t delay_us = next_process_time_us_ - clock_->TimeInMicroseconds(); |
| 448 | return std::max<int64_t>((delay_us + 500) / 1000, 0); |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | bool FakeNetworkPipe::HasTransport() const { |
| 452 | rtc::CritScope crit(&config_lock_); |
| 453 | return transport_ != nullptr; |
| 454 | } |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 455 | bool FakeNetworkPipe::HasReceiver() const { |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 456 | rtc::CritScope crit(&config_lock_); |
Sebastian Jansson | 0940811 | 2018-04-24 14:41:22 +0200 | [diff] [blame] | 457 | return receiver_ != nullptr; |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 460 | void FakeNetworkPipe::DeliverPacketWithLock(NetworkPacket* packet) { |
| 461 | rtc::CritScope crit(&config_lock_); |
| 462 | DeliverPacket(packet); |
| 463 | } |
| 464 | |
| 465 | void FakeNetworkPipe::ResetStats() { |
| 466 | rtc::CritScope crit(&process_lock_); |
| 467 | dropped_packets_ = 0; |
| 468 | sent_packets_ = 0; |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 469 | total_packet_delay_us_ = 0; |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 470 | } |
| 471 | |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 472 | void FakeNetworkPipe::AddToPacketDropCount() { |
| 473 | rtc::CritScope crit(&process_lock_); |
| 474 | ++dropped_packets_; |
| 475 | } |
| 476 | |
| 477 | void FakeNetworkPipe::AddToPacketSentCount(int count) { |
| 478 | rtc::CritScope crit(&process_lock_); |
| 479 | sent_packets_ += count; |
| 480 | } |
| 481 | |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 482 | void FakeNetworkPipe::AddToTotalDelay(int delay_us) { |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 483 | rtc::CritScope crit(&process_lock_); |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 484 | total_packet_delay_us_ += delay_us; |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 487 | int64_t FakeNetworkPipe::GetTimeInMicroseconds() const { |
| 488 | return clock_->TimeInMicroseconds(); |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 491 | bool FakeNetworkPipe::ShouldProcess(int64_t time_now_us) const { |
| 492 | return time_now_us >= next_process_time_us_; |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Sebastian Jansson | 512bdce | 2018-04-23 13:15:04 +0200 | [diff] [blame] | 495 | void FakeNetworkPipe::SetTimeToNextProcess(int64_t skip_us) { |
| 496 | next_process_time_us_ += skip_us; |
Christoffer Rodbro | 8ef59a4 | 2018-03-20 14:34:01 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Erik Språng | 0970851 | 2018-03-14 15:16:50 +0100 | [diff] [blame] | 499 | } // namespace webrtc |