Truly independent web browser
LibMedia+Compositor: Don't count video frames while not ticking a sink
Zaggy10249f22b1983a55
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cppindex 7175518e2cb..84472a7e96c 100644--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp@@ -55,8 +55,13 @@ ErrorOr<void> DisplayingVideoSink::connect_input(NonnullRefPtr<VideoProducer> codispatch_state_if_changed(PipelineStatus::Suspended);return;}+ // Update the sink to dispatch a status if we're still resolving a seek. If we're not, avoid the update so that+ // we don't count dropped frames due to the decoder waiting for room while we're not consuming frames.+ if (!m_is_ticking && m_seek_status == SeekStatus::None && !m_seek_resolution_is_pending_frame_at_target)+ return;update(MonotonicTime::now());});+ m_seek_resolution_is_pending_frame_at_target = true;input->seek(m_time_reader.current_time());input->start();return {};@@ -276,6 +281,13 @@ void DisplayingVideoSink::notify_clock_resumed()update(MonotonicTime::now());}+void DisplayingVideoSink::set_ticking(bool ticking)+{+ auto was_ticking = exchange(m_is_ticking, ticking);+ if (ticking && !was_ticking)+ seek_back_to_current_time();+}+void DisplayingVideoSink::seek_back_to_current_time(){if (m_seek_status != SeekStatus::None)diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h b/Libraries/LibMedia/Sinks/DisplayingVideoSink.hindex b39d8821bec..eb629cab514 100644--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h@@ -50,7 +50,8 @@ public:virtual void seek(AK::Duration timestamp, InvalidateHeldData = InvalidateHeldData::No) override;virtual void notify_clock_resumed() override;- void seek_back_to_current_time();+ void set_ticking(bool);+ bool is_ticking() const { return m_is_ticking; }DisplayingVideoSinkUpdateResult update(MonotonicTime now);RefPtr<VideoFrame> current_frame() const;@@ -60,10 +61,12 @@ public:private:void dispatch_state_if_changed(PipelineStatus);+ void seek_back_to_current_time();MediaTimeReader m_time_reader;RefPtr<VideoProducer> m_input;+ bool m_is_ticking { true };RefPtr<VideoFrame> m_next_frame;RefPtr<VideoFrame> m_current_frame;bool m_cached_frames_are_discontinuous { false };diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 9e507e84d6a..f29aa4f8dd9 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -352,9 +352,9 @@ void CompositorState::set_video_sink_ticking(CompositorStateWebContentClient& clauto* sink_state = video_sink_state(client, handle);if (!sink_state)return;- 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();+ sink_state->should_tick = should_tick;+ if (sink_state->sink)+ sink_state->sink->set_ticking(should_tick);if (should_tick && sink_state->sink)present_contexts_drawing_video_sink(client, handle);update_unpainted_video_update_scheduling();@@ -365,7 +365,7 @@ void CompositorState::set_video_sink_ticking(CompositorStateWebContentClient& cl// stop it, and resolves as soon as the edge is created.bool CompositorState::video_sink_updates_are_needed(VideoSinkState const& sink_state){- return sink_state.sink != nullptr && sink_state.should_tick;+ return sink_state.sink != nullptr && sink_state.sink->is_ticking();}void CompositorState::on_video_sink_ready(CompositorStateWebContentClient& client, Media::VideoSinkHandle handle, NonnullRefPtr<Media::DisplayingVideoSink> const& sink)@@ -374,6 +374,7 @@ void CompositorState::on_video_sink_ready(CompositorStateWebContentClient& clienif (!sink_state)return;sink_state->sink = sink;+ sink->set_ticking(sink_state->should_tick);sink->set_on_present_needed([this, &client, handle] {if (auto* sink_state = video_sink_state(client, handle); sink_state && sink_state->sink)client.video_sink_presented_frame(handle, sink_state->sink->current_frame(), sink_state->sink->take_frame_counts());diff --git a/Tests/LibMedia/TestDisplayingVideoSink.cpp b/Tests/LibMedia/TestDisplayingVideoSink.cppindex 4f391dac09a..84214bf840e 100644--- a/Tests/LibMedia/TestDisplayingVideoSink.cpp+++ b/Tests/LibMedia/TestDisplayingVideoSink.cpp@@ -364,11 +364,13 @@ TEST_CASE(resuming_ticking_seeks_past_frames_held_behind_the_clock)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();+ sink->set_ticking(false);+ sink->set_ticking(true);EXPECT_EQ(producer->seek_count(), seeks_before_resuming);+ sink->set_ticking(false);clock->seek(AK::Duration::from_milliseconds(1200));- sink->seek_back_to_current_time();+ sink->set_ticking(true);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)));@@ -379,6 +381,75 @@ TEST_CASE(resuming_ticking_seeks_past_frames_held_behind_the_clock)EXPECT_EQ(frame_counts.dropped, 0u);}+TEST_CASE(wakes_while_not_ticking_leave_frames_held_behind_the_clock)+{+ 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);++ sink->set_ticking(false);+ clock->seek(AK::Duration::from_milliseconds(1200));+ for (auto timestamp : { 1040, 1080, 1120, 1160 })+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(timestamp), AK::Duration::from_milliseconds(40)));+ producer->wake();+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1000));+ EXPECT(sink->take_frame_counts().is_zero());++ auto seeks_before_resuming = producer->seek_count();+ sink->set_ticking(true);+ EXPECT_EQ(producer->seek_count(), seeks_before_resuming + 1);+}++TEST_CASE(seek_while_not_ticking_resolves_once_a_frame_covers_the_target)+{+ 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()));+ Vector<Media::PipelineStatus> dispatched_statuses;+ sink->set_state_change_handler([&dispatched_statuses](Media::PipelineStatus status) {+ dispatched_statuses.append(status);+ });+ 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);++ sink->set_ticking(false);+ clock->seek(AK::Duration::from_milliseconds(1200));+ dispatched_statuses.clear();+ sink->seek(AK::Duration::from_milliseconds(1200));++ // The first frame back falls short of the target, so the seek stays pending.+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1120), AK::Duration::from_milliseconds(40)));+ producer->wake();+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT(dispatched_statuses.is_empty());++ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1160), AK::Duration::from_milliseconds(80)));+ producer->wake();+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1160));+ EXPECT_EQ(dispatched_statuses, (Vector { Media::PipelineStatus::HaveData }));+}+TEST_CASE(frames_passed_catching_up_after_resuming_from_suspension_are_not_counted){auto& loop = never_destroyed_event_loop();