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);
65 //client reshape callback
66 static gboolean reshapeCallback (void *gl_sink, void *context, GLuint width, GLuint height, gpointer data)
68 glViewport(0, 0, width, height);
69 glMatrixMode(GL_PROJECTION);
71 glMatrixMode(GL_MODELVIEW);
76 //client draw callback
77 static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
79 static GLfloat xrot = 0;
80 static GLfloat yrot = 0;
81 static GLfloat zrot = 0;
82 static GTimeVal current_time;
83 static glong last_sec = current_time.tv_sec;
84 static gint nbFrames = 0;
86 GstVideoFrame v_frame;
89 GstBuffer *buf = gst_sample_get_buffer (sample);
90 GstCaps *caps = gst_sample_get_caps (sample);
92 gst_video_info_from_caps (&v_info, caps);
94 if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
95 g_warning ("Failed to map the video buffer");
99 texture = *(guint *) v_frame.data[0];
101 g_get_current_time (¤t_time);
104 if ((current_time.tv_sec - last_sec) >= 1)
106 std::cout << "GRPHIC FPS = " << nbFrames << std::endl;
108 last_sec = current_time.tv_sec;
111 glEnable(GL_DEPTH_TEST);
113 glEnable (GL_TEXTURE_2D);
114 glBindTexture (GL_TEXTURE_2D, texture);
115 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
116 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
117 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
118 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
119 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 glMatrixMode(GL_MODELVIEW);
125 /* invert the y-axis to get the front face the correct way up */
126 glScalef (0.5f, -0.5f, 0.5f);
128 glRotatef(xrot,1.0f,0.0f,0.0f);
129 glRotatef(yrot,0.0f,1.0f,0.0f);
130 glRotatef(zrot,0.0f,0.0f,1.0f);
134 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
135 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
136 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
137 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
139 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
140 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
141 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
142 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
144 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
145 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
146 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
147 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
149 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
150 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
151 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
152 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
154 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
155 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
156 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
157 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
159 glTexCoord2f(1.0f, 0.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);
165 gst_video_frame_unmap (&v_frame);
171 glDisable (GL_DEPTH_TEST);
172 glDisable (GL_TEXTURE_2D);
178 //gst-launch-1.0 videotestsrc num_buffers=400 ! video/x-raw, width=320, height=240 !
179 //glgraphicmaker ! glfiltercube ! video/x-raw, width=800, height=600 ! glimagesink
180 gint main (gint argc, gchar *argv[])
182 GstStateChangeReturn ret;
183 GstElement *pipeline, *videosrc, *glimagesink;
188 /* FIXME: remove once the example supports gl3 and/or gles2 */
189 g_setenv ("GST_GL_API", "opengl", FALSE);
192 gst_init (&argc, &argv);
193 loop = g_main_loop_new (NULL, FALSE);
195 /* create elements */
196 pipeline = gst_pipeline_new ("pipeline");
198 /* watch for messages on the pipeline's bus (note that this will only
199 * work like this when a GLib main loop is running) */
200 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
201 gst_bus_add_watch (bus, bus_call, loop);
202 gst_object_unref (bus);
204 /* create elements */
205 videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc0");
206 glimagesink = gst_element_factory_make ("glimagesink", "glimagesink0");
209 if (!videosrc || !glimagesink)
211 g_print ("one element could not be found \n");
215 /* change video source caps */
216 GstCaps *caps = gst_caps_new_simple("video/x-raw",
217 "format", G_TYPE_STRING, "RGB",
218 "width", G_TYPE_INT, 320,
219 "height", G_TYPE_INT, 240,
220 "framerate", GST_TYPE_FRACTION, 25, 1,
223 /* configure elements */
224 g_object_set(G_OBJECT(videosrc), "num-buffers", 400, NULL);
225 g_signal_connect(G_OBJECT(glimagesink), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
226 g_signal_connect(G_OBJECT(glimagesink), "client-draw", G_CALLBACK (drawCallback), NULL);
229 gst_bin_add_many (GST_BIN (pipeline), videosrc, glimagesink, NULL);
232 gboolean link_ok = gst_element_link_filtered(videosrc, glimagesink, caps) ;
233 gst_caps_unref(caps) ;
236 g_warning("Failed to link videosrc to glimagesink!\n") ;
241 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
242 if (ret == GST_STATE_CHANGE_FAILURE)
244 g_print ("Failed to start up pipeline!\n");
246 /* check if there is an error message with details on the bus */
247 GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
252 gst_message_parse_error (msg, &err, NULL);
253 g_print ("ERROR: %s\n", err->message);
255 gst_message_unref (msg);
260 g_main_loop_run (loop);
263 gst_element_set_state (pipeline, GST_STATE_NULL);
264 gst_object_unref (pipeline);