video_decoder_factory_template.h (3776B)
1 /* 2 * Copyright (c) 2022 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_DECODER_FACTORY_TEMPLATE_H_ 12 #define API_VIDEO_CODECS_VIDEO_DECODER_FACTORY_TEMPLATE_H_ 13 14 #include <memory> 15 #include <type_traits> 16 #include <vector> 17 18 #include "absl/algorithm/container.h" 19 #include "api/array_view.h" 20 #include "api/environment/environment.h" 21 #include "api/video_codecs/sdp_video_format.h" 22 #include "api/video_codecs/video_decoder.h" 23 #include "api/video_codecs/video_decoder_factory.h" 24 25 namespace webrtc { 26 // The VideoDecoderFactoryTemplate supports decoder implementations given as 27 // template arguments. 28 // 29 // To include a decoder in the factory it requires two static members 30 // functions to be defined: 31 // 32 // // Returns the supported SdpVideoFormats this decoder can decode. 33 // static std::vector<SdpVideoFormat> SupportedFormats(); 34 // 35 // // Creates a decoder instance for the given format. 36 // static std::unique_ptr<VideoDecoder> 37 // CreateDecoder(const Environment& env, 38 // const SdpVideoFormat& format); 39 // 40 // Note that the order of the template arguments matter as the factory will 41 // return the first decoder implementation supporting the given SdpVideoFormat. 42 template <typename... Ts> 43 class VideoDecoderFactoryTemplate : public VideoDecoderFactory { 44 public: 45 std::vector<SdpVideoFormat> GetSupportedFormats() const override { 46 return GetSupportedFormatsInternal<Ts...>(); 47 } 48 49 std::unique_ptr<VideoDecoder> Create(const Environment& env, 50 const SdpVideoFormat& format) override { 51 return CreateVideoDecoderInternal<Ts...>(env, format); 52 } 53 54 private: 55 bool IsFormatInList(const SdpVideoFormat& format, 56 ArrayView<const SdpVideoFormat> supported_formats) const { 57 return absl::c_any_of( 58 supported_formats, [&](const SdpVideoFormat& supported_format) { 59 return supported_format.name == format.name && 60 supported_format.parameters == format.parameters; 61 }); 62 } 63 64 template <typename V, typename... Vs> 65 std::vector<SdpVideoFormat> GetSupportedFormatsInternal() const { 66 auto supported_formats = V::SupportedFormats(); 67 68 if constexpr (sizeof...(Vs) > 0) { 69 // Supported formats may overlap between implementations, so duplicates 70 // should be filtered out. 71 for (const auto& other_format : GetSupportedFormatsInternal<Vs...>()) { 72 if (!IsFormatInList(other_format, supported_formats)) { 73 supported_formats.push_back(other_format); 74 } 75 } 76 } 77 78 return supported_formats; 79 } 80 81 template <typename V, typename... Vs> 82 std::unique_ptr<VideoDecoder> CreateVideoDecoderInternal( 83 const Environment& env, 84 const SdpVideoFormat& format) { 85 if (IsFormatInList(format, V::SupportedFormats())) { 86 if constexpr (std::is_invocable_r_v<std::unique_ptr<VideoDecoder>, 87 decltype(V::CreateDecoder), 88 const Environment&, 89 const SdpVideoFormat&>) { 90 return V::CreateDecoder(env, format); 91 } else { 92 return V::CreateDecoder(format); 93 } 94 } 95 96 if constexpr (sizeof...(Vs) > 0) { 97 return CreateVideoDecoderInternal<Vs...>(env, format); 98 } 99 100 return nullptr; 101 } 102 }; 103 104 } // namespace webrtc 105 106 #endif // API_VIDEO_CODECS_VIDEO_DECODER_FACTORY_TEMPLATE_H_