Skip to content

Truly independent web browser

2.6 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashMap.h>
#include <AK/ThreadSafeWeakable.h>
#include <LibIPC/ConnectionFromClient.h>
#include <LibMedia/Export.h>
#include <LibMedia/PlaybackManager.h>
#include <LibMedia/Sinks/RemoteVideoSink.h>
#include <LibMedia/VideoPresentation/VideoPresentationClientEndpoint.h>
#include <LibMedia/VideoPresentation/VideoPresentationServerEndpoint.h>
#include <LibMedia/VideoSinkHandle.h>

namespace Media {

// The producer-server end of the cross-process video presentation channel, hosted in the process that owns the
// producers (WebContent today, MediaServer later). It builds a RemoteVideoSink pump per edge on request, ships
// the edge's shared-memory handles to the presentation client, and routes the consumer's demands to the pump.
class MEDIA_API VideoPresentationServerConnection final
    : public IPC::ConnectionFromClient<VideoPresentationClientEndpoint, VideoPresentationServerEndpoint>
    , public ThreadSafeWeakable<VideoPresentationServerConnection> {
    C_OBJECT(VideoPresentationServerConnection);

public:
    virtual ~VideoPresentationServerConnection() override;

    // The presentation client may ask for an edge before the handle's registration reaches this process over another
    // connection; such requests wait here until the host reports a registration.
    void retry_pending_video_edges();

private:
    explicit VideoPresentationServerConnection(NonnullOwnPtr<IPC::Transport>);

    virtual void die() override;

    virtual void create_video_edge(VideoSinkHandle video_sink_handle, u64 edge_id) override;
    virtual void release_video_edge(u64 edge_id) override;
    virtual void request_start(u64 edge_id) override;
    virtual void request_suspend(u64 edge_id) override;
    virtual void request_seek(u64 edge_id, AK::Duration timestamp, InvalidateHeldData, u64 newest_received_lend_id) override;
    virtual void release_frame(u64 edge_id, u64 lend_id) override;
    virtual void notify_sink_status(u64 edge_id, PipelineStatus status, u32 requested_seek_id, PipelineCheckpoint checkpoint) override;
    virtual void notify_sink_resize(u64 edge_id, u32 width, u32 height) override;

    struct EdgeState {
        NonnullRefPtr<RemoteVideoSink> pump;
        VideoSinkHandle handle;
        u32 actual_requested_seek_id { 0 };
    };

    void create_registered_video_edge(VideoSinkHandle, u64 edge_id);

    HashMap<u64, EdgeState> m_edge_states;
    HashMap<u64, NonnullRefPtr<RemoteVideoSink>> m_released_edge_pumps_with_lent_frames;
    HashMap<u64, VideoSinkHandle> m_pending_edge_handles_by_edge_id;
};

}