Skip to content

media: Add SourceBuffer.appendTransferredBuffer() for in-place MSE parsing - #13521

Merged
jasonzhangxx merged 1 commit into
youtube:mainfrom
jasonzhangxx:media-source-new-append-buffer-api
Oct 26, 2026
Merged

jasonzhangxx merged 1 commit into
youtube:mainfrom
jasonzhangxx:media-source-new-append-buffer-api

Conversation

@jasonzhangxx

Copy link
Copy Markdown
Contributor

Expose SourceBuffer.appendTransferredBuffer(ArrayBuffer) and SourceBuffer.appendTransferredBuffer(ArrayBufferView) as a Cobalt-only MSE extension that unconditionally retains the appended ArrayBuffer and passes a release closure down the append path, allowing the stream parser to borrow the buffer in place regardless of feature flags.

For standard SourceBuffer.appendBuffer(), only retain the appended ArrayBuffer and pass a release closure when both kCobaltInPlaceMediaSourceParser and kCobaltInPlaceMediaSourceParserBorrowMode are enabled.

In MP4StreamParser and WebMStreamParser:

  • The 2-arg AppendToParseBuffer(buf, release_runner) overload now unconditionally initializes segmented_queue_ in borrow mode (borrow_mode=true) without checking kCobaltInPlaceMediaSourceParser or kCobaltInPlaceMediaSourceParserBorrowMode.
  • The 1-arg AppendToParseBuffer(buf) overload checks if kCobaltInPlaceMediaSourceParser is enabled, and if so, initializes segmented_queue_ in copy mode (borrow_mode=false).

Bug: 558350763
TAG=agy
CONV=af5995e6-aeff-480f-8031-f5d6b3b58791

@jasonzhangxx
jasonzhangxx requested review from a team as code owners October 20, 2026 18:23

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the MP4StreamParser and WebMStreamParser to support in-place parsing in both copy and borrow modes under the kCobaltInPlaceMediaSourceParser feature flag. It also renames SourceBufferWriteHead to SourceBufferExtensions and exposes appendTransferredBuffer to the SourceBuffer IDL interface. The review feedback highlights critical use-after-free vulnerabilities that can occur if the copying and borrow-mode overloads of AppendToParseBuffer are mixed after the segmented queue is initialized. To mitigate this, it is recommended to track the queue's borrow mode using a member variable and enforce strict checks in both overloads.

Comment thread media/formats/mp4/mp4_stream_parser.h Resolved
Comment thread media/formats/mp4/mp4_stream_parser.cc Resolved
Comment thread media/formats/mp4/mp4_stream_parser.cc Resolved
Comment thread media/formats/webm/webm_stream_parser.h Resolved
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor
Context State Description
CI / All Pass ✅ PASS All checks passed
CI / android ✅ PASS Passed
CI / evergreen ✅ PASS Passed
CI / linux ✅ PASS Passed
CI / tvos ✅ PASS Passed
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


media: Add SourceBuffer.appendTransferredBuffer

Introduce SourceBuffer.appendTransferredBuffer() as a Cobalt-specific
MSE extension to allow in-place parsing by unconditionally retaining
the appended ArrayBuffer. This passes a release closure down the
append path, enabling the stream parser to borrow the buffer directly.

Update MP4StreamParser and WebMStreamParser to handle these transfers
via segmented byte queues. Standard appendBuffer calls now selectively
use borrow mode based on feature flags, while the new extension
always enables it to reduce data copying. As part of this change,
consolidate Cobalt-specific SourceBuffer extensions into a single
class.

Bug: 558350763

💡 Pro Tips for a Better Commit Message:

  1. Influence the Result: Want to change the output? You can write custom prompts or instructions directly in the Pull Request description. The model uses that text to generate the message.
  2. Re-run the Generator: Post a comment with: /generate-commit-message
…rsing

Expose `SourceBuffer.appendTransferredBuffer(ArrayBuffer)` and
`SourceBuffer.appendTransferredBuffer(ArrayBufferView)` as a Cobalt-only
MSE extension that unconditionally retains the appended `ArrayBuffer`
and passes a release closure down the append path, allowing the stream
parser to borrow the buffer in place regardless of feature flags.

For standard `SourceBuffer.appendBuffer()`, only retain the appended
`ArrayBuffer` and pass a release closure when both
`kCobaltInPlaceMediaSourceParser` and
`kCobaltInPlaceMediaSourceParserBorrowMode` are enabled.

In `MP4StreamParser` and `WebMStreamParser`:
- The 2-arg `AppendToParseBuffer(buf, release_runner)` overload now
  unconditionally initializes `segmented_queue_` in borrow mode
  (`borrow_mode=true`) without checking `kCobaltInPlaceMediaSourceParser`
  or `kCobaltInPlaceMediaSourceParserBorrowMode`.
- The 1-arg `AppendToParseBuffer(buf)` overload checks if
  `kCobaltInPlaceMediaSourceParser` is enabled, and if so, initializes
  `segmented_queue_` in copy mode (`borrow_mode=false`).

Bug: 558350763
TAG=agy
CONV=af5995e6-aeff-480f-8031-f5d6b3b58791
@jasonzhangxx
jasonzhangxx force-pushed the media-source-new-append-buffer-api branch from f6c473f to e5eade9 Compare October 23, 2026 21:09
@jasonzhangxx

Copy link
Copy Markdown
Contributor Author

Reply for #13521 (comment), I added explict CHECK to disallow mixing appendTransferredBuffer and appendBuffer.

@jasonzhangxx

Copy link
Copy Markdown
Contributor Author

IIUC, the H5 player will run its own experiment to switch between copy and borrow mode by calling different APIs: appendTransferredBuffer() for borrow mode and appendBuffer() for copy mode.

The copy mode experiment will be run with Finch experiment.

@jasonzhangxx
jasonzhangxx requested a review from kjyoun October 26, 2026 20:06

@andrewsavage1 andrewsavage1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

purely reviewed blink files, but those LGTM

@jasonzhangxx
jasonzhangxx merged commit 6caedb2 into youtube:main Oct 26, 2026
744 of 746 checks passed
@jasonzhangxx
jasonzhangxx deleted the media-source-new-append-buffer-api branch October 26, 2026 23:18
briantting pushed a commit that referenced this pull request Oct 26, 2026
…r() for in-place MSE parsing

Refer to original PR: #13521

Expose `SourceBuffer.appendTransferredBuffer(ArrayBuffer)` and
`SourceBuffer.appendTransferredBuffer(ArrayBufferView)` as a Cobalt-only
MSE extension that unconditionally retains the appended `ArrayBuffer`
and passes a release closure down the append path, allowing the stream
parser to borrow the buffer in place regardless of feature flags.

For standard `SourceBuffer.appendBuffer()`, only retain the appended
`ArrayBuffer` and pass a release closure when both
`kCobaltInPlaceMediaSourceParser` and
`kCobaltInPlaceMediaSourceParserBorrowMode` are enabled.

In `MP4StreamParser` and `WebMStreamParser`:
- The 2-arg `AppendToParseBuffer(buf, release_runner)` overload now
unconditionally initializes `segmented_queue_` in borrow mode
(`borrow_mode=true`) without checking `kCobaltInPlaceMediaSourceParser`
or `kCobaltInPlaceMediaSourceParserBorrowMode`.
- The 1-arg `AppendToParseBuffer(buf)` overload checks if
`kCobaltInPlaceMediaSourceParser` is enabled, and if so, initializes
`segmented_queue_` in copy mode (`borrow_mode=false`).

Bug: 558350763
TAG=agy
CONV=af5995e6-aeff-480f-8031-f5d6b3b58791

(cherry picked from commit 6caedb2)
briantting pushed a commit that referenced this pull request Oct 27, 2026
…r() for in-place MSE parsing

Refer to original PR: #13521

Expose `SourceBuffer.appendTransferredBuffer(ArrayBuffer)` and
`SourceBuffer.appendTransferredBuffer(ArrayBufferView)` as a Cobalt-only
MSE extension that unconditionally retains the appended `ArrayBuffer`
and passes a release closure down the append path, allowing the stream
parser to borrow the buffer in place regardless of feature flags.

For standard `SourceBuffer.appendBuffer()`, only retain the appended
`ArrayBuffer` and pass a release closure when both
`kCobaltInPlaceMediaSourceParser` and
`kCobaltInPlaceMediaSourceParserBorrowMode` are enabled.

In `MP4StreamParser` and `WebMStreamParser`:
- The 2-arg `AppendToParseBuffer(buf, release_runner)` overload now
unconditionally initializes `segmented_queue_` in borrow mode
(`borrow_mode=true`) without checking `kCobaltInPlaceMediaSourceParser`
or `kCobaltInPlaceMediaSourceParserBorrowMode`.
- The 1-arg `AppendToParseBuffer(buf)` overload checks if
`kCobaltInPlaceMediaSourceParser` is enabled, and if so, initializes
`segmented_queue_` in copy mode (`borrow_mode=false`).

Bug: 558350763
TAG=agy
CONV=af5995e6-aeff-480f-8031-f5d6b3b58791

(cherry picked from commit 6caedb2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants