Repository navigation
fix(media player): share the flow's GL context with the internal pipeline - #1108
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
- 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.
pipeline_lifecycle_test.rshas no Media Player, so teardown with the forwarding installed is not covered. Not verified.- 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); |
There was a problem hiding this comment.
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.
| - 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. |
There was a problem hiding this comment.
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
|
Review on f7f9c40 addressed in 3801f1e:
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. |
|
Verified 2026-10-27 on a fresh Open Live beta instance running Strom v0.7.2: clip sources play as before. |
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:
This is the bug #1091 reports.
Change
gst::context_forward::install(internal, flow_element). It installs a sync handler on the internal pipeline's bus that posts GLNEED_CONTEXTandHAVE_CONTEXTmessages 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).gst.gl.*and the X11/Wayland display handles). CUDA and D3D memory cross without a shared context (GL memory across pipeline boundaries: forward the context in Media Player, download at Inter Input #1099). Their contexts are tied to a device, and a bin caches one context per type. Forwarding them could therefore replace the flow's own context on a host where the two use different GPUs.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.share_flow_contexts.docs/BLOCK_GUIDELINES.mdgains 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
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, sogltestsrcstands in for the decoder inside that pipeline. With the block'sshare_flow_contextscall commented out it fails:the flow's GL converter failed on the player's frames: Failed to convert video buffer (…gstglcolorconvertelement.c(233)…).mediaplayer::bridge::tests::the_decode_pipeline_shares_the_flows_gl_context. It goes through the realcreate_decode_pipeline, with test contexts, so it needs no GL. It fails with the call removed.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.glcolorconvert"Failed to convert video buffer", then two "Internal data stream error";/api/flows/{id}/pad-capsshowsvideo/x-raw(memory:GLMemory), NV12, 1280x720atmp:appsrc_video,mp:video_outandvm:glupload_0. VideoToolbox's frames reach the mixer in GL memory, without a copy.tools/gpu-context-probeon 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) andstinger_software_decode_testalso passed on the first commit.Not run:
STROM_REQUIRE_GL).For the reviewer
video_adapt::decide()andgl_link.context_forwardtakes it. If thread priority or affinity is ever extended to the Media Player's internal pipeline withset_sync_handler, whichever handler is installed last wins silently. The GL guard test would catch the context side of that.uridecodebin'sautoplug-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.rsis 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
gldownload. The new guideline names it.🤖 Generated with Claude Code
https://claude.ai/code/session_01MwajhMq9cb3pXCT6WFZfwc