Skip to content

Truly independent web browser

LibMedia: Keep video frames lent until the consumer releases them

Zaggy102406c58443baea
diff --git a/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp b/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp
index f66ea88dcae..e84652dc6d7 100644
--- a/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp
+++ b/Libraries/LibMedia/Producers/RemoteVideoProducer.cpp
@@ -9,6 +9,15 @@
namespace Media {
+struct RemoteVideoProducer::LentSlotReleaser : public AtomicRefCounted<LentSlotReleaser> {
+ explicit LentSlotReleaser(Function<void(VideoFramePoolID, u32 slot_index)> release_slot)
+ : release_slot(move(release_slot))
+ {
+ }
+
+ Function<void(VideoFramePoolID, u32 slot_index)> release_slot;
+};
+
NonnullRefPtr<RemoteVideoProducer> RemoteVideoProducer::create(VideoEdgeQueue edge, NonnullRefPtr<VideoFrameSlotDirectory> slot_directory, Delegates delegates)
{
VERIFY(delegates.request_start);
@@ -27,12 +36,26 @@ NonnullRefPtr<RemoteVideoProducer> RemoteVideoProducer::create(VideoEdgeQueue ed
RemoteVideoProducer::RemoteVideoProducer(VideoEdgeQueue edge, NonnullRefPtr<VideoFrameSlotDirectory> slot_directory, Delegates delegates)
: m_edge(move(edge))
, m_slot_directory(move(slot_directory))
- , m_delegates(move(delegates))
+ , m_lent_slot_releaser(adopt_ref(*new LentSlotReleaser(move(delegates.release_slot))))
{
+ m_delegates = move(delegates);
+}
+
+RemoteVideoProducer::~RemoteVideoProducer() = default;
+
+void RemoteVideoProducer::stop_consuming()
+{
+ if (m_current_frame) {
+ m_current_frame = nullptr;
+ m_edge.consume();
+ }
+ m_has_stopped_consuming = true;
}
void RemoteVideoProducer::notify_data_available()
{
+ if (m_has_stopped_consuming)
+ return;
release_ring_contents_if_suspended();
m_wake_handler.dispatch();
}
@@ -97,6 +120,8 @@ void RemoteVideoProducer::set_wake_handler(PipelineWakeHandler handler)
void RemoteVideoProducer::seek(AK::Duration timestamp, InvalidateHeldData invalidate_held_data)
{
+ if (m_has_stopped_consuming)
+ return;
discard_stale_ring_heads();
// Announced before the ring is consulted, paired with the fence in the pump before it consumes an enqueued frame
@@ -131,6 +156,8 @@ bool RemoteVideoProducer::can_satisfy_seek_locally(AK::Duration timestamp) const
VideoProducerOutput RemoteVideoProducer::peek()
{
+ if (m_has_stopped_consuming)
+ return { nullptr, PipelineStatus::Pending };
discard_stale_ring_heads();
auto head = m_edge.peek();
if (!head.has_value()) {
@@ -149,6 +176,8 @@ VideoProducerOutput RemoteVideoProducer::peek()
void RemoteVideoProducer::consume()
{
+ if (m_has_stopped_consuming)
+ return;
m_current_frame = nullptr;
m_edge.consume();
m_delegates.notify_space_available();
@@ -156,14 +185,13 @@ void RemoteVideoProducer::consume()
void RemoteVideoProducer::release_lent_slot(VideoFramePoolID pool_id, u32 slot_index) const
{
- m_delegates.release_slot(pool_id, slot_index);
+ m_lent_slot_releaser->release_slot(pool_id, slot_index);
}
RefPtr<VideoFrame> RemoteVideoProducer::resolve(VideoFrameHandle const& handle)
{
- return m_slot_directory->resolve_frame(handle, [weak = make_weak_ref(), pool_id = handle.pool_id, slot_index = handle.slot_index] {
- if (auto producer = weak.strong_ref())
- producer->release_lent_slot(pool_id, slot_index);
+ return m_slot_directory->resolve_frame(handle, [releaser = m_lent_slot_releaser, pool_id = handle.pool_id, slot_index = handle.slot_index] {
+ releaser->release_slot(pool_id, slot_index);
});
}
diff --git a/Libraries/LibMedia/Producers/RemoteVideoProducer.h b/Libraries/LibMedia/Producers/RemoteVideoProducer.h
index c6b32da7b08..7adbd774b05 100644
--- a/Libraries/LibMedia/Producers/RemoteVideoProducer.h
+++ b/Libraries/LibMedia/Producers/RemoteVideoProducer.h
@@ -37,9 +37,12 @@ public:
};
static NonnullRefPtr<RemoteVideoProducer> create(VideoEdgeQueue, NonnullRefPtr<VideoFrameSlotDirectory>, Delegates);
+ virtual ~RemoteVideoProducer() override;
void notify_data_available();
+ void stop_consuming();
+
virtual void start() override;
virtual VideoProducerOutput peek() override;
virtual void consume() override;
@@ -57,9 +60,13 @@ private:
void discard_stale_ring_heads();
void release_all_ring_frames();
+ struct LentSlotReleaser;
+
VideoEdgeQueue m_edge;
NonnullRefPtr<VideoFrameSlotDirectory> m_slot_directory;
Delegates m_delegates;
+ NonnullRefPtr<LentSlotReleaser> m_lent_slot_releaser;
+ bool m_has_stopped_consuming { false };
u32 m_expected_seek_id { 0 };
RefPtr<VideoFrame> m_current_frame;
diff --git a/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp b/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp
index 763f083cdde..58f1312f4bf 100644
--- a/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp
+++ b/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp
@@ -20,6 +20,7 @@ namespace Media {
class RemoteVideoSink::ThreadData : public AtomicRefCounted<RemoteVideoSink::ThreadData> {
public:
ThreadData(VideoEdgeQueue, Delegates, PresentedFramePage);
+ ~ThreadData();
VideoEdgeQueue const& edge() const { return m_edge; }
PresentedFramePage const& presented_frame_page() const { return m_presented_frame_page; }
@@ -40,6 +41,8 @@ public:
void pump_thread_loop();
void request_exit();
+ void stop_and_reclaim_unconsumed_frames();
+ bool has_lent_slots() const;
void release_all_lends();
private:
@@ -61,10 +64,11 @@ private:
ConditionVariable m_wait_condition { m_mutex };
RefPtr<VideoProducer> m_input;
bool m_exit_requested { false };
+ bool m_pump_thread_exited { false };
bool m_wake_pending { false };
u32 m_actual_seek_id { 0 };
- Mutex m_lent_pools_mutex;
+ mutable Mutex m_lent_pools_mutex;
HashMap<VideoFramePoolID, LentPool> m_lent_pools;
Function<void(VideoFramePoolID)> m_pool_retired_observer;
PresentedFramePage m_presented_frame_page;
@@ -143,6 +147,8 @@ void RemoteVideoSink::notify_space_available() { m_thread_data->notify_space_ava
void RemoteVideoSink::release_slot(VideoFramePoolID pool_id, u32 slot_index) { m_thread_data->release_slot(pool_id, slot_index); }
Optional<RemoteVideoSink::SlotStorage> RemoteVideoSink::lent_slot_storage(VideoFramePoolID pool_id, u32 slot_index) { return m_thread_data->lent_slot_storage(pool_id, slot_index); }
void RemoteVideoSink::set_pool_retired_observer(Function<void(VideoFramePoolID)> observer) { m_thread_data->set_pool_retired_observer(move(observer)); }
+void RemoteVideoSink::stop_and_reclaim_unconsumed_frames() { m_thread_data->stop_and_reclaim_unconsumed_frames(); }
+bool RemoteVideoSink::has_lent_slots() const { return m_thread_data->has_lent_slots(); }
RemoteVideoSink::ThreadData::ThreadData(VideoEdgeQueue edge, Delegates delegates, PresentedFramePage presented_frame_page)
: m_edge(move(edge))
@@ -151,6 +157,11 @@ RemoteVideoSink::ThreadData::ThreadData(VideoEdgeQueue edge, Delegates delegates
{
}
+RemoteVideoSink::ThreadData::~ThreadData()
+{
+ release_all_lends();
+}
+
ErrorOr<void> RemoteVideoSink::ThreadData::connect_input(NonnullRefPtr<VideoProducer> const& producer)
{
producer->set_wake_handler([this] {
@@ -231,6 +242,27 @@ void RemoteVideoSink::ThreadData::request_exit()
input->set_wake_handler(nullptr);
}
+void RemoteVideoSink::ThreadData::stop_and_reclaim_unconsumed_frames()
+{
+ request_exit();
+ {
+ MutexLocker locker { m_mutex };
+ while (!m_pump_thread_exited)
+ m_wait_condition.wait();
+ }
+
+ for (auto item = m_edge.peek(); item.has_value(); item = m_edge.peek()) {
+ release_slot(item->handle.pool_id, item->handle.slot_index);
+ m_edge.consume();
+ }
+}
+
+bool RemoteVideoSink::ThreadData::has_lent_slots() const
+{
+ MutexLocker locker { m_lent_pools_mutex };
+ return !m_lent_pools.is_empty();
+}
+
void RemoteVideoSink::ThreadData::wait_on_pump_thread(MutexLocker<Mutex>&)
{
while (!m_wake_pending && !m_exit_requested)
@@ -308,7 +340,9 @@ void RemoteVideoSink::ThreadData::pump_thread_loop()
wait_on_pump_thread(locker);
}
- release_all_lends();
+ MutexLocker locker { m_mutex };
+ m_pump_thread_exited = true;
+ m_wait_condition.broadcast();
}
void RemoteVideoSink::ThreadData::enqueue_frame(VideoFrame const& frame, u32 seek_id)
diff --git a/Libraries/LibMedia/Sinks/RemoteVideoSink.h b/Libraries/LibMedia/Sinks/RemoteVideoSink.h
index 2291dfefa36..41b38ef8c17 100644
--- a/Libraries/LibMedia/Sinks/RemoteVideoSink.h
+++ b/Libraries/LibMedia/Sinks/RemoteVideoSink.h
@@ -79,6 +79,9 @@ public:
// Observes the retire_pool delegate, for a host that maps slots for readers beyond the far-end consumer.
void set_pool_retired_observer(Function<void(VideoFramePoolID)>);
+ void stop_and_reclaim_unconsumed_frames();
+ bool has_lent_slots() const;
+
private:
class ThreadData;
diff --git a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp
index f4dcf74f70c..373dc6bcfe8 100644
--- a/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp
+++ b/Libraries/LibMedia/VideoPresentation/VideoPresentationClientConnection.cpp
@@ -47,6 +47,8 @@ void VideoPresentationClientConnection::release_edge(VideoSinkHandle handle)
}
if (!edge_id_to_release.has_value())
return;
+ if (auto consumer = m_edge_states.get(*edge_id_to_release)->consumer)
+ consumer->stop_consuming();
m_edge_states.remove(*edge_id_to_release);
async_release_video_edge(*edge_id_to_release);
}
diff --git a/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.cpp b/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.cpp
index 9063a7ddeff..9ced2749898 100644
--- a/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.cpp
+++ b/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.cpp
@@ -27,6 +27,7 @@ void VideoPresentationServerConnection::die()
for (auto& entry : m_edge_states)
PlaybackManager::release_video_edge({}, entry.value.handle, *entry.value.pump);
m_edge_states.clear();
+ m_released_edge_pumps_with_lent_slots.clear();
}
void VideoPresentationServerConnection::create_video_edge(VideoSinkHandle video_sink_handle, u64 edge_id)
@@ -127,6 +128,10 @@ void VideoPresentationServerConnection::release_video_edge(u64 edge_id)
auto pump = it->value.pump;
m_edge_states.remove(it);
PlaybackManager::release_video_edge({}, handle, *pump);
+
+ pump->stop_and_reclaim_unconsumed_frames();
+ if (pump->has_lent_slots())
+ m_released_edge_pumps_with_lent_slots.set(edge_id, move(pump));
}
void VideoPresentationServerConnection::request_start(u64 edge_id)
@@ -143,8 +148,16 @@ void VideoPresentationServerConnection::request_seek(u64 edge_id, AK::Duration t
void VideoPresentationServerConnection::release_slot(u64 edge_id, VideoFramePoolID pool_id, u32 slot_index)
{
- if (auto it = m_edge_states.find(edge_id); it != m_edge_states.end())
+ if (auto it = m_edge_states.find(edge_id); it != m_edge_states.end()) {
it->value.pump->release_slot(pool_id, slot_index);
+ return;
+ }
+ auto released_pump = m_released_edge_pumps_with_lent_slots.get(edge_id);
+ if (!released_pump.has_value())
+ return;
+ (*released_pump)->release_slot(pool_id, slot_index);
+ if (!(*released_pump)->has_lent_slots())
+ m_released_edge_pumps_with_lent_slots.remove(edge_id);
}
void VideoPresentationServerConnection::notify_space_available(u64 edge_id)
diff --git a/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.h b/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.h
index d55d72f12ab..22293c7429e 100644
--- a/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.h
+++ b/Libraries/LibMedia/VideoPresentation/VideoPresentationServerConnection.h
@@ -56,6 +56,7 @@ private:
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_slots;
HashMap<u64, VideoSinkHandle> m_pending_edge_handles_by_edge_id;
};
diff --git a/Tests/LibMedia/TestRemoteVideoNode.cpp b/Tests/LibMedia/TestRemoteVideoNode.cpp
index 97b4c18c341..3b8947b0fb7 100644
--- a/Tests/LibMedia/TestRemoteVideoNode.cpp
+++ b/Tests/LibMedia/TestRemoteVideoNode.cpp
@@ -633,6 +633,46 @@ TEST_CASE(current_frame_ignores_handles_whose_lend_was_released)
EXPECT(sink->current_frame() == nullptr);
}
+TEST_CASE(stopped_pump_reclaims_only_the_frames_left_in_the_ring)
+{
+ auto& loop = never_destroyed_event_loop();
+
+ auto retired_pool_count = 0;
+ RemoteVideoSink::Delegates delegates;
+ delegates.ring_data_available = [] { };
+ delegates.transmit_seek = [](InvalidateHeldData) { };
+ delegates.transmit_time_reader = [](MediaTimeReader const&) { };
+ delegates.announce_slot = [](VideoFramePoolID, u32, Core::AnonymousBuffer, RefPtr<VideoSurface>) { };
+ delegates.retire_pool = [&](VideoFramePoolID) { retired_pool_count++; };
+ auto sink = MUST(RemoteVideoSink::create(move(delegates)));
+
+ auto pool = MUST(VideoFramePool::create());
+ auto producer = ScriptedVideoProducer::create();
+ producer->append_frame(create_pooled_frame(*pool, AK::Duration::zero()));
+ producer->append_frame(create_pooled_frame(*pool, AK::Duration::from_milliseconds(33)));
+
+ auto ring_fd = MUST(Core::System::dup(sink->edge().ring_fd()));
+ auto consumer_edge = MUST(VideoEdgeQueue::create(ring_fd, sink->edge().header_buffer()));
+
+ MUST(sink->connect_input(producer));
+ sink->start_input();
+
+ EXPECT(spin_until(loop, [&] {
+ auto newest = consumer_edge.peek_newest();
+ return newest.has_value() && newest->handle.timestamp == AK::Duration::from_milliseconds(33);
+ }));
+ auto taken_handle = consumer_edge.peek()->handle;
+ consumer_edge.consume();
+
+ sink->stop_and_reclaim_unconsumed_frames();
+ EXPECT(sink->has_lent_slots());
+ EXPECT_EQ(retired_pool_count, 0);
+
+ sink->release_slot(taken_handle.pool_id, taken_handle.slot_index);
+ EXPECT(!sink->has_lent_slots());
+ EXPECT_EQ(retired_pool_count, 1);
+}
+
TEST_CASE(consumer_forgets_announced_slots_on_suspension)
{
auto producer_edge = MUST(VideoEdgeQueue::create());