frame_dropper.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2011 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 MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
  11. #define MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include "rtc_base/numerics/exp_filter.h"
  15. namespace webrtc {
  16. // The Frame Dropper implements a variant of the leaky bucket algorithm
  17. // for keeping track of when to drop frames to avoid bit rate
  18. // over use when the encoder can't keep its bit rate.
  19. class FrameDropper {
  20. public:
  21. FrameDropper();
  22. ~FrameDropper();
  23. // Resets the FrameDropper to its initial state.
  24. void Reset();
  25. void Enable(bool enable);
  26. // Answers the question if it's time to drop a frame if we want to reach a
  27. // given frame rate. Must be called for every frame.
  28. //
  29. // Return value : True if we should drop the current frame.
  30. bool DropFrame();
  31. // Updates the FrameDropper with the size of the latest encoded frame.
  32. // The FrameDropper calculates a new drop ratio (can be seen as the
  33. // probability to drop a frame) and updates its internal statistics.
  34. //
  35. // Input:
  36. // - framesize_bytes : The size of the latest frame returned
  37. // from the encoder.
  38. // - delta_frame : True if the encoder returned a key frame.
  39. void Fill(size_t framesize_bytes, bool delta_frame);
  40. void Leak(uint32_t input_framerate);
  41. // Sets the target bit rate and the frame rate produced by the camera.
  42. //
  43. // Input:
  44. // - bitrate : The target bit rate.
  45. void SetRates(float bitrate, float incoming_frame_rate);
  46. private:
  47. void UpdateRatio();
  48. void CapAccumulator();
  49. rtc::ExpFilter key_frame_ratio_;
  50. rtc::ExpFilter delta_frame_size_avg_kbits_;
  51. // Key frames and large delta frames are not immediately accumulated in the
  52. // bucket since they can immediately overflow the bucket leading to large
  53. // drops on the following packets that may be much smaller. Instead these
  54. // large frames are accumulated over several frames when the bucket leaks.
  55. // |large_frame_accumulation_spread_| represents the number of frames over
  56. // which a large frame is accumulated.
  57. float large_frame_accumulation_spread_;
  58. // |large_frame_accumulation_count_| represents the number of frames left
  59. // to finish accumulating a large frame.
  60. int large_frame_accumulation_count_;
  61. // |large_frame_accumulation_chunk_size_| represents the size of a single
  62. // chunk for large frame accumulation.
  63. float large_frame_accumulation_chunk_size_;
  64. float accumulator_;
  65. float accumulator_max_;
  66. float target_bitrate_;
  67. bool drop_next_;
  68. rtc::ExpFilter drop_ratio_;
  69. int drop_count_;
  70. float incoming_frame_rate_;
  71. bool was_below_max_;
  72. bool enabled_;
  73. const float max_drop_duration_secs_;
  74. };
  75. } // namespace webrtc
  76. #endif // MODULES_VIDEO_CODING_UTILITY_FRAME_DROPPER_H_