pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/pacing/paced_sender.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 12 | |
jbauch | d2a2296 | 2016-02-08 23:18:25 -0800 | [diff] [blame] | 13 | #include <algorithm> |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 14 | #include <map> |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 15 | #include <queue> |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 16 | #include <set> |
jbauch | d2a2296 | 2016-02-08 23:18:25 -0800 | [diff] [blame] | 17 | #include <vector> |
Niels Möller | 9d4af01 | 2017-10-31 09:54:50 +0100 | [diff] [blame] | 18 | #include <utility> |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/include/module_common_types.h" |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 21 | #include "modules/pacing/alr_detector.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/pacing/bitrate_prober.h" |
| 23 | #include "modules/pacing/interval_budget.h" |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame] | 24 | #include "modules/pacing/packet_queue.h" |
| 25 | #include "modules/pacing/round_robin_packet_queue.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/utility/include/process_thread.h" |
| 27 | #include "rtc_base/checks.h" |
| 28 | #include "rtc_base/logging.h" |
Niels Möller | 712048c | 2017-10-18 13:08:22 +0200 | [diff] [blame] | 29 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "system_wrappers/include/clock.h" |
| 31 | #include "system_wrappers/include/field_trial.h" |
Ilya Nikolaevskiy | 2ffe3e8 | 2018-01-17 19:57:24 +0000 | [diff] [blame] | 32 | #include "system_wrappers/include/runtime_enabled_features.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 35 | // Time limit in milliseconds between packet bursts. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 36 | const int64_t kMinPacketLimitMs = 5; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 37 | const int64_t kPausedPacketIntervalMs = 500; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 38 | |
| 39 | // Upper cap on process interval, in case process has not been called in a long |
| 40 | // time. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 41 | const int64_t kMaxIntervalTimeMs = 30; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 42 | |
Ilya Nikolaevskiy | 2ffe3e8 | 2018-01-17 19:57:24 +0000 | [diff] [blame] | 43 | const char kRoundRobinExperimentName[] = "WebRTC-RoundRobinPacing"; |
| 44 | |
| 45 | bool 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.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 52 | } // namespace |
| 53 | |
| 54 | namespace webrtc { |
pwestin@webrtc.org | 52aa019 | 2013-04-25 17:35:56 +0000 | [diff] [blame] | 55 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 56 | const int64_t PacedSender::kMaxQueueLengthMs = 2000; |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 57 | const float PacedSender::kDefaultPaceMultiplier = 2.5f; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 58 | |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame] | 59 | namespace { |
| 60 | std::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 | |
philipel | c3b3f7a | 2017-03-29 01:23:13 -0700 | [diff] [blame] | 70 | PacedSender::PacedSender(const Clock* clock, |
| 71 | PacketSender* packet_sender, |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame] | 72 | RtcEventLog* event_log) |
| 73 | : PacedSender(clock, |
| 74 | packet_sender, |
| 75 | event_log, |
| 76 | CreatePacketQueue(clock, IsRoundRobinPacingEnabled())) {} |
Niels Möller | 9d4af01 | 2017-10-31 09:54:50 +0100 | [diff] [blame] | 77 | |
| 78 | PacedSender::PacedSender(const Clock* clock, |
| 79 | PacketSender* packet_sender, |
| 80 | RtcEventLog* event_log, |
Sebastian Jansson | b537496 | 2018-02-07 13:26:38 +0100 | [diff] [blame] | 81 | std::unique_ptr<PacketQueueInterface> packets) |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 82 | : clock_(clock), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 83 | packet_sender_(packet_sender), |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 84 | alr_detector_(rtc::MakeUnique<AlrDetector>(event_log)), |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 85 | paused_(false), |
Niels Möller | 712048c | 2017-10-18 13:08:22 +0200 | [diff] [blame] | 86 | media_budget_(rtc::MakeUnique<IntervalBudget>(0)), |
| 87 | padding_budget_(rtc::MakeUnique<IntervalBudget>(0)), |
| 88 | prober_(rtc::MakeUnique<BitrateProber>(event_log)), |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 89 | probing_send_failure_(false), |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 90 | estimated_bitrate_bps_(0), |
| 91 | min_send_bitrate_kbps_(0u), |
| 92 | max_padding_bitrate_kbps_(0u), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 93 | pacing_bitrate_kbps_(0), |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 94 | time_last_process_us_(clock->TimeInMicroseconds()), |
| 95 | last_send_time_us_(clock->TimeInMicroseconds()), |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 96 | first_sent_packet_ms_(-1), |
Niels Möller | 9d4af01 | 2017-10-31 09:54:50 +0100 | [diff] [blame] | 97 | packets_(std::move(packets)), |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 98 | packet_counter_(0), |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 99 | pacing_factor_(kDefaultPaceMultiplier), |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 100 | queue_time_limit(kMaxQueueLengthMs), |
| 101 | account_for_audio_(false) { |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 102 | UpdateBudgetWithElapsedTime(kMinPacketLimitMs); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 103 | } |
| 104 | |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 105 | PacedSender::~PacedSender() {} |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 106 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 107 | void PacedSender::CreateProbeCluster(int bitrate_bps) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 108 | rtc::CritScope cs(&critsect_); |
Stefan Holmer | 0e3213a | 2017-02-08 15:19:05 +0100 | [diff] [blame] | 109 | prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds()); |
philipel | eb680ea | 2016-08-17 11:11:59 +0200 | [diff] [blame] | 110 | } |
| 111 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 112 | void PacedSender::Pause() { |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 113 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 114 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 115 | if (!paused_) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 116 | RTC_LOG(LS_INFO) << "PacedSender paused."; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 117 | paused_ = true; |
sprang | ddcfb9f | 2017-08-16 05:38:49 -0700 | [diff] [blame] | 118 | packets_->SetPauseState(true, clock_->TimeInMilliseconds()); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 119 | } |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 120 | rtc::CritScope cs(&process_thread_lock_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 121 | // 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.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void PacedSender::Resume() { |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 128 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 129 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 130 | if (paused_) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 131 | RTC_LOG(LS_INFO) << "PacedSender resumed."; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 132 | paused_ = false; |
sprang | ddcfb9f | 2017-08-16 05:38:49 -0700 | [diff] [blame] | 133 | packets_->SetPauseState(false, clock_->TimeInMilliseconds()); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 134 | } |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 135 | rtc::CritScope cs(&process_thread_lock_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 136 | // 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.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 140 | } |
| 141 | |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 142 | void PacedSender::SetProbingEnabled(bool enabled) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 143 | rtc::CritScope cs(&critsect_); |
Elad Alon | 44b1fa4 | 2017-10-17 14:17:54 +0200 | [diff] [blame] | 144 | RTC_CHECK_EQ(0, packet_counter_); |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 145 | prober_->SetEnabled(enabled); |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 148 | void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) { |
| 149 | if (bitrate_bps == 0) |
| 150 | RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate."; |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 151 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 152 | 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 | |
| 161 | void 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.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 173 | void 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öm | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 181 | void 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) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 187 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 188 | RTC_DCHECK(pacing_bitrate_kbps_ > 0) |
| 189 | << "SetPacingRate must be called before InsertPacket."; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 190 | |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 191 | int64_t now_ms = clock_->TimeInMilliseconds(); |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 192 | prober_->OnIncomingPacket(bytes); |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 193 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 194 | if (capture_time_ms < 0) |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 195 | capture_time_ms = now_ms; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 196 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 197 | packets_->Push(PacketQueue::Packet(priority, ssrc, sequence_number, |
| 198 | capture_time_ms, now_ms, bytes, |
| 199 | retransmission, packet_counter_++)); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 202 | void PacedSender::SetAccountForAudioPackets(bool account_for_audio) { |
| 203 | rtc::CritScope cs(&critsect_); |
| 204 | account_for_audio_ = account_for_audio; |
| 205 | } |
| 206 | |
pkasting@chromium.org | 2656bf8 | 2014-11-17 22:21:14 +0000 | [diff] [blame] | 207 | int64_t PacedSender::ExpectedQueueTimeMs() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 208 | rtc::CritScope cs(&critsect_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 209 | RTC_DCHECK_GT(pacing_bitrate_kbps_, 0); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 210 | return static_cast<int64_t>(packets_->SizeInBytes() * 8 / |
| 211 | pacing_bitrate_kbps_); |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 214 | rtc::Optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime() |
| 215 | const { |
| 216 | rtc::CritScope cs(&critsect_); |
| 217 | return alr_detector_->GetApplicationLimitedRegionStartTime(); |
| 218 | } |
| 219 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 220 | size_t PacedSender::QueueSizePackets() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 221 | rtc::CritScope cs(&critsect_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 222 | return packets_->SizeInPackets(); |
| 223 | } |
| 224 | |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 225 | int64_t PacedSender::FirstSentPacketTimeMs() const { |
| 226 | rtc::CritScope cs(&critsect_); |
| 227 | return first_sent_packet_ms_; |
| 228 | } |
| 229 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 230 | int64_t PacedSender::QueueInMs() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 231 | rtc::CritScope cs(&critsect_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 232 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 233 | int64_t oldest_packet = packets_->OldestEnqueueTimeMs(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 234 | if (oldest_packet == 0) |
| 235 | return 0; |
| 236 | |
| 237 | return clock_->TimeInMilliseconds() - oldest_packet; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 238 | } |
| 239 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 240 | int64_t PacedSender::TimeUntilNextProcess() { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 241 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 242 | int64_t elapsed_time_us = |
| 243 | clock_->TimeInMicroseconds() - time_last_process_us_; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 244 | 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. |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 247 | if (paused_) |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 248 | return std::max<int64_t>(kPausedPacketIntervalMs - elapsed_time_ms, 0); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 249 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 250 | if (prober_->IsProbing()) { |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 251 | int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds()); |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 252 | if (ret > 0 || (ret == 0 && !probing_send_failure_)) |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 253 | return ret; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 254 | } |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 255 | return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 256 | } |
| 257 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 258 | void PacedSender::Process() { |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 259 | int64_t now_us = clock_->TimeInMicroseconds(); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 260 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 261 | time_last_process_us_ = now_us; |
| 262 | int64_t elapsed_time_ms = (now_us - last_send_time_us_ + 500) / 1000; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 263 | |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 264 | // 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. |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 266 | if (paused_) { |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 267 | // We can not send padding unless a normal packet has first been sent. If we |
| 268 | // do, timestamps get messed up. |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 269 | 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 | } |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 278 | int target_bitrate_kbps = pacing_bitrate_kbps_; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 279 | if (elapsed_time_ms > 0) { |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 280 | 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ång | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 285 | packets_->UpdateQueueTime(clock_->TimeInMilliseconds()); |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 286 | int64_t avg_time_left_ms = std::max<int64_t>( |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 287 | 1, queue_time_limit - packets_->AverageQueueTimeMs()); |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 288 | 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); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 295 | UpdateBudgetWithElapsedTime(elapsed_time_ms); |
stefan@webrtc.org | 80865fd | 2013-08-09 11:31:23 +0000 | [diff] [blame] | 296 | } |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 297 | |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 298 | last_send_time_us_ = clock_->TimeInMicroseconds(); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 299 | |
philipel | 1a93cde | 2016-06-03 01:41:45 -0700 | [diff] [blame] | 300 | bool is_probing = prober_->IsProbing(); |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 301 | PacedPacketInfo pacing_info; |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 302 | size_t bytes_sent = 0; |
| 303 | size_t recommended_probe_size = 0; |
| 304 | if (is_probing) { |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 305 | pacing_info = prober_->CurrentCluster(); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 306 | recommended_probe_size = prober_->RecommendedMinProbeSize(); |
| 307 | } |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 308 | // 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 Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 310 | while (!packets_->Empty() && !paused_) { |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 311 | // 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. |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 314 | const PacketQueue::Packet& packet = packets_->BeginPop(); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 315 | |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 316 | if (SendPacket(packet, pacing_info)) { |
Danil Chapovalov | 516036f | 2018-02-14 10:21:27 +0000 | [diff] [blame] | 317 | bytes_sent += packet.bytes; |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 318 | // Send succeeded, remove it from the queue. |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 319 | packets_->FinalizePop(packet); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 320 | if (is_probing && bytes_sent > recommended_probe_size) |
| 321 | break; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 322 | } else { |
| 323 | // Send failed, put it back into the queue. |
| 324 | packets_->CancelPop(packet); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 325 | break; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 326 | } |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 327 | } |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 328 | |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 329 | if (packets_->Empty()) { |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 330 | // 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 Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 336 | if (padding_needed > 0) { |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 337 | bytes_sent += SendPadding(padding_needed, pacing_info); |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 338 | } |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 339 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 340 | } |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 341 | if (is_probing) { |
| 342 | probing_send_failure_ = bytes_sent == 0; |
| 343 | if (!probing_send_failure_) |
| 344 | prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent); |
| 345 | } |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 346 | alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 347 | } |
| 348 | |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 349 | void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 350 | RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << std::hex << process_thread; |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 351 | rtc::CritScope cs(&process_thread_lock_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 352 | process_thread_ = process_thread; |
| 353 | } |
| 354 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 355 | bool PacedSender::SendPacket(const PacketQueue::Packet& packet, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 356 | const PacedPacketInfo& pacing_info) { |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 357 | RTC_DCHECK(!paused_); |
stefan | 099110c | 2017-02-01 03:57:42 -0800 | [diff] [blame] | 358 | if (media_budget_->bytes_remaining() == 0 && |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 359 | pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) { |
stefan | 099110c | 2017-02-01 03:57:42 -0800 | [diff] [blame] | 360 | return false; |
terelius | 8b70faf | 2016-08-01 09:47:31 -0700 | [diff] [blame] | 361 | } |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 362 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 363 | critsect_.Leave(); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 364 | const bool success = packet_sender_->TimeToSendPacket( |
| 365 | packet.ssrc, packet.sequence_number, packet.capture_time_ms, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 366 | packet.retransmission, pacing_info); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 367 | critsect_.Enter(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 368 | |
Peter Boström | 7ecc163 | 2016-03-02 14:22:25 +0100 | [diff] [blame] | 369 | if (success) { |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 370 | if (first_sent_packet_ms_ == -1) |
| 371 | first_sent_packet_ms_ = clock_->TimeInMilliseconds(); |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 372 | if (packet.priority != kHighPriority || account_for_audio_) { |
Peter Boström | 7ecc163 | 2016-03-02 14:22:25 +0100 | [diff] [blame] | 373 | // Update media bytes sent. |
eladalon | 32040ef | 2017-08-02 06:29:00 -0700 | [diff] [blame] | 374 | // 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 |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 378 | UpdateBudgetWithBytesSent(packet.bytes); |
Peter Boström | 7ecc163 | 2016-03-02 14:22:25 +0100 | [diff] [blame] | 379 | } |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 380 | } |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 381 | |
| 382 | return success; |
| 383 | } |
| 384 | |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 385 | size_t PacedSender::SendPadding(size_t padding_needed, |
| 386 | const PacedPacketInfo& pacing_info) { |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 387 | RTC_DCHECK_GT(packet_counter_, 0); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 388 | critsect_.Leave(); |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 389 | size_t bytes_sent = |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 390 | packet_sender_->TimeToSendPadding(padding_needed, pacing_info); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 391 | critsect_.Enter(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 392 | |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 393 | if (bytes_sent > 0) { |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 394 | UpdateBudgetWithBytesSent(bytes_sent); |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 395 | } |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 396 | return bytes_sent; |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 397 | } |
| 398 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 399 | void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) { |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame^] | 400 | delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms); |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 401 | media_budget_->IncreaseBudget(delta_time_ms); |
| 402 | padding_budget_->IncreaseBudget(delta_time_ms); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 403 | } |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 404 | |
| 405 | void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) { |
| 406 | media_budget_->UseBudget(bytes_sent); |
| 407 | padding_budget_->UseBudget(bytes_sent); |
| 408 | } |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 409 | |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 410 | void 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 | |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 418 | void PacedSender::SetQueueTimeLimit(int limit_ms) { |
| 419 | rtc::CritScope cs(&critsect_); |
| 420 | queue_time_limit = limit_ms; |
| 421 | } |
| 422 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 423 | } // namespace webrtc |