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" |
| 21 | #include "modules/pacing/alr_detector.h" |
| 22 | #include "modules/pacing/bitrate_prober.h" |
| 23 | #include "modules/pacing/interval_budget.h" |
| 24 | #include "modules/utility/include/process_thread.h" |
| 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/logging.h" |
Niels Möller | 712048c | 2017-10-18 13:08:22 +0200 | [diff] [blame] | 27 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "system_wrappers/include/clock.h" |
| 29 | #include "system_wrappers/include/field_trial.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 32 | // Time limit in milliseconds between packet bursts. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 33 | const int64_t kMinPacketLimitMs = 5; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 34 | const int64_t kPausedPacketIntervalMs = 500; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 35 | |
| 36 | // Upper cap on process interval, in case process has not been called in a long |
| 37 | // time. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 38 | const int64_t kMaxIntervalTimeMs = 30; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 39 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 40 | } // namespace |
| 41 | |
| 42 | namespace webrtc { |
pwestin@webrtc.org | 52aa019 | 2013-04-25 17:35:56 +0000 | [diff] [blame] | 43 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 44 | const int64_t PacedSender::kMaxQueueLengthMs = 2000; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 45 | const float PacedSender::kDefaultPaceMultiplier = 2.5f; |
| 46 | |
philipel | c3b3f7a | 2017-03-29 01:23:13 -0700 | [diff] [blame] | 47 | PacedSender::PacedSender(const Clock* clock, |
| 48 | PacketSender* packet_sender, |
Niels Möller | 9d4af01 | 2017-10-31 09:54:50 +0100 | [diff] [blame] | 49 | RtcEventLog* event_log) : |
| 50 | PacedSender(clock, packet_sender, event_log, |
Lu Liu | c1094eb | 2018-01-17 19:45:43 +0000 | [diff] [blame] | 51 | webrtc::field_trial::IsEnabled("WebRTC-RoundRobinPacing") |
Niels Möller | 9d4af01 | 2017-10-31 09:54:50 +0100 | [diff] [blame] | 52 | ? rtc::MakeUnique<PacketQueue2>(clock) |
| 53 | : rtc::MakeUnique<PacketQueue>(clock)) {} |
| 54 | |
| 55 | PacedSender::PacedSender(const Clock* clock, |
| 56 | PacketSender* packet_sender, |
| 57 | RtcEventLog* event_log, |
| 58 | std::unique_ptr<PacketQueue> packets) |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 59 | : clock_(clock), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 60 | packet_sender_(packet_sender), |
Ilya Nikolaevskiy | a4259f6 | 2017-12-05 13:19:45 +0100 | [diff] [blame] | 61 | alr_detector_(rtc::MakeUnique<AlrDetector>(event_log)), |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 62 | paused_(false), |
Niels Möller | 712048c | 2017-10-18 13:08:22 +0200 | [diff] [blame] | 63 | media_budget_(rtc::MakeUnique<IntervalBudget>(0)), |
| 64 | padding_budget_(rtc::MakeUnique<IntervalBudget>(0)), |
| 65 | prober_(rtc::MakeUnique<BitrateProber>(event_log)), |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 66 | probing_send_failure_(false), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 67 | estimated_bitrate_bps_(0), |
| 68 | min_send_bitrate_kbps_(0u), |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 69 | max_padding_bitrate_kbps_(0u), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 70 | pacing_bitrate_kbps_(0), |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 71 | time_last_update_us_(clock->TimeInMicroseconds()), |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 72 | first_sent_packet_ms_(-1), |
Niels Möller | 9d4af01 | 2017-10-31 09:54:50 +0100 | [diff] [blame] | 73 | packets_(std::move(packets)), |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 74 | packet_counter_(0), |
| 75 | pacing_factor_(kDefaultPaceMultiplier), |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 76 | queue_time_limit(kMaxQueueLengthMs), |
| 77 | account_for_audio_(false) { |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 78 | UpdateBudgetWithElapsedTime(kMinPacketLimitMs); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 79 | } |
| 80 | |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 81 | PacedSender::~PacedSender() {} |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 82 | |
philipel | fd58b61 | 2017-01-04 07:05:25 -0800 | [diff] [blame] | 83 | void PacedSender::CreateProbeCluster(int bitrate_bps) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 84 | rtc::CritScope cs(&critsect_); |
Stefan Holmer | 0e3213a | 2017-02-08 15:19:05 +0100 | [diff] [blame] | 85 | prober_->CreateProbeCluster(bitrate_bps, clock_->TimeInMilliseconds()); |
philipel | eb680ea | 2016-08-17 11:11:59 +0200 | [diff] [blame] | 86 | } |
| 87 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 88 | void PacedSender::Pause() { |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 89 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 90 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 91 | if (!paused_) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 92 | RTC_LOG(LS_INFO) << "PacedSender paused."; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 93 | paused_ = true; |
sprang | ddcfb9f | 2017-08-16 05:38:49 -0700 | [diff] [blame] | 94 | packets_->SetPauseState(true, clock_->TimeInMilliseconds()); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 95 | } |
| 96 | // Tell the process thread to call our TimeUntilNextProcess() method to get |
| 97 | // a new (longer) estimate for when to call Process(). |
| 98 | if (process_thread_) |
| 99 | process_thread_->WakeUp(this); |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void PacedSender::Resume() { |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 103 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 104 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 105 | if (paused_) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 106 | RTC_LOG(LS_INFO) << "PacedSender resumed."; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 107 | paused_ = false; |
sprang | ddcfb9f | 2017-08-16 05:38:49 -0700 | [diff] [blame] | 108 | packets_->SetPauseState(false, clock_->TimeInMilliseconds()); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 109 | } |
| 110 | // Tell the process thread to call our TimeUntilNextProcess() method to |
| 111 | // refresh the estimate for when to call Process(). |
| 112 | if (process_thread_) |
| 113 | process_thread_->WakeUp(this); |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 114 | } |
| 115 | |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 116 | void PacedSender::SetProbingEnabled(bool enabled) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 117 | rtc::CritScope cs(&critsect_); |
Elad Alon | 44b1fa4 | 2017-10-17 14:17:54 +0200 | [diff] [blame] | 118 | RTC_CHECK_EQ(0, packet_counter_); |
Irfan Sheriff | 6e11efa | 2016-08-02 12:57:37 -0700 | [diff] [blame] | 119 | prober_->SetEnabled(enabled); |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 120 | } |
| 121 | |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 122 | void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) { |
terelius | 8b70faf | 2016-08-01 09:47:31 -0700 | [diff] [blame] | 123 | if (bitrate_bps == 0) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 124 | RTC_LOG(LS_ERROR) << "PacedSender is not designed to handle 0 bitrate."; |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 125 | rtc::CritScope cs(&critsect_); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 126 | estimated_bitrate_bps_ = bitrate_bps; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 127 | padding_budget_->set_target_rate_kbps( |
| 128 | std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_)); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 129 | pacing_bitrate_kbps_ = |
| 130 | std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) * |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 131 | pacing_factor_; |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 132 | alr_detector_->SetEstimatedBitrate(bitrate_bps); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 133 | } |
| 134 | |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 135 | void PacedSender::SetSendBitrateLimits(int min_send_bitrate_bps, |
| 136 | int padding_bitrate) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 137 | rtc::CritScope cs(&critsect_); |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 138 | min_send_bitrate_kbps_ = min_send_bitrate_bps / 1000; |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 139 | pacing_bitrate_kbps_ = |
| 140 | std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) * |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 141 | pacing_factor_; |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 142 | max_padding_bitrate_kbps_ = padding_bitrate / 1000; |
| 143 | padding_budget_->set_target_rate_kbps( |
| 144 | std::min(estimated_bitrate_bps_ / 1000, max_padding_bitrate_kbps_)); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 147 | void PacedSender::InsertPacket(RtpPacketSender::Priority priority, |
| 148 | uint32_t ssrc, |
| 149 | uint16_t sequence_number, |
| 150 | int64_t capture_time_ms, |
| 151 | size_t bytes, |
| 152 | bool retransmission) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 153 | rtc::CritScope cs(&critsect_); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 154 | RTC_DCHECK(estimated_bitrate_bps_ > 0) |
| 155 | << "SetEstimatedBitrate must be called before InsertPacket."; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 156 | |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 157 | int64_t now_ms = clock_->TimeInMilliseconds(); |
philipel | 4a1ec1e | 2016-08-15 11:51:06 -0700 | [diff] [blame] | 158 | prober_->OnIncomingPacket(bytes); |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 159 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 160 | if (capture_time_ms < 0) |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 161 | capture_time_ms = now_ms; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 162 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 163 | packets_->Push(PacketQueue::Packet(priority, ssrc, sequence_number, |
| 164 | capture_time_ms, now_ms, bytes, |
| 165 | retransmission, packet_counter_++)); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 168 | void PacedSender::SetAccountForAudioPackets(bool account_for_audio) { |
| 169 | rtc::CritScope cs(&critsect_); |
| 170 | account_for_audio_ = account_for_audio; |
| 171 | } |
| 172 | |
pkasting@chromium.org | 2656bf8 | 2014-11-17 22:21:14 +0000 | [diff] [blame] | 173 | int64_t PacedSender::ExpectedQueueTimeMs() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 174 | rtc::CritScope cs(&critsect_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 175 | RTC_DCHECK_GT(pacing_bitrate_kbps_, 0); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 176 | return static_cast<int64_t>(packets_->SizeInBytes() * 8 / |
| 177 | pacing_bitrate_kbps_); |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 178 | } |
| 179 | |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 180 | rtc::Optional<int64_t> PacedSender::GetApplicationLimitedRegionStartTime() |
| 181 | const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 182 | rtc::CritScope cs(&critsect_); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 183 | return alr_detector_->GetApplicationLimitedRegionStartTime(); |
Irfan Sheriff | 1eb1293 | 2016-10-18 17:04:25 -0700 | [diff] [blame] | 184 | } |
| 185 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 186 | size_t PacedSender::QueueSizePackets() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 187 | rtc::CritScope cs(&critsect_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 188 | return packets_->SizeInPackets(); |
| 189 | } |
| 190 | |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 191 | int64_t PacedSender::FirstSentPacketTimeMs() const { |
| 192 | rtc::CritScope cs(&critsect_); |
| 193 | return first_sent_packet_ms_; |
| 194 | } |
| 195 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 196 | int64_t PacedSender::QueueInMs() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 197 | rtc::CritScope cs(&critsect_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 198 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 199 | int64_t oldest_packet = packets_->OldestEnqueueTimeMs(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 200 | if (oldest_packet == 0) |
| 201 | return 0; |
| 202 | |
| 203 | return clock_->TimeInMilliseconds() - oldest_packet; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 204 | } |
| 205 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 206 | int64_t PacedSender::TimeUntilNextProcess() { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 207 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 208 | int64_t elapsed_time_us = clock_->TimeInMicroseconds() - time_last_update_us_; |
| 209 | int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000; |
| 210 | // When paused we wake up every 500 ms to send a padding packet to ensure |
| 211 | // 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] | 212 | if (paused_) |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 213 | return std::max<int64_t>(kPausedPacketIntervalMs - elapsed_time_ms, 0); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 214 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 215 | if (prober_->IsProbing()) { |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 216 | int64_t ret = prober_->TimeUntilNextProbe(clock_->TimeInMilliseconds()); |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 217 | if (ret > 0 || (ret == 0 && !probing_send_failure_)) |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 218 | return ret; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 219 | } |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 220 | return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 221 | } |
| 222 | |
pbos | a26ac92 | 2016-02-25 04:50:01 -0800 | [diff] [blame] | 223 | void PacedSender::Process() { |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 224 | int64_t now_us = clock_->TimeInMicroseconds(); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 225 | rtc::CritScope cs(&critsect_); |
tschumim | 5afa3f2 | 2017-08-18 03:38:49 -0700 | [diff] [blame] | 226 | int64_t elapsed_time_ms = std::min( |
| 227 | kMaxIntervalTimeMs, (now_us - time_last_update_us_ + 500) / 1000); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 228 | int target_bitrate_kbps = pacing_bitrate_kbps_; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 229 | |
| 230 | if (paused_) { |
| 231 | PacedPacketInfo pacing_info; |
| 232 | time_last_update_us_ = now_us; |
| 233 | // We can not send padding unless a normal packet has first been sent. If we |
| 234 | // do, timestamps get messed up. |
| 235 | if (packet_counter_ == 0) |
| 236 | return; |
| 237 | size_t bytes_sent = SendPadding(1, pacing_info); |
tschumim | 5afa3f2 | 2017-08-18 03:38:49 -0700 | [diff] [blame] | 238 | alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (elapsed_time_ms > 0) { |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 243 | size_t queue_size_bytes = packets_->SizeInBytes(); |
| 244 | if (queue_size_bytes > 0) { |
| 245 | // Assuming equal size packets and input/output rate, the average packet |
| 246 | // has avg_time_left_ms left to get queue_size_bytes out of the queue, if |
| 247 | // time constraint shall be met. Determine bitrate needed for that. |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 248 | packets_->UpdateQueueTime(clock_->TimeInMilliseconds()); |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 249 | int64_t avg_time_left_ms = std::max<int64_t>( |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 250 | 1, queue_time_limit - packets_->AverageQueueTimeMs()); |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 251 | int min_bitrate_needed_kbps = |
| 252 | static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms); |
| 253 | if (min_bitrate_needed_kbps > target_bitrate_kbps) |
| 254 | target_bitrate_kbps = min_bitrate_needed_kbps; |
| 255 | } |
| 256 | |
| 257 | media_budget_->set_target_rate_kbps(target_bitrate_kbps); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 258 | UpdateBudgetWithElapsedTime(elapsed_time_ms); |
stefan@webrtc.org | 80865fd | 2013-08-09 11:31:23 +0000 | [diff] [blame] | 259 | } |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 260 | |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 261 | time_last_update_us_ = now_us; |
| 262 | |
philipel | 1a93cde | 2016-06-03 01:41:45 -0700 | [diff] [blame] | 263 | bool is_probing = prober_->IsProbing(); |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 264 | PacedPacketInfo pacing_info; |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 265 | size_t bytes_sent = 0; |
| 266 | size_t recommended_probe_size = 0; |
| 267 | if (is_probing) { |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 268 | pacing_info = prober_->CurrentCluster(); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 269 | recommended_probe_size = prober_->RecommendedMinProbeSize(); |
| 270 | } |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 271 | while (!packets_->Empty()) { |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 272 | // 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. |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 275 | const PacketQueue::Packet& packet = packets_->BeginPop(); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 276 | |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 277 | if (SendPacket(packet, pacing_info)) { |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 278 | // Send succeeded, remove it from the queue. |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 279 | if (first_sent_packet_ms_ == -1) |
| 280 | first_sent_packet_ms_ = clock_->TimeInMilliseconds(); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 281 | bytes_sent += packet.bytes; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 282 | packets_->FinalizePop(packet); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 283 | if (is_probing && bytes_sent > recommended_probe_size) |
| 284 | break; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 285 | } else { |
| 286 | // Send failed, put it back into the queue. |
| 287 | packets_->CancelPop(packet); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 288 | break; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 289 | } |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 290 | } |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 291 | |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 292 | if (packets_->Empty()) { |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 293 | // We can not send padding unless a normal packet has first been sent. If we |
| 294 | // do, timestamps get messed up. |
| 295 | if (packet_counter_ > 0) { |
| 296 | int padding_needed = |
| 297 | static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent) |
| 298 | : padding_budget_->bytes_remaining()); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 299 | if (padding_needed > 0) |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 300 | bytes_sent += SendPadding(padding_needed, pacing_info); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 301 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 302 | } |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 303 | if (is_probing) { |
| 304 | probing_send_failure_ = bytes_sent == 0; |
| 305 | if (!probing_send_failure_) |
| 306 | prober_->ProbeSent(clock_->TimeInMilliseconds(), bytes_sent); |
| 307 | } |
tschumim | 82c5593 | 2017-07-11 06:56:04 -0700 | [diff] [blame] | 308 | alr_detector_->OnBytesSent(bytes_sent, elapsed_time_ms); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 309 | } |
| 310 | |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 311 | void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 312 | RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << std::hex << process_thread; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 313 | process_thread_ = process_thread; |
| 314 | } |
| 315 | |
philipel | 9981bd9 | 2017-09-26 17:16:06 +0200 | [diff] [blame] | 316 | bool PacedSender::SendPacket(const PacketQueue::Packet& packet, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 317 | const PacedPacketInfo& pacing_info) { |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 318 | RTC_DCHECK(!paused_); |
stefan | 099110c | 2017-02-01 03:57:42 -0800 | [diff] [blame] | 319 | if (media_budget_->bytes_remaining() == 0 && |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 320 | pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) { |
stefan | 099110c | 2017-02-01 03:57:42 -0800 | [diff] [blame] | 321 | return false; |
terelius | 8b70faf | 2016-08-01 09:47:31 -0700 | [diff] [blame] | 322 | } |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 323 | |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 324 | critsect_.Leave(); |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 325 | const bool success = packet_sender_->TimeToSendPacket( |
| 326 | packet.ssrc, packet.sequence_number, packet.capture_time_ms, |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 327 | packet.retransmission, pacing_info); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 328 | critsect_.Enter(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 329 | |
Peter Boström | 7ecc163 | 2016-03-02 14:22:25 +0100 | [diff] [blame] | 330 | if (success) { |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 331 | if (packet.priority != kHighPriority || account_for_audio_) { |
Peter Boström | 7ecc163 | 2016-03-02 14:22:25 +0100 | [diff] [blame] | 332 | // Update media bytes sent. |
eladalon | 32040ef | 2017-08-02 06:29:00 -0700 | [diff] [blame] | 333 | // TODO(eladalon): TimeToSendPacket() can also return |true| in some |
| 334 | // situations where nothing actually ended up being sent to the network, |
| 335 | // and we probably don't want to update the budget in such cases. |
| 336 | // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052 |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 337 | UpdateBudgetWithBytesSent(packet.bytes); |
Peter Boström | 7ecc163 | 2016-03-02 14:22:25 +0100 | [diff] [blame] | 338 | } |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 339 | } |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 340 | |
| 341 | return success; |
| 342 | } |
| 343 | |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 344 | size_t PacedSender::SendPadding(size_t padding_needed, |
| 345 | const PacedPacketInfo& pacing_info) { |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 346 | RTC_DCHECK_GT(packet_counter_, 0); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 347 | critsect_.Leave(); |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 348 | size_t bytes_sent = |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 349 | packet_sender_->TimeToSendPadding(padding_needed, pacing_info); |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 350 | critsect_.Enter(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 351 | |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 352 | if (bytes_sent > 0) { |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 353 | UpdateBudgetWithBytesSent(bytes_sent); |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 354 | } |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 355 | return bytes_sent; |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 356 | } |
| 357 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 358 | void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) { |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 359 | media_budget_->IncreaseBudget(delta_time_ms); |
| 360 | padding_budget_->IncreaseBudget(delta_time_ms); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 361 | } |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 362 | |
| 363 | void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) { |
| 364 | media_budget_->UseBudget(bytes_sent); |
| 365 | padding_budget_->UseBudget(bytes_sent); |
| 366 | } |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 367 | |
| 368 | void PacedSender::SetPacingFactor(float pacing_factor) { |
| 369 | rtc::CritScope cs(&critsect_); |
| 370 | pacing_factor_ = pacing_factor; |
Erik Språng | 5fc130b | 2017-10-04 09:42:53 +0200 | [diff] [blame] | 371 | // Make sure new padding factor is applied immediately, otherwise we need to |
| 372 | // wait for the send bitrate estimate to be updated before this takes effect. |
| 373 | SetEstimatedBitrate(estimated_bitrate_bps_); |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Erik Språng | 7c8cca3 | 2017-10-24 17:05:18 +0200 | [diff] [blame] | 376 | float PacedSender::GetPacingFactor() const { |
| 377 | rtc::CritScope cs(&critsect_); |
| 378 | return pacing_factor_; |
| 379 | } |
| 380 | |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 381 | void PacedSender::SetQueueTimeLimit(int limit_ms) { |
| 382 | rtc::CritScope cs(&critsect_); |
| 383 | queue_time_limit = limit_ms; |
| 384 | } |
| 385 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 386 | } // namespace webrtc |