xref: /aosp_15_r20/external/webrtc/video/adaptation/bandwidth_quality_scaler_resource.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 "video/adaptation/bandwidth_quality_scaler_resource.h"
12 
13 #include <utility>
14 
15 #include "rtc_base/checks.h"
16 #include "rtc_base/experiments/balanced_degradation_settings.h"
17 #include "rtc_base/logging.h"
18 #include "rtc_base/time_utils.h"
19 
20 namespace webrtc {
21 
22 // static
23 rtc::scoped_refptr<BandwidthQualityScalerResource>
Create()24 BandwidthQualityScalerResource::Create() {
25   return rtc::make_ref_counted<BandwidthQualityScalerResource>();
26 }
27 
BandwidthQualityScalerResource()28 BandwidthQualityScalerResource::BandwidthQualityScalerResource()
29     : VideoStreamEncoderResource("BandwidthQualityScalerResource"),
30       bandwidth_quality_scaler_(nullptr) {}
31 
~BandwidthQualityScalerResource()32 BandwidthQualityScalerResource::~BandwidthQualityScalerResource() {
33   RTC_DCHECK(!bandwidth_quality_scaler_);
34 }
35 
is_started() const36 bool BandwidthQualityScalerResource::is_started() const {
37   RTC_DCHECK_RUN_ON(encoder_queue());
38   return bandwidth_quality_scaler_.get();
39 }
40 
StartCheckForOveruse(const std::vector<VideoEncoder::ResolutionBitrateLimits> & resolution_bitrate_limits)41 void BandwidthQualityScalerResource::StartCheckForOveruse(
42     const std::vector<VideoEncoder::ResolutionBitrateLimits>&
43         resolution_bitrate_limits) {
44   RTC_DCHECK_RUN_ON(encoder_queue());
45   RTC_DCHECK(!is_started());
46   bandwidth_quality_scaler_ = std::make_unique<BandwidthQualityScaler>(this);
47 
48   // If the configuration parameters more than one, we should define and
49   // declare the function BandwidthQualityScaler::Initialize() and call it.
50   bandwidth_quality_scaler_->SetResolutionBitrateLimits(
51       resolution_bitrate_limits);
52 }
53 
StopCheckForOveruse()54 void BandwidthQualityScalerResource::StopCheckForOveruse() {
55   RTC_DCHECK_RUN_ON(encoder_queue());
56   RTC_DCHECK(is_started());
57   // Ensure we have no pending callbacks. This makes it safe to destroy the
58   // BandwidthQualityScaler and even task queues with tasks in-flight.
59   bandwidth_quality_scaler_.reset();
60 }
61 
OnReportUsageBandwidthHigh()62 void BandwidthQualityScalerResource::OnReportUsageBandwidthHigh() {
63   OnResourceUsageStateMeasured(ResourceUsageState::kOveruse);
64 }
65 
OnReportUsageBandwidthLow()66 void BandwidthQualityScalerResource::OnReportUsageBandwidthLow() {
67   OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse);
68 }
69 
OnEncodeCompleted(const EncodedImage & encoded_image,int64_t time_sent_in_us,int64_t encoded_image_size_bytes)70 void BandwidthQualityScalerResource::OnEncodeCompleted(
71     const EncodedImage& encoded_image,
72     int64_t time_sent_in_us,
73     int64_t encoded_image_size_bytes) {
74   RTC_DCHECK_RUN_ON(encoder_queue());
75 
76   if (bandwidth_quality_scaler_) {
77     bandwidth_quality_scaler_->ReportEncodeInfo(
78         encoded_image_size_bytes, time_sent_in_us / 1000,
79         encoded_image._encodedWidth, encoded_image._encodedHeight);
80   }
81 }
82 
83 }  // namespace webrtc
84