1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/video_coding/utility/bandwidth_quality_scaler.h"
#include <cstdint>
#include <optional>
#include <vector>
#include "api/sequence_checker.h"
#include "api/task_queue/task_queue_base.h"
#include "api/video/video_codec_type.h"
#include "api/video_codecs/video_encoder.h"
#include "rtc_base/checks.h"
#include "rtc_base/experiments/encoder_info_settings.h"
#include "rtc_base/rate_statistics.h"
#include "rtc_base/weak_ptr.h"
namespace webrtc {
namespace {
constexpr int kDefaultMaxWindowSizeMs = 5000;
constexpr float kHigherMaxBitrateTolerationFactor = 0.95;
constexpr float kLowerMinBitrateTolerationFactor = 0.8;
} // namespace
BandwidthQualityScaler::BandwidthQualityScaler(
BandwidthQualityScalerUsageHandlerInterface* handler)
: handler_(handler),
encoded_bitrate_(kDefaultMaxWindowSizeMs, RateStatistics::kBpsScale),
weak_ptr_factory_(this) {
RTC_DCHECK_RUN_ON(&task_checker_);
RTC_DCHECK(handler_ != nullptr);
StartCheckForBitrate();
}
BandwidthQualityScaler::~BandwidthQualityScaler() {
RTC_DCHECK_RUN_ON(&task_checker_);
}
void BandwidthQualityScaler::StartCheckForBitrate() {
RTC_DCHECK_RUN_ON(&task_checker_);
TaskQueueBase::Current()->PostDelayedTask(
[this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), this] {
if (!this_weak_ptr) {
// The caller BandwidthQualityScaler has been deleted.
return;
}
RTC_DCHECK_RUN_ON(&task_checker_);
switch (CheckBitrate()) {
case BandwidthQualityScaler::CheckBitrateResult::kHighBitRate: {
handler_->OnReportUsageBandwidthHigh();
last_frame_size_pixels_.reset();
break;
}
case BandwidthQualityScaler::CheckBitrateResult::kLowBitRate: {
handler_->OnReportUsageBandwidthLow();
last_frame_size_pixels_.reset();
break;
}
case BandwidthQualityScaler::CheckBitrateResult::kNormalBitrate: {
break;
}
case BandwidthQualityScaler::CheckBitrateResult::
kInsufficientSamples: {
break;
}
}
StartCheckForBitrate();
},
kBitrateStateUpdateInterval);
}
void BandwidthQualityScaler::ReportEncodeInfo(int frame_size_bytes,
int64_t time_sent_in_ms,
uint32_t encoded_width,
uint32_t encoded_height) {
RTC_DCHECK_RUN_ON(&task_checker_);
last_time_sent_in_ms_ = time_sent_in_ms;
last_frame_size_pixels_ = encoded_width * encoded_height;
encoded_bitrate_.Update(frame_size_bytes, time_sent_in_ms);
}
void BandwidthQualityScaler::SetResolutionBitrateLimits(
const std::vector<VideoEncoder::ResolutionBitrateLimits>&
resolution_bitrate_limits,
VideoCodecType codec_type) {
if (resolution_bitrate_limits.empty()) {
resolution_bitrate_limits_ =
EncoderInfoSettings::GetDefaultSinglecastBitrateLimitsWhenQpIsUntrusted(
codec_type);
} else {
resolution_bitrate_limits_ = resolution_bitrate_limits;
}
}
BandwidthQualityScaler::CheckBitrateResult
BandwidthQualityScaler::CheckBitrate() {
RTC_DCHECK_RUN_ON(&task_checker_);
if (!last_frame_size_pixels_.has_value() ||
!last_time_sent_in_ms_.has_value()) {
return BandwidthQualityScaler::CheckBitrateResult::kInsufficientSamples;
}
std::optional<int64_t> current_bitrate_bps =
encoded_bitrate_.Rate(last_time_sent_in_ms_.value());
if (!current_bitrate_bps.has_value()) {
// We can't get a valid bitrate due to not enough data points.
return BandwidthQualityScaler::CheckBitrateResult::kInsufficientSamples;
}
std::optional<VideoEncoder::ResolutionBitrateLimits> suitable_bitrate_limit =
EncoderInfoSettings::
GetSinglecastBitrateLimitForResolutionWhenQpIsUntrusted(
last_frame_size_pixels_, resolution_bitrate_limits_);
if (!suitable_bitrate_limit.has_value()) {
return BandwidthQualityScaler::CheckBitrateResult::kInsufficientSamples;
}
// Multiply by toleration factor to solve the frequent adaptation due to
// critical value.
if (current_bitrate_bps > suitable_bitrate_limit->max_bitrate_bps *
kHigherMaxBitrateTolerationFactor) {
return BandwidthQualityScaler::CheckBitrateResult::kLowBitRate;
} else if (current_bitrate_bps <
suitable_bitrate_limit->min_start_bitrate_bps *
kLowerMinBitrateTolerationFactor) {
return BandwidthQualityScaler::CheckBitrateResult::kHighBitRate;
}
return BandwidthQualityScaler::CheckBitrateResult::kNormalBitrate;
}
BandwidthQualityScalerUsageHandlerInterface::
~BandwidthQualityScalerUsageHandlerInterface() {}
} // namespace webrtc
|