Truly independent web browser
LibMedia+Tests: Count frame storage toward MSE quotas
Zaggy10249b4726e283a5
diff --git a/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cpp b/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cppindex 30023d3a4be..3b48c187bde 100644--- a/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cpp+++ b/Libraries/LibMedia/MediaSourceExtensions/ISOBMFFByteStreamParser.cpp@@ -366,6 +366,9 @@ Media::DecoderErrorOr<ParseMediaSegmentResult> ISOBMFFByteStreamParser::parse_mereturn Media::DecoderError::corrupted("A sample is not contained by any Media Data box"sv);TRY(skip_to(sample.data_position));+ // AD-HOC: Skip samples that contain no data, as they are invalid for any codec we support.+ if (sample.data_size == 0)+ return {};auto data = TRY(streamer.read_bytes(sample.data_size));segment.position += sample.data_size;diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cppindex 029844c3a48..fde0d136a38 100644--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.cpp@@ -11,6 +11,8 @@namespace Media::MediaSourceExtensions {+static_assert(sizeof(Media::CodedFrame) <= SourceBufferDemuxer::buffered_bytes_per_frame);+SourceBufferDemuxer::SourceBufferDemuxer(Vector<Media::Track> const& tracks){m_scan_state.buffered_ranges_policy = buffered_ranges_policy();@@ -205,7 +207,7 @@ void SourceBufferDemuxer::recalculate_run_bounds(FrameRun& run)static size_t buffered_byte_count(Media::CodedFrame const& frame){- auto byte_count = frame.data().size();+ auto byte_count = SourceBufferDemuxer::buffered_bytes_per_frame + frame.data().size();if (auto configuration = frame.new_codec_configuration(); configuration.has_value())byte_count += configuration.value().size();return byte_count;diff --git a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.hindex 3d876642cfa..7349edd5d55 100644--- a/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h+++ b/Libraries/LibMedia/MediaSourceExtensions/SourceBufferDemuxer.h@@ -67,6 +67,9 @@ public:Media::TimeRanges track_buffer_ranges(Media::Track const&) const;Vector<Media::TrackBufferedRanges> track_buffered_ranges() const;static constexpr Media::BufferedRangesPolicy buffered_ranges_policy() { return Media::BufferedRangesPolicy::AllTracks; }++ // Every buffered frame is charged its own storage in addition to its data.+ static constexpr size_t buffered_bytes_per_frame = 128;Media::TimeRanges buffered_ranges() const;AK::Duration highest_presentation_timestamp(Media::Track const&) const;diff --git a/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp b/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cppindex 8e49ecb3f36..b009268b0ba 100644--- a/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp+++ b/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp@@ -269,6 +269,9 @@ Media::DecoderErrorOr<ParseMediaSegmentResult> WebMByteStreamParser::parse_mediaauto data_position = block.data_position();auto data_size = block.data_size();+ // AD-HOC: Skip blocks that contain no data, as they are invalid for any codec we support.+ if (data_size == 0)+ return IterationDecision::Continue;auto current_position = streamer.position();TRY(cursor.seek(data_position, AK::SeekMode::SetPosition));diff --git a/Tests/LibMedia/TestSourceBufferDemuxer.cpp b/Tests/LibMedia/TestSourceBufferDemuxer.cppindex c8563c4ba15..bace04f5d84 100644--- a/Tests/LibMedia/TestSourceBufferDemuxer.cpp+++ b/Tests/LibMedia/TestSourceBufferDemuxer.cpp@@ -10,6 +10,8 @@#include <LibTest/TestCase.h>#include <Tests/LibMedia/TestMediaCommon.h>+static constexpr auto frame_storage = Media::MediaSourceExtensions::SourceBufferDemuxer::buffered_bytes_per_frame;+static Media::CodedFrame coded_frame_at(u64 seconds, Optional<ReadonlyBytes> new_codec_configuration = {}, Media::CodecID codec_id = Media::CodecID::H264){auto data = MUST(FixedArray<u8>::create(1));@@ -215,15 +217,15 @@ TEST_CASE(buffered_bytes_include_the_codec_configurations_that_frames_carry)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);+ EXPECT_EQ(demuxer->total_bytes(track), 4 * (frame_storage + 1) + 300);- // 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);+ // 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->total_bytes(track), 3 * (frame_storage + 1) + 300);- 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->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->total_bytes(track), 0u);}@@ -243,7 +245,7 @@ TEST_CASE(evictable_bytes_leave_out_the_configuration_that_eviction_moves_forwar// 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);+ 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())@@ -419,7 +421,7 @@ TEST_CASE(frames_sharing_a_decode_timestamp_share_one_copy_of_the_codec_configurnew_configuration = MUST(FixedArray<u8>::create(configuration.span()));demuxer->add_coded_frame(track, { Media::CodecID::H264, AK::Duration::zero(), AK::Duration::zero(), AK::Duration::zero(), Media::FrameFlags::Keyframe, move(data), move(new_configuration) });}- EXPECT_EQ(demuxer->total_bytes(track), 4u + configuration.size());+ EXPECT_EQ(demuxer->total_bytes(track), 4 * (frame_storage + 1) + configuration.size());}TEST_CASE(removing_the_last_appended_frame_drops_the_frames_that_would_follow_it)@@ -432,7 +434,7 @@ TEST_CASE(removing_the_last_appended_frame_drops_the_frames_that_would_follow_itdemuxer->remove_coded_frames_and_dependants_in_range(track, AK::Duration::from_seconds(1), AK::Duration::from_seconds(2));demuxer->add_coded_frame(track, one_second_frame_at(2, 2, Media::FrameFlags::None));- EXPECT_EQ(demuxer->total_bytes(track), 1u);+ EXPECT_EQ(demuxer->total_bytes(track), frame_storage + 1);}TEST_CASE(removing_an_earlier_run_keeps_appending_to_the_last_appended_run)@@ -445,7 +447,7 @@ TEST_CASE(removing_an_earlier_run_keeps_appending_to_the_last_appended_run)demuxer->remove_coded_frames_and_dependants_in_range(track, AK::Duration::zero(), AK::Duration::from_seconds(1));demuxer->add_coded_frame(track, one_second_frame_at(11, 11, Media::FrameFlags::None));- EXPECT_EQ(demuxer->total_bytes(track), 2u);+ EXPECT_EQ(demuxer->total_bytes(track), 2 * (frame_storage + 1));}TEST_CASE(splitting_the_last_appended_run_keeps_appending_to_its_tail)@@ -458,7 +460,7 @@ TEST_CASE(splitting_the_last_appended_run_keeps_appending_to_its_tail)demuxer->remove_coded_frames_and_dependants_in_range(track, AK::Duration::from_seconds(1), AK::Duration::from_seconds(2));demuxer->add_coded_frame(track, one_second_frame_at(4, 4, Media::FrameFlags::None));- EXPECT_EQ(demuxer->total_bytes(track), 4u);+ EXPECT_EQ(demuxer->total_bytes(track), 4 * (frame_storage + 1));}static Media::CodedFrame tenth_of_a_second_frame_at(u64 milliseconds, Media::CodecID codec_id)diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-buffer-full-recovers-after-seek.txt b/Tests/LibWeb/Text/expected/HTML/media-source-buffer-full-recovers-after-seek.txtindex 2803d2154d1..dca72f6a72b 100644--- a/Tests/LibWeb/Text/expected/HTML/media-source-buffer-full-recovers-after-seek.txt+++ b/Tests/LibWeb/Text/expected/HTML/media-source-buffer-full-recovers-after-seek.txt@@ -1,3 +1,3 @@-filled: [0.000-240.000]+filled: [0.000-220.000]append at the start threw QuotaExceededError-append after seeking succeeded: [8.620-260.000]+append after seeking succeeded: [14.880-240.000]diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-eviction-to-exact-capacity.txt b/Tests/LibWeb/Text/expected/HTML/media-source-eviction-to-exact-capacity.txtindex 3b3c851243d..2d0cbc1a52a 100644--- a/Tests/LibWeb/Text/expected/HTML/media-source-eviction-to-exact-capacity.txt+++ b/Tests/LibWeb/Text/expected/HTML/media-source-eviction-to-exact-capacity.txt@@ -1,3 +1,3 @@-filled: [0.000-250.860]-after the append that evicts exactly enough: [0.200-251.860]-next append succeeded: [0.280-251.960]+filled: [0.000-222.420]+after the append that evicts exactly enough: [0.200-223.420]+next append succeeded: [0.380-223.520]diff --git a/Tests/LibWeb/Text/input/HTML/media-source-buffer-full-recovers-after-seek.html b/Tests/LibWeb/Text/input/HTML/media-source-buffer-full-recovers-after-seek.htmlindex fa95a4edd32..5e6504fee5b 100644--- a/Tests/LibWeb/Text/input/HTML/media-source-buffer-full-recovers-after-seek.html+++ b/Tests/LibWeb/Text/input/HTML/media-source-buffer-full-recovers-after-seek.html@@ -30,7 +30,7 @@await appendAndWait(sourceBuffer, generatedOpusCluster(nextFrame, new Array(FRAMES_PER_CLUSTER).fill(FRAME_SIZE)));nextFrame += FRAMES_PER_CLUSTER;};- while ((nextFrame + FRAMES_PER_CLUSTER) * FRAME_SIZE <= AUDIO_SOURCE_BUFFER_CAPACITY)+ while ((nextFrame + FRAMES_PER_CLUSTER) * (FRAME_SIZE + BUFFERED_BYTES_PER_FRAME) <= AUDIO_SOURCE_BUFFER_CAPACITY)await appendCluster();println(`filled: ${timeRangesToString(sourceBuffer.buffered, 3)}`);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 eec11439fae..ef0bb2c1985 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;+ // Evicting a frame frees its data along with the storage charged for it.+ const STORED_FRAME_SIZE = FRAME_SIZE + BUFFERED_BYTES_PER_FRAME;// The buffer also holds the OpusHead, which eviction moves to the first remaining frame.const CODEC_CONFIGURATION_SIZE = 19;@@ -32,11 +34,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 - CODEC_CONFIGURATION_SIZE - finalClusterWithoutLastFrame) % FRAME_SIZE;+ let lastFrameSize = (AUDIO_SOURCE_BUFFER_CAPACITY - CODEC_CONFIGURATION_SIZE - finalClusterWithoutLastFrame) % STORED_FRAME_SIZE;if (lastFrameSize === 0)- lastFrameSize = FRAME_SIZE;+ lastFrameSize = STORED_FRAME_SIZE;const finalClusterSize = finalClusterWithoutLastFrame + lastFrameSize;- const filledFrames = (AUDIO_SOURCE_BUFFER_CAPACITY - CODEC_CONFIGURATION_SIZE - finalClusterSize) / FRAME_SIZE + EVICTED_FRAMES;+ const filledFrames = (AUDIO_SOURCE_BUFFER_CAPACITY - CODEC_CONFIGURATION_SIZE - finalClusterSize) / STORED_FRAME_SIZE + EVICTED_FRAMES;const FRAMES_PER_CLUSTER = 1000;for (let firstFrame = 0; firstFrame < filledFrames; firstFrame += FRAMES_PER_CLUSTER) {@@ -59,17 +61,17 @@// 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 = CODEC_CONFIGURATION_SIZE + (filledFrames - EVICTED_FRAMES + FINAL_CLUSTER_FRAMES - 1) * FRAME_SIZE + lastFrameSize;+ const bufferedBytes = CODEC_CONFIGURATION_SIZE + (filledFrames - EVICTED_FRAMES + FINAL_CLUSTER_FRAMES - 1) * STORED_FRAME_SIZE + lastFrameSize + BUFFERED_BYTES_PER_FRAME;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;+ let nextLastFrameSize = (AUDIO_SOURCE_BUFFER_CAPACITY - bufferedBytes - nextClusterWithoutLastFrame) % STORED_FRAME_SIZE;if (nextLastFrameSize <= 0)- nextLastFrameSize += FRAME_SIZE;+ nextLastFrameSize += STORED_FRAME_SIZE;const nextFrameSizes = new Array(NEXT_CLUSTER_FRAMES).fill(FRAME_SIZE);nextFrameSizes[NEXT_CLUSTER_FRAMES - 1] = nextLastFrameSize;const nextCluster = generatedOpusCluster(filledFrames + FINAL_CLUSTER_FRAMES, nextFrameSizes);const nextEvictedBytes = bufferedBytes + nextCluster.length - AUDIO_SOURCE_BUFFER_CAPACITY;- if (nextEvictedBytes <= 0 || nextEvictedBytes % FRAME_SIZE !== 0)+ if (nextEvictedBytes <= 0 || nextEvictedBytes % STORED_FRAME_SIZE !== 0)throw new Error(`next cluster requires evicting ${nextEvictedBytes} bytes`);try {diff --git a/Tests/LibWeb/Text/input/HTML/media-source-generated-opus-include.js b/Tests/LibWeb/Text/input/HTML/media-source-generated-opus-include.jsindex 46004713826..2e8ae96d808 100644--- a/Tests/LibWeb/Text/input/HTML/media-source-generated-opus-include.js+++ b/Tests/LibWeb/Text/input/HTML/media-source-generated-opus-include.js@@ -2,6 +2,8 @@// NB: This must match the media server's byte capacity for a SourceBuffer with one audio track.const AUDIO_SOURCE_BUFFER_CAPACITY = 12 * 1024 * 1024;+// NB: This must match the storage that the media server charges each buffered frame in addition to its data.+const BUFFERED_BYTES_PER_FRAME = 128;const GENERATED_FRAME_DURATION_MS = 20;function ebmlSizeBytes(size) {