blob: 196c9f4be93635b4b46608c4dfee7006f6e40e1c [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
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010011#ifndef WEBRTC_MODULES_PACING_PACED_SENDER_H_
12#define WEBRTC_MODULES_PACING_PACED_SENDER_H_
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000013
14#include <list>
kwiberg22feaa32016-03-17 09:17:43 -070015#include <memory>
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000016#include <set>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000017
kwiberg84f6a3f2017-09-05 08:43:13 -070018#include "webrtc/api/optional.h"
gnisha36165c2017-08-20 09:19:58 -070019#include "webrtc/modules/pacing/pacer.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/criticalsection.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/thread_annotations.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000022#include "webrtc/typedefs.h"
23
24namespace webrtc {
isheriff31687812016-10-04 08:43:09 -070025class AlrDetector;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000026class BitrateProber;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000027class Clock;
philipelfd58b612017-01-04 07:05:25 -080028class ProbeClusterCreatedObserver;
philipelc3b3f7a2017-03-29 01:23:13 -070029class RtcEventLog;
tschumim82c55932017-07-11 06:56:04 -070030class IntervalBudget;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000031
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000032namespace paced_sender {
gnisha36165c2017-08-20 09:19:58 -070033class IntervalBudget;
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000034struct Packet;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +000035class PacketQueue;
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000036} // namespace paced_sender
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000037
gnisha36165c2017-08-20 09:19:58 -070038class PacedSender : public Pacer {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000039 public:
perkjec81bcd2016-05-11 06:01:13 -070040 class PacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000041 public:
42 // Note: packets sent as a result of a callback should not pass by this
43 // module again.
44 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000045 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000046 virtual bool TimeToSendPacket(uint32_t ssrc,
47 uint16_t sequence_number,
48 int64_t capture_time_ms,
philipel29dca2c2016-05-13 11:13:05 +020049 bool retransmission,
philipelc7bf32a2017-02-17 03:59:43 -080050 const PacedPacketInfo& cluster_info) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000051 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000052 // Returns the number of bytes sent.
philipelc7bf32a2017-02-17 03:59:43 -080053 virtual size_t TimeToSendPadding(size_t bytes,
54 const PacedPacketInfo& cluster_info) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000055
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000056 protected:
perkjec81bcd2016-05-11 06:01:13 -070057 virtual ~PacketSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000058 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000059
sprang0a43fef2015-11-20 09:00:37 -080060 // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
61 // this value, the packet producers should wait (eg drop frames rather than
62 // encoding them). Bitrate sent may temporarily exceed target set by
63 // UpdateBitrate() so that this limit will be upheld.
64 static const int64_t kMaxQueueLengthMs;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000065 // Pacing-rate relative to our target send rate.
66 // Multiplicative factor that is applied to the target bitrate to calculate
67 // the number of bytes that can be transmitted per interval.
68 // Increasing this factor will result in lower delays in cases of bitrate
69 // overshoots from the encoder.
70 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000071
philipelc3b3f7a2017-03-29 01:23:13 -070072 PacedSender(const Clock* clock,
73 PacketSender* packet_sender,
74 RtcEventLog* event_log);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000075
nisse76e62b02017-05-31 02:24:52 -070076 ~PacedSender() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000077
philipelfd58b612017-01-04 07:05:25 -080078 virtual void CreateProbeCluster(int bitrate_bps);
philipeleb680ea2016-08-17 11:11:59 +020079
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000080 // Temporarily pause all sending.
81 void Pause();
82
83 // Resume sending packets.
84 void Resume();
85
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000086 // Enable bitrate probing. Enabled by default, mostly here to simplify
87 // testing. Must be called before any packets are being sent to have an
88 // effect.
89 void SetProbingEnabled(bool enabled);
90
perkjec81bcd2016-05-11 06:01:13 -070091 // Sets the estimated capacity of the network. Must be called once before
92 // packets can be sent.
93 // |bitrate_bps| is our estimate of what we are allowed to send on average.
94 // We will pace out bursts of packets at a bitrate of
95 // |bitrate_bps| * kDefaultPaceMultiplier.
gnisha36165c2017-08-20 09:19:58 -070096 void SetEstimatedBitrate(uint32_t bitrate_bps) override;
perkjec81bcd2016-05-11 06:01:13 -070097
perkj71ee44c2016-06-15 00:47:53 -070098 // Sets the minimum send bitrate and maximum padding bitrate requested by send
99 // streams.
100 // |min_send_bitrate_bps| might be higher that the estimated available network
101 // bitrate and if so, the pacer will send with |min_send_bitrate_bps|.
102 // |max_padding_bitrate_bps| might be higher than the estimate available
103 // network bitrate and if so, the pacer will send padding packets to reach
104 // the min of the estimated available bitrate and |max_padding_bitrate_bps|.
105 void SetSendBitrateLimits(int min_send_bitrate_bps,
106 int max_padding_bitrate_bps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000107
108 // Returns true if we send the packet now, else it will add the packet
109 // information to the queue and call TimeToSendPacket when it's time to send.
Peter Boströme23e7372015-10-08 11:44:14 +0200110 void InsertPacket(RtpPacketSender::Priority priority,
111 uint32_t ssrc,
112 uint16_t sequence_number,
113 int64_t capture_time_ms,
114 size_t bytes,
115 bool retransmission) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000116
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000117 // Returns the time since the oldest queued packet was enqueued.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000118 virtual int64_t QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000119
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000120 virtual size_t QueueSizePackets() const;
121
asaperssonfc5e81c2017-04-19 23:28:53 -0700122 // Returns the time when the first packet was sent, or -1 if no packet is
123 // sent.
124 virtual int64_t FirstSentPacketTimeMs() const;
125
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000126 // Returns the number of milliseconds it will take to send the current
127 // packets in the queue, given the current size and bitrate, ignoring prio.
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000128 virtual int64_t ExpectedQueueTimeMs() const;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000129
sergeyu80ed35e2016-11-28 13:11:13 -0800130 // Returns time in milliseconds when the current application-limited region
131 // started or empty result if the sender is currently not application-limited.
132 //
133 // Application Limited Region (ALR) refers to operating in a state where the
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700134 // traffic on network is limited due to application not having enough
135 // traffic to meet the current channel capacity.
sergeyu80ed35e2016-11-28 13:11:13 -0800136 virtual rtc::Optional<int64_t> GetApplicationLimitedRegionStartTime() const;
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700137
Erik Språngad113e52015-11-26 16:26:12 +0100138 // Returns the average time since being enqueued, in milliseconds, for all
sprangddcfb9f2017-08-16 05:38:49 -0700139 // packets currently in the pacer queue, excluding any time the pacer has been
140 // paused. Returns 0 if queue is empty.
Erik Språngad113e52015-11-26 16:26:12 +0100141 virtual int64_t AverageQueueTimeMs();
142
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000143 // Returns the number of milliseconds until the module want a worker thread
144 // to call Process.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000145 int64_t TimeUntilNextProcess() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000146
147 // Process any pending packets in the queue(s).
pbosa26ac922016-02-25 04:50:01 -0800148 void Process() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000149
tommi919dce22017-03-15 07:45:36 -0700150 // Called when the prober is associated with a process thread.
151 void ProcessThreadAttached(ProcessThread* process_thread) override;
sprang89c4a7e2017-06-30 13:27:40 -0700152 void SetPacingFactor(float pacing_factor);
153 void SetQueueTimeLimit(int limit_ms);
154
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000155 private:
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000156 // Updates the number of bytes that can be sent for the next time interval.
isheriff31687812016-10-04 08:43:09 -0700157 void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms)
danilchap56359be2017-09-07 07:53:45 -0700158 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
isheriff31687812016-10-04 08:43:09 -0700159 void UpdateBudgetWithBytesSent(size_t bytes)
danilchap56359be2017-09-07 07:53:45 -0700160 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000161
philipelc7bf32a2017-02-17 03:59:43 -0800162 bool SendPacket(const paced_sender::Packet& packet,
163 const PacedPacketInfo& cluster_info)
danilchap56359be2017-09-07 07:53:45 -0700164 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
philipelc7bf32a2017-02-17 03:59:43 -0800165 size_t SendPadding(size_t padding_needed, const PacedPacketInfo& cluster_info)
danilchap56359be2017-09-07 07:53:45 -0700166 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000167
elad.alon61ce37e2017-03-09 07:09:31 -0800168 const Clock* const clock_;
perkjec81bcd2016-05-11 06:01:13 -0700169 PacketSender* const packet_sender_;
danilchap56359be2017-09-07 07:53:45 -0700170 std::unique_ptr<AlrDetector> alr_detector_ RTC_GUARDED_BY(critsect_);
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000171
kthelgason6bfe49c2017-03-30 01:14:41 -0700172 rtc::CriticalSection critsect_;
danilchap56359be2017-09-07 07:53:45 -0700173 bool paused_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000174 // This is the media budget, keeping track of how many bits of media
175 // we can pace out during the current interval.
danilchap56359be2017-09-07 07:53:45 -0700176 std::unique_ptr<IntervalBudget> media_budget_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000177 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000178 // allowed to send out during the current interval. This budget will be
179 // utilized when there's no media to send.
danilchap56359be2017-09-07 07:53:45 -0700180 std::unique_ptr<IntervalBudget> padding_budget_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000181
danilchap56359be2017-09-07 07:53:45 -0700182 std::unique_ptr<BitrateProber> prober_ RTC_GUARDED_BY(critsect_);
philipelb61927c2017-02-28 07:05:23 -0800183 bool probing_send_failure_;
sprang0a43fef2015-11-20 09:00:37 -0800184 // Actual configured bitrates (media_budget_ may temporarily be higher in
185 // order to meet pace time constraint).
danilchap56359be2017-09-07 07:53:45 -0700186 uint32_t estimated_bitrate_bps_ RTC_GUARDED_BY(critsect_);
187 uint32_t min_send_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
188 uint32_t max_padding_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
189 uint32_t pacing_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000190
danilchap56359be2017-09-07 07:53:45 -0700191 int64_t time_last_update_us_ RTC_GUARDED_BY(critsect_);
192 int64_t first_sent_packet_ms_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000193
danilchap56359be2017-09-07 07:53:45 -0700194 std::unique_ptr<paced_sender::PacketQueue> packets_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000195 uint64_t packet_counter_;
tommi919dce22017-03-15 07:45:36 -0700196 ProcessThread* process_thread_ = nullptr;
sprang89c4a7e2017-06-30 13:27:40 -0700197
danilchap56359be2017-09-07 07:53:45 -0700198 float pacing_factor_ RTC_GUARDED_BY(critsect_);
199 int64_t queue_time_limit RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000200};
201} // namespace webrtc
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +0100202#endif // WEBRTC_MODULES_PACING_PACED_SENDER_H_