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/rollingaccumulator.h" |
| 18 |
| 19 namespace webrtc { |
| 20 |
| 21 class BitrateAdjusterObserver { |
| 22 public: |
| 23 virtual ~BitrateAdjusterObserver() {} |
| 24 |
| 25 // Called when a new bitrate adjustment is computed. |
| 26 virtual void OnBitrateAdjusted(uint32_t adjusted_bitrate_kbit) = 0; |
| 27 }; |
| 28 |
| 29 // Certain hardware encoders tend to consistently overshoot the bitrate that |
| 30 // they are configured to encode at. This class estimates an adjusted bitrate |
| 31 // that when set on the encoder will produce the desired bitrate. |
| 32 class BitrateAdjuster { |
| 33 public: |
| 34 // The observer is called each time a new bitrate adjustment is computed. |
| 35 explicit BitrateAdjuster(BitrateAdjusterObserver* observer); |
| 36 virtual ~BitrateAdjuster() {} |
| 37 |
| 38 // Sets the desired bitrate in kbps. Should be called at least once before |
| 39 // Update. |
| 40 void SetTargetBitrate(uint32_t bitrate_kbit); |
| 41 uint32_t GetTargetBitrate() const; |
| 42 |
| 43 // Returns the adjusted bitrate in kbps. |
| 44 uint32_t GetAdjustedBitrate() const; |
| 45 |
| 46 // This should be called after each frame is encoded. The timestamp at which |
| 47 // it is called is used to estimate the output bitrate of the encoder. |
| 48 // Should be called from only one thread. |
| 49 void Update(size_t frame_size); |
| 50 |
| 51 private: |
| 52 static const uint32_t kBitrateUpdateIntervalMs; |
| 53 static const uint32_t kBitrateTolerancePct; |
| 54 |
| 55 void Reset(); |
| 56 void UpdateBitrate(uint32_t current_time_ms); |
| 57 void UpdateBitrateTracker(size_t frame_size, uint32_t current_time_ms); |
| 58 |
| 59 rtc::CriticalSection crit_; |
| 60 BitrateAdjusterObserver* observer_; |
| 61 // The bitrate we want. |
| 62 volatile uint32_t target_bitrate_kbit_ GUARDED_BY(crit_); |
| 63 // The bitrate we use to get what we want. |
| 64 volatile uint32_t adjusted_bitrate_kbit_ GUARDED_BY(crit_); |
| 65 // Used to estimate bitrate. |
| 66 rtc::RollingAccumulator<float> bitrate_tracker_ GUARDED_BY(crit_); |
| 67 // The last time we tried to adjust the bitrate. |
| 68 uint32_t last_bitrate_update_time_ms_ GUARDED_BY(crit_); |
| 69 // The timestamp of the last time Update was called. |
| 70 uint32_t last_update_time_ms_ GUARDED_BY(crit_); |
| 71 }; |
| 72 |
| 73 } // namespace webrtc |
| 74 |
| 75 #endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_BITRATE_ADJUSTER_H_ |
OLD | NEW |