Repository: LadybirdBrowser/ladybird
File: Libraries/LibMedia/PlaybackManager.cpp (C++)

Code before:
        })();

        playback_manager->m_time_provider = time_provider;
        playback_manager->m_audio_sink = audio_sink;

        playback_manager->m_video_tracks.extend(move(video_tracks));
        playback_manager->m_video_track_datas.extend(move(video_track_datas));
        playback_manager->m_audio_tracks.extend(move(audio_tracks));
        playback_manager->m_audio_track_datas.extend(move(audio_track_datas));
        playback_manager->m_duration = move(duration);

        playback_manager->set_up_error_handlers();

        on_complete(playback_manager->m_video_tracks, preferred_video_track, playback_manager->m_audio_tracks, preferred_audio_track, playback_manager->m_duration);
    });

    return {};
}

NonnullRefPtr<PlaybackManager> PlaybackManager::create(ReadonlyBytes data, OnCreateComplete on_complete, OnCreateError on_error)
{
    // Create the weak wrapper.
    auto weak_playback_manager = make_ref_counted<WeakPlaybackManager>();

    auto thread = Threading::Thread::construct([weak_playback_manager, data, on_complete = move(on_complete), on_error = move(on_error), &main_thread_event_loop = Core::EventLoop::current()] mutable -> int {
        auto playback_manager = weak_playback_manager->take_strong();
        if (!playback_manager)
            return 0;

        auto maybe_error = playback_manager->initialize_from_data(data, weak_playback_manager, main_thread_event_loop, move(on_complete));
        if (maybe_error.is_error()) {
            main_thread_event_loop.deferred_invoke([on_error = move(on_error), error = maybe_error.release_error()] mutable {
                on_error(move(error));
            });
            return 0;
        }
        return 0;
    });

    auto playback_manager = adopt_ref(*new (nothrow) PlaybackManager(weak_playback_manager));
    weak_playback_manager->m_manager = playback_manager;
    playback_manager->m_handler = make<PausedStateHandler>(*playback_manager);

    thread->start();
    thread->detach();

    return playback_manager;
}

PlaybackManager::PlaybackManager(NonnullRefPtr<WeakPlaybackManager> const& weak_wrapper)
    : m_weak_wrapper(weak_wrapper)

Review comment (suggestion):
I realized, having you use the weak reference here is pointless. 😅 If we're taking a strong reference immediately, and the thread has a limited lifetime, we'll be just fine creating a NonnullRefPtr in the captures.

Code after:

        if (auto audio_sink = playback_manager->m_audio_sink) {
            VERIFY(playback_manager->current_time().is_zero());
            playback_manager->m_time_provider = make_ref_counted<WrapperTimeProvider<AudioMixingSink>>(*audio_sink);
        }

        if (playback_manager->on_track_added) {
            for (auto const& audio_track : playback_manager->m_audio_tracks)
                playback_manager->on_track_added(TrackType::Audio, audio_track);
            for (auto const& video_track : playback_manager->m_video_tracks)
                playback_manager->on_track_added(TrackType::Video, video_track);
        }

        if (playback_manager->on_metadata_parsed)
            playback_manager->on_metadata_parsed();
    });

    return {};
}

NonnullRefPtr<PlaybackManager> PlaybackManager::create()
{
    auto playback_manager = adopt_ref(*new (nothrow) PlaybackManager());
    playback_manager->m_handler = make<PausedStateHandler>(*playback_manager);
    return playback_manager;
}

PlaybackManager::PlaybackManager()
    : m_weak_wrapper(make_ref_counted<WeakPlaybackManager>())
    , m_time_provider(make_ref_counted<GenericTimeProvider>())
{
    m_weak_wrapper->m_manager = this;
}

PlaybackManager::~PlaybackManager()
{
    m_weak_wrapper->revoke();
}

void PlaybackManager::add_media_source(ReadonlyBytes media_data)
{
    auto thread = Threading::Thread::construct([playback_manager = NonnullRefPtr { *this }, media_data, &main_thread_event_loop = Core::EventLoop::current()] -> int {
        auto maybe_error = playback_manager->prepare_playback_from_media_data(media_data, main_thread_event_loop);
        if (maybe_error.is_error()) {
            main_thread_event_loop.deferred_invoke([playback_manager, error = maybe_error.release_error()] mutable {
                if (playback_manager->on_unsupported_format_error)
                    playback_manager->on_unsupported_format_error(move(error));
            });
            return 0;
        }
        return 0;