Skip to content

fix(media player): share the flow's GL context with the internal pipeline - #1108

Merged
srperens merged 2 commits into
mainfrom
fix/mediaplayer-gl-context
Oct 27, 2026
Merged

srperens merged 2 commits into
mainfrom
fix/mediaplayer-gl-context

Conversation

@srperens

@srperens srperens commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #1099

Problem

A Media Player decodes in a pipeline of its own and hands the frames to the flow over an appsink/appsrc pair. Two GStreamer pipelines share no context. A decoder that outputs GL memory, VideoToolbox on macOS, made a GL context of its own. The flow's first GL converter then failed on the first frame, and the clip's video branch stayed dead until the flow restarted:

glcolorconvert0 input memory OpenGL context is different. we have <glcontext…1> memory has <glcontext…0>
Failed to convert video buffer
Internal data stream error.

This is the bug #1091 reports.

Change

  • New gst::context_forward::install(internal, flow_element). It installs a sync handler on the internal pipeline's bus that posts GL NEED_CONTEXT and HAVE_CONTEXT messages again from an element in the flow. The flow's bin answers the internal pipeline's requests from its cache and keeps the contexts the internal pipeline makes, so both sides end up in the same GL context. webrtcsink does the same for its session pipelines (CustomBusStream).
  • The Media Player installs it on its decode-mode internal pipeline, with MediaPlayerState::flow_anchor() (first video appsrc, else first audio appsrc) as the target. That choice also finds the flow's pipeline for timing, so it now lives in one place. Passthrough carries encoded media and does not install it. The internal bus had no sync handler before.
  • Order: the flow's GL elements make their display when the flow goes to READY. The internal pipeline starts once the flow plays, so it asks for a display the flow already has. A stinger clip may preroll earlier, but its appsink takes system memory, so it makes no GL context. This is written down at share_flow_contexts.
  • docs/BLOCK_GUIDELINES.md gains a rule: a block that decodes in an isolation pipeline shares the flow's GL context. It also has a guard test that builds the real block and pushes GL memory from its internal pipeline into a GL converter in the flow.

Rejected:

Evidence

  • GL frames from the Media Player's internal pipeline convert in the flow: mediaplayer_gl_context_test::gl_frames_from_the_internal_pipeline_convert_in_the_flow. It builds the real block and uses the internal pipeline and appsrc the block made. CI has no decoder that outputs GL memory, so gltestsrc stands in for the decoder inside that pipeline. With the block's share_flow_contexts call commented out it fails: the flow's GL converter failed on the player's frames: Failed to convert video buffer (…gstglcolorconvertelement.c(233)…).
  • The decode pipeline asks the flow for its GL context and gives the flow the one it makes: mediaplayer::bridge::tests::the_decode_pipeline_shares_the_flows_gl_context. It goes through the real create_decode_pipeline, with test contexts, so it needs no GL. It fails with the call removed.
  • The forwarding itself: gst::context_forward::tests. A GL request is answered by the flow, a GL context made inside is kept by the flow, a CUDA context stays inside, and a torn-down flow leaves the messages inside.
  • The bug is gone in a real Strom on macOS (checked on both commits): debug build, macOS arm64, GStreamer 1.28.6. The flow was a Media Player with a 720p H.264 clip on loop, into a GPU Vision Mixer, into a fakesink, run for 15 s. Same flow, same clip:
    • main from 2026-10-21: glcolorconvert "Failed to convert video buffer", then two "Internal data stream error";
    • this branch: no errors. /api/flows/{id}/pad-caps shows video/x-raw(memory:GLMemory), NV12, 1280x720 at mp:appsrc_video, mp:video_out and vm:glupload_0. VideoToolbox's frames reach the mixer in GL memory, without a copy.
  • That GL is the only memory type that needs this, and that forwarding gives the same context on all three platforms: measured with tools/gpu-context-probe on macOS, Linux (NVIDIA L4, strom-full:0.7.1) and Windows. The tables are in GL memory across pipeline boundaries: forward the context in Media Player, download at Inter Input #1099.

Tests

Ran on macOS arm64, GStreamer 1.28.6:

  • cargo test --features no-gui --lib -- mediaplayer context_forward: 43 passed.
  • STROM_REQUIRE_GL=1 cargo test --features no-gui --test mediaplayer_gl_context_test: 1 passed. The test ran, it did not skip.
  • media_player_control_test, media_player_hls_test, stinger_test (14 passed): pass on the second commit. stinger_clip_test (3 passed, 1 ignored as before) and stinger_software_decode_test also passed on the first commit.
  • The pre-commit hook (fmt, clippy) passed.

Not run:

  • Linux and Windows locally. CI runs the GL test on Linux with surfaceless EGL (STROM_REQUIRE_GL).
  • The test is not built on Windows: no GL context on that runner, same as the other GL tests.

For the reviewer

  • Behaviour change on macOS: a decoded clip now reaches the flow in GL memory instead of failing. Consumers that are not GL (CPU mixer, encoders, outputs) adapt as before, through video_adapt::decide() and gl_link.
  • CUDA and D3D are untouched: only GL context types are forwarded, so NVIDIA and Windows hosts keep separate CUDA contexts and D3D devices per pipeline, as before.
  • The internal bus has one sync-handler slot, and context_forward takes it. If thread priority or affinity is ever extended to the Media Player's internal pipeline with set_sync_handler, whichever handler is installed last wins silently. The GL guard test would catch the context side of that.
  • fix(media player): keep a clip's decoded video out of GL memory #1091 fixes the same bug by pinning the appsink to CUDA/system memory. This PR makes that unnecessary. fix(media player): keep a clip's decoded video out of GL memory #1091 also answers classic uridecodebin's autoplug-query, so that a second video track negotiates before its appsink is linked. That part is not needed here: with a shared context, GL memory is fine on any track.
  • mediaplayer/bridge.rs is now 2,100 lines. CLAUDE.md asks to split files over 1,500. The pipeline construction (create_*_pipeline, route_pad, link_pad_through_clocksync) and the bus watch could be two modules. Not done here, to keep the diff small.

Out of scope

🤖 Generated with Claude Code

https://claude.ai/code/session_01MwajhMq9cb3pXCT6WFZfwc

…line

A Media Player decodes in a pipeline of its own and hands the frames to
the flow over appsink/appsrc. Two pipelines share no GStreamer context,
so a decoder that outputs GL memory (VideoToolbox on macOS) made a GL
context of its own, and the flow's first GL converter failed on the
first frame: "input memory OpenGL context is different", then an
internal data stream error.

gst::context_forward posts the internal pipeline's NEED_CONTEXT and
HAVE_CONTEXT messages again from the block's appsrc in the flow, the way
webrtcsink does for its session pipelines. The flow's bin answers from
its cache and keeps what the internal pipeline makes, so both use the
same context, and GL frames reach the mixer without a copy.

BLOCK_GUIDELINES gains the rule: a block with an isolation pipeline
shares the flow's contexts and has a guard test that pushes GL memory
across its boundary.

Refs #1099, #1091

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MwajhMq9cb3pXCT6WFZfwc

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Area: Media Player, GPU memory across pipeline boundaries

The PR forwards the internal pipeline's NEED_CONTEXT and HAVE_CONTEXT to the flow, so both sides use one GL context. The approach looks right and matches #1099. The handler holds its target weakly (context_forward.rs:44) and adds no probe. The internal pipeline starts only once the flow is PLAYING (builder.rs:444), so the flow's contexts exist first.

Neighbours: #1091 fixes the same macOS bug the other way, by keeping decoded video out of GL. The two merge without a textual conflict, but if both land, #1091's appsink caps keep GL memory out and this forwarding does nothing for GL. Only one of them is needed. #1021 (Video Encoder downloading GL at runtime) is not affected. Inter Input, the other half of #1099, is not in this PR.

  1. The GL test builds both pipelines by hand, so nothing pushes GL memory through the Media Player itself (inline). The rule this PR adds to BLOCK_GUIDELINES asks for exactly that test.
  2. pipeline_lifecycle_test.rs has no Media Player, so teardown with the forwarding installed is not covered. Not verified.
  3. CI: Check (Linux) was still pending and macOS was skipped when I looked. Linux has GL with STROM_REQUIRE_GL=1, so the GL test should run there. VideoToolbox is covered only by the author's manual run.

.unwrap()
.downcast::<gst_app::AppSrc>()
.unwrap();
context_forward::install(&internal, &appsrc);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This builds both pipelines by hand and calls context_forward::install directly, so it guards the helper, not the Media Player. CLAUDE.md: "A test that reconstructs a pipeline topology by hand documents a bug; it does not stop the bug returning." The Media Player's own wiring is covered only by the bridge.rs unit test, which uses a made-up context type and no GL. Nothing sends GL frames through create_decode_pipeline into the flow.

Comment thread docs/BLOCK_GUIDELINES.md Outdated
- A block emits the memory type it naturally produces (system, GL, CUDA, ...). The **consuming** block adapts its own input. A producer does not know its consumer, so any producer-side download is wrong for half the graph and costs a GPU round trip per frame in the other half.
- Adapt at build time where the input is known (`glupload` on a GL consumer's inputs). Where it depends on what `decodebin` autoplugged upstream, decide from the negotiated caps — `gst::video_input_bridge` does this for WHEP Output's video input.
- Beware sinks that advertise GPU memory features they cannot actually process: `whepserversink` accepts `video/x-raw(memory:GLMemory)` and then fails encoder discovery. A successful link is not proof the consumer can use the frames.
- **A block that runs a pipeline of its own (an isolation pipeline behind appsink/appsrc) shares the flow's contexts with it.** GL memory is only usable in the GL context that made it, and two pipelines share no context unless the block hands it over. Install `gst::context_forward` on the internal pipeline, with an element of the flow as the target. CUDA and D3D memory cross without it, but forward every context type anyway. A guard test pushes GL memory across the block's boundary into a GL converter in the flow, and fails if the forwarding is removed: a frame from another context fails there with "input memory OpenGL context is different". Frames that cross between two flows (Inter) cannot share a context, because each flow starts and stops on its own. The consumer downloads them instead.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This rule asks for a guard test that pushes GL memory across the block's boundary into a GL converter in the flow. The block that introduces the rule has no such test: its GL test crosses a hand-built appsink/appsrc pair. Either add the test for the Media Player, or word the rule to match what is tested here, so the next isolation-pipeline block knows what it is held to.

…al block

Review findings on the first commit:

- context_forward forwarded every context type. A bin caches one context
  per type, so an internal decoder's CUDA context or D3D device could
  replace the flow's own on a multi-GPU host. CUDA and D3D memory cross
  without a shared context (#1099), so only GL context types go through
  now.
- The GL guard test built the topology by hand and called install()
  directly, so removing the block's call left it green. It now builds the
  real Media Player and pushes GL frames from the internal pipeline the
  block made into the block's own appsrc and a GL converter in the flow.
- Passthrough mode carries encoded media and no longer installs it.
- The flow-anchor choice (first video appsrc, else audio) lives in one
  place, MediaPlayerState::flow_anchor().
- The unit test picks the decoder by name instead of the first child.
- The ordering that keeps both pipelines from making a GL display at the
  same time is written down at share_flow_contexts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MwajhMq9cb3pXCT6WFZfwc
@srperens

Copy link
Copy Markdown
Collaborator Author

Review on f7f9c40 addressed in 3801f1e:

  1. The GL guard test now builds the real Media Player (mediaplayer_gl_context_test). It pushes GL frames from the internal pipeline the block made, through the block's own appsrc, into glcolorconvert in the flow. With the block's share_flow_contexts call removed it fails with Failed to convert video buffer. The rule in BLOCK_GUIDELINES now says the guard test builds the real block.
  2. Teardown: the handler holds only a weak ref to the flow appsrc, and the closure lives on the internal pipeline's bus, which the player's state owns. No strong reference crosses into the flow. Adding a Media Player to pipeline_lifecycle_test is not done here.
  3. CI on 3801f1e: Check (Linux) green, so the GL test ran there under STROM_REQUIRE_GL=1.

Also in 3801f1e, from a separate review: only GL context types are forwarded (a CUDA/D3D device context could replace the flow's on a multi-GPU host), and passthrough mode no longer installs it.

@srperens
srperens merged commit 0845f78 into main Oct 27, 2026
9 checks passed
@svensson00

Copy link
Copy Markdown

Verified 2026-10-27 on a fresh Open Live beta instance running Strom v0.7.2: clip sources play as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants