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