2014-04-29 06:38:55 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2009 Julien Isorce <julien.isorce@gmail.com>
|
|
|
|
* Copyright (C) 2009 Andrey Nechypurenko <andreynech@gmail.com>
|
|
|
|
* Copyright (C) 2010 Nuno Santos <nunosantos@imaginando.net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QGLWidget>
|
|
|
|
#include <QApplication>
|
2014-07-01 00:36:53 +00:00
|
|
|
#include <QDebug>
|
2014-04-29 06:38:55 +00:00
|
|
|
#include <QCloseEvent>
|
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
#include <GL/glx.h>
|
|
|
|
|
2014-04-29 06:38:55 +00:00
|
|
|
#include <gst/video/video.h>
|
2016-11-08 04:04:29 +00:00
|
|
|
#include <gst/gl/gl.h>
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
#if GST_GL_HAVE_PLATFORM_GLX
|
|
|
|
#include <QX11Info>
|
|
|
|
#include <gst/gl/x11/gstgldisplay_x11.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-29 06:38:55 +00:00
|
|
|
#include "gstthread.h"
|
|
|
|
#include "qglrenderer.h"
|
|
|
|
#include "pipeline.h"
|
|
|
|
|
|
|
|
#if defined(Q_WS_MAC)
|
2014-09-28 14:23:57 +00:00
|
|
|
extern void *qt_current_nsopengl_context ();
|
2014-04-29 06:38:55 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::QGLRenderer (const QString & videoLocation, QWidget * parent)
|
|
|
|
:
|
|
|
|
QGLWidget (parent),
|
|
|
|
videoLoc (videoLocation),
|
|
|
|
gst_thread (NULL),
|
|
|
|
closing (false),
|
|
|
|
frame (NULL)
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
2014-09-28 14:23:57 +00:00
|
|
|
move (20, 10);
|
|
|
|
resize (640, 480);
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::~QGLRenderer ()
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::initializeGL ()
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
2014-09-28 14:23:57 +00:00
|
|
|
GstGLContext *context;
|
|
|
|
GstGLDisplay *display;
|
|
|
|
|
|
|
|
#if GST_GL_HAVE_PLATFORM_GLX
|
|
|
|
display =
|
|
|
|
(GstGLDisplay *) gst_gl_display_x11_new_with_display (QX11Info::
|
|
|
|
display ());
|
|
|
|
#else
|
|
|
|
display = gst_gl_display_new ();
|
|
|
|
#endif
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
/* FIXME: Allow the choice at runtime */
|
2014-07-01 00:36:53 +00:00
|
|
|
#if GST_GL_HAVE_PLATFORM_WGL
|
2014-09-28 14:23:57 +00:00
|
|
|
context =
|
|
|
|
gst_gl_context_new_wrapped (display, (guintptr) wglGetCurrentContext (),
|
|
|
|
GST_GL_PLATFORM_WGL, GST_GL_API_OPENGL);
|
2014-07-01 00:36:53 +00:00
|
|
|
#elif GST_GL_HAVE_PLATFORM_CGL
|
2014-09-28 14:23:57 +00:00
|
|
|
context =
|
|
|
|
gst_gl_context_new_wrapped (display,
|
|
|
|
(guintptr) qt_current_nsopengl_context (), GST_GL_PLATFORM_CGL,
|
|
|
|
GST_GL_API_OPENGL);
|
2014-07-01 00:36:53 +00:00
|
|
|
#elif GST_GL_HAVE_PLATFORM_GLX
|
2014-09-28 14:23:57 +00:00
|
|
|
context =
|
|
|
|
gst_gl_context_new_wrapped (display, (guintptr) glXGetCurrentContext (),
|
|
|
|
GST_GL_PLATFORM_GLX, GST_GL_API_OPENGL);
|
2014-04-29 06:38:55 +00:00
|
|
|
#endif
|
2014-09-28 14:23:57 +00:00
|
|
|
gst_object_unref (display);
|
|
|
|
|
|
|
|
// We need to unset Qt context before initializing gst-gl plugin.
|
|
|
|
// Otherwise the attempt to share gst-gl context with Qt will fail.
|
|
|
|
this->doneCurrent ();
|
|
|
|
this->gst_thread =
|
|
|
|
new GstThread (display, context, this->videoLoc,
|
|
|
|
SLOT (newFrame ()), this);
|
|
|
|
this->makeCurrent ();
|
|
|
|
|
|
|
|
QObject::connect (this->gst_thread, SIGNAL (finished ()),
|
|
|
|
this, SLOT (close ()));
|
|
|
|
QObject::connect (this, SIGNAL (closeRequested ()),
|
|
|
|
this->gst_thread, SLOT (stop ()), Qt::QueuedConnection);
|
|
|
|
|
|
|
|
qglClearColor (QApplication::palette ().color (QPalette::Active,
|
|
|
|
QPalette::Window));
|
|
|
|
//glShadeModel(GL_FLAT);
|
|
|
|
//glEnable(GL_DEPTH_TEST);
|
|
|
|
//glEnable(GL_CULL_FACE);
|
|
|
|
glEnable (GL_TEXTURE_2D); // Enable Texture Mapping
|
|
|
|
|
|
|
|
this->gst_thread->start ();
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::resizeGL (int width, int height)
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
|
|
|
// Reset The Current Viewport And Perspective Transformation
|
2014-09-28 14:23:57 +00:00
|
|
|
glViewport (0, 0, width, height);
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
glMatrixMode (GL_PROJECTION);
|
|
|
|
glLoadIdentity ();
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
glMatrixMode (GL_MODELVIEW);
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::newFrame ()
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
2014-09-28 14:23:57 +00:00
|
|
|
Pipeline *pipeline = this->gst_thread->getPipeline ();
|
|
|
|
if (!pipeline)
|
|
|
|
return;
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
/* frame is initialized as null */
|
|
|
|
if (this->frame)
|
|
|
|
pipeline->queue_output_buf.put (this->frame);
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
this->frame = pipeline->queue_input_buf.get ();
|
2014-04-29 06:38:55 +00:00
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
/* direct call to paintGL (no queued) */
|
|
|
|
this->updateGL ();
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 14:23:57 +00:00
|
|
|
static void
|
|
|
|
flushGstreamerGL (GstGLContext * context, void *data G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
context->gl_vtable->Flush ();
|
2014-08-26 08:21:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 06:38:55 +00:00
|
|
|
void
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::paintGL ()
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
2014-09-28 14:23:57 +00:00
|
|
|
static GLfloat xrot = 0;
|
|
|
|
static GLfloat yrot = 0;
|
|
|
|
static GLfloat zrot = 0;
|
|
|
|
|
|
|
|
if (this->frame) {
|
|
|
|
guint tex_id;
|
|
|
|
GstMemory *mem;
|
|
|
|
GstVideoInfo v_info;
|
|
|
|
GstVideoFrame v_frame;
|
|
|
|
GstVideoMeta *v_meta;
|
|
|
|
|
|
|
|
mem = gst_buffer_peek_memory (this->frame, 0);
|
|
|
|
v_meta = gst_buffer_get_video_meta (this->frame);
|
|
|
|
|
|
|
|
Q_ASSERT (gst_is_gl_memory (mem));
|
|
|
|
|
|
|
|
GstGLMemory *gl_memory = (GstGLMemory *) mem;
|
|
|
|
|
2015-06-15 06:47:15 +00:00
|
|
|
gst_gl_context_thread_add (gl_memory->mem.context, flushGstreamerGL, NULL);
|
2014-09-28 14:23:57 +00:00
|
|
|
|
|
|
|
gst_video_info_set_format (&v_info, v_meta->format, v_meta->width,
|
|
|
|
v_meta->height);
|
|
|
|
|
|
|
|
gst_video_frame_map (&v_frame, &v_info, this->frame,
|
|
|
|
(GstMapFlags) (GST_MAP_READ | GST_MAP_GL));
|
|
|
|
|
|
|
|
tex_id = *(guint *) v_frame.data[0];
|
|
|
|
|
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
glEnable (GL_TEXTURE_2D);
|
|
|
|
glBindTexture (GL_TEXTURE_2D, tex_id);
|
|
|
|
if (glGetError () != GL_NO_ERROR) {
|
|
|
|
qDebug ("failed to bind texture that comes from gst-gl");
|
|
|
|
emit closeRequested ();
|
|
|
|
return;
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|
2014-09-28 14:23:57 +00:00
|
|
|
|
|
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
|
|
|
|
|
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glMatrixMode (GL_MODELVIEW);
|
|
|
|
glLoadIdentity ();
|
|
|
|
|
2015-06-15 06:47:15 +00:00
|
|
|
glScalef (0.5f, 0.5f, 0.5f);
|
2014-09-28 14:23:57 +00:00
|
|
|
|
|
|
|
glRotatef (xrot, 1.0f, 0.0f, 0.0f);
|
|
|
|
glRotatef (yrot, 0.0f, 1.0f, 0.0f);
|
|
|
|
glRotatef (zrot, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
|
|
|
glBegin (GL_QUADS);
|
|
|
|
// Front Face
|
|
|
|
glTexCoord2f (1.0f, 0.0f);
|
|
|
|
glVertex3f (-1.0f, -1.0f, 1.0f);
|
|
|
|
glTexCoord2f (0.0f, 0.0f);
|
|
|
|
glVertex3f (1.0f, -1.0f, 1.0f);
|
|
|
|
glTexCoord2f (0.0f, 1.0f);
|
|
|
|
glVertex3f (1.0f, 1.0f, 1.0f);
|
|
|
|
glTexCoord2f (1.0f, 1.0f);
|
|
|
|
glVertex3f (-1.0f, 1.0f, 1.0f);
|
|
|
|
// Back Face
|
|
|
|
glTexCoord2f (0.0f, 0.0f);
|
|
|
|
glVertex3f (-1.0f, -1.0f, -1.0f);
|
|
|
|
glTexCoord2f (0.0f, 1.0f);
|
|
|
|
glVertex3f (-1.0f, 1.0f, -1.0f);
|
|
|
|
glTexCoord2f (1.0f, 1.0f);
|
|
|
|
glVertex3f (1.0f, 1.0f, -1.0f);
|
|
|
|
glTexCoord2f (1.0f, 0.0f);
|
|
|
|
glVertex3f (1.0f, -1.0f, -1.0f);
|
|
|
|
// Top Face
|
|
|
|
glTexCoord2f (1.0f, 1.0f);
|
|
|
|
glVertex3f (-1.0f, 1.0f, -1.0f);
|
|
|
|
glTexCoord2f (1.0f, 0.0f);
|
|
|
|
glVertex3f (-1.0f, 1.0f, 1.0f);
|
|
|
|
glTexCoord2f (0.0f, 0.0f);
|
|
|
|
glVertex3f (1.0f, 1.0f, 1.0f);
|
|
|
|
glTexCoord2f (0.0f, 1.0f);
|
|
|
|
glVertex3f (1.0f, 1.0f, -1.0f);
|
|
|
|
// Bottom Face
|
|
|
|
glTexCoord2f (1.0f, 0.0f);
|
|
|
|
glVertex3f (-1.0f, -1.0f, -1.0f);
|
|
|
|
glTexCoord2f (0.0f, 0.0f);
|
|
|
|
glVertex3f (1.0f, -1.0f, -1.0f);
|
|
|
|
glTexCoord2f (0.0f, 1.0f);
|
|
|
|
glVertex3f (1.0f, -1.0f, 1.0f);
|
|
|
|
glTexCoord2f (1.0f, 1.0f);
|
|
|
|
glVertex3f (-1.0f, -1.0f, 1.0f);
|
|
|
|
// Right face
|
|
|
|
glTexCoord2f (0.0f, 0.0f);
|
|
|
|
glVertex3f (1.0f, -1.0f, -1.0f);
|
|
|
|
glTexCoord2f (0.0f, 1.0f);
|
|
|
|
glVertex3f (1.0f, 1.0f, -1.0f);
|
|
|
|
glTexCoord2f (1.0f, 1.0f);
|
|
|
|
glVertex3f (1.0f, 1.0f, 1.0f);
|
|
|
|
glTexCoord2f (1.0f, 0.0f);
|
|
|
|
glVertex3f (1.0f, -1.0f, 1.0f);
|
|
|
|
// Left Face
|
|
|
|
glTexCoord2f (1.0f, 0.0f);
|
|
|
|
glVertex3f (-1.0f, -1.0f, -1.0f);
|
|
|
|
glTexCoord2f (0.0f, 0.0f);
|
|
|
|
glVertex3f (-1.0f, -1.0f, 1.0f);
|
|
|
|
glTexCoord2f (0.0f, 1.0f);
|
|
|
|
glVertex3f (-1.0f, 1.0f, 1.0f);
|
|
|
|
glTexCoord2f (1.0f, 1.0f);
|
|
|
|
glVertex3f (-1.0f, 1.0f, -1.0f);
|
|
|
|
glEnd ();
|
|
|
|
|
|
|
|
xrot += 0.3f;
|
|
|
|
yrot += 0.2f;
|
|
|
|
zrot += 0.4f;
|
|
|
|
|
2016-11-08 04:04:29 +00:00
|
|
|
glLoadIdentity();
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
2014-09-28 14:23:57 +00:00
|
|
|
glBindTexture (GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
gst_video_frame_unmap (&v_frame);
|
|
|
|
}
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-28 14:23:57 +00:00
|
|
|
QGLRenderer::closeEvent (QCloseEvent * event)
|
2014-04-29 06:38:55 +00:00
|
|
|
{
|
2014-09-28 14:23:57 +00:00
|
|
|
if (this->closing == false) {
|
|
|
|
this->closing = true;
|
|
|
|
emit closeRequested ();
|
|
|
|
event->ignore ();
|
|
|
|
}
|
2014-04-29 06:38:55 +00:00
|
|
|
}
|