blob: b9151a5fc0b08a52a9f216391be67a5b0a9ce497 [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"
pbos@webrtc.org03c817e2014-07-07 10:20:35 +000019#include "webrtc/system_wrappers/interface/thread_annotations.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000020#include "webrtc/typedefs.h"
21
22namespace webrtc {
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000023class Clock;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000024class CriticalSectionWrapper;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000025
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000026namespace paced_sender {
27class IntervalBudget;
28struct Packet;
29class PacketList;
30} // namespace paced_sender
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000031
32class PacedSender : public Module {
33 public:
34 enum Priority {
35 kHighPriority = 0, // Pass through; will be sent immediately.
36 kNormalPriority = 2, // Put in back of the line.
37 kLowPriority = 3, // Put in back of the low priority line.
38 };
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000039 // Low priority packets are mixed with the normal priority packets
40 // while we are paused.
41
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000042 class Callback {
43 public:
44 // Note: packets sent as a result of a callback should not pass by this
45 // module again.
46 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000047 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000048 virtual bool TimeToSendPacket(uint32_t ssrc,
49 uint16_t sequence_number,
50 int64_t capture_time_ms,
51 bool retransmission) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000052 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000053 // Returns the number of bytes sent.
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000054 virtual int TimeToSendPadding(int bytes) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000055
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000056 protected:
57 virtual ~Callback() {}
58 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000059
60 static const int kDefaultMaxQueueLengthMs = 2000;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000061 // Pace in kbits/s until we receive first estimate.
62 static const int kDefaultInitialPaceKbps = 2000;
63 // Pacing-rate relative to our target send rate.
64 // Multiplicative factor that is applied to the target bitrate to calculate
65 // the number of bytes that can be transmitted per interval.
66 // Increasing this factor will result in lower delays in cases of bitrate
67 // overshoots from the encoder.
68 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000069
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000070 PacedSender(Clock* clock, Callback* callback, int max_bitrate_kbps,
71 int min_bitrate_kbps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000072
73 virtual ~PacedSender();
74
75 // Enable/disable pacing.
76 void SetStatus(bool enable);
77
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000078 bool Enabled() const;
79
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000080 // Temporarily pause all sending.
81 void Pause();
82
83 // Resume sending packets.
84 void Resume();
85
pbos@webrtc.org709e2972014-03-19 10:59:52 +000086 // Set target bitrates for the pacer. Padding packets will be utilized to
87 // reach |min_bitrate| unless enough media packets are available.
88 void UpdateBitrate(int max_bitrate_kbps, int min_bitrate_kbps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000089
90 // Returns true if we send the packet now, else it will add the packet
91 // information to the queue and call TimeToSendPacket when it's time to send.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +000092 virtual bool SendPacket(Priority priority,
93 uint32_t ssrc,
94 uint16_t sequence_number,
95 int64_t capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000096 int bytes,
97 bool retransmission);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000098
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000099 // Sets the max length of the pacer queue in milliseconds.
100 // A negative queue size is interpreted as infinite.
101 virtual void set_max_queue_length_ms(int max_queue_length_ms);
102
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000103 // Returns the time since the oldest queued packet was enqueued.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000104 virtual int QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000105
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000106 // Returns the number of milliseconds until the module want a worker thread
107 // to call Process.
pbos@webrtc.org01931582013-07-31 15:18:19 +0000108 virtual int32_t TimeUntilNextProcess() OVERRIDE;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000109
110 // Process any pending packets in the queue(s).
pbos@webrtc.org01931582013-07-31 15:18:19 +0000111 virtual int32_t Process() OVERRIDE;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000112
113 private:
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000114 // Return true if next packet in line should be transmitted.
115 // Return packet list that contains the next packet.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000116 bool ShouldSendNextPacket(paced_sender::PacketList** packet_list)
117 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000118
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000119 // Local helper function to GetNextPacket.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000120 paced_sender::Packet GetNextPacketFromList(paced_sender::PacketList* packets)
121 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000122
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000123 bool SendPacketFromList(paced_sender::PacketList* packet_list)
124 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000125
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000126 // Updates the number of bytes that can be sent for the next time interval.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000127 void UpdateBytesPerInterval(uint32_t delta_time_in_ms)
128 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000129
130 // Updates the buffers with the number of bytes that we sent.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000131 void UpdateMediaBytesSent(int num_bytes) EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000132
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000133 Clock* const clock_;
134 Callback* const callback_;
135
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000136 scoped_ptr<CriticalSectionWrapper> critsect_;
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000137 bool enabled_ GUARDED_BY(critsect_);
138 bool paused_ GUARDED_BY(critsect_);
139 int max_queue_length_ms_ GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000140 // This is the media budget, keeping track of how many bits of media
141 // we can pace out during the current interval.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000142 scoped_ptr<paced_sender::IntervalBudget> media_budget_ GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000143 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000144 // allowed to send out during the current interval. This budget will be
145 // utilized when there's no media to send.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000146 scoped_ptr<paced_sender::IntervalBudget> padding_budget_
147 GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000148
stefan@webrtc.org168f23f2014-07-11 13:44:02 +0000149 int64_t time_last_update_ GUARDED_BY(critsect_);
150 int64_t time_last_send_ GUARDED_BY(critsect_);
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000151 int64_t capture_time_ms_last_queued_ GUARDED_BY(critsect_);
152 int64_t capture_time_ms_last_sent_ GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000153
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000154 scoped_ptr<paced_sender::PacketList> high_priority_packets_
155 GUARDED_BY(critsect_);
156 scoped_ptr<paced_sender::PacketList> normal_priority_packets_
157 GUARDED_BY(critsect_);
158 scoped_ptr<paced_sender::PacketList> low_priority_packets_
159 GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000160};
161} // namespace webrtc
162#endif // WEBRTC_MODULES_PACED_SENDER_H_