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 | |
| 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 | // 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. |
| 32 | BitrateAdjuster(Clock* clock, |
| 33 | float min_adjusted_bitrate_pct, |
| 34 | float max_adjusted_bitrate_pct); |
| 35 | virtual ~BitrateAdjuster() {} |
| 36 | |
| 37 | static const uint32_t kBitrateUpdateIntervalMs; |
| 38 | static const uint32_t kBitrateUpdateFrameInterval; |
| 39 | static const float kBitrateTolerancePct; |
| 40 | static const float kBytesPerMsToBitsPerSecond; |
| 41 | |
| 42 | // Sets the desired bitrate in bps (bits per second). |
| 43 | // Should be called at least once before Update. |
| 44 | void SetTargetBitrateBps(uint32_t bitrate_bps); |
| 45 | uint32_t GetTargetBitrateBps() const; |
| 46 | |
| 47 | // Returns the adjusted bitrate in bps. |
| 48 | uint32_t GetAdjustedBitrateBps() const; |
| 49 | |
| 50 | // Returns what we think the current bitrate is. |
| 51 | uint32_t GetEstimatedBitrateBps(); |
| 52 | |
| 53 | // This should be called after each frame is encoded. The timestamp at which |
| 54 | // it is called is used to estimate the output bitrate of the encoder. |
| 55 | // Should be called from only one thread. |
| 56 | void Update(size_t frame_size); |
| 57 | |
| 58 | private: |
| 59 | // Returns true if the bitrate is within kBitrateTolerancePct of bitrate_bps. |
| 60 | bool IsWithinTolerance(uint32_t bitrate_bps, uint32_t target_bitrate_bps); |
| 61 | |
| 62 | // Returns smallest possible adjusted value. |
| 63 | uint32_t GetMinAdjustedBitrateBps() const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 64 | // Returns largest possible adjusted value. |
| 65 | uint32_t GetMaxAdjustedBitrateBps() const EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 66 | |
| 67 | void Reset(); |
| 68 | void UpdateBitrate(uint32_t current_time_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_); |
| 69 | |
| 70 | rtc::CriticalSection crit_; |
| 71 | Clock* const clock_; |
| 72 | const float min_adjusted_bitrate_pct_; |
| 73 | const float max_adjusted_bitrate_pct_; |
| 74 | // The bitrate we want. |
| 75 | volatile uint32_t target_bitrate_bps_ GUARDED_BY(crit_); |
| 76 | // The bitrate we use to get what we want. |
| 77 | volatile uint32_t adjusted_bitrate_bps_ GUARDED_BY(crit_); |
| 78 | // The target bitrate that the adjusted bitrate was computed from. |
| 79 | volatile uint32_t last_adjusted_target_bitrate_bps_ GUARDED_BY(crit_); |
| 80 | // Used to estimate bitrate. |
| 81 | RateStatistics bitrate_tracker_ GUARDED_BY(crit_); |
| 82 | // The last time we tried to adjust the bitrate. |
| 83 | uint32_t last_bitrate_update_time_ms_ GUARDED_BY(crit_); |
| 84 | // The number of frames since the last time we tried to adjust the bitrate. |
| 85 | uint32_t frames_since_last_update_ GUARDED_BY(crit_); |
| 86 | }; |
| 87 | |
| 88 | } // namespace webrtc |
| 89 | |
| 90 | #endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_BITRATE_ADJUSTER_H_ |