blob: d3034466b98976da3f293e4c541718f5ba3849e1 [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
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000011#ifndef WEBRTC_MODULES_PACING_INCLUDE_PACED_SENDER_H_
12#define WEBRTC_MODULES_PACING_INCLUDE_PACED_SENDER_H_
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000013
14#include <list>
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000015#include <set>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000016
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000017#include "webrtc/base/thread_annotations.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000018#include "webrtc/modules/interface/module.h"
19#include "webrtc/system_wrappers/interface/scoped_ptr.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000020#include "webrtc/typedefs.h"
21
22namespace webrtc {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000023class BitrateProber;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000024class Clock;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000025class CriticalSectionWrapper;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000026
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000027namespace paced_sender {
28class IntervalBudget;
29struct Packet;
30class PacketList;
31} // namespace paced_sender
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000032
33class PacedSender : public Module {
34 public:
35 enum Priority {
36 kHighPriority = 0, // Pass through; will be sent immediately.
37 kNormalPriority = 2, // Put in back of the line.
38 kLowPriority = 3, // Put in back of the low priority line.
39 };
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000040 // Low priority packets are mixed with the normal priority packets
41 // while we are paused.
42
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000043 class Callback {
44 public:
45 // Note: packets sent as a result of a callback should not pass by this
46 // module again.
47 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000048 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000049 virtual bool TimeToSendPacket(uint32_t ssrc,
50 uint16_t sequence_number,
51 int64_t capture_time_ms,
52 bool retransmission) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000053 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000054 // Returns the number of bytes sent.
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000055 virtual int TimeToSendPadding(int bytes) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000056
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000057 protected:
58 virtual ~Callback() {}
59 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000060
61 static const int kDefaultMaxQueueLengthMs = 2000;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000062 // Pace in kbits/s until we receive first estimate.
63 static const int kDefaultInitialPaceKbps = 2000;
64 // Pacing-rate relative to our target send rate.
65 // Multiplicative factor that is applied to the target bitrate to calculate
66 // the number of bytes that can be transmitted per interval.
67 // Increasing this factor will result in lower delays in cases of bitrate
68 // overshoots from the encoder.
69 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000070
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000071 PacedSender(Clock* clock,
72 Callback* callback,
73 int bitrate_kbps,
74 int max_bitrate_kbps,
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000075 int min_bitrate_kbps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000076
77 virtual ~PacedSender();
78
79 // Enable/disable pacing.
80 void SetStatus(bool enable);
81
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000082 bool Enabled() const;
83
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000084 // Temporarily pause all sending.
85 void Pause();
86
87 // Resume sending packets.
88 void Resume();
89
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000090 // Set target bitrates for the pacer.
91 // We will pace out bursts of packets at a bitrate of |max_bitrate_kbps|.
92 // |bitrate_kbps| is our estimate of what we are allowed to send on average.
93 // Padding packets will be utilized to reach |min_bitrate| unless enough media
94 // packets are available.
95 void UpdateBitrate(int bitrate_kbps,
96 int max_bitrate_kbps,
97 int min_bitrate_kbps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000098
99 // Returns true if we send the packet now, else it will add the packet
100 // information to the queue and call TimeToSendPacket when it's time to send.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000101 virtual bool SendPacket(Priority priority,
102 uint32_t ssrc,
103 uint16_t sequence_number,
104 int64_t capture_time_ms,
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +0000105 int bytes,
106 bool retransmission);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000107
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000108 // Sets the max length of the pacer queue in milliseconds.
109 // A negative queue size is interpreted as infinite.
110 virtual void set_max_queue_length_ms(int max_queue_length_ms);
111
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000112 // Returns the time since the oldest queued packet was enqueued.
pwestin@webrtc.orgb0061f92013-04-27 00:41:08 +0000113 virtual int QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000114
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000115 virtual size_t QueueSizePackets() const;
116
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000117 // Returns the number of milliseconds until the module want a worker thread
118 // to call Process.
pbos@webrtc.org01931582013-07-31 15:18:19 +0000119 virtual int32_t TimeUntilNextProcess() OVERRIDE;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000120
121 // Process any pending packets in the queue(s).
pbos@webrtc.org01931582013-07-31 15:18:19 +0000122 virtual int32_t Process() OVERRIDE;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000123
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000124 protected:
125 virtual bool ProbingExperimentIsEnabled() const;
126
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000127 private:
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000128 // Return true if next packet in line should be transmitted.
129 // Return packet list that contains the next packet.
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000130 bool ShouldSendNextPacket(paced_sender::PacketList** packet_list, bool probe)
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000131 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000132
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000133 // Local helper function to GetNextPacket.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000134 paced_sender::Packet GetNextPacketFromList(paced_sender::PacketList* packets)
135 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000136
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000137 bool SendPacketFromList(paced_sender::PacketList* packet_list)
138 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000139
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000140 // Updates the number of bytes that can be sent for the next time interval.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000141 void UpdateBytesPerInterval(uint32_t delta_time_in_ms)
142 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000143
144 // Updates the buffers with the number of bytes that we sent.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000145 void UpdateMediaBytesSent(int num_bytes) EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000146
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000147 Clock* const clock_;
148 Callback* const callback_;
149
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000150 scoped_ptr<CriticalSectionWrapper> critsect_;
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000151 bool enabled_ GUARDED_BY(critsect_);
152 bool paused_ GUARDED_BY(critsect_);
153 int max_queue_length_ms_ GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000154 // This is the media budget, keeping track of how many bits of media
155 // we can pace out during the current interval.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000156 scoped_ptr<paced_sender::IntervalBudget> media_budget_ GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000157 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000158 // allowed to send out during the current interval. This budget will be
159 // utilized when there's no media to send.
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000160 scoped_ptr<paced_sender::IntervalBudget> padding_budget_
161 GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000162
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000163 scoped_ptr<BitrateProber> prober_ GUARDED_BY(critsect_);
164 int bitrate_bps_ GUARDED_BY(critsect_);
165
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000166 int64_t time_last_update_us_ GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000167 // Only accessed via process thread.
168 int64_t time_last_media_send_us_;
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000169 int64_t capture_time_ms_last_queued_ GUARDED_BY(critsect_);
170 int64_t capture_time_ms_last_sent_ GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000171
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000172 scoped_ptr<paced_sender::PacketList> high_priority_packets_
173 GUARDED_BY(critsect_);
174 scoped_ptr<paced_sender::PacketList> normal_priority_packets_
175 GUARDED_BY(critsect_);
176 scoped_ptr<paced_sender::PacketList> low_priority_packets_
177 GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000178};
179} // namespace webrtc
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000180#endif // WEBRTC_MODULES_PACING_INCLUDE_PACED_SENDER_H_