blob: 0c69b9328858e76deb055222bd4dae8a12422d7d [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>
Niels Möller0a8f4352018-05-18 11:37:23 +020015#include <string>
16#include <vector>
17
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020018#include "absl/types/optional.h"
Mirko Bonadeid9708072019-01-25 20:26:48 +010019#include "api/scoped_refptr.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020020#include "api/video_codecs/sdp_video_format.h"
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020021#include "api/video_codecs/video_codec.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ref_count.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020023
24namespace webrtc {
25
26struct VideoStream {
27 VideoStream();
28 ~VideoStream();
29 VideoStream(const VideoStream& other);
30 std::string ToString() const;
31
Danil Chapovalov350531e2018-06-08 11:04:04 +000032 size_t width;
33 size_t height;
Niels Möller0a8f4352018-05-18 11:37:23 +020034 int max_framerate;
35
36 int min_bitrate_bps;
37 int target_bitrate_bps;
38 int max_bitrate_bps;
Florent Castellic1a0bcb2019-01-29 14:26:48 +010039 // Scaling factor applied to the stream size.
40 // |width| and |height| values are already scaled down.
41 double scale_resolution_down_by;
Niels Möller0a8f4352018-05-18 11:37:23 +020042 int max_qp;
43
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020044 absl::optional<size_t> num_temporal_layers;
Niels Möller0a8f4352018-05-18 11:37:23 +020045
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020046 absl::optional<double> bitrate_priority;
Niels Möller0a8f4352018-05-18 11:37:23 +020047
Niels Möller0a8f4352018-05-18 11:37:23 +020048 bool active;
49};
50
51class VideoEncoderConfig {
52 public:
53 // These are reference counted to permit copying VideoEncoderConfig and be
54 // kept alive until all encoder_specific_settings go out of scope.
55 // TODO(kthelgason): Consider removing the need for copying VideoEncoderConfig
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020056 // and use absl::optional for encoder_specific_settings instead.
Niels Möller0a8f4352018-05-18 11:37:23 +020057 class EncoderSpecificSettings : public rtc::RefCountInterface {
58 public:
59 // TODO(pbos): Remove FillEncoderSpecificSettings as soon as VideoCodec is
60 // not in use and encoder implementations ask for codec-specific structs
61 // directly.
62 void FillEncoderSpecificSettings(VideoCodec* codec_struct) const;
63
64 virtual void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const;
65 virtual void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const;
66 virtual void FillVideoCodecH264(VideoCodecH264* h264_settings) const;
67
68 private:
69 ~EncoderSpecificSettings() override {}
70 friend class VideoEncoderConfig;
71 };
72
73 class H264EncoderSpecificSettings : public EncoderSpecificSettings {
74 public:
75 explicit H264EncoderSpecificSettings(const VideoCodecH264& specifics);
76 void FillVideoCodecH264(VideoCodecH264* h264_settings) const override;
77
78 private:
79 VideoCodecH264 specifics_;
80 };
81
82 class Vp8EncoderSpecificSettings : public EncoderSpecificSettings {
83 public:
84 explicit Vp8EncoderSpecificSettings(const VideoCodecVP8& specifics);
85 void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const override;
86
87 private:
88 VideoCodecVP8 specifics_;
89 };
90
91 class Vp9EncoderSpecificSettings : public EncoderSpecificSettings {
92 public:
93 explicit Vp9EncoderSpecificSettings(const VideoCodecVP9& specifics);
94 void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const override;
95
96 private:
97 VideoCodecVP9 specifics_;
98 };
99
100 enum class ContentType {
101 kRealtimeVideo,
102 kScreen,
103 };
104
105 class VideoStreamFactoryInterface : public rtc::RefCountInterface {
106 public:
107 // An implementation should return a std::vector<VideoStream> with the
108 // wanted VideoStream settings for the given video resolution.
109 // The size of the vector may not be larger than
110 // |encoder_config.number_of_streams|.
111 virtual std::vector<VideoStream> CreateEncoderStreams(
112 int width,
113 int height,
114 const VideoEncoderConfig& encoder_config) = 0;
115
116 protected:
117 ~VideoStreamFactoryInterface() override {}
118 };
119
120 VideoEncoderConfig& operator=(VideoEncoderConfig&&) = default;
121 VideoEncoderConfig& operator=(const VideoEncoderConfig&) = delete;
122
123 // Mostly used by tests. Avoid creating copies if you can.
124 VideoEncoderConfig Copy() const { return VideoEncoderConfig(*this); }
125
126 VideoEncoderConfig();
127 VideoEncoderConfig(VideoEncoderConfig&&);
128 ~VideoEncoderConfig();
129 std::string ToString() const;
130
131 // TODO(nisse): Consolidate on one of these.
132 VideoCodecType codec_type;
133 SdpVideoFormat video_format;
134
135 rtc::scoped_refptr<VideoStreamFactoryInterface> video_stream_factory;
136 std::vector<SpatialLayer> spatial_layers;
137 ContentType content_type;
138 rtc::scoped_refptr<const EncoderSpecificSettings> encoder_specific_settings;
139
140 // Padding will be used up to this bitrate regardless of the bitrate produced
141 // by the encoder. Padding above what's actually produced by the encoder helps
142 // maintaining a higher bitrate estimate. Padding will however not be sent
143 // unless the estimated bandwidth indicates that the link can handle it.
144 int min_transmit_bitrate_bps;
145 int max_bitrate_bps;
146 // The bitrate priority used for all VideoStreams.
147 double bitrate_priority;
148
149 // The simulcast layer's configurations set by the application for this video
150 // sender. These are modified by the video_stream_factory before being passed
151 // down to lower layers for the video encoding.
Åsa Perssonbdee46d2018-06-25 11:28:06 +0200152 // |simulcast_layers| is also used for configuring non-simulcast (when there
153 // is a single VideoStream).
Niels Möller0a8f4352018-05-18 11:37:23 +0200154 std::vector<VideoStream> simulcast_layers;
155
156 // Max number of encoded VideoStreams to produce.
157 size_t number_of_streams;
158
159 private:
160 // Access to the copy constructor is private to force use of the Copy()
161 // method for those exceptional cases where we do use it.
162 VideoEncoderConfig(const VideoEncoderConfig&);
163};
164
165} // namespace webrtc
166
167#endif // API_VIDEO_CODECS_VIDEO_ENCODER_CONFIG_H_