Truly independent web browser
LibMedia: Avoid checking whether to request in incremental streams
Zaggy1024fd50c34cee30
diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp b/Libraries/LibMedia/IncrementallyPopulatedStream.cppindex 19b0909ab1c..00ee416ea57 100644--- a/Libraries/LibMedia/IncrementallyPopulatedStream.cpp+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.cpp@@ -43,6 +43,7 @@ IncrementallyPopulatedStream::~IncrementallyPopulatedStream() = default;void IncrementallyPopulatedStream::set_data_request_callback(DataRequestCallback callback){MutexLocker locker { m_mutex };+ note_request_state_changed_while_locked();if (!callback) {m_callback_event_loop = nullptr;@@ -137,6 +138,7 @@ void IncrementallyPopulatedStream::remove_byte_range(u64 start, u64 end)}}+ note_request_state_changed_while_locked();m_state_changed.broadcast();notify_available_ranges_changed_while_locked();}@@ -155,6 +157,7 @@ void IncrementallyPopulatedStream::close()MutexLocker locker { m_mutex };m_expected_size = m_last_appended_chunk_end;m_closed = true;+ note_request_state_changed_while_locked();m_state_changed.broadcast();notify_available_ranges_changed_while_locked();}@@ -191,6 +194,7 @@ void IncrementallyPopulatedStream::set_expected_size(u64 expected_size)if (m_expected_size == expected_size)return;m_expected_size = expected_size;+ note_request_state_changed_while_locked();m_state_changed.broadcast();notify_available_ranges_changed_while_locked();}@@ -210,6 +214,7 @@ void IncrementallyPopulatedStream::begin_new_request_while_locked(u64 position)m_currently_requested_position = position;m_current_append_head = position;+ note_request_state_changed_while_locked();if (m_expected_size.has_value() && position >= m_expected_size.value())return;@@ -227,6 +232,7 @@ void IncrementallyPopulatedStream::stop_request_if_idle_while_locked()if (!m_may_idle || !m_currently_requested_position.has_value() || a_cursor_is_blocked_while_locked())return;m_currently_requested_position = {};+ note_request_state_changed_while_locked();dispatch_data_request_while_locked({});}@@ -249,6 +255,7 @@ void IncrementallyPopulatedStream::set_may_idle(bool may_idle)if (m_may_idle == may_idle)return;m_may_idle = may_idle;+ note_request_state_changed_while_locked();if (may_idle) {stop_request_if_idle_while_locked();return;@@ -290,23 +297,33 @@ 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){- if (cursor.m_is_blocking) {+ auto* chunk = m_chunks.find_largest_not_above(position);+ auto data_is_available = m_closed;+ if (chunk) {+ VERIFY(position >= chunk->offset());+ u64 end = position + length;+ if (m_closed && end > m_expected_size.value())+ end = m_expected_size.value();+ data_is_available = end <= chunk->end();+ }++ if (!cursor.m_is_blocking)+ return data_is_available;++ // A read that has to wait depends on its own position, so only reads that can be satisfied reuse a decision.+ auto request_decision_is_current = chunk && data_is_available && cursor.m_request_decided_chunk_offset == chunk->offset() && cursor.m_request_decided_generation == m_request_state_generation;+ if (!request_decision_is_current) {cursor.m_active_timeout = MonotonicTime::now_coarse() + CURSOR_ACTIVE_TIME;auto request_position = select_request_position_while_locked(position);if (!m_currently_requested_position.has_value() || *m_currently_requested_position > request_position || request_position > m_current_append_head + FORWARD_REQUEST_THRESHOLD)begin_new_request_while_locked(request_position);+ if (chunk) {+ cursor.m_request_decided_chunk_offset = chunk->offset();+ cursor.m_request_decided_generation = m_request_state_generation;+ }}- auto* chunk = m_chunks.find_largest_not_above(position);- if (!chunk)- return m_closed;-- VERIFY(position >= chunk->offset());-- u64 end = position + length;- if (m_closed && end > m_expected_size.value())- end = m_expected_size.value();- return end <= chunk->end();+ return data_is_available;}size_t IncrementallyPopulatedStream::read_from_chunks_while_locked(u64 position, Bytes& bytes) const@@ -380,12 +397,14 @@ IncrementallyPopulatedStream::Cursor::Cursor(NonnullRefPtr<IncrementallyPopulate{MutexLocker locker { m_stream->m_mutex };m_stream->m_cursors.append(*this);+ m_stream->note_request_state_changed_while_locked();}IncrementallyPopulatedStream::Cursor::~Cursor(){MutexLocker locker { m_stream->m_mutex };VERIFY(m_stream->m_cursors.remove_first_matching([&](Cursor const& cursor) { return this == &cursor; }));+ m_stream->note_request_state_changed_while_locked();}void IncrementallyPopulatedStream::Cursor::set_is_blocking(bool blocking)@@ -415,7 +434,6 @@ DecoderErrorOr<void> IncrementallyPopulatedStream::Cursor::seek(i64 offset, AK::VERIFY_NOT_REACHED();}- m_active_timeout = MonotonicTime::now_coarse() + CURSOR_ACTIVE_TIME;return {};}diff --git a/Libraries/LibMedia/IncrementallyPopulatedStream.h b/Libraries/LibMedia/IncrementallyPopulatedStream.hindex 8188cf5d61d..9b53a4f0d82 100644--- a/Libraries/LibMedia/IncrementallyPopulatedStream.h+++ b/Libraries/LibMedia/IncrementallyPopulatedStream.h@@ -85,6 +85,11 @@ public:bool m_blocked { false };ReadBlockedChangeHandler m_read_blocked_change_handler;MonotonicTime m_active_timeout { MonotonicTime::now_coarse() };++ // The chunk and stream state that this cursor last chose a request position for. Reads within that chunk are+ // skipped until the state changes, since the choice would be the same.+ Optional<u64> m_request_decided_chunk_offset;+ u64 m_request_decided_generation { 0 };};private:@@ -125,6 +130,7 @@ private:bool check_if_data_is_available_or_begin_request_while_locked(Cursor&, u64 position, u64 length);size_t read_from_chunks_while_locked(u64 position, Bytes& bytes) const;void notify_available_ranges_changed_while_locked();+ void note_request_state_changed_while_locked() { m_request_state_generation++; }mutable Mutex m_mutex;Vector<Cursor&> m_cursors;@@ -142,6 +148,8 @@ private:u64 m_current_append_head { 0 };u64 m_last_appended_chunk_end { 0 };bool m_may_idle { false };+ // Changes whenever anything that the choice of request position depends on changes.+ u64 m_request_state_generation { 1 };};}diff --git a/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp b/Tests/LibMedia/TestIncrementallyPopulatedStream.cppindex 6d68b1d9644..9ff9e110c75 100644--- a/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp+++ b/Tests/LibMedia/TestIncrementallyPopulatedStream.cpp@@ -781,6 +781,56 @@ TEST_CASE(close_crossing_a_new_request_keeps_the_end_of_the_closing_fetch)EXPECT_EQ(stream->expected_size().value(), stream_size);}+TEST_CASE(reading_past_the_end_of_a_chunk_requests_data_for_the_read)+{+ auto& loop = never_destroyed_event_loop();++ static constexpr u64 stream_size = 4 * MiB;+ static constexpr u64 gap_position = 3 * MiB;+ auto data = make_test_data(stream_size);++ auto stream = Media::IncrementallyPopulatedStream::create_empty();+ stream->set_expected_size(stream_size);+ stream->add_chunk_at(0, data.bytes().trim(100));++ Vector<u64> requested_offsets;+ stream->set_data_request_callback([&](Optional<u64> offset) {+ if (!offset.has_value())+ return;+ requested_offsets.append(*offset);+ // Like a fetch, keep delivering data from the requested offset through the read.+ stream->add_chunk_at(*offset, data.bytes().slice(*offset, gap_position + 200 - *offset));+ });++ IGNORE_USE_IN_ESCAPING_LAMBDA auto cursor = stream->create_cursor();++ // A read within the chunk decides that nothing needs requesting yet.+ IGNORE_USE_IN_ESCAPING_LAMBDA Array<u8, 10> buffer;+ MUST(cursor->read_into(buffer));+ EXPECT(requested_offsets.is_empty());++ // With nothing else changing, a read far past the chunk's end must still ask for its own data.+ MUST(cursor->seek(gap_position, SeekMode::SetPosition));+ IGNORE_USE_IN_ESCAPING_LAMBDA Optional<Media::DecoderErrorOr<size_t>> result;+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> finished { false };+ auto thread = Threading::Thread::construct("TestRead"sv, [&]() -> intptr_t {+ result = cursor->read_into(buffer);+ finished = true;+ return 0;+ });+ thread->start();+ auto start_time = MonotonicTime::now_coarse();+ while (!finished && MonotonicTime::now_coarse() - start_time < AK::Duration::from_seconds(1))+ loop.pump(Core::EventLoop::WaitMode::PollForEvents);+ if (!finished)+ cursor->abort();+ MUST(thread->join());++ EXPECT_EQ(requested_offsets.size(), 1u);+ EXPECT(!result->is_error());+ EXPECT_EQ(buffer[0], static_cast<u8>(gap_position));+}+TEST_CASE(chunks_past_the_end_are_dropped_once_closed){auto stream = Media::IncrementallyPopulatedStream::create_empty();