Skip to content

Truly independent web browser

LibMedia+Tests: Evict MSE frames in bulk

Zaggy1024d05a1d3072c0
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
index fde0d136a38..968ac57b8f6 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp
@@ -536,22 +536,15 @@ bool SourceBufferDemuxer::run_ends_at_last_appended_frame(TrackData const& data,
return data.run_ending_with_last_appended_frame == run_index;
}
-size_t SourceBufferDemuxer::evictable_bytes_when_taking_all_earliest_frames(Media::Track const& track, AK::Duration current_time) const
+template<typename Callback>
+void SourceBufferDemuxer::for_each_earliest_evictable_group(TrackData const& data, AK::Duration current_time, Callback callback)
{
- MutexLocker locker { m_mutex };
- auto const& data = track_data(track);
-
size_t bytes = 0;
for (size_t run_index = 0; run_index < data.runs.size(); run_index++) {
auto const& frames = data.runs[run_index].frames;
// Eviction that stops within a run moves the last evicted configuration onto the first remaining frame.
size_t last_evicted_configuration_size = 0;
- auto bytes_when_stopping_at = [&](size_t first_remaining_frame) {
- if (frames[first_remaining_frame].new_codec_configuration().has_value())
- return bytes;
- return bytes - last_evicted_configuration_size;
- };
size_t group_start = 0;
while (group_start < frames.size()) {
@@ -563,86 +556,117 @@ size_t SourceBufferDemuxer::evictable_bytes_when_taking_all_earliest_frames(Medi
Optional<size_t> group_configuration_size;
for (size_t index = group_start; index < group_end; index++) {
if (!is_frame_evictable(data, frames[index], current_time))
- return bytes_when_stopping_at(group_start);
+ return;
group_bytes += buffered_byte_count(frames[index]);
if (auto configuration = frames[index].new_codec_configuration(); configuration.has_value())
group_configuration_size = configuration.value().size();
}
if (group_end == frames.size() && run_ends_at_last_appended_frame(data, run_index))
- return bytes_when_stopping_at(group_start);
+ return;
bytes += group_bytes;
if (group_configuration_size.has_value())
last_evicted_configuration_size = group_configuration_size.value();
+
+ auto freed_bytes = bytes;
+ if (group_end < frames.size() && !frames[group_end].new_codec_configuration().has_value())
+ freed_bytes -= last_evicted_configuration_size;
+ if (callback(frames[group_start].presentation_timestamp(), freed_bytes) == IterationDecision::Break)
+ return;
group_start = group_end;
}
}
- return bytes;
}
-Optional<AK::Duration> SourceBufferDemuxer::earliest_evictable_frame_timestamp(Media::Track const& track, AK::Duration current_time) const
+size_t SourceBufferDemuxer::evictable_bytes_when_taking_all_earliest_frames(Media::Track const& track, AK::Duration current_time) const
{
MutexLocker locker { m_mutex };
- auto const& data = track_data(track);
- if (data.runs.is_empty())
- return {};
-
- auto const& frames = data.runs.first().frames;
- size_t frame_count = 0;
- for (auto const& frame : frames) {
- if (frame_count > 0 && frame.is_keyframe())
- break;
- if (!is_frame_evictable(data, frame, current_time))
- return {};
- frame_count++;
- }
+ size_t bytes = 0;
+ for_each_earliest_evictable_group(track_data(track), current_time, [&](AK::Duration, size_t freed_bytes) {
+ bytes = freed_bytes;
+ return IterationDecision::Continue;
+ });
+ return bytes;
+}
- if (frame_count == frames.size() && run_ends_at_last_appended_frame(data, 0))
- return {};
- return frames.first().presentation_timestamp();
+Vector<SourceBufferDemuxer::EarliestEvictableGroup> SourceBufferDemuxer::earliest_evictable_groups(Media::Track const& track, AK::Duration current_time, size_t wanted_bytes) const
+{
+ MutexLocker locker { m_mutex };
+ Vector<EarliestEvictableGroup> groups;
+ for_each_earliest_evictable_group(track_data(track), current_time, [&](AK::Duration presentation_timestamp, size_t freed_bytes) {
+ groups.append({ presentation_timestamp, freed_bytes });
+ if (freed_bytes >= wanted_bytes)
+ return IterationDecision::Break;
+ return IterationDecision::Continue;
+ });
+ return groups;
}
-size_t SourceBufferDemuxer::take_earliest_frame_and_dependants(Media::Track const& track)
+size_t SourceBufferDemuxer::take_earliest_groups(Media::Track const& track, size_t group_count)
{
MutexLocker locker { m_mutex };
VERIFY(!m_reached_end_of_stream);
auto& data = track_data(track);
- VERIFY(!data.runs.is_empty());
- auto bytes = erase_frames_and_dependants(data, 0, 0, 1);
+ size_t bytes = 0;
+ while (group_count > 0) {
+ VERIFY(!data.runs.is_empty());
+ auto const& frames = data.runs.first().frames;
+ size_t frame_count = 0;
+ while (group_count > 0 && frame_count < frames.size()) {
+ frame_count++;
+ while (frame_count < frames.size() && !frames[frame_count].is_keyframe())
+ frame_count++;
+ group_count--;
+ }
+ bytes += erase_frames_and_dependants(data, 0, 0, frame_count);
+ }
buffered_data_changed_while_locked();
return bytes;
}
-Optional<AK::Duration> SourceBufferDemuxer::latest_evictable_frame_timestamp(Media::Track const& track, AK::Duration current_time) const
+Vector<SourceBufferDemuxer::LatestEvictableFrame> SourceBufferDemuxer::latest_evictable_frames(Media::Track const& track, AK::Duration current_time, size_t wanted_bytes) const
{
MutexLocker locker { m_mutex };
auto const& data = track_data(track);
- if (data.runs.is_empty())
- return {};
- if (run_ends_at_last_appended_frame(data, data.runs.size() - 1))
- return {};
-
- auto const& frame = data.runs.last().frames.last();
- if (!is_frame_evictable(data, frame, current_time))
- return {};
- return frame.presentation_timestamp();
+ Vector<LatestEvictableFrame> evictable_frames;
+ size_t bytes = 0;
+ for (size_t run_index = data.runs.size(); run_index-- > 0;) {
+ if (run_ends_at_last_appended_frame(data, run_index))
+ break;
+ auto const& frames = data.runs[run_index].frames;
+ for (size_t frame_index = frames.size(); frame_index-- > 0;) {
+ auto const& frame = frames[frame_index];
+ if (!is_frame_evictable(data, frame, current_time))
+ return evictable_frames;
+ auto byte_count = buffered_byte_count(frame);
+ evictable_frames.append({ frame.presentation_timestamp(), frame.decode_timestamp(), byte_count });
+ bytes += byte_count;
+ if (bytes >= wanted_bytes)
+ return evictable_frames;
+ }
+ }
+ return evictable_frames;
}
-SourceBufferDemuxer::RemovedFrame SourceBufferDemuxer::take_latest_frame(Media::Track const& track)
+size_t SourceBufferDemuxer::take_latest_frames(Media::Track const& track, size_t frame_count)
{
MutexLocker locker { m_mutex };
VERIFY(!m_reached_end_of_stream);
auto& data = track_data(track);
- auto& run = data.runs.last();
- auto const& frame = run.frames.last();
- RemovedFrame removed { .byte_size = buffered_byte_count(frame), .decode_timestamp = frame.decode_timestamp() };
-
- erase_frames_and_dependants(data, data.runs.size() - 1, run.frames.size() - 1, 1);
+ size_t bytes = 0;
+ while (frame_count > 0) {
+ VERIFY(!data.runs.is_empty());
+ auto run_index = data.runs.size() - 1;
+ auto run_frame_count = data.runs[run_index].frames.size();
+ auto count = min(frame_count, run_frame_count);
+ bytes += erase_frames_and_dependants(data, run_index, run_frame_count - count, count);
+ frame_count -= count;
+ }
buffered_data_changed_while_locked();
- return removed;
+ return bytes;
}
void SourceBufferDemuxer::set_reached_end_of_stream()
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
index 7349edd5d55..bfdd308926b 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h
@@ -28,9 +28,15 @@ namespace Media::MediaSourceExtensions {
// the SourceBuffer's track buffers (which write frames) and PlaybackManager (which reads them).
class MEDIA_API SourceBufferDemuxer final : public Media::Demuxer {
public:
- struct RemovedFrame {
- size_t byte_size { 0 };
+ struct EarliestEvictableGroup {
+ AK::Duration presentation_timestamp;
+ size_t bytes_freed_by_evicting_through_this_group { 0 };
+ };
+
+ struct LatestEvictableFrame {
+ AK::Duration presentation_timestamp;
AK::Duration decode_timestamp;
+ size_t byte_count { 0 };
};
struct FrameRun {
@@ -81,11 +87,13 @@ public:
size_t total_bytes(Media::Track const&) const;
size_t evictable_bytes_when_taking_all_earliest_frames(Media::Track const&, AK::Duration current_time) const;
- Optional<AK::Duration> earliest_evictable_frame_timestamp(Media::Track const&, AK::Duration current_time) const;
- size_t take_earliest_frame_and_dependants(Media::Track const&);
+ // The groups are listed in the order that eviction takes them, until evicting them would free the wanted bytes.
+ Vector<EarliestEvictableGroup> earliest_evictable_groups(Media::Track const&, AK::Duration current_time, size_t wanted_bytes) const;
+ size_t take_earliest_groups(Media::Track const&, size_t group_count);
- Optional<AK::Duration> latest_evictable_frame_timestamp(Media::Track const&, AK::Duration current_time) const;
- RemovedFrame take_latest_frame(Media::Track const&);
+ // The frames are listed in the order that eviction takes them, until evicting them would free the wanted bytes.
+ Vector<LatestEvictableFrame> latest_evictable_frames(Media::Track const&, AK::Duration current_time, size_t wanted_bytes) const;
+ size_t take_latest_frames(Media::Track const&, size_t frame_count);
void set_reached_end_of_stream();
void clear_reached_end_of_stream();
@@ -156,6 +164,8 @@ private:
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);
static bool is_frame_evictable(TrackData const&, Media::CodedFrame const&, AK::Duration current_time);
+ template<typename Callback>
+ static void for_each_earliest_evictable_group(TrackData const&, AK::Duration current_time, Callback);
static bool run_ends_at_last_appended_frame(TrackData const&, size_t run_index);
static void note_delivered_frame(TrackData&, Media::CodedFrame const&);
static void note_cursor_jumped(TrackData&);
diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp
index 96d55f424b6..a0879032e8d 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp
+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferProcessor.cpp
@@ -811,79 +811,131 @@ void SourceBufferProcessor::run_coded_frame_eviction(size_t new_data_size, AK::D
if (!m_buffer_full_flag)
return;
- // 3. Let removal ranges equal a list of presentation time ranges that can be evicted from the presentation to make
- // room for the new data.
- // 4. For each range in removal ranges, run the coded frame removal algorithm with start and end equal to the
- // removal range start and end timestamp respectively.
- // AD-HOC: Steps 3 and 4 are completed together by removing frames relative to the current playback position. We
- // first evict frames strictly before current_time across all tracks (oldest first), and if more room is
- // needed, evict frames beyond the buffered segment containing current_time (latest first). The per-track
- // demuxer enforces the "don't break the currently-playing segment" boundary so seeks naturally preserve
- // data around the new position.
+ // AD-HOC: Steps 3 and 4 run twice, removing frames relative to the current playback position. We first evict
+ // frames strictly before current_time across all tracks (oldest first), and if more room is needed, evict
+ // frames beyond the buffered segment containing current_time (latest first). The per-track demuxer
+ // enforces the "don't break the currently-playing segment" boundary so seeks naturally preserve data
+ // around the new position.
+
VERIFY(consumed_bytes_after_append > current_capacity_in_bytes);
auto bytes_to_evict = consumed_bytes_after_append - current_capacity_in_bytes;
size_t bytes_evicted = 0;
- while (bytes_evicted < bytes_to_evict) {
- TrackBuffer* oldest_track_buffer = nullptr;
- AK::Duration oldest_timestamp;
- for (auto& [track_id, track_buffer] : m_track_buffers) {
- auto candidate = track_buffer->demuxer().earliest_evictable_frame_timestamp(track_buffer->track(), current_time);
- if (!candidate.has_value())
- continue;
- if (!oldest_track_buffer || candidate.value() < oldest_timestamp) {
- oldest_track_buffer = track_buffer.ptr();
- oldest_timestamp = candidate.value();
+ {
+ // 3. Let removal ranges equal a list of presentation time ranges that can be evicted from the presentation to
+ // make room for the new data.
+ struct EarliestRemovalRange {
+ TrackBuffer& track_buffer;
+ Vector<SourceBufferDemuxer::EarliestEvictableGroup> evictable_groups;
+ size_t group_count { 0 };
+
+ size_t freed_bytes() const { return group_count == 0 ? 0 : evictable_groups[group_count - 1].bytes_freed_by_evicting_through_this_group; }
+ };
+ Vector<EarliestRemovalRange> removal_ranges;
+ for (auto& [track_id, track_buffer] : m_track_buffers)
+ removal_ranges.append({ *track_buffer, track_buffer->demuxer().earliest_evictable_groups(track_buffer->track(), current_time, bytes_to_evict) });
+
+ size_t planned_bytes = 0;
+ while (planned_bytes < bytes_to_evict) {
+ EarliestRemovalRange* oldest_range = nullptr;
+ AK::Duration oldest_timestamp;
+ for (auto& range : removal_ranges) {
+ if (range.group_count == range.evictable_groups.size())
+ continue;
+ auto candidate = range.evictable_groups[range.group_count].presentation_timestamp;
+ if (!oldest_range || candidate < oldest_timestamp) {
+ oldest_range = &range;
+ oldest_timestamp = candidate;
+ }
}
+ if (!oldest_range)
+ break;
+ planned_bytes -= oldest_range->freed_bytes();
+ oldest_range->group_count++;
+ planned_bytes += oldest_range->freed_bytes();
+ }
+
+ // 4. For each range in removal ranges, run the coded frame removal algorithm with start and end equal to the
+ // removal range start and end timestamp respectively.
+ for (auto& range : removal_ranges) {
+ if (range.group_count > 0)
+ bytes_evicted += range.track_buffer.demuxer().take_earliest_groups(range.track_buffer.track(), range.group_count);
}
- if (!oldest_track_buffer)
- break;
- bytes_evicted += oldest_track_buffer->demuxer().take_earliest_frame_and_dependants(oldest_track_buffer->track());
}
- while (bytes_evicted < bytes_to_evict) {
- TrackBuffer* latest_track_buffer = nullptr;
- AK::Duration latest_timestamp;
- for (auto& [track_id, track_buffer] : m_track_buffers) {
- auto candidate = track_buffer->demuxer().latest_evictable_frame_timestamp(track_buffer->track(), current_time);
- if (!candidate.has_value())
- continue;
- if (!latest_track_buffer || candidate.value() > latest_timestamp) {
- latest_track_buffer = track_buffer.ptr();
- latest_timestamp = candidate.value();
+ if (bytes_evicted < bytes_to_evict) {
+ auto remaining_bytes_to_evict = bytes_to_evict - bytes_evicted;
+
+ // 3. Let removal ranges equal a list of presentation time ranges that can be evicted from the presentation to
+ // make room for the new data.
+ struct LatestRemovalRange {
+ TrackBuffer& track_buffer;
+ Vector<SourceBufferDemuxer::LatestEvictableFrame> evictable_frames;
+ size_t frame_count { 0 };
+ };
+ Vector<LatestRemovalRange> removal_ranges;
+ for (auto& [track_id, track_buffer] : m_track_buffers)
+ removal_ranges.append({ *track_buffer, track_buffer->demuxer().latest_evictable_frames(track_buffer->track(), current_time, remaining_bytes_to_evict) });
+
+ Vector<LatestRemovalRange*> removal_order;
+ size_t planned_bytes = 0;
+ while (planned_bytes < remaining_bytes_to_evict) {
+ LatestRemovalRange* latest_range = nullptr;
+ AK::Duration latest_timestamp;
+ for (auto& range : removal_ranges) {
+ if (range.frame_count == range.evictable_frames.size())
+ continue;
+ auto candidate = range.evictable_frames[range.frame_count].presentation_timestamp;
+ if (!latest_range || candidate > latest_timestamp) {
+ latest_range = &range;
+ latest_timestamp = candidate;
+ }
}
+ if (!latest_range)
+ break;
+ planned_bytes += latest_range->evictable_frames[latest_range->frame_count].byte_count;
+ latest_range->frame_count++;
+ removal_order.append(latest_range);
+ }
+
+ // 4. For each range in removal ranges, run the coded frame removal algorithm with start and end equal to the
+ // removal range start and end timestamp respectively.
+ for (auto& range : removal_ranges) {
+ if (range.frame_count > 0)
+ bytes_evicted += range.track_buffer.demuxer().take_latest_frames(range.track_buffer.track(), range.frame_count);
}
- if (!latest_track_buffer)
- break;
- auto last_decode_timestamp = latest_track_buffer->last_decode_timestamp();
- auto removed_frame = latest_track_buffer->demuxer().take_latest_frame(latest_track_buffer->track());
- bytes_evicted += removed_frame.byte_size;
// https://w3c.github.io/media-source/#dfn-coded-frame-removal
- // AD-HOC: Steps starting from 3.3.1 are implemented here.
-
- // 1. For each removed frame, if the frame has a decode timestamp equal to the last decode timestamp for the
- // frame's track, run the following steps:
- // AD-HOC: The spec doesn't nest steps 2-5 below under step 1's if statement, but clearing the last decode
- // timestamp upon every removal here would potentially force a RAP unexpectedly.
- // https://github.com/w3c/media-source/issues/290
- if (last_decode_timestamp.has_value() && removed_frame.decode_timestamp == last_decode_timestamp.value()) {
- // -> If mode equals "segments":
- if (m_mode == AppendMode::Segments) {
- // Set [[group end timestamp]] to presentation timestamp.
- m_group_end_timestamp = latest_timestamp;
- }
- // -> If mode equals "sequence":
- else if (m_mode == AppendMode::Sequence) {
- // Set [[group start timestamp]] equal to the [[group end timestamp]].
- m_group_start_timestamp = m_group_end_timestamp;
+ // AD-HOC: Steps starting from 3.3.1 are implemented here, for each removed frame in the order it was removed.
+ for (auto& range : removal_ranges)
+ range.frame_count = 0;
+ for (auto* range : removal_order) {
+ auto const& removed_frame = range->evictable_frames[range->frame_count++];
+ auto last_decode_timestamp = range->track_buffer.last_decode_timestamp();
+
+ // 1. For each removed frame, if the frame has a decode timestamp equal to the last decode timestamp for
+ // the frame's track, run the following steps:
+ // AD-HOC: The spec doesn't nest steps 2-5 below under step 1's if statement, but clearing the last decode
+ // timestamp upon every removal here would potentially force a RAP unexpectedly.
+ // https://github.com/w3c/media-source/issues/290
+ if (last_decode_timestamp.has_value() && removed_frame.decode_timestamp == last_decode_timestamp.value()) {
+ // -> If mode equals "segments":
+ if (m_mode == AppendMode::Segments) {
+ // Set [[group end timestamp]] to presentation timestamp.
+ m_group_end_timestamp = removed_frame.presentation_timestamp;
+ }
+ // -> If mode equals "sequence":
+ else if (m_mode == AppendMode::Sequence) {
+ // Set [[group start timestamp]] equal to the [[group end timestamp]].
+ m_group_start_timestamp = m_group_end_timestamp;
+ }
+ // 2. Unset the last decode timestamp on all track buffers.
+ // 3. Unset the last frame duration on all track buffers.
+ // 4. Unset the highest end timestamp on all track buffers.
+ unset_all_track_buffer_timestamps();
+ // 5. Set the need random access point flag on all track buffers to true.
+ set_need_random_access_point_flag_on_all_track_buffers(true);
}
- // 2. Unset the last decode timestamp on all track buffers.
- // 3. Unset the last frame duration on all track buffers.
- // 4. Unset the highest end timestamp on all track buffers.
- unset_all_track_buffer_timestamps();
- // 5. Set the need random access point flag on all track buffers to true.
- set_need_random_access_point_flag_on_all_track_buffers(true);
}
}
diff --git a/Tests/LibMedia/TestSourceBufferDemuxer.cpp b/Tests/LibMedia/TestSourceBufferDemuxer.cpp
index bace04f5d84..f03d1c9aa76 100644
--- a/Tests/LibMedia/TestSourceBufferDemuxer.cpp
+++ b/Tests/LibMedia/TestSourceBufferDemuxer.cpp
@@ -199,7 +199,7 @@ TEST_CASE(evicting_the_first_frame_of_a_run_preserves_its_configuration)
for (u64 second = 1; second < 3; second++)
demuxer->add_coded_frame(track, coded_frame_at(second));
- demuxer->take_earliest_frame_and_dependants(track);
+ demuxer->take_earliest_groups(track, 1);
auto sample = MUST(demuxer->get_next_sample_for_track(track));
EXPECT_EQ(sample.presentation_timestamp(), AK::Duration::from_seconds(1));
@@ -220,12 +220,12 @@ TEST_CASE(buffered_bytes_include_the_codec_configurations_that_frames_carry)
EXPECT_EQ(demuxer->total_bytes(track), 4 * (frame_storage + 1) + 300);
// The next frame carries the evicted frame's configuration forward, so the configuration isn't freed.
- EXPECT_EQ(demuxer->take_earliest_frame_and_dependants(track), frame_storage + 1);
+ EXPECT_EQ(demuxer->take_earliest_groups(track, 1), frame_storage + 1);
EXPECT_EQ(demuxer->total_bytes(track), 3 * (frame_storage + 1) + 300);
- EXPECT_EQ(demuxer->take_latest_frame(track).byte_size, frame_storage + 101);
- EXPECT_EQ(demuxer->take_earliest_frame_and_dependants(track), frame_storage + 101);
- EXPECT_EQ(demuxer->take_earliest_frame_and_dependants(track), frame_storage + 101);
+ EXPECT_EQ(demuxer->take_latest_frames(track, 1), frame_storage + 101);
+ EXPECT_EQ(demuxer->take_earliest_groups(track, 1), frame_storage + 101);
+ EXPECT_EQ(demuxer->take_earliest_groups(track, 1), frame_storage + 101);
EXPECT_EQ(demuxer->total_bytes(track), 0u);
}
@@ -247,10 +247,10 @@ TEST_CASE(evictable_bytes_leave_out_the_configuration_that_eviction_moves_forwar
auto evictable_bytes = demuxer->evictable_bytes_when_taking_all_earliest_frames(track, current_time);
EXPECT_EQ(evictable_bytes, 3 * (frame_storage + 1) + 100);
- size_t evicted_bytes = 0;
- while (demuxer->earliest_evictable_frame_timestamp(track, current_time).has_value())
- evicted_bytes += demuxer->take_earliest_frame_and_dependants(track);
- EXPECT_EQ(evicted_bytes, evictable_bytes);
+ auto evictable_groups = demuxer->earliest_evictable_groups(track, current_time, NumericLimits<size_t>::max());
+ EXPECT_EQ(evictable_groups.size(), 3u);
+ EXPECT_EQ(evictable_groups.last().bytes_freed_by_evicting_through_this_group, evictable_bytes);
+ EXPECT_EQ(demuxer->take_earliest_groups(track, evictable_groups.size()), evictable_bytes);
}
// Removing the run that the cursor sits in leaves it with no position to jump from, so a later removal that
@@ -463,6 +463,41 @@ TEST_CASE(splitting_the_last_appended_run_keeps_appending_to_its_tail)
EXPECT_EQ(demuxer->total_bytes(track), 4 * (frame_storage + 1));
}
+TEST_CASE(evicting_the_earliest_groups_continues_into_the_next_run)
+{
+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });
+
+ for (u64 seconds : { 0, 1, 10, 11 })
+ demuxer->add_coded_frame(track, one_second_frame_at(seconds, seconds, Media::FrameFlags::Keyframe));
+
+ // The group being appended to is never evicted.
+ auto groups = demuxer->earliest_evictable_groups(track, AK::Duration::from_seconds(20), NumericLimits<size_t>::max());
+ EXPECT_EQ(groups.size(), 3u);
+ EXPECT_EQ(groups.last().presentation_timestamp, AK::Duration::from_seconds(10));
+ EXPECT_EQ(groups.last().bytes_freed_by_evicting_through_this_group, 3 * (frame_storage + 1));
+
+ EXPECT_EQ(demuxer->take_earliest_groups(track, groups.size()), 3 * (frame_storage + 1));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(11, 12) }));
+}
+
+TEST_CASE(evicting_the_latest_frames_continues_into_the_previous_run)
+{
+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };
+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });
+
+ // Each pair of frames decodes before the last, so the earliest run is the one being appended to.
+ for (u64 seconds : { 20, 21, 10, 11, 0, 1 })
+ demuxer->add_coded_frame(track, one_second_frame_at(seconds, seconds, Media::FrameFlags::Keyframe));
+
+ auto frames = demuxer->latest_evictable_frames(track, AK::Duration::zero(), 3 * (frame_storage + 1));
+ EXPECT_EQ(frames.size(), 3u);
+ EXPECT_EQ(frames.last().presentation_timestamp, AK::Duration::from_seconds(11));
+
+ EXPECT_EQ(demuxer->take_latest_frames(track, frames.size()), 3 * (frame_storage + 1));
+ EXPECT_EQ(demuxer->track_buffer_ranges(track), Media::TimeRanges({ seconds_range(0, 2), seconds_range(10, 11) }));
+}
+
static Media::CodedFrame tenth_of_a_second_frame_at(u64 milliseconds, Media::CodecID codec_id)
{
auto data = MUST(FixedArray<u8>::create(1));
@@ -728,7 +763,7 @@ TEST_CASE(evicting_a_group_of_frames_starts_the_buffered_ranges_at_the_next_grou
for (i64 index = 0; index < 600; index++)
demuxer->add_coded_frame(track, frame_with_rounded_timestamp(index));
- demuxer->take_earliest_frame_and_dependants(track);
+ demuxer->take_earliest_groups(track, 1);
auto end = rounded_frame_start(599) + rounded_frame_duration;
EXPECT_EQ(demuxer->track_buffer_ranges(track), (Media::TimeRanges { { rounded_frame_start(120), end } }));