Truly independent web browser
LibMedia+Compositor: Limit the video edges of a presentation connection
Zaggy10244375d36e43d5
diff --git a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cppindex 330b2dc8764..2cb1e8aba9d 100644--- a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp+++ b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp@@ -28,11 +28,14 @@ void VideoPresentationClientConnection::die()m_edge_states.clear();}-void VideoPresentationClientConnection::create_edge(VideoSinkHandle handle, Function<void(NonnullRefPtr<DisplayingVideoSink>)> on_ready)+ErrorOr<void> VideoPresentationClientConnection::create_edge(VideoSinkHandle handle, Function<void(NonnullRefPtr<DisplayingVideoSink>)> on_ready){+ if (m_edge_states.size() >= MAX_EDGE_COUNT)+ return Error::from_string_literal("Too many video edges");auto edge_id = m_next_edge_id++;m_edge_states.set(edge_id, EdgeState { handle, move(on_ready), nullptr, nullptr, {} });async_create_video_edge(handle, edge_id);+ return {};}void VideoPresentationClientConnection::release_edge(VideoSinkHandle handle)diff --git a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.h b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.hindex 2f3e2c56aaa..2f6b2a4b165 100644--- a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.h+++ b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.h@@ -31,12 +31,15 @@ class MEDIA_API VideoPresentationClientConnection finalC_OBJECT_ABSTRACT(VideoPresentationClientConnection);public:+ // Each edge keeps as many buffers mapped here as it has slots, so a client may only have this many edges at once.+ static constexpr size_t MAX_EDGE_COUNT = 64;+explicit VideoPresentationClientConnection(NonnullOwnPtr<IPC::Transport>);virtual ~VideoPresentationClientConnection() override;// Asks the producer-server to create an edge for the given sink, then invokes on_ready with the built// DisplayingVideoSink once it arrives. The edge id correlating the request and reply is allocated and kept here.- void create_edge(VideoSinkHandle, Function<void(NonnullRefPtr<DisplayingVideoSink>)> on_ready);+ ErrorOr<void> create_edge(VideoSinkHandle, Function<void(NonnullRefPtr<DisplayingVideoSink>)> on_ready);void release_edge(VideoSinkHandle);private:diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cppindex 9d997b9ddbb..6999ad5d66a 100644--- a/Services/Compositor/ConnectionFromWebContent.cpp+++ b/Services/Compositor/ConnectionFromWebContent.cpp@@ -121,9 +121,11 @@ void ConnectionFromWebContent::create_video_edge(Media::VideoSinkHandle video_si{if (!m_video_presentation_connection)return;- m_video_presentation_connection->create_edge(video_sink_handle, [this, video_sink_handle](NonnullRefPtr<Media::DisplayingVideoSink> sink) {+ auto result = m_video_presentation_connection->create_edge(video_sink_handle, [this, video_sink_handle](NonnullRefPtr<Media::DisplayingVideoSink> sink) {m_compositor_state->on_video_sink_ready(*this, video_sink_handle, move(sink));});+ if (result.is_error())+ dbgln("Compositor: Not showing video sink {}: {}", video_sink_handle.value(), result.error());}void ConnectionFromWebContent::release_video_edge(Media::VideoSinkHandle video_sink_handle)diff --git a/Tests/LibMedia/TestRemoteVideoNode.cpp b/Tests/LibMedia/TestRemoteVideoNode.cppindex f60a1b40225..807ded3eb58 100644--- a/Tests/LibMedia/TestRemoteVideoNode.cpp+++ b/Tests/LibMedia/TestRemoteVideoNode.cpp@@ -366,9 +366,9 @@ TEST_CASE(video_presentation_server_releases_playback_edge_when_connection_dies)auto server = VideoPresentationServerConnection::construct(move(paired.local));auto client = adopt_ref(*new VideoPresentationClientConnection(MUST(paired.remote_handle.create_transport())));bool edge_ready = false;- client->create_edge(handle, [&](NonnullRefPtr<DisplayingVideoSink>) {+ MUST(client->create_edge(handle, [&](NonnullRefPtr<DisplayingVideoSink>) {edge_ready = true;- });+ }));if (!spin_until(loop, [&] { return edge_ready; })) {FAIL("Timed out waiting for video edge");return;@@ -379,6 +379,25 @@ TEST_CASE(video_presentation_server_releases_playback_edge_when_connection_dies)EXPECT(!manager->track_is_enabled(track));}+// Each edge keeps buffers mapped in the Compositor, so a client that asks for edges without end is refused past the+// limit, until it releases one.+TEST_CASE(video_presentation_client_limits_its_edges)+{+ auto paired = MUST(IPC::Transport::create_paired());+ auto server = VideoPresentationServerConnection::construct(move(paired.local));+ auto client = adopt_ref(*new VideoPresentationClientConnection(MUST(paired.remote_handle.create_transport())));++ Vector<VideoSinkHandle> handles;+ for (size_t i = 0; i < VideoPresentationClientConnection::MAX_EDGE_COUNT; i++) {+ handles.append(allocate_video_sink_handle());+ EXPECT(!client->create_edge(handles.last(), [](NonnullRefPtr<DisplayingVideoSink>) { }).is_error());+ }+ EXPECT(client->create_edge(allocate_video_sink_handle(), [](NonnullRefPtr<DisplayingVideoSink>) { }).is_error());++ client->release_edge(handles.first());+ EXPECT(!client->create_edge(allocate_video_sink_handle(), [](NonnullRefPtr<DisplayingVideoSink>) { }).is_error());+}+// A forward seek landing inside the buffered lookahead is resolved locally; a seek past it re-fetches.TEST_CASE(consumer_resolves_forward_seeks_within_the_lookahead){