1 /* 2 * Copyright (c) 2013 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 11 #ifndef VIDEO_CONFIG_VIDEO_ENCODER_CONFIG_H_ 12 #define VIDEO_CONFIG_VIDEO_ENCODER_CONFIG_H_ 13 14 #include <stddef.h> 15 16 #include <string> 17 #include <vector> 18 19 #include "absl/types/optional.h" 20 #include "api/scoped_refptr.h" 21 #include "api/video/resolution.h" 22 #include "api/video_codecs/scalability_mode.h" 23 #include "api/video_codecs/sdp_video_format.h" 24 #include "api/video_codecs/video_codec.h" 25 #include "rtc_base/ref_count.h" 26 27 namespace webrtc { 28 29 // The `VideoStream` struct describes a simulcast layer, or "stream". 30 struct VideoStream { 31 VideoStream(); 32 ~VideoStream(); 33 VideoStream(const VideoStream& other); 34 std::string ToString() const; 35 36 // Width/Height in pixels. 37 // This is the actual width and height used to configure encoder, 38 // which might be less than `requested_resolution` due to adaptation 39 // or due to the source providing smaller frames than requested. 40 size_t width; 41 size_t height; 42 43 // Frame rate in fps. 44 int max_framerate; 45 46 // Bitrate, in bps, for the stream. 47 int min_bitrate_bps; 48 int target_bitrate_bps; 49 int max_bitrate_bps; 50 51 // Scaling factor applied to the stream size. 52 // `width` and `height` values are already scaled down. 53 double scale_resolution_down_by; 54 55 // Maximum Quantization Parameter to use when encoding the stream. 56 int max_qp; 57 58 // Determines the number of temporal layers that the stream should be 59 // encoded with. This value should be greater than zero. 60 // TODO(brandtr): This class is used both for configuring the encoder 61 // (meaning that this field _must_ be set), and for signaling the app-level 62 // encoder settings (meaning that the field _may_ be set). We should separate 63 // this and remove this optional instead. 64 absl::optional<size_t> num_temporal_layers; 65 66 // The priority of this stream, to be used when allocating resources 67 // between multiple streams. 68 absl::optional<double> bitrate_priority; 69 70 absl::optional<ScalabilityMode> scalability_mode; 71 72 // If this stream is enabled by the user, or not. 73 bool active; 74 75 // An optional user supplied max_frame_resolution 76 // than can be set independently of (adapted) VideoSource. 77 // This value is set from RtpEncodingParameters::requested_resolution 78 // (i.e. used for signaling app-level settings). 79 // 80 // The actual encode resolution is in `width` and `height`, 81 // which can be lower than requested_resolution, 82 // e.g. if source only provides lower resolution or 83 // if resource adaptation is active. 84 absl::optional<Resolution> requested_resolution; 85 }; 86 87 class VideoEncoderConfig { 88 public: 89 // These are reference counted to permit copying VideoEncoderConfig and be 90 // kept alive until all encoder_specific_settings go out of scope. 91 // TODO(kthelgason): Consider removing the need for copying VideoEncoderConfig 92 // and use absl::optional for encoder_specific_settings instead. 93 class EncoderSpecificSettings : public rtc::RefCountInterface { 94 public: 95 // TODO(pbos): Remove FillEncoderSpecificSettings as soon as VideoCodec is 96 // not in use and encoder implementations ask for codec-specific structs 97 // directly. 98 void FillEncoderSpecificSettings(VideoCodec* codec_struct) const; 99 100 virtual void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const; 101 virtual void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const; 102 103 private: ~EncoderSpecificSettings()104 ~EncoderSpecificSettings() override {} 105 friend class VideoEncoderConfig; 106 }; 107 108 class Vp8EncoderSpecificSettings : public EncoderSpecificSettings { 109 public: 110 explicit Vp8EncoderSpecificSettings(const VideoCodecVP8& specifics); 111 void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const override; 112 113 private: 114 VideoCodecVP8 specifics_; 115 }; 116 117 class Vp9EncoderSpecificSettings : public EncoderSpecificSettings { 118 public: 119 explicit Vp9EncoderSpecificSettings(const VideoCodecVP9& specifics); 120 void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const override; 121 122 private: 123 VideoCodecVP9 specifics_; 124 }; 125 126 enum class ContentType { 127 kRealtimeVideo, 128 kScreen, 129 }; 130 131 class VideoStreamFactoryInterface : public rtc::RefCountInterface { 132 public: 133 // An implementation should return a std::vector<VideoStream> with the 134 // wanted VideoStream settings for the given video resolution. 135 // The size of the vector may not be larger than 136 // `encoder_config.number_of_streams`. 137 virtual std::vector<VideoStream> CreateEncoderStreams( 138 int frame_width, 139 int frame_height, 140 const VideoEncoderConfig& encoder_config) = 0; 141 142 protected: ~VideoStreamFactoryInterface()143 ~VideoStreamFactoryInterface() override {} 144 }; 145 146 VideoEncoderConfig& operator=(VideoEncoderConfig&&) = default; 147 VideoEncoderConfig& operator=(const VideoEncoderConfig&) = delete; 148 149 // Mostly used by tests. Avoid creating copies if you can. Copy()150 VideoEncoderConfig Copy() const { return VideoEncoderConfig(*this); } 151 152 VideoEncoderConfig(); 153 VideoEncoderConfig(VideoEncoderConfig&&); 154 ~VideoEncoderConfig(); 155 std::string ToString() const; 156 157 // TODO(bugs.webrtc.org/6883): Consolidate on one of these. 158 VideoCodecType codec_type; 159 SdpVideoFormat video_format; 160 161 // Note: This factory can be unset, and VideoStreamEncoder will 162 // then use the EncoderStreamFactory. The factory is only set by 163 // tests. 164 rtc::scoped_refptr<VideoStreamFactoryInterface> video_stream_factory; 165 std::vector<SpatialLayer> spatial_layers; 166 ContentType content_type; 167 bool frame_drop_enabled; 168 rtc::scoped_refptr<const EncoderSpecificSettings> encoder_specific_settings; 169 170 // Padding will be used up to this bitrate regardless of the bitrate produced 171 // by the encoder. Padding above what's actually produced by the encoder helps 172 // maintaining a higher bitrate estimate. Padding will however not be sent 173 // unless the estimated bandwidth indicates that the link can handle it. 174 int min_transmit_bitrate_bps; 175 int max_bitrate_bps; 176 // The bitrate priority used for all VideoStreams. 177 double bitrate_priority; 178 179 // The simulcast layer's configurations set by the application for this video 180 // sender. These are modified by the video_stream_factory before being passed 181 // down to lower layers for the video encoding. 182 // `simulcast_layers` is also used for configuring non-simulcast (when there 183 // is a single VideoStream). 184 std::vector<VideoStream> simulcast_layers; 185 186 // Max number of encoded VideoStreams to produce. 187 size_t number_of_streams; 188 189 // Legacy Google conference mode flag for simulcast screenshare 190 bool legacy_conference_mode; 191 192 // Indicates whether quality scaling can be used or not. 193 bool is_quality_scaling_allowed; 194 195 // Maximum Quantization Parameter. 196 // This value is fed into EncoderStreamFactory that 197 // apply it to all simulcast layers/spatial layers. 198 int max_qp; 199 200 private: 201 // Access to the copy constructor is private to force use of the Copy() 202 // method for those exceptional cases where we do use it. 203 VideoEncoderConfig(const VideoEncoderConfig&); 204 }; 205 206 } // namespace webrtc 207 208 #endif // VIDEO_CONFIG_VIDEO_ENCODER_CONFIG_H_ 209