blob: ebe551a409749c15aaf1d6815f1fb69392be473f [file] [log] [blame]
Erik Språng09708512018-03-14 15:16:50 +01001/*
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 <assert.h>
12#include <math.h>
13#include <string.h>
14
15#include <algorithm>
16#include <cmath>
17#include <utility>
18
19#include "call/call.h"
20#include "call/fake_network_pipe.h"
Erik Språng09708512018-03-14 15:16:50 +010021#include "rtc_base/logging.h"
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020022#include "rtc_base/ptr_util.h"
Erik Språng09708512018-03-14 15:16:50 +010023#include "system_wrappers/include/clock.h"
24
25namespace webrtc {
26
27namespace {
28constexpr int64_t kDefaultProcessIntervalMs = 5;
Sebastian Jansson512bdce2018-04-23 13:15:04 +020029constexpr int64_t kLogIntervalMs = 5000;
Erik Språng09708512018-03-14 15:16:50 +010030} // namespace
31
32NetworkPacket::NetworkPacket(rtc::CopyOnWriteBuffer packet,
33 int64_t send_time,
34 int64_t arrival_time,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020035 absl::optional<PacketOptions> packet_options,
Erik Språng09708512018-03-14 15:16:50 +010036 bool is_rtcp,
37 MediaType media_type,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020038 absl::optional<PacketTime> packet_time)
Erik Språng09708512018-03-14 15:16:50 +010039 : packet_(std::move(packet)),
40 send_time_(send_time),
41 arrival_time_(arrival_time),
42 packet_options_(packet_options),
43 is_rtcp_(is_rtcp),
44 media_type_(media_type),
45 packet_time_(packet_time) {}
46
47NetworkPacket::NetworkPacket(NetworkPacket&& o)
48 : packet_(std::move(o.packet_)),
49 send_time_(o.send_time_),
50 arrival_time_(o.arrival_time_),
51 packet_options_(o.packet_options_),
52 is_rtcp_(o.is_rtcp_),
53 media_type_(o.media_type_),
54 packet_time_(o.packet_time_) {}
55
56NetworkPacket& NetworkPacket::operator=(NetworkPacket&& o) {
57 packet_ = std::move(o.packet_);
58 send_time_ = o.send_time_;
59 arrival_time_ = o.arrival_time_;
60 packet_options_ = o.packet_options_;
61 is_rtcp_ = o.is_rtcp_;
62 media_type_ = o.media_type_;
63 packet_time_ = o.packet_time_;
64
65 return *this;
66}
67
Erik Språng09708512018-03-14 15:16:50 +010068FakeNetworkPipe::FakeNetworkPipe(Clock* clock,
69 const FakeNetworkPipe::Config& config)
70 : FakeNetworkPipe(clock, config, nullptr, 1) {}
71
72FakeNetworkPipe::FakeNetworkPipe(Clock* clock,
73 const FakeNetworkPipe::Config& config,
Sebastian Jansson09408112018-04-24 14:41:22 +020074 PacketReceiver* receiver)
75 : FakeNetworkPipe(clock, config, receiver, 1) {}
Erik Språng09708512018-03-14 15:16:50 +010076
77FakeNetworkPipe::FakeNetworkPipe(Clock* clock,
78 const FakeNetworkPipe::Config& config,
Sebastian Jansson09408112018-04-24 14:41:22 +020079 PacketReceiver* receiver,
Erik Språng09708512018-03-14 15:16:50 +010080 uint64_t seed)
81 : clock_(clock),
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020082 network_simulation_(rtc::MakeUnique<SimulatedNetwork>(config, seed)),
Sebastian Jansson09408112018-04-24 14:41:22 +020083 receiver_(receiver),
Erik Språng09708512018-03-14 15:16:50 +010084 transport_(nullptr),
Sebastian Jansson7e85d672018-04-06 09:56:21 +020085 clock_offset_ms_(0),
Erik Språng09708512018-03-14 15:16:50 +010086 dropped_packets_(0),
87 sent_packets_(0),
Sebastian Jansson512bdce2018-04-23 13:15:04 +020088 total_packet_delay_us_(0),
Sebastian Jansson512bdce2018-04-23 13:15:04 +020089 next_process_time_us_(clock_->TimeInMicroseconds()),
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020090 last_log_time_us_(clock_->TimeInMicroseconds()) {}
Erik Språng09708512018-03-14 15:16:50 +010091
92FakeNetworkPipe::FakeNetworkPipe(Clock* clock,
93 const FakeNetworkPipe::Config& config,
94 Transport* transport)
95 : clock_(clock),
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020096 network_simulation_(rtc::MakeUnique<SimulatedNetwork>(config, 1)),
Erik Språng09708512018-03-14 15:16:50 +010097 receiver_(nullptr),
98 transport_(transport),
Sebastian Jansson7e85d672018-04-06 09:56:21 +020099 clock_offset_ms_(0),
Erik Språng09708512018-03-14 15:16:50 +0100100 dropped_packets_(0),
101 sent_packets_(0),
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200102 total_packet_delay_us_(0),
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200103 next_process_time_us_(clock_->TimeInMicroseconds()),
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200104 last_log_time_us_(clock_->TimeInMicroseconds()) {}
Erik Språng09708512018-03-14 15:16:50 +0100105
106FakeNetworkPipe::~FakeNetworkPipe() = default;
107
108void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) {
109 rtc::CritScope crit(&config_lock_);
Erik Språng09708512018-03-14 15:16:50 +0100110 receiver_ = receiver;
111}
112
113bool FakeNetworkPipe::SendRtp(const uint8_t* packet,
114 size_t length,
115 const PacketOptions& options) {
116 RTC_DCHECK(HasTransport());
117 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), options, false,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200118 MediaType::ANY, absl::nullopt);
Erik Språng09708512018-03-14 15:16:50 +0100119 return true;
120}
121
122bool FakeNetworkPipe::SendRtcp(const uint8_t* packet, size_t length) {
123 RTC_DCHECK(HasTransport());
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200124 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), absl::nullopt, true,
125 MediaType::ANY, absl::nullopt);
Erik Språng09708512018-03-14 15:16:50 +0100126 return true;
127}
128
129PacketReceiver::DeliveryStatus FakeNetworkPipe::DeliverPacket(
130 MediaType media_type,
131 rtc::CopyOnWriteBuffer packet,
132 const PacketTime& packet_time) {
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200133 return EnqueuePacket(std::move(packet), absl::nullopt, false, media_type,
Erik Språng09708512018-03-14 15:16:50 +0100134 packet_time)
135 ? PacketReceiver::DELIVERY_OK
136 : PacketReceiver::DELIVERY_PACKET_ERROR;
137}
138
Sebastian Jansson7e85d672018-04-06 09:56:21 +0200139void FakeNetworkPipe::SetClockOffset(int64_t offset_ms) {
140 rtc::CritScope crit(&config_lock_);
141 clock_offset_ms_ = offset_ms;
142}
143
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200144SimulatedNetwork::SimulatedNetwork(SimulatedNetwork::Config config,
145 uint64_t random_seed)
146 : random_(random_seed), bursting_(false) {
147 SetConfig(config);
148}
149
Erik Språng09708512018-03-14 15:16:50 +0100150void FakeNetworkPipe::SetConfig(const FakeNetworkPipe::Config& config) {
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200151 network_simulation_->SetConfig(config);
152}
153
154void SimulatedNetwork::SetConfig(const SimulatedNetwork::Config& config) {
Erik Språng09708512018-03-14 15:16:50 +0100155 rtc::CritScope crit(&config_lock_);
156 config_ = config; // Shallow copy of the struct.
157 double prob_loss = config.loss_percent / 100.0;
158 if (config_.avg_burst_loss_length == -1) {
159 // Uniform loss
160 prob_loss_bursting_ = prob_loss;
161 prob_start_bursting_ = prob_loss;
162 } else {
163 // Lose packets according to a gilbert-elliot model.
164 int avg_burst_loss_length = config.avg_burst_loss_length;
165 int min_avg_burst_loss_length = std::ceil(prob_loss / (1 - prob_loss));
166
167 RTC_CHECK_GT(avg_burst_loss_length, min_avg_burst_loss_length)
168 << "For a total packet loss of " << config.loss_percent << "%% then"
169 << " avg_burst_loss_length must be " << min_avg_burst_loss_length + 1
170 << " or higher.";
171
172 prob_loss_bursting_ = (1.0 - 1.0 / avg_burst_loss_length);
173 prob_start_bursting_ = prob_loss / (1 - prob_loss) / avg_burst_loss_length;
174 }
175}
176
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200177bool SimulatedNetwork::EnqueuePacket(PacketInFlightInfo packet) {
Erik Språng09708512018-03-14 15:16:50 +0100178 Config config;
179 {
180 rtc::CritScope crit(&config_lock_);
181 config = config_;
182 }
183 rtc::CritScope crit(&process_lock_);
184 if (config.queue_length_packets > 0 &&
185 capacity_link_.size() >= config.queue_length_packets) {
186 // Too many packet on the link, drop this one.
Erik Språng09708512018-03-14 15:16:50 +0100187 return false;
188 }
189
Erik Språng09708512018-03-14 15:16:50 +0100190 // Delay introduced by the link capacity.
191 int64_t capacity_delay_ms = 0;
192 if (config.link_capacity_kbps > 0) {
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200193 // Using bytes per millisecond to avoid losing precision.
194 const int64_t bytes_per_millisecond = config.link_capacity_kbps / 8;
Erik Språng09708512018-03-14 15:16:50 +0100195 // To round to the closest millisecond we add half a milliseconds worth of
196 // bytes to the delay calculation.
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200197 capacity_delay_ms = (packet.size + capacity_delay_error_bytes_ +
Erik Språng09708512018-03-14 15:16:50 +0100198 bytes_per_millisecond / 2) /
199 bytes_per_millisecond;
200 capacity_delay_error_bytes_ +=
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200201 packet.size - capacity_delay_ms * bytes_per_millisecond;
Erik Språng09708512018-03-14 15:16:50 +0100202 }
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200203 int64_t network_start_time_us = packet.send_time_us;
Erik Språng09708512018-03-14 15:16:50 +0100204
205 // Check if there already are packets on the link and change network start
206 // time forward if there is.
207 if (!capacity_link_.empty() &&
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200208 network_start_time_us < capacity_link_.back().arrival_time_us)
209 network_start_time_us = capacity_link_.back().arrival_time_us;
Erik Språng09708512018-03-14 15:16:50 +0100210
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200211 int64_t arrival_time_us = network_start_time_us + capacity_delay_ms * 1000;
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200212 capacity_link_.push({packet, arrival_time_us});
Erik Språng09708512018-03-14 15:16:50 +0100213 return true;
214}
215
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200216absl::optional<int64_t> SimulatedNetwork::NextDeliveryTimeUs() const {
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200217 if (!delay_link_.empty())
218 return delay_link_.begin()->arrival_time_us;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200219 return absl::nullopt;
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200220}
221
222FakeNetworkPipe::StoredPacket::StoredPacket(NetworkPacket&& packet)
223 : packet(std::move(packet)) {}
224
225bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200226 absl::optional<PacketOptions> options,
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200227 bool is_rtcp,
228 MediaType media_type,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200229 absl::optional<PacketTime> packet_time) {
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200230 int64_t time_now_us = clock_->TimeInMicroseconds();
231 rtc::CritScope crit(&process_lock_);
232 size_t packet_size = packet.size();
233 NetworkPacket net_packet(std::move(packet), time_now_us, time_now_us, options,
234 is_rtcp, media_type, packet_time);
235
236 packets_in_flight_.emplace_back(StoredPacket(std::move(net_packet)));
237 int64_t packet_id = reinterpret_cast<uint64_t>(&packets_in_flight_.back());
238 bool sent = network_simulation_->EnqueuePacket(
239 PacketInFlightInfo(packet_size, time_now_us, packet_id));
240
241 if (!sent) {
242 packets_in_flight_.pop_back();
243 ++dropped_packets_;
244 }
245 return sent;
246}
247
Erik Språng09708512018-03-14 15:16:50 +0100248float FakeNetworkPipe::PercentageLoss() {
249 rtc::CritScope crit(&process_lock_);
250 if (sent_packets_ == 0)
251 return 0;
252
253 return static_cast<float>(dropped_packets_) /
254 (sent_packets_ + dropped_packets_);
255}
256
257int FakeNetworkPipe::AverageDelay() {
258 rtc::CritScope crit(&process_lock_);
259 if (sent_packets_ == 0)
260 return 0;
261
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200262 return static_cast<int>(total_packet_delay_us_ /
263 (1000 * static_cast<int64_t>(sent_packets_)));
Erik Språng09708512018-03-14 15:16:50 +0100264}
265
266size_t FakeNetworkPipe::DroppedPackets() {
267 rtc::CritScope crit(&process_lock_);
268 return dropped_packets_;
269}
270
271size_t FakeNetworkPipe::SentPackets() {
272 rtc::CritScope crit(&process_lock_);
273 return sent_packets_;
274}
275
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200276std::vector<PacketDeliveryInfo> SimulatedNetwork::DequeueDeliverablePackets(
277 int64_t receive_time_us) {
278 int64_t time_now_us = receive_time_us;
Erik Språng09708512018-03-14 15:16:50 +0100279 Config config;
280 double prob_loss_bursting;
281 double prob_start_bursting;
282 {
283 rtc::CritScope crit(&config_lock_);
284 config = config_;
285 prob_loss_bursting = prob_loss_bursting_;
286 prob_start_bursting = prob_start_bursting_;
287 }
288 {
289 rtc::CritScope crit(&process_lock_);
Erik Språng09708512018-03-14 15:16:50 +0100290 // Check the capacity link first.
291 if (!capacity_link_.empty()) {
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200292 int64_t last_arrival_time_us =
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200293 delay_link_.empty() ? -1 : delay_link_.back().arrival_time_us;
Erik Språng09708512018-03-14 15:16:50 +0100294 bool needs_sort = false;
295 while (!capacity_link_.empty() &&
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200296 time_now_us >= capacity_link_.front().arrival_time_us) {
Erik Språng09708512018-03-14 15:16:50 +0100297 // Time to get this packet.
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200298 PacketInfo packet = std::move(capacity_link_.front());
Erik Språng09708512018-03-14 15:16:50 +0100299 capacity_link_.pop();
300
301 // Drop packets at an average rate of |config_.loss_percent| with
302 // and average loss burst length of |config_.avg_burst_loss_length|.
303 if ((bursting_ && random_.Rand<double>() < prob_loss_bursting) ||
304 (!bursting_ && random_.Rand<double>() < prob_start_bursting)) {
305 bursting_ = true;
306 continue;
307 } else {
308 bursting_ = false;
309 }
310
Sebastian Janssone6c09642018-05-11 10:53:02 +0200311 int64_t arrival_time_jitter_us = std::max(
312 random_.Gaussian(config.queue_delay_ms * 1000,
313 config.delay_standard_deviation_ms * 1000),
314 0.0);
Erik Språng09708512018-03-14 15:16:50 +0100315
316 // If reordering is not allowed then adjust arrival_time_jitter
317 // to make sure all packets are sent in order.
318 if (!config.allow_reordering && !delay_link_.empty() &&
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200319 packet.arrival_time_us + arrival_time_jitter_us <
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200320 last_arrival_time_us) {
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200321 arrival_time_jitter_us =
322 last_arrival_time_us - packet.arrival_time_us;
Erik Språng09708512018-03-14 15:16:50 +0100323 }
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200324 packet.arrival_time_us += arrival_time_jitter_us;
325 if (packet.arrival_time_us >= last_arrival_time_us) {
326 last_arrival_time_us = packet.arrival_time_us;
Erik Språng09708512018-03-14 15:16:50 +0100327 } else {
328 needs_sort = true;
329 }
330 delay_link_.emplace_back(std::move(packet));
331 }
332
333 if (needs_sort) {
334 // Packet(s) arrived out of order, make sure list is sorted.
335 std::sort(delay_link_.begin(), delay_link_.end(),
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200336 [](const PacketInfo& p1, const PacketInfo& p2) {
337 return p1.arrival_time_us < p2.arrival_time_us;
338 });
Erik Språng09708512018-03-14 15:16:50 +0100339 }
340 }
341
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200342 std::vector<PacketDeliveryInfo> packets_to_deliver;
Erik Språng09708512018-03-14 15:16:50 +0100343 // Check the extra delay queue.
344 while (!delay_link_.empty() &&
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200345 time_now_us >= delay_link_.front().arrival_time_us) {
346 PacketInfo packet_info = delay_link_.front();
347 packets_to_deliver.emplace_back(
348 PacketDeliveryInfo(packet_info.packet, packet_info.arrival_time_us));
Erik Språng09708512018-03-14 15:16:50 +0100349 delay_link_.pop_front();
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200350 }
351 return packets_to_deliver;
352 }
353}
354
355void FakeNetworkPipe::Process() {
356 int64_t time_now_us = clock_->TimeInMicroseconds();
357 std::queue<NetworkPacket> packets_to_deliver;
358 {
359 rtc::CritScope crit(&process_lock_);
360 if (time_now_us - last_log_time_us_ > kLogIntervalMs * 1000) {
361 int64_t queueing_delay_us = 0;
362 if (!packets_in_flight_.empty())
363 queueing_delay_us =
364 time_now_us - packets_in_flight_.front().packet.send_time();
365
366 RTC_LOG(LS_INFO) << "Network queue: " << queueing_delay_us / 1000
367 << " ms.";
368 last_log_time_us_ = time_now_us;
369 }
370
371 std::vector<PacketDeliveryInfo> delivery_infos =
372 network_simulation_->DequeueDeliverablePackets(time_now_us);
373 for (auto& delivery_info : delivery_infos) {
374 // In the common case where no reordering happens, find will return early
375 // as the first packet will be a match.
376 auto packet_it =
377 std::find_if(packets_in_flight_.begin(), packets_in_flight_.end(),
378 [&delivery_info](StoredPacket& packet_ref) {
379 return reinterpret_cast<uint64_t>(&packet_ref) ==
380 delivery_info.packet_id;
381 });
382 // Check that the packet is in the deque of packets in flight.
383 RTC_CHECK(packet_it != packets_in_flight_.end());
384 // Check that the packet is not already removed.
385 RTC_DCHECK(!packet_it->removed);
386
387 NetworkPacket packet = std::move(packet_it->packet);
388 packet_it->removed = true;
389
390 // Cleanup of removed packets at the beginning of the deque.
391 while (!packets_in_flight_.empty() &&
392 packets_in_flight_.front().removed) {
393 packets_in_flight_.pop_front();
394 }
395
396 if (delivery_info.receive_time_us != PacketDeliveryInfo::kNotReceived) {
397 int64_t added_delay_us =
398 delivery_info.receive_time_us - packet.send_time();
399 packet.IncrementArrivalTime(added_delay_us);
400 packets_to_deliver.emplace(std::move(packet));
401 // |time_now_us| might be later than when the packet should have
402 // arrived, due to NetworkProcess being called too late. For stats, use
403 // the time it should have been on the link.
404 total_packet_delay_us_ += added_delay_us;
405 }
Erik Språng09708512018-03-14 15:16:50 +0100406 }
407 sent_packets_ += packets_to_deliver.size();
408 }
409
410 rtc::CritScope crit(&config_lock_);
411 while (!packets_to_deliver.empty()) {
412 NetworkPacket packet = std::move(packets_to_deliver.front());
413 packets_to_deliver.pop();
414 DeliverPacket(&packet);
415 }
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200416 absl::optional<int64_t> delivery_us =
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200417 network_simulation_->NextDeliveryTimeUs();
418 next_process_time_us_ = delivery_us
419 ? *delivery_us
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200420 : time_now_us + kDefaultProcessIntervalMs * 1000;
Erik Språng09708512018-03-14 15:16:50 +0100421}
422
423void FakeNetworkPipe::DeliverPacket(NetworkPacket* packet) {
Sebastian Janssona44ab182018-04-06 12:59:14 +0200424 if (transport_) {
425 RTC_DCHECK(!receiver_);
Erik Språng09708512018-03-14 15:16:50 +0100426 if (packet->is_rtcp()) {
427 transport_->SendRtcp(packet->data(), packet->data_length());
428 } else {
429 transport_->SendRtp(packet->data(), packet->data_length(),
430 packet->packet_options());
431 }
Sebastian Jansson09408112018-04-24 14:41:22 +0200432 } else if (receiver_) {
Erik Språng09708512018-03-14 15:16:50 +0100433 PacketTime packet_time = packet->packet_time();
434 if (packet_time.timestamp != -1) {
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200435 int64_t queue_time_us = packet->arrival_time() - packet->send_time();
436 RTC_CHECK(queue_time_us >= 0);
437 packet_time.timestamp += queue_time_us;
Sebastian Jansson7e85d672018-04-06 09:56:21 +0200438 packet_time.timestamp += (clock_offset_ms_ * 1000);
Erik Språng09708512018-03-14 15:16:50 +0100439 }
Sebastian Jansson09408112018-04-24 14:41:22 +0200440 receiver_->DeliverPacket(packet->media_type(),
441 std::move(*packet->raw_packet()), packet_time);
Erik Språng09708512018-03-14 15:16:50 +0100442 }
443}
444
445int64_t FakeNetworkPipe::TimeUntilNextProcess() {
446 rtc::CritScope crit(&process_lock_);
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200447 int64_t delay_us = next_process_time_us_ - clock_->TimeInMicroseconds();
448 return std::max<int64_t>((delay_us + 500) / 1000, 0);
Erik Språng09708512018-03-14 15:16:50 +0100449}
450
451bool FakeNetworkPipe::HasTransport() const {
452 rtc::CritScope crit(&config_lock_);
453 return transport_ != nullptr;
454}
Sebastian Jansson09408112018-04-24 14:41:22 +0200455bool FakeNetworkPipe::HasReceiver() const {
Erik Språng09708512018-03-14 15:16:50 +0100456 rtc::CritScope crit(&config_lock_);
Sebastian Jansson09408112018-04-24 14:41:22 +0200457 return receiver_ != nullptr;
Erik Språng09708512018-03-14 15:16:50 +0100458}
459
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100460void FakeNetworkPipe::DeliverPacketWithLock(NetworkPacket* packet) {
461 rtc::CritScope crit(&config_lock_);
462 DeliverPacket(packet);
463}
464
465void FakeNetworkPipe::ResetStats() {
466 rtc::CritScope crit(&process_lock_);
467 dropped_packets_ = 0;
468 sent_packets_ = 0;
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200469 total_packet_delay_us_ = 0;
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100470}
471
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100472void FakeNetworkPipe::AddToPacketDropCount() {
473 rtc::CritScope crit(&process_lock_);
474 ++dropped_packets_;
475}
476
477void FakeNetworkPipe::AddToPacketSentCount(int count) {
478 rtc::CritScope crit(&process_lock_);
479 sent_packets_ += count;
480}
481
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200482void FakeNetworkPipe::AddToTotalDelay(int delay_us) {
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100483 rtc::CritScope crit(&process_lock_);
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200484 total_packet_delay_us_ += delay_us;
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100485}
486
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200487int64_t FakeNetworkPipe::GetTimeInMicroseconds() const {
488 return clock_->TimeInMicroseconds();
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100489}
490
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200491bool FakeNetworkPipe::ShouldProcess(int64_t time_now_us) const {
492 return time_now_us >= next_process_time_us_;
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100493}
494
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200495void FakeNetworkPipe::SetTimeToNextProcess(int64_t skip_us) {
496 next_process_time_us_ += skip_us;
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100497}
498
Erik Språng09708512018-03-14 15:16:50 +0100499} // namespace webrtc