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 | |
| 11 | #ifndef WEBRTC_MODULES_PACED_SENDER_H_ |
| 12 | #define WEBRTC_MODULES_PACED_SENDER_H_ |
| 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 | |
| 17 | #include "webrtc/modules/interface/module.h" |
| 18 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/interface/thread_annotations.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 20 | #include "webrtc/typedefs.h" |
| 21 | |
| 22 | namespace webrtc { |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 23 | class Clock; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 24 | class CriticalSectionWrapper; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 25 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 26 | namespace paced_sender { |
| 27 | class IntervalBudget; |
| 28 | struct Packet; |
| 29 | class PacketList; |
| 30 | } // namespace paced_sender |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 31 | |
| 32 | class PacedSender : public Module { |
| 33 | public: |
| 34 | enum Priority { |
| 35 | kHighPriority = 0, // Pass through; will be sent immediately. |
| 36 | kNormalPriority = 2, // Put in back of the line. |
| 37 | kLowPriority = 3, // Put in back of the low priority line. |
| 38 | }; |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 39 | // Low priority packets are mixed with the normal priority packets |
| 40 | // while we are paused. |
| 41 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 42 | class Callback { |
| 43 | public: |
| 44 | // Note: packets sent as a result of a callback should not pass by this |
| 45 | // module again. |
| 46 | // Called when it's time to send a queued packet. |
hclam@chromium.org | 2e402ce | 2013-06-20 20:18:31 +0000 | [diff] [blame] | 47 | // Returns false if packet cannot be sent. |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 48 | virtual bool TimeToSendPacket(uint32_t ssrc, |
| 49 | uint16_t sequence_number, |
| 50 | int64_t capture_time_ms, |
| 51 | bool retransmission) = 0; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 52 | // 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] | 53 | // Returns the number of bytes sent. |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 54 | virtual int TimeToSendPadding(int bytes) = 0; |
jiayl@webrtc.org | 9fd8d87 | 2014-02-27 22:32:40 +0000 | [diff] [blame] | 55 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 56 | protected: |
| 57 | virtual ~Callback() {} |
| 58 | }; |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 59 | |
| 60 | static const int kDefaultMaxQueueLengthMs = 2000; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 61 | // Pace in kbits/s until we receive first estimate. |
| 62 | static const int kDefaultInitialPaceKbps = 2000; |
| 63 | // Pacing-rate relative to our target send rate. |
| 64 | // Multiplicative factor that is applied to the target bitrate to calculate |
| 65 | // the number of bytes that can be transmitted per interval. |
| 66 | // Increasing this factor will result in lower delays in cases of bitrate |
| 67 | // overshoots from the encoder. |
| 68 | static const float kDefaultPaceMultiplier; |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 69 | |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 70 | PacedSender(Clock* clock, Callback* callback, int max_bitrate_kbps, |
| 71 | int min_bitrate_kbps); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 72 | |
| 73 | virtual ~PacedSender(); |
| 74 | |
| 75 | // Enable/disable pacing. |
| 76 | void SetStatus(bool enable); |
| 77 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 78 | bool Enabled() const; |
| 79 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 80 | // Temporarily pause all sending. |
| 81 | void Pause(); |
| 82 | |
| 83 | // Resume sending packets. |
| 84 | void Resume(); |
| 85 | |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 86 | // Set target bitrates for the pacer. Padding packets will be utilized to |
| 87 | // reach |min_bitrate| unless enough media packets are available. |
| 88 | void UpdateBitrate(int max_bitrate_kbps, int min_bitrate_kbps); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 89 | |
| 90 | // Returns true if we send the packet now, else it will add the packet |
| 91 | // information to the queue and call TimeToSendPacket when it's time to send. |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 92 | virtual bool SendPacket(Priority priority, |
| 93 | uint32_t ssrc, |
| 94 | uint16_t sequence_number, |
| 95 | int64_t capture_time_ms, |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 96 | int bytes, |
| 97 | bool retransmission); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 98 | |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 99 | // Sets the max length of the pacer queue in milliseconds. |
| 100 | // A negative queue size is interpreted as infinite. |
| 101 | virtual void set_max_queue_length_ms(int max_queue_length_ms); |
| 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. |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 104 | virtual int QueueInMs() const; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 105 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 106 | // Returns the number of milliseconds until the module want a worker thread |
| 107 | // to call Process. |
pbos@webrtc.org | 0193158 | 2013-07-31 15:18:19 +0000 | [diff] [blame] | 108 | virtual int32_t TimeUntilNextProcess() OVERRIDE; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 109 | |
| 110 | // Process any pending packets in the queue(s). |
pbos@webrtc.org | 0193158 | 2013-07-31 15:18:19 +0000 | [diff] [blame] | 111 | virtual int32_t Process() OVERRIDE; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
hclam@chromium.org | 2e402ce | 2013-06-20 20:18:31 +0000 | [diff] [blame] | 114 | // Return true if next packet in line should be transmitted. |
| 115 | // Return packet list that contains the next packet. |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 116 | bool ShouldSendNextPacket(paced_sender::PacketList** packet_list) |
| 117 | EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 118 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 119 | // Local helper function to GetNextPacket. |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 120 | paced_sender::Packet GetNextPacketFromList(paced_sender::PacketList* packets) |
| 121 | EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 122 | |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 123 | bool SendPacketFromList(paced_sender::PacketList* packet_list) |
| 124 | EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 125 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 126 | // Updates the number of bytes that can be sent for the next time interval. |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 127 | void UpdateBytesPerInterval(uint32_t delta_time_in_ms) |
| 128 | EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 129 | |
| 130 | // Updates the buffers with the number of bytes that we sent. |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 131 | void UpdateMediaBytesSent(int num_bytes) EXCLUSIVE_LOCKS_REQUIRED(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 132 | |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 133 | Clock* const clock_; |
| 134 | Callback* const callback_; |
| 135 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 136 | scoped_ptr<CriticalSectionWrapper> critsect_; |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 137 | bool enabled_ GUARDED_BY(critsect_); |
| 138 | bool paused_ GUARDED_BY(critsect_); |
| 139 | int max_queue_length_ms_ GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 140 | // This is the media budget, keeping track of how many bits of media |
| 141 | // we can pace out during the current interval. |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 142 | scoped_ptr<paced_sender::IntervalBudget> media_budget_ GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 143 | // 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] | 144 | // allowed to send out during the current interval. This budget will be |
| 145 | // utilized when there's no media to send. |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 146 | scoped_ptr<paced_sender::IntervalBudget> padding_budget_ |
| 147 | GUARDED_BY(critsect_); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 148 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 149 | int64_t time_last_update_ GUARDED_BY(critsect_); |
| 150 | int64_t time_last_send_ GUARDED_BY(critsect_); |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 151 | int64_t capture_time_ms_last_queued_ GUARDED_BY(critsect_); |
| 152 | int64_t capture_time_ms_last_sent_ GUARDED_BY(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 153 | |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 154 | scoped_ptr<paced_sender::PacketList> high_priority_packets_ |
| 155 | GUARDED_BY(critsect_); |
| 156 | scoped_ptr<paced_sender::PacketList> normal_priority_packets_ |
| 157 | GUARDED_BY(critsect_); |
| 158 | scoped_ptr<paced_sender::PacketList> low_priority_packets_ |
| 159 | GUARDED_BY(critsect_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 160 | }; |
| 161 | } // namespace webrtc |
| 162 | #endif // WEBRTC_MODULES_PACED_SENDER_H_ |