decoder/plugin: add "suffixes" function

For decoder plugins like FFmpeg where the supported codecs cannot be
determined at compile time.
This commit is contained in:
Max Kellermann 2023-03-12 08:55:16 +01:00
parent 8b77da545d
commit 78a9c17bc6
4 changed files with 25 additions and 2 deletions

View File

@ -147,6 +147,10 @@ static void version()
for (; *suffixes != nullptr; ++suffixes) for (; *suffixes != nullptr; ++suffixes)
fmt::print(" {}", *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) if (plugin.protocols != nullptr)
for (const auto &i : plugin.protocols()) for (const auto &i : plugin.protocols())
fmt::print(" {}", i); fmt::print(" {}", i);

View File

@ -23,8 +23,10 @@ DecoderPlugin::SupportsUri(const char *uri) const noexcept
bool bool
DecoderPlugin::SupportsSuffix(std::string_view suffix) const noexcept DecoderPlugin::SupportsSuffix(std::string_view suffix) const noexcept
{ {
return suffixes != nullptr && return (suffixes != nullptr &&
StringArrayContainsCase(suffixes, suffix); StringArrayContainsCase(suffixes, suffix)) ||
(suffixes_function != nullptr &&
suffixes_function().contains(suffix));
} }
bool bool

View File

@ -35,6 +35,13 @@ struct DecoderPlugin {
*/ */
void (*finish)() noexcept = nullptr; 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<std::string, std::less<>> (*suffixes_function)() noexcept = nullptr;
/** /**
* Return a set of supported protocols. * Return a set of supported protocols.
*/ */
@ -152,6 +159,12 @@ struct DecoderPlugin {
return copy; return copy;
} }
constexpr auto WithSuffixes(std::set<std::string, std::less<>> (*_suffixes)() noexcept) const noexcept {
auto copy = *this;
copy.suffixes_function = _suffixes;
return copy;
}
constexpr auto WithMimeTypes(const char *const*_mime_types) const noexcept { constexpr auto WithMimeTypes(const char *const*_mime_types) const noexcept {
auto copy = *this; auto copy = *this;
copy.mime_types = _mime_types; copy.mime_types = _mime_types;

View File

@ -25,6 +25,10 @@ decoder_plugin_print(Response &r,
for (p = plugin.suffixes; *p != nullptr; ++p) for (p = plugin.suffixes; *p != nullptr; ++p)
r.Fmt(FMT_STRING("suffix: {}\n"), *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) if (plugin.mime_types != nullptr)
for (p = plugin.mime_types; *p != nullptr; ++p) for (p = plugin.mime_types; *p != nullptr; ++p)
r.Fmt(FMT_STRING("mime_type: {}\n"), *p); r.Fmt(FMT_STRING("mime_type: {}\n"), *p);