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