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