blob: 45c89b77e3b047a53f9db8d0cbb2be1a6bd13bb3 [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;
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000024namespace paced_sender {
25class IntervalBudget;
26struct Packet;
27class PacketList;
28} // namespace paced_sender
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000029
30class PacedSender : public Module {
31 public:
32 enum Priority {
33 kHighPriority = 0, // Pass through; will be sent immediately.
34 kNormalPriority = 2, // Put in back of the line.
35 kLowPriority = 3, // Put in back of the low priority line.
36 };
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000037 // Low priority packets are mixed with the normal priority packets
38 // while we are paused.
39
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000040 class Callback {
41 public:
42 // Note: packets sent as a result of a callback should not pass by this
43 // module again.
44 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000045 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000046 virtual bool TimeToSendPacket(uint32_t ssrc,
47 uint16_t sequence_number,
48 int64_t capture_time_ms,
49 bool retransmission) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000050 // Called when it's a good time to send a padding data.
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000051 virtual int TimeToSendPadding(int bytes) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000052
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000053 protected:
54 virtual ~Callback() {}
55 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000056
57 static const int kDefaultMaxQueueLengthMs = 2000;
58
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +000059 PacedSender(Callback* callback, int target_bitrate_kbps,
60 float pace_multiplier);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000061
62 virtual ~PacedSender();
63
64 // Enable/disable pacing.
65 void SetStatus(bool enable);
66
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000067 bool Enabled() const;
68
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000069 // Temporarily pause all sending.
70 void Pause();
71
72 // Resume sending packets.
73 void Resume();
74
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000075 // Set the pacing target bitrate and the bitrate up to which we are allowed to
76 // pad. We will send padding packets to increase the total bitrate until we
77 // reach |pad_up_to_bitrate_kbps|. If the media bitrate is above
78 // |pad_up_to_bitrate_kbps| no padding will be sent.
stefan@webrtc.orgb2c8a952013-09-06 13:58:01 +000079 void UpdateBitrate(int target_bitrate_kbps,
80 int max_padding_bitrate_kbps,
81 int pad_up_to_bitrate_kbps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000082
83 // Returns true if we send the packet now, else it will add the packet
84 // information to the queue and call TimeToSendPacket when it's time to send.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000085 virtual bool SendPacket(Priority priority,
86 uint32_t ssrc,
87 uint16_t sequence_number,
88 int64_t capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000089 int bytes,
90 bool retransmission);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000091
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000092 // Sets the max length of the pacer queue in milliseconds.
93 // A negative queue size is interpreted as infinite.
94 virtual void set_max_queue_length_ms(int max_queue_length_ms);
95
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +000096 // Returns the time since the oldest queued packet was enqueued.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000097 virtual int QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000098
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000099 // Returns the number of milliseconds until the module want a worker thread
100 // to call Process.
pbos@webrtc.org01931582013-07-31 15:18:19 +0000101 virtual int32_t TimeUntilNextProcess() OVERRIDE;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000102
103 // Process any pending packets in the queue(s).
pbos@webrtc.org01931582013-07-31 15:18:19 +0000104 virtual int32_t Process() OVERRIDE;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000105
106 private:
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000107 // Return true if next packet in line should be transmitted.
108 // Return packet list that contains the next packet.
109 bool ShouldSendNextPacket(paced_sender::PacketList** packet_list);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000110
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000111 // Local helper function to GetNextPacket.
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000112 paced_sender::Packet GetNextPacketFromList(paced_sender::PacketList* packets);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000113
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000114 bool SendPacketFromList(paced_sender::PacketList* packet_list);
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.
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000120 void UpdateMediaBytesSent(int num_bytes);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000121
122 Callback* callback_;
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000123 const float pace_multiplier_;
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000124 bool enabled_;
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000125 bool paused_;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000126 int max_queue_length_ms_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000127 scoped_ptr<CriticalSectionWrapper> critsect_;
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000128 // This is the media budget, keeping track of how many bits of media
129 // we can pace out during the current interval.
130 scoped_ptr<paced_sender::IntervalBudget> media_budget_;
131 // This is the padding budget, keeping track of how many bits of padding we're
132 // allowed to send out during the current interval.
133 scoped_ptr<paced_sender::IntervalBudget> padding_budget_;
134 // Media and padding share this budget, therefore no padding will be sent if
135 // media uses all of this budget. This is used to avoid padding above a given
136 // bitrate.
137 scoped_ptr<paced_sender::IntervalBudget> pad_up_to_bitrate_budget_;
138
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000139 TickTime time_last_update_;
140 TickTime time_last_send_;
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000141 int64_t capture_time_ms_last_queued_;
142 int64_t capture_time_ms_last_sent_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000143
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000144 scoped_ptr<paced_sender::PacketList> high_priority_packets_;
145 scoped_ptr<paced_sender::PacketList> normal_priority_packets_;
146 scoped_ptr<paced_sender::PacketList> low_priority_packets_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000147};
148} // namespace webrtc
149#endif // WEBRTC_MODULES_PACED_SENDER_H_