blob: bd4880de473f1b602fa9c549d263f8d4027c8580 [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.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000065 virtual bool SendPacket(Priority priority,
66 uint32_t ssrc,
67 uint16_t sequence_number,
68 int64_t capture_time_ms,
69 int bytes);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000070
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000071 // Returns the time since the oldest queued packet was captured.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000072 virtual int QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000073
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000074 // Returns the number of milliseconds until the module want a worker thread
75 // to call Process.
76 virtual int32_t TimeUntilNextProcess();
77
78 // Process any pending packets in the queue(s).
79 virtual int32_t Process();
80
81 private:
82 struct Packet {
83 Packet(uint32_t ssrc, uint16_t seq_number, int64_t capture_time_ms,
84 int length_in_bytes)
85 : ssrc_(ssrc),
86 sequence_number_(seq_number),
87 capture_time_ms_(capture_time_ms),
88 bytes_(length_in_bytes) {
89 }
90 uint32_t ssrc_;
91 uint16_t sequence_number_;
92 int64_t capture_time_ms_;
93 int bytes_;
94 };
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000095
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000096 // STL list style class which prevents duplicates in the list.
97 class PacketList {
98 public:
99 PacketList() {};
100
101 bool empty() const;
102 Packet front() const;
103 void pop_front();
104 void push_back(const Packet& packet);
105
106 private:
107 std::list<Packet> packet_list_;
108 std::set<uint16_t> sequence_number_set_;
109 };
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000110
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000111 // Checks if next packet in line can be transmitted. Returns true on success.
112 bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number,
113 int64_t* capture_time_ms);
114
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000115 // Local helper function to GetNextPacket.
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +0000116 void GetNextPacketFromList(PacketList* list,
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000117 uint32_t* ssrc, uint16_t* sequence_number, int64_t* capture_time_ms);
118
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000119 // Updates the number of bytes that can be sent for the next time interval.
120 void UpdateBytesPerInterval(uint32_t delta_time_in_ms);
121
122 // Updates the buffers with the number of bytes that we sent.
123 void UpdateState(int num_bytes);
124
125 Callback* callback_;
126 bool enable_;
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000127 bool paused_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000128 scoped_ptr<CriticalSectionWrapper> critsect_;
129 int target_bitrate_kbytes_per_s_;
130 int bytes_remaining_interval_;
131 int padding_bytes_remaining_interval_;
132 TickTime time_last_update_;
133 TickTime time_last_send_;
134
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000135 PacketList high_priority_packets_;
136 PacketList normal_priority_packets_;
137 PacketList low_priority_packets_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000138};
139} // namespace webrtc
140#endif // WEBRTC_MODULES_PACED_SENDER_H_