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 "call/fake_network_pipe.h"
12
13 #include <string.h>
14
15 #include <algorithm>
16 #include <queue>
17 #include <utility>
18 #include <vector>
19
20 #include "api/media_types.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/logging.h"
23 #include "system_wrappers/include/clock.h"
24
25 namespace webrtc {
26
27 namespace {
28 constexpr int64_t kLogIntervalMs = 5000;
29 } // namespace
30
NetworkPacket(rtc::CopyOnWriteBuffer packet,int64_t send_time,int64_t arrival_time,absl::optional<PacketOptions> packet_options,bool is_rtcp,MediaType media_type,absl::optional<int64_t> packet_time_us,Transport * transport)31 NetworkPacket::NetworkPacket(rtc::CopyOnWriteBuffer packet,
32 int64_t send_time,
33 int64_t arrival_time,
34 absl::optional<PacketOptions> packet_options,
35 bool is_rtcp,
36 MediaType media_type,
37 absl::optional<int64_t> packet_time_us,
38 Transport* transport)
39 : 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_us_(packet_time_us),
46 transport_(transport) {}
47
NetworkPacket(NetworkPacket && o)48 NetworkPacket::NetworkPacket(NetworkPacket&& o)
49 : packet_(std::move(o.packet_)),
50 send_time_(o.send_time_),
51 arrival_time_(o.arrival_time_),
52 packet_options_(o.packet_options_),
53 is_rtcp_(o.is_rtcp_),
54 media_type_(o.media_type_),
55 packet_time_us_(o.packet_time_us_),
56 transport_(o.transport_) {}
57
58 NetworkPacket::~NetworkPacket() = default;
59
operator =(NetworkPacket && o)60 NetworkPacket& NetworkPacket::operator=(NetworkPacket&& o) {
61 packet_ = std::move(o.packet_);
62 send_time_ = o.send_time_;
63 arrival_time_ = o.arrival_time_;
64 packet_options_ = o.packet_options_;
65 is_rtcp_ = o.is_rtcp_;
66 media_type_ = o.media_type_;
67 packet_time_us_ = o.packet_time_us_;
68 transport_ = o.transport_;
69
70 return *this;
71 }
72
FakeNetworkPipe(Clock * clock,std::unique_ptr<NetworkBehaviorInterface> network_behavior)73 FakeNetworkPipe::FakeNetworkPipe(
74 Clock* clock,
75 std::unique_ptr<NetworkBehaviorInterface> network_behavior)
76 : FakeNetworkPipe(clock, std::move(network_behavior), nullptr, 1) {}
77
FakeNetworkPipe(Clock * clock,std::unique_ptr<NetworkBehaviorInterface> network_behavior,PacketReceiver * receiver)78 FakeNetworkPipe::FakeNetworkPipe(
79 Clock* clock,
80 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
81 PacketReceiver* receiver)
82 : FakeNetworkPipe(clock, std::move(network_behavior), receiver, 1) {}
83
FakeNetworkPipe(Clock * clock,std::unique_ptr<NetworkBehaviorInterface> network_behavior,PacketReceiver * receiver,uint64_t seed)84 FakeNetworkPipe::FakeNetworkPipe(
85 Clock* clock,
86 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
87 PacketReceiver* receiver,
88 uint64_t seed)
89 : clock_(clock),
90 network_behavior_(std::move(network_behavior)),
91 receiver_(receiver),
92 global_transport_(nullptr),
93 clock_offset_ms_(0),
94 dropped_packets_(0),
95 sent_packets_(0),
96 total_packet_delay_us_(0),
97 last_log_time_us_(clock_->TimeInMicroseconds()) {}
98
FakeNetworkPipe(Clock * clock,std::unique_ptr<NetworkBehaviorInterface> network_behavior,Transport * transport)99 FakeNetworkPipe::FakeNetworkPipe(
100 Clock* clock,
101 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
102 Transport* transport)
103 : clock_(clock),
104 network_behavior_(std::move(network_behavior)),
105 receiver_(nullptr),
106 global_transport_(transport),
107 clock_offset_ms_(0),
108 dropped_packets_(0),
109 sent_packets_(0),
110 total_packet_delay_us_(0),
111 last_log_time_us_(clock_->TimeInMicroseconds()) {
112 RTC_DCHECK(global_transport_);
113 AddActiveTransport(global_transport_);
114 }
115
~FakeNetworkPipe()116 FakeNetworkPipe::~FakeNetworkPipe() {
117 if (global_transport_) {
118 RemoveActiveTransport(global_transport_);
119 }
120 RTC_DCHECK(active_transports_.empty());
121 }
122
SetReceiver(PacketReceiver * receiver)123 void FakeNetworkPipe::SetReceiver(PacketReceiver* receiver) {
124 MutexLock lock(&config_lock_);
125 receiver_ = receiver;
126 }
127
AddActiveTransport(Transport * transport)128 void FakeNetworkPipe::AddActiveTransport(Transport* transport) {
129 MutexLock lock(&config_lock_);
130 active_transports_[transport]++;
131 }
132
RemoveActiveTransport(Transport * transport)133 void FakeNetworkPipe::RemoveActiveTransport(Transport* transport) {
134 MutexLock lock(&config_lock_);
135 auto it = active_transports_.find(transport);
136 RTC_CHECK(it != active_transports_.end());
137 if (--(it->second) == 0) {
138 active_transports_.erase(it);
139 }
140 }
141
SendRtp(const uint8_t * packet,size_t length,const PacketOptions & options)142 bool FakeNetworkPipe::SendRtp(const uint8_t* packet,
143 size_t length,
144 const PacketOptions& options) {
145 RTC_DCHECK(global_transport_);
146 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), options, false,
147 global_transport_);
148 return true;
149 }
150
SendRtcp(const uint8_t * packet,size_t length)151 bool FakeNetworkPipe::SendRtcp(const uint8_t* packet, size_t length) {
152 RTC_DCHECK(global_transport_);
153 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), absl::nullopt, true,
154 global_transport_);
155 return true;
156 }
157
SendRtp(const uint8_t * packet,size_t length,const PacketOptions & options,Transport * transport)158 bool FakeNetworkPipe::SendRtp(const uint8_t* packet,
159 size_t length,
160 const PacketOptions& options,
161 Transport* transport) {
162 RTC_DCHECK(transport);
163 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), options, false,
164 transport);
165 return true;
166 }
167
SendRtcp(const uint8_t * packet,size_t length,Transport * transport)168 bool FakeNetworkPipe::SendRtcp(const uint8_t* packet,
169 size_t length,
170 Transport* transport) {
171 RTC_DCHECK(transport);
172 EnqueuePacket(rtc::CopyOnWriteBuffer(packet, length), absl::nullopt, true,
173 transport);
174 return true;
175 }
176
DeliverPacket(MediaType media_type,rtc::CopyOnWriteBuffer packet,int64_t packet_time_us)177 PacketReceiver::DeliveryStatus FakeNetworkPipe::DeliverPacket(
178 MediaType media_type,
179 rtc::CopyOnWriteBuffer packet,
180 int64_t packet_time_us) {
181 return EnqueuePacket(std::move(packet), absl::nullopt, false, media_type,
182 packet_time_us)
183 ? PacketReceiver::DELIVERY_OK
184 : PacketReceiver::DELIVERY_PACKET_ERROR;
185 }
186
SetClockOffset(int64_t offset_ms)187 void FakeNetworkPipe::SetClockOffset(int64_t offset_ms) {
188 MutexLock lock(&config_lock_);
189 clock_offset_ms_ = offset_ms;
190 }
191
StoredPacket(NetworkPacket && packet)192 FakeNetworkPipe::StoredPacket::StoredPacket(NetworkPacket&& packet)
193 : packet(std::move(packet)) {}
194
EnqueuePacket(rtc::CopyOnWriteBuffer packet,absl::optional<PacketOptions> options,bool is_rtcp,MediaType media_type,absl::optional<int64_t> packet_time_us)195 bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet,
196 absl::optional<PacketOptions> options,
197 bool is_rtcp,
198 MediaType media_type,
199 absl::optional<int64_t> packet_time_us) {
200 MutexLock lock(&process_lock_);
201 int64_t time_now_us = clock_->TimeInMicroseconds();
202 return EnqueuePacket(NetworkPacket(std::move(packet), time_now_us,
203 time_now_us, options, is_rtcp, media_type,
204 packet_time_us, nullptr));
205 }
206
EnqueuePacket(rtc::CopyOnWriteBuffer packet,absl::optional<PacketOptions> options,bool is_rtcp,Transport * transport)207 bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet,
208 absl::optional<PacketOptions> options,
209 bool is_rtcp,
210 Transport* transport) {
211 MutexLock lock(&process_lock_);
212 int64_t time_now_us = clock_->TimeInMicroseconds();
213 return EnqueuePacket(NetworkPacket(std::move(packet), time_now_us,
214 time_now_us, options, is_rtcp,
215 MediaType::ANY, absl::nullopt, transport));
216 }
217
EnqueuePacket(NetworkPacket && net_packet)218 bool FakeNetworkPipe::EnqueuePacket(NetworkPacket&& net_packet) {
219 int64_t send_time_us = net_packet.send_time();
220 size_t packet_size = net_packet.data_length();
221
222 packets_in_flight_.emplace_back(StoredPacket(std::move(net_packet)));
223 int64_t packet_id = reinterpret_cast<uint64_t>(&packets_in_flight_.back());
224 bool sent = network_behavior_->EnqueuePacket(
225 PacketInFlightInfo(packet_size, send_time_us, packet_id));
226
227 if (!sent) {
228 packets_in_flight_.pop_back();
229 ++dropped_packets_;
230 }
231 return sent;
232 }
233
PercentageLoss()234 float FakeNetworkPipe::PercentageLoss() {
235 MutexLock lock(&process_lock_);
236 if (sent_packets_ == 0)
237 return 0;
238
239 return static_cast<float>(dropped_packets_) /
240 (sent_packets_ + dropped_packets_);
241 }
242
AverageDelay()243 int FakeNetworkPipe::AverageDelay() {
244 MutexLock lock(&process_lock_);
245 if (sent_packets_ == 0)
246 return 0;
247
248 return static_cast<int>(total_packet_delay_us_ /
249 (1000 * static_cast<int64_t>(sent_packets_)));
250 }
251
DroppedPackets()252 size_t FakeNetworkPipe::DroppedPackets() {
253 MutexLock lock(&process_lock_);
254 return dropped_packets_;
255 }
256
SentPackets()257 size_t FakeNetworkPipe::SentPackets() {
258 MutexLock lock(&process_lock_);
259 return sent_packets_;
260 }
261
Process()262 void FakeNetworkPipe::Process() {
263 int64_t time_now_us;
264 std::queue<NetworkPacket> packets_to_deliver;
265 {
266 MutexLock lock(&process_lock_);
267 time_now_us = clock_->TimeInMicroseconds();
268 if (time_now_us - last_log_time_us_ > kLogIntervalMs * 1000) {
269 int64_t queueing_delay_us = 0;
270 if (!packets_in_flight_.empty())
271 queueing_delay_us =
272 time_now_us - packets_in_flight_.front().packet.send_time();
273
274 RTC_LOG(LS_INFO) << "Network queue: " << queueing_delay_us / 1000
275 << " ms.";
276 last_log_time_us_ = time_now_us;
277 }
278
279 std::vector<PacketDeliveryInfo> delivery_infos =
280 network_behavior_->DequeueDeliverablePackets(time_now_us);
281 for (auto& delivery_info : delivery_infos) {
282 // In the common case where no reordering happens, find will return early
283 // as the first packet will be a match.
284 auto packet_it =
285 std::find_if(packets_in_flight_.begin(), packets_in_flight_.end(),
286 [&delivery_info](StoredPacket& packet_ref) {
287 return reinterpret_cast<uint64_t>(&packet_ref) ==
288 delivery_info.packet_id;
289 });
290 // Check that the packet is in the deque of packets in flight.
291 RTC_CHECK(packet_it != packets_in_flight_.end());
292 // Check that the packet is not already removed.
293 RTC_DCHECK(!packet_it->removed);
294
295 NetworkPacket packet = std::move(packet_it->packet);
296 packet_it->removed = true;
297
298 // Cleanup of removed packets at the beginning of the deque.
299 while (!packets_in_flight_.empty() &&
300 packets_in_flight_.front().removed) {
301 packets_in_flight_.pop_front();
302 }
303
304 if (delivery_info.receive_time_us != PacketDeliveryInfo::kNotReceived) {
305 int64_t added_delay_us =
306 delivery_info.receive_time_us - packet.send_time();
307 packet.IncrementArrivalTime(added_delay_us);
308 packets_to_deliver.emplace(std::move(packet));
309 // `time_now_us` might be later than when the packet should have
310 // arrived, due to NetworkProcess being called too late. For stats, use
311 // the time it should have been on the link.
312 total_packet_delay_us_ += added_delay_us;
313 ++sent_packets_;
314 } else {
315 ++dropped_packets_;
316 }
317 }
318 }
319
320 MutexLock lock(&config_lock_);
321 while (!packets_to_deliver.empty()) {
322 NetworkPacket packet = std::move(packets_to_deliver.front());
323 packets_to_deliver.pop();
324 DeliverNetworkPacket(&packet);
325 }
326 }
327
DeliverNetworkPacket(NetworkPacket * packet)328 void FakeNetworkPipe::DeliverNetworkPacket(NetworkPacket* packet) {
329 Transport* transport = packet->transport();
330 if (transport) {
331 RTC_DCHECK(!receiver_);
332 if (active_transports_.find(transport) == active_transports_.end()) {
333 // Transport has been destroyed, ignore this packet.
334 return;
335 }
336 if (packet->is_rtcp()) {
337 transport->SendRtcp(packet->data(), packet->data_length());
338 } else {
339 transport->SendRtp(packet->data(), packet->data_length(),
340 packet->packet_options());
341 }
342 } else if (receiver_) {
343 int64_t packet_time_us = packet->packet_time_us().value_or(-1);
344 if (packet_time_us != -1) {
345 int64_t queue_time_us = packet->arrival_time() - packet->send_time();
346 RTC_CHECK(queue_time_us >= 0);
347 packet_time_us += queue_time_us;
348 packet_time_us += (clock_offset_ms_ * 1000);
349 }
350 receiver_->DeliverPacket(packet->media_type(),
351 std::move(*packet->raw_packet()), packet_time_us);
352 }
353 }
354
TimeUntilNextProcess()355 absl::optional<int64_t> FakeNetworkPipe::TimeUntilNextProcess() {
356 MutexLock lock(&process_lock_);
357 absl::optional<int64_t> delivery_us = network_behavior_->NextDeliveryTimeUs();
358 if (delivery_us) {
359 int64_t delay_us = *delivery_us - clock_->TimeInMicroseconds();
360 return std::max<int64_t>((delay_us + 500) / 1000, 0);
361 }
362 return absl::nullopt;
363 }
364
HasReceiver() const365 bool FakeNetworkPipe::HasReceiver() const {
366 MutexLock lock(&config_lock_);
367 return receiver_ != nullptr;
368 }
369
DeliverPacketWithLock(NetworkPacket * packet)370 void FakeNetworkPipe::DeliverPacketWithLock(NetworkPacket* packet) {
371 MutexLock lock(&config_lock_);
372 DeliverNetworkPacket(packet);
373 }
374
ResetStats()375 void FakeNetworkPipe::ResetStats() {
376 MutexLock lock(&process_lock_);
377 dropped_packets_ = 0;
378 sent_packets_ = 0;
379 total_packet_delay_us_ = 0;
380 }
381
GetTimeInMicroseconds() const382 int64_t FakeNetworkPipe::GetTimeInMicroseconds() const {
383 return clock_->TimeInMicroseconds();
384 }
385
386 } // namespace webrtc
387