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