Skip to content

Truly independent web browser

LibMedia: Notify of unblocked source buffer reads after an append

Zaggy10248536a4cdc684
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
index ef1d85bf43d..98ac2bb67ad 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
@@ -177,6 +177,7 @@ void SourceBufferDemuxer::publish_buffered_data_changes_while_locked()
}
m_buffered_ranges = scan_state_while_locked().buffered_ranges();
+ unblock_reads_that_can_progress_while_locked();
m_data_changed.broadcast();
queue_scan_state_change_dispatch_while_locked();
}
@@ -840,65 +841,87 @@ bool SourceBufferDemuxer::may_read_frame_while_locked(TrackData const& data, Med
return frame.presentation_timestamp() < range->end;
}
-Media::DecoderErrorOr<Media::CodedFrame> SourceBufferDemuxer::get_next_sample_for_track(Media::Track const& track)
+bool SourceBufferDemuxer::continue_into_next_run_while_locked(TrackData const& data, Cursor& cursor) const
{
- MutexLocker locker { m_mutex };
- auto& data = track_data(track);
-
- auto continue_into_next_run_while_locked = [&] {
- if (data.cursor.run + 1 >= data.runs.size())
- return false;
- auto const& current_run = data.runs[data.cursor.run];
- auto const& next_run = data.runs[data.cursor.run + 1];
- auto gap = next_run.presentation_start - current_run.presentation_end;
- if (gap > maximum_time_range_gap(data))
- return false;
+ if (cursor.run + 1 >= data.runs.size())
+ return false;
+ auto const& current_run = data.runs[cursor.run];
+ auto const& next_run = data.runs[cursor.run + 1];
+ auto gap = next_run.presentation_start - current_run.presentation_end;
+ if (gap > maximum_time_range_gap(data))
+ return false;
- data.cursor.run++;
- data.cursor.frame = 0;
- note_cursor_jumped(data.cursor);
+ cursor.run++;
+ cursor.frame = 0;
+ note_cursor_jumped(cursor);
- return true;
- };
+ return true;
+}
- bool notified_blocked = false;
- Optional<Media::DecoderError> error;
+SourceBufferDemuxer::ReadProgress SourceBufferDemuxer::advance_cursor_to_next_readable_frame_while_locked(TrackData const& data, Cursor& cursor)
+{
while (true) {
bool has_frame_at_cursor = false;
- if (data.cursor.continuity == CursorContinuity::NeedsReanchoring) {
- auto anchor = data.cursor.presentation_timestamp.value_or(AK::Duration::zero());
- if (move_cursor_to_presentation_time_while_locked(data, data.cursor, anchor))
+ if (cursor.continuity == CursorContinuity::NeedsReanchoring) {
+ auto anchor = cursor.presentation_timestamp.value_or(AK::Duration::zero());
+ if (move_cursor_to_presentation_time_while_locked(data, cursor, anchor))
continue;
- } else if (data.cursor.run < data.runs.size() && data.cursor.frame < data.runs[data.cursor.run].frames.size()) {
+ } else if (cursor.run < data.runs.size() && cursor.frame < data.runs[cursor.run].frames.size()) {
has_frame_at_cursor = true;
- if (may_read_frame_while_locked(data, data.runs[data.cursor.run].frames[data.cursor.frame]))
- break;
- } else if (continue_into_next_run_while_locked()) {
+ if (may_read_frame_while_locked(data, data.runs[cursor.run].frames[cursor.frame]))
+ return ReadProgress::FrameAtCursor;
+ } else if (continue_into_next_run_while_locked(data, cursor)) {
continue;
}
- if (data.aborted.load()) {
- error = Media::DecoderError::with_description(Media::DecoderErrorCategory::Aborted, "Read aborted"sv);
- break;
- }
+ if (data.aborted.load())
+ return ReadProgress::Aborted;
// Once the stream has ended, a frame outside the buffered ranges will never become readable.
- if (m_reached_end_of_stream && (has_frame_at_cursor || data.cursor.run + 1 >= data.runs.size())) {
- error = Media::DecoderError::with_description(Media::DecoderErrorCategory::EndOfStream, "End of stream"sv);
- data.delivered_end_of_stream = true;
- break;
- }
- if (!notified_blocked) {
- notified_blocked = true;
- if (data.read_blocked_change_handler)
- data.read_blocked_change_handler(Media::ReadBlocked::Yes);
- }
- m_data_changed.wait();
+ if (m_reached_end_of_stream && (has_frame_at_cursor || cursor.run + 1 >= data.runs.size()))
+ return ReadProgress::EndOfStream;
+ return ReadProgress::Blocked;
}
+}
+
+void SourceBufferDemuxer::set_read_is_blocked_while_locked(TrackData& data, bool blocked)
+{
+ if (data.read_is_blocked == blocked)
+ return;
+ data.read_is_blocked = blocked;
+ if (data.read_blocked_change_handler)
+ data.read_blocked_change_handler(blocked ? Media::ReadBlocked::Yes : Media::ReadBlocked::No);
+}
- if (notified_blocked && data.read_blocked_change_handler)
- data.read_blocked_change_handler(Media::ReadBlocked::No);
+void SourceBufferDemuxer::unblock_reads_that_can_progress_while_locked()
+{
+ for (auto& data : m_tracks) {
+ if (!data->read_is_blocked)
+ continue;
+ // Later appends may still change where the reader continues from, so only a copy of its cursor moves here.
+ auto cursor = data->cursor;
+ if (advance_cursor_to_next_readable_frame_while_locked(*data, cursor) != ReadProgress::Blocked)
+ set_read_is_blocked_while_locked(*data, false);
+ }
+}
- if (error.has_value())
- return error.release_value();
+Media::DecoderErrorOr<Media::CodedFrame> SourceBufferDemuxer::get_next_sample_for_track(Media::Track const& track)
+{
+ MutexLocker locker { m_mutex };
+ auto& data = track_data(track);
+
+ auto progress = advance_cursor_to_next_readable_frame_while_locked(data, data.cursor);
+ while (progress == ReadProgress::Blocked) {
+ set_read_is_blocked_while_locked(data, true);
+ m_data_changed.wait();
+ progress = advance_cursor_to_next_readable_frame_while_locked(data, data.cursor);
+ }
+ set_read_is_blocked_while_locked(data, false);
+
+ if (progress == ReadProgress::Aborted)
+ return Media::DecoderError::with_description(Media::DecoderErrorCategory::Aborted, "Read aborted"sv);
+ if (progress == ReadProgress::EndOfStream) {
+ data.delivered_end_of_stream = true;
+ return Media::DecoderError::with_description(Media::DecoderErrorCategory::EndOfStream, "End of stream"sv);
+ }
auto const& stored_frame = data.runs[data.cursor.run].frames[data.cursor.frame];
if (!data.read_anchor.has_value() || stored_frame.presentation_timestamp() > data.read_anchor.value())
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
index 6f72f33f791..380137c172c 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
@@ -157,6 +157,14 @@ private:
size_t total_bytes { 0 };
Atomic<bool> aborted { false };
Media::ReadBlockedChangeHandler read_blocked_change_handler;
+ bool read_is_blocked { false };
+ };
+
+ enum class ReadProgress : u8 {
+ FrameAtCursor,
+ Aborted,
+ EndOfStream,
+ Blocked,
};
TrackData& track_data(Media::Track const&);
@@ -184,6 +192,10 @@ private:
size_t erase_frames_and_dependants(TrackData&, size_t run_index, size_t first_frame, size_t minimum_frame_count);
Optional<size_t> find_run_to_play_from_while_locked(TrackData const&, AK::Duration) const;
bool move_cursor_to_presentation_time_while_locked(TrackData const&, Cursor&, AK::Duration);
+ bool continue_into_next_run_while_locked(TrackData const&, Cursor&) const;
+ ReadProgress advance_cursor_to_next_readable_frame_while_locked(TrackData const&, Cursor&);
+ static void set_read_is_blocked_while_locked(TrackData&, bool);
+ void unblock_reads_that_can_progress_while_locked();
static void extend_run_bounds_for_frame(FrameRun&, Media::CodedFrame const&);
static void recalculate_run_bounds(FrameRun&);