1 /*
2 * GStreamer
3 * Copyright (C) 2015 Julien Isorce <julien.isorce@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifdef WIN32
25 #include <windows.h>
26 #endif
27
28 #include <GL/gl.h>
29 #include "SDL/SDL.h"
30 #include "SDL/SDL_opengl.h"
31
32 #ifndef WIN32
33 #include <GL/glx.h>
34 #include "SDL/SDL_syswm.h"
35 #include <gst/gl/x11/gstgldisplay_x11.h>
36 #endif
37
38 #include <gst/gst.h>
39 #include <gst/gl/gl.h>
40
41 static GstGLContext *sdl_context;
42 static GstGLDisplay *sdl_gl_display;
43
44 /* rotation angle for the triangle. */
45 float rtri = 0.0f;
46
47 /* rotation angle for the quadrilateral. */
48 float rquad = 0.0f;
49
50 /* A general OpenGL initialization function. Sets all of the initial parameters. */
51 static void
InitGL(int Width,int Height)52 InitGL (int Width, int Height) // We call this right after our OpenGL window is created.
53 {
54 glViewport (0, 0, Width, Height);
55 glClearColor (0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
56 glClearDepth (1.0); // Enables Clearing Of The Depth Buffer
57 glDepthFunc (GL_LESS); // The Type Of Depth Test To Do
58 glEnable (GL_DEPTH_TEST); // Enables Depth Testing
59 glShadeModel (GL_SMOOTH); // Enables Smooth Color Shading
60
61 glMatrixMode (GL_PROJECTION);
62 glLoadIdentity (); // Reset The Projection Matrix
63
64 glMatrixMode (GL_MODELVIEW);
65 }
66
67 /* The main drawing function. */
68 static void
DrawGLScene(GstVideoFrame * v_frame)69 DrawGLScene (GstVideoFrame * v_frame)
70 {
71 guint texture = 0;
72
73 #ifdef WIN32
74 if (!wglGetCurrentContext ())
75 return;
76 #else
77 if (!glXGetCurrentContext ())
78 return;
79 #endif
80
81 texture = *(guint *) v_frame->data[0];
82
83 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
84 glLoadIdentity (); // Reset The View
85
86 glTranslatef (-0.4f, 0.0f, 0.0f); // Move Left 1.5 Units And Into The Screen 6.0
87
88 glRotatef (rtri, 0.0f, 1.0f, 0.0f); // Rotate The Triangle On The Y axis
89 // draw a triangle (in smooth coloring mode)
90 glBegin (GL_POLYGON); // start drawing a polygon
91 glColor3f (1.0f, 0.0f, 0.0f); // Set The Color To Red
92 glVertex3f (0.0f, 0.4f, 0.0f); // Top
93 glColor3f (0.0f, 1.0f, 0.0f); // Set The Color To Green
94 glVertex3f (0.4f, -0.4f, 0.0f); // Bottom Right
95 glColor3f (0.0f, 0.0f, 1.0f); // Set The Color To Blue
96 glVertex3f (-0.4f, -0.4f, 0.0f); // Bottom Left
97 glEnd (); // we're done with the polygon (smooth color interpolation)
98
99 glEnable (GL_TEXTURE_2D);
100 glBindTexture (GL_TEXTURE_2D, texture);
101 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
102 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
103 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
104 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
105 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
106
107 glLoadIdentity (); // make sure we're no longer rotated.
108 glTranslatef (0.5f, 0.0f, 0.0f); // Move Right 3 Units, and back into the screen 6.0
109
110 glRotatef (rquad, 1.0f, 0.0f, 0.0f); // Rotate The Quad On The X axis
111 // draw a square (quadrilateral)
112 glColor3f (0.4f, 0.4f, 1.0f); // set color to a blue shade.
113 glBegin (GL_QUADS); // start drawing a polygon (4 sided)
114 glTexCoord3f (0.0f, 1.0f, 0.0f);
115 glVertex3f (-0.4f, 0.4f, 0.0f); // Top Left
116 glTexCoord3f (1.0f, 1.0f, 0.0f);
117 glVertex3f (0.4f, 0.4f, 0.0f); // Top Right
118 glTexCoord3f (1.0f, 0.0f, 0.0f);
119 glVertex3f (0.4f, -0.4f, 0.0f); // Bottom Right
120 glTexCoord3f (0.0f, 0.0f, 0.0f);
121 glVertex3f (-0.4f, -0.4f, 0.0f); // Bottom Left
122 glEnd (); // done with the polygon
123
124 glBindTexture (GL_TEXTURE_2D, 0);
125
126 rtri += 1.0f; // Increase The Rotation Variable For The Triangle
127 rquad -= 1.0f; // Decrease The Rotation Variable For The Quad
128
129 // swap buffers to display, since we're double buffered.
130 SDL_GL_SwapBuffers ();
131 }
132
133 static GMutex app_lock;
134 static GCond app_cond;
135 static gboolean app_rendered = FALSE;
136 static gboolean app_quit = FALSE;
137
138 static void
stop_pipeline(GstElement * pipeline)139 stop_pipeline (GstElement * pipeline)
140 {
141 g_mutex_lock (&app_lock);
142 app_quit = TRUE;
143 g_cond_signal (&app_cond);
144 g_mutex_unlock (&app_lock);
145 gst_element_send_event (pipeline, gst_event_new_eos ());
146 }
147
148 static gboolean
update_sdl_scene(gpointer data)149 update_sdl_scene (gpointer data)
150 {
151 GstElement *pipeline = (GstElement *) data;
152 SDL_Event event;
153
154 while (SDL_PollEvent (&event)) {
155 if (event.type == SDL_QUIT) {
156 stop_pipeline (pipeline);
157 return FALSE;
158 }
159 if (event.type == SDL_KEYDOWN) {
160 if (event.key.keysym.sym == SDLK_ESCAPE) {
161 stop_pipeline (pipeline);
162 return FALSE;
163 }
164 }
165 }
166
167 return TRUE;
168 }
169
170 static gboolean
executeCallback(gpointer data)171 executeCallback (gpointer data)
172 {
173 g_mutex_lock (&app_lock);
174
175 if (!app_quit)
176 DrawGLScene (data);
177
178 app_rendered = TRUE;
179 g_cond_signal (&app_cond);
180 g_mutex_unlock (&app_lock);
181
182 return FALSE;
183 }
184
185 static gboolean
on_client_draw(GstElement * glsink,GstGLContext * context,GstSample * sample,gpointer data)186 on_client_draw (GstElement * glsink, GstGLContext * context, GstSample * sample,
187 gpointer data)
188 {
189 GstBuffer *buf = gst_sample_get_buffer (sample);
190 GstCaps *caps = gst_sample_get_caps (sample);
191 GstVideoFrame v_frame;
192 GstVideoInfo v_info;
193
194 /* FIXME don't do that every frame */
195 gst_video_info_from_caps (&v_info, caps);
196
197 if (!gst_video_frame_map (&v_frame, &v_info, buf, GST_MAP_READ | GST_MAP_GL)) {
198 g_warning ("Failed to map the video buffer");
199 return TRUE;
200 }
201
202 g_mutex_lock (&app_lock);
203
204 app_rendered = FALSE;
205 g_idle_add_full (G_PRIORITY_HIGH, executeCallback, &v_frame, NULL);
206
207 while (!app_rendered && !app_quit)
208 g_cond_wait (&app_cond, &app_lock);
209
210 g_mutex_unlock (&app_lock);
211
212 gst_video_frame_unmap (&v_frame);
213
214 return TRUE;
215 }
216
217 /* gst bus signal watch callback */
218 static void
end_stream_cb(GstBus * bus,GstMessage * msg,GMainLoop * loop)219 end_stream_cb (GstBus * bus, GstMessage * msg, GMainLoop * loop)
220 {
221 switch (GST_MESSAGE_TYPE (msg)) {
222
223 case GST_MESSAGE_EOS:
224 g_print ("End-of-stream\n");
225 break;
226
227 case GST_MESSAGE_ERROR:
228 {
229 gchar *debug = NULL;
230 GError *err = NULL;
231
232 gst_message_parse_error (msg, &err, &debug);
233
234 g_print ("Error: %s\n", err->message);
235 g_error_free (err);
236
237 if (debug) {
238 g_print ("Debug deails: %s\n", debug);
239 g_free (debug);
240 }
241
242 break;
243 }
244
245 default:
246 break;
247 }
248
249 g_main_loop_quit (loop);
250 }
251
252 static gboolean
sync_bus_call(GstBus * bus,GstMessage * msg,gpointer data)253 sync_bus_call (GstBus * bus, GstMessage * msg, gpointer data)
254 {
255 switch (GST_MESSAGE_TYPE (msg)) {
256 case GST_MESSAGE_NEED_CONTEXT:
257 {
258 const gchar *context_type;
259
260 gst_message_parse_context_type (msg, &context_type);
261 g_print ("got need context %s\n", context_type);
262
263 if (g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
264 GstContext *display_context =
265 gst_context_new (GST_GL_DISPLAY_CONTEXT_TYPE, TRUE);
266 gst_context_set_gl_display (display_context, sdl_gl_display);
267 gst_element_set_context (GST_ELEMENT (msg->src), display_context);
268 return TRUE;
269 } else if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
270 GstContext *app_context = gst_context_new ("gst.gl.app_context", TRUE);
271 GstStructure *s = gst_context_writable_structure (app_context);
272 gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, sdl_context,
273 NULL);
274 gst_element_set_context (GST_ELEMENT (msg->src), app_context);
275 return TRUE;
276 }
277 break;
278 }
279 default:
280 break;
281 }
282 return FALSE;
283 }
284
285 int
main(int argc,char ** argv)286 main (int argc, char **argv)
287 {
288 #ifdef WIN32
289 HGLRC sdl_gl_context = 0;
290 HDC sdl_dc = 0;
291 #else
292 SDL_SysWMinfo info;
293 Display *sdl_display = NULL;
294 Window sdl_win = 0;
295 GLXContext sdl_gl_context = NULL;
296 #endif
297
298 GMainLoop *loop = NULL;
299 GstPipeline *pipeline = NULL;
300 GstBus *bus = NULL;
301 GstElement *glimagesink = NULL;
302 const gchar *platform;
303
304 /* Initialize SDL for video output */
305 if (SDL_Init (SDL_INIT_VIDEO) < 0) {
306 fprintf (stderr, "Unable to initialize SDL: %s\n", SDL_GetError ());
307 return -1;
308 }
309
310 /* Create a 640x480 OpenGL screen */
311 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) {
312 fprintf (stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError ());
313 SDL_Quit ();
314 return -1;
315 }
316
317 /* Set the title bar in environments that support it */
318 SDL_WM_SetCaption ("SDL and gst-plugins-gl", NULL);
319
320
321 /* Loop, drawing and checking events */
322 InitGL (640, 480);
323
324 gst_init (&argc, &argv);
325 loop = g_main_loop_new (NULL, FALSE);
326
327 /* retrieve and turn off sdl opengl context */
328 #ifdef WIN32
329 sdl_gl_context = wglGetCurrentContext ();
330 sdl_dc = wglGetCurrentDC ();
331 wglMakeCurrent (0, 0);
332 platform = "wgl";
333 sdl_gl_display = gst_gl_display_new ();
334 #else
335 SDL_VERSION (&info.version);
336 SDL_GetWMInfo (&info);
337 /* FIXME: This display is different to the one that SDL uses to create the
338 * GL context inside SDL_SetVideoMode() above which fails on Intel hardware
339 */
340 sdl_display = info.info.x11.gfxdisplay;
341 sdl_win = info.info.x11.window;
342 sdl_gl_context = glXGetCurrentContext ();
343 glXMakeCurrent (sdl_display, None, 0);
344 platform = "glx";
345 sdl_gl_display =
346 (GstGLDisplay *) gst_gl_display_x11_new_with_display (sdl_display);
347 #endif
348
349 sdl_context =
350 gst_gl_context_new_wrapped (sdl_gl_display, (guintptr) sdl_gl_context,
351 gst_gl_platform_from_string (platform), GST_GL_API_OPENGL);
352
353 pipeline =
354 GST_PIPELINE (gst_parse_launch
355 ("videotestsrc ! video/x-raw, width=320, height=240, framerate=(fraction)30/1 ! "
356 "glimagesink name=glimagesink0", NULL));
357
358 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
359 gst_bus_add_signal_watch (bus);
360 g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb), loop);
361 g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb), loop);
362 g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), loop);
363 gst_bus_enable_sync_message_emission (bus);
364 g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), NULL);
365 gst_object_unref (bus);
366
367 glimagesink = gst_bin_get_by_name (GST_BIN (pipeline), "glimagesink0");
368 g_signal_connect (G_OBJECT (glimagesink), "client-draw",
369 G_CALLBACK (on_client_draw), NULL);
370 gst_object_unref (glimagesink);
371
372 /* NULL to PAUSED state pipeline to make sure the gst opengl context is created and
373 * shared with the sdl one */
374 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
375
376 /* turn on back sdl opengl context */
377 #ifdef WIN32
378 wglMakeCurrent (sdl_dc, sdl_gl_context);
379 #else
380 glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context);
381 #endif
382
383 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
384
385 g_timeout_add (100, update_sdl_scene, pipeline);
386
387 g_main_loop_run (loop);
388
389 /* before to deinitialize the gst-gl-opengl context,
390 * no shared context (here the sdl one) must be current
391 */
392 #ifdef WIN32
393 wglMakeCurrent (0, 0);
394 #else
395 glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context);
396 #endif
397
398 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
399 gst_object_unref (pipeline);
400
401 /* turn on back sdl opengl context */
402 #ifdef WIN32
403 wglMakeCurrent (sdl_dc, sdl_gl_context);
404 #else
405 glXMakeCurrent (sdl_display, None, 0);
406 #endif
407
408 SDL_Quit ();
409
410 return 0;
411 }
412