blob: 4a0ade8012e581808e81870087bfdeb7710e1a0d [file] [log] [blame]
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +00001/*
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.org52aa0192013-04-25 17:35:56 +000015#include <set>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000016
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
22namespace webrtc {
23class CriticalSectionWrapper;
24
25class PacedSender : public Module {
26 public:
27 enum Priority {
28 kHighPriority = 0, // Pass through; will be sent immediately.
29 kNormalPriority = 2, // Put in back of the line.
30 kLowPriority = 3, // Put in back of the low priority line.
31 };
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000032 // Low priority packets are mixed with the normal priority packets
33 // while we are paused.
34
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000035 class Callback {
36 public:
37 // Note: packets sent as a result of a callback should not pass by this
38 // module again.
39 // Called when it's time to send a queued packet.
40 virtual void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
41 int64_t capture_time_ms) = 0;
42 // Called when it's a good time to send a padding data.
43 virtual void TimeToSendPadding(int bytes) = 0;
44 protected:
45 virtual ~Callback() {}
46 };
47 PacedSender(Callback* callback, int target_bitrate_kbps);
48
49 virtual ~PacedSender();
50
51 // Enable/disable pacing.
52 void SetStatus(bool enable);
53
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000054 // Temporarily pause all sending.
55 void Pause();
56
57 // Resume sending packets.
58 void Resume();
59
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000060 // Current total estimated bitrate.
61 void UpdateBitrate(int target_bitrate_kbps);
62
63 // Returns true if we send the packet now, else it will add the packet
64 // information to the queue and call TimeToSendPacket when it's time to send.
65 bool SendPacket(Priority priority, uint32_t ssrc, uint16_t sequence_number,
66 int64_t capture_time_ms, int bytes);
67
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000068 // Returns the time since the oldest queued packet was captured.
69 int QueueInMs() const;
70
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000071 // Returns the number of milliseconds until the module want a worker thread
72 // to call Process.
73 virtual int32_t TimeUntilNextProcess();
74
75 // Process any pending packets in the queue(s).
76 virtual int32_t Process();
77
78 private:
79 struct Packet {
80 Packet(uint32_t ssrc, uint16_t seq_number, int64_t capture_time_ms,
81 int length_in_bytes)
82 : ssrc_(ssrc),
83 sequence_number_(seq_number),
84 capture_time_ms_(capture_time_ms),
85 bytes_(length_in_bytes) {
86 }
87 uint32_t ssrc_;
88 uint16_t sequence_number_;
89 int64_t capture_time_ms_;
90 int bytes_;
91 };
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000092
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000093 // STL list style class which prevents duplicates in the list.
94 class PacketList {
95 public:
96 PacketList() {};
97
98 bool empty() const;
99 Packet front() const;
100 void pop_front();
101 void push_back(const Packet& packet);
102
103 private:
104 std::list<Packet> packet_list_;
105 std::set<uint16_t> sequence_number_set_;
106 };
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000107
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000108 // Checks if next packet in line can be transmitted. Returns true on success.
109 bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number,
110 int64_t* capture_time_ms);
111
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000112 // Local helper function to GetNextPacket.
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +0000113 void GetNextPacketFromList(PacketList* list,
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000114 uint32_t* ssrc, uint16_t* sequence_number, int64_t* capture_time_ms);
115
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000116 // Updates the number of bytes that can be sent for the next time interval.
117 void UpdateBytesPerInterval(uint32_t delta_time_in_ms);
118
119 // Updates the buffers with the number of bytes that we sent.
120 void UpdateState(int num_bytes);
121
122 Callback* callback_;
123 bool enable_;
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000124 bool paused_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000125 scoped_ptr<CriticalSectionWrapper> critsect_;
126 int target_bitrate_kbytes_per_s_;
127 int bytes_remaining_interval_;
128 int padding_bytes_remaining_interval_;
129 TickTime time_last_update_;
130 TickTime time_last_send_;
131
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000132 PacketList high_priority_packets_;
133 PacketList normal_priority_packets_;
134 PacketList low_priority_packets_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000135};
136} // namespace webrtc
137#endif // WEBRTC_MODULES_PACED_SENDER_H_