blob: 6177ca61fbbe5098748cc7c625218fb77a97b5de [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/pacing/bitrate_prober.h"
19#include "modules/pacing/interval_budget.h"
20#include "modules/utility/include/process_thread.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "system_wrappers/include/clock.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000024
Per Kjellander7ef34f82019-02-22 13:09:32 +010025namespace webrtc {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000026namespace {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000027// Time limit in milliseconds between packet bursts.
Christoffer Rodbroe2e00002018-12-20 15:46:03 +010028const int64_t kDefaultMinPacketLimitMs = 5;
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +010029const int64_t kCongestedPacketIntervalMs = 500;
30const int64_t kPausedProcessIntervalMs = kCongestedPacketIntervalMs;
Sebastian Janssone5d8c572018-02-28 08:53:06 +010031const int64_t kMaxElapsedTimeMs = 2000;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000032
33// Upper cap on process interval, in case process has not been called in a long
34// time.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000035const int64_t kMaxIntervalTimeMs = 30;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000036
Per Kjellander7ef34f82019-02-22 13:09:32 +010037bool IsDisabled(const WebRtcKeyValueConfig& field_trials,
38 absl::string_view key) {
39 return field_trials.Lookup(key).find("Disabled") == 0;
40}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000041
Per Kjellander7ef34f82019-02-22 13:09:32 +010042bool IsEnabled(const WebRtcKeyValueConfig& field_trials,
43 absl::string_view key) {
44 return field_trials.Lookup(key).find("Enabled") == 0;
45}
46
Erik Språng58ee1872019-06-18 16:20:11 +020047int GetPriorityForType(RtpPacketToSend::Type type) {
48 switch (type) {
49 case RtpPacketToSend::Type::kAudio:
50 // Audio is always prioritized over other packet types.
51 return 0;
52 case RtpPacketToSend::Type::kRetransmission:
53 // Send retransmissions before new media.
54 return 1;
55 case RtpPacketToSend::Type::kVideo:
56 // Video has "normal" priority, in the old speak.
57 return 2;
58 case RtpPacketToSend::Type::kForwardErrorCorrection:
59 // Redundancy is OK to drop, but the content is hopefully not useless.
60 return 3;
61 case RtpPacketToSend::Type::kPadding:
62 // Packets that are in themselves likely useless, only sent to keep the
63 // BWE high.
64 return 4;
65 }
66}
67
Per Kjellander7ef34f82019-02-22 13:09:32 +010068} // namespace
sprang0a43fef2015-11-20 09:00:37 -080069const int64_t PacedSender::kMaxQueueLengthMs = 2000;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000070const float PacedSender::kDefaultPaceMultiplier = 2.5f;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000071
Sebastian Janssonaa01f272019-01-30 11:28:59 +010072PacedSender::PacedSender(Clock* clock,
Erik Språnge7942432019-06-12 13:30:02 +020073 PacketRouter* packet_router,
Per Kjellander7ef34f82019-02-22 13:09:32 +010074 RtcEventLog* event_log,
75 const WebRtcKeyValueConfig* field_trials)
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000076 : clock_(clock),
Erik Språnge7942432019-06-12 13:30:02 +020077 packet_router_(packet_router),
Per Kjellander5b698732019-04-15 12:36:33 +020078 fallback_field_trials_(
79 !field_trials ? absl::make_unique<FieldTrialBasedConfig>() : nullptr),
80 field_trials_(field_trials ? field_trials : fallback_field_trials_.get()),
Per Kjellander5b698732019-04-15 12:36:33 +020081 drain_large_queues_(
82 !IsDisabled(*field_trials_, "WebRTC-Pacer-DrainQueue")),
Sebastian Janssonc235a8d2018-06-15 14:46:11 +020083 send_padding_if_silent_(
Per Kjellander5b698732019-04-15 12:36:33 +020084 IsEnabled(*field_trials_, "WebRTC-Pacer-PadInSilence")),
85 pace_audio_(!IsDisabled(*field_trials_, "WebRTC-Pacer-BlockAudio")),
Christoffer Rodbroe2e00002018-12-20 15:46:03 +010086 min_packet_limit_ms_("", kDefaultMinPacketLimitMs),
Erik Språng96816752018-09-04 18:40:19 +020087 last_timestamp_ms_(clock_->TimeInMilliseconds()),
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000088 paused_(false),
Sebastian Jansson03914462018-10-11 20:22:03 +020089 media_budget_(0),
90 padding_budget_(0),
Per Kjellander5b698732019-04-15 12:36:33 +020091 prober_(*field_trials_),
philipelb61927c2017-02-28 07:05:23 -080092 probing_send_failure_(false),
perkjec81bcd2016-05-11 06:01:13 -070093 pacing_bitrate_kbps_(0),
Sebastian Janssona1630f82018-02-21 13:39:26 +010094 time_last_process_us_(clock->TimeInMicroseconds()),
95 last_send_time_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070096 first_sent_packet_ms_(-1),
Sebastian Jansson2560e2e2018-10-11 20:17:22 +020097 packets_(clock->TimeInMicroseconds()),
sprang89c4a7e2017-06-30 13:27:40 -070098 packet_counter_(0),
Alex Narest78609d52017-10-20 10:37:47 +020099 queue_time_limit(kMaxQueueLengthMs),
100 account_for_audio_(false) {
Christoffer Rodbroe2e00002018-12-20 15:46:03 +0100101 if (!drain_large_queues_) {
Sebastian Jansson0601d682018-06-25 19:23:05 +0200102 RTC_LOG(LS_WARNING) << "Pacer queues will not be drained,"
103 "pushback experiment must be enabled.";
Christoffer Rodbroe2e00002018-12-20 15:46:03 +0100104 }
105 ParseFieldTrial({&min_packet_limit_ms_},
Per Kjellander5b698732019-04-15 12:36:33 +0200106 field_trials_->Lookup("WebRTC-Pacer-MinPacketLimitMs"));
Christoffer Rodbroe2e00002018-12-20 15:46:03 +0100107 UpdateBudgetWithElapsedTime(min_packet_limit_ms_);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000108}
109
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000110PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000111
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800112void PacedSender::CreateProbeCluster(int bitrate_bps, int cluster_id) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700113 rtc::CritScope cs(&critsect_);
Piotr (Peter) Slatalac39f4622019-02-15 07:38:04 -0800114 prober_.CreateProbeCluster(bitrate_bps, TimeMilliseconds(), cluster_id);
philipeleb680ea2016-08-17 11:11:59 +0200115}
116
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000117void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -0700118 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700119 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700120 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100121 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -0700122 paused_ = true;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200123 packets_.SetPauseState(true, TimeMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700124 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100125 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700126 // Tell the process thread to call our TimeUntilNextProcess() method to get
127 // a new (longer) estimate for when to call Process().
128 if (process_thread_)
129 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000130}
131
132void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700133 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700134 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700135 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100136 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700137 paused_ = false;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200138 packets_.SetPauseState(false, TimeMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700139 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100140 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700141 // Tell the process thread to call our TimeUntilNextProcess() method to
142 // refresh the estimate for when to call Process().
143 if (process_thread_)
144 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000145}
146
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100147void PacedSender::SetCongestionWindow(int64_t congestion_window_bytes) {
148 rtc::CritScope cs(&critsect_);
149 congestion_window_bytes_ = congestion_window_bytes;
150}
151
152void PacedSender::UpdateOutstandingData(int64_t outstanding_bytes) {
153 rtc::CritScope cs(&critsect_);
154 outstanding_bytes_ = outstanding_bytes;
155}
156
157bool PacedSender::Congested() const {
158 if (congestion_window_bytes_ == kNoCongestionWindow)
159 return false;
160 return outstanding_bytes_ >= congestion_window_bytes_;
161}
162
Erik Språng96816752018-09-04 18:40:19 +0200163int64_t PacedSender::TimeMilliseconds() const {
164 int64_t time_ms = clock_->TimeInMilliseconds();
165 if (time_ms < last_timestamp_ms_) {
166 RTC_LOG(LS_WARNING)
167 << "Non-monotonic clock behavior observed. Previous timestamp: "
168 << last_timestamp_ms_ << ", new timestamp: " << time_ms;
169 RTC_DCHECK_GE(time_ms, last_timestamp_ms_);
170 time_ms = last_timestamp_ms_;
171 }
172 last_timestamp_ms_ = time_ms;
173 return time_ms;
174}
175
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000176void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700177 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200178 RTC_CHECK_EQ(0, packet_counter_);
Sebastian Jansson03914462018-10-11 20:22:03 +0200179 prober_.SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000180}
181
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100182void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
183 uint32_t padding_rate_bps) {
184 rtc::CritScope cs(&critsect_);
185 RTC_DCHECK(pacing_rate_bps > 0);
186 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
Sebastian Jansson03914462018-10-11 20:22:03 +0200187 padding_budget_.set_target_rate_kbps(padding_rate_bps / 1000);
Evan Shrubsolefee13e82019-02-26 15:25:52 +0100188
189 RTC_LOG(LS_VERBOSE) << "bwe:pacer_updated pacing_kbps="
190 << pacing_bitrate_kbps_
191 << " padding_budget_kbps=" << padding_rate_bps / 1000;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100192}
193
Peter Boströme23e7372015-10-08 11:44:14 +0200194void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
195 uint32_t ssrc,
196 uint16_t sequence_number,
197 int64_t capture_time_ms,
198 size_t bytes,
199 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700200 rtc::CritScope cs(&critsect_);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100201 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
202 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000203
Erik Språng96816752018-09-04 18:40:19 +0200204 int64_t now_ms = TimeMilliseconds();
Sebastian Jansson03914462018-10-11 20:22:03 +0200205 prober_.OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100206
sprang0a43fef2015-11-20 09:00:37 -0800207 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100208 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000209
Erik Språng58ee1872019-06-18 16:20:11 +0200210 RtpPacketToSend::Type type;
211 switch (priority) {
212 case RtpPacketPacer::kHighPriority:
213 type = RtpPacketToSend::Type::kAudio;
214 break;
215 case RtpPacketPacer::kNormalPriority:
216 type = RtpPacketToSend::Type::kRetransmission;
217 break;
218 default:
219 type = RtpPacketToSend::Type::kVideo;
220 }
221 packets_.Push(GetPriorityForType(type), type, ssrc, sequence_number,
222 capture_time_ms, now_ms, bytes, retransmission,
223 packet_counter_++);
224}
225
226void PacedSender::EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet) {
227 rtc::CritScope cs(&critsect_);
228 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
229 << "SetPacingRate must be called before InsertPacket.";
230
231 int64_t now_ms = TimeMilliseconds();
232 prober_.OnIncomingPacket(packet->payload_size());
233
234 if (packet->capture_time_ms() < 0) {
235 packet->set_capture_time_ms(now_ms);
236 }
237
238 RTC_CHECK(packet->packet_type());
239 int priority = GetPriorityForType(*packet->packet_type());
240 packets_.Push(priority, now_ms, packet_counter_++, std::move(packet));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000241}
242
Alex Narest78609d52017-10-20 10:37:47 +0200243void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
244 rtc::CritScope cs(&critsect_);
245 account_for_audio_ = account_for_audio;
246}
247
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000248int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700249 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800250 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200251 return static_cast<int64_t>(packets_.SizeInBytes() * 8 /
perkjec81bcd2016-05-11 06:01:13 -0700252 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000253}
254
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000255size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700256 rtc::CritScope cs(&critsect_);
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200257 return packets_.SizeInPackets();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000258}
259
Christoffer Rodbroc610e262019-01-08 10:49:19 +0100260int64_t PacedSender::QueueSizeBytes() const {
261 rtc::CritScope cs(&critsect_);
262 return packets_.SizeInBytes();
263}
264
asaperssonfc5e81c2017-04-19 23:28:53 -0700265int64_t PacedSender::FirstSentPacketTimeMs() const {
266 rtc::CritScope cs(&critsect_);
267 return first_sent_packet_ms_;
268}
269
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000270int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700271 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000272
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200273 int64_t oldest_packet = packets_.OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000274 if (oldest_packet == 0)
275 return 0;
276
Erik Språng96816752018-09-04 18:40:19 +0200277 return TimeMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000278}
279
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000280int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700281 rtc::CritScope cs(&critsect_);
Sebastian Janssona1630f82018-02-21 13:39:26 +0100282 int64_t elapsed_time_us =
283 clock_->TimeInMicroseconds() - time_last_process_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700284 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
285 // When paused we wake up every 500 ms to send a padding packet to ensure
286 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700287 if (paused_)
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100288 return std::max<int64_t>(kPausedProcessIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700289
Sebastian Jansson03914462018-10-11 20:22:03 +0200290 if (prober_.IsProbing()) {
291 int64_t ret = prober_.TimeUntilNextProbe(TimeMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800292 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000293 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000294 }
Christoffer Rodbroe2e00002018-12-20 15:46:03 +0100295 return std::max<int64_t>(min_packet_limit_ms_ - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000296}
297
Sebastian Jansson916ae082018-10-26 13:10:23 +0200298int64_t PacedSender::UpdateTimeAndGetElapsedMs(int64_t now_us) {
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200299 int64_t elapsed_time_ms = (now_us - time_last_process_us_ + 500) / 1000;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100300 time_last_process_us_ = now_us;
Sebastian Janssone5d8c572018-02-28 08:53:06 +0100301 if (elapsed_time_ms > kMaxElapsedTimeMs) {
302 RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time_ms
303 << " ms) longer than expected, limiting to "
304 << kMaxElapsedTimeMs << " ms";
305 elapsed_time_ms = kMaxElapsedTimeMs;
306 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200307 return elapsed_time_ms;
308}
309
310bool PacedSender::ShouldSendKeepalive(int64_t now_us) const {
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200311 if (send_padding_if_silent_ || paused_ || Congested()) {
312 // We send a padding packet every 500 ms to ensure we won't get stuck in
313 // congested state due to no feedback being received.
Sebastian Janssonb544f6c2018-06-04 19:02:41 +0200314 int64_t elapsed_since_last_send_us = now_us - last_send_time_us_;
315 if (elapsed_since_last_send_us >= kCongestedPacketIntervalMs * 1000) {
Sebastian Jansson682c6492018-04-12 13:08:22 +0200316 // We can not send padding unless a normal packet has first been sent. If
317 // we do, timestamps get messed up.
318 if (packet_counter_ > 0) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200319 return true;
Sebastian Jansson682c6492018-04-12 13:08:22 +0200320 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100321 }
stefan9e117c5e12017-08-16 08:16:25 -0700322 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200323 return false;
324}
325
326void PacedSender::Process() {
327 rtc::CritScope cs(&critsect_);
328 int64_t now_us = clock_->TimeInMicroseconds();
329 int64_t elapsed_time_ms = UpdateTimeAndGetElapsedMs(now_us);
330 if (ShouldSendKeepalive(now_us)) {
331 critsect_.Leave();
Erik Språnge7942432019-06-12 13:30:02 +0200332 size_t bytes_sent = packet_router_->TimeToSendPadding(1, PacedPacketInfo());
Sebastian Jansson916ae082018-10-26 13:10:23 +0200333 critsect_.Enter();
334 OnPaddingSent(bytes_sent);
Sebastian Jansson916ae082018-10-26 13:10:23 +0200335 }
336
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200337 if (paused_)
338 return;
stefan9e117c5e12017-08-16 08:16:25 -0700339
340 if (elapsed_time_ms > 0) {
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200341 int target_bitrate_kbps = pacing_bitrate_kbps_;
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200342 size_t queue_size_bytes = packets_.SizeInBytes();
sprang0a43fef2015-11-20 09:00:37 -0800343 if (queue_size_bytes > 0) {
344 // Assuming equal size packets and input/output rate, the average packet
345 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
346 // time constraint shall be met. Determine bitrate needed for that.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200347 packets_.UpdateQueueTime(TimeMilliseconds());
Sebastian Jansson0601d682018-06-25 19:23:05 +0200348 if (drain_large_queues_) {
349 int64_t avg_time_left_ms = std::max<int64_t>(
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200350 1, queue_time_limit - packets_.AverageQueueTimeMs());
Sebastian Jansson0601d682018-06-25 19:23:05 +0200351 int min_bitrate_needed_kbps =
352 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
Evan Shrubsolefee13e82019-02-26 15:25:52 +0100353 if (min_bitrate_needed_kbps > target_bitrate_kbps) {
Sebastian Jansson0601d682018-06-25 19:23:05 +0200354 target_bitrate_kbps = min_bitrate_needed_kbps;
Evan Shrubsolefee13e82019-02-26 15:25:52 +0100355 RTC_LOG(LS_VERBOSE) << "bwe:large_pacing_queue pacing_rate_kbps="
356 << target_bitrate_kbps;
357 }
Sebastian Jansson0601d682018-06-25 19:23:05 +0200358 }
sprang0a43fef2015-11-20 09:00:37 -0800359 }
360
Sebastian Jansson03914462018-10-11 20:22:03 +0200361 media_budget_.set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700362 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000363 }
philipela1ed0b32016-06-01 06:31:17 -0700364
Sebastian Jansson03914462018-10-11 20:22:03 +0200365 bool is_probing = prober_.IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800366 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700367 size_t bytes_sent = 0;
368 size_t recommended_probe_size = 0;
369 if (is_probing) {
Sebastian Jansson03914462018-10-11 20:22:03 +0200370 pacing_info = prober_.CurrentCluster();
371 recommended_probe_size = prober_.RecommendedMinProbeSize();
isheriffcc5903e2016-10-04 08:29:38 -0700372 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200373 // The paused state is checked in the loop since it leaves the critical
374 // section allowing the paused state to be changed from other code.
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200375 while (!packets_.Empty() && !paused_) {
Erik Språng58ee1872019-06-18 16:20:11 +0200376 auto* packet = GetPendingPacket(pacing_info);
Sebastian Jansson916ae082018-10-26 13:10:23 +0200377 if (packet == nullptr)
378 break;
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100379
Erik Språng58ee1872019-06-18 16:20:11 +0200380 std::unique_ptr<RtpPacketToSend> rtp_packet = packet->ReleasePacket();
381 const bool owned_rtp_packet = rtp_packet != nullptr;
382
Sebastian Jansson916ae082018-10-26 13:10:23 +0200383 critsect_.Leave();
Erik Språng58ee1872019-06-18 16:20:11 +0200384
385 RtpPacketSendResult success;
386 if (rtp_packet != nullptr) {
387 packet_router_->SendPacket(std::move(rtp_packet), pacing_info);
388 success = RtpPacketSendResult::kSuccess;
389 } else {
390 success = packet_router_->TimeToSendPacket(
391 packet->ssrc(), packet->sequence_number(), packet->capture_time_ms(),
392 packet->is_retransmission(), pacing_info);
393 }
394
Sebastian Jansson916ae082018-10-26 13:10:23 +0200395 critsect_.Enter();
Erik Språngd2879622019-05-10 08:29:01 -0700396 if (success == RtpPacketSendResult::kSuccess ||
397 success == RtpPacketSendResult::kPacketNotFound) {
398 // Packet sent or invalid packet, remove it from queue.
399 // TODO(webrtc:8052): Don't consume media budget on kInvalid.
Erik Språng58ee1872019-06-18 16:20:11 +0200400 bytes_sent += packet->size_in_bytes();
Sebastian Janssona1630f82018-02-21 13:39:26 +0100401 // Send succeeded, remove it from the queue.
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100402 OnPacketSent(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700403 if (is_probing && bytes_sent > recommended_probe_size)
404 break;
Erik Språng58ee1872019-06-18 16:20:11 +0200405 } else if (owned_rtp_packet) {
406 // Send failed, but we can't put it back in the queue, remove it without
407 // consuming budget.
408 packets_.FinalizePop();
409 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200410 } else {
411 // Send failed, put it back into the queue.
Erik Språng58ee1872019-06-18 16:20:11 +0200412 packets_.CancelPop();
isheriffcc5903e2016-10-04 08:29:38 -0700413 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200414 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000415 }
Peter Boströme23e7372015-10-08 11:44:14 +0200416
Sebastian Jansson60570dc2018-09-13 17:11:06 +0200417 if (packets_.Empty() && !Congested()) {
isheriffcc5903e2016-10-04 08:29:38 -0700418 // We can not send padding unless a normal packet has first been sent. If we
419 // do, timestamps get messed up.
420 if (packet_counter_ > 0) {
421 int padding_needed =
422 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
Sebastian Jansson03914462018-10-11 20:22:03 +0200423 : padding_budget_.bytes_remaining());
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100424 if (padding_needed > 0) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200425 critsect_.Leave();
426 size_t padding_sent =
Erik Språnge7942432019-06-12 13:30:02 +0200427 packet_router_->TimeToSendPadding(padding_needed, pacing_info);
Sebastian Jansson916ae082018-10-26 13:10:23 +0200428 critsect_.Enter();
429 bytes_sent += padding_sent;
430 OnPaddingSent(padding_sent);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100431 }
isheriffcc5903e2016-10-04 08:29:38 -0700432 }
perkj71ee44c2016-06-15 00:47:53 -0700433 }
philipelb61927c2017-02-28 07:05:23 -0800434 if (is_probing) {
435 probing_send_failure_ = bytes_sent == 0;
436 if (!probing_send_failure_)
Sebastian Jansson03914462018-10-11 20:22:03 +0200437 prober_.ProbeSent(TimeMilliseconds(), bytes_sent);
philipelb61927c2017-02-28 07:05:23 -0800438 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000439}
440
tommi919dce22017-03-15 07:45:36 -0700441void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Karl Wiberg43432732018-05-23 11:13:31 +0200442 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << process_thread;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100443 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700444 process_thread_ = process_thread;
445}
446
Erik Språng58ee1872019-06-18 16:20:11 +0200447RoundRobinPacketQueue::QueuedPacket* PacedSender::GetPendingPacket(
Sebastian Jansson916ae082018-10-26 13:10:23 +0200448 const PacedPacketInfo& pacing_info) {
449 // Since we need to release the lock in order to send, we first pop the
450 // element from the priority queue but keep it in storage, so that we can
451 // reinsert it if send fails.
Erik Språng58ee1872019-06-18 16:20:11 +0200452 RoundRobinPacketQueue::QueuedPacket* packet = packets_.BeginPop();
453 bool audio_packet = packet->type() == RtpPacketToSend::Type::kAudio;
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100454 bool apply_pacing = !audio_packet || pace_audio_;
Sebastian Jansson03914462018-10-11 20:22:03 +0200455 if (apply_pacing && (Congested() || (media_budget_.bytes_remaining() == 0 &&
Sebastian Janssonce4829a2018-06-15 14:47:35 +0200456 pacing_info.probe_cluster_id ==
457 PacedPacketInfo::kNotAProbe))) {
Erik Språng58ee1872019-06-18 16:20:11 +0200458 packets_.CancelPop();
Sebastian Jansson916ae082018-10-26 13:10:23 +0200459 return nullptr;
terelius8b70faf2016-08-01 09:47:31 -0700460 }
Sebastian Jansson916ae082018-10-26 13:10:23 +0200461 return packet;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000462}
463
Erik Språng58ee1872019-06-18 16:20:11 +0200464void PacedSender::OnPacketSent(RoundRobinPacketQueue::QueuedPacket* packet) {
Sebastian Jansson916ae082018-10-26 13:10:23 +0200465 if (first_sent_packet_ms_ == -1)
466 first_sent_packet_ms_ = TimeMilliseconds();
Erik Språng58ee1872019-06-18 16:20:11 +0200467 bool audio_packet = packet->type() == RtpPacketToSend::Type::kAudio;
Sebastian Jansson916ae082018-10-26 13:10:23 +0200468 if (!audio_packet || account_for_audio_) {
469 // Update media bytes sent.
Erik Språng58ee1872019-06-18 16:20:11 +0200470 UpdateBudgetWithBytesSent(packet->size_in_bytes());
Sebastian Jansson916ae082018-10-26 13:10:23 +0200471 last_send_time_us_ = clock_->TimeInMicroseconds();
472 }
473 // Send succeeded, remove it from the queue.
Erik Språng58ee1872019-06-18 16:20:11 +0200474 packets_.FinalizePop();
Sebastian Jansson916ae082018-10-26 13:10:23 +0200475}
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000476
Sebastian Jansson916ae082018-10-26 13:10:23 +0200477void PacedSender::OnPaddingSent(size_t bytes_sent) {
Stefan Holmer01b48882015-05-05 10:21:24 +0200478 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700479 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200480 }
Sebastian Janssonc235a8d2018-06-15 14:46:11 +0200481 last_send_time_us_ = clock_->TimeInMicroseconds();
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000482}
483
isheriff31687812016-10-04 08:43:09 -0700484void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100485 delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms);
Sebastian Jansson03914462018-10-11 20:22:03 +0200486 media_budget_.IncreaseBudget(delta_time_ms);
487 padding_budget_.IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000488}
isheriff31687812016-10-04 08:43:09 -0700489
490void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
Sebastian Jansson45d9c1d2018-03-09 12:48:01 +0100491 outstanding_bytes_ += bytes_sent;
Sebastian Jansson03914462018-10-11 20:22:03 +0200492 media_budget_.UseBudget(bytes_sent);
493 padding_budget_.UseBudget(bytes_sent);
isheriff31687812016-10-04 08:43:09 -0700494}
sprang89c4a7e2017-06-30 13:27:40 -0700495
sprang89c4a7e2017-06-30 13:27:40 -0700496void PacedSender::SetQueueTimeLimit(int limit_ms) {
497 rtc::CritScope cs(&critsect_);
498 queue_time_limit = limit_ms;
499}
500
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000501} // namespace webrtc