blob: 24340a2f29d17c0d5a2314ef06a040032bc9ca93 [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
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 12:49:3918#include <set>
Erik Språng09708512018-03-14 14:16:5019#include <string>
Sebastian Jansson7ee2e252018-05-07 12:49:3920#include <vector>
Erik Språng09708512018-03-14 14:16:5021
22#include "api/call/transport.h"
Patrik Höglundb6b29e02018-06-21 14:58:0123#include "api/test/simulated_network.h"
Erik Språng09708512018-03-14 14:16:5024#include "call/call.h"
Artem Titov46c4e602018-08-17 12:26:5425#include "call/simulated_packet_receiver.h"
Steve Anton10542f22019-01-11 17:11:0026#include "rtc_base/constructor_magic.h"
27#include "rtc_base/critical_section.h"
Erik Språng09708512018-03-14 14:16:5028#include "rtc_base/thread_annotations.h"
Erik Språng09708512018-03-14 14:16:5029
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 10:28:0741 absl::optional<PacketOptions> packet_options,
Erik Språng09708512018-03-14 14:16:5042 bool is_rtcp,
Niels Möller70082872018-08-07 09:03:1243 MediaType media_type,
Erik Språngeea605d2019-08-12 13:56:5144 absl::optional<int64_t> packet_time_us,
45 Transport* transport);
Niels Möller70082872018-08-07 09:03:1246
Christoffer Rodbro8ef59a42018-03-20 13:34:0147 // Disallow copy constructor and copy assignment (no deep copies of |data_|).
Erik Språng09708512018-03-14 14:16:5048 NetworkPacket(const NetworkPacket&) = delete;
Mirko Bonadeied1dcf92018-07-26 07:15:1149 ~NetworkPacket();
Christoffer Rodbro8ef59a42018-03-20 13:34:0150 NetworkPacket& operator=(const NetworkPacket&) = delete;
Erik Språng09708512018-03-14 14:16:5051 // 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 09:03:1268 absl::optional<int64_t> packet_time_us() const { return packet_time_us_; }
Erik Språngeea605d2019-08-12 13:56:5169 Transport* transport() const { return transport_; }
Erik Språng09708512018-03-14 14:16:5070
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 10:28:0779 absl::optional<PacketOptions> packet_options_;
Erik Språng09708512018-03-14 14:16:5080 bool is_rtcp_;
81 // If using a PacketReceiver for incoming degradation, populate with
Niels Möller29e13fd2018-12-17 11:35:3082 // 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 14:16:5084 // network pipe.
85 MediaType media_type_;
Niels Möller70082872018-08-07 09:03:1286 absl::optional<int64_t> packet_time_us_;
Erik Språngeea605d2019-08-12 13:56:5187 Transport* transport_;
Erik Språng09708512018-03-14 14:16:5088};
89
Sebastian Jansson7ee2e252018-05-07 12:49:3990// Class faking a network link, internally is uses an implementation of a
91// SimulatedNetworkInterface to simulate network behavior.
Sebastian Jansson836fee12019-02-08 15:08:1092class FakeNetworkPipe : public SimulatedPacketReceiverInterface {
Sebastian Jansson7ee2e252018-05-07 12:49:3993 public:
Artem Titov8ea1e9d2018-10-04 12:46:3194 // Will keep |network_behavior| alive while pipe is alive itself.
Artem Titov8ea1e9d2018-10-04 12:46:3195 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 14:16:50104
Artem Titovb0050872018-08-16 15:02:20105 // Use this constructor if you plan to insert packets using SendRt[c?]p().
Artem Titov8ea1e9d2018-10-04 12:46:31106 FakeNetworkPipe(Clock* clock,
107 std::unique_ptr<NetworkBehaviorInterface> network_behavior,
108 Transport* transport);
Erik Språng09708512018-03-14 14:16:50109
Mirko Bonadeied1dcf92018-07-26 07:15:11110 ~FakeNetworkPipe() override;
Erik Språng09708512018-03-14 14:16:50111
Artem Titovc02df812018-08-20 08:03:22112 void SetClockOffset(int64_t offset_ms);
Sebastian Jansson7e85d672018-04-06 07:56:21113
Sebastian Jansson09408112018-04-24 12:41:22114 // Must not be called in parallel with DeliverPacket or Process.
Artem Titov46c4e602018-08-17 12:26:54115 void SetReceiver(PacketReceiver* receiver) override;
Erik Språng09708512018-03-14 14:16:50116
Erik Språngeea605d2019-08-12 13:56:51117 // 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 14:16:50123 // 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 15:08:10129 const PacketOptions& options);
130 bool SendRtcp(const uint8_t* packet, size_t length);
Erik Språng09708512018-03-14 14:16:50131
Erik Språngeea605d2019-08-12 13:56:51132 // 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 14:16:50141 // 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 11:35:30143 // 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 14:16:50145 // fake network pipe.
Niels Möller70082872018-08-07 09:03:12146 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 14:16:50153
154 // Processes the network queues and trigger PacketReceiver::IncomingPacket for
155 // packets ready to be delivered.
156 void Process() override;
Sebastian Jansson836fee12019-02-08 15:08:10157 absl::optional<int64_t> TimeUntilNextProcess() override;
Erik Språng09708512018-03-14 14:16:50158
159 // Get statistics.
160 float PercentageLoss();
Artem Titov46c4e602018-08-17 12:26:54161 int AverageDelay() override;
Erik Språng09708512018-03-14 14:16:50162 size_t DroppedPackets();
163 size_t SentPackets();
Christoffer Rodbro8ef59a42018-03-20 13:34:01164 void ResetStats();
165
166 protected:
167 void DeliverPacketWithLock(NetworkPacket* packet);
Sebastian Jansson512bdce2018-04-23 11:15:04168 int64_t GetTimeInMicroseconds() const;
Sebastian Jansson512bdce2018-04-23 11:15:04169 bool ShouldProcess(int64_t time_now_us) const;
170 void SetTimeToNextProcess(int64_t skip_us);
Erik Språng09708512018-03-14 14:16:50171
172 private:
Sebastian Jansson7ee2e252018-05-07 12:49:39173 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 13:56:51183 // 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 09:03:12185 bool EnqueuePacket(rtc::CopyOnWriteBuffer packet,
186 absl::optional<PacketOptions> options,
187 bool is_rtcp,
Erik Språngeea605d2019-08-12 13:56:51188 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 09:03:12201 void DeliverNetworkPacket(NetworkPacket* packet)
Erik Språng09708512018-03-14 14:16:50202 RTC_EXCLUSIVE_LOCKS_REQUIRED(config_lock_);
Sebastian Jansson09408112018-04-24 12:41:22203 bool HasReceiver() const;
Erik Språng09708512018-03-14 14:16:50204
205 Clock* const clock_;
206 // |config_lock| guards the mostly constant things like the callbacks.
207 rtc::CriticalSection config_lock_;
Artem Titov8ea1e9d2018-10-04 12:46:31208 const std::unique_ptr<NetworkBehaviorInterface> network_behavior_;
Erik Språng09708512018-03-14 14:16:50209 PacketReceiver* receiver_ RTC_GUARDED_BY(config_lock_);
Erik Språngeea605d2019-08-12 13:56:51210 Transport* const global_transport_;
Erik Språng09708512018-03-14 14:16:50211
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 12:49:39215 // 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 14:16:50221
Sebastian Jansson7e85d672018-04-06 07:56:21222 int64_t clock_offset_ms_ RTC_GUARDED_BY(config_lock_);
223
Erik Språng09708512018-03-14 14:16:50224 // 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 11:15:04227 int64_t total_packet_delay_us_ RTC_GUARDED_BY(process_lock_);
Sebastian Jansson512bdce2018-04-23 11:15:04228 int64_t last_log_time_us_;
Erik Språng09708512018-03-14 14:16:50229
Erik Språngeea605d2019-08-12 13:56:51230 std::map<Transport*, size_t> active_transports_ RTC_GUARDED_BY(config_lock_);
231
Erik Språng09708512018-03-14 14:16:50232 RTC_DISALLOW_COPY_AND_ASSIGN(FakeNetworkPipe);
233};
234
235} // namespace webrtc
236
237#endif // CALL_FAKE_NETWORK_PIPE_H_