• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/video/videooverlay.h>
22 #include <gst/video/video.h>
23 #include <GL/gl.h>
24 #include "../gl-compat-defines.h"
25 #include "pipeline.h"
26 
27 #define GST_MAP_GL (GST_MAP_FLAG_LAST << 1)
28 
Pipeline(const WId id,const QString videoLocation)29 Pipeline::Pipeline(const WId id, const QString videoLocation):
30     m_winId(id),
31     m_videoLocation(videoLocation),
32     m_loop(NULL),
33     m_bus(NULL),
34     m_pipeline(NULL),
35     m_glimagesink(NULL)
36 {
37     create();
38 }
39 
~Pipeline()40 Pipeline::~Pipeline()
41 {
42 }
43 
create()44 void Pipeline::create()
45 {
46     qDebug("Loading video: %s", m_videoLocation.toLatin1().data());
47 
48     gst_init (NULL, NULL);
49 
50 #ifdef WIN32
51     m_loop = g_main_loop_new (NULL, FALSE);
52 #endif
53     m_pipeline = gst_pipeline_new ("pipeline");
54 
55     m_bus = gst_pipeline_get_bus (GST_PIPELINE (m_pipeline));
56     gst_bus_add_watch (m_bus, (GstBusFunc) bus_call, this);
57     gst_bus_set_sync_handler (m_bus, (GstBusSyncHandler) create_window, this, NULL);
58     gst_object_unref (m_bus);
59 
60     GstElement* videosrc = gst_element_factory_make ("filesrc", "filesrc0");
61     GstElement* decodebin = gst_element_factory_make ("decodebin", "decodebin0");
62     m_glimagesink  = gst_element_factory_make ("glimagesink", "sink0");
63 
64     if (!videosrc || !decodebin || !m_glimagesink )
65     {
66         qDebug ("one element could not be found");
67         return;
68     }
69 
70     g_object_set(G_OBJECT(videosrc), "num-buffers", 800, NULL);
71     g_object_set(G_OBJECT(videosrc), "location", m_videoLocation.toLatin1().data(), NULL);
72     g_signal_connect(G_OBJECT(m_glimagesink), "client-reshape", G_CALLBACK (reshapeCallback), NULL);
73     g_signal_connect(G_OBJECT(m_glimagesink), "client-draw", G_CALLBACK (drawCallback), NULL);
74 
75     gst_bin_add_many (GST_BIN (m_pipeline), videosrc, decodebin, m_glimagesink, NULL);
76 
77     gst_element_link_pads (videosrc, "src", decodebin, "sink");
78 
79     g_signal_connect (decodebin, "pad-added", G_CALLBACK (cb_new_pad), this);
80 }
81 
start()82 void Pipeline::start()
83 {
84     GstStateChangeReturn ret = gst_element_set_state (m_pipeline, GST_STATE_PLAYING);
85     if (ret == GST_STATE_CHANGE_FAILURE)
86     {
87         qDebug ("Failed to start up pipeline!");
88 
89         /* check if there is an error message with details on the bus */
90         GstMessage* msg = gst_bus_poll (m_bus, GST_MESSAGE_ERROR, 0);
91         if (msg)
92         {
93             GError *err = NULL;
94             gst_message_parse_error (msg, &err, NULL);
95             qDebug ("ERROR: %s", err->message);
96             g_error_free (err);
97             gst_message_unref (msg);
98         }
99         return;
100     }
101 
102 #ifdef WIN32
103     g_main_loop_run(m_loop);
104 #endif
105 }
106 
107 //we don't want a thread safe stop in this example
stop()108 void Pipeline::stop()
109 {
110 #ifdef WIN32
111     g_main_loop_quit(m_loop);
112 #else
113     emit stopRequested();
114 #endif
115 }
116 
unconfigure() const117 void Pipeline::unconfigure() const
118 {
119     gst_element_set_state (m_pipeline, GST_STATE_NULL);
120     gst_object_unref (m_pipeline);
121 }
122 
show()123 void Pipeline::show()
124 {
125     emit showRequested();
126 }
127 
128 //redraw the current frame in the drawable
doExpose() const129 void Pipeline::doExpose() const
130 {
131     if (m_pipeline && m_glimagesink)
132         gst_video_overlay_expose (GST_VIDEO_OVERLAY (m_glimagesink));
133 }
134 
135 //post message to g_main_loop in order to call expose
136 //in the gt thread
exposeRequested()137 void Pipeline::exposeRequested()
138 {
139     g_idle_add(cb_expose, this);
140 }
141 
142 //rotate the cube
doRotate()143 void Pipeline::doRotate()
144 {
145     m_xrot += 3.0f;
146     m_yrot += 2.0f;
147     m_zrot += 4.0f;
148 }
149 
150 //post message to g_main_loop in order to call rotate
151 //in the gt thread
rotateRequested()152 void Pipeline::rotateRequested()
153 {
154     g_idle_add(cb_rotate, this);
155 }
156 
157 //-----------------------------------------------------------------------
158 //----------------------------- static members --------------------------
159 //-----------------------------------------------------------------------
160 
161 float Pipeline::m_xrot = 0;
162 float Pipeline::m_yrot = 0;
163 float Pipeline::m_zrot = 0;
164 
165 //client reshape callback
reshapeCallback(void * sink,void * context,guint width,guint height,gpointer data)166 gboolean Pipeline::reshapeCallback (void *sink, void *context, guint width, guint height, gpointer data)
167 {
168     glViewport(0, 0, width, height);
169     glMatrixMode(GL_PROJECTION);
170     glLoadIdentity();
171     glMatrixMode(GL_MODELVIEW);
172 
173     return TRUE;
174 }
175 
176 //client draw callback
drawCallback(void * sink,void * context,GstSample * sample,gpointer data)177 gboolean Pipeline::drawCallback (void * sink, void *context, GstSample * sample, gpointer data)
178 {
179     static gint64 current_time;
180     static glong last_sec = 0;
181     static gint nbFrames = 0;
182 
183     GstVideoFrame v_frame;
184     GstVideoInfo v_info;
185     guint texture = 0;
186     GstBuffer *buf = gst_sample_get_buffer (sample);
187     GstCaps *caps = gst_sample_get_caps (sample);
188 
189     gst_video_info_from_caps (&v_info, caps);
190 
191     if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
192       g_warning ("Failed to map the video buffer");
193       return TRUE;
194     }
195 
196     texture = *(guint *) v_frame.data[0];
197 
198     current_time = g_get_monotonic_time ();
199     nbFrames++ ;
200 
201     if ((current_time / G_USEC_PER_SEC - last_sec) >= 1)
202     {
203         qDebug ("GRAPHIC FPS = %d", nbFrames);
204         nbFrames = 0;
205         last_sec = current_time / G_USEC_PER_SEC;
206     }
207 
208     glEnable(GL_DEPTH_TEST);
209 
210     glEnable (GL_TEXTURE_2D);
211     glBindTexture (GL_TEXTURE_2D, texture);
212     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
213     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
214     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
215     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
216     glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
217 
218     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
219     glMatrixMode(GL_MODELVIEW);
220     glLoadIdentity();
221 
222     glScalef(0.5f,0.5f,0.5f);
223 
224     glRotatef(m_xrot,1.0f,0.0f,0.0f);
225     glRotatef(m_yrot,0.0f,1.0f,0.0f);
226     glRotatef(m_zrot,0.0f,0.0f,1.0f);
227 
228     glBegin(GL_QUADS);
229 	      // Front Face
230 	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
231 	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
232 	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
233 	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
234 	      // Back Face
235 	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
236 	      glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
237 	      glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
238 	      glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
239 	      // Top Face
240 	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
241 	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
242 	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
243 	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
244 	      // Bottom Face
245 	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
246 	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
247 	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
248 	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
249 	      // Right face
250 	      glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
251 	      glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
252 	      glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
253 	      glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
254 	      // Left Face
255 	      glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
256 	      glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
257 	      glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
258 	      glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
259     glEnd();
260 
261     glLoadIdentity();
262     glDisable(GL_DEPTH_TEST);
263     glBindTexture (GL_TEXTURE_2D, 0);
264 
265     gst_video_frame_unmap (&v_frame);
266 
267     return TRUE;
268 }
269 
bus_call(GstBus * bus,GstMessage * msg,Pipeline * p)270 gboolean Pipeline::bus_call (GstBus *bus, GstMessage *msg, Pipeline* p)
271 {
272     switch (GST_MESSAGE_TYPE (msg))
273     {
274         case GST_MESSAGE_EOS:
275             qDebug ("End-of-stream");
276             p->stop();
277             break;
278         case GST_MESSAGE_ERROR:
279         {
280             gchar *debug = NULL;
281             GError *err = NULL;
282             gst_message_parse_error (msg, &err, &debug);
283             qDebug ("Error: %s", err->message);
284             g_error_free (err);
285             if (debug)
286             {
287                 qDebug ("Debug deails: %s", debug);
288                 g_free (debug);
289             }
290             p->stop();
291             break;
292         }
293         default:
294             break;
295     }
296 
297     return TRUE;
298 }
299 
cb_new_pad(GstElement * decodebin,GstPad * pad,Pipeline * p)300 void Pipeline::cb_new_pad (GstElement* decodebin, GstPad* pad, Pipeline* p)
301 {
302     GstElement* glimagesink = p->getVideoSink();
303     GstPad* glpad = gst_element_get_static_pad (glimagesink, "sink");
304 
305     //only link once
306     if (GST_PAD_IS_LINKED (glpad))
307     {
308         gst_object_unref (glpad);
309         return;
310     }
311 
312     GstCaps* caps = gst_pad_get_current_caps (pad);
313     GstStructure* str = gst_caps_get_structure (caps, 0);
314     if (!g_strrstr (gst_structure_get_name (str), "video"))
315     {
316         gst_caps_unref (caps);
317         gst_object_unref (glpad);
318         return;
319     }
320     gst_caps_unref (caps);
321 
322     GstPadLinkReturn ret = gst_pad_link (pad, glpad);
323     if (ret != GST_PAD_LINK_OK)
324         g_warning ("Failed to link with decodebin!\n");
325 
326     p->show();
327 }
328 
cb_expose(gpointer data)329 gboolean Pipeline::cb_expose (gpointer data)
330 {
331     ((Pipeline*)data)->doExpose();
332     return FALSE;
333 }
334 
cb_rotate(gpointer data)335 gboolean Pipeline::cb_rotate (gpointer data)
336 {
337     ((Pipeline*)data)->doRotate();
338     return FALSE;
339 }
340 
create_window(GstBus * bus,GstMessage * message,const Pipeline * p)341 GstBusSyncReply Pipeline::create_window (GstBus* bus, GstMessage* message, const Pipeline* p)
342 {
343     // ignore anything but 'prepare-window-handle' element messages
344     if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
345         return GST_BUS_PASS;
346 
347     if (!gst_is_video_overlay_prepare_window_handle_message (message))
348         return GST_BUS_PASS;
349 
350     qDebug ("setting window handle");
351 
352     //Passing 0 as the window_handle will tell the overlay to stop using that window and create an internal one.
353     //In the directdrawsink's gst_video_overlay_set_window_handle implementation, window_handle (parameter 2) is casted to HWND before it used.
354     gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message)), (guintptr)p->winId());
355 
356     gst_message_unref (message);
357 
358     return GST_BUS_DROP;
359 }
360