stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [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 "webrtc/test/fake_network_pipe.h" |
| 12 | |
| 13 | #include <assert.h> |
| 14 | #include <math.h> |
| 15 | #include <string.h> |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 16 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 17 | #include <algorithm> |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 18 | #include <cmath> |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 19 | |
stefan | e9ad271 | 2017-02-10 06:09:28 -0800 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
ossu | f515ab8 | 2016-12-07 04:52:58 -0800 | [diff] [blame] | 21 | #include "webrtc/call/call.h" |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 22 | #include "webrtc/system_wrappers/include/clock.h" |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 26 | FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
| 27 | const FakeNetworkPipe::Config& config) |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 28 | : FakeNetworkPipe(clock, config, 1) {} |
| 29 | |
| 30 | FakeNetworkPipe::FakeNetworkPipe(Clock* clock, |
| 31 | const FakeNetworkPipe::Config& config, |
| 32 | uint64_t seed) |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 33 | : clock_(clock), |
| 34 | packet_receiver_(NULL), |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 35 | random_(seed), |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame^] | 36 | config_(), |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 37 | dropped_packets_(0), |
| 38 | sent_packets_(0), |
| 39 | total_packet_delay_(0), |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 40 | bursting_(false), |
stefan | e9ad271 | 2017-02-10 06:09:28 -0800 | [diff] [blame] | 41 | next_process_time_(clock_->TimeInMilliseconds()), |
| 42 | last_log_time_(clock_->TimeInMilliseconds()) { |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame^] | 43 | SetConfig(config); |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 44 | } |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 45 | |
| 46 | FakeNetworkPipe::~FakeNetworkPipe() { |
| 47 | while (!capacity_link_.empty()) { |
| 48 | delete capacity_link_.front(); |
| 49 | capacity_link_.pop(); |
| 50 | } |
| 51 | while (!delay_link_.empty()) { |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 52 | delete *delay_link_.begin(); |
| 53 | delay_link_.erase(delay_link_.begin()); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) { |
| 58 | packet_receiver_ = receiver; |
| 59 | } |
| 60 | |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 61 | void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 62 | rtc::CritScope crit(&lock_); |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 63 | config_ = config; // Shallow copy of the struct. |
philipel | 5ef2bc1 | 2017-02-21 07:28:31 -0800 | [diff] [blame^] | 64 | double prob_loss = config.loss_percent / 100.0; |
| 65 | if (config_.avg_burst_loss_length == -1) { |
| 66 | // Uniform loss |
| 67 | prob_loss_bursting_ = prob_loss; |
| 68 | prob_start_bursting_ = prob_loss; |
| 69 | } else { |
| 70 | // Lose packets according to a gilbert-elliot model. |
| 71 | int avg_burst_loss_length = config.avg_burst_loss_length; |
| 72 | int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss)); |
| 73 | |
| 74 | RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length) |
| 75 | << "For a total packet loss of " << config.loss_percent << "%% then" |
| 76 | << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1 |
| 77 | << " or higher."; |
| 78 | |
| 79 | prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length); |
| 80 | prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length; |
| 81 | } |
henrik.lundin@webrtc.org | c0e9aeb | 2014-02-26 13:34:52 +0000 | [diff] [blame] | 82 | } |
| 83 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 84 | void FakeNetworkPipe::SendPacket(const uint8_t* data, size_t data_length) { |
| 85 | // A NULL packet_receiver_ means that this pipe will terminate the flow of |
| 86 | // packets. |
| 87 | if (packet_receiver_ == NULL) |
| 88 | return; |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 89 | rtc::CritScope crit(&lock_); |
stefan@webrtc.org | b8e9e44 | 2014-07-09 11:29:06 +0000 | [diff] [blame] | 90 | if (config_.queue_length_packets > 0 && |
| 91 | capacity_link_.size() >= config_.queue_length_packets) { |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 92 | // Too many packet on the link, drop this one. |
| 93 | ++dropped_packets_; |
| 94 | return; |
| 95 | } |
| 96 | |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 97 | int64_t time_now = clock_->TimeInMilliseconds(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 98 | |
| 99 | // Delay introduced by the link capacity. |
| 100 | int64_t capacity_delay_ms = 0; |
| 101 | if (config_.link_capacity_kbps > 0) |
| 102 | capacity_delay_ms = data_length / (config_.link_capacity_kbps / 8); |
| 103 | int64_t network_start_time = time_now; |
| 104 | |
| 105 | // Check if there already are packets on the link and change network start |
danilchap | a6a7007 | 2016-06-01 11:20:43 -0700 | [diff] [blame] | 106 | // time forward if there is. |
| 107 | if (!capacity_link_.empty() && |
| 108 | network_start_time < capacity_link_.back()->arrival_time()) |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 109 | network_start_time = capacity_link_.back()->arrival_time(); |
| 110 | |
| 111 | int64_t arrival_time = network_start_time + capacity_delay_ms; |
| 112 | NetworkPacket* packet = new NetworkPacket(data, data_length, time_now, |
| 113 | arrival_time); |
| 114 | capacity_link_.push(packet); |
| 115 | } |
| 116 | |
| 117 | float FakeNetworkPipe::PercentageLoss() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 118 | rtc::CritScope crit(&lock_); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 119 | if (sent_packets_ == 0) |
| 120 | return 0; |
| 121 | |
| 122 | return static_cast<float>(dropped_packets_) / |
| 123 | (sent_packets_ + dropped_packets_); |
| 124 | } |
| 125 | |
| 126 | int FakeNetworkPipe::AverageDelay() { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 127 | rtc::CritScope crit(&lock_); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 128 | if (sent_packets_ == 0) |
| 129 | return 0; |
| 130 | |
Stefan Holmer | ff2a635 | 2016-01-14 10:00:21 +0100 | [diff] [blame] | 131 | return static_cast<int>(total_packet_delay_ / |
| 132 | static_cast<int64_t>(sent_packets_)); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void FakeNetworkPipe::Process() { |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 136 | int64_t time_now = clock_->TimeInMilliseconds(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 137 | std::queue<NetworkPacket*> packets_to_deliver; |
| 138 | { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 139 | rtc::CritScope crit(&lock_); |
stefan | e9ad271 | 2017-02-10 06:09:28 -0800 | [diff] [blame] | 140 | if (time_now - last_log_time_ > 5000) { |
| 141 | int64_t queueing_delay_ms = 0; |
| 142 | if (!capacity_link_.empty()) { |
| 143 | queueing_delay_ms = time_now - capacity_link_.front()->send_time(); |
| 144 | } |
| 145 | LOG(LS_INFO) << "Network queue: " << queueing_delay_ms << " ms."; |
| 146 | last_log_time_ = time_now; |
| 147 | } |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 148 | // Check the capacity link first. |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 149 | while (!capacity_link_.empty() && |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 150 | time_now >= capacity_link_.front()->arrival_time()) { |
| 151 | // Time to get this packet. |
| 152 | NetworkPacket* packet = capacity_link_.front(); |
| 153 | capacity_link_.pop(); |
| 154 | |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 155 | // Drop packets at an average rate of |config_.loss_percent| with |
| 156 | // and average loss burst length of |config_.avg_burst_loss_length|. |
| 157 | if ((bursting_ && random_.Rand<double>() < prob_loss_bursting_) || |
| 158 | (!bursting_ && random_.Rand<double>() < prob_start_bursting_)) { |
| 159 | bursting_ = true; |
stefan@webrtc.org | bfe6e08 | 2014-07-31 12:30:18 +0000 | [diff] [blame] | 160 | delete packet; |
| 161 | continue; |
philipel | 536378b | 2016-05-31 03:20:23 -0700 | [diff] [blame] | 162 | } else { |
| 163 | bursting_ = false; |
stefan@webrtc.org | bfe6e08 | 2014-07-31 12:30:18 +0000 | [diff] [blame] | 164 | } |
| 165 | |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 166 | int arrival_time_jitter = random_.Gaussian( |
| 167 | config_.queue_delay_ms, config_.delay_standard_deviation_ms); |
| 168 | |
| 169 | // If reordering is not allowed then adjust arrival_time_jitter |
| 170 | // to make sure all packets are sent in order. |
| 171 | if (!config_.allow_reordering && !delay_link_.empty() && |
| 172 | packet->arrival_time() + arrival_time_jitter < |
| 173 | (*delay_link_.rbegin())->arrival_time()) { |
| 174 | arrival_time_jitter = |
| 175 | (*delay_link_.rbegin())->arrival_time() - packet->arrival_time(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 176 | } |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 177 | packet->IncrementArrivalTime(arrival_time_jitter); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 178 | if (packet->arrival_time() < next_process_time_) |
| 179 | next_process_time_ = packet->arrival_time(); |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 180 | delay_link_.insert(packet); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Check the extra delay queue. |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 184 | while (!delay_link_.empty() && |
| 185 | time_now >= (*delay_link_.begin())->arrival_time()) { |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 186 | // Deliver this packet. |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 187 | NetworkPacket* packet = *delay_link_.begin(); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 188 | packets_to_deliver.push(packet); |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 189 | delay_link_.erase(delay_link_.begin()); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 190 | // |time_now| might be later than when the packet should have arrived, due |
| 191 | // to NetworkProcess being called too late. For stats, use the time it |
| 192 | // should have been on the link. |
| 193 | total_packet_delay_ += packet->arrival_time() - packet->send_time(); |
| 194 | } |
| 195 | sent_packets_ += packets_to_deliver.size(); |
| 196 | } |
| 197 | while (!packets_to_deliver.empty()) { |
| 198 | NetworkPacket* packet = packets_to_deliver.front(); |
| 199 | packets_to_deliver.pop(); |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 200 | packet_receiver_->DeliverPacket(MediaType::ANY, packet->data(), |
stefan | 68786d2 | 2015-09-08 05:36:15 -0700 | [diff] [blame] | 201 | packet->data_length(), PacketTime()); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 202 | delete packet; |
| 203 | } |
| 204 | } |
| 205 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 206 | int64_t FakeNetworkPipe::TimeUntilNextProcess() const { |
Peter Boström | f2f8283 | 2015-05-01 13:00:41 +0200 | [diff] [blame] | 207 | rtc::CritScope crit(&lock_); |
isheriff | 90ce01d | 2016-09-29 02:02:11 -0700 | [diff] [blame] | 208 | const int64_t kDefaultProcessIntervalMs = 5; |
philipel | a2c5523 | 2016-01-26 08:41:53 -0800 | [diff] [blame] | 209 | if (capacity_link_.empty() || delay_link_.empty()) |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 210 | return kDefaultProcessIntervalMs; |
Peter Boström | d3c9447 | 2015-12-09 11:20:58 +0100 | [diff] [blame] | 211 | return std::max<int64_t>(next_process_time_ - clock_->TimeInMilliseconds(), |
| 212 | 0); |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } // namespace webrtc |