blob: 0862d1d01e07cac8fd04e09081e0230f3c4cfe9d [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>
pbos@webrtc.org03c817e2014-07-07 10:20:35 +000014#include <map>
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +000015#include <queue>
pbos@webrtc.org03c817e2014-07-07 10:20:35 +000016#include <set>
Niels Möller9d4af012017-10-31 09:54:50 +010017#include <utility>
Yves Gerey665174f2018-06-19 15:03:05 +020018#include <vector>
pbos@webrtc.org03c817e2014-07-07 10:20:35 +000019
Karl Wiberg918f50c2018-07-05 11:40:33 +020020#include "absl/memory/memory.h"
Sebastian Janssonf9f49a32018-06-25 17:56:08 +020021#include "modules/congestion_controller/goog_cc/alr_detector.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/pacing/bitrate_prober.h"
24#include "modules/pacing/interval_budget.h"
25#include "modules/utility/include/process_thread.h"
26#include "rtc_base/checks.h"
27#include "rtc_base/logging.h"
28#include "system_wrappers/include/clock.h"
29#include "system_wrappers/include/field_trial.h"
Ilya Nikolaevskiy2ffe3e82018-01-17 19:57:24 +000030#include "system_wrappers/include/runtime_enabled_features.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000031
32namespace {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000033// Time limit in milliseconds between packet bursts.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000034const int64_t kMinPacketLimitMs = 5;
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010035const int64_t kCongestedPacketIntervalMs = 500;
36const int64_t kPausedProcessIntervalMs = kCongestedPacketIntervalMs;
Sebastian Janssone5d8c572018-02-28 08:53:06 +010037const int64_t kMaxElapsedTimeMs = 2000;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000038
39// Upper cap on process interval, in case process has not been called in a long
40// time.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000041const int64_t kMaxIntervalTimeMs = 30;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000042
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000043} // namespace
44
45namespace webrtc {
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000046
sprang0a43fef2015-11-20 09:00:37 -080047const int64_t PacedSender::kMaxQueueLengthMs = 2000;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000048const float PacedSender::kDefaultPaceMultiplier = 2.5f;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000049
philipelc3b3f7a2017-03-29 01:23:13 -070050PacedSender::PacedSender(const Clock* clock,
51 PacketSender* packet_sender,
Sebastian Janssonb5374962018-02-07 13:26:38 +010052 RtcEventLog* event_log)
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000053 : clock_(clock),
perkjec81bcd2016-05-11 06:01:13 -070054 packet_sender_(packet_sender),
Karl Wiberg918f50c2018-07-05 11:40:33 +020055 alr_detector_(absl::make_unique<AlrDetector>(event_log)),
Sebastian Jansson0601d682018-06-25 19:23:05 +020056 drain_large_queues_(!field_trial::IsDisabled("WebRTC-Pacer-DrainQueue")),
Sebastian Janssonc235a8d2018-06-15 14:46:11 +020057 send_padding_if_silent_(
58 field_trial::IsEnabled("WebRTC-Pacer-PadInSilence")),
Sebastian Janssonce4829a2018-06-15 14:47:35 +020059 video_blocks_audio_(!field_trial::IsDisabled("WebRTC-Pacer-BlockAudio")),
Erik Språng96816752018-09-04 18:40:19 +020060 last_timestamp_ms_(clock_->TimeInMilliseconds()),
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000061 paused_(false),
Karl Wiberg918f50c2018-07-05 11:40:33 +020062 media_budget_(absl::make_unique<IntervalBudget>(0)),
63 padding_budget_(absl::make_unique<IntervalBudget>(0)),
64 prober_(absl::make_unique<BitrateProber>(event_log)),
philipelb61927c2017-02-28 07:05:23 -080065 probing_send_failure_(false),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000066 estimated_bitrate_bps_(0),
67 min_send_bitrate_kbps_(0u),
68 max_padding_bitrate_kbps_(0u),
perkjec81bcd2016-05-11 06:01:13 -070069 pacing_bitrate_kbps_(0),
Sebastian Janssona1630f82018-02-21 13:39:26 +010070 time_last_process_us_(clock->TimeInMicroseconds()),
71 last_send_time_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070072 first_sent_packet_ms_(-1),
Sebastian Jansson60570dc2018-09-13 17:11:06 +020073 packets_(clock),
sprang89c4a7e2017-06-30 13:27:40 -070074 packet_counter_(0),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000075 pacing_factor_(kDefaultPaceMultiplier),
Alex Narest78609d52017-10-20 10:37:47 +020076 queue_time_limit(kMaxQueueLengthMs),
77 account_for_audio_(false) {
Sebastian Jansson0601d682018-06-25 19:23:05 +020078 if (!drain_large_queues_)
79 RTC_LOG(LS_WARNING) << "Pacer queues will not be drained,"
80 "pushback experiment must be enabled.";
isheriff31687812016-10-04 08:43:09 -070081 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000082}
83
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +000084PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000085
philipelfd58b612017-01-04 07:05:25 -080086void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -070087 rtc::CritScope cs(&critsect_);
Erik Språng96816752018-09-04 18:40:19 +020088 prober_->CreateProbeCluster(bitrate_bps, TimeMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +020089}
90
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000091void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -070092 {
kthelgason6bfe49c2017-03-30 01:14:41 -070093 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -070094 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +010095 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -070096 paused_ = true;
Sebastian Jansson60570dc2018-09-13 17:11:06 +020097 packets_.SetPauseState(true, TimeMilliseconds());
tommi919dce22017-03-15 07:45:36 -070098 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +010099 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700100 // Tell the process thread to call our TimeUntilNextProcess() method to get
101 // a new (longer) estimate for when to call Process().
102 if (process_thread_)
103 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000104}
105
106void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700107 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700108 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700109 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100110 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700111 paused_ = false;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200112 packets_.SetPauseState(false, TimeMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700113 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100114 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700115 // Tell the process thread to call our TimeUntilNextProcess() method to
116 // refresh the estimate for when to call Process().
117 if (process_thread_)
118 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000119}
120
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100121void PacedSender::SetCongestionWindow(int64_t congestion_window_bytes) {
122 rtc::CritScope cs(&critsect_);
123 congestion_window_bytes_ = congestion_window_bytes;
124}
125
126void PacedSender::UpdateOutstandingData(int64_t outstanding_bytes) {
127 rtc::CritScope cs(&critsect_);
128 outstanding_bytes_ = outstanding_bytes;
129}
130
131bool PacedSender::Congested() const {
132 if (congestion_window_bytes_ == kNoCongestionWindow)
133 return false;
134 return outstanding_bytes_ >= congestion_window_bytes_;
135}
136
Erik Språng96816752018-09-04 18:40:19 +0200137int64_t PacedSender::TimeMilliseconds() const {
138 int64_t time_ms = clock_->TimeInMilliseconds();
139 if (time_ms < last_timestamp_ms_) {
140 RTC_LOG(LS_WARNING)
141 << "Non-monotonic clock behavior observed. Previous timestamp: "
142 << last_timestamp_ms_ << ", new timestamp: " << time_ms;
143 RTC_DCHECK_GE(time_ms, last_timestamp_ms_);
144 time_ms = last_timestamp_ms_;
145 }
146 last_timestamp_ms_ = time_ms;
147 return time_ms;
148}
149
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000150void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700151 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200152 RTC_CHECK_EQ(0, packet_counter_);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700153 prober_->SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000154}
155
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000156void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) {
157 if (bitrate_bps == 0)
158 RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate.";
kthelgason6bfe49c2017-03-30 01:14:41 -0700159 rtc::CritScope cs(&critsect_);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000160 estimated_bitrate_bps_ = bitrate_bps;
161 padding_budget_->set_target_rate_kbps(
162 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
163 pacing_bitrate_kbps_ =
164 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
165 pacing_factor_;
166 alr_detector_->SetEstimatedBitrate(bitrate_bps);
167}
168
169void PacedSender::SetSendBitrateLimits(int min_send_bitrate_bps,
170 int padding_bitrate) {
171 rtc::CritScope cs(&critsect_);
172 min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000;
173 pacing_bitrate_kbps_ =
174 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
175 pacing_factor_;
176 max_padding_bitrate_kbps_ = padding_bitrate / 1000;
177 padding_budget_->set_target_rate_kbps(
178 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000179}
180
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100181void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
182 uint32_t padding_rate_bps) {
183 rtc::CritScope cs(&critsect_);
184 RTC_DCHECK(pacing_rate_bps > 0);
185 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
186 padding_budget_->set_target_rate_kbps(padding_rate_bps / 1000);
187}
188
Peter Boströme23e7372015-10-08 11:44:14 +0200189void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
190 uint32_t ssrc,
191 uint16_t sequence_number,
192 int64_t capture_time_ms,
193 size_t bytes,
194 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700195 rtc::CritScope cs(&critsect_);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100196 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
197 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000198
Erik Språng96816752018-09-04 18:40:19 +0200199 int64_t now_ms = TimeMilliseconds();
philipel4a1ec1e2016-08-15 11:51:06 -0700200 prober_->OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100201
sprang0a43fef2015-11-20 09:00:37 -0800202 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100203 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000204
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200205 packets_.Push(RoundRobinPacketQueue::Packet(
Yves Gerey665174f2018-06-19 15:03:05 +0200206 priority, ssrc, sequence_number, capture_time_ms, now_ms, bytes,
207 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000208}
209
Alex Narest78609d52017-10-20 10:37:47 +0200210void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
211 rtc::CritScope cs(&critsect_);
212 account_for_audio_ = account_for_audio;
213}
214
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000215int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700216 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800217 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200218 return static_cast<int64_t>(packets_.SizeInBytes() * 8 /
perkjec81bcd2016-05-11 06:01:13 -0700219 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000220}
221
Danil Chapovalov0040b662018-06-18 10:48:16 +0200222absl::optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime()
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000223 const {
224 rtc::CritScope cs(&critsect_);
225 return alr_detector_->GetApplicationLimitedRegionStartTime();
226}
227
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000228size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700229 rtc::CritScope cs(&critsect_);
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200230 return packets_.SizeInPackets();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000231}
232
asaperssonfc5e81c2017-04-19 23:28:53 -0700233int64_t PacedSender::FirstSentPacketTimeMs() const {
234 rtc::CritScope cs(&critsect_);
235 return first_sent_packet_ms_;
236}
237
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000238int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700239 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000240
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200241 int64_t oldest_packet = packets_.OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000242 if (oldest_packet == 0)
243 return 0;
244
Erik Språng96816752018-09-04 18:40:19 +0200245 return TimeMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000246}
247
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000248int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700249 rtc::CritScope cs(&critsect_);
Sebastian Janssona1630f82018-02-21 13:39:26 +0100250 int64_t elapsed_time_us =
251 clock_->TimeInMicroseconds() - time_last_process_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700252 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
253 // When paused we wake up every 500 ms to send a padding packet to ensure
254 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700255 if (paused_)
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100256 return std::max<int64_t>(kPausedProcessIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700257
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000258 if (prober_->IsProbing()) {
Erik Språng96816752018-09-04 18:40:19 +0200259 int64_t ret = prober_->TimeUntilNextProbe(TimeMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800260 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000261 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000262 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000263 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000264}
265
pbosa26ac922016-02-25 04:50:01 -0800266void PacedSender::Process() {
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000267 int64_t now_us = clock_->TimeInMicroseconds();
kthelgason6bfe49c2017-03-30 01:14:41 -0700268 rtc::CritScope cs(&critsect_);
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200269 int64_t elapsed_time_ms = (now_us - time_last_process_us_ + 500) / 1000;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100270 time_last_process_us_ = now_us;
Sebastian Janssone5d8c572018-02-28 08:53:06 +0100271 if (elapsed_time_ms > kMaxElapsedTimeMs) {
272 RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time_ms
273 << " ms) longer than expected, limiting to "
274 << kMaxElapsedTimeMs << " ms";
275 elapsed_time_ms = kMaxElapsedTimeMs;
276 }
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) {
285 PacedPacketInfo pacing_info;
286 size_t bytes_sent = SendPadding(1, pacing_info);
Sebastian Janssonf9f49a32018-06-25 17:56:08 +0200287 alr_detector_->OnBytesSent(bytes_sent, now_us / 1000);
Sebastian Jansson682c6492018-04-12 13:08:22 +0200288 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100289 }
stefan9e117c5e12017-08-16 08:16:25 -0700290 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200291 if (paused_)
292 return;
stefan9e117c5e12017-08-16 08:16:25 -0700293
294 if (elapsed_time_ms > 0) {
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200295 int target_bitrate_kbps = pacing_bitrate_kbps_;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200296 size_t queue_size_bytes = packets_.SizeInBytes();
sprang0a43fef2015-11-20 09:00:37 -0800297 if (queue_size_bytes > 0) {
298 // Assuming equal size packets and input/output rate, the average packet
299 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
300 // time constraint shall be met. Determine bitrate needed for that.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200301 packets_.UpdateQueueTime(TimeMilliseconds());
Sebastian Jansson0601d682018-06-25 19:23:05 +0200302 if (drain_large_queues_) {
303 int64_t avg_time_left_ms = std::max<int64_t>(
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200304 1, queue_time_limit - packets_.AverageQueueTimeMs());
Sebastian Jansson0601d682018-06-25 19:23:05 +0200305 int min_bitrate_needed_kbps =
306 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
307 if (min_bitrate_needed_kbps > target_bitrate_kbps)
308 target_bitrate_kbps = min_bitrate_needed_kbps;
309 }
sprang0a43fef2015-11-20 09:00:37 -0800310 }
311
312 media_budget_->set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700313 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000314 }
philipela1ed0b32016-06-01 06:31:17 -0700315
philipel1a93cde2016-06-03 01:41:45 -0700316 bool is_probing = prober_->IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800317 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700318 size_t bytes_sent = 0;
319 size_t recommended_probe_size = 0;
320 if (is_probing) {
philipelc7bf32a2017-02-17 03:59:43 -0800321 pacing_info = prober_->CurrentCluster();
isheriffcc5903e2016-10-04 08:29:38 -0700322 recommended_probe_size = prober_->RecommendedMinProbeSize();
323 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100324 // The paused state is checked in the loop since SendPacket leaves the
325 // critical section allowing the paused state to be changed from other code.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200326 while (!packets_.Empty() && !paused_) {
Peter Boströme23e7372015-10-08 11:44:14 +0200327 // Since we need to release the lock in order to send, we first pop the
328 // element from the priority queue but keep it in storage, so that we can
329 // reinsert it if send fails.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200330 const RoundRobinPacketQueue::Packet& packet = packets_.BeginPop();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100331
philipelc7bf32a2017-02-17 03:59:43 -0800332 if (SendPacket(packet, pacing_info)) {
Danil Chapovalov516036f2018-02-14 10:21:27 +0000333 bytes_sent += packet.bytes;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100334 // Send succeeded, remove it from the queue.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200335 packets_.FinalizePop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700336 if (is_probing && bytes_sent > recommended_probe_size)
337 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200338 } else {
339 // Send failed, put it back into the queue.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200340 packets_.CancelPop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700341 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200342 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000343 }
Peter Boströme23e7372015-10-08 11:44:14 +0200344
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200345 if (packets_.Empty() && !Congested()) {
isheriffcc5903e2016-10-04 08:29:38 -0700346 // We can not send padding unless a normal packet has first been sent. If we
347 // do, timestamps get messed up.
348 if (packet_counter_ > 0) {
349 int padding_needed =
350 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
351 : padding_budget_->bytes_remaining());
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100352 if (padding_needed > 0) {
philipelc7bf32a2017-02-17 03:59:43 -0800353 bytes_sent += SendPadding(padding_needed, pacing_info);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100354 }
isheriffcc5903e2016-10-04 08:29:38 -0700355 }
perkj71ee44c2016-06-15 00:47:53 -0700356 }
philipelb61927c2017-02-28 07:05:23 -0800357 if (is_probing) {
358 probing_send_failure_ = bytes_sent == 0;
359 if (!probing_send_failure_)
Erik Språng96816752018-09-04 18:40:19 +0200360 prober_->ProbeSent(TimeMilliseconds(), bytes_sent);
philipelb61927c2017-02-28 07:05:23 -0800361 }
Sebastian Janssonf9f49a32018-06-25 17:56:08 +0200362 alr_detector_->OnBytesSent(bytes_sent, now_us / 1000);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000363}
364
tommi919dce22017-03-15 07:45:36 -0700365void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Karl Wiberg43432732018-05-23 11:13:31 +0200366 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << process_thread;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100367 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700368 process_thread_ = process_thread;
369}
370
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200371bool PacedSender::SendPacket(const RoundRobinPacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800372 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700373 RTC_DCHECK(!paused_);
Sebastian Janssonce4829a2018-06-15 14:47:35 +0200374 bool audio_packet = packet.priority == kHighPriority;
375 bool apply_pacing =
376 !audio_packet || account_for_audio_ || video_blocks_audio_;
377 if (apply_pacing && (Congested() || (media_budget_->bytes_remaining() == 0 &&
378 pacing_info.probe_cluster_id ==
379 PacedPacketInfo::kNotAProbe))) {
stefan099110c2017-02-01 03:57:42 -0800380 return false;
terelius8b70faf2016-08-01 09:47:31 -0700381 }
philipelc7bf32a2017-02-17 03:59:43 -0800382
kthelgason6bfe49c2017-03-30 01:14:41 -0700383 critsect_.Leave();
perkjec81bcd2016-05-11 06:01:13 -0700384 const bool success = packet_sender_->TimeToSendPacket(
385 packet.ssrc, packet.sequence_number, packet.capture_time_ms,
philipelc7bf32a2017-02-17 03:59:43 -0800386 packet.retransmission, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700387 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000388
Peter Boström7ecc1632016-03-02 14:22:25 +0100389 if (success) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100390 if (first_sent_packet_ms_ == -1)
Erik Språng96816752018-09-04 18:40:19 +0200391 first_sent_packet_ms_ = TimeMilliseconds();
Sebastian Janssonce4829a2018-06-15 14:47:35 +0200392 if (!audio_packet || account_for_audio_) {
Peter Boström7ecc1632016-03-02 14:22:25 +0100393 // Update media bytes sent.
eladalon32040ef2017-08-02 06:29:00 -0700394 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
395 // situations where nothing actually ended up being sent to the network,
396 // and we probably don't want to update the budget in such cases.
397 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
isheriff31687812016-10-04 08:43:09 -0700398 UpdateBudgetWithBytesSent(packet.bytes);
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200399 last_send_time_us_ = clock_->TimeInMicroseconds();
Peter Boström7ecc1632016-03-02 14:22:25 +0100400 }
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000401 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000402
403 return success;
404}
405
philipelc7bf32a2017-02-17 03:59:43 -0800406size_t PacedSender::SendPadding(size_t padding_needed,
407 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700408 RTC_DCHECK_GT(packet_counter_, 0);
kthelgason6bfe49c2017-03-30 01:14:41 -0700409 critsect_.Leave();
philipela1ed0b32016-06-01 06:31:17 -0700410 size_t bytes_sent =
philipelc7bf32a2017-02-17 03:59:43 -0800411 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700412 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000413
Stefan Holmer01b48882015-05-05 10:21:24 +0200414 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700415 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200416 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200417 last_send_time_us_ = clock_->TimeInMicroseconds();
isheriffcc5903e2016-10-04 08:29:38 -0700418 return bytes_sent;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000419}
420
isheriff31687812016-10-04 08:43:09 -0700421void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100422 delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000423 media_budget_->IncreaseBudget(delta_time_ms);
424 padding_budget_->IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000425}
isheriff31687812016-10-04 08:43:09 -0700426
427void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100428 outstanding_bytes_ += bytes_sent;
isheriff31687812016-10-04 08:43:09 -0700429 media_budget_->UseBudget(bytes_sent);
430 padding_budget_->UseBudget(bytes_sent);
431}
sprang89c4a7e2017-06-30 13:27:40 -0700432
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000433void PacedSender::SetPacingFactor(float pacing_factor) {
434 rtc::CritScope cs(&critsect_);
435 pacing_factor_ = pacing_factor;
436 // Make sure new padding factor is applied immediately, otherwise we need to
437 // wait for the send bitrate estimate to be updated before this takes effect.
438 SetEstimatedBitrate(estimated_bitrate_bps_);
439}
440
sprang89c4a7e2017-06-30 13:27:40 -0700441void PacedSender::SetQueueTimeLimit(int limit_ms) {
442 rtc::CritScope cs(&critsect_);
443 queue_time_limit = limit_ms;
444}
445
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000446} // namespace webrtc