1 /*
2 * GStreamer
3 * Copyright (C) 2009 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(GstBuffer * buf)69 DrawGLScene (GstBuffer * buf)
70 {
71 GstVideoFrame v_frame;
72 GstVideoInfo v_info;
73 guint texture;
74
75 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, 320, 240);
76
77 if (!gst_video_frame_map (&v_frame, &v_info, buf, GST_MAP_READ | GST_MAP_GL)) {
78 g_warning ("Failed to map the video buffer");
79 return;
80 }
81
82 texture = *(guint *) v_frame.data[0];
83
84 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
85 glLoadIdentity (); // Reset The View
86
87 glTranslatef (-0.4f, 0.0f, 0.0f); // Move Left 1.5 Units And Into The Screen 6.0
88
89 glRotatef (rtri, 0.0f, 1.0f, 0.0f); // Rotate The Triangle On The Y axis
90 // draw a triangle (in smooth coloring mode)
91 glBegin (GL_POLYGON); // start drawing a polygon
92 glColor3f (1.0f, 0.0f, 0.0f); // Set The Color To Red
93 glVertex3f (0.0f, 0.4f, 0.0f); // Top
94 glColor3f (0.0f, 1.0f, 0.0f); // Set The Color To Green
95 glVertex3f (0.4f, -0.4f, 0.0f); // Bottom Right
96 glColor3f (0.0f, 0.0f, 1.0f); // Set The Color To Blue
97 glVertex3f (-0.4f, -0.4f, 0.0f); // Bottom Left
98 glEnd (); // we're done with the polygon (smooth color interpolation)
99
100 glEnable (GL_TEXTURE_2D);
101 glBindTexture (GL_TEXTURE_2D, texture);
102 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
104 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
105 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
106 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
107
108 glLoadIdentity (); // make sure we're no longer rotated.
109 glTranslatef (0.5f, 0.0f, 0.0f); // Move Right 3 Units, and back into the screen 6.0
110
111 glRotatef (rquad, 1.0f, 0.0f, 0.0f); // Rotate The Quad On The X axis
112 // draw a square (quadrilateral)
113 glColor3f (0.4f, 0.4f, 1.0f); // set color to a blue shade.
114 glBegin (GL_QUADS); // start drawing a polygon (4 sided)
115 glTexCoord3f (0.0f, 1.0f, 0.0f);
116 glVertex3f (-0.4f, 0.4f, 0.0f); // Top Left
117 glTexCoord3f (1.0f, 1.0f, 0.0f);
118 glVertex3f (0.4f, 0.4f, 0.0f); // Top Right
119 glTexCoord3f (1.0f, 0.0f, 0.0f);
120 glVertex3f (0.4f, -0.4f, 0.0f); // Bottom Right
121 glTexCoord3f (0.0f, 0.0f, 0.0f);
122 glVertex3f (-0.4f, -0.4f, 0.0f); // Bottom Left
123 glEnd (); // done with the polygon
124
125 glBindTexture (GL_TEXTURE_2D, 0);
126
127 rtri += 1.0f; // Increase The Rotation Variable For The Triangle
128 rquad -= 1.0f; // Decrease The Rotation Variable For The Quad
129
130 // swap buffers to display, since we're double buffered.
131 SDL_GL_SwapBuffers ();
132
133 gst_video_frame_unmap (&v_frame);
134 }
135
136 static gboolean
update_sdl_scene(void * fk)137 update_sdl_scene (void *fk)
138 {
139 GstElement *fakesink = (GstElement *) fk;
140 GMainLoop *loop =
141 (GMainLoop *) g_object_get_data (G_OBJECT (fakesink), "loop");
142 GAsyncQueue *queue_input_buf =
143 (GAsyncQueue *) g_object_get_data (G_OBJECT (fakesink),
144 "queue_input_buf");
145 GAsyncQueue *queue_output_buf =
146 (GAsyncQueue *) g_object_get_data (G_OBJECT (fakesink),
147 "queue_output_buf");
148 GstBuffer *buf = (GstBuffer *) g_async_queue_pop (queue_input_buf);
149
150 SDL_Event event;
151 while (SDL_PollEvent (&event)) {
152 if (event.type == SDL_QUIT) {
153 g_main_loop_quit (loop);
154 }
155 if (event.type == SDL_KEYDOWN) {
156 if (event.key.keysym.sym == SDLK_ESCAPE) {
157 g_main_loop_quit (loop);
158 }
159 }
160 }
161
162 DrawGLScene (buf);
163
164 /* push buffer so it can be unref later */
165 g_async_queue_push (queue_output_buf, buf);
166
167 return FALSE;
168 }
169
170 /* fakesink handoff callback */
171 static void
on_gst_buffer(GstElement * fakesink,GstBuffer * buf,GstPad * pad,gpointer data)172 on_gst_buffer (GstElement * fakesink, GstBuffer * buf, GstPad * pad,
173 gpointer data)
174 {
175 GAsyncQueue *queue_input_buf = NULL;
176 GAsyncQueue *queue_output_buf = NULL;
177
178 /* ref then push buffer to use it in sdl */
179 gst_buffer_ref (buf);
180 queue_input_buf =
181 (GAsyncQueue *) g_object_get_data (G_OBJECT (fakesink),
182 "queue_input_buf");
183 g_async_queue_push (queue_input_buf, buf);
184 if (g_async_queue_length (queue_input_buf) > 3)
185 g_idle_add (update_sdl_scene, (gpointer) fakesink);
186
187 /* pop then unref buffer we have finished to use in sdl */
188 queue_output_buf =
189 (GAsyncQueue *) g_object_get_data (G_OBJECT (fakesink),
190 "queue_output_buf");
191 if (g_async_queue_length (queue_output_buf) > 3) {
192 GstBuffer *buf_old = (GstBuffer *) g_async_queue_pop (queue_output_buf);
193 gst_buffer_unref (buf_old);
194 }
195 }
196
197 /* gst bus signal watch callback */
198 static void
end_stream_cb(GstBus * bus,GstMessage * msg,GMainLoop * loop)199 end_stream_cb (GstBus * bus, GstMessage * msg, GMainLoop * loop)
200 {
201 switch (GST_MESSAGE_TYPE (msg)) {
202
203 case GST_MESSAGE_EOS:
204 g_print ("End-of-stream\n");
205 g_print
206 ("For more information, try to run: GST_DEBUG=gl*:3 ./sdlshare\n");
207 break;
208
209 case GST_MESSAGE_ERROR:
210 {
211 gchar *debug = NULL;
212 GError *err = NULL;
213
214 gst_message_parse_error (msg, &err, &debug);
215
216 g_print ("Error: %s\n", err->message);
217 g_error_free (err);
218
219 if (debug) {
220 g_print ("Debug deails: %s\n", debug);
221 g_free (debug);
222 }
223
224 break;
225 }
226
227 default:
228 break;
229 }
230
231 g_main_loop_quit (loop);
232 }
233
234 static gboolean
sync_bus_call(GstBus * bus,GstMessage * msg,gpointer data)235 sync_bus_call (GstBus * bus, GstMessage * msg, gpointer data)
236 {
237 switch (GST_MESSAGE_TYPE (msg)) {
238 case GST_MESSAGE_NEED_CONTEXT:
239 {
240 const gchar *context_type;
241
242 gst_message_parse_context_type (msg, &context_type);
243 g_print ("got need context %s\n", context_type);
244
245 if (g_strcmp0 (context_type, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
246 GstContext *display_context =
247 gst_context_new (GST_GL_DISPLAY_CONTEXT_TYPE, TRUE);
248 gst_context_set_gl_display (display_context, sdl_gl_display);
249 gst_element_set_context (GST_ELEMENT (msg->src), display_context);
250 return TRUE;
251 } else if (g_strcmp0 (context_type, "gst.gl.app_context") == 0) {
252 GstContext *app_context = gst_context_new ("gst.gl.app_context", TRUE);
253 GstStructure *s = gst_context_writable_structure (app_context);
254 gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, sdl_context,
255 NULL);
256 gst_element_set_context (GST_ELEMENT (msg->src), app_context);
257 return TRUE;
258 }
259 break;
260 }
261 default:
262 break;
263 }
264 return FALSE;
265 }
266
267 int
main(int argc,char ** argv)268 main (int argc, char **argv)
269 {
270
271 #ifdef WIN32
272 HGLRC sdl_gl_context = 0;
273 HDC sdl_dc = 0;
274 #else
275 SDL_SysWMinfo info;
276 Display *sdl_display = NULL;
277 Window sdl_win = 0;
278 GLXContext sdl_gl_context = NULL;
279 #endif
280
281 GMainLoop *loop = NULL;
282 GstPipeline *pipeline = NULL;
283 GstBus *bus = NULL;
284 GstElement *fakesink = NULL;
285 GstState state;
286 GAsyncQueue *queue_input_buf = NULL;
287 GAsyncQueue *queue_output_buf = NULL;
288 const gchar *platform;
289
290 /* Initialize SDL for video output */
291 if (SDL_Init (SDL_INIT_VIDEO) < 0) {
292 fprintf (stderr, "Unable to initialize SDL: %s\n", SDL_GetError ());
293 return -1;
294 }
295
296 /* Create a 640x480 OpenGL screen */
297 if (SDL_SetVideoMode (640, 480, 0, SDL_OPENGL) == NULL) {
298 fprintf (stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError ());
299 SDL_Quit ();
300 return -1;
301 }
302
303 /* Set the title bar in environments that support it */
304 SDL_WM_SetCaption ("SDL and gst-plugins-gl", NULL);
305
306
307 /* Loop, drawing and checking events */
308 InitGL (640, 480);
309
310 gst_init (&argc, &argv);
311 loop = g_main_loop_new (NULL, FALSE);
312
313 /* retrieve and turn off sdl opengl context */
314 #ifdef WIN32
315 sdl_gl_context = wglGetCurrentContext ();
316 sdl_dc = wglGetCurrentDC ();
317 wglMakeCurrent (0, 0);
318 platform = "wgl";
319 sdl_gl_display = gst_gl_display_new ();
320 #else
321 SDL_VERSION (&info.version);
322 SDL_GetWMInfo (&info);
323 /* FIXME: This display is different to the one that SDL uses to create the
324 * GL context inside SDL_SetVideoMode() above which fails on Intel hardware
325 */
326 sdl_display = info.info.x11.gfxdisplay;
327 sdl_win = info.info.x11.window;
328 sdl_gl_context = glXGetCurrentContext ();
329 glXMakeCurrent (sdl_display, None, 0);
330 platform = "glx";
331 sdl_gl_display =
332 (GstGLDisplay *) gst_gl_display_x11_new_with_display (sdl_display);
333 #endif
334
335 sdl_context =
336 gst_gl_context_new_wrapped (sdl_gl_display, (guintptr) sdl_gl_context,
337 gst_gl_platform_from_string (platform), GST_GL_API_OPENGL);
338
339 pipeline =
340 GST_PIPELINE (gst_parse_launch
341 ("videotestsrc ! video/x-raw, width=320, height=240, framerate=(fraction)30/1 ! "
342 "glupload ! gleffects effect=5 ! fakesink sync=1", NULL));
343
344 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
345 gst_bus_add_signal_watch (bus);
346 g_signal_connect (bus, "message::error", G_CALLBACK (end_stream_cb), loop);
347 g_signal_connect (bus, "message::warning", G_CALLBACK (end_stream_cb), loop);
348 g_signal_connect (bus, "message::eos", G_CALLBACK (end_stream_cb), loop);
349 gst_bus_enable_sync_message_emission (bus);
350 g_signal_connect (bus, "sync-message", G_CALLBACK (sync_bus_call), NULL);
351 gst_object_unref (bus);
352
353 /* NULL to PAUSED state pipeline to make sure the gst opengl context is created and
354 * shared with the sdl one */
355 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
356 state = GST_STATE_PAUSED;
357 if (gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL,
358 GST_CLOCK_TIME_NONE) != GST_STATE_CHANGE_SUCCESS) {
359 g_debug ("failed to pause pipeline\n");
360 return -1;
361 }
362
363 /* turn on back sdl opengl context */
364 #ifdef WIN32
365 wglMakeCurrent (sdl_dc, sdl_gl_context);
366 #else
367 glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context);
368 #endif
369
370 /* append a gst-gl texture to this queue when you do not need it no more */
371 fakesink = gst_bin_get_by_name (GST_BIN (pipeline), "fakesink0");
372 g_object_set (G_OBJECT (fakesink), "signal-handoffs", TRUE, NULL);
373 g_signal_connect (fakesink, "handoff", G_CALLBACK (on_gst_buffer), NULL);
374 queue_input_buf = g_async_queue_new ();
375 queue_output_buf = g_async_queue_new ();
376 g_object_set_data (G_OBJECT (fakesink), "queue_input_buf", queue_input_buf);
377 g_object_set_data (G_OBJECT (fakesink), "queue_output_buf", queue_output_buf);
378 g_object_set_data (G_OBJECT (fakesink), "loop", loop);
379 gst_object_unref (fakesink);
380
381 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
382
383 g_main_loop_run (loop);
384
385 /* before to deinitialize the gst-gl-opengl context,
386 * no shared context (here the sdl one) must be current
387 */
388 #ifdef WIN32
389 wglMakeCurrent (0, 0);
390 #else
391 glXMakeCurrent (sdl_display, sdl_win, sdl_gl_context);
392 #endif
393
394 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
395 gst_object_unref (pipeline);
396
397 /* make sure there is no pending gst gl buffer in the communication queues
398 * between sdl and gst-gl
399 */
400 while (g_async_queue_length (queue_input_buf) > 0) {
401 GstBuffer *buf = (GstBuffer *) g_async_queue_pop (queue_input_buf);
402 gst_buffer_unref (buf);
403 }
404
405 while (g_async_queue_length (queue_output_buf) > 0) {
406 GstBuffer *buf = (GstBuffer *) g_async_queue_pop (queue_output_buf);
407 gst_buffer_unref (buf);
408 }
409
410 /* turn on back sdl opengl context */
411 #ifdef WIN32
412 wglMakeCurrent (sdl_dc, sdl_gl_context);
413 #else
414 glXMakeCurrent (sdl_display, None, 0);
415 #endif
416
417 SDL_Quit ();
418
419 return 0;
420 }
421