From 78a9c17bc6d7c6c5099a30de77636a551fd52362 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 12 Mar 2023 08:55:16 +0100 Subject: [PATCH] decoder/plugin: add "suffixes" function For decoder plugins like FFmpeg where the supported codecs cannot be determined at compile time. --- src/CommandLine.cxx | 4 ++++ src/decoder/DecoderPlugin.cxx | 6 ++++-- src/decoder/DecoderPlugin.hxx | 13 +++++++++++++ src/decoder/DecoderPrint.cxx | 4 ++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index 91601fb37..49298e593 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -147,6 +147,10 @@ static void version() for (; *suffixes != nullptr; ++suffixes) fmt::print(" {}", *suffixes); + if (plugin.suffixes_function != nullptr) + for (const auto &i : plugin.suffixes_function()) + printf(" %s", i.c_str()); + if (plugin.protocols != nullptr) for (const auto &i : plugin.protocols()) fmt::print(" {}", i); diff --git a/src/decoder/DecoderPlugin.cxx b/src/decoder/DecoderPlugin.cxx index 3b54380e9..b96510a00 100644 --- a/src/decoder/DecoderPlugin.cxx +++ b/src/decoder/DecoderPlugin.cxx @@ -23,8 +23,10 @@ DecoderPlugin::SupportsUri(const char *uri) const noexcept bool DecoderPlugin::SupportsSuffix(std::string_view suffix) const noexcept { - return suffixes != nullptr && - StringArrayContainsCase(suffixes, suffix); + return (suffixes != nullptr && + StringArrayContainsCase(suffixes, suffix)) || + (suffixes_function != nullptr && + suffixes_function().contains(suffix)); } bool diff --git a/src/decoder/DecoderPlugin.hxx b/src/decoder/DecoderPlugin.hxx index 67b838952..b1a1e3ea3 100644 --- a/src/decoder/DecoderPlugin.hxx +++ b/src/decoder/DecoderPlugin.hxx @@ -35,6 +35,13 @@ struct DecoderPlugin { */ void (*finish)() noexcept = nullptr; + /** + * Return a set of supported filename suffixes. Use this + * instead of #suffixes if the supported suffixes can only be + * determined at runtime. + */ + std::set> (*suffixes_function)() noexcept = nullptr; + /** * Return a set of supported protocols. */ @@ -152,6 +159,12 @@ struct DecoderPlugin { return copy; } + constexpr auto WithSuffixes(std::set> (*_suffixes)() noexcept) const noexcept { + auto copy = *this; + copy.suffixes_function = _suffixes; + return copy; + } + constexpr auto WithMimeTypes(const char *const*_mime_types) const noexcept { auto copy = *this; copy.mime_types = _mime_types; diff --git a/src/decoder/DecoderPrint.cxx b/src/decoder/DecoderPrint.cxx index ab96973c8..81f016b21 100644 --- a/src/decoder/DecoderPrint.cxx +++ b/src/decoder/DecoderPrint.cxx @@ -25,6 +25,10 @@ decoder_plugin_print(Response &r, for (p = plugin.suffixes; *p != nullptr; ++p) r.Fmt(FMT_STRING("suffix: {}\n"), *p); + if (plugin.suffixes_function != nullptr) + for (const auto &i : plugin.suffixes_function()) + r.Fmt(FMT_STRING("suffix: {}\n"), i); + if (plugin.mime_types != nullptr) for (p = plugin.mime_types; *p != nullptr; ++p) r.Fmt(FMT_STRING("mime_type: {}\n"), *p);