How to feed OpenGL/GLFW framebuffer/texture to appsrc with video/x-raw(memory:GLMemory) caps?

Hello all,

GStreamer 1.29.2, OpenGL/GLFW 3.3 Core Profile

I have a working OpenGL/GLFW bit of code that essentially renders RGBA to a GL_TEXTURE_2D with a framebuffer, which is in turn rendered onto the default window (created from window = glfwCreateWindow() and the context made current via glfwMakeContextCurrent(window). The code is very similar to what you would find in learnopengl.com.

What I would like to do next is to feed the contents of the framebuffer 2D texture to appsrc with caps video/x-raw(memory:GLMemory),width=W,height=H,etc., ideally to be sink-ed by nvh265enc or similar, at a controllable framerate.

How should I go about doing so?

I have read through the gst-gl docs and my take away is that gst_gl_context_new_wrapped() is needed to pass OpenGL context to GStreamer, but I’m not sure of the GstGLDisplay parameter to pass as I already have an OpenGL window created with my existing code.

Or am I completely off-target, and it is possible to use gst_gl_memory_copy_into() to copy the framebuffer 2D texture into a GstGLMemory, somehow wrap that into a GstBuffer and push it to appsrc?

Two options:

  1. Keep your glfw created window/texture, share it with GStreamer’s OpenGL texture and push GstBuffer’s downstream from appsrc with an appropriate GstGLSyncMeta.
  2. Remove glfw from the equation entirely and use GStreamer’s OpenGL context to render into the relevant GL texture and push that downstream using an appsrc.
  1. is preferrable as you don’t need to share OpenGL context’s (which can be problematic on some platforms/OS).

Regarding option 2, could you share an example (preferably C/C++)?

Retrieve GstGLContext from a downstream OpenGL element through a context property. Call gst_gl_context_thread_add and framebuffer render your texture without glfw. Push the GstGLMemory backed GstBuffer with the appropriate memory:GLMemory caps downstream through an appsrc.