Skip to content

Truly independent web browser

LibMedia: Drop a late video frame only once the next one is due

sideshowbarkerbf978661f4fe
diff --git a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
index b7bc27013d3..efd50853df7 100644
--- a/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
+++ b/Libraries/LibMedia/Sinks/DisplayingVideoSink.cpp
@@ -262,7 +262,9 @@ DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now)
last_status = next.status;
m_input_checkpoint = next.checkpoint;
if (!is_terminal(last_status)) {
- if (last_status == PipelineStatus::HaveData) {
+ // A passed frame stays on screen until the next one replaces it.
+ auto next_frame_is_due = last_status == PipelineStatus::HaveData && next.frame->timestamp() <= current_time;
+ if (next_frame_is_due) {
if (!m_seek_resolution_is_pending_frame_at_target) {
passed_frame_count++;
last_passed_frame_was_late = true;
@@ -273,7 +275,8 @@ DisplayingVideoSinkUpdateResult DisplayingVideoSink::update(MonotonicTime now)
m_cached_frames_are_discontinuous = true;
continue;
}
- break;
+ if (last_status != PipelineStatus::HaveData)
+ break;
}
}
m_current_frame = m_next_frame.release_nonnull();
diff --git a/Tests/LibMedia/TestDisplayingVideoSink.cpp b/Tests/LibMedia/TestDisplayingVideoSink.cpp
index fcb805c881a..12b209a8854 100644
--- a/Tests/LibMedia/TestDisplayingVideoSink.cpp
+++ b/Tests/LibMedia/TestDisplayingVideoSink.cpp
@@ -294,6 +294,35 @@ TEST_CASE(frames_passed_while_waiting_on_a_late_frame_are_counted_as_dropped)
EXPECT(sink->take_frame_counts().is_zero());
}
+TEST_CASE(frame_past_its_duration_is_presented_until_the_next_frame_is_due)
+{
+ 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));
+ 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);
+ EXPECT_EQ(sink->take_frame_counts().presented, 1u);
+
+ // With reordered frames, a container can give a frame a duration of a single tick, although the frame after it
+ // isn't due for another 40ms.
+ clock->seek(AK::Duration::from_milliseconds(1060));
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1040), AK::Duration::from_milliseconds(1)));
+ producer->append_frame(create_test_frame(*pool, AK::Duration::from_milliseconds(1080), AK::Duration::from_milliseconds(40)));
+ EXPECT(sink->update(MonotonicTime::now()).new_frame_available);
+ EXPECT_EQ(sink->current_frame()->timestamp(), AK::Duration::from_milliseconds(1040));
+ auto frame_counts = sink->take_frame_counts();
+ EXPECT_EQ(frame_counts.presented, 1u);
+ EXPECT_EQ(frame_counts.dropped, 0u);
+}
+
TEST_CASE(frames_passed_by_an_update_landing_on_time_are_counted_as_presented)
{
never_destroyed_event_loop();