1 /*
2 * GStreamer
3 * Copyright (C) 2008-2009 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
21 #include <gst/gst.h>
22 #include <gst/gl/gl.h>
23 #include <gst/gl/gstglfuncs.h>
24
25
26 #include <iostream>
27 #include <string>
28
bus_call(GstBus * bus,GstMessage * msg,gpointer data)29 static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data)
30 {
31 GMainLoop *loop = (GMainLoop*)data;
32
33 switch (GST_MESSAGE_TYPE (msg))
34 {
35 case GST_MESSAGE_EOS:
36 g_print ("End-of-stream\n");
37 g_main_loop_quit (loop);
38 break;
39 case GST_MESSAGE_ERROR:
40 {
41 gchar *debug = NULL;
42 GError *err = NULL;
43
44 gst_message_parse_error (msg, &err, &debug);
45
46 g_print ("Error: %s\n", err->message);
47 g_error_free (err);
48
49 if (debug)
50 {
51 g_print ("Debug deails: %s\n", debug);
52 g_free (debug);
53 }
54
55 g_main_loop_quit (loop);
56 break;
57 }
58 default:
59 break;
60 }
61
62 return TRUE;
63 }
64
65 //client reshape callback
reshapeCallback(void * gl_sink,void * context,GLuint width,GLuint height,gpointer data)66 static gboolean reshapeCallback (void *gl_sink, void *context, GLuint width, GLuint height, gpointer data)
67 {
68 glViewport(0, 0, width, height);
69 glMatrixMode(GL_PROJECTION);
70 glLoadIdentity();
71 glMatrixMode(GL_MODELVIEW);
72
73 return TRUE;
74 }
75
76 //client draw callback
drawCallback(GstElement * gl_sink,GstGLContext * context,GstSample * sample,gpointer data)77 static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
78 {
79 static GLfloat xrot = 0;
80 static GLfloat yrot = 0;
81 static GLfloat zrot = 0;
82 static GstClockTime current_time;
83 static GstClockTime last_time = gst_util_get_timestamp();
84 static gint nbFrames = 0;
85
86 GstVideoFrame v_frame;
87 GstVideoInfo v_info;
88 guint texture = 0;
89 GstBuffer *buf = gst_sample_get_buffer (sample);
90 GstCaps *caps = gst_sample_get_caps (sample);
91
92 gst_video_info_from_caps (&v_info, caps);
93
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");
96 return TRUE;
97 }
98
99 texture = *(guint *) v_frame.data[0];
100
101 current_time = gst_util_get_timestamp ();
102 nbFrames++ ;
103
104 if ((current_time - last_time) >= GST_SECOND)
105 {
106 std::cout << "GRAPHIC FPS = " << nbFrames << std::endl;
107 nbFrames = 0;
108 last_time = current_time;
109 }
110
111 glEnable(GL_DEPTH_TEST);
112
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);
120
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 glMatrixMode(GL_MODELVIEW);
123 glLoadIdentity();
124
125 /* invert the y-axis to get the front face the correct way up */
126 glScalef (0.5f, -0.5f, 0.5f);
127
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);
131
132 glBegin(GL_QUADS);
133 // Front Face
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);
138 // Back Face
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);
143 // Top Face
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);
148 // Bottom Face
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);
153 // Right face
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);
158 // Left Face
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);
163 glEnd();
164
165 gst_video_frame_unmap (&v_frame);
166
167 xrot+=0.3f;
168 yrot+=0.2f;
169 zrot+=0.4f;
170
171 glDisable (GL_DEPTH_TEST);
172 glDisable (GL_TEXTURE_2D);
173
174 return TRUE;
175 }
176
177
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
main(gint argc,gchar * argv[])180 gint main (gint argc, gchar *argv[])
181 {
182 GstStateChangeReturn ret;
183 GstElement *pipeline, *videosrc, *glimagesink;
184
185 GMainLoop *loop;
186 GstBus *bus;
187
188 /* FIXME: remove once the example supports gl3 and/or gles2 */
189 g_setenv ("GST_GL_API", "opengl", FALSE);
190
191 /* initialization */
192 gst_init (&argc, &argv);
193 loop = g_main_loop_new (NULL, FALSE);
194
195 /* create elements */
196 pipeline = gst_pipeline_new ("pipeline");
197
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);
203
204 /* create elements */
205 videosrc = gst_element_factory_make ("videotestsrc", "videotestsrc0");
206 glimagesink = gst_element_factory_make ("glimagesink", "glimagesink0");
207
208
209 if (!videosrc || !glimagesink)
210 {
211 g_print ("one element could not be found \n");
212 return -1;
213 }
214
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,
221 NULL) ;
222
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);
227
228 /* add elements */
229 gst_bin_add_many (GST_BIN (pipeline), videosrc, glimagesink, NULL);
230
231 /* link elements */
232 gboolean link_ok = gst_element_link_filtered(videosrc, glimagesink, caps) ;
233 gst_caps_unref(caps) ;
234 if(!link_ok)
235 {
236 g_warning("Failed to link videosrc to glimagesink!\n") ;
237 return -1 ;
238 }
239
240 /* run */
241 ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
242 if (ret == GST_STATE_CHANGE_FAILURE)
243 {
244 g_print ("Failed to start up pipeline!\n");
245
246 /* check if there is an error message with details on the bus */
247 GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
248 if (msg)
249 {
250 GError *err = NULL;
251
252 gst_message_parse_error (msg, &err, NULL);
253 g_print ("ERROR: %s\n", err->message);
254 g_error_free (err);
255 gst_message_unref (msg);
256 }
257 return -1;
258 }
259
260 g_main_loop_run (loop);
261
262 /* clean up */
263 gst_element_set_state (pipeline, GST_STATE_NULL);
264 gst_object_unref (pipeline);
265
266 return 0;
267 }
268