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. |
| 45 | virtual void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number, |
| 46 | int64_t capture_time_ms) = 0; |
| 47 | // 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^] | 48 | virtual int TimeToSendPadding(int bytes) = 0; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 49 | protected: |
| 50 | virtual ~Callback() {} |
| 51 | }; |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 52 | PacedSender(Callback* callback, int target_bitrate_kbps, |
| 53 | float pace_multiplier); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 54 | |
| 55 | virtual ~PacedSender(); |
| 56 | |
| 57 | // Enable/disable pacing. |
| 58 | void SetStatus(bool enable); |
| 59 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 60 | bool Enabled() const; |
| 61 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 62 | // Temporarily pause all sending. |
| 63 | void Pause(); |
| 64 | |
| 65 | // Resume sending packets. |
| 66 | void Resume(); |
| 67 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 68 | // Set the pacing target bitrate and the bitrate up to which we are allowed to |
| 69 | // pad. We will send padding packets to increase the total bitrate until we |
| 70 | // reach |pad_up_to_bitrate_kbps|. If the media bitrate is above |
| 71 | // |pad_up_to_bitrate_kbps| no padding will be sent. |
| 72 | void UpdateBitrate(int target_bitrate_kbps, int pad_up_to_bitrate_kbps); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 73 | |
| 74 | // Returns true if we send the packet now, else it will add the packet |
| 75 | // 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] | 76 | virtual bool SendPacket(Priority priority, |
| 77 | uint32_t ssrc, |
| 78 | uint16_t sequence_number, |
| 79 | int64_t capture_time_ms, |
| 80 | int bytes); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 81 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 82 | // Returns the time since the oldest queued packet was captured. |
pwestin@webrtc.org | b0061f9 | 2013-04-27 00:41:08 +0000 | [diff] [blame] | 83 | virtual int QueueInMs() const; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 84 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 85 | // Returns the number of milliseconds until the module want a worker thread |
| 86 | // to call Process. |
| 87 | virtual int32_t TimeUntilNextProcess(); |
| 88 | |
| 89 | // Process any pending packets in the queue(s). |
| 90 | virtual int32_t Process(); |
| 91 | |
| 92 | private: |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 93 | // Checks if next packet in line can be transmitted. Returns true on success. |
| 94 | bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number, |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 95 | int64_t* capture_time_ms, Priority* priority, |
| 96 | bool* last_packet); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 97 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 98 | // Local helper function to GetNextPacket. |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 99 | void GetNextPacketFromList(paced_sender::PacketList* packets, |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 100 | uint32_t* ssrc, uint16_t* sequence_number, int64_t* capture_time_ms, |
| 101 | bool* last_packet); |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 102 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 103 | // Updates the number of bytes that can be sent for the next time interval. |
| 104 | void UpdateBytesPerInterval(uint32_t delta_time_in_ms); |
| 105 | |
| 106 | // Updates the buffers with the number of bytes that we sent. |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 107 | void UpdateMediaBytesSent(int num_bytes); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 108 | |
| 109 | Callback* callback_; |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 110 | const float pace_multiplier_; |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 111 | bool enabled_; |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 112 | bool paused_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 113 | scoped_ptr<CriticalSectionWrapper> critsect_; |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 114 | // This is the media budget, keeping track of how many bits of media |
| 115 | // we can pace out during the current interval. |
| 116 | scoped_ptr<paced_sender::IntervalBudget> media_budget_; |
| 117 | // This is the padding budget, keeping track of how many bits of padding we're |
| 118 | // allowed to send out during the current interval. |
| 119 | scoped_ptr<paced_sender::IntervalBudget> padding_budget_; |
| 120 | // Media and padding share this budget, therefore no padding will be sent if |
| 121 | // media uses all of this budget. This is used to avoid padding above a given |
| 122 | // bitrate. |
| 123 | scoped_ptr<paced_sender::IntervalBudget> pad_up_to_bitrate_budget_; |
| 124 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 125 | TickTime time_last_update_; |
| 126 | TickTime time_last_send_; |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 127 | int64_t capture_time_ms_last_queued_; |
| 128 | int64_t capture_time_ms_last_sent_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 129 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame^] | 130 | scoped_ptr<paced_sender::PacketList> high_priority_packets_; |
| 131 | scoped_ptr<paced_sender::PacketList> normal_priority_packets_; |
| 132 | scoped_ptr<paced_sender::PacketList> low_priority_packets_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 133 | }; |
| 134 | } // namespace webrtc |
| 135 | #endif // WEBRTC_MODULES_PACED_SENDER_H_ |