| 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 MODULES_PACING_PACING_CONTROLLER_H_ |
| 12 | #define MODULES_PACING_PACING_CONTROLLER_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include <array> |
| 18 | #include <atomic> |
| 19 | #include <memory> |
| 20 | #include <vector> |
| 21 |
| 22 | #include "absl/types/optional.h" |
| 23 | #include "api/field_trials_view.h" |
| 24 | #include "api/function_view.h" |
| 25 | #include "api/transport/field_trial_based_config.h" |
| 26 | #include "api/transport/network_types.h" |
| 27 | #include "modules/pacing/bitrate_prober.h" |
| 28 | #include "modules/pacing/interval_budget.h" |
| 29 | #include "modules/pacing/prioritized_packet_queue.h" |
| 30 | #include "modules/pacing/rtp_packet_pacer.h" |
| 31 | #include "modules/rtp_rtcp/include/rtp_packet_sender.h" |
| 32 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 33 | #include "modules/rtp_rtcp/source/rtp_packet_to_send.h" |
| 34 | #include "rtc_base/experiments/field_trial_parser.h" |
| 35 | #include "rtc_base/thread_annotations.h" |
| 36 |
| 37 | namespace webrtc { |
| 38 |
| 39 | // This class implements a leaky-bucket packet pacing algorithm. It handles the |
| 40 | // logic of determining which packets to send when, but the actual timing of |
| 41 | // the processing is done externally (e.g. RtpPacketPacer). Furthermore, the |
| 42 | // forwarding of packets when they are ready to be sent is also handled |
| 43 | // externally, via the PacingController::PacketSender interface. |
| 44 | class PacingController { |
| 45 | public: |
| 46 | class PacketSender { |
| 47 | public: |
| 48 | virtual ~PacketSender() = default; |
| 49 | virtual void SendPacket(std::unique_ptr<RtpPacketToSend> packet, |
| 50 | const PacedPacketInfo& cluster_info) = 0; |
| 51 | // Should be called after each call to SendPacket(). |
| 52 | virtual std::vector<std::unique_ptr<RtpPacketToSend>> FetchFec() = 0; |
| 53 | virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding( |
| 54 | DataSize size) = 0; |
| 55 |
| 56 | // TODO(bugs.webrtc.org/11340): Make pure virtual once downstream projects |
| 57 | // have been updated. |
| 58 | virtual void OnAbortedRetransmissions( |
| 59 | uint32_t ssrc, |
| 60 | rtc::ArrayView<const uint16_t> sequence_numbers) {} |
| 61 | virtual absl::optional<uint32_t> GetRtxSsrcForMedia(uint32_t ssrc) const { |
| 62 | return absl::nullopt; |
| 63 | } |
| 64 | }; |
| 65 |
| 66 | // Expected max pacer delay. If ExpectedQueueTime() is higher than |
| 67 | // this value, the packet producers should wait (eg drop frames rather than |
| 68 | // encoding them). Bitrate sent may temporarily exceed target set by |
| 69 | // UpdateBitrate() so that this limit will be upheld. |
| 70 | static const TimeDelta kMaxExpectedQueueLength; |
| 71 | // If no media or paused, wake up at least every `kPausedProcessIntervalMs` in |
| 72 | // order to send a keep-alive packet so we don't get stuck in a bad state due |
| 73 | // to lack of feedback. |
| 74 | static const TimeDelta kPausedProcessInterval; |
| 75 |
| 76 | static const TimeDelta kMinSleepTime; |
| 77 |
| 78 | // Allow probes to be processed slightly ahead of inteded send time. Currently |
| 79 | // set to 1ms as this is intended to allow times be rounded down to the |
| 80 | // nearest millisecond. |
| 81 | static const TimeDelta kMaxEarlyProbeProcessing; |
| 82 |
| 83 | PacingController(Clock* clock, |
| 84 | PacketSender* packet_sender, |
| 85 | const FieldTrialsView& field_trials); |
| 86 |
| 87 | ~PacingController(); |
| 88 |
| 89 | // Adds the packet to the queue and calls PacketRouter::SendPacket() when |
| 90 | // it's time to send. |
| 91 | void EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet); |
| 92 |
| 93 | // ABSL_DEPRECATED("Use CreateProbeClusters instead") |
| 94 | void CreateProbeCluster(DataRate bitrate, int cluster_id); |
| 95 | void CreateProbeClusters( |
| 96 | rtc::ArrayView<const ProbeClusterConfig> probe_cluster_configs); |
| 97 |
| 98 | void Pause(); // Temporarily pause all sending. |
| 99 | void Resume(); // Resume sending packets. |
| 100 | bool IsPaused() const; |
| 101 |
| 102 | void SetCongested(bool congested); |
| 103 |
| 104 | // Sets the pacing rates. Must be called once before packets can be sent. |
| 105 | void SetPacingRates(DataRate pacing_rate, DataRate padding_rate); |
| 106 | DataRate pacing_rate() const { return adjusted_media_rate_; } |
| 107 |
| 108 | // Currently audio traffic is not accounted by pacer and passed through. |
| 109 | // With the introduction of audio BWE audio traffic will be accounted for |
| 110 | // the pacer budget calculation. The audio traffic still will be injected |
| 111 | // at high priority. |
| 112 | void SetAccountForAudioPackets(bool account_for_audio); |
| 113 | void SetIncludeOverhead(); |
| 114 |
| 115 | void SetTransportOverhead(DataSize overhead_per_packet); |
| 116 | // The pacer is allowed to send enqued packets in bursts and can build up a |
| 117 | // packet "debt" that correspond to approximately the send rate during |
| 118 | // 'burst_interval'. |
| 119 | void SetSendBurstInterval(TimeDelta burst_interval); |
| 120 |
| 121 | // Returns the time when the oldest packet was queued. |
| 122 | Timestamp OldestPacketEnqueueTime() const; |
| 123 |
| 124 | // Number of packets in the pacer queue. |
| 125 | size_t QueueSizePackets() const; |
| 126 | // Number of packets in the pacer queue per media type (RtpPacketMediaType |
| 127 | // values are used as lookup index). |
| 128 | const std::array<int, kNumMediaTypes>& SizeInPacketsPerRtpPacketMediaType() |
| 129 | const; |
| 130 | // Totals size of packets in the pacer queue. |
| 131 | DataSize QueueSizeData() const; |
| 132 |
| 133 | // Current buffer level, i.e. max of media and padding debt. |
| 134 | DataSize CurrentBufferLevel() const; |
| 135 |
| 136 | // Returns the time when the first packet was sent. |
| 137 | absl::optional<Timestamp> FirstSentPacketTime() const; |
| 138 |
| 139 | // Returns the number of milliseconds it will take to send the current |
| 140 | // packets in the queue, given the current size and bitrate, ignoring prio. |
| 141 | TimeDelta ExpectedQueueTime() const; |
| 142 |
| 143 | void SetQueueTimeLimit(TimeDelta limit); |
| 144 |
| 145 | // Enable bitrate probing. Enabled by default, mostly here to simplify |
| 146 | // testing. Must be called before any packets are being sent to have an |
| 147 | // effect. |
| 148 | void SetProbingEnabled(bool enabled); |
| 149 |
| 150 | // Returns the next time we expect ProcessPackets() to be called. |
| 151 | Timestamp NextSendTime() const; |
| 152 |
| 153 | // Check queue of pending packets and send them or padding packets, if budget |
| 154 | // is available. |
| 155 | void ProcessPackets(); |
| 156 |
| 157 | bool IsProbing() const; |
| 158 |
| 159 | private: |
| 160 | TimeDelta UpdateTimeAndGetElapsed(Timestamp now); |
| 161 | bool ShouldSendKeepalive(Timestamp now) const; |
| 162 |
| 163 | // Updates the number of bytes that can be sent for the next time interval. |
| 164 | void UpdateBudgetWithElapsedTime(TimeDelta delta); |
| 165 | void UpdateBudgetWithSentData(DataSize size); |
| 166 | void UpdatePaddingBudgetWithSentData(DataSize size); |
| 167 |
| 168 | DataSize PaddingToAdd(DataSize recommended_probe_size, |
| 169 | DataSize data_sent) const; |
| 170 |
| 171 | std::unique_ptr<RtpPacketToSend> GetPendingPacket( |
| 172 | const PacedPacketInfo& pacing_info, |
| 173 | Timestamp target_send_time, |
| 174 | Timestamp now); |
| 175 | void OnPacketSent(RtpPacketMediaType packet_type, |
| 176 | DataSize packet_size, |
| 177 | Timestamp send_time); |
| 178 | void MaybeUpdateMediaRateDueToLongQueue(Timestamp now); |
| 179 |
| 180 | Timestamp CurrentTime() const; |
| 181 |
| 182 | // Helper methods for packet that may not be paced. Returns a finite Timestamp |
| 183 | // if a packet type is configured to not be paced and the packet queue has at |
| 184 | // least one packet of that type. Otherwise returns |
| 185 | // Timestamp::MinusInfinity(). |
| 186 | Timestamp NextUnpacedSendTime() const; |
| 187 |
| 188 | Clock* const clock_; |
| 189 | PacketSender* const packet_sender_; |
| 190 | const FieldTrialsView& field_trials_; |
| 191 |
| 192 | const bool drain_large_queues_; |
| 193 | const bool send_padding_if_silent_; |
| 194 | const bool pace_audio_; |
| 195 | const bool ignore_transport_overhead_; |
| 196 | const bool fast_retransmissions_; |
| 197 |
| 198 | TimeDelta min_packet_limit_; |
| 199 | DataSize transport_overhead_per_packet_; |
| 200 | TimeDelta send_burst_interval_; |
| 201 |
| 202 | // TODO(webrtc:9716): Remove this when we are certain clocks are monotonic. |
| 203 | // The last millisecond timestamp returned by `clock_`. |
| 204 | mutable Timestamp last_timestamp_; |
| 205 | bool paused_; |
| 206 |
| 207 | // Amount of outstanding data for media and padding. |
| 208 | DataSize media_debt_; |
| 209 | DataSize padding_debt_; |
| 210 |
| 211 | // The target pacing rate, signaled via SetPacingRates(). |
| 212 | DataRate pacing_rate_; |
| 213 | // The media send rate, which might adjusted from pacing_rate_, e.g. if the |
| 214 | // pacing queue is growing too long. |
| 215 | DataRate adjusted_media_rate_; |
| 216 | // The padding target rate. We aim to fill up to this rate with padding what |
| 217 | // is not already used by media. |
| 218 | DataRate padding_rate_; |
| 219 |
| 220 | BitrateProber prober_; |
| 221 | bool probing_send_failure_; |
| 222 |
| 223 | Timestamp last_process_time_; |
| 224 | Timestamp last_send_time_; |
| 225 | absl::optional<Timestamp> first_sent_packet_time_; |
| 226 | bool seen_first_packet_; |
| 227 |
| 228 | PrioritizedPacketQueue packet_queue_; |
| 229 |
| 230 | bool congested_; |
| 231 |
| 232 | TimeDelta queue_time_limit_; |
| 233 | bool account_for_audio_; |
| 234 | bool include_overhead_; |
| 235 | }; |
| 236 | } // namespace webrtc |
| 237 |
| 238 | #endif // MODULES_PACING_PACING_CONTROLLER_H_ |
| 239 |