blob: b9bb1c39a484da59142efc62b7c5a0254cf1a5b6 [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
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010011#ifndef WEBRTC_MODULES_PACING_PACED_SENDER_H_
12#define WEBRTC_MODULES_PACING_PACED_SENDER_H_
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000013
14#include <list>
kwiberg22feaa32016-03-17 09:17:43 -070015#include <memory>
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000016#include <set>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000017
sergeyu80ed35e2016-11-28 13:11:13 -080018#include "webrtc/base/optional.h"
pbos@webrtc.org38344ed2014-09-24 06:05:00 +000019#include "webrtc/base/thread_annotations.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/module.h"
21#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000022#include "webrtc/typedefs.h"
23
24namespace webrtc {
isheriff31687812016-10-04 08:43:09 -070025class AlrDetector;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000026class BitrateProber;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000027class Clock;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000028class CriticalSectionWrapper;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000029
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000030namespace paced_sender {
31class IntervalBudget;
32struct Packet;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +000033class PacketQueue;
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000034} // namespace paced_sender
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000035
sprangebbf8a82015-09-21 15:11:14 -070036class PacedSender : public Module, public RtpPacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000037 public:
perkjec81bcd2016-05-11 06:01:13 -070038 class PacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000039 public:
40 // Note: packets sent as a result of a callback should not pass by this
41 // module again.
42 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000043 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000044 virtual bool TimeToSendPacket(uint32_t ssrc,
45 uint16_t sequence_number,
46 int64_t capture_time_ms,
philipel29dca2c2016-05-13 11:13:05 +020047 bool retransmission,
48 int probe_cluster_id) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000049 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000050 // Returns the number of bytes sent.
philipela1ed0b32016-06-01 06:31:17 -070051 virtual size_t TimeToSendPadding(size_t bytes, int probe_cluster_id) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000052
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000053 protected:
perkjec81bcd2016-05-11 06:01:13 -070054 virtual ~PacketSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000055 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000056
sprang0a43fef2015-11-20 09:00:37 -080057 // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
58 // this value, the packet producers should wait (eg drop frames rather than
59 // encoding them). Bitrate sent may temporarily exceed target set by
60 // UpdateBitrate() so that this limit will be upheld.
61 static const int64_t kMaxQueueLengthMs;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000062 // Pacing-rate relative to our target send rate.
63 // Multiplicative factor that is applied to the target bitrate to calculate
64 // the number of bytes that can be transmitted per interval.
65 // Increasing this factor will result in lower delays in cases of bitrate
66 // overshoots from the encoder.
67 static const float kDefaultPaceMultiplier;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000068
sprangebbf8a82015-09-21 15:11:14 -070069 static const size_t kMinProbePacketSize = 200;
70
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000071 PacedSender(Clock* clock,
perkjec81bcd2016-05-11 06:01:13 -070072 PacketSender* packet_sender);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000073
74 virtual ~PacedSender();
75
Irfan Sheriffb2540bb2016-09-12 12:28:54 -070076 virtual void CreateProbeCluster(int bitrate_bps, int num_packets);
philipeleb680ea2016-08-17 11:11:59 +020077
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000078 // Temporarily pause all sending.
79 void Pause();
80
81 // Resume sending packets.
82 void Resume();
83
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000084 // Enable bitrate probing. Enabled by default, mostly here to simplify
85 // testing. Must be called before any packets are being sent to have an
86 // effect.
87 void SetProbingEnabled(bool enabled);
88
perkjec81bcd2016-05-11 06:01:13 -070089 // Sets the estimated capacity of the network. Must be called once before
90 // packets can be sent.
91 // |bitrate_bps| is our estimate of what we are allowed to send on average.
92 // We will pace out bursts of packets at a bitrate of
93 // |bitrate_bps| * kDefaultPaceMultiplier.
94 virtual void SetEstimatedBitrate(uint32_t bitrate_bps);
95
perkj71ee44c2016-06-15 00:47:53 -070096 // Sets the minimum send bitrate and maximum padding bitrate requested by send
97 // streams.
98 // |min_send_bitrate_bps| might be higher that the estimated available network
99 // bitrate and if so, the pacer will send with |min_send_bitrate_bps|.
100 // |max_padding_bitrate_bps| might be higher than the estimate available
101 // network bitrate and if so, the pacer will send padding packets to reach
102 // the min of the estimated available bitrate and |max_padding_bitrate_bps|.
103 void SetSendBitrateLimits(int min_send_bitrate_bps,
104 int max_padding_bitrate_bps);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000105
106 // Returns true if we send the packet now, else it will add the packet
107 // information to the queue and call TimeToSendPacket when it's time to send.
Peter Boströme23e7372015-10-08 11:44:14 +0200108 void InsertPacket(RtpPacketSender::Priority priority,
109 uint32_t ssrc,
110 uint16_t sequence_number,
111 int64_t capture_time_ms,
112 size_t bytes,
113 bool retransmission) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000114
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000115 // Returns the time since the oldest queued packet was enqueued.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000116 virtual int64_t QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000117
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000118 virtual size_t QueueSizePackets() const;
119
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000120 // Returns the number of milliseconds it will take to send the current
121 // packets in the queue, given the current size and bitrate, ignoring prio.
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000122 virtual int64_t ExpectedQueueTimeMs() const;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000123
sergeyu80ed35e2016-11-28 13:11:13 -0800124 // Returns time in milliseconds when the current application-limited region
125 // started or empty result if the sender is currently not application-limited.
126 //
127 // Application Limited Region (ALR) refers to operating in a state where the
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700128 // traffic on network is limited due to application not having enough
129 // traffic to meet the current channel capacity.
sergeyu80ed35e2016-11-28 13:11:13 -0800130 virtual rtc::Optional<int64_t> GetApplicationLimitedRegionStartTime() const;
Irfan Sheriff1eb12932016-10-18 17:04:25 -0700131
Erik Språngad113e52015-11-26 16:26:12 +0100132 // Returns the average time since being enqueued, in milliseconds, for all
133 // packets currently in the pacer queue, or 0 if queue is empty.
134 virtual int64_t AverageQueueTimeMs();
135
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000136 // Returns the number of milliseconds until the module want a worker thread
137 // to call Process.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000138 int64_t TimeUntilNextProcess() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000139
140 // Process any pending packets in the queue(s).
pbosa26ac922016-02-25 04:50:01 -0800141 void Process() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000142
143 private:
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000144 // Updates the number of bytes that can be sent for the next time interval.
isheriff31687812016-10-04 08:43:09 -0700145 void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms)
146 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
147 void UpdateBudgetWithBytesSent(size_t bytes)
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000148 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000149
philipel29dca2c2016-05-13 11:13:05 +0200150 bool SendPacket(const paced_sender::Packet& packet, int probe_cluster_id)
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000151 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
isheriffcc5903e2016-10-04 08:29:38 -0700152 size_t SendPadding(size_t padding_needed, int probe_cluster_id)
philipela1ed0b32016-06-01 06:31:17 -0700153 EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000154
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000155 Clock* const clock_;
perkjec81bcd2016-05-11 06:01:13 -0700156 PacketSender* const packet_sender_;
isheriff31687812016-10-04 08:43:09 -0700157 std::unique_ptr<AlrDetector> alr_detector_ GUARDED_BY(critsect_);
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000158
kwiberg22feaa32016-03-17 09:17:43 -0700159 std::unique_ptr<CriticalSectionWrapper> critsect_;
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000160 bool paused_ GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000161 // This is the media budget, keeping track of how many bits of media
162 // we can pace out during the current interval.
kwiberg22feaa32016-03-17 09:17:43 -0700163 std::unique_ptr<paced_sender::IntervalBudget> media_budget_
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000164 GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000165 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000166 // allowed to send out during the current interval. This budget will be
167 // utilized when there's no media to send.
kwiberg22feaa32016-03-17 09:17:43 -0700168 std::unique_ptr<paced_sender::IntervalBudget> padding_budget_
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000169 GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000170
kwiberg22feaa32016-03-17 09:17:43 -0700171 std::unique_ptr<BitrateProber> prober_ GUARDED_BY(critsect_);
sprang0a43fef2015-11-20 09:00:37 -0800172 // Actual configured bitrates (media_budget_ may temporarily be higher in
173 // order to meet pace time constraint).
perkjec81bcd2016-05-11 06:01:13 -0700174 uint32_t estimated_bitrate_bps_ GUARDED_BY(critsect_);
175 uint32_t min_send_bitrate_kbps_ GUARDED_BY(critsect_);
perkj71ee44c2016-06-15 00:47:53 -0700176 uint32_t max_padding_bitrate_kbps_ GUARDED_BY(critsect_);
perkjec81bcd2016-05-11 06:01:13 -0700177 uint32_t pacing_bitrate_kbps_ GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000178
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000179 int64_t time_last_update_us_ GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000180
kwiberg22feaa32016-03-17 09:17:43 -0700181 std::unique_ptr<paced_sender::PacketQueue> packets_ GUARDED_BY(critsect_);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000182 uint64_t packet_counter_;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000183};
184} // namespace webrtc
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +0100185#endif // WEBRTC_MODULES_PACING_PACED_SENDER_H_