1 /* 2 * Copyright (c) 2018 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/congestion_controller/rtp/control_handler.h" 12 13 #include <algorithm> 14 #include <vector> 15 16 #include "api/units/data_rate.h" 17 #include "modules/pacing/pacing_controller.h" 18 #include "rtc_base/logging.h" 19 #include "rtc_base/numerics/safe_conversions.h" 20 #include "rtc_base/numerics/safe_minmax.h" 21 #include "system_wrappers/include/field_trial.h" 22 23 namespace webrtc { 24 namespace { 25 26 // By default, pacer emergency stops encoder when buffer reaches a high level. IsPacerEmergencyStopDisabled()27bool IsPacerEmergencyStopDisabled() { 28 return field_trial::IsEnabled("WebRTC-DisablePacerEmergencyStop"); 29 } 30 31 } // namespace CongestionControlHandler()32CongestionControlHandler::CongestionControlHandler() 33 : disable_pacer_emergency_stop_(IsPacerEmergencyStopDisabled()) { 34 sequenced_checker_.Detach(); 35 } 36 ~CongestionControlHandler()37CongestionControlHandler::~CongestionControlHandler() {} 38 SetTargetRate(TargetTransferRate new_target_rate)39void CongestionControlHandler::SetTargetRate( 40 TargetTransferRate new_target_rate) { 41 RTC_DCHECK_RUN_ON(&sequenced_checker_); 42 RTC_CHECK(new_target_rate.at_time.IsFinite()); 43 last_incoming_ = new_target_rate; 44 } 45 SetNetworkAvailability(bool network_available)46void CongestionControlHandler::SetNetworkAvailability(bool network_available) { 47 RTC_DCHECK_RUN_ON(&sequenced_checker_); 48 network_available_ = network_available; 49 } 50 SetPacerQueue(TimeDelta expected_queue_time)51void CongestionControlHandler::SetPacerQueue(TimeDelta expected_queue_time) { 52 RTC_DCHECK_RUN_ON(&sequenced_checker_); 53 pacer_expected_queue_ms_ = expected_queue_time.ms(); 54 } 55 GetUpdate()56absl::optional<TargetTransferRate> CongestionControlHandler::GetUpdate() { 57 RTC_DCHECK_RUN_ON(&sequenced_checker_); 58 if (!last_incoming_.has_value()) 59 return absl::nullopt; 60 TargetTransferRate new_outgoing = *last_incoming_; 61 DataRate log_target_rate = new_outgoing.target_rate; 62 bool pause_encoding = false; 63 if (!network_available_) { 64 pause_encoding = true; 65 } else if (!disable_pacer_emergency_stop_ && 66 pacer_expected_queue_ms_ > 67 PacingController::kMaxExpectedQueueLength.ms()) { 68 pause_encoding = true; 69 } 70 if (pause_encoding) 71 new_outgoing.target_rate = DataRate::Zero(); 72 if (!last_reported_ || 73 last_reported_->target_rate != new_outgoing.target_rate || 74 (!new_outgoing.target_rate.IsZero() && 75 (last_reported_->network_estimate.loss_rate_ratio != 76 new_outgoing.network_estimate.loss_rate_ratio || 77 last_reported_->network_estimate.round_trip_time != 78 new_outgoing.network_estimate.round_trip_time))) { 79 if (encoder_paused_in_last_report_ != pause_encoding) 80 RTC_LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " 81 << ToString(log_target_rate) << "."; 82 encoder_paused_in_last_report_ = pause_encoding; 83 last_reported_ = new_outgoing; 84 return new_outgoing; 85 } 86 return absl::nullopt; 87 } 88 89 } // namespace webrtc 90