Skip to content

Truly independent web browser

LibMedia+Tests: Re-emit the last emitted frame/block when seeks skip

Zaggy10243c25bc71c142
diff --git a/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp b/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp
index a8d81cfde1f..5636447f07f 100644
--- a/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp
+++ b/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp
@@ -385,7 +385,6 @@ bool DecodedAudioProducer::ThreadData::handle_auto_suspension()
m_queue.clear();
m_decoder.clear();
- m_decoder_needs_keyframe_next_seek = true;
m_decoder_needs_codec_configuration_next_seek = true;
m_auto_suspend_requested = false;
m_current_halting_status = PipelineStatus::Suspended;
@@ -497,7 +496,6 @@ bool DecodedAudioProducer::ThreadData::seek_to_replace_decoder_if_it_cannot_deco
void DecodedAudioProducer::ThreadData::seek_to_resubmit_while_locked()
{
- m_decoder_needs_keyframe_next_seek = true;
m_decoder_needs_codec_configuration_next_seek = true;
if (m_last_processed_seek_id == m_seek_id)
m_seek_timestamp = m_earliest_available_timestamp;
@@ -534,11 +532,10 @@ void DecodedAudioProducer::ThreadData::handle_seek()
m_frame_awaiting_decoder_replacement.clear();
}
- auto seek_options = DemuxerSeekOptions::None;
+ // All of our audio codecs have independently decodable frames, so always moving the demuxer is cheap.
+ auto seek_options = DemuxerSeekOptions::Force;
if (m_decoder_needs_codec_configuration_next_seek)
seek_options |= DemuxerSeekOptions::NeedCodecConfiguration;
- if (m_decoder_needs_keyframe_next_seek)
- seek_options |= DemuxerSeekOptions::Force;
auto demuxer_seek_result_or_error = m_demuxer->seek_to_most_recent_keyframe(m_track, timestamp, seek_options);
if (demuxer_seek_result_or_error.is_error() && demuxer_seek_result_or_error.error().category() != DecoderErrorCategory::EndOfStream) {
if (demuxer_seek_result_or_error.error().category() == DecoderErrorCategory::Aborted)
@@ -548,14 +545,9 @@ void DecodedAudioProducer::ThreadData::handle_seek()
}
m_decoder_needs_codec_configuration_next_seek = false;
- m_decoder_needs_keyframe_next_seek = false;
- auto demuxer_seek_result = demuxer_seek_result_or_error.value_or(DemuxerSeekResult::MovedPosition);
-
- if (demuxer_seek_result == DemuxerSeekResult::MovedPosition) {
- flush_decoder();
- last_block.clear();
- }
+ flush_decoder();
+ last_block.clear();
auto new_seek_id = m_seek_id.load();
diff --git a/Libraries/LibMedia/Producers/DecodedAudioProducer.h b/Libraries/LibMedia/Producers/DecodedAudioProducer.h
index 2308abd52a8..c95191b6bff 100644
--- a/Libraries/LibMedia/Producers/DecodedAudioProducer.h
+++ b/Libraries/LibMedia/Producers/DecodedAudioProducer.h
@@ -147,7 +147,6 @@ private:
AudioDecoderSelection m_decoder_that_failed_due_to_missing_features;
Optional<CodedFrame> m_frame_awaiting_decoder_replacement;
OwnPtr<AudioDecoder> m_decoder;
- bool m_decoder_needs_keyframe_next_seek { false };
bool m_decoder_needs_codec_configuration_next_seek { true };
NonnullOwnPtr<Audio::AudioConverter> m_converter;
i64 m_last_output_frame { NumericLimits<i64>::min() };
diff --git a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp b/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp
index 7e644534756..146bc5c8269 100644
--- a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp
+++ b/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp
@@ -569,6 +569,10 @@ void DecodedVideoProducer::ThreadData::handle_seek()
return;
}
+ // If the demuxer keeps its position, the decoder won't output this frame again.
+ if (!m_held_frames_are_invalid && m_last_queued_frame != nullptr && m_last_queued_frame->timestamp() <= timestamp)
+ last_frame = m_last_queued_frame;
+
m_queue.clear();
m_held_frames_are_invalid = false;
m_last_queued_frame = nullptr;
diff --git a/Tests/LibMedia/TestDecodedVideoProducer.cpp b/Tests/LibMedia/TestDecodedVideoProducer.cpp
index 5dc8f4097b0..99b9e5a509f 100644
--- a/Tests/LibMedia/TestDecodedVideoProducer.cpp
+++ b/Tests/LibMedia/TestDecodedVideoProducer.cpp
@@ -747,6 +747,64 @@ TEST_CASE(seeks_land_on_the_right_frame_while_output_arrives_asynchronously)
}
}
+// Seeking back to an earlier time within the frame of the last sample the demuxer read lets the demuxer keep its
+// position, so the decoder will not output that frame again. The seek must still land on it.
+TEST_CASE(a_seek_back_within_the_last_decoded_frame_lands_on_it)
+{
+ auto& loop = never_destroyed_event_loop();
+ // A decoder that delays its output would hold back the last frame read before the demuxer blocks.
+ auto withheld_outputs = make_ref_counted<WithheldOutputs>();
+ WithholdingVideoDecoder::set_withheld_outputs_for_new_decoders(withheld_outputs);
+ ScopeGuard stop_withholding = [] { WithholdingVideoDecoder::set_withheld_outputs_for_new_decoders(nullptr); };
+
+ auto sample_timestamps = presentation_timestamps_of_first_samples("./vp9_in_webm.webm"sv, 4);
+ VERIFY(sample_timestamps.size() == 4);
+ auto last_read_sample_timestamp = sample_timestamps[2];
+ auto frame_interval_in_milliseconds = (sample_timestamps[3] - last_read_sample_timestamp).to_milliseconds();
+
+ auto [inner_demuxer, track] = demuxer_and_video_track_for("./vp9_in_webm.webm"sv);
+ auto demuxer = BlockingDemuxer::create(inner_demuxer, 3);
+ auto producer = TRY_OR_FAIL(Media::DecodedVideoProducer::try_create(loop, demuxer, track));
+ producer->set_error_handler([&](Media::DecoderError&& error) {
+ FAIL(ByteString::formatted("An error occurred while decoding: {}", error.description()));
+ });
+ producer->seek(last_read_sample_timestamp + AK::Duration::from_milliseconds(frame_interval_in_milliseconds * 3 / 4));
+ producer->start();
+ AsynchronousOutputReleaser releaser { withheld_outputs };
+
+ auto time_limit = AK::Duration::from_seconds(5);
+ auto peek_frame = [&] -> RefPtr<Media::VideoFrame> {
+ auto deadline = MonotonicTime::now_coarse() + time_limit;
+ while (MonotonicTime::now_coarse() < deadline) {
+ auto output = producer->peek();
+ if (output.status == Media::PipelineStatus::HaveData)
+ return output.frame;
+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);
+ }
+ return nullptr;
+ };
+
+ auto frame = peek_frame();
+ if (frame == nullptr) {
+ FAIL("The first seek did not resolve");
+ return;
+ }
+ EXPECT_EQ(frame->timestamp(), last_read_sample_timestamp);
+ if (!demuxer->wait_until_a_read_blocks(time_limit)) {
+ FAIL("Timed out waiting for the decode thread to block on the demuxer");
+ return;
+ }
+
+ producer->seek(last_read_sample_timestamp + AK::Duration::from_milliseconds(frame_interval_in_milliseconds / 4));
+ demuxer->stop_blocking_reads();
+ frame = peek_frame();
+ if (frame == nullptr) {
+ FAIL("The second seek did not resolve");
+ return;
+ }
+ EXPECT_EQ(frame->timestamp(), last_read_sample_timestamp);
+}
+
// A stream can grow after the demuxer reported its end, by which point the decoder was drained on that report.
// Nothing can be decoded from the samples that follow, so they are ignored rather than ending playback in an error.
TEST_CASE(samples_that_follow_a_transient_end_of_stream_are_ignored)