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> |
| 15 | |
| 16 | #include "webrtc/modules/interface/module.h" |
| 17 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 18 | #include "webrtc/system_wrappers/interface/tick_util.h" |
| 19 | #include "webrtc/typedefs.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | class CriticalSectionWrapper; |
| 23 | |
| 24 | class PacedSender : public Module { |
| 25 | public: |
| 26 | enum Priority { |
| 27 | kHighPriority = 0, // Pass through; will be sent immediately. |
| 28 | kNormalPriority = 2, // Put in back of the line. |
| 29 | kLowPriority = 3, // Put in back of the low priority line. |
| 30 | }; |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame^] | 31 | // Low priority packets are mixed with the normal priority packets |
| 32 | // while we are paused. |
| 33 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 34 | class Callback { |
| 35 | public: |
| 36 | // Note: packets sent as a result of a callback should not pass by this |
| 37 | // module again. |
| 38 | // Called when it's time to send a queued packet. |
| 39 | virtual void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number, |
| 40 | int64_t capture_time_ms) = 0; |
| 41 | // Called when it's a good time to send a padding data. |
| 42 | virtual void TimeToSendPadding(int bytes) = 0; |
| 43 | protected: |
| 44 | virtual ~Callback() {} |
| 45 | }; |
| 46 | PacedSender(Callback* callback, int target_bitrate_kbps); |
| 47 | |
| 48 | virtual ~PacedSender(); |
| 49 | |
| 50 | // Enable/disable pacing. |
| 51 | void SetStatus(bool enable); |
| 52 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame^] | 53 | // Temporarily pause all sending. |
| 54 | void Pause(); |
| 55 | |
| 56 | // Resume sending packets. |
| 57 | void Resume(); |
| 58 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 59 | // Current total estimated bitrate. |
| 60 | void UpdateBitrate(int target_bitrate_kbps); |
| 61 | |
| 62 | // Returns true if we send the packet now, else it will add the packet |
| 63 | // information to the queue and call TimeToSendPacket when it's time to send. |
| 64 | bool SendPacket(Priority priority, uint32_t ssrc, uint16_t sequence_number, |
| 65 | int64_t capture_time_ms, int bytes); |
| 66 | |
| 67 | // Returns the number of milliseconds until the module want a worker thread |
| 68 | // to call Process. |
| 69 | virtual int32_t TimeUntilNextProcess(); |
| 70 | |
| 71 | // Process any pending packets in the queue(s). |
| 72 | virtual int32_t Process(); |
| 73 | |
| 74 | private: |
| 75 | struct Packet { |
| 76 | Packet(uint32_t ssrc, uint16_t seq_number, int64_t capture_time_ms, |
| 77 | int length_in_bytes) |
| 78 | : ssrc_(ssrc), |
| 79 | sequence_number_(seq_number), |
| 80 | capture_time_ms_(capture_time_ms), |
| 81 | bytes_(length_in_bytes) { |
| 82 | } |
| 83 | uint32_t ssrc_; |
| 84 | uint16_t sequence_number_; |
| 85 | int64_t capture_time_ms_; |
| 86 | int bytes_; |
| 87 | }; |
| 88 | // Checks if next packet in line can be transmitted. Returns true on success. |
| 89 | bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number, |
| 90 | int64_t* capture_time_ms); |
| 91 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame^] | 92 | // Local helper function to GetNextPacket. |
| 93 | void GetNextPacketFromList(std::list<Packet>* list, |
| 94 | uint32_t* ssrc, uint16_t* sequence_number, int64_t* capture_time_ms); |
| 95 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 96 | // Updates the number of bytes that can be sent for the next time interval. |
| 97 | void UpdateBytesPerInterval(uint32_t delta_time_in_ms); |
| 98 | |
| 99 | // Updates the buffers with the number of bytes that we sent. |
| 100 | void UpdateState(int num_bytes); |
| 101 | |
| 102 | Callback* callback_; |
| 103 | bool enable_; |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame^] | 104 | bool paused_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 105 | scoped_ptr<CriticalSectionWrapper> critsect_; |
| 106 | int target_bitrate_kbytes_per_s_; |
| 107 | int bytes_remaining_interval_; |
| 108 | int padding_bytes_remaining_interval_; |
| 109 | TickTime time_last_update_; |
| 110 | TickTime time_last_send_; |
| 111 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame^] | 112 | std::list<Packet> high_priority_packets_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 113 | std::list<Packet> normal_priority_packets_; |
| 114 | std::list<Packet> low_priority_packets_; |
| 115 | }; |
| 116 | } // namespace webrtc |
| 117 | #endif // WEBRTC_MODULES_PACED_SENDER_H_ |