bool MediaPlayerPrivateGStreamerBase::ensureGstGLContext() { if (m_glContext) return true; if (!m_glDisplay) { const auto& sharedDisplay = PlatformDisplay::sharedDisplay(); #if PLATFORM(X11) m_glDisplay = GST_GL_DISPLAY(gst_gl_display_x11_new_with_display(downcast<PlatformDisplayX11>(sharedDisplay).native())); #elif PLATFORM(WAYLAND) m_glDisplay = GST_GL_DISPLAY(gst_gl_display_egl_new_with_egl_display(downcast<PlatformDisplayWayland>(sharedDisplay).native())); #endif } GLContext* webkitContext = GLContext::sharingContext(); // EGL and GLX are mutually exclusive, no need for ifdefs here. GstGLPlatform glPlatform = webkitContext->isEGLContext() ? GST_GL_PLATFORM_EGL : GST_GL_PLATFORM_GLX; #if USE(OPENGL_ES_2) GstGLAPI glAPI = GST_GL_API_GLES2; #elif USE(OPENGL) GstGLAPI glAPI = GST_GL_API_OPENGL; #else ASSERT_NOT_REACHED(); #endif PlatformGraphicsContext3D contextHandle = webkitContext->platformContext(); if (!contextHandle) return false; m_glContext = gst_gl_context_new_wrapped(m_glDisplay.get(), reinterpret_cast<guintptr>(contextHandle), glPlatform, glAPI); return true; }
void QGLRenderer::initializeGL () { GstGLContext *context; GstGLDisplay *display; #if GST_GL_HAVE_PLATFORM_GLX display = (GstGLDisplay *) gst_gl_display_x11_new_with_display (QX11Info:: display ()); #else display = gst_gl_display_new (); #endif /* FIXME: Allow the choice at runtime */ #if GST_GL_HAVE_PLATFORM_WGL context = gst_gl_context_new_wrapped (display, (guintptr) wglGetCurrentContext (), GST_GL_PLATFORM_WGL, GST_GL_API_OPENGL); #elif GST_GL_HAVE_PLATFORM_CGL context = gst_gl_context_new_wrapped (display, (guintptr) qt_current_nsopengl_context (), GST_GL_PLATFORM_CGL, GST_GL_API_OPENGL); #elif GST_GL_HAVE_PLATFORM_GLX context = gst_gl_context_new_wrapped (display, (guintptr) glXGetCurrentContext (), GST_GL_PLATFORM_GLX, GST_GL_API_OPENGL); #endif gst_object_unref (display); // We need to unset Qt context before initializing gst-gl plugin. // Otherwise the attempt to share gst-gl context with Qt will fail. this->doneCurrent (); this->gst_thread = new GstThread (display, context, this->videoLoc, SLOT (newFrame ()), this); this->makeCurrent (); QObject::connect (this->gst_thread, SIGNAL (finished ()), this, SLOT (close ())); QObject::connect (this, SIGNAL (closeRequested ()), this->gst_thread, SLOT (stop ()), Qt::QueuedConnection); qglClearColor (QApplication::palette ().color (QPalette::Active, QPalette::Window)); //glShadeModel(GL_FLAT); //glEnable(GL_DEPTH_TEST); //glEnable(GL_CULL_FACE); glEnable (GL_TEXTURE_2D); // Enable Texture Mapping this->gst_thread->start (); }
static void gst_gl_display_context_query (GstElement * element, GstGLDisplay ** display_ptr) { GstContext *ctxt; GstQuery *query; #ifndef GST_DISABLE_GST_DEBUG if (!GST_CAT_CONTEXT) GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT"); #endif query = _gst_context_query (element, display_ptr, GST_GL_DISPLAY_CONTEXT_TYPE); gst_query_parse_context (query, &ctxt); if (ctxt && gst_context_has_context_type (ctxt, GST_GL_DISPLAY_CONTEXT_TYPE)) gst_context_get_gl_display (ctxt, display_ptr); if (*display_ptr) goto out; #if GST_GL_HAVE_WINDOW_X11 gst_query_unref (query); query = _gst_context_query (element, display_ptr, "gst.x11.display.handle"); gst_query_parse_context (query, &ctxt); if (ctxt && gst_context_has_context_type (ctxt, "gst.x11.display.handle")) { const GstStructure *s; Display *display; s = gst_context_get_structure (ctxt); if (gst_structure_get (s, "display", G_TYPE_POINTER, &display, NULL) && display) { *display_ptr = (GstGLDisplay *) gst_gl_display_x11_new_with_display (display); } } if (*display_ptr) goto out; #endif out: gst_query_unref (query); }
int main (int argc, char **argv) { #ifdef WIN32 HGLRC sdl_gl_context = 0; HDC sdl_dc = 0; #else SDL_SysWMinfo info; Display *sdl_display = NULL; Window sdl_win = 0; GLXContext sdl_gl_context = NULL; #endif GMainLoop *loop = NULL; GstPipeline *pipeline = NULL; GstBus *bus = NULL; GstElement *glimagesink = NULL; const gchar *platform; /* Initialize SDL for video output */ if (SDL_Init (SDL_INIT_VIDEO) < 0) { fprintf (stderr, "Unable to initialize SDL: %s\n", SDL_GetError ()); return -1; } /* Create a 640x480 OpenGL screen */ if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) { fprintf (stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError ()); SDL_Quit (); return -1; } /* Set the title bar in environments that support it */ SDL_WM_SetCaption ("SDL and gst-plugins-gl", NULL); /* Loop, drawing and checking events */ InitGL (640, 480); gst_init (&argc, &argv); loop = g_main_loop_new (NULL, FALSE); /* retrieve and turn off sdl opengl context */ #ifdef WIN32 sdl_gl_context = wglGetCurrentContext (); sdl_dc = wglGetCurrentDC (); wglMakeCurrent (0, 0); platform = "wgl"; sdl_gl_display = gst_gl_display_new (); #else SDL_VERSION (&info.version); SDL_GetWMInfo (&info); /* FIXME: This display is different to the one that SDL uses to create the * GL context inside SDL_SetVideoMode() above which fails on Intel hardware */ sdl_display = info.info.x11.gfxdisplay; sdl_win = info.info.x11.window; sdl_gl_context = glXGetCurrentContext (); glXMakeCurrent (sdl_display, None, 0); platform = "glx"; sdl_gl_display = (GstGLDisplay *) gst_gl_display_x11_new_with_display (sdl_display); #endif sdl_context = gst_gl_context_new_wrapped (sdl_gl_display, (guintptr) sdl_gl_context, gst_gl_platform_from_string (platform), GST_GL_API_OPENGL); pipeline = GST_PIPELINE (gst_parse_launch ("videotestsrc ! video/x-raw, width=320, height=240, framerate=(fraction)30/1 ! " "glimagesink name=glimagesink0", NULL)); bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); gst_bus_add_signal_watch (bus); g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb), loop); g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb), loop); g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), loop); gst_bus_enable_sync_message_emission (bus); g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), NULL); gst_object_unref (bus); glimagesink = gst_bin_get_by_name (GST_BIN (pipeline), "glimagesink0"); g_signal_connect (G_OBJECT (glimagesink), "client-draw", G_CALLBACK (on_client_draw), NULL); gst_object_unref (glimagesink); /* NULL to PAUSED state pipeline to make sure the gst opengl context is created and * shared with the sdl one */ gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED); /* turn on back sdl opengl context */ #ifdef WIN32 wglMakeCurrent (sdl_dc, sdl_gl_context); #else glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context); #endif gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING); g_timeout_add (100, update_sdl_scene, pipeline); g_main_loop_run (loop); /* before to deinitialize the gst-gl-opengl context, * no shared context (here the sdl one) must be current */ #ifdef WIN32 wglMakeCurrent (0, 0); #else glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context); #endif gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL); gst_object_unref (pipeline); /* turn on back sdl opengl context */ #ifdef WIN32 wglMakeCurrent (sdl_dc, sdl_gl_context); #else glXMakeCurrent (sdl_display, None, 0); #endif SDL_Quit (); return 0; }
int main (int argc, char **argv) { #ifdef WIN32 HGLRC sdl_gl_context = 0; HDC sdl_dc = 0; #else SDL_SysWMinfo info; Display *sdl_display = NULL; Window sdl_win = 0; GLXContext sdl_gl_context = NULL; #endif GMainLoop *loop = NULL; GstPipeline *pipeline = NULL; GstBus *bus = NULL; GstElement *fakesink = NULL; GstState state; GAsyncQueue *queue_input_buf = NULL; GAsyncQueue *queue_output_buf = NULL; const gchar *platform; /* Initialize SDL for video output */ if (SDL_Init (SDL_INIT_VIDEO) < 0) { fprintf (stderr, "Unable to initialize SDL: %s\n", SDL_GetError ()); return -1; } /* Create a 640x480 OpenGL screen */ if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) { fprintf (stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError ()); SDL_Quit (); return -1; } /* Set the title bar in environments that support it */ SDL_WM_SetCaption ("SDL and gst-plugins-gl", NULL); /* Loop, drawing and checking events */ InitGL (640, 480); gst_init (&argc, &argv); loop = g_main_loop_new (NULL, FALSE); /* retrieve and turn off sdl opengl context */ #ifdef WIN32 sdl_gl_context = wglGetCurrentContext (); sdl_dc = wglGetCurrentDC (); wglMakeCurrent (0, 0); platform = "wgl"; sdl_gl_display = gst_gl_display_new (); #else SDL_VERSION (&info.version); SDL_GetWMInfo (&info); /* FIXME: This display is different to the one that SDL uses to create the * GL context inside SDL_SetVideoMode() above which fails on Intel hardware */ sdl_display = info.info.x11.gfxdisplay; sdl_win = info.info.x11.window; sdl_gl_context = glXGetCurrentContext (); glXMakeCurrent (sdl_display, None, 0); platform = "glx"; sdl_gl_display = (GstGLDisplay *) gst_gl_display_x11_new_with_display (sdl_display); #endif sdl_context = gst_gl_context_new_wrapped (sdl_gl_display, (guintptr) sdl_gl_context, gst_gl_platform_from_string (platform), GST_GL_API_OPENGL); pipeline = GST_PIPELINE (gst_parse_launch ("videotestsrc ! video/x-raw, width=320, height=240, framerate=(fraction)30/1 ! " "gleffects effect=5 ! fakesink sync=1", NULL)); bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); gst_bus_add_signal_watch (bus); g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb), loop); g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb), loop); g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), loop); gst_bus_enable_sync_message_emission (bus); g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), NULL); gst_object_unref (bus); /* NULL to PAUSED state pipeline to make sure the gst opengl context is created and * shared with the sdl one */ gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED); state = GST_STATE_PAUSED; if (gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, GST_CLOCK_TIME_NONE) != GST_STATE_CHANGE_SUCCESS) { g_debug ("failed to pause pipeline\n"); return -1; } /* turn on back sdl opengl context */ #ifdef WIN32 wglMakeCurrent (sdl_dc, sdl_gl_context); #else glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context); #endif /* append a gst-gl texture to this queue when you do not need it no more */ fakesink = gst_bin_get_by_name (GST_BIN (pipeline), "fakesink0"); g_object_set (G_OBJECT (fakesink), "signal-handoffs", TRUE, NULL); g_signal_connect (fakesink, "handoff", G_CALLBACK (on_gst_buffer), NULL); queue_input_buf = g_async_queue_new (); queue_output_buf = g_async_queue_new (); g_object_set_data (G_OBJECT (fakesink), "queue_input_buf", queue_input_buf); g_object_set_data (G_OBJECT (fakesink), "queue_output_buf", queue_output_buf); g_object_set_data (G_OBJECT (fakesink), "loop", loop); gst_object_unref (fakesink); gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING); g_main_loop_run (loop); /* before to deinitialize the gst-gl-opengl context, * no shared context (here the sdl one) must be current */ #ifdef WIN32 wglMakeCurrent (0, 0); #else glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context); #endif gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL); gst_object_unref (pipeline); /* turn on back sdl opengl context */ #ifdef WIN32 wglMakeCurrent (sdl_dc, sdl_gl_context); #else glXMakeCurrent (sdl_display, None, 0); #endif SDL_Quit (); /* make sure there is no pending gst gl buffer in the communication queues * between sdl and gst-gl */ while (g_async_queue_length (queue_input_buf) > 0) { GstBuffer *buf = (GstBuffer *) g_async_queue_pop (queue_input_buf); gst_buffer_unref (buf); } while (g_async_queue_length (queue_output_buf) > 0) { GstBuffer *buf = (GstBuffer *) g_async_queue_pop (queue_output_buf); gst_buffer_unref (buf); } return 0; }
/** * gst_gl_handle_set_context: * @element: a #GstElement * @context: a #GstContext * @display: (inout) (transfer full): location of a #GstGLDisplay * @other_context: (inout) (transfer full): location of a #GstGLContext * * Helper function for implementing GstElement::set_context() in OpenGL capable * elements. * * Retrieve's the #GstGLDisplay or #GstGLContext in @context and places the * result in @display or @other_context respectively. * * Returns: whether the @display or @other_context could be set successfully */ gboolean gst_gl_handle_set_context (GstElement * element, GstContext * context, GstGLDisplay ** display, GstGLContext ** other_context) { GstGLDisplay *display_replacement = NULL; GstGLContext *context_replacement = NULL; const gchar *context_type; g_return_val_if_fail (display != NULL, FALSE); g_return_val_if_fail (other_context != NULL, FALSE); if (!context) return FALSE; context_type = gst_context_get_context_type (context); if (g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) { if (!gst_context_get_gl_display (context, &display_replacement)) { GST_WARNING_OBJECT (element, "Failed to get display from context"); return FALSE; } } #if GST_GL_HAVE_WINDOW_X11 else if (g_strcmp0 (context_type, "gst.x11.display.handle") == 0) { const GstStructure *s; Display *display; s = gst_context_get_structure (context); if (gst_structure_get (s, "display", G_TYPE_POINTER, &display, NULL)) display_replacement = (GstGLDisplay *) gst_gl_display_x11_new_with_display (display); } #endif #if GST_GL_HAVE_WINDOW_WAYLAND else if (g_strcmp0 (context_type, "GstWaylandDisplayHandleContextType") == 0) { const GstStructure *s; struct wl_display *display; s = gst_context_get_structure (context); if (gst_structure_get (s, "display", G_TYPE_POINTER, &display, NULL)) display_replacement = (GstGLDisplay *) gst_gl_display_wayland_new_with_display (display); } #endif else if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) { const GstStructure *s = gst_context_get_structure (context); GstGLDisplay *context_display; GstGLDisplay *element_display; if (gst_structure_get (s, "context", GST_TYPE_GL_CONTEXT, &context_replacement, NULL)) { context_display = gst_gl_context_get_display (context_replacement); element_display = display_replacement ? display_replacement : *display; if (element_display && (gst_gl_display_get_handle_type (element_display) & gst_gl_display_get_handle_type (context_display)) == 0) { GST_ELEMENT_WARNING (element, LIBRARY, SETTINGS, ("%s", "Cannot set a GL context with a different display type"), ("%s", "Cannot set a GL context with a different display type")); gst_object_unref (context_replacement); context_replacement = NULL; } gst_object_unref (context_display); } } if (display_replacement) { GstGLDisplay *old = *display; *display = display_replacement; if (old) gst_object_unref (old); } if (context_replacement) { GstGLContext *old = *other_context; *other_context = context_replacement; if (old) gst_object_unref (old); } return TRUE; }
bool ofGstVideoUtils::setPipeline(string pipeline, ofPixelFormat pixelFormat, bool isStream, int w, int h){ internalPixelFormat = pixelFormat; #ifndef OF_USE_GST_GL string caps; #if GST_VERSION_MAJOR==0 switch(pixelFormat){ case OF_PIXELS_MONO: caps="video/x-raw-gray, depth=8, bpp=8"; break; case OF_PIXELS_RGBA: caps="video/x-raw-rgb, depth=24, bpp=32, endianness=4321, red_mask=0xff0000, green_mask=0x00ff00, blue_mask=0x0000ff, alpha_mask=0x000000ff"; break; case OF_PIXELS_BGRA: caps="video/x-raw-rgb, depth=24, bpp=32, endianness=4321, red_mask=0x0000ff, green_mask=0x00ff00, blue_mask=0xff0000, alpha_mask=0x000000ff"; break; case OF_PIXELS_RGB: default: caps="video/x-raw-rgb, depth=24, bpp=24, endianness=4321, red_mask=0xff0000, green_mask=0x00ff00, blue_mask=0x0000ff, alpha_mask=0x000000ff"; break; } #else if(pixelFormat!=OF_PIXELS_NATIVE){ caps="video/x-raw, format="+getGstFormatName(pixelFormat); }else{ caps = "video/x-raw,format={RGBA,BGRA,RGB,BGR,RGB16,GRAY8,YV12,I420,NV12,NV21,YUY2}"; } #endif if(w!=-1 && h!=-1){ caps+=", width=" + ofToString(w) + ", height=" + ofToString(h); } string pipeline_string = pipeline + " ! appsink name=ofappsink enable-last-sample=0 caps=\"" + caps + "\""; if((w==-1 || h==-1) || pixelFormat==OF_PIXELS_NATIVE || allocate(w,h,pixelFormat)){ return setPipelineWithSink(pipeline_string,"ofappsink",isStream); }else{ return false; } #else string pipeline_string = pipeline + " ! glcolorscale name=gl_filter ! appsink name=ofappsink enable-last-sample=0 caps=\"video/x-raw,format=RGBA\""; bool ret; if((w==-1 || h==-1) || pixelFormat==OF_PIXELS_NATIVE || allocate(w,h,pixelFormat)){ ret = setPipelineWithSink(pipeline_string,"ofappsink",isStream); }else{ ret = false; } auto glfilter = gst_bin_get_by_name(GST_BIN(getPipeline()),"gl_filter"); #if defined(TARGET_LINUX) && !defined(TARGET_OPENGLES) glXMakeCurrent (ofGetX11Display(), None, 0); glDisplay = (GstGLDisplay *)gst_gl_display_x11_new_with_display(ofGetX11Display()); glContext = gst_gl_context_new_wrapped (glDisplay, (guintptr) ofGetGLXContext(), GST_GL_PLATFORM_GLX, GST_GL_API_OPENGL); g_object_set (G_OBJECT (glfilter), "other-context", glContext, NULL); // FIXME: this seems to be the way to add the context in 1.4.5 // // GstBus * bus = gst_pipeline_get_bus (GST_PIPELINE(gstPipeline)); // gst_bus_enable_sync_message_emission (bus); // g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), this); // gst_object_unref(bus); glXMakeCurrent (ofGetX11Display(), ofGetX11Window(), ofGetGLXContext()); #elif defined(TARGET_OPENGLES) cout << "current display " << ofGetEGLDisplay() << endl; eglMakeCurrent (eglGetDisplay(EGL_DEFAULT_DISPLAY), 0,0, 0); glDisplay = (GstGLDisplay *)gst_gl_display_egl_new_with_egl_display(eglGetDisplay(EGL_DEFAULT_DISPLAY)); glContext = gst_gl_context_new_wrapped (glDisplay, (guintptr) ofGetEGLContext(), GST_GL_PLATFORM_GLX, GST_GL_API_OPENGL); g_object_set (G_OBJECT (glfilter), "other-context", glContext, NULL); // FIXME: this seems to be the way to add the context in 1.4.5 // // GstBus * bus = gst_pipeline_get_bus (GST_PIPELINE(gstPipeline)); // gst_bus_enable_sync_message_emission (bus); // g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), this); // gst_object_unref(bus); eglMakeCurrent (ofGetEGLDisplay(), ofGetEGLSurface(), ofGetEGLSurface(), ofGetEGLContext()); #endif return ret; #endif }