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