blob: ef8db100a32a205bbd9d6fe79b25342f930c4e05 [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
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010027// 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.
49 // |width| and |height| values are already scaled down.
50 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
Rasmus Brandt43bfe0b2020-01-21 13:54:11 +010067 // If this stream is enabled by the user, or not.
Niels Möller0a8f4352018-05-18 11:37:23 +020068 bool active;
69};
70
71class 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
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020076 // and use absl::optional for encoder_specific_settings instead.
Niels Möller0a8f4352018-05-18 11:37:23 +020077 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:
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:
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.
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.
Åsa Perssonbdee46d2018-06-25 11:28:06 +0200172 // |simulcast_layers| is also used for configuring non-simulcast (when there
173 // is a single VideoStream).
Niels Möller0a8f4352018-05-18 11:37:23 +0200174 std::vector<VideoStream> simulcast_layers;
175
176 // Max number of encoded VideoStreams to produce.
177 size_t number_of_streams;
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_