blob: 862f4e189f778993a1396671f1138ba4f5e4613e [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/pacing/bitrate_prober.h"
22#include "modules/pacing/interval_budget.h"
Sebastian Janssonb5374962018-02-07 13:26:38 +010023#include "modules/pacing/packet_queue.h"
24#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;
stefan9e117c5e12017-08-16 08:16:25 -070036const int64_t kPausedPacketIntervalMs = 500;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000037
38// Upper cap on process interval, in case process has not been called in a long
39// time.
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000040const int64_t kMaxIntervalTimeMs = 30;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000041
Ilya Nikolaevskiy2ffe3e82018-01-17 19:57:24 +000042const char kRoundRobinExperimentName[] = "WebRTC-RoundRobinPacing";
43
44bool IsRoundRobinPacingEnabled() {
45 return webrtc::field_trial::IsEnabled(kRoundRobinExperimentName) || (
46 !webrtc::field_trial::IsDisabled(kRoundRobinExperimentName) &&
47 webrtc::runtime_enabled_features::IsFeatureEnabled(
48 webrtc::runtime_enabled_features::kDualStreamModeFeatureName));
49}
50
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000051} // namespace
52
53namespace webrtc {
pwestin@webrtc.org52aa0192013-04-25 17:35:56 +000054
sprang0a43fef2015-11-20 09:00:37 -080055const int64_t PacedSender::kMaxQueueLengthMs = 2000;
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000056
Sebastian Janssonb5374962018-02-07 13:26:38 +010057namespace {
58std::unique_ptr<PacketQueueInterface> CreatePacketQueue(const Clock* clock,
59 bool round_robin) {
60 if (round_robin) {
61 return rtc::MakeUnique<RoundRobinPacketQueue>(clock);
62 } else {
63 return rtc::MakeUnique<PacketQueue>(clock);
64 }
65}
66} // namespace
67
philipelc3b3f7a2017-03-29 01:23:13 -070068PacedSender::PacedSender(const Clock* clock,
69 PacketSender* packet_sender,
Sebastian Janssonb5374962018-02-07 13:26:38 +010070 RtcEventLog* event_log)
71 : PacedSender(clock,
72 packet_sender,
73 event_log,
74 CreatePacketQueue(clock, IsRoundRobinPacingEnabled())) {}
Niels Möller9d4af012017-10-31 09:54:50 +010075
76PacedSender::PacedSender(const Clock* clock,
77 PacketSender* packet_sender,
78 RtcEventLog* event_log,
Sebastian Janssonb5374962018-02-07 13:26:38 +010079 std::unique_ptr<PacketQueueInterface> packets)
stefan@webrtc.org88e0dda2014-07-04 09:20:42 +000080 : clock_(clock),
perkjec81bcd2016-05-11 06:01:13 -070081 packet_sender_(packet_sender),
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +000082 paused_(false),
Niels Möller712048c2017-10-18 13:08:22 +020083 media_budget_(rtc::MakeUnique<IntervalBudget>(0)),
84 padding_budget_(rtc::MakeUnique<IntervalBudget>(0)),
85 prober_(rtc::MakeUnique<BitrateProber>(event_log)),
philipelb61927c2017-02-28 07:05:23 -080086 probing_send_failure_(false),
perkjec81bcd2016-05-11 06:01:13 -070087 pacing_bitrate_kbps_(0),
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +000088 time_last_update_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070089 first_sent_packet_ms_(-1),
Niels Möller9d4af012017-10-31 09:54:50 +010090 packets_(std::move(packets)),
sprang89c4a7e2017-06-30 13:27:40 -070091 packet_counter_(0),
Alex Narest78609d52017-10-20 10:37:47 +020092 queue_time_limit(kMaxQueueLengthMs),
93 account_for_audio_(false) {
isheriff31687812016-10-04 08:43:09 -070094 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000095}
96
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +000097PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000098
philipelfd58b612017-01-04 07:05:25 -080099void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700100 rtc::CritScope cs(&critsect_);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100101 prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +0200102}
103
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000104void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -0700105 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700106 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700107 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100108 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -0700109 paused_ = true;
sprangddcfb9f2017-08-16 05:38:49 -0700110 packets_->SetPauseState(true, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700111 }
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100112 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700113 // Tell the process thread to call our TimeUntilNextProcess() method to get
114 // a new (longer) estimate for when to call Process().
115 if (process_thread_)
116 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000117}
118
119void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700120 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700121 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700122 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100123 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700124 paused_ = false;
sprangddcfb9f2017-08-16 05:38:49 -0700125 packets_->SetPauseState(false, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700126 }
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100127 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700128 // Tell the process thread to call our TimeUntilNextProcess() method to
129 // refresh the estimate for when to call Process().
130 if (process_thread_)
131 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000132}
133
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000134void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700135 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200136 RTC_CHECK_EQ(0, packet_counter_);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700137 prober_->SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000138}
139
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100140void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
141 uint32_t padding_rate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700142 rtc::CritScope cs(&critsect_);
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100143 RTC_DCHECK(pacing_rate_bps > 0);
144 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
145 padding_budget_->set_target_rate_kbps(padding_rate_bps / 1000);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000146}
147
Peter Boströme23e7372015-10-08 11:44:14 +0200148void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
149 uint32_t ssrc,
150 uint16_t sequence_number,
151 int64_t capture_time_ms,
152 size_t bytes,
153 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700154 rtc::CritScope cs(&critsect_);
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100155 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
156 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000157
Erik Språngad113e52015-11-26 16:26:12 +0100158 int64_t now_ms = clock_->TimeInMilliseconds();
philipel4a1ec1e2016-08-15 11:51:06 -0700159 prober_->OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100160
sprang0a43fef2015-11-20 09:00:37 -0800161 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100162 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000163
philipel9981bd92017-09-26 17:16:06 +0200164 packets_->Push(PacketQueue::Packet(priority, ssrc, sequence_number,
165 capture_time_ms, now_ms, bytes,
166 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000167}
168
Alex Narest78609d52017-10-20 10:37:47 +0200169void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
170 rtc::CritScope cs(&critsect_);
171 account_for_audio_ = account_for_audio;
172}
173
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000174int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700175 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800176 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
perkjec81bcd2016-05-11 06:01:13 -0700177 return static_cast<int64_t>(packets_->SizeInBytes() * 8 /
178 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000179}
180
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000181size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700182 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000183 return packets_->SizeInPackets();
184}
185
asaperssonfc5e81c2017-04-19 23:28:53 -0700186int64_t PacedSender::FirstSentPacketTimeMs() const {
187 rtc::CritScope cs(&critsect_);
188 return first_sent_packet_ms_;
189}
190
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000191int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700192 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000193
sprang0a43fef2015-11-20 09:00:37 -0800194 int64_t oldest_packet = packets_->OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000195 if (oldest_packet == 0)
196 return 0;
197
198 return clock_->TimeInMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000199}
200
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000201int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700202 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700203 int64_t elapsed_time_us = clock_->TimeInMicroseconds() - time_last_update_us_;
204 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
205 // When paused we wake up every 500 ms to send a padding packet to ensure
206 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700207 if (paused_)
stefan9e117c5e12017-08-16 08:16:25 -0700208 return std::max<int64_t>(kPausedPacketIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700209
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000210 if (prober_->IsProbing()) {
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000211 int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800212 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000213 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000214 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000215 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000216}
217
pbosa26ac922016-02-25 04:50:01 -0800218void PacedSender::Process() {
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000219 int64_t now_us = clock_->TimeInMicroseconds();
kthelgason6bfe49c2017-03-30 01:14:41 -0700220 rtc::CritScope cs(&critsect_);
tschumim5afa3f22017-08-18 03:38:49 -0700221 int64_t elapsed_time_ms = std::min(
222 kMaxIntervalTimeMs, (now_us - time_last_update_us_ + 500) / 1000);
perkjec81bcd2016-05-11 06:01:13 -0700223 int target_bitrate_kbps = pacing_bitrate_kbps_;
stefan9e117c5e12017-08-16 08:16:25 -0700224
225 if (paused_) {
226 PacedPacketInfo pacing_info;
227 time_last_update_us_ = now_us;
228 // We can not send padding unless a normal packet has first been sent. If we
229 // do, timestamps get messed up.
230 if (packet_counter_ == 0)
231 return;
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100232 SendPadding(1, pacing_info);
stefan9e117c5e12017-08-16 08:16:25 -0700233 return;
234 }
235
236 if (elapsed_time_ms > 0) {
sprang0a43fef2015-11-20 09:00:37 -0800237 size_t queue_size_bytes = packets_->SizeInBytes();
238 if (queue_size_bytes > 0) {
239 // Assuming equal size packets and input/output rate, the average packet
240 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
241 // time constraint shall be met. Determine bitrate needed for that.
Erik Språngad113e52015-11-26 16:26:12 +0100242 packets_->UpdateQueueTime(clock_->TimeInMilliseconds());
sprang0a43fef2015-11-20 09:00:37 -0800243 int64_t avg_time_left_ms = std::max<int64_t>(
sprang89c4a7e2017-06-30 13:27:40 -0700244 1, queue_time_limit - packets_->AverageQueueTimeMs());
sprang0a43fef2015-11-20 09:00:37 -0800245 int min_bitrate_needed_kbps =
246 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
247 if (min_bitrate_needed_kbps > target_bitrate_kbps)
248 target_bitrate_kbps = min_bitrate_needed_kbps;
249 }
250
251 media_budget_->set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700252 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000253 }
philipela1ed0b32016-06-01 06:31:17 -0700254
stefan9e117c5e12017-08-16 08:16:25 -0700255 time_last_update_us_ = now_us;
256
philipel1a93cde2016-06-03 01:41:45 -0700257 bool is_probing = prober_->IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800258 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700259 size_t bytes_sent = 0;
260 size_t recommended_probe_size = 0;
261 if (is_probing) {
philipelc7bf32a2017-02-17 03:59:43 -0800262 pacing_info = prober_->CurrentCluster();
isheriffcc5903e2016-10-04 08:29:38 -0700263 recommended_probe_size = prober_->RecommendedMinProbeSize();
264 }
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100265 while (!packets_->Empty() && !paused_) {
Peter Boströme23e7372015-10-08 11:44:14 +0200266 // Since we need to release the lock in order to send, we first pop the
267 // element from the priority queue but keep it in storage, so that we can
268 // reinsert it if send fails.
philipel9981bd92017-09-26 17:16:06 +0200269 const PacketQueue::Packet& packet = packets_->BeginPop();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100270
philipelc7bf32a2017-02-17 03:59:43 -0800271 if (SendPacket(packet, pacing_info)) {
Peter Boströme23e7372015-10-08 11:44:14 +0200272 // Send succeeded, remove it from the queue.
asaperssonfc5e81c2017-04-19 23:28:53 -0700273 if (first_sent_packet_ms_ == -1)
274 first_sent_packet_ms_ = clock_->TimeInMilliseconds();
isheriffcc5903e2016-10-04 08:29:38 -0700275 bytes_sent += packet.bytes;
Peter Boströme23e7372015-10-08 11:44:14 +0200276 packets_->FinalizePop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700277 if (is_probing && bytes_sent > recommended_probe_size)
278 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200279 } else {
280 // Send failed, put it back into the queue.
281 packets_->CancelPop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700282 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200283 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000284 }
Peter Boströme23e7372015-10-08 11:44:14 +0200285
stefan9e117c5e12017-08-16 08:16:25 -0700286 if (packets_->Empty()) {
isheriffcc5903e2016-10-04 08:29:38 -0700287 // We can not send padding unless a normal packet has first been sent. If we
288 // do, timestamps get messed up.
289 if (packet_counter_ > 0) {
290 int padding_needed =
291 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
292 : padding_budget_->bytes_remaining());
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100293 if (padding_needed > 0) {
philipelc7bf32a2017-02-17 03:59:43 -0800294 bytes_sent += SendPadding(padding_needed, pacing_info);
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100295 }
isheriffcc5903e2016-10-04 08:29:38 -0700296 }
perkj71ee44c2016-06-15 00:47:53 -0700297 }
philipelb61927c2017-02-28 07:05:23 -0800298 if (is_probing) {
299 probing_send_failure_ = bytes_sent == 0;
300 if (!probing_send_failure_)
301 prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent);
302 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000303}
304
tommi919dce22017-03-15 07:45:36 -0700305void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100306 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << std::hex << process_thread;
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100307 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700308 process_thread_ = process_thread;
309}
310
philipel9981bd92017-09-26 17:16:06 +0200311bool PacedSender::SendPacket(const PacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800312 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700313 RTC_DCHECK(!paused_);
stefan099110c2017-02-01 03:57:42 -0800314 if (media_budget_->bytes_remaining() == 0 &&
philipelc7bf32a2017-02-17 03:59:43 -0800315 pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) {
stefan099110c2017-02-01 03:57:42 -0800316 return false;
terelius8b70faf2016-08-01 09:47:31 -0700317 }
philipelc7bf32a2017-02-17 03:59:43 -0800318
kthelgason6bfe49c2017-03-30 01:14:41 -0700319 critsect_.Leave();
perkjec81bcd2016-05-11 06:01:13 -0700320 const bool success = packet_sender_->TimeToSendPacket(
321 packet.ssrc, packet.sequence_number, packet.capture_time_ms,
philipelc7bf32a2017-02-17 03:59:43 -0800322 packet.retransmission, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700323 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000324
Peter Boström7ecc1632016-03-02 14:22:25 +0100325 if (success) {
Alex Narest78609d52017-10-20 10:37:47 +0200326 if (packet.priority != kHighPriority || account_for_audio_) {
Peter Boström7ecc1632016-03-02 14:22:25 +0100327 // Update media bytes sent.
eladalon32040ef2017-08-02 06:29:00 -0700328 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
329 // situations where nothing actually ended up being sent to the network,
330 // and we probably don't want to update the budget in such cases.
331 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
isheriff31687812016-10-04 08:43:09 -0700332 UpdateBudgetWithBytesSent(packet.bytes);
Peter Boström7ecc1632016-03-02 14:22:25 +0100333 }
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000334 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000335
336 return success;
337}
338
philipelc7bf32a2017-02-17 03:59:43 -0800339size_t PacedSender::SendPadding(size_t padding_needed,
340 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700341 RTC_DCHECK_GT(packet_counter_, 0);
kthelgason6bfe49c2017-03-30 01:14:41 -0700342 critsect_.Leave();
philipela1ed0b32016-06-01 06:31:17 -0700343 size_t bytes_sent =
philipelc7bf32a2017-02-17 03:59:43 -0800344 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700345 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000346
Stefan Holmer01b48882015-05-05 10:21:24 +0200347 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700348 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200349 }
isheriffcc5903e2016-10-04 08:29:38 -0700350 return bytes_sent;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000351}
352
isheriff31687812016-10-04 08:43:09 -0700353void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000354 media_budget_->IncreaseBudget(delta_time_ms);
355 padding_budget_->IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000356}
isheriff31687812016-10-04 08:43:09 -0700357
358void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
359 media_budget_->UseBudget(bytes_sent);
360 padding_budget_->UseBudget(bytes_sent);
361}
sprang89c4a7e2017-06-30 13:27:40 -0700362
sprang89c4a7e2017-06-30 13:27:40 -0700363void PacedSender::SetQueueTimeLimit(int limit_ms) {
364 rtc::CritScope cs(&critsect_);
365 queue_time_limit = limit_ms;
366}
367
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000368} // namespace webrtc