Truly independent web browser
LibMedia: Check for pending seeks more often in producers
Zaggy10240f85a4a582ad
diff --git a/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp b/Libraries/LibMedia/Producers/DecodedAudioProducer.cppindex 1573af977d5..a8d81cfde1f 100644--- a/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp+++ b/Libraries/LibMedia/Producers/DecodedAudioProducer.cpp@@ -169,25 +169,26 @@ void DecodedAudioProducer::ThreadData::note_consumer_activity_while_locked() conm_auto_suspend_requested = false;}-void DecodedAudioProducer::ThreadData::wait_for_queue_space_or_auto_suspend_while_locked()+DecodedAudioProducer::ThreadData::WaitResult DecodedAudioProducer::ThreadData::wait_for_queue_space_while_locked(){if (m_requested_state != RequestedState::Running)- return;- if (m_auto_suspended || m_auto_suspend_requested)- return;+ return WaitResult::ExitDecodeLoop;+ if (m_auto_suspend_requested)+ return WaitResult::ExitDecodeLoop;if (m_queue.size() < m_queue_max_size)- return;+ return WaitResult::ContinueDecoding;auto idle_at = m_last_consumer_activity + m_auto_suspend_idle_timeout;auto now = MonotonicTime::now();if (now < idle_at) {if (m_wait_condition.wait_for(idle_at - now))- return;+ return WaitResult::ContinueDecoding;if (MonotonicTime::now() < idle_at)- return;+ return WaitResult::ContinueDecoding;}m_auto_suspend_requested = true;+ return WaitResult::ExitDecodeLoop;}AudioDecoderSelection DecodedAudioProducer::ThreadData::select_decoder_for_frame(CodedFrame const& frame, AudioDecoderSelection after) const@@ -381,13 +382,11 @@ bool DecodedAudioProducer::ThreadData::handle_auto_suspension()auto locker = take_lock();if (!m_auto_suspend_requested)return false;- VERIFY(!m_auto_suspended);m_queue.clear();m_decoder.clear();m_decoder_needs_keyframe_next_seek = true;m_decoder_needs_codec_configuration_next_seek = true;- m_auto_suspended = true;m_auto_suspend_requested = false;m_current_halting_status = PipelineStatus::Suspended;// The consumer's last peek predates the suspension, so wake it regardless of what it saw.@@ -401,7 +400,6 @@ bool DecodedAudioProducer::ThreadData::handle_auto_suspension()break;m_wait_condition.wait();}- m_auto_suspended = false;m_current_halting_status = PipelineStatus::Pending;return true;@@ -507,13 +505,13 @@ void DecodedAudioProducer::ThreadData::seek_to_resubmit_while_locked()m_downstream_needs_wake = true;}-bool DecodedAudioProducer::ThreadData::handle_seek()+void DecodedAudioProducer::ThreadData::handle_seek(){VERIFY(m_decode_thread_id.is_current_thread());auto seek_id = m_seek_id.load();if (m_last_processed_seek_id == seek_id)- return false;+ return;AK::Duration timestamp;AudioBlock last_block;@@ -546,7 +544,7 @@ bool DecodedAudioProducer::ThreadData::handle_seek()if (demuxer_seek_result_or_error.error().category() == DecoderErrorCategory::Aborted)continue;handle_error(demuxer_seek_result_or_error.release_error());- return true;+ return;}m_decoder_needs_codec_configuration_next_seek = false;@@ -577,7 +575,7 @@ bool DecodedAudioProducer::ThreadData::handle_seek()continue;}handle_error(error_or_decoder_was_replaced.release_error());- return true;+ return;}if (error_or_decoder_was_replaced.value())continue;@@ -586,7 +584,7 @@ bool DecodedAudioProducer::ThreadData::handle_seek()resolve_seek(seek_id);if (!last_block.is_empty())queue_block(last_block);- return true;+ return;}if (block_result.error().category() == DecoderErrorCategory::NeedsMoreInput)@@ -597,14 +595,14 @@ bool DecodedAudioProducer::ThreadData::handle_seek()continue;}handle_error(block_result.release_error());- return true;+ return;}if (current_block.contains_media_time(timestamp)) {auto locker = take_lock();resolve_seek(seek_id);queue_block(current_block);- return true;+ return;}if (current_block.media_time_start() > timestamp) {@@ -615,7 +613,7 @@ bool DecodedAudioProducer::ThreadData::handle_seek()queue_block(last_block);queue_block(current_block);- return true;+ return;}last_block = current_block;@@ -632,12 +630,12 @@ bool DecodedAudioProducer::ThreadData::handle_seek()if (m_decoder == nullptr) {auto locker = take_lock();resolve_seek(seek_id);- return true;+ return;}m_decoder->signal_end_of_stream();} else if (category != DecoderErrorCategory::Aborted) {handle_error(coded_frame_result.release_error());- return true;+ return;}continue;}@@ -650,7 +648,7 @@ bool DecodedAudioProducer::ThreadData::handle_seek()continue;}handle_error(decode_result.release_error());- return true;+ return;}new_seek_id = m_seek_id;@@ -684,25 +682,30 @@ void DecodedAudioProducer::ThreadData::push_data_and_decode_a_block()if (check_if_seek_is_needed_or_allow_demuxer_reads(m_last_processed_seek_id))return;+ auto seek_is_pending = [&] {+ return m_seek_id.load(AK::MemoryOrder::memory_order_relaxed) != m_last_processed_seek_id;+ };+while (m_decoder != nullptr) {+ if (seek_is_pending())+ return;+auto queue_size = [&] {auto locker = take_lock();return m_queue.size();}();while (queue_size >= m_queue_max_size) {- if (handle_seek())- return;-- if (handle_auto_suspension())- return;-{auto locker = take_lock();+ if (seek_is_pending())+ return;+queue_size = m_queue.size();if (queue_size < m_queue_max_size)continue;- wait_for_queue_space_or_auto_suspend_while_locked();+ if (wait_for_queue_space_while_locked() == WaitResult::ExitDecodeLoop)+ return;if (should_thread_exit_while_locked())return;queue_size = m_queue.size();@@ -742,6 +745,9 @@ void DecodedAudioProducer::ThreadData::push_data_and_decode_a_block()if (m_frame_awaiting_decoder_replacement.has_value())return;+ if (seek_is_pending())+ return;+auto sample_result = m_demuxer->get_next_sample_for_track(m_track);if (sample_result.is_error()) {auto category = sample_result.error().category();diff --git a/Libraries/LibMedia/Producers/DecodedAudioProducer.h b/Libraries/LibMedia/Producers/DecodedAudioProducer.hindex de6cd11991b..2308abd52a8 100644--- a/Libraries/LibMedia/Producers/DecodedAudioProducer.h+++ b/Libraries/LibMedia/Producers/DecodedAudioProducer.h@@ -96,7 +96,7 @@ private:void dispatch_error(DecoderError&&);void flush_decoder();DecoderErrorOr<void> retrieve_next_block(AudioBlock&);- bool handle_seek();+ void handle_seek();void resolve_seek(u32 seek_id);bool seek_to_resubmit_if_decoder_lost_its_state(DecoderError const&);bool seek_to_replace_decoder_if_it_cannot_decode_the_stream(DecoderError const&);@@ -126,7 +126,11 @@ private:};void note_consumer_activity_while_locked() const;- void wait_for_queue_space_or_auto_suspend_while_locked();+ enum class WaitResult : u8 {+ ContinueDecoding,+ ExitDecodeLoop,+ };+ WaitResult wait_for_queue_space_while_locked();void dispatch_read_blocked_change(ReadBlocked blocked);Core::EventLoop& m_main_thread_event_loop;@@ -164,7 +168,6 @@ private:AK::Duration const m_auto_suspend_idle_timeout;mutable MonotonicTime m_last_consumer_activity { MonotonicTime::now() };- bool m_auto_suspended { false };mutable bool m_auto_suspend_requested { false };};diff --git a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp b/Libraries/LibMedia/Producers/DecodedVideoProducer.cppindex 8aa5d725ab0..7e644534756 100644--- a/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp+++ b/Libraries/LibMedia/Producers/DecodedVideoProducer.cpp@@ -349,23 +349,24 @@ void DecodedVideoProducer::ThreadData::note_consumer_activity_while_locked() conm_auto_suspend_requested = false;}-void DecodedVideoProducer::ThreadData::wait_to_decode_or_auto_suspend_while_locked()+DecodedVideoProducer::ThreadData::WaitResult DecodedVideoProducer::ThreadData::wait_for_frame_storage_while_locked(){if (m_requested_state != RequestedState::Running)- return;- if (m_auto_suspended || m_auto_suspend_requested)- return;+ return WaitResult::ExitDecodeLoop;+ if (m_auto_suspend_requested)+ return WaitResult::ExitDecodeLoop;auto idle_at = m_last_consumer_activity + m_auto_suspend_idle_timeout;auto now = MonotonicTime::now();if (now < idle_at) {if (m_wait_state->condition.wait_for(idle_at - now))- return;+ return WaitResult::ContinueDecoding;if (MonotonicTime::now() < idle_at)- return;+ return WaitResult::ContinueDecoding;}m_auto_suspend_requested = true;+ return WaitResult::ExitDecodeLoop;}DecodedVideoProducer::FrameQueue& DecodedVideoProducer::ThreadData::queue()@@ -442,7 +443,6 @@ bool DecodedVideoProducer::ThreadData::handle_auto_suspension()auto locker = take_lock();if (!m_auto_suspend_requested)return false;- VERIFY(!m_auto_suspended);m_queue.clear();m_last_queued_frame = nullptr;@@ -450,7 +450,6 @@ bool DecodedVideoProducer::ThreadData::handle_auto_suspension()m_decoder.clear();m_decoder_needs_keyframe_next_seek = true;m_decoder_needs_codec_configuration_next_seek = true;- m_auto_suspended = true;m_auto_suspend_requested = false;m_current_halting_status = PipelineStatus::Suspended;// The consumer's last peek predates the suspension, so wake it regardless of what it saw.@@ -464,7 +463,6 @@ bool DecodedVideoProducer::ThreadData::handle_auto_suspension()break;m_wait_state->condition.wait();}- m_auto_suspended = false;m_current_halting_status = PipelineStatus::Pending;return true;@@ -535,13 +533,13 @@ void DecodedVideoProducer::ThreadData::seek_to_resubmit_while_locked()m_downstream_needs_wake = true;}-bool DecodedVideoProducer::ThreadData::handle_seek()+void DecodedVideoProducer::ThreadData::handle_seek(){VERIFY(m_decode_thread_id.is_current_thread());auto seek_id = m_seek_id.load(AK::MemoryOrder::memory_order_relaxed);if (seek_id == m_last_processed_seek_id.load(AK::MemoryOrder::memory_order_relaxed))- return false;+ return;AK::Duration timestamp;RefPtr<VideoFrame> last_frame;@@ -558,7 +556,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()auto locker = take_lock();seek_id = m_seek_id.load(MemoryOrder::memory_order_relaxed);if (seek_id == m_last_processed_seek_id.load(MemoryOrder::memory_order_relaxed))- return false;+ return;seek_id = m_seek_id;timestamp = m_seek_timestamp;m_demuxer->reset_blocking_reads_aborted_for_track(m_track);@@ -568,7 +566,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()re_emit_last_frame_if_at_end_of_stream_while_locked();resolve_seek(seek_id);dispatch_wake_if_needed_while_locked();- return true;+ return;}m_queue.clear();@@ -592,7 +590,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()if (demuxer_seek_result_or_error.error().category() == DecoderErrorCategory::Aborted)continue;handle_error(demuxer_seek_result_or_error.release_error());- return true;+ return;}m_decoder_needs_codec_configuration_next_seek = false;@@ -625,7 +623,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()continue;}handle_error(error_or_decoder_was_replaced.release_error());- return true;+ return;}if (error_or_decoder_was_replaced.value())continue;@@ -648,7 +646,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()resolve_seek(seek_id);if (last_frame != nullptr)queue_frame(last_frame.release_nonnull());- return true;+ return;}if (frame_result.error().category() == DecoderErrorCategory::NeedsMoreInput)@@ -657,7 +655,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()if (frame_result.error().category() == DecoderErrorCategory::TryAgain) {auto locker = take_lock();if (should_thread_exit_while_locked())- return true;+ return;if (m_seek_id != seek_id) {new_seek_id = m_seek_id;continue;@@ -677,7 +675,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()continue;}handle_error(frame_result.release_error());- return true;+ return;}auto current_frame = frame_result.release_value();@@ -686,7 +684,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()auto locker = take_lock();resolve_seek(seek_id);queue_frame(current_frame);- return true;+ return;}if (current_frame->timestamp() > timestamp) {@@ -697,7 +695,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()queue_frame(last_frame.release_nonnull());queue_frame(current_frame);- return true;+ return;}last_frame = move(current_frame);@@ -715,12 +713,12 @@ bool DecodedVideoProducer::ThreadData::handle_seek()if (m_decoder == nullptr) {auto locker = take_lock();resolve_seek(seek_id);- return true;+ return;}m_decoder->signal_end_of_stream();} else if (category != DecoderErrorCategory::Aborted) {handle_error(coded_frame_result.release_error());- return true;+ return;}continue;}@@ -741,7 +739,7 @@ bool DecodedVideoProducer::ThreadData::handle_seek()continue;}handle_error(decode_result.release_error());- return true;+ return;}new_seek_id = m_seek_id;@@ -779,21 +777,24 @@ void DecodedVideoProducer::ThreadData::push_data_and_decode_some_frames()if (check_if_seek_is_needed_or_allow_demuxer_reads(m_last_processed_seek_id.load(AK::MemoryOrder::memory_order_relaxed)))return;+ auto seek_is_pending = [&] {+ return m_seek_id.load(AK::MemoryOrder::memory_order_relaxed) != m_last_processed_seek_id.load(AK::MemoryOrder::memory_order_relaxed);+ };+while (m_decoder != nullptr) {+ if (seek_is_pending())+ return;+auto frame_result = m_decoder->take_next_output(m_track.video_data().cicp);if (frame_result.is_error()) {if (frame_result.error().category() == DecoderErrorCategory::NeedsMoreInput)break;if (frame_result.error().category() == DecoderErrorCategory::TryAgain) {- if (handle_seek())+ auto locker = take_lock();+ if (seek_is_pending())return;-- if (handle_auto_suspension())+ if (!m_wait_state->frame_storage_was_freed && wait_for_frame_storage_while_locked() == WaitResult::ExitDecodeLoop)return;-- auto locker = take_lock();- if (!m_wait_state->frame_storage_was_freed)- wait_to_decode_or_auto_suspend_while_locked();m_wait_state->frame_storage_was_freed = false;if (should_thread_exit_while_locked())return;@@ -830,6 +831,9 @@ void DecodedVideoProducer::ThreadData::push_data_and_decode_some_frames()if (m_frame_awaiting_decoder_replacement.has_value())return;+ if (seek_is_pending())+ return;+auto sample_result = m_demuxer->get_next_sample_for_track(m_track);m_wait_state->output_should_interrupt_demuxer_reads.store(false, AK::MemoryOrder::memory_order_relaxed);if (sample_result.is_error()) {diff --git a/Libraries/LibMedia/Producers/DecodedVideoProducer.h b/Libraries/LibMedia/Producers/DecodedVideoProducer.hindex 77530676ffb..3c031b937ad 100644--- a/Libraries/LibMedia/Producers/DecodedVideoProducer.h+++ b/Libraries/LibMedia/Producers/DecodedVideoProducer.h@@ -103,7 +103,7 @@ private:void invoke_on_main_thread_while_locked(Invokee);void queue_frame(NonnullRefPtr<VideoFrame> const&);void dispatch_error(DecoderError&&);- bool handle_seek();+ void handle_seek();void resolve_seek(u32 seek_id);bool seek_to_resubmit_if_decoder_lost_its_state(DecoderError const&);bool seek_to_replace_decoder_if_it_cannot_decode_the_stream(DecoderError const&);@@ -127,7 +127,11 @@ private:};void note_consumer_activity_while_locked() const;- void wait_to_decode_or_auto_suspend_while_locked();+ enum class WaitResult : u8 {+ ContinueDecoding,+ ExitDecodeLoop,+ };+ WaitResult wait_for_frame_storage_while_locked();void dispatch_read_blocked_change(ReadBlocked blocked);Core::EventLoop& m_main_thread_event_loop;@@ -176,7 +180,6 @@ private:AK::Duration const m_auto_suspend_idle_timeout;mutable MonotonicTime m_last_consumer_activity { MonotonicTime::now() };- bool m_auto_suspended { false };mutable bool m_auto_suspend_requested { false };};diff --git a/Tests/LibMedia/TestDecodedVideoProducer.cpp b/Tests/LibMedia/TestDecodedVideoProducer.cppindex a4c7f6b1f41..5dc8f4097b0 100644--- a/Tests/LibMedia/TestDecodedVideoProducer.cpp+++ b/Tests/LibMedia/TestDecodedVideoProducer.cpp@@ -53,12 +53,15 @@ public:}m_samples_served++;}+ if (m_seek_is_expected.load())+ m_reads_before_the_expected_seek++;return m_inner->get_next_sample_for_track(track);}virtual AK::Duration select_fast_seek_target_for_track(Media::Track const& track, AK::Duration target, Media::SeekMode mode) override { return m_inner->select_fast_seek_target_for_track(track, target, mode); }virtual Media::DecoderErrorOr<Media::DemuxerSeekResult> seek_to_most_recent_keyframe(Media::Track const& track, AK::Duration timestamp, Media::DemuxerSeekOptions options) override{+ m_seek_is_expected.store(false);m_seek_count++;return m_inner->seek_to_most_recent_keyframe(track, timestamp, options);}@@ -108,6 +111,9 @@ public:size_t seek_count() const { return m_seek_count; }+ void expect_seek() { m_seek_is_expected.store(true); }+ size_t reads_before_the_expected_seek() const { return m_reads_before_the_expected_seek; }+private:BlockingDemuxer(NonnullRefPtr<Media::Demuxer> inner, size_t samples_before_blocking): m_inner(move(inner))@@ -125,6 +131,8 @@ private:bool m_reads_are_aborted { false };bool m_read_is_blocked { false };Atomic<size_t> m_seek_count { 0 };+ Atomic<bool> m_seek_is_expected { false };+ Atomic<size_t> m_reads_before_the_expected_seek { 0 };};// Presents one track whose coded frames come from one demuxer and then another, so that the codec of a track's@@ -642,6 +650,42 @@ TEST_CASE(a_seek_resolves_on_output_that_arrives_while_the_demuxer_blocks)demuxer->stop_blocking_reads();}+// Output finishing while a seek is requested wakes the decode thread between taking output and reading the next+// sample. It must not read, and so submit, another sample of the old position before it seeks.+TEST_CASE(no_further_sample_is_read_once_a_seek_is_requested)+{+ auto& loop = never_destroyed_event_loop();+ 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 [inner_demuxer, track] = demuxer_and_video_track_for("./vp9_in_webm.webm"sv);+ auto demuxer = BlockingDemuxer::create(inner_demuxer, NumericLimits<size_t>::max());+ 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->start();++ // Once as many outputs are withheld as the decoder decodes at once, the decode thread waits in it for one.+ auto time_limit = AK::Duration::from_seconds(5);+ if (!withheld_outputs->wait_until_the_caller_waits_for_output(time_limit)) {+ FAIL("Timed out waiting for the decode thread to wait for output");+ return;+ }+ EXPECT_EQ(withheld_outputs->withheld_count(), WithholdingVideoDecoder::MAXIMUM_WITHHELD_OUTPUTS);++ demuxer->expect_seek();+ producer->seek(AK::Duration::from_milliseconds(500));+ EXPECT(withheld_outputs->release_next());++ auto deadline = MonotonicTime::now_coarse() + time_limit;+ while (demuxer->seek_count() == 0 && MonotonicTime::now_coarse() < deadline)+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ EXPECT_EQ(demuxer->seek_count(), 1u);+ EXPECT_EQ(demuxer->reads_before_the_expected_seek(), 0u);+}+// A decoder finishing output on its own threads wakes the decode thread at arbitrary points of its loops. Seeking back// and forth through such a decoder must land on the frame covering each target every time, and never report the end// of the stream or stop answering.diff --git a/Tests/LibMedia/WithholdingVideoDecoder.h b/Tests/LibMedia/WithholdingVideoDecoder.hindex d611d3fb066..beac0bb581c 100644--- a/Tests/LibMedia/WithholdingVideoDecoder.h+++ b/Tests/LibMedia/WithholdingVideoDecoder.h@@ -59,12 +59,26 @@ public:return true;}+ bool wait_until_the_caller_waits_for_output(AK::Duration timeout)+ {+ auto deadline = MonotonicTime::now() + timeout;+ MutexLocker locker { m_mutex };+ while (!m_caller_is_waiting_for_output) {+ auto now = MonotonicTime::now();+ if (now >= deadline)+ return false;+ m_caller_wait_changed.wait_for(deadline - now);+ }+ return true;+ }+private:friend class WithholdingVideoDecoder;mutable Mutex m_mutex;ConditionVariable m_released_changed { m_mutex };ConditionVariable m_withheld_changed { m_mutex };+ ConditionVariable m_caller_wait_changed { m_mutex };Vector<Output> m_withheld;Vector<Output> m_released;Function<void()> m_output_arrived_callback;@@ -115,6 +129,7 @@ public:// A device decoding as many frames as it can at once takes the next one only when one of them finishes.while (m_withheld_outputs->m_withheld.size() >= MAXIMUM_WITHHELD_OUTPUTS) {m_withheld_outputs->m_caller_is_waiting_for_output = true;+ m_withheld_outputs->m_caller_wait_changed.broadcast();m_withheld_outputs->m_released_changed.wait();m_withheld_outputs->m_caller_is_waiting_for_output = false;}@@ -137,6 +152,7 @@ public:};while (must_wait_for_a_release()) {m_withheld_outputs->m_caller_is_waiting_for_output = true;+ m_withheld_outputs->m_caller_wait_changed.broadcast();m_withheld_outputs->m_released_changed.wait();m_withheld_outputs->m_caller_is_waiting_for_output = false;}