-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: split large appendBuffer to stop QuotaExceededError on high-bitrate segments #7753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alchemyyy
wants to merge
2
commits into
video-dev:master
Choose a base branch
from
alchemyyy:fix/chunked-append-large-segments
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||||||
| import { DEFAULT_TARGET_DURATION } from '../loader/level-details'; | ||||||||||||||||||||||
| import { PlaylistLevelType } from '../types/loader'; | ||||||||||||||||||||||
| import { BufferHelper } from '../utils/buffer-helper'; | ||||||||||||||||||||||
| import { readUint32 } from '../utils/mp4-tools'; | ||||||||||||||||||||||
| import { | ||||||||||||||||||||||
| areCodecsMediaSourceSupported, | ||||||||||||||||||||||
| getCodecCompatibleName, | ||||||||||||||||||||||
|
|
@@ -76,6 +77,120 @@ | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Maximum bytes per appendBuffer() call. Chromium's per-SourceBuffer quota is | ||||||||||||||||||||||
| // ~150MB; keeping each append well under this prevents QuotaExceededError when | ||||||||||||||||||||||
| // a single fMP4 segment is very large (common with high-bitrate remuxed streams). | ||||||||||||||||||||||
| const MAX_APPEND_SIZE = 16 * 1024 * 1024; // 16MB | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Read an unsigned 32-bit big-endian integer from a Uint8Array. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| function readBoxSize(data: Uint8Array, offset: number): number { | ||||||||||||||||||||||
| return readUint32(data, offset); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Create a view into `data` from `start` to `start + length`. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| function sliceView( | ||||||||||||||||||||||
| data: Uint8Array<ArrayBuffer>, | ||||||||||||||||||||||
| start: number, | ||||||||||||||||||||||
| length: number, | ||||||||||||||||||||||
| ): Uint8Array<ArrayBuffer> { | ||||||||||||||||||||||
| return new Uint8Array(data.buffer, data.byteOffset + start, length); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Read the size of an fMP4 box at the given offset, handling both standard | ||||||||||||||||||||||
| * and 64-bit extended sizes. Returns the box size in bytes, or 0 if the | ||||||||||||||||||||||
| * box header is invalid or truncated. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| function readMp4BoxSize(data: Uint8Array, pos: number, end: number): number { | ||||||||||||||||||||||
| if (pos + 8 > end) { | ||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| const size = readBoxSize(data, pos); | ||||||||||||||||||||||
| if (size === 0) { | ||||||||||||||||||||||
| return end - pos; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if (size === 1 && pos + 16 <= end) { | ||||||||||||||||||||||
| const hi = readBoxSize(data, pos + 8); | ||||||||||||||||||||||
| return hi > 0 ? end - pos : readBoxSize(data, pos + 12); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if (size < 8 || pos + size > end) { | ||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return size; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Try to split data at fMP4 top-level box boundaries into chunks of at | ||||||||||||||||||||||
| * most maxBytes. Returns null if box-boundary splitting cannot produce | ||||||||||||||||||||||
| * chunks that all fit within maxBytes (e.g. a single giant mdat box). | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| function splitAtBoxBoundaries( | ||||||||||||||||||||||
| data: Uint8Array<ArrayBuffer>, | ||||||||||||||||||||||
| maxBytes: number, | ||||||||||||||||||||||
| ): Uint8Array<ArrayBuffer>[] | null { | ||||||||||||||||||||||
| if (data.byteLength < 8) { | ||||||||||||||||||||||
| return null; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| const chunks: Uint8Array<ArrayBuffer>[] = []; | ||||||||||||||||||||||
| const end = data.byteLength; | ||||||||||||||||||||||
| let chunkStart = 0; | ||||||||||||||||||||||
| let pos = 0; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| while (pos < end) { | ||||||||||||||||||||||
| const boxSize = readMp4BoxSize(data, pos, end); | ||||||||||||||||||||||
| if (boxSize === 0) { | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if (pos > chunkStart && pos - chunkStart + boxSize > maxBytes) { | ||||||||||||||||||||||
| chunks.push(sliceView(data, chunkStart, pos - chunkStart)); | ||||||||||||||||||||||
| chunkStart = pos; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| pos += boxSize; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if (chunkStart < end) { | ||||||||||||||||||||||
| chunks.push(sliceView(data, chunkStart, end - chunkStart)); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const allFit = | ||||||||||||||||||||||
| chunks.length > 1 && chunks.every((c) => c.byteLength <= maxBytes); | ||||||||||||||||||||||
| return allFit ? chunks : null; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Split a large buffer into chunks of at most maxBytes. | ||||||||||||||||||||||
| * Tries to split at fMP4 top-level box boundaries first. If the data | ||||||||||||||||||||||
| * contains a single box larger than maxBytes (e.g. a giant mdat), falls | ||||||||||||||||||||||
| * back to naive byte splitting — MSE SourceBuffer handles reassembly of | ||||||||||||||||||||||
| * partial boxes internally. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| function splitAppendData( | ||||||||||||||||||||||
| data: Uint8Array<ArrayBuffer>, | ||||||||||||||||||||||
| maxBytes: number, | ||||||||||||||||||||||
| ): Uint8Array<ArrayBuffer>[] { | ||||||||||||||||||||||
| if (data.byteLength <= maxBytes) { | ||||||||||||||||||||||
| return [data]; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const boxChunks = splitAtBoxBoundaries(data, maxBytes); | ||||||||||||||||||||||
| if (boxChunks) { | ||||||||||||||||||||||
| return boxChunks; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Fallback: naive byte splitting. MSE SourceBuffer handles partial box | ||||||||||||||||||||||
| // reassembly across sequential appendBuffer() calls. | ||||||||||||||||||||||
| const chunks: Uint8Array<ArrayBuffer>[] = []; | ||||||||||||||||||||||
| const end = data.byteLength; | ||||||||||||||||||||||
| for (let offset = 0; offset < end; offset += maxBytes) { | ||||||||||||||||||||||
| chunks.push(sliceView(data, offset, Math.min(maxBytes, end - offset))); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return chunks; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export default class BufferController extends Logger implements ComponentAPI { | ||||||||||||||||||||||
| private hls: Hls; | ||||||||||||||||||||||
| private fragmentTracker: FragmentTracker; | ||||||||||||||||||||||
|
|
@@ -779,8 +894,29 @@ | |||||||||||||||||||||
| event: Events.BUFFER_APPENDING, | ||||||||||||||||||||||
| eventData: BufferAppendingData, | ||||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| const { data, type } = eventData; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Split large fMP4 segments into smaller chunks to avoid QuotaExceededError. | ||||||||||||||||||||||
| // Each chunk becomes a separate operation with full error handling. | ||||||||||||||||||||||
| if (data.byteLength > MAX_APPEND_SIZE) { | ||||||||||||||||||||||
|
alchemyyy marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
| const chunks = splitAppendData(data, MAX_APPEND_SIZE); | ||||||||||||||||||||||
| if (chunks.length > 1) { | ||||||||||||||||||||||
| this.log( | ||||||||||||||||||||||
| `Splitting large ${type} append (sn:${eventData.frag.sn}, ` + | ||||||||||||||||||||||
| `${(data.byteLength / 1e6).toFixed(1)}MB) into ${chunks.length} chunks`, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| for (let i = 0; i < chunks.length; i++) { | ||||||||||||||||||||||
| this.onBufferAppending(event, { | ||||||||||||||||||||||
| ...eventData, | ||||||||||||||||||||||
| data: chunks[i], | ||||||||||||||||||||||
|
Comment on lines
+796
to
+798
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The BUFFER_APPENDING event data's hls.js/src/controller/base-stream-controller.ts Lines 1242 to 1251 in 6373b33
|
||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const { tracks } = this; | ||||||||||||||||||||||
| const { data, type, parent, frag, part, chunkMeta, offset } = eventData; | ||||||||||||||||||||||
| const { parent, frag, part, chunkMeta, offset } = eventData; | ||||||||||||||||||||||
| const chunkStats = chunkMeta.buffering[type]; | ||||||||||||||||||||||
| const { sn, cc } = frag; | ||||||||||||||||||||||
| const bufferAppendingStart = self.performance.now(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.