blob: 86d89d53da5d9c006a3cdffe46e2287301253a89 [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öller79d566b2022-04-29 11:03:13 +020021#include "api/video_codecs/scalability_mode.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020022#include "api/video_codecs/sdp_video_format.h"
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020023#include "api/video_codecs/video_codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/ref_count.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020025
26namespace webrtc {
27
Artem Titov0e61fdd2021-07-25 21:50:14 +020028// The `VideoStream` struct describes a simulcast layer, or "stream".
Niels Möller0a8f4352018-05-18 11:37:23 +020029struct VideoStream {
30 VideoStream();
31 ~VideoStream();
32 VideoStream(const VideoStream& other);
33 std::string ToString() const;
34
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010035 // Width in pixels.
Danil Chapovalov350531e2018-06-08 11:04:04 +000036 size_t width;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010037
38 // Height in pixels.
Danil Chapovalov350531e2018-06-08 11:04:04 +000039 size_t height;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010040
41 // Frame rate in fps.
Niels Möller0a8f4352018-05-18 11:37:23 +020042 int max_framerate;
43
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010044 // Bitrate, in bps, for the stream.
Niels Möller0a8f4352018-05-18 11:37:23 +020045 int min_bitrate_bps;
46 int target_bitrate_bps;
47 int max_bitrate_bps;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010048
Florent Castellic1a0bcb2019-01-29 14:26:48 +010049 // Scaling factor applied to the stream size.
Artem Titov0e61fdd2021-07-25 21:50:14 +020050 // `width` and `height` values are already scaled down.
Florent Castellic1a0bcb2019-01-29 14:26:48 +010051 double scale_resolution_down_by;
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010052
53 // Maximum Quantization Parameter to use when encoding the stream.
Niels Möller0a8f4352018-05-18 11:37:23 +020054 int max_qp;
55
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010056 // Determines the number of temporal layers that the stream should be
57 // encoded with. This value should be greater than zero.
58 // TODO(brandtr): This class is used both for configuring the encoder
59 // (meaning that this field _must_ be set), and for signaling the app-level
60 // encoder settings (meaning that the field _may_ be set). We should separate
61 // this and remove this optional instead.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020062 absl::optional<size_t> num_temporal_layers;
Niels Möller0a8f4352018-05-18 11:37:23 +020063
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010064 // The priority of this stream, to be used when allocating resources
65 // between multiple streams.
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020066 absl::optional<double> bitrate_priority;
Niels Möller0a8f4352018-05-18 11:37:23 +020067
Niels Möller79d566b2022-04-29 11:03:13 +020068 absl::optional<ScalabilityMode> scalability_mode;
philipel87e99092020-11-18 11:52:04 +010069
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010070 // If this stream is enabled by the user, or not.
Niels Möller0a8f4352018-05-18 11:37:23 +020071 bool active;
72};
73
74class VideoEncoderConfig {
75 public:
76 // These are reference counted to permit copying VideoEncoderConfig and be
77 // kept alive until all encoder_specific_settings go out of scope.
78 // TODO(kthelgason): Consider removing the need for copying VideoEncoderConfig
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020079 // and use absl::optional for encoder_specific_settings instead.
Niels Möller0a8f4352018-05-18 11:37:23 +020080 class EncoderSpecificSettings : public rtc::RefCountInterface {
81 public:
82 // TODO(pbos): Remove FillEncoderSpecificSettings as soon as VideoCodec is
83 // not in use and encoder implementations ask for codec-specific structs
84 // directly.
85 void FillEncoderSpecificSettings(VideoCodec* codec_struct) const;
86
87 virtual void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const;
88 virtual void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const;
Niels Möller0a8f4352018-05-18 11:37:23 +020089
90 private:
91 ~EncoderSpecificSettings() override {}
92 friend class VideoEncoderConfig;
93 };
94
Niels Möller0a8f4352018-05-18 11:37:23 +020095 class Vp8EncoderSpecificSettings : public EncoderSpecificSettings {
96 public:
97 explicit Vp8EncoderSpecificSettings(const VideoCodecVP8& specifics);
98 void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const override;
99
100 private:
101 VideoCodecVP8 specifics_;
102 };
103
104 class Vp9EncoderSpecificSettings : public EncoderSpecificSettings {
105 public:
106 explicit Vp9EncoderSpecificSettings(const VideoCodecVP9& specifics);
107 void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const override;
108
109 private:
110 VideoCodecVP9 specifics_;
111 };
112
113 enum class ContentType {
114 kRealtimeVideo,
115 kScreen,
116 };
117
118 class VideoStreamFactoryInterface : public rtc::RefCountInterface {
119 public:
120 // An implementation should return a std::vector<VideoStream> with the
121 // wanted VideoStream settings for the given video resolution.
122 // The size of the vector may not be larger than
Artem Titovcfea2182021-08-10 01:22:31 +0200123 // `encoder_config.number_of_streams`.
Niels Möller0a8f4352018-05-18 11:37:23 +0200124 virtual std::vector<VideoStream> CreateEncoderStreams(
125 int width,
126 int height,
127 const VideoEncoderConfig& encoder_config) = 0;
128
129 protected:
130 ~VideoStreamFactoryInterface() override {}
131 };
132
133 VideoEncoderConfig& operator=(VideoEncoderConfig&&) = default;
134 VideoEncoderConfig& operator=(const VideoEncoderConfig&) = delete;
135
136 // Mostly used by tests. Avoid creating copies if you can.
137 VideoEncoderConfig Copy() const { return VideoEncoderConfig(*this); }
138
139 VideoEncoderConfig();
140 VideoEncoderConfig(VideoEncoderConfig&&);
141 ~VideoEncoderConfig();
142 std::string ToString() const;
143
Niels Möller6939f632022-07-05 08:55:19 +0200144 // TODO(bugs.webrtc.org/6883): Consolidate on one of these.
Niels Möller0a8f4352018-05-18 11:37:23 +0200145 VideoCodecType codec_type;
146 SdpVideoFormat video_format;
147
148 rtc::scoped_refptr<VideoStreamFactoryInterface> video_stream_factory;
149 std::vector<SpatialLayer> spatial_layers;
150 ContentType content_type;
Niels Möller807328f2022-05-12 16:16:39 +0200151 bool frame_drop_enabled;
Niels Möller0a8f4352018-05-18 11:37:23 +0200152 rtc::scoped_refptr<const EncoderSpecificSettings> encoder_specific_settings;
153
154 // Padding will be used up to this bitrate regardless of the bitrate produced
155 // by the encoder. Padding above what's actually produced by the encoder helps
156 // maintaining a higher bitrate estimate. Padding will however not be sent
157 // unless the estimated bandwidth indicates that the link can handle it.
158 int min_transmit_bitrate_bps;
159 int max_bitrate_bps;
160 // The bitrate priority used for all VideoStreams.
161 double bitrate_priority;
162
163 // The simulcast layer's configurations set by the application for this video
164 // sender. These are modified by the video_stream_factory before being passed
165 // down to lower layers for the video encoding.
Artem Titov0e61fdd2021-07-25 21:50:14 +0200166 // `simulcast_layers` is also used for configuring non-simulcast (when there
Åsa Perssonbdee46d2018-06-25 11:28:06 +0200167 // is a single VideoStream).
Niels Möller0a8f4352018-05-18 11:37:23 +0200168 std::vector<VideoStream> simulcast_layers;
169
170 // Max number of encoded VideoStreams to produce.
171 size_t number_of_streams;
172
Florent Castellid3511012020-08-04 11:40:23 +0200173 // Legacy Google conference mode flag for simulcast screenshare
174 bool legacy_conference_mode;
175
Sergey Silkind19e3b92021-03-16 10:05:30 +0000176 // Indicates whether quality scaling can be used or not.
177 bool is_quality_scaling_allowed;
178
Niels Möller0a8f4352018-05-18 11:37:23 +0200179 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_