Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_ |
| 12 | #define API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_ |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
Johannes Kron | c29e1f5 | 2021-04-26 22:18:57 +0200 | [diff] [blame] | 15 | #include <string> |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 18 | #include "absl/types/optional.h" |
| 19 | #include "api/units/data_rate.h" |
philipel | 6daa304 | 2022-04-11 10:48:28 +0200 | [diff] [blame] | 20 | #include "api/video/render_resolution.h" |
philipel | 0bb0881 | 2019-07-11 13:23:16 +0200 | [diff] [blame] | 21 | #include "api/video_codecs/sdp_video_format.h" |
| 22 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | |
| 25 | class VideoEncoder; |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 26 | |
| 27 | // A factory that creates VideoEncoders. |
| 28 | // NOTE: This class is still under development and may change without notice. |
| 29 | class VideoEncoderFactory { |
| 30 | public: |
Johannes Kron | c29e1f5 | 2021-04-26 22:18:57 +0200 | [diff] [blame] | 31 | struct CodecSupport { |
| 32 | bool is_supported = false; |
| 33 | bool is_power_efficient = false; |
| 34 | }; |
| 35 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 36 | // An injectable class that is continuously updated with encoding conditions |
Jonas Oreland | 6545516 | 2022-06-08 11:25:46 +0200 | [diff] [blame] | 37 | // 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. |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 41 | 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 Dvornicic | 4f34d78 | 2020-02-26 13:01:19 +0100 | [diff] [blame] | 49 | // Called every time the available bitrate is updated. Should return a |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 50 | // non-empty if an encoder switch should be performed. |
Mirta Dvornicic | 4f34d78 | 2020-02-26 13:01:19 +0100 | [diff] [blame] | 51 | virtual absl::optional<SdpVideoFormat> OnAvailableBitrate( |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 52 | const DataRate& rate) = 0; |
| 53 | |
philipel | 6daa304 | 2022-04-11 10:48:28 +0200 | [diff] [blame] | 54 | // 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 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 61 | // 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 Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 66 | // 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 | |
philipel | 0bb0881 | 2019-07-11 13:23:16 +0200 | [diff] [blame] | 70 | // 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 Kron | c29e1f5 | 2021-04-26 22:18:57 +0200 | [diff] [blame] | 78 | // 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 Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 82 | // of valid values for `scalability_mode`. |
Johannes Kron | c29e1f5 | 2021-04-26 22:18:57 +0200 | [diff] [blame] | 83 | // 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 Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 98 | // Creates a VideoEncoder for the specified format. |
| 99 | virtual std::unique_ptr<VideoEncoder> CreateVideoEncoder( |
| 100 | const SdpVideoFormat& format) = 0; |
| 101 | |
Jonas Oreland | 6545516 | 2022-06-08 11:25:46 +0200 | [diff] [blame] | 102 | // 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`. |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 118 | virtual std::unique_ptr<EncoderSelectorInterface> GetEncoderSelector() const { |
| 119 | return nullptr; |
| 120 | } |
| 121 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 122 | virtual ~VideoEncoderFactory() {} |
| 123 | }; |
| 124 | |
| 125 | } // namespace webrtc |
| 126 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 127 | #endif // API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_ |