2019-08-14 10:43:47 +02: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_PACING_CONTROLLER_H_
|
|
|
|
|
#define MODULES_PACING_PACING_CONTROLLER_H_
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#include "absl/types/optional.h"
|
2022-03-29 11:04:48 +02:00
|
|
|
#include "api/field_trials_view.h"
|
2019-08-14 10:43:47 +02:00
|
|
|
#include "api/function_view.h"
|
|
|
|
|
#include "api/transport/field_trial_based_config.h"
|
|
|
|
|
#include "api/transport/network_types.h"
|
|
|
|
|
#include "modules/pacing/bitrate_prober.h"
|
|
|
|
|
#include "modules/pacing/interval_budget.h"
|
|
|
|
|
#include "modules/pacing/rtp_packet_pacer.h"
|
|
|
|
|
#include "modules/rtp_rtcp/include/rtp_packet_sender.h"
|
2020-02-06 16:35:46 +01:00
|
|
|
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
|
2019-08-14 10:43:47 +02:00
|
|
|
#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
|
|
|
|
|
#include "rtc_base/experiments/field_trial_parser.h"
|
|
|
|
|
#include "rtc_base/thread_annotations.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2020-01-28 16:07:36 +01:00
|
|
|
// This class implements a leaky-bucket packet pacing algorithm. It handles the
|
2019-08-14 10:43:47 +02:00
|
|
|
// logic of determining which packets to send when, but the actual timing of
|
2021-11-10 12:58:27 +08:00
|
|
|
// the processing is done externally (e.g. RtpPacketPacer). Furthermore, the
|
2019-08-14 10:43:47 +02:00
|
|
|
// forwarding of packets when they are ready to be sent is also handled
|
2021-11-10 12:58:27 +08:00
|
|
|
// externally, via the PacingController::PacketSender interface.
|
2019-08-14 10:43:47 +02:00
|
|
|
class PacingController {
|
|
|
|
|
public:
|
|
|
|
|
class PacketSender {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~PacketSender() = default;
|
2020-06-30 11:53:37 +00:00
|
|
|
virtual void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
|
|
|
|
|
const PacedPacketInfo& cluster_info) = 0;
|
2020-07-02 17:41:32 +02:00
|
|
|
// Should be called after each call to SendPacket().
|
|
|
|
|
virtual std::vector<std::unique_ptr<RtpPacketToSend>> FetchFec() = 0;
|
2019-08-14 10:43:47 +02:00
|
|
|
virtual std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
|
|
|
|
|
DataSize size) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-27 12:33:02 +02:00
|
|
|
// Interface for class hanlding storage of and prioritization of packets
|
|
|
|
|
// pending to be sent by the pacer.
|
|
|
|
|
// Note that for the methods taking a Timestamp as parameter, the parameter
|
|
|
|
|
// will never decrease between two subsequent calls.
|
|
|
|
|
class PacketQueue {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~PacketQueue() = default;
|
|
|
|
|
|
|
|
|
|
virtual void Push(Timestamp enqueue_time,
|
|
|
|
|
std::unique_ptr<RtpPacketToSend> packet) = 0;
|
|
|
|
|
virtual std::unique_ptr<RtpPacketToSend> Pop() = 0;
|
|
|
|
|
|
2022-05-02 21:18:08 +02:00
|
|
|
virtual int SizeInPackets() const = 0;
|
2022-04-27 12:33:02 +02:00
|
|
|
bool Empty() const { return SizeInPackets() == 0; }
|
|
|
|
|
virtual DataSize SizeInPayloadBytes() const = 0;
|
|
|
|
|
|
|
|
|
|
// If the next packet, that would be returned by Pop() if called
|
|
|
|
|
// now, is an audio packet this method returns the enqueue time
|
|
|
|
|
// of that packet. If queue is empty or top packet is not audio,
|
|
|
|
|
// returns Timestamp::MinusInfinity().
|
|
|
|
|
virtual Timestamp LeadingAudioPacketEnqueueTime() const = 0;
|
|
|
|
|
|
|
|
|
|
// Enqueue time of the oldest packet in the queue,
|
|
|
|
|
// Timestamp::MinusInfinity() if queue is empty.
|
|
|
|
|
virtual Timestamp OldestEnqueueTime() const = 0;
|
|
|
|
|
|
|
|
|
|
// Average queue time for the packets currently in the queue.
|
|
|
|
|
// The queuing time is calculated from Push() to the last UpdateQueueTime()
|
|
|
|
|
// call - with any time spent in a paused state subtracted.
|
2022-05-02 21:18:08 +02:00
|
|
|
// Returns TimeDelta::Zero() for an empty queue.
|
2022-04-27 12:33:02 +02:00
|
|
|
virtual TimeDelta AverageQueueTime() const = 0;
|
|
|
|
|
|
|
|
|
|
// Called during packet processing or when pause stats changes. Since the
|
|
|
|
|
// AverageQueueTime() method does not look at the wall time, this method
|
|
|
|
|
// needs to be called before querying queue time.
|
|
|
|
|
virtual void UpdateAverageQueueTime(Timestamp now) = 0;
|
|
|
|
|
|
|
|
|
|
// Set the pause state, while `paused` is true queuing time is not counted.
|
|
|
|
|
virtual void SetPauseState(bool paused, Timestamp now) = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-14 10:43:47 +02:00
|
|
|
// Expected max pacer delay. If ExpectedQueueTime() is higher than
|
|
|
|
|
// this value, the packet producers should wait (eg drop frames rather than
|
|
|
|
|
// encoding them). Bitrate sent may temporarily exceed target set by
|
|
|
|
|
// UpdateBitrate() so that this limit will be upheld.
|
|
|
|
|
static const TimeDelta kMaxExpectedQueueLength;
|
|
|
|
|
// Pacing-rate relative to our target send rate.
|
|
|
|
|
// Multiplicative factor that is applied to the target bitrate to calculate
|
|
|
|
|
// the number of bytes that can be transmitted per interval.
|
|
|
|
|
// Increasing this factor will result in lower delays in cases of bitrate
|
|
|
|
|
// overshoots from the encoder.
|
|
|
|
|
static const float kDefaultPaceMultiplier;
|
2021-07-28 20:28:28 +02:00
|
|
|
// If no media or paused, wake up at least every `kPausedProcessIntervalMs` in
|
2019-08-14 10:43:47 +02:00
|
|
|
// order to send a keep-alive packet so we don't get stuck in a bad state due
|
|
|
|
|
// to lack of feedback.
|
|
|
|
|
static const TimeDelta kPausedProcessInterval;
|
|
|
|
|
|
2019-11-14 14:15:15 +01:00
|
|
|
static const TimeDelta kMinSleepTime;
|
|
|
|
|
|
2022-03-19 15:38:51 +08:00
|
|
|
// Allow probes to be processed slightly ahead of inteded send time. Currently
|
|
|
|
|
// set to 1ms as this is intended to allow times be rounded down to the
|
|
|
|
|
// nearest millisecond.
|
|
|
|
|
static const TimeDelta kMaxEarlyProbeProcessing;
|
|
|
|
|
|
2022-05-16 13:20:36 +02:00
|
|
|
PacingController(Clock* clock,
|
|
|
|
|
PacketSender* packet_sender,
|
|
|
|
|
const FieldTrialsView& field_trials);
|
|
|
|
|
|
2019-08-14 10:43:47 +02:00
|
|
|
~PacingController();
|
|
|
|
|
|
|
|
|
|
// Adds the packet to the queue and calls PacketRouter::SendPacket() when
|
|
|
|
|
// it's time to send.
|
|
|
|
|
void EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet);
|
|
|
|
|
|
2022-05-16 19:58:40 +02:00
|
|
|
// ABSL_DEPRECATED("Use CreateProbeClusters instead")
|
2019-08-14 10:43:47 +02:00
|
|
|
void CreateProbeCluster(DataRate bitrate, int cluster_id);
|
2022-05-16 19:58:40 +02:00
|
|
|
void CreateProbeClusters(
|
|
|
|
|
rtc::ArrayView<const ProbeClusterConfig> probe_cluster_configs);
|
2019-08-14 10:43:47 +02:00
|
|
|
|
|
|
|
|
void Pause(); // Temporarily pause all sending.
|
|
|
|
|
void Resume(); // Resume sending packets.
|
|
|
|
|
bool IsPaused() const;
|
|
|
|
|
|
2022-03-16 14:20:49 +01:00
|
|
|
void SetCongested(bool congested);
|
2019-08-14 10:43:47 +02:00
|
|
|
|
|
|
|
|
// Sets the pacing rates. Must be called once before packets can be sent.
|
|
|
|
|
void SetPacingRates(DataRate pacing_rate, DataRate padding_rate);
|
2022-05-16 13:20:36 +02:00
|
|
|
DataRate pacing_rate() const { return media_rate_; }
|
2019-08-14 10:43:47 +02:00
|
|
|
|
|
|
|
|
// Currently audio traffic is not accounted by pacer and passed through.
|
|
|
|
|
// With the introduction of audio BWE audio traffic will be accounted for
|
|
|
|
|
// the pacer budget calculation. The audio traffic still will be injected
|
|
|
|
|
// at high priority.
|
|
|
|
|
void SetAccountForAudioPackets(bool account_for_audio);
|
2020-01-29 17:42:52 +01:00
|
|
|
void SetIncludeOverhead();
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2020-01-29 18:45:00 +00:00
|
|
|
void SetTransportOverhead(DataSize overhead_per_packet);
|
|
|
|
|
|
2021-12-07 19:34:36 +08:00
|
|
|
// Returns the time when the oldest packet was queued.
|
|
|
|
|
Timestamp OldestPacketEnqueueTime() const;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2019-11-26 17:48:49 +01:00
|
|
|
// Number of packets in the pacer queue.
|
2019-08-14 10:43:47 +02:00
|
|
|
size_t QueueSizePackets() const;
|
2019-11-26 17:48:49 +01:00
|
|
|
// Totals size of packets in the pacer queue.
|
2019-08-14 10:43:47 +02:00
|
|
|
DataSize QueueSizeData() const;
|
|
|
|
|
|
2019-11-26 17:48:49 +01:00
|
|
|
// Current buffer level, i.e. max of media and padding debt.
|
|
|
|
|
DataSize CurrentBufferLevel() const;
|
|
|
|
|
|
2021-12-07 19:34:36 +08:00
|
|
|
// Returns the time when the first packet was sent.
|
2019-08-14 10:43:47 +02:00
|
|
|
absl::optional<Timestamp> FirstSentPacketTime() const;
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
void SetQueueTimeLimit(TimeDelta limit);
|
|
|
|
|
|
|
|
|
|
// Enable bitrate probing. Enabled by default, mostly here to simplify
|
|
|
|
|
// testing. Must be called before any packets are being sent to have an
|
|
|
|
|
// effect.
|
|
|
|
|
void SetProbingEnabled(bool enabled);
|
|
|
|
|
|
2019-11-14 14:15:15 +01:00
|
|
|
// Returns the next time we expect ProcessPackets() to be called.
|
|
|
|
|
Timestamp NextSendTime() const;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
|
|
|
|
// Check queue of pending packets and send them or padding packets, if budget
|
|
|
|
|
// is available.
|
|
|
|
|
void ProcessPackets();
|
|
|
|
|
|
2020-05-19 17:40:58 +02:00
|
|
|
bool IsProbing() const;
|
|
|
|
|
|
2019-08-14 10:43:47 +02:00
|
|
|
private:
|
|
|
|
|
TimeDelta UpdateTimeAndGetElapsed(Timestamp now);
|
|
|
|
|
bool ShouldSendKeepalive(Timestamp now) const;
|
|
|
|
|
|
|
|
|
|
// Updates the number of bytes that can be sent for the next time interval.
|
|
|
|
|
void UpdateBudgetWithElapsedTime(TimeDelta delta);
|
|
|
|
|
void UpdateBudgetWithSentData(DataSize size);
|
2022-03-19 15:38:51 +08:00
|
|
|
void UpdatePaddingBudgetWithSentData(DataSize size);
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2020-07-17 12:06:12 +02:00
|
|
|
DataSize PaddingToAdd(DataSize recommended_probe_size,
|
2019-11-14 14:15:15 +01:00
|
|
|
DataSize data_sent) const;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2019-11-18 13:40:24 +01:00
|
|
|
std::unique_ptr<RtpPacketToSend> GetPendingPacket(
|
2019-11-14 14:15:15 +01:00
|
|
|
const PacedPacketInfo& pacing_info,
|
|
|
|
|
Timestamp target_send_time,
|
|
|
|
|
Timestamp now);
|
2020-02-06 16:35:46 +01:00
|
|
|
void OnPacketSent(RtpPacketMediaType packet_type,
|
2019-11-18 13:40:24 +01:00
|
|
|
DataSize packet_size,
|
2019-11-14 14:15:15 +01:00
|
|
|
Timestamp send_time);
|
2019-08-14 10:43:47 +02:00
|
|
|
|
|
|
|
|
Timestamp CurrentTime() const;
|
|
|
|
|
|
|
|
|
|
Clock* const clock_;
|
|
|
|
|
PacketSender* const packet_sender_;
|
2022-03-29 11:04:48 +02:00
|
|
|
const FieldTrialsView& field_trials_;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
|
|
|
|
const bool drain_large_queues_;
|
|
|
|
|
const bool send_padding_if_silent_;
|
|
|
|
|
const bool pace_audio_;
|
2020-01-29 18:45:00 +00:00
|
|
|
const bool ignore_transport_overhead_;
|
2020-04-16 19:41:07 +02:00
|
|
|
// In dynamic mode, indicates the target size when requesting padding,
|
|
|
|
|
// expressed as a duration in order to adjust for varying padding rate.
|
|
|
|
|
const TimeDelta padding_target_duration_;
|
2019-11-14 14:15:15 +01:00
|
|
|
|
2019-08-14 10:43:47 +02:00
|
|
|
TimeDelta min_packet_limit_;
|
|
|
|
|
|
2020-01-29 18:45:00 +00:00
|
|
|
DataSize transport_overhead_per_packet_;
|
|
|
|
|
|
2019-08-14 10:43:47 +02:00
|
|
|
// TODO(webrtc:9716): Remove this when we are certain clocks are monotonic.
|
2021-07-28 20:28:28 +02:00
|
|
|
// The last millisecond timestamp returned by `clock_`.
|
2019-08-14 10:43:47 +02:00
|
|
|
mutable Timestamp last_timestamp_;
|
|
|
|
|
bool paused_;
|
2019-11-14 14:15:15 +01:00
|
|
|
|
|
|
|
|
DataSize media_debt_;
|
|
|
|
|
DataSize padding_debt_;
|
|
|
|
|
DataRate media_rate_;
|
|
|
|
|
DataRate padding_rate_;
|
|
|
|
|
|
2019-08-14 10:43:47 +02:00
|
|
|
BitrateProber prober_;
|
|
|
|
|
bool probing_send_failure_;
|
|
|
|
|
|
2019-11-14 14:15:15 +01:00
|
|
|
Timestamp last_process_time_;
|
2019-08-14 10:43:47 +02:00
|
|
|
Timestamp last_send_time_;
|
|
|
|
|
absl::optional<Timestamp> first_sent_packet_time_;
|
2022-04-27 12:33:02 +02:00
|
|
|
bool seen_first_packet_;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2022-04-27 12:33:02 +02:00
|
|
|
std::unique_ptr<PacketQueue> packet_queue_;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2022-03-16 14:20:49 +01:00
|
|
|
bool congested_;
|
2019-08-14 10:43:47 +02:00
|
|
|
|
2022-03-19 15:38:51 +08:00
|
|
|
TimeDelta queue_time_limit_;
|
2019-08-14 10:43:47 +02:00
|
|
|
bool account_for_audio_;
|
2020-01-29 17:42:52 +01:00
|
|
|
bool include_overhead_;
|
2019-08-14 10:43:47 +02:00
|
|
|
};
|
|
|
|
|
} // namespace webrtc
|
|
|
|
|
|
|
|
|
|
#endif // MODULES_PACING_PACING_CONTROLLER_H_
|