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 | 0bb0881 | 2019-07-11 13:23:16 +0200 | [diff] [blame] | 20 | #include "api/video_codecs/sdp_video_format.h" |
| 21 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | class VideoEncoder; |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 25 | |
| 26 | // A factory that creates VideoEncoders. |
| 27 | // NOTE: This class is still under development and may change without notice. |
| 28 | class VideoEncoderFactory { |
| 29 | public: |
| 30 | // TODO(magjed): Try to get rid of this struct. |
| 31 | struct CodecInfo { |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 32 | // |has_internal_source| is true if encoders created by this factory of the |
| 33 | // given codec will use internal camera sources, meaning that they don't |
| 34 | // require/expect frames to be delivered via webrtc::VideoEncoder::Encode. |
| 35 | // This flag is used as the internal_source parameter to |
| 36 | // webrtc::ViEExternalCodec::RegisterExternalSendCodec. |
Niels Möller | 3592839 | 2020-07-16 12:08:44 +0200 | [diff] [blame] | 37 | bool has_internal_source = false; |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Johannes Kron | c29e1f5 | 2021-04-26 22:18:57 +0200 | [diff] [blame] | 40 | struct CodecSupport { |
| 41 | bool is_supported = false; |
| 42 | bool is_power_efficient = false; |
| 43 | }; |
| 44 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 45 | // An injectable class that is continuously updated with encoding conditions |
| 46 | // and selects the best encoder given those conditions. |
| 47 | class EncoderSelectorInterface { |
| 48 | public: |
| 49 | virtual ~EncoderSelectorInterface() {} |
| 50 | |
| 51 | // Informs the encoder selector about which encoder that is currently being |
| 52 | // used. |
| 53 | virtual void OnCurrentEncoder(const SdpVideoFormat& format) = 0; |
| 54 | |
Mirta Dvornicic | 4f34d78 | 2020-02-26 13:01:19 +0100 | [diff] [blame] | 55 | // Called every time the available bitrate is updated. Should return a |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 56 | // non-empty if an encoder switch should be performed. |
Mirta Dvornicic | 4f34d78 | 2020-02-26 13:01:19 +0100 | [diff] [blame] | 57 | virtual absl::optional<SdpVideoFormat> OnAvailableBitrate( |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 58 | const DataRate& rate) = 0; |
| 59 | |
| 60 | // Called if the currently used encoder reports itself as broken. Should |
| 61 | // return a non-empty if an encoder switch should be performed. |
| 62 | virtual absl::optional<SdpVideoFormat> OnEncoderBroken() = 0; |
| 63 | }; |
| 64 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 65 | // Returns a list of supported video formats in order of preference, to use |
| 66 | // for signaling etc. |
| 67 | virtual std::vector<SdpVideoFormat> GetSupportedFormats() const = 0; |
| 68 | |
philipel | 0bb0881 | 2019-07-11 13:23:16 +0200 | [diff] [blame] | 69 | // Returns a list of supported video formats in order of preference, that can |
| 70 | // also be tagged with additional information to allow the VideoEncoderFactory |
| 71 | // to separate between different implementations when CreateVideoEncoder is |
| 72 | // called. |
| 73 | virtual std::vector<SdpVideoFormat> GetImplementations() const { |
| 74 | return GetSupportedFormats(); |
| 75 | } |
| 76 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 77 | // Returns information about how this format will be encoded. The specified |
| 78 | // format must be one of the supported formats by this factory. |
Niels Möller | 2b781bf | 2020-07-16 14:19:41 +0200 | [diff] [blame] | 79 | |
| 80 | // TODO(magjed): Try to get rid of this method. Since is_hardware_accelerated |
| 81 | // is unused, only factories producing internal source encoders (in itself a |
| 82 | // deprecated feature) needs to override this method. |
| 83 | virtual CodecInfo QueryVideoEncoder(const SdpVideoFormat& format) const { |
| 84 | return CodecInfo(); |
| 85 | } |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 86 | |
Johannes Kron | c29e1f5 | 2021-04-26 22:18:57 +0200 | [diff] [blame] | 87 | // Query whether the specifed format is supported or not and if it will be |
| 88 | // power efficient, which is currently interpreted as if there is support for |
| 89 | // hardware acceleration. |
| 90 | // See https://w3c.github.io/webrtc-svc/#scalabilitymodes* for a specification |
| 91 | // of valid values for |scalability_mode|. |
| 92 | // NOTE: QueryCodecSupport is currently an experimental feature that is |
| 93 | // subject to change without notice. |
| 94 | virtual CodecSupport QueryCodecSupport( |
| 95 | const SdpVideoFormat& format, |
| 96 | absl::optional<std::string> scalability_mode) const { |
| 97 | // Default implementation, query for supported formats and check if the |
| 98 | // specified format is supported. Returns false if scalability_mode is |
| 99 | // specified. |
| 100 | CodecSupport codec_support; |
| 101 | if (!scalability_mode) { |
| 102 | codec_support.is_supported = format.IsCodecInList(GetSupportedFormats()); |
| 103 | } |
| 104 | return codec_support; |
| 105 | } |
| 106 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 107 | // Creates a VideoEncoder for the specified format. |
| 108 | virtual std::unique_ptr<VideoEncoder> CreateVideoEncoder( |
| 109 | const SdpVideoFormat& format) = 0; |
| 110 | |
philipel | 9b05803 | 2020-02-10 11:30:00 +0100 | [diff] [blame] | 111 | virtual std::unique_ptr<EncoderSelectorInterface> GetEncoderSelector() const { |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
Magnus Jedvert | d4b0c05 | 2017-09-14 10:24:54 +0200 | [diff] [blame] | 115 | virtual ~VideoEncoderFactory() {} |
| 116 | }; |
| 117 | |
| 118 | } // namespace webrtc |
| 119 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 120 | #endif // API_VIDEO_CODECS_VIDEO_ENCODER_FACTORY_H_ |