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