You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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/source/rtp_packet_to_send.h"
  29. #include "rtc_base/critical_section.h"
  30. #include "rtc_base/experiments/field_trial_parser.h"
  31. #include "rtc_base/thread_annotations.h"
  32. namespace webrtc {
  33. // This class implements a leaky-buck packet pacing algorithm. It handles the
  34. // logic of determining which packets to send when, but the actual timing of
  35. // the processing is done externally (e.g. PacedSender). Furthermore, the
  36. // forwarding of packets when they are ready to be sent is also handled
  37. // externally, via the PacedSendingController::PacketSender interface.
  38. //
  39. class PacingController {
  40. public:
  41. class PacketSender {
  42. public:
  43. virtual ~PacketSender() = default;
  44. virtual void SendRtpPacket(std::unique_ptr<RtpPacketToSend> packet,
  45. const PacedPacketInfo& cluster_info) = 0;
  46. virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
  47. DataSize size) = 0;
  48. };
  49. // Expected max pacer delay. If ExpectedQueueTime() is higher than
  50. // this value, the packet producers should wait (eg drop frames rather than
  51. // encoding them). Bitrate sent may temporarily exceed target set by
  52. // UpdateBitrate() so that this limit will be upheld.
  53. static const TimeDelta kMaxExpectedQueueLength;
  54. // Pacing-rate relative to our target send rate.
  55. // Multiplicative factor that is applied to the target bitrate to calculate
  56. // the number of bytes that can be transmitted per interval.
  57. // Increasing this factor will result in lower delays in cases of bitrate
  58. // overshoots from the encoder.
  59. static const float kDefaultPaceMultiplier;
  60. // If no media or paused, wake up at least every |kPausedProcessIntervalMs| in
  61. // order to send a keep-alive packet so we don't get stuck in a bad state due
  62. // to lack of feedback.
  63. static const TimeDelta kPausedProcessInterval;
  64. PacingController(Clock* clock,
  65. PacketSender* packet_sender,
  66. RtcEventLog* event_log,
  67. const WebRtcKeyValueConfig* field_trials);
  68. ~PacingController();
  69. // Adds the packet to the queue and calls PacketRouter::SendPacket() when
  70. // it's time to send.
  71. void EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet);
  72. void CreateProbeCluster(DataRate bitrate, int cluster_id);
  73. void Pause(); // Temporarily pause all sending.
  74. void Resume(); // Resume sending packets.
  75. bool IsPaused() const;
  76. void SetCongestionWindow(DataSize congestion_window_size);
  77. void UpdateOutstandingData(DataSize outstanding_data);
  78. // Sets the pacing rates. Must be called once before packets can be sent.
  79. void SetPacingRates(DataRate pacing_rate, DataRate padding_rate);
  80. // Currently audio traffic is not accounted by pacer and passed through.
  81. // With the introduction of audio BWE audio traffic will be accounted for
  82. // the pacer budget calculation. The audio traffic still will be injected
  83. // at high priority.
  84. void SetAccountForAudioPackets(bool account_for_audio);
  85. // Returns the time since the oldest queued packet was enqueued.
  86. TimeDelta OldestPacketWaitTime() const;
  87. size_t QueueSizePackets() const;
  88. DataSize QueueSizeData() const;
  89. // Returns the time when the first packet was sent;
  90. absl::optional<Timestamp> FirstSentPacketTime() const;
  91. // Returns the number of milliseconds it will take to send the current
  92. // packets in the queue, given the current size and bitrate, ignoring prio.
  93. TimeDelta ExpectedQueueTime() const;
  94. void SetQueueTimeLimit(TimeDelta limit);
  95. // Enable bitrate probing. Enabled by default, mostly here to simplify
  96. // testing. Must be called before any packets are being sent to have an
  97. // effect.
  98. void SetProbingEnabled(bool enabled);
  99. // Time until next probe should be sent. If this value is set, it should be
  100. // respected - i.e. don't call ProcessPackets() before this specified time as
  101. // that can have unintended side effects.
  102. absl::optional<TimeDelta> TimeUntilNextProbe();
  103. // Time since ProcessPackets() was last executed.
  104. TimeDelta TimeElapsedSinceLastProcess() const;
  105. TimeDelta TimeUntilAvailableBudget() const;
  106. // Check queue of pending packets and send them or padding packets, if budget
  107. // is available.
  108. void ProcessPackets();
  109. bool Congested() const;
  110. private:
  111. void EnqueuePacketInternal(std::unique_ptr<RtpPacketToSend> packet,
  112. int priority);
  113. TimeDelta UpdateTimeAndGetElapsed(Timestamp now);
  114. bool ShouldSendKeepalive(Timestamp now) const;
  115. // Updates the number of bytes that can be sent for the next time interval.
  116. void UpdateBudgetWithElapsedTime(TimeDelta delta);
  117. void UpdateBudgetWithSentData(DataSize size);
  118. DataSize PaddingToAdd(absl::optional<DataSize> recommended_probe_size,
  119. DataSize data_sent);
  120. RoundRobinPacketQueue::QueuedPacket* GetPendingPacket(
  121. const PacedPacketInfo& pacing_info);
  122. void OnPacketSent(RoundRobinPacketQueue::QueuedPacket* packet);
  123. void OnPaddingSent(DataSize padding_sent);
  124. Timestamp CurrentTime() const;
  125. Clock* const clock_;
  126. PacketSender* const packet_sender_;
  127. const std::unique_ptr<FieldTrialBasedConfig> fallback_field_trials_;
  128. const WebRtcKeyValueConfig* field_trials_;
  129. const bool drain_large_queues_;
  130. const bool send_padding_if_silent_;
  131. const bool pace_audio_;
  132. const bool small_first_probe_packet_;
  133. TimeDelta min_packet_limit_;
  134. // TODO(webrtc:9716): Remove this when we are certain clocks are monotonic.
  135. // The last millisecond timestamp returned by |clock_|.
  136. mutable Timestamp last_timestamp_;
  137. bool paused_;
  138. // This is the media budget, keeping track of how many bits of media
  139. // we can pace out during the current interval.
  140. IntervalBudget media_budget_;
  141. // This is the padding budget, keeping track of how many bits of padding we're
  142. // allowed to send out during the current interval. This budget will be
  143. // utilized when there's no media to send.
  144. IntervalBudget padding_budget_;
  145. BitrateProber prober_;
  146. bool probing_send_failure_;
  147. bool padding_failure_state_;
  148. DataRate pacing_bitrate_;
  149. Timestamp time_last_process_;
  150. Timestamp last_send_time_;
  151. absl::optional<Timestamp> first_sent_packet_time_;
  152. RoundRobinPacketQueue packet_queue_;
  153. uint64_t packet_counter_;
  154. DataSize congestion_window_size_;
  155. DataSize outstanding_data_;
  156. TimeDelta queue_time_limit;
  157. bool account_for_audio_;
  158. };
  159. } // namespace webrtc
  160. #endif // MODULES_PACING_PACING_CONTROLLER_H_