3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #include <gst/video/video.h>
28 #include <QGuiApplication>
29 #include <QQuickWindow>
30 #include <QSGSimpleTextureNode>
32 #include "gstqsgtexture.h"
34 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (HAVE_QT_X11)
36 #include <gst/gl/x11/gstgldisplay_x11.h>
37 #include <gst/gl/x11/gstglcontext_glx.h>
40 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
41 #include <gst/gl/wayland/gstgldisplay_wayland.h>
45 * SECTION:gtkgstglwidget
46 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
47 * @see_also: #GtkGLArea, #GstBuffer
49 * #QtGLVideoItem is an #GtkWidget that renders GStreamer video buffers.
52 #define GST_CAT_DEFAULT qt_item_debug
53 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
55 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
56 GTK_TYPE_GST_GL_WIDGET, QtGLVideoItemPrivate))
58 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
59 #define DEFAULT_PAR_N 0
60 #define DEFAULT_PAR_D 1
65 PROP_FORCE_ASPECT_RATIO,
66 PROP_PIXEL_ASPECT_RATIO,
69 struct _QtGLVideoItemPrivate
74 gboolean force_aspect_ratio;
86 GstGLDisplay *display;
87 QOpenGLContext *qt_context;
88 GstGLContext *other_context;
89 GstGLContext *context;
92 QtGLVideoItem::QtGLVideoItem()
94 QGuiApplication *app = dynamic_cast<QGuiApplication *> (QCoreApplication::instance ());
95 static volatile gsize _debug;
97 g_assert (app != NULL);
99 if (g_once_init_enter (&_debug)) {
100 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
101 g_once_init_leave (&_debug, 1);
104 this->setFlag (QQuickItem::ItemHasContents, true);
106 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
108 this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
109 this->priv->par_n = DEFAULT_PAR_N;
110 this->priv->par_d = DEFAULT_PAR_D;
112 g_mutex_init (&this->priv->lock);
114 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
115 if (QString::fromUtf8 ("xcb") == app->platformName())
116 this->priv->display = (GstGLDisplay *)
117 gst_gl_display_x11_new_with_display (QX11Info::display ());
120 if (!this->priv->display)
121 this->priv->display = gst_gl_display_new ();
123 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
124 SLOT(handleWindowChanged(QQuickWindow*)));
126 GST_DEBUG ("%p init Qt Video Item", this);
129 QtGLVideoItem::~QtGLVideoItem()
131 g_mutex_clear (&this->priv->lock);
138 QtGLVideoItem::setDAR(gint num, gint den)
140 this->priv->par_n = num;
141 this->priv->par_d = den;
145 QtGLVideoItem::getDAR(gint * num, gint * den)
148 *num = this->priv->par_n;
150 *den = this->priv->par_d;
154 QtGLVideoItem::setForceAspectRatio(bool far)
156 this->priv->force_aspect_ratio = far;
160 QtGLVideoItem::getForceAspectRatio()
162 return this->priv->force_aspect_ratio;
166 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
167 UpdatePaintNodeData * updatePaintNodeData)
169 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
170 GstVideoRectangle src, dst, result;
173 g_mutex_lock (&this->priv->lock);
174 gst_gl_context_activate (this->priv->other_context, TRUE);
176 GST_TRACE ("%p updatePaintNode", this);
178 if (!this->priv->caps) {
179 g_mutex_unlock (&this->priv->lock);
184 texNode = new QSGSimpleTextureNode ();
185 texNode->setOwnsTexture (true);
187 tex = static_cast<GstQSGTexture *> (texNode->texture());
190 tex = new GstQSGTexture ();
191 tex->setCaps (this->priv->caps);
192 tex->setBuffer (this->priv->buffer);
193 texNode->setTexture (tex);
195 if (this->priv->force_aspect_ratio) {
196 src.w = this->priv->display_width;
197 src.h = this->priv->display_height;
199 dst.x = boundingRect().x();
200 dst.y = boundingRect().y();
201 dst.w = boundingRect().width();
202 dst.h = boundingRect().height();
204 gst_video_sink_center_rect (src, dst, &result, TRUE);
206 result.x = boundingRect().x();
207 result.y = boundingRect().y();
208 result.w = boundingRect().width();
209 result.h = boundingRect().height();
212 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
214 gst_gl_context_activate (this->priv->other_context, FALSE);
215 g_mutex_unlock (&this->priv->lock);
221 _reset (QtGLVideoItem * qt_item)
223 gst_buffer_replace (&qt_item->priv->buffer, NULL);
225 gst_caps_replace (&qt_item->priv->caps, NULL);
227 qt_item->priv->negotiated = FALSE;
228 qt_item->priv->initted = FALSE;
232 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
234 g_return_if_fail (widget != NULL);
235 g_return_if_fail (widget->priv->negotiated);
237 g_mutex_lock (&widget->priv->lock);
239 gst_buffer_replace (&widget->priv->buffer, buffer);
241 QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
243 g_mutex_unlock (&widget->priv->lock);
247 QtGLVideoItem::onSceneGraphInitialized ()
249 GstGLPlatform platform;
253 GST_DEBUG ("scene graph initialization with Qt GL context %p",
254 this->window()->openglContext ());
256 if (this->priv->qt_context == this->window()->openglContext ())
259 this->priv->qt_context = this->window()->openglContext ();
260 if (this->priv->qt_context == NULL) {
261 g_assert_not_reached ();
265 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
266 if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
267 platform = GST_GL_PLATFORM_GLX;
268 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
269 gl_handle = gst_gl_context_get_current_gl_context (platform);
271 this->priv->other_context =
272 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
276 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
277 if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
278 platform = GST_GL_PLATFORM_EGL;
279 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
280 gl_handle = gst_gl_context_get_current_gl_context (platform);
282 this->priv->other_context =
283 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
292 if (this->priv->other_context) {
293 GError *error = NULL;
295 gst_gl_context_activate (this->priv->other_context, TRUE);
296 if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
297 GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
298 g_object_unref (this->priv->other_context);
299 this->priv->other_context = NULL;
301 gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
302 gst_gl_context_activate (this->priv->other_context, FALSE);
306 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
307 this->priv->other_context);
311 QtGLVideoItem::onSceneGraphInvalidated ()
313 GST_FIXME ("%p scene graph invalidated", this);
317 qt_item_init_winsys (QtGLVideoItem * widget)
319 g_return_val_if_fail (widget != NULL, FALSE);
321 g_mutex_lock (&widget->priv->lock);
323 if (widget->priv->display && widget->priv->qt_context
324 && widget->priv->other_context && widget->priv->context) {
325 /* already have the necessary state */
326 g_mutex_unlock (&widget->priv->lock);
330 if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
331 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
332 widget, widget->priv->display);
333 g_mutex_unlock (&widget->priv->lock);
337 if (!GST_GL_IS_CONTEXT (widget->priv->other_context)) {
338 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
339 widget->priv->other_context);
340 g_mutex_unlock (&widget->priv->lock);
344 widget->priv->context = gst_gl_context_new (widget->priv->display);
346 if (!widget->priv->context) {
347 g_mutex_unlock (&widget->priv->lock);
351 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
354 g_mutex_unlock (&widget->priv->lock);
359 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
362 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
363 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
365 this->priv->qt_context = NULL;
370 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
375 gint display_par_n, display_par_d;
376 guint display_ratio_num, display_ratio_den;
378 width = GST_VIDEO_INFO_WIDTH (info);
379 height = GST_VIDEO_INFO_HEIGHT (info);
381 par_n = GST_VIDEO_INFO_PAR_N (info);
382 par_d = GST_VIDEO_INFO_PAR_D (info);
387 /* get display's PAR */
388 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
389 display_par_n = widget->priv->par_n;
390 display_par_d = widget->priv->par_d;
396 ok = gst_video_calculate_display_ratio (&display_ratio_num,
397 &display_ratio_den, width, height, par_n, par_d, display_par_n,
403 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
405 if (height % display_ratio_den == 0) {
406 GST_DEBUG ("keeping video height");
407 widget->priv->display_width = (guint)
408 gst_util_uint64_scale_int (height, display_ratio_num,
410 widget->priv->display_height = height;
411 } else if (width % display_ratio_num == 0) {
412 GST_DEBUG ("keeping video width");
413 widget->priv->display_width = width;
414 widget->priv->display_height = (guint)
415 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
417 GST_DEBUG ("approximating while keeping video height");
418 widget->priv->display_width = (guint)
419 gst_util_uint64_scale_int (height, display_ratio_num,
421 widget->priv->display_height = height;
423 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
424 widget->priv->display_height);
430 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
434 g_return_val_if_fail (widget != NULL, FALSE);
435 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
436 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
438 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
441 if (!gst_video_info_from_caps (&v_info, caps))
444 g_mutex_lock (&widget->priv->lock);
448 gst_caps_replace (&widget->priv->caps, caps);
450 if (!_calculate_par (widget, &v_info)) {
451 g_mutex_unlock (&widget->priv->lock);
455 widget->priv->v_info = v_info;
456 widget->priv->negotiated = TRUE;
458 g_mutex_unlock (&widget->priv->lock);
464 qt_item_get_qt_context (QtGLVideoItem * qt_item)
466 g_return_val_if_fail (qt_item != NULL, NULL);
468 if (!qt_item->priv->other_context)
471 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
475 qt_item_get_context (QtGLVideoItem * qt_item)
477 g_return_val_if_fail (qt_item != NULL, NULL);
479 if (!qt_item->priv->context)
482 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
486 qt_item_get_display (QtGLVideoItem * qt_item)
488 g_return_val_if_fail (qt_item != NULL, NULL);
490 if (!qt_item->priv->display)
493 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);