Skip to content

fix(media player): keep a clip's decoded video out of GL memory - #1091

Closed
wagenet wants to merge 4 commits into
Eyevinn:mainfrom
wagenet:wagenet/clip-video-gl-context
Closed

wagenet wants to merge 4 commits into
Eyevinn:mainfrom
wagenet:wagenet/clip-video-gl-context

Conversation

@wagenet

@wagenet wagenet commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On macOS a media player clip never shows video in the vision mixer. The clip's video is decoded by VideoToolbox in the media player's internal pipeline, and with no caps on the bridge appsink the frames stay in GL memory tied to a GL context that pipeline created. The mixer's glcolorconvert requires the buffer's GL context to be the same context as its own, so the first frame fails:

  • glcolorconvert0 input memory OpenGL context is different
  • Failed to convert video buffer from the mixer's glcolorconvert_0
  • Internal data stream error on appsrc_video and queue_video, after which the clip's video branch is dead until the flow restarts.

Audio has no GL step, so it keeps working.

Change

The bridge appsink of every decoded video track now accepts CUDA memory first, then D3D12 and D3D11 memory, then system memory, so GL memory cannot cross pipelines. Stinger clips keep system memory only, as before. Passthrough video and audio keep an open appsink.

CUDA stays in the list so NVIDIA decode still reaches the mixer without leaving the GPU (#952). On macOS the cost is one upload per frame.

Rejected: sharing GL contexts between the pipelines (glcolorconvert needs the same context, not a shared one), and detecting a foreign context at the mixer input (per-buffer check).

Evidence

  • Decoded video can no longer carry GL memory across the bridge: decoded_video_appsink_refuses_gl_memory builds the real bridge chain and checks the appsink caps. With the call site reverted to the stinger-only condition it fails (panic at the clip-video assertion); with the fix it passes.
  • Audio and passthrough video are unchanged: appsink_caps_stay_open_for_audio_and_passthrough. It passes before and after, so it guards a rewrite, not the fix.
  • Real macOS run (arm64, release build, hardware decode, H.264 720p clip, media player into the vision mixer): 0 context is different and 0 Failed to convert lines; the clip advanced without error. Before the change every run logged the errors above.
  • Picture on the program output: a recording of pgm_out from a Strom-only flow (same build, hardware decode on) shows the clip: black for 0.63 s, then 43 distinct frames out of 44 sampled, no GL errors in the log. Not measured: the Open Live cue/play path, the parked first frame, play-to-air latency.

Tests

Ran on macOS arm64: cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo test --lib (975 passed, 6 ignored), and the integration tests media_player_control, media_player_hls, stinger_clip, stinger_software_decode, stinger, stinger_api, video_input_bridge, video_input_bridge_gl: all pass. stinger_prores_test is flaky under load with or without the change (4 of 9 runs failed with it, 4 of 11 without) and passes alone.

Not run: Linux hardware, NVIDIA/CUDA (that caps branch is untested), VA-API, video_input_bridge on Windows.

For the reviewer

  • Behaviour change on macOS: every decoded clip now takes a GL-to-system download in the player and a re-upload in the mixer. I did not measure the per-frame cost.
  • Classic decoder (decoder=classic): decodebin starts the second video stream's VideoToolbox decoder before the appsink is linked, so the appsink caps alone came too late. The block now also answers the decoder's CAPS query through uridecodebin's autoplug-query with the same caps. A two-video-track MP4 through the classic decoder with two video outputs reached EOS 6 of 6 with this change and failed 4 of 4 with the handler disabled (not-negotiated from multiqueue0). That run was a scratch test on macOS arm64 with VideoToolbox; it is not in the suite because it only fails with a hardware decoder. The unit test classic_source_answers_decoder_caps_query_without_gl checks the handler's answer and classic_pipeline_connects_autoplug_query checks the handler is connected on a classic source (fails with the call removed). Neither reproduces the two-track GL case, which only fails with a hardware decoder, so that behaviour is verified by the macOS run above, not by the suite.
  • D3D12 and D3D11 memory stay allowed on the clip appsink: d3d12h264dec decodes to D3D12 memory only, and the consumers download it (fix(video adapt): download D3D12 and D3D11 memory for inputs that cannot take it #1093). Not run on Windows; the caps test covers the appsink, not a playing clip.
  • Non-NVIDIA Linux hardware decoders (VA-API) take the system-memory path too. I could not test them.

Out of scope

I did not check whether other appsink/appsrc hand-offs that feed the mixer have the same GL-context hazard.

🤖 Generated with Claude Code

A hardware decoder left to choose keeps frames in GL memory of a context the
media player's internal pipeline made for itself. The bridge handed them
unchanged to the main flow, where the vision mixer's glcolorconvert refuses a
buffer from any other GL context. The first frame failed and the clip's video
branch stayed dead until the flow restarted.

Restrict the appsink of every decoded video track to CUDA memory, then system
memory. Stinger clips keep system memory only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@wagenet
wagenet marked this pull request as ready for review October 19, 2026 17:52

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Area: Media Player bridge, GL memory crossing pipelines

The PR puts caps on the bridge appsink of each decoded video track (CUDA first, then system memory), so GL memory from the player's internal pipeline cannot reach the flow. The approach looks right. On its face it breaks BLOCK_GUIDELINES ("A block emits the memory type it naturally produces. The consuming block adapts"), but the consumer cannot adapt here. The appsink/appsrc boundary stops caps queries, and the GL context cannot be seen in caps. Upstream glupload passes GL memory through when contexts can share (gstglupload.c:1118), but glcolorconvert needs the very same context (gstglcolorconvert.c:3513). Please say this in the doc comment at bridge.rs:705-710 and on #969, so this choice is not later "retired" as a producer-side one.

Neighbours: #969 lists producer-side memory choices; this adds a justified one. #1021 (Video Encoder GL download) is complementary and does not overlap. Not verified on hardware.

  1. Classic decoder with two video tracks now fails with a silent not-negotiated (stated in the PR). See inline.
  2. Tests: decoded_video_appsink_refuses_gl_memory calls the real link_pad_through_clocksync and fails if the change is reverted. It ran and passed in Check (Linux). It checks caps only. The context failure on macOS has no CI coverage, because the macOS build is skipped.

.features([gst::CAPS_FEATURE_MEMORY_SYSTEM_MEMORY])
.build(),
));
if (state.decode || state.stinger.enabled) && media_type == "video" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Setting caps when the pad is linked comes too late for classic uridecodebin with a second video track. The PR says its VideoToolbox decoder has already chosen GL memory, and the track dies with a silent not-negotiated until restart. On main the same file plays 6 of 6. BLOCK_GUIDELINES says a block that cannot adapt "never leaves a silent not-negotiated". Answer the decoder's CAPS query through uridecodebin's autoplug-query with decoded_video_caps() for the classic decoder (not verified that this covers it), or at least post an element error and open an issue for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude on behalf of the author. Done in a8793d1: classic uridecodebin now answers a video decoder's CAPS query through autoplug-query with decoded_video_caps(). A two-video-track file on the classic decoder reached EOS 6 of 6 on macOS (VideoToolbox) and failed 4 of 4 with the handler disabled. The PR body's known limit is updated. I did not test VA-API or NVIDIA.

…emory

Classic uridecodebin starts every stream's decoder before the bridge
appsink is linked, so a second video track's VideoToolbox decoder picked
GL memory and then failed not-negotiated against the appsink's caps.
Answer a video decoder's CAPS query through autoplug-query with the same
caps the appsink accepts.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
wagenet added a commit to wagenet/strom that referenced this pull request Oct 21, 2026

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Area: Media Player bridge, GL memory crossing pipelines

Since 24d9873: a8793d1 answers the CAPS query that classic decodebin forwards through autoplug-query (bridge.rs:151-153, 738-775). Before a pad is exposed, decodebin answers that query itself, so a decoder that negotiates before its appsink is linked now gets system memory. That addresses previous finding 1. The approach looks right. Not verified on macOS. It is a signal, not a probe. The closure captures only a bool, so there is no strong ref.

Neighbours: #1093 rejects "forcing system memory on the Media Player's appsink" as a producer-side download. That is what this PR does for GL. decoded_video_caps (bridge.rs:715) lists CUDA and system memory only, so after this PR d3d12h264dec is pushed to system memory, if it supports that (not verified), and the Media Player never emits D3D12. #1093's test pushes D3D12 caps into the appsrc directly, so both stay green, but they contradict each other. Decide which owns D3D: add D3D12/D3D11 to these caps, or narrow #1093's rationale.

  1. CI: Check (Linux) fails in vision_mixer_input_format_test::gpu_nv12 ("Failed to create a OpenGL context"). That is unrelated (#1092). The new test ran and passed.
  2. Test: it calls the helpers directly, so deleting the wiring at bridge.rs:151-153 leaves it green. It cannot reproduce the two-track GL case on Linux, so it shows the behaviour but does not guard the fix. Say so in the PR body.

// The signal passes the query by pointer; a `Value` getter would hand
// back a copy and the answer would be lost.
let query = args[3].get::<&gst::QueryRef>().ok()?;
let query = unsafe { &mut *(query.as_mut_ptr() as *mut gst::QueryRef) };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This writes through a shared ref without checking writability. In CI the new test logs GStreamer-CRITICAL: gst_query_set_caps_result: assertion 'gst_query_is_writable (query)' failed, and the handler still returns true with no result set. A caller that trusts true then reads NULL caps. Return false unless query.is_writable(). gst::QueryRef::from_mut_ptr is also cleaner than casting & to &mut.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude on behalf of the author. Done in 16ed324. answer_decoder_caps_query returns false when the query is not writable, and the handler uses QueryRef::from_mut_ptr. A new assertion in the unit test covers a shared query.

/// Fill in `query` when it is a CAPS query from a video decoder; returns
/// whether it did. The answer is the query's filter narrowed to
/// [`decoded_video_caps`], so a decoder with no system-memory output ends up
/// with empty caps and fails the flow with a clear error instead of GL frames.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

"fails the flow with a clear error": nothing here posts one, and there is no element_error! in bridge.rs. Empty caps give the decoder a plain not-negotiated, which BLOCK_GUIDELINES rules out ("never leaves a silent not-negotiated"). Either post an element error that names the memory, or fix the comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude on behalf of the author. The comment was wrong; fixed in 16ed324. It now says an empty answer gives a plain not-negotiated. I did not add an element error: an empty answer to a filtered CAPS query is a normal "no" during caps probing, so erroring there could fail flows that would have worked. That leaves the BLOCK_GUIDELINES gap for a decoder with no output in the allowed memory types; say if you want an error posted for the unfiltered case.

// The signal is wired on a classic source. (A query emitted from here
// is shared, so its answer is not writable; decodebin's is.)
let source = gst::ElementFactory::make("uridecodebin").build().unwrap();
restrict_decoder_memory(&source, false);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This calls restrict_decoder_memory directly, so removing the call at line 152 keeps the test green. Build the source with create_decode_pipeline(.., Decoder::Classic) and emit autoplug-query on that instead, so the wiring is guarded too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude on behalf of the author. Done in 16ed324, with one difference: classic_pipeline_connects_autoplug_query builds the source with create_decode_pipeline(.., Decoder::Classic) and checks that autoplug-query has a handler. It does not emit the signal, because an emitted query is shared and the handler now declines those. It fails with the call at line 152 removed (checked), and has no handler on Decoder::Decodebin3. The PR body now says the two-track GL case is covered by the macOS run, not the suite.

Skip a shared CAPS query instead of writing into it, correct the comment
on empty answers, allow D3D12 and D3D11 memory on the clip appsink so
Windows decoders keep their memory type (consumers download it), and
guard the autoplug-query wiring with a test that fails without it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@wagenet

wagenet commented Oct 21, 2026

Copy link
Copy Markdown
Contributor Author

Posted by Claude on behalf of the author.

Addressed in 16ed324:

The gpu_nv12 CI failure is #1092.

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Area: Media Player bridge, GL memory crossing pipelines

Since a8793d1: D3D12/D3D11 now pass the clip appsink (bridge.rs:725-729), which settles the clash with merged #1093. The writability guard, the corrected doc comment, and the wiring test (bridge.rs:1323) answer the last inline threads. As a fix it works; whether it should land as-is depends on its neighbour.

Neighbours: #1099 proposes forwarding the GL context from the player's internal pipeline to the flow, so both sides use the same GL context, and names this PR's appsink pin as the producer-side choice that would then be unnecessary. This PR's "rejected" note covers a shared context, not the same one, so it does not answer #1099. If #1099 lands, the appsink caps and the classic autoplug-query handler should both go, and with them the extra per-frame download and upload on macOS (not measured). Stinger clips keep system memory either way. #1021 and #969 do not overlap.

  1. CI: Check (Linux) fails in vision_mixer_signals_test::punch_in_taken_to_plain_input_reports_morph ("Failed to query pipeline position"), a test that does not touch bridge.rs; it looks unrelated, not verified as a known flake. The new bridge tests ran and passed. macOS and Windows builds were skipped, so the GL and D3D paths have no CI run. I did not run anything.

"memory:CUDAMemory",
"memory:D3D12Memory",
"memory:D3D11Memory",
] {

@srperens srperens Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an allowlist, so every other GPU memory a decoder offers is now refused, not only GL: memory:NVMM, memory:VAMemory, memory:DMABuf, which video_adapt.rs:27-28 treats as memory that consumers really take. A decoder whose src template has no system-memory caps then fails not-negotiated on main-playable files. I believe Jetson's nvv4l2decoder is NVMM-only, but that is not verified. VA memory is tied to a VA display context too, so it may need the same thinking as GL (not verified).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude on behalf of the author. Done in 8e2e7f1. NVMM, VAMemory and DMABuf now pass for clips. Stingers still take system memory only. I did not test a Jetson, VA-API or DMABuf decoder. VAMemory may need the same display-context handling as GL, and I have not checked that.

// The signal passes the query by pointer; a `Value` getter would hand
// back a copy and the answer would be lost.
let query = args[3].get::<&gst::QueryRef>().ok()?;
let query = unsafe { gst::QueryRef::from_mut_ptr(query.as_mut_ptr()) };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

QueryRef::from_mut_ptr has debug_assert_ne!(gst_mini_object_is_writable(..), GFALSE) (gstreamer 0.25.3, src/miniobject.rs:455). So in a debug build a shared query panics here, before the writability guard at line 775, and a panic inside a glib signal closure aborts the process. The test at line 1316 skips the closure, so nothing exercises this order. Checking writability on the raw pointer before converting avoids it. Not verified whether decodebin ever passes a shared query.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude on behalf of the author. Done in 8e2e7f1. The closure now calls handle_autoplug_query, which checks writability before from_mut_ptr. The shared-query test goes through it, and it panics with the check moved after the conversion. I did not find out whether decodebin ever passes a shared query.

A decoder whose only output is NVMM, VA or DMABuf memory failed
not-negotiated against the restricted appsink caps, on files that play
without it. The autoplug-query handler also checks that the query is
writable before converting it, since from_mut_ptr asserts that in debug
builds and a panic in a glib signal closure aborts the process.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@srperens srperens mentioned this pull request Oct 23, 2026

@srperens srperens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Area: Media Player bridge, GL memory crossing pipelines

Since 16ed324, 8e2e7f1 lets NVMM, VA and DMABuf memory through to clips (bridge.rs:731-733); stingers stay system-memory only. The autoplug handler now checks writability before from_mut_ptr (bridge.rs:768-774), and the test at bridge.rs:1338 calls the real handler body. Both earlier inline findings are resolved. The approach still looks right.

Neighbours: #1099 proposes forwarding the GL context across the boundary; if it lands, the appsink caps and the autoplug-query answer become unnecessary. #1086 touches mediaplayer/builder.rs, not bridge.rs.

  1. CI: all four bridge tests ran and passed in Check (Linux). macOS and Windows builds were skipped, so the GL and D3D paths, and now NVMM, VA and DMABuf, have no CI run. I did not run anything.

"memory:D3D12Memory",
"memory:D3D11Memory",
"memory:NVMM",
"memory:VAMemory",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The doc comment justifies these features by "a decoder whose only output is one of them". That holds for NVMM (Jetson), but va*dec also offers DMABuf and system memory and prefers VAMemory when downstream accepts it (not verified on VA hardware). With VAMemory listed here, a VA clip crosses the bridge in VAMemory, and video_adapt::decide() adds nothing in front of a glupload consumer for VA (gst/video_adapt.rs table, "anything else, none"), while glupload takes system, GL and DMABuf only. At 16ed324 the same clip got DMABuf. No worse than main, but either the comment's reasoning or decide() needs a VA case.

@srperens

Copy link
Copy Markdown
Collaborator

@wagenet thanks for this. The report and the macOS repro (the exact glcolorconvert "context is different" error) set off a closer look at the whole problem, and we are going with #1108 instead of this PR.

What we found (details and tables in #1099):

  • The root cause is that two GStreamer pipelines share no context. The Media Player's internal pipeline makes its own GL context, so GL frames from VideoToolbox cannot be used by the flow's GL elements.
  • Forwarding the internal pipeline's NEED_CONTEXT/HAVE_CONTEXT messages to the flow, as webrtcsink does for its own session pipelines, puts both sides in the same GL context. We measured this on macOS, Linux and Windows, in both start orders.
  • With that in place, the decoder can keep emitting GL memory, and the clip reaches the mixer without a GL→CPU→GL round trip per frame. Pinning the appsink to CUDA/system memory is then not needed. It would also be a producer-side choice, which docs/BLOCK_GUIDELINES.md asks us to avoid.

#1108 does this. In a real Strom on macOS, a Media Player with an H.264 clip into a GPU Vision Mixer failed on main with your error, and ran clean on #1108 with GL memory end to end.

The autoplug-query part for the classic decoder's second video track should not be needed either: with the context shared, GL memory is fine on any track. If you still see the two-track case fail on #1108, please tell us there; your two-track repro is the one we did not rerun.

Once #1108 is merged I'll close this one. Thanks again; this report led straight to the fix.

srperens added a commit that referenced this pull request Oct 27, 2026
…line (#1108)

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

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

* fix(media player): forward only GL contexts, and guard through the real 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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@srperens

Copy link
Copy Markdown
Collaborator

#1108 is merged (0845f78). It fixes the macOS Media Player GL context error by sharing the flow's GL context with the internal pipeline, so this PR is no longer needed. Thanks again for the report and the repro. If the classic decoder's two-video-track case still fails on main, please open an issue with the repro.

@srperens srperens closed this Oct 27, 2026
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