Skip to content

Truly independent web browser

LibMedia+Tests: Defer updating source buffers' ranges after processing

Zaggy1024c6c067bb04a3
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
index 228713e7c0a..0a1d54ce58a 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
@@ -20,6 +20,27 @@ SourceBufferDemuxer::SourceBufferDemuxer(Vector<Media::Track> const& tracks)
SourceBufferDemuxer::~SourceBufferDemuxer() = default;
+SourceBufferDemuxer::ChangeBatch::ChangeBatch(RefPtr<SourceBufferDemuxer> demuxer)
+ : m_demuxer(move(demuxer))
+{
+ if (!m_demuxer)
+ return;
+ MutexLocker locker { m_demuxer->m_mutex };
+ VERIFY(!m_demuxer->m_in_change_batch);
+ m_demuxer->m_in_change_batch = true;
+}
+
+SourceBufferDemuxer::ChangeBatch::~ChangeBatch()
+{
+ if (!m_demuxer)
+ return;
+ MutexLocker locker { m_demuxer->m_mutex };
+ VERIFY(m_demuxer->m_in_change_batch);
+ m_demuxer->m_in_change_batch = false;
+ if (m_demuxer->m_has_unpublished_changes)
+ m_demuxer->publish_buffered_data_changes_while_locked();
+}
+
// The tracks are fixed at construction, so looking one up needs no lock.
SourceBufferDemuxer::TrackData& SourceBufferDemuxer::track_data(Media::Track const& track)
{
@@ -56,26 +77,43 @@ bool SourceBufferDemuxer::is_within_start_gap_allowance(AK::Duration timestamp,
return timestamp >= AK::Duration::zero() && timestamp < first_data_start && first_data_start <= start_gap_allowance;
}
-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)
+void SourceBufferDemuxer::bring_track_buffer_ranges_up_to_date(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);
- data.coalesced_ranges = max_gap.is_zero() ? data.track_buffer_ranges : data.track_buffer_ranges.coalesced(max_gap);
- data.coalesced_ranges_gap = max_gap;
+ auto maximum_gap = maximum_time_range_gap(data);
+
+ if (data.track_buffer_ranges_need_rebuild) {
+ data.track_buffer_ranges_need_rebuild = false;
+ data.frame_intervals_to_add_to_ranges.clear_with_capacity();
+ Vector<Media::TimeRanges::Range> frame_intervals;
+ for (auto const& run : data.runs) {
+ for (auto const& frame : run.frames)
+ frame_intervals.append({ frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration() });
+ }
+ data.track_buffer_ranges = {};
+ data.track_buffer_ranges.add_ranges(frame_intervals, maximum_gap);
+ data.track_buffer_ranges_maximum_gap = maximum_gap;
+ return;
+ }
+
+ // Only erasing frames can narrow the gap, and that rebuilds the ranges.
+ VERIFY(maximum_gap >= data.track_buffer_ranges_maximum_gap);
+ if (maximum_gap > data.track_buffer_ranges_maximum_gap) {
+ data.track_buffer_ranges = data.track_buffer_ranges.coalesced(maximum_gap);
+ data.track_buffer_ranges_maximum_gap = maximum_gap;
+ }
+ if (!data.frame_intervals_to_add_to_ranges.is_empty()) {
+ data.track_buffer_ranges.add_ranges(data.frame_intervals_to_add_to_ranges, maximum_gap);
+ data.frame_intervals_to_add_to_ranges.clear();
+ }
}
Media::TimeRanges SourceBufferDemuxer::track_buffer_ranges(Media::Track const& track) const
{
MutexLocker locker { m_mutex };
- return coalesced_track_buffer_ranges(track_data(track));
+ return track_data(track).track_buffer_ranges;
}
Media::TimeRanges SourceBufferDemuxer::buffered_ranges() const
@@ -97,7 +135,7 @@ Vector<Media::TrackBufferedRanges> SourceBufferDemuxer::track_buffered_ranges_wh
Vector<Media::TrackBufferedRanges> track_buffered_ranges;
for (auto const& data : m_tracks) {
if (contributes_to_buffered_ranges(*data))
- track_buffered_ranges.append({ data->track.identifier(), coalesced_track_buffer_ranges(*data), m_reached_end_of_stream });
+ track_buffered_ranges.append({ data->track.identifier(), data->track_buffer_ranges, m_reached_end_of_stream });
}
return track_buffered_ranges;
}
@@ -120,6 +158,21 @@ Media::DemuxerScanState SourceBufferDemuxer::scan_state_while_locked() const
void SourceBufferDemuxer::buffered_data_changed_while_locked()
{
+ m_has_unpublished_changes = true;
+ if (!m_in_change_batch)
+ publish_buffered_data_changes_while_locked();
+}
+
+void SourceBufferDemuxer::publish_buffered_data_changes_while_locked()
+{
+ m_has_unpublished_changes = false;
+ for (auto& data : m_tracks)
+ bring_track_buffer_ranges_up_to_date(*data);
+ if (!m_frame_intervals_to_invalidate.is_empty()) {
+ m_invalidated_ranges.add_ranges(m_frame_intervals_to_invalidate);
+ m_frame_intervals_to_invalidate.clear();
+ }
+
m_buffered_ranges = scan_state_while_locked().buffered_ranges();
m_data_changed.broadcast();
queue_scan_state_change_dispatch_while_locked();
@@ -230,12 +283,7 @@ void SourceBufferDemuxer::add_coded_frame(Media::Track const& track, Media::Code
}
count_frame_duration(data, 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.frame_intervals_to_add_to_ranges.append({ frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration() });
data.last_appended_decode_timestamp = frame.decode_timestamp();
if (run_to_continue.has_value()) {
@@ -345,14 +393,12 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_t
size_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;
- 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());
+ m_frame_intervals_to_invalidate.append({ frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration() });
bytes += buffered_byte_count(frame);
if (frame.duration() == data.maximum_frame_duration)
removed_frames_with_the_maximum_duration++;
@@ -372,9 +418,8 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_t
data.total_bytes -= bytes;
run.frames.remove(first_frame, frame_count);
- data.track_buffer_ranges.remove_ranges(removed_ranges);
+ data.track_buffer_ranges_need_rebuild = true;
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.h
index d8818bcaa57..d56083c1b23 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
@@ -10,7 +10,9 @@
#include <AK/ConditionVariable.h>
#include <AK/FixedArray.h>
#include <AK/Mutex.h>
+#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h>
+#include <AK/RefPtr.h>
#include <AK/Vector.h>
#include <LibCore/Forward.h>
#include <LibMedia/Demuxer.h>
@@ -46,6 +48,19 @@ public:
NeedsReanchoring,
};
+ // Defers internal bookkeeping until this goes out of scope to avoid unnecessary work.
+ class MEDIA_API ChangeBatch {
+ AK_MAKE_NONCOPYABLE(ChangeBatch);
+ AK_MAKE_NONMOVABLE(ChangeBatch);
+
+ public:
+ explicit ChangeBatch(RefPtr<SourceBufferDemuxer>);
+ ~ChangeBatch();
+
+ private:
+ RefPtr<SourceBufferDemuxer> m_demuxer;
+ };
+
explicit SourceBufferDemuxer(Vector<Media::Track> const&);
virtual ~SourceBufferDemuxer() override;
@@ -117,11 +132,9 @@ 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 track_buffer_ranges_maximum_gap;
+ Vector<Media::TimeRanges::Range> frame_intervals_to_add_to_ranges;
+ bool track_buffer_ranges_need_rebuild { false };
AK::Duration maximum_frame_duration;
size_t frames_at_maximum_duration { 0 };
size_t total_bytes { 0 };
@@ -135,8 +148,7 @@ private:
static bool contributes_to_buffered_ranges(TrackData const&);
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 bring_track_buffer_ranges_up_to_date(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);
@@ -160,6 +172,7 @@ private:
AK::Duration highest_end_time_while_locked() const;
bool may_read_frame_while_locked(TrackData const&, Media::CodedFrame const&) const;
void buffered_data_changed_while_locked();
+ void publish_buffered_data_changes_while_locked();
void queue_scan_state_change_dispatch_while_locked();
Vector<NonnullOwnPtr<TrackData>> m_tracks;
@@ -171,6 +184,9 @@ private:
// The ranges in which every audio and video track has data, which bound what reads may deliver.
Media::TimeRanges m_buffered_ranges;
+ bool m_in_change_batch { false };
+ bool m_has_unpublished_changes { false };
+
// Owned by the thread that installed the change handler; mutated only via its event loop.
Media::DemuxerScanState m_scan_state;
ScanStateChangeHandler m_scan_state_change_handler;
@@ -179,6 +195,7 @@ private:
bool m_scan_state_change_dispatch_pending { false };
// Reported, then reset, by the next scan state change dispatch.
Media::TimeRanges m_invalidated_ranges;
+ Vector<Media::TimeRanges::Range> m_frame_intervals_to_invalidate;
};
}
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp
index 81735e280a1..17b1639ab55 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp
@@ -209,6 +209,9 @@ void SourceBufferProcessor::run_segment_parser_loop()
{
VERIFY(m_parser);
+ // NB: Let the demuxer itself defer changes until the segment parser loop completes to avoid unnecessary work.
+ Optional<SourceBufferDemuxer::ChangeBatch> change_batch;
+
while (true) {
// 1. Loop Top: If the [[input buffer]] is empty, then jump to the need more data step below.
if (m_cursor->position() >= m_cursor->blocking_size())
@@ -314,6 +317,9 @@ void SourceBufferProcessor::run_segment_parser_loop()
return;
}
+ // NB: The demuxer may have been created by an initialization segment earlier in this loop.
+ if (!change_batch.has_value())
+ change_batch.emplace(m_demuxer);
run_coded_frame_processing(parse_result.value().coded_frames);
// FIXME: 3. If this SourceBuffer is full and cannot accept more media data, then set the [[buffer full flag]]
@@ -711,6 +717,9 @@ void SourceBufferProcessor::run_coded_frame_processing(Vector<DemuxedCodedFrame>
// https://w3c.github.io/media-source/#dfn-coded-frame-removal
void SourceBufferProcessor::run_coded_frame_removal(AK::Duration start, AK::Duration end)
{
+ // NB: Let the demuxer itself defer changes until removal is complete to avoid unnecessary work.
+ SourceBufferDemuxer::ChangeBatch change_batch { m_demuxer };
+
// 1. Let start be the starting presentation timestamp for the removal range.
// 2. Let end be the end presentation timestamp for the removal range.
@@ -774,6 +783,9 @@ void SourceBufferProcessor::run_coded_frame_removal(AK::Duration start, AK::Dura
// https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction
void SourceBufferProcessor::run_coded_frame_eviction(size_t new_data_size, AK::Duration current_time)
{
+ // NB: Let the demuxer itself defer changes until eviction is done to avoid unnecessary work.
+ SourceBufferDemuxer::ChangeBatch change_batch { m_demuxer };
+
// https://w3c.github.io/media-source/#dfn-coded-frame-removal
// AD-HOC: We'll run some of the final steps from the coded frame removal algorithm below.
// We explicitly do not remove dependencies here (step 4), since that may evict data ahead of what the
diff --git a/Libraries/LibMedia/TimeRanges.cpp b/Libraries/LibMedia/TimeRanges.cpp
index a0b4db6be6b..fb66cf83282 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, AK::Duration maximum_gap)
+void TimeRanges::add_range(AK::Duration start, AK::Duration end, AK::Duration maximum_gap_to_bridge)
{
if (start >= end)
return;
@@ -26,7 +26,7 @@ void TimeRanges::add_range(AK::Duration start, AK::Duration end, AK::Duration ma
binary_search(m_ranges, start, &insert_index, [](AK::Duration needle, Range const& range) -> int {
return needle <=> range.start;
});
- if (m_ranges[insert_index].end + maximum_gap < start)
+ if (m_ranges[insert_index].end + maximum_gap_to_bridge < 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, AK::Duration ma
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 + maximum_gap) {
+ while (merge_end_index < m_ranges.size() && m_ranges[merge_end_index].start <= merged_end + maximum_gap_to_bridge) {
merged_end = max(merged_end, m_ranges[merge_end_index].end);
merge_end_index++;
}
@@ -52,38 +52,75 @@ void TimeRanges::add_range(AK::Duration start, AK::Duration end, AK::Duration ma
}
}
-void TimeRanges::remove_range(AK::Duration start, AK::Duration end)
+static void append_merging_with_last(Vector<TimeRanges::Range>& ranges, TimeRanges::Range const& range, AK::Duration maximum_gap_to_bridge)
{
- if (start >= end)
+ if (!ranges.is_empty() && range.start <= ranges.last().end + maximum_gap_to_bridge) {
+ ranges.last().end = max(ranges.last().end, range.end);
return;
+ }
+ ranges.append(range);
+}
+
+static Vector<TimeRanges::Range> merge_sorted_ranges(Vector<TimeRanges::Range> const& a, Vector<TimeRanges::Range> const& b, AK::Duration maximum_gap_to_bridge)
+{
+ Vector<TimeRanges::Range> result;
+ result.ensure_capacity(a.size() + b.size());
+ size_t a_index = 0;
+ size_t b_index = 0;
+ while (a_index < a.size() || b_index < b.size()) {
+ if (b_index == b.size() || (a_index < a.size() && a[a_index].start <= b[b_index].start))
+ append_merging_with_last(result, a[a_index++], maximum_gap_to_bridge);
+ else
+ append_merging_with_last(result, b[b_index++], maximum_gap_to_bridge);
+ }
+ return result;
+}
- TimeRanges removed;
- removed.m_ranges.append({ start, end });
- remove_ranges(removed);
+void TimeRanges::add_ranges(Vector<Range> const& ranges, AK::Duration maximum_gap_to_bridge)
+{
+ // Split the ranges into ascending stretches, then merge neighboring stretches until one remains.
+ Vector<Vector<Range>> stretches;
+ for (auto const& range : ranges) {
+ if (range.start >= range.end)
+ continue;
+ if (stretches.is_empty() || range.start < stretches.last().last().start)
+ stretches.append({});
+ append_merging_with_last(stretches.last(), range, maximum_gap_to_bridge);
+ }
+ if (stretches.is_empty())
+ return;
+ stretches.append(move(m_ranges));
+
+ while (stretches.size() > 1) {
+ Vector<Vector<Range>> merged_stretches;
+ merged_stretches.ensure_capacity((stretches.size() + 1) / 2);
+ for (size_t index = 0; index + 1 < stretches.size(); index += 2)
+ merged_stretches.unchecked_append(merge_sorted_ranges(stretches[index], stretches[index + 1], maximum_gap_to_bridge));
+ if (stretches.size() % 2 == 1)
+ merged_stretches.unchecked_append(move(stretches.last()));
+ stretches = move(merged_stretches);
+ }
+ m_ranges = move(stretches.first());
}
-void TimeRanges::remove_ranges(TimeRanges const& removed)
+void TimeRanges::remove_range(AK::Duration start, AK::Duration end)
{
- if (removed.is_empty() || is_empty())
+ if (start >= end)
return;
- Vector<Range> remaining;
- size_t first_removed_index = 0;
+ Vector<Range> new_ranges;
for (auto const& range : m_ranges) {
- 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.end <= start || range.start >= end) {
+ new_ranges.append(range);
+ continue;
}
- if (start < range.end)
- remaining.append({ start, range.end });
+
+ if (range.start < start)
+ new_ranges.append({ range.start, start });
+ if (range.end > end)
+ new_ranges.append({ end, range.end });
}
- m_ranges = move(remaining);
+ m_ranges = move(new_ranges);
}
AK::Duration TimeRanges::highest_end_time() const
diff --git a/Libraries/LibMedia/TimeRanges.h b/Libraries/LibMedia/TimeRanges.h
index 36e7a035d21..eb0f1fceb2c 100644
--- a/Libraries/LibMedia/TimeRanges.h
+++ b/Libraries/LibMedia/TimeRanges.h
@@ -34,12 +34,10 @@ public:
add_range(range.start, range.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 add_range(AK::Duration start, AK::Duration end, AK::Duration maximum_gap_to_bridge = AK::Duration::zero());
+ // Takes linear time when the ranges are in order, and O(n log n) time in any order.
+ void add_ranges(Vector<Range> const&, AK::Duration maximum_gap_to_bridge = 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.cpp
index 064d5dd4b72..aaf942fe15a 100644
--- a/Tests/LibMedia/TestSourceBufferDemuxer.cpp
+++ b/Tests/LibMedia/TestSourceBufferDemuxer.cpp
@@ -327,6 +327,78 @@ TEST_CASE(a_track_stops_where_another_tracks_data_ends)
EXPECT_EQ(MUST(demuxer->get_next_sample_for_track(video_track)).presentation_timestamp(), AK::Duration::from_seconds(3));
}
+TEST_CASE(track_buffer_ranges_are_published_once_the_change_batch_ends)
+{
+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });
+
+ {
+ Media::MediaSourceExtensions::SourceBufferDemuxer::ChangeBatch change_batch { demuxer };
+ for (u64 second : { 6u, 5u, 1u, 0u })
+ demuxer->add_coded_frame(track, coded_frame_at(second));
+ EXPECT(demuxer->track_buffer_ranges(track).is_empty());
+ EXPECT(demuxer->buffered_ranges().is_empty());
+ }
+
+ Media::TimeRanges expected_ranges {
+ { AK::Duration::from_seconds(0), AK::Duration::from_seconds(2) },
+ { AK::Duration::from_seconds(5), AK::Duration::from_seconds(7) },
+ };
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), expected_ranges);
+ EXPECT_EQ(demuxer->buffered_ranges(), expected_ranges);
+}
+
+static Media::CodedFrame keyframe_at(u64 seconds, u64 duration_in_seconds)
+{
+ auto data = MUST(FixedArray<u8>::create(1));
+ auto timestamp = AK::Duration::from_seconds(seconds);
+ return { Media::CodecID::H264, timestamp, timestamp, AK::Duration::from_seconds(duration_in_seconds), Media::FrameFlags::Keyframe, move(data), {} };
+}
+
+static Media::TimeRanges::Range seconds_range(u64 start, u64 end)
+{
+ return { AK::Duration::from_seconds(start), AK::Duration::from_seconds(end) };
+}
+
+TEST_CASE(removing_a_frame_keeps_the_range_that_an_overlapping_frame_covers)
+{
+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });
+
+ demuxer->add_coded_frame(track, keyframe_at(0, 2));
+ demuxer->add_coded_frame(track, keyframe_at(1, 2));
+
+ demuxer->remove_coded_frames_and_dependants_in_range(track, AK::Duration::zero(), AK::Duration::from_seconds(1));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(1, 3) }));
+}
+
+TEST_CASE(a_longer_frame_bridges_the_gaps_before_it)
+{
+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });
+
+ demuxer->add_coded_frame(track, keyframe_at(0, 1));
+ demuxer->add_coded_frame(track, keyframe_at(5, 1));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(0, 1), seconds_range(5, 6) }));
+
+ demuxer->add_coded_frame(track, keyframe_at(20, 3));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(0, 6), seconds_range(20, 23) }));
+}
+
+TEST_CASE(removing_the_longest_frame_stops_bridging_the_gaps_it_allowed)
+{
+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });
+
+ demuxer->add_coded_frame(track, keyframe_at(0, 3));
+ demuxer->add_coded_frame(track, keyframe_at(10, 1));
+ demuxer->add_coded_frame(track, keyframe_at(15, 1));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(0, 3), seconds_range(10, 16) }));
+
+ demuxer->remove_coded_frames_and_dependants_in_range(track, AK::Duration::zero(), AK::Duration::from_seconds(3));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(10, 11), seconds_range(15, 16) }));
+}
+
static Media::CodedFrame tenth_of_a_second_frame_at(u64 milliseconds, Media::CodecID codec_id)
{
auto data = MUST(FixedArray<u8>::create(1));
@@ -484,11 +556,6 @@ static Media::TimeRanges ranges_reported_as_invalidated_after(Media::MediaSource
return reported;
}
-static Media::TimeRanges::Range seconds_range(u64 start, u64 end)
-{
- return { AK::Duration::from_seconds(start), AK::Duration::from_seconds(end) };
-}
-
TEST_CASE(replacing_delivered_frames_reports_their_ranges_as_invalidated)
{
Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
diff --git a/Tests/LibMedia/TestTimeRanges.cpp b/Tests/LibMedia/TestTimeRanges.cpp
index ea81c76afd9..bf9bc9fefb3 100644
--- a/Tests/LibMedia/TestTimeRanges.cpp
+++ b/Tests/LibMedia/TestTimeRanges.cpp
@@ -319,7 +319,7 @@ 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()
+// add_range() with a maximum gap
TEST_CASE(add_range_bridges_gaps_up_to_the_maximum)
{
@@ -356,30 +356,76 @@ TEST_CASE(adding_with_a_maximum_gap_matches_coalescing_afterwards)
EXPECT_EQ(bridged, exact.coalesced(maximum_gap));
}
-TEST_CASE(remove_ranges_matches_removing_one_at_a_time)
+// add_ranges()
+
+TEST_CASE(add_ranges_to_empty_in_descending_order)
{
TimeRanges ranges;
- for (i64 index = 0; index < 600; index++)
- ranges.add_range(rounded_frame_start(index), rounded_frame_start(index) + rounded_frame_duration);
+ ranges.add_ranges({ { ms(500), ms(600) }, { ms(300), ms(400) }, { ms(100), ms(200) } });
+ EXPECT_EQ(ranges, (TimeRanges { { ms(100), ms(200) }, { ms(300), ms(400) }, { ms(500), ms(600) } }));
+}
- 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));
+TEST_CASE(add_ranges_merges_with_existing_ranges)
+{
+ TimeRanges ranges;
+ ranges.add_range(ms(0), ms(100));
+ ranges.add_range(ms(400), ms(500));
+ ranges.add_ranges({ { ms(450), ms(700) }, { ms(100), ms(200) }, { ms(800), ms(900) }, { ms(150), ms(250) } });
+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(250) }, { ms(400), ms(700) }, { ms(800), ms(900) } }));
+}
- ranges.remove_ranges(removed);
- EXPECT_EQ(ranges, removed_one_at_a_time);
+TEST_CASE(add_ranges_bridges_existing_ranges)
+{
+ TimeRanges ranges;
+ ranges.add_range(ms(0), ms(100));
+ ranges.add_range(ms(200), ms(300));
+ ranges.add_ranges({ { ms(150), ms(250) }, { ms(50), ms(150) } });
+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(300) } }));
}
-TEST_CASE(remove_ranges_keeps_what_lies_between_them)
+TEST_CASE(add_ranges_ignores_invalid_ranges)
{
- 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) } }));
+ TimeRanges ranges;
+ ranges.add_range(ms(100), ms(200));
+ ranges.add_ranges({ { ms(300), ms(300) }, { ms(500), ms(400) } });
+ EXPECT_EQ(ranges, (TimeRanges { { ms(100), ms(200) } }));
+}
+
+TEST_CASE(add_ranges_bridges_gaps_up_to_the_maximum)
+{
+ TimeRanges ranges;
+ ranges.add_range(ms(0), ms(100));
+ ranges.add_range(ms(300), ms(400));
+ ranges.add_ranges({ { ms(150), ms(250) } }, ms(50));
+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(400) } }));
+}
+
+TEST_CASE(add_ranges_keeps_gaps_over_the_maximum)
+{
+ TimeRanges ranges;
+ ranges.add_range(ms(0), ms(100));
+ ranges.add_ranges({ { ms(160), ms(200) } }, ms(50));
+ EXPECT_EQ(ranges, (TimeRanges { { ms(0), ms(100) }, { ms(160), ms(200) } }));
+}
+
+TEST_CASE(adding_ranges_with_a_maximum_gap_matches_coalescing_afterwards)
+{
+ auto maximum_gap = rounded_frame_duration + rounded_frame_duration;
+ TimeRanges exact;
+ Vector<TimeRanges::Range> frame_intervals;
+ for (i64 index = 6000; index-- > 0;) {
+ // 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);
+ frame_intervals.append({ start, start + rounded_frame_duration });
+ }
+ TimeRanges bridged;
+ bridged.add_ranges(move(frame_intervals), maximum_gap);
+ EXPECT(exact.size() > 3000);
+ EXPECT_EQ(bridged.size(), 2u);
+ EXPECT_EQ(bridged, exact.coalesced(maximum_gap));
}
// highest_end_time()