| 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 "call/simulated_network.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <cmath> |
| 15 | #include <utility> |
| 16 |
| 17 | #include "api/units/data_rate.h" |
| 18 | #include "api/units/data_size.h" |
| 19 | #include "api/units/time_delta.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 |
| 22 | namespace webrtc { |
| 23 | namespace { |
| 24 | constexpr TimeDelta kDefaultProcessDelay = TimeDelta::Millis(5); |
| 25 | } // namespace |
| 26 |
| 27 | SimulatedNetwork::SimulatedNetwork(Config config, uint64_t random_seed) |
| 28 | : random_(random_seed), bursting_(false) { |
| 29 | SetConfig(config); |
| 30 | } |
| 31 |
| 32 | SimulatedNetwork::~SimulatedNetwork() = default; |
| 33 |
| 34 | void SimulatedNetwork::SetConfig(const Config& config) { |
| 35 | MutexLock lock(&config_lock_); |
| 36 | config_state_.config = config; // Shallow copy of the struct. |
| 37 | double prob_loss = config.loss_percent / 100.0; |
| 38 | if (config_state_.config.avg_burst_loss_length == -1) { |
| 39 | // Uniform loss |
| 40 | config_state_.prob_loss_bursting = prob_loss; |
| 41 | config_state_.prob_start_bursting = prob_loss; |
| 42 | } else { |
| 43 | // Lose packets according to a gilbert-elliot model. |
| 44 | int avg_burst_loss_length = config.avg_burst_loss_length; |
| 45 | int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
| 46 |
| 47 | RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
| 48 | << "For a total packet loss of " << config.loss_percent |
| 49 | << "%% then" |
| 50 | " avg_burst_loss_length must be " |
| 51 | << min_avg_burst_loss_length + 1 << " or higher."; |
| 52 |
| 53 | config_state_.prob_loss_bursting = (1.0 - 1.0 / avg_burst_loss_length); |
| 54 | config_state_.prob_start_bursting = |
| 55 | prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
| 56 | } |
| 57 | } |
| 58 |
| 59 | void SimulatedNetwork::UpdateConfig( |
| 60 | std::function<void(BuiltInNetworkBehaviorConfig*)> config_modifier) { |
| 61 | MutexLock lock(&config_lock_); |
| 62 | config_modifier(&config_state_.config); |
| 63 | } |
| 64 |
| 65 | void SimulatedNetwork::PauseTransmissionUntil(int64_t until_us) { |
| 66 | MutexLock lock(&config_lock_); |
| 67 | config_state_.pause_transmission_until_us = until_us; |
| 68 | } |
| 69 |
| 70 | bool SimulatedNetwork::EnqueuePacket(PacketInFlightInfo packet) { |
| 71 | RTC_DCHECK_RUNS_SERIALIZED(&process_checker_); |
| 72 | ConfigState state = GetConfigState(); |
| 73 |
| 74 | UpdateCapacityQueue(state, packet.send_time_us); |
| 75 |
| 76 | packet.size += state.config.packet_overhead; |
| 77 |
| 78 | if (state.config.queue_length_packets > 0 && |
| 79 | capacity_link_.size() >= state.config.queue_length_packets) { |
| 80 | // Too many packet on the link, drop this one. |
| 81 | return false; |
| 82 | } |
| 83 |
| 84 | // Set arrival time = send time for now; actual arrival time will be |
| 85 | // calculated in UpdateCapacityQueue. |
| 86 | queue_size_bytes_ += packet.size; |
| 87 | capacity_link_.push({packet, packet.send_time_us}); |
| 88 | if (!next_process_time_us_) { |
| 89 | next_process_time_us_ = packet.send_time_us + kDefaultProcessDelay.us(); |
| 90 | } |
| 91 |
| 92 | return true; |
| 93 | } |
| 94 |
| 95 | absl::optional<int64_t> SimulatedNetwork::NextDeliveryTimeUs() const { |
| 96 | RTC_DCHECK_RUNS_SERIALIZED(&process_checker_); |
| 97 | return next_process_time_us_; |
| 98 | } |
| 99 |
| 100 | void SimulatedNetwork::UpdateCapacityQueue(ConfigState state, |
| 101 | int64_t time_now_us) { |
| 102 | bool needs_sort = false; |
| 103 |
| 104 | // Catch for thread races. |
| 105 | if (time_now_us < last_capacity_link_visit_us_.value_or(time_now_us)) |
| 106 | return; |
| 107 |
| 108 | int64_t time_us = last_capacity_link_visit_us_.value_or(time_now_us); |
| 109 | // Check the capacity link first. |
| 110 | while (!capacity_link_.empty()) { |
| 111 | int64_t time_until_front_exits_us = 0; |
| 112 | if (state.config.link_capacity_kbps > 0) { |
| 113 | int64_t remaining_bits = |
| 114 | capacity_link_.front().packet.size * 8 - pending_drain_bits_; |
| 115 | RTC_DCHECK(remaining_bits > 0); |
| 116 | // Division rounded up - packet not delivered until its last bit is. |
| 117 | time_until_front_exits_us = |
| 118 | (1000 * remaining_bits + state.config.link_capacity_kbps - 1) / |
| 119 | state.config.link_capacity_kbps; |
| 120 | } |
| 121 |
| 122 | if (time_us + time_until_front_exits_us > time_now_us) { |
| 123 | // Packet at front will not exit yet. Will not enter here on infinite |
| 124 | // capacity(=0) so no special handling needed. |
| 125 | pending_drain_bits_ += |
| 126 | ((time_now_us - time_us) * state.config.link_capacity_kbps) / 1000; |
| 127 | break; |
| 128 | } |
| 129 | if (state.config.link_capacity_kbps > 0) { |
| 130 | pending_drain_bits_ += |
| 131 | (time_until_front_exits_us * state.config.link_capacity_kbps) / 1000; |
| 132 | } else { |
| 133 | // Enough to drain the whole queue. |
| 134 | pending_drain_bits_ = queue_size_bytes_ * 8; |
| 135 | } |
| 136 |
| 137 | // Time to get this packet. |
| 138 | PacketInfo packet = capacity_link_.front(); |
| 139 | capacity_link_.pop(); |
| 140 |
| 141 | time_us += time_until_front_exits_us; |
| 142 | RTC_DCHECK(time_us >= packet.packet.send_time_us); |
| 143 | packet.arrival_time_us = |
| 144 | std::max(state.pause_transmission_until_us, time_us); |
| 145 | queue_size_bytes_ -= packet.packet.size; |
| 146 | pending_drain_bits_ -= packet.packet.size * 8; |
| 147 | RTC_DCHECK(pending_drain_bits_ >= 0); |
| 148 |
| 149 | // Drop packets at an average rate of `state.config.loss_percent` with |
| 150 | // and average loss burst length of `state.config.avg_burst_loss_length`. |
| 151 | if ((bursting_ && random_.Rand<double>() < state.prob_loss_bursting) || |
| 152 | (!bursting_ && random_.Rand<double>() < state.prob_start_bursting)) { |
| 153 | bursting_ = true; |
| 154 | packet.arrival_time_us = PacketDeliveryInfo::kNotReceived; |
| 155 | } else { |
| 156 | bursting_ = false; |
| 157 | int64_t arrival_time_jitter_us = std::max( |
| 158 | random_.Gaussian(state.config.queue_delay_ms * 1000, |
| 159 | state.config.delay_standard_deviation_ms * 1000), |
| 160 | 0.0); |
| 161 |
| 162 | // If reordering is not allowed then adjust arrival_time_jitter |
| 163 | // to make sure all packets are sent in order. |
| 164 | int64_t last_arrival_time_us = |
| 165 | delay_link_.empty() ? -1 : delay_link_.back().arrival_time_us; |
| 166 | if (!state.config.allow_reordering && !delay_link_.empty() && |
| 167 | packet.arrival_time_us + arrival_time_jitter_us < |
| 168 | last_arrival_time_us) { |
| 169 | arrival_time_jitter_us = last_arrival_time_us - packet.arrival_time_us; |
| 170 | } |
| 171 | packet.arrival_time_us += arrival_time_jitter_us; |
| 172 | if (packet.arrival_time_us >= last_arrival_time_us) { |
| 173 | last_arrival_time_us = packet.arrival_time_us; |
| 174 | } else { |
| 175 | needs_sort = true; |
| 176 | } |
| 177 | } |
| 178 | delay_link_.emplace_back(packet); |
| 179 | } |
| 180 | last_capacity_link_visit_us_ = time_now_us; |
| 181 | // Cannot save unused capacity for later. |
| 182 | pending_drain_bits_ = std::min(pending_drain_bits_, queue_size_bytes_ * 8); |
| 183 |
| 184 | if (needs_sort) { |
| 185 | // Packet(s) arrived out of order, make sure list is sorted. |
| 186 | std::sort(delay_link_.begin(), delay_link_.end(), |
| 187 | [](const PacketInfo& p1, const PacketInfo& p2) { |
| 188 | return p1.arrival_time_us < p2.arrival_time_us; |
| 189 | }); |
| 190 | } |
| 191 | } |
| 192 |
| 193 | SimulatedNetwork::ConfigState SimulatedNetwork::GetConfigState() const { |
| 194 | MutexLock lock(&config_lock_); |
| 195 | return config_state_; |
| 196 | } |
| 197 |
| 198 | std::vector<PacketDeliveryInfo> SimulatedNetwork::DequeueDeliverablePackets( |
| 199 | int64_t receive_time_us) { |
| 200 | RTC_DCHECK_RUNS_SERIALIZED(&process_checker_); |
| 201 | UpdateCapacityQueue(GetConfigState(), receive_time_us); |
| 202 | std::vector<PacketDeliveryInfo> packets_to_deliver; |
| 203 | // Check the extra delay queue. |
| 204 | while (!delay_link_.empty() && |
| 205 | receive_time_us >= delay_link_.front().arrival_time_us) { |
| 206 | PacketInfo packet_info = delay_link_.front(); |
| 207 | packets_to_deliver.emplace_back( |
| 208 | PacketDeliveryInfo(packet_info.packet, packet_info.arrival_time_us)); |
| 209 | delay_link_.pop_front(); |
| 210 | } |
| 211 |
| 212 | if (!delay_link_.empty()) { |
| 213 | next_process_time_us_ = delay_link_.front().arrival_time_us; |
| 214 | } else if (!capacity_link_.empty()) { |
| 215 | next_process_time_us_ = receive_time_us + kDefaultProcessDelay.us(); |
| 216 | } else { |
| 217 | next_process_time_us_.reset(); |
| 218 | } |
| 219 | return packets_to_deliver; |
| 220 | } |
| 221 |
| 222 | } // namespace webrtc |
| 223 |