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