encoder_overshoot_detector.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_
  11. #define VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_
  12. #include <deque>
  13. #include "absl/types/optional.h"
  14. #include "api/units/data_rate.h"
  15. namespace webrtc {
  16. class EncoderOvershootDetector {
  17. public:
  18. explicit EncoderOvershootDetector(int64_t window_size_ms);
  19. ~EncoderOvershootDetector();
  20. void SetTargetRate(DataRate target_bitrate,
  21. double target_framerate_fps,
  22. int64_t time_ms);
  23. // A frame has been encoded or dropped. |bytes| == 0 indicates a drop.
  24. void OnEncodedFrame(size_t bytes, int64_t time_ms);
  25. // This utilization factor reaches 1.0 only if the encoder produces encoded
  26. // frame in such a way that they can be sent onto the network at
  27. // |target_bitrate| without building growing queues.
  28. absl::optional<double> GetNetworkRateUtilizationFactor(int64_t time_ms);
  29. // This utilization factor is based just on actual encoded frame sizes in
  30. // relation to ideal sizes. An undershoot may be compensated by an
  31. // overshoot so that the average over time is close to |target_bitrate|.
  32. absl::optional<double> GetMediaRateUtilizationFactor(int64_t time_ms);
  33. void Reset();
  34. private:
  35. int64_t IdealFrameSizeBits() const;
  36. void LeakBits(int64_t time_ms);
  37. void CullOldUpdates(int64_t time_ms);
  38. // Updates provided buffer and checks if overuse ensues, returns
  39. // the calculated utilization factor for this frame.
  40. double HandleEncodedFrame(size_t frame_size_bits,
  41. int64_t ideal_frame_size_bits,
  42. int64_t time_ms,
  43. int64_t* buffer_level_bits) const;
  44. const int64_t window_size_ms_;
  45. int64_t time_last_update_ms_;
  46. struct BitrateUpdate {
  47. BitrateUpdate(double network_utilization_factor,
  48. double media_utilization_factor,
  49. int64_t update_time_ms)
  50. : network_utilization_factor(network_utilization_factor),
  51. media_utilization_factor(media_utilization_factor),
  52. update_time_ms(update_time_ms) {}
  53. // The utilization factor based on strict network rate.
  54. double network_utilization_factor;
  55. // The utilization based on average media rate.
  56. double media_utilization_factor;
  57. int64_t update_time_ms;
  58. };
  59. std::deque<BitrateUpdate> utilization_factors_;
  60. double sum_network_utilization_factors_;
  61. double sum_media_utilization_factors_;
  62. DataRate target_bitrate_;
  63. double target_framerate_fps_;
  64. int64_t network_buffer_level_bits_;
  65. int64_t media_buffer_level_bits_;
  66. };
  67. } // namespace webrtc
  68. #endif // VIDEO_ENCODER_OVERSHOOT_DETECTOR_H_