Commit b1bbdcee authored by Matthew Waters's avatar Matthew Waters 🍧

new qt5 qml GL video sink

Very much in the same spirit as the Gtk GL sink

Two things are provided
1. A QQuickItem subclass that renders out RGBA filled GstGLMemory
   buffers that is instantiated from qml.
2. A sink element that will push buffers into (1)

To use
1. Declare the GstGLVideoItem in qml with an appropriate
   objectName property set.
2. Get the aforementioned GstGLVideoItem from qml using something like

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

QObject *rootObject = engine.rootObjects().first();
QQuickItem *videoItem = rootObject->findChild<QQuickItem *> ("videoItem");

3. Set the videoItem on the sink

https://bugzilla.gnome.org/show_bug.cgi?id=752185
parents
# preamble
NULL =
moc_headers = \
qtitem.h \
gstqsgtexture.h \
$(NULL)
moc_generated = \
moc_qtitem.cc \
moc_gstqsgtexture.cc \
$(NULL)
#anything generated by the Qt tools...
BUILT_SOURCES = $(moc_generated)
CLEANFILES = $(moc_generated)
sources = \
gstqsgtexture.cc \
qtitem.cc \
gstqtsink.cc \
gstqtsink.h \
gstplugin.cc \
$(moc_headers) \
$(moc_generated) \
$(NULL)
libqtsink_la_CXXFLAGS = \
-I$(top_srcdir)/gst-libs \
-I$(top_builddir)/gst-libs \
$(GST_CXXFLAGS) \
$(GST_BASE_CFLAGS) \
$(GST_PLUGINS_BASE_CFLAGS) \
$(QT_CFLAGS)
libqtsink_la_LIBADD = \
$(GST_BASE_LIBS) \
$(GST_PLUGINS_BASE_LIBS) \
$(QT_LIBS) \
$(top_builddir)/gst-libs/gst/gl/libgstgl-$(GST_API_VERSION).la \
-lgstvideo-$(GST_API_VERSION)
libqtsink_la_SOURCES = $(sources)
libqtsink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libqtsink_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS)
plugin_LTLIBRARIES = libqtsink.la
$(moc_generated): moc_%.cc: %.h
@@MOC@ -o$@ $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(MOC_CPPFLAGS) $<
ui-%.h: %.ui
@@UIC@ -o $@ $<
qrc-%.cc: %.qrc
@@RCC@ -o $@ $<
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstqtsink.h"
#include <QQmlApplicationEngine>
static gboolean
plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "qmlglsink",
GST_RANK_NONE, GST_TYPE_QT_SINK)) {
return FALSE;
}
/* this means the plugin must be loaded before the qml engine is loaded */
qmlRegisterType<QtGLVideoItem> ("org.freedesktop.gstreamer.GLVideoItem", 1, 0, "GstGLVideoItem");
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
qt,
"Qt sink",
plugin_init, PACKAGE_VERSION, GST_LICENSE, GST_PACKAGE_NAME,
GST_PACKAGE_ORIGIN)
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <gst/video/video.h>
#include <gst/gl/gl.h>
#include "gstqsgtexture.h"
#define GST_CAT_DEFAULT gst_qsg_texture_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
GstQSGTexture::GstQSGTexture ()
{
static volatile gsize _debug;
initializeOpenGLFunctions();
if (g_once_init_enter (&_debug)) {
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtqsgtexture", 0,
"Qt Scenegraph Texture");
g_once_init_leave (&_debug, 1);
}
gst_video_info_init (&this->v_info);
this->buffer_ = NULL;
}
GstQSGTexture::~GstQSGTexture ()
{
gst_buffer_replace (&this->buffer_, NULL);
}
/* only called from the streaming thread with scene graph thread blocked */
void
GstQSGTexture::setCaps (GstCaps * caps)
{
GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
gst_video_info_from_caps (&this->v_info, caps);
}
/* only called from the streaming thread with scene graph thread blocked */
void
GstQSGTexture::setBuffer (GstBuffer * buffer)
{
GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
/* FIXME: update more state here */
gst_buffer_replace (&this->buffer_, buffer);
}
/* only called from qt's scene graph render thread */
void
GstQSGTexture::bind ()
{
guint tex_id;
if (!this->buffer_)
return;
if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
return;
/* FIXME: should really lock the memory to prevent write access */
if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
(GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
g_assert_not_reached ();
return;
}
tex_id = *(guint *) this->v_frame.data[0];
GST_LOG ("%p binding Qt texture %u", this, tex_id);
glBindTexture (GL_TEXTURE_2D, tex_id);
gst_video_frame_unmap (&this->v_frame);
}
/* can be called from any thread */
int
GstQSGTexture::textureId () const
{
int tex_id = 0;
if (this->buffer_) {
GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
tex_id = ((GstGLMemory *) mem)->tex_id;
}
GST_LOG ("%p get texture id %u", this, tex_id);
return tex_id;
}
/* can be called from any thread */
QSize
GstQSGTexture::textureSize () const
{
if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
return QSize (0, 0);
GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
this->v_info.height);
return QSize (this->v_info.width, this->v_info.height);
}
/* can be called from any thread */
bool
GstQSGTexture::hasAlphaChannel () const
{
/* FIXME: support RGB textures */
return true;
}
/* can be called from any thread */
bool
GstQSGTexture::hasMipmaps () const
{
return false;
}
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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.
*/
#ifndef __GST_QSG_TEXTURE_H__
#define __GST_QSG_TEXTURE_H__
#include <gst/gst.h>
#include <QSGTexture>
#include <QOpenGLFunctions>
#include <gst/video/video.h>
class GstQSGTexture : public QSGTexture, protected QOpenGLFunctions
{
Q_OBJECT
public:
GstQSGTexture ();
~GstQSGTexture ();
void setCaps (GstCaps * caps);
void setBuffer (GstBuffer * buffer);
/* QSGTexture */
void bind ();
int textureId () const;
QSize textureSize () const;
bool hasAlphaChannel () const;
bool hasMipmaps () const;
private:
GstBuffer * buffer_;
GstVideoInfo v_info;
GstVideoFrame v_frame;
};
#endif /* __GST_QSG_TEXTURE_H__ */
This diff is collapsed.
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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.
*/
#ifndef __GST_QT_SINK_H__
#define __GST_QT_SINK_H__
#include <gst/gst.h>
#include <gst/video/gstvideosink.h>
#include <gst/video/video.h>
#include <gst/gl/gl.h>
#include "qtitem.h"
typedef struct _GstQtSink GstQtSink;
typedef struct _GstQtSinkClass GstQtSinkClass;
typedef struct _GstQtSinkPrivate GstQtSinkPrivate;
G_BEGIN_DECLS
GType gst_qt_sink_get_type (void);
#define GST_TYPE_QT_SINK (gst_qt_sink_get_type())
#define GST_QT_SINK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QT_SINK,GstQtSink))
#define GST_QT_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_QT_SINK,GstQtSinkClass))
#define GST_IS_QT_SINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QT_SINK))
#define GST_IS_QT_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QT_SINK))
#define GST_QT_SINK_CAST(obj) ((GstQtSink*)(obj))
/**
* GstQtSink:
*
* Opaque #GstQtSink object
*/
struct _GstQtSink
{
/* <private> */
GstVideoSink parent;
QtGLVideoItem *widget;
GstVideoInfo v_info;
GstBufferPool *pool;
GstGLDisplay *display;
GstGLContext *context;
GstGLContext *qt_context;
GstQtSinkPrivate *priv;
};
/**
* GstQtSinkClass:
*
* The #GstQtSinkClass struct only contains private data
*/
struct _GstQtSinkClass
{
/* <private> */
GstVideoSinkClass object_class;
};
GstQtSink * gst_qt_sink_new (void);
G_END_DECLS
#endif /* __GST_QT_SINK_H__ */
This diff is collapsed.
/*
* GStreamer
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
*
* 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.
*/
#ifndef __QT_ITEM_H__
#define __QT_ITEM_H__
#include <gst/gst.h>
#include <gst/gl/gl.h>
#include <QQuickItem>
#include <QOpenGLContext>
#include <QOpenGLFunctions>
typedef struct _QtGLVideoItemPrivate QtGLVideoItemPrivate;
class QtGLVideoItem : public QQuickItem, protected QOpenGLFunctions
{
Q_OBJECT
public:
QtGLVideoItem();
~QtGLVideoItem();
void setDAR(gint, gint);
void getDAR(gint *, gint *);
void setForceAspectRatio(bool);
bool getForceAspectRatio();
/* private for C interface ... */
QtGLVideoItemPrivate *priv;
private Q_SLOTS:
void handleWindowChanged(QQuickWindow * win);
void onSceneGraphInitialized();
void onSceneGraphInvalidated();
protected:
QSGNode * updatePaintNode (QSGNode * oldNode, UpdatePaintNodeData * updatePaintNodeData);
private:
void setViewportSize(const QSize &size);
void shareContext();
QSize m_viewportSize;
};
extern "C"
{
void qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer);
gboolean qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps);
gboolean qt_item_init_winsys (QtGLVideoItem * widget);
GstGLContext * qt_item_get_qt_context (QtGLVideoItem * qt_item);
GstGLContext * qt_item_get_context (QtGLVideoItem * qt_item);
GstGLDisplay * qt_item_get_display (QtGLVideoItem * qt_item);
}
#endif /* __QT_ITEM_H__ */
deployment.pri
play
qrc_qml.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQuickItem>
#include <QRunnable>
#include <gst/gst.h>
class SetPlaying : public QRunnable
{
public:
SetPlaying(GstElement *);
~SetPlaying();
void run ();
private:
GstElement * pipeline_;
};
SetPlaying::SetPlaying (GstElement * pipeline)
{
this->pipeline_ = pipeline ? static_cast<GstElement *> (gst_object_ref (pipeline)) : NULL;
}
SetPlaying::~SetPlaying ()
{
if (this->pipeline_)
gst_object_unref (this->pipeline_);
}
void
SetPlaying::run ()
{
if (this->pipeline_)
gst_element_set_state (this->pipeline_, GST_STATE_PLAYING);
}
int main(int argc, char *argv[])
{
int ret;
QGuiApplication app(argc, argv);
gst_init (&argc, &argv);
GstElement *pipeline = gst_pipeline_new (NULL);
GstElement *src = gst_element_factory_make ("videotestsrc", NULL);
GstElement *glupload = gst_element_factory_make ("glupload", NULL);
/* the plugin must be loaded before loading the qml file to register the
* GstGLVideoItem qml item */
GstElement *sink = gst_element_factory_make ("qmlglsink", NULL);
g_assert (src && glupload && sink);
gst_bin_add_many (GST_BIN (pipeline), src, glupload, sink, NULL);
gst_element_link_many (src, glupload, sink, NULL);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QQuickItem *videoItem;
QQuickWindow *rootObject;
/* find and set the videoItem on the sink */
rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first());
videoItem = rootObject->findChild<QQuickItem *> ("videoItem");
g_assert (videoItem);
g_object_set(sink, "widget", videoItem, NULL);
rootObject->scheduleRenderJob (new SetPlaying (pipeline),
QQuickWindow::BeforeSynchronizingStage);
ret = app.exec();
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
gst_deinit ();
return ret;
}
import QtQuick 2.4
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.3
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.1
import org.freedesktop.gstreamer.GLVideoItem 1.0
ApplicationWindow {
id: window
visible: true
width: 640
height: 480
x: 30
y: 30
color: "black"
Item {
anchors.fill: parent
GstGLVideoItem {
id: video
objectName: "videoItem"
anchors.centerIn: parent
width: parent.width
height: parent.height
}
Rectangle {
color: Qt.rgba(1, 1, 1, 0.7)
border.width: 1
border.color: "white"
anchors.bottom: video.bottom
anchors.bottomMargin: 15
anchors.horizontalCenter: parent.horizontalCenter
width : childrenRect.width + 20
height: childrenRect.height + 20
radius: 8
MouseArea {
id: mousearea
anchors.fill: parent
hoverEnabled: true
onEntered: {
parent.opacity = 1.0
hidetimer.start()
}
}
Timer {
id: hidetimer
interval: 5000
onTriggered: {
parent.opacity = 0.0
stop()
}
}
}
}
}
TEMPLATE = app
QT += qml quick widgets
QT_CONFIG -= no-pkg-config
CONFIG += link_pkgconfig debug
PKGCONFIG = \
gstreamer-1.0 \
gstreamer-video-1.0
DEFINES += GST_USE_UNSTABLE_API
INCLUDEPATH += ../lib
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment