blob: 6f3f9fb487be41fea9e85529fdb28a84bc3689d9 [file] [log] [blame]
Erik Språngd05edec2019-08-14 10:43:47 +02001/*
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
37namespace webrtc {
38
39// This class implements a leaky-buck 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. 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//
45class PacingController {
46 public:
47 class PacketSender {
48 public:
49 virtual ~PacketSender() = default;
50 virtual void SendRtpPacket(std::unique_ptr<RtpPacketToSend> packet,
51 const PacedPacketInfo& cluster_info) = 0;
52 virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
53 DataSize size) = 0;
Erik Språngd05edec2019-08-14 10:43:47 +020054 };
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
72 PacingController(Clock* clock,
73 PacketSender* packet_sender,
74 RtcEventLog* event_log,
75 const WebRtcKeyValueConfig* field_trials);
76
77 ~PacingController();
78
Erik Språngd05edec2019-08-14 10:43:47 +020079 // Adds the packet to the queue and calls PacketRouter::SendPacket() when
80 // it's time to send.
81 void EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet);
82
83 void CreateProbeCluster(DataRate bitrate, int cluster_id);
84
85 void Pause(); // Temporarily pause all sending.
86 void Resume(); // Resume sending packets.
87 bool IsPaused() const;
88
89 void SetCongestionWindow(DataSize congestion_window_size);
90 void UpdateOutstandingData(DataSize outstanding_data);
91
92 // Sets the pacing rates. Must be called once before packets can be sent.
93 void SetPacingRates(DataRate pacing_rate, DataRate padding_rate);
94
95 // Currently audio traffic is not accounted by pacer and passed through.
96 // With the introduction of audio BWE audio traffic will be accounted for
97 // the pacer budget calculation. The audio traffic still will be injected
98 // at high priority.
99 void SetAccountForAudioPackets(bool account_for_audio);
100
101 // Returns the time since the oldest queued packet was enqueued.
102 TimeDelta OldestPacketWaitTime() const;
103
104 size_t QueueSizePackets() const;
105 DataSize QueueSizeData() const;
106
107 // Returns the time when the first packet was sent;
108 absl::optional<Timestamp> FirstSentPacketTime() const;
109
110 // Returns the number of milliseconds it will take to send the current
111 // packets in the queue, given the current size and bitrate, ignoring prio.
112 TimeDelta ExpectedQueueTime() const;
113
114 void SetQueueTimeLimit(TimeDelta limit);
115
116 // Enable bitrate probing. Enabled by default, mostly here to simplify
117 // testing. Must be called before any packets are being sent to have an
118 // effect.
119 void SetProbingEnabled(bool enabled);
120
Erik Språngb210eeb2019-11-05 11:21:48 +0100121 // Time at which next probe should be sent. If this value is set, it should be
Erik Språngd05edec2019-08-14 10:43:47 +0200122 // respected - i.e. don't call ProcessPackets() before this specified time as
123 // that can have unintended side effects.
Erik Språngb210eeb2019-11-05 11:21:48 +0100124 // If no scheduled probe, Timestamp::PlusInifinity() is returned.
125 Timestamp NextProbeTime();
Erik Språngd05edec2019-08-14 10:43:47 +0200126
127 // Time since ProcessPackets() was last executed.
128 TimeDelta TimeElapsedSinceLastProcess() const;
129
130 TimeDelta TimeUntilAvailableBudget() const;
131
132 // Check queue of pending packets and send them or padding packets, if budget
133 // is available.
134 void ProcessPackets();
135
136 bool Congested() const;
137
138 private:
Erik Språng78c82a42019-10-03 18:46:04 +0200139 void EnqueuePacketInternal(std::unique_ptr<RtpPacketToSend> packet,
140 int priority);
Erik Språngd05edec2019-08-14 10:43:47 +0200141 TimeDelta UpdateTimeAndGetElapsed(Timestamp now);
142 bool ShouldSendKeepalive(Timestamp now) const;
143
144 // Updates the number of bytes that can be sent for the next time interval.
145 void UpdateBudgetWithElapsedTime(TimeDelta delta);
146 void UpdateBudgetWithSentData(DataSize size);
147
148 DataSize PaddingToAdd(absl::optional<DataSize> recommended_probe_size,
149 DataSize data_sent);
150
Erik Språngf660e812019-09-01 12:26:44 +0000151 RoundRobinPacketQueue::QueuedPacket* GetPendingPacket(
152 const PacedPacketInfo& pacing_info);
153 void OnPacketSent(RoundRobinPacketQueue::QueuedPacket* packet);
Erik Språngd05edec2019-08-14 10:43:47 +0200154 void OnPaddingSent(DataSize padding_sent);
155
156 Timestamp CurrentTime() const;
157
158 Clock* const clock_;
159 PacketSender* const packet_sender_;
160 const std::unique_ptr<FieldTrialBasedConfig> fallback_field_trials_;
161 const WebRtcKeyValueConfig* field_trials_;
162
163 const bool drain_large_queues_;
164 const bool send_padding_if_silent_;
165 const bool pace_audio_;
Erik Språng78c82a42019-10-03 18:46:04 +0200166 const bool small_first_probe_packet_;
Erik Språngd05edec2019-08-14 10:43:47 +0200167 TimeDelta min_packet_limit_;
168
169 // TODO(webrtc:9716): Remove this when we are certain clocks are monotonic.
170 // The last millisecond timestamp returned by |clock_|.
171 mutable Timestamp last_timestamp_;
172 bool paused_;
173 // This is the media budget, keeping track of how many bits of media
174 // we can pace out during the current interval.
175 IntervalBudget media_budget_;
176 // This is the padding budget, keeping track of how many bits of padding we're
177 // allowed to send out during the current interval. This budget will be
178 // utilized when there's no media to send.
179 IntervalBudget padding_budget_;
180
181 BitrateProber prober_;
182 bool probing_send_failure_;
183 bool padding_failure_state_;
184
185 DataRate pacing_bitrate_;
186
187 Timestamp time_last_process_;
188 Timestamp last_send_time_;
189 absl::optional<Timestamp> first_sent_packet_time_;
190
191 RoundRobinPacketQueue packet_queue_;
192 uint64_t packet_counter_;
193
194 DataSize congestion_window_size_;
195 DataSize outstanding_data_;
196
197 TimeDelta queue_time_limit;
198 bool account_for_audio_;
Erik Språngd05edec2019-08-14 10:43:47 +0200199};
200} // namespace webrtc
201
202#endif // MODULES_PACING_PACING_CONTROLLER_H_