Truly independent web browser
LibMedia: Allow stream cursors to wait for and skip data
Zaggy10240d05fac1c7d8
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp b/Libraries/LibMedia/IncrementallyPopulatedStream.cppindex ee73e9d3cc2..63a66c93b4e 100644--- a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp@@ -318,7 +318,7 @@ u64 IncrementallyPopulatedStream::select_request_position_while_locked(u64 positbool IncrementallyPopulatedStream::check_if_data_is_available_or_begin_request_while_locked(Cursor& cursor, u64 position, u64 length){auto* chunk = m_chunks.find_largest_not_above(position);- auto data_is_available = m_closed;+ auto data_is_available = m_closed && position >= m_expected_size.value();if (chunk) {VERIFY(position >= chunk->offset());u64 end = saturating_add(position, length);@@ -370,14 +370,12 @@ size_t IncrementallyPopulatedStream::read_from_chunks_while_locked(u64 position,return copy_size;}-DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, size_t position, Bytes& bytes)+DecoderErrorOr<void> IncrementallyPopulatedStream::wait_for_data_while_locked(Cursor& cursor, u64 position, u64 length){- MutexLocker locker { m_mutex };-bool notified_blocked = false;bool data_is_permanently_unavailable = false;while (!cursor.m_aborted.load(AK::MemoryOrder::memory_order_relaxed) && cursor.m_is_blocking) {- if (check_if_data_is_available_or_begin_request_while_locked(cursor, position, bytes.size()))+ if (check_if_data_is_available_or_begin_request_while_locked(cursor, position, length))break;if (m_missing_data_is_permanently_unavailable) {data_is_permanently_unavailable = true;@@ -404,6 +402,13 @@ DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, sizreturn DecoderError::with_description(DecoderErrorCategory::Aborted, "Blocking read was aborted"sv);if (data_is_permanently_unavailable)return DecoderError::with_description(DecoderErrorCategory::PermanentlyUnavailable, "The data will never arrive"sv);+ return {};+}++DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, size_t position, Bytes& bytes)+{+ MutexLocker locker { m_mutex };+ TRY(wait_for_data_while_locked(cursor, position, bytes.size()));if (m_closed && position >= m_expected_size.value())return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "Blocking read reached end of stream"sv);@@ -414,6 +419,24 @@ DecoderErrorOr<size_t> IncrementallyPopulatedStream::read_at(Cursor& cursor, sizreturn read_from_chunks_while_locked(position, bytes);}+DecoderErrorOr<void> IncrementallyPopulatedStream::discard_at(Cursor& cursor, size_t position, size_t length)+{+ MutexLocker locker { m_mutex };+ auto extends_past_the_expected_size = [&] {+ return m_expected_size.has_value() && (position >= m_expected_size.value() || length > m_expected_size.value() - position);+ };+ if (extends_past_the_expected_size())+ return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "Discarded bytes extend past the end of the stream"sv);++ TRY(wait_for_data_while_locked(cursor, position, length));++ if (extends_past_the_expected_size())+ return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "Discarded bytes extend past the end of the stream"sv);+ if (!cursor.m_is_blocking && !check_if_data_is_available_or_begin_request_while_locked(cursor, position, length))+ return DecoderError::with_description(DecoderErrorCategory::NeedsMoreInput, "The discarded bytes are not all present"sv);+ return {};+}+NonnullRefPtr<MediaStreamCursor> IncrementallyPopulatedStream::create_cursor(){return adopt_ref(*new Cursor(NonnullRefPtr { *this }));@@ -477,6 +500,19 @@ DecoderErrorOr<size_t> IncrementallyPopulatedStream::Cursor::read_into(Bytes bytreturn read_count;}+DecoderErrorOr<void> IncrementallyPopulatedStream::Cursor::read_and_discard(size_t size)+{+ if (size == 0)+ return {};++ if (size > static_cast<size_t>(NumericLimits<i64>::max()) - m_position)+ return DecoderError::corrupted("Discarded bytes extend outside the stream"sv);++ TRY(m_stream->discard_at(*this, m_position, size));+ m_position += size;+ return {};+}+DecoderErrorOr<FixedArray<u8>> IncrementallyPopulatedStream::Cursor::read_bytes(size_t size){if (size == 0)diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.h b/Libraries/LibMedia/IncrementallyPopulatedStream.hindex cb11c769e81..9e37fd80ec5 100644--- a/Libraries/LibMedia/IncrementallyPopulatedStream.h+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.h@@ -65,6 +65,7 @@ public:virtual DecoderErrorOr<void> seek(i64 offset, AK::SeekMode mode) override;virtual DecoderErrorOr<size_t> read_into(Bytes bytes) override;virtual DecoderErrorOr<FixedArray<u8>> read_bytes(size_t size) override;+ virtual DecoderErrorOr<void> read_and_discard(size_t size) override;virtual size_t position() const override { return m_position; }virtual Optional<u64> size() const override { return m_stream->expected_size(); }@@ -125,7 +126,9 @@ private:using Chunks = AK::RedBlackTree<u64, DataChunk>;+ DecoderErrorOr<void> wait_for_data_while_locked(Cursor&, u64 position, u64 length);DecoderErrorOr<size_t> read_at(Cursor&, size_t position, Bytes&);+ DecoderErrorOr<void> discard_at(Cursor&, size_t position, size_t length);bool a_cursor_is_blocked_while_locked() const;u64 select_request_position_while_locked(u64 position);diff --git a/Libraries/LibMedia/MediaStream.h b/Libraries/LibMedia/MediaStream.hindex ada305c8246..0a45773f279 100644--- a/Libraries/LibMedia/MediaStream.h+++ b/Libraries/LibMedia/MediaStream.h@@ -33,6 +33,8 @@ public:virtual DecoderErrorOr<void> seek(i64 offset, AK::SeekMode) = 0;virtual DecoderErrorOr<size_t> read_into(Bytes) = 0;virtual DecoderErrorOr<FixedArray<u8>> read_bytes(size_t size) = 0;+ // Advances past the bytes once they are all present without copying anything.+ virtual DecoderErrorOr<void> read_and_discard(size_t size) = 0;virtual size_t position() const = 0;virtual Optional<u64> size() const = 0;// Waits for the size of the stream to become known. Only for callers with no way to express andiff --git a/Libraries/LibMedia/ReadonlyBytesCursor.h b/Libraries/LibMedia/ReadonlyBytesCursor.hindex 619ead21c36..db50c223992 100644--- a/Libraries/LibMedia/ReadonlyBytesCursor.h+++ b/Libraries/LibMedia/ReadonlyBytesCursor.h@@ -66,6 +66,15 @@ public:return buffer;}+ virtual DecoderErrorOr<void> read_and_discard(size_t size) override+ {+ if (m_position > m_data.size() || size > m_data.size() - m_position)+ return DecoderError::with_description(DecoderErrorCategory::EndOfStream, "End of buffer"sv);++ m_position += size;+ return {};+ }+virtual size_t position() const override { return m_position; }virtual Optional<u64> size() const override { return m_data.size(); }virtual size_t blocking_size() const override { return m_data.size(); }diff --git a/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp b/Tests/LibMedia/TestIncrementallyPopulatedStream.cppindex e416fbc7fdd..c5d8d4ddd26 100644--- a/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp+++ b/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp@@ -1174,3 +1174,114 @@ TEST_CASE(cursor_rejects_seeks_outside_the_stream)EXPECT(!cursor->seek(-2, AK::SeekMode::FromEndPosition).is_error());EXPECT_EQ(cursor->position(), 8u);}++TEST_CASE(read_and_discard_requires_every_byte_in_between)+{+ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(100);+ auto data = make_test_data(100);+ stream->add_chunk_at(0, data.bytes().slice(0, 40));+ stream->add_chunk_at(60, data.bytes().slice(60));++ auto cursor = stream->create_cursor();+ cursor->set_is_blocking(false);+ MUST(cursor->seek(30, AK::SeekMode::SetPosition));++ // The landing point is present, but the bytes before it are not.+ auto result = cursor->read_and_discard(35);+ EXPECT(result.is_error());+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::NeedsMoreInput);+ EXPECT_EQ(cursor->position(), 30u);++ stream->add_chunk_at(40, data.bytes().slice(40, 20));+ MUST(cursor->read_and_discard(35));+ EXPECT_EQ(cursor->position(), 65u);++ stream->close();+ result = cursor->read_and_discard(40);+ EXPECT(result.is_error());+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::EndOfStream);+ EXPECT_EQ(cursor->position(), 65u);+}++TEST_CASE(read_and_discard_in_a_closed_stream_requires_a_chunk_before_the_gap)+{+ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ auto data = make_test_data(100);+ stream->add_chunk_at(60, data.bytes().slice(60));+ stream->close();++ auto cursor = stream->create_cursor();+ cursor->set_is_blocking(false);+ auto result = cursor->read_and_discard(10);+ EXPECT(result.is_error());+ EXPECT_EQ(result.error().category(), Media::DecoderErrorCategory::NeedsMoreInput);+ EXPECT_EQ(cursor->position(), 0u);++ stream->add_chunk_at(0, data.bytes().slice(0, 60));+ EXPECT(!cursor->read_and_discard(10).is_error());+ EXPECT_EQ(cursor->position(), 10u);+}++TEST_CASE(blocking_read_and_discard_past_the_expected_size_does_not_wait)+{+ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(100);+ auto data = make_test_data(100);+ stream->add_chunk_at(0, data.bytes());++ auto cursor = stream->create_cursor();++ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> reached_end_of_stream { false };+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> discard_finished { false };+ auto thread = Threading::Thread::construct("TestDiscard"sv, [&, cursor]() -> intptr_t {+ auto result = cursor->read_and_discard(101);+ reached_end_of_stream = result.is_error() && result.error().category() == Media::DecoderErrorCategory::EndOfStream;+ discard_finished = true;+ return 0;+ });+ thread->start();++ auto start_time = MonotonicTime::now_coarse();+ while (!discard_finished.load() && MonotonicTime::now_coarse() - start_time < AK::Duration::from_seconds(1))+ ;+ if (!discard_finished.load())+ cursor->abort();+ MUST(thread->join());++ EXPECT(reached_end_of_stream.load());+ EXPECT_EQ(cursor->position(), 0u);+}++TEST_CASE(blocking_read_and_discard_waits_for_every_byte)+{+ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(100);+ auto data = make_test_data(100);+ stream->add_chunk_at(0, data.bytes().slice(0, 40));+ stream->add_chunk_at(60, data.bytes().slice(60));++ auto cursor = stream->create_cursor();++ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> read_blocked { false };+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> discard_succeeded { false };+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> discard_finished { false };+ cursor->set_blocked_change_handler([&](Media::ReadBlocked blocked) {+ read_blocked = blocked == Media::ReadBlocked::Yes;+ });++ auto thread = Threading::Thread::construct("TestDiscard"sv, [&, cursor]() -> intptr_t {+ discard_succeeded = !cursor->read_and_discard(70).is_error();+ discard_finished = true;+ return 0;+ });+ thread->start();++ while (!read_blocked.load() && !discard_finished.load())+ ;+ stream->add_chunk_at(40, data.bytes().slice(40, 20));+ MUST(thread->join());++ EXPECT(discard_succeeded.load());+ EXPECT_EQ(cursor->position(), 70u);+}diff --git a/Tests/LibMedia/TransientlyAbortingStream.h b/Tests/LibMedia/TransientlyAbortingStream.hindex 93e98ef16c1..051f3013584 100644--- a/Tests/LibMedia/TransientlyAbortingStream.h+++ b/Tests/LibMedia/TransientlyAbortingStream.h@@ -51,6 +51,12 @@ private:return m_bytes->read_bytes(size);}+ virtual Media::DecoderErrorOr<void> read_and_discard(size_t size) override+ {+ TRY(abort_if_reading_the_aborted_position());+ return m_bytes->read_and_discard(size);+ }+virtual size_t position() const override { return m_bytes->position(); }virtual Optional<u64> size() const override { return m_bytes->size(); }virtual size_t blocking_size() const override { return m_bytes->blocking_size(); }