• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
12 #define COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include "absl/types/optional.h"
18 #include "rtc_base/rate_statistics.h"
19 #include "rtc_base/synchronization/mutex.h"
20 #include "rtc_base/system/rtc_export.h"
21 #include "rtc_base/thread_annotations.h"
22 
23 namespace webrtc {
24 
25 // Certain hardware encoders tend to consistently overshoot the bitrate that
26 // they are configured to encode at. This class estimates an adjusted bitrate
27 // that when set on the encoder will produce the desired bitrate.
28 class RTC_EXPORT BitrateAdjuster {
29  public:
30   // min_adjusted_bitrate_pct and max_adjusted_bitrate_pct are the lower and
31   // upper bound outputted adjusted bitrates as a percentage of the target
32   // bitrate.
33   BitrateAdjuster(float min_adjusted_bitrate_pct,
34                   float max_adjusted_bitrate_pct);
~BitrateAdjuster()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   absl::optional<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
64       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
65   // Returns largest possible adjusted value.
66   uint32_t GetMaxAdjustedBitrateBps() const
67       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
68 
69   void Reset();
70   void UpdateBitrate(uint32_t current_time_ms)
71       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
72 
73   mutable Mutex mutex_;
74   const float min_adjusted_bitrate_pct_;
75   const float max_adjusted_bitrate_pct_;
76   // The bitrate we want.
77   volatile uint32_t target_bitrate_bps_ RTC_GUARDED_BY(mutex_);
78   // The bitrate we use to get what we want.
79   volatile uint32_t adjusted_bitrate_bps_ RTC_GUARDED_BY(mutex_);
80   // The target bitrate that the adjusted bitrate was computed from.
81   volatile uint32_t last_adjusted_target_bitrate_bps_ RTC_GUARDED_BY(mutex_);
82   // Used to estimate bitrate.
83   RateStatistics bitrate_tracker_ RTC_GUARDED_BY(mutex_);
84   // The last time we tried to adjust the bitrate.
85   uint32_t last_bitrate_update_time_ms_ RTC_GUARDED_BY(mutex_);
86   // The number of frames since the last time we tried to adjust the bitrate.
87   uint32_t frames_since_last_update_ RTC_GUARDED_BY(mutex_);
88 };
89 
90 }  // namespace webrtc
91 
92 #endif  // COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
93