diff options
author | Tim-Philipp Müller <tim@centricular.com> | 2018-02-12 18:13:17 +0000 |
---|---|---|
committer | Tim-Philipp Müller <tim@centricular.com> | 2018-02-12 18:13:17 +0000 |
commit | 6c75e6c5e0d5ae128b52437c790c9130acf89a7b (patch) | |
tree | 493deecdddc4563e8c377983693a5fa40e87a1f3 | |
parent | 16a70b58a6edd5927299406494544b5199e2e240 (diff) | |
parent | 93cf3ef912e4f2868c3875a631e2ccefedc4f4ea (diff) |
Move qt plugin from -bad
https://bugzilla.gnome.org/show_bug.cgi?id=754094
28 files changed, 3487 insertions, 0 deletions
diff --git a/ext/qt/.gitignore b/ext/qt/.gitignore new file mode 100644 index 000000000..f830c38fc --- /dev/null +++ b/ext/qt/.gitignore @@ -0,0 +1 @@ +moc_*.cc diff --git a/ext/qt/Makefile.am b/ext/qt/Makefile.am new file mode 100644 index 000000000..cf2f761c3 --- /dev/null +++ b/ext/qt/Makefile.am @@ -0,0 +1,60 @@ +plugin_LTLIBRARIES = libgstqmlgl.la + +noinst_HEADERS = \ + gstqsgtexture.h \ + gstqtglutility.h \ + gstqtsink.h \ + gstqtsrc.h \ + qtitem.h \ + qtwindow.h + +moc_generated = \ + moc_qtitem.cc \ + moc_qtwindow.cc \ + moc_gstqsgtexture.cc + +#anything generated by the Qt tools... +BUILT_SOURCES = $(moc_generated) +CLEANFILES = $(moc_generated) + +nodist_libgstqmlgl_la_SOURCES = $(BUILT_SOURCES) + +libgstqmlgl_la_SOURCES = \ + gstqsgtexture.cc \ + gstqtglutility.cc \ + qtitem.cc \ + qtwindow.cc \ + gstqtsink.cc \ + gstqtsink.h \ + gstqtsrc.cc \ + gstqtsrc.h \ + gstqtgl.h \ + gstplugin.cc + +libgstqmlgl_la_CXXFLAGS = \ + $(GST_PLUGINS_BAD_CFLAGS) \ + $(GST_PLUGINS_BASE_CFLAGS) \ + $(GST_GL_CFLAGS) \ + $(GST_BASE_CFLAGS) \ + $(GST_CXXFLAGS) \ + $(QT_CFLAGS) \ + $(GL_CFLAGS) -std=c++11 + +libgstqmlgl_la_LIBADD = \ + $(GST_PLUGINS_BASE_LIBS) \ + $(GST_GL_LIBS) \ + -lgstvideo-$(GST_API_VERSION) \ + $(GST_BASE_LIBS) \ + $(QT_LIBS) + +libgstqmlgl_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + + +$(moc_generated): moc_%.cc: %.h + @MOC@ -o $@ $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(MOC_CPPFLAGS) $< + +ui-%.h: %.ui + @UIC@ -o $@ $< + +qrc-%.cc: %.qrc + @RCC@ -o $@ $< diff --git a/ext/qt/gstplugin.cc b/ext/qt/gstplugin.cc new file mode 100644 index 000000000..79fb18189 --- /dev/null +++ b/ext/qt/gstplugin.cc @@ -0,0 +1,60 @@ +/* + * 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 "gstqtsrc.h" +#include <QtQml/QQmlApplicationEngine> + +static gboolean +plugin_init (GstPlugin * plugin) +{ + if (!gst_element_register (plugin, "qmlglsink", + GST_RANK_NONE, GST_TYPE_QT_SINK)) { + return FALSE; + } + + if (!gst_element_register (plugin, "qmlglsrc", + GST_RANK_NONE, GST_TYPE_QT_SRC)) { + 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; +} + +#ifndef GST_PACKAGE_NAME +#define GST_PACKAGE_NAME "GStreamer Bad Plug-ins (qmake)" +#define GST_PACKAGE_ORIGIN "Unknown package origin" +#define GST_LICENSE "LGPL" +#define PACKAGE "gst-plugins-bad (qmake)" +#define PACKAGE_VERSION "1.13.0.1" +#endif + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + qmlgl, + "Qt gl plugin", + plugin_init, PACKAGE_VERSION, GST_LICENSE, GST_PACKAGE_NAME, + GST_PACKAGE_ORIGIN) diff --git a/ext/qt/gstqsgtexture.cc b/ext/qt/gstqsgtexture.cc new file mode 100644 index 000000000..2b314e0ee --- /dev/null +++ b/ext/qt/gstqsgtexture.cc @@ -0,0 +1,224 @@ +/* + * 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 <vector> +#include <stdio.h> + +#include <gst/video/video.h> +#include <gst/gl/gl.h> +#include <gst/gl/gstglfuncs.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; + this->qt_context_ = NULL; + this->sync_buffer_ = gst_buffer_new (); + this->dummy_tex_id_ = 0; +} + +GstQSGTexture::~GstQSGTexture () +{ + gst_buffer_replace (&this->buffer_, NULL); + gst_buffer_replace (&this->sync_buffer_, NULL); + if (this->dummy_tex_id_ && QOpenGLContext::currentContext ()) { + QOpenGLContext::currentContext ()->functions ()->glDeleteTextures (1, + &this->dummy_tex_id_); + } +} + +/* 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 */ +gboolean +GstQSGTexture::setBuffer (GstBuffer * buffer) +{ + GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer); + /* FIXME: update more state here */ + if (!gst_buffer_replace (&this->buffer_, buffer)) + return FALSE; + + this->qt_context_ = gst_gl_context_get_current (); + + return TRUE; +} + +/* only called from qt's scene graph render thread */ +void +GstQSGTexture::bind () +{ + const GstGLFuncs *gl; + GstGLContext *context; + GstGLSyncMeta *sync_meta; + GstMemory *mem; + guint tex_id; + gboolean use_dummy_tex = TRUE; + + if (!this->qt_context_) + return; + + gst_gl_context_activate (this->qt_context_, TRUE); + + if (!this->buffer_) + goto out; + if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN) + goto out; + + this->mem_ = gst_buffer_peek_memory (this->buffer_, 0); + if (!this->mem_) + goto out; + + g_assert (this->qt_context_); + gl = this->qt_context_->gl_vtable; + + /* 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 (); + goto out; + } + + mem = gst_buffer_peek_memory (this->buffer_, 0); + g_assert (gst_is_gl_memory (mem)); + + context = ((GstGLBaseMemory *)mem)->context; + + sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_); + if (!sync_meta) + sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_); + + gst_gl_sync_meta_set_sync_point (sync_meta, context); + + gst_gl_sync_meta_wait (sync_meta, this->qt_context_); + + tex_id = *(guint *) this->v_frame.data[0]; + GST_LOG ("%p binding Qt texture %u", this, tex_id); + + gl->BindTexture (GL_TEXTURE_2D, tex_id); + + gst_video_frame_unmap (&this->v_frame); + + /* Texture was successfully bound, so we do not need + * to use the dummy texture */ + use_dummy_tex = FALSE; + +out: + if (G_UNLIKELY (use_dummy_tex)) { + QOpenGLContext *qglcontext = QOpenGLContext::currentContext (); + QOpenGLFunctions *funcs = qglcontext->functions (); + + /* Create dummy texture if not already present. + * Use the Qt OpenGL functions instead of the GstGL ones, + * since we are using the Qt OpenGL context here, and we must + * be able to delete the texture in the destructor. */ + if (this->dummy_tex_id_ == 0) { + /* Make this a black 64x64 pixel RGBA texture. + * This size and format is supported pretty much everywhere, so these + * are a safe pick. (64 pixel sidelength must be supported according + * to the GLES2 spec, table 6.18.) + * Set min/mag filters to GL_LINEAR to make sure no mipmapping is used. */ + const int tex_sidelength = 64; + std::vector < guint8 > dummy_data (tex_sidelength * tex_sidelength * 4, 0); + + funcs->glGenTextures (1, &this->dummy_tex_id_); + funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_); + funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + funcs->glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_sidelength, + tex_sidelength, 0, GL_RGBA, GL_UNSIGNED_BYTE, &dummy_data[0]); + } + + g_assert (this->dummy_tex_id_ != 0); + + funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_); + } + + gst_gl_context_activate (this->qt_context_, FALSE); +} + +/* 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; +} diff --git a/ext/qt/gstqsgtexture.h b/ext/qt/gstqsgtexture.h new file mode 100644 index 000000000..fdabe93a9 --- /dev/null +++ b/ext/qt/gstqsgtexture.h @@ -0,0 +1,59 @@ +/* + * 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 <gst/video/video.h> +#include <gst/gl/gl.h> + +#include "gstqtgl.h" +#include <QtQuick/QSGTexture> +#include <QtGui/QOpenGLFunctions> + +class GstQSGTexture : public QSGTexture, protected QOpenGLFunctions +{ + Q_OBJECT +public: + GstQSGTexture (); + ~GstQSGTexture (); + + void setCaps (GstCaps * caps); + gboolean setBuffer (GstBuffer * buffer); + + /* QSGTexture */ + void bind (); + int textureId () const; + QSize textureSize () const; + bool hasAlphaChannel () const; + bool hasMipmaps () const; + +private: + GstBuffer * buffer_; + GstBuffer * sync_buffer_; + GstGLContext * qt_context_; + GstMemory * mem_; + GLuint dummy_tex_id_; + GstVideoInfo v_info; + GstVideoFrame v_frame; +}; + +#endif /* __GST_QSG_TEXTURE_H__ */ diff --git a/ext/qt/gstqtgl.h b/ext/qt/gstqtgl.h new file mode 100644 index 000000000..85aad6205 --- /dev/null +++ b/ext/qt/gstqtgl.h @@ -0,0 +1,37 @@ +/* + * 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. + */ + +/* qt uses the same trick as us to typedef GLsync on gles2 but to a different + * type which confuses the preprocessor. As it's never actually used by qt + * public headers, define it to something else to avoid redefinition + * warnings/errors */ + +#include <gst/gl/gstglconfig.h> +#include <QtCore/qglobal.h> +#if (QT_VERSION >= QT_VERSION_CHECK(5, 9, 0)) +#include <QtGui/qtgui-config.h> +#endif + +#if defined(QT_OPENGL_ES_2) && GST_GL_HAVE_WINDOW_ANDROID +#define GLsync gst_qt_GLsync +#include <QOpenGLContext> +#include <QOpenGLFunctions> +#undef GLsync +#endif /* defined(QT_OPENGL_ES_2) */ diff --git a/ext/qt/gstqtglutility.cc b/ext/qt/gstqtglutility.cc new file mode 100644 index 000000000..8d5b24283 --- /dev/null +++ b/ext/qt/gstqtglutility.cc @@ -0,0 +1,247 @@ +/* + * GStreamer + * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved. + * + * 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 "gstqtglutility.h" +#include <QtGui/QGuiApplication> + +#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11) +#include <QX11Info> +#include <gst/gl/x11/gstgldisplay_x11.h> +#endif + +#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND) +#include <qpa/qplatformnativeinterface.h> +#include <gst/gl/wayland/gstgldisplay_wayland.h> +#endif + +#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS) +#if GST_GL_HAVE_WINDOW_VIV_FB +#include <qpa/qplatformnativeinterface.h> +#include <gst/gl/viv-fb/gstgldisplay_viv_fb.h> +#else +#include <gst/gl/egl/gstegl.h> +#include <gst/gl/egl/gstgldisplay_egl.h> +#endif +#endif + +#define GST_CAT_DEFAULT qt_gl_utils_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +GstGLDisplay * +gst_qt_get_gl_display () +{ + GstGLDisplay *display = NULL; + QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ()); + static volatile gsize _debug; + + g_assert (app != NULL); + + if (g_once_init_enter (&_debug)) { + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0, + "Qt gl utility functions"); + g_once_init_leave (&_debug, 1); + } + GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data()); + +#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11) + if (QString::fromUtf8 ("xcb") == app->platformName()) + display = (GstGLDisplay *) + gst_gl_display_x11_new_with_display (QX11Info::display ()); +#endif +#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND) + if (QString::fromUtf8 ("wayland") == app->platformName() + || QString::fromUtf8 ("wayland-egl") == app->platformName()){ + struct wl_display * wayland_display; + QPlatformNativeInterface *native = + QGuiApplication::platformNativeInterface(); + wayland_display = (struct wl_display *) + native->nativeResourceForWindow("display", NULL); + display = (GstGLDisplay *) + gst_gl_display_wayland_new_with_display (wayland_display); + } +#endif +#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID + if (QString::fromUtf8 ("android") == app->platformName()) + display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (eglGetDisplay(EGL_DEFAULT_DISPLAY)); +#elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS) + if (QString::fromUtf8("eglfs") == app->platformName()) { +#if GST_GL_HAVE_WINDOW_VIV_FB + /* FIXME: Could get the display directly from Qt like this + QPlatformNativeInterface *native = + QGuiApplication::platformNativeInterface(); + EGLDisplay egl_display = (EGLDisplay) + native->nativeResourceForWindow("egldisplay", NULL); + + However we seem to have no way for getting the EGLNativeDisplayType, aka + native_display, via public API. As such we have to assume that display 0 + is always used. Only way around that is parsing the index the same way as + Qt does in QEGLDeviceIntegration::fbDeviceName(), so let's do that. + */ + const gchar *fb_dev; + gint disp_idx = 0; + + fb_dev = g_getenv ("QT_QPA_EGLFS_FB"); + if (fb_dev) { + if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1) + disp_idx = 0; + } + + display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx); +#else + display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (eglGetDisplay(EGL_DEFAULT_DISPLAY)); +#endif + } +#endif + +#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC) + if (QString::fromUtf8 ("cocoa") == app->platformName()) + display = (GstGLDisplay *) gst_gl_display_new (); +#endif +#if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS) + if (QString::fromUtf8 ("ios") == app->platformName()) + display = gst_gl_display_new (); +#endif +#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32) + if (QString::fromUtf8 ("windows") == app->platformName()) + display = gst_gl_display_new (); +#endif + + if (!display) + display = gst_gl_display_new (); + + return display; +} + +gboolean +gst_qt_get_gl_wrapcontext (GstGLDisplay * display, + GstGLContext **wrap_glcontext, GstGLContext **context) +{ + GstGLPlatform platform = (GstGLPlatform) 0; + GstGLAPI gl_api; + guintptr gl_handle; + GError *error = NULL; + + g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE); + +#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11) + if (GST_IS_GL_DISPLAY_X11 (display)) { +#if GST_GL_HAVE_PLATFORM_GLX + platform = GST_GL_PLATFORM_GLX; +#elif GST_GL_HAVE_PLATFORM_EGL + platform = GST_GL_PLATFORM_EGL; +#endif + } +#endif +#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND) + if (GST_IS_GL_DISPLAY_WAYLAND (display)) { + platform = GST_GL_PLATFORM_EGL; + } +#endif +#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS) +#if GST_GL_HAVE_WINDOW_VIV_FB + if (GST_IS_GL_DISPLAY_VIV_FB (display)) { +#else + if (GST_IS_GL_DISPLAY_EGL (display)) { +#endif + platform = GST_GL_PLATFORM_EGL; + } +#endif + if (platform == 0) { +#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC) + platform = GST_GL_PLATFORM_CGL; +#elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS) + platform = GST_GL_PLATFORM_EAGL; +#elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32) + platform = GST_GL_PLATFORM_WGL; +#else + GST_ERROR ("Unknown platform"); + return FALSE; +#endif + } + + gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL); + gl_handle = gst_gl_context_get_current_gl_context (platform); + if (gl_handle) + *wrap_glcontext = + gst_gl_context_new_wrapped (display, gl_handle, + platform, gl_api); + + if (!*wrap_glcontext) { + GST_ERROR ("cannot wrap qt OpenGL context"); + return FALSE; + } + + (void) platform; + (void) gl_api; + (void) gl_handle; + + gst_gl_context_activate (*wrap_glcontext, TRUE); + if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) { + GST_ERROR ("failed to retrieve qt context info: %s", error->message); + g_object_unref (*wrap_glcontext); + *wrap_glcontext = NULL; + return FALSE; + } else { + gst_gl_display_filter_gl_api (display, gst_gl_context_get_gl_api (*wrap_glcontext)); +#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32) + g_return_val_if_fail (context != NULL, FALSE); + + G_STMT_START { + GstGLWindow *window; + HDC device; + + /* If there's no wglCreateContextAttribsARB() support, then we would fallback to + * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL + * contexts are current in any other thread. + * + * The workaround here is to temporarily disable Qt's GL context while we + * set up our own. + * + * Sometimes wglCreateContextAttribsARB() + * exists, but isn't functional (some Intel drivers), so it's easiest to do this + * unconditionally. + */ + *context = gst_gl_context_new (display); + window = gst_gl_context_get_window (*context); + device = (HDC) gst_gl_window_get_display (window); + + wglMakeCurrent (device, 0); + gst_object_unref (window); + if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) { + GST_ERROR ("%p failed to create shared GL context: %s", this, error->message); + g_object_unref (*context); + *context = NULL; + g_object_unref (*wrap_glcontext); + *wrap_glcontext = NULL; + wglMakeCurrent (device, (HGLRC) gl_handle); + return FALSE; + } + wglMakeCurrent (device, (HGLRC) gl_handle); + } +#endif + gst_gl_context_activate (*wrap_glcontext, FALSE); + } G_STMT_END; + + return TRUE; +} diff --git a/ext/qt/gstqtglutility.h b/ext/qt/gstqtglutility.h new file mode 100644 index 000000000..ca9023773 --- /dev/null +++ b/ext/qt/gstqtglutility.h @@ -0,0 +1,34 @@ +/* + * GStreamer + * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved. + * + * 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_GL_UTILS_H__ +#define __QT_GL_UTILS_H__ + +#include <gst/gst.h> +#include <gst/gl/gl.h> + +G_BEGIN_DECLS + +GstGLDisplay * gst_qt_get_gl_display (); +gboolean gst_qt_get_gl_wrapcontext (GstGLDisplay * display, + GstGLContext **wrap_glcontext, GstGLContext **context); + +G_END_DECLS +#endif /* __QT_GL_UTILS_H__ */ diff --git a/ext/qt/gstqtsink.cc b/ext/qt/gstqtsink.cc new file mode 100644 index 000000000..2a4ad7ccc --- /dev/null +++ b/ext/qt/gstqtsink.cc @@ -0,0 +1,493 @@ +/* + * 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. + */ + +/** + * SECTION:gstqtsink + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstqtsink.h" +#include <QtGui/QGuiApplication> + +#include <gst/gl/gstglfuncs.h> + +#define GST_CAT_DEFAULT gst_debug_qt_gl_sink +GST_DEBUG_CATEGORY (GST_CAT_DEFAULT); + +static void gst_qt_sink_finalize (GObject * object); +static void gst_qt_sink_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * param_spec); +static void gst_qt_sink_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * param_spec); + +static gboolean gst_qt_sink_stop (GstBaseSink * bsink); + +static gboolean gst_qt_sink_query (GstBaseSink * bsink, GstQuery * query); + +static GstStateChangeReturn +gst_qt_sink_change_state (GstElement * element, GstStateChange transition); + +static void gst_qt_sink_get_times (GstBaseSink * bsink, GstBuffer * buf, + GstClockTime * start, GstClockTime * end); +static gboolean gst_qt_sink_set_caps (GstBaseSink * bsink, GstCaps * caps); +static GstFlowReturn gst_qt_sink_show_frame (GstVideoSink * bsink, + GstBuffer * buf); +static gboolean gst_qt_sink_propose_allocation (GstBaseSink * bsink, + GstQuery * query); + +static GstStaticPadTemplate gst_qt_sink_template = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), " + "format = (string) RGBA, " + "width = " GST_VIDEO_SIZE_RANGE ", " + "height = " GST_VIDEO_SIZE_RANGE ", " + "framerate = " GST_VIDEO_FPS_RANGE ", " + "texture-target = (string) 2D")); + +#define DEFAULT_FORCE_ASPECT_RATIO TRUE +#define DEFAULT_PAR_N 0 +#define DEFAULT_PAR_D 1 + +enum +{ + ARG_0, + PROP_WIDGET, + PROP_FORCE_ASPECT_RATIO, + PROP_PIXEL_ASPECT_RATIO, +}; + +enum +{ + SIGNAL_0, + LAST_SIGNAL +}; + +#define gst_qt_sink_parent_class parent_class +G_DEFINE_TYPE_WITH_CODE (GstQtSink, gst_qt_sink, + GST_TYPE_VIDEO_SINK, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, + "qtsink", 0, "Qt Video Sink")); + +static void +gst_qt_sink_class_init (GstQtSinkClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + GstBaseSinkClass *gstbasesink_class; + GstVideoSinkClass *gstvideosink_class; + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + gstbasesink_class = (GstBaseSinkClass *) klass; + gstvideosink_class = (GstVideoSinkClass *) klass; + + gobject_class->set_property = gst_qt_sink_set_property; + gobject_class->get_property = gst_qt_sink_get_property; + + gst_element_class_set_metadata (gstelement_class, "Qt Video Sink", + "Sink/Video", "A video sink that renders to a QQuickItem", + "Matthew Waters <matthew@centricular.com>"); + + g_object_class_install_property (gobject_class, PROP_WIDGET, + g_param_spec_pointer ("widget", "QQuickItem", + "The QQuickItem to place in the object hierarchy", + (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); + + g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO, + g_param_spec_boolean ("force-aspect-ratio", + "Force aspect ratio", + "When enabled, scaling will respect original aspect ratio", + DEFAULT_FORCE_ASPECT_RATIO, + (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); + + g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO, + gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio", + "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D, + G_MAXINT, 1, 1, 1, + (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); + + gst_element_class_add_static_pad_template (gstelement_class, &gst_qt_sink_template); + + gobject_class->finalize = gst_qt_sink_finalize; + + gstelement_class->change_state = gst_qt_sink_change_state; + gstbasesink_class->query = gst_qt_sink_query; + gstbasesink_class->set_caps = gst_qt_sink_set_caps; + gstbasesink_class->get_times = gst_qt_sink_get_times; + gstbasesink_class->propose_allocation = gst_qt_sink_propose_allocation; + gstbasesink_class->stop = gst_qt_sink_stop; + + gstvideosink_class->show_frame = gst_qt_sink_show_frame; +} + +static void +gst_qt_sink_init (GstQtSink * qt_sink) +{ + qt_sink->widget = QSharedPointer<QtGLVideoItemInterface>(); +} + +static void +gst_qt_sink_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstQtSink *qt_sink = GST_QT_SINK (object); + + switch (prop_id) { + case PROP_WIDGET: { + QtGLVideoItem *qt_item = static_cast<QtGLVideoItem *> (g_value_get_pointer (value)); + if (qt_item) + qt_sink->widget = qt_item->getInterface(); + else + qt_sink->widget.clear(); + break; + } + case PROP_FORCE_ASPECT_RATIO: + g_return_if_fail (qt_sink->widget); + qt_sink->widget->setForceAspectRatio (g_value_get_boolean (value)); + break; + case PROP_PIXEL_ASPECT_RATIO: + g_return_if_fail (qt_sink->widget); + qt_sink->widget->setDAR (gst_value_get_fraction_numerator (value), + gst_value_get_fraction_denominator (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +_reset (GstQtSink * qt_sink) +{ + if (qt_sink->display) { + gst_object_unref (qt_sink->display); + qt_sink->display = NULL; + } + + if (qt_sink->context) { + gst_object_unref (qt_sink->context); + qt_sink->context = NULL; + } + + if (qt_sink->qt_context) { + gst_object_unref (qt_sink->qt_context); + qt_sink->qt_context = NULL; + } +} + +static void +gst_qt_sink_finalize (GObject * object) +{ + GstQtSink *qt_sink = GST_QT_SINK (object); + + _reset (qt_sink); + + qt_sink->widget.clear(); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +gst_qt_sink_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstQtSink *qt_sink = GST_QT_SINK (object); + + switch (prop_id) { + case PROP_WIDGET: + /* This is not really safe - the app needs to be + * sure the widget is going to be kept alive or + * this can crash */ + if (qt_sink->widget) + g_value_set_pointer (value, qt_sink->widget->videoItem()); + else + g_value_set_pointer (value, NULL); + break; + case PROP_FORCE_ASPECT_RATIO: + if (qt_sink->widget) + g_value_set_boolean (value, qt_sink->widget->getForceAspectRatio ()); + else + g_value_set_boolean (value, DEFAULT_FORCE_ASPECT_RATIO); + break; + case PROP_PIXEL_ASPECT_RATIO: + if (qt_sink->widget) { + gint num, den; + qt_sink->widget->getDAR (&num, &den); + gst_value_set_fraction (value, num, den); + } else { + gst_value_set_fraction (value, DEFAULT_PAR_N, DEFAULT_PAR_D); + } + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static gboolean +gst_qt_sink_query (GstBaseSink * bsink, GstQuery * query) +{ + GstQtSink *qt_sink = GST_QT_SINK (bsink); + gboolean res = FALSE; + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_CONTEXT: + { + if (gst_gl_handle_context_query ((GstElement *) qt_sink, query, + qt_sink->display, qt_sink->context, qt_sink->qt_context)) + return TRUE; + + /* fallthrough */ + } + default: + res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query); + break; + } + + return res; +} + +static gboolean +gst_qt_sink_stop (GstBaseSink * bsink) +{ + return TRUE; +} + +static GstStateChangeReturn +gst_qt_sink_change_state (GstElement * element, GstStateChange transition) +{ + GstQtSink *qt_sink = GST_QT_SINK (element); + GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; + QGuiApplication *app; + + GST_DEBUG ("changing state: %s => %s", + gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)), + gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition))); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + app = static_cast<QGuiApplication *> (QCoreApplication::instance ()); + if (!app) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Failed to connect to Qt"), + ("%s", "Could not retrieve QGuiApplication instance")); + return GST_STATE_CHANGE_FAILURE; + } + + if (!qt_sink->widget) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Required property \'widget\' not set"), + (NULL)); + return GST_STATE_CHANGE_FAILURE; + } + + if (!qt_sink->widget->initWinSys()) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Could not initialize window system"), + (NULL)); + return GST_STATE_CHANGE_FAILURE; + } + + qt_sink->display = qt_sink->widget->getDisplay(); + qt_sink->context = qt_sink->widget->getContext(); + qt_sink->qt_context = qt_sink->widget->getQtContext(); + + if (!qt_sink->display || !qt_sink->context || !qt_sink->qt_context) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Could not retrieve window system OpenGL configuration"), + (NULL)); + return GST_STATE_CHANGE_FAILURE; + } + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + break; + case GST_STATE_CHANGE_PAUSED_TO_PLAYING: + break; + default: + break; + } + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + if (ret == GST_STATE_CHANGE_FAILURE) + return ret; + + switch (transition) { + case GST_STATE_CHANGE_PLAYING_TO_PAUSED: + break; + case GST_STATE_CHANGE_PAUSED_TO_READY: + if (qt_sink->widget) + qt_sink->widget->setBuffer(NULL); + break; + case GST_STATE_CHANGE_READY_TO_NULL: + break; + default: + break; + } + + return ret; +} + +static void +gst_qt_sink_get_times (GstBaseSink * bsink, GstBuffer * buf, + GstClockTime * start, GstClockTime * end) +{ + GstQtSink *qt_sink; + + qt_sink = GST_QT_SINK (bsink); + + if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) { + *start = GST_BUFFER_TIMESTAMP (buf); + if (GST_BUFFER_DURATION_IS_VALID (buf)) + *end = *start + GST_BUFFER_DURATION (buf); + else { + if (GST_VIDEO_INFO_FPS_N (&qt_sink->v_info) > 0) { + *end = *start + + gst_util_uint64_scale_int (GST_SECOND, + GST_VIDEO_INFO_FPS_D (&qt_sink->v_info), + GST_VIDEO_INFO_FPS_N (&qt_sink->v_info)); + } + } + } +} + +gboolean +gst_qt_sink_set_caps (GstBaseSink * bsink, GstCaps * caps) +{ + GstQtSink *qt_sink = GST_QT_SINK (bsink); + + GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps); + + if (!gst_video_info_from_caps (&qt_sink->v_info, caps)) + return FALSE; + + if (!qt_sink->widget) + return FALSE; + + return qt_sink->widget->setCaps(caps); +} + +static GstFlowReturn +gst_qt_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf) +{ + GstQtSink *qt_sink; + + GST_TRACE ("rendering buffer:%p", buf); + + qt_sink = GST_QT_SINK (vsink); + + if (qt_sink->widget) + qt_sink->widget->setBuffer(buf); + + return GST_FLOW_OK; +} + +static gboolean +gst_qt_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query) +{ + GstQtSink *qt_sink = GST_QT_SINK (bsink); + GstBufferPool *pool; + GstStructure *config; + GstCaps *caps; + guint size; + gboolean need_pool; + + if (!qt_sink->display || !qt_sink->context) + return FALSE; + + gst_query_parse_allocation (query, &caps, &need_pool); + + if (caps == NULL) + goto no_caps; + + /* FIXME re-using buffer pool breaks renegotiation */ + if ((pool = qt_sink->pool)) + gst_object_ref (pool); + + if (pool != NULL) { + GstCaps *pcaps; + + /* we had a pool, check caps */ + GST_DEBUG_OBJECT (qt_sink, "check existing pool caps"); + config = gst_buffer_pool_get_config (pool); + gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL); + + if (!gst_caps_is_equal (caps, pcaps)) { + GST_DEBUG_OBJECT (qt_sink, "pool has different caps"); + /* different caps, we can't use this pool */ + gst_object_unref (pool); + pool = NULL; + } + gst_structure_free (config); + } else { + GstVideoInfo info; + + if (!gst_video_info_from_caps (&info, caps)) + goto invalid_caps; + + /* the normal size of a frame */ + size = info.size; + } + + if (pool == NULL && need_pool) { + + GST_DEBUG_OBJECT (qt_sink, "create new pool"); + pool = gst_gl_buffer_pool_new (qt_sink->context); + + config = gst_buffer_pool_get_config (pool); + gst_buffer_pool_config_set_params (config, caps, size, 0, 0); + if (!gst_buffer_pool_set_config (pool, config)) + goto config_failed; + } + + /* we need at least 2 buffer because we hold on to the last one */ + gst_query_add_allocation_pool (query, pool, size, 2, 0); + if (pool) + gst_object_unref (pool); + + /* we also support various metadata */ + gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0); + + if (qt_sink->context->gl_vtable->FenceSync) + gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0); + + return TRUE; + + /* ERRORS */ +no_caps: + { + GST_DEBUG_OBJECT (bsink, "no caps specified"); + return FALSE; + } +invalid_caps: + { + GST_DEBUG_OBJECT (bsink, "invalid caps specified"); + return FALSE; + } +config_failed: + { + GST_DEBUG_OBJECT (bsink, "failed setting config"); + return FALSE; + } +} diff --git a/ext/qt/gstqtsink.h b/ext/qt/gstqtsink.h new file mode 100644 index 000000000..3ee20b7d5 --- /dev/null +++ b/ext/qt/gstqtsink.h @@ -0,0 +1,79 @@ +/* + * 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; + + GstVideoInfo v_info; + GstBufferPool *pool; + + GstGLDisplay *display; + GstGLContext *context; + GstGLContext *qt_context; + + QSharedPointer<QtGLVideoItemInterface> widget; +}; + +/** + * 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__ */ diff --git a/ext/qt/gstqtsrc.cc b/ext/qt/gstqtsrc.cc new file mode 100644 index 000000000..1cf9592b5 --- /dev/null +++ b/ext/qt/gstqtsrc.cc @@ -0,0 +1,548 @@ +/* + * GStreamer + * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved. + * + * 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. + */ + +/** + * SECTION:qmlglsrc + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstqtsrc.h" +#include <QtGui/QGuiApplication> + +#define GST_CAT_DEFAULT gst_debug_qt_gl_src +GST_DEBUG_CATEGORY (GST_CAT_DEFAULT); + +#define DEFAULT_IS_LIVE TRUE + +static void gst_qt_src_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_qt_src_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); + +static void gst_qt_src_finalize (GObject * object); + +static gboolean gst_qt_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps); +static GstCaps *gst_qt_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter); +static gboolean gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query); + +static gboolean gst_qt_src_decide_allocation (GstBaseSrc * bsrc, + GstQuery * query); +static GstFlowReturn gst_qt_src_fill (GstPushSrc * psrc, GstBuffer * buffer); +static GstStateChangeReturn gst_qt_src_change_state (GstElement * element, + GstStateChange transition); +static gboolean gst_qt_src_start (GstBaseSrc * basesrc); +static gboolean gst_qt_src_stop (GstBaseSrc * basesrc); + +static GstStaticPadTemplate gst_qt_src_template = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), " + "format = (string) RGBA, " + "width = " GST_VIDEO_SIZE_RANGE ", " + "height = " GST_VIDEO_SIZE_RANGE ", " + "framerate = " GST_VIDEO_FPS_RANGE ", " + "texture-target = (string) 2D")); + +enum +{ + ARG_0, + PROP_WINDOW, + PROP_DEFAULT_FBO +}; + +#define gst_qt_src_parent_class parent_class +G_DEFINE_TYPE_WITH_CODE (GstQtSrc, gst_qt_src, + GST_TYPE_PUSH_SRC, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, + "qtsrc", 0, "Qt Video Src")); + +static const gfloat vertical_flip_matrix[] = { + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, -1.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f, +}; + +static void +gst_qt_src_class_init (GstQtSrcClass * klass) +{ + GObjectClass *gobject_class = (GObjectClass *) klass; + GstElementClass *gstelement_class = (GstElementClass *) klass; + GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass; + GstPushSrcClass *gstpushsrc_class = (GstPushSrcClass *) klass; + + gobject_class->set_property = gst_qt_src_set_property; + gobject_class->get_property = gst_qt_src_get_property; + gobject_class->finalize = gst_qt_src_finalize; + + gst_element_class_set_metadata (gstelement_class, "Qt Video Source", + "Source/Video", "A video src that captures a window from a QML view", + "Multimedia Team <shmmmw@freescale.com>"); + + g_object_class_install_property (gobject_class, PROP_WINDOW, + g_param_spec_pointer ("window", "QQuickWindow", + "The QQuickWindow to place in the object hierarchy", + (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); + + g_object_class_install_property (gobject_class, PROP_DEFAULT_FBO, + g_param_spec_boolean ("use-default-fbo", + "Whether to use default FBO", + "When set it will not create a new FBO for the QML render thread", + FALSE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); + + gst_element_class_add_pad_template (gstelement_class, + gst_static_pad_template_get (&gst_qt_src_template)); + + gstelement_class->change_state = gst_qt_src_change_state; + gstbasesrc_class->set_caps = gst_qt_src_setcaps; + gstbasesrc_class->get_caps = gst_qt_src_get_caps; + gstbasesrc_class->query = gst_qt_src_query; + gstbasesrc_class->start = gst_qt_src_start; + gstbasesrc_class->stop = gst_qt_src_stop; + gstbasesrc_class->decide_allocation = gst_qt_src_decide_allocation; + + gstpushsrc_class->fill = gst_qt_src_fill; +} + +static void +gst_qt_src_init (GstQtSrc * src) +{ + gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME); + gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE); + src->default_fbo = FALSE; + src->pending_image_orientation = TRUE; +} + +static void +gst_qt_src_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec) +{ + GstQtSrc *qt_src = GST_QT_SRC (object); + + switch (prop_id) { + case PROP_WINDOW:{ + qt_src->qwindow = + static_cast < QQuickWindow * >(g_value_get_pointer (value)); + + if (qt_src->window) { + delete qt_src->window; + qt_src->window = NULL; + } + + if (qt_src->qwindow) + qt_src->window = new QtGLWindow (NULL, qt_src->qwindow); + + break; + } + case PROP_DEFAULT_FBO: + qt_src->default_fbo = g_value_get_boolean (value); + if (qt_src->window) + qt_window_use_default_fbo (qt_src->window, qt_src->default_fbo); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_qt_src_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec) +{ + GstQtSrc *qt_src = GST_QT_SRC (object); + + switch (prop_id) { + case PROP_WINDOW: + g_value_set_pointer (value, qt_src->qwindow); + break; + case PROP_DEFAULT_FBO: + g_value_set_boolean (value, qt_src->default_fbo); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_qt_src_finalize (GObject * object) +{ + GstQtSrc *qt_src = GST_QT_SRC (object); + + GST_DEBUG ("qmlglsrc finalize"); + if (qt_src->context) + gst_object_unref (qt_src->context); + qt_src->context = NULL; + + if (qt_src->qt_context) + gst_object_unref (qt_src->qt_context); + qt_src->qt_context = NULL; + + if (qt_src->display) + gst_object_unref (qt_src->display); + qt_src->display = NULL; + + if (qt_src->window) + delete qt_src->window; + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static gboolean +gst_qt_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps) +{ + GstQtSrc *qt_src = GST_QT_SRC (bsrc); + + GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps); + + if (!gst_video_info_from_caps (&qt_src->v_info, caps)) + return FALSE; + + if (!qt_window_set_caps (qt_src->window, caps)) + return FALSE; + + return TRUE; +} + +static GstCaps * +gst_qt_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter) +{ + GstCaps *caps = NULL, *temp = NULL; + GstPadTemplate *pad_template; + GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc); + GstQtSrc *qt_src = GST_QT_SRC (bsrc); + guint i; + gint width, height; + + if (qt_src->window) { + qt_src->window->getGeometry (&width, &height); + } + + pad_template = + gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src"); + if (pad_template != NULL) + caps = gst_pad_template_get_caps (pad_template); + + if (qt_src->window) { + temp = gst_caps_copy (caps); + guint n_caps = gst_caps_get_size (caps); + + for (i = 0; i < n_caps; i++) { + GstStructure *s = gst_caps_get_structure (temp, i); + gst_structure_set (s, "width", G_TYPE_INT, width, NULL); + gst_structure_set (s, "height", G_TYPE_INT, height, NULL); + /* because the framerate is unknown */ + gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL); + gst_structure_set (s, "pixel-aspect-ratio", + GST_TYPE_FRACTION, 1, 1, NULL); + } + + gst_caps_unref (caps); + caps = temp; + } + + if (filter) { + GstCaps *intersection; + + intersection = + gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST); + gst_caps_unref (caps); + caps = intersection; + } + + return caps; +} + +static gboolean +gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query) +{ + GstQtSrc *qt_src = GST_QT_SRC (bsrc); + gboolean res = FALSE; + + switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_CONTEXT: + { + if (!qt_window_is_scenegraph_initialized (qt_src->window)) + return FALSE; + + if (!qt_src->display && !qt_src->qt_context) { + qt_src->display = qt_window_get_display (qt_src->window); + qt_src->qt_context = qt_window_get_qt_context (qt_src->window); + } + + if (gst_gl_handle_context_query ((GstElement *) qt_src, query, + qt_src->display, qt_src->context, qt_src->qt_context)) + return TRUE; + + /* fallthrough */ + } + default: + res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query); + break; + } + + return res; +} + +static gboolean +_find_local_gl_context (GstQtSrc * qt_src) +{ + if (gst_gl_query_local_gl_context (GST_ELEMENT (qt_src), GST_PAD_SRC, + &qt_src->context)) + return TRUE; + return FALSE; +} + +static gboolean +gst_qt_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query) +{ + GstBufferPool *pool = NULL; + GstStructure *config; + GstCaps *caps; + guint min, max, size, n, i; + gboolean update_pool, update_allocator; + GstAllocator *allocator; + GstAllocationParams params; + GstGLVideoAllocationParams *glparams; + GstVideoInfo vinfo; + GstQtSrc *qt_src = GST_QT_SRC (bsrc); + + if (gst_query_find_allocation_meta (query, + GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, NULL)) { + qt_src->downstream_supports_affine_meta = TRUE; + } else { + qt_src->downstream_supports_affine_meta = FALSE; + } + + gst_query_parse_allocation (query, &caps, NULL); + if (!caps) + return FALSE; + + gst_video_info_from_caps (&vinfo, caps); + + n = gst_query_get_n_allocation_pools (query); + if (n > 0) { + update_pool = TRUE; + for (i = 0; i < n; i++) { + gst_query_parse_nth_allocation_pool (query, i, &pool, &size, &min, &max); + + if (!pool || !GST_IS_GL_BUFFER_POOL (pool)) { + if (pool) + gst_object_unref (pool); + pool = NULL; + } + } + } + + if (!pool) { + size = vinfo.size; + min = max = 0; + update_pool = FALSE; + } + + if (!qt_src->context && !_find_local_gl_context (qt_src)) + return FALSE; + + if (!pool) { + if (!qt_src->context || !GST_IS_GL_CONTEXT (qt_src->context)) + return FALSE; + + pool = gst_gl_buffer_pool_new (qt_src->context); + GST_INFO_OBJECT (qt_src, "No pool, create one ourself %p", pool); + } + + config = gst_buffer_pool_get_config (pool); + + gst_buffer_pool_config_set_params (config, caps, size, min, max); + gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META); + if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL)) + gst_buffer_pool_config_add_option (config, + GST_BUFFER_POOL_OPTION_GL_SYNC_META); + + if (gst_query_get_n_allocation_params (query) > 0) { + gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms); + gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); + GST_INFO_OBJECT (qt_src, "got allocator %p", allocator); + update_allocator = TRUE; + } else { + allocator = NULL; + gst_allocation_params_init (¶ms); + update_allocator = FALSE; + } + + glparams = + gst_gl_video_allocation_params_new (qt_src->context, ¶ms, &vinfo, 0, + NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA); + gst_buffer_pool_config_set_gl_allocation_params (config, + (GstGLAllocationParams *) glparams); + gst_gl_allocation_params_free ((GstGLAllocationParams *) glparams); + + if (!gst_buffer_pool_set_config (pool, config)) + GST_WARNING_OBJECT (qt_src, "Failed to set buffer pool config"); + + if (update_allocator) + gst_query_set_nth_allocation_param (query, 0, allocator, ¶ms); + else + gst_query_add_allocation_param (query, allocator, ¶ms); + if (allocator) + gst_object_unref (allocator); + + if (update_pool) + gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max); + else + gst_query_add_allocation_pool (query, pool, size, min, max); + gst_object_unref (pool); + + GST_INFO_OBJECT (qt_src, "successfully decide_allocation"); + return TRUE; +} + +static GstFlowReturn +gst_qt_src_fill (GstPushSrc * psrc, GstBuffer * buffer) +{ + GstQtSrc *qt_src = GST_QT_SRC (psrc); + + GST_DEBUG_OBJECT (qt_src, "setting buffer %p", buffer); + + if (!qt_window_set_buffer (qt_src->window, buffer)) { + GST_ERROR_OBJECT (qt_src, "failed to fill buffer %p", buffer); + return GST_FLOW_ERROR; + } + + if (!qt_src->downstream_supports_affine_meta) { + if (qt_src->pending_image_orientation) { + /* let downstream know the image orientation is vertical filp */ + GstTagList *image_orientation_tag = + gst_tag_list_new (GST_TAG_IMAGE_ORIENTATION, "flip-rotate-180", NULL); + + gst_pad_push_event (GST_BASE_SRC_PAD (psrc), + gst_event_new_tag (image_orientation_tag)); + + qt_src->pending_image_orientation = FALSE; + } + } else { + GstVideoAffineTransformationMeta *trans_meta; + trans_meta = gst_buffer_add_video_affine_transformation_meta (buffer); + gst_video_affine_transformation_meta_apply_matrix (trans_meta, + vertical_flip_matrix); + } + + GST_DEBUG_OBJECT (qt_src, "buffer fill done %p", buffer); + + return GST_FLOW_OK; +} + +static GstStateChangeReturn +gst_qt_src_change_state (GstElement * element, GstStateChange transition) +{ + GstQtSrc *qt_src = GST_QT_SRC (element); + GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; + QGuiApplication *app; + + GST_DEBUG ("changing state: %s => %s", + gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)), + gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition))); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + app = static_cast < QGuiApplication * >(QCoreApplication::instance ()); + if (!app) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Failed to connect to Qt"), + ("%s", "Could not retrieve QGuiApplication instance")); + return GST_STATE_CHANGE_FAILURE; + } + + if (!qt_src->window) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Required property \'window\' not set"), (NULL)); + return GST_STATE_CHANGE_FAILURE; + } + + if (!qt_window_is_scenegraph_initialized (qt_src->window)) { + GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND, + ("%s", "Could not initialize window system"), (NULL)); + return GST_STATE_CHANGE_FAILURE; + } + + qt_window_use_default_fbo (qt_src->window, qt_src->default_fbo); + + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + break; + case GST_STATE_CHANGE_PAUSED_TO_PLAYING: + break; + default: + break; + } + + ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); + if (ret == GST_STATE_CHANGE_FAILURE) + return ret; + + switch (transition) { + case GST_STATE_CHANGE_PLAYING_TO_PAUSED: + break; + case GST_STATE_CHANGE_PAUSED_TO_READY: + break; + case GST_STATE_CHANGE_READY_TO_NULL: + break; + default: + break; + } + + return ret; +} + +static gboolean +gst_qt_src_start (GstBaseSrc * basesrc) +{ + GstQtSrc *qt_src = GST_QT_SRC (basesrc); + + /* already has get OpenGL configuration from qt */ + if (qt_src->display && qt_src->qt_context) + return TRUE; + + if (!qt_window_is_scenegraph_initialized (qt_src->window)) + return FALSE; + + qt_src->display = qt_window_get_display (qt_src->window); + qt_src->qt_context = qt_window_get_qt_context (qt_src->window); + + if (!qt_src->display || !qt_src->qt_context) { + GST_ERROR_OBJECT (qt_src, + "Could not retrieve window system OpenGL configuration"); + return FALSE; + } + + GST_DEBUG_OBJECT (qt_src, "Got qt display %p and qt gl context %p", + qt_src->display, qt_src->qt_context); + return TRUE; +} + +static gboolean +gst_qt_src_stop (GstBaseSrc * basesrc) +{ + return TRUE; +} diff --git a/ext/qt/gstqtsrc.h b/ext/qt/gstqtsrc.h new file mode 100644 index 000000000..d84544224 --- /dev/null +++ b/ext/qt/gstqtsrc.h @@ -0,0 +1,80 @@ +/* + * GStreamer + * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved. + * + * 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_SRC_H__ +#define __GST_QT_SRC_H__ + +#include <gst/gst.h> +#include <gst/base/gstpushsrc.h> +#include <gst/video/video.h> +#include <gst/gl/gl.h> +#include "qtwindow.h" + +typedef struct _GstQtSrc GstQtSrc; +typedef struct _GstQtSrcClass GstQtSrcClass; + +G_BEGIN_DECLS + +GType gst_qt_src_get_type (void); +#define GST_TYPE_QT_SRC (gst_qt_src_get_type()) +#define GST_QT_SRC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QT_SRC,GstQtSrc)) +#define GST_QT_SRC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_QT_SRC,GstQtSrcClass)) +#define GST_IS_QT_SRC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QT_SRC)) +#define GST_IS_QT_SRC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QT_SRC)) +#define GST_QT_SRC_CAST(obj) ((GstQtSrc*)(obj)) + +/** + * GstQtSrc: + * + * Opaque #GstQtSrc object + */ +struct _GstQtSrc +{ + /* <private> */ + GstPushSrc parent; + + QQuickWindow *qwindow; + QtGLWindow *window; + + GstVideoInfo v_info; + + GstGLDisplay *display; + GstGLContext *context; + GstGLContext *qt_context; + + gboolean default_fbo; + gboolean downstream_supports_affine_meta; + gboolean pending_image_orientation; +}; + +/** + * GstQtSrcClass: + * + * The #GstQtSrcClass struct only contains private data + */ +struct _GstQtSrcClass +{ + /* <private> */ + GstPushSrcClass object_class; +}; + +G_END_DECLS + +#endif /* __GST_QT_SRC_H__ */ diff --git a/ext/qt/qtitem.cc b/ext/qt/qtitem.cc new file mode 100644 index 000000000..65b883eab --- /dev/null +++ b/ext/qt/qtitem.cc @@ -0,0 +1,546 @@ +/* + * 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 "qtitem.h" +#include "gstqsgtexture.h" +#include "gstqtglutility.h" + +#include <QtCore/QRunnable> +#include <QtCore/QMutexLocker> +#include <QtGui/QGuiApplication> +#include <QtQuick/QQuickWindow> +#include <QtQuick/QSGSimpleTextureNode> + +/** + * SECTION:gtkgstglwidget + * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers + * @see_also: #GtkGLArea, #GstBuffer + * + * #QtGLVideoItem is an #QQuickItem that renders GStreamer video buffers. + */ + +#define GST_CAT_DEFAULT qt_item_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +#define DEFAULT_FORCE_ASPECT_RATIO TRUE +#define DEFAULT_PAR_N 0 +#define DEFAULT_PAR_D 1 + +enum +{ + PROP_0, + PROP_FORCE_ASPECT_RATIO, + PROP_PIXEL_ASPECT_RATIO, +}; + +struct _QtGLVideoItemPrivate +{ + GMutex lock; + + /* properties */ + gboolean force_aspect_ratio; + gint par_n, par_d; + + gint display_width; + gint display_height; + + gboolean negotiated; + GstBuffer *buffer; + GstCaps *caps; + GstVideoInfo v_info; + + gboolean initted; + GstGLDisplay *display; + QOpenGLContext *qt_context; + GstGLContext *other_context; + GstGLContext *context; +}; + +class InitializeSceneGraph : public QRunnable +{ +public: + InitializeSceneGraph(QtGLVideoItem *item); + void run(); + +private: + QtGLVideoItem *item_; +}; + +InitializeSceneGraph::InitializeSceneGraph(QtGLVideoItem *item) : + item_(item) +{ +} + +void InitializeSceneGraph::run() +{ + item_->onSceneGraphInitialized(); +} + +QtGLVideoItem::QtGLVideoItem() +{ + static volatile gsize _debug; + + if (g_once_init_enter (&_debug)) { + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget"); + g_once_init_leave (&_debug, 1); + } + this->m_openGlContextInitialized = false; + this->setFlag (QQuickItem::ItemHasContents, true); + + this->priv = g_new0 (QtGLVideoItemPrivate, 1); + + this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO; + this->priv->par_n = DEFAULT_PAR_N; + this->priv->par_d = DEFAULT_PAR_D; + + g_mutex_init (&this->priv->lock); + + this->priv->display = gst_qt_get_gl_display(); + + connect(this, SIGNAL(windowChanged(QQuickWindow*)), this, + SLOT(handleWindowChanged(QQuickWindow*))); + + this->proxy = QSharedPointer<QtGLVideoItemInterface>(new QtGLVideoItemInterface(this)); + + GST_DEBUG ("%p init Qt Video Item", this); +} + +QtGLVideoItem::~QtGLVideoItem() +{ + /* Before destroying the priv info, make sure + * no qmlglsink's will call in again, and that + * any ongoing calls are done by invalidating the proxy + * pointer */ + GST_INFO ("Destroying QtGLVideoItem and invalidating the proxy"); + proxy->invalidateRef(); + proxy.clear(); + + g_mutex_clear (&this->priv->lock); + if (this->priv->context) + gst_object_unref(this->priv->context); + if (this->priv->other_context) + gst_object_unref(this->priv->other_context); + if (this->priv->display) + gst_object_unref(this->priv->display); + g_free (this->priv); + this->priv = NULL; +} + +void +QtGLVideoItem::setDAR(gint num, gint den) +{ + this->priv->par_n = num; + this->priv->par_d = den; +} + +void +QtGLVideoItem::getDAR(gint * num, gint * den) +{ + if (num) + *num = this->priv->par_n; + if (den) + *den = this->priv->par_d; +} + +void +QtGLVideoItem::setForceAspectRatio(bool force_aspect_ratio) +{ + this->priv->force_aspect_ratio = !!force_aspect_ratio; +} + +bool +QtGLVideoItem::getForceAspectRatio() +{ + return this->priv->force_aspect_ratio; +} + +bool +QtGLVideoItem::itemInitialized() +{ + return m_openGlContextInitialized; +} + +QSGNode * +QtGLVideoItem::updatePaintNode(QSGNode * oldNode, + UpdatePaintNodeData * updatePaintNodeData) +{ + if (!m_openGlContextInitialized) { + return oldNode; + } + + QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode); + GstVideoRectangle src, dst, result; + GstQSGTexture *tex; + + g_mutex_lock (&this->priv->lock); + gst_gl_context_activate (this->priv->other_context, TRUE); + + GST_TRACE ("%p updatePaintNode", this); + + if (!this->priv->caps) { + g_mutex_unlock (&this->priv->lock); + return NULL; + } + + if (!texNode) { + texNode = new QSGSimpleTextureNode (); + texNode->setOwnsTexture (true); + texNode->setTexture (new GstQSGTexture ()); + } + + tex = static_cast<GstQSGTexture *> (texNode->texture()); + tex->setCaps (this->priv->caps); + tex->setBuffer (this->priv->buffer); + texNode->markDirty(QSGNode::DirtyMaterial); + + if (this->priv->force_aspect_ratio) { + src.w = this->priv->display_width; + src.h = this->priv->display_height; + + dst.x = boundingRect().x(); + dst.y = boundingRect().y(); + dst.w = boundingRect().width(); + dst.h = boundingRect().height(); + + gst_video_sink_center_rect (src, dst, &result, TRUE); + } else { + result.x = boundingRect().x(); + result.y = boundingRect().y(); + result.w = boundingRect().width(); + result.h = boundingRect().height(); + } + + texNode->setRect (QRectF (result.x, result.y, result.w, result.h)); + + gst_gl_context_activate (this->priv->other_context, FALSE); + g_mutex_unlock (&this->priv->lock); + + return texNode; +} + +static void +_reset (QtGLVideoItem * qt_item) +{ + gst_buffer_replace (&qt_item->priv->buffer, NULL); + + gst_caps_replace (&qt_item->priv->caps, NULL); + + qt_item->priv->negotiated = FALSE; + qt_item->priv->initted = FALSE; +} + +void +QtGLVideoItemInterface::setBuffer (GstBuffer * buffer) +{ + QMutexLocker locker(&lock); + + if (qt_item == NULL) + return; + + if (!qt_item->priv->negotiated) { + GST_WARNING ("Got buffer on unnegotiated QtGLVideoItem. Dropping"); + return; + } + + g_mutex_lock (&qt_item->priv->lock); + + gst_buffer_replace (&qt_item->priv->buffer, buffer); + + QMetaObject::invokeMethod(qt_item, "update", Qt::QueuedConnection); + + g_mutex_unlock (&qt_item->priv->lock); +} + +void +QtGLVideoItem::onSceneGraphInitialized () +{ + GST_DEBUG ("scene graph initialization with Qt GL context %p", + this->window()->openglContext ()); + + if (this->priv->qt_context == this->window()->openglContext ()) + return; + + this->priv->qt_context = this->window()->openglContext (); + if (this->priv->qt_context == NULL) { + g_assert_not_reached (); + return; + } + + m_openGlContextInitialized = gst_qt_get_gl_wrapcontext (this->priv->display, + &this->priv->other_context, &this->priv->context); + + GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this, + this->priv->other_context); + + emit itemInitializedChanged(); +} + +void +QtGLVideoItem::onSceneGraphInvalidated () +{ + GST_FIXME ("%p scene graph invalidated", this); +} + +gboolean +QtGLVideoItemInterface::initWinSys () +{ + QMutexLocker locker(&lock); + + GError *error = NULL; + + if (qt_item == NULL) + return FALSE; + + g_mutex_lock (&qt_item->priv->lock); + + if (qt_item->priv->display && qt_item->priv->qt_context + && qt_item->priv->other_context && qt_item->priv->context) { + /* already have the necessary state */ + g_mutex_unlock (&qt_item->priv->lock); + return TRUE; + } + + if (!GST_IS_GL_DISPLAY (qt_item->priv->display)) { + GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT, + qt_item, qt_item->priv->display); + g_mutex_unlock (&qt_item->priv->lock); + return FALSE; + } + + if (!GST_IS_GL_CONTEXT (qt_item->priv->other_context)) { + GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, qt_item, + qt_item->priv->other_context); + g_mutex_unlock (&qt_item->priv->lock); + return FALSE; + } + + qt_item->priv->context = gst_gl_context_new (qt_item->priv->display); + + if (!qt_item->priv->context) { + g_mutex_unlock (&qt_item->priv->lock); + return FALSE; + } + + if (!gst_gl_context_create (qt_item->priv->context, qt_item->priv->other_context, + &error)) { + GST_ERROR ("%s", error->message); + g_mutex_unlock (&qt_item->priv->lock); + return FALSE; + } + + g_mutex_unlock (&qt_item->priv->lock); + return TRUE; +} + +void +QtGLVideoItem::handleWindowChanged(QQuickWindow *win) +{ + if (win) { + if (win->isSceneGraphInitialized()) + win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage); + else + connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection); + + connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection); + } else { + this->priv->qt_context = NULL; + } +} + +static gboolean +_calculate_par (QtGLVideoItem * widget, GstVideoInfo * info) +{ + gboolean ok; + gint width, height; + gint par_n, par_d; + gint display_par_n, display_par_d; + guint display_ratio_num, display_ratio_den; + + width = GST_VIDEO_INFO_WIDTH (info); + height = GST_VIDEO_INFO_HEIGHT (info); + + par_n = GST_VIDEO_INFO_PAR_N (info); + par_d = GST_VIDEO_INFO_PAR_D (info); + + if (!par_n) + par_n = 1; + + /* get display's PAR */ + if (widget->priv->par_n != 0 && widget->priv->par_d != 0) { + display_par_n = widget->priv->par_n; + display_par_d = widget->priv->par_d; + } else { + display_par_n = 1; + display_par_d = 1; + } + + ok = gst_video_calculate_display_ratio (&display_ratio_num, + &display_ratio_den, width, height, par_n, par_d, display_par_n, + display_par_d); + + if (!ok) + return FALSE; + + GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d); + + if (height % display_ratio_den == 0) { + GST_DEBUG ("keeping video height"); + widget->priv->display_width = (guint) + gst_util_uint64_scale_int (height, display_ratio_num, + display_ratio_den); + widget->priv->display_height = height; + } else if (width % display_ratio_num == 0) { + GST_DEBUG ("keeping video width"); + widget->priv->display_width = width; + widget->priv->display_height = (guint) + gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num); + } else { + GST_DEBUG ("approximating while keeping video height"); + widget->priv->display_width = (guint) + gst_util_uint64_scale_int (height, display_ratio_num, + display_ratio_den); + widget->priv->display_height = height; + } + GST_DEBUG ("scaling to %dx%d", widget->priv->display_width, + widget->priv->display_height); + + return TRUE; +} + +gboolean +QtGLVideoItemInterface::setCaps (GstCaps * caps) +{ + QMutexLocker locker(&lock); + GstVideoInfo v_info; + + g_return_val_if_fail (GST_IS_CAPS (caps), FALSE); + g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE); + + if (qt_item == NULL) + return FALSE; + + if (qt_item->priv->caps && gst_caps_is_equal_fixed (qt_item->priv->caps, caps)) + return TRUE; + + if (!gst_video_info_from_caps (&v_info, caps)) + return FALSE; + + g_mutex_lock (&qt_item->priv->lock); + + _reset (qt_item); + + gst_caps_replace (&qt_item->priv->caps, caps); + + if (!_calculate_par (qt_item, &v_info)) { + g_mutex_unlock (&qt_item->priv->lock); + return FALSE; + } + + qt_item->priv->v_info = v_info; + qt_item->priv->negotiated = TRUE; + + g_mutex_unlock (&qt_item->priv->lock); + + return TRUE; +} + +GstGLContext * +QtGLVideoItemInterface::getQtContext () +{ + QMutexLocker locker(&lock); + + if (!qt_item || !qt_item->priv->other_context) + return NULL; + + return (GstGLContext *) gst_object_ref (qt_item->priv->other_context); +} + +GstGLContext * +QtGLVideoItemInterface::getContext () +{ + QMutexLocker locker(&lock); + + if (!qt_item || !qt_item->priv->context) + return NULL; + + return (GstGLContext *) gst_object_ref (qt_item->priv->context); +} + +GstGLDisplay * +QtGLVideoItemInterface::getDisplay() +{ + QMutexLocker locker(&lock); + + if (!qt_item || !qt_item->priv->display) + return NULL; + + return (GstGLDisplay *) gst_object_ref (qt_item->priv->display); +} + +void +QtGLVideoItemInterface::setDAR(gint num, gint den) +{ + QMutexLocker locker(&lock); + if (!qt_item) + return; + qt_item->setDAR(num, den); +} + +void +QtGLVideoItemInterface::getDAR(gint * num, gint * den) +{ + QMutexLocker locker(&lock); + if (!qt_item) + return; + qt_item->getDAR (num, den); +} + +void +QtGLVideoItemInterface::setForceAspectRatio(bool force_aspect_ratio) +{ + QMutexLocker locker(&lock); + if (!qt_item) + return; + qt_item->setForceAspectRatio(force_aspect_ratio); +} + +bool +QtGLVideoItemInterface::getForceAspectRatio() +{ + QMutexLocker locker(&lock); + if (!qt_item) + return FALSE; + return qt_item->getForceAspectRatio(); +} + +void +QtGLVideoItemInterface::invalidateRef() +{ + QMutexLocker locker(&lock); + qt_item = NULL; +} + diff --git a/ext/qt/qtitem.h b/ext/qt/qtitem.h new file mode 100644 index 000000000..065d043ea --- /dev/null +++ b/ext/qt/qtitem.h @@ -0,0 +1,109 @@ +/* + * 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 "gstqtgl.h" +#include <QtCore/QMutex> +#include <QtQuick/QQuickItem> +#include <QtGui/QOpenGLContext> +#include <QtGui/QOpenGLFunctions> + +typedef struct _QtGLVideoItemPrivate QtGLVideoItemPrivate; + +class QtGLVideoItem; + +class QtGLVideoItemInterface : public QObject +{ + Q_OBJECT +public: + QtGLVideoItemInterface (QtGLVideoItem *w) : qt_item (w), lock() {}; + + void invalidateRef(); + + void setBuffer (GstBuffer * buffer); + gboolean setCaps (GstCaps *caps); + gboolean initWinSys (); + GstGLContext *getQtContext(); + GstGLContext *getContext(); + GstGLDisplay *getDisplay(); + QtGLVideoItem *videoItem () { return qt_item; }; + + void setDAR(gint, gint); + void getDAR(gint *, gint *); + void setForceAspectRatio(bool); + bool getForceAspectRatio(); +private: + QtGLVideoItem *qt_item; + QMutex lock; +}; + +class InitializeSceneGraph; + +class QtGLVideoItem : public QQuickItem, protected QOpenGLFunctions +{ + Q_OBJECT + + Q_PROPERTY(bool itemInitialized + READ itemInitialized + NOTIFY itemInitializedChanged) + +public: + QtGLVideoItem(); + ~QtGLVideoItem(); + + void setDAR(gint, gint); + void getDAR(gint *, gint *); + void setForceAspectRatio(bool); + bool getForceAspectRatio(); + bool itemInitialized(); + + QSharedPointer<QtGLVideoItemInterface> getInterface() { return proxy; }; + /* private for C interface ... */ + QtGLVideoItemPrivate *priv; + +Q_SIGNALS: + void itemInitializedChanged(); + +private Q_SLOTS: + void handleWindowChanged(QQuickWindow * win); + void onSceneGraphInitialized(); + void onSceneGraphInvalidated(); + +protected: + QSGNode * updatePaintNode (QSGNode * oldNode, UpdatePaintNodeData * updatePaintNodeData); + +private: + + friend class InitializeSceneGraph; + void setViewportSize(const QSize &size); + void shareContext(); + + QSize m_viewportSize; + bool m_openGlContextInitialized; + + QSharedPointer<QtGLVideoItemInterface> proxy; +}; + +#endif /* __QT_ITEM_H__ */ diff --git a/ext/qt/qtplugin.pro b/ext/qt/qtplugin.pro new file mode 100644 index 000000000..2337087a3 --- /dev/null +++ b/ext/qt/qtplugin.pro @@ -0,0 +1,40 @@ +TEMPLATE = lib + +TARGET = gstqmlgl + +QT += qml quick widgets gui + +QT_CONFIG -= no-pkg-config +CONFIG += link_pkgconfig debug plugin +PKGCONFIG = \ + gstreamer-1.0 \ + gstreamer-video-1.0 \ + gstreamer-gl-1.0 + +DEFINES += \ + GST_USE_UNSTABLE_API \ + HAVE_QT_WIN32 + +SOURCES += \ + gstplugin.cc \ + gstqtglutility.cc \ + gstqsgtexture.cc \ + gstqtsink.cc \ + gstqtsrc.cc \ + qtwindow.cc \ + qtitem.cc + +HEADERS += \ + gstqsgtexture.h \ + gstqtgl.h \ + gstqtglutility.h \ + gstqtsink.h \ + gstqtsrc.h \ + qtwindow.h \ + qtitem.h + +INCLUDEPATH += \ + $$(GSTREAMER_ROOT)/include \ + $$[QT_INSTALL_PREFIX]/include/QtGui/$$[QT_VERSION]/QtGui/ + + diff --git a/ext/qt/qtwindow.cc b/ext/qt/qtwindow.cc new file mode 100644 index 000000000..62d26b647 --- /dev/null +++ b/ext/qt/qtwindow.cc @@ -0,0 +1,434 @@ +/* + * GStreamer + * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved. + * + * 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/gstglfuncs.h> +#include "qtwindow.h" +#include "gstqsgtexture.h" +#include "gstqtglutility.h" + +#include <QtCore/QDateTime> +#include <QtCore/QRunnable> +#include <QtGui/QGuiApplication> +#include <QtQuick/QQuickWindow> +#include <QOpenGLFramebufferObject> + +/* compatability definitions... */ +#ifndef GL_READ_FRAMEBUFFER +#define GL_READ_FRAMEBUFFER 0x8CA8 +#endif +#ifndef GL_DRAW_FRAMEBUFFER +#define GL_DRAW_FRAMEBUFFER 0x8CA9 +#endif + +/** + * SECTION: + * + * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers. + */ + +GST_DEBUG_CATEGORY_STATIC (qt_window_debug); +#define GST_CAT_DEFAULT qt_window_debug + +struct _QtGLWindowPrivate +{ + GMutex lock; + GCond update_cond; + + GstBuffer *buffer; + GstCaps *caps; + GstVideoInfo v_info; + + gboolean initted; + gboolean updated; + gboolean quit; + gboolean result; + gboolean useDefaultFbo; + + GstGLDisplay *display; + GstGLContext *other_context; + + GLuint fbo; + + /* frames that qmlview rendered in its gl thread */ + quint64 frames_rendered; + quint64 start; + quint64 stop; +}; + +class InitQtGLContext : public QRunnable +{ +public: + InitQtGLContext(QtGLWindow *window); + void run(); + +private: + QtGLWindow *window_; +}; + +InitQtGLContext::InitQtGLContext(QtGLWindow *window) : + window_(window) +{ +} + +void InitQtGLContext::run() +{ + window_->onSceneGraphInitialized(); +} + +QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) : + QQuickWindow( parent ), source (src) +{ + QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ()); + static volatile gsize _debug; + + g_assert (app != NULL); + + if (g_once_init_enter (&_debug)) { + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow"); + g_once_init_leave (&_debug, 1); + } + + this->priv = g_new0 (QtGLWindowPrivate, 1); + + g_mutex_init (&this->priv->lock); + g_cond_init (&this->priv->update_cond); + + this->priv->display = gst_qt_get_gl_display(); + + connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection); + connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection); + connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection); + if (source->isSceneGraphInitialized()) + source->scheduleRenderJob(new InitQtGLContext(this), QQuickWindow::BeforeSynchronizingStage); + else + connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection); + + connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection); + + GST_DEBUG ("%p init Qt Window", this->priv->display); +} + +QtGLWindow::~QtGLWindow() +{ + GST_DEBUG ("deinit Qt Window"); + g_mutex_clear (&this->priv->lock); + g_cond_clear (&this->priv->update_cond); + if (this->priv->other_context) + gst_object_unref(this->priv->other_context); + if (this->priv->display) + gst_object_unref(this->priv->display); + g_free (this->priv); + this->priv = NULL; +} + +void +QtGLWindow::beforeRendering() +{ + unsigned int width, height; + + g_mutex_lock (&this->priv->lock); + + static volatile gsize once = 0; + if (g_once_init_enter(&once)) { + this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch(); + g_once_init_leave(&once,1); + } + + if (!fbo && !this->priv->useDefaultFbo) { + + width = source->width(); + height = source->height(); + + GST_DEBUG ("create new framebuffer object %dX%d", width, height); + + fbo.reset(new QOpenGLFramebufferObject (width, height, + QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA)); + + source->setRenderTarget(fbo.data()); + } else if (this->priv->useDefaultFbo) { + GST_DEBUG ("use default fbo for render target"); + fbo.reset(NULL); + source->setRenderTarget(NULL); + } + + g_mutex_unlock (&this->priv->lock); +} + + +void +QtGLWindow::afterRendering() +{ + GstVideoFrame gl_frame; + GstVideoInfo *info; + GstGLContext *context; + gboolean ret; + guint width, height; + const GstGLFuncs *gl; + GLuint dst_tex; + + g_mutex_lock (&this->priv->lock); + + this->priv->frames_rendered++; + + if(!this->priv->buffer || this->priv->updated == TRUE) { + GST_DEBUG ("skip this frame"); + g_mutex_unlock (&this->priv->lock); + return; + } + + GST_DEBUG ("copy buffer %p",this->priv->buffer); + + width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info); + height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info); + info = &this->priv->v_info; + context = this->priv->other_context; + + gst_gl_context_activate (context, TRUE); + gl = context->gl_vtable; + + ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer, + (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL)); + + if (!ret) { + this->priv->buffer = NULL; + GST_ERROR ("Failed to map video frame"); + goto errors; + } + + gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId()); + + ret = gst_gl_context_check_framebuffer_status (context, GL_READ_FRAMEBUFFER); + if (!ret) { + GST_ERROR ("FBO errors"); + goto errors; + } + + dst_tex = *(guint *) gl_frame.data[0]; + GST_DEBUG ("qml render target id %d, render to tex %d %dX%d", + this->source->renderTargetId(), dst_tex, width,height); + + gl->BindTexture (GL_TEXTURE_2D, dst_tex); + if (gl->BlitFramebuffer) { + gl->BindFramebuffer (GL_DRAW_FRAMEBUFFER, this->priv->fbo); + gl->FramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, dst_tex, 0); + + ret = gst_gl_context_check_framebuffer_status (context, GL_DRAW_FRAMEBUFFER); + if (!ret) { + GST_ERROR ("FBO errors"); + goto errors; + } + gl->ReadBuffer (GL_COLOR_ATTACHMENT0); + gl->BlitFramebuffer (0, 0, width, height, + 0, 0, width, height, + GL_COLOR_BUFFER_BIT, GL_LINEAR); + } else { + gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0); + } + + GST_DEBUG ("rendering finished"); + +errors: + gl->BindFramebuffer (GL_FRAMEBUFFER, 0); + gst_video_frame_unmap (&gl_frame); + + gst_gl_context_activate (context, FALSE); + + this->priv->result = ret; + this->priv->updated = TRUE; + g_cond_signal (&this->priv->update_cond); + g_mutex_unlock (&this->priv->lock); +} + +void +QtGLWindow::aboutToQuit() +{ + g_mutex_lock (&this->priv->lock); + + this->priv->updated = TRUE; + this->priv->quit = TRUE; + g_cond_signal (&this->priv->update_cond); + + this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch(); + qint64 duration = this->priv->stop - this->priv->start; + float fps = ((float)this->priv->frames_rendered / duration * 1000); + + GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f", + this->priv->frames_rendered, (float)duration / 1000, fps); + + g_mutex_unlock (&this->priv->lock); +} + +void +QtGLWindow::onSceneGraphInitialized() +{ + GST_DEBUG ("scene graph initialization with Qt GL context %p", + this->source->openglContext ()); + + this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display, + &this->priv->other_context, NULL); + + if (this->priv->initted && this->priv->other_context) { + const GstGLFuncs *gl; + + gst_gl_context_activate (this->priv->other_context, TRUE); + gl = this->priv->other_context->gl_vtable; + + gl->GenFramebuffers (1, &this->priv->fbo); + + gst_gl_context_activate (this->priv->other_context, FALSE); + } + + GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this, + this->priv->other_context); +} + +void +QtGLWindow::onSceneGraphInvalidated() +{ + GST_DEBUG ("scene graph invalidated"); + + if (this->priv->fbo && this->priv->other_context) { + const GstGLFuncs *gl; + + gst_gl_context_activate (this->priv->other_context, TRUE); + gl = this->priv->other_context->gl_vtable; + + gl->DeleteFramebuffers (1, &this->priv->fbo); + + gst_gl_context_activate (this->priv->other_context, FALSE); + } +} + +bool +QtGLWindow::getGeometry(int * width, int * height) +{ + if (width == NULL || height == NULL) + return FALSE; + + *width = this->source->width(); + *height = this->source->height(); + + return TRUE; +} + +GstGLContext * +qt_window_get_qt_context (QtGLWindow * qt_window) +{ + g_return_val_if_fail (qt_window != NULL, NULL); + + if (!qt_window->priv->other_context) + return NULL; + + return (GstGLContext *) gst_object_ref (qt_window->priv->other_context); +} + +GstGLDisplay * +qt_window_get_display (QtGLWindow * qt_window) +{ + g_return_val_if_fail (qt_window != NULL, NULL); + + if (!qt_window->priv->display) + return NULL; + + return (GstGLDisplay *) gst_object_ref (qt_window->priv->display); +} + +gboolean +qt_window_is_scenegraph_initialized (QtGLWindow * qt_window) +{ + g_return_val_if_fail (qt_window != NULL, FALSE); + + return qt_window->priv->initted; +} + +gboolean +qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps) +{ + GstVideoInfo v_info; + + g_return_val_if_fail (qt_window != NULL, FALSE); + g_return_val_if_fail (GST_IS_CAPS (caps), FALSE); + g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE); + + if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps)) + return TRUE; + + if (!gst_video_info_from_caps (&v_info, caps)) + return FALSE; + + g_mutex_lock (&qt_window->priv->lock); + + gst_caps_replace (&qt_window->priv->caps, caps); + + qt_window->priv->v_info = v_info; + + g_mutex_unlock (&qt_window->priv->lock); + + return TRUE; +} + +gboolean +qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer) +{ + g_return_val_if_fail (qt_window != NULL, FALSE); + g_return_val_if_fail (qt_window->priv->initted, FALSE); + gboolean ret; + + g_mutex_lock (&qt_window->priv->lock); + + if (qt_window->priv->quit){ + GST_DEBUG("about to quit, drop this buffer"); + g_mutex_unlock (&qt_window->priv->lock); + return TRUE; + } + + qt_window->priv->updated = FALSE; + qt_window->priv->buffer = buffer; + + while (!qt_window->priv->updated) + g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock); + + ret = qt_window->priv->result; + + g_mutex_unlock (&qt_window->priv->lock); + + return ret; +} + +void +qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo) +{ + g_return_if_fail (qt_window != NULL); + + g_mutex_lock (&qt_window->priv->lock); + + GST_DEBUG ("set to use default fbo %d", useDefaultFbo); + qt_window->priv->useDefaultFbo = useDefaultFbo; + + g_mutex_unlock (&qt_window->priv->lock); +} diff --git a/ext/qt/qtwindow.h b/ext/qt/qtwindow.h new file mode 100644 index 000000000..697f3897a --- /dev/null +++ b/ext/qt/qtwindow.h @@ -0,0 +1,70 @@ +/* + * GStreamer + * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved. + * + * 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_WINDOW_H__ +#define __QT_WINDOW_H__ + +#include <gst/gst.h> +#include <gst/gl/gl.h> + +#include "gstqtgl.h" +#include <QtQuick/QQuickWindow> +#include <QtGui/QOpenGLContext> +#include <QtGui/QOpenGLFunctions> + +typedef struct _QtGLWindowPrivate QtGLWindowPrivate; + +class InitQtGLContext; + +class QtGLWindow : public QQuickWindow, protected QOpenGLFunctions +{ + Q_OBJECT +public: + QtGLWindow (QWindow * parent = NULL, QQuickWindow *source = NULL); + ~QtGLWindow (); + bool getGeometry (int * width, int * height); + + /* private for C interface ... */ + QtGLWindowPrivate *priv; + +private Q_SLOTS: + void beforeRendering (); + void afterRendering (); + void onSceneGraphInitialized (); + void onSceneGraphInvalidated (); + void aboutToQuit(); + +private: + friend class InitQtGLContext; + QQuickWindow * source; + QScopedPointer<QOpenGLFramebufferObject> fbo; +}; + +extern "C" +{ +gboolean qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer); +gboolean qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps); +GstGLContext * qt_window_get_qt_context (QtGLWindow * qt_window); +GstGLDisplay * qt_window_get_display (QtGLWindow * qt_window); +gboolean qt_window_is_scenegraph_initialized (QtGLWindow * qt_window); +void qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo); +} + +#endif /* __QT_WINDOW_H__ */ diff --git a/tests/examples/qt/qmlsink/.gitignore b/tests/examples/qt/qmlsink/.gitignore new file mode 100644 index 000000000..1f8151858 --- /dev/null +++ b/tests/examples/qt/qmlsink/.gitignore @@ -0,0 +1,3 @@ +deployment.pri +play +qrc_qml.cpp diff --git a/tests/examples/qt/qmlsink/CMakeLists.txt b/tests/examples/qt/qmlsink/CMakeLists.txt new file mode 100644 index 000000000..02b0e11ea --- /dev/null +++ b/tests/examples/qt/qmlsink/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.3) + +include(FindPkgConfig) +if(!${PKG_CONFIG_FOUND}) + MESSAGE(FATAL_ERROR "pkg-config required. Please install it") + return ("Error - pkg-config not found") +endif() + +pkg_check_modules(GSTREAMER gstreamer-1.0>=1.6 gstreamer-video-1.0>=1.6 REQUIRED) + +find_package(Qt5Core REQUIRED) +find_package(Qt5Widgets REQUIRED) +find_package(Qt5Qml REQUIRED) +find_package(Qt5Quick REQUIRED) + +set (SRC_LIST main.cpp) +qt5_add_resources(RESOURCES qml.qrc) +link_directories(${GSTREAMER_LIBRARY_DIRS}) +include_directories (${GSTREAMER_INCLUDE_DIRS}) +add_executable(qml-example ${SRC_LIST} ${RESOURCES}) +target_link_libraries (qml-example ${GSTREAMER_LIBRARIES}) +qt5_use_modules(qml-example Core Widgets Qml Quick) + diff --git a/tests/examples/qt/qmlsink/main.cpp b/tests/examples/qt/qmlsink/main.cpp new file mode 100644 index 000000000..a5cd5606b --- /dev/null +++ b/tests/examples/qt/qmlsink/main.cpp @@ -0,0 +1,83 @@ +#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; + + gst_init (&argc, &argv); + + { + QGuiApplication app(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; +} diff --git a/tests/examples/qt/qmlsink/main.qml b/tests/examples/qt/qmlsink/main.qml new file mode 100644 index 000000000..ffd3cd165 --- /dev/null +++ b/tests/examples/qt/qmlsink/main.qml @@ -0,0 +1,60 @@ +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 : parent.width - 30 + height: parent.height - 30 + 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() + } + } + } + } +} diff --git a/tests/examples/qt/qmlsink/play.pro b/tests/examples/qt/qmlsink/play.pro new file mode 100644 index 000000000..374e40297 --- /dev/null +++ b/tests/examples/qt/qmlsink/play.pro @@ -0,0 +1,20 @@ +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 = diff --git a/tests/examples/qt/qmlsink/qml.qrc b/tests/examples/qt/qmlsink/qml.qrc new file mode 100644 index 000000000..5f6483ac3 --- /dev/null +++ b/tests/examples/qt/qmlsink/qml.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>main.qml</file> + </qresource> +</RCC> diff --git a/tests/examples/qt/qmlsrc/.gitignore b/tests/examples/qt/qmlsrc/.gitignore new file mode 100644 index 000000000..d2246aeb2 --- /dev/null +++ b/tests/examples/qt/qmlsrc/.gitignore @@ -0,0 +1,2 @@ +grabqml +qrc_qml.cpp diff --git a/tests/examples/qt/qmlsrc/grabqml.pro b/tests/examples/qt/qmlsrc/grabqml.pro new file mode 100644 index 000000000..374e40297 --- /dev/null +++ b/tests/examples/qt/qmlsrc/grabqml.pro @@ -0,0 +1,20 @@ +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 = diff --git a/tests/examples/qt/qmlsrc/main.cpp b/tests/examples/qt/qmlsrc/main.cpp new file mode 100644 index 000000000..35cedcb09 --- /dev/null +++ b/tests/examples/qt/qmlsrc/main.cpp @@ -0,0 +1,80 @@ +#include <QApplication> +#include <QQmlApplicationEngine> +#include <QQuickWindow> +#include <QQuickItem> +#include <QQuickView> +#include <QRunnable> +#include <QDebug> +#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 ("qmlglsrc", NULL); + GstElement *sink = gst_element_factory_make ("glimagesink", NULL); + + g_assert (src && sink); + + gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL); + gst_element_link_many (src, sink, NULL); + + QQmlApplicationEngine engine; + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + + QQuickWindow *rootObject; + + /* find and set the QQuickWindow on the src */ + rootObject = static_cast<QQuickWindow *> (engine.rootObjects().first()); + g_object_set(src, "window", rootObject, NULL); + g_object_set(src, "use-default-fbo", TRUE, NULL); + /* output buffer of qmlglsrc is vertical flip, get the image orientation tag */ + g_object_set(sink, "rotate-method", 8, 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; +} diff --git a/tests/examples/qt/qmlsrc/main.qml b/tests/examples/qt/qmlsrc/main.qml new file mode 100644 index 000000000..18b36e650 --- /dev/null +++ b/tests/examples/qt/qmlsrc/main.qml @@ -0,0 +1,65 @@ +import QtQuick 2.4 +import QtQuick.Controls 1.1 +import QtQuick.Controls.Styles 1.1 +import QtQuick.Dialogs 1.1 +import QtQuick.Window 2.1 + +ApplicationWindow { + id: window + visible: true + width: 640 + height: 480 + x: 30 + y: 30 + color: "dodgerblue" + + Item { + anchors.fill: parent + + Rectangle { + color: Qt.rgba(1, 1, 1, 0.7) + border.width: 1 + border.color: "white" + anchors.bottomMargin: 15 + anchors.horizontalCenter: parent.horizontalCenter + width : parent.width - 30 + height: parent.height - 30 + radius: 8 + + Text { + id: text1 + anchors.centerIn: parent + text: "Hello World!" + font.pointSize: 24 + visible: timer.tex1_visible + } + + Text { + id: text2 + anchors.centerIn: parent + text: "This is qmlglsrc demo!" + font.pointSize: 24 + visible: timer.tex2_visible + } + + Timer { + id: timer + property int count: 0 + property int tex1_visible: 1 + property int tex2_visible: 0 + interval: 30; running: true; repeat: true + onTriggered: { + count++; + if (count%2 == 0) { + tex1_visible = 1; + tex2_visible = 0; + } + else { + tex1_visible = 0; + tex2_visible = 1; + } + } + } + } + } +} diff --git a/tests/examples/qt/qmlsrc/qml.qrc b/tests/examples/qt/qmlsrc/qml.qrc new file mode 100644 index 000000000..5f6483ac3 --- /dev/null +++ b/tests/examples/qt/qmlsrc/qml.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>main.qml</file> + </qresource> +</RCC> |