Truly independent web browser
LibMedia+LibWeb: Start video sink inputs once ticking is decided
Zaggy1024e9eee95a75f9
diff --git a/Libraries/LibMedia/PlaybackManager.cpp b/Libraries/LibMedia/PlaybackManager.cppindex b6a5d8f78fa..bba8fde2516 100644--- a/Libraries/LibMedia/PlaybackManager.cpp+++ b/Libraries/LibMedia/PlaybackManager.cpp@@ -368,7 +368,7 @@ PipelineStatus PlaybackManager::combined_pipeline_status() constif (!track_data.handle.has_value())continue;auto track_status = track_data.sink_status;- if (!track_data.ticking && track_status != PipelineStatus::Error) {+ if (track_data.ticking == VideoSinkTicking::No && track_status != PipelineStatus::Error) {auto verified_end_time = verified_end_time_for_source(track_data.demuxer);if (verified_end_time.has_value() && current_time() >= *verified_end_time)track_status = PipelineStatus::EndOfStream;@@ -390,7 +390,7 @@ void PlaybackManager::update_pipeline_state()for (auto const& track_data : m_audio_track_datas)breakdown.appendff(", audio track {} {}", track_data.track.identifier(), track_data.enabled ? "enabled" : "disabled");for (auto const& track_data : m_video_track_datas)- breakdown.appendff(", video track {} sink {}{}", track_data.track.identifier(), track_data.sink_status, track_data.ticking ? "" : " unticked");+ breakdown.appendff(", video track {} sink {}{}", track_data.track.identifier(), track_data.sink_status, track_data.ticking == VideoSinkTicking::No ? " unticked" : "");dbgln("PlaybackManager({:p}): Combined status {} in {} ({})", this, status, m_handler->state(), breakdown.string_view());}}@@ -542,7 +542,8 @@ void PlaybackManager::attach_video_sink(VideoTrackData& track_data, NonnullRefPt});track_data.producer->set_checkpoint(m_latest_checkpoint.load(AK::MemoryOrder::memory_order_relaxed));MUST(video_sink->connect_input(track_data.producer));- video_sink->set_ticking(track_data.ticking);+ if (track_data.ticking != VideoSinkTicking::Undecided)+ video_sink->set_ticking(track_data.ticking == VideoSinkTicking::Yes);track_data.video_sink = move(video_sink);update_pipeline_state();dispatch_buffered_ranges_change();@@ -561,7 +562,7 @@ void PlaybackManager::reserve_video_sink_handle(Track const& track, VideoSinkHanif (track_data.handle.has_value())disable_video_sink_by_handle(*track_data.handle);track_data.handle = handle;- track_data.ticking = true;+ track_data.ticking = VideoSinkTicking::Undecided;video_sink_registrations().set(handle, this);apply_track_change_to_ended_state(resume_ended_playback);update_pipeline_state();@@ -609,9 +610,10 @@ void PlaybackManager::set_video_sink_ticking(VideoSinkHandle handle, bool tickinif (!manager)return;auto& track_data = manager->get_video_data_for_handle(handle);- if (track_data.ticking == ticking)+ auto new_ticking = ticking ? VideoSinkTicking::Yes : VideoSinkTicking::No;+ if (track_data.ticking == new_ticking)return;- track_data.ticking = ticking;+ track_data.ticking = new_ticking;if (track_data.video_sink)track_data.video_sink->set_ticking(ticking);manager->update_pipeline_state();diff --git a/Libraries/LibMedia/PlaybackManager.h b/Libraries/LibMedia/PlaybackManager.hindex c8df3ef3cf7..6f047091add 100644--- a/Libraries/LibMedia/PlaybackManager.h+++ b/Libraries/LibMedia/PlaybackManager.h@@ -30,6 +30,7 @@#include <LibMedia/PlaybackStates/PlaybackState.h>#include <LibMedia/Sinks/AudioOutputFactory.h>#include <LibMedia/Sinks/RemoteVideoSink.h>+#include <LibMedia/Sinks/VideoSinkTicking.h>#include <LibMedia/TimeRanges.h>#include <LibMedia/Track.h>#include <LibMedia/VideoSinkHandle.h>@@ -166,9 +167,9 @@ private:RefPtr<RemoteVideoSink> video_edge_sink { nullptr };PipelineStatus sink_status { PipelineStatus::Pending };PipelineCheckpoint sink_checkpoint { 0 };- // While ticking, the sink's dispatched status is live and remains the sole ending+ // While ticking, or until the owner decides, the sink's dispatched status is live and remains the sole ending// authority; while unticked, it is stale and the track ends at its verified end time.- bool ticking { true };+ VideoSinkTicking ticking { VideoSinkTicking::Undecided };Function<void(Gfx::Size<u32>)> on_resize { nullptr };};using VideoTrackDatas = Vector<VideoTrackData, EXPECTED_VIDEO_TRACK_COUNT>;diff --git a/Libraries/LibMedia/PlaybackStates/PlayingStateHandler.cpp b/Libraries/LibMedia/PlaybackStates/PlayingStateHandler.cppindex af9d572f59d..8dc95809d07 100644--- a/Libraries/LibMedia/PlaybackStates/PlayingStateHandler.cpp+++ b/Libraries/LibMedia/PlaybackStates/PlayingStateHandler.cpp@@ -46,7 +46,7 @@ void PlayingStateHandler::update_unticked_end_of_stream_timer()auto latest_verified_end_time = now;for (auto const& track_data : manager().m_video_track_datas) {- if (!track_data.handle.has_value() || track_data.ticking)+ if (!track_data.handle.has_value() || track_data.ticking != VideoSinkTicking::No)continue;if (is_terminal(track_data.sink_status))continue;diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cppindex a6f710c8307..5e0f375243a 100644--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp@@ -56,7 +56,7 @@ ErrorOr<void> DisplayingVideoSink::connect_input(NonnullRefPtr<VideoProducer> coif (output.status == PipelineStatus::Suspended) {m_next_frame = nullptr;// A ticking sink's update resumes the input if the time is moving, and reports the suspension otherwise.- if (m_is_ticking) {+ if (is_ticking()) {update(MonotonicTime::now());return;}@@ -72,16 +72,32 @@ ErrorOr<void> DisplayingVideoSink::connect_input(NonnullRefPtr<VideoProducer> coreturn true;return is_waiting_for_data(m_latest_status);}();- if (!m_is_ticking && !status_may_change)+ if (!is_ticking() && !status_may_change)return;update(MonotonicTime::now());});- m_seek_resolution_is_pending_frame_at_target = true;- input->seek(m_time_reader.current_time());- input->start();+ if (m_ticking != VideoSinkTicking::Undecided) {+ start_input();+ report_freshness_if_changed();+ }return {};}+void DisplayingVideoSink::start_input()+{+ VERIFY(m_ticking != VideoSinkTicking::Undecided);+ if (!is_ticking()) {+ m_input->suspend();+ return;+ }+ m_next_frame = nullptr;+ m_latest_status = PipelineStatus::Pending;+ m_seek_status = SeekStatus::CatchingUp;+ m_seek_resolution_is_pending_frame_at_target = true;+ m_input->seek(m_time_reader.current_time());+ m_input->start();+}+void DisplayingVideoSink::disconnect_input(NonnullRefPtr<VideoProducer> const& input){VERIFY(m_input == input);@@ -103,6 +119,10 @@ void DisplayingVideoSink::begin_seek(AK::Duration timestamp, InvalidateHeldDatam_last_dispatched_status = PipelineStatus::Pending;m_seek_resolution_is_pending_frame_at_target = true;+ // The input is seeked to the current time when ticking starts. Don't seek and possibly unsuspend the input yet.+ if (m_ticking == VideoSinkTicking::Undecided)+ return;+auto can_resolve_seek_within_cached_frames = [&] {if (invalidate_held_data == InvalidateHeldData::Yes)return false;@@ -153,7 +173,7 @@ void DisplayingVideoSink::dispatch_state_if_changed(PipelineStatus status, PipelDisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now){- if (m_input == nullptr)+ if (m_input == nullptr || m_ticking == VideoSinkTicking::Undecided)return {};if (is_ticking())m_has_updated_since_ticking_began = true;@@ -311,16 +331,22 @@ DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now)void DisplayingVideoSink::notify_clock_resumed(){- update(MonotonicTime::now());+ if (is_ticking())+ update(MonotonicTime::now());}void DisplayingVideoSink::set_ticking(bool ticking){- auto was_ticking = exchange(m_is_ticking, ticking);- if (ticking == was_ticking)+ auto new_ticking = ticking ? VideoSinkTicking::Yes : VideoSinkTicking::No;+ auto previous_ticking = exchange(m_ticking, new_ticking);+ if (previous_ticking == new_ticking)return;- if (ticking) {+ if (ticking)m_has_updated_since_ticking_began = false;+ if (previous_ticking == VideoSinkTicking::Undecided) {+ if (m_input != nullptr)+ start_input();+ } else if (ticking) {catch_up_to_clock();}report_freshness_if_changed();@@ -343,7 +369,7 @@ void DisplayingVideoSink::report_freshness_if_changed()void DisplayingVideoSink::catch_up_to_clock(){- if (m_seek_status != SeekStatus::None)+ if (m_ticking == VideoSinkTicking::Undecided || m_seek_status != SeekStatus::None)return;auto current_time = m_time_reader.current_time();diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h b/Libraries/LibMedia/Sinks/DisplayingVideoSink.hindex cbb8cfec04d..6edcc0e2779 100644--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h@@ -18,6 +18,7 @@#include <LibMedia/MediaTime.h>#include <LibMedia/PipelineStatus.h>#include <LibMedia/Sinks/VideoSink.h>+#include <LibMedia/Sinks/VideoSinkTicking.h>namespace Media {@@ -51,7 +52,8 @@ public:virtual void seek(AK::Duration timestamp, InvalidateHeldData = InvalidateHeldData::No) override;virtual void notify_clock_resumed() override;virtual void set_ticking(bool) override;- bool is_ticking() const { return m_is_ticking; }+ bool is_ticking() const { return m_ticking == VideoSinkTicking::Yes; }+ bool ticking_is_decided() const { return m_ticking != VideoSinkTicking::Undecided; }// Called when the sink starts or stops ticking, catching up, or having an up-to-date frame.void set_on_freshness_changed(Function<void()> handler) { m_on_freshness_changed = move(handler); }// Seeks the input to the clock's time if the current frame doesn't show it, whether or not the sink is ticking.@@ -70,11 +72,12 @@ public:private:void dispatch_state_if_changed(PipelineStatus, PipelineCheckpoint);void report_freshness_if_changed();+ void start_input();MediaTimeReader m_time_reader;RefPtr<VideoProducer> m_input;- bool m_is_ticking { true };+ VideoSinkTicking m_ticking { VideoSinkTicking::Undecided };bool m_has_updated_since_ticking_began { false };RefPtr<VideoFrame> m_next_frame;RefPtr<VideoFrame> m_current_frame;diff --git a/Libraries/LibMedia/Sinks/VideoSink.h b/Libraries/LibMedia/Sinks/VideoSink.hindex db58f61101c..9163299a0fa 100644--- a/Libraries/LibMedia/Sinks/VideoSink.h+++ b/Libraries/LibMedia/Sinks/VideoSink.h@@ -27,7 +27,7 @@ public:virtual void seek(AK::Duration timestamp, InvalidateHeldData = InvalidateHeldData::No) = 0;virtual void notify_clock_resumed() = 0;- // Whether the sink follows the clock.+ // Whether the sink follows the clock. Until its owner first decides, the sink leaves its input unstarted.virtual void set_ticking(bool) = 0;virtual void set_time_reader(MediaTimeReader) = 0;diff --git a/Libraries/LibMedia/Sinks/VideoSinkTicking.h b/Libraries/LibMedia/Sinks/VideoSinkTicking.hnew file mode 100644index 00000000000..8909563d659--- /dev/null+++ b/Libraries/LibMedia/Sinks/VideoSinkTicking.h@@ -0,0 +1,20 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Types.h>++namespace Media {++// Whether a video sink follows its clock, which its owner decides once the sink is reserved.+enum class VideoSinkTicking : u8 {+ Undecided,+ Yes,+ No,+};++}diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex c9ab0fd9d51..65d11c204cd 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -292,6 +292,12 @@ void HTMLMediaElement::initialize_element()self.sync_video_sink_ticking();}));+ static constexpr int capture_keepalive_timeout_ms = 5000;+ m_video_frame_capture_keepalive_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(capture_keepalive_timeout_ms), GC::weak_callback(*this, [](auto& self) {+ self.m_video_frame_was_recently_captured = false;+ self.video_sink_ticking_inputs_changed();+ }));+document().page().register_media_element({}, unique_id());}@@ -2323,8 +2329,10 @@ void HTMLMediaElement::attach_selected_video_track_sink(Media::Track const& tracvideo_element->set_intrinsic_video_dimensions(size);}));m_active_video_sink = make<ActiveVideoSink>(handle);- m_video_sink_is_ticking = true;+ m_video_sink_is_ticking = {};add_current_video_sink(handle);+ // NB: The input is not started until the sink's ticking state is decided, which a visible document does in the next+ // rendering update.video_sink_ticking_inputs_changed();if (auto* video_element = as_if<HTMLVideoElement>(this))Painting::push_video_paint_facts(*video_element);@@ -3272,23 +3280,20 @@ void HTMLMediaElement::sync_video_sink_ticking(DocumentWasLaidOut document_was_lm_playback_manager->set_video_sink_ticking(*handle, should_tick);}-// Visible documents re-evaluate sink ticking in each rendering update, once layout has settled. Hidden documents skip+// Visible documents re-evaluate sink ticking in a rendering update, once layout has settled. Hidden documents skip// that update, but decide without layout, so they re-evaluate right away.-void HTMLMediaElement::video_sink_ticking_inputs_changed() const+void HTMLMediaElement::video_sink_ticking_inputs_changed(){- if (document().hidden())+ if (document().hidden()) {sync_video_sink_ticking();+ return;+ }+ document().page().client().request_frame();}void HTMLMediaElement::note_frame_captured() const{- static constexpr int capture_keepalive_timeout_ms = 5000;m_video_frame_was_recently_captured = true;- if (!m_video_frame_capture_keepalive_timer) {- m_video_frame_capture_keepalive_timer = Core::Timer::create_single_shot(AK::Duration::from_milliseconds(capture_keepalive_timeout_ms), GC::weak_callback(*this, [](auto& self) {- self.m_video_frame_was_recently_captured = false;- }));- }m_video_frame_capture_keepalive_timer->restart();}diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.hindex 98a9e7d2386..d95d5aff4da 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h@@ -185,7 +185,7 @@ public:Yes,};void sync_video_sink_ticking(DocumentWasLaidOut = DocumentWasLaidOut::Yes) const;- void video_sink_ticking_inputs_changed() const;+ void video_sink_ticking_inputs_changed();void detach_video_sink_edge();GC::Ref<TextTrack> add_text_track(Bindings::TextTrackKind kind, Utf16View label, Utf16View language);@@ -340,8 +340,9 @@ private:virtual bool is_shown_elsewhere() const { return false; }virtual HTMLMediaElement& media_element_for_controls() { return *this; }- // Mirrors what PlaybackManager was last told; a freshly reserved sink is assumed to tick.- mutable bool m_video_sink_is_ticking { true };+ // Mirrors what PlaybackManager was last told. A freshly reserved sink is undecided, and leaves its input unstarted+ // until the first decision.+ mutable Optional<bool> m_video_sink_is_ticking;void note_frame_captured() const;bool is_eligible_for_autoplay() const;diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cppindex 13e1adcbea5..183c9b8445b 100644--- a/Libraries/LibWeb/Internals/Internals.cpp+++ b/Libraries/LibWeb/Internals/Internals.cpp@@ -2889,7 +2889,7 @@ Vector<String> Internals::tab_audibility_transitions()bool Internals::media_element_video_sink_is_ticking(HTML::HTMLMediaElement& element){- return element.m_video_sink_is_ticking;+ return element.m_video_sink_is_ticking.value_or(false);}void Internals::set_media_element_ready_state(HTML::HTMLMediaElement& element, u16 ready_state)diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 737a570531c..a767030118e 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -858,8 +858,8 @@ void CompositorState::catch_up_newly_drawn_screenshot_sinks(PendingScreenshot& s}screenshot.sinks[*index].is_drawn = true;auto& screenshot_sink = screenshot.sinks[*index];- // A sink found before its edge attached is caught up once it attaches.- if (screenshot_sink.was_asked_to_catch_up || !sink_state->sink)+ // A sink found before its edge attached or its ticking was decided is caught up once it can be.+ if (screenshot_sink.was_asked_to_catch_up || !sink_state->sink || !sink_state->sink->ticking_is_decided())continue;screenshot_sink.was_asked_to_catch_up = true;if (!sink_state->sink->frame_is_up_to_date())diff --git a/Tests/LibMedia/TestDisplayingVideoSink.cpp b/Tests/LibMedia/TestDisplayingVideoSink.cppindex 10449b855b6..02f5836e58f 100644--- a/Tests/LibMedia/TestDisplayingVideoSink.cpp+++ b/Tests/LibMedia/TestDisplayingVideoSink.cpp@@ -42,6 +42,7 @@ TEST_CASE(seek_resolving_frame_is_presented_even_when_stale)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);// The producer resolves the seek with a frame that covers the target, but whose presentation// interval has already passed relative to the clock.@@ -69,6 +70,7 @@ TEST_CASE(suspension_wake_releases_the_prefetched_frame)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);// Present the frame covering the current time, leaving the following frame prefetched.producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);@@ -102,6 +104,7 @@ TEST_CASE(updates_are_required_until_the_clock_stops_and_frames_are_presented)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);// A seek in flight keeps updates required while the clock is stopped.sink->seek(AK::Duration::from_milliseconds(1000));@@ -135,6 +138,7 @@ TEST_CASE(paused_sink_before_its_first_frame_stays_suspended)});auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);// The first frame starts after the paused time, so the seek resolves with nothing to present yet.sink->seek(AK::Duration::zero());@@ -175,6 +179,7 @@ TEST_CASE(seek_within_the_displayed_frame_resumes_a_suspended_input)});auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);EXPECT(sink->update(MonotonicTime::now()).new_frame_available);@@ -207,6 +212,7 @@ TEST_CASE(held_late_frame_waiting_on_input_reports_pending)});auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1033), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);@@ -238,6 +244,7 @@ TEST_CASE(seek_past_the_cached_frames_waits_for_the_frame_at_the_target)});auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1033), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);@@ -269,6 +276,7 @@ TEST_CASE(frames_passed_while_waiting_on_a_late_frame_are_counted_as_dropped)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -297,6 +305,7 @@ TEST_CASE(frames_passed_by_an_update_landing_on_time_are_counted_as_presented)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(4)));EXPECT(sink->update(MonotonicTime::now()).new_frame_available);@@ -324,6 +333,7 @@ TEST_CASE(frames_passed_by_a_seek_within_the_cached_frames_are_not_counted)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);// A long frame followed by a short one, so that a seek within the long one's conservative end lands after the// short one's.@@ -354,6 +364,7 @@ TEST_CASE(resuming_ticking_seeks_past_frames_held_behind_the_clock)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);auto seeks_before_resuming = producer->seek_count();producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));@@ -393,6 +404,7 @@ TEST_CASE(a_suspension_is_not_followed_by_the_status_it_replaced)});auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);sink->set_ticking(false);producer->append_output(nullptr, Media::PipelineStatus::Blocked);@@ -421,6 +433,7 @@ TEST_CASE(wakes_before_a_status_is_dispatched_leave_frames_held_behind_the_clockauto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -447,6 +460,7 @@ TEST_CASE(wakes_while_not_ticking_leave_frames_held_behind_the_clock)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -482,6 +496,7 @@ TEST_CASE(seek_while_not_ticking_resolves_once_a_frame_covers_the_target)});auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -516,6 +531,7 @@ TEST_CASE(frames_passed_catching_up_after_resuming_from_suspension_are_not_countauto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -562,6 +578,7 @@ TEST_CASE(frames_replaced_within_an_update_are_presented_when_the_last_one_is_onauto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -589,6 +606,7 @@ TEST_CASE(frames_passed_for_a_late_frame_ending_the_stream_are_counted_as_presenauto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -618,6 +636,7 @@ TEST_CASE(frames_out_of_order_with_a_passed_frame_are_not_counted)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);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);@@ -647,6 +666,7 @@ TEST_CASE(updates_are_required_until_the_last_frame_finishes_presenting)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);sink->seek(AK::Duration::from_milliseconds(1000));producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(100)));@@ -660,6 +680,88 @@ TEST_CASE(updates_are_required_until_the_last_frame_finishes_presenting)EXPECT(!sink->update(MonotonicTime::now()).may_require_updates);}+// Until the sink knows whether it ticks, starting its input either way could be wrong, so it holds the input and any+// seeks back and reports that it is waiting, which keeps the clock from starting.+TEST_CASE(undecided_sink_leaves_its_input_unstarted)+{+ auto& loop = never_destroyed_event_loop();++ auto clock = MUST(Media::MonotonicMediaClock::try_create());+ auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));+ Vector<Media::PipelineStatus> dispatched_statuses;+ sink->set_state_change_handler([&](Media::PipelineStatus status, Media::PipelineCheckpoint) { dispatched_statuses.append(status); });+ auto producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(producer));++ clock->seek(AK::Duration::from_milliseconds(500));+ sink->seek(AK::Duration::from_milliseconds(500));+ (void)sink->update(MonotonicTime::now());+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT(!sink->is_ticking());+ EXPECT_EQ(producer->start_count(), 0u);+ EXPECT_EQ(producer->suspend_count(), 0u);+ EXPECT_EQ(producer->seek_count(), 0u);+ EXPECT(dispatched_statuses.is_empty());++ sink->set_ticking(true);+ EXPECT_EQ(producer->start_count(), 1u);+ EXPECT_EQ(producer->suspend_count(), 0u);+ EXPECT_EQ(producer->seek_count(), 1u);+}++TEST_CASE(sink_first_decided_not_to_tick_suspends_its_input)+{+ auto& loop = never_destroyed_event_loop();++ auto clock = MUST(Media::MonotonicMediaClock::try_create());+ auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));+ Vector<Media::PipelineStatus> dispatched_statuses;+ sink->set_state_change_handler([&](Media::PipelineStatus status, Media::PipelineCheckpoint) { dispatched_statuses.append(status); });+ auto producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(producer));+ sink->seek(AK::Duration::zero());++ sink->set_ticking(false);+ EXPECT_EQ(producer->start_count(), 0u);+ EXPECT_EQ(producer->suspend_count(), 1u);+ EXPECT_EQ(producer->seek_count(), 0u);++ producer->append_output(nullptr, Media::PipelineStatus::Suspended);+ producer->wake();+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT_EQ(dispatched_statuses, Vector { Media::PipelineStatus::Suspended });++ // Playing on while unticked leaves the input suspended.+ clock->resume();+ sink->notify_clock_resumed();+ EXPECT_EQ(producer->seek_count(), 0u);++ // Ticking resumes the input at the current time.+ sink->set_ticking(true);+ EXPECT_EQ(producer->seek_count(), 1u);+}++// A sink that is connected after its ticking was decided starts its input right away.+TEST_CASE(sink_decided_before_connecting_starts_its_input_on_connect)+{+ never_destroyed_event_loop();++ auto clock = MUST(Media::MonotonicMediaClock::try_create());+ auto ticking_sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));+ ticking_sink->set_ticking(true);+ auto ticking_producer = ScriptedVideoProducer::create();+ MUST(ticking_sink->connect_input(ticking_producer));+ EXPECT_EQ(ticking_producer->start_count(), 1u);+ EXPECT_EQ(ticking_producer->seek_count(), 1u);++ auto unticked_sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));+ unticked_sink->set_ticking(false);+ auto unticked_producer = ScriptedVideoProducer::create();+ MUST(unticked_sink->connect_input(unticked_producer));+ EXPECT_EQ(unticked_producer->suspend_count(), 1u);+ EXPECT_EQ(unticked_producer->seek_count(), 0u);+}+TEST_CASE(frame_is_up_to_date_once_a_ticking_sink_has_caught_up){never_destroyed_event_loop();@@ -670,8 +772,12 @@ TEST_CASE(frame_is_up_to_date_once_a_ticking_sink_has_caught_up)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ EXPECT(!sink->frame_is_up_to_date());- // A sink that has yet to update has yet to bring its frame to the clock.+ // A sink that has just begun ticking has yet to bring its frame to the clock, even once it has updated.+ sink->set_ticking(true);+ EXPECT(!sink->frame_is_up_to_date());+ (void)sink->update(MonotonicTime::now());EXPECT(!sink->frame_is_up_to_date());producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));(void)sink->update(MonotonicTime::now());@@ -723,10 +829,10 @@ TEST_CASE(unticked_sink_catches_up_to_its_clock_on_request)// Paused at a time the sink has no frame for, nothing but a request makes it seek.clock->seek(AK::Duration::from_milliseconds(2000));(void)sink->update(MonotonicTime::now());- auto seeks_before_catching_up = producer->seek_count();+ EXPECT_EQ(producer->seek_count(), 0u);sink->catch_up_to_clock();- EXPECT_EQ(producer->seek_count(), seeks_before_catching_up + 1);+ EXPECT_EQ(producer->seek_count(), 1u);EXPECT(sink->is_catching_up());producer->consume();@@ -739,7 +845,7 @@ TEST_CASE(unticked_sink_catches_up_to_its_clock_on_request)// A frame that already shows the clock's time needs no seek.sink->catch_up_to_clock();- EXPECT_EQ(producer->seek_count(), seeks_before_catching_up + 1);+ EXPECT_EQ(producer->seek_count(), 1u);EXPECT(!sink->is_catching_up());}@@ -755,24 +861,89 @@ TEST_CASE(sink_reports_changes_to_its_freshness)auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ // An undecided sink and an unticked one are equally behind the clock.sink->set_ticking(false);- EXPECT_EQ(changes, 1u);+ EXPECT_EQ(changes, 0u);// Ticking starts a catch-up, and the frame is up to date once that is done.clock->seek(AK::Duration::from_milliseconds(1000));sink->set_ticking(true);- EXPECT_EQ(changes, 2u);+ EXPECT_EQ(changes, 1u);EXPECT(sink->is_catching_up());producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));producer->wake();loop.pump(Core::EventLoop::WaitMode::PollForEvents);EXPECT(sink->frame_is_up_to_date());- EXPECT_EQ(changes, 3u);+ EXPECT_EQ(changes, 2u);sink->set_ticking(true);- EXPECT_EQ(changes, 3u);+ EXPECT_EQ(changes, 2u);sink->set_ticking(false);- EXPECT_EQ(changes, 4u);+ EXPECT_EQ(changes, 3u);+}++TEST_CASE(sink_reconnected_to_a_new_input_takes_its_frames)+{+ 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 first_producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(first_producer));+ sink->set_ticking(true);++ // The frame after the current one waits in the sink until the clock reaches it.+ first_producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));+ first_producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1040), AK::Duration::from_milliseconds(40)));+ (void)sink->update(MonotonicTime::now());+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1000));+ EXPECT(sink->frame_is_up_to_date());+ sink->disconnect_input(first_producer);++ // The new input starts with a catch-up, which the sink reports.+ size_t changes = 0;+ sink->set_on_freshness_changed([&] { changes++; });+ auto second_producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(second_producer));+ EXPECT(!sink->frame_is_up_to_date());+ EXPECT_EQ(changes, 1u);+ auto frame = create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40));+ second_producer->append_frame(frame);+ (void)sink->update(MonotonicTime::now());+ EXPECT(!sink->is_catching_up());+ EXPECT_EQ(sink->current_frame().ptr(), frame.ptr());+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+}++// A catch-up that the previous input left blocked says nothing about the new input's catch-up.+TEST_CASE(sink_reconnected_after_a_blocked_catch_up_catches_up_its_new_input)+{+ 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 first_producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(first_producer));+ sink->set_ticking(true);+ first_producer->append_output(nullptr, Media::PipelineStatus::Blocked);+ (void)sink->update(MonotonicTime::now());+ EXPECT(!sink->is_catching_up());+ sink->disconnect_input(first_producer);++ auto second_producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(second_producer));+ EXPECT(sink->is_catching_up());+ EXPECT(!sink->frame_is_up_to_date());++ second_producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1000), AK::Duration::from_milliseconds(40)));+ (void)sink->update(MonotonicTime::now());+ EXPECT(!sink->is_catching_up());+ EXPECT(sink->frame_is_up_to_date());+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);}// Data that has yet to arrive holds a catch-up up indefinitely, so the frame from before it counts as up to date.@@ -785,6 +956,7 @@ TEST_CASE(catch_up_that_waits_for_data_leaves_the_frame_up_to_date)auto sink = MUST(Media::DisplayingVideoSink::try_create(clock->time_reader()));auto producer = ScriptedVideoProducer::create();MUST(sink->connect_input(producer));+ sink->set_ticking(true);producer->append_frame(create_test_frame(*pool, AK::Duration::zero(), AK::Duration::from_milliseconds(40)));(void)sink->update(MonotonicTime::now());EXPECT(sink->frame_is_up_to_date());diff --git a/Tests/LibMedia/TestMediaCommon.h b/Tests/LibMedia/TestMediaCommon.hindex 457b8238b35..2f68e4491a7 100644--- a/Tests/LibMedia/TestMediaCommon.h+++ b/Tests/LibMedia/TestMediaCommon.h@@ -170,8 +170,8 @@ public:m_wake_handler();}- virtual void start() override { }- virtual void suspend() override { }+ virtual void start() override { m_start_count++; }+ virtual void suspend() override { m_suspend_count++; }virtual Media::VideoProducerOutput peek() override{MutexLocker locker { m_mutex };@@ -188,6 +188,8 @@ public:virtual void seek(AK::Duration, Media::InvalidateHeldData = Media::InvalidateHeldData::No) override { m_seek_count++; }size_t seek_count() const { return m_seek_count; }+ size_t start_count() const { return m_start_count; }+ size_t suspend_count() const { return m_suspend_count; }private:ScriptedVideoProducer() = default;@@ -195,6 +197,8 @@ private:Mutex m_mutex;Vector<Media::VideoProducerOutput> m_outputs;size_t m_seek_count { 0 };+ size_t m_start_count { 0 };+ size_t m_suspend_count { 0 };Media::PipelineWakeHandler m_wake_handler;};diff --git a/Tests/LibMedia/TestPipelineNodes.cpp b/Tests/LibMedia/TestPipelineNodes.cppindex 06dc467dd5b..3fb5ee573ca 100644--- a/Tests/LibMedia/TestPipelineNodes.cpp+++ b/Tests/LibMedia/TestPipelineNodes.cpp@@ -463,6 +463,7 @@ TEST_CASE(displaying_video_sink_reports_a_suspended_input_while_unticked)Optional<Media::PipelineStatus> last_dispatched_status;sink->set_state_change_handler([&](Media::PipelineStatus status, Media::PipelineCheckpoint) { last_dispatched_status = status; });TRY_OR_FAIL(sink->connect_input(producer));+ sink->set_ticking(true);EXPECT(pump_until_the_sink_displays_a_frame(loop, *sink, *producer, *clock));@@ -485,6 +486,7 @@ TEST_CASE(displaying_video_sink_demand_seeks_a_suspended_input)auto clock = TRY_OR_FAIL(Media::MonotonicMediaClock::try_create());auto sink = TRY_OR_FAIL(Media::DisplayingVideoSink::try_create(clock->time_reader()));TRY_OR_FAIL(sink->connect_input(producer));+ sink->set_ticking(true);// Display the first frame, then leave the sink unticked until the producer suspends.EXPECT(pump_until_the_sink_displays_a_frame(loop, *sink, *producer, *clock));diff --git a/Tests/LibMedia/TestRemoteVideoNode.cpp b/Tests/LibMedia/TestRemoteVideoNode.cppindex 1060a4dccbf..0254c347279 100644--- a/Tests/LibMedia/TestRemoteVideoNode.cpp+++ b/Tests/LibMedia/TestRemoteVideoNode.cpp@@ -686,7 +686,7 @@ TEST_CASE(video_presentation_edge_carries_the_ticking_decision_to_its_sink)}auto handle = manager->reserve_video_sink_handle(manager->video_tracks()[0]);- PlaybackManager::set_video_sink_ticking(handle, false);+ PlaybackManager::set_video_sink_ticking(handle, true);auto paired = MUST(IPC::Transport::create_paired());auto server = VideoPresentationServerConnection::construct(move(paired.local));@@ -695,12 +695,15 @@ TEST_CASE(video_presentation_edge_carries_the_ticking_decision_to_its_sink)MUST(client->create_edge(handle, [&](NonnullRefPtr<DisplayingVideoSink> ready_sink) {sink = move(ready_sink);}));- EXPECT(spin_until(loop, [&] { return sink && !sink->is_ticking(); }));+ if (!spin_until(loop, [&] { return sink && sink->is_ticking(); })) {+ FAIL("Timed out waiting for a ticking video edge");+ return;+ }- PlaybackManager::set_video_sink_ticking(handle, true);- EXPECT(spin_until(loop, [&] { return sink->is_ticking(); }));PlaybackManager::set_video_sink_ticking(handle, false);EXPECT(spin_until(loop, [&] { return !sink->is_ticking(); }));+ PlaybackManager::set_video_sink_ticking(handle, true);+ EXPECT(spin_until(loop, [&] { return sink->is_ticking(); }));server->shutdown();}@@ -828,6 +831,7 @@ TEST_CASE(sink_resumes_a_suspended_consumer_when_the_clock_resumes)auto clock = MUST(MonotonicMediaClock::try_create());auto sink = MUST(DisplayingVideoSink::try_create(clock->time_reader()));MUST(sink->connect_input(consumer));+ sink->set_ticking(true);EXPECT_EQ(upstream_seeks, 1u);consumer->receive_frame(1, 1, create_received_frame(*pool, AK::Duration::zero()));