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>
29 #include "gstqsgtexture.h"
31 #include <QtGui/QGuiApplication>
32 #include <QtQuick/QQuickWindow>
33 #include <QtQuick/QSGSimpleTextureNode>
35 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (HAVE_QT_X11)
37 #include <gst/gl/x11/gstgldisplay_x11.h>
38 #include <gst/gl/x11/gstglcontext_glx.h>
41 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
42 #include <gst/gl/wayland/gstgldisplay_wayland.h>
45 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
46 #include <gst/gl/egl/gstgldisplay_egl.h>
47 #include <gst/gl/egl/gstglcontext_egl.h>
50 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
51 #include <gst/gl/coaoa/gstgldisplay_cocoa.h>
55 * SECTION:gtkgstglwidget
56 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
57 * @see_also: #GtkGLArea, #GstBuffer
59 * #QtGLVideoItem is an #GtkWidget that renders GStreamer video buffers.
62 #define GST_CAT_DEFAULT qt_item_debug
63 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
65 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
66 GTK_TYPE_GST_GL_WIDGET, QtGLVideoItemPrivate))
68 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
69 #define DEFAULT_PAR_N 0
70 #define DEFAULT_PAR_D 1
75 PROP_FORCE_ASPECT_RATIO,
76 PROP_PIXEL_ASPECT_RATIO,
79 struct _QtGLVideoItemPrivate
84 gboolean force_aspect_ratio;
96 GstGLDisplay *display;
97 QOpenGLContext *qt_context;
98 GstGLContext *other_context;
99 GstGLContext *context;
102 QtGLVideoItem::QtGLVideoItem()
104 QGuiApplication *app = dynamic_cast<QGuiApplication *> (QCoreApplication::instance ());
105 static volatile gsize _debug;
107 g_assert (app != NULL);
109 if (g_once_init_enter (&_debug)) {
110 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
111 g_once_init_leave (&_debug, 1);
114 this->setFlag (QQuickItem::ItemHasContents, true);
116 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
118 this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
119 this->priv->par_n = DEFAULT_PAR_N;
120 this->priv->par_d = DEFAULT_PAR_D;
122 g_mutex_init (&this->priv->lock);
124 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
125 if (QString::fromUtf8 ("xcb") == app->platformName())
126 this->priv->display = (GstGLDisplay *)
127 gst_gl_display_x11_new_with_display (QX11Info::display ());
129 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
130 if (QString::fromUtf8 ("android") == app->platformName())
131 this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
133 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
134 if (QString::fromUtf8 ("cocoa") == app->platformName())
135 this->priv->display = (GstGLDisplay *) gst_gl_display_cocoa_new ();
137 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
138 if (QString::fromUtf8 ("ios") == app->platformName())
139 this->priv->display = gst_gl_display_new ();
142 if (!this->priv->display)
143 this->priv->display = gst_gl_display_new ();
145 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
146 SLOT(handleWindowChanged(QQuickWindow*)));
148 GST_DEBUG ("%p init Qt Video Item", this);
151 QtGLVideoItem::~QtGLVideoItem()
153 g_mutex_clear (&this->priv->lock);
160 QtGLVideoItem::setDAR(gint num, gint den)
162 this->priv->par_n = num;
163 this->priv->par_d = den;
167 QtGLVideoItem::getDAR(gint * num, gint * den)
170 *num = this->priv->par_n;
172 *den = this->priv->par_d;
176 QtGLVideoItem::setForceAspectRatio(bool far)
178 this->priv->force_aspect_ratio = far;
182 QtGLVideoItem::getForceAspectRatio()
184 return this->priv->force_aspect_ratio;
188 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
189 UpdatePaintNodeData * updatePaintNodeData)
191 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
192 GstVideoRectangle src, dst, result;
195 g_mutex_lock (&this->priv->lock);
196 gst_gl_context_activate (this->priv->other_context, TRUE);
198 GST_TRACE ("%p updatePaintNode", this);
200 if (!this->priv->caps) {
201 g_mutex_unlock (&this->priv->lock);
206 texNode = new QSGSimpleTextureNode ();
207 texNode->setOwnsTexture (true);
208 texNode->setTexture (new GstQSGTexture ());
211 tex = static_cast<GstQSGTexture *> (texNode->texture());
212 tex->setCaps (this->priv->caps);
213 tex->setBuffer (this->priv->buffer);
214 texNode->markDirty(QSGNode::DirtyMaterial);
216 if (this->priv->force_aspect_ratio) {
217 src.w = this->priv->display_width;
218 src.h = this->priv->display_height;
220 dst.x = boundingRect().x();
221 dst.y = boundingRect().y();
222 dst.w = boundingRect().width();
223 dst.h = boundingRect().height();
225 gst_video_sink_center_rect (src, dst, &result, TRUE);
227 result.x = boundingRect().x();
228 result.y = boundingRect().y();
229 result.w = boundingRect().width();
230 result.h = boundingRect().height();
233 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
235 gst_gl_context_activate (this->priv->other_context, FALSE);
236 g_mutex_unlock (&this->priv->lock);
242 _reset (QtGLVideoItem * qt_item)
244 gst_buffer_replace (&qt_item->priv->buffer, NULL);
246 gst_caps_replace (&qt_item->priv->caps, NULL);
248 qt_item->priv->negotiated = FALSE;
249 qt_item->priv->initted = FALSE;
253 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
255 g_return_if_fail (widget != NULL);
256 g_return_if_fail (widget->priv->negotiated);
258 g_mutex_lock (&widget->priv->lock);
260 gst_buffer_replace (&widget->priv->buffer, buffer);
262 QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
264 g_mutex_unlock (&widget->priv->lock);
268 QtGLVideoItem::onSceneGraphInitialized ()
270 GstGLPlatform platform;
274 GST_DEBUG ("scene graph initialization with Qt GL context %p",
275 this->window()->openglContext ());
277 if (this->priv->qt_context == this->window()->openglContext ())
280 this->priv->qt_context = this->window()->openglContext ();
281 if (this->priv->qt_context == NULL) {
282 g_assert_not_reached ();
286 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
287 if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
288 platform = GST_GL_PLATFORM_GLX;
289 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
290 gl_handle = gst_gl_context_get_current_gl_context (platform);
292 this->priv->other_context =
293 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
297 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
298 if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
299 platform = GST_GL_PLATFORM_EGL;
300 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
301 gl_handle = gst_gl_context_get_current_gl_context (platform);
303 this->priv->other_context =
304 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
308 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
309 if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
310 platform = GST_GL_PLATFORM_EGL;
311 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
312 gl_handle = gst_gl_context_get_current_gl_context (platform);
314 this->priv->other_context =
315 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
319 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
320 if (this->priv->display) {
321 platform = GST_GL_PLATFORM_CGL;
322 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
323 gl_handle = gst_gl_context_get_current_gl_context (platform);
325 this->priv->other_context =
326 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
330 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
331 if (this->priv->display) {
332 platform = GST_GL_PLATFORM_EAGL;
333 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
334 gl_handle = gst_gl_context_get_current_gl_context (platform);
336 this->priv->other_context =
337 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
346 if (this->priv->other_context) {
347 GError *error = NULL;
349 gst_gl_context_activate (this->priv->other_context, TRUE);
350 if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
351 GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
352 g_object_unref (this->priv->other_context);
353 this->priv->other_context = NULL;
355 gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
356 gst_gl_context_activate (this->priv->other_context, FALSE);
360 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
361 this->priv->other_context);
365 QtGLVideoItem::onSceneGraphInvalidated ()
367 GST_FIXME ("%p scene graph invalidated", this);
371 qt_item_init_winsys (QtGLVideoItem * widget)
373 g_return_val_if_fail (widget != NULL, FALSE);
375 g_mutex_lock (&widget->priv->lock);
377 if (widget->priv->display && widget->priv->qt_context
378 && widget->priv->other_context && widget->priv->context) {
379 /* already have the necessary state */
380 g_mutex_unlock (&widget->priv->lock);
384 if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
385 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
386 widget, widget->priv->display);
387 g_mutex_unlock (&widget->priv->lock);
391 if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
392 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
393 widget->priv->other_context);
394 g_mutex_unlock (&widget->priv->lock);
398 widget->priv->context = gst_gl_context_new (widget->priv->display);
400 if (!widget->priv->context) {
401 g_mutex_unlock (&widget->priv->lock);
405 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
408 g_mutex_unlock (&widget->priv->lock);
413 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
416 if (win->isSceneGraphInitialized())
417 onSceneGraphInitialized();
419 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
421 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
423 this->priv->qt_context = NULL;
428 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
433 gint display_par_n, display_par_d;
434 guint display_ratio_num, display_ratio_den;
436 width = GST_VIDEO_INFO_WIDTH (info);
437 height = GST_VIDEO_INFO_HEIGHT (info);
439 par_n = GST_VIDEO_INFO_PAR_N (info);
440 par_d = GST_VIDEO_INFO_PAR_D (info);
445 /* get display's PAR */
446 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
447 display_par_n = widget->priv->par_n;
448 display_par_d = widget->priv->par_d;
454 ok = gst_video_calculate_display_ratio (&display_ratio_num,
455 &display_ratio_den, width, height, par_n, par_d, display_par_n,
461 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
463 if (height % display_ratio_den == 0) {
464 GST_DEBUG ("keeping video height");
465 widget->priv->display_width = (guint)
466 gst_util_uint64_scale_int (height, display_ratio_num,
468 widget->priv->display_height = height;
469 } else if (width % display_ratio_num == 0) {
470 GST_DEBUG ("keeping video width");
471 widget->priv->display_width = width;
472 widget->priv->display_height = (guint)
473 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
475 GST_DEBUG ("approximating while keeping video height");
476 widget->priv->display_width = (guint)
477 gst_util_uint64_scale_int (height, display_ratio_num,
479 widget->priv->display_height = height;
481 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
482 widget->priv->display_height);
488 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
492 g_return_val_if_fail (widget != NULL, FALSE);
493 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
494 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
496 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
499 if (!gst_video_info_from_caps (&v_info, caps))
502 g_mutex_lock (&widget->priv->lock);
506 gst_caps_replace (&widget->priv->caps, caps);
508 if (!_calculate_par (widget, &v_info)) {
509 g_mutex_unlock (&widget->priv->lock);
513 widget->priv->v_info = v_info;
514 widget->priv->negotiated = TRUE;
516 g_mutex_unlock (&widget->priv->lock);
522 qt_item_get_qt_context (QtGLVideoItem * qt_item)
524 g_return_val_if_fail (qt_item != NULL, NULL);
526 if (!qt_item->priv->other_context)
529 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
533 qt_item_get_context (QtGLVideoItem * qt_item)
535 g_return_val_if_fail (qt_item != NULL, NULL);
537 if (!qt_item->priv->context)
540 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
544 qt_item_get_display (QtGLVideoItem * qt_item)
546 g_return_val_if_fail (qt_item != NULL, NULL);
548 if (!qt_item->priv->display)
551 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);