blob: 9272d993280f0d673fc97942a28341f07e1b81fe [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"
Sebastian Jansson74c066c2018-10-15 14:31:24 +020017#include "rtc_base/logging.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "rtc_base/numerics/safe_conversions.h"
Sebastian Jansson74c066c2018-10-15 14:31:24 +020019#include "rtc_base/numerics/safe_minmax.h"
20#include "system_wrappers/include/field_trial.h"
21
22namespace webrtc {
23namespace {
24
Christoffer Rodbrob357e542018-11-23 11:19:32 +010025// By default, pacer emergency stops encoder when buffer reaches a high level.
26bool IsPacerEmergencyStopDisabled() {
27 return field_trial::IsEnabled("WebRTC-DisablePacerEmergencyStop");
28}
29
Sebastian Jansson74c066c2018-10-15 14:31:24 +020030} // namespace
Sebastian Jansson16180952018-12-12 16:49:10 +010031CongestionControlHandler::CongestionControlHandler()
Christoffer Rodbro412dc5f2019-04-09 11:38:52 +020032 : disable_pacer_emergency_stop_(IsPacerEmergencyStopDisabled()) {
Sebastian Jansson74c066c2018-10-15 14:31:24 +020033 sequenced_checker_.Detach();
34}
35
36CongestionControlHandler::~CongestionControlHandler() {}
37
Sebastian Jansson16180952018-12-12 16:49:10 +010038void CongestionControlHandler::SetTargetRate(
39 TargetTransferRate new_target_rate) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020040 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson16180952018-12-12 16:49:10 +010041 last_incoming_ = new_target_rate;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020042}
43
Sebastian Jansson16180952018-12-12 16:49:10 +010044void CongestionControlHandler::SetNetworkAvailability(bool network_available) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020045 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson16180952018-12-12 16:49:10 +010046 network_available_ = network_available;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020047}
48
Sebastian Jansson16180952018-12-12 16:49:10 +010049void CongestionControlHandler::SetPacerQueue(TimeDelta expected_queue_time) {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020050 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson74c066c2018-10-15 14:31:24 +020051 pacer_expected_queue_ms_ = expected_queue_time.ms();
Sebastian Jansson74c066c2018-10-15 14:31:24 +020052}
53
Sebastian Jansson16180952018-12-12 16:49:10 +010054absl::optional<TargetTransferRate> CongestionControlHandler::GetUpdate() {
Sebastian Janssonb55015e2019-04-09 13:44:04 +020055 RTC_DCHECK_RUN_ON(&sequenced_checker_);
Sebastian Jansson16180952018-12-12 16:49:10 +010056 if (!last_incoming_.has_value())
57 return absl::nullopt;
58 TargetTransferRate new_outgoing = *last_incoming_;
59 DataRate log_target_rate = new_outgoing.target_rate;
60 bool pause_encoding = false;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020061 if (!network_available_) {
Sebastian Jansson16180952018-12-12 16:49:10 +010062 pause_encoding = true;
Sebastian Jansson16180952018-12-12 16:49:10 +010063 } else if (!disable_pacer_emergency_stop_ &&
64 pacer_expected_queue_ms_ > PacedSender::kMaxQueueLengthMs) {
65 pause_encoding = true;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020066 }
Sebastian Jansson16180952018-12-12 16:49:10 +010067 if (pause_encoding)
68 new_outgoing.target_rate = DataRate::Zero();
69 if (!last_reported_ ||
70 last_reported_->target_rate != new_outgoing.target_rate ||
71 (!new_outgoing.target_rate.IsZero() &&
72 (last_reported_->network_estimate.loss_rate_ratio !=
73 new_outgoing.network_estimate.loss_rate_ratio ||
74 last_reported_->network_estimate.round_trip_time !=
75 new_outgoing.network_estimate.round_trip_time))) {
76 if (encoder_paused_in_last_report_ != pause_encoding)
77 RTC_LOG(LS_INFO) << "Bitrate estimate state changed, BWE: "
78 << ToString(log_target_rate) << ".";
79 encoder_paused_in_last_report_ = pause_encoding;
80 last_reported_ = new_outgoing;
81 return new_outgoing;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020082 }
Sebastian Jansson16180952018-12-12 16:49:10 +010083 return absl::nullopt;
Sebastian Jansson74c066c2018-10-15 14:31:24 +020084}
85
86} // namespace webrtc