Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | |
| 11 | #include "modules/pacing/pacing_controller.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "absl/memory/memory.h" |
| 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 "rtc_base/time_utils.h" |
| 24 | #include "system_wrappers/include/clock.h" |
| 25 | |
| 26 | namespace webrtc { |
| 27 | namespace { |
| 28 | // Time limit in milliseconds between packet bursts. |
| 29 | constexpr TimeDelta kDefaultMinPacketLimit = TimeDelta::Millis<5>(); |
| 30 | constexpr TimeDelta kCongestedPacketInterval = TimeDelta::Millis<500>(); |
| 31 | constexpr TimeDelta kMaxElapsedTime = TimeDelta::Seconds<2>(); |
| 32 | |
| 33 | // Upper cap on process interval, in case process has not been called in a long |
| 34 | // time. |
| 35 | constexpr TimeDelta kMaxProcessingInterval = TimeDelta::Millis<30>(); |
| 36 | |
| 37 | bool IsDisabled(const WebRtcKeyValueConfig& field_trials, |
| 38 | absl::string_view key) { |
| 39 | return field_trials.Lookup(key).find("Disabled") == 0; |
| 40 | } |
| 41 | |
| 42 | bool IsEnabled(const WebRtcKeyValueConfig& field_trials, |
| 43 | absl::string_view key) { |
| 44 | return field_trials.Lookup(key).find("Enabled") == 0; |
| 45 | } |
| 46 | |
| 47 | int GetPriorityForType(RtpPacketToSend::Type type) { |
| 48 | switch (type) { |
| 49 | case RtpPacketToSend::Type::kAudio: |
| 50 | // Audio is always prioritized over other packet types. |
| 51 | return 0; |
| 52 | case RtpPacketToSend::Type::kRetransmission: |
| 53 | // Send retransmissions before new media. |
| 54 | return 1; |
| 55 | case RtpPacketToSend::Type::kVideo: |
| 56 | // Video has "normal" priority, in the old speak. |
| 57 | return 2; |
| 58 | case RtpPacketToSend::Type::kForwardErrorCorrection: |
| 59 | // Send redundancy concurrently to video. If it is delayed it might have a |
| 60 | // lower chance of being useful. |
| 61 | return 2; |
| 62 | case RtpPacketToSend::Type::kPadding: |
| 63 | // Packets that are in themselves likely useless, only sent to keep the |
| 64 | // BWE high. |
| 65 | return 3; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | } // namespace |
| 70 | |
| 71 | const TimeDelta PacingController::kMaxExpectedQueueLength = |
| 72 | TimeDelta::Millis<2000>(); |
| 73 | const float PacingController::kDefaultPaceMultiplier = 2.5f; |
| 74 | const TimeDelta PacingController::kPausedProcessInterval = |
| 75 | kCongestedPacketInterval; |
| 76 | |
| 77 | PacingController::PacingController(Clock* clock, |
| 78 | PacketSender* packet_sender, |
| 79 | RtcEventLog* event_log, |
| 80 | const WebRtcKeyValueConfig* field_trials) |
| 81 | : clock_(clock), |
| 82 | packet_sender_(packet_sender), |
| 83 | fallback_field_trials_( |
| 84 | !field_trials ? absl::make_unique<FieldTrialBasedConfig>() : nullptr), |
| 85 | field_trials_(field_trials ? field_trials : fallback_field_trials_.get()), |
| 86 | drain_large_queues_( |
| 87 | !IsDisabled(*field_trials_, "WebRTC-Pacer-DrainQueue")), |
| 88 | send_padding_if_silent_( |
| 89 | IsEnabled(*field_trials_, "WebRTC-Pacer-PadInSilence")), |
| 90 | pace_audio_(!IsDisabled(*field_trials_, "WebRTC-Pacer-BlockAudio")), |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 91 | send_side_bwe_with_overhead_( |
| 92 | IsEnabled(*field_trials_, "WebRTC-SendSideBwe-WithOverhead")), |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 93 | min_packet_limit_(kDefaultMinPacketLimit), |
| 94 | last_timestamp_(clock_->CurrentTime()), |
| 95 | paused_(false), |
| 96 | media_budget_(0), |
| 97 | padding_budget_(0), |
| 98 | prober_(*field_trials_), |
| 99 | probing_send_failure_(false), |
| 100 | padding_failure_state_(false), |
| 101 | pacing_bitrate_(DataRate::Zero()), |
| 102 | time_last_process_(clock->CurrentTime()), |
| 103 | last_send_time_(time_last_process_), |
| 104 | packet_queue_(time_last_process_, field_trials), |
| 105 | packet_counter_(0), |
| 106 | congestion_window_size_(DataSize::PlusInfinity()), |
| 107 | outstanding_data_(DataSize::Zero()), |
| 108 | queue_time_limit(kMaxExpectedQueueLength), |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 109 | account_for_audio_(false) { |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 110 | if (!drain_large_queues_) { |
| 111 | RTC_LOG(LS_WARNING) << "Pacer queues will not be drained," |
| 112 | "pushback experiment must be enabled."; |
| 113 | } |
| 114 | FieldTrialParameter<int> min_packet_limit_ms("", min_packet_limit_.ms()); |
| 115 | ParseFieldTrial({&min_packet_limit_ms}, |
| 116 | field_trials_->Lookup("WebRTC-Pacer-MinPacketLimitMs")); |
| 117 | min_packet_limit_ = TimeDelta::ms(min_packet_limit_ms.Get()); |
| 118 | UpdateBudgetWithElapsedTime(min_packet_limit_); |
| 119 | } |
| 120 | |
| 121 | PacingController::~PacingController() = default; |
| 122 | |
| 123 | void PacingController::CreateProbeCluster(DataRate bitrate, int cluster_id) { |
| 124 | prober_.CreateProbeCluster(bitrate.bps(), CurrentTime().ms(), cluster_id); |
| 125 | } |
| 126 | |
| 127 | void PacingController::Pause() { |
| 128 | if (!paused_) |
| 129 | RTC_LOG(LS_INFO) << "PacedSender paused."; |
| 130 | paused_ = true; |
| 131 | packet_queue_.SetPauseState(true, CurrentTime()); |
| 132 | } |
| 133 | |
| 134 | void PacingController::Resume() { |
| 135 | if (paused_) |
| 136 | RTC_LOG(LS_INFO) << "PacedSender resumed."; |
| 137 | paused_ = false; |
| 138 | packet_queue_.SetPauseState(false, CurrentTime()); |
| 139 | } |
| 140 | |
| 141 | bool PacingController::IsPaused() const { |
| 142 | return paused_; |
| 143 | } |
| 144 | |
| 145 | void PacingController::SetCongestionWindow(DataSize congestion_window_size) { |
| 146 | congestion_window_size_ = congestion_window_size; |
| 147 | } |
| 148 | |
| 149 | void PacingController::UpdateOutstandingData(DataSize outstanding_data) { |
| 150 | outstanding_data_ = outstanding_data; |
| 151 | } |
| 152 | |
| 153 | bool PacingController::Congested() const { |
| 154 | if (congestion_window_size_.IsFinite()) { |
| 155 | return outstanding_data_ >= congestion_window_size_; |
| 156 | } |
| 157 | return false; |
| 158 | } |
| 159 | |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 160 | DataSize PacingController::PacketSize(const RtpPacketToSend& packet) const { |
| 161 | return DataSize::bytes(send_side_bwe_with_overhead_ |
| 162 | ? packet.size() |
| 163 | : packet.payload_size() + packet.padding_size()); |
| 164 | } |
| 165 | |
| 166 | bool PacingController::ShouldSendPacket(const RtpPacketToSend& packet, |
| 167 | PacedPacketInfo pacing_info) const { |
| 168 | if (!pace_audio_ && packet.packet_type() == RtpPacketToSend::Type::kAudio) { |
| 169 | // If audio, and we don't pace audio, pop packet regardless. |
| 170 | return true; |
| 171 | } |
| 172 | // Pacing applies, check if we can. |
| 173 | if (Congested()) { |
| 174 | // Don't try to send more packets while we are congested. |
| 175 | return false; |
| 176 | } else if (media_budget_.bytes_remaining() == 0 && |
| 177 | pacing_info.probe_cluster_id == PacedPacketInfo::kNotAProbe) { |
| 178 | // No budget left, and not a probe (which can override budget levels), |
| 179 | // don't pop this packet. |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // No blocks for sending packets found! |
| 184 | return true; |
| 185 | } |
| 186 | |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 187 | Timestamp PacingController::CurrentTime() const { |
| 188 | Timestamp time = clock_->CurrentTime(); |
| 189 | if (time < last_timestamp_) { |
| 190 | RTC_LOG(LS_WARNING) |
| 191 | << "Non-monotonic clock behavior observed. Previous timestamp: " |
| 192 | << last_timestamp_.ms() << ", new timestamp: " << time.ms(); |
| 193 | RTC_DCHECK_GE(time, last_timestamp_); |
| 194 | time = last_timestamp_; |
| 195 | } |
| 196 | last_timestamp_ = time; |
| 197 | return time; |
| 198 | } |
| 199 | |
| 200 | void PacingController::SetProbingEnabled(bool enabled) { |
| 201 | RTC_CHECK_EQ(0, packet_counter_); |
| 202 | prober_.SetEnabled(enabled); |
| 203 | } |
| 204 | |
| 205 | void PacingController::SetPacingRates(DataRate pacing_rate, |
| 206 | DataRate padding_rate) { |
| 207 | RTC_DCHECK_GT(pacing_rate, DataRate::Zero()); |
| 208 | pacing_bitrate_ = pacing_rate; |
| 209 | padding_budget_.set_target_rate_kbps(padding_rate.kbps()); |
| 210 | |
| 211 | RTC_LOG(LS_VERBOSE) << "bwe:pacer_updated pacing_kbps=" |
| 212 | << pacing_bitrate_.kbps() |
| 213 | << " padding_budget_kbps=" << padding_rate.kbps(); |
| 214 | } |
| 215 | |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 216 | void PacingController::EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet) { |
| 217 | RTC_DCHECK(pacing_bitrate_ > DataRate::Zero()) |
| 218 | << "SetPacingRate must be called before InsertPacket."; |
| 219 | |
| 220 | Timestamp now = CurrentTime(); |
| 221 | prober_.OnIncomingPacket(packet->payload_size()); |
| 222 | |
| 223 | if (packet->capture_time_ms() < 0) { |
| 224 | packet->set_capture_time_ms(now.ms()); |
| 225 | } |
| 226 | |
| 227 | RTC_CHECK(packet->packet_type()); |
| 228 | int priority = GetPriorityForType(*packet->packet_type()); |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 229 | DataSize size = PacketSize(*packet); |
| 230 | packet_queue_.Push(priority, now, packet_counter_++, size, std::move(packet)); |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void PacingController::SetAccountForAudioPackets(bool account_for_audio) { |
| 234 | account_for_audio_ = account_for_audio; |
| 235 | } |
| 236 | |
| 237 | TimeDelta PacingController::ExpectedQueueTime() const { |
| 238 | RTC_DCHECK_GT(pacing_bitrate_, DataRate::Zero()); |
| 239 | return TimeDelta::ms( |
| 240 | (QueueSizeData().bytes() * 8 * rtc::kNumMillisecsPerSec) / |
| 241 | pacing_bitrate_.bps()); |
| 242 | } |
| 243 | |
| 244 | size_t PacingController::QueueSizePackets() const { |
| 245 | return packet_queue_.SizeInPackets(); |
| 246 | } |
| 247 | |
| 248 | DataSize PacingController::QueueSizeData() const { |
| 249 | return packet_queue_.Size(); |
| 250 | } |
| 251 | |
| 252 | absl::optional<Timestamp> PacingController::FirstSentPacketTime() const { |
| 253 | return first_sent_packet_time_; |
| 254 | } |
| 255 | |
| 256 | TimeDelta PacingController::OldestPacketWaitTime() const { |
| 257 | Timestamp oldest_packet = packet_queue_.OldestEnqueueTime(); |
| 258 | if (oldest_packet.IsInfinite()) { |
| 259 | return TimeDelta::Zero(); |
| 260 | } |
| 261 | |
| 262 | return CurrentTime() - oldest_packet; |
| 263 | } |
| 264 | |
| 265 | TimeDelta PacingController::UpdateTimeAndGetElapsed(Timestamp now) { |
| 266 | TimeDelta elapsed_time = now - time_last_process_; |
| 267 | time_last_process_ = now; |
| 268 | if (elapsed_time > kMaxElapsedTime) { |
| 269 | RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time.ms() |
| 270 | << " ms) longer than expected, limiting to " |
| 271 | << kMaxElapsedTime.ms(); |
| 272 | elapsed_time = kMaxElapsedTime; |
| 273 | } |
| 274 | return elapsed_time; |
| 275 | } |
| 276 | |
| 277 | bool PacingController::ShouldSendKeepalive(Timestamp now) const { |
| 278 | if (send_padding_if_silent_ || paused_ || Congested()) { |
| 279 | // We send a padding packet every 500 ms to ensure we won't get stuck in |
| 280 | // congested state due to no feedback being received. |
| 281 | TimeDelta elapsed_since_last_send = now - last_send_time_; |
| 282 | if (elapsed_since_last_send >= kCongestedPacketInterval) { |
| 283 | // We can not send padding unless a normal packet has first been sent. If |
| 284 | // we do, timestamps get messed up. |
| 285 | if (packet_counter_ > 0) { |
| 286 | return true; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | absl::optional<TimeDelta> PacingController::TimeUntilNextProbe() { |
| 294 | if (!prober_.IsProbing()) { |
| 295 | return absl::nullopt; |
| 296 | } |
| 297 | |
| 298 | TimeDelta time_delta = |
| 299 | TimeDelta::ms(prober_.TimeUntilNextProbe(CurrentTime().ms())); |
| 300 | if (time_delta > TimeDelta::Zero() || |
| 301 | (time_delta == TimeDelta::Zero() && !probing_send_failure_)) { |
| 302 | return time_delta; |
| 303 | } |
| 304 | |
| 305 | return absl::nullopt; |
| 306 | } |
| 307 | |
| 308 | TimeDelta PacingController::TimeElapsedSinceLastProcess() const { |
| 309 | return CurrentTime() - time_last_process_; |
| 310 | } |
| 311 | |
| 312 | void PacingController::ProcessPackets() { |
| 313 | Timestamp now = CurrentTime(); |
| 314 | TimeDelta elapsed_time = UpdateTimeAndGetElapsed(now); |
| 315 | if (ShouldSendKeepalive(now)) { |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 316 | DataSize keepalive_data_sent = DataSize::Zero(); |
| 317 | std::vector<std::unique_ptr<RtpPacketToSend>> keepalive_packets = |
| 318 | packet_sender_->GeneratePadding(DataSize::bytes(1)); |
| 319 | for (auto& packet : keepalive_packets) { |
| 320 | keepalive_data_sent += |
| 321 | DataSize::bytes(packet->payload_size() + packet->padding_size()); |
| 322 | packet_sender_->SendRtpPacket(std::move(packet), PacedPacketInfo()); |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 323 | } |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 324 | OnPaddingSent(keepalive_data_sent); |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (paused_) |
| 328 | return; |
| 329 | |
| 330 | if (elapsed_time > TimeDelta::Zero()) { |
| 331 | DataRate target_rate = pacing_bitrate_; |
| 332 | DataSize queue_size_data = packet_queue_.Size(); |
| 333 | if (queue_size_data > DataSize::Zero()) { |
| 334 | // Assuming equal size packets and input/output rate, the average packet |
| 335 | // has avg_time_left_ms left to get queue_size_bytes out of the queue, if |
| 336 | // time constraint shall be met. Determine bitrate needed for that. |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 337 | packet_queue_.UpdateQueueTime(now); |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 338 | if (drain_large_queues_) { |
| 339 | TimeDelta avg_time_left = |
| 340 | std::max(TimeDelta::ms(1), |
| 341 | queue_time_limit - packet_queue_.AverageQueueTime()); |
| 342 | DataRate min_rate_needed = queue_size_data / avg_time_left; |
| 343 | if (min_rate_needed > target_rate) { |
| 344 | target_rate = min_rate_needed; |
| 345 | RTC_LOG(LS_VERBOSE) << "bwe:large_pacing_queue pacing_rate_kbps=" |
| 346 | << target_rate.kbps(); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | media_budget_.set_target_rate_kbps(target_rate.kbps()); |
| 352 | UpdateBudgetWithElapsedTime(elapsed_time); |
| 353 | } |
| 354 | |
| 355 | bool is_probing = prober_.IsProbing(); |
| 356 | PacedPacketInfo pacing_info; |
| 357 | absl::optional<DataSize> recommended_probe_size; |
| 358 | if (is_probing) { |
| 359 | pacing_info = prober_.CurrentCluster(); |
| 360 | recommended_probe_size = DataSize::bytes(prober_.RecommendedMinProbeSize()); |
| 361 | } |
| 362 | |
| 363 | DataSize data_sent = DataSize::Zero(); |
| 364 | // The paused state is checked in the loop since it leaves the critical |
| 365 | // section allowing the paused state to be changed from other code. |
| 366 | while (!paused_) { |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 367 | std::unique_ptr<RtpPacketToSend> rtp_packet; |
| 368 | if (!packet_queue_.Empty()) { |
| 369 | const RtpPacketToSend& stored_packet = packet_queue_.Top(); |
| 370 | if (ShouldSendPacket(stored_packet, pacing_info)) { |
| 371 | rtp_packet = packet_queue_.Pop(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (rtp_packet == nullptr) { |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 376 | // No packet available to send, check if we should send padding. |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 377 | DataSize padding_to_add = PaddingToAdd(recommended_probe_size, data_sent); |
| 378 | if (padding_to_add > DataSize::Zero()) { |
| 379 | std::vector<std::unique_ptr<RtpPacketToSend>> padding_packets = |
| 380 | packet_sender_->GeneratePadding(padding_to_add); |
| 381 | if (padding_packets.empty()) { |
| 382 | // No padding packets were generated, quite send loop. |
| 383 | break; |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 384 | } |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 385 | for (auto& packet : padding_packets) { |
| 386 | EnqueuePacket(std::move(packet)); |
| 387 | } |
| 388 | // Continue loop to send the padding that was just added. |
| 389 | continue; |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Can't fetch new packet and no padding to send, exit send loop. |
| 393 | break; |
| 394 | } |
| 395 | |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 396 | RTC_DCHECK(rtp_packet); |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 397 | const DataSize packet_size = PacketSize(*rtp_packet); |
| 398 | const bool audio_packet = |
| 399 | rtp_packet->packet_type() == RtpPacketToSend::Type::kAudio; |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 400 | packet_sender_->SendRtpPacket(std::move(rtp_packet), pacing_info); |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 401 | data_sent += packet_size; |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 402 | |
Erik Språng | 7db900e | 2019-08-29 09:24:13 +0200 | [diff] [blame^] | 403 | if (!first_sent_packet_time_) { |
| 404 | first_sent_packet_time_ = now; |
| 405 | } |
| 406 | |
| 407 | if (!audio_packet || account_for_audio_) { |
| 408 | // Update media bytes sent. |
| 409 | UpdateBudgetWithSentData(packet_size); |
| 410 | last_send_time_ = now; |
| 411 | } |
| 412 | |
| 413 | padding_failure_state_ = false; |
| 414 | |
Erik Språng | f5815fa | 2019-08-21 14:27:31 +0200 | [diff] [blame] | 415 | if (recommended_probe_size && data_sent > *recommended_probe_size) |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 416 | break; |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (is_probing) { |
| 420 | probing_send_failure_ = data_sent == DataSize::Zero(); |
| 421 | if (!probing_send_failure_) { |
| 422 | prober_.ProbeSent(CurrentTime().ms(), data_sent.bytes()); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | DataSize PacingController::PaddingToAdd( |
| 428 | absl::optional<DataSize> recommended_probe_size, |
| 429 | DataSize data_sent) { |
| 430 | if (!packet_queue_.Empty()) { |
| 431 | // Actual payload available, no need to add padding. |
| 432 | return DataSize::Zero(); |
| 433 | } |
| 434 | |
| 435 | if (Congested()) { |
| 436 | // Don't add padding if congested, even if requested for probing. |
| 437 | return DataSize::Zero(); |
| 438 | } |
| 439 | |
| 440 | if (packet_counter_ == 0) { |
| 441 | // We can not send padding unless a normal packet has first been sent. If we |
| 442 | // do, timestamps get messed up. |
| 443 | return DataSize::Zero(); |
| 444 | } |
| 445 | |
| 446 | if (recommended_probe_size) { |
| 447 | if (*recommended_probe_size > data_sent) { |
| 448 | return *recommended_probe_size - data_sent; |
| 449 | } |
| 450 | return DataSize::Zero(); |
| 451 | } |
| 452 | |
| 453 | return DataSize::bytes(padding_budget_.bytes_remaining()); |
| 454 | } |
| 455 | |
Erik Språng | d05edec | 2019-08-14 10:43:47 +0200 | [diff] [blame] | 456 | void PacingController::OnPaddingSent(DataSize data_sent) { |
| 457 | if (data_sent > DataSize::Zero()) { |
| 458 | UpdateBudgetWithSentData(data_sent); |
| 459 | } else { |
| 460 | padding_failure_state_ = true; |
| 461 | } |
| 462 | last_send_time_ = CurrentTime(); |
| 463 | } |
| 464 | |
| 465 | void PacingController::UpdateBudgetWithElapsedTime(TimeDelta delta) { |
| 466 | delta = std::min(kMaxProcessingInterval, delta); |
| 467 | media_budget_.IncreaseBudget(delta.ms()); |
| 468 | padding_budget_.IncreaseBudget(delta.ms()); |
| 469 | } |
| 470 | |
| 471 | void PacingController::UpdateBudgetWithSentData(DataSize size) { |
| 472 | outstanding_data_ += size; |
| 473 | media_budget_.UseBudget(size.bytes()); |
| 474 | padding_budget_.UseBudget(size.bytes()); |
| 475 | } |
| 476 | |
| 477 | void PacingController::SetQueueTimeLimit(TimeDelta limit) { |
| 478 | queue_time_limit = limit; |
| 479 | } |
| 480 | |
| 481 | } // namespace webrtc |