pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_PACING_PACED_SENDER_H_ |
| 12 | #define MODULES_PACING_PACED_SENDER_H_ |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 13 | |
kwiberg | 22feaa3 | 2016-03-17 09:17:43 -0700 | [diff] [blame] | 14 | #include <memory> |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/optional.h" |
| 17 | #include "modules/pacing/pacer.h" |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 18 | #include "modules/pacing/packet_queue.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/criticalsection.h" |
| 20 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 21 | #include "typedefs.h" // NOLINT(build/include) |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 24 | class AlrDetector; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 25 | class BitrateProber; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 26 | class Clock; |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 27 | class ProbeClusterCreatedObserver; |
philipel | c3b3f7a | 2017-03-29 01:23:13 -0700 | [diff] [blame] | 28 | class RtcEventLog; |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 29 | class IntervalBudget; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 30 | |
gnish | a36165c | 2017-08-20 09:19:58 -0700 | [diff] [blame] | 31 | class PacedSender : public Pacer { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 32 | public: |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 33 | class PacketSender { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 34 | 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.org | 2e402ce | 2013-06-20 20:18:31 +0000 | [diff] [blame] | 38 | // Returns false if packet cannot be sent. |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 39 | virtual bool TimeToSendPacket(uint32_t ssrc, |
| 40 | uint16_t sequence_number, |
| 41 | int64_t capture_time_ms, |
philipel | 29dca2c | 2016-05-13 11:13:05 +0200 | [diff] [blame] | 42 | bool retransmission, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 43 | const PacedPacketInfo& cluster_info) = 0; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 44 | // Called when it's a good time to send a padding data. |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 45 | // Returns the number of bytes sent. |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 46 | virtual size_t TimeToSendPadding(size_t bytes, |
| 47 | const PacedPacketInfo& cluster_info) = 0; |
jiayl@webrtc.org | 9fd8d87 | 2014-02-27 22:32:40 +0000 | [diff] [blame] | 48 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 49 | protected: |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 50 | virtual ~PacketSender() {} |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 51 | }; |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 52 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 53 | // 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.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 58 | // 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.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 64 | |
philipel | c3b3f7a | 2017-03-29 01:23:13 -0700 | [diff] [blame] | 65 | PacedSender(const Clock* clock, |
| 66 | PacketSender* packet_sender, |
| 67 | RtcEventLog* event_log); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 68 | |
nisse | 76e62b0 | 2017-05-31 02:24:52 -0700 | [diff] [blame] | 69 | ~PacedSender() override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 70 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 71 | virtual void CreateProbeCluster(int bitrate_bps); |
philipel | eb680ea | 2016-08-17 11:11:59 +0200 | [diff] [blame] | 72 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 73 | // Temporarily pause all sending. |
| 74 | void Pause(); |
| 75 | |
| 76 | // Resume sending packets. |
| 77 | void Resume(); |
| 78 | |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 79 | // Enable bitrate probing. Enabled by default, mostly here to simplify |
| 80 | // testing. Must be called before any packets are being sent to have an |
| 81 | // effect. |
| 82 | void SetProbingEnabled(bool enabled); |
| 83 | |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 84 | // Sets the estimated capacity of the network. Must be called once before |
| 85 | // packets can be sent. |
| 86 | // |bitrate_bps| is our estimate of what we are allowed to send on average. |
| 87 | // We will pace out bursts of packets at a bitrate of |
| 88 | // |bitrate_bps| * kDefaultPaceMultiplier. |
gnish | a36165c | 2017-08-20 09:19:58 -0700 | [diff] [blame] | 89 | void SetEstimatedBitrate(uint32_t bitrate_bps) override; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 90 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 91 | // Sets the minimum send bitrate and maximum padding bitrate requested by send |
| 92 | // streams. |
| 93 | // |min_send_bitrate_bps| might be higher that the estimated available network |
| 94 | // bitrate and if so, the pacer will send with |min_send_bitrate_bps|. |
| 95 | // |max_padding_bitrate_bps| might be higher than the estimate available |
| 96 | // network bitrate and if so, the pacer will send padding packets to reach |
| 97 | // the min of the estimated available bitrate and |max_padding_bitrate_bps|. |
| 98 | void SetSendBitrateLimits(int min_send_bitrate_bps, |
| 99 | int max_padding_bitrate_bps); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 100 | |
| 101 | // Returns true if we send the packet now, else it will add the packet |
| 102 | // information to the queue and call TimeToSendPacket when it's time to send. |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 103 | void InsertPacket(RtpPacketSender::Priority priority, |
| 104 | uint32_t ssrc, |
| 105 | uint16_t sequence_number, |
| 106 | int64_t capture_time_ms, |
| 107 | size_t bytes, |
| 108 | bool retransmission) override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 109 | |
stefan@webrtc.org | dd393e7 | 2013-12-13 22:03:27 +0000 | [diff] [blame] | 110 | // Returns the time since the oldest queued packet was enqueued. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 111 | virtual int64_t QueueInMs() const; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 112 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 113 | virtual size_t QueueSizePackets() const; |
| 114 | |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 115 | // Returns the time when the first packet was sent, or -1 if no packet is |
| 116 | // sent. |
| 117 | virtual int64_t FirstSentPacketTimeMs() const; |
| 118 | |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 119 | // Returns the number of milliseconds it will take to send the current |
| 120 | // packets in the queue, given the current size and bitrate, ignoring prio. |
pkasting@chromium.org | 2656bf8 | 2014-11-17 22:21:14 +0000 | [diff] [blame] | 121 | virtual int64_t ExpectedQueueTimeMs() const; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 122 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 123 | // Returns time in milliseconds when the current application-limited region |
| 124 | // started or empty result if the sender is currently not application-limited. |
| 125 | // |
| 126 | // Application Limited Region (ALR) refers to operating in a state where the |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 127 | // traffic on network is limited due to application not having enough |
| 128 | // traffic to meet the current channel capacity. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 129 | virtual rtc::Optional<int64_t> GetApplicationLimitedRegionStartTime() const; |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 130 | |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 131 | // Returns the average time since being enqueued, in milliseconds, for all |
sprang | ddcfb9f | 2017-08-16 05:38:49 -0700 | [diff] [blame] | 132 | // packets currently in the pacer queue, excluding any time the pacer has been |
| 133 | // paused. Returns 0 if queue is empty. |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 134 | virtual int64_t AverageQueueTimeMs(); |
| 135 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 136 | // Returns the number of milliseconds until the module want a worker thread |
| 137 | // to call Process. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 138 | int64_t TimeUntilNextProcess() override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 139 | |
| 140 | // Process any pending packets in the queue(s). |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 141 | void Process() override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 142 | |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 143 | // Called when the prober is associated with a process thread. |
| 144 | void ProcessThreadAttached(ProcessThread* process_thread) override; |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 145 | void SetPacingFactor(float pacing_factor); |
| 146 | void SetQueueTimeLimit(int limit_ms); |
| 147 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 148 | private: |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 149 | // Updates the number of bytes that can be sent for the next time interval. |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 150 | void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 151 | RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 152 | void UpdateBudgetWithBytesSent(size_t bytes) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 153 | RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 154 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 155 | bool SendPacket(const PacketQueue::Packet& packet, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 156 | const PacedPacketInfo& cluster_info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 157 | RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 158 | size_t SendPadding(size_t padding_needed, const PacedPacketInfo& cluster_info) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 159 | RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 160 | |
elad.alon | 61ce37e | 2017-03-09 07:09:31 -0800 | [diff] [blame] | 161 | const Clock* const clock_; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 162 | PacketSender* const packet_sender_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 163 | std::unique_ptr<AlrDetector> alr_detector_ RTC_GUARDED_BY(critsect_); |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 164 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 165 | rtc::CriticalSection critsect_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 166 | bool paused_ RTC_GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 167 | // This is the media budget, keeping track of how many bits of media |
| 168 | // we can pace out during the current interval. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 169 | std::unique_ptr<IntervalBudget> media_budget_ RTC_GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 170 | // This is the padding budget, keeping track of how many bits of padding we're |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 171 | // allowed to send out during the current interval. This budget will be |
| 172 | // utilized when there's no media to send. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 173 | std::unique_ptr<IntervalBudget> padding_budget_ RTC_GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 174 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 175 | std::unique_ptr<BitrateProber> prober_ RTC_GUARDED_BY(critsect_); |
Elad Alon | 44b1fa4 | 2017-10-17 14:17:54 +0200 | [diff] [blame] | 176 | bool probing_send_failure_ RTC_GUARDED_BY(critsect_); |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 177 | // Actual configured bitrates (media_budget_ may temporarily be higher in |
| 178 | // order to meet pace time constraint). |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 179 | uint32_t estimated_bitrate_bps_ RTC_GUARDED_BY(critsect_); |
| 180 | uint32_t min_send_bitrate_kbps_ RTC_GUARDED_BY(critsect_); |
| 181 | uint32_t max_padding_bitrate_kbps_ RTC_GUARDED_BY(critsect_); |
| 182 | uint32_t pacing_bitrate_kbps_ RTC_GUARDED_BY(critsect_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 183 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 184 | int64_t time_last_update_us_ RTC_GUARDED_BY(critsect_); |
| 185 | int64_t first_sent_packet_ms_ RTC_GUARDED_BY(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 186 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 187 | std::unique_ptr<PacketQueue> packets_ RTC_GUARDED_BY(critsect_); |
Elad Alon | 44b1fa4 | 2017-10-17 14:17:54 +0200 | [diff] [blame] | 188 | uint64_t packet_counter_ RTC_GUARDED_BY(critsect_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 189 | ProcessThread* process_thread_ = nullptr; |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 190 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 191 | float pacing_factor_ RTC_GUARDED_BY(critsect_); |
| 192 | int64_t queue_time_limit RTC_GUARDED_BY(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 193 | }; |
| 194 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 195 | #endif // MODULES_PACING_PACED_SENDER_H_ |