blob: d28a2a40359da183bca3a4291a626ce8e0f173d2 [file] [log] [blame]
Magnus Jedvertd4b0c052017-09-14 10:24:54 +02001/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_
12#define API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020013
14#include <memory>
Johannes Kronc29e1f52021-04-26 22:18:57 +020015#include <string>
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020016#include <vector>
17
philipel9b058032020-02-10 11:30:00 +010018#include "absl/types/optional.h"
19#include "api/units/data_rate.h"
philipel6daa3042022-04-11 10:48:28 +020020#include "api/video/render_resolution.h"
philipel0bb08812019-07-11 13:23:16 +020021#include "api/video_codecs/sdp_video_format.h"
22
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020023namespace webrtc {
24
25class VideoEncoder;
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020026
27// A factory that creates VideoEncoders.
28// NOTE: This class is still under development and may change without notice.
29class VideoEncoderFactory {
30 public:
Johannes Kronc29e1f52021-04-26 22:18:57 +020031 struct CodecSupport {
32 bool is_supported = false;
33 bool is_power_efficient = false;
34 };
35
philipel9b058032020-02-10 11:30:00 +010036 // An injectable class that is continuously updated with encoding conditions
Jonas Oreland65455162022-06-08 11:25:46 +020037 // and selects the best encoder given those conditions. An implementation is
38 // typically stateful to avoid toggling between different encoders, which is
39 // costly due to recreation of objects, a new codec will always start with a
40 // key-frame.
philipel9b058032020-02-10 11:30:00 +010041 class EncoderSelectorInterface {
42 public:
43 virtual ~EncoderSelectorInterface() {}
44
45 // Informs the encoder selector about which encoder that is currently being
46 // used.
47 virtual void OnCurrentEncoder(const SdpVideoFormat& format) = 0;
48
Mirta Dvornicic4f34d782020-02-26 13:01:19 +010049 // Called every time the available bitrate is updated. Should return a
philipel9b058032020-02-10 11:30:00 +010050 // non-empty if an encoder switch should be performed.
Mirta Dvornicic4f34d782020-02-26 13:01:19 +010051 virtual absl::optional<SdpVideoFormat> OnAvailableBitrate(
philipel9b058032020-02-10 11:30:00 +010052 const DataRate& rate) = 0;
53
philipel6daa3042022-04-11 10:48:28 +020054 // Called every time the encoder input resolution change. Should return a
55 // non-empty if an encoder switch should be performed.
56 virtual absl::optional<SdpVideoFormat> OnResolutionChange(
57 const RenderResolution& resolution) {
58 return absl::nullopt;
59 }
60
philipel9b058032020-02-10 11:30:00 +010061 // Called if the currently used encoder reports itself as broken. Should
62 // return a non-empty if an encoder switch should be performed.
63 virtual absl::optional<SdpVideoFormat> OnEncoderBroken() = 0;
64 };
65
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020066 // Returns a list of supported video formats in order of preference, to use
67 // for signaling etc.
68 virtual std::vector<SdpVideoFormat> GetSupportedFormats() const = 0;
69
philipel0bb08812019-07-11 13:23:16 +020070 // Returns a list of supported video formats in order of preference, that can
71 // also be tagged with additional information to allow the VideoEncoderFactory
72 // to separate between different implementations when CreateVideoEncoder is
73 // called.
74 virtual std::vector<SdpVideoFormat> GetImplementations() const {
75 return GetSupportedFormats();
76 }
77
Johannes Kronc29e1f52021-04-26 22:18:57 +020078 // Query whether the specifed format is supported or not and if it will be
79 // power efficient, which is currently interpreted as if there is support for
80 // hardware acceleration.
81 // See https://w3c.github.io/webrtc-svc/#scalabilitymodes* for a specification
Artem Titov0e61fdd2021-07-25 21:50:14 +020082 // of valid values for `scalability_mode`.
Johannes Kronc29e1f52021-04-26 22:18:57 +020083 // NOTE: QueryCodecSupport is currently an experimental feature that is
84 // subject to change without notice.
85 virtual CodecSupport QueryCodecSupport(
86 const SdpVideoFormat& format,
87 absl::optional<std::string> scalability_mode) const {
88 // Default implementation, query for supported formats and check if the
89 // specified format is supported. Returns false if scalability_mode is
90 // specified.
91 CodecSupport codec_support;
92 if (!scalability_mode) {
93 codec_support.is_supported = format.IsCodecInList(GetSupportedFormats());
94 }
95 return codec_support;
96 }
97
Magnus Jedvertd4b0c052017-09-14 10:24:54 +020098 // Creates a VideoEncoder for the specified format.
99 virtual std::unique_ptr<VideoEncoder> CreateVideoEncoder(
100 const SdpVideoFormat& format) = 0;
101
Jonas Oreland65455162022-06-08 11:25:46 +0200102 // This method creates a EncoderSelector to use for a VideoSendStream.
103 // (and hence should probably been called CreateEncoderSelector()).
104 //
105 // Note: This method is unsuitable if encoding several streams that
106 // are using same VideoEncoderFactory (either by several streams in one
107 // PeerConnection or streams with different PeerConnection but same
108 // PeerConnectionFactory). This is due to the fact that the method is not
109 // given any stream identifier, nor is the EncoderSelectorInterface given any
110 // stream identifiers, i.e one does not know which stream is being encoded
111 // with help of the selector.
112 //
113 // In such scenario, the `RtpSenderInterface::SetEncoderSelector` is
114 // recommended.
115 //
116 // TODO(bugs.webrtc.org:14122): Deprecate and remove in favor of
117 // `RtpSenderInterface::SetEncoderSelector`.
philipel9b058032020-02-10 11:30:00 +0100118 virtual std::unique_ptr<EncoderSelectorInterface> GetEncoderSelector() const {
119 return nullptr;
120 }
121
Magnus Jedvertd4b0c052017-09-14 10:24:54 +0200122 virtual ~VideoEncoderFactory() {}
123};
124
125} // namespace webrtc
126
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200127#endif // API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_