blob: 050fbb651a0d5e922e7a450562e7dddedba754aa [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#include "modules/pacing/paced_sender.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000012
jbauchd2a22962016-02-08 23:18:25 -080013#include <algorithm>
Sebastian Jansson916ae082018-10-26 13:10:23 +020014#include <utility>
pbos@webrtc.org03c817e2014-07-07 10:20:35 +000015
Karl Wiberg918f50c2018-07-05 11:40:33 +020016#include "absl/memory/memory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020017#include "logging/rtc_event_log/rtc_event_log.h"
Sebastian Janssonf9f49a32018-06-25 17:56:08 +020018#include "modules/congestion_controller/goog_cc/alr_detector.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/pacing/bitrate_prober.h"
20#include "modules/pacing/interval_budget.h"
21#include "modules/utility/include/process_thread.h"
22#include "rtc_base/checks.h"
23#include "rtc_base/logging.h"
24#include "system_wrappers/include/clock.h"
25#include "system_wrappers/include/field_trial.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000026
27namespace {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000028// Time limit in milliseconds between packet bursts.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000029const int64_t kMinPacketLimitMs = 5;
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010030const int64_t kCongestedPacketIntervalMs = 500;
31const int64_t kPausedProcessIntervalMs = kCongestedPacketIntervalMs;
Sebastian Janssone5d8c572018-02-28 08:53:06 +010032const int64_t kMaxElapsedTimeMs = 2000;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000033
34// Upper cap on process interval, in case process has not been called in a long
35// time.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000036const int64_t kMaxIntervalTimeMs = 30;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000037
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000038} // namespace
39
40namespace webrtc {
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000041
sprang0a43fef2015-11-20 09:00:37 -080042const int64_t PacedSender::kMaxQueueLengthMs = 2000;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000043const float PacedSender::kDefaultPaceMultiplier = 2.5f;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000044
philipelc3b3f7a2017-03-29 01:23:13 -070045PacedSender::PacedSender(const Clock* clock,
46 PacketSender* packet_sender,
Sebastian Janssonb5374962018-02-07 13:26:38 +010047 RtcEventLog* event_log)
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000048 : clock_(clock),
perkjec81bcd2016-05-11 06:01:13 -070049 packet_sender_(packet_sender),
Bjorn Tereliusa956d492018-12-07 17:45:41 +010050 alr_detector_(),
Sebastian Jansson0601d682018-06-25 19:23:05 +020051 drain_large_queues_(!field_trial::IsDisabled("WebRTC-Pacer-DrainQueue")),
Sebastian Janssonc235a8d2018-06-15 14:46:11 +020052 send_padding_if_silent_(
53 field_trial::IsEnabled("WebRTC-Pacer-PadInSilence")),
Sebastian Janssonce4829a2018-06-15 14:47:35 +020054 video_blocks_audio_(!field_trial::IsDisabled("WebRTC-Pacer-BlockAudio")),
Erik Språng96816752018-09-04 18:40:19 +020055 last_timestamp_ms_(clock_->TimeInMilliseconds()),
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000056 paused_(false),
Sebastian Jansson03914462018-10-11 20:22:03 +020057 media_budget_(0),
58 padding_budget_(0),
59 prober_(event_log),
philipelb61927c2017-02-28 07:05:23 -080060 probing_send_failure_(false),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000061 estimated_bitrate_bps_(0),
62 min_send_bitrate_kbps_(0u),
63 max_padding_bitrate_kbps_(0u),
perkjec81bcd2016-05-11 06:01:13 -070064 pacing_bitrate_kbps_(0),
Sebastian Janssona1630f82018-02-21 13:39:26 +010065 time_last_process_us_(clock->TimeInMicroseconds()),
66 last_send_time_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070067 first_sent_packet_ms_(-1),
Sebastian Jansson2560e2e2018-10-11 20:17:22 +020068 packets_(clock->TimeInMicroseconds()),
sprang89c4a7e2017-06-30 13:27:40 -070069 packet_counter_(0),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000070 pacing_factor_(kDefaultPaceMultiplier),
Alex Narest78609d52017-10-20 10:37:47 +020071 queue_time_limit(kMaxQueueLengthMs),
72 account_for_audio_(false) {
Sebastian Jansson0601d682018-06-25 19:23:05 +020073 if (!drain_large_queues_)
74 RTC_LOG(LS_WARNING) << "Pacer queues will not be drained,"
75 "pushback experiment must be enabled.";
isheriff31687812016-10-04 08:43:09 -070076 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000077}
78
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +000079PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000080
philipelfd58b612017-01-04 07:05:25 -080081void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -070082 rtc::CritScope cs(&critsect_);
Sebastian Jansson03914462018-10-11 20:22:03 +020083 prober_.CreateProbeCluster(bitrate_bps, TimeMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +020084}
85
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000086void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -070087 {
kthelgason6bfe49c2017-03-30 01:14:41 -070088 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -070089 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +010090 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -070091 paused_ = true;
Sebastian Jansson60570dc2018-09-13 17:11:06 +020092 packets_.SetPauseState(true, TimeMilliseconds());
tommi919dce22017-03-15 07:45:36 -070093 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010094 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -070095 // Tell the process thread to call our TimeUntilNextProcess() method to get
96 // a new (longer) estimate for when to call Process().
97 if (process_thread_)
98 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000099}
100
101void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700102 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700103 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700104 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100105 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700106 paused_ = false;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200107 packets_.SetPauseState(false, TimeMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700108 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100109 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700110 // Tell the process thread to call our TimeUntilNextProcess() method to
111 // refresh the estimate for when to call Process().
112 if (process_thread_)
113 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000114}
115
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100116void PacedSender::SetCongestionWindow(int64_t congestion_window_bytes) {
117 rtc::CritScope cs(&critsect_);
118 congestion_window_bytes_ = congestion_window_bytes;
119}
120
121void PacedSender::UpdateOutstandingData(int64_t outstanding_bytes) {
122 rtc::CritScope cs(&critsect_);
123 outstanding_bytes_ = outstanding_bytes;
124}
125
126bool PacedSender::Congested() const {
127 if (congestion_window_bytes_ == kNoCongestionWindow)
128 return false;
129 return outstanding_bytes_ >= congestion_window_bytes_;
130}
131
Erik Språng96816752018-09-04 18:40:19 +0200132int64_t PacedSender::TimeMilliseconds() const {
133 int64_t time_ms = clock_->TimeInMilliseconds();
134 if (time_ms < last_timestamp_ms_) {
135 RTC_LOG(LS_WARNING)
136 << "Non-monotonic clock behavior observed. Previous timestamp: "
137 << last_timestamp_ms_ << ", new timestamp: " << time_ms;
138 RTC_DCHECK_GE(time_ms, last_timestamp_ms_);
139 time_ms = last_timestamp_ms_;
140 }
141 last_timestamp_ms_ = time_ms;
142 return time_ms;
143}
144
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000145void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700146 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200147 RTC_CHECK_EQ(0, packet_counter_);
Sebastian Jansson03914462018-10-11 20:22:03 +0200148 prober_.SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000149}
150
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000151void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) {
152 if (bitrate_bps == 0)
153 RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate.";
kthelgason6bfe49c2017-03-30 01:14:41 -0700154 rtc::CritScope cs(&critsect_);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000155 estimated_bitrate_bps_ = bitrate_bps;
Sebastian Jansson03914462018-10-11 20:22:03 +0200156 padding_budget_.set_target_rate_kbps(
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000157 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
158 pacing_bitrate_kbps_ =
159 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
160 pacing_factor_;
Bjorn Tereliusa956d492018-12-07 17:45:41 +0100161 if (!alr_detector_)
162 alr_detector_ = absl::make_unique<AlrDetector>(nullptr /*event_log*/);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000163 alr_detector_->SetEstimatedBitrate(bitrate_bps);
164}
165
166void PacedSender::SetSendBitrateLimits(int min_send_bitrate_bps,
167 int padding_bitrate) {
168 rtc::CritScope cs(&critsect_);
169 min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000;
170 pacing_bitrate_kbps_ =
171 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
172 pacing_factor_;
173 max_padding_bitrate_kbps_ = padding_bitrate / 1000;
Sebastian Jansson03914462018-10-11 20:22:03 +0200174 padding_budget_.set_target_rate_kbps(
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000175 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000176}
177
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100178void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
179 uint32_t padding_rate_bps) {
180 rtc::CritScope cs(&critsect_);
181 RTC_DCHECK(pacing_rate_bps > 0);
182 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
Sebastian Jansson03914462018-10-11 20:22:03 +0200183 padding_budget_.set_target_rate_kbps(padding_rate_bps / 1000);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100184}
185
Peter Boströme23e7372015-10-08 11:44:14 +0200186void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
187 uint32_t ssrc,
188 uint16_t sequence_number,
189 int64_t capture_time_ms,
190 size_t bytes,
191 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700192 rtc::CritScope cs(&critsect_);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100193 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
194 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000195
Erik Språng96816752018-09-04 18:40:19 +0200196 int64_t now_ms = TimeMilliseconds();
Sebastian Jansson03914462018-10-11 20:22:03 +0200197 prober_.OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100198
sprang0a43fef2015-11-20 09:00:37 -0800199 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100200 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000201
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200202 packets_.Push(RoundRobinPacketQueue::Packet(
Yves Gerey665174f2018-06-19 15:03:05 +0200203 priority, ssrc, sequence_number, capture_time_ms, now_ms, bytes,
204 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000205}
206
Alex Narest78609d52017-10-20 10:37:47 +0200207void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
208 rtc::CritScope cs(&critsect_);
209 account_for_audio_ = account_for_audio;
210}
211
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000212int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700213 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800214 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200215 return static_cast<int64_t>(packets_.SizeInBytes() * 8 /
perkjec81bcd2016-05-11 06:01:13 -0700216 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000217}
218
Bjorn Tereliusa956d492018-12-07 17:45:41 +0100219absl::optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime() {
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000220 rtc::CritScope cs(&critsect_);
Bjorn Tereliusa956d492018-12-07 17:45:41 +0100221 if (!alr_detector_)
222 alr_detector_ = absl::make_unique<AlrDetector>(nullptr /*event_log*/);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000223 return alr_detector_->GetApplicationLimitedRegionStartTime();
224}
225
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000226size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700227 rtc::CritScope cs(&critsect_);
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200228 return packets_.SizeInPackets();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000229}
230
asaperssonfc5e81c2017-04-19 23:28:53 -0700231int64_t PacedSender::FirstSentPacketTimeMs() const {
232 rtc::CritScope cs(&critsect_);
233 return first_sent_packet_ms_;
234}
235
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000236int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700237 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000238
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200239 int64_t oldest_packet = packets_.OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000240 if (oldest_packet == 0)
241 return 0;
242
Erik Språng96816752018-09-04 18:40:19 +0200243 return TimeMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000244}
245
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000246int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700247 rtc::CritScope cs(&critsect_);
Sebastian Janssona1630f82018-02-21 13:39:26 +0100248 int64_t elapsed_time_us =
249 clock_->TimeInMicroseconds() - time_last_process_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700250 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
251 // When paused we wake up every 500 ms to send a padding packet to ensure
252 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700253 if (paused_)
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100254 return std::max<int64_t>(kPausedProcessIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700255
Sebastian Jansson03914462018-10-11 20:22:03 +0200256 if (prober_.IsProbing()) {
257 int64_t ret = prober_.TimeUntilNextProbe(TimeMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800258 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000259 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000260 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000261 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000262}
263
Sebastian Jansson916ae082018-10-26 13:10:23 +0200264int64_t PacedSender::UpdateTimeAndGetElapsedMs(int64_t now_us) {
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200265 int64_t elapsed_time_ms = (now_us - time_last_process_us_ + 500) / 1000;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100266 time_last_process_us_ = now_us;
Sebastian Janssone5d8c572018-02-28 08:53:06 +0100267 if (elapsed_time_ms > kMaxElapsedTimeMs) {
268 RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time_ms
269 << " ms) longer than expected, limiting to "
270 << kMaxElapsedTimeMs << " ms";
271 elapsed_time_ms = kMaxElapsedTimeMs;
272 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200273 return elapsed_time_ms;
274}
275
276bool PacedSender::ShouldSendKeepalive(int64_t now_us) const {
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200277 if (send_padding_if_silent_ || paused_ || Congested()) {
278 // We send a padding packet every 500 ms to ensure we won't get stuck in
279 // congested state due to no feedback being received.
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200280 int64_t elapsed_since_last_send_us = now_us - last_send_time_us_;
281 if (elapsed_since_last_send_us >= kCongestedPacketIntervalMs * 1000) {
Sebastian Jansson682c6492018-04-12 13:08:22 +0200282 // We can not send padding unless a normal packet has first been sent. If
283 // we do, timestamps get messed up.
284 if (packet_counter_ > 0) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200285 return true;
Sebastian Jansson682c6492018-04-12 13:08:22 +0200286 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100287 }
stefan9e117c5e12017-08-16 08:16:25 -0700288 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200289 return false;
290}
291
292void PacedSender::Process() {
293 rtc::CritScope cs(&critsect_);
294 int64_t now_us = clock_->TimeInMicroseconds();
295 int64_t elapsed_time_ms = UpdateTimeAndGetElapsedMs(now_us);
296 if (ShouldSendKeepalive(now_us)) {
297 critsect_.Leave();
298 size_t bytes_sent = packet_sender_->TimeToSendPadding(1, PacedPacketInfo());
299 critsect_.Enter();
300 OnPaddingSent(bytes_sent);
Bjorn Tereliusa956d492018-12-07 17:45:41 +0100301 if (alr_detector_)
302 alr_detector_->OnBytesSent(bytes_sent, now_us / 1000);
Sebastian Jansson916ae082018-10-26 13:10:23 +0200303 }
304
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200305 if (paused_)
306 return;
stefan9e117c5e12017-08-16 08:16:25 -0700307
308 if (elapsed_time_ms > 0) {
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200309 int target_bitrate_kbps = pacing_bitrate_kbps_;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200310 size_t queue_size_bytes = packets_.SizeInBytes();
sprang0a43fef2015-11-20 09:00:37 -0800311 if (queue_size_bytes > 0) {
312 // Assuming equal size packets and input/output rate, the average packet
313 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
314 // time constraint shall be met. Determine bitrate needed for that.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200315 packets_.UpdateQueueTime(TimeMilliseconds());
Sebastian Jansson0601d682018-06-25 19:23:05 +0200316 if (drain_large_queues_) {
317 int64_t avg_time_left_ms = std::max<int64_t>(
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200318 1, queue_time_limit - packets_.AverageQueueTimeMs());
Sebastian Jansson0601d682018-06-25 19:23:05 +0200319 int min_bitrate_needed_kbps =
320 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
321 if (min_bitrate_needed_kbps > target_bitrate_kbps)
322 target_bitrate_kbps = min_bitrate_needed_kbps;
323 }
sprang0a43fef2015-11-20 09:00:37 -0800324 }
325
Sebastian Jansson03914462018-10-11 20:22:03 +0200326 media_budget_.set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700327 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000328 }
philipela1ed0b32016-06-01 06:31:17 -0700329
Sebastian Jansson03914462018-10-11 20:22:03 +0200330 bool is_probing = prober_.IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800331 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700332 size_t bytes_sent = 0;
333 size_t recommended_probe_size = 0;
334 if (is_probing) {
Sebastian Jansson03914462018-10-11 20:22:03 +0200335 pacing_info = prober_.CurrentCluster();
336 recommended_probe_size = prober_.RecommendedMinProbeSize();
isheriffcc5903e2016-10-04 08:29:38 -0700337 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200338 // The paused state is checked in the loop since it leaves the critical
339 // section allowing the paused state to be changed from other code.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200340 while (!packets_.Empty() && !paused_) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200341 const auto* packet = GetPendingPacket(pacing_info);
342 if (packet == nullptr)
343 break;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100344
Sebastian Jansson916ae082018-10-26 13:10:23 +0200345 critsect_.Leave();
346 bool success = packet_sender_->TimeToSendPacket(
347 packet->ssrc, packet->sequence_number, packet->capture_time_ms,
348 packet->retransmission, pacing_info);
349 critsect_.Enter();
350 if (success) {
351 bytes_sent += packet->bytes;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100352 // Send succeeded, remove it from the queue.
Sebastian Jansson916ae082018-10-26 13:10:23 +0200353 OnPacketSent(std::move(packet));
isheriffcc5903e2016-10-04 08:29:38 -0700354 if (is_probing && bytes_sent > recommended_probe_size)
355 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200356 } else {
357 // Send failed, put it back into the queue.
Sebastian Jansson916ae082018-10-26 13:10:23 +0200358 packets_.CancelPop(*packet);
isheriffcc5903e2016-10-04 08:29:38 -0700359 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200360 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000361 }
Peter Boströme23e7372015-10-08 11:44:14 +0200362
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200363 if (packets_.Empty() && !Congested()) {
isheriffcc5903e2016-10-04 08:29:38 -0700364 // We can not send padding unless a normal packet has first been sent. If we
365 // do, timestamps get messed up.
366 if (packet_counter_ > 0) {
367 int padding_needed =
368 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
Sebastian Jansson03914462018-10-11 20:22:03 +0200369 : padding_budget_.bytes_remaining());
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100370 if (padding_needed > 0) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200371 critsect_.Leave();
372 size_t padding_sent =
373 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
374 critsect_.Enter();
375 bytes_sent += padding_sent;
376 OnPaddingSent(padding_sent);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100377 }
isheriffcc5903e2016-10-04 08:29:38 -0700378 }
perkj71ee44c2016-06-15 00:47:53 -0700379 }
philipelb61927c2017-02-28 07:05:23 -0800380 if (is_probing) {
381 probing_send_failure_ = bytes_sent == 0;
382 if (!probing_send_failure_)
Sebastian Jansson03914462018-10-11 20:22:03 +0200383 prober_.ProbeSent(TimeMilliseconds(), bytes_sent);
philipelb61927c2017-02-28 07:05:23 -0800384 }
Bjorn Tereliusa956d492018-12-07 17:45:41 +0100385 if (alr_detector_)
386 alr_detector_->OnBytesSent(bytes_sent, now_us / 1000);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000387}
388
tommi919dce22017-03-15 07:45:36 -0700389void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Karl Wiberg43432732018-05-23 11:13:31 +0200390 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << process_thread;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100391 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700392 process_thread_ = process_thread;
393}
394
Sebastian Jansson916ae082018-10-26 13:10:23 +0200395const RoundRobinPacketQueue::Packet* PacedSender::GetPendingPacket(
396 const PacedPacketInfo& pacing_info) {
397 // Since we need to release the lock in order to send, we first pop the
398 // element from the priority queue but keep it in storage, so that we can
399 // reinsert it if send fails.
400 const RoundRobinPacketQueue::Packet* packet = &packets_.BeginPop();
401 bool audio_packet = packet->priority == kHighPriority;
Sebastian Janssonce4829a2018-06-15 14:47:35 +0200402 bool apply_pacing =
403 !audio_packet || account_for_audio_ || video_blocks_audio_;
Sebastian Jansson03914462018-10-11 20:22:03 +0200404 if (apply_pacing && (Congested() || (media_budget_.bytes_remaining() == 0 &&
Sebastian Janssonce4829a2018-06-15 14:47:35 +0200405 pacing_info.probe_cluster_id ==
406 PacedPacketInfo::kNotAProbe))) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200407 packets_.CancelPop(*packet);
408 return nullptr;
terelius8b70faf2016-08-01 09:47:31 -0700409 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200410 return packet;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000411}
412
Sebastian Jansson916ae082018-10-26 13:10:23 +0200413void PacedSender::OnPacketSent(const RoundRobinPacketQueue::Packet* packet) {
414 if (first_sent_packet_ms_ == -1)
415 first_sent_packet_ms_ = TimeMilliseconds();
416 bool audio_packet = packet->priority == kHighPriority;
417 if (!audio_packet || account_for_audio_) {
418 // Update media bytes sent.
419 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
420 // situations where nothing actually ended up being sent to the network,
421 // and we probably don't want to update the budget in such cases.
422 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
423 UpdateBudgetWithBytesSent(packet->bytes);
424 last_send_time_us_ = clock_->TimeInMicroseconds();
425 }
426 // Send succeeded, remove it from the queue.
427 packets_.FinalizePop(*packet);
428}
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000429
Sebastian Jansson916ae082018-10-26 13:10:23 +0200430void PacedSender::OnPaddingSent(size_t bytes_sent) {
Stefan Holmer01b48882015-05-05 10:21:24 +0200431 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700432 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200433 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200434 last_send_time_us_ = clock_->TimeInMicroseconds();
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000435}
436
isheriff31687812016-10-04 08:43:09 -0700437void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100438 delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms);
Sebastian Jansson03914462018-10-11 20:22:03 +0200439 media_budget_.IncreaseBudget(delta_time_ms);
440 padding_budget_.IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000441}
isheriff31687812016-10-04 08:43:09 -0700442
443void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100444 outstanding_bytes_ += bytes_sent;
Sebastian Jansson03914462018-10-11 20:22:03 +0200445 media_budget_.UseBudget(bytes_sent);
446 padding_budget_.UseBudget(bytes_sent);
isheriff31687812016-10-04 08:43:09 -0700447}
sprang89c4a7e2017-06-30 13:27:40 -0700448
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000449void PacedSender::SetPacingFactor(float pacing_factor) {
450 rtc::CritScope cs(&critsect_);
451 pacing_factor_ = pacing_factor;
452 // Make sure new padding factor is applied immediately, otherwise we need to
453 // wait for the send bitrate estimate to be updated before this takes effect.
454 SetEstimatedBitrate(estimated_bitrate_bps_);
455}
456
sprang89c4a7e2017-06-30 13:27:40 -0700457void PacedSender::SetQueueTimeLimit(int limit_ms) {
458 rtc::CritScope cs(&critsect_);
459 queue_time_limit = limit_ms;
460}
461
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000462} // namespace webrtc