bitrate_adjuster.h (3557B)
1 /* 2 * Copyright 2016 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 COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_ 12 #define COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <optional> 18 19 #include "rtc_base/rate_statistics.h" 20 #include "rtc_base/synchronization/mutex.h" 21 #include "rtc_base/system/rtc_export.h" 22 #include "rtc_base/thread_annotations.h" 23 24 namespace webrtc { 25 26 // Certain hardware encoders tend to consistently overshoot the bitrate that 27 // they are configured to encode at. This class estimates an adjusted bitrate 28 // that when set on the encoder will produce the desired bitrate. 29 class RTC_EXPORT BitrateAdjuster { 30 public: 31 // min_adjusted_bitrate_pct and max_adjusted_bitrate_pct are the lower and 32 // upper bound outputted adjusted bitrates as a percentage of the target 33 // bitrate. 34 BitrateAdjuster(float min_adjusted_bitrate_pct, 35 float max_adjusted_bitrate_pct); 36 virtual ~BitrateAdjuster() {} 37 38 static const uint32_t kBitrateUpdateIntervalMs; 39 static const uint32_t kBitrateUpdateFrameInterval; 40 static const float kBitrateTolerancePct; 41 static const float kBytesPerMsToBitsPerSecond; 42 43 // Sets the desired bitrate in bps (bits per second). 44 // Should be called at least once before Update. 45 void SetTargetBitrateBps(uint32_t bitrate_bps); 46 uint32_t GetTargetBitrateBps() const; 47 48 // Returns the adjusted bitrate in bps. 49 uint32_t GetAdjustedBitrateBps() const; 50 51 // Returns what we think the current bitrate is. 52 std::optional<uint32_t> GetEstimatedBitrateBps(); 53 54 // This should be called after each frame is encoded. The timestamp at which 55 // it is called is used to estimate the output bitrate of the encoder. 56 // Should be called from only one thread. 57 void Update(size_t frame_size); 58 59 private: 60 // Returns true if the bitrate is within kBitrateTolerancePct of bitrate_bps. 61 bool IsWithinTolerance(uint32_t bitrate_bps, uint32_t target_bitrate_bps); 62 63 // Returns smallest possible adjusted value. 64 uint32_t GetMinAdjustedBitrateBps() const 65 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 66 // Returns largest possible adjusted value. 67 uint32_t GetMaxAdjustedBitrateBps() const 68 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 69 70 void Reset(); 71 void UpdateBitrate(uint32_t current_time_ms) 72 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 73 74 mutable Mutex mutex_; 75 const float min_adjusted_bitrate_pct_; 76 const float max_adjusted_bitrate_pct_; 77 // The bitrate we want. 78 volatile uint32_t target_bitrate_bps_ RTC_GUARDED_BY(mutex_); 79 // The bitrate we use to get what we want. 80 volatile uint32_t adjusted_bitrate_bps_ RTC_GUARDED_BY(mutex_); 81 // The target bitrate that the adjusted bitrate was computed from. 82 volatile uint32_t last_adjusted_target_bitrate_bps_ RTC_GUARDED_BY(mutex_); 83 // Used to estimate bitrate. 84 RateStatistics bitrate_tracker_ RTC_GUARDED_BY(mutex_); 85 // The last time we tried to adjust the bitrate. 86 uint32_t last_bitrate_update_time_ms_ RTC_GUARDED_BY(mutex_); 87 // The number of frames since the last time we tried to adjust the bitrate. 88 uint32_t frames_since_last_update_ RTC_GUARDED_BY(mutex_); 89 }; 90 91 } // namespace webrtc 92 93 #endif // COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_