blob: 179c7882433ce0b6238f8e4d035e0afa6891d11f [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/packet_queue.h"
25#include "modules/pacing/round_robin_packet_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/utility/include/process_thread.h"
27#include "rtc_base/checks.h"
28#include "rtc_base/logging.h"
Niels Möller712048c2017-10-18 13:08:22 +020029#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "system_wrappers/include/clock.h"
31#include "system_wrappers/include/field_trial.h"
Ilya Nikolaevskiy2ffe3e82018-01-17 19:57:24 +000032#include "system_wrappers/include/runtime_enabled_features.h"
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000033
34namespace {
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000035// Time limit in milliseconds between packet bursts.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000036const int64_t kMinPacketLimitMs = 5;
stefan9e117c5e12017-08-16 08:16:25 -070037const int64_t kPausedPacketIntervalMs = 500;
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
Ilya Nikolaevskiy2ffe3e82018-01-17 19:57:24 +000043const char kRoundRobinExperimentName[] = "WebRTC-RoundRobinPacing";
44
45bool IsRoundRobinPacingEnabled() {
46 return webrtc::field_trial::IsEnabled(kRoundRobinExperimentName) || (
47 !webrtc::field_trial::IsDisabled(kRoundRobinExperimentName) &&
48 webrtc::runtime_enabled_features::IsFeatureEnabled(
49 webrtc::runtime_enabled_features::kDualStreamModeFeatureName));
50}
51
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000052} // namespace
53
54namespace webrtc {
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000055
sprang0a43fef2015-11-20 09:00:37 -080056const int64_t PacedSender::kMaxQueueLengthMs = 2000;
Sebastian Janssonea86bb72018-02-14 16:53:38 +000057const float PacedSender::kDefaultPaceMultiplier = 2.5f;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000058
Sebastian Janssonb5374962018-02-07 13:26:38 +010059namespace {
60std::unique_ptr<PacketQueueInterface> CreatePacketQueue(const Clock* clock,
61 bool round_robin) {
62 if (round_robin) {
63 return rtc::MakeUnique<RoundRobinPacketQueue>(clock);
64 } else {
65 return rtc::MakeUnique<PacketQueue>(clock);
66 }
67}
68} // namespace
69
philipelc3b3f7a2017-03-29 01:23:13 -070070PacedSender::PacedSender(const Clock* clock,
71 PacketSender* packet_sender,
Sebastian Janssonb5374962018-02-07 13:26:38 +010072 RtcEventLog* event_log)
73 : PacedSender(clock,
74 packet_sender,
75 event_log,
76 CreatePacketQueue(clock, IsRoundRobinPacingEnabled())) {}
Niels Möller9d4af012017-10-31 09:54:50 +010077
78PacedSender::PacedSender(const Clock* clock,
79 PacketSender* packet_sender,
80 RtcEventLog* event_log,
Sebastian Janssonb5374962018-02-07 13:26:38 +010081 std::unique_ptr<PacketQueueInterface> packets)
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000082 : clock_(clock),
perkjec81bcd2016-05-11 06:01:13 -070083 packet_sender_(packet_sender),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000084 alr_detector_(rtc::MakeUnique<AlrDetector>(event_log)),
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000085 paused_(false),
Niels Möller712048c2017-10-18 13:08:22 +020086 media_budget_(rtc::MakeUnique<IntervalBudget>(0)),
87 padding_budget_(rtc::MakeUnique<IntervalBudget>(0)),
88 prober_(rtc::MakeUnique<BitrateProber>(event_log)),
philipelb61927c2017-02-28 07:05:23 -080089 probing_send_failure_(false),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000090 estimated_bitrate_bps_(0),
91 min_send_bitrate_kbps_(0u),
92 max_padding_bitrate_kbps_(0u),
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),
Niels Möller9d4af012017-10-31 09:54:50 +010097 packets_(std::move(packets)),
sprang89c4a7e2017-06-30 13:27:40 -070098 packet_counter_(0),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000099 pacing_factor_(kDefaultPaceMultiplier),
Alex Narest78609d52017-10-20 10:37:47 +0200100 queue_time_limit(kMaxQueueLengthMs),
101 account_for_audio_(false) {
isheriff31687812016-10-04 08:43:09 -0700102 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000103}
104
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000105PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000106
philipelfd58b612017-01-04 07:05:25 -0800107void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700108 rtc::CritScope cs(&critsect_);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100109 prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +0200110}
111
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000112void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -0700113 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700114 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700115 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100116 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -0700117 paused_ = true;
sprangddcfb9f2017-08-16 05:38:49 -0700118 packets_->SetPauseState(true, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700119 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100120 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700121 // Tell the process thread to call our TimeUntilNextProcess() method to get
122 // a new (longer) estimate for when to call Process().
123 if (process_thread_)
124 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000125}
126
127void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700128 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700129 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700130 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100131 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700132 paused_ = false;
sprangddcfb9f2017-08-16 05:38:49 -0700133 packets_->SetPauseState(false, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700134 }
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100135 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700136 // Tell the process thread to call our TimeUntilNextProcess() method to
137 // refresh the estimate for when to call Process().
138 if (process_thread_)
139 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000140}
141
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000142void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700143 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200144 RTC_CHECK_EQ(0, packet_counter_);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700145 prober_->SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000146}
147
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000148void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) {
149 if (bitrate_bps == 0)
150 RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate.";
kthelgason6bfe49c2017-03-30 01:14:41 -0700151 rtc::CritScope cs(&critsect_);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000152 estimated_bitrate_bps_ = bitrate_bps;
153 padding_budget_->set_target_rate_kbps(
154 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
155 pacing_bitrate_kbps_ =
156 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
157 pacing_factor_;
158 alr_detector_->SetEstimatedBitrate(bitrate_bps);
159}
160
161void PacedSender::SetSendBitrateLimits(int min_send_bitrate_bps,
162 int padding_bitrate) {
163 rtc::CritScope cs(&critsect_);
164 min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000;
165 pacing_bitrate_kbps_ =
166 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
167 pacing_factor_;
168 max_padding_bitrate_kbps_ = padding_bitrate / 1000;
169 padding_budget_->set_target_rate_kbps(
170 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000171}
172
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100173void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
174 uint32_t padding_rate_bps) {
175 rtc::CritScope cs(&critsect_);
176 RTC_DCHECK(pacing_rate_bps > 0);
177 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
178 padding_budget_->set_target_rate_kbps(padding_rate_bps / 1000);
179}
180
Peter Boströme23e7372015-10-08 11:44:14 +0200181void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
182 uint32_t ssrc,
183 uint16_t sequence_number,
184 int64_t capture_time_ms,
185 size_t bytes,
186 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700187 rtc::CritScope cs(&critsect_);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100188 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
189 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000190
Erik Språngad113e52015-11-26 16:26:12 +0100191 int64_t now_ms = clock_->TimeInMilliseconds();
philipel4a1ec1e2016-08-15 11:51:06 -0700192 prober_->OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100193
sprang0a43fef2015-11-20 09:00:37 -0800194 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100195 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000196
philipel9981bd92017-09-26 17:16:06 +0200197 packets_->Push(PacketQueue::Packet(priority, ssrc, sequence_number,
198 capture_time_ms, now_ms, bytes,
199 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000200}
201
Alex Narest78609d52017-10-20 10:37:47 +0200202void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
203 rtc::CritScope cs(&critsect_);
204 account_for_audio_ = account_for_audio;
205}
206
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000207int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700208 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800209 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
perkjec81bcd2016-05-11 06:01:13 -0700210 return static_cast<int64_t>(packets_->SizeInBytes() * 8 /
211 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000212}
213
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000214rtc::Optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime()
215 const {
216 rtc::CritScope cs(&critsect_);
217 return alr_detector_->GetApplicationLimitedRegionStartTime();
218}
219
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000220size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700221 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000222 return packets_->SizeInPackets();
223}
224
asaperssonfc5e81c2017-04-19 23:28:53 -0700225int64_t PacedSender::FirstSentPacketTimeMs() const {
226 rtc::CritScope cs(&critsect_);
227 return first_sent_packet_ms_;
228}
229
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000230int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700231 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000232
sprang0a43fef2015-11-20 09:00:37 -0800233 int64_t oldest_packet = packets_->OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000234 if (oldest_packet == 0)
235 return 0;
236
237 return clock_->TimeInMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000238}
239
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000240int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700241 rtc::CritScope cs(&critsect_);
Sebastian Janssona1630f82018-02-21 13:39:26 +0100242 int64_t elapsed_time_us =
243 clock_->TimeInMicroseconds() - time_last_process_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700244 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
245 // When paused we wake up every 500 ms to send a padding packet to ensure
246 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700247 if (paused_)
stefan9e117c5e12017-08-16 08:16:25 -0700248 return std::max<int64_t>(kPausedPacketIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700249
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000250 if (prober_->IsProbing()) {
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000251 int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800252 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000253 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000254 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000255 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000256}
257
pbosa26ac922016-02-25 04:50:01 -0800258void PacedSender::Process() {
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000259 int64_t now_us = clock_->TimeInMicroseconds();
kthelgason6bfe49c2017-03-30 01:14:41 -0700260 rtc::CritScope cs(&critsect_);
Sebastian Janssona1630f82018-02-21 13:39:26 +0100261 time_last_process_us_ = now_us;
262 int64_t elapsed_time_ms = (now_us - last_send_time_us_ + 500) / 1000;
stefan9e117c5e12017-08-16 08:16:25 -0700263
Sebastian Janssona1630f82018-02-21 13:39:26 +0100264 // When paused we send a padding packet every 500 ms to ensure we won't get
265 // stuck in the paused state due to no feedback being received.
stefan9e117c5e12017-08-16 08:16:25 -0700266 if (paused_) {
stefan9e117c5e12017-08-16 08:16:25 -0700267 // We can not send padding unless a normal packet has first been sent. If we
268 // do, timestamps get messed up.
Sebastian Janssona1630f82018-02-21 13:39:26 +0100269 if (elapsed_time_ms >= kPausedPacketIntervalMs && packet_counter_ > 0) {
270 PacedPacketInfo pacing_info;
271 size_t bytes_sent = SendPadding(1, pacing_info);
272 alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms);
273 last_send_time_us_ = clock_->TimeInMicroseconds();
274 }
stefan9e117c5e12017-08-16 08:16:25 -0700275 return;
276 }
277
Sebastian Janssona1630f82018-02-21 13:39:26 +0100278 int target_bitrate_kbps = pacing_bitrate_kbps_;
stefan9e117c5e12017-08-16 08:16:25 -0700279 if (elapsed_time_ms > 0) {
sprang0a43fef2015-11-20 09:00:37 -0800280 size_t queue_size_bytes = packets_->SizeInBytes();
281 if (queue_size_bytes > 0) {
282 // Assuming equal size packets and input/output rate, the average packet
283 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
284 // time constraint shall be met. Determine bitrate needed for that.
Erik Språngad113e52015-11-26 16:26:12 +0100285 packets_->UpdateQueueTime(clock_->TimeInMilliseconds());
sprang0a43fef2015-11-20 09:00:37 -0800286 int64_t avg_time_left_ms = std::max<int64_t>(
sprang89c4a7e2017-06-30 13:27:40 -0700287 1, queue_time_limit - packets_->AverageQueueTimeMs());
sprang0a43fef2015-11-20 09:00:37 -0800288 int min_bitrate_needed_kbps =
289 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
290 if (min_bitrate_needed_kbps > target_bitrate_kbps)
291 target_bitrate_kbps = min_bitrate_needed_kbps;
292 }
293
294 media_budget_->set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700295 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000296 }
philipela1ed0b32016-06-01 06:31:17 -0700297
Sebastian Janssona1630f82018-02-21 13:39:26 +0100298 last_send_time_us_ = clock_->TimeInMicroseconds();
stefan9e117c5e12017-08-16 08:16:25 -0700299
philipel1a93cde2016-06-03 01:41:45 -0700300 bool is_probing = prober_->IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800301 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700302 size_t bytes_sent = 0;
303 size_t recommended_probe_size = 0;
304 if (is_probing) {
philipelc7bf32a2017-02-17 03:59:43 -0800305 pacing_info = prober_->CurrentCluster();
isheriffcc5903e2016-10-04 08:29:38 -0700306 recommended_probe_size = prober_->RecommendedMinProbeSize();
307 }
Sebastian Janssona1630f82018-02-21 13:39:26 +0100308 // The paused state is checked in the loop since SendPacket leaves the
309 // critical section allowing the paused state to be changed from other code.
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100310 while (!packets_->Empty() && !paused_) {
Peter Boströme23e7372015-10-08 11:44:14 +0200311 // Since we need to release the lock in order to send, we first pop the
312 // element from the priority queue but keep it in storage, so that we can
313 // reinsert it if send fails.
philipel9981bd92017-09-26 17:16:06 +0200314 const PacketQueue::Packet& packet = packets_->BeginPop();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100315
philipelc7bf32a2017-02-17 03:59:43 -0800316 if (SendPacket(packet, pacing_info)) {
Danil Chapovalov516036f2018-02-14 10:21:27 +0000317 bytes_sent += packet.bytes;
Sebastian Janssona1630f82018-02-21 13:39:26 +0100318 // Send succeeded, remove it from the queue.
Peter Boströme23e7372015-10-08 11:44:14 +0200319 packets_->FinalizePop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700320 if (is_probing && bytes_sent > recommended_probe_size)
321 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200322 } else {
323 // Send failed, put it back into the queue.
324 packets_->CancelPop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700325 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200326 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000327 }
Peter Boströme23e7372015-10-08 11:44:14 +0200328
stefan9e117c5e12017-08-16 08:16:25 -0700329 if (packets_->Empty()) {
isheriffcc5903e2016-10-04 08:29:38 -0700330 // We can not send padding unless a normal packet has first been sent. If we
331 // do, timestamps get messed up.
332 if (packet_counter_ > 0) {
333 int padding_needed =
334 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
335 : padding_budget_->bytes_remaining());
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100336 if (padding_needed > 0) {
philipelc7bf32a2017-02-17 03:59:43 -0800337 bytes_sent += SendPadding(padding_needed, pacing_info);
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100338 }
isheriffcc5903e2016-10-04 08:29:38 -0700339 }
perkj71ee44c2016-06-15 00:47:53 -0700340 }
philipelb61927c2017-02-28 07:05:23 -0800341 if (is_probing) {
342 probing_send_failure_ = bytes_sent == 0;
343 if (!probing_send_failure_)
344 prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent);
345 }
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000346 alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000347}
348
tommi919dce22017-03-15 07:45:36 -0700349void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100350 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << std::hex << process_thread;
Sebastian Jansson439f0bc2018-02-20 10:46:39 +0100351 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700352 process_thread_ = process_thread;
353}
354
philipel9981bd92017-09-26 17:16:06 +0200355bool PacedSender::SendPacket(const PacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800356 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700357 RTC_DCHECK(!paused_);
stefan099110c2017-02-01 03:57:42 -0800358 if (media_budget_->bytes_remaining() == 0 &&
philipelc7bf32a2017-02-17 03:59:43 -0800359 pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) {
stefan099110c2017-02-01 03:57:42 -0800360 return false;
terelius8b70faf2016-08-01 09:47:31 -0700361 }
philipelc7bf32a2017-02-17 03:59:43 -0800362
kthelgason6bfe49c2017-03-30 01:14:41 -0700363 critsect_.Leave();
perkjec81bcd2016-05-11 06:01:13 -0700364 const bool success = packet_sender_->TimeToSendPacket(
365 packet.ssrc, packet.sequence_number, packet.capture_time_ms,
philipelc7bf32a2017-02-17 03:59:43 -0800366 packet.retransmission, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700367 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000368
Peter Boström7ecc1632016-03-02 14:22:25 +0100369 if (success) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100370 if (first_sent_packet_ms_ == -1)
371 first_sent_packet_ms_ = clock_->TimeInMilliseconds();
Alex Narest78609d52017-10-20 10:37:47 +0200372 if (packet.priority != kHighPriority || account_for_audio_) {
Peter Boström7ecc1632016-03-02 14:22:25 +0100373 // Update media bytes sent.
eladalon32040ef2017-08-02 06:29:00 -0700374 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
375 // situations where nothing actually ended up being sent to the network,
376 // and we probably don't want to update the budget in such cases.
377 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
isheriff31687812016-10-04 08:43:09 -0700378 UpdateBudgetWithBytesSent(packet.bytes);
Peter Boström7ecc1632016-03-02 14:22:25 +0100379 }
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000380 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000381
382 return success;
383}
384
philipelc7bf32a2017-02-17 03:59:43 -0800385size_t PacedSender::SendPadding(size_t padding_needed,
386 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700387 RTC_DCHECK_GT(packet_counter_, 0);
kthelgason6bfe49c2017-03-30 01:14:41 -0700388 critsect_.Leave();
philipela1ed0b32016-06-01 06:31:17 -0700389 size_t bytes_sent =
philipelc7bf32a2017-02-17 03:59:43 -0800390 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700391 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000392
Stefan Holmer01b48882015-05-05 10:21:24 +0200393 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700394 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200395 }
isheriffcc5903e2016-10-04 08:29:38 -0700396 return bytes_sent;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000397}
398
isheriff31687812016-10-04 08:43:09 -0700399void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
Sebastian Janssona1630f82018-02-21 13:39:26 +0100400 delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000401 media_budget_->IncreaseBudget(delta_time_ms);
402 padding_budget_->IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000403}
isheriff31687812016-10-04 08:43:09 -0700404
405void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
406 media_budget_->UseBudget(bytes_sent);
407 padding_budget_->UseBudget(bytes_sent);
408}
sprang89c4a7e2017-06-30 13:27:40 -0700409
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000410void PacedSender::SetPacingFactor(float pacing_factor) {
411 rtc::CritScope cs(&critsect_);
412 pacing_factor_ = pacing_factor;
413 // Make sure new padding factor is applied immediately, otherwise we need to
414 // wait for the send bitrate estimate to be updated before this takes effect.
415 SetEstimatedBitrate(estimated_bitrate_bps_);
416}
417
sprang89c4a7e2017-06-30 13:27:40 -0700418void PacedSender::SetQueueTimeLimit(int limit_ms) {
419 rtc::CritScope cs(&critsect_);
420 queue_time_limit = limit_ms;
421}
422
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000423} // namespace webrtc