blob: f12917408a6b41684228fb040983d29b3f838120 [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
kwiberg22feaa32016-03-17 09:17:43 -070014#include <memory>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000015
Danil Chapovalov0040b662018-06-18 10:48:16 +020016#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/pacing/pacer.h"
Sebastian Jansson60570dc2018-09-13 17:11:06 +020018#include "modules/pacing/round_robin_packet_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/criticalsection.h"
20#include "rtc_base/thread_annotations.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000021
22namespace webrtc {
Sebastian Janssonea86bb72018-02-14 16:53:38 +000023class AlrDetector;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000024class BitrateProber;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000025class Clock;
philipelc3b3f7a2017-03-29 01:23:13 -070026class RtcEventLog;
tschumim82c55932017-07-11 06:56:04 -070027class IntervalBudget;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000028
gnisha36165c2017-08-20 09:19:58 -070029class PacedSender : public Pacer {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000030 public:
perkjec81bcd2016-05-11 06:01:13 -070031 class PacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000032 public:
33 // Note: packets sent as a result of a callback should not pass by this
34 // module again.
35 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000036 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000037 virtual bool TimeToSendPacket(uint32_t ssrc,
38 uint16_t sequence_number,
39 int64_t capture_time_ms,
philipel29dca2c2016-05-13 11:13:05 +020040 bool retransmission,
philipelc7bf32a2017-02-17 03:59:43 -080041 const PacedPacketInfo& cluster_info) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000042 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000043 // Returns the number of bytes sent.
philipelc7bf32a2017-02-17 03:59:43 -080044 virtual size_t TimeToSendPadding(size_t bytes,
45 const PacedPacketInfo& cluster_info) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000046
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000047 protected:
perkjec81bcd2016-05-11 06:01:13 -070048 virtual ~PacketSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000049 };
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010050 static constexpr int64_t kNoCongestionWindow = -1;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000051
sprang0a43fef2015-11-20 09:00:37 -080052 // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
53 // this value, the packet producers should wait (eg drop frames rather than
54 // encoding them). Bitrate sent may temporarily exceed target set by
55 // UpdateBitrate() so that this limit will be upheld.
56 static const int64_t kMaxQueueLengthMs;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000057 // Pacing-rate relative to our target send rate.
58 // Multiplicative factor that is applied to the target bitrate to calculate
59 // the number of bytes that can be transmitted per interval.
60 // Increasing this factor will result in lower delays in cases of bitrate
61 // overshoots from the encoder.
62 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000063
philipelc3b3f7a2017-03-29 01:23:13 -070064 PacedSender(const Clock* clock,
65 PacketSender* packet_sender,
66 RtcEventLog* event_log);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000067
nisse76e62b02017-05-31 02:24:52 -070068 ~PacedSender() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000069
philipelfd58b612017-01-04 07:05:25 -080070 virtual void CreateProbeCluster(int bitrate_bps);
philipeleb680ea2016-08-17 11:11:59 +020071
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000072 // Temporarily pause all sending.
73 void Pause();
74
75 // Resume sending packets.
76 void Resume();
77
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010078 void SetCongestionWindow(int64_t congestion_window_bytes);
79 void UpdateOutstandingData(int64_t outstanding_bytes);
80
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000081 // Enable bitrate probing. Enabled by default, mostly here to simplify
82 // testing. Must be called before any packets are being sent to have an
83 // effect.
84 void SetProbingEnabled(bool enabled);
85
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010086 // Deprecated, SetPacingRates should be used instead.
Sebastian Janssonea86bb72018-02-14 16:53:38 +000087 void SetEstimatedBitrate(uint32_t bitrate_bps) override;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010088 // Deprecated, SetPacingRates should be used instead.
Sebastian Janssonea86bb72018-02-14 16:53:38 +000089 void SetSendBitrateLimits(int min_send_bitrate_bps,
90 int max_padding_bitrate_bps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000091
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010092 // Sets the pacing rates. Must be called once before packets can be sent.
93 void SetPacingRates(uint32_t pacing_rate_bps,
94 uint32_t padding_rate_bps) override;
95
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000096 // Returns true if we send the packet now, else it will add the packet
97 // information to the queue and call TimeToSendPacket when it's time to send.
Peter Boströme23e7372015-10-08 11:44:14 +020098 void InsertPacket(RtpPacketSender::Priority priority,
99 uint32_t ssrc,
100 uint16_t sequence_number,
101 int64_t capture_time_ms,
102 size_t bytes,
103 bool retransmission) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000104
Alex Narest78609d52017-10-20 10:37:47 +0200105 // Currently audio traffic is not accounted by pacer and passed through.
106 // With the introduction of audio BWE audio traffic will be accounted for
107 // the pacer budget calculation. The audio traffic still will be injected
108 // at high priority.
109 void SetAccountForAudioPackets(bool account_for_audio) override;
110
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000111 // Returns the time since the oldest queued packet was enqueued.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000112 virtual int64_t QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000113
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000114 virtual size_t QueueSizePackets() const;
115
asaperssonfc5e81c2017-04-19 23:28:53 -0700116 // Returns the time when the first packet was sent, or -1 if no packet is
117 // sent.
118 virtual int64_t FirstSentPacketTimeMs() const;
119
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000120 // Returns the number of milliseconds it will take to send the current
121 // packets in the queue, given the current size and bitrate, ignoring prio.
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000122 virtual int64_t ExpectedQueueTimeMs() const;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000123
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100124 // Deprecated, alr detection will be moved out of the pacer.
Danil Chapovalov0040b662018-06-18 10:48:16 +0200125 virtual absl::optional<int64_t> GetApplicationLimitedRegionStartTime() const;
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000126
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000127 // Returns the number of milliseconds until the module want a worker thread
128 // to call Process.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000129 int64_t TimeUntilNextProcess() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000130
131 // Process any pending packets in the queue(s).
pbosa26ac922016-02-25 04:50:01 -0800132 void Process() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000133
tommi919dce22017-03-15 07:45:36 -0700134 // Called when the prober is associated with a process thread.
135 void ProcessThreadAttached(ProcessThread* process_thread) override;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100136 // Deprecated, SetPacingRates should be used instead.
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000137 void SetPacingFactor(float pacing_factor);
sprang89c4a7e2017-06-30 13:27:40 -0700138 void SetQueueTimeLimit(int limit_ms);
139
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000140 private:
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000141 // Updates the number of bytes that can be sent for the next time interval.
isheriff31687812016-10-04 08:43:09 -0700142 void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms)
danilchap56359be2017-09-07 07:53:45 -0700143 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
isheriff31687812016-10-04 08:43:09 -0700144 void UpdateBudgetWithBytesSent(size_t bytes)
danilchap56359be2017-09-07 07:53:45 -0700145 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000146
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200147 bool SendPacket(const RoundRobinPacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800148 const PacedPacketInfo& cluster_info)
danilchap56359be2017-09-07 07:53:45 -0700149 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
philipelc7bf32a2017-02-17 03:59:43 -0800150 size_t SendPadding(size_t padding_needed, const PacedPacketInfo& cluster_info)
danilchap56359be2017-09-07 07:53:45 -0700151 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000152
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100153 void OnBytesSent(size_t bytes_sent) RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
154 bool Congested() const RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
Erik Språng96816752018-09-04 18:40:19 +0200155 int64_t TimeMilliseconds() const RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100156
elad.alon61ce37e2017-03-09 07:09:31 -0800157 const Clock* const clock_;
perkjec81bcd2016-05-11 06:01:13 -0700158 PacketSender* const packet_sender_;
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000159 const std::unique_ptr<AlrDetector> alr_detector_ RTC_PT_GUARDED_BY(critsect_);
Sebastian Jansson0601d682018-06-25 19:23:05 +0200160
161 const bool drain_large_queues_;
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200162 const bool send_padding_if_silent_;
Sebastian Janssonce4829a2018-06-15 14:47:35 +0200163 const bool video_blocks_audio_;
Erik Språng96816752018-09-04 18:40:19 +0200164
kthelgason6bfe49c2017-03-30 01:14:41 -0700165 rtc::CriticalSection critsect_;
Erik Språng96816752018-09-04 18:40:19 +0200166 // TODO(webrtc:9716): Remove this when we are certain clocks are monotonic.
167 // The last millisecond timestamp returned by |clock_|.
168 mutable int64_t last_timestamp_ms_ RTC_GUARDED_BY(critsect_);
danilchap56359be2017-09-07 07:53:45 -0700169 bool paused_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000170 // This is the media budget, keeping track of how many bits of media
171 // we can pace out during the current interval.
Niels Möller712048c2017-10-18 13:08:22 +0200172 const std::unique_ptr<IntervalBudget> media_budget_
173 RTC_PT_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000174 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000175 // allowed to send out during the current interval. This budget will be
176 // utilized when there's no media to send.
Niels Möller712048c2017-10-18 13:08:22 +0200177 const std::unique_ptr<IntervalBudget> padding_budget_
178 RTC_PT_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000179
Niels Möller712048c2017-10-18 13:08:22 +0200180 const std::unique_ptr<BitrateProber> prober_ RTC_PT_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200181 bool probing_send_failure_ RTC_GUARDED_BY(critsect_);
sprang0a43fef2015-11-20 09:00:37 -0800182 // Actual configured bitrates (media_budget_ may temporarily be higher in
183 // order to meet pace time constraint).
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000184 uint32_t estimated_bitrate_bps_ RTC_GUARDED_BY(critsect_);
185 uint32_t min_send_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
186 uint32_t max_padding_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
danilchap56359be2017-09-07 07:53:45 -0700187 uint32_t pacing_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000188
Sebastian Janssona1630f82018-02-21 13:39:26 +0100189 int64_t time_last_process_us_ RTC_GUARDED_BY(critsect_);
190 int64_t last_send_time_us_ RTC_GUARDED_BY(critsect_);
danilchap56359be2017-09-07 07:53:45 -0700191 int64_t first_sent_packet_ms_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000192
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200193 RoundRobinPacketQueue packets_ RTC_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200194 uint64_t packet_counter_ RTC_GUARDED_BY(critsect_);
sprang89c4a7e2017-06-30 13:27:40 -0700195
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100196 int64_t congestion_window_bytes_ RTC_GUARDED_BY(critsect_) =
197 kNoCongestionWindow;
198 int64_t outstanding_bytes_ RTC_GUARDED_BY(critsect_) = 0;
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000199 float pacing_factor_ RTC_GUARDED_BY(critsect_);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100200 // Lock to avoid race when attaching process thread. This can happen due to
201 // the Call class setting network state on SendSideCongestionController, which
202 // in turn calls Pause/Resume on Pacedsender, before actually starting the
203 // pacer process thread. If SendSideCongestionController is running on a task
204 // queue separate from the thread used by Call, this causes a race.
205 rtc::CriticalSection process_thread_lock_;
206 ProcessThread* process_thread_ RTC_GUARDED_BY(process_thread_lock_) = nullptr;
207
danilchap56359be2017-09-07 07:53:45 -0700208 int64_t queue_time_limit RTC_GUARDED_BY(critsect_);
Alex Narest78609d52017-10-20 10:37:47 +0200209 bool account_for_audio_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000210};
211} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200212#endif // MODULES_PACING_PACED_SENDER_H_