Truly independent web browser
LibMedia: Count codec configurations toward SourceBuffer quotas
Zaggy1024e45c3172e5a8
diff --git a/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cpp b/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cppindex 21bbeecafd2..30023d3a4be 100644--- a/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cpp+++ b/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cpp@@ -376,7 +376,7 @@ Media::DecoderErrorOr<ParseMediaSegmentResult> ISOBMFFByteStreamParser::parse_meauto& active_description = m_active_sample_descriptions.ensure(sample.track_id);auto current_sample_description = ActiveSampleDescription { track_entry, sample.sample_description_index };if (active_description != current_sample_description)- new_codec_configuration = MUST(sample_entry.codec_initialization_data.clone());+ new_codec_configuration = DECODER_TRY_ALLOC(sample_entry.codec_initialization_data.clone());active_description = move(current_sample_description);result.coded_frames.append({diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cppindex 19cfe3d0e54..3f590b4e71c 100644--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp@@ -5,6 +5,7 @@*/#include <AK/BinarySearch.h>+#include <AK/SaturatingMath.h>#include <LibCore/EventLoop.h>#include <LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h>@@ -144,6 +145,14 @@ void SourceBufferDemuxer::recalculate_run_bounds(FrameRun& run)extend_run_bounds_for_frame(run, frame);}+static size_t buffered_byte_count(Media::CodedFrame const& frame)+{+ auto byte_count = frame.data().size();+ if (auto configuration = frame.new_codec_configuration(); configuration.has_value())+ byte_count += configuration.value().size();+ return byte_count;+}+// A run's first frame carries the codec configuration to decode it from, so a run never has to be searched// past to find one.Optional<ReadonlyBytes> SourceBufferDemuxer::codec_configuration_after_frame_prefix(FrameRun const& run, size_t frame_count)@@ -217,13 +226,13 @@ void SourceBufferDemuxer::add_coded_frame(Media::Track const& track, Media::Codecount_frame_duration(data, frame.duration());data.track_buffer_ranges.add_range(frame.presentation_timestamp(), frame.presentation_timestamp() + frame.duration());- data.total_bytes += frame.data().size();data.last_appended_decode_timestamp = frame.decode_timestamp();if (run_to_continue.has_value()) {auto& run = data.runs[run_to_continue.value()];extend_run_bounds_for_frame(run, frame);update_last_appended_codec_configuration();+ data.total_bytes += buffered_byte_count(frame);run.frames.append(move(frame));buffered_data_changed_while_locked();return;@@ -234,6 +243,7 @@ void SourceBufferDemuxer::add_coded_frame(Media::Track const& track, Media::Codeframe.set_new_codec_configuration(MUST(data.last_appended_codec_configuration->clone()));extend_run_bounds_for_frame(run, frame);update_last_appended_codec_configuration();+ data.total_bytes += buffered_byte_count(frame);run.frames.append(move(frame));size_t insert_index = 0;@@ -278,8 +288,10 @@ void SourceBufferDemuxer::split_run(TrackData& data, size_t run_index, size_t spfor (size_t index = split_at; index < run.frames.size(); index++)tail.frames.append(move(run.frames[index]));run.frames.remove(split_at, run.frames.size() - split_at);- if (!tail.frames.first().new_codec_configuration().has_value())+ if (!tail.frames.first().new_codec_configuration().has_value()) {+ data.total_bytes += codec_configuration_before_tail.size();tail.frames.first().set_new_codec_configuration(move(codec_configuration_before_tail));+ }recalculate_run_bounds(run);recalculate_run_bounds(tail);@@ -318,6 +330,7 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_tauto& run = data.runs[run_index];VERIFY(minimum_frame_count > 0);VERIFY(first_frame + minimum_frame_count <= run.frames.size());+ auto total_bytes_before_erasing = data.total_bytes;size_t bytes = 0;size_t frame_count = 0;@@ -329,7 +342,7 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_tdata.track_buffer_ranges.remove_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 += frame.data().size();+ bytes += buffered_byte_count(frame);if (frame.duration() == data.maximum_frame_duration)removed_frames_with_the_maximum_duration++;frame_count++;@@ -365,19 +378,21 @@ size_t SourceBufferDemuxer::erase_frames_and_dependants(TrackData& data, size_tdata.current_run--;else if (data.current_run == run_index)data.cursor_continuity = CursorContinuity::NeedsReanchoring;- return bytes;+ return saturating_sub(total_bytes_before_erasing, data.total_bytes);}if (first_frame == 0) {- if (!run.frames.first().new_codec_configuration().has_value())+ if (!run.frames.first().new_codec_configuration().has_value()) {+ data.total_bytes += codec_configuration_before_remaining_frames.size();run.frames.first().set_new_codec_configuration(move(codec_configuration_before_remaining_frames));+ }} else if (first_frame < run.frames.size()) {VERIFY(run.frames[first_frame].is_keyframe());split_run(data, run_index, first_frame, move(codec_configuration_before_remaining_frames));- return bytes;+ return saturating_sub(total_bytes_before_erasing, data.total_bytes);}recalculate_run_bounds(run);- return bytes;+ return saturating_sub(total_bytes_before_erasing, data.total_bytes);}void SourceBufferDemuxer::remove_coded_frames_and_dependants_in_range(Media::Track const& track, AK::Duration start, AK::Duration end)@@ -460,6 +475,15 @@ size_t SourceBufferDemuxer::evictable_bytes_when_taking_all_earliest_frames(Medisize_t bytes = 0;for (auto const& run : data.runs) {auto const& frames = run.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()) {size_t group_end = group_start + 1;@@ -467,15 +491,20 @@ size_t SourceBufferDemuxer::evictable_bytes_when_taking_all_earliest_frames(Medigroup_end++;size_t group_bytes = 0;+ 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;- group_bytes += frames[index].data().size();+ return bytes_when_stopping_at(group_start);+ 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))- return bytes;+ return bytes_when_stopping_at(group_start);bytes += group_bytes;+ if (group_configuration_size.has_value())+ last_evicted_configuration_size = group_configuration_size.value();group_start = group_end;}}@@ -540,7 +569,7 @@ SourceBufferDemuxer::RemovedFrame SourceBufferDemuxer::take_latest_frame(Media::auto& run = data.runs.last();auto const& frame = run.frames.last();- RemovedFrame removed { .byte_size = frame.data().size(), .decode_timestamp = frame.decode_timestamp() };+ 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);buffered_data_changed_while_locked();diff --git a/Tests/LibMedia/TestSourceBufferDemuxer.cpp b/Tests/LibMedia/TestSourceBufferDemuxer.cppindex cac337e1d9c..d939c8867da 100644--- a/Tests/LibMedia/TestSourceBufferDemuxer.cpp+++ b/Tests/LibMedia/TestSourceBufferDemuxer.cpp@@ -187,6 +187,53 @@ TEST_CASE(evicting_the_first_frame_of_a_run_preserves_its_configuration)EXPECT_EQ(sample.new_codec_configuration(), initial_configuration.span());}+TEST_CASE(buffered_bytes_include_the_codec_configurations_that_frames_carry)+{+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };+ Array<u8, 100> configuration {};+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });++ // Each switch of configuration is charged, and so is the copy that starts a new run.+ demuxer->add_coded_frame(track, coded_frame_at(0, configuration.span()));+ demuxer->add_coded_frame(track, coded_frame_at(1));+ demuxer->add_coded_frame(track, coded_frame_at(2, configuration.span()));+ demuxer->add_coded_frame(track, coded_frame_at(10));+ EXPECT_EQ(demuxer->total_bytes(track), 304u);++ // The next frame carries the evicted frame's configuration forward, so only its sample is freed.+ EXPECT_EQ(demuxer->take_earliest_frame_and_dependants(track), 1u);+ EXPECT_EQ(demuxer->total_bytes(track), 303u);++ EXPECT_EQ(demuxer->take_latest_frame(track).byte_size, 101u);+ EXPECT_EQ(demuxer->take_earliest_frame_and_dependants(track), 101u);+ EXPECT_EQ(demuxer->take_earliest_frame_and_dependants(track), 101u);+ EXPECT_EQ(demuxer->total_bytes(track), 0u);+}++TEST_CASE(evictable_bytes_leave_out_the_configuration_that_eviction_moves_forward)+{+ Media::Track track { Media::TrackType::Video, 1, Media::Track::Kind::Main, {}, {} };+ Array<u8, 100> initial_configuration {};+ Array<u8, 50> changed_configuration {};+ auto demuxer = make_ref_counted<Media::MediaSourceExtensions::SourceBufferDemuxer>(Vector { track });++ demuxer->add_coded_frame(track, coded_frame_at(0, initial_configuration.span()));+ demuxer->add_coded_frame(track, coded_frame_at(1));+ demuxer->add_coded_frame(track, coded_frame_at(2, changed_configuration.span()));+ demuxer->add_coded_frame(track, coded_frame_at(3));+ demuxer->add_coded_frame(track, coded_frame_at(10));++ // Eviction stops at the frame at 3 seconds, which receives the changed configuration.+ auto current_time = AK::Duration::from_seconds(3);+ auto evictable_bytes = demuxer->evictable_bytes_when_taking_all_earliest_frames(track, current_time);+ EXPECT_EQ(evictable_bytes, 103u);++ 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);+}+// Removing the run that the cursor sits in leaves it with no position to jump from, so a later removal that// would otherwise displace it must not discard that.TEST_CASE(a_pending_reanchor_outranks_a_later_jump)diff --git a/Tests/LibWeb/Text/input/HTML/media-source-eviction-to-exact-capacity.html b/Tests/LibWeb/Text/input/HTML/media-source-eviction-to-exact-capacity.htmlindex eac65b3773a..eec11439fae 100644--- a/Tests/LibWeb/Text/input/HTML/media-source-eviction-to-exact-capacity.html+++ b/Tests/LibWeb/Text/input/HTML/media-source-eviction-to-exact-capacity.html@@ -7,6 +7,8 @@// Coded frame eviction that frees exactly enough room for the appended data leaves the buffer able to accept it,// so the buffer full flag must not stay set and reject the next append.const FRAME_SIZE = 1000;+ // The buffer also holds the OpusHead, which eviction moves to the first remaining frame.+ const CODEC_CONFIGURATION_SIZE = 19;asyncTest(done => {function fail(name, error) {@@ -30,11 +32,11 @@const EVICTED_FRAMES = 10;const FINAL_CLUSTER_FRAMES = 50;const finalClusterWithoutLastFrame = generatedOpusCluster(0, new Array(FINAL_CLUSTER_FRAMES).fill(FRAME_SIZE)).length - FRAME_SIZE;- let lastFrameSize = (AUDIO_SOURCE_BUFFER_CAPACITY - finalClusterWithoutLastFrame) % FRAME_SIZE;+ let lastFrameSize = (AUDIO_SOURCE_BUFFER_CAPACITY - CODEC_CONFIGURATION_SIZE - finalClusterWithoutLastFrame) % FRAME_SIZE;if (lastFrameSize === 0)lastFrameSize = FRAME_SIZE;const finalClusterSize = finalClusterWithoutLastFrame + lastFrameSize;- const filledFrames = (AUDIO_SOURCE_BUFFER_CAPACITY - finalClusterSize) / FRAME_SIZE + EVICTED_FRAMES;+ const filledFrames = (AUDIO_SOURCE_BUFFER_CAPACITY - CODEC_CONFIGURATION_SIZE - finalClusterSize) / FRAME_SIZE + EVICTED_FRAMES;const FRAMES_PER_CLUSTER = 1000;for (let firstFrame = 0; firstFrame < filledFrames; firstFrame += FRAMES_PER_CLUSTER) {@@ -57,7 +59,7 @@// Size the next append so that it also requires evicting a whole number of frames exactly, so that an// eviction run for it before appending cannot overshoot and clear the flag either.- const bufferedBytes = (filledFrames - EVICTED_FRAMES + FINAL_CLUSTER_FRAMES - 1) * FRAME_SIZE + lastFrameSize;+ const bufferedBytes = CODEC_CONFIGURATION_SIZE + (filledFrames - EVICTED_FRAMES + FINAL_CLUSTER_FRAMES - 1) * FRAME_SIZE + lastFrameSize;const NEXT_CLUSTER_FRAMES = 5;const nextClusterWithoutLastFrame = generatedOpusCluster(0, new Array(NEXT_CLUSTER_FRAMES).fill(FRAME_SIZE)).length - FRAME_SIZE;let nextLastFrameSize = (AUDIO_SOURCE_BUFFER_CAPACITY - bufferedBytes - nextClusterWithoutLastFrame) % FRAME_SIZE;