• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 #include "api/call/transport.h"
23 #include "api/test/simulated_network.h"
24 #include "call/call.h"
25 #include "call/simulated_packet_receiver.h"
26 #include "rtc_base/synchronization/mutex.h"
27 #include "rtc_base/thread_annotations.h"
28 
29 namespace webrtc {
30 
31 class Clock;
32 class PacketReceiver;
33 enum class MediaType;
34 
35 class NetworkPacket {
36  public:
37   NetworkPacket(rtc::CopyOnWriteBuffer packet,
38                 int64_t send_time,
39                 int64_t arrival_time,
40                 absl::optional<PacketOptions> packet_options,
41                 bool is_rtcp,
42                 MediaType media_type,
43                 absl::optional<int64_t> packet_time_us,
44                 Transport* transport);
45 
46   // Disallow copy constructor and copy assignment (no deep copies of `data_`).
47   NetworkPacket(const NetworkPacket&) = delete;
48   ~NetworkPacket();
49   NetworkPacket& operator=(const NetworkPacket&) = delete;
50   // Allow move constructor/assignment, so that we can use in stl containers.
51   NetworkPacket(NetworkPacket&&);
52   NetworkPacket& operator=(NetworkPacket&&);
53 
data()54   const uint8_t* data() const { return packet_.data(); }
data_length()55   size_t data_length() const { return packet_.size(); }
raw_packet()56   rtc::CopyOnWriteBuffer* raw_packet() { return &packet_; }
send_time()57   int64_t send_time() const { return send_time_; }
arrival_time()58   int64_t arrival_time() const { return arrival_time_; }
IncrementArrivalTime(int64_t extra_delay)59   void IncrementArrivalTime(int64_t extra_delay) {
60     arrival_time_ += extra_delay;
61   }
packet_options()62   PacketOptions packet_options() const {
63     return packet_options_.value_or(PacketOptions());
64   }
is_rtcp()65   bool is_rtcp() const { return is_rtcp_; }
media_type()66   MediaType media_type() const { return media_type_; }
packet_time_us()67   absl::optional<int64_t> packet_time_us() const { return packet_time_us_; }
transport()68   Transport* transport() const { return transport_; }
69 
70  private:
71   rtc::CopyOnWriteBuffer packet_;
72   // The time the packet was sent out on the network.
73   int64_t send_time_;
74   // The time the packet should arrive at the receiver.
75   int64_t arrival_time_;
76   // If using a Transport for outgoing degradation, populate with
77   // PacketOptions (transport-wide sequence number) for RTP.
78   absl::optional<PacketOptions> packet_options_;
79   bool is_rtcp_;
80   // If using a PacketReceiver for incoming degradation, populate with
81   // appropriate MediaType and packet time. This type/timing will be kept and
82   // forwarded. The packet time might be altered to reflect time spent in fake
83   // network pipe.
84   MediaType media_type_;
85   absl::optional<int64_t> packet_time_us_;
86   Transport* transport_;
87 };
88 
89 // Class faking a network link, internally is uses an implementation of a
90 // SimulatedNetworkInterface to simulate network behavior.
91 class FakeNetworkPipe : public SimulatedPacketReceiverInterface {
92  public:
93   // Will keep `network_behavior` alive while pipe is alive itself.
94   FakeNetworkPipe(Clock* clock,
95                   std::unique_ptr<NetworkBehaviorInterface> network_behavior);
96   FakeNetworkPipe(Clock* clock,
97                   std::unique_ptr<NetworkBehaviorInterface> network_behavior,
98                   PacketReceiver* receiver);
99   FakeNetworkPipe(Clock* clock,
100                   std::unique_ptr<NetworkBehaviorInterface> network_behavior,
101                   PacketReceiver* receiver,
102                   uint64_t seed);
103 
104   // Use this constructor if you plan to insert packets using SendRt[c?]p().
105   FakeNetworkPipe(Clock* clock,
106                   std::unique_ptr<NetworkBehaviorInterface> network_behavior,
107                   Transport* transport);
108 
109   ~FakeNetworkPipe() override;
110 
111   FakeNetworkPipe(const FakeNetworkPipe&) = delete;
112   FakeNetworkPipe& operator=(const FakeNetworkPipe&) = delete;
113 
114   void SetClockOffset(int64_t offset_ms);
115 
116   // Must not be called in parallel with DeliverPacket or Process.
117   void SetReceiver(PacketReceiver* receiver) override;
118 
119   // Adds/subtracts references to Transport instances. If a Transport is
120   // destroyed we cannot use to forward a potential delayed packet, these
121   // methods are used to maintain a map of which instances are live.
122   void AddActiveTransport(Transport* transport);
123   void RemoveActiveTransport(Transport* transport);
124 
125   // Implements Transport interface. When/if packets are delivered, they will
126   // be passed to the transport instance given in SetReceiverTransport(). These
127   // methods should only be called if a Transport instance was provided in the
128   // constructor.
129   bool SendRtp(const uint8_t* packet,
130                size_t length,
131                const PacketOptions& options);
132   bool SendRtcp(const uint8_t* packet, size_t length);
133 
134   // Methods for use with Transport interface. When/if packets are delivered,
135   // they will be passed to the instance specified by the `transport` parameter.
136   // Note that that instance must be in the map of active transports.
137   bool SendRtp(const uint8_t* packet,
138                size_t length,
139                const PacketOptions& options,
140                Transport* transport);
141   bool SendRtcp(const uint8_t* packet, size_t length, Transport* transport);
142 
143   // Implements the PacketReceiver interface. When/if packets are delivered,
144   // they will be passed directly to the receiver instance given in
145   // SetReceiver(), without passing through a Demuxer. The receive time
146   // will be increased by the amount of time the packet spent in the
147   // fake network pipe.
148   PacketReceiver::DeliveryStatus DeliverPacket(MediaType media_type,
149                                                rtc::CopyOnWriteBuffer packet,
150                                                int64_t packet_time_us) override;
151 
152   // TODO(bugs.webrtc.org/9584): Needed to inherit the alternative signature for
153   // this method.
154   using PacketReceiver::DeliverPacket;
155 
156   // Processes the network queues and trigger PacketReceiver::IncomingPacket for
157   // packets ready to be delivered.
158   void Process() override;
159   absl::optional<int64_t> TimeUntilNextProcess() override;
160 
161   // Get statistics.
162   float PercentageLoss();
163   int AverageDelay() override;
164   size_t DroppedPackets();
165   size_t SentPackets();
166   void ResetStats();
167 
168  protected:
169   void DeliverPacketWithLock(NetworkPacket* packet);
170   int64_t GetTimeInMicroseconds() const;
171   bool ShouldProcess(int64_t time_now_us) const;
172   void SetTimeToNextProcess(int64_t skip_us);
173 
174  private:
175   struct StoredPacket {
176     NetworkPacket packet;
177     bool removed = false;
178     explicit StoredPacket(NetworkPacket&& packet);
179     StoredPacket(StoredPacket&&) = default;
180     StoredPacket(const StoredPacket&) = delete;
181     StoredPacket& operator=(const StoredPacket&) = delete;
182     StoredPacket() = delete;
183   };
184 
185   // Returns true if enqueued, or false if packet was dropped. Use this method
186   // when enqueueing packets that should be received by PacketReceiver instance.
187   bool EnqueuePacket(rtc::CopyOnWriteBuffer packet,
188                      absl::optional<PacketOptions> options,
189                      bool is_rtcp,
190                      MediaType media_type,
191                      absl::optional<int64_t> packet_time_us);
192 
193   // Returns true if enqueued, or false if packet was dropped. Use this method
194   // when enqueueing packets that should be received by Transport instance.
195   bool EnqueuePacket(rtc::CopyOnWriteBuffer packet,
196                      absl::optional<PacketOptions> options,
197                      bool is_rtcp,
198                      Transport* transport);
199 
200   bool EnqueuePacket(NetworkPacket&& net_packet)
201       RTC_EXCLUSIVE_LOCKS_REQUIRED(process_lock_);
202 
203   void DeliverNetworkPacket(NetworkPacket* packet)
204       RTC_EXCLUSIVE_LOCKS_REQUIRED(config_lock_);
205   bool HasReceiver() const;
206 
207   Clock* const clock_;
208   // `config_lock` guards the mostly constant things like the callbacks.
209   mutable Mutex config_lock_;
210   const std::unique_ptr<NetworkBehaviorInterface> network_behavior_;
211   PacketReceiver* receiver_ RTC_GUARDED_BY(config_lock_);
212   Transport* const global_transport_;
213 
214   // `process_lock` guards the data structures involved in delay and loss
215   // processes, such as the packet queues.
216   Mutex process_lock_;
217   // Packets  are added at the back of the deque, this makes the deque ordered
218   // by increasing send time. The common case when removing packets from the
219   // deque is removing early packets, which will be close to the front of the
220   // deque. This makes finding the packets in the deque efficient in the common
221   // case.
222   std::deque<StoredPacket> packets_in_flight_ RTC_GUARDED_BY(process_lock_);
223 
224   int64_t clock_offset_ms_ RTC_GUARDED_BY(config_lock_);
225 
226   // Statistics.
227   size_t dropped_packets_ RTC_GUARDED_BY(process_lock_);
228   size_t sent_packets_ RTC_GUARDED_BY(process_lock_);
229   int64_t total_packet_delay_us_ RTC_GUARDED_BY(process_lock_);
230   int64_t last_log_time_us_;
231 
232   std::map<Transport*, size_t> active_transports_ RTC_GUARDED_BY(config_lock_);
233 };
234 
235 }  // namespace webrtc
236 
237 #endif  // CALL_FAKE_NETWORK_PIPE_H_
238