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