tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_ |
| 12 | #define COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_ |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 16 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 17 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/criticalsection.h" |
| 19 | #include "rtc_base/rate_statistics.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame^] | 20 | #include "rtc_base/thread_annotations.h" |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 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 | // min_adjusted_bitrate_pct and max_adjusted_bitrate_pct are the lower and |
| 30 | // upper bound outputted adjusted bitrates as a percentage of the target |
| 31 | // bitrate. |
Niels Möller | 2cb7b5e | 2018-04-19 10:02:26 +0200 | [diff] [blame] | 32 | BitrateAdjuster(float min_adjusted_bitrate_pct, |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 33 | float max_adjusted_bitrate_pct); |
| 34 | virtual ~BitrateAdjuster() {} |
| 35 | |
| 36 | static const uint32_t kBitrateUpdateIntervalMs; |
| 37 | static const uint32_t kBitrateUpdateFrameInterval; |
| 38 | static const float kBitrateTolerancePct; |
| 39 | static const float kBytesPerMsToBitsPerSecond; |
| 40 | |
| 41 | // Sets the desired bitrate in bps (bits per second). |
| 42 | // Should be called at least once before Update. |
| 43 | void SetTargetBitrateBps(uint32_t bitrate_bps); |
| 44 | uint32_t GetTargetBitrateBps() const; |
| 45 | |
| 46 | // Returns the adjusted bitrate in bps. |
| 47 | uint32_t GetAdjustedBitrateBps() const; |
| 48 | |
| 49 | // Returns what we think the current bitrate is. |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 50 | absl::optional<uint32_t> GetEstimatedBitrateBps(); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 51 | |
| 52 | // This should be called after each frame is encoded. The timestamp at which |
| 53 | // it is called is used to estimate the output bitrate of the encoder. |
| 54 | // Should be called from only one thread. |
| 55 | void Update(size_t frame_size); |
| 56 | |
| 57 | private: |
| 58 | // Returns true if the bitrate is within kBitrateTolerancePct of bitrate_bps. |
| 59 | bool IsWithinTolerance(uint32_t bitrate_bps, uint32_t target_bitrate_bps); |
| 60 | |
| 61 | // Returns smallest possible adjusted value. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 62 | uint32_t GetMinAdjustedBitrateBps() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 63 | // Returns largest possible adjusted value. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 64 | uint32_t GetMaxAdjustedBitrateBps() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 65 | |
| 66 | void Reset(); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 67 | void UpdateBitrate(uint32_t current_time_ms) |
| 68 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 69 | |
| 70 | rtc::CriticalSection crit_; |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 71 | const float min_adjusted_bitrate_pct_; |
| 72 | const float max_adjusted_bitrate_pct_; |
| 73 | // The bitrate we want. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 74 | volatile uint32_t target_bitrate_bps_ RTC_GUARDED_BY(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 75 | // The bitrate we use to get what we want. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 76 | volatile uint32_t adjusted_bitrate_bps_ RTC_GUARDED_BY(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 77 | // The target bitrate that the adjusted bitrate was computed from. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 78 | volatile uint32_t last_adjusted_target_bitrate_bps_ RTC_GUARDED_BY(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 79 | // Used to estimate bitrate. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 80 | RateStatistics bitrate_tracker_ RTC_GUARDED_BY(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 81 | // The last time we tried to adjust the bitrate. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 82 | uint32_t last_bitrate_update_time_ms_ RTC_GUARDED_BY(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 83 | // The number of frames since the last time we tried to adjust the bitrate. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 84 | uint32_t frames_since_last_update_ RTC_GUARDED_BY(crit_); |
tkchin | f75d008 | 2016-02-23 22:49:42 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace webrtc |
| 88 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 89 | #endif // COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_ |