Truly independent web browser
LibMedia: Leave an input suspended when seeking an unticked video
Zaggy1024cacce0300612
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cppindex 4e13db18432..b7bc27013d3 100644--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp@@ -124,6 +124,17 @@ void DisplayingVideoSink::begin_seek(AK::Duration timestamp, InvalidateHeldDataif (m_ticking == VideoSinkTicking::Undecided)return;+ // If the sink isn't ticking, don't unsuspend the input unnecessarily. Let it catch up later. Data that the seek+ // invalidates may be what the current frame shows, so the seek replaces the frame then.+ if (seek_status == SeekStatus::InProgress && invalidate_held_data == InvalidateHeldData::No && !is_ticking() && m_input != nullptr && m_input->peek().status == PipelineStatus::Suspended) {+ m_next_frame = nullptr;+ m_seek_status = SeekStatus::None;+ m_seek_resolution_is_pending_frame_at_target = false;+ m_latest_status = PipelineStatus::Suspended;+ defer_latest_status_dispatch();+ return;+ }+auto can_resolve_seek_within_cached_frames = [&] {if (invalidate_held_data == InvalidateHeldData::Yes)return false;@@ -172,6 +183,22 @@ void DisplayingVideoSink::dispatch_latest_state_if_changed()m_on_state_changed(m_latest_status, m_input_checkpoint);}+// Dispatches with a deferred invoke to avoid reentrancy. This prevents a seek from resolving while an update or a seek+// is being processed.+void DisplayingVideoSink::defer_latest_status_dispatch()+{+ if (m_latest_status == m_last_dispatched_status && m_input_checkpoint == m_last_dispatched_checkpoint)+ return;+ Core::deferred_invoke([weak_self = make_weak_ref(), seek_id = m_seek_id] {+ auto self = weak_self.strong_ref();+ if (!self)+ return;+ if (seek_id != self->m_seek_id)+ return;+ self->dispatch_latest_state_if_changed();+ });+}+DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now){if (m_input == nullptr || m_ticking == VideoSinkTicking::Undecided)@@ -299,18 +326,7 @@ DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now)m_on_present_needed();m_latest_status = status_to_dispatch;- if (status_to_dispatch != m_last_dispatched_status || m_input_checkpoint != m_last_dispatched_checkpoint) {- // Dispatch the new state with a deferred invoke to avoid reentrancy. This prevents a seek from resolving while- // an update is being processed.- Core::deferred_invoke([weak_self = make_weak_ref(), seek_id = m_seek_id] {- auto self = weak_self.strong_ref();- if (!self)- return;- if (seek_id != self->m_seek_id)- return;- self->dispatch_latest_state_if_changed();- });- }+ defer_latest_status_dispatch();auto may_require_updates = !is_terminal(status_to_dispatch) && (time_state.is_advancing || m_seek_status != SeekStatus::None);diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h b/Libraries/LibMedia/Sinks/DisplayingVideoSink.hindex 8f683ab467f..3df79793779 100644--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h@@ -71,6 +71,7 @@ public:private:void dispatch_latest_state_if_changed();+ void defer_latest_status_dispatch();void report_freshness_if_changed();void start_input();diff --git a/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp b/Libraries/LibMedia/Sinks/RemoteVideoSink.cppindex 69c233e5293..02889f75f0f 100644--- a/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp+++ b/Libraries/LibMedia/Sinks/RemoteVideoSink.cpp@@ -115,12 +115,17 @@ void RemoteVideoSink::disconnect_input(NonnullRefPtr<VideoProducer> const& produvoid RemoteVideoSink::seek(AK::Duration timestamp, InvalidateHeldData invalidate_held_data){- if (invalidate_held_data == InvalidateHeldData::No)+ if (invalidate_held_data == InvalidateHeldData::No && m_consumer_is_ticking)m_thread_data->seek_input_directly(timestamp);m_thread_data->transmit_seek(invalidate_held_data);}void RemoteVideoSink::notify_clock_resumed() { m_thread_data->notify_clock_resumed(); }-void RemoteVideoSink::set_ticking(bool ticking) { m_thread_data->transmit_ticking(ticking); }++void RemoteVideoSink::set_ticking(bool ticking)+{+ m_consumer_is_ticking = ticking;+ m_thread_data->transmit_ticking(ticking);+}void RemoteVideoSink::seek_upstream(AK::Duration timestamp, InvalidateHeldData invalidate_held_data, u64 newest_received_lend_id) { m_thread_data->seek_upstream(timestamp, invalidate_held_data, newest_received_lend_id); }void RemoteVideoSink::set_time_reader(MediaTimeReader time_reader)diff --git a/Libraries/LibMedia/Sinks/RemoteVideoSink.h b/Libraries/LibMedia/Sinks/RemoteVideoSink.hindex 53819a1e7ef..099ae6ea878 100644--- a/Libraries/LibMedia/Sinks/RemoteVideoSink.h+++ b/Libraries/LibMedia/Sinks/RemoteVideoSink.h@@ -84,6 +84,9 @@ private:PipelineStateChangeHandler m_on_state_changed;PipelineResizeHandler m_on_resize;+ // A seek reaches the input directly, ahead of the consumer's request for it, only while the consumer ticks. One+ // that doesn't may leave its suspended input be for the seek.+ bool m_consumer_is_ticking { false };};}diff --git a/Tests/LibMedia/TestDisplayingVideoSink.cpp b/Tests/LibMedia/TestDisplayingVideoSink.cppindex 24ae4479541..fcb805c881a 100644--- a/Tests/LibMedia/TestDisplayingVideoSink.cpp+++ b/Tests/LibMedia/TestDisplayingVideoSink.cpp@@ -762,6 +762,67 @@ TEST_CASE(sink_decided_before_connecting_starts_its_input_on_connect)EXPECT_EQ(unticked_producer->seek_count(), 0u);}+// Seeking a sink that isn't ticking would otherwise wake its suspended input only to decode frames that nothing shows.+TEST_CASE(unticked_sink_resolves_a_seek_without_waking_its_suspended_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()));+ 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->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);+ producer->append_output(nullptr, Media::PipelineStatus::Suspended);+ sink->set_ticking(false);+ producer->wake();+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ auto seeks_before = producer->seek_count();++ dispatched_statuses.clear();+ clock->seek(AK::Duration::zero());+ sink->seek(AK::Duration::zero());+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT_EQ(producer->seek_count(), seeks_before);+ EXPECT_EQ(dispatched_statuses, Vector { Media::PipelineStatus::Suspended });+ // The frame from before the seek stays until the sink catches up.+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1000));++ sink->set_ticking(true);+ EXPECT_EQ(producer->seek_count(), seeks_before + 1);+ EXPECT_EQ(producer->last_seek_timestamp(), AK::Duration::zero());+}++// The frame a sink keeps may show data that a seek invalidates, so such a seek reaches the input even when it isn't+// ticking.+TEST_CASE(unticked_sink_passes_on_a_seek_that_invalidates_held_data)+{+ auto& loop = never_destroyed_event_loop();++ auto pool = MUST(Media::VideoFramePool::create());+ auto clock = MUST(Media::MonotonicMediaClock::try_create());+ 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());+ producer->append_output(nullptr, Media::PipelineStatus::Suspended);+ sink->set_ticking(false);+ producer->wake();+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);++ auto seeks_before = producer->seek_count();+ sink->seek(AK::Duration::zero(), Media::InvalidateHeldData::Yes);+ EXPECT_EQ(producer->seek_count(), seeks_before + 1);+}+TEST_CASE(frame_is_up_to_date_once_a_ticking_sink_has_caught_up){never_destroyed_event_loop();diff --git a/Tests/LibMedia/TestMediaCommon.h b/Tests/LibMedia/TestMediaCommon.hindex 70be4d3e121..a91acdee413 100644--- a/Tests/LibMedia/TestMediaCommon.h+++ b/Tests/LibMedia/TestMediaCommon.h@@ -185,9 +185,14 @@ public:(void)m_outputs.take_first();}virtual void set_wake_handler(Media::PipelineWakeHandler handler) override { m_wake_handler = move(handler); }- virtual void seek(AK::Duration, Media::InvalidateHeldData = Media::InvalidateHeldData::No) override { m_seek_count++; }+ virtual void seek(AK::Duration timestamp, Media::InvalidateHeldData = Media::InvalidateHeldData::No) override+ {+ m_seek_count++;+ m_last_seek_timestamp = timestamp;+ }size_t seek_count() const { return m_seek_count; }+ Optional<AK::Duration> last_seek_timestamp() const { return m_last_seek_timestamp; }size_t start_count() const { return m_start_count; }size_t suspend_count() const { return m_suspend_count; }@@ -197,6 +202,7 @@ private:Mutex m_mutex;Vector<Media::VideoProducerOutput> m_outputs;size_t m_seek_count { 0 };+ Optional<AK::Duration> m_last_seek_timestamp;size_t m_start_count { 0 };size_t m_suspend_count { 0 };Media::PipelineWakeHandler m_wake_handler;diff --git a/Tests/LibMedia/TestRemoteVideoNode.cpp b/Tests/LibMedia/TestRemoteVideoNode.cppindex 0254c347279..a6dfee60063 100644--- a/Tests/LibMedia/TestRemoteVideoNode.cpp+++ b/Tests/LibMedia/TestRemoteVideoNode.cpp@@ -298,6 +298,7 @@ void give_lookahead(RemoteVideoProducer& consumer, VideoFramePool& pool, Functiostruct LentFrameRecorder : public AtomicRefCounted<LentFrameRecorder> {Mutex mutex;Vector<RemoteVideoSink::LentFrame> lent_frames;+ Vector<bool> transmitted_ticking;size_t count(){@@ -323,8 +324,14 @@ NonnullRefPtr<RemoteVideoSink> make_recorded_sink(LentFrameRecorder& recorder)delegates.notify_clock_resumed = [] { };delegates.transmit_seek = [](InvalidateHeldData) { };delegates.transmit_time_reader = [](MediaTimeReader const&) { };- delegates.transmit_ticking = [](bool) { };- return MUST(RemoteVideoSink::create(move(delegates)));+ delegates.transmit_ticking = [recorder = NonnullRefPtr { recorder }](bool ticking) {+ MutexLocker locker { recorder->mutex };+ recorder->transmitted_ticking.append(ticking);+ };+ auto sink = MUST(RemoteVideoSink::create(move(delegates)));+ // The consumer these sinks stand in for ticks.+ sink->set_ticking(true);+ return sink;}NonnullRefPtr<RemoteVideoSink> make_sink_with_inert_delegates()@@ -940,6 +947,26 @@ TEST_CASE(sink_seeks_its_input_directly_when_the_consumer_holds_nothing_for_the_EXPECT_EQ(producer->seek_count(), 2u);}+// A consumer that doesn't tick may leave its suspended input be for a seek, so the seek waits for its request. The sink+// passes the ticking on to the consumer.+TEST_CASE(sink_leaves_seeks_to_a_consumer_that_does_not_tick)+{+ auto recorder = make_ref_counted<LentFrameRecorder>();+ auto sink = make_recorded_sink(*recorder);+ sink->set_ticking(false);+ {+ MutexLocker locker { recorder->mutex };+ EXPECT_EQ(recorder->transmitted_ticking, (Vector { true, false }));+ }+ auto producer = ScriptedVideoProducer::create();+ MUST(sink->connect_input(producer));++ sink->seek(AK::Duration::from_seconds(1));+ EXPECT_EQ(producer->seek_count(), 0u);+ sink->seek_upstream(AK::Duration::from_seconds(1), InvalidateHeldData::No, 0);+ EXPECT_EQ(producer->seek_count(), 1u);+}+// The consumer may seek on its own while a direct seek awaits its request, as when it starts ticking again. Its// request supersedes the direct seek, so the input follows the consumer, pumping resumes under the request's stamp,// and the request for the direct seek, which arrives after, is a seek of its own again.