blob: 24340a2f29d17c0d5a2314ef06a040032bc9ca93 [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#ifndef CALL_FAKE_NETWORK_PIPE_H_
12#define CALL_FAKE_NETWORK_PIPE_H_
13
14#include <deque>
15#include <map>
16#include <memory>
17#include <queue>
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020018#include <set>
Erik Språng09708512018-03-14 15:16:50 +010019#include <string>
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020020#include <vector>
Erik Språng09708512018-03-14 15:16:50 +010021
22#include "api/call/transport.h"
Patrik Höglundb6b29e02018-06-21 16:58:01 +020023#include "api/test/simulated_network.h"
Erik Språng09708512018-03-14 15:16:50 +010024#include "call/call.h"
Artem Titov46c4e602018-08-17 14:26:54 +020025#include "call/simulated_packet_receiver.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/constructor_magic.h"
27#include "rtc_base/critical_section.h"
Erik Språng09708512018-03-14 15:16:50 +010028#include "rtc_base/thread_annotations.h"
Erik Språng09708512018-03-14 15:16:50 +010029
30namespace webrtc {
31
32class Clock;
33class PacketReceiver;
34enum class MediaType;
35
36class NetworkPacket {
37 public:
38 NetworkPacket(rtc::CopyOnWriteBuffer packet,
39 int64_t send_time,
40 int64_t arrival_time,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020041 absl::optional<PacketOptions> packet_options,
Erik Språng09708512018-03-14 15:16:50 +010042 bool is_rtcp,
Niels Möller70082872018-08-07 11:03:12 +020043 MediaType media_type,
Erik Språngeea605d2019-08-12 15:56:51 +020044 absl::optional<int64_t> packet_time_us,
45 Transport* transport);
Niels Möller70082872018-08-07 11:03:12 +020046
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +010047 // Disallow copy constructor and copy assignment (no deep copies of |data_|).
Erik Språng09708512018-03-14 15:16:50 +010048 NetworkPacket(const NetworkPacket&) = delete;
Mirko Bonadeied1dcf92018-07-26 09:15:11 +020049 ~NetworkPacket();
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +010050 NetworkPacket& operator=(const NetworkPacket&) = delete;
Erik Språng09708512018-03-14 15:16:50 +010051 // Allow move constructor/assignment, so that we can use in stl containers.
52 NetworkPacket(NetworkPacket&&);
53 NetworkPacket& operator=(NetworkPacket&&);
54
55 const uint8_t* data() const { return packet_.data(); }
56 size_t data_length() const { return packet_.size(); }
57 rtc::CopyOnWriteBuffer* raw_packet() { return &packet_; }
58 int64_t send_time() const { return send_time_; }
59 int64_t arrival_time() const { return arrival_time_; }
60 void IncrementArrivalTime(int64_t extra_delay) {
61 arrival_time_ += extra_delay;
62 }
63 PacketOptions packet_options() const {
64 return packet_options_.value_or(PacketOptions());
65 }
66 bool is_rtcp() const { return is_rtcp_; }
67 MediaType media_type() const { return media_type_; }
Niels Möller70082872018-08-07 11:03:12 +020068 absl::optional<int64_t> packet_time_us() const { return packet_time_us_; }
Erik Språngeea605d2019-08-12 15:56:51 +020069 Transport* transport() const { return transport_; }
Erik Språng09708512018-03-14 15:16:50 +010070
71 private:
72 rtc::CopyOnWriteBuffer packet_;
73 // The time the packet was sent out on the network.
74 int64_t send_time_;
75 // The time the packet should arrive at the receiver.
76 int64_t arrival_time_;
77 // If using a Transport for outgoing degradation, populate with
78 // PacketOptions (transport-wide sequence number) for RTP.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020079 absl::optional<PacketOptions> packet_options_;
Erik Språng09708512018-03-14 15:16:50 +010080 bool is_rtcp_;
81 // If using a PacketReceiver for incoming degradation, populate with
Niels Möller29e13fd2018-12-17 12:35:30 +010082 // appropriate MediaType and packet time. This type/timing will be kept and
83 // forwarded. The packet time might be altered to reflect time spent in fake
Erik Språng09708512018-03-14 15:16:50 +010084 // network pipe.
85 MediaType media_type_;
Niels Möller70082872018-08-07 11:03:12 +020086 absl::optional<int64_t> packet_time_us_;
Erik Språngeea605d2019-08-12 15:56:51 +020087 Transport* transport_;
Erik Språng09708512018-03-14 15:16:50 +010088};
89
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020090// Class faking a network link, internally is uses an implementation of a
91// SimulatedNetworkInterface to simulate network behavior.
Sebastian Jansson836fee12019-02-08 16:08:10 +010092class FakeNetworkPipe : public SimulatedPacketReceiverInterface {
Sebastian Jansson7ee2e252018-05-07 14:49:39 +020093 public:
Artem Titov8ea1e9d2018-10-04 14:46:31 +020094 // Will keep |network_behavior| alive while pipe is alive itself.
Artem Titov8ea1e9d2018-10-04 14:46:31 +020095 FakeNetworkPipe(Clock* clock,
96 std::unique_ptr<NetworkBehaviorInterface> network_behavior);
97 FakeNetworkPipe(Clock* clock,
98 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
99 PacketReceiver* receiver);
100 FakeNetworkPipe(Clock* clock,
101 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
102 PacketReceiver* receiver,
103 uint64_t seed);
Erik Språng09708512018-03-14 15:16:50 +0100104
Artem Titovb0050872018-08-16 17:02:20 +0200105 // Use this constructor if you plan to insert packets using SendRt[c?]p().
Artem Titov8ea1e9d2018-10-04 14:46:31 +0200106 FakeNetworkPipe(Clock* clock,
107 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
108 Transport* transport);
Erik Språng09708512018-03-14 15:16:50 +0100109
Mirko Bonadeied1dcf92018-07-26 09:15:11 +0200110 ~FakeNetworkPipe() override;
Erik Språng09708512018-03-14 15:16:50 +0100111
Artem Titovc02df812018-08-20 10:03:22 +0200112 void SetClockOffset(int64_t offset_ms);
Sebastian Jansson7e85d672018-04-06 09:56:21 +0200113
Sebastian Jansson09408112018-04-24 14:41:22 +0200114 // Must not be called in parallel with DeliverPacket or Process.
Artem Titov46c4e602018-08-17 14:26:54 +0200115 void SetReceiver(PacketReceiver* receiver) override;
Erik Språng09708512018-03-14 15:16:50 +0100116
Erik Språngeea605d2019-08-12 15:56:51 +0200117 // Adds/subtracts references to Transport instances. If a Transport is
118 // destroyed we cannot use to forward a potential delayed packet, these
119 // methods are used to maintain a map of which instances are live.
120 void AddActiveTransport(Transport* transport);
121 void RemoveActiveTransport(Transport* transport);
122
Erik Språng09708512018-03-14 15:16:50 +0100123 // Implements Transport interface. When/if packets are delivered, they will
124 // be passed to the transport instance given in SetReceiverTransport(). These
125 // methods should only be called if a Transport instance was provided in the
126 // constructor.
127 bool SendRtp(const uint8_t* packet,
128 size_t length,
Sebastian Jansson836fee12019-02-08 16:08:10 +0100129 const PacketOptions& options);
130 bool SendRtcp(const uint8_t* packet, size_t length);
Erik Språng09708512018-03-14 15:16:50 +0100131
Erik Språngeea605d2019-08-12 15:56:51 +0200132 // Methods for use with Transport interface. When/if packets are delivered,
133 // they will be passed to the instance specified by the |transport| parameter.
134 // Note that that instance must be in the map of active transports.
135 bool SendRtp(const uint8_t* packet,
136 size_t length,
137 const PacketOptions& options,
138 Transport* transport);
139 bool SendRtcp(const uint8_t* packet, size_t length, Transport* transport);
140
Erik Språng09708512018-03-14 15:16:50 +0100141 // Implements the PacketReceiver interface. When/if packets are delivered,
142 // they will be passed directly to the receiver instance given in
Niels Möller29e13fd2018-12-17 12:35:30 +0100143 // SetReceiver(), without passing through a Demuxer. The receive time
144 // will be increased by the amount of time the packet spent in the
Erik Språng09708512018-03-14 15:16:50 +0100145 // fake network pipe.
Niels Möller70082872018-08-07 11:03:12 +0200146 PacketReceiver::DeliveryStatus DeliverPacket(MediaType media_type,
147 rtc::CopyOnWriteBuffer packet,
148 int64_t packet_time_us) override;
149
150 // TODO(bugs.webrtc.org/9584): Needed to inherit the alternative signature for
151 // this method.
152 using PacketReceiver::DeliverPacket;
Erik Språng09708512018-03-14 15:16:50 +0100153
154 // Processes the network queues and trigger PacketReceiver::IncomingPacket for
155 // packets ready to be delivered.
156 void Process() override;
Sebastian Jansson836fee12019-02-08 16:08:10 +0100157 absl::optional<int64_t> TimeUntilNextProcess() override;
Erik Språng09708512018-03-14 15:16:50 +0100158
159 // Get statistics.
160 float PercentageLoss();
Artem Titov46c4e602018-08-17 14:26:54 +0200161 int AverageDelay() override;
Erik Språng09708512018-03-14 15:16:50 +0100162 size_t DroppedPackets();
163 size_t SentPackets();
Christoffer Rodbro8ef59a42018-03-20 14:34:01 +0100164 void ResetStats();
165
166 protected:
167 void DeliverPacketWithLock(NetworkPacket* packet);
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200168 int64_t GetTimeInMicroseconds() const;
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200169 bool ShouldProcess(int64_t time_now_us) const;
170 void SetTimeToNextProcess(int64_t skip_us);
Erik Språng09708512018-03-14 15:16:50 +0100171
172 private:
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200173 struct StoredPacket {
174 NetworkPacket packet;
175 bool removed = false;
176 explicit StoredPacket(NetworkPacket&& packet);
177 StoredPacket(StoredPacket&&) = default;
178 StoredPacket(const StoredPacket&) = delete;
179 StoredPacket& operator=(const StoredPacket&) = delete;
180 StoredPacket() = delete;
181 };
182
Erik Språngeea605d2019-08-12 15:56:51 +0200183 // Returns true if enqueued, or false if packet was dropped. Use this method
184 // when enqueueing packets that should be received by PacketReceiver instance.
Niels Möller70082872018-08-07 11:03:12 +0200185 bool EnqueuePacket(rtc::CopyOnWriteBuffer packet,
186 absl::optional<PacketOptions> options,
187 bool is_rtcp,
Erik Språngeea605d2019-08-12 15:56:51 +0200188 MediaType media_type,
189 absl::optional<int64_t> packet_time_us);
190
191 // Returns true if enqueued, or false if packet was dropped. Use this method
192 // when enqueueing packets that should be received by Transport instance.
193 bool EnqueuePacket(rtc::CopyOnWriteBuffer packet,
194 absl::optional<PacketOptions> options,
195 bool is_rtcp,
196 Transport* transport);
197
198 bool EnqueuePacket(NetworkPacket&& net_packet)
199 RTC_EXCLUSIVE_LOCKS_REQUIRED(process_lock_);
200
Niels Möller70082872018-08-07 11:03:12 +0200201 void DeliverNetworkPacket(NetworkPacket* packet)
Erik Språng09708512018-03-14 15:16:50 +0100202 RTC_EXCLUSIVE_LOCKS_REQUIRED(config_lock_);
Sebastian Jansson09408112018-04-24 14:41:22 +0200203 bool HasReceiver() const;
Erik Språng09708512018-03-14 15:16:50 +0100204
205 Clock* const clock_;
206 // |config_lock| guards the mostly constant things like the callbacks.
207 rtc::CriticalSection config_lock_;
Artem Titov8ea1e9d2018-10-04 14:46:31 +0200208 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_;
Erik Språng09708512018-03-14 15:16:50 +0100209 PacketReceiver* receiver_ RTC_GUARDED_BY(config_lock_);
Erik Språngeea605d2019-08-12 15:56:51 +0200210 Transport* const global_transport_;
Erik Språng09708512018-03-14 15:16:50 +0100211
212 // |process_lock| guards the data structures involved in delay and loss
213 // processes, such as the packet queues.
214 rtc::CriticalSection process_lock_;
Sebastian Jansson7ee2e252018-05-07 14:49:39 +0200215 // Packets are added at the back of the deque, this makes the deque ordered
216 // by increasing send time. The common case when removing packets from the
217 // deque is removing early packets, which will be close to the front of the
218 // deque. This makes finding the packets in the deque efficient in the common
219 // case.
220 std::deque<StoredPacket> packets_in_flight_ RTC_GUARDED_BY(process_lock_);
Erik Språng09708512018-03-14 15:16:50 +0100221
Sebastian Jansson7e85d672018-04-06 09:56:21 +0200222 int64_t clock_offset_ms_ RTC_GUARDED_BY(config_lock_);
223
Erik Språng09708512018-03-14 15:16:50 +0100224 // Statistics.
225 size_t dropped_packets_ RTC_GUARDED_BY(process_lock_);
226 size_t sent_packets_ RTC_GUARDED_BY(process_lock_);
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200227 int64_t total_packet_delay_us_ RTC_GUARDED_BY(process_lock_);
Sebastian Jansson512bdce2018-04-23 13:15:04 +0200228 int64_t last_log_time_us_;
Erik Språng09708512018-03-14 15:16:50 +0100229
Erik Språngeea605d2019-08-12 15:56:51 +0200230 std::map<Transport*, size_t> active_transports_ RTC_GUARDED_BY(config_lock_);
231
Erik Språng09708512018-03-14 15:16:50 +0100232 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe);
233};
234
235} // namespace webrtc
236
237#endif // CALL_FAKE_NETWORK_PIPE_H_