Skip to content

media: Plumb a release closure through MSE append path - #12846

Merged
jasonzhangxx merged 1 commit into
youtube:mainfrom
jasonzhangxx:zero-copy-append-plumbing
Sep 30, 2026
Merged

jasonzhangxx merged 1 commit into
youtube:mainfrom
jasonzhangxx:zero-copy-append-plumbing

Conversation

@jasonzhangxx

Copy link
Copy Markdown
Contributor

Thread a base::ScopedClosureRunner alongside the data from SourceBuffer down to StreamParser, so that a parser can borrow the span instead of copying it. The caller guarantees the span stays valid until the runner is destroyed. The parser destroys it once it no longer needs the bytes, which may happen on any thread.

Every layer from SourceBuffer down to SourceBufferState carries the runner unconditionally, so each has a single AppendToParseBuffer() whose signature differs between builds, rather than a second overload beside the existing one. StreamParser is the exception: there the new overload is non-pure and copies by default, which leaves the five existing parsers and MockStreamParser untouched.

Gate this on kCobaltZeroCopyMediaSourceParser, disabled by default, as retaining the ArrayBuffer deviates from the specification.

No behavior change yet with the feature off SourceBuffer passes a null runner, and with it on the parsers still take StreamParser's copying default.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

@jasonzhangxx
jasonzhangxx requested review from a team as code owners September 22, 2026 21:15
@github-actions

Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


media: Plumb release closure through MSE append path

Thread a base::ScopedClosureRunner from SourceBuffer down to the
StreamParser to enable zero-copy media parsing. This allows the
parser to borrow the data span rather than copying it into an
internal buffer, reducing memory overhead.

The runner ensures the memory remains valid until the parser
explicitly releases it. This optimization is gated by the
kCobaltZeroCopyMediaSourceParser feature flag and is currently
disabled by default.

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
@github-actions

github-actions Bot commented Sep 22, 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
@jasonzhangxx jasonzhangxx changed the title media: Plumb a release closure through the MSE append path Sep 22, 2026

@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 introduces a zero-copy media source parser feature (kCobaltZeroCopyMediaSourceParser) for Cobalt, allowing stream parsers to parse appended data in place by borrowing the data span and managing its lifetime with a base::ScopedClosureRunner. The feedback highlights critical compilation issues where base::ScopedClosureRunner is incorrectly used in boolean contexts (as it lacks operator bool() and operator!), and suggests using .is_null() instead. Additionally, a defensive null check is recommended in RetainAppendedArrayBuffer to prevent potential null pointer dereferences.

Comment thread media/filters/source_buffer_state.cc Resolved
Comment thread media/base/media_switches.cc Resolved
Comment thread media/base/stream_parser.h Resolved
Comment thread media/base/stream_parser.h Resolved
@jasonzhangxx
jasonzhangxx force-pushed the zero-copy-append-plumbing branch from e5a5930 to 4437ab8 Compare September 27, 2026 03:14
@jasonzhangxx
jasonzhangxx requested a review from a team September 29, 2026 03:04
Comment thread media/base/media_switches.cc Resolved
Thread a base::ScopedClosureRunner alongside the data from SourceBuffer
down to StreamParser, so that a parser can borrow the span instead of
copying it. The caller guarantees the span stays valid until the runner
is destroyed. The parser destroys it once it no longer needs the bytes,
which may happen on any thread.

Every layer from SourceBuffer down to SourceBufferState carries the
runner unconditionally, so each has a single AppendToParseBuffer() whose
signature differs between builds, rather than a second overload beside
the existing one. StreamParser is the exception: there the new overload
is non-pure and copies by default, which leaves the five existing parsers
and MockStreamParser untouched.

Gate this on kCobaltInPlaceMediaSourceParser, disabled by default, as
retaining the ArrayBuffer deviates from the specification.

No behavior change yet with the feature off SourceBuffer passes a null
runner, and with it on the parsers still take StreamParser's copying
default.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

@yell0wd0g yell0wd0g 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.

Some comments on the ergonomics and some question
but in general makes sense? //third_party LGTM.

Comment thread media/base/stream_parser.h Resolved
Comment thread media/base/stream_parser.h Resolved
@jasonzhangxx
jasonzhangxx merged commit 7299a39 into youtube:main Sep 30, 2026
956 of 968 checks passed
@jasonzhangxx
jasonzhangxx deleted the zero-copy-append-plumbing branch September 30, 2026 18:32
@github-actions github-actions Bot added the cp-27.lts Cherry Pick to the 27.lts branch label Sep 30, 2026
briantting pushed a commit that referenced this pull request Sep 30, 2026
…h MSE append path

Refer to original PR: #12846

Thread a base::ScopedClosureRunner alongside the data from SourceBuffer
down to StreamParser, so that a parser can borrow the span instead of
copying it. The caller guarantees the span stays valid until the runner
is destroyed. The parser destroys it once it no longer needs the bytes,
which may happen on any thread.

Every layer from SourceBuffer down to SourceBufferState carries the
runner unconditionally, so each has a single AppendToParseBuffer() whose
signature differs between builds, rather than a second overload beside
the existing one. StreamParser is the exception: there the new overload
is non-pure and copies by default, which leaves the five existing
parsers and MockStreamParser untouched.

Gate this on kCobaltZeroCopyMediaSourceParser, disabled by default, as
retaining the ArrayBuffer deviates from the specification.

No behavior change yet with the feature off SourceBuffer passes a null
runner, and with it on the parsers still take StreamParser's copying
default.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

(cherry picked from commit 7299a39)
briantting pushed a commit that referenced this pull request Sep 30, 2026
…ugh MSE append path (#13035)

Automated cherry-pick roll to staging.

Original pull requests:
- #12846

CONFLICTED files:
```
third_party/blink/renderer/modules/mediasource/source_buffer.cc
```

To merge these changes, run the autoroll script:

```bash
GITHUB_REPOSITORY=youtube/cobalt python3 cobalt/devinfra/github/autoroll/merge_autoroll.py \
  --source-branch main \
  --target-branch staging
```

⚠️ This PR has conflicts that must be resolved manually (don't forget to
update the `.github/AUTOROLL` file). Use the following commands to
checkout the branch locally and resolve the conflicts in a separate
commit:

```bash
gh pr checkout <pull_request_number> -R youtube/cobalt

# Resolve conflicts and update .github/AUTOROLL then
git add <resolved_files>

# Copy CONFLICTED commit's message and metadata
# Replace CONFLICTED in title with RESOLVED
git commit -c <conflicted_sha>

git push origin autoroll-main-to-staging
```

---------

Co-authored-by: Jason <jasonzh@google.com>
@oxve oxve removed the cp-27.lts Cherry Pick to the 27.lts branch label Oct 5, 2026
@oxve oxve added cp-27.lts Cherry Pick to the 27.lts branch and removed cp-27.lts Cherry Pick to the 27.lts branch labels Oct 13, 2026
briantting pushed a commit that referenced this pull request Oct 16, 2026
…end path

Refer to original PR: #12846

Thread a base::ScopedClosureRunner alongside the data from SourceBuffer
down to StreamParser, so that a parser can borrow the span instead of
copying it. The caller guarantees the span stays valid until the runner
is destroyed. The parser destroys it once it no longer needs the bytes,
which may happen on any thread.

Every layer from SourceBuffer down to SourceBufferState carries the
runner unconditionally, so each has a single AppendToParseBuffer() whose
signature differs between builds, rather than a second overload beside
the existing one. StreamParser is the exception: there the new overload
is non-pure and copies by default, which leaves the five existing
parsers and MockStreamParser untouched.

Gate this on kCobaltZeroCopyMediaSourceParser, disabled by default, as
retaining the ArrayBuffer deviates from the specification.

No behavior change yet with the feature off SourceBuffer passes a null
runner, and with it on the parsers still take StreamParser's copying
default.

Bug: 558350763
TAG=agy
CONV=f7ab1644-7417-41cb-a782-c47dc051b09f

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

Labels

None yet

6 participants