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 #ifndef MODULES_VIDEO_CODING_UTILITY_BANDWIDTH_QUALITY_SCALER_H_ 12 #define MODULES_VIDEO_CODING_UTILITY_BANDWIDTH_QUALITY_SCALER_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <memory> 18 #include <vector> 19 20 #include "absl/types/optional.h" 21 #include "api/scoped_refptr.h" 22 #include "api/sequence_checker.h" 23 #include "api/video_codecs/video_encoder.h" 24 #include "rtc_base/experiments/encoder_info_settings.h" 25 #include "rtc_base/logging.h" 26 #include "rtc_base/numerics/exp_filter.h" 27 #include "rtc_base/rate_statistics.h" 28 #include "rtc_base/ref_count.h" 29 #include "rtc_base/system/no_unique_address.h" 30 #include "rtc_base/weak_ptr.h" 31 32 namespace webrtc { 33 34 class BandwidthQualityScalerUsageHandlerInterface { 35 public: 36 virtual ~BandwidthQualityScalerUsageHandlerInterface(); 37 38 virtual void OnReportUsageBandwidthHigh() = 0; 39 virtual void OnReportUsageBandwidthLow() = 0; 40 }; 41 42 // BandwidthQualityScaler runs asynchronously and monitors bandwidth values of 43 // encoded frames. It holds a reference to a 44 // BandwidthQualityScalerUsageHandlerInterface implementation to signal an 45 // overuse or underuse of bandwidth (which indicate a desire to scale the video 46 // stream down or up). 47 class BandwidthQualityScaler { 48 public: 49 explicit BandwidthQualityScaler( 50 BandwidthQualityScalerUsageHandlerInterface* handler); 51 virtual ~BandwidthQualityScaler(); 52 53 void ReportEncodeInfo(int frame_size_bytes, 54 int64_t time_sent_in_ms, 55 uint32_t encoded_width, 56 uint32_t encoded_height); 57 58 // We prioritise to using the |resolution_bitrate_limits| provided by the 59 // current decoder. If not provided, we will use the default data by 60 // GetDefaultResolutionBitrateLimits(). 61 void SetResolutionBitrateLimits( 62 const std::vector<VideoEncoder::ResolutionBitrateLimits>& 63 resolution_bitrate_limits); 64 65 const TimeDelta kBitrateStateUpdateInterval; 66 67 private: 68 enum class CheckBitrateResult { 69 kInsufficientSamples, 70 kNormalBitrate, 71 kHighBitRate, 72 kLowBitRate, 73 }; 74 75 // We will periodically check encode bitrate, this function will make 76 // resolution up or down decisions and report the decision to the adapter. 77 void StartCheckForBitrate(); 78 CheckBitrateResult CheckBitrate(); 79 80 RTC_NO_UNIQUE_ADDRESS SequenceChecker task_checker_; 81 BandwidthQualityScalerUsageHandlerInterface* const handler_ 82 RTC_GUARDED_BY(&task_checker_); 83 84 absl::optional<int64_t> last_time_sent_in_ms_ RTC_GUARDED_BY(&task_checker_); 85 RateStatistics encoded_bitrate_ RTC_GUARDED_BY(&task_checker_); 86 absl::optional<int> last_frame_size_pixels_ RTC_GUARDED_BY(&task_checker_); 87 rtc::WeakPtrFactory<BandwidthQualityScaler> weak_ptr_factory_; 88 89 std::vector<VideoEncoder::ResolutionBitrateLimits> resolution_bitrate_limits_; 90 }; 91 92 } // namespace webrtc 93 #endif // MODULES_VIDEO_CODING_UTILITY_BANDWIDTH_QUALITY_SCALER_H_ 94