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>
27 static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
29 GMainLoop *loop = (GMainLoop*)data;
31 switch (GST_MESSAGE_TYPE (msg))
34 g_print ("End-of-stream\n");
35 g_main_loop_quit (loop);
37 case GST_MESSAGE_ERROR:
42 gst_message_parse_error (msg, &err, &debug);
44 g_print ("Error: %s\n", err->message);
49 g_print ("Debug deails: %s\n", debug);
53 g_main_loop_quit (loop);
63 //client reshape callback
64 static gboolean reshapeCallback (void *gl_sink, void *context, GLuint width, GLuint height, gpointer data)
66 glViewport(0, 0, width, height);
67 glMatrixMode(GL_PROJECTION);
69 glMatrixMode(GL_MODELVIEW);
74 //client draw callback
75 static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
77 static GLfloat xrot = 0;
78 static GLfloat yrot = 0;
79 static GLfloat zrot = 0;
80 static GTimeVal current_time;
81 static glong last_sec = current_time.tv_sec;
82 static gint nbFrames = 0;
84 GstVideoFrame v_frame;
87 GstBuffer *buf = gst_sample_get_buffer (sample);
88 GstCaps *caps = gst_sample_get_caps (sample);
90 gst_video_info_from_caps (&v_info, caps);
92 if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
93 g_warning ("Failed to map the video buffer");
97 texture = *(guint *) v_frame.data[0];
99 g_get_current_time (¤t_time);
102 if ((current_time.tv_sec - last_sec) >= 1)
104 std::cout << "GRPHIC FPS = " << nbFrames << std::endl;
106 last_sec = current_time.tv_sec;
109 glEnable(GL_DEPTH_TEST);
111 glEnable (GL_TEXTURE_2D);
112 glBindTexture (GL_TEXTURE_2D, texture);
113 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
114 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
115 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
117 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
119 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120 glMatrixMode(GL_MODELVIEW);
123 glScalef(0.5f,0.5f,0.5f);
125 glRotatef(xrot,1.0f,0.0f,0.0f);
126 glRotatef(yrot,0.0f,1.0f,0.0f);
127 glRotatef(zrot,0.0f,0.0f,1.0f);
131 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
132 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
133 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
134 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
136 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
137 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
138 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
139 glTexCoord2f(1.0f, 0.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);
143 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
144 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
146 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
147 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
148 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
149 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
151 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
152 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
153 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
154 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.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);
162 gst_video_frame_unmap (&v_frame);
168 glDisable (GL_DEPTH_TEST);
169 glDisable (GL_TEXTURE_2D);
175 //gst-launch-1.0 videotestsrc num_buffers=400 ! video/x-raw, width=320, height=240 !
176 //glgraphicmaker ! glfiltercube ! video/x-raw, width=800, height=600 ! glimagesink
177 gint main (gint argc, gchar *argv[])
179 GstStateChangeReturn ret;
180 GstElement *pipeline, *videosrc, *glimagesink;
186 gst_init (&argc, &argv);
187 loop = g_main_loop_new (NULL, FALSE);
189 /* create elements */
190 pipeline = gst_pipeline_new ("pipeline");
192 /* watch for messages on the pipeline's bus (note that this will only
193 * work like this when a GLib main loop is running) */
194 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
195 gst_bus_add_watch (bus, bus_call, loop);
196 gst_object_unref (bus);
198 /* create elements */
199 videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc0");
200 glimagesink = gst_element_factory_make ("glimagesink", "glimagesink0");
203 if (!videosrc || !glimagesink)
205 g_print ("one element could not be found \n");
209 /* change video source caps */
210 GstCaps *caps = gst_caps_new_simple("video/x-raw",
211 "format", G_TYPE_STRING, "RGB",
212 "width", G_TYPE_INT, 320,
213 "height", G_TYPE_INT, 240,
214 "framerate", GST_TYPE_FRACTION, 25, 1,
217 /* configure elements */
218 g_object_set(G_OBJECT(videosrc), "num-buffers", 400, NULL);
219 g_signal_connect(G_OBJECT(glimagesink), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
220 g_signal_connect(G_OBJECT(glimagesink), "client-draw", G_CALLBACK (drawCallback), NULL);
223 gst_bin_add_many (GST_BIN (pipeline), videosrc, glimagesink, NULL);
226 gboolean link_ok = gst_element_link_filtered(videosrc, glimagesink, caps) ;
227 gst_caps_unref(caps) ;
230 g_warning("Failed to link videosrc to glimagesink!\n") ;
235 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
236 if (ret == GST_STATE_CHANGE_FAILURE)
238 g_print ("Failed to start up pipeline!\n");
240 /* check if there is an error message with details on the bus */
241 GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
246 gst_message_parse_error (msg, &err, NULL);
247 g_print ("ERROR: %s\n", err->message);
249 gst_message_unref (msg);
254 g_main_loop_run (loop);
257 gst_element_set_state (pipeline, GST_STATE_NULL);
258 gst_object_unref (pipeline);