blob: 5440f1f43517db891b0b233860ab88726c9ea6e3 [file] [log] [blame]
Niels Möller0a8f4352018-05-18 11:37:23 +02001/*
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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Niels Möller0a8f4352018-05-18 11:37:23 +020016#include <string>
17#include <vector>
18
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020019#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010020#include "api/scoped_refptr.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020021#include "api/video_codecs/sdp_video_format.h"
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020022#include "api/video_codecs/video_codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/ref_count.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020024
25namespace webrtc {
26
Artem Titov0e61fdd2021-07-25 21:50:14 +020027// The `VideoStream` struct describes a simulcast layer, or "stream".
Niels Möller0a8f4352018-05-18 11:37:23 +020028struct VideoStream {
29 VideoStream();
30 ~VideoStream();
31 VideoStream(const VideoStream& other);
32 std::string ToString() const;
33
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010034 // Width in pixels.
Danil Chapovalov350531e2018-06-08 11:04:04 +000035 size_t width;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010036
37 // Height in pixels.
Danil Chapovalov350531e2018-06-08 11:04:04 +000038 size_t height;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010039
40 // Frame rate in fps.
Niels Möller0a8f4352018-05-18 11:37:23 +020041 int max_framerate;
42
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010043 // Bitrate, in bps, for the stream.
Niels Möller0a8f4352018-05-18 11:37:23 +020044 int min_bitrate_bps;
45 int target_bitrate_bps;
46 int max_bitrate_bps;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010047
Florent Castellic1a0bcb2019-01-29 14:26:48 +010048 // Scaling factor applied to the stream size.
Artem Titov0e61fdd2021-07-25 21:50:14 +020049 // `width` and `height` values are already scaled down.
Florent Castellic1a0bcb2019-01-29 14:26:48 +010050 double scale_resolution_down_by;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010051
52 // Maximum Quantization Parameter to use when encoding the stream.
Niels Möller0a8f4352018-05-18 11:37:23 +020053 int max_qp;
54
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010055 // 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.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020061 absl::optional<size_t> num_temporal_layers;
Niels Möller0a8f4352018-05-18 11:37:23 +020062
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010063 // The priority of this stream, to be used when allocating resources
64 // between multiple streams.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020065 absl::optional<double> bitrate_priority;
Niels Möller0a8f4352018-05-18 11:37:23 +020066
philipel87e99092020-11-18 11:52:04 +010067 absl::optional<std::string> scalability_mode;
68
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010069 // If this stream is enabled by the user, or not.
Niels Möller0a8f4352018-05-18 11:37:23 +020070 bool active;
71};
72
73class VideoEncoderConfig {
74 public:
75 // These are reference counted to permit copying VideoEncoderConfig and be
76 // kept alive until all encoder_specific_settings go out of scope.
77 // TODO(kthelgason): Consider removing the need for copying VideoEncoderConfig
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020078 // and use absl::optional for encoder_specific_settings instead.
Niels Möller0a8f4352018-05-18 11:37:23 +020079 class EncoderSpecificSettings : public rtc::RefCountInterface {
80 public:
81 // TODO(pbos): Remove FillEncoderSpecificSettings as soon as VideoCodec is
82 // not in use and encoder implementations ask for codec-specific structs
83 // directly.
84 void FillEncoderSpecificSettings(VideoCodec* codec_struct) const;
85
86 virtual void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const;
87 virtual void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const;
88 virtual void FillVideoCodecH264(VideoCodecH264* h264_settings) const;
89
90 private:
91 ~EncoderSpecificSettings() override {}
92 friend class VideoEncoderConfig;
93 };
94
95 class H264EncoderSpecificSettings : public EncoderSpecificSettings {
96 public:
97 explicit H264EncoderSpecificSettings(const VideoCodecH264& specifics);
98 void FillVideoCodecH264(VideoCodecH264* h264_settings) const override;
99
100 private:
101 VideoCodecH264 specifics_;
102 };
103
104 class Vp8EncoderSpecificSettings : public EncoderSpecificSettings {
105 public:
106 explicit Vp8EncoderSpecificSettings(const VideoCodecVP8& specifics);
107 void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const override;
108
109 private:
110 VideoCodecVP8 specifics_;
111 };
112
113 class Vp9EncoderSpecificSettings : public EncoderSpecificSettings {
114 public:
115 explicit Vp9EncoderSpecificSettings(const VideoCodecVP9& specifics);
116 void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const override;
117
118 private:
119 VideoCodecVP9 specifics_;
120 };
121
122 enum class ContentType {
123 kRealtimeVideo,
124 kScreen,
125 };
126
127 class VideoStreamFactoryInterface : public rtc::RefCountInterface {
128 public:
129 // An implementation should return a std::vector<VideoStream> with the
130 // wanted VideoStream settings for the given video resolution.
131 // The size of the vector may not be larger than
132 // |encoder_config.number_of_streams|.
133 virtual std::vector<VideoStream> CreateEncoderStreams(
134 int width,
135 int height,
136 const VideoEncoderConfig& encoder_config) = 0;
137
138 protected:
139 ~VideoStreamFactoryInterface() override {}
140 };
141
142 VideoEncoderConfig& operator=(VideoEncoderConfig&&) = default;
143 VideoEncoderConfig& operator=(const VideoEncoderConfig&) = delete;
144
145 // Mostly used by tests. Avoid creating copies if you can.
146 VideoEncoderConfig Copy() const { return VideoEncoderConfig(*this); }
147
148 VideoEncoderConfig();
149 VideoEncoderConfig(VideoEncoderConfig&&);
150 ~VideoEncoderConfig();
151 std::string ToString() const;
152
153 // TODO(nisse): Consolidate on one of these.
154 VideoCodecType codec_type;
155 SdpVideoFormat video_format;
156
157 rtc::scoped_refptr<VideoStreamFactoryInterface> video_stream_factory;
158 std::vector<SpatialLayer> spatial_layers;
159 ContentType content_type;
160 rtc::scoped_refptr<const EncoderSpecificSettings> encoder_specific_settings;
161
162 // Padding will be used up to this bitrate regardless of the bitrate produced
163 // by the encoder. Padding above what's actually produced by the encoder helps
164 // maintaining a higher bitrate estimate. Padding will however not be sent
165 // unless the estimated bandwidth indicates that the link can handle it.
166 int min_transmit_bitrate_bps;
167 int max_bitrate_bps;
168 // The bitrate priority used for all VideoStreams.
169 double bitrate_priority;
170
171 // The simulcast layer's configurations set by the application for this video
172 // sender. These are modified by the video_stream_factory before being passed
173 // down to lower layers for the video encoding.
Artem Titov0e61fdd2021-07-25 21:50:14 +0200174 // `simulcast_layers` is also used for configuring non-simulcast (when there
Åsa Perssonbdee46d2018-06-25 11:28:06 +0200175 // is a single VideoStream).
Niels Möller0a8f4352018-05-18 11:37:23 +0200176 std::vector<VideoStream> simulcast_layers;
177
178 // Max number of encoded VideoStreams to produce.
179 size_t number_of_streams;
180
Florent Castellid3511012020-08-04 11:40:23 +0200181 // Legacy Google conference mode flag for simulcast screenshare
182 bool legacy_conference_mode;
183
Sergey Silkind19e3b92021-03-16 10:05:30 +0000184 // Indicates whether quality scaling can be used or not.
185 bool is_quality_scaling_allowed;
186
Niels Möller0a8f4352018-05-18 11:37:23 +0200187 private:
188 // Access to the copy constructor is private to force use of the Copy()
189 // method for those exceptional cases where we do use it.
190 VideoEncoderConfig(const VideoEncoderConfig&);
191};
192
193} // namespace webrtc
194
195#endif // API_VIDEO_CODECS_VIDEO_ENCODER_CONFIG_H_