blob: c1090ecbc02a746cffeb259d3d98118092aeee15 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/optional.h"
17#include "modules/pacing/pacer.h"
philipelccdfcca2017-10-23 12:42:17 +020018#include "modules/pacing/packet_queue2.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/criticalsection.h"
20#include "rtc_base/thread_annotations.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "typedefs.h" // NOLINT(build/include)
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000022
23namespace webrtc {
isheriff31687812016-10-04 08:43:09 -070024class AlrDetector;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000025class BitrateProber;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000026class Clock;
philipelfd58b612017-01-04 07:05:25 -080027class ProbeClusterCreatedObserver;
philipelc3b3f7a2017-03-29 01:23:13 -070028class RtcEventLog;
tschumim82c55932017-07-11 06:56:04 -070029class IntervalBudget;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000030
gnisha36165c2017-08-20 09:19:58 -070031class PacedSender : public Pacer {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000032 public:
perkjec81bcd2016-05-11 06:01:13 -070033 class PacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000034 public:
35 // Note: packets sent as a result of a callback should not pass by this
36 // module again.
37 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000038 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000039 virtual bool TimeToSendPacket(uint32_t ssrc,
40 uint16_t sequence_number,
41 int64_t capture_time_ms,
philipel29dca2c2016-05-13 11:13:05 +020042 bool retransmission,
philipelc7bf32a2017-02-17 03:59:43 -080043 const PacedPacketInfo& cluster_info) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000044 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000045 // Returns the number of bytes sent.
philipelc7bf32a2017-02-17 03:59:43 -080046 virtual size_t TimeToSendPadding(size_t bytes,
47 const PacedPacketInfo& cluster_info) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000048
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000049 protected:
perkjec81bcd2016-05-11 06:01:13 -070050 virtual ~PacketSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000051 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000052
sprang0a43fef2015-11-20 09:00:37 -080053 // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
54 // this value, the packet producers should wait (eg drop frames rather than
55 // encoding them). Bitrate sent may temporarily exceed target set by
56 // UpdateBitrate() so that this limit will be upheld.
57 static const int64_t kMaxQueueLengthMs;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000058 // Pacing-rate relative to our target send rate.
59 // Multiplicative factor that is applied to the target bitrate to calculate
60 // the number of bytes that can be transmitted per interval.
61 // Increasing this factor will result in lower delays in cases of bitrate
62 // overshoots from the encoder.
63 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000064
philipelc3b3f7a2017-03-29 01:23:13 -070065 PacedSender(const Clock* clock,
66 PacketSender* packet_sender,
67 RtcEventLog* event_log);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000068
Niels Möller9d4af012017-10-31 09:54:50 +010069 PacedSender(const Clock* clock,
70 PacketSender* packet_sender,
71 RtcEventLog* event_log,
72 std::unique_ptr<PacketQueue> packets);
73
nisse76e62b02017-05-31 02:24:52 -070074 ~PacedSender() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000075
philipelfd58b612017-01-04 07:05:25 -080076 virtual void CreateProbeCluster(int bitrate_bps);
philipeleb680ea2016-08-17 11:11:59 +020077
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000078 // Temporarily pause all sending.
79 void Pause();
80
81 // Resume sending packets.
82 void Resume();
83
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000084 // Enable bitrate probing. Enabled by default, mostly here to simplify
85 // testing. Must be called before any packets are being sent to have an
86 // effect.
87 void SetProbingEnabled(bool enabled);
88
perkjec81bcd2016-05-11 06:01:13 -070089 // Sets the estimated capacity of the network. Must be called once before
90 // packets can be sent.
91 // |bitrate_bps| is our estimate of what we are allowed to send on average.
92 // We will pace out bursts of packets at a bitrate of
93 // |bitrate_bps| * kDefaultPaceMultiplier.
gnisha36165c2017-08-20 09:19:58 -070094 void SetEstimatedBitrate(uint32_t bitrate_bps) override;
perkjec81bcd2016-05-11 06:01:13 -070095
perkj71ee44c2016-06-15 00:47:53 -070096 // Sets the minimum send bitrate and maximum padding bitrate requested by send
97 // streams.
98 // |min_send_bitrate_bps| might be higher that the estimated available network
99 // bitrate and if so, the pacer will send with |min_send_bitrate_bps|.
100 // |max_padding_bitrate_bps| might be higher than the estimate available
101 // network bitrate and if so, the pacer will send padding packets to reach
102 // the min of the estimated available bitrate and |max_padding_bitrate_bps|.
103 void SetSendBitrateLimits(int min_send_bitrate_bps,
104 int max_padding_bitrate_bps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000105
106 // Returns true if we send the packet now, else it will add the packet
107 // information to the queue and call TimeToSendPacket when it's time to send.
Peter Boströme23e7372015-10-08 11:44:14 +0200108 void InsertPacket(RtpPacketSender::Priority priority,
109 uint32_t ssrc,
110 uint16_t sequence_number,
111 int64_t capture_time_ms,
112 size_t bytes,
113 bool retransmission) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000114
Alex Narest78609d52017-10-20 10:37:47 +0200115 // Currently audio traffic is not accounted by pacer and passed through.
116 // With the introduction of audio BWE audio traffic will be accounted for
117 // the pacer budget calculation. The audio traffic still will be injected
118 // at high priority.
119 void SetAccountForAudioPackets(bool account_for_audio) override;
120
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000121 // Returns the time since the oldest queued packet was enqueued.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000122 virtual int64_t QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000123
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000124 virtual size_t QueueSizePackets() const;
125
asaperssonfc5e81c2017-04-19 23:28:53 -0700126 // Returns the time when the first packet was sent, or -1 if no packet is
127 // sent.
128 virtual int64_t FirstSentPacketTimeMs() const;
129
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000130 // Returns the number of milliseconds it will take to send the current
131 // packets in the queue, given the current size and bitrate, ignoring prio.
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000132 virtual int64_t ExpectedQueueTimeMs() const;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000133
sergeyu80ed35e2016-11-28 13:11:13 -0800134 // Returns time in milliseconds when the current application-limited region
135 // started or empty result if the sender is currently not application-limited.
136 //
137 // Application Limited Region (ALR) refers to operating in a state where the
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700138 // traffic on network is limited due to application not having enough
139 // traffic to meet the current channel capacity.
sergeyu80ed35e2016-11-28 13:11:13 -0800140 virtual rtc::Optional<int64_t> GetApplicationLimitedRegionStartTime() const;
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700141
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000142 // Returns the number of milliseconds until the module want a worker thread
143 // to call Process.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000144 int64_t TimeUntilNextProcess() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000145
146 // Process any pending packets in the queue(s).
pbosa26ac922016-02-25 04:50:01 -0800147 void Process() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000148
tommi919dce22017-03-15 07:45:36 -0700149 // Called when the prober is associated with a process thread.
150 void ProcessThreadAttached(ProcessThread* process_thread) override;
sprang89c4a7e2017-06-30 13:27:40 -0700151 void SetPacingFactor(float pacing_factor);
Erik Språng7c8cca32017-10-24 17:05:18 +0200152 float GetPacingFactor() const;
sprang89c4a7e2017-06-30 13:27:40 -0700153 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
philipel9981bd92017-09-26 17:16:06 +0200162 bool SendPacket(const PacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800163 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_;
Niels Möller712048c2017-10-18 13:08:22 +0200170 const std::unique_ptr<AlrDetector> alr_detector_ RTC_PT_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.
Niels Möller712048c2017-10-18 13:08:22 +0200176 const std::unique_ptr<IntervalBudget> media_budget_
177 RTC_PT_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000178 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000179 // allowed to send out during the current interval. This budget will be
180 // utilized when there's no media to send.
Niels Möller712048c2017-10-18 13:08:22 +0200181 const std::unique_ptr<IntervalBudget> padding_budget_
182 RTC_PT_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000183
Niels Möller712048c2017-10-18 13:08:22 +0200184 const std::unique_ptr<BitrateProber> prober_ RTC_PT_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200185 bool probing_send_failure_ RTC_GUARDED_BY(critsect_);
sprang0a43fef2015-11-20 09:00:37 -0800186 // Actual configured bitrates (media_budget_ may temporarily be higher in
187 // order to meet pace time constraint).
danilchap56359be2017-09-07 07:53:45 -0700188 uint32_t estimated_bitrate_bps_ RTC_GUARDED_BY(critsect_);
189 uint32_t min_send_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
190 uint32_t max_padding_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
191 uint32_t pacing_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000192
danilchap56359be2017-09-07 07:53:45 -0700193 int64_t time_last_update_us_ RTC_GUARDED_BY(critsect_);
194 int64_t first_sent_packet_ms_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000195
Niels Möller712048c2017-10-18 13:08:22 +0200196 const std::unique_ptr<PacketQueue> packets_ RTC_PT_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200197 uint64_t packet_counter_ RTC_GUARDED_BY(critsect_);
tommi919dce22017-03-15 07:45:36 -0700198 ProcessThread* process_thread_ = nullptr;
sprang89c4a7e2017-06-30 13:27:40 -0700199
danilchap56359be2017-09-07 07:53:45 -0700200 float pacing_factor_ RTC_GUARDED_BY(critsect_);
201 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_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000203};
204} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200205#endif // MODULES_PACING_PACED_SENDER_H_