Truly independent web browser
LibMedia: Keep MSE buffered ranges up to date as frames arrive
sideshowbarkere57221cfa403
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cppindex 3f590b4e71c..228713e7c0a 100644--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp@@ -57,14 +57,19 @@ bool SourceBufferDemuxer::is_within_start_gap_allowance(AK::Duration timestamp,}Media::TimeRanges SourceBufferDemuxer::coalesced_track_buffer_ranges(TrackData const& data)+{+ VERIFY(data.coalesced_ranges_gap == maximum_time_range_gap(data));+ return data.coalesced_ranges;+}++void SourceBufferDemuxer::recoalesce_track_buffer_ranges(TrackData& data){// https://w3c.github.io/media-source/#track-buffer-ranges// NOTE: Implementations MAY coalesce adjacent ranges separated by a gap smaller than 2 times the// maximum frame duration buffered so far in this track buffer.auto max_gap = maximum_time_range_gap(data);- if (max_gap.is_zero())- return data.track_buffer_ranges;- return data.track_buffer_ranges.coalesced(max_gap);+ data.coalesced_ranges = max_gap.is_zero() ? data.track_buffer_ranges : data.track_buffer_ranges.coalesced(max_gap);+ data.coalesced_ranges_gap = max_gap;}Media::TimeRanges SourceBufferDemuxer::track_buffer_ranges(Media::Track const& track) const@@ -225,7 +230,12 @@ void SourceBufferDemuxer::add_coded_frame(Media::Track const& track, Media::Code}count_frame_duration(data, frame.duration());- data.track_buffer_ranges.add_range(frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration());+ auto frame_end = frame.presentation_timestamp() + frame.duration();+ data.track_buffer_ranges.add_range(frame.presentation_timestamp(), frame_end);+ if (data.coalesced_ranges_gap == maximum_time_range_gap(data))+ data.coalesced_ranges.add_range(frame.presentation_timestamp(), frame_end, data.coalesced_ranges_gap);+ else+ recoalesce_track_buffer_ranges(data);data.last_appended_decode_timestamp = frame.decode_timestamp();if (run_to_continue.has_value()) {@@ -335,11 +345,12 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_tsize_t bytes = 0;size_t frame_count = 0;size_t removed_frames_with_the_maximum_duration = 0;+ Media::TimeRanges removed_ranges;for (size_t index = first_frame; index < run.frames.size(); index++) {auto const& frame = run.frames[index];if (frame_count >= minimum_frame_count && frame.is_keyframe())break;- data.track_buffer_ranges.remove_range(frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration());+ removed_ranges.add_range(frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration());if (data.highest_delivered_presentation_end.has_value() && frame.presentation_timestamp() < data.highest_delivered_presentation_end.value())m_invalidated_ranges.add_range(frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration());bytes += buffered_byte_count(frame);@@ -361,7 +372,9 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_tdata.total_bytes -= bytes;run.frames.remove(first_frame, frame_count);+ data.track_buffer_ranges.remove_ranges(removed_ranges);decrement_frames_with_maximum_duration(data, removed_frames_with_the_maximum_duration);+ recoalesce_track_buffer_ranges(data);if (data.current_run == run_index) {if (data.current_frame >= first_frame + frame_count) {diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.hindex 900f2d565c8..d8818bcaa57 100644--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h@@ -117,6 +117,11 @@ private:Optional<FixedArray<u8>> codec_configuration_of_dropped_frame;Media::TimeRanges track_buffer_ranges;+ // The track buffer ranges with every gap of at most coalesced_ranges_gap bridged, as buffered ranges report+ // them. They're kept up to date as frames are added, since coalescing every frame's range on each query+ // takes as long as the track has ranges.+ Media::TimeRanges coalesced_ranges;+ AK::Duration coalesced_ranges_gap;AK::Duration maximum_frame_duration;size_t frames_at_maximum_duration { 0 };size_t total_bytes { 0 };@@ -131,6 +136,7 @@ private:static AK::Duration maximum_time_range_gap(TrackData const&);static bool is_within_start_gap_allowance(AK::Duration timestamp, AK::Duration first_data_start);static Media::TimeRanges coalesced_track_buffer_ranges(TrackData const&);+ static void recoalesce_track_buffer_ranges(TrackData&);static void count_frame_duration(TrackData&, AK::Duration);static void decrement_frames_with_maximum_duration(TrackData&, size_t count);static Optional<ReadonlyBytes> codec_configuration_at_position(TrackData const&, size_t run_index, size_t frame_index);diff --git a/Libraries/LibMedia/TimeRanges.cpp b/Libraries/LibMedia/TimeRanges.cppindex 1ddea1874ae..a0b4db6be6b 100644--- a/Libraries/LibMedia/TimeRanges.cpp+++ b/Libraries/LibMedia/TimeRanges.cpp@@ -11,7 +11,7 @@namespace Media {-void TimeRanges::add_range(AK::Duration start, AK::Duration end)+void TimeRanges::add_range(AK::Duration start, AK::Duration end, AK::Duration maximum_gap){if (start >= end)return;@@ -26,7 +26,7 @@ void TimeRanges::add_range(AK::Duration start, AK::Duration end)binary_search(m_ranges, start, &insert_index, [](AK::Duration needle, Range const& range) -> int {return needle <=> range.start;});- if (m_ranges[insert_index].end < start)+ if (m_ranges[insert_index].end + maximum_gap < start)insert_index++;// Determine the range of ranges that will be made contiguous by this new range.@@ -37,7 +37,7 @@ void TimeRanges::add_range(AK::Duration start, AK::Duration end)if (merge_end_index < m_ranges.size()) {merged_start = min(merged_start, m_ranges[merge_end_index].start);- while (merge_end_index < m_ranges.size() && m_ranges[merge_end_index].start <= merged_end) {+ while (merge_end_index < m_ranges.size() && m_ranges[merge_end_index].start <= merged_end + maximum_gap) {merged_end = max(merged_end, m_ranges[merge_end_index].end);merge_end_index++;}@@ -57,19 +57,33 @@ void TimeRanges::remove_range(AK::Duration start, AK::Duration end)if (start >= end)return;- Vector<Range> new_ranges;+ TimeRanges removed;+ removed.m_ranges.append({ start, end });+ remove_ranges(removed);+}++void TimeRanges::remove_ranges(TimeRanges const& removed)+{+ if (removed.is_empty() || is_empty())+ return;++ Vector<Range> remaining;+ size_t first_removed_index = 0;for (auto const& range : m_ranges) {- if (range.end <= start || range.start >= end) {- new_ranges.append(range);- continue;+ while (first_removed_index < removed.m_ranges.size() && removed.m_ranges[first_removed_index].end <= range.start)+ first_removed_index++;++ auto start = range.start;+ for (size_t index = first_removed_index; index < removed.m_ranges.size() && removed.m_ranges[index].start < range.end; index++) {+ auto const& cut = removed.m_ranges[index];+ if (cut.start > start)+ remaining.append({ start, cut.start });+ start = max(start, cut.end);}-- if (range.start < start)- new_ranges.append({ range.start, start });- if (range.end > end)- new_ranges.append({ end, range.end });+ if (start < range.end)+ remaining.append({ start, range.end });}- m_ranges = move(new_ranges);+ m_ranges = move(remaining);}AK::Duration TimeRanges::highest_end_time() constdiff --git a/Libraries/LibMedia/TimeRanges.h b/Libraries/LibMedia/TimeRanges.hindex 25dffaf4f39..36e7a035d21 100644--- a/Libraries/LibMedia/TimeRanges.h+++ b/Libraries/LibMedia/TimeRanges.h@@ -34,8 +34,12 @@ public:add_range(range.start, range.end);}- void add_range(AK::Duration start, AK::Duration end);+ // Adds a range, merging it with the ranges it overlaps or touches, and with any it's separated from by a gap of at+ // most maximum_gap.+ void add_range(AK::Duration start, AK::Duration end, AK::Duration maximum_gap = AK::Duration::zero());void remove_range(AK::Duration start, AK::Duration end);+ // Removes every range in the given set, in one pass over both.+ void remove_ranges(TimeRanges const&);size_t size() const { return m_ranges.size(); }bool is_empty() const { return m_ranges.is_empty(); }diff --git a/Tests/LibMedia/TestSourceBufferDemuxer.cpp b/Tests/LibMedia/TestSourceBufferDemuxer.cppindex d939c8867da..064d5dd4b72 100644--- a/Tests/LibMedia/TestSourceBufferDemuxer.cpp+++ b/Tests/LibMedia/TestSourceBufferDemuxer.cpp@@ -31,6 +31,23 @@ static Media::CodedFrame coded_frame_at(u64 seconds, Optional<ReadonlyBytes> new};}+// Frames at 60 fps whose timestamps are rounded to whole milliseconds while their duration isn't, as in a WebM stream,+// with a keyframe every 120 frames.+static AK::Duration rounded_frame_start(i64 index)+{+ return AK::Duration::from_milliseconds((index * 1000 + 30) / 60);+}++static constexpr auto rounded_frame_duration = AK::Duration::from_nanoseconds(16'666'667);++static Media::CodedFrame frame_with_rounded_timestamp(i64 index)+{+ auto data = MUST(FixedArray<u8>::create(1));+ auto timestamp = rounded_frame_start(index);+ auto flags = index % 120 == 0 ? Media::FrameFlags::Keyframe : Media::FrameFlags::None;+ return { Media::CodecID::VP9, timestamp, timestamp, rounded_frame_duration, flags, move(data) };+}+TEST_CASE(seek_provides_the_active_codec_configuration){Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };@@ -560,3 +577,28 @@ TEST_CASE(retracting_a_delivered_end_of_stream_reports_what_follows_as_invalidat});EXPECT_EQ(reported_after_delivering_the_end, Media::TimeRanges({ { AK::Duration::from_seconds(1), AK::Duration::max() } }));}++TEST_CASE(buffered_ranges_bridge_the_rounding_gaps_between_frames)+{+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });++ for (i64 index = 0; index < 600; index++)+ demuxer->add_coded_frame(track, frame_with_rounded_timestamp(index));++ auto end = rounded_frame_start(599) + rounded_frame_duration;+ EXPECT_EQ(demuxer->track_buffer_ranges(track), (Media::TimeRanges { { AK::Duration::zero(), end } }));+}++TEST_CASE(evicting_a_group_of_frames_starts_the_buffered_ranges_at_the_next_group)+{+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });++ for (i64 index = 0; index < 600; index++)+ demuxer->add_coded_frame(track, frame_with_rounded_timestamp(index));+ demuxer->take_earliest_frame_and_dependants(track);++ auto end = rounded_frame_start(599) + rounded_frame_duration;+ EXPECT_EQ(demuxer->track_buffer_ranges(track), (Media::TimeRanges { { rounded_frame_start(120), end } }));+}diff --git a/Tests/LibMedia/TestTimeRanges.cpp b/Tests/LibMedia/TestTimeRanges.cppindex f32c37a6d0b..ea81c76afd9 100644--- a/Tests/LibMedia/TestTimeRanges.cpp+++ b/Tests/LibMedia/TestTimeRanges.cpp@@ -16,6 +16,15 @@ static AK::Duration ms(i64 milliseconds)return AK::Duration::from_milliseconds(milliseconds);}+// Frames at 60 fps whose timestamps are rounded to whole milliseconds while their duration isn't, as in a WebM stream,+// so that most of them don't quite reach the next frame.+static AK::Duration rounded_frame_start(i64 index)+{+ return ms((index * 1000 + 30) / 60);+}++static constexpr auto rounded_frame_duration = AK::Duration::from_nanoseconds(16'666'667);+TEST_CASE(buffered_sources_without_sources_are_not_complete){auto report = Media::DemuxerScanState::buffered_ranges_of_sources({});@@ -310,6 +319,69 @@ TEST_CASE(remove_range_across_multiple_ranges)EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(100) }, { ms(900), ms(1000) } }));}+// add_range() with a maximum gap, and remove_ranges()++TEST_CASE(add_range_bridges_gaps_up_to_the_maximum)+{+ TimeRanges ranges;+ ranges.add_range(ms(0), ms(100));+ ranges.add_range(ms(300), ms(400));+ ranges.add_range(ms(150), ms(250), ms(50));+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(400) } }));+}++TEST_CASE(add_range_keeps_gaps_over_the_maximum)+{+ TimeRanges ranges;+ ranges.add_range(ms(0), ms(100));+ ranges.add_range(ms(160), ms(200), ms(50));+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(100) }, { ms(160), ms(200) } }));+}++TEST_CASE(adding_with_a_maximum_gap_matches_coalescing_afterwards)+{+ auto maximum_gap = rounded_frame_duration + rounded_frame_duration;+ TimeRanges exact;+ TimeRanges bridged;+ for (i64 index = 0; index < 6000; index++) {+ // A real gap, which nothing bridges.+ if (index >= 3000 && index < 3010)+ continue;+ auto start = rounded_frame_start(index);+ exact.add_range(start, start + rounded_frame_duration);+ bridged.add_range(start, start + rounded_frame_duration, maximum_gap);+ }+ EXPECT(exact.size() > 3000);+ EXPECT_EQ(bridged.size(), 2u);+ EXPECT_EQ(bridged, exact.coalesced(maximum_gap));+}++TEST_CASE(remove_ranges_matches_removing_one_at_a_time)+{+ TimeRanges ranges;+ for (i64 index = 0; index < 600; index++)+ ranges.add_range(rounded_frame_start(index), rounded_frame_start(index) + rounded_frame_duration);++ auto removed_one_at_a_time = ranges;+ TimeRanges removed;+ for (i64 index = 0; index < 600; index += 3) {+ removed.add_range(rounded_frame_start(index), rounded_frame_start(index) + rounded_frame_duration);+ removed_one_at_a_time.remove_range(rounded_frame_start(index), rounded_frame_start(index) + rounded_frame_duration);+ }+ removed.add_range(ms(5000), ms(6000));+ removed_one_at_a_time.remove_range(ms(5000), ms(6000));++ ranges.remove_ranges(removed);+ EXPECT_EQ(ranges, removed_one_at_a_time);+}++TEST_CASE(remove_ranges_keeps_what_lies_between_them)+{+ TimeRanges ranges { { ms(0), ms(1000) } };+ ranges.remove_ranges(TimeRanges { { ms(100), ms(200) }, { ms(500), ms(600) } });+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(100) }, { ms(200), ms(500) }, { ms(600), ms(1000) } }));+}+// highest_end_time()TEST_CASE(highest_end_time_empty)