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