OLD | NEW |
(Empty) | |
| 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 WEBRTC_MODULES_VIDEO_CODING_INCLUDE_BITRATE_ADJUSTER_H_ |
| 12 #define WEBRTC_MODULES_VIDEO_CODING_INCLUDE_BITRATE_ADJUSTER_H_ |
| 13 |
| 14 #include <functional> |
| 15 |
| 16 #include "webrtc/base/criticalsection.h" |
| 17 #include "webrtc/base/gtest_prod_util.h" |
| 18 #include "webrtc/base/rate_statistics.h" |
| 19 |
| 20 namespace webrtc { |
| 21 |
| 22 class Clock; |
| 23 |
| 24 // Certain hardware encoders tend to consistently overshoot the bitrate that |
| 25 // they are configured to encode at. This class estimates an adjusted bitrate |
| 26 // that when set on the encoder will produce the desired bitrate. |
| 27 class BitrateAdjuster { |
| 28 public: |
| 29 explicit BitrateAdjuster(Clock* clock); |
| 30 virtual ~BitrateAdjuster() {} |
| 31 |
| 32 static const uint32_t kBitrateUpdateIntervalMs; |
| 33 static const uint32_t kBitrateUpdateFrameInterval; |
| 34 static const float kBitrateTolerancePct; |
| 35 static const float kBytesPerMsToBitsPerSecond; |
| 36 |
| 37 // Sets the desired bitrate in bps (bits per second). |
| 38 // Should be called at least once before Update. |
| 39 void SetTargetBitrateBps(uint32_t bitrate_bps); |
| 40 uint32_t GetTargetBitrateBps() const; |
| 41 |
| 42 // Returns the adjusted bitrate in bps. |
| 43 uint32_t GetAdjustedBitrateBps() const; |
| 44 |
| 45 // Sets the minimum value of the output adjusted bitrate as a percentage of |
| 46 // the target bitrate. Default is 0.5 (50%). |
| 47 void SetMinAdjustedBitratePct(float target_bitrate_pct); |
| 48 // Sets the maximum value of the output adjusted bitrate as a percentage of |
| 49 // the target bitrate. Default is 1.0 (100%). |
| 50 void SetMaxAdjustedBitratePct(float target_bitrate_pct); |
| 51 |
| 52 // Returns what we think the current bitrate is. |
| 53 uint32_t GetEstimatedBitrateBps(); |
| 54 |
| 55 // This should be called after each frame is encoded. The timestamp at which |
| 56 // it is called is used to estimate the output bitrate of the encoder. |
| 57 // Should be called from only one thread. |
| 58 void Update(size_t frame_size); |
| 59 |
| 60 private: |
| 61 friend class BitrateAdjusterTest; |
| 62 FRIEND_TEST_ALL_PREFIXES(BitrateAdjusterTest, VaryingBitrates); |
| 63 |
| 64 // Returns true if the bitrate is within kBitrateTolerancePct of bitrate_bps. |
| 65 bool IsWithinTolerance(uint32_t bitrate_bps, uint32_t target_bitrate_bps); |
| 66 |
| 67 // Returns smallest possible adjusted value. |
| 68 uint32_t GetMinAdjustedBitrateBps() const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 69 // Returns largest possible adjusted value. |
| 70 uint32_t GetMaxAdjustedBitrateBps() const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 71 |
| 72 void Reset(); |
| 73 void UpdateBitrate(uint32_t current_time_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 74 |
| 75 rtc::CriticalSection crit_; |
| 76 Clock* const clock_; |
| 77 // The bitrate we want. |
| 78 volatile uint32_t target_bitrate_bps_ GUARDED_BY(crit_); |
| 79 // The bitrate we use to get what we want. |
| 80 volatile uint32_t adjusted_bitrate_bps_ GUARDED_BY(crit_); |
| 81 // The target bitrate that the adjusted bitrate was computed from. |
| 82 volatile uint32_t last_adjusted_target_bitrate_bps_ GUARDED_BY(crit_); |
| 83 volatile float min_adjusted_bitrate_pct_ GUARDED_BY(crit_); |
| 84 volatile float max_adjusted_bitrate_pct_ GUARDED_BY(crit_); |
| 85 // Used to estimate bitrate. |
| 86 RateStatistics bitrate_tracker_ GUARDED_BY(crit_); |
| 87 // The last time we tried to adjust the bitrate. |
| 88 uint32_t last_bitrate_update_time_ms_ GUARDED_BY(crit_); |
| 89 // The number of frames since the last time we tried to adjust the bitrate. |
| 90 uint32_t frames_since_last_update_ GUARDED_BY(crit_); |
| 91 }; |
| 92 |
| 93 } // namespace webrtc |
| 94 |
| 95 #endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_BITRATE_ADJUSTER_H_ |
OLD | NEW |