Skip to content

media: Fix crash in Decode-to-Texture pipeline - #13114

Merged
at-ninja merged 3 commits into
youtube:mainfrom
at-ninja:dtt-crash
Oct 6, 2026
Merged

at-ninja merged 3 commits into
youtube:mainfrom
at-ninja:dtt-crash

Conversation

@at-ninja

@at-ninja at-ninja commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

On Android, SbPlayerCreate() in decode-to-texture mode constructs a DecodeTarget whose GL texture / SurfaceTexture / Surface are created inside a closure run through SbDecodeTargetRunInGlesContext(). That call is routed to StarboardRendererWrapper::GraphicsContextRunner, which posts the closure to the gpu thread via StarboardGpuFactoryImpl::RunSbDecodeTargetFunctionOnGpu. If the gpu thread cannot make the command buffer's GL context current (the stub was destroyed, or the context is not restorable yet after the app sat idle), the closure is silently skipped. DecodeTarget's constructor then returns with a null surface_texture_, and MediaCodecVideoDecoder:: InitializeCodec() passes it to VideoSurfaceTextureBridge:: SetOnFrameAvailableListener(), which aborts the process.

This change:

  1. Makes RunSbDecodeTargetFunctionOnGpu() report the outcome of the request through a shared, lock-protected GlesClosureRun (ran / no command buffer stub / context could not be made current), and re-acquire the stub if it was cleared by OnWillDestroyStub(), using the channel token / route id cached in Initialize().
  2. Makes the cross-thread path of GraphicsContextRunner() bounded and honest about whether the closure ran. The gpu-thread path is unchanged.
    • "context could not be made current" is transient (the context comes back within a few hundred ms after idle), so it is retried every 100 ms for up to 1 s of wall-clock time.
    • "no command buffer stub" is not retried: this is what a player sees when it is torn down after its renderer lost the stub, and waiting cannot help (an earlier draft retried here and stalled SbPlayerDestroy() for ~5 s on every such teardown).
    • Each request waits at most 2 s for the gpu thread. If the gpu thread does not respond, the request is abandoned: the gpu task takes the same lock before touching the context argument and sees |abandoned|, so the caller's stack-owned context is never used after the caller returns. Worst-case blocking of the caller is therefore ~2 s (wedged gpu thread) or ~1 s plus one gpu round trip (persistent context loss); the normal case is unchanged (one round trip).
  3. Default-initializes DecodeTarget's native_window_/info_ and guards its destructor so a target whose creation closure never ran can be freed, and adds DecodeTarget::is_initialized().
  4. Makes InitializeCodec() fail with a decode error (instead of a JNI abort) if the decode target is still uninitialized after the retries. The output mode is fixed at SbPlayerCreate() and cannot be renegotiated from inside the decoder, so a decoder-side punch-out fallback would leave the renderer polling an empty decode target; a decode error reaches the web app as a normal player error, which can retry.

Bug: 490474392
Bug: 565889635

@at-ninja
at-ninja requested a review from borongc October 5, 2026 19:49
@at-ninja
at-ninja requested a review from a team as a code owner October 5, 2026 19:49

@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 reference-counted GlesClosureRun structure to safely manage RunSbDecodeTargetFunctionOnGpu requests, preventing memory access issues when a caller times out and abandons a task. It also updates StarboardRendererWrapper to handle timeouts and retries, and adds initialization checks to DecodeTarget and MediaCodecVideoDecoder to prevent crashes. The review feedback correctly identifies a memory leak in MediaCodecVideoDecoder::InitializeCodec where calling AddRef() followed by Release() fails to free the acquired decode_target reference, and suggests using SbDecodeTargetRelease instead.

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

🤖 Gemini Suggested Commit Message


media: Fix Decode-to-Texture pipeline crashes

Improve error handling and retry logic when creating decode targets via
the GLES context runner. On Android, if the GPU thread cannot make the GL
context current (e.g., after the app is idle), the decode target
creation closure was previously skipped silently. This led to null
pointer dereferences and process aborts during JNI calls in the video
decoder.

This change updates StarboardGpuFactory to report the outcome of GLES
closures and implements a retry mechanism in StarboardRendererWrapper
for transient context loss. It also ensures DecodeTarget is safely
destructible if uninitialized and makes MediaCodecVideoDecoder report a
decode error rather than crashing when a target cannot be acquired.

Bug: 490474392, 565889635

💡 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 Oct 5, 2026 •

Copy link
Copy Markdown
Contributor
Context State Description
CI / All Pass ⏳ PENDING Checks are running
CI / android ⏳ PENDING Checks are running
CI / evergreen ⏳ PENDING Checks are running
CI / linux ✅ PASS Passed
CI / tvos ✅ PASS Passed
@at-ninja
at-ninja requested a review from kjyoun October 6, 2026 18:23
Drew Thomas added 3 commits October 6, 2026 19:25
On Android, SbPlayerCreate() in decode-to-texture mode constructs a
DecodeTarget whose GL texture / SurfaceTexture / Surface are created inside
a closure run through SbDecodeTargetRunInGlesContext(). That call is routed
to StarboardRendererWrapper::GraphicsContextRunner, which posts the closure
to the gpu thread via StarboardGpuFactoryImpl::RunSbDecodeTargetFunctionOnGpu.
If the gpu thread cannot make the command buffer's GL context current (the
stub was destroyed, or the context is not restorable yet after the app sat
idle), the closure is silently skipped. DecodeTarget's constructor then
returns with a null surface_texture_, and MediaCodecVideoDecoder::
InitializeCodec() passes it to VideoSurfaceTextureBridge::
SetOnFrameAvailableListener(), which aborts the process.

This change:

1. Makes RunSbDecodeTargetFunctionOnGpu() report the outcome of the request
   through a shared, lock-protected GlesClosureRun (ran / no command buffer
   stub / context could not be made current), and re-acquire the stub if it
   was cleared by OnWillDestroyStub(), using the channel token / route id
   cached in Initialize().
2. Makes the cross-thread path of GraphicsContextRunner() bounded and
   honest about whether the closure ran. The gpu-thread path is unchanged.
   - "context could not be made current" is transient (the context comes
     back within a few hundred ms after idle), so it is retried every
     100 ms for up to 1 s of wall-clock time.
   - "no command buffer stub" is not retried: this is what a player sees
     when it is torn down after its renderer lost the stub, and waiting
     cannot help (an earlier draft retried here and stalled
     SbPlayerDestroy() for ~5 s on every such teardown).
   - Each request waits at most 2 s for the gpu thread. If the gpu thread
     does not respond, the request is abandoned: the gpu task takes the
     same lock before touching the context argument and sees |abandoned|,
     so the caller's stack-owned context is never used after the caller
     returns. Worst-case blocking of the caller is therefore ~2 s (wedged
     gpu thread) or ~1 s plus one gpu round trip (persistent context loss);
     the normal case is unchanged (one round trip).
3. Default-initializes DecodeTarget's native_window_/info_ and guards its
   destructor so a target whose creation closure never ran can be freed, and
   adds DecodeTarget::is_initialized().
4. Makes InitializeCodec() fail with a decode error (instead of a JNI abort)
   if the decode target is still uninitialized after the retries. The output
   mode is fixed at SbPlayerCreate() and cannot be renegotiated from inside
   the decoder, so a decoder-side punch-out fallback would leave the renderer
   polling an empty decode target; a decode error reaches the web app as a
   normal player error, which can retry.

Bug: 490474392, 565889635
@at-ninja
at-ninja enabled auto-merge (squash) October 6, 2026 19:30
@at-ninja
at-ninja merged commit 1166e26 into youtube:main Oct 6, 2026
518 of 527 checks passed
briantting pushed a commit that referenced this pull request Oct 8, 2026
… pipeline

Refer to original PR: #13114

On Android, SbPlayerCreate() in decode-to-texture mode constructs a
DecodeTarget whose GL texture / SurfaceTexture / Surface are created
inside a closure run through SbDecodeTargetRunInGlesContext(). That call
is routed to StarboardRendererWrapper::GraphicsContextRunner, which
posts the closure to the gpu thread via
StarboardGpuFactoryImpl::RunSbDecodeTargetFunctionOnGpu. If the gpu
thread cannot make the command buffer's GL context current (the stub was
destroyed, or the context is not restorable yet after the app sat idle),
the closure is silently skipped. DecodeTarget's constructor then returns
with a null surface_texture_, and MediaCodecVideoDecoder::
InitializeCodec() passes it to VideoSurfaceTextureBridge::
SetOnFrameAvailableListener(), which aborts the process.

This change:

1. Makes RunSbDecodeTargetFunctionOnGpu() report the outcome of the
request through a shared, lock-protected GlesClosureRun (ran / no
command buffer stub / context could not be made current), and re-acquire
the stub if it was cleared by OnWillDestroyStub(), using the channel
token / route id cached in Initialize().
2. Makes the cross-thread path of GraphicsContextRunner() bounded and
honest about whether the closure ran. The gpu-thread path is unchanged.
- "context could not be made current" is transient (the context comes
back within a few hundred ms after idle), so it is retried every 100 ms
for up to 1 s of wall-clock time.
- "no command buffer stub" is not retried: this is what a player sees
when it is torn down after its renderer lost the stub, and waiting
cannot help (an earlier draft retried here and stalled SbPlayerDestroy()
for ~5 s on every such teardown).
- Each request waits at most 2 s for the gpu thread. If the gpu thread
does not respond, the request is abandoned: the gpu task takes the same
lock before touching the context argument and sees |abandoned|, so the
caller's stack-owned context is never used after the caller returns.
Worst-case blocking of the caller is therefore ~2 s (wedged gpu thread)
or ~1 s plus one gpu round trip (persistent context loss); the normal
case is unchanged (one round trip).
3. Default-initializes DecodeTarget's native_window_/info_ and guards
its destructor so a target whose creation closure never ran can be
freed, and adds DecodeTarget::is_initialized().
4. Makes InitializeCodec() fail with a decode error (instead of a JNI
abort) if the decode target is still uninitialized after the retries.
The output mode is fixed at SbPlayerCreate() and cannot be renegotiated
from inside the decoder, so a decoder-side punch-out fallback would
leave the renderer polling an empty decode target; a decode error
reaches the web app as a normal player error, which can retry.

Bug: 490474392
Bug: 565889635

---------

Co-authored-by: Drew Thomas <acthomas@google.com>

(cherry picked from commit 1166e26)
briantting pushed a commit that referenced this pull request Oct 8, 2026
…re pipeline (#13205)

Automated cherry-pick roll to staging.

Original pull requests:
- #13114

CONFLICTED files:
```
media/mojo/services/starboard/starboard_renderer_wrapper.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: Drew Thomas <async@google.com>
briantting pushed a commit that referenced this pull request Oct 16, 2026
Refer to original PR: #13114

On Android, SbPlayerCreate() in decode-to-texture mode constructs a
DecodeTarget whose GL texture / SurfaceTexture / Surface are created
inside a closure run through SbDecodeTargetRunInGlesContext(). That call
is routed to StarboardRendererWrapper::GraphicsContextRunner, which
posts the closure to the gpu thread via
StarboardGpuFactoryImpl::RunSbDecodeTargetFunctionOnGpu. If the gpu
thread cannot make the command buffer's GL context current (the stub was
destroyed, or the context is not restorable yet after the app sat idle),
the closure is silently skipped. DecodeTarget's constructor then returns
with a null surface_texture_, and MediaCodecVideoDecoder::
InitializeCodec() passes it to VideoSurfaceTextureBridge::
SetOnFrameAvailableListener(), which aborts the process.

This change:

1. Makes RunSbDecodeTargetFunctionOnGpu() report the outcome of the
request through a shared, lock-protected GlesClosureRun (ran / no
command buffer stub / context could not be made current), and re-acquire
the stub if it was cleared by OnWillDestroyStub(), using the channel
token / route id cached in Initialize().
2. Makes the cross-thread path of GraphicsContextRunner() bounded and
honest about whether the closure ran. The gpu-thread path is unchanged.
- "context could not be made current" is transient (the context comes
back within a few hundred ms after idle), so it is retried every 100 ms
for up to 1 s of wall-clock time.
- "no command buffer stub" is not retried: this is what a player sees
when it is torn down after its renderer lost the stub, and waiting
cannot help (an earlier draft retried here and stalled SbPlayerDestroy()
for ~5 s on every such teardown).
- Each request waits at most 2 s for the gpu thread. If the gpu thread
does not respond, the request is abandoned: the gpu task takes the same
lock before touching the context argument and sees |abandoned|, so the
caller's stack-owned context is never used after the caller returns.
Worst-case blocking of the caller is therefore ~2 s (wedged gpu thread)
or ~1 s plus one gpu round trip (persistent context loss); the normal
case is unchanged (one round trip).
3. Default-initializes DecodeTarget's native_window_/info_ and guards
its destructor so a target whose creation closure never ran can be
freed, and adds DecodeTarget::is_initialized().
4. Makes InitializeCodec() fail with a decode error (instead of a JNI
abort) if the decode target is still uninitialized after the retries.
The output mode is fixed at SbPlayerCreate() and cannot be renegotiated
from inside the decoder, so a decoder-side punch-out fallback would
leave the renderer polling an empty decode target; a decode error
reaches the web app as a normal player error, which can retry.

Bug: 490474392
Bug: 565889635

---------

Co-authored-by: Drew Thomas <acthomas@google.com>

(cherry picked from commit 1166e26)
briantting pushed a commit that referenced this pull request Oct 16, 2026
Refer to original PR: #13114

On Android, SbPlayerCreate() in decode-to-texture mode constructs a
DecodeTarget whose GL texture / SurfaceTexture / Surface are created
inside a closure run through SbDecodeTargetRunInGlesContext(). That call
is routed to StarboardRendererWrapper::GraphicsContextRunner, which
posts the closure to the gpu thread via
StarboardGpuFactoryImpl::RunSbDecodeTargetFunctionOnGpu. If the gpu
thread cannot make the command buffer's GL context current (the stub was
destroyed, or the context is not restorable yet after the app sat idle),
the closure is silently skipped. DecodeTarget's constructor then returns
with a null surface_texture_, and MediaCodecVideoDecoder::
InitializeCodec() passes it to VideoSurfaceTextureBridge::
SetOnFrameAvailableListener(), which aborts the process.

This change:

1. Makes RunSbDecodeTargetFunctionOnGpu() report the outcome of the
request through a shared, lock-protected GlesClosureRun (ran / no
command buffer stub / context could not be made current), and re-acquire
the stub if it was cleared by OnWillDestroyStub(), using the channel
token / route id cached in Initialize().
2. Makes the cross-thread path of GraphicsContextRunner() bounded and
honest about whether the closure ran. The gpu-thread path is unchanged.
- "context could not be made current" is transient (the context comes
back within a few hundred ms after idle), so it is retried every 100 ms
for up to 1 s of wall-clock time.
- "no command buffer stub" is not retried: this is what a player sees
when it is torn down after its renderer lost the stub, and waiting
cannot help (an earlier draft retried here and stalled SbPlayerDestroy()
for ~5 s on every such teardown).
- Each request waits at most 2 s for the gpu thread. If the gpu thread
does not respond, the request is abandoned: the gpu task takes the same
lock before touching the context argument and sees |abandoned|, so the
caller's stack-owned context is never used after the caller returns.
Worst-case blocking of the caller is therefore ~2 s (wedged gpu thread)
or ~1 s plus one gpu round trip (persistent context loss); the normal
case is unchanged (one round trip).
3. Default-initializes DecodeTarget's native_window_/info_ and guards
its destructor so a target whose creation closure never ran can be
freed, and adds DecodeTarget::is_initialized().
4. Makes InitializeCodec() fail with a decode error (instead of a JNI
abort) if the decode target is still uninitialized after the retries.
The output mode is fixed at SbPlayerCreate() and cannot be renegotiated
from inside the decoder, so a decoder-side punch-out fallback would
leave the renderer polling an empty decode target; a decode error
reaches the web app as a normal player error, which can retry.

Bug: 490474392
Bug: 565889635

---------

Co-authored-by: Drew Thomas <acthomas@google.com>

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

Labels

None yet

3 participants