blob: c35c50c67b8c3b5056104c494c7835438f303bd1 [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),
Sebastian Jansson18cf4b62018-02-13 13:54:33 +010088 time_last_process_us_(clock->TimeInMicroseconds()),
89 last_send_time_us_(clock->TimeInMicroseconds()),
asaperssonfc5e81c2017-04-19 23:28:53 -070090 first_sent_packet_ms_(-1),
Niels Möller9d4af012017-10-31 09:54:50 +010091 packets_(std::move(packets)),
sprang89c4a7e2017-06-30 13:27:40 -070092 packet_counter_(0),
Alex Narest78609d52017-10-20 10:37:47 +020093 queue_time_limit(kMaxQueueLengthMs),
94 account_for_audio_(false) {
isheriff31687812016-10-04 08:43:09 -070095 UpdateBudgetWithElapsedTime(kMinPacketLimitMs);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000096}
97
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +000098PacedSender::~PacedSender() {}
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +000099
philipelfd58b612017-01-04 07:05:25 -0800100void PacedSender::CreateProbeCluster(int bitrate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700101 rtc::CritScope cs(&critsect_);
Stefan Holmer0e3213a2017-02-08 15:19:05 +0100102 prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds());
philipeleb680ea2016-08-17 11:11:59 +0200103}
104
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000105void PacedSender::Pause() {
tommi919dce22017-03-15 07:45:36 -0700106 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700107 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700108 if (!paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100109 RTC_LOG(LS_INFO) << "PacedSender paused.";
tommi919dce22017-03-15 07:45:36 -0700110 paused_ = true;
sprangddcfb9f2017-08-16 05:38:49 -0700111 packets_->SetPauseState(true, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700112 }
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100113 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700114 // Tell the process thread to call our TimeUntilNextProcess() method to get
115 // a new (longer) estimate for when to call Process().
116 if (process_thread_)
117 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000118}
119
120void PacedSender::Resume() {
tommi919dce22017-03-15 07:45:36 -0700121 {
kthelgason6bfe49c2017-03-30 01:14:41 -0700122 rtc::CritScope cs(&critsect_);
stefan9e117c5e12017-08-16 08:16:25 -0700123 if (paused_)
Mirko Bonadei675513b2017-11-09 11:09:25 +0100124 RTC_LOG(LS_INFO) << "PacedSender resumed.";
tommi919dce22017-03-15 07:45:36 -0700125 paused_ = false;
sprangddcfb9f2017-08-16 05:38:49 -0700126 packets_->SetPauseState(false, clock_->TimeInMilliseconds());
tommi919dce22017-03-15 07:45:36 -0700127 }
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100128 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700129 // Tell the process thread to call our TimeUntilNextProcess() method to
130 // refresh the estimate for when to call Process().
131 if (process_thread_)
132 process_thread_->WakeUp(this);
pwestin@webrtc.orgdb418562013-03-22 23:39:29 +0000133}
134
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000135void PacedSender::SetProbingEnabled(bool enabled) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700136 rtc::CritScope cs(&critsect_);
Elad Alon44b1fa42017-10-17 14:17:54 +0200137 RTC_CHECK_EQ(0, packet_counter_);
Irfan Sheriff6e11efa2016-08-02 12:57:37 -0700138 prober_->SetEnabled(enabled);
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000139}
140
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100141void PacedSender::SetPacingRates(uint32_t pacing_rate_bps,
142 uint32_t padding_rate_bps) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700143 rtc::CritScope cs(&critsect_);
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100144 RTC_DCHECK(pacing_rate_bps > 0);
145 pacing_bitrate_kbps_ = pacing_rate_bps / 1000;
146 padding_budget_->set_target_rate_kbps(padding_rate_bps / 1000);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000147}
148
Peter Boströme23e7372015-10-08 11:44:14 +0200149void PacedSender::InsertPacket(RtpPacketSender::Priority priority,
150 uint32_t ssrc,
151 uint16_t sequence_number,
152 int64_t capture_time_ms,
153 size_t bytes,
154 bool retransmission) {
kthelgason6bfe49c2017-03-30 01:14:41 -0700155 rtc::CritScope cs(&critsect_);
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100156 RTC_DCHECK(pacing_bitrate_kbps_ > 0)
157 << "SetPacingRate must be called before InsertPacket.";
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000158
Erik Språngad113e52015-11-26 16:26:12 +0100159 int64_t now_ms = clock_->TimeInMilliseconds();
philipel4a1ec1e2016-08-15 11:51:06 -0700160 prober_->OnIncomingPacket(bytes);
Peter Boström0453ef82016-02-16 16:23:08 +0100161
sprang0a43fef2015-11-20 09:00:37 -0800162 if (capture_time_ms < 0)
Erik Språngad113e52015-11-26 16:26:12 +0100163 capture_time_ms = now_ms;
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000164
philipel9981bd92017-09-26 17:16:06 +0200165 packets_->Push(PacketQueue::Packet(priority, ssrc, sequence_number,
166 capture_time_ms, now_ms, bytes,
167 retransmission, packet_counter_++));
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000168}
169
Alex Narest78609d52017-10-20 10:37:47 +0200170void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
171 rtc::CritScope cs(&critsect_);
172 account_for_audio_ = account_for_audio;
173}
174
pkasting@chromium.org2656bf82014-11-17 22:21:14 +0000175int64_t PacedSender::ExpectedQueueTimeMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700176 rtc::CritScope cs(&critsect_);
kwibergaf476c72016-11-28 15:21:39 -0800177 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0);
perkjec81bcd2016-05-11 06:01:13 -0700178 return static_cast<int64_t>(packets_->SizeInBytes() * 8 /
179 pacing_bitrate_kbps_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000180}
181
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000182size_t PacedSender::QueueSizePackets() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700183 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000184 return packets_->SizeInPackets();
185}
186
asaperssonfc5e81c2017-04-19 23:28:53 -0700187int64_t PacedSender::FirstSentPacketTimeMs() const {
188 rtc::CritScope cs(&critsect_);
189 return first_sent_packet_ms_;
190}
191
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000192int64_t PacedSender::QueueInMs() const {
kthelgason6bfe49c2017-03-30 01:14:41 -0700193 rtc::CritScope cs(&critsect_);
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000194
sprang0a43fef2015-11-20 09:00:37 -0800195 int64_t oldest_packet = packets_->OldestEnqueueTimeMs();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000196 if (oldest_packet == 0)
197 return 0;
198
199 return clock_->TimeInMilliseconds() - oldest_packet;
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000200}
201
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000202int64_t PacedSender::TimeUntilNextProcess() {
kthelgason6bfe49c2017-03-30 01:14:41 -0700203 rtc::CritScope cs(&critsect_);
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100204 int64_t elapsed_time_us =
205 clock_->TimeInMicroseconds() - time_last_process_us_;
stefan9e117c5e12017-08-16 08:16:25 -0700206 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000;
207 // When paused we wake up every 500 ms to send a padding packet to ensure
208 // we won't get stuck in the paused state due to no feedback being received.
tommi919dce22017-03-15 07:45:36 -0700209 if (paused_)
stefan9e117c5e12017-08-16 08:16:25 -0700210 return std::max<int64_t>(kPausedPacketIntervalMs - elapsed_time_ms, 0);
tommi919dce22017-03-15 07:45:36 -0700211
stefan@webrtc.org82462aa2014-10-23 11:57:05 +0000212 if (prober_->IsProbing()) {
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000213 int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds());
philipelb61927c2017-02-28 07:05:23 -0800214 if (ret > 0 || (ret == 0 && !probing_send_failure_))
stefan@webrtc.orge9f0f592015-02-16 15:47:51 +0000215 return ret;
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000216 }
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +0000217 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000218}
219
pbosa26ac922016-02-25 04:50:01 -0800220void PacedSender::Process() {
stefan@webrtc.org89fd1e82014-07-15 16:40:38 +0000221 int64_t now_us = clock_->TimeInMicroseconds();
kthelgason6bfe49c2017-03-30 01:14:41 -0700222 rtc::CritScope cs(&critsect_);
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100223 time_last_process_us_ = now_us;
224 int64_t elapsed_time_ms = (now_us - last_send_time_us_ + 500) / 1000;
stefan9e117c5e12017-08-16 08:16:25 -0700225
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100226 // When paused we send a padding packet every 500 ms to ensure we won't get
227 // stuck in the paused state due to no feedback being received.
stefan9e117c5e12017-08-16 08:16:25 -0700228 if (paused_) {
stefan9e117c5e12017-08-16 08:16:25 -0700229 // We can not send padding unless a normal packet has first been sent. If we
230 // do, timestamps get messed up.
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100231 if (elapsed_time_ms >= kPausedPacketIntervalMs && packet_counter_ > 0) {
232 PacedPacketInfo pacing_info;
233 SendPadding(1, pacing_info);
234 last_send_time_us_ = clock_->TimeInMicroseconds();
235 }
stefan9e117c5e12017-08-16 08:16:25 -0700236 return;
237 }
238
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100239 int target_bitrate_kbps = pacing_bitrate_kbps_;
stefan9e117c5e12017-08-16 08:16:25 -0700240 if (elapsed_time_ms > 0) {
sprang0a43fef2015-11-20 09:00:37 -0800241 size_t queue_size_bytes = packets_->SizeInBytes();
242 if (queue_size_bytes > 0) {
243 // Assuming equal size packets and input/output rate, the average packet
244 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
245 // time constraint shall be met. Determine bitrate needed for that.
Erik Språngad113e52015-11-26 16:26:12 +0100246 packets_->UpdateQueueTime(clock_->TimeInMilliseconds());
sprang0a43fef2015-11-20 09:00:37 -0800247 int64_t avg_time_left_ms = std::max<int64_t>(
sprang89c4a7e2017-06-30 13:27:40 -0700248 1, queue_time_limit - packets_->AverageQueueTimeMs());
sprang0a43fef2015-11-20 09:00:37 -0800249 int min_bitrate_needed_kbps =
250 static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms);
251 if (min_bitrate_needed_kbps > target_bitrate_kbps)
252 target_bitrate_kbps = min_bitrate_needed_kbps;
253 }
254
255 media_budget_->set_target_rate_kbps(target_bitrate_kbps);
isheriff31687812016-10-04 08:43:09 -0700256 UpdateBudgetWithElapsedTime(elapsed_time_ms);
stefan@webrtc.org80865fd2013-08-09 11:31:23 +0000257 }
philipela1ed0b32016-06-01 06:31:17 -0700258
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100259 last_send_time_us_ = clock_->TimeInMicroseconds();
stefan9e117c5e12017-08-16 08:16:25 -0700260
philipel1a93cde2016-06-03 01:41:45 -0700261 bool is_probing = prober_->IsProbing();
philipelc7bf32a2017-02-17 03:59:43 -0800262 PacedPacketInfo pacing_info;
isheriffcc5903e2016-10-04 08:29:38 -0700263 size_t bytes_sent = 0;
264 size_t recommended_probe_size = 0;
265 if (is_probing) {
philipelc7bf32a2017-02-17 03:59:43 -0800266 pacing_info = prober_->CurrentCluster();
isheriffcc5903e2016-10-04 08:29:38 -0700267 recommended_probe_size = prober_->RecommendedMinProbeSize();
268 }
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100269 // The paused state is checked in the loop since SendPacket leaves the
270 // critical section allowing the paused state to be changed from other code.
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100271 while (!packets_->Empty() && !paused_) {
Peter Boströme23e7372015-10-08 11:44:14 +0200272 // Since we need to release the lock in order to send, we first pop the
273 // element from the priority queue but keep it in storage, so that we can
274 // reinsert it if send fails.
philipel9981bd92017-09-26 17:16:06 +0200275 const PacketQueue::Packet& packet = packets_->BeginPop();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100276
philipelc7bf32a2017-02-17 03:59:43 -0800277 if (SendPacket(packet, pacing_info)) {
isheriffcc5903e2016-10-04 08:29:38 -0700278 bytes_sent += packet.bytes;
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100279 // Send succeeded, remove it from the queue.
Peter Boströme23e7372015-10-08 11:44:14 +0200280 packets_->FinalizePop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700281 if (is_probing && bytes_sent > recommended_probe_size)
282 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200283 } else {
284 // Send failed, put it back into the queue.
285 packets_->CancelPop(packet);
isheriffcc5903e2016-10-04 08:29:38 -0700286 break;
Peter Boströme23e7372015-10-08 11:44:14 +0200287 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000288 }
Peter Boströme23e7372015-10-08 11:44:14 +0200289
stefan9e117c5e12017-08-16 08:16:25 -0700290 if (packets_->Empty()) {
isheriffcc5903e2016-10-04 08:29:38 -0700291 // We can not send padding unless a normal packet has first been sent. If we
292 // do, timestamps get messed up.
293 if (packet_counter_ > 0) {
294 int padding_needed =
295 static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent)
296 : padding_budget_->bytes_remaining());
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100297 if (padding_needed > 0) {
philipelc7bf32a2017-02-17 03:59:43 -0800298 bytes_sent += SendPadding(padding_needed, pacing_info);
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100299 }
isheriffcc5903e2016-10-04 08:29:38 -0700300 }
perkj71ee44c2016-06-15 00:47:53 -0700301 }
philipelb61927c2017-02-28 07:05:23 -0800302 if (is_probing) {
303 probing_send_failure_ = bytes_sent == 0;
304 if (!probing_send_failure_)
305 prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent);
306 }
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000307}
308
tommi919dce22017-03-15 07:45:36 -0700309void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100310 RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << std::hex << process_thread;
Sebastian Jansson57daeb72018-02-05 17:15:09 +0100311 rtc::CritScope cs(&process_thread_lock_);
tommi919dce22017-03-15 07:45:36 -0700312 process_thread_ = process_thread;
313}
314
philipel9981bd92017-09-26 17:16:06 +0200315bool PacedSender::SendPacket(const PacketQueue::Packet& packet,
philipelc7bf32a2017-02-17 03:59:43 -0800316 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700317 RTC_DCHECK(!paused_);
stefan099110c2017-02-01 03:57:42 -0800318 if (media_budget_->bytes_remaining() == 0 &&
philipelc7bf32a2017-02-17 03:59:43 -0800319 pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) {
stefan099110c2017-02-01 03:57:42 -0800320 return false;
terelius8b70faf2016-08-01 09:47:31 -0700321 }
philipelc7bf32a2017-02-17 03:59:43 -0800322
kthelgason6bfe49c2017-03-30 01:14:41 -0700323 critsect_.Leave();
perkjec81bcd2016-05-11 06:01:13 -0700324 const bool success = packet_sender_->TimeToSendPacket(
325 packet.ssrc, packet.sequence_number, packet.capture_time_ms,
philipelc7bf32a2017-02-17 03:59:43 -0800326 packet.retransmission, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700327 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000328
Peter Boström7ecc1632016-03-02 14:22:25 +0100329 if (success) {
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100330 if (first_sent_packet_ms_ == -1)
331 first_sent_packet_ms_ = clock_->TimeInMilliseconds();
Alex Narest78609d52017-10-20 10:37:47 +0200332 if (packet.priority != kHighPriority || account_for_audio_) {
Peter Boström7ecc1632016-03-02 14:22:25 +0100333 // Update media bytes sent.
eladalon32040ef2017-08-02 06:29:00 -0700334 // TODO(eladalon): TimeToSendPacket() can also return |true| in some
335 // situations where nothing actually ended up being sent to the network,
336 // and we probably don't want to update the budget in such cases.
337 // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
isheriff31687812016-10-04 08:43:09 -0700338 UpdateBudgetWithBytesSent(packet.bytes);
Peter Boström7ecc1632016-03-02 14:22:25 +0100339 }
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000340 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000341
342 return success;
343}
344
philipelc7bf32a2017-02-17 03:59:43 -0800345size_t PacedSender::SendPadding(size_t padding_needed,
346 const PacedPacketInfo& pacing_info) {
stefan9e117c5e12017-08-16 08:16:25 -0700347 RTC_DCHECK_GT(packet_counter_, 0);
kthelgason6bfe49c2017-03-30 01:14:41 -0700348 critsect_.Leave();
philipela1ed0b32016-06-01 06:31:17 -0700349 size_t bytes_sent =
philipelc7bf32a2017-02-17 03:59:43 -0800350 packet_sender_->TimeToSendPadding(padding_needed, pacing_info);
kthelgason6bfe49c2017-03-30 01:14:41 -0700351 critsect_.Enter();
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000352
Stefan Holmer01b48882015-05-05 10:21:24 +0200353 if (bytes_sent > 0) {
isheriff31687812016-10-04 08:43:09 -0700354 UpdateBudgetWithBytesSent(bytes_sent);
Stefan Holmer01b48882015-05-05 10:21:24 +0200355 }
isheriffcc5903e2016-10-04 08:29:38 -0700356 return bytes_sent;
stefan@webrtc.org19a40ff2013-11-27 14:16:20 +0000357}
358
isheriff31687812016-10-04 08:43:09 -0700359void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) {
Sebastian Jansson18cf4b62018-02-13 13:54:33 +0100360 delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms);
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +0000361 media_budget_->IncreaseBudget(delta_time_ms);
362 padding_budget_->IncreaseBudget(delta_time_ms);
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000363}
isheriff31687812016-10-04 08:43:09 -0700364
365void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
366 media_budget_->UseBudget(bytes_sent);
367 padding_budget_->UseBudget(bytes_sent);
368}
sprang89c4a7e2017-06-30 13:27:40 -0700369
sprang89c4a7e2017-06-30 13:27:40 -0700370void PacedSender::SetQueueTimeLimit(int limit_ms) {
371 rtc::CritScope cs(&critsect_);
372 queue_time_limit = limit_ms;
373}
374
pwestin@webrtc.orgb5180172012-11-09 20:56:23 +0000375} // namespace webrtc