3 * Copyright (C) 2008-2009 Julien Isorce <julien.isorce@gmail.com>
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.
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.
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.
22 #include <gst/gl/gl.h>
23 #include <gst/gl/gstglfuncs.h>
29 static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
31 GMainLoop *loop = (GMainLoop*)data;
33 switch (GST_MESSAGE_TYPE (msg))
36 g_print ("End-of-stream\n");
37 g_main_loop_quit (loop);
39 case GST_MESSAGE_ERROR:
44 gst_message_parse_error (msg, &err, &debug);
46 g_print ("Error: %s\n", err->message);
51 g_print ("Debug details: %s\n", debug);
55 g_main_loop_quit (loop);
66 //display video framerate
67 static GstPadProbeReturn textoverlay_sink_pad_probe_cb (GstPad *pad, GstPadProbeInfo *info, GstElement* textoverlay)
69 static GstClockTime last_timestamp = 0;
70 static gint nbFrames = 0 ;
72 //display estimated video FPS
74 if (GST_BUFFER_TIMESTAMP(info->data) - last_timestamp >= 1000000000)
76 gchar *s = g_strdup_printf ("video framerate = %d", nbFrames);
77 g_object_set(G_OBJECT(textoverlay), "text", s, NULL);
79 last_timestamp = GST_BUFFER_TIMESTAMP(info->data) ;
83 return GST_PAD_PROBE_OK;
87 //client reshape callback
88 static gboolean reshapeCallback (void *gl_sink, void *context, GLuint width, GLuint height)
90 glViewport(0, 0, width, height);
91 glMatrixMode(GL_PROJECTION);
93 glMatrixMode(GL_MODELVIEW);
99 //client draw callback
100 static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
102 static GLfloat xrot = 0;
103 static GLfloat yrot = 0;
104 static GLfloat zrot = 0;
105 static GTimeVal current_time;
106 static glong last_sec = current_time.tv_sec;
107 static gint nbFrames = 0;
109 GstVideoFrame v_frame;
112 GstBuffer *buf = gst_sample_get_buffer (sample);
113 GstCaps *caps = gst_sample_get_caps (sample);
115 gst_video_info_from_caps (&v_info, caps);
117 if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
118 g_warning ("Failed to map the video buffer");
122 texture = *(guint *) v_frame.data[0];
124 g_get_current_time (¤t_time);
127 if ((current_time.tv_sec - last_sec) >= 1)
129 std::cout << "GRAPHIC FPS of the scene which contains the custom cube) = " << nbFrames << std::endl;
131 last_sec = current_time.tv_sec;
134 glEnable(GL_DEPTH_TEST);
136 glEnable (GL_TEXTURE_2D);
137 glBindTexture (GL_TEXTURE_2D, texture);
138 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
140 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
141 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
144 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
145 glMatrixMode(GL_MODELVIEW);
148 glTranslatef(0.0f,0.0f,-5.0f);
150 glRotatef(xrot,1.0f,0.0f,0.0f);
151 glRotatef(yrot,0.0f,1.0f,0.0f);
152 glRotatef(zrot,0.0f,0.0f,1.0f);
156 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
157 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
158 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
159 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
161 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
162 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
163 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
164 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
166 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
167 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
168 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
169 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
171 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
172 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
173 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
174 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
176 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
177 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
178 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
179 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
181 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
182 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
183 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
184 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
187 gst_video_frame_unmap (&v_frame);
197 static void cb_new_pad (GstElement* decodebin, GstPad* pad, GstElement* element)
199 GstPad* element_pad = gst_element_get_static_pad (element, "sink");
202 if (!element_pad || GST_PAD_IS_LINKED (element_pad))
204 gst_object_unref (element_pad);
208 GstCaps* caps = gst_pad_get_current_caps (pad);
209 GstStructure* str = gst_caps_get_structure (caps, 0);
211 GstCaps* caps2 = gst_pad_query_caps (element_pad, NULL);
212 gst_caps_unref (caps2);
214 if (!g_strrstr (gst_structure_get_name (str), "video"))
216 gst_caps_unref (caps);
217 gst_object_unref (element_pad);
220 gst_caps_unref (caps);
222 GstPadLinkReturn ret = gst_pad_link (pad, element_pad);
223 if (ret != GST_PAD_LINK_OK)
224 g_warning ("Failed to link with decodebin %d!\n", ret);
225 gst_object_unref (element_pad);
229 gint main (gint argc, gchar *argv[])
233 g_warning ("usage: doublecube.exe videolocation\n");
237 std::string video_location(argv[1]);
240 gst_init (&argc, &argv);
241 GMainLoop* loop = g_main_loop_new (NULL, FALSE);
243 /* create elements */
244 GstElement* pipeline = gst_pipeline_new ("pipeline");
246 /* watch for messages on the pipeline's bus (note that this will only
247 * work like this when a GLib main loop is running) */
248 GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
249 gst_bus_add_watch (bus, bus_call, loop);
250 gst_object_unref (bus);
252 /* create elements */
253 GstElement* videosrc = gst_element_factory_make ("filesrc", "filesrc0");
254 GstElement* decodebin = gst_element_factory_make ("decodebin", "decodebin0");
255 GstElement* videoconvert = gst_element_factory_make ("videoscale", "videoconvert0");
256 GstElement* textoverlay = gst_element_factory_make ("textoverlay", "textoverlay0"); //textoverlay required I420
257 GstElement* tee = gst_element_factory_make ("tee", "tee0");
259 GstElement* queue0 = gst_element_factory_make ("queue", "queue0");
260 GstElement* glimagesink0 = gst_element_factory_make ("glimagesink", "glimagesink0");
262 GstElement* queue1 = gst_element_factory_make ("queue", "queue1");
263 GstElement* glfiltercube = gst_element_factory_make ("glfiltercube", "glfiltercube");
264 GstElement* glimagesink1 = gst_element_factory_make ("glimagesink", "glimagesink1");
266 GstElement* queue2 = gst_element_factory_make ("queue", "queue2");
267 GstElement* glimagesink2 = gst_element_factory_make ("glimagesink", "glimagesink2");
270 if (!videosrc || !decodebin || !videoconvert || !textoverlay || !tee ||
271 !queue0 || !glimagesink0 ||
272 !queue1 || !glfiltercube || !glimagesink1 ||
273 !queue2 || !glimagesink2)
275 g_warning ("one element could not be found \n");
279 GstCaps* cubecaps = gst_caps_new_simple("video/x-raw",
280 "width", G_TYPE_INT, 600,
281 "height", G_TYPE_INT, 400,
284 /* configure elements */
285 g_object_set(G_OBJECT(videosrc), "num-buffers", 1000, NULL);
286 g_object_set(G_OBJECT(videosrc), "location", video_location.c_str(), NULL);
287 g_object_set(G_OBJECT(textoverlay), "font_desc", "Ahafoni CLM Bold 30", NULL);
288 g_signal_connect(G_OBJECT(glimagesink0), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
289 g_signal_connect(G_OBJECT(glimagesink0), "client-draw", G_CALLBACK (drawCallback), NULL);
292 gst_bin_add_many (GST_BIN (pipeline), videosrc, decodebin, videoconvert, textoverlay, tee,
293 queue0, glimagesink0,
294 queue1, glfiltercube, glimagesink1,
295 queue2, glimagesink2, NULL);
297 GstPad* textoverlay_sink_pad = gst_element_get_static_pad (textoverlay, "video_sink");
298 gst_pad_add_probe (textoverlay_sink_pad, GST_PAD_PROBE_TYPE_BUFFER,
299 (GstPadProbeCallback) textoverlay_sink_pad_probe_cb, (gpointer)textoverlay, NULL);
300 gst_object_unref (textoverlay_sink_pad);
302 if (!gst_element_link_many(videoconvert, textoverlay, tee, NULL))
304 g_print ("Failed to link videoconvert to tee!\n");
308 if (!gst_element_link(videosrc, decodebin))
310 g_print ("Failed to link videosrc to decodebin!\n");
314 g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad), videoconvert);
316 if (!gst_element_link_many(tee, queue0, NULL))
318 g_warning ("Failed to link one or more elements bettween tee and queue0!\n");
322 gboolean link_ok = gst_element_link_filtered(queue0, glimagesink0, cubecaps) ;
323 gst_caps_unref(cubecaps) ;
326 g_warning("Failed to link queue0 to glimagesink0!\n") ;
330 if (!gst_element_link_many(tee, queue1, glfiltercube, glimagesink1, NULL))
332 g_warning ("Failed to link one or more elements bettween tee and glimagesink1!\n");
336 if (!gst_element_link_many(tee, queue2, glimagesink2, NULL))
338 g_warning ("Failed to link one or more elements bettween tee and glimagesink2!\n");
343 GstStateChangeReturn ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
344 if (ret == GST_STATE_CHANGE_FAILURE)
346 g_print ("Failed to start up pipeline!\n");
348 /* check if there is an error message with details on the bus */
349 GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
354 gst_message_parse_error (msg, &err, NULL);
355 g_print ("ERROR: %s\n", err->message);
357 gst_message_unref (msg);
362 g_main_loop_run (loop);
365 gst_element_set_state (pipeline, GST_STATE_NULL);
366 gst_object_unref (pipeline);