blob: a1d2474ca1866b8c507ed0b6828f0fd03b8c18b2 [file] [log] [blame]
Erik Språng4314a492019-11-26 16:48:491/*
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_TASK_QUEUE_PACED_SENDER_H_
12#define MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
13
14#include <stddef.h>
15#include <stdint.h>
16
Erik Språng4314a492019-11-26 16:48:4917#include <memory>
Erik Språng4314a492019-11-26 16:48:4918#include <vector>
19
20#include "absl/types/optional.h"
Jonas Orelande62c2f22022-03-29 09:04:4821#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 13:31:2422#include "api/sequence_checker.h"
Per K8b5bf6d2023-04-13 08:08:4523#include "api/task_queue/pending_task_safety_flag.h"
Erik Språng4314a492019-11-26 16:48:4924#include "api/units/data_size.h"
25#include "api/units/time_delta.h"
26#include "api/units/timestamp.h"
Erik Språng4314a492019-11-26 16:48:4927#include "modules/pacing/pacing_controller.h"
Erik Språng4314a492019-11-26 16:48:4928#include "modules/pacing/rtp_packet_pacer.h"
29#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
Henrik Boströma768f522022-04-13 10:37:5930#include "rtc_base/experiments/field_trial_parser.h"
Erik Språng0f86c1f2021-10-26 14:19:0331#include "rtc_base/numerics/exp_filter.h"
Erik Språng4314a492019-11-26 16:48:4932#include "rtc_base/thread_annotations.h"
33
34namespace webrtc {
35class Clock;
Erik Språng4314a492019-11-26 16:48:4936
Erik Språnged1fb192020-06-30 11:53:3737class TaskQueuePacedSender : public RtpPacketPacer, public RtpPacketSender {
Erik Språng4314a492019-11-26 16:48:4938 public:
Jianhui Daidf59e532022-03-19 07:38:5139 static const int kNoPacketHoldback;
40
Henrik Boströmcf2856b2022-11-15 08:23:1941 // The pacer can be configured using `field_trials` or specified parameters.
42 //
Artem Titovee3e3fd2021-07-28 18:28:2843 // The `hold_back_window` parameter sets a lower bound on time to sleep if
Erik Språng4ab61cb2020-05-19 15:40:5844 // there is currently a pacer queue and packets can't immediately be
45 // processed. Increasing this reduces thread wakeups at the expense of higher
46 // latency.
Henrik Boströmcf2856b2022-11-15 08:23:1947 //
Per K401c14a2023-04-11 07:53:4848 // The taskqueue used when constructing a TaskQueuePacedSender will also be
49 // used for pacing.
Per Kb202bc12023-11-27 15:06:4450 TaskQueuePacedSender(Clock* clock,
51 PacingController::PacketSender* packet_sender,
52 const FieldTrialsView& field_trials,
53 TimeDelta max_hold_back_window,
54 int max_hold_back_window_in_packets);
Erik Språng4314a492019-11-26 16:48:4955
56 ~TaskQueuePacedSender() override;
57
Per Kb202bc12023-11-27 15:06:4458 // The pacer is allowed to send enqued packets in bursts and can build up a
59 // packet "debt" that correspond to approximately the send rate during
60 // 'burst_interval'.
61 void SetSendBurstInterval(TimeDelta burst_interval);
62
Per K98db63c2024-01-26 11:57:0163 // A probe may be sent without first waing for a media packet.
64 void SetAllowProbeWithoutMediaPacket(bool allow);
65
Etienne Pierre-doray03bce3f2021-03-29 17:36:1566 // Ensure that necessary delayed tasks are scheduled.
67 void EnsureStarted();
68
Erik Språng4314a492019-11-26 16:48:4969 // Methods implementing RtpPacketSender.
70
Per Kjellanderf0759172021-04-09 11:41:5371 // Adds the packet to the queue and calls
72 // PacingController::PacketSender::SendPacket() when it's time to send.
Erik Språng4314a492019-11-26 16:48:4973 void EnqueuePackets(
74 std::vector<std::unique_ptr<RtpPacketToSend>> packets) override;
Erik Språng1b11b582022-12-09 20:38:4475 // Remove any pending packets matching this SSRC from the packet queue.
76 void RemovePacketsForSsrc(uint32_t ssrc) override;
Erik Språng4314a492019-11-26 16:48:4977
Jianhui Dai94457792021-12-07 11:34:3678 // Methods implementing RtpPacketPacer.
Erik Språng4314a492019-11-26 16:48:4979
Per Kjellander88af2032022-05-16 17:58:4080 void CreateProbeClusters(
81 std::vector<ProbeClusterConfig> probe_cluster_configs) override;
Erik Språng4314a492019-11-26 16:48:4982
83 // Temporarily pause all sending.
84 void Pause() override;
85
86 // Resume sending packets.
87 void Resume() override;
88
Erik Språng66734372022-03-16 13:20:4989 void SetCongested(bool congested) override;
Erik Språng4314a492019-11-26 16:48:4990
91 // Sets the pacing rates. Must be called once before packets can be sent.
92 void SetPacingRates(DataRate pacing_rate, DataRate padding_rate) override;
93
94 // Currently audio traffic is not accounted for by pacer and passed through.
95 // With the introduction of audio BWE, audio traffic will be accounted for
96 // in the pacer budget calculation. The audio traffic will still be injected
97 // at high priority.
98 void SetAccountForAudioPackets(bool account_for_audio) override;
99
Sebastian Janssonc3eb9fd2020-01-29 16:42:52100 void SetIncludeOverhead() override;
Mirko Bonadeie7bc3a32020-01-29 18:45:00101 void SetTransportOverhead(DataSize overhead_per_packet) override;
102
Erik Språng4314a492019-11-26 16:48:49103 // Returns the time since the oldest queued packet was enqueued.
104 TimeDelta OldestPacketWaitTime() const override;
105
106 // Returns total size of all packets in the pacer queue.
107 DataSize QueueSizeData() const override;
108
109 // Returns the time when the first packet was sent;
110 absl::optional<Timestamp> FirstSentPacketTime() const override;
111
112 // Returns the number of milliseconds it will take to send the current
113 // packets in the queue, given the current size and bitrate, ignoring prio.
114 TimeDelta ExpectedQueueTime() const override;
115
116 // Set the max desired queuing delay, pacer will override the pacing rate
117 // specified by SetPacingRates() if needed to achieve this goal.
118 void SetQueueTimeLimit(TimeDelta limit) override;
119
Erik Språng998524a2020-05-29 14:13:32120 protected:
121 // Exposed as protected for test.
Erik Språng4314a492019-11-26 16:48:49122 struct Stats {
123 Stats()
Jianhui Dai94457792021-12-07 11:34:36124 : oldest_packet_enqueue_time(Timestamp::MinusInfinity()),
Erik Språng4314a492019-11-26 16:48:49125 queue_size(DataSize::Zero()),
126 expected_queue_time(TimeDelta::Zero()) {}
Jianhui Dai94457792021-12-07 11:34:36127 Timestamp oldest_packet_enqueue_time;
Erik Språng4314a492019-11-26 16:48:49128 DataSize queue_size;
129 TimeDelta expected_queue_time;
130 absl::optional<Timestamp> first_sent_packet_time;
131 };
Jianhui Dai94457792021-12-07 11:34:36132 void OnStatsUpdated(const Stats& stats);
Erik Språng4314a492019-11-26 16:48:49133
Erik Språng998524a2020-05-29 14:13:32134 private:
Markus Handelle32b6222023-05-08 22:59:46135 // Call in response to state updates that could warrant sending out packets.
136 // Protected against re-entry from packet sent receipts.
137 void MaybeScheduleProcessPackets() RTC_RUN_ON(task_queue_);
Erik Språng4314a492019-11-26 16:48:49138 // Check if it is time to send packets, or schedule a delayed task if not.
139 // Use Timestamp::MinusInfinity() to indicate that this call has _not_
Markus Handelle32b6222023-05-08 22:59:46140 // been scheduled by the pacing controller. If this is the case, check if we
Erik Språng4314a492019-11-26 16:48:49141 // can execute immediately otherwise schedule a delay task that calls this
142 // method again with desired (finite) scheduled process time.
143 void MaybeProcessPackets(Timestamp scheduled_process_time);
144
Jianhui Dai94457792021-12-07 11:34:36145 void UpdateStats() RTC_RUN_ON(task_queue_);
Erik Språng4314a492019-11-26 16:48:49146 Stats GetStats() const;
147
148 Clock* const clock_;
Evan Shrubsole262b2d82023-02-17 09:08:06149
Henrik Boström554bb392022-03-16 09:16:29150 // The holdback window prevents too frequent delayed MaybeProcessPackets()
Henrik Boströma768f522022-04-13 10:37:59151 // calls. These are only applicable if `allow_low_precision` is false.
Erik Språng0f86c1f2021-10-26 14:19:03152 const TimeDelta max_hold_back_window_;
153 const int max_hold_back_window_in_packets_;
154
Erik Språng4314a492019-11-26 16:48:49155 PacingController pacing_controller_ RTC_GUARDED_BY(task_queue_);
156
157 // We want only one (valid) delayed process task in flight at a time.
Artem Titovee3e3fd2021-07-28 18:28:28158 // If the value of `next_process_time_` is finite, it is an id for a
Erik Språng4314a492019-11-26 16:48:49159 // delayed task that will call MaybeProcessPackets() with that time
160 // as parameter.
161 // Timestamp::MinusInfinity() indicates no valid pending task.
162 Timestamp next_process_time_ RTC_GUARDED_BY(task_queue_);
163
Etienne Pierre-doray03bce3f2021-03-29 17:36:15164 // Indicates if this task queue is started. If not, don't allow
165 // posting delayed tasks yet.
Jianhui Dai94457792021-12-07 11:34:36166 bool is_started_ RTC_GUARDED_BY(task_queue_);
Etienne Pierre-doray03bce3f2021-03-29 17:36:15167
Erik Språng4314a492019-11-26 16:48:49168 // Indicates if this task queue is shutting down. If so, don't allow
169 // posting any more delayed tasks as that can cause the task queue to
170 // never drain.
171 bool is_shutdown_ RTC_GUARDED_BY(task_queue_);
172
Erik Språng0f86c1f2021-10-26 14:19:03173 // Filtered size of enqueued packets, in bytes.
174 rtc::ExpFilter packet_size_ RTC_GUARDED_BY(task_queue_);
Jianhui Daidf59e532022-03-19 07:38:51175 bool include_overhead_ RTC_GUARDED_BY(task_queue_);
Erik Språng0f86c1f2021-10-26 14:19:03176
Per K401c14a2023-04-11 07:53:48177 Stats current_stats_ RTC_GUARDED_BY(task_queue_);
Markus Handelle32b6222023-05-08 22:59:46178 // Protects against ProcessPackets reentry from packet sent receipts.
179 bool processing_packets_ RTC_GUARDED_BY(task_queue_) = false;
Erik Språng4314a492019-11-26 16:48:49180
Per Kjellander9dc43052022-10-04 11:45:09181 ScopedTaskSafety safety_;
Per K401c14a2023-04-11 07:53:48182 TaskQueueBase* task_queue_;
Erik Språng4314a492019-11-26 16:48:49183};
184} // namespace webrtc
185#endif // MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_