Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 1 | /* |
| 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" |
yingyingma | d341697 | 2023-04-20 06:28:01 | [diff] [blame] | 18 | #include "api/video_codecs/video_codec.h" |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | class EncoderOvershootDetector { |
| 23 | public: |
yingyingma | d341697 | 2023-04-20 06:28:01 | [diff] [blame] | 24 | explicit EncoderOvershootDetector(int64_t window_size_ms, |
| 25 | VideoCodecType codec, |
| 26 | bool is_screenshare); |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 27 | ~EncoderOvershootDetector(); |
| 28 | |
| 29 | void SetTargetRate(DataRate target_bitrate, |
| 30 | double target_framerate_fps, |
| 31 | int64_t time_ms); |
Artem Titov | ab30d72 | 2021-07-27 14:22:11 | [diff] [blame] | 32 | // A frame has been encoded or dropped. `bytes` == 0 indicates a drop. |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 33 | void OnEncodedFrame(size_t bytes, int64_t time_ms); |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 34 | // 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 Titov | ab30d72 | 2021-07-27 14:22:11 | [diff] [blame] | 36 | // `target_bitrate` without building growing queues. |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 37 | 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 Titov | ab30d72 | 2021-07-27 14:22:11 | [diff] [blame] | 40 | // overshoot so that the average over time is close to `target_bitrate`. |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 41 | absl::optional<double> GetMediaRateUtilizationFactor(int64_t time_ms); |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 42 | void Reset(); |
| 43 | |
| 44 | private: |
| 45 | int64_t IdealFrameSizeBits() const; |
| 46 | void LeakBits(int64_t time_ms); |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 47 | 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ång | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 54 | |
| 55 | const int64_t window_size_ms_; |
| 56 | int64_t time_last_update_ms_; |
| 57 | struct BitrateUpdate { |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 58 | 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ång | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 63 | update_time_ms(update_time_ms) {} |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 64 | // 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ång | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 68 | int64_t update_time_ms; |
| 69 | }; |
yingyingma | d341697 | 2023-04-20 06:28:01 | [diff] [blame] | 70 | void UpdateHistograms(); |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 71 | std::deque<BitrateUpdate> utilization_factors_; |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 72 | double sum_network_utilization_factors_; |
| 73 | double sum_media_utilization_factors_; |
Erik Språng | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 74 | DataRate target_bitrate_; |
| 75 | double target_framerate_fps_; |
Erik Språng | 6c072ef | 2019-04-01 10:57:28 | [diff] [blame] | 76 | int64_t network_buffer_level_bits_; |
| 77 | int64_t media_buffer_level_bits_; |
yingyingma | d341697 | 2023-04-20 06:28:01 | [diff] [blame] | 78 | 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ång | 7ca375c | 2019-02-06 15:20:17 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace webrtc |
| 86 | |
| 87 | #endif // VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_ |