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