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