| 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 <memory> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 |
| 18 | #include "absl/strings/match.h" |
| 19 | #include "modules/pacing/bitrate_prober.h" |
| 20 | #include "modules/pacing/interval_budget.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/experiments/field_trial_parser.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/time_utils.h" |
| 25 | #include "system_wrappers/include/clock.h" |
| 26 |
| 27 | namespace webrtc { |
| 28 | namespace { |
| 29 | // Time limit in milliseconds between packet bursts. |
| 30 | constexpr TimeDelta kDefaultMinPacketLimit = TimeDelta::Millis(5); |
| 31 | constexpr TimeDelta kCongestedPacketInterval = TimeDelta::Millis(500); |
| 32 | // TODO(sprang): Consider dropping this limit. |
| 33 | // The maximum debt level, in terms of time, capped when sending packets. |
| 34 | constexpr TimeDelta kMaxDebtInTime = TimeDelta::Millis(500); |
| 35 | constexpr TimeDelta kMaxElapsedTime = TimeDelta::Seconds(2); |
| 36 | constexpr TimeDelta kTargetPaddingDuration = TimeDelta::Millis(5); |
| 37 |
| 38 | bool IsDisabled(const FieldTrialsView& field_trials, absl::string_view key) { |
| 39 | return absl::StartsWith(field_trials.Lookup(key), "Disabled"); |
| 40 | } |
| 41 |
| 42 | bool IsEnabled(const FieldTrialsView& field_trials, absl::string_view key) { |
| 43 | return absl::StartsWith(field_trials.Lookup(key), "Enabled"); |
| 44 | } |
| 45 |
| 46 | } // namespace |
| 47 |
| 48 | const TimeDelta PacingController::kMaxExpectedQueueLength = |
| 49 | TimeDelta::Millis(2000); |
| 50 | const TimeDelta PacingController::kPausedProcessInterval = |
| 51 | kCongestedPacketInterval; |
| 52 | const TimeDelta PacingController::kMinSleepTime = TimeDelta::Millis(1); |
| 53 | const TimeDelta PacingController::kMaxEarlyProbeProcessing = |
| 54 | TimeDelta::Millis(1); |
| 55 |
| 56 | PacingController::PacingController(Clock* clock, |
| 57 | PacketSender* packet_sender, |
| 58 | const FieldTrialsView& field_trials) |
| 59 | : clock_(clock), |
| 60 | packet_sender_(packet_sender), |
| 61 | field_trials_(field_trials), |
| 62 | drain_large_queues_( |
| 63 | !IsDisabled(field_trials_, "WebRTC-Pacer-DrainQueue")), |
| 64 | send_padding_if_silent_( |
| 65 | IsEnabled(field_trials_, "WebRTC-Pacer-PadInSilence")), |
| 66 | pace_audio_(IsEnabled(field_trials_, "WebRTC-Pacer-BlockAudio")), |
| 67 | ignore_transport_overhead_( |
| 68 | IsEnabled(field_trials_, "WebRTC-Pacer-IgnoreTransportOverhead")), |
| 69 | fast_retransmissions_( |
| 70 | IsEnabled(field_trials_, "WebRTC-Pacer-FastRetransmissions")), |
| 71 | min_packet_limit_(kDefaultMinPacketLimit), |
| 72 | transport_overhead_per_packet_(DataSize::Zero()), |
| 73 | send_burst_interval_(TimeDelta::Zero()), |
| 74 | last_timestamp_(clock_->CurrentTime()), |
| 75 | paused_(false), |
| 76 | media_debt_(DataSize::Zero()), |
| 77 | padding_debt_(DataSize::Zero()), |
| 78 | pacing_rate_(DataRate::Zero()), |
| 79 | adjusted_media_rate_(DataRate::Zero()), |
| 80 | padding_rate_(DataRate::Zero()), |
| 81 | prober_(field_trials_), |
| 82 | probing_send_failure_(false), |
| 83 | last_process_time_(clock->CurrentTime()), |
| 84 | last_send_time_(last_process_time_), |
| 85 | seen_first_packet_(false), |
| 86 | packet_queue_(/*creation_time=*/last_process_time_), |
| 87 | congested_(false), |
| 88 | queue_time_limit_(kMaxExpectedQueueLength), |
| 89 | account_for_audio_(false), |
| 90 | include_overhead_(false) { |
| 91 | if (!drain_large_queues_) { |
| 92 | RTC_LOG(LS_WARNING) << "Pacer queues will not be drained," |
| 93 | "pushback experiment must be enabled."; |
| 94 | } |
| 95 | FieldTrialParameter<int> min_packet_limit_ms("", min_packet_limit_.ms()); |
| 96 | ParseFieldTrial({&min_packet_limit_ms}, |
| 97 | field_trials_.Lookup("WebRTC-Pacer-MinPacketLimitMs")); |
| 98 | min_packet_limit_ = TimeDelta::Millis(min_packet_limit_ms.Get()); |
| 99 | UpdateBudgetWithElapsedTime(min_packet_limit_); |
| 100 | } |
| 101 |
| 102 | PacingController::~PacingController() = default; |
| 103 |
| 104 | void PacingController::CreateProbeCluster(DataRate bitrate, int cluster_id) { |
| 105 | prober_.CreateProbeCluster({.at_time = CurrentTime(), |
| 106 | .target_data_rate = bitrate, |
| 107 | .target_duration = TimeDelta::Millis(15), |
| 108 | .target_probe_count = 5, |
| 109 | .id = cluster_id}); |
| 110 | } |
| 111 |
| 112 | void PacingController::CreateProbeClusters( |
| 113 | rtc::ArrayView<const ProbeClusterConfig> probe_cluster_configs) { |
| 114 | for (const ProbeClusterConfig probe_cluster_config : probe_cluster_configs) { |
| 115 | prober_.CreateProbeCluster(probe_cluster_config); |
| 116 | } |
| 117 | } |
| 118 |
| 119 | void PacingController::Pause() { |
| 120 | if (!paused_) |
| 121 | RTC_LOG(LS_INFO) << "PacedSender paused."; |
| 122 | paused_ = true; |
| 123 | packet_queue_.SetPauseState(true, CurrentTime()); |
| 124 | } |
| 125 |
| 126 | void PacingController::Resume() { |
| 127 | if (paused_) |
| 128 | RTC_LOG(LS_INFO) << "PacedSender resumed."; |
| 129 | paused_ = false; |
| 130 | packet_queue_.SetPauseState(false, CurrentTime()); |
| 131 | } |
| 132 |
| 133 | bool PacingController::IsPaused() const { |
| 134 | return paused_; |
| 135 | } |
| 136 |
| 137 | void PacingController::SetCongested(bool congested) { |
| 138 | if (congested_ && !congested) { |
| 139 | UpdateBudgetWithElapsedTime(UpdateTimeAndGetElapsed(CurrentTime())); |
| 140 | } |
| 141 | congested_ = congested; |
| 142 | } |
| 143 |
| 144 | bool PacingController::IsProbing() const { |
| 145 | return prober_.is_probing(); |
| 146 | } |
| 147 |
| 148 | Timestamp PacingController::CurrentTime() const { |
| 149 | Timestamp time = clock_->CurrentTime(); |
| 150 | if (time < last_timestamp_) { |
| 151 | RTC_LOG(LS_WARNING) |
| 152 | << "Non-monotonic clock behavior observed. Previous timestamp: " |
| 153 | << last_timestamp_.ms() << ", new timestamp: " << time.ms(); |
| 154 | RTC_DCHECK_GE(time, last_timestamp_); |
| 155 | time = last_timestamp_; |
| 156 | } |
| 157 | last_timestamp_ = time; |
| 158 | return time; |
| 159 | } |
| 160 |
| 161 | void PacingController::SetProbingEnabled(bool enabled) { |
| 162 | RTC_CHECK(!seen_first_packet_); |
| 163 | prober_.SetEnabled(enabled); |
| 164 | } |
| 165 |
| 166 | void PacingController::SetPacingRates(DataRate pacing_rate, |
| 167 | DataRate padding_rate) { |
| 168 | static constexpr DataRate kMaxRate = DataRate::KilobitsPerSec(100'000); |
| 169 | RTC_CHECK_GT(pacing_rate, DataRate::Zero()); |
| 170 | RTC_CHECK_GE(padding_rate, DataRate::Zero()); |
| 171 | if (padding_rate > pacing_rate) { |
| 172 | RTC_LOG(LS_WARNING) << "Padding rate " << padding_rate.kbps() |
| 173 | << "kbps is higher than the pacing rate " |
| 174 | << pacing_rate.kbps() << "kbps, capping."; |
| 175 | padding_rate = pacing_rate; |
| 176 | } |
| 177 |
| 178 | if (pacing_rate > kMaxRate || padding_rate > kMaxRate) { |
| 179 | RTC_LOG(LS_WARNING) << "Very high pacing rates ( > " << kMaxRate.kbps() |
| 180 | << " kbps) configured: pacing = " << pacing_rate.kbps() |
| 181 | << " kbps, padding = " << padding_rate.kbps() |
| 182 | << " kbps."; |
| 183 | } |
| 184 | pacing_rate_ = pacing_rate; |
| 185 | padding_rate_ = padding_rate; |
| 186 | MaybeUpdateMediaRateDueToLongQueue(CurrentTime()); |
| 187 |
| 188 | RTC_LOG(LS_VERBOSE) << "bwe:pacer_updated pacing_kbps=" << pacing_rate_.kbps() |
| 189 | << " padding_budget_kbps=" << padding_rate.kbps(); |
| 190 | } |
| 191 |
| 192 | void PacingController::EnqueuePacket(std::unique_ptr<RtpPacketToSend> packet) { |
| 193 | RTC_DCHECK(pacing_rate_ > DataRate::Zero()) |
| 194 | << "SetPacingRate must be called before InsertPacket."; |
| 195 | RTC_CHECK(packet->packet_type()); |
| 196 |
| 197 | prober_.OnIncomingPacket(DataSize::Bytes(packet->payload_size())); |
| 198 |
| 199 | const Timestamp now = CurrentTime(); |
| 200 | if (packet_queue_.Empty()) { |
| 201 | // If queue is empty, we need to "fast-forward" the last process time, |
| 202 | // so that we don't use passed time as budget for sending the first new |
| 203 | // packet. |
| 204 | Timestamp target_process_time = now; |
| 205 | Timestamp next_send_time = NextSendTime(); |
| 206 | if (next_send_time.IsFinite()) { |
| 207 | // There was already a valid planned send time, such as a keep-alive. |
| 208 | // Use that as last process time only if it's prior to now. |
| 209 | target_process_time = std::min(now, next_send_time); |
| 210 | } |
| 211 | UpdateBudgetWithElapsedTime(UpdateTimeAndGetElapsed(target_process_time)); |
| 212 | } |
| 213 | packet_queue_.Push(now, std::move(packet)); |
| 214 | seen_first_packet_ = true; |
| 215 |
| 216 | // Queue length has increased, check if we need to change the pacing rate. |
| 217 | MaybeUpdateMediaRateDueToLongQueue(now); |
| 218 | } |
| 219 |
| 220 | void PacingController::SetAccountForAudioPackets(bool account_for_audio) { |
| 221 | account_for_audio_ = account_for_audio; |
| 222 | } |
| 223 |
| 224 | void PacingController::SetIncludeOverhead() { |
| 225 | include_overhead_ = true; |
| 226 | } |
| 227 |
| 228 | void PacingController::SetTransportOverhead(DataSize overhead_per_packet) { |
| 229 | if (ignore_transport_overhead_) |
| 230 | return; |
| 231 | transport_overhead_per_packet_ = overhead_per_packet; |
| 232 | } |
| 233 |
| 234 | void PacingController::SetSendBurstInterval(TimeDelta burst_interval) { |
| 235 | send_burst_interval_ = burst_interval; |
| 236 | } |
| 237 |
| 238 | TimeDelta PacingController::ExpectedQueueTime() const { |
| 239 | RTC_DCHECK_GT(adjusted_media_rate_, DataRate::Zero()); |
| 240 | return QueueSizeData() / adjusted_media_rate_; |
| 241 | } |
| 242 |
| 243 | size_t PacingController::QueueSizePackets() const { |
| 244 | return rtc::checked_cast<size_t>(packet_queue_.SizeInPackets()); |
| 245 | } |
| 246 |
| 247 | const std::array<int, kNumMediaTypes>& |
| 248 | PacingController::SizeInPacketsPerRtpPacketMediaType() const { |
| 249 | return packet_queue_.SizeInPacketsPerRtpPacketMediaType(); |
| 250 | } |
| 251 |
| 252 | DataSize PacingController::QueueSizeData() const { |
| 253 | DataSize size = packet_queue_.SizeInPayloadBytes(); |
| 254 | if (include_overhead_) { |
| 255 | size += static_cast<int64_t>(packet_queue_.SizeInPackets()) * |
| 256 | transport_overhead_per_packet_; |
| 257 | } |
| 258 | return size; |
| 259 | } |
| 260 |
| 261 | DataSize PacingController::CurrentBufferLevel() const { |
| 262 | return std::max(media_debt_, padding_debt_); |
| 263 | } |
| 264 |
| 265 | absl::optional<Timestamp> PacingController::FirstSentPacketTime() const { |
| 266 | return first_sent_packet_time_; |
| 267 | } |
| 268 |
| 269 | Timestamp PacingController::OldestPacketEnqueueTime() const { |
| 270 | return packet_queue_.OldestEnqueueTime(); |
| 271 | } |
| 272 |
| 273 | TimeDelta PacingController::UpdateTimeAndGetElapsed(Timestamp now) { |
| 274 | // If no previous processing, or last process was "in the future" because of |
| 275 | // early probe processing, then there is no elapsed time to add budget for. |
| 276 | if (last_process_time_.IsMinusInfinity() || now < last_process_time_) { |
| 277 | return TimeDelta::Zero(); |
| 278 | } |
| 279 | TimeDelta elapsed_time = now - last_process_time_; |
| 280 | last_process_time_ = now; |
| 281 | if (elapsed_time > kMaxElapsedTime) { |
| 282 | RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time.ms() |
| 283 | << " ms) longer than expected, limiting to " |
| 284 | << kMaxElapsedTime.ms(); |
| 285 | elapsed_time = kMaxElapsedTime; |
| 286 | } |
| 287 | return elapsed_time; |
| 288 | } |
| 289 |
| 290 | bool PacingController::ShouldSendKeepalive(Timestamp now) const { |
| 291 | if (send_padding_if_silent_ || paused_ || congested_ || !seen_first_packet_) { |
| 292 | // We send a padding packet every 500 ms to ensure we won't get stuck in |
| 293 | // congested state due to no feedback being received. |
| 294 | if (now - last_send_time_ >= kCongestedPacketInterval) { |
| 295 | return true; |
| 296 | } |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 |
| 301 | Timestamp PacingController::NextSendTime() const { |
| 302 | const Timestamp now = CurrentTime(); |
| 303 | Timestamp next_send_time = Timestamp::PlusInfinity(); |
| 304 |
| 305 | if (paused_) { |
| 306 | return last_send_time_ + kPausedProcessInterval; |
| 307 | } |
| 308 |
| 309 | // If probing is active, that always takes priority. |
| 310 | if (prober_.is_probing() && !probing_send_failure_) { |
| 311 | Timestamp probe_time = prober_.NextProbeTime(now); |
| 312 | if (!probe_time.IsPlusInfinity()) { |
| 313 | return probe_time.IsMinusInfinity() ? now : probe_time; |
| 314 | } |
| 315 | } |
| 316 |
| 317 | // If queue contains a packet which should not be paced, its target send time |
| 318 | // is the time at which it was enqueued. |
| 319 | Timestamp unpaced_send_time = NextUnpacedSendTime(); |
| 320 | if (unpaced_send_time.IsFinite()) { |
| 321 | return unpaced_send_time; |
| 322 | } |
| 323 |
| 324 | if (congested_ || !seen_first_packet_) { |
| 325 | // We need to at least send keep-alive packets with some interval. |
| 326 | return last_send_time_ + kCongestedPacketInterval; |
| 327 | } |
| 328 |
| 329 | if (adjusted_media_rate_ > DataRate::Zero() && !packet_queue_.Empty()) { |
| 330 | // If packets are allowed to be sent in a burst, the |
| 331 | // debt is allowed to grow up to one packet more than what can be sent |
| 332 | // during 'send_burst_period_'. |
| 333 | TimeDelta drain_time = media_debt_ / adjusted_media_rate_; |
| 334 | next_send_time = |
| 335 | last_process_time_ + |
| 336 | ((send_burst_interval_ > drain_time) ? TimeDelta::Zero() : drain_time); |
| 337 | } else if (padding_rate_ > DataRate::Zero() && packet_queue_.Empty()) { |
| 338 | // If we _don't_ have pending packets, check how long until we have |
| 339 | // bandwidth for padding packets. Both media and padding debts must |
| 340 | // have been drained to do this. |
| 341 | RTC_DCHECK_GT(adjusted_media_rate_, DataRate::Zero()); |
| 342 | TimeDelta drain_time = std::max(media_debt_ / adjusted_media_rate_, |
| 343 | padding_debt_ / padding_rate_); |
| 344 |
| 345 | if (drain_time.IsZero() && |
| 346 | (!media_debt_.IsZero() || !padding_debt_.IsZero())) { |
| 347 | // We have a non-zero debt, but drain time is smaller than tick size of |
| 348 | // TimeDelta, round it up to the smallest possible non-zero delta. |
| 349 | drain_time = TimeDelta::Micros(1); |
| 350 | } |
| 351 | next_send_time = last_process_time_ + drain_time; |
| 352 | } else { |
| 353 | // Nothing to do. |
| 354 | next_send_time = last_process_time_ + kPausedProcessInterval; |
| 355 | } |
| 356 |
| 357 | if (send_padding_if_silent_) { |
| 358 | next_send_time = |
| 359 | std::min(next_send_time, last_send_time_ + kPausedProcessInterval); |
| 360 | } |
| 361 |
| 362 | return next_send_time; |
| 363 | } |
| 364 |
| 365 | void PacingController::ProcessPackets() { |
| 366 | const Timestamp now = CurrentTime(); |
| 367 | Timestamp target_send_time = now; |
| 368 |
| 369 | if (ShouldSendKeepalive(now)) { |
| 370 | DataSize keepalive_data_sent = DataSize::Zero(); |
| 371 | // We can not send padding unless a normal packet has first been sent. If |
| 372 | // we do, timestamps get messed up. |
| 373 | if (seen_first_packet_) { |
| 374 | std::vector<std::unique_ptr<RtpPacketToSend>> keepalive_packets = |
| 375 | packet_sender_->GeneratePadding(DataSize::Bytes(1)); |
| 376 | for (auto& packet : keepalive_packets) { |
| 377 | keepalive_data_sent += |
| 378 | DataSize::Bytes(packet->payload_size() + packet->padding_size()); |
| 379 | packet_sender_->SendPacket(std::move(packet), PacedPacketInfo()); |
| 380 | for (auto& packet : packet_sender_->FetchFec()) { |
| 381 | EnqueuePacket(std::move(packet)); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | OnPacketSent(RtpPacketMediaType::kPadding, keepalive_data_sent, now); |
| 386 | } |
| 387 |
| 388 | if (paused_) { |
| 389 | return; |
| 390 | } |
| 391 |
| 392 | TimeDelta early_execute_margin = |
| 393 | prober_.is_probing() ? kMaxEarlyProbeProcessing : TimeDelta::Zero(); |
| 394 |
| 395 | target_send_time = NextSendTime(); |
| 396 | if (now + early_execute_margin < target_send_time) { |
| 397 | // We are too early, but if queue is empty still allow draining some debt. |
| 398 | // Probing is allowed to be sent up to kMinSleepTime early. |
| 399 | UpdateBudgetWithElapsedTime(UpdateTimeAndGetElapsed(now)); |
| 400 | return; |
| 401 | } |
| 402 |
| 403 | TimeDelta elapsed_time = UpdateTimeAndGetElapsed(target_send_time); |
| 404 |
| 405 | if (elapsed_time > TimeDelta::Zero()) { |
| 406 | UpdateBudgetWithElapsedTime(elapsed_time); |
| 407 | } |
| 408 |
| 409 | PacedPacketInfo pacing_info; |
| 410 | DataSize recommended_probe_size = DataSize::Zero(); |
| 411 | bool is_probing = prober_.is_probing(); |
| 412 | if (is_probing) { |
| 413 | // Probe timing is sensitive, and handled explicitly by BitrateProber, so |
| 414 | // use actual send time rather than target. |
| 415 | pacing_info = prober_.CurrentCluster(now).value_or(PacedPacketInfo()); |
| 416 | if (pacing_info.probe_cluster_id != PacedPacketInfo::kNotAProbe) { |
| 417 | recommended_probe_size = prober_.RecommendedMinProbeSize(); |
| 418 | RTC_DCHECK_GT(recommended_probe_size, DataSize::Zero()); |
| 419 | } else { |
| 420 | // No valid probe cluster returned, probe might have timed out. |
| 421 | is_probing = false; |
| 422 | } |
| 423 | } |
| 424 |
| 425 | DataSize data_sent = DataSize::Zero(); |
| 426 | // Circuit breaker, making sure main loop isn't forever. |
| 427 | static constexpr int kMaxIterations = 1 << 16; |
| 428 | int iteration = 0; |
| 429 | int packets_sent = 0; |
| 430 | int padding_packets_generated = 0; |
| 431 | for (; iteration < kMaxIterations; ++iteration) { |
| 432 | // Fetch packet, so long as queue is not empty or budget is not |
| 433 | // exhausted. |
| 434 | std::unique_ptr<RtpPacketToSend> rtp_packet = |
| 435 | GetPendingPacket(pacing_info, target_send_time, now); |
| 436 | if (rtp_packet == nullptr) { |
| 437 | // No packet available to send, check if we should send padding. |
| 438 | DataSize padding_to_add = PaddingToAdd(recommended_probe_size, data_sent); |
| 439 | if (padding_to_add > DataSize::Zero()) { |
| 440 | std::vector<std::unique_ptr<RtpPacketToSend>> padding_packets = |
| 441 | packet_sender_->GeneratePadding(padding_to_add); |
| 442 | if (!padding_packets.empty()) { |
| 443 | padding_packets_generated += padding_packets.size(); |
| 444 | for (auto& packet : padding_packets) { |
| 445 | EnqueuePacket(std::move(packet)); |
| 446 | } |
| 447 | // Continue loop to send the padding that was just added. |
| 448 | continue; |
| 449 | } else { |
| 450 | // Can't generate padding, still update padding budget for next send |
| 451 | // time. |
| 452 | UpdatePaddingBudgetWithSentData(padding_to_add); |
| 453 | } |
| 454 | } |
| 455 | // Can't fetch new packet and no padding to send, exit send loop. |
| 456 | break; |
| 457 | } else { |
| 458 | RTC_DCHECK(rtp_packet); |
| 459 | RTC_DCHECK(rtp_packet->packet_type().has_value()); |
| 460 | const RtpPacketMediaType packet_type = *rtp_packet->packet_type(); |
| 461 | DataSize packet_size = DataSize::Bytes(rtp_packet->payload_size() + |
| 462 | rtp_packet->padding_size()); |
| 463 |
| 464 | if (include_overhead_) { |
| 465 | packet_size += DataSize::Bytes(rtp_packet->headers_size()) + |
| 466 | transport_overhead_per_packet_; |
| 467 | } |
| 468 |
| 469 | packet_sender_->SendPacket(std::move(rtp_packet), pacing_info); |
| 470 | for (auto& packet : packet_sender_->FetchFec()) { |
| 471 | EnqueuePacket(std::move(packet)); |
| 472 | } |
| 473 | data_sent += packet_size; |
| 474 | ++packets_sent; |
| 475 |
| 476 | // Send done, update send time. |
| 477 | OnPacketSent(packet_type, packet_size, now); |
| 478 |
| 479 | if (is_probing) { |
| 480 | pacing_info.probe_cluster_bytes_sent += packet_size.bytes(); |
| 481 | // If we are currently probing, we need to stop the send loop when we |
| 482 | // have reached the send target. |
| 483 | if (data_sent >= recommended_probe_size) { |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 |
| 488 | // Update target send time in case that are more packets that we are late |
| 489 | // in processing. |
| 490 | target_send_time = NextSendTime(); |
| 491 | if (target_send_time > now) { |
| 492 | // Exit loop if not probing. |
| 493 | if (!is_probing) { |
| 494 | break; |
| 495 | } |
| 496 | target_send_time = now; |
| 497 | } |
| 498 | UpdateBudgetWithElapsedTime(UpdateTimeAndGetElapsed(target_send_time)); |
| 499 | } |
| 500 | } |
| 501 |
| 502 | if (iteration >= kMaxIterations) { |
| 503 | // Circuit break activated. Log warning, adjust send time and return. |
| 504 | // TODO(sprang): Consider completely clearing state. |
| 505 | RTC_LOG(LS_ERROR) << "PacingController exceeded max iterations in " |
| 506 | "send-loop: packets sent = " |
| 507 | << packets_sent << ", padding packets generated = " |
| 508 | << padding_packets_generated |
| 509 | << ", bytes sent = " << data_sent.bytes(); |
| 510 | last_send_time_ = now; |
| 511 | last_process_time_ = now; |
| 512 | return; |
| 513 | } |
| 514 |
| 515 | if (is_probing) { |
| 516 | probing_send_failure_ = data_sent == DataSize::Zero(); |
| 517 | if (!probing_send_failure_) { |
| 518 | prober_.ProbeSent(CurrentTime(), data_sent); |
| 519 | } |
| 520 | } |
| 521 |
| 522 | // Queue length has probably decreased, check if pacing rate needs to updated. |
| 523 | // Poll the time again, since we might have enqueued new fec/padding packets |
| 524 | // with a later timestamp than `now`. |
| 525 | MaybeUpdateMediaRateDueToLongQueue(CurrentTime()); |
| 526 | } |
| 527 |
| 528 | DataSize PacingController::PaddingToAdd(DataSize recommended_probe_size, |
| 529 | DataSize data_sent) const { |
| 530 | if (!packet_queue_.Empty()) { |
| 531 | // Actual payload available, no need to add padding. |
| 532 | return DataSize::Zero(); |
| 533 | } |
| 534 |
| 535 | if (congested_) { |
| 536 | // Don't add padding if congested, even if requested for probing. |
| 537 | return DataSize::Zero(); |
| 538 | } |
| 539 |
| 540 | if (!seen_first_packet_) { |
| 541 | // We can not send padding unless a normal packet has first been sent. If |
| 542 | // we do, timestamps get messed up. |
| 543 | return DataSize::Zero(); |
| 544 | } |
| 545 |
| 546 | if (!recommended_probe_size.IsZero()) { |
| 547 | if (recommended_probe_size > data_sent) { |
| 548 | return recommended_probe_size - data_sent; |
| 549 | } |
| 550 | return DataSize::Zero(); |
| 551 | } |
| 552 |
| 553 | if (padding_rate_ > DataRate::Zero() && padding_debt_ == DataSize::Zero()) { |
| 554 | return kTargetPaddingDuration * padding_rate_; |
| 555 | } |
| 556 | return DataSize::Zero(); |
| 557 | } |
| 558 |
| 559 | std::unique_ptr<RtpPacketToSend> PacingController::GetPendingPacket( |
| 560 | const PacedPacketInfo& pacing_info, |
| 561 | Timestamp target_send_time, |
| 562 | Timestamp now) { |
| 563 | const bool is_probe = |
| 564 | pacing_info.probe_cluster_id != PacedPacketInfo::kNotAProbe; |
| 565 | // If first packet in probe, insert a small padding packet so we have a |
| 566 | // more reliable start window for the rate estimation. |
| 567 | if (is_probe && pacing_info.probe_cluster_bytes_sent == 0) { |
| 568 | auto padding = packet_sender_->GeneratePadding(DataSize::Bytes(1)); |
| 569 | // If no RTP modules sending media are registered, we may not get a |
| 570 | // padding packet back. |
| 571 | if (!padding.empty()) { |
| 572 | // We should never get more than one padding packets with a requested |
| 573 | // size of 1 byte. |
| 574 | RTC_DCHECK_EQ(padding.size(), 1u); |
| 575 | return std::move(padding[0]); |
| 576 | } |
| 577 | } |
| 578 |
| 579 | if (packet_queue_.Empty()) { |
| 580 | return nullptr; |
| 581 | } |
| 582 |
| 583 | // First, check if there is any reason _not_ to send the next queued packet. |
| 584 | // Unpaced packets and probes are exempted from send checks. |
| 585 | if (NextUnpacedSendTime().IsInfinite() && !is_probe) { |
| 586 | if (congested_) { |
| 587 | // Don't send anything if congested. |
| 588 | return nullptr; |
| 589 | } |
| 590 |
| 591 | if (now <= target_send_time && send_burst_interval_.IsZero()) { |
| 592 | // We allow sending slightly early if we think that we would actually |
| 593 | // had been able to, had we been right on time - i.e. the current debt |
| 594 | // is not more than would be reduced to zero at the target sent time. |
| 595 | // If we allow packets to be sent in a burst, packet are allowed to be |
| 596 | // sent early. |
| 597 | TimeDelta flush_time = media_debt_ / adjusted_media_rate_; |
| 598 | if (now + flush_time > target_send_time) { |
| 599 | return nullptr; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 |
| 604 | return packet_queue_.Pop(); |
| 605 | } |
| 606 |
| 607 | void PacingController::OnPacketSent(RtpPacketMediaType packet_type, |
| 608 | DataSize packet_size, |
| 609 | Timestamp send_time) { |
| 610 | if (!first_sent_packet_time_ && packet_type != RtpPacketMediaType::kPadding) { |
| 611 | first_sent_packet_time_ = send_time; |
| 612 | } |
| 613 |
| 614 | bool audio_packet = packet_type == RtpPacketMediaType::kAudio; |
| 615 | if ((!audio_packet || account_for_audio_) && packet_size > DataSize::Zero()) { |
| 616 | UpdateBudgetWithSentData(packet_size); |
| 617 | } |
| 618 |
| 619 | last_send_time_ = send_time; |
| 620 | } |
| 621 |
| 622 | void PacingController::UpdateBudgetWithElapsedTime(TimeDelta delta) { |
| 623 | media_debt_ -= std::min(media_debt_, adjusted_media_rate_ * delta); |
| 624 | padding_debt_ -= std::min(padding_debt_, padding_rate_ * delta); |
| 625 | } |
| 626 |
| 627 | void PacingController::UpdateBudgetWithSentData(DataSize size) { |
| 628 | media_debt_ += size; |
| 629 | media_debt_ = std::min(media_debt_, adjusted_media_rate_ * kMaxDebtInTime); |
| 630 | UpdatePaddingBudgetWithSentData(size); |
| 631 | } |
| 632 |
| 633 | void PacingController::UpdatePaddingBudgetWithSentData(DataSize size) { |
| 634 | padding_debt_ += size; |
| 635 | padding_debt_ = std::min(padding_debt_, padding_rate_ * kMaxDebtInTime); |
| 636 | } |
| 637 |
| 638 | void PacingController::SetQueueTimeLimit(TimeDelta limit) { |
| 639 | queue_time_limit_ = limit; |
| 640 | } |
| 641 |
| 642 | void PacingController::MaybeUpdateMediaRateDueToLongQueue(Timestamp now) { |
| 643 | adjusted_media_rate_ = pacing_rate_; |
| 644 | if (!drain_large_queues_) { |
| 645 | return; |
| 646 | } |
| 647 |
| 648 | DataSize queue_size_data = QueueSizeData(); |
| 649 | if (queue_size_data > DataSize::Zero()) { |
| 650 | // Assuming equal size packets and input/output rate, the average packet |
| 651 | // has avg_time_left_ms left to get queue_size_bytes out of the queue, if |
| 652 | // time constraint shall be met. Determine bitrate needed for that. |
| 653 | packet_queue_.UpdateAverageQueueTime(now); |
| 654 | TimeDelta avg_time_left = |
| 655 | std::max(TimeDelta::Millis(1), |
| 656 | queue_time_limit_ - packet_queue_.AverageQueueTime()); |
| 657 | DataRate min_rate_needed = queue_size_data / avg_time_left; |
| 658 | if (min_rate_needed > pacing_rate_) { |
| 659 | adjusted_media_rate_ = min_rate_needed; |
| 660 | RTC_LOG(LS_VERBOSE) << "bwe:large_pacing_queue pacing_rate_kbps=" |
| 661 | << pacing_rate_.kbps(); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 |
| 666 | Timestamp PacingController::NextUnpacedSendTime() const { |
| 667 | if (!pace_audio_) { |
| 668 | Timestamp leading_audio_send_time = |
| 669 | packet_queue_.LeadingPacketEnqueueTime(RtpPacketMediaType::kAudio); |
| 670 | if (leading_audio_send_time.IsFinite()) { |
| 671 | return leading_audio_send_time; |
| 672 | } |
| 673 | } |
| 674 | if (fast_retransmissions_) { |
| 675 | Timestamp leading_retransmission_send_time = |
| 676 | packet_queue_.LeadingPacketEnqueueTime( |
| 677 | RtpPacketMediaType::kRetransmission); |
| 678 | if (leading_retransmission_send_time.IsFinite()) { |
| 679 | return leading_retransmission_send_time; |
| 680 | } |
| 681 | } |
| 682 | return Timestamp::MinusInfinity(); |
| 683 | } |
| 684 |
| 685 | } // namespace webrtc |
| 686 |