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 deails: %s\n", debug);
55 g_main_loop_quit (loop);
66 //display video framerate
67 static void identityCallback (GstElement *src, GstBuffer *buffer, GstElement* textoverlay)
69 static GstClockTime last_timestamp = 0;
70 static gint nbFrames = 0 ;
72 //display estimated video FPS
74 if (GST_BUFFER_TIMESTAMP(buffer) - 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(buffer) ;
85 //client reshape callback
86 static gboolean reshapeCallback (void * gl_sink, void *context, GLuint width, GLuint height, gpointer data)
88 glViewport(0, 0, width, height);
89 glMatrixMode(GL_PROJECTION);
91 glMatrixMode(GL_MODELVIEW);
97 //client draw callback
98 static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
100 static GLfloat xrot = 0;
101 static GLfloat yrot = 0;
102 static GLfloat zrot = 0;
103 static GTimeVal current_time;
104 static glong last_sec = current_time.tv_sec;
105 static gint nbFrames = 0;
107 GstVideoFrame v_frame;
110 GstBuffer *buf = gst_sample_get_buffer (sample);
111 GstCaps *caps = gst_sample_get_caps (sample);
113 gst_video_info_from_caps (&v_info, caps);
115 if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
116 g_warning ("Failed to map the video buffer");
120 texture = *(guint *) v_frame.data[0];
122 g_get_current_time (¤t_time);
125 if ((current_time.tv_sec - last_sec) >= 1)
127 std::cout << "GRPHIC FPS = " << nbFrames << std::endl;
129 last_sec = current_time.tv_sec;
132 glEnable(GL_DEPTH_TEST);
134 glEnable (GL_TEXTURE_2D);
135 glBindTexture (GL_TEXTURE_2D, texture);
136 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
137 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
138 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
139 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
140 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
142 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143 glMatrixMode(GL_MODELVIEW);
146 glRotatef(xrot,1.0f,0.0f,0.0f);
147 glRotatef(yrot,0.0f,1.0f,0.0f);
148 glRotatef(zrot,0.0f,0.0f,1.0f);
150 /* invert the y-axis to get the front face the correct way up */
151 glScalef (0.5f, -0.5f, 0.5f);
155 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
156 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
157 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
158 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
160 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
161 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
162 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
163 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
165 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
166 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
167 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
168 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
170 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
171 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
172 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
173 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
175 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
176 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
177 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
178 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
180 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
181 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
182 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
183 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
186 glDisable(GL_DEPTH_TEST);
188 gst_video_frame_unmap (&v_frame);
198 static void cb_new_pad (GstElement* decodebin, GstPad* pad, GstElement* identity)
200 GstPad* identity_pad = gst_element_get_static_pad (identity, "sink");
203 if (GST_PAD_IS_LINKED (identity_pad))
205 gst_object_unref (identity_pad);
209 GstCaps* caps = gst_pad_get_current_caps (pad);
210 GstStructure* str = gst_caps_get_structure (caps, 0);
211 if (!g_strrstr (gst_structure_get_name (str), "video"))
213 gst_caps_unref (caps);
214 gst_object_unref (identity_pad);
217 gst_caps_unref (caps);
219 GstPadLinkReturn ret = gst_pad_link (pad, identity_pad);
220 if (ret != GST_PAD_LINK_OK)
221 g_warning ("Failed to link with decodebin!\n");
225 gint main (gint argc, gchar *argv[])
229 g_warning ("usage: cubeyuv.exe videolocation\n");
233 /* FIXME: remove once the example supports gl3 and/or gles2 */
234 g_setenv ("GST_GL_API", "opengl", FALSE);
236 std::string video_location(argv[1]);
239 gst_init (&argc, &argv);
240 GMainLoop* loop = g_main_loop_new (NULL, FALSE);
242 /* create elements */
243 GstElement* pipeline = gst_pipeline_new ("pipeline");
245 /* watch for messages on the pipeline's bus (note that this will only
246 * work like this when a GLib main loop is running) */
247 GstBus* bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
248 gst_bus_add_watch (bus, bus_call, loop);
249 gst_object_unref (bus);
251 /* create elements */
252 GstElement* videosrc = gst_element_factory_make ("filesrc", "filesrc0");
253 GstElement* decodebin = gst_element_factory_make ("decodebin", "decodebin");
254 GstElement* identity = gst_element_factory_make ("identity", "identity0");
255 GstElement* textoverlay = gst_element_factory_make ("textoverlay", "textoverlay0");
256 GstElement* glimagesink = gst_element_factory_make ("glimagesink", "glimagesink0");
259 if (!videosrc || !decodebin || !identity || !textoverlay || !glimagesink)
261 g_print ("one element could not be found \n");
265 /* configure elements */
266 g_object_set(G_OBJECT(videosrc), "num-buffers", 800, NULL);
267 g_object_set(G_OBJECT(videosrc), "location", video_location.c_str(), NULL);
268 g_signal_connect(identity, "handoff", G_CALLBACK(identityCallback), textoverlay) ;
269 g_object_set(G_OBJECT(textoverlay), "font_desc", "Ahafoni CLM Bold 30", NULL);
270 g_signal_connect(G_OBJECT(glimagesink), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
271 g_signal_connect(G_OBJECT(glimagesink), "client-draw", G_CALLBACK (drawCallback), NULL);
274 gst_bin_add_many (GST_BIN (pipeline), videosrc, decodebin, identity,
275 textoverlay, glimagesink, NULL);
278 gst_element_link_pads (videosrc, "src", decodebin, "sink");
280 g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad), identity);
282 if (!gst_element_link_pads(identity, "src", textoverlay, "video_sink"))
284 g_print ("Failed to link identity to textoverlay!\n");
288 gboolean link_ok = gst_element_link (textoverlay, glimagesink);
291 g_warning("Failed to link textoverlay to glimagesink!\n") ;
296 GstStateChangeReturn ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
297 if (ret == GST_STATE_CHANGE_FAILURE)
299 g_print ("Failed to start up pipeline!\n");
301 /* check if there is an error message with details on the bus */
302 GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
307 gst_message_parse_error (msg, &err, NULL);
308 g_print ("ERROR: %s\n", err->message);
310 gst_message_unref (msg);
315 g_main_loop_run (loop);
318 gst_element_set_state (pipeline, GST_STATE_NULL);
319 gst_object_unref (pipeline);