2019-11-26 17:48:49 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
|
|
|
|
|
#define MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
2024-08-29 13:00:40 +00:00
|
|
|
#include <optional>
|
2019-11-26 17:48:49 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
2022-03-29 11:04:48 +02:00
|
|
|
#include "api/field_trials_view.h"
|
2024-09-04 12:49:05 +02:00
|
|
|
#include "api/rtp_packet_sender.h"
|
2021-02-10 14:31:24 +01:00
|
|
|
#include "api/sequence_checker.h"
|
2023-04-13 10:08:45 +02:00
|
|
|
#include "api/task_queue/pending_task_safety_flag.h"
|
2024-09-04 12:49:05 +02:00
|
|
|
#include "api/task_queue/task_queue_base.h"
|
|
|
|
|
#include "api/transport/network_types.h"
|
|
|
|
|
#include "api/units/data_rate.h"
|
2019-11-26 17:48:49 +01:00
|
|
|
#include "api/units/data_size.h"
|
|
|
|
|
#include "api/units/time_delta.h"
|
|
|
|
|
#include "api/units/timestamp.h"
|
|
|
|
|
#include "modules/pacing/pacing_controller.h"
|
|
|
|
|
#include "modules/pacing/rtp_packet_pacer.h"
|
|
|
|
|
#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
|
2021-10-26 16:19:03 +02:00
|
|
|
#include "rtc_base/numerics/exp_filter.h"
|
2019-11-26 17:48:49 +01:00
|
|
|
#include "rtc_base/thread_annotations.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
class Clock;
|
|
|
|
|
|
2020-06-30 11:53:37 +00:00
|
|
|
class TaskQueuePacedSender : public RtpPacketPacer, public RtpPacketSender {
|
2019-11-26 17:48:49 +01:00
|
|
|
public:
|
2022-03-19 15:38:51 +08:00
|
|
|
static const int kNoPacketHoldback;
|
|
|
|
|
|
2022-11-15 09:23:19 +01:00
|
|
|
// The pacer can be configured using `field_trials` or specified parameters.
|
|
|
|
|
//
|
2021-07-28 20:28:28 +02:00
|
|
|
// The `hold_back_window` parameter sets a lower bound on time to sleep if
|
2020-05-19 17:40:58 +02:00
|
|
|
// there is currently a pacer queue and packets can't immediately be
|
|
|
|
|
// processed. Increasing this reduces thread wakeups at the expense of higher
|
|
|
|
|
// latency.
|
2022-11-15 09:23:19 +01:00
|
|
|
//
|
2023-04-11 09:53:48 +02:00
|
|
|
// The taskqueue used when constructing a TaskQueuePacedSender will also be
|
|
|
|
|
// used for pacing.
|
2023-11-27 16:06:44 +01:00
|
|
|
TaskQueuePacedSender(Clock* clock,
|
|
|
|
|
PacingController::PacketSender* packet_sender,
|
|
|
|
|
const FieldTrialsView& field_trials,
|
|
|
|
|
TimeDelta max_hold_back_window,
|
|
|
|
|
int max_hold_back_window_in_packets);
|
2019-11-26 17:48:49 +01:00
|
|
|
|
|
|
|
|
~TaskQueuePacedSender() override;
|
|
|
|
|
|
2023-11-27 16:06:44 +01:00
|
|
|
// The pacer is allowed to send enqued packets in bursts and can build up a
|
|
|
|
|
// packet "debt" that correspond to approximately the send rate during
|
|
|
|
|
// 'burst_interval'.
|
|
|
|
|
void SetSendBurstInterval(TimeDelta burst_interval);
|
|
|
|
|
|
2024-01-26 12:57:01 +01:00
|
|
|
// A probe may be sent without first waing for a media packet.
|
|
|
|
|
void SetAllowProbeWithoutMediaPacket(bool allow);
|
|
|
|
|
|
2021-03-29 17:36:15 +00:00
|
|
|
// Ensure that necessary delayed tasks are scheduled.
|
|
|
|
|
void EnsureStarted();
|
|
|
|
|
|
2019-11-26 17:48:49 +01:00
|
|
|
// Methods implementing RtpPacketSender.
|
|
|
|
|
|
2021-04-09 13:41:53 +02:00
|
|
|
// Adds the packet to the queue and calls
|
|
|
|
|
// PacingController::PacketSender::SendPacket() when it's time to send.
|
2019-11-26 17:48:49 +01:00
|
|
|
void EnqueuePackets(
|
|
|
|
|
std::vector<std::unique_ptr<RtpPacketToSend>> packets) override;
|
2022-12-09 21:38:44 +01:00
|
|
|
// Remove any pending packets matching this SSRC from the packet queue.
|
|
|
|
|
void RemovePacketsForSsrc(uint32_t ssrc) override;
|
2019-11-26 17:48:49 +01:00
|
|
|
|
2021-12-07 19:34:36 +08:00
|
|
|
// Methods implementing RtpPacketPacer.
|
2019-11-26 17:48:49 +01:00
|
|
|
|
2022-05-16 19:58:40 +02:00
|
|
|
void CreateProbeClusters(
|
|
|
|
|
std::vector<ProbeClusterConfig> probe_cluster_configs) override;
|
2019-11-26 17:48:49 +01:00
|
|
|
|
|
|
|
|
// Temporarily pause all sending.
|
|
|
|
|
void Pause() override;
|
|
|
|
|
|
|
|
|
|
// Resume sending packets.
|
|
|
|
|
void Resume() override;
|
|
|
|
|
|
2022-03-16 14:20:49 +01:00
|
|
|
void SetCongested(bool congested) override;
|
2019-11-26 17:48:49 +01:00
|
|
|
|
|
|
|
|
// Sets the pacing rates. Must be called once before packets can be sent.
|
|
|
|
|
void SetPacingRates(DataRate pacing_rate, DataRate padding_rate) override;
|
|
|
|
|
|
|
|
|
|
// Currently audio traffic is not accounted for by pacer and passed through.
|
|
|
|
|
// With the introduction of audio BWE, audio traffic will be accounted for
|
|
|
|
|
// in the pacer budget calculation. The audio traffic will still be injected
|
|
|
|
|
// at high priority.
|
|
|
|
|
void SetAccountForAudioPackets(bool account_for_audio) override;
|
|
|
|
|
|
2020-01-29 17:42:52 +01:00
|
|
|
void SetIncludeOverhead() override;
|
2020-01-29 18:45:00 +00:00
|
|
|
void SetTransportOverhead(DataSize overhead_per_packet) override;
|
|
|
|
|
|
2019-11-26 17:48:49 +01:00
|
|
|
// Returns the time since the oldest queued packet was enqueued.
|
|
|
|
|
TimeDelta OldestPacketWaitTime() const override;
|
|
|
|
|
|
|
|
|
|
// Returns total size of all packets in the pacer queue.
|
|
|
|
|
DataSize QueueSizeData() const override;
|
|
|
|
|
|
|
|
|
|
// Returns the time when the first packet was sent;
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<Timestamp> FirstSentPacketTime() const override;
|
2019-11-26 17:48:49 +01:00
|
|
|
|
|
|
|
|
// Returns the number of milliseconds it will take to send the current
|
|
|
|
|
// packets in the queue, given the current size and bitrate, ignoring prio.
|
|
|
|
|
TimeDelta ExpectedQueueTime() const override;
|
|
|
|
|
|
|
|
|
|
// Set the max desired queuing delay, pacer will override the pacing rate
|
|
|
|
|
// specified by SetPacingRates() if needed to achieve this goal.
|
|
|
|
|
void SetQueueTimeLimit(TimeDelta limit) override;
|
|
|
|
|
|
2020-05-29 16:13:32 +02:00
|
|
|
protected:
|
|
|
|
|
// Exposed as protected for test.
|
2019-11-26 17:48:49 +01:00
|
|
|
struct Stats {
|
|
|
|
|
Stats()
|
2021-12-07 19:34:36 +08:00
|
|
|
: oldest_packet_enqueue_time(Timestamp::MinusInfinity()),
|
2019-11-26 17:48:49 +01:00
|
|
|
queue_size(DataSize::Zero()),
|
|
|
|
|
expected_queue_time(TimeDelta::Zero()) {}
|
2021-12-07 19:34:36 +08:00
|
|
|
Timestamp oldest_packet_enqueue_time;
|
2019-11-26 17:48:49 +01:00
|
|
|
DataSize queue_size;
|
|
|
|
|
TimeDelta expected_queue_time;
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<Timestamp> first_sent_packet_time;
|
2019-11-26 17:48:49 +01:00
|
|
|
};
|
2021-12-07 19:34:36 +08:00
|
|
|
void OnStatsUpdated(const Stats& stats);
|
2019-11-26 17:48:49 +01:00
|
|
|
|
2020-05-29 16:13:32 +02:00
|
|
|
private:
|
2023-05-09 00:59:46 +02:00
|
|
|
// Call in response to state updates that could warrant sending out packets.
|
|
|
|
|
// Protected against re-entry from packet sent receipts.
|
|
|
|
|
void MaybeScheduleProcessPackets() RTC_RUN_ON(task_queue_);
|
2019-11-26 17:48:49 +01:00
|
|
|
// Check if it is time to send packets, or schedule a delayed task if not.
|
|
|
|
|
// Use Timestamp::MinusInfinity() to indicate that this call has _not_
|
2023-05-09 00:59:46 +02:00
|
|
|
// been scheduled by the pacing controller. If this is the case, check if we
|
2019-11-26 17:48:49 +01:00
|
|
|
// can execute immediately otherwise schedule a delay task that calls this
|
|
|
|
|
// method again with desired (finite) scheduled process time.
|
|
|
|
|
void MaybeProcessPackets(Timestamp scheduled_process_time);
|
|
|
|
|
|
2021-12-07 19:34:36 +08:00
|
|
|
void UpdateStats() RTC_RUN_ON(task_queue_);
|
2019-11-26 17:48:49 +01:00
|
|
|
Stats GetStats() const;
|
|
|
|
|
|
|
|
|
|
Clock* const clock_;
|
2023-02-17 09:08:06 +00:00
|
|
|
|
2022-03-16 10:16:29 +01:00
|
|
|
// The holdback window prevents too frequent delayed MaybeProcessPackets()
|
2022-04-13 12:37:59 +02:00
|
|
|
// calls. These are only applicable if `allow_low_precision` is false.
|
2021-10-26 16:19:03 +02:00
|
|
|
const TimeDelta max_hold_back_window_;
|
|
|
|
|
const int max_hold_back_window_in_packets_;
|
|
|
|
|
|
2019-11-26 17:48:49 +01:00
|
|
|
PacingController pacing_controller_ RTC_GUARDED_BY(task_queue_);
|
|
|
|
|
|
|
|
|
|
// We want only one (valid) delayed process task in flight at a time.
|
2021-07-28 20:28:28 +02:00
|
|
|
// If the value of `next_process_time_` is finite, it is an id for a
|
2019-11-26 17:48:49 +01:00
|
|
|
// delayed task that will call MaybeProcessPackets() with that time
|
|
|
|
|
// as parameter.
|
|
|
|
|
// Timestamp::MinusInfinity() indicates no valid pending task.
|
|
|
|
|
Timestamp next_process_time_ RTC_GUARDED_BY(task_queue_);
|
|
|
|
|
|
2021-03-29 17:36:15 +00:00
|
|
|
// Indicates if this task queue is started. If not, don't allow
|
|
|
|
|
// posting delayed tasks yet.
|
2021-12-07 19:34:36 +08:00
|
|
|
bool is_started_ RTC_GUARDED_BY(task_queue_);
|
2021-03-29 17:36:15 +00:00
|
|
|
|
2019-11-26 17:48:49 +01:00
|
|
|
// Indicates if this task queue is shutting down. If so, don't allow
|
|
|
|
|
// posting any more delayed tasks as that can cause the task queue to
|
|
|
|
|
// never drain.
|
|
|
|
|
bool is_shutdown_ RTC_GUARDED_BY(task_queue_);
|
|
|
|
|
|
2021-10-26 16:19:03 +02:00
|
|
|
// Filtered size of enqueued packets, in bytes.
|
|
|
|
|
rtc::ExpFilter packet_size_ RTC_GUARDED_BY(task_queue_);
|
2022-03-19 15:38:51 +08:00
|
|
|
bool include_overhead_ RTC_GUARDED_BY(task_queue_);
|
2021-10-26 16:19:03 +02:00
|
|
|
|
2023-04-11 09:53:48 +02:00
|
|
|
Stats current_stats_ RTC_GUARDED_BY(task_queue_);
|
2023-05-09 00:59:46 +02:00
|
|
|
// Protects against ProcessPackets reentry from packet sent receipts.
|
|
|
|
|
bool processing_packets_ RTC_GUARDED_BY(task_queue_) = false;
|
2019-11-26 17:48:49 +01:00
|
|
|
|
2022-10-04 13:45:09 +02:00
|
|
|
ScopedTaskSafety safety_;
|
2023-04-11 09:53:48 +02:00
|
|
|
TaskQueueBase* task_queue_;
|
2019-11-26 17:48:49 +01:00
|
|
|
};
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
#endif // MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
|