Truly independent web browser
LibWebView+Compositor+MediaServer: Let the Browser mint sibling channels
Zaggy1024947cb3f19d94
diff --git a/Libraries/LibIPC/TransportHandle.cpp b/Libraries/LibIPC/TransportHandle.cppindex 60bd41cd105..c0922dcd26a 100644--- a/Libraries/LibIPC/TransportHandle.cpp+++ b/Libraries/LibIPC/TransportHandle.cpp@@ -5,12 +5,19 @@*/#include <LibCore/Socket.h>+#include <LibCore/System.h>#include <LibIPC/Attachment.h>#include <LibIPC/Decoder.h>#include <LibIPC/Encoder.h>#include <LibIPC/Transport.h>#include <LibIPC/TransportHandle.h>+#if defined(AK_OS_WINDOWS)+# include <AK/Windows.h>+#else+# include <sys/socket.h>+#endif+namespace IPC {#if defined(AK_OS_MACOS) || defined(AK_OS_IOS)@@ -34,6 +41,23 @@ ErrorOr<NonnullOwnPtr<Transport>> TransportHandle::create_transport() constreturn make<Transport>(move(m_receive_right), move(m_send_right));}+ErrorOr<TransportHandlePair> TransportHandle::create_pair()+{+ auto port_a_receive_right = TRY(Core::MachPort::create_with_right(Core::MachPort::PortRight::Receive));+ auto port_a_send_right = TRY(port_a_receive_right.insert_right(Core::MachPort::MessageRight::MakeSend));+ auto port_b_receive_right = TRY(Core::MachPort::create_with_right(Core::MachPort::PortRight::Receive));+ auto port_b_send_right = TRY(port_b_receive_right.insert_right(Core::MachPort::MessageRight::MakeSend));++ // Either end may be written to before the other has constructed its transport.+ TransportMachPort::raise_receive_queue_limit(port_a_receive_right);+ TransportMachPort::raise_receive_queue_limit(port_b_receive_right);++ return TransportHandlePair {+ TransportHandle { move(port_a_receive_right), move(port_b_send_right) },+ TransportHandle { move(port_b_receive_right), move(port_a_send_right) },+ };+}+template<>ErrorOr<void> encode(Encoder& encoder, TransportHandle const& handle){@@ -76,6 +100,22 @@ ErrorOr<NonnullOwnPtr<Transport>> TransportHandle::create_transport() constreturn make<Transport>(move(socket));}+ErrorOr<TransportHandlePair> TransportHandle::create_pair()+{+ int fds[2] {};+# if defined(AK_OS_WINDOWS)+ TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fds));+# else+ TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, fds));+# endif+ TransportHandlePair pair { TransportHandle { File::adopt_fd(fds[0]) }, TransportHandle { File::adopt_fd(fds[1]) } };+# if defined(AK_OS_WINDOWS)+ TRY(Core::System::set_close_on_exec(fds[0], true));+ TRY(Core::System::set_close_on_exec(fds[1], true));+# endif+ return pair;+}+template<>ErrorOr<void> encode(Encoder& encoder, TransportHandle const& handle){diff --git a/Libraries/LibIPC/TransportHandle.h b/Libraries/LibIPC/TransportHandle.hindex 56ea5f0a10e..cb7294ccd59 100644--- a/Libraries/LibIPC/TransportHandle.h+++ b/Libraries/LibIPC/TransportHandle.h@@ -19,6 +19,8 @@namespace IPC {+struct TransportHandlePair;+class TransportHandle {AK_MAKE_NONCOPYABLE(TransportHandle);@@ -35,6 +37,9 @@ public:ErrorOr<NonnullOwnPtr<Transport>> create_transport() const;+ // Both ends of a new channel, for a process that connects two others without touching the channel itself.+ static ErrorOr<TransportHandlePair> create_pair();+private:#if defined(AK_OS_MACOS) || defined(AK_OS_IOS)mutable Core::MachPort m_receive_right;@@ -50,4 +55,9 @@ private:friend ErrorOr<U> decode(Decoder&);};+struct TransportHandlePair {+ TransportHandle first;+ TransportHandle second;+};+}diff --git a/Libraries/LibMediaClient/Client.cpp b/Libraries/LibMediaClient/Client.cppindex 3964b8d55b6..10f01a80077 100644--- a/Libraries/LibMediaClient/Client.cpp+++ b/Libraries/LibMediaClient/Client.cpp@@ -88,14 +88,6 @@ u64 Client::allocate_id()return m_next_id++;}-ErrorOr<IPC::TransportHandle> Client::create_video_presentation_channel()-{- auto response = send_sync_but_allow_failure<Messages::MediaServer::CreateVideoPresentationChannel>();- if (!response || !response->handle().has_value())- return Error::from_string_literal("The media server did not create a video presentation channel");- return response->take_handle().release_value();-}-Media::MediaSupportInfo Client::query_file_media_support(StringView type, StringView subtype, Optional<String> codecs_parameter){auto response = send_sync_but_allow_failure<Messages::MediaServer::QueryFileMediaSupport>(String::from_utf8_without_validation(type.bytes()), String::from_utf8_without_validation(subtype.bytes()), move(codecs_parameter));diff --git a/Libraries/LibMediaClient/Client.h b/Libraries/LibMediaClient/Client.hindex 6361438acb8..25a1182b69b 100644--- a/Libraries/LibMediaClient/Client.h+++ b/Libraries/LibMediaClient/Client.h@@ -50,8 +50,6 @@ public:// Set by the Browser on the controller connection; a server's request for an AudioServer connection goes here.Function<ErrorOr<IPC::TransportHandle>()> on_request_audio_server_connection;- ErrorOr<IPC::TransportHandle> create_video_presentation_channel();-// The synchronous queries serve APIs that must answer on the spot; everything else asks through a promise.Media::MediaSupportInfo query_file_media_support(StringView type, StringView subtype, Optional<String> codecs_parameter);Optional<Media::DecoderCapabilities> query_decoder_capabilities(StringView codecs_parameter);diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cppindex f00719f20f1..941529fce5f 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp@@ -91,9 +91,11 @@ void CompositorConnection::die()void CompositorConnection::ensure_video_presentation_channel(){- if (!can_send_message_to_compositor())+ if (!can_send_message_to_compositor() || !on_request_video_presentation_channel)return;+ // Acquiring the client has the Browser connect this process to a media server, which the Browser then connects to+ // the Compositor on request. A replacement server needs a channel of its own.auto media_client_or_error = MediaClient::Client::acquire();if (media_client_or_error.is_error()) {dbgln("Failed to reach the media server for a video presentation channel: {}", media_client_or_error.error());@@ -103,27 +105,17 @@ void CompositorConnection::ensure_video_presentation_channel()if (m_video_presentation_channel_media_client_generation == media_client->generation())return;- auto handle_or_error = media_client->create_video_presentation_channel();- if (handle_or_error.is_error()) {- dbgln("Failed to create video presentation channel: {}", handle_or_error.error());- return;- }-- async_offer_video_presentation_channel(handle_or_error.release_value());+ on_request_video_presentation_channel();m_video_presentation_channel_media_client_generation = media_client->generation();- dbgln_if(VIDEO_PRESENTATION_CHANNEL_DEBUG, "WebContent: offered the media server's video presentation channel to Compositor");+ dbgln_if(VIDEO_PRESENTATION_CHANNEL_DEBUG, "WebContent: asked the Browser for a video presentation channel to the Compositor");}-void CompositorConnection::attach_render_clock()+void CompositorConnection::attach_render_clock(IPC::TransportHandle handle){if (!can_send_message_to_compositor())return;- auto handle_or_error = RenderClock::the().attach();- if (handle_or_error.is_error()) {- dbgln("Failed to attach the render clock to the compositor: {}", handle_or_error.error());- return;- }- async_offer_render_clock_channel(handle_or_error.release_value());+ if (auto result = RenderClock::the().attach(move(handle)); result.is_error())+ dbgln("Failed to attach the render clock to the compositor: {}", result.error());}void CompositorConnection::set_parent_context(Web::CompositorContextId context_id, Optional<Web::CompositorContextId> parent_context_id)diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.h b/Libraries/LibWeb/Compositor/CompositorConnection.hindex 2469b80890a..09dd6375270 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.h+++ b/Libraries/LibWeb/Compositor/CompositorConnection.h@@ -89,11 +89,14 @@ public:bool read_webgl_buffer_sub_data(Compositing::CanvasId, Compositing::WebGL::GLenum target, Compositing::WebGL::GLintptr offset, Compositing::WebGL::GLintptr size, Core::AnonymousBuffer const& data);void ensure_video_presentation_channel();- // Offers the Compositor the process's render clock, which it delivers display ticks to from then on.- void attach_render_clock();+ // Takes up the render clock channel the Browser made between this process and the Compositor, which delivers+ // display ticks over it from then on.+ void attach_render_clock(IPC::TransportHandle);Function<void(Web::PageId page_id, Web::MouseEvent)> on_mouse_event;Function<void(Web::PageId page_id, Web::KeyEvent)> on_key_event;Function<void()> on_compositor_lost;+ // Asks the Browser to connect this process's media server to the Compositor.+ Function<void()> on_request_video_presentation_channel;private:// Only a test of the transport hands a frame straight to the compositor.diff --git a/Libraries/LibWeb/Compositor/RenderClock.cpp b/Libraries/LibWeb/Compositor/RenderClock.cppindex 70a54edbd05..260627b7bb9 100644--- a/Libraries/LibWeb/Compositor/RenderClock.cpp+++ b/Libraries/LibWeb/Compositor/RenderClock.cpp@@ -139,16 +139,16 @@ bool RenderClock::invoke_on_clock_thread(Function<void()> function)return true;}-ErrorOr<IPC::TransportHandle> RenderClock::attach()+ErrorOr<void> RenderClock::attach(IPC::TransportHandle handle){Mutex result_mutex;ConditionVariable result_condition { result_mutex };- Optional<ErrorOr<IPC::TransportHandle>> result;+ Optional<ErrorOr<void>> result;- auto invoked = invoke_on_clock_thread([&] {- auto handle_or_error = replace_channel();+ auto invoked = invoke_on_clock_thread([&, handle = move(handle)]() mutable {+ auto channel_result = replace_channel(move(handle));MutexLocker locker(result_mutex);- result = move(handle_or_error);+ result = move(channel_result);result_condition.broadcast();});if (!invoked)@@ -160,14 +160,14 @@ ErrorOr<IPC::TransportHandle> RenderClock::attach()return result.release_value();}-ErrorOr<IPC::TransportHandle> RenderClock::replace_channel()+ErrorOr<void> RenderClock::replace_channel(IPC::TransportHandle handle){// A new channel starts with nothing armed: the contexts armed on the previous one were armed for a Compositor that// is gone.drop_channel();- auto paired = TRY(IPC::Transport::create_paired());- m_channel = adopt_ref(*new RenderClockChannel(move(paired.local), *this));- return move(paired.remote_handle);+ auto transport = TRY(handle.create_transport());+ m_channel = adopt_ref(*new RenderClockChannel(move(transport), *this));+ return {};}void RenderClock::drop_channel()diff --git a/Libraries/LibWeb/Compositor/RenderClock.h b/Libraries/LibWeb/Compositor/RenderClock.hindex 1d8b7c112ee..c11360de854 100644--- a/Libraries/LibWeb/Compositor/RenderClock.h+++ b/Libraries/LibWeb/Compositor/RenderClock.h@@ -87,9 +87,9 @@ public:// The process's render clock, made the first time it is asked for.static RenderClock& the();- // Builds a new channel on the clock thread, in place of the previous one and with nothing armed, and returns the- // end to offer the Compositor. Blocks until the channel exists.- ErrorOr<IPC::TransportHandle> attach();+ // Takes up a channel to the Compositor on the clock thread, in place of the previous one and with nothing armed.+ // Blocks until the channel exists.+ ErrorOr<void> attach(IPC::TransportHandle);// Asynchronous. Hands the pointer moves over the context to `ticks` from now on, until another document's ticks take// their place or the channel is lost, and arms the context's ticks for them where they ask for them; with@@ -128,7 +128,7 @@ private:void run_on_clock_thread_and_wait(Function<void()>);// On the clock thread.- ErrorOr<IPC::TransportHandle> replace_channel();+ ErrorOr<void> replace_channel(IPC::TransportHandle);void drop_channel();void request_clock_tick(Web::CompositorContextId, double maximum_frames_per_second);void did_receive_clock_tick(Web::CompositorContextId, i64 frame_time_nanoseconds, ReadonlySpan<Web::CompositorScrollOffset>);diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 9fb7ea2f24b..f2f861e9ebd 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -1325,11 +1325,24 @@ ErrorOr<void> Application::connect_web_content_to_compositor(WebContentClient& wreturn Error::from_string_literal("Compositor process disconnected while connecting WebContent");auto response = response_or_error.release_value();+ // The render clock channel is made here, so neither process is handed an end of the other's choosing.+ auto render_clock_channel = TRY(IPC::TransportHandle::create_pair());+web_content_client.set_compositor_connection_id({}, response.web_content_connection_id());- web_content_client.async_connect_to_compositor_process(response.take_handle());+ m_compositor_client->async_connect_render_clock(response.web_content_connection_id(), move(render_clock_channel.first));+ web_content_client.async_connect_to_compositor_process(response.take_handle(), move(render_clock_channel.second));return {};}+bool Application::connect_video_presentation_to_compositor(WebContentClient& web_content_client, IPC::TransportHandle compositor_end)+{+ auto compositor_connection_id = web_content_client.compositor_connection_id({});+ if (!compositor_connection_id.has_value() || !can_send_compositor_process_ipc(m_compositor_client))+ return false;+ m_compositor_client->async_connect_video_presentation(*compositor_connection_id, move(compositor_end));+ return true;+}+ErrorOr<IPC::TransportHandle> Application::connect_new_compositor_canvas_client(){if (!m_compositor_client)diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex 9c4d4baed93..37dcc80e8b2 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -228,6 +228,9 @@ public:Web::CompositorContextId allocate_compositor_context_id();ErrorOr<void> connect_web_content_to_compositor(WebContentClient&);+ // Hands the Compositor its end of a video presentation channel for the renderer. False if the renderer has no+ // Compositor connection to pair it with.+ bool connect_video_presentation_to_compositor(WebContentClient&, IPC::TransportHandle compositor_end);ErrorOr<IPC::TransportHandle> connect_new_compositor_canvas_client();void register_compositor_context(WebContentClient&, Web::CompositorContextId, Optional<Web::PageId> page_id);ErrorOr<void> try_register_compositor_context(WebContentClient&, Web::CompositorContextId, Optional<Web::PageId> page_id);diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cppindex 58b67c6cc57..a07d1736cd1 100644--- a/Libraries/LibWebView/HelperProcess.cpp+++ b/Libraries/LibWebView/HelperProcess.cpp@@ -649,7 +649,7 @@ ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(RefPtr<AudioServerreturn response->take_handle().release_value();}-ErrorOr<IPC::TransportHandle> connect_new_media_server_client(RefPtr<MediaClient::Client>& controller, Function<ErrorOr<IPC::TransportHandle>()> connect_audio_server)+ErrorOr<MediaServerClientConnection> connect_new_media_server_client(RefPtr<MediaClient::Client>& controller, Function<ErrorOr<IPC::TransportHandle>()> connect_audio_server){if (!controller || !controller->is_open()) {controller = TRY(launch_media_server_process());@@ -659,7 +659,7 @@ ErrorOr<IPC::TransportHandle> connect_new_media_server_client(RefPtr<MediaClientauto response = controller->send_sync_but_allow_failure<Messages::MediaServer::ConnectNewClient>();if (!response || !response->handle().has_value())return Error::from_string_literal("Failed to connect to MediaServer");- return response->take_handle().release_value();+ return MediaServerClientConnection { response->take_handle().release_value(), response->client_id() };}#if defined(HAVE_WASM_COMPILER_SERVICE)diff --git a/Libraries/LibWebView/HelperProcess.h b/Libraries/LibWebView/HelperProcess.hindex be09ba75e32..9cd54d85772 100644--- a/Libraries/LibWebView/HelperProcess.h+++ b/Libraries/LibWebView/HelperProcess.h@@ -67,7 +67,11 @@ WEBVIEW_API ErrorOr<void> ensure_audio_server_is_running(RefPtr<AudioServerContrWEBVIEW_API ErrorOr<IPC::TransportHandle> connect_new_audio_server_client(RefPtr<AudioServerControlClient>& controller, u64 audio_tab_id);// Launches the MediaServer for a renderer if it has none, keeping its controller connection in the given slot, and// connects a new client to it. The server gets its AudioServer connection through connect_audio_server.-WEBVIEW_API ErrorOr<IPC::TransportHandle> connect_new_media_server_client(RefPtr<MediaClient::Client>& controller, Function<ErrorOr<IPC::TransportHandle>()> connect_audio_server);+struct MediaServerClientConnection {+ IPC::TransportHandle handle;+ int client_id { -1 };+};+WEBVIEW_API ErrorOr<MediaServerClientConnection> connect_new_media_server_client(RefPtr<MediaClient::Client>& controller, Function<ErrorOr<IPC::TransportHandle>()> connect_audio_server);#if defined(HAVE_WASM_COMPILER_SERVICE)WEBVIEW_API ErrorOr<IPC::TransportHandle> connect_new_wasm_compiler_client();#endifdiff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex 61b29d336a0..b5689b02cdf 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -768,12 +768,30 @@ Messages::WebContentClient::DidLoseRequestServerConnectionResponse WebContentCliMessages::WebContentClient::RequestMediaServerConnectionResponse WebContentClient::request_media_server_connection(){- auto handle = connect_new_media_server_client(m_media_server_client, [this] { return connect_audio_server_client(); });- if (handle.is_error()) {- warnln("Unable to connect a MediaServer client: {}", handle.error());+ auto connection = connect_new_media_server_client(m_media_server_client, [this] { return connect_audio_server_client(); });+ if (connection.is_error()) {+ warnln("Unable to connect a MediaServer client: {}", connection.error());return OptionalNone {};}- return handle.release_value();+ m_media_server_client_id = connection.value().client_id;+ return move(connection.value().handle);+}++void WebContentClient::request_video_presentation_channel()+{+ if (!m_media_server_client || !m_media_server_client->is_open() || !m_media_server_client_id.has_value()) {+ dbgln("WebContentClient: Process {} asked for a video presentation channel before connecting a media server", pid());+ return;+ }++ auto channel = IPC::TransportHandle::create_pair();+ if (channel.is_error()) {+ warnln("Unable to create a video presentation channel: {}", channel.error());+ return;+ }+ if (!Application::the().connect_video_presentation_to_compositor(*this, move(channel.value().first)))+ return;+ m_media_server_client->async_connect_video_presentation_channel(*m_media_server_client_id, move(channel.value().second));}ErrorOr<IPC::TransportHandle> WebContentClient::connect_audio_server_client()diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex 244a18a9c10..d629cad01cf 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -192,6 +192,7 @@ private:virtual Messages::WebContentClient::DidLoseRequestServerConnectionResponse did_lose_request_server_connection() override;virtual Messages::WebContentClient::RequestMediaServerConnectionResponse request_media_server_connection() override;virtual Messages::WebContentClient::RequestAudioServerConnectionResponse request_audio_server_connection() override;+ virtual void request_video_presentation_channel() override;virtual void did_start_using_gamepads() override;virtual void gamepad_play_effect(Web::Gamepad::GamepadHandle handle, Web::Gamepad::GamepadEffect effect) override;virtual void gamepad_stop_effects(Web::Gamepad::GamepadHandle handle) override;@@ -234,6 +235,7 @@ private:// The controller connections to the MediaServer and AudioServer spawned for this process, from its first media use// until it exits. The AudioServer mixes for this process alone, so a compromised one hears only its own site.RefPtr<MediaClient::Client> m_media_server_client;+ Optional<int> m_media_server_client_id;RefPtr<AudioServerControlClient> m_audio_server_client;u64 m_audio_tab_id { 0 };// The pages the user let capture audio, and whether an open page is one of them; the AudioServer is told of the latter,diff --git a/Libraries/LibWebView/WebWorkerClient.cpp b/Libraries/LibWebView/WebWorkerClient.cppindex 4a6fb269735..e126286ff07 100644--- a/Libraries/LibWebView/WebWorkerClient.cpp+++ b/Libraries/LibWebView/WebWorkerClient.cpp@@ -159,14 +159,14 @@ Messages::WebWorkerClient::DidIsKnownHstsHostResponse WebWorkerClient::did_is_knMessages::WebWorkerClient::RequestMediaServerConnectionResponse WebWorkerClient::request_media_server_connection(){- auto handle = connect_new_media_server_client(m_media_server_client, [this] {+ auto connection = connect_new_media_server_client(m_media_server_client, [this] {return connect_new_audio_server_client(m_audio_server_client, m_audio_tab_id);});- if (handle.is_error()) {- warnln("Unable to connect a MediaServer client: {}", handle.error());+ if (connection.is_error()) {+ warnln("Unable to connect a MediaServer client: {}", connection.error());return OptionalNone {};}- return handle.release_value();+ return move(connection.value().handle);}void WebWorkerClient::did_post_broadcast_channel_message(Web::HTML::PostedBroadcastChannelMessage message)diff --git a/Services/Compositor/CompositorControlServer.ipc b/Services/Compositor/CompositorControlServer.ipcindex c5faefbc81d..c55d43e1028 100644--- a/Services/Compositor/CompositorControlServer.ipc+++ b/Services/Compositor/CompositorControlServer.ipc@@ -11,6 +11,8 @@ endpoint CompositorControlServerset_font_service(IPC::TransportHandle handle, IPC::File catalog, u64 catalog_size, u64 generation) =|connect_web_content() => (IPC::TransportHandle handle, i32 web_content_connection_id)+ connect_render_clock(i32 web_content_connection_id, IPC::TransportHandle handle) =|+ connect_video_presentation(i32 web_content_connection_id, IPC::TransportHandle handle) =|create_context(Web::CompositorContextId context_id, Optional<u64> page_id, i32 web_content_connection_id) => ()diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 3fc5d86e6d7..a52ecd84cfd 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -264,6 +264,15 @@ void CompositorState::add_video_sink(CompositorStateWebContentClient& client, Meclient.create_video_edge(handle);}+void CompositorState::create_video_edges(CompositorStateWebContentClient& client)+{+ auto client_sinks = m_video_sink_states.get(&client);+ if (!client_sinks.has_value())+ return;+ for (auto& sink_entry : *client_sinks)+ client.create_video_edge(sink_entry.key);+}+void CompositorState::remove_video_sink(CompositorStateWebContentClient& client, Media::VideoSinkHandle handle){if (auto client_sinks = m_video_sink_states.get(&client); client_sinks.has_value())diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex 6f5dd2b5c3f..06635c21d96 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -96,6 +96,8 @@ public:void update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction&&);void update_scroll_state(Web::CompositorContextId, Compositing::ScrollStateSnapshot&&, Compositing::KeyboardScrollState);void add_video_sink(CompositorStateWebContentClient&, Media::VideoSinkHandle);+ // For a presentation channel that arrives after the sinks did, or that replaces the one their edges were on.+ void create_video_edges(CompositorStateWebContentClient&);void remove_video_sink(CompositorStateWebContentClient&, Media::VideoSinkHandle);void set_video_sink_ticking(CompositorStateWebContentClient&, Media::VideoSinkHandle, bool should_tick);void on_video_sink_ready(CompositorStateWebContentClient&, Media::VideoSinkHandle, NonnullRefPtr<Media::DisplayingVideoSink> const&);diff --git a/Services/Compositor/CompositorWebContentServer.ipc b/Services/Compositor/CompositorWebContentServer.ipcindex d40f552f521..523f0ef3749 100644--- a/Services/Compositor/CompositorWebContentServer.ipc+++ b/Services/Compositor/CompositorWebContentServer.ipc@@ -21,9 +21,6 @@endpoint CompositorWebContentServer{- offer_video_presentation_channel(IPC::TransportHandle handle) =|- offer_render_clock_channel(IPC::TransportHandle handle) =|-add_video_sink(Media::VideoSinkHandle video_sink_handle) =|remove_video_sink(Media::VideoSinkHandle video_sink_handle) =|set_video_sink_ticking(Media::VideoSinkHandle video_sink_handle, bool should_tick) =|diff --git a/Services/Compositor/ConnectionFromClient.cpp b/Services/Compositor/ConnectionFromClient.cppindex d0abbfa8eac..11335915316 100644--- a/Services/Compositor/ConnectionFromClient.cpp+++ b/Services/Compositor/ConnectionFromClient.cpp@@ -80,6 +80,18 @@ Messages::CompositorControlServer::ConnectWebContentResponse ConnectionFromClienreturn { move(paired_transport.remote_handle), web_content_connection_id };}+void ConnectionFromClient::connect_render_clock(i32 web_content_connection_id, IPC::TransportHandle handle)+{+ if (auto connection = m_web_content_connections.get(web_content_connection_id); connection.has_value())+ (*connection)->install_render_clock_channel(move(handle));+}++void ConnectionFromClient::connect_video_presentation(i32 web_content_connection_id, IPC::TransportHandle handle)+{+ if (auto connection = m_web_content_connections.get(web_content_connection_id); connection.has_value())+ (*connection)->install_video_presentation_channel(move(handle));+}+void ConnectionFromClient::create_context(Web::CompositorContextId context_id, Optional<u64> page_id, i32 web_content_connection_id){auto* connection = web_content_connection(web_content_connection_id);diff --git a/Services/Compositor/ConnectionFromClient.h b/Services/Compositor/ConnectionFromClient.hindex b02c269eb43..8cfeebe0ace 100644--- a/Services/Compositor/ConnectionFromClient.h+++ b/Services/Compositor/ConnectionFromClient.h@@ -41,6 +41,8 @@ private:virtual void set_font_service(IPC::TransportHandle, IPC::File catalog, u64 catalog_size, u64 generation) override;virtual Messages::CompositorControlServer::ConnectWebContentResponse connect_web_content() override;+ virtual void connect_render_clock(i32 web_content_connection_id, IPC::TransportHandle) override;+ virtual void connect_video_presentation(i32 web_content_connection_id, IPC::TransportHandle) override;virtual void create_context(Web::CompositorContextId, Optional<u64> page_id, i32 web_content_connection_id) override;virtual void viewport_size_updated(Web::CompositorContextId, Gfx::IntSize, Compositing::WindowResizingInProgress) override;virtual void set_paused_debugger_overlay(Web::CompositorContextId, bool visible, double device_pixel_ratio, Optional<String> font_family, Optional<u8> hovered_action) override;diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cppindex f6ff94122bb..8356820e558 100644--- a/Services/Compositor/ConnectionFromWebContent.cpp+++ b/Services/Compositor/ConnectionFromWebContent.cpp@@ -30,23 +30,24 @@ void ConnectionFromWebContent::die()m_on_death(*this);}-void ConnectionFromWebContent::offer_video_presentation_channel(IPC::TransportHandle handle)+void ConnectionFromWebContent::install_video_presentation_channel(IPC::TransportHandle handle){auto transport_or_error = handle.create_transport();if (transport_or_error.is_error()) {- did_misbehave("WebContent sent an unusable video presentation transport handle");+ dbgln("Compositor: Could not take up a video presentation channel: {}", transport_or_error.error());return;}m_video_presentation_connection = adopt_ref(*new Media::VideoPresentationClientConnection(transport_or_error.release_value()));+ m_compositor_state->create_video_edges(*this);dbgln_if(VIDEO_PRESENTATION_CHANNEL_DEBUG, "Compositor: established video presentation channel for WebContent (client_id={})", client_id());}-void ConnectionFromWebContent::offer_render_clock_channel(IPC::TransportHandle handle)+void ConnectionFromWebContent::install_render_clock_channel(IPC::TransportHandle handle){auto transport_or_error = handle.create_transport();if (transport_or_error.is_error()) {- did_misbehave("WebContent sent an unusable render clock transport handle");+ dbgln("Compositor: Could not take up a render clock channel: {}", transport_or_error.error());return;}// A new channel replaces the one before it, whose requests were for the contexts the process armed then.diff --git a/Services/Compositor/ConnectionFromWebContent.h b/Services/Compositor/ConnectionFromWebContent.hindex 870bb79e425..eb1b5b5640b 100644--- a/Services/Compositor/ConnectionFromWebContent.h+++ b/Services/Compositor/ConnectionFromWebContent.h@@ -33,13 +33,15 @@ public:void notify_compositor_lost();void set_on_death(Function<void(ConnectionFromWebContent&)> handler) { m_on_death = move(handler); }+ // The Browser makes these channels and hands each end to the process it belongs to.+ void install_video_presentation_channel(IPC::TransportHandle);+ void install_render_clock_channel(IPC::TransportHandle);+private:explicit ConnectionFromWebContent(NonnullOwnPtr<IPC::Transport>, NonnullRefPtr<CompositorState>, int client_id);virtual void die() override;- virtual void offer_video_presentation_channel(IPC::TransportHandle handle) override;- virtual void offer_render_clock_channel(IPC::TransportHandle handle) override;virtual void add_video_sink(Media::VideoSinkHandle) override;virtual void remove_video_sink(Media::VideoSinkHandle) override;virtual void set_video_sink_ticking(Media::VideoSinkHandle, bool should_tick) override;diff --git a/Services/MediaServer/ConnectionFromClient.cpp b/Services/MediaServer/ConnectionFromClient.cppindex 9e985367fed..db215d63b5e 100644--- a/Services/MediaServer/ConnectionFromClient.cpp+++ b/Services/MediaServer/ConnectionFromClient.cpp@@ -57,7 +57,7 @@ ErrorOr<NonnullOwnPtr<IPC::Transport>> ConnectionFromClient::request_audio_servereturn response->take_handle()->create_transport();}-ErrorOr<IPC::TransportHandle> ConnectionFromClient::create_renderer_connection()+ErrorOr<ConnectionFromClient::RendererConnection> ConnectionFromClient::create_renderer_connection(){auto paired_transports = TRY(IPC::Transport::create_paired());auto handle = move(paired_transports.remote_handle);@@ -65,22 +65,22 @@ ErrorOr<IPC::TransportHandle> ConnectionFromClient::create_renderer_connection()// The static connection map owns this connection until its peer disconnects.auto client = adopt_ref(*new ConnectionFromClient(move(paired_transports.local), Role::Renderer));- return handle;+ return RendererConnection { move(handle), client->client_id() };}Messages::MediaServer::ConnectNewClientResponse ConnectionFromClient::connect_new_client(){if (m_role != Role::Controller) {did_misbehave("Only the controller may connect a new media client");- return OptionalNone {};+ return { OptionalNone {}, -1 };}- auto handle = create_renderer_connection();- if (handle.is_error()) {- dbgln("Failed to connect a media client: {}", handle.error());- return OptionalNone {};+ auto connection = create_renderer_connection();+ if (connection.is_error()) {+ dbgln("Failed to connect a media client: {}", connection.error());+ return { OptionalNone {}, -1 };}- return handle.release_value();+ return { move(connection.value().handle), connection.value().client_id };}bool ConnectionFromClient::verify_renderer_role()@@ -91,21 +91,31 @@ bool ConnectionFromClient::verify_renderer_role()return false;}-Messages::MediaServer::CreateVideoPresentationChannelResponse ConnectionFromClient::create_video_presentation_channel()+void ConnectionFromClient::connect_video_presentation_channel(int client_id, IPC::TransportHandle handle){- if (!verify_renderer_role())- return OptionalNone {};+ if (m_role != Role::Controller) {+ did_misbehave("Only the controller may connect a video presentation channel");+ return;+ }- auto paired_transports_or_error = IPC::Transport::create_paired();- if (paired_transports_or_error.is_error()) {- dbgln("Failed to create a video presentation channel: {}", paired_transports_or_error.error());- return OptionalNone {};+ auto renderer = s_connections->get(client_id);+ if (!renderer.has_value() || (*renderer)->m_role != Role::Renderer) {+ dbgln("MediaServer: No renderer connection {} to take a video presentation channel", client_id);+ return;+ }+ (*renderer)->install_video_presentation_channel(move(handle));+}++void ConnectionFromClient::install_video_presentation_channel(IPC::TransportHandle handle)+{+ auto transport = handle.create_transport();+ if (transport.is_error()) {+ dbgln("MediaServer: Could not take up a video presentation channel: {}", transport.error());+ return;}- auto paired_transports = paired_transports_or_error.release_value();// A replaced connection releases its edges; the presentation client that held them is gone or will ask again.- m_video_presentation_connection = Media::VideoPresentationServerConnection::construct(move(paired_transports.local));- return move(paired_transports.remote_handle);+ m_video_presentation_connection = Media::VideoPresentationServerConnection::construct(transport.release_value());}static Media::MediaSupportInfo file_media_support_for(String type, String subtype, Optional<String> codecs_parameter)diff --git a/Services/MediaServer/ConnectionFromClient.h b/Services/MediaServer/ConnectionFromClient.hindex df8bd542057..adea9e0a850 100644--- a/Services/MediaServer/ConnectionFromClient.h+++ b/Services/MediaServer/ConnectionFromClient.h@@ -43,7 +43,7 @@ public:private:virtual Messages::MediaServer::ConnectNewClientResponse connect_new_client() override;- virtual Messages::MediaServer::CreateVideoPresentationChannelResponse create_video_presentation_channel() override;+ virtual void connect_video_presentation_channel(int client_id, IPC::TransportHandle handle) override;virtual Messages::MediaServer::QueryFileMediaSupportResponse query_file_media_support(String type, String subtype, Optional<String> codecs_parameter) override;virtual Messages::MediaServer::QueryDecoderCapabilitiesResponse query_decoder_capabilities(String codecs_parameter) override;@@ -90,7 +90,12 @@ private:virtual void set_source_buffer_pending_initialization_segment_for_change_type_flag(u64 session_id, u64 source_buffer_id, bool flag) override;virtual void set_source_buffer_reached_end_of_stream(u64 session_id, u64 source_buffer_id, bool reached) override;- static ErrorOr<IPC::TransportHandle> create_renderer_connection();+ struct RendererConnection {+ IPC::TransportHandle handle;+ int client_id { -1 };+ };+ static ErrorOr<RendererConnection> create_renderer_connection();+ void install_video_presentation_channel(IPC::TransportHandle);bool verify_renderer_role();PlaybackSession* find_playback_session(u64 session_id);Media::IncrementallyPopulatedStream* find_media_stream(u64 stream_id);diff --git a/Services/MediaServer/MediaServer.ipc b/Services/MediaServer/MediaServer.ipcindex 060d76566ba..6191040c97b 100644--- a/Services/MediaServer/MediaServer.ipc+++ b/Services/MediaServer/MediaServer.ipc@@ -13,9 +13,8 @@endpoint MediaServer{- connect_new_client() => (Optional<IPC::TransportHandle> handle)-- create_video_presentation_channel() => (Optional<IPC::TransportHandle> handle)+ connect_new_client() => (Optional<IPC::TransportHandle> handle, int client_id)+ connect_video_presentation_channel(int client_id, IPC::TransportHandle handle) =|query_file_media_support(String type, String subtype, Optional<String> codecs_parameter) => (Media::MediaSupportInfo info)query_decoder_capabilities(String codecs_parameter) => (Optional<Media::DecoderCapabilities> capabilities)diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex 54ea2518a94..e8c7ec95166 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -432,7 +432,7 @@ void ConnectionFromClient::connect_to_wasm_compiler([[maybe_unused]] IPC::Transp#endif}-void ConnectionFromClient::connect_to_compositor_process(IPC::TransportHandle handle)+void ConnectionFromClient::connect_to_compositor_process(IPC::TransportHandle handle, IPC::TransportHandle render_clock_handle){auto transport = MUST(handle.create_transport());m_compositor_connection = adopt_ref(*new Web::Compositor::CompositorConnection(move(transport)));@@ -445,8 +445,11 @@ void ConnectionFromClient::connect_to_compositor_process(IPC::TransportHandle ham_compositor_connection->on_compositor_lost = [this] {m_page_host->compositor_process_lost();};+ m_compositor_connection->on_request_video_presentation_channel = [this] {+ async_request_video_presentation_channel();+ };- m_compositor_connection->attach_render_clock();+ m_compositor_connection->attach_render_clock(move(render_clock_handle));m_compositor_connection->hand_frame_sink_to_paint_thread();}diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.hindex 6c9fbd379b6..be39624ed57 100644--- a/Services/WebContent/ConnectionFromClient.h+++ b/Services/WebContent/ConnectionFromClient.h@@ -116,7 +116,7 @@ private:virtual void connect_to_test_endpoint(IPC::TransportHandle handle) override;virtual void connect_to_image_decoder(IPC::TransportHandle handle) override;virtual void connect_to_wasm_compiler(IPC::TransportHandle handle) override;- virtual void connect_to_compositor_process(IPC::TransportHandle handle) override;+ virtual void connect_to_compositor_process(IPC::TransportHandle handle, IPC::TransportHandle render_clock_handle) override;virtual void set_site_compatibility_data(JsonValue data) override;virtual void compositor_process_reconnected() override;virtual void update_system_theme(Web::PageId page_id, Core::AnonymousBuffer) override;diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipcindex 0915579d2f7..5a6364d7b2b 100644--- a/Services/WebContent/WebContentClient.ipc+++ b/Services/WebContent/WebContentClient.ipc@@ -190,6 +190,7 @@ endpoint WebContentClient routed by Web::PageId page_iddid_lose_request_server_connection() => (Optional<IPC::TransportHandle> handle)request_media_server_connection() => (Optional<IPC::TransportHandle> handle)request_audio_server_connection() => (Optional<IPC::TransportHandle> handle)+ request_video_presentation_channel() =|[NotRouted] did_request_storage_item(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id, Utf16String bottle_key) => (Optional<Utf16String> value)[NotRouted] did_set_storage_item(Web::PageId page_id, Web::StorageAPI::StorageEndpointType storage_endpoint, Web::HTML::EnvironmentId environment_id, Utf16String bottle_key, Utf16String value) => (WebView::StorageSetResult result)diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipcindex dbb019b5445..7c2f7d4beed 100644--- a/Services/WebContent/WebContentServer.ipc+++ b/Services/WebContent/WebContentServer.ipc@@ -86,7 +86,7 @@ endpoint WebContentServer routed by Web::PageId page_idconnect_to_test_endpoint(IPC::TransportHandle handle) =|connect_to_image_decoder(IPC::TransportHandle handle) =|connect_to_wasm_compiler(IPC::TransportHandle handle) =|- connect_to_compositor_process(IPC::TransportHandle handle) =|+ connect_to_compositor_process(IPC::TransportHandle handle, IPC::TransportHandle render_clock_handle) =|compositor_process_reconnected() =|set_site_compatibility_data(JsonValue data) =|