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