blob: 76ce9e9901f71f4d6e736958ce1e3205a685e4e7 [file] [log] [blame]
tkchinf75d0082016-02-23 22:49:42 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
12#define COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
tkchinf75d0082016-02-23 22:49:42 -080013
14#include <functional>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/criticalsection.h"
17#include "rtc_base/rate_statistics.h"
tkchinf75d0082016-02-23 22:49:42 -080018
19namespace webrtc {
20
tkchinf75d0082016-02-23 22:49:42 -080021// Certain hardware encoders tend to consistently overshoot the bitrate that
22// they are configured to encode at. This class estimates an adjusted bitrate
23// that when set on the encoder will produce the desired bitrate.
24class BitrateAdjuster {
25 public:
26 // min_adjusted_bitrate_pct and max_adjusted_bitrate_pct are the lower and
27 // upper bound outputted adjusted bitrates as a percentage of the target
28 // bitrate.
Niels Möller2cb7b5e2018-04-19 10:02:26 +020029 BitrateAdjuster(float min_adjusted_bitrate_pct,
tkchinf75d0082016-02-23 22:49:42 -080030 float max_adjusted_bitrate_pct);
31 virtual ~BitrateAdjuster() {}
32
33 static const uint32_t kBitrateUpdateIntervalMs;
34 static const uint32_t kBitrateUpdateFrameInterval;
35 static const float kBitrateTolerancePct;
36 static const float kBytesPerMsToBitsPerSecond;
37
38 // Sets the desired bitrate in bps (bits per second).
39 // Should be called at least once before Update.
40 void SetTargetBitrateBps(uint32_t bitrate_bps);
41 uint32_t GetTargetBitrateBps() const;
42
43 // Returns the adjusted bitrate in bps.
44 uint32_t GetAdjustedBitrateBps() const;
45
46 // Returns what we think the current bitrate is.
Erik Språng51e60302016-06-10 22:13:21 +020047 rtc::Optional<uint32_t> GetEstimatedBitrateBps();
tkchinf75d0082016-02-23 22:49:42 -080048
49 // This should be called after each frame is encoded. The timestamp at which
50 // it is called is used to estimate the output bitrate of the encoder.
51 // Should be called from only one thread.
52 void Update(size_t frame_size);
53
54 private:
55 // Returns true if the bitrate is within kBitrateTolerancePct of bitrate_bps.
56 bool IsWithinTolerance(uint32_t bitrate_bps, uint32_t target_bitrate_bps);
57
58 // Returns smallest possible adjusted value.
danilchapa37de392017-09-09 04:17:22 -070059 uint32_t GetMinAdjustedBitrateBps() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
tkchinf75d0082016-02-23 22:49:42 -080060 // Returns largest possible adjusted value.
danilchapa37de392017-09-09 04:17:22 -070061 uint32_t GetMaxAdjustedBitrateBps() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
tkchinf75d0082016-02-23 22:49:42 -080062
63 void Reset();
danilchapa37de392017-09-09 04:17:22 -070064 void UpdateBitrate(uint32_t current_time_ms)
65 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
tkchinf75d0082016-02-23 22:49:42 -080066
67 rtc::CriticalSection crit_;
tkchinf75d0082016-02-23 22:49:42 -080068 const float min_adjusted_bitrate_pct_;
69 const float max_adjusted_bitrate_pct_;
70 // The bitrate we want.
danilchapa37de392017-09-09 04:17:22 -070071 volatile uint32_t target_bitrate_bps_ RTC_GUARDED_BY(crit_);
tkchinf75d0082016-02-23 22:49:42 -080072 // The bitrate we use to get what we want.
danilchapa37de392017-09-09 04:17:22 -070073 volatile uint32_t adjusted_bitrate_bps_ RTC_GUARDED_BY(crit_);
tkchinf75d0082016-02-23 22:49:42 -080074 // The target bitrate that the adjusted bitrate was computed from.
danilchapa37de392017-09-09 04:17:22 -070075 volatile uint32_t last_adjusted_target_bitrate_bps_ RTC_GUARDED_BY(crit_);
tkchinf75d0082016-02-23 22:49:42 -080076 // Used to estimate bitrate.
danilchapa37de392017-09-09 04:17:22 -070077 RateStatistics bitrate_tracker_ RTC_GUARDED_BY(crit_);
tkchinf75d0082016-02-23 22:49:42 -080078 // The last time we tried to adjust the bitrate.
danilchapa37de392017-09-09 04:17:22 -070079 uint32_t last_bitrate_update_time_ms_ RTC_GUARDED_BY(crit_);
tkchinf75d0082016-02-23 22:49:42 -080080 // The number of frames since the last time we tried to adjust the bitrate.
danilchapa37de392017-09-09 04:17:22 -070081 uint32_t frames_since_last_update_ RTC_GUARDED_BY(crit_);
tkchinf75d0082016-02-23 22:49:42 -080082};
83
84} // namespace webrtc
85
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020086#endif // COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_