blob: ffa373aebad1c605dfbeda3f64014a9971147fa8 [file] [log] [blame]
Sebastian Jansson74c066c2018-10-15 14:31:24 +02001/*
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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <algorithm>
14#include <vector>
15
16#include "api/units/data_rate.h"
Erik Språngf3f3a612022-05-13 15:55:29 +020017#include "modules/pacing/pacing_controller.h"
Sebastian Jansson74c066c2018-10-15 14:31:24 +020018#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/numerics/safe_conversions.h"
Sebastian Jansson74c066c2018-10-15 14:31:24 +020020#include "rtc_base/numerics/safe_minmax.h"
21#include "system_wrappers/include/field_trial.h"
22
23namespace webrtc {
24namespace {
25
Christoffer Rodbrob357e542018-11-23 11:19:32 +010026// By default, pacer emergency stops encoder when buffer reaches a high level.
27bool IsPacerEmergencyStopDisabled() {
28 return field_trial::IsEnabled("WebRTC-DisablePacerEmergencyStop");
29}
30
Sebastian Jansson74c066c2018-10-15 14:31:24 +020031} // namespace
Sebastian Jansson16180952018-12-12 16:49:10 +010032CongestionControlHandler::CongestionControlHandler()
Christoffer Rodbro412dc5f2019-04-09 11:38:52 +020033 : disable_pacer_emergency_stop_(IsPacerEmergencyStopDisabled()) {
Sebastian Jansson74c066c2018-10-15 14:31:24 +020034 sequenced_checker_.Detach();
35}
36
37CongestionControlHandler::~CongestionControlHandler() {}
38
Sebastian Jansson16180952018-12-12 16:49:10 +010039void CongestionControlHandler::SetTargetRate(
40 TargetTransferRate new_target_rate) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020041 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson40de3cc2019-09-19 14:54:43 +020042 RTC_CHECK(new_target_rate.at_time.IsFinite());
Sebastian Jansson16180952018-12-12 16:49:10 +010043 last_incoming_ = new_target_rate;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020044}
45
Sebastian Jansson16180952018-12-12 16:49:10 +010046void CongestionControlHandler::SetNetworkAvailability(bool network_available) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020047 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson16180952018-12-12 16:49:10 +010048 network_available_ = network_available;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020049}
50
Sebastian Jansson16180952018-12-12 16:49:10 +010051void CongestionControlHandler::SetPacerQueue(TimeDelta expected_queue_time) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020052 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson74c066c2018-10-15 14:31:24 +020053 pacer_expected_queue_ms_ = expected_queue_time.ms();
Sebastian Jansson74c066c2018-10-15 14:31:24 +020054}
55
Sebastian Jansson16180952018-12-12 16:49:10 +010056absl::optional<TargetTransferRate> CongestionControlHandler::GetUpdate() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020057 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson16180952018-12-12 16:49:10 +010058 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;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020063 if (!network_available_) {
Sebastian Jansson16180952018-12-12 16:49:10 +010064 pause_encoding = true;
Sebastian Jansson16180952018-12-12 16:49:10 +010065 } else if (!disable_pacer_emergency_stop_ &&
Erik Språngf3f3a612022-05-13 15:55:29 +020066 pacer_expected_queue_ms_ >
67 PacingController::kMaxExpectedQueueLength.ms()) {
Sebastian Jansson16180952018-12-12 16:49:10 +010068 pause_encoding = true;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020069 }
Sebastian Jansson16180952018-12-12 16:49:10 +010070 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;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020085 }
Sebastian Jansson16180952018-12-12 16:49:10 +010086 return absl::nullopt;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020087}
88
89} // namespace webrtc