blob: 4bccb000c2f03db96fc2df562e719614c2ee7f36 [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),
Danil Chapovalov516036f2018-02-14 10:21:27 +000094 time_last_update_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070095 first_sent_packet_ms_(-1),
Niels Möller9d4af012017-10-31 09:54:50 +010096 packets_(std::move(packets)),
sprang89c4a7e2017-06-30 13:27:40 -070097 packet_counter_(0),
Sebastian Janssonea86bb72018-02-14 16:53:38 +000098 pacing_factor_(kDefaultPaceMultiplier),
Alex Narest78609d52017-10-20 10:37:47 +020099 queue_time_limit(kMaxQueueLengthMs),
100 account_for_audio_(false) {
isheriff31687812016-10-04 08:43:09 -0700101 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000102}
103
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000104PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000105
philipelfd58b612017-01-04 07:05:25 -0800106void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700107 rtc::CritScope cs(&critsect_);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100108 prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +0200109}
110
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000111void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -0700112 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700113 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700114 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -0700116 paused_ = true;
sprangddcfb9f2017-08-16 05:38:49 -0700117 packets_->SetPauseState(true, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700118 }
119 // Tell the process thread to call our TimeUntilNextProcess() method to get
120 // a new (longer) 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
125void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700126 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700127 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700128 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100129 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700130 paused_ = false;
sprangddcfb9f2017-08-16 05:38:49 -0700131 packets_->SetPauseState(false, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700132 }
133 // Tell the process thread to call our TimeUntilNextProcess() method to
134 // refresh the estimate for when to call Process().
135 if (process_thread_)
136 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000137}
138
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000139void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700140 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200141 RTC_CHECK_EQ(0, packet_counter_);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700142 prober_->SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000143}
144
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000145void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) {
146 if (bitrate_bps == 0)
147 RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate.";
kthelgason6bfe49c2017-03-30 01:14:41 -0700148 rtc::CritScope cs(&critsect_);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000149 estimated_bitrate_bps_ = bitrate_bps;
150 padding_budget_->set_target_rate_kbps(
151 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
152 pacing_bitrate_kbps_ =
153 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
154 pacing_factor_;
155 alr_detector_->SetEstimatedBitrate(bitrate_bps);
156}
157
158void PacedSender::SetSendBitrateLimits(int min_send_bitrate_bps,
159 int padding_bitrate) {
160 rtc::CritScope cs(&critsect_);
161 min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000;
162 pacing_bitrate_kbps_ =
163 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) *
164 pacing_factor_;
165 max_padding_bitrate_kbps_ = padding_bitrate / 1000;
166 padding_budget_->set_target_rate_kbps(
167 std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000168}
169
Peter Boströme23e7372015-10-08 11:44:14 +0200170void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
171 uint32_t ssrc,
172 uint16_t sequence_number,
173 int64_t capture_time_ms,
174 size_t bytes,
175 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700176 rtc::CritScope cs(&critsect_);
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000177 RTC_DCHECK(estimated_bitrate_bps_ > 0)
178 << "SetEstimatedBitrate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000179
Erik Språngad113e52015-11-26 16:26:12 +0100180 int64_t now_ms = clock_->TimeInMilliseconds();
philipel4a1ec1e2016-08-15 11:51:06 -0700181 prober_->OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100182
sprang0a43fef2015-11-20 09:00:37 -0800183 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100184 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000185
philipel9981bd92017-09-26 17:16:06 +0200186 packets_->Push(PacketQueue::Packet(priority, ssrc, sequence_number,
187 capture_time_ms, now_ms, bytes,
188 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000189}
190
Alex Narest78609d52017-10-20 10:37:47 +0200191void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
192 rtc::CritScope cs(&critsect_);
193 account_for_audio_ = account_for_audio;
194}
195
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000196int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700197 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800198 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
perkjec81bcd2016-05-11 06:01:13 -0700199 return static_cast<int64_t>(packets_->SizeInBytes() * 8 /
200 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000201}
202
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000203rtc::Optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime()
204 const {
205 rtc::CritScope cs(&critsect_);
206 return alr_detector_->GetApplicationLimitedRegionStartTime();
207}
208
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000209size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700210 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000211 return packets_->SizeInPackets();
212}
213
asaperssonfc5e81c2017-04-19 23:28:53 -0700214int64_t PacedSender::FirstSentPacketTimeMs() const {
215 rtc::CritScope cs(&critsect_);
216 return first_sent_packet_ms_;
217}
218
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000219int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700220 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000221
sprang0a43fef2015-11-20 09:00:37 -0800222 int64_t oldest_packet = packets_->OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000223 if (oldest_packet == 0)
224 return 0;
225
226 return clock_->TimeInMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000227}
228
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000229int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700230 rtc::CritScope cs(&critsect_);
Danil Chapovalov516036f2018-02-14 10:21:27 +0000231 int64_t elapsed_time_us = clock_->TimeInMicroseconds() - time_last_update_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700232 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
233 // When paused we wake up every 500 ms to send a padding packet to ensure
234 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700235 if (paused_)
stefan9e117c5e12017-08-16 08:16:25 -0700236 return std::max<int64_t>(kPausedPacketIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700237
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000238 if (prober_->IsProbing()) {
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000239 int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800240 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000241 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000242 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000243 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000244}
245
pbosa26ac922016-02-25 04:50:01 -0800246void PacedSender::Process() {
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000247 int64_t now_us = clock_->TimeInMicroseconds();
kthelgason6bfe49c2017-03-30 01:14:41 -0700248 rtc::CritScope cs(&critsect_);
Danil Chapovalov516036f2018-02-14 10:21:27 +0000249 int64_t elapsed_time_ms = std::min(
250 kMaxIntervalTimeMs, (now_us - time_last_update_us_ + 500) / 1000);
251 int target_bitrate_kbps = pacing_bitrate_kbps_;
stefan9e117c5e12017-08-16 08:16:25 -0700252
253 if (paused_) {
Danil Chapovalov516036f2018-02-14 10:21:27 +0000254 PacedPacketInfo pacing_info;
255 time_last_update_us_ = now_us;
stefan9e117c5e12017-08-16 08:16:25 -0700256 // We can not send padding unless a normal packet has first been sent. If we
257 // do, timestamps get messed up.
Danil Chapovalov516036f2018-02-14 10:21:27 +0000258 if (packet_counter_ == 0)
259 return;
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000260 size_t bytes_sent = SendPadding(1, pacing_info);
261 alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms);
stefan9e117c5e12017-08-16 08:16:25 -0700262 return;
263 }
264
265 if (elapsed_time_ms > 0) {
sprang0a43fef2015-11-20 09:00:37 -0800266 size_t queue_size_bytes = packets_->SizeInBytes();
267 if (queue_size_bytes > 0) {
268 // Assuming equal size packets and input/output rate, the average packet
269 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
270 // time constraint shall be met. Determine bitrate needed for that.
Erik Språngad113e52015-11-26 16:26:12 +0100271 packets_->UpdateQueueTime(clock_->TimeInMilliseconds());
sprang0a43fef2015-11-20 09:00:37 -0800272 int64_t avg_time_left_ms = std::max<int64_t>(
sprang89c4a7e2017-06-30 13:27:40 -0700273 1, queue_time_limit - packets_->AverageQueueTimeMs());
sprang0a43fef2015-11-20 09:00:37 -0800274 int min_bitrate_needed_kbps =
275 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
276 if (min_bitrate_needed_kbps > target_bitrate_kbps)
277 target_bitrate_kbps = min_bitrate_needed_kbps;
278 }
279
280 media_budget_->set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700281 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000282 }
philipela1ed0b32016-06-01 06:31:17 -0700283
Danil Chapovalov516036f2018-02-14 10:21:27 +0000284 time_last_update_us_ = now_us;
stefan9e117c5e12017-08-16 08:16:25 -0700285
philipel1a93cde2016-06-03 01:41:45 -0700286 bool is_probing = prober_->IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800287 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700288 size_t bytes_sent = 0;
289 size_t recommended_probe_size = 0;
290 if (is_probing) {
philipelc7bf32a2017-02-17 03:59:43 -0800291 pacing_info = prober_->CurrentCluster();
isheriffcc5903e2016-10-04 08:29:38 -0700292 recommended_probe_size = prober_->RecommendedMinProbeSize();
293 }
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000294 while (!packets_->Empty()) {
Peter Boströme23e7372015-10-08 11:44:14 +0200295 // Since we need to release the lock in order to send, we first pop the
296 // element from the priority queue but keep it in storage, so that we can
297 // reinsert it if send fails.
philipel9981bd92017-09-26 17:16:06 +0200298 const PacketQueue::Packet& packet = packets_->BeginPop();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100299
philipelc7bf32a2017-02-17 03:59:43 -0800300 if (SendPacket(packet, pacing_info)) {
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100301 // Send succeeded, remove it from the queue.
Danil Chapovalov516036f2018-02-14 10:21:27 +0000302 if (first_sent_packet_ms_ == -1)
303 first_sent_packet_ms_ = clock_->TimeInMilliseconds();
304 bytes_sent += packet.bytes;
Peter Boströme23e7372015-10-08 11:44:14 +0200305 packets_->FinalizePop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700306 if (is_probing && bytes_sent > recommended_probe_size)
307 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200308 } else {
309 // Send failed, put it back into the queue.
310 packets_->CancelPop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700311 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200312 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000313 }
Peter Boströme23e7372015-10-08 11:44:14 +0200314
stefan9e117c5e12017-08-16 08:16:25 -0700315 if (packets_->Empty()) {
isheriffcc5903e2016-10-04 08:29:38 -0700316 // We can not send padding unless a normal packet has first been sent. If we
317 // do, timestamps get messed up.
318 if (packet_counter_ > 0) {
319 int padding_needed =
320 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
321 : padding_budget_->bytes_remaining());
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000322 if (padding_needed > 0)
philipelc7bf32a2017-02-17 03:59:43 -0800323 bytes_sent += SendPadding(padding_needed, pacing_info);
isheriffcc5903e2016-10-04 08:29:38 -0700324 }
perkj71ee44c2016-06-15 00:47:53 -0700325 }
philipelb61927c2017-02-28 07:05:23 -0800326 if (is_probing) {
327 probing_send_failure_ = bytes_sent == 0;
328 if (!probing_send_failure_)
329 prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent);
330 }
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000331 alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000332}
333
tommi919dce22017-03-15 07:45:36 -0700334void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100335 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << std::hex << process_thread;
tommi919dce22017-03-15 07:45:36 -0700336 process_thread_ = process_thread;
337}
338
philipel9981bd92017-09-26 17:16:06 +0200339bool PacedSender::SendPacket(const PacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800340 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700341 RTC_DCHECK(!paused_);
stefan099110c2017-02-01 03:57:42 -0800342 if (media_budget_->bytes_remaining() == 0 &&
philipelc7bf32a2017-02-17 03:59:43 -0800343 pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) {
stefan099110c2017-02-01 03:57:42 -0800344 return false;
terelius8b70faf2016-08-01 09:47:31 -0700345 }
philipelc7bf32a2017-02-17 03:59:43 -0800346
kthelgason6bfe49c2017-03-30 01:14:41 -0700347 critsect_.Leave();
perkjec81bcd2016-05-11 06:01:13 -0700348 const bool success = packet_sender_->TimeToSendPacket(
349 packet.ssrc, packet.sequence_number, packet.capture_time_ms,
philipelc7bf32a2017-02-17 03:59:43 -0800350 packet.retransmission, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700351 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000352
Peter Boström7ecc1632016-03-02 14:22:25 +0100353 if (success) {
Alex Narest78609d52017-10-20 10:37:47 +0200354 if (packet.priority != kHighPriority || account_for_audio_) {
Peter Boström7ecc1632016-03-02 14:22:25 +0100355 // Update media bytes sent.
eladalon32040ef2017-08-02 06:29:00 -0700356 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
357 // situations where nothing actually ended up being sent to the network,
358 // and we probably don't want to update the budget in such cases.
359 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
isheriff31687812016-10-04 08:43:09 -0700360 UpdateBudgetWithBytesSent(packet.bytes);
Peter Boström7ecc1632016-03-02 14:22:25 +0100361 }
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000362 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000363
364 return success;
365}
366
philipelc7bf32a2017-02-17 03:59:43 -0800367size_t PacedSender::SendPadding(size_t padding_needed,
368 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700369 RTC_DCHECK_GT(packet_counter_, 0);
kthelgason6bfe49c2017-03-30 01:14:41 -0700370 critsect_.Leave();
philipela1ed0b32016-06-01 06:31:17 -0700371 size_t bytes_sent =
philipelc7bf32a2017-02-17 03:59:43 -0800372 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700373 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000374
Stefan Holmer01b48882015-05-05 10:21:24 +0200375 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700376 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200377 }
isheriffcc5903e2016-10-04 08:29:38 -0700378 return bytes_sent;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000379}
380
isheriff31687812016-10-04 08:43:09 -0700381void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000382 media_budget_->IncreaseBudget(delta_time_ms);
383 padding_budget_->IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000384}
isheriff31687812016-10-04 08:43:09 -0700385
386void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
387 media_budget_->UseBudget(bytes_sent);
388 padding_budget_->UseBudget(bytes_sent);
389}
sprang89c4a7e2017-06-30 13:27:40 -0700390
Sebastian Janssonea86bb72018-02-14 16:53:38 +0000391void PacedSender::SetPacingFactor(float pacing_factor) {
392 rtc::CritScope cs(&critsect_);
393 pacing_factor_ = pacing_factor;
394 // Make sure new padding factor is applied immediately, otherwise we need to
395 // wait for the send bitrate estimate to be updated before this takes effect.
396 SetEstimatedBitrate(estimated_bitrate_bps_);
397}
398
sprang89c4a7e2017-06-30 13:27:40 -0700399void PacedSender::SetQueueTimeLimit(int limit_ms) {
400 rtc::CritScope cs(&critsect_);
401 queue_time_limit = limit_ms;
402}
403
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000404} // namespace webrtc