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 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_PACING_INCLUDE_PACED_SENDER_H_ |
| 12 | #define WEBRTC_MODULES_PACING_INCLUDE_PACED_SENDER_H_ |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
pwestin@webrtc.org | 52aa019 | 2013-04-25 17:35:56 +0000 | [diff] [blame] | 15 | #include <set> |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 16 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 17 | #include "webrtc/base/scoped_ptr.h" |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 18 | #include "webrtc/base/thread_annotations.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/interface/module.h" |
sprang | ebbf8a8 | 2015-09-21 15:11:14 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 21 | #include "webrtc/typedefs.h" |
| 22 | |
| 23 | namespace webrtc { |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 24 | class BitrateProber; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 25 | class Clock; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 26 | class CriticalSectionWrapper; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 27 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 28 | namespace paced_sender { |
| 29 | class IntervalBudget; |
| 30 | struct Packet; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 31 | class PacketQueue; |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 32 | } // namespace paced_sender |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 33 | |
sprang | ebbf8a8 | 2015-09-21 15:11:14 -0700 | [diff] [blame] | 34 | class PacedSender : public Module, public RtpPacketSender { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 35 | public: |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 36 | class Callback { |
| 37 | public: |
| 38 | // Note: packets sent as a result of a callback should not pass by this |
| 39 | // module again. |
| 40 | // Called when it's time to send a queued packet. |
hclam@chromium.org | 2e402ce | 2013-06-20 20:18:31 +0000 | [diff] [blame] | 41 | // Returns false if packet cannot be sent. |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 42 | virtual bool TimeToSendPacket(uint32_t ssrc, |
| 43 | uint16_t sequence_number, |
| 44 | int64_t capture_time_ms, |
| 45 | bool retransmission) = 0; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 46 | // 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] | 47 | // Returns the number of bytes sent. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 48 | virtual size_t TimeToSendPadding(size_t bytes) = 0; |
jiayl@webrtc.org | 9fd8d87 | 2014-02-27 22:32:40 +0000 | [diff] [blame] | 49 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 50 | protected: |
| 51 | virtual ~Callback() {} |
| 52 | }; |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 53 | |
pkasting@chromium.org | 2656bf8 | 2014-11-17 22:21:14 +0000 | [diff] [blame] | 54 | static const int64_t kDefaultMaxQueueLengthMs = 2000; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 55 | // Pace in kbits/s until we receive first estimate. |
| 56 | static const int kDefaultInitialPaceKbps = 2000; |
| 57 | // 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.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 63 | |
sprang | ebbf8a8 | 2015-09-21 15:11:14 -0700 | [diff] [blame] | 64 | static const size_t kMinProbePacketSize = 200; |
| 65 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 66 | PacedSender(Clock* clock, |
| 67 | Callback* callback, |
| 68 | int bitrate_kbps, |
| 69 | int max_bitrate_kbps, |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 70 | int min_bitrate_kbps); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 71 | |
| 72 | virtual ~PacedSender(); |
| 73 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 74 | // Temporarily pause all sending. |
| 75 | void Pause(); |
| 76 | |
| 77 | // Resume sending packets. |
| 78 | void Resume(); |
| 79 | |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 80 | // Enable bitrate probing. Enabled by default, mostly here to simplify |
| 81 | // testing. Must be called before any packets are being sent to have an |
| 82 | // effect. |
| 83 | void SetProbingEnabled(bool enabled); |
| 84 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 85 | // Set target bitrates for the pacer. |
| 86 | // We will pace out bursts of packets at a bitrate of |max_bitrate_kbps|. |
| 87 | // |bitrate_kbps| is our estimate of what we are allowed to send on average. |
| 88 | // Padding packets will be utilized to reach |min_bitrate| unless enough media |
| 89 | // packets are available. |
| 90 | void UpdateBitrate(int bitrate_kbps, |
| 91 | int max_bitrate_kbps, |
| 92 | int min_bitrate_kbps); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 93 | |
| 94 | // Returns true if we send the packet now, else it will add the packet |
| 95 | // 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^] | 96 | void InsertPacket(RtpPacketSender::Priority priority, |
| 97 | uint32_t ssrc, |
| 98 | uint16_t sequence_number, |
| 99 | int64_t capture_time_ms, |
| 100 | size_t bytes, |
| 101 | bool retransmission) override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 102 | |
stefan@webrtc.org | dd393e7 | 2013-12-13 22:03:27 +0000 | [diff] [blame] | 103 | // Returns the time since the oldest queued packet was enqueued. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 104 | virtual int64_t QueueInMs() const; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 105 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 106 | virtual size_t QueueSizePackets() const; |
| 107 | |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 108 | // Returns the number of milliseconds it will take to send the current |
| 109 | // 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] | 110 | virtual int64_t ExpectedQueueTimeMs() const; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 111 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 112 | // Returns the number of milliseconds until the module want a worker thread |
| 113 | // to call Process. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 114 | int64_t TimeUntilNextProcess() override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 115 | |
| 116 | // Process any pending packets in the queue(s). |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 117 | int32_t Process() override; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 118 | |
| 119 | private: |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 120 | // Updates the number of bytes that can be sent for the next time interval. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 121 | void UpdateBytesPerInterval(int64_t delta_time_in_ms) |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 122 | EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 123 | |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 124 | bool SendPacket(const paced_sender::Packet& packet) |
| 125 | EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 126 | void SendPadding(size_t padding_needed) EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 127 | |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 128 | Clock* const clock_; |
| 129 | Callback* const callback_; |
| 130 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 131 | rtc::scoped_ptr<CriticalSectionWrapper> critsect_; |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 132 | bool paused_ GUARDED_BY(critsect_); |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 133 | bool probing_enabled_; |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 134 | // This is the media budget, keeping track of how many bits of media |
| 135 | // we can pace out during the current interval. |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 136 | rtc::scoped_ptr<paced_sender::IntervalBudget> media_budget_ |
| 137 | GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 138 | // 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] | 139 | // allowed to send out during the current interval. This budget will be |
| 140 | // utilized when there's no media to send. |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 141 | rtc::scoped_ptr<paced_sender::IntervalBudget> padding_budget_ |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 142 | GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 143 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 144 | rtc::scoped_ptr<BitrateProber> prober_ GUARDED_BY(critsect_); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 145 | int bitrate_bps_ GUARDED_BY(critsect_); |
| 146 | |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 147 | int64_t time_last_update_us_ GUARDED_BY(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 148 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 149 | rtc::scoped_ptr<paced_sender::PacketQueue> packets_ GUARDED_BY(critsect_); |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 150 | uint64_t packet_counter_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 151 | }; |
| 152 | } // namespace webrtc |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 153 | #endif // WEBRTC_MODULES_PACING_INCLUDE_PACED_SENDER_H_ |