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> |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 14 | #include <utility> |
pbos@webrtc.org | 03c817e | 2014-07-07 10:20:35 +0000 | [diff] [blame] | 15 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | #include "logging/rtc_event_log/rtc_event_log.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/pacing/bitrate_prober.h" |
| 19 | #include "modules/pacing/interval_budget.h" |
| 20 | #include "modules/utility/include/process_thread.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/logging.h" |
| 23 | #include "system_wrappers/include/clock.h" |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 24 | |
Per Kjellander | 7ef34f8 | 2019-02-22 13:09:32 +0100 | [diff] [blame] | 25 | namespace webrtc { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 26 | namespace { |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 27 | // Time limit in milliseconds between packet bursts. |
Christoffer Rodbro | e2e0000 | 2018-12-20 15:46:03 +0100 | [diff] [blame] | 28 | const int64_t kDefaultMinPacketLimitMs = 5; |
Sebastian Jansson | 45d9c1d | 2018-03-09 12:48:01 +0100 | [diff] [blame] | 29 | const int64_t kCongestedPacketIntervalMs = 500; |
| 30 | const int64_t kPausedProcessIntervalMs = kCongestedPacketIntervalMs; |
Sebastian Jansson | e5d8c57 | 2018-02-28 08:53:06 +0100 | [diff] [blame] | 31 | const int64_t kMaxElapsedTimeMs = 2000; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 32 | |
| 33 | // Upper cap on process interval, in case process has not been called in a long |
| 34 | // time. |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 35 | const int64_t kMaxIntervalTimeMs = 30; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 36 | |
Per Kjellander | 7ef34f8 | 2019-02-22 13:09:32 +0100 | [diff] [blame] | 37 | bool IsDisabled(const WebRtcKeyValueConfig& field_trials, |
| 38 | absl::string_view key) { |
| 39 | return field_trials.Lookup(key).find("Disabled") == 0; |
| 40 | } |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 41 | |
Per Kjellander | 7ef34f8 | 2019-02-22 13:09:32 +0100 | [diff] [blame] | 42 | bool IsEnabled(const WebRtcKeyValueConfig& field_trials, |
| 43 | absl::string_view key) { |
| 44 | return field_trials.Lookup(key).find("Enabled") == 0; |
| 45 | } |
| 46 | |
| 47 | } // namespace |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 48 | const int64_t PacedSender::kMaxQueueLengthMs = 2000; |
Sebastian Jansson | ea86bb7 | 2018-02-14 16:53:38 +0000 | [diff] [blame] | 49 | const float PacedSender::kDefaultPaceMultiplier = 2.5f; |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 50 | |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 51 | PacedSender::PacedSender(Clock* clock, |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 52 | PacketRouter* packet_router, |
Per Kjellander | 7ef34f8 | 2019-02-22 13:09:32 +0100 | [diff] [blame] | 53 | RtcEventLog* event_log, |
| 54 | const WebRtcKeyValueConfig* field_trials) |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 55 | : clock_(clock), |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 56 | packet_router_(packet_router), |
Per Kjellander | 5b69873 | 2019-04-15 12:36:33 +0200 | [diff] [blame] | 57 | fallback_field_trials_( |
| 58 | !field_trials ? absl::make_unique<FieldTrialBasedConfig>() : nullptr), |
| 59 | field_trials_(field_trials ? field_trials : fallback_field_trials_.get()), |
Per Kjellander | 5b69873 | 2019-04-15 12:36:33 +0200 | [diff] [blame] | 60 | drain_large_queues_( |
| 61 | !IsDisabled(*field_trials_, "WebRTC-Pacer-DrainQueue")), |
Sebastian Jansson | c235a8d | 2018-06-15 14:46:11 +0200 | [diff] [blame] | 62 | send_padding_if_silent_( |
Per Kjellander | 5b69873 | 2019-04-15 12:36:33 +0200 | [diff] [blame] | 63 | IsEnabled(*field_trials_, "WebRTC-Pacer-PadInSilence")), |
| 64 | pace_audio_(!IsDisabled(*field_trials_, "WebRTC-Pacer-BlockAudio")), |
Christoffer Rodbro | e2e0000 | 2018-12-20 15:46:03 +0100 | [diff] [blame] | 65 | min_packet_limit_ms_("", kDefaultMinPacketLimitMs), |
Erik Språng | 9681675 | 2018-09-04 18:40:19 +0200 | [diff] [blame] | 66 | last_timestamp_ms_(clock_->TimeInMilliseconds()), |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 67 | paused_(false), |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 68 | media_budget_(0), |
| 69 | padding_budget_(0), |
Per Kjellander | 5b69873 | 2019-04-15 12:36:33 +0200 | [diff] [blame] | 70 | prober_(*field_trials_), |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 71 | probing_send_failure_(false), |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 72 | pacing_bitrate_kbps_(0), |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame] | 73 | time_last_process_us_(clock->TimeInMicroseconds()), |
| 74 | last_send_time_us_(clock->TimeInMicroseconds()), |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 75 | first_sent_packet_ms_(-1), |
Sebastian Jansson | 2560e2e | 2018-10-11 20:17:22 +0200 | [diff] [blame] | 76 | packets_(clock->TimeInMicroseconds()), |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 77 | packet_counter_(0), |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 78 | queue_time_limit(kMaxQueueLengthMs), |
| 79 | account_for_audio_(false) { |
Christoffer Rodbro | e2e0000 | 2018-12-20 15:46:03 +0100 | [diff] [blame] | 80 | if (!drain_large_queues_) { |
Sebastian Jansson | 0601d68 | 2018-06-25 19:23:05 +0200 | [diff] [blame] | 81 | RTC_LOG(LS_WARNING) << "Pacer queues will not be drained," |
| 82 | "pushback experiment must be enabled."; |
Christoffer Rodbro | e2e0000 | 2018-12-20 15:46:03 +0100 | [diff] [blame] | 83 | } |
| 84 | ParseFieldTrial({&min_packet_limit_ms_}, |
Per Kjellander | 5b69873 | 2019-04-15 12:36:33 +0200 | [diff] [blame] | 85 | field_trials_->Lookup("WebRTC-Pacer-MinPacketLimitMs")); |
Christoffer Rodbro | e2e0000 | 2018-12-20 15:46:03 +0100 | [diff] [blame] | 86 | UpdateBudgetWithElapsedTime(min_packet_limit_ms_); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 87 | } |
| 88 | |
stefan@webrtc.org | 89fd1e8 | 2014-07-15 16:40:38 +0000 | [diff] [blame] | 89 | PacedSender::~PacedSender() {} |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 90 | |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 91 | void PacedSender::CreateProbeCluster(int bitrate_bps, int cluster_id) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 92 | rtc::CritScope cs(&critsect_); |
Piotr (Peter) Slatala | c39f462 | 2019-02-15 07:38:04 -0800 | [diff] [blame] | 93 | prober_.CreateProbeCluster(bitrate_bps, TimeMilliseconds(), cluster_id); |
philipel | eb680ea | 2016-08-17 11:11:59 +0200 | [diff] [blame] | 94 | } |
| 95 | |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 96 | void PacedSender::Pause() { |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 97 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 98 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 99 | if (!paused_) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 100 | RTC_LOG(LS_INFO) << "PacedSender paused."; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 101 | paused_ = true; |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 102 | packets_.SetPauseState(true, TimeMilliseconds()); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 103 | } |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 104 | rtc::CritScope cs(&process_thread_lock_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 105 | // Tell the process thread to call our TimeUntilNextProcess() method to get |
| 106 | // a new (longer) estimate for when to call Process(). |
| 107 | if (process_thread_) |
| 108 | process_thread_->WakeUp(this); |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void PacedSender::Resume() { |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 112 | { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 113 | rtc::CritScope cs(&critsect_); |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 114 | if (paused_) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 115 | RTC_LOG(LS_INFO) << "PacedSender resumed."; |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 116 | paused_ = false; |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 117 | packets_.SetPauseState(false, TimeMilliseconds()); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 118 | } |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 119 | rtc::CritScope cs(&process_thread_lock_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 120 | // Tell the process thread to call our TimeUntilNextProcess() method to |
| 121 | // refresh the estimate for when to call Process(). |
| 122 | if (process_thread_) |
| 123 | process_thread_->WakeUp(this); |
pwestin@webrtc.org | db41856 | 2013-03-22 23:39:29 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Sebastian Jansson | 45d9c1d | 2018-03-09 12:48:01 +0100 | [diff] [blame] | 126 | void PacedSender::SetCongestionWindow(int64_t congestion_window_bytes) { |
| 127 | rtc::CritScope cs(&critsect_); |
| 128 | congestion_window_bytes_ = congestion_window_bytes; |
| 129 | } |
| 130 | |
| 131 | void PacedSender::UpdateOutstandingData(int64_t outstanding_bytes) { |
| 132 | rtc::CritScope cs(&critsect_); |
| 133 | outstanding_bytes_ = outstanding_bytes; |
| 134 | } |
| 135 | |
| 136 | bool PacedSender::Congested() const { |
| 137 | if (congestion_window_bytes_ == kNoCongestionWindow) |
| 138 | return false; |
| 139 | return outstanding_bytes_ >= congestion_window_bytes_; |
| 140 | } |
| 141 | |
Erik Språng | 9681675 | 2018-09-04 18:40:19 +0200 | [diff] [blame] | 142 | int64_t PacedSender::TimeMilliseconds() const { |
| 143 | int64_t time_ms = clock_->TimeInMilliseconds(); |
| 144 | if (time_ms < last_timestamp_ms_) { |
| 145 | RTC_LOG(LS_WARNING) |
| 146 | << "Non-monotonic clock behavior observed. Previous timestamp: " |
| 147 | << last_timestamp_ms_ << ", new timestamp: " << time_ms; |
| 148 | RTC_DCHECK_GE(time_ms, last_timestamp_ms_); |
| 149 | time_ms = last_timestamp_ms_; |
| 150 | } |
| 151 | last_timestamp_ms_ = time_ms; |
| 152 | return time_ms; |
| 153 | } |
| 154 | |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 155 | void PacedSender::SetProbingEnabled(bool enabled) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 156 | rtc::CritScope cs(&critsect_); |
Elad Alon | 44b1fa4 | 2017-10-17 14:17:54 +0200 | [diff] [blame] | 157 | RTC_CHECK_EQ(0, packet_counter_); |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 158 | prober_.SetEnabled(enabled); |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 161 | void PacedSender::SetPacingRates(uint32_t pacing_rate_bps, |
| 162 | uint32_t padding_rate_bps) { |
| 163 | rtc::CritScope cs(&critsect_); |
| 164 | RTC_DCHECK(pacing_rate_bps > 0); |
| 165 | pacing_bitrate_kbps_ = pacing_rate_bps / 1000; |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 166 | padding_budget_.set_target_rate_kbps(padding_rate_bps / 1000); |
Evan Shrubsole | fee13e8 | 2019-02-26 15:25:52 +0100 | [diff] [blame] | 167 | |
| 168 | RTC_LOG(LS_VERBOSE) << "bwe:pacer_updated pacing_kbps=" |
| 169 | << pacing_bitrate_kbps_ |
| 170 | << " padding_budget_kbps=" << padding_rate_bps / 1000; |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 173 | void PacedSender::InsertPacket(RtpPacketSender::Priority priority, |
| 174 | uint32_t ssrc, |
| 175 | uint16_t sequence_number, |
| 176 | int64_t capture_time_ms, |
| 177 | size_t bytes, |
| 178 | bool retransmission) { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 179 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 180 | RTC_DCHECK(pacing_bitrate_kbps_ > 0) |
| 181 | << "SetPacingRate must be called before InsertPacket."; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 182 | |
Erik Språng | 9681675 | 2018-09-04 18:40:19 +0200 | [diff] [blame] | 183 | int64_t now_ms = TimeMilliseconds(); |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 184 | prober_.OnIncomingPacket(bytes); |
Peter Boström | 0453ef8 | 2016-02-16 16:23:08 +0100 | [diff] [blame] | 185 | |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 186 | if (capture_time_ms < 0) |
Erik Språng | ad113e5 | 2015-11-26 16:26:12 +0100 | [diff] [blame] | 187 | capture_time_ms = now_ms; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 188 | |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 189 | packets_.Push(RoundRobinPacketQueue::Packet( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 190 | priority, ssrc, sequence_number, capture_time_ms, now_ms, bytes, |
| 191 | retransmission, packet_counter_++)); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 194 | void PacedSender::SetAccountForAudioPackets(bool account_for_audio) { |
| 195 | rtc::CritScope cs(&critsect_); |
| 196 | account_for_audio_ = account_for_audio; |
| 197 | } |
| 198 | |
pkasting@chromium.org | 2656bf8 | 2014-11-17 22:21:14 +0000 | [diff] [blame] | 199 | int64_t PacedSender::ExpectedQueueTimeMs() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 200 | rtc::CritScope cs(&critsect_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 201 | RTC_DCHECK_GT(pacing_bitrate_kbps_, 0); |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 202 | return static_cast<int64_t>(packets_.SizeInBytes() * 8 / |
perkj | ec81bcd | 2016-05-11 06:01:13 -0700 | [diff] [blame] | 203 | pacing_bitrate_kbps_); |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 204 | } |
| 205 | |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 206 | size_t PacedSender::QueueSizePackets() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 207 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 208 | return packets_.SizeInPackets(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Christoffer Rodbro | c610e26 | 2019-01-08 10:49:19 +0100 | [diff] [blame] | 211 | int64_t PacedSender::QueueSizeBytes() const { |
| 212 | rtc::CritScope cs(&critsect_); |
| 213 | return packets_.SizeInBytes(); |
| 214 | } |
| 215 | |
asapersson | fc5e81c | 2017-04-19 23:28:53 -0700 | [diff] [blame] | 216 | int64_t PacedSender::FirstSentPacketTimeMs() const { |
| 217 | rtc::CritScope cs(&critsect_); |
| 218 | return first_sent_packet_ms_; |
| 219 | } |
| 220 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 221 | int64_t PacedSender::QueueInMs() const { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 222 | rtc::CritScope cs(&critsect_); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 223 | |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 224 | int64_t oldest_packet = packets_.OldestEnqueueTimeMs(); |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 225 | if (oldest_packet == 0) |
| 226 | return 0; |
| 227 | |
Erik Språng | 9681675 | 2018-09-04 18:40:19 +0200 | [diff] [blame] | 228 | return TimeMilliseconds() - oldest_packet; |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 229 | } |
| 230 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 +0000 | [diff] [blame] | 231 | int64_t PacedSender::TimeUntilNextProcess() { |
kthelgason | 6bfe49c | 2017-03-30 01:14:41 -0700 | [diff] [blame] | 232 | rtc::CritScope cs(&critsect_); |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame] | 233 | int64_t elapsed_time_us = |
| 234 | clock_->TimeInMicroseconds() - time_last_process_us_; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 235 | int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000; |
| 236 | // When paused we wake up every 500 ms to send a padding packet to ensure |
| 237 | // 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] | 238 | if (paused_) |
Sebastian Jansson | 45d9c1d | 2018-03-09 12:48:01 +0100 | [diff] [blame] | 239 | return std::max<int64_t>(kPausedProcessIntervalMs - elapsed_time_ms, 0); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 240 | |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 241 | if (prober_.IsProbing()) { |
| 242 | int64_t ret = prober_.TimeUntilNextProbe(TimeMilliseconds()); |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 243 | if (ret > 0 || (ret == 0 && !probing_send_failure_)) |
stefan@webrtc.org | e9f0f59 | 2015-02-16 15:47:51 +0000 | [diff] [blame] | 244 | return ret; |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 245 | } |
Christoffer Rodbro | e2e0000 | 2018-12-20 15:46:03 +0100 | [diff] [blame] | 246 | return std::max<int64_t>(min_packet_limit_ms_ - elapsed_time_ms, 0); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 249 | int64_t PacedSender::UpdateTimeAndGetElapsedMs(int64_t now_us) { |
Sebastian Jansson | b544f6c | 2018-06-04 19:02:41 +0200 | [diff] [blame] | 250 | int64_t elapsed_time_ms = (now_us - time_last_process_us_ + 500) / 1000; |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame] | 251 | time_last_process_us_ = now_us; |
Sebastian Jansson | e5d8c57 | 2018-02-28 08:53:06 +0100 | [diff] [blame] | 252 | if (elapsed_time_ms > kMaxElapsedTimeMs) { |
| 253 | RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time_ms |
| 254 | << " ms) longer than expected, limiting to " |
| 255 | << kMaxElapsedTimeMs << " ms"; |
| 256 | elapsed_time_ms = kMaxElapsedTimeMs; |
| 257 | } |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 258 | return elapsed_time_ms; |
| 259 | } |
| 260 | |
| 261 | bool PacedSender::ShouldSendKeepalive(int64_t now_us) const { |
Sebastian Jansson | c235a8d | 2018-06-15 14:46:11 +0200 | [diff] [blame] | 262 | if (send_padding_if_silent_ || paused_ || Congested()) { |
| 263 | // We send a padding packet every 500 ms to ensure we won't get stuck in |
| 264 | // congested state due to no feedback being received. |
Sebastian Jansson | b544f6c | 2018-06-04 19:02:41 +0200 | [diff] [blame] | 265 | int64_t elapsed_since_last_send_us = now_us - last_send_time_us_; |
| 266 | if (elapsed_since_last_send_us >= kCongestedPacketIntervalMs * 1000) { |
Sebastian Jansson | 682c649 | 2018-04-12 13:08:22 +0200 | [diff] [blame] | 267 | // We can not send padding unless a normal packet has first been sent. If |
| 268 | // we do, timestamps get messed up. |
| 269 | if (packet_counter_ > 0) { |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 270 | return true; |
Sebastian Jansson | 682c649 | 2018-04-12 13:08:22 +0200 | [diff] [blame] | 271 | } |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame] | 272 | } |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 273 | } |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
| 277 | void PacedSender::Process() { |
| 278 | rtc::CritScope cs(&critsect_); |
| 279 | int64_t now_us = clock_->TimeInMicroseconds(); |
| 280 | int64_t elapsed_time_ms = UpdateTimeAndGetElapsedMs(now_us); |
| 281 | if (ShouldSendKeepalive(now_us)) { |
| 282 | critsect_.Leave(); |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 283 | size_t bytes_sent = packet_router_->TimeToSendPadding(1, PacedPacketInfo()); |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 284 | critsect_.Enter(); |
| 285 | OnPaddingSent(bytes_sent); |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Sebastian Jansson | c235a8d | 2018-06-15 14:46:11 +0200 | [diff] [blame] | 288 | if (paused_) |
| 289 | return; |
stefan | 9e117c5e1 | 2017-08-16 08:16:25 -0700 | [diff] [blame] | 290 | |
| 291 | if (elapsed_time_ms > 0) { |
Sebastian Jansson | c235a8d | 2018-06-15 14:46:11 +0200 | [diff] [blame] | 292 | int target_bitrate_kbps = pacing_bitrate_kbps_; |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 293 | size_t queue_size_bytes = packets_.SizeInBytes(); |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 294 | if (queue_size_bytes > 0) { |
| 295 | // Assuming equal size packets and input/output rate, the average packet |
| 296 | // has avg_time_left_ms left to get queue_size_bytes out of the queue, if |
| 297 | // time constraint shall be met. Determine bitrate needed for that. |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 298 | packets_.UpdateQueueTime(TimeMilliseconds()); |
Sebastian Jansson | 0601d68 | 2018-06-25 19:23:05 +0200 | [diff] [blame] | 299 | if (drain_large_queues_) { |
| 300 | int64_t avg_time_left_ms = std::max<int64_t>( |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 301 | 1, queue_time_limit - packets_.AverageQueueTimeMs()); |
Sebastian Jansson | 0601d68 | 2018-06-25 19:23:05 +0200 | [diff] [blame] | 302 | int min_bitrate_needed_kbps = |
| 303 | static_cast<int>(queue_size_bytes * 8 / avg_time_left_ms); |
Evan Shrubsole | fee13e8 | 2019-02-26 15:25:52 +0100 | [diff] [blame] | 304 | if (min_bitrate_needed_kbps > target_bitrate_kbps) { |
Sebastian Jansson | 0601d68 | 2018-06-25 19:23:05 +0200 | [diff] [blame] | 305 | target_bitrate_kbps = min_bitrate_needed_kbps; |
Evan Shrubsole | fee13e8 | 2019-02-26 15:25:52 +0100 | [diff] [blame] | 306 | RTC_LOG(LS_VERBOSE) << "bwe:large_pacing_queue pacing_rate_kbps=" |
| 307 | << target_bitrate_kbps; |
| 308 | } |
Sebastian Jansson | 0601d68 | 2018-06-25 19:23:05 +0200 | [diff] [blame] | 309 | } |
sprang | 0a43fef | 2015-11-20 09:00:37 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 312 | media_budget_.set_target_rate_kbps(target_bitrate_kbps); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 313 | UpdateBudgetWithElapsedTime(elapsed_time_ms); |
stefan@webrtc.org | 80865fd | 2013-08-09 11:31:23 +0000 | [diff] [blame] | 314 | } |
philipel | a1ed0b3 | 2016-06-01 06:31:17 -0700 | [diff] [blame] | 315 | |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 316 | bool is_probing = prober_.IsProbing(); |
philipel | c7bf32a | 2017-02-17 03:59:43 -0800 | [diff] [blame] | 317 | PacedPacketInfo pacing_info; |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 318 | size_t bytes_sent = 0; |
| 319 | size_t recommended_probe_size = 0; |
| 320 | if (is_probing) { |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 321 | pacing_info = prober_.CurrentCluster(); |
| 322 | recommended_probe_size = prober_.RecommendedMinProbeSize(); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 323 | } |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 324 | // The paused state is checked in the loop since it leaves the critical |
| 325 | // section allowing the paused state to be changed from other code. |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 326 | while (!packets_.Empty() && !paused_) { |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 327 | const auto* packet = GetPendingPacket(pacing_info); |
| 328 | if (packet == nullptr) |
| 329 | break; |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 330 | |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 331 | critsect_.Leave(); |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 332 | RtpPacketSendResult success = packet_router_->TimeToSendPacket( |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 333 | packet->ssrc, packet->sequence_number, packet->capture_time_ms, |
| 334 | packet->retransmission, pacing_info); |
| 335 | critsect_.Enter(); |
Erik Språng | d287962 | 2019-05-10 08:29:01 -0700 | [diff] [blame] | 336 | if (success == RtpPacketSendResult::kSuccess || |
| 337 | success == RtpPacketSendResult::kPacketNotFound) { |
| 338 | // Packet sent or invalid packet, remove it from queue. |
| 339 | // TODO(webrtc:8052): Don't consume media budget on kInvalid. |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 340 | bytes_sent += packet->bytes; |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame] | 341 | // Send succeeded, remove it from the queue. |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 342 | OnPacketSent(packet); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 343 | if (is_probing && bytes_sent > recommended_probe_size) |
| 344 | break; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 345 | } else { |
| 346 | // Send failed, put it back into the queue. |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 347 | packets_.CancelPop(*packet); |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 348 | break; |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 349 | } |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 350 | } |
Peter Boström | e23e737 | 2015-10-08 11:44:14 +0200 | [diff] [blame] | 351 | |
Sebastian Jansson | 60570dc | 2018-09-13 17:11:06 +0200 | [diff] [blame] | 352 | if (packets_.Empty() && !Congested()) { |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 353 | // We can not send padding unless a normal packet has first been sent. If we |
| 354 | // do, timestamps get messed up. |
| 355 | if (packet_counter_ > 0) { |
| 356 | int padding_needed = |
| 357 | static_cast<int>(is_probing ? (recommended_probe_size - bytes_sent) |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 358 | : padding_budget_.bytes_remaining()); |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 359 | if (padding_needed > 0) { |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 360 | critsect_.Leave(); |
| 361 | size_t padding_sent = |
Erik Språng | e794243 | 2019-06-12 13:30:02 +0200 | [diff] [blame] | 362 | packet_router_->TimeToSendPadding(padding_needed, pacing_info); |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 363 | critsect_.Enter(); |
| 364 | bytes_sent += padding_sent; |
| 365 | OnPaddingSent(padding_sent); |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 366 | } |
isheriff | cc5903e | 2016-10-04 08:29:38 -0700 | [diff] [blame] | 367 | } |
perkj | 71ee44c | 2016-06-15 00:47:53 -0700 | [diff] [blame] | 368 | } |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 369 | if (is_probing) { |
| 370 | probing_send_failure_ = bytes_sent == 0; |
| 371 | if (!probing_send_failure_) |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 372 | prober_.ProbeSent(TimeMilliseconds(), bytes_sent); |
philipel | b61927c | 2017-02-28 07:05:23 -0800 | [diff] [blame] | 373 | } |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 374 | } |
| 375 | |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 376 | void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) { |
Karl Wiberg | 4343273 | 2018-05-23 11:13:31 +0200 | [diff] [blame] | 377 | RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << process_thread; |
Sebastian Jansson | 439f0bc | 2018-02-20 10:46:39 +0100 | [diff] [blame] | 378 | rtc::CritScope cs(&process_thread_lock_); |
tommi | 919dce2 | 2017-03-15 07:45:36 -0700 | [diff] [blame] | 379 | process_thread_ = process_thread; |
| 380 | } |
| 381 | |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 382 | const RoundRobinPacketQueue::Packet* PacedSender::GetPendingPacket( |
| 383 | const PacedPacketInfo& pacing_info) { |
| 384 | // Since we need to release the lock in order to send, we first pop the |
| 385 | // element from the priority queue but keep it in storage, so that we can |
| 386 | // reinsert it if send fails. |
| 387 | const RoundRobinPacketQueue::Packet* packet = &packets_.BeginPop(); |
| 388 | bool audio_packet = packet->priority == kHighPriority; |
Sebastian Jansson | 470a5ea | 2019-01-23 12:37:49 +0100 | [diff] [blame] | 389 | bool apply_pacing = !audio_packet || pace_audio_; |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 390 | if (apply_pacing && (Congested() || (media_budget_.bytes_remaining() == 0 && |
Sebastian Jansson | ce4829a | 2018-06-15 14:47:35 +0200 | [diff] [blame] | 391 | pacing_info.probe_cluster_id == |
| 392 | PacedPacketInfo::kNotAProbe))) { |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 393 | packets_.CancelPop(*packet); |
| 394 | return nullptr; |
terelius | 8b70faf | 2016-08-01 09:47:31 -0700 | [diff] [blame] | 395 | } |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 396 | return packet; |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 399 | void PacedSender::OnPacketSent(const RoundRobinPacketQueue::Packet* packet) { |
| 400 | if (first_sent_packet_ms_ == -1) |
| 401 | first_sent_packet_ms_ = TimeMilliseconds(); |
| 402 | bool audio_packet = packet->priority == kHighPriority; |
| 403 | if (!audio_packet || account_for_audio_) { |
| 404 | // Update media bytes sent. |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 405 | UpdateBudgetWithBytesSent(packet->bytes); |
| 406 | last_send_time_us_ = clock_->TimeInMicroseconds(); |
| 407 | } |
| 408 | // Send succeeded, remove it from the queue. |
| 409 | packets_.FinalizePop(*packet); |
| 410 | } |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 411 | |
Sebastian Jansson | 916ae08 | 2018-10-26 13:10:23 +0200 | [diff] [blame] | 412 | void PacedSender::OnPaddingSent(size_t bytes_sent) { |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 413 | if (bytes_sent > 0) { |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 414 | UpdateBudgetWithBytesSent(bytes_sent); |
Stefan Holmer | 01b4888 | 2015-05-05 10:21:24 +0200 | [diff] [blame] | 415 | } |
Sebastian Jansson | c235a8d | 2018-06-15 14:46:11 +0200 | [diff] [blame] | 416 | last_send_time_us_ = clock_->TimeInMicroseconds(); |
stefan@webrtc.org | 19a40ff | 2013-11-27 14:16:20 +0000 | [diff] [blame] | 417 | } |
| 418 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 419 | void PacedSender::UpdateBudgetWithElapsedTime(int64_t delta_time_ms) { |
Sebastian Jansson | a1630f8 | 2018-02-21 13:39:26 +0100 | [diff] [blame] | 420 | delta_time_ms = std::min(kMaxIntervalTimeMs, delta_time_ms); |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 421 | media_budget_.IncreaseBudget(delta_time_ms); |
| 422 | padding_budget_.IncreaseBudget(delta_time_ms); |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 423 | } |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 424 | |
| 425 | void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) { |
Sebastian Jansson | 45d9c1d | 2018-03-09 12:48:01 +0100 | [diff] [blame] | 426 | outstanding_bytes_ += bytes_sent; |
Sebastian Jansson | 0391446 | 2018-10-11 20:22:03 +0200 | [diff] [blame] | 427 | media_budget_.UseBudget(bytes_sent); |
| 428 | padding_budget_.UseBudget(bytes_sent); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 429 | } |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 430 | |
sprang | 89c4a7e | 2017-06-30 13:27:40 -0700 | [diff] [blame] | 431 | void PacedSender::SetQueueTimeLimit(int limit_ms) { |
| 432 | rtc::CritScope cs(&critsect_); |
| 433 | queue_time_limit = limit_ms; |
| 434 | } |
| 435 | |
pwestin@webrtc.org | b518017 | 2012-11-09 20:56:23 +0000 | [diff] [blame] | 436 | } // namespace webrtc |