blob: 46adcb47a98d688f457d4c4a05f61e027c103df4 [file] [log] [blame]
Erik Språng09708512018-03-14 14:16:501/*
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
Erik Språng09708512018-03-14 14:16:5011#include <string.h>
Erik Språng09708512018-03-14 14:16:5012#include <algorithm>
Yves Gerey3e707812018-11-28 15:47:4913#include <queue>
Erik Språng09708512018-03-14 14:16:5014#include <utility>
Yves Gerey3e707812018-11-28 15:47:4915#include <vector>
Erik Språng09708512018-03-14 14:16:5016
Steve Anton10542f22019-01-11 17:11:0017#include "api/media_types.h"
Erik Språng09708512018-03-14 14:16:5018#include "call/fake_network_pipe.h"
Sebastian Jansson71822862018-10-30 07:23:2719#include "modules/utility/include/process_thread.h"
Yves Gerey3e707812018-11-28 15:47:4920#include "rtc_base/checks.h"
Erik Språng09708512018-03-14 14:16:5021#include "rtc_base/logging.h"
22#include "system_wrappers/include/clock.h"
23
24namespace webrtc {
25
26namespace {
Sebastian Jansson512bdce2018-04-23 11:15:0427constexpr int64_t kLogIntervalMs = 5000;
Erik Språng09708512018-03-14 14:16:5028} // namespace
29
30NetworkPacket::NetworkPacket(rtc::CopyOnWriteBuffer packet,
31 int64_t send_time,
32 int64_t arrival_time,
Danil Chapovalovb9b146c2018-06-15 10:28:0733 absl::optional<PacketOptions> packet_options,
Erik Språng09708512018-03-14 14:16:5034 bool is_rtcp,
35 MediaType media_type,
Niels Möller70082872018-08-07 09:03:1236 absl::optional<int64_t> packet_time_us)
Erik Språng09708512018-03-14 14:16:5037 : packet_(std::move(packet)),
38 send_time_(send_time),
39 arrival_time_(arrival_time),
40 packet_options_(packet_options),
41 is_rtcp_(is_rtcp),
42 media_type_(media_type),
Niels Möller70082872018-08-07 09:03:1243 packet_time_us_(packet_time_us) {}
Artem Titov537b7fe2018-08-16 09:20:4544
Erik Språng09708512018-03-14 14:16:5045NetworkPacket::NetworkPacket(NetworkPacket&& o)
46 : packet_(std::move(o.packet_)),
47 send_time_(o.send_time_),
48 arrival_time_(o.arrival_time_),
49 packet_options_(o.packet_options_),
50 is_rtcp_(o.is_rtcp_),
51 media_type_(o.media_type_),
Niels Möller70082872018-08-07 09:03:1252 packet_time_us_(o.packet_time_us_) {}
Erik Språng09708512018-03-14 14:16:5053
Mirko Bonadeied1dcf92018-07-26 07:15:1154NetworkPacket::~NetworkPacket() = default;
55
Erik Språng09708512018-03-14 14:16:5056NetworkPacket& 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_;
Niels Möller70082872018-08-07 09:03:1263 packet_time_us_ = o.packet_time_us_;
Erik Språng09708512018-03-14 14:16:5064
65 return *this;
66}
67
Artem Titovb0050872018-08-16 15:02:2068FakeNetworkPipe::FakeNetworkPipe(
69 Clock* clock,
Artem Titov8ea1e9d2018-10-04 12:46:3170 std::unique_ptr<NetworkBehaviorInterface> network_behavior)
71 : FakeNetworkPipe(clock, std::move(network_behavior), nullptr, 1) {}
Artem Titovb0050872018-08-16 15:02:2072
73FakeNetworkPipe::FakeNetworkPipe(
74 Clock* clock,
Artem Titov8ea1e9d2018-10-04 12:46:3175 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
Artem Titovb0050872018-08-16 15:02:2076 PacketReceiver* receiver)
Artem Titov8ea1e9d2018-10-04 12:46:3177 : FakeNetworkPipe(clock, std::move(network_behavior), receiver, 1) {}
Artem Titovb0050872018-08-16 15:02:2078
79FakeNetworkPipe::FakeNetworkPipe(
80 Clock* clock,
Artem Titov8ea1e9d2018-10-04 12:46:3181 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
Artem Titovb0050872018-08-16 15:02:2082 PacketReceiver* receiver,
83 uint64_t seed)
84 : clock_(clock),
Artem Titov8ea1e9d2018-10-04 12:46:3185 network_behavior_(std::move(network_behavior)),
Artem Titovb0050872018-08-16 15:02:2086 receiver_(receiver),
87 transport_(nullptr),
88 clock_offset_ms_(0),
89 dropped_packets_(0),
90 sent_packets_(0),
91 total_packet_delay_us_(0),
Artem Titovb0050872018-08-16 15:02:2092 last_log_time_us_(clock_->TimeInMicroseconds()) {}
93
94FakeNetworkPipe::FakeNetworkPipe(
95 Clock* clock,
Artem Titov8ea1e9d2018-10-04 12:46:3196 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
Artem Titovb0050872018-08-16 15:02:2097 Transport* transport)
98 : clock_(clock),
Artem Titov8ea1e9d2018-10-04 12:46:3199 network_behavior_(std::move(network_behavior)),
Artem Titovb0050872018-08-16 15:02:20100 receiver_(nullptr),
101 transport_(transport),
102 clock_offset_ms_(0),
103 dropped_packets_(0),
104 sent_packets_(0),
105 total_packet_delay_us_(0),
Artem Titovb0050872018-08-16 15:02:20106 last_log_time_us_(clock_->TimeInMicroseconds()) {}
107
Erik Språng09708512018-03-14 14:16:50108FakeNetworkPipe::~FakeNetworkPipe() = default;
109
110void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) {
111 rtc::CritScope crit(&config_lock_);
Erik Språng09708512018-03-14 14:16:50112 receiver_ = receiver;
113}
114
115bool FakeNetworkPipe::SendRtp(const uint8_t* packet,
116 size_t length,
117 const PacketOptions& options) {
118 RTC_DCHECK(HasTransport());
119 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), options, false,
Niels Möller70082872018-08-07 09:03:12120 MediaType::ANY);
Erik Språng09708512018-03-14 14:16:50121 return true;
122}
123
124bool FakeNetworkPipe::SendRtcp(const uint8_t* packet, size_t length) {
125 RTC_DCHECK(HasTransport());
Danil Chapovalovb9b146c2018-06-15 10:28:07126 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), absl::nullopt, true,
Niels Möller70082872018-08-07 09:03:12127 MediaType::ANY);
Erik Språng09708512018-03-14 14:16:50128 return true;
129}
130
131PacketReceiver::DeliveryStatus FakeNetworkPipe::DeliverPacket(
132 MediaType media_type,
133 rtc::CopyOnWriteBuffer packet,
Niels Möller70082872018-08-07 09:03:12134 int64_t packet_time_us) {
Danil Chapovalovb9b146c2018-06-15 10:28:07135 return EnqueuePacket(std::move(packet), absl::nullopt, false, media_type,
Niels Möller70082872018-08-07 09:03:12136 packet_time_us)
Erik Språng09708512018-03-14 14:16:50137 ? PacketReceiver::DELIVERY_OK
138 : PacketReceiver::DELIVERY_PACKET_ERROR;
139}
140
Sebastian Jansson7e85d672018-04-06 07:56:21141void FakeNetworkPipe::SetClockOffset(int64_t offset_ms) {
142 rtc::CritScope crit(&config_lock_);
143 clock_offset_ms_ = offset_ms;
144}
145
Sebastian Jansson7ee2e252018-05-07 12:49:39146FakeNetworkPipe::StoredPacket::StoredPacket(NetworkPacket&& packet)
147 : packet(std::move(packet)) {}
148
149bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet,
Danil Chapovalovb9b146c2018-06-15 10:28:07150 absl::optional<PacketOptions> options,
Sebastian Jansson7ee2e252018-05-07 12:49:39151 bool is_rtcp,
152 MediaType media_type,
Niels Möller70082872018-08-07 09:03:12153 absl::optional<int64_t> packet_time_us) {
Sebastian Jansson7ee2e252018-05-07 12:49:39154 rtc::CritScope crit(&process_lock_);
Christoffer Rodbro1803bb22018-10-25 10:39:49155 int64_t time_now_us = clock_->TimeInMicroseconds();
Sebastian Jansson7ee2e252018-05-07 12:49:39156 size_t packet_size = packet.size();
Niels Möllerf189c482018-08-17 07:49:20157 NetworkPacket net_packet(std::move(packet), time_now_us, time_now_us, options,
158 is_rtcp, media_type, packet_time_us);
Sebastian Jansson7ee2e252018-05-07 12:49:39159
160 packets_in_flight_.emplace_back(StoredPacket(std::move(net_packet)));
161 int64_t packet_id = reinterpret_cast<uint64_t>(&packets_in_flight_.back());
Artem Titov8ea1e9d2018-10-04 12:46:31162 bool sent = network_behavior_->EnqueuePacket(
Sebastian Jansson7ee2e252018-05-07 12:49:39163 PacketInFlightInfo(packet_size, time_now_us, packet_id));
164
165 if (!sent) {
166 packets_in_flight_.pop_back();
167 ++dropped_packets_;
168 }
169 return sent;
170}
171
Erik Språng09708512018-03-14 14:16:50172float FakeNetworkPipe::PercentageLoss() {
173 rtc::CritScope crit(&process_lock_);
174 if (sent_packets_ == 0)
175 return 0;
176
177 return static_cast<float>(dropped_packets_) /
178 (sent_packets_ + dropped_packets_);
179}
180
181int FakeNetworkPipe::AverageDelay() {
182 rtc::CritScope crit(&process_lock_);
183 if (sent_packets_ == 0)
184 return 0;
185
Sebastian Jansson512bdce2018-04-23 11:15:04186 return static_cast<int>(total_packet_delay_us_ /
187 (1000 * static_cast<int64_t>(sent_packets_)));
Erik Språng09708512018-03-14 14:16:50188}
189
190size_t FakeNetworkPipe::DroppedPackets() {
191 rtc::CritScope crit(&process_lock_);
192 return dropped_packets_;
193}
194
195size_t FakeNetworkPipe::SentPackets() {
196 rtc::CritScope crit(&process_lock_);
197 return sent_packets_;
198}
199
Sebastian Jansson7ee2e252018-05-07 12:49:39200void FakeNetworkPipe::Process() {
Christoffer Rodbro1803bb22018-10-25 10:39:49201 int64_t time_now_us;
Sebastian Jansson7ee2e252018-05-07 12:49:39202 std::queue<NetworkPacket> packets_to_deliver;
203 {
204 rtc::CritScope crit(&process_lock_);
Christoffer Rodbro1803bb22018-10-25 10:39:49205 time_now_us = clock_->TimeInMicroseconds();
Sebastian Jansson7ee2e252018-05-07 12:49:39206 if (time_now_us - last_log_time_us_ > kLogIntervalMs * 1000) {
207 int64_t queueing_delay_us = 0;
208 if (!packets_in_flight_.empty())
209 queueing_delay_us =
210 time_now_us - packets_in_flight_.front().packet.send_time();
211
212 RTC_LOG(LS_INFO) << "Network queue: " << queueing_delay_us / 1000
213 << " ms.";
214 last_log_time_us_ = time_now_us;
215 }
216
217 std::vector<PacketDeliveryInfo> delivery_infos =
Artem Titov8ea1e9d2018-10-04 12:46:31218 network_behavior_->DequeueDeliverablePackets(time_now_us);
Sebastian Jansson7ee2e252018-05-07 12:49:39219 for (auto& delivery_info : delivery_infos) {
220 // In the common case where no reordering happens, find will return early
221 // as the first packet will be a match.
222 auto packet_it =
223 std::find_if(packets_in_flight_.begin(), packets_in_flight_.end(),
224 [&delivery_info](StoredPacket& packet_ref) {
225 return reinterpret_cast<uint64_t>(&packet_ref) ==
226 delivery_info.packet_id;
227 });
228 // Check that the packet is in the deque of packets in flight.
229 RTC_CHECK(packet_it != packets_in_flight_.end());
230 // Check that the packet is not already removed.
231 RTC_DCHECK(!packet_it->removed);
232
233 NetworkPacket packet = std::move(packet_it->packet);
234 packet_it->removed = true;
235
236 // Cleanup of removed packets at the beginning of the deque.
237 while (!packets_in_flight_.empty() &&
238 packets_in_flight_.front().removed) {
239 packets_in_flight_.pop_front();
240 }
241
242 if (delivery_info.receive_time_us != PacketDeliveryInfo::kNotReceived) {
243 int64_t added_delay_us =
244 delivery_info.receive_time_us - packet.send_time();
245 packet.IncrementArrivalTime(added_delay_us);
246 packets_to_deliver.emplace(std::move(packet));
247 // |time_now_us| might be later than when the packet should have
248 // arrived, due to NetworkProcess being called too late. For stats, use
249 // the time it should have been on the link.
250 total_packet_delay_us_ += added_delay_us;
Christoffer Rodbro3284b612018-10-23 14:51:51251 ++sent_packets_;
252 } else {
253 ++dropped_packets_;
Sebastian Jansson7ee2e252018-05-07 12:49:39254 }
Erik Språng09708512018-03-14 14:16:50255 }
Erik Språng09708512018-03-14 14:16:50256 }
257
258 rtc::CritScope crit(&config_lock_);
259 while (!packets_to_deliver.empty()) {
260 NetworkPacket packet = std::move(packets_to_deliver.front());
261 packets_to_deliver.pop();
Niels Möller70082872018-08-07 09:03:12262 DeliverNetworkPacket(&packet);
Erik Språng09708512018-03-14 14:16:50263 }
Erik Språng09708512018-03-14 14:16:50264}
265
Niels Möller70082872018-08-07 09:03:12266void FakeNetworkPipe::DeliverNetworkPacket(NetworkPacket* packet) {
Sebastian Janssona44ab182018-04-06 10:59:14267 if (transport_) {
268 RTC_DCHECK(!receiver_);
Erik Språng09708512018-03-14 14:16:50269 if (packet->is_rtcp()) {
270 transport_->SendRtcp(packet->data(), packet->data_length());
271 } else {
272 transport_->SendRtp(packet->data(), packet->data_length(),
273 packet->packet_options());
274 }
Sebastian Jansson09408112018-04-24 12:41:22275 } else if (receiver_) {
Niels Möller70082872018-08-07 09:03:12276 int64_t packet_time_us = packet->packet_time_us().value_or(-1);
277 if (packet_time_us != -1) {
Sebastian Jansson512bdce2018-04-23 11:15:04278 int64_t queue_time_us = packet->arrival_time() - packet->send_time();
279 RTC_CHECK(queue_time_us >= 0);
Niels Möller70082872018-08-07 09:03:12280 packet_time_us += queue_time_us;
281 packet_time_us += (clock_offset_ms_ * 1000);
Erik Språng09708512018-03-14 14:16:50282 }
Sebastian Jansson09408112018-04-24 12:41:22283 receiver_->DeliverPacket(packet->media_type(),
Niels Möller70082872018-08-07 09:03:12284 std::move(*packet->raw_packet()), packet_time_us);
Erik Språng09708512018-03-14 14:16:50285 }
286}
287
Sebastian Jansson836fee12019-02-08 15:08:10288absl::optional<int64_t> FakeNetworkPipe::TimeUntilNextProcess() {
Erik Språng09708512018-03-14 14:16:50289 rtc::CritScope crit(&process_lock_);
Sebastian Jansson71822862018-10-30 07:23:27290 absl::optional<int64_t> delivery_us = network_behavior_->NextDeliveryTimeUs();
291 if (delivery_us) {
292 int64_t delay_us = *delivery_us - clock_->TimeInMicroseconds();
293 return std::max<int64_t>((delay_us + 500) / 1000, 0);
294 }
Sebastian Jansson836fee12019-02-08 15:08:10295 return absl::nullopt;
Erik Språng09708512018-03-14 14:16:50296}
297
298bool FakeNetworkPipe::HasTransport() const {
299 rtc::CritScope crit(&config_lock_);
300 return transport_ != nullptr;
301}
Sebastian Jansson09408112018-04-24 12:41:22302bool FakeNetworkPipe::HasReceiver() const {
Erik Språng09708512018-03-14 14:16:50303 rtc::CritScope crit(&config_lock_);
Sebastian Jansson09408112018-04-24 12:41:22304 return receiver_ != nullptr;
Erik Språng09708512018-03-14 14:16:50305}
306
Christoffer Rodbro8ef59a42018-03-20 13:34:01307void FakeNetworkPipe::DeliverPacketWithLock(NetworkPacket* packet) {
308 rtc::CritScope crit(&config_lock_);
Niels Möller70082872018-08-07 09:03:12309 DeliverNetworkPacket(packet);
Christoffer Rodbro8ef59a42018-03-20 13:34:01310}
311
312void FakeNetworkPipe::ResetStats() {
313 rtc::CritScope crit(&process_lock_);
314 dropped_packets_ = 0;
315 sent_packets_ = 0;
Sebastian Jansson512bdce2018-04-23 11:15:04316 total_packet_delay_us_ = 0;
Christoffer Rodbro8ef59a42018-03-20 13:34:01317}
318
Sebastian Jansson512bdce2018-04-23 11:15:04319int64_t FakeNetworkPipe::GetTimeInMicroseconds() const {
320 return clock_->TimeInMicroseconds();
Christoffer Rodbro8ef59a42018-03-20 13:34:01321}
322
Erik Språng09708512018-03-14 14:16:50323} // namespace webrtc