Skip to content

Commit

Permalink
AK+Kernel+LibVideo: Include workarounds for missing P0960 only in Xcode
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
2 people authored and BertalanD committed May 21, 2024
1 parent 960a4b6 commit 38b51b7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 47 deletions.
8 changes: 6 additions & 2 deletions AK/NonnullOwnPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ requires(IsConstructible<T, Args...>) inline NonnullOwnPtr<T> make(Args&&... arg
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T(forward<Args>(args)...));
}

// FIXME: Remove once P0960R3 is available in Clang.
# ifdef AK_COMPILER_APPLE_CLANG
// FIXME: Remove once P0960R3 is available in Apple Clang.
template<class T, class... Args>
inline NonnullOwnPtr<T> make(Args&&... args)
{
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *new T { forward<Args>(args)... });
}
# endif

#endif

Expand All @@ -174,13 +176,15 @@ requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullOwnPtr<T>> try_make(
return adopt_nonnull_own_or_enomem(new (nothrow) T(forward<Args>(args)...));
}

// FIXME: Remove once P0960R3 is available in Clang.
#ifdef AK_COMPILER_APPLE_CLANG
// FIXME: Remove once P0960R3 is available in Apple Clang.
template<typename T, class... Args>
inline ErrorOr<NonnullOwnPtr<T>> try_make(Args&&... args)

{
return adopt_nonnull_own_or_enomem(new (nothrow) T { forward<Args>(args)... });
}
#endif

template<typename T>
struct Traits<NonnullOwnPtr<T>> : public DefaultTraits<NonnullOwnPtr<T>> {
Expand Down
8 changes: 6 additions & 2 deletions AK/NonnullRefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,14 @@ requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullRefPtr<T>> try_make_
return adopt_nonnull_ref_or_enomem(new (nothrow) T(forward<Args>(args)...));
}

// FIXME: Remove once P0960R3 is available in Clang.
#ifdef AK_COMPILER_APPLE_CLANG
// FIXME: Remove once P0960R3 is available in Apple Clang.
template<typename T, class... Args>
inline ErrorOr<NonnullRefPtr<T>> try_make_ref_counted(Args&&... args)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) T { forward<Args>(args)... });
}
#endif

template<Formattable T>
struct Formatter<NonnullRefPtr<T>> : Formatter<T> {
Expand Down Expand Up @@ -277,12 +279,14 @@ requires(IsConstructible<T, Args...>) inline NonnullRefPtr<T> make_ref_counted(A
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T(forward<Args>(args)...));
}

// FIXME: Remove once P0960R3 is available in Clang.
#ifdef AK_COMPILER_APPLE_CLANG
// FIXME: Remove once P0960R3 is available in Apple Clang.
template<typename T, class... Args>
inline NonnullRefPtr<T> make_ref_counted(Args&&... args)
{
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *new T { forward<Args>(args)... });
}
#endif

template<typename T>
struct Traits<NonnullRefPtr<T>> : public DefaultTraits<NonnullRefPtr<T>> {
Expand Down
4 changes: 4 additions & 0 deletions AK/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion Kernel/Library/LockRefPtr.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,14 @@ requires(IsConstructible<T, Args...>) inline ErrorOr<NonnullLockRefPtr<T>> try_m
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) T(forward<Args>(args)...));
}

// FIXME: Remove once P0960R3 is available in Clang.
#ifdef AK_COMPILER_APPLE_CLANG
// FIXME: Remove once P0960R3 is available in Apple Clang.
template<typename T, class... Args>
inline ErrorOr<NonnullLockRefPtr<T>> try_make_lock_ref_counted(Args&&... args)
{
return adopt_nonnull_lock_ref_or_enomem(new (nothrow) T { forward<Args>(args)... });
}
#endif

template<typename T>
inline ErrorOr<NonnullLockRefPtr<T>> adopt_nonnull_lock_ref_or_enomem(T* object)
Expand Down
84 changes: 42 additions & 42 deletions Userland/Libraries/LibVideo/PlaybackManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,6 @@ DecoderErrorOr<NonnullOwnPtr<PlaybackManager>> PlaybackManager::from_data(Readon
return create(move(demuxer));
}

DecoderErrorOr<NonnullOwnPtr<PlaybackManager>> PlaybackManager::create(NonnullOwnPtr<Demuxer> 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<VideoDecoder> decoder;
switch (codec_id) {
case CodecID::VP9:
decoder = DECODER_TRY_ALLOC(try_make<VP9::Decoder>());
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<PlaybackManager>(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<SeekingStateHandler>(*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>& demuxer, Track video_track, NonnullOwnPtr<VideoDecoder>&& decoder, VideoFrameQueue&& frame_queue)
: m_demuxer(move(demuxer))
, m_selected_video_track(video_track)
Expand Down Expand Up @@ -732,4 +690,46 @@ class PlaybackManager::StoppedStateHandler : public PlaybackManager::PlaybackSta
PlaybackState get_state() const override { return PlaybackState::Stopped; }
};

DecoderErrorOr<NonnullOwnPtr<PlaybackManager>> PlaybackManager::create(NonnullOwnPtr<Demuxer> 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<VideoDecoder> decoder;
switch (codec_id) {
case CodecID::VP9:
decoder = DECODER_TRY_ALLOC(try_make<VP9::Decoder>());
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<PlaybackManager>(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<SeekingStateHandler>(*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;
}

}

0 comments on commit 38b51b7

Please sign in to comment.