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);
210 tex = new GstQSGTexture ();
211 tex->setCaps (this->priv->caps);
212 tex->setBuffer (this->priv->buffer);
213 texNode->setTexture (tex);
215 if (this->priv->force_aspect_ratio) {
216 src.w = this->priv->display_width;
217 src.h = this->priv->display_height;
219 dst.x = boundingRect().x();
220 dst.y = boundingRect().y();
221 dst.w = boundingRect().width();
222 dst.h = boundingRect().height();
224 gst_video_sink_center_rect (src, dst, &result, TRUE);
226 result.x = boundingRect().x();
227 result.y = boundingRect().y();
228 result.w = boundingRect().width();
229 result.h = boundingRect().height();
232 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
234 gst_gl_context_activate (this->priv->other_context, FALSE);
235 g_mutex_unlock (&this->priv->lock);
241 _reset (QtGLVideoItem * qt_item)
243 gst_buffer_replace (&qt_item->priv->buffer, NULL);
245 gst_caps_replace (&qt_item->priv->caps, NULL);
247 qt_item->priv->negotiated = FALSE;
248 qt_item->priv->initted = FALSE;
252 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
254 g_return_if_fail (widget != NULL);
255 g_return_if_fail (widget->priv->negotiated);
257 g_mutex_lock (&widget->priv->lock);
259 gst_buffer_replace (&widget->priv->buffer, buffer);
261 QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
263 g_mutex_unlock (&widget->priv->lock);
267 QtGLVideoItem::onSceneGraphInitialized ()
269 GstGLPlatform platform;
273 GST_DEBUG ("scene graph initialization with Qt GL context %p",
274 this->window()->openglContext ());
276 if (this->priv->qt_context == this->window()->openglContext ())
279 this->priv->qt_context = this->window()->openglContext ();
280 if (this->priv->qt_context == NULL) {
281 g_assert_not_reached ();
285 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
286 if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
287 platform = GST_GL_PLATFORM_GLX;
288 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
289 gl_handle = gst_gl_context_get_current_gl_context (platform);
291 this->priv->other_context =
292 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
296 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
297 if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
298 platform = GST_GL_PLATFORM_EGL;
299 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
300 gl_handle = gst_gl_context_get_current_gl_context (platform);
302 this->priv->other_context =
303 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
307 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
308 if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
309 platform = GST_GL_PLATFORM_EGL;
310 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
311 gl_handle = gst_gl_context_get_current_gl_context (platform);
313 this->priv->other_context =
314 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
318 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
319 if (this->priv->display) {
320 platform = GST_GL_PLATFORM_CGL;
321 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
322 gl_handle = gst_gl_context_get_current_gl_context (platform);
324 this->priv->other_context =
325 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
329 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
330 if (this->priv->display) {
331 platform = GST_GL_PLATFORM_EAGL;
332 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
333 gl_handle = gst_gl_context_get_current_gl_context (platform);
335 this->priv->other_context =
336 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
345 if (this->priv->other_context) {
346 GError *error = NULL;
348 gst_gl_context_activate (this->priv->other_context, TRUE);
349 if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
350 GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
351 g_object_unref (this->priv->other_context);
352 this->priv->other_context = NULL;
354 gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
355 gst_gl_context_activate (this->priv->other_context, FALSE);
359 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
360 this->priv->other_context);
364 QtGLVideoItem::onSceneGraphInvalidated ()
366 GST_FIXME ("%p scene graph invalidated", this);
370 qt_item_init_winsys (QtGLVideoItem * widget)
372 g_return_val_if_fail (widget != NULL, FALSE);
374 g_mutex_lock (&widget->priv->lock);
376 if (widget->priv->display && widget->priv->qt_context
377 && widget->priv->other_context && widget->priv->context) {
378 /* already have the necessary state */
379 g_mutex_unlock (&widget->priv->lock);
383 if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
384 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
385 widget, widget->priv->display);
386 g_mutex_unlock (&widget->priv->lock);
390 if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
391 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
392 widget->priv->other_context);
393 g_mutex_unlock (&widget->priv->lock);
397 widget->priv->context = gst_gl_context_new (widget->priv->display);
399 if (!widget->priv->context) {
400 g_mutex_unlock (&widget->priv->lock);
404 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
407 g_mutex_unlock (&widget->priv->lock);
412 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
415 if (win->isSceneGraphInitialized())
416 onSceneGraphInitialized();
418 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
420 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
422 this->priv->qt_context = NULL;
427 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
432 gint display_par_n, display_par_d;
433 guint display_ratio_num, display_ratio_den;
435 width = GST_VIDEO_INFO_WIDTH (info);
436 height = GST_VIDEO_INFO_HEIGHT (info);
438 par_n = GST_VIDEO_INFO_PAR_N (info);
439 par_d = GST_VIDEO_INFO_PAR_D (info);
444 /* get display's PAR */
445 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
446 display_par_n = widget->priv->par_n;
447 display_par_d = widget->priv->par_d;
453 ok = gst_video_calculate_display_ratio (&display_ratio_num,
454 &display_ratio_den, width, height, par_n, par_d, display_par_n,
460 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
462 if (height % display_ratio_den == 0) {
463 GST_DEBUG ("keeping video height");
464 widget->priv->display_width = (guint)
465 gst_util_uint64_scale_int (height, display_ratio_num,
467 widget->priv->display_height = height;
468 } else if (width % display_ratio_num == 0) {
469 GST_DEBUG ("keeping video width");
470 widget->priv->display_width = width;
471 widget->priv->display_height = (guint)
472 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
474 GST_DEBUG ("approximating while keeping video height");
475 widget->priv->display_width = (guint)
476 gst_util_uint64_scale_int (height, display_ratio_num,
478 widget->priv->display_height = height;
480 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
481 widget->priv->display_height);
487 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
491 g_return_val_if_fail (widget != NULL, FALSE);
492 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
493 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
495 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
498 if (!gst_video_info_from_caps (&v_info, caps))
501 g_mutex_lock (&widget->priv->lock);
505 gst_caps_replace (&widget->priv->caps, caps);
507 if (!_calculate_par (widget, &v_info)) {
508 g_mutex_unlock (&widget->priv->lock);
512 widget->priv->v_info = v_info;
513 widget->priv->negotiated = TRUE;
515 g_mutex_unlock (&widget->priv->lock);
521 qt_item_get_qt_context (QtGLVideoItem * qt_item)
523 g_return_val_if_fail (qt_item != NULL, NULL);
525 if (!qt_item->priv->other_context)
528 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
532 qt_item_get_context (QtGLVideoItem * qt_item)
534 g_return_val_if_fail (qt_item != NULL, NULL);
536 if (!qt_item->priv->context)
539 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
543 qt_item_get_display (QtGLVideoItem * qt_item)
545 g_return_val_if_fail (qt_item != NULL, NULL);
547 if (!qt_item->priv->display)
550 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);