Truly independent web browser
LibMedia: Allow ADTSDemuxer and MP3Demuxer to abort seeks
Zaggy1024feb6426efdeb
diff --git a/Libraries/LibMedia/Containers/ADTS/ADTSDemuxer.cpp b/Libraries/LibMedia/Containers/ADTS/ADTSDemuxer.cppindex b1d68635462..d5d6b54a223 100644--- a/Libraries/LibMedia/Containers/ADTS/ADTSDemuxer.cpp+++ b/Libraries/LibMedia/Containers/ADTS/ADTSDemuxer.cpp@@ -16,34 +16,28 @@ namespace {class FrameScanSource final : public Media::FrameScanSource {public:- virtual Optional<Frame> next_frame(MediaStreamCursor& cursor, size_t position) const override+ virtual DecoderErrorOr<Optional<Frame>> next_frame(MediaStreamCursor& cursor, size_t position) const override{FrameIterator iterator { NonnullRefPtr { cursor }, position, FrameIterator::Resynchronize::Yes };- auto frame = iterator.next();- if (frame.is_error() || !frame.value().has_value())- return {};+ auto frame = TRY(absorb_data_errors(iterator.next()));+ if (!frame.has_value())+ return Optional<Frame> {};return Frame {- .position = frame.value()->position,- .byte_size = frame.value()->header.frame_byte_size,- .duration = { frame.value()->header.sample_count, frame.value()->header.sample_rate },+ .position = frame->position,+ .byte_size = frame->header.frame_byte_size,+ .duration = { frame->header.sample_count, frame->header.sample_rate },};}- virtual Optional<size_t> find_boundary_at_or_after(MediaStreamCursor& cursor, size_t start_byte, size_t upper_bound) const override+ virtual DecoderErrorOr<Optional<size_t>> find_boundary_at_or_after(MediaStreamCursor& cursor, size_t start_byte, size_t upper_bound) const override{- auto boundary = Reader::find_frame_boundary_at_or_after(NonnullRefPtr { cursor }, start_byte, upper_bound);- if (boundary.is_error())- return {};- return boundary.release_value();+ return absorb_data_errors(Reader::find_frame_boundary_at_or_after(NonnullRefPtr { cursor }, start_byte, upper_bound));}- virtual Optional<size_t> find_boundary_at_or_before(MediaStreamCursor& cursor, size_t target_byte, size_t lower_bound, size_t upper_bound) const override+ virtual DecoderErrorOr<Optional<size_t>> find_boundary_at_or_before(MediaStreamCursor& cursor, size_t target_byte, size_t lower_bound, size_t upper_bound) const override{- auto boundary = Reader::find_frame_boundary_at_or_before(NonnullRefPtr { cursor }, target_byte, lower_bound, upper_bound);- if (boundary.is_error())- return {};- return boundary.release_value();+ return absorb_data_errors(Reader::find_frame_boundary_at_or_before(NonnullRefPtr { cursor }, target_byte, lower_bound, upper_bound));}};@@ -218,10 +212,10 @@ DecoderErrorOr<DemuxerSeekResult> ADTSDemuxer::seek_to_most_recent_keyframe(TracVERIFY(track == m_track);auto& status = track_status();+ auto seek_result = TRY(m_buffered_scan_thread->payload().timeline->seek_to_timestamp(m_seek_scanning_cursor, m_seek_cursor, *m_stream, timestamp));if (has_flag(options, DemuxerSeekOptions::NeedCodecConfiguration))status.needs_codec_configuration = true;- auto seek_result = TRY(m_buffered_scan_thread->payload().timeline->seek_to_timestamp(m_seek_scanning_cursor, m_seek_cursor, *m_stream, timestamp));auto const* seeked = seek_result.get_pointer<SeekedPosition>();if (seeked == nullptr)return DemuxerSeekResult::KeptCurrentPosition;@@ -257,11 +251,13 @@ void ADTSDemuxer::set_scan_state_change_handler(ScanStateChangeHandler handler)void ADTSDemuxer::set_blocking_reads_aborted_for_track(Track const&){track_status().iterator.cursor().abort();+ m_seek_cursor->abort();}void ADTSDemuxer::reset_blocking_reads_aborted_for_track(Track const&){track_status().iterator.cursor().reset_abort();+ m_seek_cursor->reset_abort();}void ADTSDemuxer::set_read_blocked_change_handler_for_track(Track const&, ReadBlockedChangeHandler handler)diff --git a/Libraries/LibMedia/Containers/FrameScanTimeline.cpp b/Libraries/LibMedia/Containers/FrameScanTimeline.cppindex d5c278d2a40..b519bace40b 100644--- a/Libraries/LibMedia/Containers/FrameScanTimeline.cpp+++ b/Libraries/LibMedia/Containers/FrameScanTimeline.cpp@@ -152,14 +152,14 @@ DecoderErrorOr<SeekResult> FrameScanTimeline::seek_to_timestamp(NonnullRefPtr<Meauto file_size = stream.expected_size();update_scanned_ranges(*scanning_cursor, stream.available_byte_ranges(), file_size);- auto lookup = look_up_timestamp(*scanning_cursor, timestamp, file_size);+ auto lookup = TRY(look_up_timestamp(*scanning_cursor, timestamp, file_size));if (auto const* landed = lookup.get_pointer<ByteTimePoint>())return seeked_position(*landed);gap = lookup.get<Gap>();}auto estimated_byte = estimate_byte_for_timestamp(timestamp, gap.start, gap.end);- auto resynced_frame_byte = m_frames->find_boundary_at_or_before(*seek_cursor, estimated_byte, gap.start.byte, gap.end.byte).value_or(gap.start.byte);+ auto resynced_frame_byte = TRY(m_frames->find_boundary_at_or_before(*seek_cursor, estimated_byte, gap.start.byte, gap.end.byte)).value_or(gap.start.byte);// The resync above has blocked until the data at the target is available. Now that the data is available, the// frames walked by the scan may have a different bitrate than we expected. Walk the frames from an established@@ -173,7 +173,8 @@ DecoderErrorOr<SeekResult> FrameScanTimeline::seek_to_timestamp(NonnullRefPtr<Meif (!range.has_value())return seeked_position({ resynced_frame_byte, interpolate_timestamp_at_byte(resynced_frame_byte, gap.start, gap.end) });- return seeked_position(walk_to_timestamp(*seek_cursor, walk_start_for(*range, timestamp), timestamp).point);+ auto walk = TRY(walk_to_timestamp(*seek_cursor, walk_start_for(*range, timestamp), timestamp));+ return seeked_position(walk.point);}SeekedPosition FrameScanTimeline::seeked_position(ByteTimePoint const& point)@@ -266,7 +267,7 @@ Optional<FrameScanTimeline::ByteTimePoint> FrameScanTimeline::end_anchor(Optionareturn ByteTimePoint { end_byte, last.time + gap_duration(end_byte - last.byte, rate_for_gap(walked_rate(), file_size)) };}-FrameScanTimeline::TimestampLookup FrameScanTimeline::look_up_timestamp(MediaStreamCursor& cursor, AK::Duration target, Optional<u64> file_size) const+DecoderErrorOr<FrameScanTimeline::TimestampLookup> FrameScanTimeline::look_up_timestamp(MediaStreamCursor& cursor, AK::Duration target, Optional<u64> file_size) const{Optional<size_t> index;for (size_t i = 0; i < m_scanned_ranges.size(); i++) {@@ -277,9 +278,9 @@ FrameScanTimeline::TimestampLookup FrameScanTimeline::look_up_timestamp(MediaStrauto scanned_end = origin_point();if (index.has_value()) {- auto walk = walk_to_timestamp(cursor, walk_start_for(m_scanned_ranges[*index], target), target);+ auto walk = TRY(walk_to_timestamp(cursor, walk_start_for(m_scanned_ranges[*index], target), target));if (walk.spans_target)- return walk.point;+ return TimestampLookup { walk.point };scanned_end = walk.point;}@@ -291,8 +292,8 @@ FrameScanTimeline::TimestampLookup FrameScanTimeline::look_up_timestamp(MediaStrgap_end = end_anchor(file_size);if (!gap_end.has_value() || scanned_end.byte >= gap_end->byte)- return scanned_end;- return Gap { scanned_end, *gap_end };+ return TimestampLookup { scanned_end };+ return TimestampLookup { Gap { scanned_end, *gap_end } };}Optional<FrameScanTimeline::ScannedByteRange> FrameScanTimeline::last_range_starting_at_or_before_byte(size_t byte) const@@ -314,31 +315,32 @@ FrameScanTimeline::ByteTimePoint FrameScanTimeline::walk_start_for(ScannedByteRareturn scanned_start(range);}-FrameScanTimeline::FrameWalk FrameScanTimeline::walk_to_timestamp(MediaStreamCursor& cursor, ByteTimePoint const& start, AK::Duration target) const+DecoderErrorOr<FrameScanTimeline::FrameWalk> FrameScanTimeline::walk_to_timestamp(MediaStreamCursor& cursor, ByteTimePoint const& start, AK::Duration target) const{auto target_duration_in_ticks = (target - start.time).to_time_units(1, FRAME_DURATION_TIMEBASE);u64 duration_in_ticks = 0;auto position = start.byte;while (true) {- auto frame = m_frames->next_frame(cursor, position);+ auto frame = TRY(m_frames->next_frame(cursor, position));if (!frame.has_value())break;auto frame_ticks = frame_duration_in_ticks(frame->duration);if (static_cast<i64>(duration_in_ticks + frame_ticks) > target_duration_in_ticks)- return { { frame->position, start.time + duration_from_ticks(duration_in_ticks) }, true };+ return FrameWalk { { frame->position, start.time + duration_from_ticks(duration_in_ticks) }, true };duration_in_ticks += frame_ticks;position = frame->end_position();}- return { { position, start.time + duration_from_ticks(duration_in_ticks) }, false };+ return FrameWalk { { position, start.time + duration_from_ticks(duration_in_ticks) }, false };}void FrameScanTimeline::scan_frames_into_range(MediaStreamCursor& cursor, ScannedByteRange& range, size_t limit){while (true) {- auto frame = m_frames->next_frame(cursor, range.last_scanned_byte);- if (!frame.has_value())+ auto frame_or_error = m_frames->next_frame(cursor, range.last_scanned_byte);+ if (frame_or_error.is_error() || !frame_or_error.value().has_value())break;+ auto frame = frame_or_error.release_value();if (frame->end_position() > limit) {range.last_scanned_byte = min(frame->position, limit);break;@@ -416,9 +418,10 @@ void FrameScanTimeline::create_ranges_for_unscanned_bytes(MediaStreamCursor& curif (search_start >= search_end)continue;- auto frame_boundary = m_frames->find_boundary_at_or_after(cursor, search_start, search_end);- if (!frame_boundary.has_value())+ auto frame_boundary_or_error = m_frames->find_boundary_at_or_after(cursor, search_start, search_end);+ if (frame_boundary_or_error.is_error() || !frame_boundary_or_error.value().has_value())continue;+ auto frame_boundary = frame_boundary_or_error.release_value();ScannedByteRange range {.byte_start = *frame_boundary,diff --git a/Libraries/LibMedia/Containers/FrameScanTimeline.h b/Libraries/LibMedia/Containers/FrameScanTimeline.hindex 02b27de42e1..6bbf9627575 100644--- a/Libraries/LibMedia/Containers/FrameScanTimeline.h+++ b/Libraries/LibMedia/Containers/FrameScanTimeline.h@@ -42,10 +42,19 @@ public:virtual ~FrameScanSource() = default;// Reads the frame at the position, skipping over bytes that head no frame.- virtual Optional<Frame> next_frame(MediaStreamCursor&, size_t position) const = 0;-- virtual Optional<size_t> find_boundary_at_or_after(MediaStreamCursor&, size_t start_byte, size_t upper_bound) const = 0;- virtual Optional<size_t> find_boundary_at_or_before(MediaStreamCursor&, size_t target_byte, size_t lower_bound, size_t upper_bound) const = 0;+ virtual DecoderErrorOr<Optional<Frame>> next_frame(MediaStreamCursor&, size_t position) const = 0;++ virtual DecoderErrorOr<Optional<size_t>> find_boundary_at_or_after(MediaStreamCursor&, size_t start_byte, size_t upper_bound) const = 0;+ virtual DecoderErrorOr<Optional<size_t>> find_boundary_at_or_before(MediaStreamCursor&, size_t target_byte, size_t lower_bound, size_t upper_bound) const = 0;++protected:+ template<typename T>+ static DecoderErrorOr<Optional<T>> absorb_data_errors(DecoderErrorOr<Optional<T>> result)+ {+ if (result.is_error() && result.error().category() != DecoderErrorCategory::Aborted)+ return Optional<T> {};+ return result;+ }};enum class DurationSource : u8 {@@ -95,9 +104,9 @@ private:ByteRate rate_for_gap(ByteRate const& walked_before_gap, Optional<u64> file_size) const;ByteTimePoint last_scanned_endpoint() const;Optional<ByteTimePoint> end_anchor(Optional<u64> file_size) const;- TimestampLookup look_up_timestamp(MediaStreamCursor&, AK::Duration target, Optional<u64> file_size) const;+ DecoderErrorOr<TimestampLookup> look_up_timestamp(MediaStreamCursor&, AK::Duration target, Optional<u64> file_size) const;Optional<ScannedByteRange> last_range_starting_at_or_before_byte(size_t byte) const;- FrameWalk walk_to_timestamp(MediaStreamCursor&, ByteTimePoint const& start, AK::Duration target) const;+ DecoderErrorOr<FrameWalk> walk_to_timestamp(MediaStreamCursor&, ByteTimePoint const& start, AK::Duration target) const;void scan_frames_into_range(MediaStreamCursor&, ScannedByteRange&, size_t limit);void update_scanned_ranges(MediaStreamCursor&, Vector<MediaStream::ByteRange> const&, Optional<u64> file_size);void drop_ranges_whose_bytes_are_gone(Vector<MediaStream::ByteRange> const&);diff --git a/Libraries/LibMedia/Containers/MP3/MP3Demuxer.cpp b/Libraries/LibMedia/Containers/MP3/MP3Demuxer.cppindex c9d649c328c..d1b1b90e4d2 100644--- a/Libraries/LibMedia/Containers/MP3/MP3Demuxer.cpp+++ b/Libraries/LibMedia/Containers/MP3/MP3Demuxer.cpp@@ -15,34 +15,28 @@ namespace {class FrameScanSource final : public Media::FrameScanSource {public:- virtual Optional<Frame> next_frame(MediaStreamCursor& cursor, size_t position) const override+ virtual DecoderErrorOr<Optional<Frame>> next_frame(MediaStreamCursor& cursor, size_t position) const override{FrameIterator iterator { NonnullRefPtr { cursor }, position, FrameIterator::Resynchronize::Yes };- auto frame = iterator.next();- if (frame.is_error() || !frame.value().has_value())- return {};+ auto frame = TRY(absorb_data_errors(iterator.next()));+ if (!frame.has_value())+ return Optional<Frame> {};return Frame {- .position = frame.value()->position,- .byte_size = frame.value()->header.frame_byte_size,- .duration = { frame.value()->header.sample_count, frame.value()->header.sample_rate },+ .position = frame->position,+ .byte_size = frame->header.frame_byte_size,+ .duration = { frame->header.sample_count, frame->header.sample_rate },};}- virtual Optional<size_t> find_boundary_at_or_after(MediaStreamCursor& cursor, size_t start_byte, size_t upper_bound) const override+ virtual DecoderErrorOr<Optional<size_t>> find_boundary_at_or_after(MediaStreamCursor& cursor, size_t start_byte, size_t upper_bound) const override{- auto boundary = Reader::find_frame_boundary_at_or_after(NonnullRefPtr { cursor }, start_byte, upper_bound);- if (boundary.is_error())- return {};- return boundary.release_value();+ return absorb_data_errors(Reader::find_frame_boundary_at_or_after(NonnullRefPtr { cursor }, start_byte, upper_bound));}- virtual Optional<size_t> find_boundary_at_or_before(MediaStreamCursor& cursor, size_t target_byte, size_t lower_bound, size_t upper_bound) const override+ virtual DecoderErrorOr<Optional<size_t>> find_boundary_at_or_before(MediaStreamCursor& cursor, size_t target_byte, size_t lower_bound, size_t upper_bound) const override{- auto boundary = Reader::find_frame_boundary_at_or_before(NonnullRefPtr { cursor }, target_byte, lower_bound, upper_bound);- if (boundary.is_error())- return {};- return boundary.release_value();+ return absorb_data_errors(Reader::find_frame_boundary_at_or_before(NonnullRefPtr { cursor }, target_byte, lower_bound, upper_bound));}};@@ -188,10 +182,10 @@ DecoderErrorOr<DemuxerSeekResult> MP3Demuxer::seek_to_most_recent_keyframe(TrackVERIFY(track == m_track);auto& status = track_status();+ auto seek_result = TRY(m_buffered_scan_thread->payload().timeline->seek_to_timestamp(m_seek_scanning_cursor, m_seek_cursor, *m_stream, timestamp));if (has_flag(options, DemuxerSeekOptions::NeedCodecConfiguration))status.needs_codec_configuration = true;- auto seek_result = TRY(m_buffered_scan_thread->payload().timeline->seek_to_timestamp(m_seek_scanning_cursor, m_seek_cursor, *m_stream, timestamp));auto const* seeked = seek_result.get_pointer<SeekedPosition>();if (seeked == nullptr)return DemuxerSeekResult::KeptCurrentPosition;@@ -227,11 +221,13 @@ void MP3Demuxer::set_scan_state_change_handler(ScanStateChangeHandler handler)void MP3Demuxer::set_blocking_reads_aborted_for_track(Track const&){track_status().iterator.cursor().abort();+ m_seek_cursor->abort();}void MP3Demuxer::reset_blocking_reads_aborted_for_track(Track const&){track_status().iterator.cursor().reset_abort();+ m_seek_cursor->reset_abort();}void MP3Demuxer::set_read_blocked_change_handler_for_track(Track const&, ReadBlockedChangeHandler handler)diff --git a/Tests/LibMedia/TestADTSDemuxer.cpp b/Tests/LibMedia/TestADTSDemuxer.cppindex abc589ef4fb..77f12886dcb 100644--- a/Tests/LibMedia/TestADTSDemuxer.cpp+++ b/Tests/LibMedia/TestADTSDemuxer.cpp@@ -157,6 +157,51 @@ static void expect_an_aborted_read_to_resume_with_its_frame(ReadonlyBytes bytes,EXPECT_EQ(actual_timestamps, expected_timestamps);}+enum class SeekInterruption : u8 {+ AbortedReads,+ PermanentlyUnavailableData,+};++static void expect_an_interrupted_seek_to_keep_the_position(ReadonlyBytes bytes, size_t available_byte_count, SeekInterruption interruption)+{+ Vector<AK::Duration> expected_timestamps;+ {+ auto demuxer = create_demuxer_for(bytes);+ auto track = audio_track(*demuxer);+ expected_timestamps = timestamps_of_every_frame(*demuxer, track);+ }++ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(bytes.size());+ stream->add_chunk_at(0, bytes.trim(available_byte_count));+ auto demuxer = MUST(Media::ADTS::ADTSDemuxer::from_stream(stream));+ auto track = audio_track(*demuxer);++ constexpr size_t frames_read_before_seeking = 3;+ for (size_t index = 0; index < frames_read_before_seeking; index++)+ EXPECT_EQ(MUST(demuxer->get_next_sample_for_track(track)).presentation_timestamp(), expected_timestamps[index]);++ auto expected_category = Media::DecoderErrorCategory::Aborted;+ if (interruption == SeekInterruption::AbortedReads) {+ demuxer->set_blocking_reads_aborted_for_track(track);+ } else {+ stream->make_missing_data_permanently_unavailable();+ expected_category = Media::DecoderErrorCategory::PermanentlyUnavailable;+ }++ // The target lies in data the stream does not have, so the seek has to read it.+ auto seek_result = demuxer->seek_to_most_recent_keyframe(track, expected_timestamps.last(), Media::DemuxerSeekOptions::None);+ EXPECT(seek_result.is_error());+ if (seek_result.is_error())+ EXPECT_EQ(seek_result.error().category(), expected_category);++ if (interruption == SeekInterruption::AbortedReads)+ demuxer->reset_blocking_reads_aborted_for_track(track);+ // A demuxer that moved into the missing data fails this read instead of waiting on it forever.+ stream->make_missing_data_permanently_unavailable();+ EXPECT_EQ(MUST(demuxer->get_next_sample_for_track(track)).presentation_timestamp(), expected_timestamps[frames_read_before_seeking]);+}+TEST_CASE(demuxer_reads_a_stream_of_nothing_but_frames){// Nine ADTS AAC-LC frames containing 48 kHz stereo silence, as an encoder wrote them. The first@@ -363,3 +408,17 @@ TEST_CASE(demuxer_resumes_after_a_transient_abort)append_frames(bytes, 400);expect_a_transient_abort_to_resume_with_its_frame(bytes, 200 * FRAME_BYTE_SIZE);}++TEST_CASE(demuxer_keeps_its_position_when_a_seek_is_aborted)+{+ Vector<u8> bytes;+ append_frames(bytes, 400);+ expect_an_interrupted_seek_to_keep_the_position(bytes, 200 * FRAME_BYTE_SIZE, SeekInterruption::AbortedReads);+}++TEST_CASE(demuxer_keeps_its_position_when_a_seek_needs_permanently_unavailable_data)+{+ Vector<u8> bytes;+ append_frames(bytes, 400);+ expect_an_interrupted_seek_to_keep_the_position(bytes, 200 * FRAME_BYTE_SIZE, SeekInterruption::PermanentlyUnavailableData);+}diff --git a/Tests/LibMedia/TestFrameScanTimeline.cpp b/Tests/LibMedia/TestFrameScanTimeline.cppindex 4eb343afd81..c3c2bfe655a 100644--- a/Tests/LibMedia/TestFrameScanTimeline.cpp+++ b/Tests/LibMedia/TestFrameScanTimeline.cpp@@ -57,7 +57,7 @@ constexpr size_t RESYNC_BYTES_AFTER_TARGET = 400;class FixedSizeFrames final : public Media::FrameScanSource {public:- virtual Optional<Frame> next_frame(Media::MediaStreamCursor& cursor, size_t position) const override+ virtual Media::DecoderErrorOr<Optional<Frame>> next_frame(Media::MediaStreamCursor& cursor, size_t position) const override{auto bytes_are_readable = [&](size_t start, size_t end) {if (cursor.seek(start, AK::SeekMode::SetPosition).is_error())@@ -69,7 +69,7 @@ public:if (is_non_frame_byte(position)) {if (!bytes_are_readable(position, s_non_frame_bytes->end))- return {};+ return Optional<Frame> {};position = s_non_frame_bytes->end;}@@ -79,7 +79,7 @@ public:readable = whole_frame_is_readable();}if (!readable)- return {};+ return Optional<Frame> {};auto sample_count = 1152u;if (s_doubled_frame_bytes.has_value() && position >= s_doubled_frame_bytes->start && position < s_doubled_frame_bytes->end)@@ -87,17 +87,17 @@ public:return Frame { position, FRAME_BYTE_SIZE, Media::SampleDuration { sample_count, 48000 } };}- virtual Optional<size_t> find_boundary_at_or_after(Media::MediaStreamCursor&, size_t start_byte, size_t upper_bound) const override+ virtual Media::DecoderErrorOr<Optional<size_t>> find_boundary_at_or_after(Media::MediaStreamCursor&, size_t start_byte, size_t upper_bound) const override{auto boundary = (start_byte + FRAME_BYTE_SIZE - 1) / FRAME_BYTE_SIZE * FRAME_BYTE_SIZE;if (is_non_frame_byte(boundary))boundary = s_non_frame_bytes->end;if (boundary + FRAME_BYTE_SIZE > upper_bound)- return {};+ return Optional<size_t> {};return boundary;}- virtual Optional<size_t> find_boundary_at_or_before(Media::MediaStreamCursor&, size_t target_byte, size_t lower_bound, size_t upper_bound) const override+ virtual Media::DecoderErrorOr<Optional<size_t>> find_boundary_at_or_before(Media::MediaStreamCursor&, size_t target_byte, size_t lower_bound, size_t upper_bound) const override{if (s_on_resync)s_on_resync(target_byte);@@ -108,7 +108,7 @@ public:if (is_non_frame_byte(boundary))boundary = s_non_frame_bytes->start - FRAME_BYTE_SIZE;if (boundary < lower_bound)- return {};+ return Optional<size_t> {};return boundary;}};diff --git a/Tests/LibMedia/TestMP3Demuxer.cpp b/Tests/LibMedia/TestMP3Demuxer.cppindex 3bebaadddbc..d06364fe06c 100644--- a/Tests/LibMedia/TestMP3Demuxer.cpp+++ b/Tests/LibMedia/TestMP3Demuxer.cpp@@ -136,6 +136,51 @@ static void expect_an_aborted_read_to_resume_with_its_frame(ReadonlyBytes bytes,EXPECT_EQ(actual_timestamps, expected_timestamps);}+enum class SeekInterruption : u8 {+ AbortedReads,+ PermanentlyUnavailableData,+};++static void expect_an_interrupted_seek_to_keep_the_position(ReadonlyBytes bytes, size_t available_byte_count, SeekInterruption interruption)+{+ Vector<AK::Duration> expected_timestamps;+ {+ auto demuxer = create_demuxer_for(bytes);+ auto track = audio_track(*demuxer);+ expected_timestamps = timestamps_of_every_frame(*demuxer, track);+ }++ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(bytes.size());+ stream->add_chunk_at(0, bytes.trim(available_byte_count));+ auto demuxer = MUST(Media::MP3::MP3Demuxer::from_stream(stream));+ auto track = audio_track(*demuxer);++ constexpr size_t frames_read_before_seeking = 3;+ for (size_t index = 0; index < frames_read_before_seeking; index++)+ EXPECT_EQ(MUST(demuxer->get_next_sample_for_track(track)).presentation_timestamp(), expected_timestamps[index]);++ auto expected_category = Media::DecoderErrorCategory::Aborted;+ if (interruption == SeekInterruption::AbortedReads) {+ demuxer->set_blocking_reads_aborted_for_track(track);+ } else {+ stream->make_missing_data_permanently_unavailable();+ expected_category = Media::DecoderErrorCategory::PermanentlyUnavailable;+ }++ // The target lies in data the stream does not have, so the seek has to read it.+ auto seek_result = demuxer->seek_to_most_recent_keyframe(track, expected_timestamps.last(), Media::DemuxerSeekOptions::None);+ EXPECT(seek_result.is_error());+ if (seek_result.is_error())+ EXPECT_EQ(seek_result.error().category(), expected_category);++ if (interruption == SeekInterruption::AbortedReads)+ demuxer->reset_blocking_reads_aborted_for_track(track);+ // A demuxer that moved into the missing data fails this read instead of waiting on it forever.+ stream->make_missing_data_permanently_unavailable();+ EXPECT_EQ(MUST(demuxer->get_next_sample_for_track(track)).presentation_timestamp(), expected_timestamps[frames_read_before_seeking]);+}+TEST_CASE(demuxer_describes_the_only_track_of_the_stream){auto demuxer = create_demuxer("buffered-ranges/tone.mp3"sv);@@ -293,3 +338,15 @@ TEST_CASE(demuxer_resumes_after_a_transient_abort)auto bytes = constant_bitrate_mp3_frames(80);expect_a_transient_abort_to_resume_with_its_frame(bytes, 40 * 384);}++TEST_CASE(demuxer_keeps_its_position_when_a_seek_is_aborted)+{+ auto bytes = constant_bitrate_mp3_frames(80);+ expect_an_interrupted_seek_to_keep_the_position(bytes, 40 * 384, SeekInterruption::AbortedReads);+}++TEST_CASE(demuxer_keeps_its_position_when_a_seek_needs_permanently_unavailable_data)+{+ auto bytes = constant_bitrate_mp3_frames(80);+ expect_an_interrupted_seek_to_keep_the_position(bytes, 40 * 384, SeekInterruption::PermanentlyUnavailableData);+}