Skip to content

Truly independent web browser

LibMedia+Compositor: Seek back to current time when sinks start ticking

Zaggy1024acac2d346111
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
index 05a9beff046..7175518e2cb 100644
--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
@@ -233,6 +233,7 @@ DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now)
if (time_is_changing) {
m_next_frame = nullptr;
m_seek_status = SeekStatus::InProgress;
+ m_seek_resolution_is_pending_frame_at_target = true;
m_input->seek(current_time);
status_to_dispatch = PipelineStatus::Pending;
} else {
@@ -275,6 +276,19 @@ void DisplayingVideoSink::notify_clock_resumed()
update(MonotonicTime::now());
}
+void DisplayingVideoSink::seek_back_to_current_time()
+{
+ if (m_seek_status != SeekStatus::None)
+ return;
+ auto current_time = m_time_reader.current_time();
+
+ // Don't request a seek to the same frame, or we'll wake upstream while suspended for no reason.
+ if (m_current_frame != nullptr && m_current_frame->contains_timestamp(current_time))
+ return;
+
+ seek(current_time);
+}
+
RefPtr<VideoFrame> DisplayingVideoSink::current_frame() const
{
return m_current_frame;
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h
index ea1ce934706..b39d8821bec 100644
--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h
+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h
@@ -50,6 +50,7 @@ public:
virtual void seek(AK::Duration timestamp, InvalidateHeldData = InvalidateHeldData::No) override;
virtual void notify_clock_resumed() override;
+ void seek_back_to_current_time();
DisplayingVideoSinkUpdateResult update(MonotonicTime now);
RefPtr<VideoFrame> current_frame() const;
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index b8b72b1d5c8..825eeeb50a9 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -292,7 +292,9 @@ void CompositorState::set_video_sink_ticking(CompositorStateWebContentClient& cl
auto* sink_state = video_sink_state(client, handle);
if (!sink_state)
return;
- sink_state->should_tick = should_tick;
+ auto was_ticking = exchange(sink_state->should_tick, should_tick);
+ if (should_tick && !was_ticking && sink_state->sink)
+ sink_state->sink->seek_back_to_current_time();
if (should_tick && sink_state->sink)
present_contexts_drawing_video_sink(client, handle);
update_unpainted_video_update_scheduling();
diff --git a/Tests/LibMedia/TestDisplayingVideoSink.cpp b/Tests/LibMedia/TestDisplayingVideoSink.cpp
index cea00f986d4..4f391dac09a 100644
--- a/Tests/LibMedia/TestDisplayingVideoSink.cpp
+++ b/Tests/LibMedia/TestDisplayingVideoSink.cpp
@@ -345,6 +345,86 @@ TEST_CASE(frames_passed_by_a_seek_within_the_cached_frames_are_not_counted)
EXPECT_EQ(frame_counts.dropped, 0u);
}
+TEST_CASE(resuming_ticking_seeks_past_frames_held_behind_the_clock)
+{
+ never_destroyed_event_loop();
+
+ auto pool = MUST(Media::VideoFramePool::create());
+ auto clock = MUST(Media::MonotonicMediaClock::try_create());
+ clock->seek(AK::Duration::from_milliseconds(1000));
+
+ auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));
+ auto producer = ScriptedVideoProducer::create();
+ MUST(sink->connect_input(producer));
+ auto seeks_before_resuming = producer->seek_count();
+
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1040), AK::Duration::from_milliseconds(40)));
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ EXPECT_EQ(sink->take_frame_counts().presented, 1u);
+
+ // The displayed frame still covers the clock, so there is nothing to skip.
+ sink->seek_back_to_current_time();
+ EXPECT_EQ(producer->seek_count(), seeks_before_resuming);
+
+ clock->seek(AK::Duration::from_milliseconds(1200));
+ sink->seek_back_to_current_time();
+ EXPECT_EQ(producer->seek_count(), seeks_before_resuming + 1);
+
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1200), AK::Duration::from_milliseconds(40)));
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1200));
+ auto frame_counts = sink->take_frame_counts();
+ EXPECT_EQ(frame_counts.presented, 1u);
+ EXPECT_EQ(frame_counts.dropped, 0u);
+}
+
+TEST_CASE(frames_passed_catching_up_after_resuming_from_suspension_are_not_counted)
+{
+ auto& loop = never_destroyed_event_loop();
+
+ auto pool = MUST(Media::VideoFramePool::create());
+ auto clock = MUST(Media::MonotonicMediaClock::try_create());
+ clock->seek(AK::Duration::from_milliseconds(1000));
+
+ auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));
+ auto producer = ScriptedVideoProducer::create();
+ MUST(sink->connect_input(producer));
+
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+ EXPECT_EQ(sink->take_frame_counts().presented, 1u);
+
+ producer->append_output(nullptr, Media::PipelineStatus::Suspended);
+ producer->wake();
+
+ // A running clock makes the update resume the input with a seek.
+ clock->seek(AK::Duration::from_milliseconds(2000));
+ clock->resume();
+ auto seeks_before_resuming = producer->seek_count();
+ sink->update(MonotonicTime::now());
+ EXPECT_EQ(producer->seek_count(), seeks_before_resuming + 1);
+ producer->consume();
+
+ // The clock passes the frame at the target before it arrives, and the following frames trail behind it.
+ clock->seek(AK::Duration::from_milliseconds(2100));
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(2000), AK::Duration::from_milliseconds(40)));
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+
+ clock->seek(AK::Duration::from_milliseconds(2200));
+ for (auto timestamp : { 2040, 2080 })
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(timestamp), AK::Duration::from_milliseconds(40)));
+ sink->update(MonotonicTime::now());
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(2200), AK::Duration::from_milliseconds(40)));
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(2200));
+ EXPECT_EQ(sink->take_frame_counts().dropped, 0u);
+}
+
TEST_CASE(frames_replaced_within_an_update_are_presented_when_the_last_one_is_on_time)
{
never_destroyed_event_loop();