cc1bb51a5da510f622531a2027b6d23f88a6688b
[platform/upstream/gstreamer.git] / tests / examples / gl / generic / cubeyuv / main.cpp
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 #include <iostream>
26 #include <sstream>
27 #include <string>
28
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
66 //display video framerate
67 static void identityCallback (GstElement *src, GstBuffer  *buffer, GstElement* textoverlay)
68 {
69     static GstClockTime last_timestamp = 0;
70     static gint nbFrames = 0 ;
71
72     //display estimated video FPS
73     nbFrames++ ;
74     if (GST_BUFFER_TIMESTAMP(buffer) - last_timestamp >= 1000000000)
75     {
76         gchar *s = g_strdup_printf ("video framerate = %d", nbFrames);
77         g_object_set(G_OBJECT(textoverlay), "text", s, NULL);
78         g_free (s);
79         last_timestamp = GST_BUFFER_TIMESTAMP(buffer) ;
80         nbFrames = 0 ;
81     }
82 }
83
84
85 //client reshape callback
86 static gboolean reshapeCallback (void * gl_sink, void *context, GLuint width, GLuint height, gpointer data)
87 {
88     glViewport(0, 0, width, height);
89     glMatrixMode(GL_PROJECTION);
90     glLoadIdentity();
91     glMatrixMode(GL_MODELVIEW);
92
93     return TRUE;
94 }
95
96
97 //client draw callback
98 static gboolean drawCallback (GstElement * gl_sink, GstGLContext *context, GstSample * sample, gpointer data)
99 {
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;
106
107     GstVideoFrame v_frame;
108     GstVideoInfo v_info;
109     guint texture = 0;
110     GstBuffer *buf = gst_sample_get_buffer (sample);
111     GstCaps *caps = gst_sample_get_caps (sample);
112
113     gst_video_info_from_caps (&v_info, caps);
114
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");
117       return TRUE;
118     }
119
120     texture = *(guint *) v_frame.data[0];
121
122     g_get_current_time (&current_time);
123     nbFrames++ ;
124
125     if ((current_time.tv_sec - last_sec) >= 1)
126     {
127         std::cout << "GRPHIC FPS = " << nbFrames << std::endl;
128         nbFrames = 0;
129         last_sec = current_time.tv_sec;
130     }
131
132     glEnable(GL_DEPTH_TEST);
133
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);
141
142     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
143     glMatrixMode(GL_MODELVIEW);
144     glLoadIdentity();
145
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);
149
150     /* invert the y-axis to get the front face the correct way up */
151     glScalef (0.5f, -0.5f, 0.5f);
152
153     glBegin(GL_QUADS);
154               // Front Face
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);
159               // Back Face
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);
164               // Top Face
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);
169               // Bottom Face
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);
174               // Right face
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);
179               // Left Face
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);
184     glEnd();
185
186     glDisable(GL_DEPTH_TEST);
187
188     gst_video_frame_unmap (&v_frame);
189
190         xrot+=0.03f;
191     yrot+=0.02f;
192     zrot+=0.04f;
193
194     return TRUE;
195 }
196
197
198 static void cb_new_pad (GstElement* decodebin, GstPad* pad, GstElement* identity)
199 {
200     GstPad* identity_pad = gst_element_get_static_pad (identity, "sink");
201
202     //only link once
203     if (GST_PAD_IS_LINKED (identity_pad))
204     {
205         gst_object_unref (identity_pad);
206         return;
207     }
208
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"))
212     {
213         gst_caps_unref (caps);
214         gst_object_unref (identity_pad);
215         return;
216     }
217     gst_caps_unref (caps);
218
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");
222 }
223
224
225 gint main (gint argc, gchar *argv[])
226 {
227     if (argc != 2)
228     {
229         g_warning ("usage: cubeyuv.exe videolocation\n");
230         return -1;
231     }
232
233     /* FIXME: remove once the example supports gl3 and/or gles2 */
234     g_setenv ("GST_GL_API", "opengl", FALSE);
235
236     std::string video_location(argv[1]);
237
238     /* initialization */
239     gst_init (&argc, &argv);
240     GMainLoop* loop = g_main_loop_new (NULL, FALSE);
241
242     /* create elements */
243     GstElement* pipeline = gst_pipeline_new ("pipeline");
244
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);
250
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");
257
258
259     if (!videosrc || !decodebin || !identity || !textoverlay || !glimagesink)
260     {
261         g_print ("one element could not be found \n");
262         return -1;
263     }
264
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);
272
273     /* add elements */
274     gst_bin_add_many (GST_BIN (pipeline), videosrc, decodebin, identity,
275         textoverlay, glimagesink, NULL);
276
277     /* link elements */
278         gst_element_link_pads (videosrc, "src", decodebin, "sink");
279
280     g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad), identity);
281
282     if (!gst_element_link_pads(identity, "src", textoverlay, "video_sink"))
283     {
284         g_print ("Failed to link identity to textoverlay!\n");
285         return -1;
286     }
287
288     gboolean link_ok = gst_element_link (textoverlay, glimagesink);
289     if(!link_ok)
290     {
291         g_warning("Failed to link textoverlay to glimagesink!\n") ;
292         return -1 ;
293     }
294
295     /* run */
296     GstStateChangeReturn ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
297     if (ret == GST_STATE_CHANGE_FAILURE)
298     {
299         g_print ("Failed to start up pipeline!\n");
300
301         /* check if there is an error message with details on the bus */
302         GstMessage* msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
303         if (msg)
304         {
305           GError *err = NULL;
306
307           gst_message_parse_error (msg, &err, NULL);
308           g_print ("ERROR: %s\n", err->message);
309           g_error_free (err);
310           gst_message_unref (msg);
311         }
312         return -1;
313     }
314
315     g_main_loop_run (loop);
316
317     /* clean up */
318     gst_element_set_state (pipeline, GST_STATE_NULL);
319     gst_object_unref (pipeline);
320
321     return 0;
322 }