pacing_controller.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2019 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 MODULES_PACING_PACING_CONTROLLER_H_
  11. #define MODULES_PACING_PACING_CONTROLLER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <atomic>
  15. #include <memory>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "api/function_view.h"
  19. #include "api/rtc_event_log/rtc_event_log.h"
  20. #include "api/transport/field_trial_based_config.h"
  21. #include "api/transport/network_types.h"
  22. #include "api/transport/webrtc_key_value_config.h"
  23. #include "modules/pacing/bitrate_prober.h"
  24. #include "modules/pacing/interval_budget.h"
  25. #include "modules/pacing/round_robin_packet_queue.h"
  26. #include "modules/pacing/rtp_packet_pacer.h"
  27. #include "modules/rtp_rtcp/include/rtp_packet_sender.h"
  28. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  29. #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
  30. #include "rtc_base/critical_section.h"
  31. #include "rtc_base/experiments/field_trial_parser.h"
  32. #include "rtc_base/thread_annotations.h"
  33. namespace webrtc {
  34. // This class implements a leaky-bucket packet pacing algorithm. It handles the
  35. // logic of determining which packets to send when, but the actual timing of
  36. // the processing is done externally (e.g. PacedSender). Furthermore, the
  37. // forwarding of packets when they are ready to be sent is also handled
  38. // externally, via the PacedSendingController::PacketSender interface.
  39. //
  40. class PacingController {
  41. public:
  42. // Periodic mode uses the IntervalBudget class for tracking bitrate
  43. // budgets, and expected ProcessPackets() to be called a fixed rate,
  44. // e.g. every 5ms as implemented by PacedSender.
  45. // Dynamic mode allows for arbitrary time delta between calls to
  46. // ProcessPackets.
  47. enum class ProcessMode { kPeriodic, kDynamic };
  48. class PacketSender {
  49. public:
  50. virtual ~PacketSender() = default;
  51. virtual void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
  52. const PacedPacketInfo& cluster_info) = 0;
  53. virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
  54. DataSize size) = 0;
  55. };
  56. // Expected max pacer delay. If ExpectedQueueTime() is higher than
  57. // this value, the packet producers should wait (eg drop frames rather than
  58. // encoding them). Bitrate sent may temporarily exceed target set by
  59. // UpdateBitrate() so that this limit will be upheld.
  60. static const TimeDelta kMaxExpectedQueueLength;
  61. // Pacing-rate relative to our target send rate.
  62. // Multiplicative factor that is applied to the target bitrate to calculate
  63. // the number of bytes that can be transmitted per interval.
  64. // Increasing this factor will result in lower delays in cases of bitrate
  65. // overshoots from the encoder.
  66. static const float kDefaultPaceMultiplier;
  67. // If no media or paused, wake up at least every |kPausedProcessIntervalMs| in
  68. // order to send a keep-alive packet so we don't get stuck in a bad state due
  69. // to lack of feedback.
  70. static const TimeDelta kPausedProcessInterval;
  71. static const TimeDelta kMinSleepTime;
  72. PacingController(Clock* clock,
  73. PacketSender* packet_sender,
  74. RtcEventLog* event_log,
  75. const WebRtcKeyValueConfig* field_trials,
  76. ProcessMode mode);
  77. ~PacingController();
  78. // Adds the packet to the queue and calls PacketRouter::SendPacket() when
  79. // it's time to send.
  80. void EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet);
  81. void CreateProbeCluster(DataRate bitrate, int cluster_id);
  82. void Pause(); // Temporarily pause all sending.
  83. void Resume(); // Resume sending packets.
  84. bool IsPaused() const;
  85. void SetCongestionWindow(DataSize congestion_window_size);
  86. void UpdateOutstandingData(DataSize outstanding_data);
  87. // Sets the pacing rates. Must be called once before packets can be sent.
  88. void SetPacingRates(DataRate pacing_rate, DataRate padding_rate);
  89. // Currently audio traffic is not accounted by pacer and passed through.
  90. // With the introduction of audio BWE audio traffic will be accounted for
  91. // the pacer budget calculation. The audio traffic still will be injected
  92. // at high priority.
  93. void SetAccountForAudioPackets(bool account_for_audio);
  94. void SetIncludeOverhead();
  95. void SetTransportOverhead(DataSize overhead_per_packet);
  96. // Returns the time since the oldest queued packet was enqueued.
  97. TimeDelta OldestPacketWaitTime() const;
  98. // Number of packets in the pacer queue.
  99. size_t QueueSizePackets() const;
  100. // Totals size of packets in the pacer queue.
  101. DataSize QueueSizeData() const;
  102. // Current buffer level, i.e. max of media and padding debt.
  103. DataSize CurrentBufferLevel() const;
  104. // Returns the time when the first packet was sent;
  105. absl::optional<Timestamp> FirstSentPacketTime() const;
  106. // Returns the number of milliseconds it will take to send the current
  107. // packets in the queue, given the current size and bitrate, ignoring prio.
  108. TimeDelta ExpectedQueueTime() const;
  109. void SetQueueTimeLimit(TimeDelta limit);
  110. // Enable bitrate probing. Enabled by default, mostly here to simplify
  111. // testing. Must be called before any packets are being sent to have an
  112. // effect.
  113. void SetProbingEnabled(bool enabled);
  114. // Returns the next time we expect ProcessPackets() to be called.
  115. Timestamp NextSendTime() const;
  116. // Check queue of pending packets and send them or padding packets, if budget
  117. // is available.
  118. void ProcessPackets();
  119. bool Congested() const;
  120. bool IsProbing() const;
  121. private:
  122. void EnqueuePacketInternal(std::unique_ptr<RtpPacketToSend> packet,
  123. int priority);
  124. TimeDelta UpdateTimeAndGetElapsed(Timestamp now);
  125. bool ShouldSendKeepalive(Timestamp now) const;
  126. // Updates the number of bytes that can be sent for the next time interval.
  127. void UpdateBudgetWithElapsedTime(TimeDelta delta);
  128. void UpdateBudgetWithSentData(DataSize size);
  129. DataSize PaddingToAdd(absl::optional<DataSize> recommended_probe_size,
  130. DataSize data_sent) const;
  131. std::unique_ptr<RtpPacketToSend> GetPendingPacket(
  132. const PacedPacketInfo& pacing_info,
  133. Timestamp target_send_time,
  134. Timestamp now);
  135. void OnPacketSent(RtpPacketMediaType packet_type,
  136. DataSize packet_size,
  137. Timestamp send_time);
  138. void OnPaddingSent(DataSize padding_sent);
  139. Timestamp CurrentTime() const;
  140. const ProcessMode mode_;
  141. Clock* const clock_;
  142. PacketSender* const packet_sender_;
  143. const std::unique_ptr<FieldTrialBasedConfig> fallback_field_trials_;
  144. const WebRtcKeyValueConfig* field_trials_;
  145. const bool drain_large_queues_;
  146. const bool send_padding_if_silent_;
  147. const bool pace_audio_;
  148. const bool small_first_probe_packet_;
  149. const bool ignore_transport_overhead_;
  150. // In dynamic mode, indicates the target size when requesting padding,
  151. // expressed as a duration in order to adjust for varying padding rate.
  152. const TimeDelta padding_target_duration_;
  153. TimeDelta min_packet_limit_;
  154. DataSize transport_overhead_per_packet_;
  155. // TODO(webrtc:9716): Remove this when we are certain clocks are monotonic.
  156. // The last millisecond timestamp returned by |clock_|.
  157. mutable Timestamp last_timestamp_;
  158. bool paused_;
  159. // If |use_interval_budget_| is true, |media_budget_| and |padding_budget_|
  160. // will be used to track when packets can be sent. Otherwise the media and
  161. // padding debt counters will be used together with the target rates.
  162. // This is the media budget, keeping track of how many bits of media
  163. // we can pace out during the current interval.
  164. IntervalBudget media_budget_;
  165. // This is the padding budget, keeping track of how many bits of padding we're
  166. // allowed to send out during the current interval. This budget will be
  167. // utilized when there's no media to send.
  168. IntervalBudget padding_budget_;
  169. DataSize media_debt_;
  170. DataSize padding_debt_;
  171. DataRate media_rate_;
  172. DataRate padding_rate_;
  173. BitrateProber prober_;
  174. bool probing_send_failure_;
  175. DataRate pacing_bitrate_;
  176. Timestamp last_process_time_;
  177. Timestamp last_send_time_;
  178. absl::optional<Timestamp> first_sent_packet_time_;
  179. RoundRobinPacketQueue packet_queue_;
  180. uint64_t packet_counter_;
  181. DataSize congestion_window_size_;
  182. DataSize outstanding_data_;
  183. TimeDelta queue_time_limit;
  184. bool account_for_audio_;
  185. bool include_overhead_;
  186. };
  187. } // namespace webrtc
  188. #endif // MODULES_PACING_PACING_CONTROLLER_H_