blob: 12c4bba5db96f1cf5216e91a5d2a9efb98f86681 [file] [log] [blame]
Erik Språng7ca375c2019-02-06 15:20:171/*
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"
yingyingmad3416972023-04-20 06:28:0118#include "api/video_codecs/video_codec.h"
Erik Språng7ca375c2019-02-06 15:20:1719
20namespace webrtc {
21
22class EncoderOvershootDetector {
23 public:
yingyingmad3416972023-04-20 06:28:0124 explicit EncoderOvershootDetector(int64_t window_size_ms,
25 VideoCodecType codec,
26 bool is_screenshare);
Erik Språng7ca375c2019-02-06 15:20:1727 ~EncoderOvershootDetector();
28
29 void SetTargetRate(DataRate target_bitrate,
30 double target_framerate_fps,
31 int64_t time_ms);
Artem Titovab30d722021-07-27 14:22:1132 // A frame has been encoded or dropped. `bytes` == 0 indicates a drop.
Erik Språng7ca375c2019-02-06 15:20:1733 void OnEncodedFrame(size_t bytes, int64_t time_ms);
Erik Språng6c072ef2019-04-01 10:57:2834 // This utilization factor reaches 1.0 only if the encoder produces encoded
35 // frame in such a way that they can be sent onto the network at
Artem Titovab30d722021-07-27 14:22:1136 // `target_bitrate` without building growing queues.
Erik Språng6c072ef2019-04-01 10:57:2837 absl::optional<double> GetNetworkRateUtilizationFactor(int64_t time_ms);
38 // This utilization factor is based just on actual encoded frame sizes in
39 // relation to ideal sizes. An undershoot may be compensated by an
Artem Titovab30d722021-07-27 14:22:1140 // overshoot so that the average over time is close to `target_bitrate`.
Erik Språng6c072ef2019-04-01 10:57:2841 absl::optional<double> GetMediaRateUtilizationFactor(int64_t time_ms);
Erik Språng7ca375c2019-02-06 15:20:1742 void Reset();
43
44 private:
45 int64_t IdealFrameSizeBits() const;
46 void LeakBits(int64_t time_ms);
Erik Språng6c072ef2019-04-01 10:57:2847 void CullOldUpdates(int64_t time_ms);
48 // Updates provided buffer and checks if overuse ensues, returns
49 // the calculated utilization factor for this frame.
50 double HandleEncodedFrame(size_t frame_size_bits,
51 int64_t ideal_frame_size_bits,
52 int64_t time_ms,
53 int64_t* buffer_level_bits) const;
Erik Språng7ca375c2019-02-06 15:20:1754
55 const int64_t window_size_ms_;
56 int64_t time_last_update_ms_;
57 struct BitrateUpdate {
Erik Språng6c072ef2019-04-01 10:57:2858 BitrateUpdate(double network_utilization_factor,
59 double media_utilization_factor,
60 int64_t update_time_ms)
61 : network_utilization_factor(network_utilization_factor),
62 media_utilization_factor(media_utilization_factor),
Erik Språng7ca375c2019-02-06 15:20:1763 update_time_ms(update_time_ms) {}
Erik Språng6c072ef2019-04-01 10:57:2864 // The utilization factor based on strict network rate.
65 double network_utilization_factor;
66 // The utilization based on average media rate.
67 double media_utilization_factor;
Erik Språng7ca375c2019-02-06 15:20:1768 int64_t update_time_ms;
69 };
yingyingmad3416972023-04-20 06:28:0170 void UpdateHistograms();
Erik Språng7ca375c2019-02-06 15:20:1771 std::deque<BitrateUpdate> utilization_factors_;
Erik Språng6c072ef2019-04-01 10:57:2872 double sum_network_utilization_factors_;
73 double sum_media_utilization_factors_;
Erik Språng7ca375c2019-02-06 15:20:1774 DataRate target_bitrate_;
75 double target_framerate_fps_;
Erik Språng6c072ef2019-04-01 10:57:2876 int64_t network_buffer_level_bits_;
77 int64_t media_buffer_level_bits_;
yingyingmad3416972023-04-20 06:28:0178 VideoCodecType codec_;
79 bool is_screenshare_;
80 int64_t frame_count_;
81 int64_t sum_diff_kbps_squared_;
82 int64_t sum_overshoot_percent_;
Erik Språng7ca375c2019-02-06 15:20:1783};
84
85} // namespace webrtc
86
87#endif // VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_