Truly independent web browser
LibWebView+MediaServer+Tests: Start MediaServer if it has just exited
Zaggy1024fe73e2720ec2
diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cppindex b9663a09afa..7a1c5101ca0 100644--- a/Libraries/LibWebView/HelperProcess.cpp+++ b/Libraries/LibWebView/HelperProcess.cpp@@ -729,11 +729,25 @@ static ErrorOr<IPC::TransportHandle> connect_new_video_decoder_server_client(Refreturn response->take_handle().release_value();}+namespace {++struct AudioServerConnectorHolder : public RefCounted<AudioServerConnectorHolder> {+ explicit AudioServerConnectorHolder(Function<ErrorOr<IPC::TransportHandle>()> connect)+ : connect(move(connect))+ {+ }++ Function<ErrorOr<IPC::TransportHandle>()> connect;+};++}+ErrorOr<MediaServerClientConnection> connect_new_media_server_client(RefPtr<MediaClient::Client>& controller, u64 owner_child_id, IsPrivate is_private, Function<ErrorOr<IPC::TransportHandle>()> connect_audio_server){- if (!controller || !controller->is_open()) {+ auto audio_server_connector = make_ref_counted<AudioServerConnectorHolder>(move(connect_audio_server));+ auto launch = [&]() -> ErrorOr<void> {controller = TRY(launch_media_server_process(owner_child_id, is_private));- controller->on_request_audio_server_connection = move(connect_audio_server);+ controller->on_request_audio_server_connection = [audio_server_connector] { return audio_server_connector->connect(); };// Each MediaServer has a VideoDecoderServer of its own, which lives as long as the controller holding it. The// server is launched once the Compositor has named the GPU that hardware decoders must output to, and once this// launch's probe has decided what the server may use there.@@ -744,9 +758,19 @@ ErrorOr<MediaServerClientConnection> connect_new_media_server_client(RefPtr<Medi});});};- }+ return {};+ };++ if (!controller || !controller->is_open())+ TRY(launch());auto response = send_sync_to_helper<Messages::MediaServer::ConnectNewClient>(*controller);+ // A server exits once its last renderer has gone, which this process only notices later, so a server that is+ // already exiting can get the request. One that does not answer is replaced by a new one, which is asked instead.+ if (!response) {+ TRY(launch());+ response = send_sync_to_helper<Messages::MediaServer::ConnectNewClient>(*controller);+ }if (!response || !response->handle().has_value())return Error::from_string_literal("Failed to connect to MediaServer");return MediaServerClientConnection { response->take_handle().release_value(), response->client_id() };diff --git a/Services/MediaServer/ConnectionFromClient.cpp b/Services/MediaServer/ConnectionFromClient.cppindex 66368d38c3b..4a270f2d2d4 100644--- a/Services/MediaServer/ConnectionFromClient.cpp+++ b/Services/MediaServer/ConnectionFromClient.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/AnyOf.h>#include <AK/Checked.h>#include <AK/HashMap.h>#include <AK/NeverDestroyed.h>@@ -41,12 +42,21 @@ ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<IPC::Transport> transpos_connections->set(client_id(), *this);}-ConnectionFromClient::~ConnectionFromClient() = default;+ConnectionFromClient::~ConnectionFromClient()+{+ for (auto& [stream_id, stream] : m_media_streams) {+ stream->set_data_request_callback(nullptr);+ stream->make_missing_data_permanently_unavailable();+ }+}void ConnectionFromClient::die(){- s_connections->remove(client_id());- Core::Process::terminate_immediately(0);+ auto connection = s_connections->take(client_id());+ // A renderer can be connected while the previous one is going away, and the server must stay up for it.+ auto a_renderer_remains = any_of(*s_connections, [](auto const& entry) { return entry.value->m_role == Role::Renderer; });+ if (m_role == Role::Controller || !a_renderer_remains)+ Core::Process::terminate_immediately(0);}ErrorOr<NonnullOwnPtr<IPC::Transport>> ConnectionFromClient::request_audio_server_transport()diff --git a/Tests/LibWeb/Text/expected/HTML/media-server-reconnects-after-it-exits.txt b/Tests/LibWeb/Text/expected/HTML/media-server-reconnects-after-it-exits.txtnew file mode 100644index 00000000000..edb31a07ecb--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/media-server-reconnects-after-it-exits.txt@@ -0,0 +1,2 @@+supported before the media server exits: true+supported after the media server exits: truediff --git a/Tests/LibWeb/Text/input/HTML/media-server-reconnects-after-it-exits.html b/Tests/LibWeb/Text/input/HTML/media-server-reconnects-after-it-exits.htmlnew file mode 100644index 00000000000..c447d93182d--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/media-server-reconnects-after-it-exits.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // Disconnecting makes this process's media server exit. Media used right afterwards must get a new media server,+ // even if the browser has not yet noticed that the old one is gone.+ asyncTest(async done => {+ const type = 'video/webm; codecs="vp9"';+ println(`supported before the media server exits: ${MediaSource.isTypeSupported(type)}`);++ const video = document.createElement("video");+ const mediaSource = new MediaSource();+ video.src = URL.createObjectURL(mediaSource);+ await new Promise(resolve => mediaSource.addEventListener("sourceopen", resolve, { once: true }));++ const error = new Promise(resolve => video.addEventListener("error", resolve, { once: true }));+ internals.disconnectMediaServer();+ await error;++ println(`supported after the media server exits: ${MediaSource.isTypeSupported(type)}`);+ done();+ });+</script>