control_handler.cc (2635B)
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 <optional> 14 15 #include "api/sequence_checker.h" 16 #include "api/transport/network_types.h" 17 #include "api/units/data_rate.h" 18 #include "api/units/time_delta.h" 19 #include "modules/pacing/pacing_controller.h" 20 #include "rtc_base/checks.h" 21 #include "rtc_base/logging.h" 22 23 namespace webrtc { 24 25 void CongestionControlHandler::SetTargetRate( 26 TargetTransferRate new_target_rate) { 27 RTC_DCHECK_RUN_ON(&sequenced_checker_); 28 RTC_CHECK(new_target_rate.at_time.IsFinite()); 29 last_incoming_ = new_target_rate; 30 } 31 32 void CongestionControlHandler::SetNetworkAvailability(bool network_available) { 33 RTC_DCHECK_RUN_ON(&sequenced_checker_); 34 network_available_ = network_available; 35 } 36 37 void CongestionControlHandler::SetPacerQueue(TimeDelta expected_queue_time) { 38 RTC_DCHECK_RUN_ON(&sequenced_checker_); 39 pacer_expected_queue_ms_ = expected_queue_time.ms(); 40 } 41 42 std::optional<TargetTransferRate> CongestionControlHandler::GetUpdate() { 43 RTC_DCHECK_RUN_ON(&sequenced_checker_); 44 if (!last_incoming_.has_value()) 45 return std::nullopt; 46 TargetTransferRate new_outgoing = *last_incoming_; 47 DataRate log_target_rate = new_outgoing.target_rate; 48 bool pause_encoding = false; 49 if (!network_available_) { 50 pause_encoding = true; 51 } else if (pacer_expected_queue_ms_ > 52 PacingController::kMaxExpectedQueueLength.ms()) { 53 pause_encoding = true; 54 } 55 if (pause_encoding) 56 new_outgoing.target_rate = DataRate::Zero(); 57 if (!last_reported_ || 58 last_reported_->target_rate != new_outgoing.target_rate || 59 (!new_outgoing.target_rate.IsZero() && 60 (last_reported_->network_estimate.loss_rate_ratio != 61 new_outgoing.network_estimate.loss_rate_ratio || 62 last_reported_->network_estimate.round_trip_time != 63 new_outgoing.network_estimate.round_trip_time))) { 64 if (encoder_paused_in_last_report_ != pause_encoding) 65 RTC_LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " 66 << ToString(log_target_rate) << "."; 67 encoder_paused_in_last_report_ = pause_encoding; 68 last_reported_ = new_outgoing; 69 return new_outgoing; 70 } 71 return std::nullopt; 72 } 73 74 } // namespace webrtc