blob: 5fd1e3851b1c6369f1cc937a7400c0f8d86fd280 [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
emircan55a401e2016-04-26 12:55:07 -070011#ifndef WEBRTC_COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
12#define WEBRTC_COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_
tkchinf75d0082016-02-23 22:49:42 -080013
14#include <functional>
15
16#include "webrtc/base/criticalsection.h"
tkchinf75d0082016-02-23 22:49:42 -080017#include "webrtc/base/rate_statistics.h"
18
19namespace webrtc {
20
21class Clock;
22
23// Certain hardware encoders tend to consistently overshoot the bitrate that
24// they are configured to encode at. This class estimates an adjusted bitrate
25// that when set on the encoder will produce the desired bitrate.
26class BitrateAdjuster {
27 public:
28 // min_adjusted_bitrate_pct and max_adjusted_bitrate_pct are the lower and
29 // upper bound outputted adjusted bitrates as a percentage of the target
30 // bitrate.
31 BitrateAdjuster(Clock* clock,
32 float min_adjusted_bitrate_pct,
33 float max_adjusted_bitrate_pct);
34 virtual ~BitrateAdjuster() {}
35
36 static const uint32_t kBitrateUpdateIntervalMs;
37 static const uint32_t kBitrateUpdateFrameInterval;
38 static const float kBitrateTolerancePct;
39 static const float kBytesPerMsToBitsPerSecond;
40
41 // Sets the desired bitrate in bps (bits per second).
42 // Should be called at least once before Update.
43 void SetTargetBitrateBps(uint32_t bitrate_bps);
44 uint32_t GetTargetBitrateBps() const;
45
46 // Returns the adjusted bitrate in bps.
47 uint32_t GetAdjustedBitrateBps() const;
48
49 // Returns what we think the current bitrate is.
Erik Språng51e60302016-06-10 22:13:21 +020050 rtc::Optional<uint32_t> GetEstimatedBitrateBps();
tkchinf75d0082016-02-23 22:49:42 -080051
52 // This should be called after each frame is encoded. The timestamp at which
53 // it is called is used to estimate the output bitrate of the encoder.
54 // Should be called from only one thread.
55 void Update(size_t frame_size);
56
57 private:
58 // Returns true if the bitrate is within kBitrateTolerancePct of bitrate_bps.
59 bool IsWithinTolerance(uint32_t bitrate_bps, uint32_t target_bitrate_bps);
60
61 // Returns smallest possible adjusted value.
62 uint32_t GetMinAdjustedBitrateBps() const EXCLUSIVE_LOCKS_REQUIRED(crit_);
63 // Returns largest possible adjusted value.
64 uint32_t GetMaxAdjustedBitrateBps() const EXCLUSIVE_LOCKS_REQUIRED(crit_);
65
66 void Reset();
67 void UpdateBitrate(uint32_t current_time_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_);
68
69 rtc::CriticalSection crit_;
70 Clock* const clock_;
71 const float min_adjusted_bitrate_pct_;
72 const float max_adjusted_bitrate_pct_;
73 // The bitrate we want.
74 volatile uint32_t target_bitrate_bps_ GUARDED_BY(crit_);
75 // The bitrate we use to get what we want.
76 volatile uint32_t adjusted_bitrate_bps_ GUARDED_BY(crit_);
77 // The target bitrate that the adjusted bitrate was computed from.
78 volatile uint32_t last_adjusted_target_bitrate_bps_ GUARDED_BY(crit_);
79 // Used to estimate bitrate.
80 RateStatistics bitrate_tracker_ GUARDED_BY(crit_);
81 // The last time we tried to adjust the bitrate.
82 uint32_t last_bitrate_update_time_ms_ GUARDED_BY(crit_);
83 // The number of frames since the last time we tried to adjust the bitrate.
84 uint32_t frames_since_last_update_ GUARDED_BY(crit_);
85};
86
87} // namespace webrtc
88
emircan55a401e2016-04-26 12:55:07 -070089#endif // WEBRTC_COMMON_VIDEO_INCLUDE_BITRATE_ADJUSTER_H_