Skip to content

GL memory across pipeline boundaries: forward the context in Media Player, download at Inter Input #1099

Closed

Description

@srperens

Description

GL video memory is only usable in the exact GL context that created it. GStreamer shares a context inside one pipeline: the bin caches HAVE_CONTEXT and answers NEED_CONTEXT. Between pipelines it shares nothing unless the application hands the context over. Strom does not hand it over anywhere.

Two Strom boundaries carry decoded video from one pipeline into another:

  • Media Player: an internal decode pipeline → appsink → appsrc in the flow.
  • Inter Output → Inter Input: intersink in flow A → intersrc in flow B.

When GL memory crosses either boundary, the first GL element on the far side that converts it fails:

glcolorconvert0 input memory OpenGL context is different. we have <glcontext…1> memory has <glcontext…0>

The video branch then dies until the flow restarts. The Media Player case on macOS (VideoToolbox decodes to GL memory) is the bug that #1091 reports. The Inter case is reachable today: a GPU Vision Mixer with gl_download=false sends GL memory into Inter Output.

Thesis

  1. Only GL needs a shared context. CUDA, D3D12 and D3D11 memory crossed both boundaries without any context sharing on every host measured.
  2. Forwarding the context fixes GL at a boundary we own. The internal pipeline's bus gets a sync handler that re-posts NEED_CONTEXT and HAVE_CONTEXT from an element in the flow pipeline. The flow's bin then answers from its cache, or caches what the internal pipeline created. Both sides end up in the same GLContext, whichever pipeline starts first. webrtcsink already does this for its own session and discovery pipelines (CustomBusStream in webrtcsink/imp.rs).
  3. Two flows cannot share one context without a process-wide GL context. Flows start and stop independently. A process-wide display would put all GL work of all flows on one GL thread, and a flow that hangs would take every other flow with it. At the Inter boundary the consumer downloads instead: gldownload in flow B reads GL memory from flow A's context without sharing.
  4. WHEP Output's "always download" rule stays. webrtcsink uses one encoder per codec: the highest-ranked one at rank MARGINAL or above. It builds its converter for the input memory in front of that encoder whether or not the encoder takes that memory. Whether the memory works is therefore decided by that one encoder, and on every host measured the download was the right call:
    • macOS: no encoder takes GL memory.
    • Windows: the top encoders (nvh26xenc, nvav1enc) do not take D3D11 memory. whepserversink refuses D3D12 memory at link time. GL memory into nvenc works, but nvenc falls back to a CPU copy (Couldn't get GL context).
    • Linux with NVIDIA: GL memory works for H.264 and H.265, but not for VP9, and Strom offers VP9 to viewers when the input is raw.

Evidence

A standalone probe, tools/gpu-context-probe on branch explore/gpu-context-probe, runs every case in its own process (gpu-context-probe all, gpu-context-probe webrtc). Hosts measured:

  • macOS arm64, GStreamer 1.28.6
  • Linux x86_64 with NVIDIA L4, in the strom-full:0.7.1 image (GStreamer 1.26.6). Host GStreamer 1.24.2 was also measured.
  • Windows 11, GStreamer 1.26.8, NVIDIA RTX 2000 Ada and Intel Arc
Crossing (appsink→appsrc and intersink→intersrc) No sharing Context forwarded
GL, glcolorconvert on the far side (macOS, Linux, Windows) FAIL, context is different OK, same GLContext
GL from a real VideoToolbox decode (macOS) FAIL OK
GL, gldownload on the far side (macOS) OK —
CUDA (Linux, Windows) OK OK
D3D12, D3D11 (Windows) OK OK

Through a real whepserversink, the probe predicted every outcome from the registry alone: "the top encoder for the codec lists the memory". The two exceptions are not about the memory rule. webrtcsink downloads CUDA itself (cudadownload ! videoconvert when cudaconvert is missing). QSV VP9 on Windows is unreliable with any memory type.

Proposed Fix

  1. Media Player: forward NEED_CONTEXT/HAVE_CONTEXT from the internal pipeline's bus to the block's appsrc in the flow.
  2. Inter Input: splice gldownload when the incoming caps carry memory:GLMemory, as a consumer-side rule in video_adapt::decide(). The decision is on caps alone: a buffer arriving at Inter Input always comes from another pipeline's context, so no per-buffer check is needed.
  3. WHEP Output: no change. fix(video adapt): download D3D12 and D3D11 memory for inputs that cannot take it #1093 stands, with its description corrected: the sink refuses D3D12 memory, it does not accept it and then fail.

Rejected:

Test Case

  • Media Player:
    • A test through the real create_decode_pipeline shows that a NEED_CONTEXT from the internal pipeline is answered by the flow's bin.
    • On the CI GL runner, GL memory from the internal pipeline reaches a glcolorconvert in the flow.
    • Both must fail with forwarding removed. The probe reproduces "context is different" with surfaceless EGL on Linux, the environment CI uses.
  • Inter: GL memory into Inter Input reaches a glcolorconvert in flow B. The test must fail without the adaptation.

Out of scope

  • Keeping GL memory into WHEP Output on NVIDIA Linux, for H.264/H.265 only. That would need WHEP Output to offer only codecs whose top encoder takes the memory. It is an optimisation, not a fix.
  • AV1 through whepserversink reported "No codec available" for every memory type on Linux. Not investigated.
  • QSV VP9 instability on Windows (MFX_WRN_INCOMPATIBLE_VIDEO_PARAM, one heap-corruption crash).

Related: #1091 (Media Player GL context on macOS; thanks for the clear report and repro), #1093 (D3D downloads in video_adapt::decide()).

Activity

  1. srperens commented on Oct 21, 2026

    @srperens
    CollaboratorAuthor

    Area: GPU memory across pipeline boundaries (Media Player bridge, Inter Input)

    About: GL frames only work in the GL context that made them, and Strom never passes a context between pipelines. GL memory crossing the Media Player bridge or Inter Output→Inter Input fails at the first GL converter on the far side.

    Relates to: #1091 (same bug, producer-side fix this proposes to make unnecessary), merged #1093 (same video_adapt::decide() rules, D3D side), #1021 and #969 (other consumer-side GL downloads), #1097 (process-wide GL display, seen from CI).

    References: GstContext design

    Thoughts: The code matches: the internal bus has only a signal watch, and Inter Input is a bare intersrc. Stinger clips already pin system memory, for alpha. GL buffers held in an inter channel keep flow A's context alive after A stops; worth a lifecycle test.

  2. srperens commented on Oct 27, 2026

    @srperens
    CollaboratorAuthor

    Open question: WHIP Input's decoders

    WHIP Input runs each publisher in a session pipeline of its own. Only encoded RTP crosses into the flow, and the decoders sit in the flow pipeline: per slot, appsrc → decodebin → videoconvert → tee (slots.rs). So no GPU memory crosses that boundary today, and this issue does not touch WHIP Input.

    It is not settled that the decoders belong there. A slot is not tied to one publisher. The next session in the same slot can be a different publisher with a different codec, resolution or profile. The flow-side decodebin then has to follow a stream change it did not start, which is the kind of change the Media Player keeps inside its own pipeline. The alternative is a decoder inside each session pipeline, with the session forwarding its contexts to the flow as proposed here for the Media Player. A new publisher would then get a new decoder, and the flow would see one continuous raw stream.

    Not decided. It needs its own look: how the flow-side decodebin handles a new publisher in a reused slot today, and what moving it would cost (decoder startup per session, GPU memory crossing per frame). Until then, WHIP Input keeps its decoders where they are.

  3. srperens commented on Oct 27, 2026

    @srperens
    CollaboratorAuthor

    The Media Player half is released in v0.7.2: #1108 (fix(media player): share the flow's GL context with the internal pipeline). It forwards GL context messages from the decode pipeline to the flow, so VideoToolbox frames reach the flow in GL memory without a copy.

    Still open here: Inter Input, which needs to download GL memory that arrives from another flow's context.

    🤖 Generated with Claude Code

  4. added a commit that references this issue on Oct 27, 2026
    0798ef4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions