blob: f7ef990830eb273e999ddf4bb27b7a0ff3abd4fd [file] [log] [blame]
Erik Språng7ca375c2019-02-06 16:20:17 +01001/*
2 * Copyright (c) 2019 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 VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_
12#define VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_
13
14#include <deque>
15
16#include "absl/types/optional.h"
17#include "api/units/data_rate.h"
18
19namespace webrtc {
20
21class EncoderOvershootDetector {
22 public:
23 explicit EncoderOvershootDetector(int64_t window_size_ms);
24 ~EncoderOvershootDetector();
25
26 void SetTargetRate(DataRate target_bitrate,
27 double target_framerate_fps,
28 int64_t time_ms);
Erik Språng6c072ef2019-04-01 12:57:28 +020029 // A frame has been encoded or dropped. |bytes| == 0 indicates a drop.
Erik Språng7ca375c2019-02-06 16:20:17 +010030 void OnEncodedFrame(size_t bytes, int64_t time_ms);
Erik Språng6c072ef2019-04-01 12:57:28 +020031 // This utilization factor reaches 1.0 only if the encoder produces encoded
32 // frame in such a way that they can be sent onto the network at
33 // |target_bitrate| without building growing queues.
34 absl::optional<double> GetNetworkRateUtilizationFactor(int64_t time_ms);
35 // This utilization factor is based just on actual encoded frame sizes in
36 // relation to ideal sizes. An undershoot may be compensated by an
37 // overshoot so that the average over time is close to |target_bitrate|.
38 absl::optional<double> GetMediaRateUtilizationFactor(int64_t time_ms);
Erik Språng7ca375c2019-02-06 16:20:17 +010039 void Reset();
40
41 private:
42 int64_t IdealFrameSizeBits() const;
43 void LeakBits(int64_t time_ms);
Erik Språng6c072ef2019-04-01 12:57:28 +020044 void CullOldUpdates(int64_t time_ms);
45 // Updates provided buffer and checks if overuse ensues, returns
46 // the calculated utilization factor for this frame.
47 double HandleEncodedFrame(size_t frame_size_bits,
48 int64_t ideal_frame_size_bits,
49 int64_t time_ms,
50 int64_t* buffer_level_bits) const;
Erik Språng7ca375c2019-02-06 16:20:17 +010051
52 const int64_t window_size_ms_;
53 int64_t time_last_update_ms_;
54 struct BitrateUpdate {
Erik Språng6c072ef2019-04-01 12:57:28 +020055 BitrateUpdate(double network_utilization_factor,
56 double media_utilization_factor,
57 int64_t update_time_ms)
58 : network_utilization_factor(network_utilization_factor),
59 media_utilization_factor(media_utilization_factor),
Erik Språng7ca375c2019-02-06 16:20:17 +010060 update_time_ms(update_time_ms) {}
Erik Språng6c072ef2019-04-01 12:57:28 +020061 // The utilization factor based on strict network rate.
62 double network_utilization_factor;
63 // The utilization based on average media rate.
64 double media_utilization_factor;
Erik Språng7ca375c2019-02-06 16:20:17 +010065 int64_t update_time_ms;
66 };
67 std::deque<BitrateUpdate> utilization_factors_;
Erik Språng6c072ef2019-04-01 12:57:28 +020068 double sum_network_utilization_factors_;
69 double sum_media_utilization_factors_;
Erik Språng7ca375c2019-02-06 16:20:17 +010070 DataRate target_bitrate_;
71 double target_framerate_fps_;
Erik Språng6c072ef2019-04-01 12:57:28 +020072 int64_t network_buffer_level_bits_;
73 int64_t media_buffer_level_bits_;
Erik Språng7ca375c2019-02-06 16:20:17 +010074};
75
76} // namespace webrtc
77
78#endif // VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_