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