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 | |
| 25 | // When PacerPushbackExperiment is enabled, build-up in the pacer due to |
| 26 | // the congestion window and/or data spikes reduces encoder allocations. |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 27 | bool IsPacerPushbackExperimentEnabled() { |
Christoffer Rodbro | b357e54 | 2018-11-23 11:19:32 +0100 | [diff] [blame] | 28 | return field_trial::IsEnabled("WebRTC-PacerPushbackExperiment"); |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 29 | } |
Christoffer Rodbro | b357e54 | 2018-11-23 11:19:32 +0100 | [diff] [blame] | 30 | |
| 31 | // By default, pacer emergency stops encoder when buffer reaches a high level. |
| 32 | bool IsPacerEmergencyStopDisabled() { |
| 33 | return field_trial::IsEnabled("WebRTC-DisablePacerEmergencyStop"); |
| 34 | } |
| 35 | |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 36 | } // namespace |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 37 | CongestionControlHandler::CongestionControlHandler() |
| 38 | : pacer_pushback_experiment_(IsPacerPushbackExperimentEnabled()), |
Christoffer Rodbro | b357e54 | 2018-11-23 11:19:32 +0100 | [diff] [blame] | 39 | disable_pacer_emergency_stop_(IsPacerEmergencyStopDisabled()) { |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 40 | sequenced_checker_.Detach(); |
| 41 | } |
| 42 | |
| 43 | CongestionControlHandler::~CongestionControlHandler() {} |
| 44 | |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 45 | void CongestionControlHandler::SetTargetRate( |
| 46 | TargetTransferRate new_target_rate) { |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 47 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 48 | last_incoming_ = new_target_rate; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 51 | void CongestionControlHandler::SetNetworkAvailability(bool network_available) { |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 52 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 53 | network_available_ = network_available; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 56 | void CongestionControlHandler::SetPacerQueue(TimeDelta expected_queue_time) { |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 57 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 58 | pacer_expected_queue_ms_ = expected_queue_time.ms(); |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 61 | absl::optional<TargetTransferRate> CongestionControlHandler::GetUpdate() { |
| 62 | RTC_DCHECK_CALLED_SEQUENTIALLY(&sequenced_checker_); |
| 63 | if (!last_incoming_.has_value()) |
| 64 | return absl::nullopt; |
| 65 | TargetTransferRate new_outgoing = *last_incoming_; |
| 66 | DataRate log_target_rate = new_outgoing.target_rate; |
| 67 | bool pause_encoding = false; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 68 | if (!network_available_) { |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 69 | pause_encoding = true; |
Christoffer Rodbro | b357e54 | 2018-11-23 11:19:32 +0100 | [diff] [blame] | 70 | } else if (pacer_pushback_experiment_) { |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 71 | const int64_t queue_length_ms = pacer_expected_queue_ms_; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 72 | if (queue_length_ms == 0) { |
| 73 | encoding_rate_ratio_ = 1.0; |
| 74 | } else if (queue_length_ms > 50) { |
| 75 | double encoding_ratio = 1.0 - queue_length_ms / 1000.0; |
| 76 | encoding_rate_ratio_ = std::min(encoding_rate_ratio_, encoding_ratio); |
| 77 | encoding_rate_ratio_ = std::max(encoding_rate_ratio_, 0.0); |
| 78 | } |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 79 | new_outgoing.target_rate = new_outgoing.target_rate * encoding_rate_ratio_; |
| 80 | log_target_rate = new_outgoing.target_rate; |
| 81 | if (new_outgoing.target_rate < DataRate::kbps(50)) |
| 82 | pause_encoding = true; |
| 83 | } else if (!disable_pacer_emergency_stop_ && |
| 84 | pacer_expected_queue_ms_ > PacedSender::kMaxQueueLengthMs) { |
| 85 | pause_encoding = true; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 86 | } |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 87 | if (pause_encoding) |
| 88 | new_outgoing.target_rate = DataRate::Zero(); |
| 89 | if (!last_reported_ || |
| 90 | last_reported_->target_rate != new_outgoing.target_rate || |
| 91 | (!new_outgoing.target_rate.IsZero() && |
| 92 | (last_reported_->network_estimate.loss_rate_ratio != |
| 93 | new_outgoing.network_estimate.loss_rate_ratio || |
| 94 | last_reported_->network_estimate.round_trip_time != |
| 95 | new_outgoing.network_estimate.round_trip_time))) { |
| 96 | if (encoder_paused_in_last_report_ != pause_encoding) |
| 97 | RTC_LOG(LS_INFO) << "Bitrate estimate state changed, BWE: " |
| 98 | << ToString(log_target_rate) << "."; |
| 99 | encoder_paused_in_last_report_ = pause_encoding; |
| 100 | last_reported_ = new_outgoing; |
| 101 | return new_outgoing; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 102 | } |
Sebastian Jansson | 1618095 | 2018-12-12 16:49:10 +0100 | [diff] [blame] | 103 | return absl::nullopt; |
Sebastian Jansson | 74c066c | 2018-10-15 14:31:24 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace webrtc |