Truly independent web browser
LibMediaCodecs: Only load the system FFmpeg when a process asks to
Zaggy1024be06bb340878
diff --git a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cpp b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cppindex f49971778cf..114ceffac7e 100644--- a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cpp+++ b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.cpp@@ -7,6 +7,7 @@#include <AK/AnyOf.h>#include <AK/Debug.h>#include <AK/NeverDestroyed.h>+#include <AK/Optional.h>#include <AK/OwnPtr.h>#include <AK/ScopeGuard.h>#include <LibCore/Environment.h>@@ -111,10 +112,19 @@ static OwnPtr<SystemFFmpeg> load_preferred_library()return {};}+static NeverDestroyed<Optional<OwnPtr<SystemFFmpeg>>> s_library;++void SystemFFmpeg::load()+{+ if (!s_library->has_value())+ *s_library = load_preferred_library();+}+SystemFFmpeg const* SystemFFmpeg::the(){- static NeverDestroyed<OwnPtr<SystemFFmpeg> const> library { load_preferred_library() };- return library->ptr();+ if (!s_library->has_value())+ return nullptr;+ return s_library->value().ptr();}SystemFFmpeg::SystemFFmpeg(void* library_handle, NonnullOwnPtr<FFmpegFunctions> functions, unsigned major)diff --git a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.h b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.hindex 05daf27c315..025783a271c 100644--- a/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.h+++ b/Libraries/LibMediaCodecs/FFmpeg/SystemFFmpeg.h@@ -20,6 +20,10 @@ class MEDIACODECS_API SystemFFmpeg {public:AK_ALLOC_WITH_KMALLOC;+ // The library's dependencies make syscalls that sandboxes do not allow, so a process loads it before sandboxing+ // itself. Only the first call loads it.+ static void load();+ // The library that load() found, or null if it found none or the process never loaded it.static SystemFFmpeg const* the();static DecoderErrorOr<NonnullOwnPtr<SystemFFmpeg>> try_load(ByteString const& library_path);diff --git a/Services/MediaServer/main.cpp b/Services/MediaServer/main.cppindex d66d7842897..d835c04a9e7 100644--- a/Services/MediaServer/main.cpp+++ b/Services/MediaServer/main.cpp@@ -60,7 +60,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)#if defined(AK_OS_LINUX)// The system library's dependencies make syscalls the sandbox will not allow, so it loads first.- (void)Media::FFmpeg::SystemFFmpeg::the();+ Media::FFmpeg::SystemFFmpeg::load();#endifMedia::install_video_decoders({ Media::remote_video_decoders(), Media::ffmpeg_video_decoders() });diff --git a/Services/VideoDecoderServer/main.cpp b/Services/VideoDecoderServer/main.cppindex 7bdd6fc6b83..08739f29213 100644--- a/Services/VideoDecoderServer/main.cpp+++ b/Services/VideoDecoderServer/main.cpp@@ -102,7 +102,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)#if defined(USE_VULKAN_DMABUF_IMAGES) && defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)// H.264 and HEVC decode through the system library, whose dependencies make syscalls the sandbox will not allow.if (Media::VulkanVideoDevice::the() != nullptr)- (void)Media::FFmpeg::SystemFFmpeg::the();+ Media::FFmpeg::SystemFFmpeg::load();#endifVector<Media::VideoDecoderRegistration> hosted_decoders;diff --git a/Tests/LibMedia/TestH264Decode.cpp b/Tests/LibMedia/TestH264Decode.cppindex aa889a0af33..a2582014d96 100644--- a/Tests/LibMedia/TestH264Decode.cpp+++ b/Tests/LibMedia/TestH264Decode.cpp@@ -16,6 +16,7 @@// H.264 is left out of the bundled FFmpeg, so these run against whatever system libavcodec decodes it.static Media::FFmpeg::SystemFFmpeg const* h264_library(){+ Media::FFmpeg::SystemFFmpeg::load();auto const* library = Media::FFmpeg::SystemFFmpeg::the();if (library == nullptr || !library->has_decoder(Media::CodecID::H264)) {warnln("No system FFmpeg decodes H.264, skipping");diff --git a/Tests/LibMedia/TestSystemFFmpeg.cpp b/Tests/LibMedia/TestSystemFFmpeg.cppindex 9afebe3eeac..f25e791e46f 100644--- a/Tests/LibMedia/TestSystemFFmpeg.cpp+++ b/Tests/LibMedia/TestSystemFFmpeg.cpp@@ -81,6 +81,7 @@ TEST_CASE(decodes_through_the_loaded_library)// HEVC sits after codecs that newer libavcodec majors removed, so its ID differs from the bundled headers'.TEST_CASE(decodes_hevc_through_the_system_library){+ Media::FFmpeg::SystemFFmpeg::load();auto const* library = Media::FFmpeg::SystemFFmpeg::the();if (library == nullptr || !library->has_decoder(Media::CodecID::H265)) {warnln("No system FFmpeg decodes HEVC, skipping");@@ -144,6 +145,7 @@ static Vector<float> decode_aac_lc_with_configuration(Media::FFmpeg::SystemFFmpe// hls.js 0.12.4 declares SBR at the core's own rate over AAC-LC, which FFmpeg decodes into a distorted core.TEST_CASE(aac_with_spectral_band_replication_at_the_core_sample_rate_decodes_as_its_core){+ Media::FFmpeg::SystemFFmpeg::load();auto const* library = Media::FFmpeg::SystemFFmpeg::the();if (library == nullptr || !library->has_decoder(Media::CodecID::AAC)) {warnln("No system FFmpeg decodes AAC, skipping");