fake_network_pipe.h 9.1 KB

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