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