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