blob: 837af27b20254227f6ede561987a7fed75b431a7 [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 };
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +000047 PacedSender(Callback* callback, int target_bitrate_kbps,
48 float pace_multiplier);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000049
50 virtual ~PacedSender();
51
52 // Enable/disable pacing.
53 void SetStatus(bool enable);
54
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000055 // Temporarily pause all sending.
56 void Pause();
57
58 // Resume sending packets.
59 void Resume();
60
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000061 // Current total estimated bitrate.
62 void UpdateBitrate(int target_bitrate_kbps);
63
64 // Returns true if we send the packet now, else it will add the packet
65 // information to the queue and call TimeToSendPacket when it's time to send.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000066 virtual bool SendPacket(Priority priority,
67 uint32_t ssrc,
68 uint16_t sequence_number,
69 int64_t capture_time_ms,
70 int bytes);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000071
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000072 // Returns the time since the oldest queued packet was captured.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000073 virtual int QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000074
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000075 // Returns the number of milliseconds until the module want a worker thread
76 // to call Process.
77 virtual int32_t TimeUntilNextProcess();
78
79 // Process any pending packets in the queue(s).
80 virtual int32_t Process();
81
82 private:
83 struct Packet {
84 Packet(uint32_t ssrc, uint16_t seq_number, int64_t capture_time_ms,
85 int length_in_bytes)
86 : ssrc_(ssrc),
87 sequence_number_(seq_number),
88 capture_time_ms_(capture_time_ms),
89 bytes_(length_in_bytes) {
90 }
91 uint32_t ssrc_;
92 uint16_t sequence_number_;
93 int64_t capture_time_ms_;
94 int bytes_;
95 };
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000096
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000097 // STL list style class which prevents duplicates in the list.
98 class PacketList {
99 public:
100 PacketList() {};
101
102 bool empty() const;
103 Packet front() const;
104 void pop_front();
105 void push_back(const Packet& packet);
106
107 private:
108 std::list<Packet> packet_list_;
109 std::set<uint16_t> sequence_number_set_;
110 };
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000111
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000112 // Checks if next packet in line can be transmitted. Returns true on success.
113 bool GetNextPacket(uint32_t* ssrc, uint16_t* sequence_number,
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000114 int64_t* capture_time_ms, Priority* priority,
115 bool* last_packet);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000116
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000117 // Local helper function to GetNextPacket.
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +0000118 void GetNextPacketFromList(PacketList* list,
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000119 uint32_t* ssrc, uint16_t* sequence_number, int64_t* capture_time_ms,
120 bool* last_packet);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000121
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000122 // Updates the number of bytes that can be sent for the next time interval.
123 void UpdateBytesPerInterval(uint32_t delta_time_in_ms);
124
125 // Updates the buffers with the number of bytes that we sent.
126 void UpdateState(int num_bytes);
127
128 Callback* callback_;
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000129 const float pace_multiplier_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000130 bool enable_;
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000131 bool paused_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000132 scoped_ptr<CriticalSectionWrapper> critsect_;
133 int target_bitrate_kbytes_per_s_;
134 int bytes_remaining_interval_;
135 int padding_bytes_remaining_interval_;
136 TickTime time_last_update_;
137 TickTime time_last_send_;
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000138 int64_t capture_time_ms_last_queued_;
139 int64_t capture_time_ms_last_sent_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000140
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000141 PacketList high_priority_packets_;
142 PacketList normal_priority_packets_;
143 PacketList low_priority_packets_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000144};
145} // namespace webrtc
146#endif // WEBRTC_MODULES_PACED_SENDER_H_