From 38b51b791e082e550e29c4685143a2419ad48244 Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Fri, 17 May 2024 12:39:29 -0400 Subject: [PATCH] AK+Kernel+LibVideo: Include workarounds for missing P0960 only in Xcode With this change, ".*make.*" function family now does error checking earlier, which improves experience while using clangd. Note that the change also make them instantiate classes a bit more eagerly, so in LibVideo/PlaybackManager, we have to first define SeekingStateHandler and only then make() it. Co-Authored-By: stelar7 --- AK/NonnullOwnPtr.h | 8 +- AK/NonnullRefPtr.h | 8 +- AK/Platform.h | 4 + Kernel/Library/LockRefPtr.h | 4 +- .../Libraries/LibVideo/PlaybackManager.cpp | 84 +++++++++---------- 5 files changed, 61 insertions(+), 47 deletions(-) diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 445ebba2f57..e8ab1ab139e 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -150,12 +150,14 @@ requires(IsConstructible) inline NonnullOwnPtr make(Args&&... arg return NonnullOwnPtr(NonnullOwnPtr::Adopt, *new T(forward(args)...)); } -// FIXME: Remove once P0960R3 is available in Clang. +# ifdef AK_COMPILER_APPLE_CLANG +// FIXME: Remove once P0960R3 is available in Apple Clang. template inline NonnullOwnPtr make(Args&&... args) { return NonnullOwnPtr(NonnullOwnPtr::Adopt, *new T { forward(args)... }); } +# endif #endif @@ -174,13 +176,15 @@ requires(IsConstructible) inline ErrorOr> try_make( return adopt_nonnull_own_or_enomem(new (nothrow) T(forward(args)...)); } -// FIXME: Remove once P0960R3 is available in Clang. +#ifdef AK_COMPILER_APPLE_CLANG +// FIXME: Remove once P0960R3 is available in Apple Clang. template inline ErrorOr> try_make(Args&&... args) { return adopt_nonnull_own_or_enomem(new (nothrow) T { forward(args)... }); } +#endif template struct Traits> : public DefaultTraits> { diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 09e0e69d436..24cdd781f04 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -240,12 +240,14 @@ requires(IsConstructible) inline ErrorOr> try_make_ return adopt_nonnull_ref_or_enomem(new (nothrow) T(forward(args)...)); } -// FIXME: Remove once P0960R3 is available in Clang. +#ifdef AK_COMPILER_APPLE_CLANG +// FIXME: Remove once P0960R3 is available in Apple Clang. template inline ErrorOr> try_make_ref_counted(Args&&... args) { return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward(args)... }); } +#endif template struct Formatter> : Formatter { @@ -277,12 +279,14 @@ requires(IsConstructible) inline NonnullRefPtr make_ref_counted(A return NonnullRefPtr(NonnullRefPtr::Adopt, *new T(forward(args)...)); } -// FIXME: Remove once P0960R3 is available in Clang. +#ifdef AK_COMPILER_APPLE_CLANG +// FIXME: Remove once P0960R3 is available in Apple Clang. template inline NonnullRefPtr make_ref_counted(Args&&... args) { return NonnullRefPtr(NonnullRefPtr::Adopt, *new T { forward(args)... }); } +#endif template struct Traits> : public DefaultTraits> { diff --git a/AK/Platform.h b/AK/Platform.h index 75cb77b1ce6..b75aa087e76 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -57,6 +57,10 @@ # define AK_COMPILER_GCC #endif +#if defined(AK_COMPILER_CLANG) && defined(__apple_build_version__) +# define AK_COMPILER_APPLE_CLANG +#endif + #if defined(__GLIBC__) # define AK_LIBC_GLIBC # define AK_LIBC_GLIBC_PREREQ(maj, min) __GLIBC_PREREQ((maj), (min)) diff --git a/Kernel/Library/LockRefPtr.h b/Kernel/Library/LockRefPtr.h index da8b29e40ff..3a66bd8e07e 100644 --- a/Kernel/Library/LockRefPtr.h +++ b/Kernel/Library/LockRefPtr.h @@ -500,12 +500,14 @@ requires(IsConstructible) inline ErrorOr> try_m return adopt_nonnull_lock_ref_or_enomem(new (nothrow) T(forward(args)...)); } -// FIXME: Remove once P0960R3 is available in Clang. +#ifdef AK_COMPILER_APPLE_CLANG +// FIXME: Remove once P0960R3 is available in Apple Clang. template inline ErrorOr> try_make_lock_ref_counted(Args&&... args) { return adopt_nonnull_lock_ref_or_enomem(new (nothrow) T { forward(args)... }); } +#endif template inline ErrorOr> adopt_nonnull_lock_ref_or_enomem(T* object) diff --git a/Userland/Libraries/LibVideo/PlaybackManager.cpp b/Userland/Libraries/LibVideo/PlaybackManager.cpp index 84370c08bd1..031c5bdb0b4 100644 --- a/Userland/Libraries/LibVideo/PlaybackManager.cpp +++ b/Userland/Libraries/LibVideo/PlaybackManager.cpp @@ -43,48 +43,6 @@ DecoderErrorOr> PlaybackManager::from_data(Readon return create(move(demuxer)); } -DecoderErrorOr> PlaybackManager::create(NonnullOwnPtr demuxer) -{ - auto video_tracks = TRY(demuxer->get_tracks_for_type(TrackType::Video)); - if (video_tracks.is_empty()) - return DecoderError::with_description(DecoderErrorCategory::Invalid, "No video track is present"sv); - auto track = video_tracks[0]; - - dbgln_if(PLAYBACK_MANAGER_DEBUG, "Selecting video track number {}", track.identifier()); - - auto codec_id = TRY(demuxer->get_codec_id_for_track(track)); - OwnPtr decoder; - switch (codec_id) { - case CodecID::VP9: - decoder = DECODER_TRY_ALLOC(try_make()); - break; - - default: - return DecoderError::format(DecoderErrorCategory::Invalid, "Unsupported codec: {}", codec_id); - } - auto decoder_non_null = decoder.release_nonnull(); - auto frame_queue = DECODER_TRY_ALLOC(VideoFrameQueue::create()); - auto playback_manager = DECODER_TRY_ALLOC(try_make(demuxer, track, move(decoder_non_null), move(frame_queue))); - - playback_manager->m_state_update_timer = Core::Timer::create_single_shot(0, [&self = *playback_manager] { self.timer_callback(); }); - - playback_manager->m_decode_thread = DECODER_TRY_ALLOC(Threading::Thread::try_create([&self = *playback_manager] { - while (!self.m_stop_decoding.load()) - self.decode_and_queue_one_sample(); - - dbgln_if(PLAYBACK_MANAGER_DEBUG, "Media Decoder thread ended."); - return 0; - }, - "Media Decoder"sv)); - - playback_manager->m_playback_handler = make(*playback_manager, false, Duration::zero(), SeekMode::Fast); - DECODER_TRY_ALLOC(playback_manager->m_playback_handler->on_enter()); - - playback_manager->m_decode_thread->start(); - - return playback_manager; -} - PlaybackManager::PlaybackManager(NonnullOwnPtr& demuxer, Track video_track, NonnullOwnPtr&& decoder, VideoFrameQueue&& frame_queue) : m_demuxer(move(demuxer)) , m_selected_video_track(video_track) @@ -732,4 +690,46 @@ private: PlaybackState get_state() const override { return PlaybackState::Stopped; } }; +DecoderErrorOr> PlaybackManager::create(NonnullOwnPtr demuxer) +{ + auto video_tracks = TRY(demuxer->get_tracks_for_type(TrackType::Video)); + if (video_tracks.is_empty()) + return DecoderError::with_description(DecoderErrorCategory::Invalid, "No video track is present"sv); + auto track = video_tracks[0]; + + dbgln_if(PLAYBACK_MANAGER_DEBUG, "Selecting video track number {}", track.identifier()); + + auto codec_id = TRY(demuxer->get_codec_id_for_track(track)); + OwnPtr decoder; + switch (codec_id) { + case CodecID::VP9: + decoder = DECODER_TRY_ALLOC(try_make()); + break; + + default: + return DecoderError::format(DecoderErrorCategory::Invalid, "Unsupported codec: {}", codec_id); + } + auto decoder_non_null = decoder.release_nonnull(); + auto frame_queue = DECODER_TRY_ALLOC(VideoFrameQueue::create()); + auto playback_manager = DECODER_TRY_ALLOC(try_make(demuxer, track, move(decoder_non_null), move(frame_queue))); + + playback_manager->m_state_update_timer = Core::Timer::create_single_shot(0, [&self = *playback_manager] { self.timer_callback(); }); + + playback_manager->m_decode_thread = DECODER_TRY_ALLOC(Threading::Thread::try_create([&self = *playback_manager] { + while (!self.m_stop_decoding.load()) + self.decode_and_queue_one_sample(); + + dbgln_if(PLAYBACK_MANAGER_DEBUG, "Media Decoder thread ended."); + return 0; + }, + "Media Decoder"sv)); + + playback_manager->m_playback_handler = make(*playback_manager, false, Duration::zero(), SeekMode::Fast); + DECODER_TRY_ALLOC(playback_manager->m_playback_handler->on_enter()); + + playback_manager->m_decode_thread->start(); + + return playback_manager; +} + }