qt6: add qml6glsrc element

Same functionality as qmlglsrc (Qt5) but for Qt6.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3737>
This commit is contained in:
Matthew Waters 2022-12-22 18:11:19 +11:00 committed by GStreamer Marge Bot
parent fa23e24f41
commit 293ad62035
13 changed files with 1382 additions and 0 deletions

View file

@ -30,6 +30,7 @@ plugin_init (GstPlugin * plugin)
gboolean ret = FALSE;
ret |= GST_ELEMENT_REGISTER (qml6glsink, plugin);
ret |= GST_ELEMENT_REGISTER (qml6glsrc, plugin);
return ret;
}

View file

@ -0,0 +1,593 @@
/*
* GStreamer
* Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
* Copyright (C) 2022 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:qml6glsrc
*
* Since: 1.24
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstqt6elements.h"
#include "gstqml6glsrc.h"
#include <QtGui/QGuiApplication>
#define GST_CAT_DEFAULT gst_debug_qml6_gl_src
GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
#define DEFAULT_IS_LIVE TRUE
static void gst_qml6_gl_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_qml6_gl_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static void gst_qml6_gl_src_finalize (GObject * object);
static gboolean gst_qml6_gl_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
static GstCaps *gst_qml6_gl_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter);
static gboolean gst_qml6_gl_src_query (GstBaseSrc * bsrc, GstQuery * query);
static gboolean gst_qml6_gl_src_decide_allocation (GstBaseSrc * bsrc,
GstQuery * query);
static GstFlowReturn gst_qml6_gl_src_create (GstPushSrc * psrc, GstBuffer ** buffer);
static gboolean gst_qml6_gl_src_unlock(GstBaseSrc * bsrc);
static gboolean gst_qml6_gl_src_unlock_stop (GstBaseSrc * bsrc);
static GstStateChangeReturn gst_qml6_gl_src_change_state (GstElement * element,
GstStateChange transition);
static gboolean gst_qml6_gl_src_start (GstBaseSrc * basesrc);
static gboolean gst_qml6_gl_src_stop (GstBaseSrc * basesrc);
static GstStaticPadTemplate gst_qml6_gl_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_qml6_gl_src_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstQml6GLSrc, gst_qml6_gl_src,
GST_TYPE_PUSH_SRC, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
"qml6glsrc", 0, "Qt6 Qml Video Src"));
GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (qml6glsrc, "qml6glsrc",
GST_RANK_NONE, GST_TYPE_QML6_GL_SRC, qt6_element_init (plugin));
static const gfloat vertical_flip_matrix[] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f, 1.0f,
};
static void
gst_qml6_gl_src_class_init (GstQml6GLSrcClass * 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_qml6_gl_src_set_property;
gobject_class->get_property = gst_qml6_gl_src_get_property;
gobject_class->finalize = gst_qml6_gl_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_qml6_gl_src_template));
gstelement_class->change_state = gst_qml6_gl_src_change_state;
gstbasesrc_class->set_caps = gst_qml6_gl_src_setcaps;
gstbasesrc_class->get_caps = gst_qml6_gl_src_get_caps;
gstbasesrc_class->query = gst_qml6_gl_src_query;
gstbasesrc_class->start = gst_qml6_gl_src_start;
gstbasesrc_class->stop = gst_qml6_gl_src_stop;
gstbasesrc_class->decide_allocation = gst_qml6_gl_src_decide_allocation;
gstbasesrc_class->unlock = gst_qml6_gl_src_unlock;
gstbasesrc_class->unlock_stop = gst_qml6_gl_src_unlock_stop;
gstpushsrc_class->create = gst_qml6_gl_src_create;
}
static void
gst_qml6_gl_src_init (GstQml6GLSrc * 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_qml6_gl_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_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 Qt6GLWindow (NULL, qt_src->qwindow);
break;
}
case PROP_DEFAULT_FBO:
qt_src->default_fbo = g_value_get_boolean (value);
if (qt_src->window)
qt6_gl_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_qml6_gl_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_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_qml6_gl_src_finalize (GObject * object)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_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_qml6_gl_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_SRC (bsrc);
GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
if (!gst_video_info_from_caps (&qt_src->v_info, caps))
return FALSE;
return TRUE;
}
static GstCaps *
gst_qml6_gl_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
{
GstCaps *caps = NULL, *temp = NULL;
GstPadTemplate *pad_template;
GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc);
GstQml6GLSrc *qt_src = GST_QML6_GL_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_qml6_gl_src_query (GstBaseSrc * bsrc, GstQuery * query)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_SRC (bsrc);
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONTEXT:
{
if (!qt6_gl_window_is_scenegraph_initialized (qt_src->window))
return FALSE;
if (!qt_src->display && !qt_src->qt_context) {
if (!qt_src->display)
qt_src->display = qt6_gl_window_get_display (qt_src->window);
if (!qt_src->qt_context)
qt_src->qt_context = qt6_gl_window_get_qt_context (qt_src->window);
if (!qt_src->context)
qt_src->context = qt6_gl_window_get_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 (GstQml6GLSrc * 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_qml6_gl_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;
GstQml6GLSrc *qt_src = GST_QML6_GL_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 (!qt6_gl_window_set_context (qt_src->window, qt_src->context))
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, &params);
gst_buffer_pool_config_set_allocator (config, allocator, &params);
GST_INFO_OBJECT (qt_src, "got allocator %p", allocator);
update_allocator = TRUE;
} else {
allocator = NULL;
gst_allocation_params_init (&params);
update_allocator = FALSE;
}
glparams =
gst_gl_video_allocation_params_new (qt_src->context, &params, &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, &params);
else
gst_query_add_allocation_param (query, allocator, &params);
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_qml6_gl_src_create (GstPushSrc * psrc, GstBuffer ** buffer)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_SRC (psrc);
GstCaps *updated_caps = NULL;
*buffer = qt6_gl_window_take_buffer (qt_src->window, &updated_caps);
GST_DEBUG_OBJECT (qt_src, "produced buffer %p", *buffer);
if (!*buffer)
return GST_FLOW_FLUSHING;
if (updated_caps) {
GST_DEBUG_OBJECT (qt_src, "new_caps %" GST_PTR_FORMAT, updated_caps);
gst_base_src_set_caps (GST_BASE_SRC (qt_src), updated_caps);
}
gst_clear_caps (&updated_caps);
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 create done %p", *buffer);
return GST_FLOW_OK;
}
static gboolean
gst_qml6_gl_src_unlock (GstBaseSrc * bsrc)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_SRC (bsrc);
if (!qt_src->window)
return TRUE;
qt6_gl_window_unlock (qt_src->window);
return TRUE;
}
static gboolean
gst_qml6_gl_src_unlock_stop (GstBaseSrc * bsrc)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_SRC (bsrc);
if (!qt_src->window)
return TRUE;
qt6_gl_window_unlock_stop (qt_src->window);
return TRUE;
}
static GstStateChangeReturn
gst_qml6_gl_src_change_state (GstElement * element, GstStateChange transition)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_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 (!qt6_gl_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;
}
qt6_gl_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_qml6_gl_src_start (GstBaseSrc * basesrc)
{
GstQml6GLSrc *qt_src = GST_QML6_GL_SRC (basesrc);
/* already has get OpenGL configuration from qt */
if (qt_src->display && qt_src->qt_context)
return TRUE;
if (!qt6_gl_window_is_scenegraph_initialized (qt_src->window))
return FALSE;
qt_src->display = qt6_gl_window_get_display (qt_src->window);
qt_src->qt_context = qt6_gl_window_get_qt_context (qt_src->window);
qt_src->context = qt6_gl_window_get_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_qml6_gl_src_stop (GstBaseSrc * basesrc)
{
return TRUE;
}

View file

@ -0,0 +1,62 @@
/*
* 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_QML6_GL_SRC_H__
#define __GST_QML6_GL_SRC_H__
#include <gst/gst.h>
#include <gst/base/gstpushsrc.h>
#include <gst/video/video.h>
#include <gst/gl/gl.h>
#include "qt6glwindow.h"
G_BEGIN_DECLS
#define GST_TYPE_QML6_GL_SRC (gst_qml6_gl_src_get_type())
G_DECLARE_FINAL_TYPE (GstQml6GLSrc, gst_qml6_gl_src, GST, QML6_GL_SRC, GstPushSrc)
#define GST_QML6_GL_SRC_CAST(obj) ((GstQml6GLSrc*)(obj))
/**
* GstQml6GLSrc:
*
* Opaque #GstQml6GLSrc object
*/
struct _GstQml6GLSrc
{
/* <private> */
GstPushSrc parent;
QQuickWindow *qwindow;
Qt6GLWindow *window;
GstVideoInfo v_info;
GstGLDisplay *display;
GstGLContext *context;
GstGLContext *qt_context;
gboolean default_fbo;
gboolean downstream_supports_affine_meta;
gboolean pending_image_orientation;
};
G_END_DECLS
#endif /* __GST_QML6_GL_SRC_H__ */

View file

@ -27,6 +27,7 @@ G_BEGIN_DECLS
void qt6_element_init (GstPlugin * plugin);
GST_ELEMENT_REGISTER_DECLARE (qml6glsink);
GST_ELEMENT_REGISTER_DECLARE (qml6glsrc);
G_END_DECLS

View file

@ -4,12 +4,15 @@ sources = [
'gstqsg6glnode.cc',
'gstqt6glutility.cc',
'gstqml6glsink.cc',
'gstqml6glsrc.cc',
'qt6glitem.cc',
'qt6glwindow.cc',
]
moc_headers = [
'qt6glitem.h',
'gstqsg6glnode.h',
'qt6glwindow.h',
]
qt6qml_dep = dependency('', required: false)

View file

@ -0,0 +1,470 @@
/*
* GStreamer
* Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
* Copyright (C) 2022 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <gst/video/video.h>
#include <gst/gl/gstglfuncs.h>
#include "qt6glwindow.h"
#include "gstqt6glutility.h"
#include <QtCore/QDateTime>
#include <QtGui/QGuiApplication>
#include <QtQuick/QQuickWindow>
#include <QQuickRenderTarget>
/* compatibility definitions... */
#ifndef GL_READ_FRAMEBUFFER
#define GL_READ_FRAMEBUFFER 0x8CA8
#endif
#ifndef GL_DRAW_FRAMEBUFFER
#define GL_DRAW_FRAMEBUFFER 0x8CA9
#endif
/**
* SECTION:
*
* #Qt6GLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
*/
GST_DEBUG_CATEGORY_STATIC (qt6_gl_window_debug);
#define GST_CAT_DEFAULT qt6_gl_window_debug
struct _Qt6GLWindowPrivate
{
GMutex lock;
GCond update_cond;
GstBuffer *buffer;
GstVideoInfo v_info;
GstVideoFrame mapped_frame;
GstGLBaseMemoryAllocator *gl_allocator;
GstGLAllocationParams *gl_params;
gboolean initted;
gboolean updated;
gboolean quit;
gboolean result;
gboolean useDefaultFbo;
GstGLDisplay *display;
GstGLContext *other_context;
GstGLContext *context;
guint fbo;
gboolean new_caps;
GstBuffer *produced_buffer;
};
Qt6GLWindow::Qt6GLWindow (QWindow * parent, QQuickWindow *src)
: QQuickWindow( parent ), source (src)
{
QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
static gsize _debug;
g_assert (app != NULL);
if (g_once_init_enter (&_debug)) {
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qt6glwindow", 0, "Qt6 GL QuickWindow");
g_once_init_leave (&_debug, 1);
}
this->priv = g_new0 (Qt6GLWindowPrivate, 1);
g_mutex_init (&this->priv->lock);
g_cond_init (&this->priv->update_cond);
this->priv->display = gst_qml6_get_gl_display(FALSE);
this->priv->result = TRUE;
connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
if (source->isSceneGraphInitialized())
source->scheduleRenderJob(new RenderJob(std::bind(&Qt6GLWindow::onSceneGraphInitialized, 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);
}
Qt6GLWindow::~Qt6GLWindow()
{
GST_DEBUG ("deinit Qt Window");
g_mutex_clear (&this->priv->lock);
g_cond_clear (&this->priv->update_cond);
gst_clear_object (&this->priv->other_context);
gst_clear_buffer (&this->priv->buffer);
gst_clear_buffer (&this->priv->produced_buffer);
gst_clear_object (&this->priv->display);
gst_clear_object (&this->priv->context);
gst_clear_object (&this->priv->gl_allocator);
if (this->priv->gl_params)
gst_gl_allocation_params_free (this->priv->gl_params);
this->priv->gl_params = NULL;
g_free (this->priv);
this->priv = NULL;
}
void
Qt6GLWindow::beforeRendering()
{
g_mutex_lock (&this->priv->lock);
if (!this->priv->context) {
GST_LOG ("no GStreamer GL context set yet, skipping frame");
g_mutex_unlock (&this->priv->lock);
return;
}
QSize size = source->size();
if (!this->priv->gl_allocator)
this->priv->gl_allocator =
(GstGLBaseMemoryAllocator *) gst_gl_memory_allocator_get_default (this->priv->context);
if (GST_VIDEO_INFO_WIDTH (&this->priv->v_info) != size.width()
|| GST_VIDEO_INFO_HEIGHT (&this->priv->v_info) != size.height()) {
this->priv->new_caps = TRUE;
gst_video_info_set_format (&this->priv->v_info, GST_VIDEO_FORMAT_RGBA,
size.width(), size.height());
if (this->priv->gl_params) {
GstGLVideoAllocationParams *gl_vid_params = (GstGLVideoAllocationParams *) this->priv->gl_params;
if (GST_VIDEO_INFO_WIDTH (gl_vid_params->v_info) != source->width()
|| GST_VIDEO_INFO_HEIGHT (gl_vid_params->v_info) != source->height())
this->priv->gl_params = NULL;
gst_clear_buffer (&this->priv->buffer);
}
}
if (!this->priv->gl_params) {
this->priv->gl_params = (GstGLAllocationParams *)
gst_gl_video_allocation_params_new (this->priv->context, NULL,
&this->priv->v_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
}
if (!this->priv->buffer) {
GstGLMemory *gl_mem =
(GstGLMemory *) gst_gl_base_memory_alloc (this->priv->gl_allocator,
this->priv->gl_params);
this->priv->buffer = gst_buffer_new ();
gst_buffer_append_memory (this->priv->buffer, (GstMemory *) gl_mem);
}
if (!gst_video_frame_map (&this->priv->mapped_frame, &this->priv->v_info,
this->priv->buffer, (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL))) {
GST_WARNING ("failed map video frame");
gst_clear_buffer (&this->priv->buffer);
return;
}
if (!this->priv->useDefaultFbo) {
guint tex_id = *(guint *) this->priv->mapped_frame.data[0];
source->setRenderTarget(QQuickRenderTarget::fromOpenGLTexture(tex_id, source->size()));
} else if (this->priv->useDefaultFbo) {
GST_DEBUG ("use default fbo for render target");
source->setRenderTarget(QQuickRenderTarget());
}
g_mutex_unlock (&this->priv->lock);
}
void
Qt6GLWindow::afterRendering()
{
gboolean ret;
guint width, height;
const GstGLFuncs *gl;
GstGLSyncMeta *sync_meta;
g_mutex_lock (&this->priv->lock);
if (!this->priv->buffer) {
GST_LOG ("no buffer created in beforeRendering(), skipping");
g_mutex_unlock (&this->priv->lock);
return;
}
width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
gst_gl_context_activate (this->priv->other_context, TRUE);
gl = this->priv->other_context->gl_vtable;
if (!this->priv->useDefaultFbo) {
gst_video_frame_unmap (&this->priv->mapped_frame);
ret = TRUE;
} else {
gl->BindFramebuffer (GL_READ_FRAMEBUFFER, 0);
ret = gst_gl_context_check_framebuffer_status (this->priv->other_context, GL_READ_FRAMEBUFFER);
if (!ret) {
GST_ERROR ("FBO errors");
goto errors;
}
guint dst_tex = *(guint *) this->priv->mapped_frame.data[0];
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 (this->priv->other_context, GL_DRAW_FRAMEBUFFER);
if (!ret) {
GST_ERROR ("FBO errors");
goto errors;
}
gl->ReadBuffer (GL_BACK);
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_video_frame_unmap (&this->priv->mapped_frame);
gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
if (this->priv->context) {
sync_meta = gst_buffer_get_gl_sync_meta (this->priv->buffer);
if (!sync_meta) {
sync_meta = gst_buffer_add_gl_sync_meta (this->priv->context, this->priv->buffer);
}
gst_gl_sync_meta_set_sync_point (sync_meta, this->priv->other_context);
}
GST_DEBUG ("rendering finished");
done:
gst_gl_context_activate (this->priv->other_context, FALSE);
this->priv->result = ret;
this->priv->produced_buffer = this->priv->buffer;
this->priv->buffer = NULL;
this->priv->updated = TRUE;
g_cond_signal (&this->priv->update_cond);
g_mutex_unlock (&this->priv->lock);
return;
errors:
gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
gst_video_frame_unmap (&this->priv->mapped_frame);
goto done;
}
void
Qt6GLWindow::onSceneGraphInitialized()
{
QSGRendererInterface *renderer = source->rendererInterface();
if (!renderer)
return;
if (renderer->graphicsApi() != QSGRendererInterface::GraphicsApi::OpenGL) {
GST_WARNING ("%p scene graph initialized with a non-OpenGL renderer interface", this);
return;
}
this->priv->initted = gst_qml6_get_gl_wrapcontext (this->priv->display,
&this->priv->other_context, &this->priv->context);
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
Qt6GLWindow::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);
}
gst_clear_buffer (&this->priv->buffer);
gst_clear_buffer (&this->priv->produced_buffer);
}
bool
Qt6GLWindow::getGeometry(int * width, int * height)
{
if (width == NULL || height == NULL)
return FALSE;
*width = this->source->width();
*height = this->source->height();
return TRUE;
}
GstGLContext *
qt6_gl_window_get_qt_context (Qt6GLWindow * qt6_gl_window)
{
g_return_val_if_fail (qt6_gl_window != NULL, NULL);
if (!qt6_gl_window->priv->other_context)
return NULL;
return (GstGLContext *) gst_object_ref (qt6_gl_window->priv->other_context);
}
GstGLDisplay *
qt6_gl_window_get_display (Qt6GLWindow * qt6_gl_window)
{
g_return_val_if_fail (qt6_gl_window != NULL, NULL);
if (!qt6_gl_window->priv->display)
return NULL;
return (GstGLDisplay *) gst_object_ref (qt6_gl_window->priv->display);
}
GstGLContext *
qt6_gl_window_get_context (Qt6GLWindow * qt6_gl_window)
{
g_return_val_if_fail (qt6_gl_window != NULL, NULL);
if (!qt6_gl_window->priv->context)
return NULL;
return (GstGLContext *) gst_object_ref (qt6_gl_window->priv->context);
}
gboolean
qt6_gl_window_set_context (Qt6GLWindow * qt6_gl_window, GstGLContext * context)
{
g_return_val_if_fail (qt6_gl_window != NULL, FALSE);
if (qt6_gl_window->priv->context && qt6_gl_window->priv->context != context)
return FALSE;
gst_object_replace ((GstObject **) &qt6_gl_window->priv->context, (GstObject *) context);
return TRUE;
}
gboolean
qt6_gl_window_is_scenegraph_initialized (Qt6GLWindow * qt6_gl_window)
{
g_return_val_if_fail (qt6_gl_window != NULL, FALSE);
return qt6_gl_window->priv->initted;
}
GstBuffer *
qt6_gl_window_take_buffer (Qt6GLWindow * qt6_gl_window, GstCaps ** updated_caps)
{
g_return_val_if_fail (qt6_gl_window != NULL, FALSE);
g_return_val_if_fail (qt6_gl_window->priv->initted, FALSE);
GstBuffer *ret;
g_mutex_lock (&qt6_gl_window->priv->lock);
if (qt6_gl_window->priv->quit){
GST_DEBUG("about to quit, drop this buffer");
g_mutex_unlock (&qt6_gl_window->priv->lock);
return NULL;
}
while (!qt6_gl_window->priv->produced_buffer && qt6_gl_window->priv->result)
g_cond_wait (&qt6_gl_window->priv->update_cond, &qt6_gl_window->priv->lock);
ret = qt6_gl_window->priv->produced_buffer;
qt6_gl_window->priv->produced_buffer = NULL;
if (qt6_gl_window->priv->new_caps) {
*updated_caps = gst_video_info_to_caps (&qt6_gl_window->priv->v_info);
gst_caps_set_features (*updated_caps, 0,
gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
qt6_gl_window->priv->new_caps = FALSE;
}
g_mutex_unlock (&qt6_gl_window->priv->lock);
return ret;
}
void
qt6_gl_window_use_default_fbo (Qt6GLWindow * qt6_gl_window, gboolean useDefaultFbo)
{
g_return_if_fail (qt6_gl_window != NULL);
g_mutex_lock (&qt6_gl_window->priv->lock);
GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
qt6_gl_window->priv->useDefaultFbo = useDefaultFbo;
g_mutex_unlock (&qt6_gl_window->priv->lock);
}
void
qt6_gl_window_unlock(Qt6GLWindow* qt6_gl_window)
{
g_mutex_lock(&qt6_gl_window->priv->lock);
GST_DEBUG("unlock window");
qt6_gl_window->priv->result = FALSE;
g_cond_signal(&qt6_gl_window->priv->update_cond);
g_mutex_unlock(&qt6_gl_window->priv->lock);
}
void
qt6_gl_window_unlock_stop(Qt6GLWindow* qt6_gl_window)
{
g_mutex_lock(&qt6_gl_window->priv->lock);
GST_DEBUG("unlock stop window");
qt6_gl_window->priv->result = TRUE;
g_cond_signal(&qt6_gl_window->priv->update_cond);
g_mutex_unlock(&qt6_gl_window->priv->lock);
}

View file

@ -0,0 +1,69 @@
/*
* GStreamer
* Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
* Copyright (C) 2022 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 __QT6_GL_WINDOW_H__
#define __QT6_GL_WINDOW_H__
#include <gst/gst.h>
#include <gst/gl/gl.h>
#include "gstqt6gl.h"
#include <QtQuick/QQuickWindow>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFunctions>
typedef struct _Qt6GLWindowPrivate Qt6GLWindowPrivate;
class Qt6GLWindow : public QQuickWindow, protected QOpenGLFunctions
{
Q_OBJECT
public:
Qt6GLWindow (QWindow * parent = NULL, QQuickWindow *source = NULL);
~Qt6GLWindow ();
bool getGeometry (int * width, int * height);
/* private for C interface ... */
Qt6GLWindowPrivate *priv;
private Q_SLOTS:
void beforeRendering ();
void afterRendering ();
void onSceneGraphInitialized ();
void onSceneGraphInvalidated ();
private:
QQuickWindow * source;
};
extern "C"
{
GstBuffer * qt6_gl_window_take_buffer (Qt6GLWindow * qt6_window, GstCaps ** updated_caps);
GstGLContext * qt6_gl_window_get_qt_context (Qt6GLWindow * qt6_window);
GstGLContext * qt6_gl_window_get_context (Qt6GLWindow * qt6_window);
gboolean qt6_gl_window_set_context (Qt6GLWindow * qt6_window, GstGLContext * context);
GstGLDisplay * qt6_gl_window_get_display (Qt6GLWindow * qt6_window);
gboolean qt6_gl_window_is_scenegraph_initialized (Qt6GLWindow * qt6_window);
void qt6_gl_window_use_default_fbo (Qt6GLWindow * qt6_window, gboolean useDefaultFbo);
void qt6_gl_window_unlock(Qt6GLWindow* qt6_window);
void qt6_gl_window_unlock_stop(Qt6GLWindow* qt6_window);
}
#endif /* __QT6_GL_WINDOW_H__ */

View file

@ -15,3 +15,4 @@ if not qt6qml_example_deps.found()
endif
subdir('qmlsink')
subdir('qmlsrc')

View file

@ -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 += qmlsrc.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

View file

@ -0,0 +1,82 @@
#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);
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
GstElement *pipeline = gst_pipeline_new (NULL);
GstElement *src = gst_element_factory_make ("qml6glsrc", 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;
}

View file

@ -0,0 +1,64 @@
import QtQuick 6.0
import QtQuick.Controls 6.0
import QtQuick.Dialogs 6.0
import QtQuick.Window 6.0
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;
}
}
}
}
}
}

View file

@ -0,0 +1,11 @@
sources = [
'main.cpp',
]
qt_preprocessed = qt6_mod.preprocess(qresources : 'qmlsrc.qrc')
executable('qml6src', sources, qt_preprocessed,
dependencies : [gst_dep, qt6qml_example_deps],
override_options : ['cpp_std=c++17'],
c_args : gst_plugins_good_args,
include_directories : [configinc],
install: false)

View file

@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>