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_object (G_OBJECT(m_glimagesink), "client-reshape", (GCallback) reshapeCallback, NULL, G_CONNECT_AFTER);
73 g_signal_connect_object (G_OBJECT(m_glimagesink), "client-draw", (GCallback) drawCallback, NULL, G_CONNECT_AFTER);
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
143 //-----------------------------------------------------------------------
144 //----------------------------- static members --------------------------
145 //-----------------------------------------------------------------------
146
147 //client reshape callback
reshapeCallback(GstElement * sink,void * context,guint width,guint height,gpointer data)148 gboolean Pipeline::reshapeCallback (GstElement *sink, void *context, guint width, guint height, gpointer data)
149 {
150 glViewport(0, 0, width, height);
151 glMatrixMode(GL_PROJECTION);
152 glLoadIdentity();
153 glMatrixMode(GL_MODELVIEW);
154
155 return TRUE;
156 }
157
158 //client draw callback
drawCallback(GstElement * gl_sink,void * context,GstSample * sample,gpointer data)159 gboolean Pipeline::drawCallback (GstElement * gl_sink, void *context, GstSample * sample, gpointer data)
160 {
161 static GLfloat xrot = 0;
162 static GLfloat yrot = 0;
163 static GLfloat zrot = 0;
164 static gint64 current_time;
165 static glong last_sec = 0;
166 static gint nbFrames = 0;
167
168 GstVideoFrame v_frame;
169 GstVideoInfo v_info;
170 guint texture = 0;
171 GstBuffer *buf = gst_sample_get_buffer (sample);
172 GstCaps *caps = gst_sample_get_caps (sample);
173
174 gst_video_info_from_caps (&v_info, caps);
175
176 if (!gst_video_frame_map (&v_frame, &v_info, buf, (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
177 g_warning ("Failed to map the video buffer");
178 return TRUE;
179 }
180
181 texture = *(guint *) v_frame.data[0];
182
183 current_time = g_get_monotonic_time ();
184 nbFrames++ ;
185
186 if ((current_time / G_USEC_PER_SEC - last_sec) >= 1)
187 {
188 qDebug ("GRPHIC FPS = %d", nbFrames);
189 nbFrames = 0;
190 last_sec = current_time / G_USEC_PER_SEC;
191 }
192
193 glEnable(GL_DEPTH_TEST);
194
195 glEnable (GL_TEXTURE_2D);
196 glBindTexture (GL_TEXTURE_2D, texture);
197 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
198 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
199 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
200 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
201 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
202
203 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
204 glMatrixMode(GL_PROJECTION);
205 glLoadIdentity();
206
207 glScalef (0.5, 0.5, 0.5);
208
209 glRotatef(xrot,1.0f,0.0f,0.0f);
210 glRotatef(yrot,0.0f,1.0f,0.0f);
211 glRotatef(zrot,0.0f,0.0f,1.0f);
212
213 glBegin(GL_QUADS);
214 // Front Face
215 glTexCoord2f(1.0, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
216 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
217 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
218 glTexCoord2f(1.0, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
219 // Back Face
220 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
221 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
222 glTexCoord2f(1.0, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
223 glTexCoord2f(1.0, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
224 // Top Face
225 glTexCoord2f(1.0, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
226 glTexCoord2f(1.0, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
227 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
228 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
229 // Bottom Face
230 glTexCoord2f(1.0, 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.0,1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
234 // Right 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.0, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
238 glTexCoord2f(1.0, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
239 // Left Face
240 glTexCoord2f(1.0, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
241 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
242 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
243 glTexCoord2f(1.0, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
244 glEnd();
245
246 glLoadIdentity();
247 glDisable(GL_DEPTH_TEST);
248 glBindTexture (GL_TEXTURE_2D, 0);
249
250 gst_video_frame_unmap (&v_frame);
251
252 xrot+=0.03f;
253 yrot+=0.02f;
254 zrot+=0.04f;
255
256 return TRUE;
257 }
258
bus_call(GstBus * bus,GstMessage * msg,Pipeline * p)259 gboolean Pipeline::bus_call (GstBus *bus, GstMessage *msg, Pipeline* p)
260 {
261 switch (GST_MESSAGE_TYPE (msg))
262 {
263 case GST_MESSAGE_EOS:
264 qDebug ("End-of-stream");
265 p->stop();
266 break;
267 case GST_MESSAGE_ERROR:
268 {
269 gchar *debug = NULL;
270 GError *err = NULL;
271 gst_message_parse_error (msg, &err, &debug);
272 qDebug ("Error: %s", err->message);
273 g_error_free (err);
274 if (debug)
275 {
276 qDebug ("Debug deails: %s", debug);
277 g_free (debug);
278 }
279 p->stop();
280 break;
281 }
282 default:
283 break;
284 }
285
286 return TRUE;
287 }
288
cb_new_pad(GstElement * decodebin,GstPad * pad,Pipeline * p)289 void Pipeline::cb_new_pad (GstElement* decodebin, GstPad* pad, Pipeline* p)
290 {
291 GstElement* sink = p->getVideoSink();
292 GstPad* glpad = gst_element_get_static_pad (sink, "sink");
293
294 //only link once
295 if (GST_PAD_IS_LINKED (glpad))
296 {
297 gst_object_unref (glpad);
298 return;
299 }
300
301 GstCaps* caps = gst_pad_get_current_caps (pad);
302 GstStructure* str = gst_caps_get_structure (caps, 0);
303 if (!g_strrstr (gst_structure_get_name (str), "video"))
304 {
305 gst_caps_unref (caps);
306 gst_object_unref (glpad);
307 return;
308 }
309 gst_caps_unref (caps);
310
311 GstPadLinkReturn ret = gst_pad_link (pad, glpad);
312 if (ret != GST_PAD_LINK_OK)
313 g_warning ("Failed to link with decodebin!\n");
314
315 p->show();
316 }
317
cb_expose(gpointer data)318 gboolean Pipeline::cb_expose (gpointer data)
319 {
320 ((Pipeline*)data)->doExpose();
321 return FALSE;
322 }
323
create_window(GstBus * bus,GstMessage * message,const Pipeline * p)324 GstBusSyncReply Pipeline::create_window (GstBus* bus, GstMessage* message, const Pipeline* p)
325 {
326 // ignore anything but 'prepare-window-handle' element messages
327 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
328 return GST_BUS_PASS;
329
330 if (!gst_is_video_overlay_prepare_window_handle_message (message))
331 return GST_BUS_PASS;
332
333 qDebug ("setting window handle");
334
335 //Passing 0 as the window_handle will tell the overlay to stop using that window and create an internal one.
336 //In the directdrawsink's gst_video_overlay_set_window_handle implementation, window_handle (parameter 2) is casted to HWND before it used.
337 gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message)), (guintptr)p->winId());
338
339 gst_message_unref (message);
340
341 return GST_BUS_DROP;
342 }
343