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 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame^] | 67 | // Returns the time since the oldest queued packet was captured. |
| 68 | int QueueInMs() const; |
| 69 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 70 | // Returns the number of milliseconds until the module want a worker thread |
| 71 | // to call Process. |
| 72 | virtual int32_t TimeUntilNextProcess(); |
| 73 | |
| 74 | // Process any pending packets in the queue(s). |
| 75 | virtual int32_t Process(); |
| 76 | |
| 77 | private: |
| 78 | struct Packet { |
| 79 | Packet(uint32_t ssrc, uint16_t seq_number, int64_t capture_time_ms, |
| 80 | int length_in_bytes) |
| 81 | : ssrc_(ssrc), |
| 82 | sequence_number_(seq_number), |
| 83 | capture_time_ms_(capture_time_ms), |
| 84 | bytes_(length_in_bytes) { |
| 85 | } |
| 86 | uint32_t ssrc_; |
| 87 | uint16_t sequence_number_; |
| 88 | int64_t capture_time_ms_; |
| 89 | int bytes_; |
| 90 | }; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame^] | 91 | |
| 92 | typedef std::list<Packet> PacketList; |
| 93 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 94 | // Checks if next packet in line can be transmitted. Returns true on success. |
| 95 | bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number, |
| 96 | int64_t* capture_time_ms); |
| 97 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 98 | // Local helper function to GetNextPacket. |
| 99 | void GetNextPacketFromList(std::list<Packet>* list, |
| 100 | uint32_t* ssrc, uint16_t* sequence_number, int64_t* capture_time_ms); |
| 101 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 102 | // Updates the number of bytes that can be sent for the next time interval. |
| 103 | void UpdateBytesPerInterval(uint32_t delta_time_in_ms); |
| 104 | |
| 105 | // Updates the buffers with the number of bytes that we sent. |
| 106 | void UpdateState(int num_bytes); |
| 107 | |
| 108 | Callback* callback_; |
| 109 | bool enable_; |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 110 | bool paused_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 111 | scoped_ptr<CriticalSectionWrapper> critsect_; |
| 112 | int target_bitrate_kbytes_per_s_; |
| 113 | int bytes_remaining_interval_; |
| 114 | int padding_bytes_remaining_interval_; |
| 115 | TickTime time_last_update_; |
| 116 | TickTime time_last_send_; |
| 117 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame^] | 118 | PacketList high_priority_packets_; |
| 119 | PacketList normal_priority_packets_; |
| 120 | PacketList low_priority_packets_; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 121 | }; |
| 122 | } // namespace webrtc |
| 123 | #endif // WEBRTC_MODULES_PACED_SENDER_H_ |