blob: 85a49eca9cab8c8326d13328d8727042676a70e7 [file] [log] [blame]
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +00001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_PACING_PACED_SENDER_H_
12#define MODULES_PACING_PACED_SENDER_H_
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Christoffer Rodbroc610e262019-01-08 10:49:19 +010017#include <atomic>
kwiberg22feaa32016-03-17 09:17:43 -070018#include <memory>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000019
Danil Chapovalov0040b662018-06-18 10:48:16 +020020#include "absl/types/optional.h"
Erik Språngf6468d22019-07-05 16:53:43 +020021#include "api/function_view.h"
Per Kjellander7ef34f82019-02-22 13:09:32 +010022#include "api/transport/field_trial_based_config.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "api/transport/network_types.h"
Per Kjellander7ef34f82019-02-22 13:09:32 +010024#include "api/transport/webrtc_key_value_config.h"
Sebastian Jansson4ad51d82019-06-11 11:24:40 +020025#include "modules/include/module.h"
Sebastian Jansson03914462018-10-11 20:22:03 +020026#include "modules/pacing/bitrate_prober.h"
27#include "modules/pacing/interval_budget.h"
Erik Språnge7942432019-06-12 13:30:02 +020028#include "modules/pacing/packet_router.h"
Sebastian Jansson60570dc2018-09-13 17:11:06 +020029#include "modules/pacing/round_robin_packet_queue.h"
Erik Språngaa59eca2019-07-24 14:52:55 +020030#include "modules/rtp_rtcp/include/rtp_packet_sender.h"
Erik Språng58ee1872019-06-18 16:20:11 +020031#include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
Yves Gerey988cc082018-10-23 12:03:01 +020032#include "modules/utility/include/process_thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/critical_section.h"
Christoffer Rodbroe2e00002018-12-20 15:46:03 +010034#include "rtc_base/experiments/field_trial_parser.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/thread_annotations.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000036
37namespace webrtc {
Yves Gerey988cc082018-10-23 12:03:01 +020038class Clock;
39class RtcEventLog;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000040
Erik Språngaa59eca2019-07-24 14:52:55 +020041class PacedSender : public Module, public RtpPacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000042 public:
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010043 static constexpr int64_t kNoCongestionWindow = -1;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000044
sprang0a43fef2015-11-20 09:00:37 -080045 // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
46 // this value, the packet producers should wait (eg drop frames rather than
47 // encoding them). Bitrate sent may temporarily exceed target set by
48 // UpdateBitrate() so that this limit will be upheld.
49 static const int64_t kMaxQueueLengthMs;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000050 // Pacing-rate relative to our target send rate.
51 // Multiplicative factor that is applied to the target bitrate to calculate
52 // the number of bytes that can be transmitted per interval.
53 // Increasing this factor will result in lower delays in cases of bitrate
54 // overshoots from the encoder.
55 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000056
Sebastian Janssonaa01f272019-01-30 11:28:59 +010057 PacedSender(Clock* clock,
Erik Språnge7942432019-06-12 13:30:02 +020058 PacketRouter* packet_router,
Per Kjellander7ef34f82019-02-22 13:09:32 +010059 RtcEventLog* event_log,
60 const WebRtcKeyValueConfig* field_trials = nullptr);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000061
nisse76e62b02017-05-31 02:24:52 -070062 ~PacedSender() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000063
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -080064 virtual void CreateProbeCluster(int bitrate_bps, int cluster_id);
philipeleb680ea2016-08-17 11:11:59 +020065
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000066 // Temporarily pause all sending.
67 void Pause();
68
69 // Resume sending packets.
70 void Resume();
71
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010072 void SetCongestionWindow(int64_t congestion_window_bytes);
73 void UpdateOutstandingData(int64_t outstanding_bytes);
74
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000075 // Enable bitrate probing. Enabled by default, mostly here to simplify
76 // testing. Must be called before any packets are being sent to have an
77 // effect.
78 void SetProbingEnabled(bool enabled);
79
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010080 // Sets the pacing rates. Must be called once before packets can be sent.
Sebastian Jansson4ad51d82019-06-11 11:24:40 +020081 void SetPacingRates(uint32_t pacing_rate_bps, uint32_t padding_rate_bps);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010082
Erik Språng58ee1872019-06-18 16:20:11 +020083 // Adds the packet information to the queue and calls TimeToSendPacket
84 // when it's time to send.
Peter Boströme23e7372015-10-08 11:44:14 +020085 void InsertPacket(RtpPacketSender::Priority priority,
86 uint32_t ssrc,
87 uint16_t sequence_number,
88 int64_t capture_time_ms,
89 size_t bytes,
90 bool retransmission) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000091
Erik Språng58ee1872019-06-18 16:20:11 +020092 // Adds the packet to the queue and calls PacketRouter::SendPacket() when
93 // it's time to send.
94 void EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet) override;
95
Alex Narest78609d52017-10-20 10:37:47 +020096 // Currently audio traffic is not accounted by pacer and passed through.
97 // With the introduction of audio BWE audio traffic will be accounted for
98 // the pacer budget calculation. The audio traffic still will be injected
99 // at high priority.
Erik Språngaa59eca2019-07-24 14:52:55 +0200100 void SetAccountForAudioPackets(bool account_for_audio);
Alex Narest78609d52017-10-20 10:37:47 +0200101
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000102 // Returns the time since the oldest queued packet was enqueued.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000103 virtual int64_t QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000104
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000105 virtual size_t QueueSizePackets() const;
Christoffer Rodbroc610e262019-01-08 10:49:19 +0100106 virtual int64_t QueueSizeBytes() const;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000107
asaperssonfc5e81c2017-04-19 23:28:53 -0700108 // Returns the time when the first packet was sent, or -1 if no packet is
109 // sent.
110 virtual int64_t FirstSentPacketTimeMs() const;
111
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000112 // 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.
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000114 virtual int64_t ExpectedQueueTimeMs() const;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000115
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000116 // Returns the number of milliseconds until the module want a worker thread
117 // to call Process.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 int64_t TimeUntilNextProcess() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000119
120 // Process any pending packets in the queue(s).
pbosa26ac922016-02-25 04:50:01 -0800121 void Process() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000122
tommi919dce22017-03-15 07:45:36 -0700123 // Called when the prober is associated with a process thread.
124 void ProcessThreadAttached(ProcessThread* process_thread) override;
sprang89c4a7e2017-06-30 13:27:40 -0700125 void SetQueueTimeLimit(int limit_ms);
126
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000127 private:
Sebastian Jansson916ae082018-10-26 13:10:23 +0200128 int64_t UpdateTimeAndGetElapsedMs(int64_t now_us)
129 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
130 bool ShouldSendKeepalive(int64_t at_time_us) const
131 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
132
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000133 // Updates the number of bytes that can be sent for the next time interval.
isheriff31687812016-10-04 08:43:09 -0700134 void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms)
danilchap56359be2017-09-07 07:53:45 -0700135 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
isheriff31687812016-10-04 08:43:09 -0700136 void UpdateBudgetWithBytesSent(size_t bytes)
danilchap56359be2017-09-07 07:53:45 -0700137 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000138
Erik Språngf6468d22019-07-05 16:53:43 +0200139 size_t PaddingBytesToAdd(absl::optional<size_t> recommended_probe_size,
140 size_t bytes_sent)
141 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
142
Erik Språng58ee1872019-06-18 16:20:11 +0200143 RoundRobinPacketQueue::QueuedPacket* GetPendingPacket(
Sebastian Jansson916ae082018-10-26 13:10:23 +0200144 const PacedPacketInfo& pacing_info)
danilchap56359be2017-09-07 07:53:45 -0700145 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
Erik Språng58ee1872019-06-18 16:20:11 +0200146 void OnPacketSent(RoundRobinPacketQueue::QueuedPacket* packet)
Sebastian Jansson916ae082018-10-26 13:10:23 +0200147 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
148 void OnPaddingSent(size_t padding_sent)
danilchap56359be2017-09-07 07:53:45 -0700149 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000150
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100151 bool Congested() const RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
Erik Språng96816752018-09-04 18:40:19 +0200152 int64_t TimeMilliseconds() const RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100153
Sebastian Janssonaa01f272019-01-30 11:28:59 +0100154 Clock* const clock_;
Erik Språnge7942432019-06-12 13:30:02 +0200155 PacketRouter* const packet_router_;
Per Kjellander5b698732019-04-15 12:36:33 +0200156 const std::unique_ptr<FieldTrialBasedConfig> fallback_field_trials_;
157 const WebRtcKeyValueConfig* field_trials_;
Sebastian Jansson0601d682018-06-25 19:23:05 +0200158
159 const bool drain_large_queues_;
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200160 const bool send_padding_if_silent_;
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100161 const bool pace_audio_;
Christoffer Rodbroe2e00002018-12-20 15:46:03 +0100162 FieldTrialParameter<int> min_packet_limit_ms_;
Erik Språng96816752018-09-04 18:40:19 +0200163
kthelgason6bfe49c2017-03-30 01:14:41 -0700164 rtc::CriticalSection critsect_;
Erik Språng96816752018-09-04 18:40:19 +0200165 // TODO(webrtc:9716): Remove this when we are certain clocks are monotonic.
166 // The last millisecond timestamp returned by |clock_|.
167 mutable int64_t last_timestamp_ms_ RTC_GUARDED_BY(critsect_);
danilchap56359be2017-09-07 07:53:45 -0700168 bool paused_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000169 // This is the media budget, keeping track of how many bits of media
170 // we can pace out during the current interval.
Sebastian Jansson03914462018-10-11 20:22:03 +0200171 IntervalBudget media_budget_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000172 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000173 // allowed to send out during the current interval. This budget will be
174 // utilized when there's no media to send.
Sebastian Jansson03914462018-10-11 20:22:03 +0200175 IntervalBudget padding_budget_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000176
Sebastian Jansson03914462018-10-11 20:22:03 +0200177 BitrateProber prober_ RTC_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200178 bool probing_send_failure_ RTC_GUARDED_BY(critsect_);
Sebastian Jansson4ad51d82019-06-11 11:24:40 +0200179
danilchap56359be2017-09-07 07:53:45 -0700180 uint32_t pacing_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000181
Sebastian Janssona1630f82018-02-21 13:39:26 +0100182 int64_t time_last_process_us_ RTC_GUARDED_BY(critsect_);
183 int64_t last_send_time_us_ RTC_GUARDED_BY(critsect_);
danilchap56359be2017-09-07 07:53:45 -0700184 int64_t first_sent_packet_ms_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000185
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200186 RoundRobinPacketQueue packets_ RTC_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200187 uint64_t packet_counter_ RTC_GUARDED_BY(critsect_);
sprang89c4a7e2017-06-30 13:27:40 -0700188
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100189 int64_t congestion_window_bytes_ RTC_GUARDED_BY(critsect_) =
190 kNoCongestionWindow;
191 int64_t outstanding_bytes_ RTC_GUARDED_BY(critsect_) = 0;
Sebastian Jansson4ad51d82019-06-11 11:24:40 +0200192
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100193 // Lock to avoid race when attaching process thread. This can happen due to
Sebastian Jansson4ad51d82019-06-11 11:24:40 +0200194 // the Call class setting network state on RtpTransportControllerSend, which
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100195 // in turn calls Pause/Resume on Pacedsender, before actually starting the
Sebastian Jansson4ad51d82019-06-11 11:24:40 +0200196 // pacer process thread. If RtpTransportControllerSend is running on a task
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100197 // queue separate from the thread used by Call, this causes a race.
198 rtc::CriticalSection process_thread_lock_;
199 ProcessThread* process_thread_ RTC_GUARDED_BY(process_thread_lock_) = nullptr;
200
danilchap56359be2017-09-07 07:53:45 -0700201 int64_t queue_time_limit RTC_GUARDED_BY(critsect_);
Alex Narest78609d52017-10-20 10:37:47 +0200202 bool account_for_audio_ RTC_GUARDED_BY(critsect_);
Erik Språngf6468d22019-07-05 16:53:43 +0200203
204 // If true, PacedSender should only reference packets as in legacy mode.
205 // If false, PacedSender may have direct ownership of RtpPacketToSend objects.
206 // Defaults to true, will be changed to default false soon.
207 const bool legacy_packet_referencing_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000208};
209} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200210#endif // MODULES_PACING_PACED_SENDER_H_