screenshare_layers.h (5450B)
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 12 #include <cstddef> 13 #include <cstdint> 14 #include <map> 15 #include <memory> 16 #include <optional> 17 #include <vector> 18 19 #include "absl/container/inlined_vector.h" 20 #include "absl/strings/string_view.h" 21 #include "api/environment/environment.h" 22 #include "api/transport/rtp/dependency_descriptor.h" 23 #include "api/video_codecs/video_encoder.h" 24 #include "api/video_codecs/vp8_frame_buffer_controller.h" 25 #include "api/video_codecs/vp8_frame_config.h" 26 #include "modules/video_coding/codecs/vp8/include/temporal_layers_checker.h" 27 #include "modules/video_coding/include/video_codec_interface.h" 28 #include "rtc_base/numerics/sequence_number_unwrapper.h" 29 #include "rtc_base/rate_statistics.h" 30 31 namespace webrtc { 32 33 class ScreenshareLayers final : public Vp8FrameBufferController { 34 public: 35 static const double kMaxTL0FpsReduction; 36 static const double kAcceptableTargetOvershoot; 37 static const int kMaxFrameIntervalMs; 38 39 ScreenshareLayers(const Environment& env, int num_temporal_layers); 40 ~ScreenshareLayers() override; 41 42 void SetQpLimits(size_t stream_index, int min_qp, int max_qp) override; 43 44 size_t StreamCount() const override; 45 46 bool SupportsEncoderFrameDropping(size_t stream_index) const override; 47 48 // Returns the recommended VP8 encode flags needed. May refresh the decoder 49 // and/or update the reference buffers. 50 Vp8FrameConfig NextFrameConfig(size_t stream_index, 51 uint32_t rtp_timestamp) override; 52 53 // New target bitrate, per temporal layer. 54 void OnRatesUpdated(size_t stream_index, 55 const std::vector<uint32_t>& bitrates_bps, 56 int framerate_fps) override; 57 58 Vp8EncoderConfig UpdateConfiguration(size_t stream_index) override; 59 60 void OnEncodeDone(size_t stream_index, 61 uint32_t rtp_timestamp, 62 size_t size_bytes, 63 bool is_keyframe, 64 int qp, 65 CodecSpecificInfo* info) override; 66 67 void OnFrameDropped(size_t stream_index, uint32_t rtp_timestamp) override; 68 69 void OnPacketLossRateUpdate(float packet_loss_rate) override; 70 71 void OnRttUpdate(int64_t rtt_ms) override; 72 73 void OnLossNotification( 74 const VideoEncoder::LossNotification& loss_notification) override; 75 76 private: 77 enum class TemporalLayerState : int { kDrop, kTl0, kTl1, kTl1Sync }; 78 79 struct DependencyInfo { 80 DependencyInfo() = default; 81 DependencyInfo(absl::string_view indication_symbols, 82 Vp8FrameConfig frame_config) 83 : decode_target_indications( 84 webrtc_impl::StringToDecodeTargetIndications(indication_symbols)), 85 frame_config(frame_config) {} 86 87 absl::InlinedVector<DecodeTargetIndication, 10> decode_target_indications; 88 Vp8FrameConfig frame_config; 89 }; 90 91 bool TimeToSync(int64_t timestamp) const; 92 uint32_t GetCodecTargetBitrateKbps() const; 93 94 const Environment env_; 95 const int number_of_temporal_layers_; 96 97 // TODO(eladalon/sprang): These should be made into const-int set in the ctor. 98 std::optional<int> min_qp_; 99 std::optional<int> max_qp_; 100 101 int active_layer_; 102 int64_t last_timestamp_; 103 int64_t last_sync_timestamp_; 104 int64_t last_emitted_tl0_timestamp_; 105 int64_t last_frame_time_ms_; 106 RtpTimestampUnwrapper time_wrap_handler_; 107 uint32_t max_debt_bytes_; 108 109 std::map<uint32_t, DependencyInfo> pending_frame_configs_; 110 111 // Configured max framerate. 112 std::optional<uint32_t> target_framerate_; 113 // Incoming framerate from capturer. 114 std::optional<uint32_t> capture_framerate_; 115 116 // Tracks what framerate we actually encode, and drops frames on overshoot. 117 RateStatistics encode_framerate_; 118 bool bitrate_updated_; 119 120 static constexpr int kMaxNumTemporalLayers = 2; 121 struct TemporalLayer { 122 TemporalLayer() 123 : state(State::kNormal), 124 enhanced_max_qp(-1), 125 last_qp(-1), 126 debt_bytes_(0), 127 target_rate_kbps_(0) {} 128 129 enum class State { 130 kNormal, 131 kDropped, 132 kReencoded, 133 kQualityBoost, 134 kKeyFrame 135 } state; 136 137 int enhanced_max_qp; 138 int last_qp; 139 uint32_t debt_bytes_; 140 uint32_t target_rate_kbps_; 141 142 void UpdateDebt(int64_t delta_ms); 143 } layers_[kMaxNumTemporalLayers]; 144 145 void UpdateHistograms(); 146 FrameDependencyStructure GetTemplateStructure(int num_layers) const; 147 148 // Data for histogram statistics. 149 struct Stats { 150 int64_t first_frame_time_ms_ = -1; 151 int64_t num_tl0_frames_ = 0; 152 int64_t num_tl1_frames_ = 0; 153 int64_t num_dropped_frames_ = 0; 154 int64_t num_overshoots_ = 0; 155 int64_t tl0_qp_sum_ = 0; 156 int64_t tl1_qp_sum_ = 0; 157 int64_t tl0_target_bitrate_sum_ = 0; 158 int64_t tl1_target_bitrate_sum_ = 0; 159 } stats_; 160 161 Vp8EncoderConfig encoder_config_; 162 163 // Optional utility used to verify reference validity. 164 std::unique_ptr<TemporalLayersChecker> checker_; 165 }; 166 } // namespace webrtc 167 168 #endif // MODULES_VIDEO_CODING_CODECS_VP8_SCREENSHARE_LAYERS_H_