bitrate_adjuster.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/critical_section.h"
  16. #include "rtc_base/rate_statistics.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 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  52. // Returns largest possible adjusted value.
  53. uint32_t GetMaxAdjustedBitrateBps() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  54. void Reset();
  55. void UpdateBitrate(uint32_t current_time_ms)
  56. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  57. rtc::CriticalSection crit_;
  58. const float min_adjusted_bitrate_pct_;
  59. const float max_adjusted_bitrate_pct_;
  60. // The bitrate we want.
  61. volatile uint32_t target_bitrate_bps_ RTC_GUARDED_BY(crit_);
  62. // The bitrate we use to get what we want.
  63. volatile uint32_t adjusted_bitrate_bps_ RTC_GUARDED_BY(crit_);
  64. // The target bitrate that the adjusted bitrate was computed from.
  65. volatile uint32_t last_adjusted_target_bitrate_bps_ RTC_GUARDED_BY(crit_);
  66. // Used to estimate bitrate.
  67. RateStatistics bitrate_tracker_ RTC_GUARDED_BY(crit_);
  68. // The last time we tried to adjust the bitrate.
  69. uint32_t last_bitrate_update_time_ms_ RTC_GUARDED_BY(crit_);
  70. // The number of frames since the last time we tried to adjust the bitrate.
  71. uint32_t frames_since_last_update_ RTC_GUARDED_BY(crit_);
  72. };
  73. } // namespace webrtc
  74. #endif // COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_