Blame tests/examples/gl/sdl/sdlshare2.c

Packit 971217
/*
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2015 Julien Isorce <julien.isorce@gmail.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#ifdef WIN32
Packit 971217
#include <windows.h>
Packit 971217
#endif
Packit 971217
Packit 971217
#include <GL/gl.h>
Packit 971217
#include "SDL/SDL.h"
Packit 971217
#include "SDL/SDL_opengl.h"
Packit 971217
Packit 971217
#ifndef WIN32
Packit 971217
#include <GL/glx.h>
Packit 971217
#include "SDL/SDL_syswm.h"
Packit 971217
#include <gst/gl/x11/gstgldisplay_x11.h>
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
#include <gst/gl/gl.h>
Packit 971217
Packit 971217
static GstGLContext *sdl_context;
Packit 971217
static GstGLDisplay *sdl_gl_display;
Packit 971217
Packit 971217
/* rotation angle for the triangle. */
Packit 971217
float rtri = 0.0f;
Packit 971217
Packit 971217
/* rotation angle for the quadrilateral. */
Packit 971217
float rquad = 0.0f;
Packit 971217
Packit 971217
/* A general OpenGL initialization function.  Sets all of the initial parameters. */
Packit 971217
static void
Packit 971217
InitGL (int Width, int Height)  // We call this right after our OpenGL window is created.
Packit 971217
{
Packit 971217
  glViewport (0, 0, Width, Height);
Packit 971217
  glClearColor (0.0f, 0.0f, 0.0f, 0.0f);        // This Will Clear The Background Color To Black
Packit 971217
  glClearDepth (1.0);           // Enables Clearing Of The Depth Buffer
Packit 971217
  glDepthFunc (GL_LESS);        // The Type Of Depth Test To Do
Packit 971217
  glEnable (GL_DEPTH_TEST);     // Enables Depth Testing
Packit 971217
  glShadeModel (GL_SMOOTH);     // Enables Smooth Color Shading
Packit 971217
Packit 971217
  glMatrixMode (GL_PROJECTION);
Packit 971217
  glLoadIdentity ();            // Reset The Projection Matrix
Packit 971217
Packit 971217
  glMatrixMode (GL_MODELVIEW);
Packit 971217
}
Packit 971217
Packit 971217
/* The main drawing function. */
Packit 971217
static void
Packit 971217
DrawGLScene (GstVideoFrame * v_frame)
Packit 971217
{
Packit 971217
  guint texture = 0;
Packit 971217
Packit 971217
#ifdef WIN32
Packit 971217
  if (!wglGetCurrentContext ())
Packit 971217
    return;
Packit 971217
#else
Packit 971217
  if (!glXGetCurrentContext ())
Packit 971217
    return;
Packit 971217
#endif
Packit 971217
Packit 971217
  texture = *(guint *) v_frame->data[0];
Packit 971217
Packit 971217
  glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Clear The Screen And The Depth Buffer
Packit 971217
  glLoadIdentity ();            // Reset The View
Packit 971217
Packit 971217
  glTranslatef (-0.4f, 0.0f, 0.0f);     // Move Left 1.5 Units And Into The Screen 6.0
Packit 971217
Packit 971217
  glRotatef (rtri, 0.0f, 1.0f, 0.0f);   // Rotate The Triangle On The Y axis 
Packit 971217
  // draw a triangle (in smooth coloring mode)
Packit 971217
  glBegin (GL_POLYGON);         // start drawing a polygon
Packit 971217
  glColor3f (1.0f, 0.0f, 0.0f); // Set The Color To Red
Packit 971217
  glVertex3f (0.0f, 0.4f, 0.0f);        // Top
Packit 971217
  glColor3f (0.0f, 1.0f, 0.0f); // Set The Color To Green
Packit 971217
  glVertex3f (0.4f, -0.4f, 0.0f);       // Bottom Right
Packit 971217
  glColor3f (0.0f, 0.0f, 1.0f); // Set The Color To Blue
Packit 971217
  glVertex3f (-0.4f, -0.4f, 0.0f);      // Bottom Left  
Packit 971217
  glEnd ();                     // we're done with the polygon (smooth color interpolation)
Packit 971217
Packit 971217
  glEnable (GL_TEXTURE_2D);
Packit 971217
  glBindTexture (GL_TEXTURE_2D, texture);
Packit 971217
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Packit 971217
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Packit 971217
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Packit 971217
  glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Packit 971217
  glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Packit 971217
Packit 971217
  glLoadIdentity ();            // make sure we're no longer rotated.
Packit 971217
  glTranslatef (0.5f, 0.0f, 0.0f);      // Move Right 3 Units, and back into the screen 6.0
Packit 971217
Packit 971217
  glRotatef (rquad, 1.0f, 0.0f, 0.0f);  // Rotate The Quad On The X axis 
Packit 971217
  // draw a square (quadrilateral)
Packit 971217
  glColor3f (0.4f, 0.4f, 1.0f); // set color to a blue shade.
Packit 971217
  glBegin (GL_QUADS);           // start drawing a polygon (4 sided)
Packit 971217
  glTexCoord3f (0.0f, 1.0f, 0.0f);
Packit 971217
  glVertex3f (-0.4f, 0.4f, 0.0f);       // Top Left
Packit 971217
  glTexCoord3f (1.0f, 1.0f, 0.0f);
Packit 971217
  glVertex3f (0.4f, 0.4f, 0.0f);        // Top Right
Packit 971217
  glTexCoord3f (1.0f, 0.0f, 0.0f);
Packit 971217
  glVertex3f (0.4f, -0.4f, 0.0f);       // Bottom Right
Packit 971217
  glTexCoord3f (0.0f, 0.0f, 0.0f);
Packit 971217
  glVertex3f (-0.4f, -0.4f, 0.0f);      // Bottom Left  
Packit 971217
  glEnd ();                     // done with the polygon
Packit 971217
Packit 971217
  glBindTexture (GL_TEXTURE_2D, 0);
Packit 971217
Packit 971217
  rtri += 1.0f;                 // Increase The Rotation Variable For The Triangle
Packit 971217
  rquad -= 1.0f;                // Decrease The Rotation Variable For The Quad 
Packit 971217
Packit 971217
  // swap buffers to display, since we're double buffered.
Packit 971217
  SDL_GL_SwapBuffers ();
Packit 971217
}
Packit 971217
Packit 971217
static GMutex app_lock;
Packit 971217
static GCond app_cond;
Packit 971217
static gboolean app_rendered = FALSE;
Packit 971217
static gboolean app_quit = FALSE;
Packit 971217
Packit 971217
static void
Packit 971217
stop_pipeline (GstElement * pipeline)
Packit 971217
{
Packit 971217
  g_mutex_lock (&app_lock);
Packit 971217
  app_quit = TRUE;
Packit 971217
  g_cond_signal (&app_cond);
Packit 971217
  g_mutex_unlock (&app_lock);
Packit 971217
  gst_element_send_event (pipeline, gst_event_new_eos ());
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
update_sdl_scene (gpointer data)
Packit 971217
{
Packit 971217
  GstElement *pipeline = (GstElement *) data;
Packit 971217
  SDL_Event event;
Packit 971217
Packit 971217
  while (SDL_PollEvent (&event)) {
Packit 971217
    if (event.type == SDL_QUIT) {
Packit 971217
      stop_pipeline (pipeline);
Packit 971217
      return FALSE;
Packit 971217
    }
Packit 971217
    if (event.type == SDL_KEYDOWN) {
Packit 971217
      if (event.key.keysym.sym == SDLK_ESCAPE) {
Packit 971217
        stop_pipeline (pipeline);
Packit 971217
        return FALSE;
Packit 971217
      }
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
executeCallback (gpointer data)
Packit 971217
{
Packit 971217
  g_mutex_lock (&app_lock);
Packit 971217
Packit 971217
  if (!app_quit)
Packit 971217
    DrawGLScene (data);
Packit 971217
Packit 971217
  app_rendered = TRUE;
Packit 971217
  g_cond_signal (&app_cond);
Packit 971217
  g_mutex_unlock (&app_lock);
Packit 971217
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
on_client_draw (GstElement * glsink, GstGLContext * context, GstSample * sample,
Packit 971217
    gpointer data)
Packit 971217
{
Packit 971217
  GstBuffer *buf = gst_sample_get_buffer (sample);
Packit 971217
  GstCaps *caps = gst_sample_get_caps (sample);
Packit 971217
  GstVideoFrame v_frame;
Packit 971217
  GstVideoInfo v_info;
Packit 971217
Packit 971217
  /* FIXME don't do that every frame */
Packit 971217
  gst_video_info_from_caps (&v_info, caps);
Packit 971217
Packit 971217
  if (!gst_video_frame_map (&v_frame, &v_info, buf, GST_MAP_READ | GST_MAP_GL)) {
Packit 971217
    g_warning ("Failed to map the video buffer");
Packit 971217
    return TRUE;
Packit 971217
  }
Packit 971217
Packit 971217
  g_mutex_lock (&app_lock);
Packit 971217
Packit 971217
  app_rendered = FALSE;
Packit 971217
  g_idle_add_full (G_PRIORITY_HIGH, executeCallback, &v_frame, NULL);
Packit 971217
Packit 971217
  while (!app_rendered && !app_quit)
Packit 971217
    g_cond_wait (&app_cond, &app_lock);
Packit 971217
Packit 971217
  g_mutex_unlock (&app_lock);
Packit 971217
Packit 971217
  gst_video_frame_unmap (&v_frame);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/* gst bus signal watch callback */
Packit 971217
static void
Packit 971217
end_stream_cb (GstBus * bus, GstMessage * msg, GMainLoop * loop)
Packit 971217
{
Packit 971217
  switch (GST_MESSAGE_TYPE (msg)) {
Packit 971217
Packit 971217
    case GST_MESSAGE_EOS:
Packit 971217
      g_print ("End-of-stream\n");
Packit 971217
      break;
Packit 971217
Packit 971217
    case GST_MESSAGE_ERROR:
Packit 971217
    {
Packit 971217
      gchar *debug = NULL;
Packit 971217
      GError *err = NULL;
Packit 971217
Packit 971217
      gst_message_parse_error (msg, &err, &debug);
Packit 971217
Packit 971217
      g_print ("Error: %s\n", err->message);
Packit 971217
      g_error_free (err);
Packit 971217
Packit 971217
      if (debug) {
Packit 971217
        g_print ("Debug deails: %s\n", debug);
Packit 971217
        g_free (debug);
Packit 971217
      }
Packit 971217
Packit 971217
      break;
Packit 971217
    }
Packit 971217
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
Packit 971217
  g_main_loop_quit (loop);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
sync_bus_call (GstBus * bus, GstMessage * msg, gpointer data)
Packit 971217
{
Packit 971217
  switch (GST_MESSAGE_TYPE (msg)) {
Packit 971217
    case GST_MESSAGE_NEED_CONTEXT:
Packit 971217
    {
Packit 971217
      const gchar *context_type;
Packit 971217
Packit 971217
      gst_message_parse_context_type (msg, &context_type);
Packit 971217
      g_print ("got need context %s\n", context_type);
Packit 971217
Packit 971217
      if (g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
Packit 971217
        GstContext *display_context =
Packit 971217
            gst_context_new (GST_GL_DISPLAY_CONTEXT_TYPE, TRUE);
Packit 971217
        gst_context_set_gl_display (display_context, sdl_gl_display);
Packit 971217
        gst_element_set_context (GST_ELEMENT (msg->src), display_context);
Packit 971217
        return TRUE;
Packit 971217
      } else if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
Packit 971217
        GstContext *app_context = gst_context_new ("gst.gl.app_context", TRUE);
Packit 971217
        GstStructure *s = gst_context_writable_structure (app_context);
Packit 971217
        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, sdl_context,
Packit 971217
            NULL);
Packit 971217
        gst_element_set_context (GST_ELEMENT (msg->src), app_context);
Packit 971217
        return TRUE;
Packit 971217
      }
Packit 971217
      break;
Packit 971217
    }
Packit 971217
    default:
Packit 971217
      break;
Packit 971217
  }
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
int
Packit 971217
main (int argc, char **argv)
Packit 971217
{
Packit 971217
#ifdef WIN32
Packit 971217
  HGLRC sdl_gl_context = 0;
Packit 971217
  HDC sdl_dc = 0;
Packit 971217
#else
Packit 971217
  SDL_SysWMinfo info;
Packit 971217
  Display *sdl_display = NULL;
Packit 971217
  Window sdl_win = 0;
Packit 971217
  GLXContext sdl_gl_context = NULL;
Packit 971217
#endif
Packit 971217
Packit 971217
  GMainLoop *loop = NULL;
Packit 971217
  GstPipeline *pipeline = NULL;
Packit 971217
  GstBus *bus = NULL;
Packit 971217
  GstElement *glimagesink = NULL;
Packit 971217
  const gchar *platform;
Packit 971217
Packit 971217
  /* Initialize SDL for video output */
Packit 971217
  if (SDL_Init (SDL_INIT_VIDEO) < 0) {
Packit 971217
    fprintf (stderr, "Unable to initialize SDL: %s\n", SDL_GetError ());
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
Packit 971217
  /* Create a 640x480 OpenGL screen */
Packit 971217
  if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) {
Packit 971217
    fprintf (stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError ());
Packit 971217
    SDL_Quit ();
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
Packit 971217
  /* Set the title bar in environments that support it */
Packit 971217
  SDL_WM_SetCaption ("SDL and gst-plugins-gl", NULL);
Packit 971217
Packit 971217
Packit 971217
  /* Loop, drawing and checking events */
Packit 971217
  InitGL (640, 480);
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
  loop = g_main_loop_new (NULL, FALSE);
Packit 971217
Packit 971217
  /* retrieve and turn off sdl opengl context */
Packit 971217
#ifdef WIN32
Packit 971217
  sdl_gl_context = wglGetCurrentContext ();
Packit 971217
  sdl_dc = wglGetCurrentDC ();
Packit 971217
  wglMakeCurrent (0, 0);
Packit 971217
  platform = "wgl";
Packit 971217
  sdl_gl_display = gst_gl_display_new ();
Packit 971217
#else
Packit 971217
  SDL_VERSION (&info.version);
Packit 971217
  SDL_GetWMInfo (&info;;
Packit 971217
  /* FIXME: This display is different to the one that SDL uses to create the
Packit 971217
   * GL context inside SDL_SetVideoMode() above which fails on Intel hardware
Packit 971217
   */
Packit 971217
  sdl_display = info.info.x11.gfxdisplay;
Packit 971217
  sdl_win = info.info.x11.window;
Packit 971217
  sdl_gl_context = glXGetCurrentContext ();
Packit 971217
  glXMakeCurrent (sdl_display, None, 0);
Packit 971217
  platform = "glx";
Packit 971217
  sdl_gl_display =
Packit 971217
      (GstGLDisplay *) gst_gl_display_x11_new_with_display (sdl_display);
Packit 971217
#endif
Packit 971217
Packit 971217
  sdl_context =
Packit 971217
      gst_gl_context_new_wrapped (sdl_gl_display, (guintptr) sdl_gl_context,
Packit 971217
      gst_gl_platform_from_string (platform), GST_GL_API_OPENGL);
Packit 971217
Packit 971217
  pipeline =
Packit 971217
      GST_PIPELINE (gst_parse_launch
Packit 971217
      ("videotestsrc ! video/x-raw, width=320, height=240, framerate=(fraction)30/1 ! "
Packit 971217
          "glimagesink name=glimagesink0", NULL));
Packit 971217
Packit 971217
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 971217
  gst_bus_add_signal_watch (bus);
Packit 971217
  g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb), loop);
Packit 971217
  g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb), loop);
Packit 971217
  g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), loop);
Packit 971217
  gst_bus_enable_sync_message_emission (bus);
Packit 971217
  g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), NULL);
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
  glimagesink = gst_bin_get_by_name (GST_BIN (pipeline), "glimagesink0");
Packit 971217
  g_signal_connect (G_OBJECT (glimagesink), "client-draw",
Packit 971217
      G_CALLBACK (on_client_draw), NULL);
Packit 971217
  gst_object_unref (glimagesink);
Packit 971217
Packit 971217
  /* NULL to PAUSED state pipeline to make sure the gst opengl context is created and
Packit 971217
   * shared with the sdl one */
Packit 971217
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
Packit 971217
Packit 971217
  /* turn on back sdl opengl context */
Packit 971217
#ifdef WIN32
Packit 971217
  wglMakeCurrent (sdl_dc, sdl_gl_context);
Packit 971217
#else
Packit 971217
  glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context);
Packit 971217
#endif
Packit 971217
Packit 971217
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
Packit 971217
Packit 971217
  g_timeout_add (100, update_sdl_scene, pipeline);
Packit 971217
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  /* before to deinitialize the gst-gl-opengl context,
Packit 971217
   * no shared context (here the sdl one) must be current
Packit 971217
   */
Packit 971217
#ifdef WIN32
Packit 971217
  wglMakeCurrent (0, 0);
Packit 971217
#else
Packit 971217
  glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context);
Packit 971217
#endif
Packit 971217
Packit 971217
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
Packit 971217
  gst_object_unref (pipeline);
Packit 971217
Packit 971217
  /* turn on back sdl opengl context */
Packit 971217
#ifdef WIN32
Packit 971217
  wglMakeCurrent (sdl_dc, sdl_gl_context);
Packit 971217
#else
Packit 971217
  glXMakeCurrent (sdl_display, None, 0);
Packit 971217
#endif
Packit 971217
Packit 971217
  SDL_Quit ();
Packit 971217
Packit 971217
  return 0;
Packit 971217
}