screenshare_layers.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  2. *
  3. * Use of this source code is governed by a BSD-style license
  4. * that can be found in the LICENSE file in the root of the source
  5. * tree. An additional intellectual property rights grant can be found
  6. * in the file PATENTS. All contributing project authors may
  7. * be found in the AUTHORS file in the root of the source tree.
  8. */
  9. #ifndef MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_
  10. #define MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_
  11. #include <map>
  12. #include <memory>
  13. #include <utility>
  14. #include <vector>
  15. #include "api/video_codecs/vp8_frame_config.h"
  16. #include "api/video_codecs/vp8_temporal_layers.h"
  17. #include "modules/video_coding/codecs/vp8/include/temporal_layers_checker.h"
  18. #include "modules/video_coding/include/video_codec_interface.h"
  19. #include "modules/video_coding/utility/frame_dropper.h"
  20. #include "rtc_base/rate_statistics.h"
  21. #include "rtc_base/time_utils.h"
  22. namespace webrtc {
  23. struct CodecSpecificInfoVP8;
  24. class Clock;
  25. class ScreenshareLayers final : public Vp8FrameBufferController {
  26. public:
  27. static const double kMaxTL0FpsReduction;
  28. static const double kAcceptableTargetOvershoot;
  29. static const int kMaxFrameIntervalMs;
  30. explicit ScreenshareLayers(int num_temporal_layers);
  31. ~ScreenshareLayers() override;
  32. void SetQpLimits(size_t stream_index, int min_qp, int max_qp) override;
  33. size_t StreamCount() const override;
  34. bool SupportsEncoderFrameDropping(size_t stream_index) const override;
  35. // Returns the recommended VP8 encode flags needed. May refresh the decoder
  36. // and/or update the reference buffers.
  37. Vp8FrameConfig NextFrameConfig(size_t stream_index,
  38. uint32_t rtp_timestamp) override;
  39. // New target bitrate, per temporal layer.
  40. void OnRatesUpdated(size_t stream_index,
  41. const std::vector<uint32_t>& bitrates_bps,
  42. int framerate_fps) override;
  43. Vp8EncoderConfig UpdateConfiguration(size_t stream_index) override;
  44. void OnEncodeDone(size_t stream_index,
  45. uint32_t rtp_timestamp,
  46. size_t size_bytes,
  47. bool is_keyframe,
  48. int qp,
  49. CodecSpecificInfo* info) override;
  50. void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) override;
  51. void OnPacketLossRateUpdate(float packet_loss_rate) override;
  52. void OnRttUpdate(int64_t rtt_ms) override;
  53. void OnLossNotification(
  54. const VideoEncoder::LossNotification& loss_notification) override;
  55. private:
  56. enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync };
  57. struct DependencyInfo {
  58. DependencyInfo() = default;
  59. DependencyInfo(absl::string_view indication_symbols,
  60. Vp8FrameConfig frame_config)
  61. : decode_target_indications(
  62. webrtc_impl::StringToDecodeTargetIndications(indication_symbols)),
  63. frame_config(frame_config) {}
  64. absl::InlinedVector<DecodeTargetIndication, 10> decode_target_indications;
  65. Vp8FrameConfig frame_config;
  66. };
  67. bool TimeToSync(int64_t timestamp) const;
  68. uint32_t GetCodecTargetBitrateKbps() const;
  69. const int number_of_temporal_layers_;
  70. // TODO(eladalon/sprang): These should be made into const-int set in the ctor.
  71. absl::optional<int> min_qp_;
  72. absl::optional<int> max_qp_;
  73. int active_layer_;
  74. int64_t last_timestamp_;
  75. int64_t last_sync_timestamp_;
  76. int64_t last_emitted_tl0_timestamp_;
  77. int64_t last_frame_time_ms_;
  78. rtc::TimestampWrapAroundHandler time_wrap_handler_;
  79. uint32_t max_debt_bytes_;
  80. std::map<uint32_t, DependencyInfo> pending_frame_configs_;
  81. // Configured max framerate.
  82. absl::optional<uint32_t> target_framerate_;
  83. // Incoming framerate from capturer.
  84. absl::optional<uint32_t> capture_framerate_;
  85. // Tracks what framerate we actually encode, and drops frames on overshoot.
  86. RateStatistics encode_framerate_;
  87. bool bitrate_updated_;
  88. static constexpr int kMaxNumTemporalLayers = 2;
  89. struct TemporalLayer {
  90. TemporalLayer()
  91. : state(State::kNormal),
  92. enhanced_max_qp(-1),
  93. last_qp(-1),
  94. debt_bytes_(0),
  95. target_rate_kbps_(0) {}
  96. enum class State {
  97. kNormal,
  98. kDropped,
  99. kReencoded,
  100. kQualityBoost,
  101. kKeyFrame
  102. } state;
  103. int enhanced_max_qp;
  104. int last_qp;
  105. uint32_t debt_bytes_;
  106. uint32_t target_rate_kbps_;
  107. void UpdateDebt(int64_t delta_ms);
  108. } layers_[kMaxNumTemporalLayers];
  109. void UpdateHistograms();
  110. FrameDependencyStructure GetTemplateStructure(int num_layers) const;
  111. // Data for histogram statistics.
  112. struct Stats {
  113. int64_t first_frame_time_ms_ = -1;
  114. int64_t num_tl0_frames_ = 0;
  115. int64_t num_tl1_frames_ = 0;
  116. int64_t num_dropped_frames_ = 0;
  117. int64_t num_overshoots_ = 0;
  118. int64_t tl0_qp_sum_ = 0;
  119. int64_t tl1_qp_sum_ = 0;
  120. int64_t tl0_target_bitrate_sum_ = 0;
  121. int64_t tl1_target_bitrate_sum_ = 0;
  122. } stats_;
  123. Vp8EncoderConfig encoder_config_;
  124. // Optional utility used to verify reference validity.
  125. std::unique_ptr<TemporalLayersChecker> checker_;
  126. };
  127. } // namespace webrtc
  128. #endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_