blob: 883c33fbbe4917b9c11326a08620ebf8058f68aa [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>
jbauchd2a22962016-02-08 23:18:25 -080017#include <vector>
Niels Möller9d4af012017-10-31 09:54:50 +010018#include <utility>
pbos@webrtc.org03c817e2014-07-07 10:20:35 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/include/module_common_types.h"
Sebastian Janssonea86bb72018-02-14 16:53:38 +000021#include "modules/pacing/alr_detector.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/pacing/bitrate_prober.h"
23#include "modules/pacing/interval_budget.h"
Sebastian Janssonb5374962018-02-07 13:26:38 +010024#include "modules/pacing/round_robin_packet_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/utility/include/process_thread.h"
26#include "rtc_base/checks.h"
27#include "rtc_base/logging.h"
Niels Möller712048c2017-10-18 13:08:22 +020028#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "system_wrappers/include/clock.h"
30#include "system_wrappers/include/field_trial.h"
Ilya Nikolaevskiy2ffe3e82018-01-17 19:57:24 +000031#include "system_wrappers/include/runtime_enabled_features.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000032
33namespace {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000034// Time limit in milliseconds between packet bursts.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000035const int64_t kMinPacketLimitMs = 5;
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010036const int64_t kCongestedPacketIntervalMs = 500;
37const int64_t kPausedProcessIntervalMs = kCongestedPacketIntervalMs;
Sebastian Janssone5d8c572018-02-28 08:53:06 +010038const int64_t kMaxElapsedTimeMs = 2000;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000039
40// Upper cap on process interval, in case process has not been called in a long
41// time.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000042const int64_t kMaxIntervalTimeMs = 30;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000043
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000044} // namespace
45
46namespace webrtc {
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000047
sprang0a43fef2015-11-20 09:00:37 -080048const int64_t PacedSender::kMaxQueueLengthMs = 2000;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000049const float PacedSender::kDefaultPaceMultiplier = 2.5f;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000050
philipelc3b3f7a2017-03-29 01:23:13 -070051PacedSender::PacedSender(const Clock* clock,
52 PacketSender* packet_sender,
Sebastian Janssonb5374962018-02-07 13:26:38 +010053 RtcEventLog* event_log)
54 : PacedSender(clock,
55 packet_sender,
56 event_log,
Ilya Nikolaevskiy94065692018-03-02 12:32:05 +010057 rtc::MakeUnique<RoundRobinPacketQueue>(clock)) {}
Niels Möller9d4af012017-10-31 09:54:50 +010058
59PacedSender::PacedSender(const Clock* clock,
60 PacketSender* packet_sender,
61 RtcEventLog* event_log,
Sebastian Janssonb5374962018-02-07 13:26:38 +010062 std::unique_ptr<PacketQueueInterface> packets)
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000063 : clock_(clock),
perkjec81bcd2016-05-11 06:01:13 -070064 packet_sender_(packet_sender),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000065 alr_detector_(rtc::MakeUnique<AlrDetector>(event_log)),
Sebastian Janssonc235a8d2018-06-15 14:46:11 +020066 send_padding_if_silent_(
67 field_trial::IsEnabled("WebRTC-Pacer-PadInSilence")),
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000068 paused_(false),
Niels Möller712048c2017-10-18 13:08:22 +020069 media_budget_(rtc::MakeUnique<IntervalBudget>(0)),
70 padding_budget_(rtc::MakeUnique<IntervalBudget>(0)),
71 prober_(rtc::MakeUnique<BitrateProber>(event_log)),
philipelb61927c2017-02-28 07:05:23 -080072 probing_send_failure_(false),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000073 estimated_bitrate_bps_(0),
74 min_send_bitrate_kbps_(0u),
75 max_padding_bitrate_kbps_(0u),
perkjec81bcd2016-05-11 06:01:13 -070076 pacing_bitrate_kbps_(0),
Sebastian Janssona1630f82018-02-21 13:39:26 +010077 time_last_process_us_(clock->TimeInMicroseconds()),
78 last_send_time_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070079 first_sent_packet_ms_(-1),
Niels Möller9d4af012017-10-31 09:54:50 +010080 packets_(std::move(packets)),
sprang89c4a7e2017-06-30 13:27:40 -070081 packet_counter_(0),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000082 pacing_factor_(kDefaultPaceMultiplier),
Alex Narest78609d52017-10-20 10:37:47 +020083 queue_time_limit(kMaxQueueLengthMs),
84 account_for_audio_(false) {
isheriff31687812016-10-04 08:43:09 -070085 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000086}
87
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +000088PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000089
philipelfd58b612017-01-04 07:05:25 -080090void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -070091 rtc::CritScope cs(&critsect_);
Stefan Holmer0e3213a2017-02-08 15:19:05 +010092 prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +020093}
94
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000095void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -070096 {
kthelgason6bfe49c2017-03-30 01:14:41 -070097 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -070098 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +010099 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -0700100 paused_ = true;
sprangddcfb9f2017-08-16 05:38:49 -0700101 packets_->SetPauseState(true, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700102 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100103 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700104 // Tell the process thread to call our TimeUntilNextProcess() method to get
105 // a new (longer) estimate for when to call Process().
106 if (process_thread_)
107 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000108}
109
110void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700111 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700112 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700113 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100114 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700115 paused_ = false;
sprangddcfb9f2017-08-16 05:38:49 -0700116 packets_->SetPauseState(false, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700117 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100118 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700119 // Tell the process thread to call our TimeUntilNextProcess() method to
120 // refresh the estimate for when to call Process().
121 if (process_thread_)
122 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000123}
124
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100125void PacedSender::SetCongestionWindow(int64_t congestion_window_bytes) {
126 rtc::CritScope cs(&critsect_);
127 congestion_window_bytes_ = congestion_window_bytes;
128}
129
130void PacedSender::UpdateOutstandingData(int64_t outstanding_bytes) {
131 rtc::CritScope cs(&critsect_);
132 outstanding_bytes_ = outstanding_bytes;
133}
134
135bool PacedSender::Congested() const {
136 if (congestion_window_bytes_ == kNoCongestionWindow)
137 return false;
138 return outstanding_bytes_ >= congestion_window_bytes_;
139}
140
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000141void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700142 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200143 RTC_CHECK_EQ(0, packet_counter_);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700144 prober_->SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000145}
146
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000147void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) {
148 if (bitrate_bps == 0)
149 RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate.";
kthelgason6bfe49c2017-03-30 01:14:41 -0700150 rtc::CritScope cs(&critsect_);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000151 estimated_bitrate_bps_ = bitrate_bps;
152 padding_budget_->set_target_rate_kbps(
153 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
154 pacing_bitrate_kbps_ =
155 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
156 pacing_factor_;
157 alr_detector_->SetEstimatedBitrate(bitrate_bps);
158}
159
160void PacedSender::SetSendBitrateLimits(int min_send_bitrate_bps,
161 int padding_bitrate) {
162 rtc::CritScope cs(&critsect_);
163 min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000;
164 pacing_bitrate_kbps_ =
165 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
166 pacing_factor_;
167 max_padding_bitrate_kbps_ = padding_bitrate / 1000;
168 padding_budget_->set_target_rate_kbps(
169 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000170}
171
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100172void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
173 uint32_t padding_rate_bps) {
174 rtc::CritScope cs(&critsect_);
175 RTC_DCHECK(pacing_rate_bps > 0);
176 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
177 padding_budget_->set_target_rate_kbps(padding_rate_bps / 1000);
178}
179
Peter Boströme23e7372015-10-08 11:44:14 +0200180void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
181 uint32_t ssrc,
182 uint16_t sequence_number,
183 int64_t capture_time_ms,
184 size_t bytes,
185 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700186 rtc::CritScope cs(&critsect_);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100187 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
188 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000189
Erik Språngad113e52015-11-26 16:26:12 +0100190 int64_t now_ms = clock_->TimeInMilliseconds();
philipel4a1ec1e2016-08-15 11:51:06 -0700191 prober_->OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100192
sprang0a43fef2015-11-20 09:00:37 -0800193 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100194 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000195
Ilya Nikolaevskiy94065692018-03-02 12:32:05 +0100196 packets_->Push(PacketQueueInterface::Packet(priority, ssrc, sequence_number,
philipel9981bd92017-09-26 17:16:06 +0200197 capture_time_ms, now_ms, bytes,
198 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000199}
200
Alex Narest78609d52017-10-20 10:37:47 +0200201void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
202 rtc::CritScope cs(&critsect_);
203 account_for_audio_ = account_for_audio;
204}
205
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000206int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700207 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800208 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
perkjec81bcd2016-05-11 06:01:13 -0700209 return static_cast<int64_t>(packets_->SizeInBytes() * 8 /
210 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000211}
212
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000213rtc::Optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime()
214 const {
215 rtc::CritScope cs(&critsect_);
216 return alr_detector_->GetApplicationLimitedRegionStartTime();
217}
218
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000219size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700220 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000221 return packets_->SizeInPackets();
222}
223
asaperssonfc5e81c2017-04-19 23:28:53 -0700224int64_t PacedSender::FirstSentPacketTimeMs() const {
225 rtc::CritScope cs(&critsect_);
226 return first_sent_packet_ms_;
227}
228
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000229int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700230 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000231
sprang0a43fef2015-11-20 09:00:37 -0800232 int64_t oldest_packet = packets_->OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000233 if (oldest_packet == 0)
234 return 0;
235
236 return clock_->TimeInMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000237}
238
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000239int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700240 rtc::CritScope cs(&critsect_);
Sebastian Janssona1630f82018-02-21 13:39:26 +0100241 int64_t elapsed_time_us =
242 clock_->TimeInMicroseconds() - time_last_process_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700243 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
244 // When paused we wake up every 500 ms to send a padding packet to ensure
245 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700246 if (paused_)
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100247 return std::max<int64_t>(kPausedProcessIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700248
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000249 if (prober_->IsProbing()) {
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000250 int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800251 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000252 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000253 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000254 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000255}
256
pbosa26ac922016-02-25 04:50:01 -0800257void PacedSender::Process() {
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000258 int64_t now_us = clock_->TimeInMicroseconds();
kthelgason6bfe49c2017-03-30 01:14:41 -0700259 rtc::CritScope cs(&critsect_);
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200260 int64_t elapsed_time_ms = (now_us - time_last_process_us_ + 500) / 1000;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100261 time_last_process_us_ = now_us;
Sebastian Janssone5d8c572018-02-28 08:53:06 +0100262 if (elapsed_time_ms > kMaxElapsedTimeMs) {
263 RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time_ms
264 << " ms) longer than expected, limiting to "
265 << kMaxElapsedTimeMs << " ms";
266 elapsed_time_ms = kMaxElapsedTimeMs;
267 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200268 if (send_padding_if_silent_ || paused_ || Congested()) {
269 // We send a padding packet every 500 ms to ensure we won't get stuck in
270 // congested state due to no feedback being received.
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200271 int64_t elapsed_since_last_send_us = now_us - last_send_time_us_;
272 if (elapsed_since_last_send_us >= kCongestedPacketIntervalMs * 1000) {
Sebastian Jansson682c6492018-04-12 13:08:22 +0200273 // We can not send padding unless a normal packet has first been sent. If
274 // we do, timestamps get messed up.
275 if (packet_counter_ > 0) {
276 PacedPacketInfo pacing_info;
277 size_t bytes_sent = SendPadding(1, pacing_info);
278 alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms);
279 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100280 }
stefan9e117c5e12017-08-16 08:16:25 -0700281 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200282 if (paused_)
283 return;
stefan9e117c5e12017-08-16 08:16:25 -0700284
285 if (elapsed_time_ms > 0) {
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200286 int target_bitrate_kbps = pacing_bitrate_kbps_;
sprang0a43fef2015-11-20 09:00:37 -0800287 size_t queue_size_bytes = packets_->SizeInBytes();
288 if (queue_size_bytes > 0) {
289 // Assuming equal size packets and input/output rate, the average packet
290 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
291 // time constraint shall be met. Determine bitrate needed for that.
Erik Språngad113e52015-11-26 16:26:12 +0100292 packets_->UpdateQueueTime(clock_->TimeInMilliseconds());
sprang0a43fef2015-11-20 09:00:37 -0800293 int64_t avg_time_left_ms = std::max<int64_t>(
sprang89c4a7e2017-06-30 13:27:40 -0700294 1, queue_time_limit - packets_->AverageQueueTimeMs());
sprang0a43fef2015-11-20 09:00:37 -0800295 int min_bitrate_needed_kbps =
296 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
297 if (min_bitrate_needed_kbps > target_bitrate_kbps)
298 target_bitrate_kbps = min_bitrate_needed_kbps;
299 }
300
301 media_budget_->set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700302 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000303 }
philipela1ed0b32016-06-01 06:31:17 -0700304
philipel1a93cde2016-06-03 01:41:45 -0700305 bool is_probing = prober_->IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800306 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700307 size_t bytes_sent = 0;
308 size_t recommended_probe_size = 0;
309 if (is_probing) {
philipelc7bf32a2017-02-17 03:59:43 -0800310 pacing_info = prober_->CurrentCluster();
isheriffcc5903e2016-10-04 08:29:38 -0700311 recommended_probe_size = prober_->RecommendedMinProbeSize();
312 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100313 // The paused state is checked in the loop since SendPacket leaves the
314 // critical section allowing the paused state to be changed from other code.
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200315 while (!packets_->Empty() && !paused_) {
Peter Boströme23e7372015-10-08 11:44:14 +0200316 // Since we need to release the lock in order to send, we first pop the
317 // element from the priority queue but keep it in storage, so that we can
318 // reinsert it if send fails.
Ilya Nikolaevskiy94065692018-03-02 12:32:05 +0100319 const PacketQueueInterface::Packet& packet = packets_->BeginPop();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100320
philipelc7bf32a2017-02-17 03:59:43 -0800321 if (SendPacket(packet, pacing_info)) {
Danil Chapovalov516036f2018-02-14 10:21:27 +0000322 bytes_sent += packet.bytes;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100323 // Send succeeded, remove it from the queue.
Peter Boströme23e7372015-10-08 11:44:14 +0200324 packets_->FinalizePop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700325 if (is_probing && bytes_sent > recommended_probe_size)
326 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200327 } else {
328 // Send failed, put it back into the queue.
329 packets_->CancelPop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700330 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200331 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000332 }
Peter Boströme23e7372015-10-08 11:44:14 +0200333
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100334 if (packets_->Empty() && !Congested()) {
isheriffcc5903e2016-10-04 08:29:38 -0700335 // We can not send padding unless a normal packet has first been sent. If we
336 // do, timestamps get messed up.
337 if (packet_counter_ > 0) {
338 int padding_needed =
339 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
340 : padding_budget_->bytes_remaining());
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100341 if (padding_needed > 0) {
philipelc7bf32a2017-02-17 03:59:43 -0800342 bytes_sent += SendPadding(padding_needed, pacing_info);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100343 }
isheriffcc5903e2016-10-04 08:29:38 -0700344 }
perkj71ee44c2016-06-15 00:47:53 -0700345 }
philipelb61927c2017-02-28 07:05:23 -0800346 if (is_probing) {
347 probing_send_failure_ = bytes_sent == 0;
348 if (!probing_send_failure_)
349 prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent);
350 }
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000351 alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000352}
353
tommi919dce22017-03-15 07:45:36 -0700354void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Karl Wiberg43432732018-05-23 11:13:31 +0200355 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << process_thread;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100356 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700357 process_thread_ = process_thread;
358}
359
Ilya Nikolaevskiy94065692018-03-02 12:32:05 +0100360bool PacedSender::SendPacket(const PacketQueueInterface::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800361 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700362 RTC_DCHECK(!paused_);
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200363 if (Congested() ||
364 (media_budget_->bytes_remaining() == 0 &&
365 pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe)) {
stefan099110c2017-02-01 03:57:42 -0800366 return false;
terelius8b70faf2016-08-01 09:47:31 -0700367 }
philipelc7bf32a2017-02-17 03:59:43 -0800368
kthelgason6bfe49c2017-03-30 01:14:41 -0700369 critsect_.Leave();
perkjec81bcd2016-05-11 06:01:13 -0700370 const bool success = packet_sender_->TimeToSendPacket(
371 packet.ssrc, packet.sequence_number, packet.capture_time_ms,
philipelc7bf32a2017-02-17 03:59:43 -0800372 packet.retransmission, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700373 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000374
Peter Boström7ecc1632016-03-02 14:22:25 +0100375 if (success) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100376 if (first_sent_packet_ms_ == -1)
377 first_sent_packet_ms_ = clock_->TimeInMilliseconds();
Alex Narest78609d52017-10-20 10:37:47 +0200378 if (packet.priority != kHighPriority || account_for_audio_) {
Peter Boström7ecc1632016-03-02 14:22:25 +0100379 // Update media bytes sent.
eladalon32040ef2017-08-02 06:29:00 -0700380 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
381 // situations where nothing actually ended up being sent to the network,
382 // and we probably don't want to update the budget in such cases.
383 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
isheriff31687812016-10-04 08:43:09 -0700384 UpdateBudgetWithBytesSent(packet.bytes);
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200385 last_send_time_us_ = clock_->TimeInMicroseconds();
Peter Boström7ecc1632016-03-02 14:22:25 +0100386 }
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000387 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000388
389 return success;
390}
391
philipelc7bf32a2017-02-17 03:59:43 -0800392size_t PacedSender::SendPadding(size_t padding_needed,
393 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700394 RTC_DCHECK_GT(packet_counter_, 0);
kthelgason6bfe49c2017-03-30 01:14:41 -0700395 critsect_.Leave();
philipela1ed0b32016-06-01 06:31:17 -0700396 size_t bytes_sent =
philipelc7bf32a2017-02-17 03:59:43 -0800397 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700398 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000399
Stefan Holmer01b48882015-05-05 10:21:24 +0200400 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700401 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200402 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200403 last_send_time_us_ = clock_->TimeInMicroseconds();
isheriffcc5903e2016-10-04 08:29:38 -0700404 return bytes_sent;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000405}
406
isheriff31687812016-10-04 08:43:09 -0700407void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100408 delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000409 media_budget_->IncreaseBudget(delta_time_ms);
410 padding_budget_->IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000411}
isheriff31687812016-10-04 08:43:09 -0700412
413void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100414 outstanding_bytes_ += bytes_sent;
isheriff31687812016-10-04 08:43:09 -0700415 media_budget_->UseBudget(bytes_sent);
416 padding_budget_->UseBudget(bytes_sent);
417}
sprang89c4a7e2017-06-30 13:27:40 -0700418
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000419void PacedSender::SetPacingFactor(float pacing_factor) {
420 rtc::CritScope cs(&critsect_);
421 pacing_factor_ = pacing_factor;
422 // Make sure new padding factor is applied immediately, otherwise we need to
423 // wait for the send bitrate estimate to be updated before this takes effect.
424 SetEstimatedBitrate(estimated_bitrate_bps_);
425}
426
sprang89c4a7e2017-06-30 13:27:40 -0700427void PacedSender::SetQueueTimeLimit(int limit_ms) {
428 rtc::CritScope cs(&critsect_);
429 queue_time_limit = limit_ms;
430}
431
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000432} // namespace webrtc