bitrate_adjuster.h 3.6 KB

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