Skip to content

Truly independent web browser

LibMedia: Ensure that video seeks resolve with a frame at the target

Zaggy1024f07079ee1512
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
index 5233635751c..de6f473b0d1 100644
--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
@@ -74,6 +74,7 @@ void DisplayingVideoSink::seek(AK::Duration timestamp, InvalidateHeldData invali
{
m_seek_id++;
m_last_dispatched_status = PipelineStatus::Pending;
+ m_seek_resolution_is_pending_frame_at_target = true;
auto can_resolve_seek_within_cached_frames = [&] {
if (invalidate_held_data == InvalidateHeldData::Yes)
@@ -193,18 +194,19 @@ DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now)
auto status_to_dispatch = last_status;
if (m_current_frame != nullptr && m_seek_status == SeekStatus::None) {
- AK::Duration current_frame_end;
- if (is_terminal(last_status))
- current_frame_end = m_current_frame->timestamp() + m_current_frame->duration();
- else
- current_frame_end = m_current_frame->conservative_end();
- if (current_time <= current_frame_end)
+ auto current_frame_end = m_current_frame->conservative_end();
+ if (is_terminal(last_status) || m_seek_resolution_is_pending_frame_at_target)
+ current_frame_end = m_current_frame->end();
+ if (current_time < current_frame_end)
status_to_dispatch = PipelineStatus::HaveData;
}
if (m_next_frame != nullptr && m_next_frame->timestamp() > current_time)
status_to_dispatch = PipelineStatus::HaveData;
+ if (resolves_seek(status_to_dispatch))
+ m_seek_resolution_is_pending_frame_at_target = false;
+
if (last_status == PipelineStatus::Suspended) {
auto time_is_changing = time_state.is_advancing || m_seek_status != SeekStatus::None;
if (time_is_changing) {
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h
index 79e05ac8d68..4c1f00a6a8e 100644
--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.h
+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.h
@@ -60,6 +60,7 @@ private:
RefPtr<VideoFrame> m_next_frame;
RefPtr<VideoFrame> m_current_frame;
bool m_cached_frames_are_discontinuous { false };
+ bool m_seek_resolution_is_pending_frame_at_target { false };
enum class SeekStatus : u8 {
None,
diff --git a/Tests/LibMedia/TestDisplayingVideoSink.cpp b/Tests/LibMedia/TestDisplayingVideoSink.cpp
index 7b85947581d..12d1b0038c4 100644
--- a/Tests/LibMedia/TestDisplayingVideoSink.cpp
+++ b/Tests/LibMedia/TestDisplayingVideoSink.cpp
@@ -225,6 +225,41 @@ TEST_CASE(held_late_frame_waiting_on_input_reports_pending)
EXPECT_EQ(dispatched_statuses, (Vector { Media::PipelineStatus::Pending }));
}
+TEST_CASE(seek_past_the_cached_frames_waits_for_the_frame_at_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_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);
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+
+ // The target lies after the cached frames end, but before the later one's conservative end.
+ clock->seek(AK::Duration::from_milliseconds(1080));
+ dispatched_statuses.clear();
+ sink->seek(AK::Duration::from_milliseconds(1080));
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+ EXPECT(dispatched_statuses.is_empty());
+
+ producer->append_output(create_test_frame(*pool, AK::Duration::from_milliseconds(1066), AK::Duration::from_milliseconds(33)), Media::PipelineStatus::HaveData);
+ producer->wake();
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1066));
+ EXPECT_EQ(dispatched_statuses, (Vector { Media::PipelineStatus::HaveData }));
+}
+
// The input reaching its end leaves the last frame on screen until its interval ends, and only an update after that
// can report the end of the stream.
TEST_CASE(updates_are_required_until_the_last_frame_finishes_presenting)