blob: 0501bbb691474c0d5dd9a4f01e83fc2cb0ecdf17 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_PACING_PACED_SENDER_H_
12#define MODULES_PACING_PACED_SENDER_H_
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000013
kwiberg22feaa32016-03-17 09:17:43 -070014#include <memory>
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/optional.h"
17#include "modules/pacing/pacer.h"
Sebastian Janssonb5374962018-02-07 13:26:38 +010018#include "modules/pacing/packet_queue_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/criticalsection.h"
20#include "rtc_base/thread_annotations.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "typedefs.h" // NOLINT(build/include)
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000022
23namespace webrtc {
stefan@webrtc.org82462aa2014-10-23 11:57:05 +000024class BitrateProber;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000025class Clock;
philipelfd58b612017-01-04 07:05:25 -080026class ProbeClusterCreatedObserver;
philipelc3b3f7a2017-03-29 01:23:13 -070027class RtcEventLog;
tschumim82c55932017-07-11 06:56:04 -070028class IntervalBudget;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000029
gnisha36165c2017-08-20 09:19:58 -070030class PacedSender : public Pacer {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000031 public:
perkjec81bcd2016-05-11 06:01:13 -070032 class PacketSender {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000033 public:
34 // Note: packets sent as a result of a callback should not pass by this
35 // module again.
36 // Called when it's time to send a queued packet.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +000037 // Returns false if packet cannot be sent.
stefan@webrtc.org9b82f5a2013-11-13 15:29:21 +000038 virtual bool TimeToSendPacket(uint32_t ssrc,
39 uint16_t sequence_number,
40 int64_t capture_time_ms,
philipel29dca2c2016-05-13 11:13:05 +020041 bool retransmission,
philipelc7bf32a2017-02-17 03:59:43 -080042 const PacedPacketInfo& cluster_info) = 0;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000043 // Called when it's a good time to send a padding data.
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000044 // Returns the number of bytes sent.
philipelc7bf32a2017-02-17 03:59:43 -080045 virtual size_t TimeToSendPadding(size_t bytes,
46 const PacedPacketInfo& cluster_info) = 0;
jiayl@webrtc.org9fd8d872014-02-27 22:32:40 +000047
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000048 protected:
perkjec81bcd2016-05-11 06:01:13 -070049 virtual ~PacketSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000050 };
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000051
sprang0a43fef2015-11-20 09:00:37 -080052 // Expected max pacer delay in ms. If ExpectedQueueTimeMs() is higher than
53 // this value, the packet producers should wait (eg drop frames rather than
54 // encoding them). Bitrate sent may temporarily exceed target set by
55 // UpdateBitrate() so that this limit will be upheld.
56 static const int64_t kMaxQueueLengthMs;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +000057
philipelc3b3f7a2017-03-29 01:23:13 -070058 PacedSender(const Clock* clock,
59 PacketSender* packet_sender,
60 RtcEventLog* event_log);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000061
Niels Möller9d4af012017-10-31 09:54:50 +010062 PacedSender(const Clock* clock,
63 PacketSender* packet_sender,
64 RtcEventLog* event_log,
Sebastian Janssonb5374962018-02-07 13:26:38 +010065 std::unique_ptr<PacketQueueInterface> packets);
Niels Möller9d4af012017-10-31 09:54:50 +010066
nisse76e62b02017-05-31 02:24:52 -070067 ~PacedSender() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000068
philipelfd58b612017-01-04 07:05:25 -080069 virtual void CreateProbeCluster(int bitrate_bps);
philipeleb680ea2016-08-17 11:11:59 +020070
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000071 // Temporarily pause all sending.
72 void Pause();
73
74 // Resume sending packets.
75 void Resume();
76
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +000077 // Enable bitrate probing. Enabled by default, mostly here to simplify
78 // testing. Must be called before any packets are being sent to have an
79 // effect.
80 void SetProbingEnabled(bool enabled);
81
Sebastian Jansson57daeb72018-02-05 17:15:09 +010082 // Sets the pacing rates. Must be called once before packets can be sent.
83 void SetPacingRates(uint32_t pacing_rate_bps,
84 uint32_t padding_rate_bps) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000085
86 // Returns true if we send the packet now, else it will add the packet
87 // information to the queue and call TimeToSendPacket when it's time to send.
Peter Boströme23e7372015-10-08 11:44:14 +020088 void InsertPacket(RtpPacketSender::Priority priority,
89 uint32_t ssrc,
90 uint16_t sequence_number,
91 int64_t capture_time_ms,
92 size_t bytes,
93 bool retransmission) override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000094
Alex Narest78609d52017-10-20 10:37:47 +020095 // Currently audio traffic is not accounted by pacer and passed through.
96 // With the introduction of audio BWE audio traffic will be accounted for
97 // the pacer budget calculation. The audio traffic still will be injected
98 // at high priority.
99 void SetAccountForAudioPackets(bool account_for_audio) override;
100
stefan@webrtc.orgdd393e72013-12-13 22:03:27 +0000101 // Returns the time since the oldest queued packet was enqueued.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000102 virtual int64_t QueueInMs() const;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000103
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000104 virtual size_t QueueSizePackets() const;
105
asaperssonfc5e81c2017-04-19 23:28:53 -0700106 // Returns the time when the first packet was sent, or -1 if no packet is
107 // sent.
108 virtual int64_t FirstSentPacketTimeMs() const;
109
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000110 // Returns the number of milliseconds it will take to send the current
111 // packets in the queue, given the current size and bitrate, ignoring prio.
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000112 virtual int64_t ExpectedQueueTimeMs() const;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000113
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000114 // Returns the number of milliseconds until the module want a worker thread
115 // to call Process.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000116 int64_t TimeUntilNextProcess() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000117
118 // Process any pending packets in the queue(s).
pbosa26ac922016-02-25 04:50:01 -0800119 void Process() override;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000120
tommi919dce22017-03-15 07:45:36 -0700121 // Called when the prober is associated with a process thread.
122 void ProcessThreadAttached(ProcessThread* process_thread) override;
sprang89c4a7e2017-06-30 13:27:40 -0700123 void SetQueueTimeLimit(int limit_ms);
124
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000125 private:
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000126 // Updates the number of bytes that can be sent for the next time interval.
isheriff31687812016-10-04 08:43:09 -0700127 void UpdateBudgetWithElapsedTime(int64_t delta_time_in_ms)
danilchap56359be2017-09-07 07:53:45 -0700128 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
isheriff31687812016-10-04 08:43:09 -0700129 void UpdateBudgetWithBytesSent(size_t bytes)
danilchap56359be2017-09-07 07:53:45 -0700130 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000131
Sebastian Janssonb5374962018-02-07 13:26:38 +0100132 bool SendPacket(const PacketQueueInterface::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800133 const PacedPacketInfo& cluster_info)
danilchap56359be2017-09-07 07:53:45 -0700134 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
philipelc7bf32a2017-02-17 03:59:43 -0800135 size_t SendPadding(size_t padding_needed, const PacedPacketInfo& cluster_info)
danilchap56359be2017-09-07 07:53:45 -0700136 RTC_EXCLUSIVE_LOCKS_REQUIRED(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000137
elad.alon61ce37e2017-03-09 07:09:31 -0800138 const Clock* const clock_;
perkjec81bcd2016-05-11 06:01:13 -0700139 PacketSender* const packet_sender_;
pbos@webrtc.org03c817e2014-07-07 10:20:35 +0000140
kthelgason6bfe49c2017-03-30 01:14:41 -0700141 rtc::CriticalSection critsect_;
danilchap56359be2017-09-07 07:53:45 -0700142 bool paused_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000143 // This is the media budget, keeping track of how many bits of media
144 // we can pace out during the current interval.
Niels Möller712048c2017-10-18 13:08:22 +0200145 const std::unique_ptr<IntervalBudget> media_budget_
146 RTC_PT_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000147 // This is the padding budget, keeping track of how many bits of padding we're
pbos@webrtc.org709e2972014-03-19 10:59:52 +0000148 // allowed to send out during the current interval. This budget will be
149 // utilized when there's no media to send.
Niels Möller712048c2017-10-18 13:08:22 +0200150 const std::unique_ptr<IntervalBudget> padding_budget_
151 RTC_PT_GUARDED_BY(critsect_);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000152
Niels Möller712048c2017-10-18 13:08:22 +0200153 const std::unique_ptr<BitrateProber> prober_ RTC_PT_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200154 bool probing_send_failure_ RTC_GUARDED_BY(critsect_);
sprang0a43fef2015-11-20 09:00:37 -0800155 // Actual configured bitrates (media_budget_ may temporarily be higher in
156 // order to meet pace time constraint).
danilchap56359be2017-09-07 07:53:45 -0700157 uint32_t pacing_bitrate_kbps_ RTC_GUARDED_BY(critsect_);
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000158
danilchap56359be2017-09-07 07:53:45 -0700159 int64_t time_last_update_us_ RTC_GUARDED_BY(critsect_);
160 int64_t first_sent_packet_ms_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000161
Sebastian Janssonb5374962018-02-07 13:26:38 +0100162 const std::unique_ptr<PacketQueueInterface> packets_
163 RTC_PT_GUARDED_BY(critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200164 uint64_t packet_counter_ RTC_GUARDED_BY(critsect_);
sprang89c4a7e2017-06-30 13:27:40 -0700165
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100166 // Lock to avoid race when attaching process thread. This can happen due to
167 // the Call class setting network state on SendSideCongestionController, which
168 // in turn calls Pause/Resume on Pacedsender, before actually starting the
169 // pacer process thread. If SendSideCongestionController is running on a task
170 // queue separate from the thread used by Call, this causes a race.
171 rtc::CriticalSection process_thread_lock_;
172 ProcessThread* process_thread_ RTC_GUARDED_BY(process_thread_lock_) = nullptr;
173
danilchap56359be2017-09-07 07:53:45 -0700174 int64_t queue_time_limit RTC_GUARDED_BY(critsect_);
Alex Narest78609d52017-10-20 10:37:47 +0200175 bool account_for_audio_ RTC_GUARDED_BY(critsect_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000176};
177} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200178#endif // MODULES_PACING_PACED_SENDER_H_