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 <QGuiApplication>
32 #include <QQuickWindow>
33 #include <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>
51 * SECTION:gtkgstglwidget
52 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
53 * @see_also: #GtkGLArea, #GstBuffer
55 * #QtGLVideoItem is an #GtkWidget that renders GStreamer video buffers.
58 #define GST_CAT_DEFAULT qt_item_debug
59 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
61 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
62 GTK_TYPE_GST_GL_WIDGET, QtGLVideoItemPrivate))
64 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
65 #define DEFAULT_PAR_N 0
66 #define DEFAULT_PAR_D 1
71 PROP_FORCE_ASPECT_RATIO,
72 PROP_PIXEL_ASPECT_RATIO,
75 struct _QtGLVideoItemPrivate
80 gboolean force_aspect_ratio;
92 GstGLDisplay *display;
93 QOpenGLContext *qt_context;
94 GstGLContext *other_context;
95 GstGLContext *context;
98 QtGLVideoItem::QtGLVideoItem()
100 QGuiApplication *app = dynamic_cast<QGuiApplication *> (QCoreApplication::instance ());
101 static volatile gsize _debug;
103 g_assert (app != NULL);
105 if (g_once_init_enter (&_debug)) {
106 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
107 g_once_init_leave (&_debug, 1);
110 this->setFlag (QQuickItem::ItemHasContents, true);
112 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
114 this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
115 this->priv->par_n = DEFAULT_PAR_N;
116 this->priv->par_d = DEFAULT_PAR_D;
118 g_mutex_init (&this->priv->lock);
120 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
121 if (QString::fromUtf8 ("xcb") == app->platformName())
122 this->priv->display = (GstGLDisplay *)
123 gst_gl_display_x11_new_with_display (QX11Info::display ());
125 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
126 if (QString::fromUtf8 ("android") == app->platformName())
127 this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
130 if (!this->priv->display)
131 this->priv->display = gst_gl_display_new ();
133 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
134 SLOT(handleWindowChanged(QQuickWindow*)));
136 GST_DEBUG ("%p init Qt Video Item", this);
139 QtGLVideoItem::~QtGLVideoItem()
141 g_mutex_clear (&this->priv->lock);
148 QtGLVideoItem::setDAR(gint num, gint den)
150 this->priv->par_n = num;
151 this->priv->par_d = den;
155 QtGLVideoItem::getDAR(gint * num, gint * den)
158 *num = this->priv->par_n;
160 *den = this->priv->par_d;
164 QtGLVideoItem::setForceAspectRatio(bool far)
166 this->priv->force_aspect_ratio = far;
170 QtGLVideoItem::getForceAspectRatio()
172 return this->priv->force_aspect_ratio;
176 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
177 UpdatePaintNodeData * updatePaintNodeData)
179 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
180 GstVideoRectangle src, dst, result;
183 g_mutex_lock (&this->priv->lock);
184 gst_gl_context_activate (this->priv->other_context, TRUE);
186 GST_TRACE ("%p updatePaintNode", this);
188 if (!this->priv->caps) {
189 g_mutex_unlock (&this->priv->lock);
194 texNode = new QSGSimpleTextureNode ();
195 texNode->setOwnsTexture (true);
197 tex = static_cast<GstQSGTexture *> (texNode->texture());
200 tex = new GstQSGTexture ();
201 tex->setCaps (this->priv->caps);
202 tex->setBuffer (this->priv->buffer);
203 texNode->setTexture (tex);
205 if (this->priv->force_aspect_ratio) {
206 src.w = this->priv->display_width;
207 src.h = this->priv->display_height;
209 dst.x = boundingRect().x();
210 dst.y = boundingRect().y();
211 dst.w = boundingRect().width();
212 dst.h = boundingRect().height();
214 gst_video_sink_center_rect (src, dst, &result, TRUE);
216 result.x = boundingRect().x();
217 result.y = boundingRect().y();
218 result.w = boundingRect().width();
219 result.h = boundingRect().height();
222 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
224 gst_gl_context_activate (this->priv->other_context, FALSE);
225 g_mutex_unlock (&this->priv->lock);
231 _reset (QtGLVideoItem * qt_item)
233 gst_buffer_replace (&qt_item->priv->buffer, NULL);
235 gst_caps_replace (&qt_item->priv->caps, NULL);
237 qt_item->priv->negotiated = FALSE;
238 qt_item->priv->initted = FALSE;
242 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
244 g_return_if_fail (widget != NULL);
245 g_return_if_fail (widget->priv->negotiated);
247 g_mutex_lock (&widget->priv->lock);
249 gst_buffer_replace (&widget->priv->buffer, buffer);
251 QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
253 g_mutex_unlock (&widget->priv->lock);
257 QtGLVideoItem::onSceneGraphInitialized ()
259 GstGLPlatform platform;
263 GST_DEBUG ("scene graph initialization with Qt GL context %p",
264 this->window()->openglContext ());
266 if (this->priv->qt_context == this->window()->openglContext ())
269 this->priv->qt_context = this->window()->openglContext ();
270 if (this->priv->qt_context == NULL) {
271 g_assert_not_reached ();
275 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
276 if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
277 platform = GST_GL_PLATFORM_GLX;
278 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
279 gl_handle = gst_gl_context_get_current_gl_context (platform);
281 this->priv->other_context =
282 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
286 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
287 if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
288 platform = GST_GL_PLATFORM_EGL;
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_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
298 if (GST_IS_GL_DISPLAY_EGL (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,
313 if (this->priv->other_context) {
314 GError *error = NULL;
316 gst_gl_context_activate (this->priv->other_context, TRUE);
317 if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
318 GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
319 g_object_unref (this->priv->other_context);
320 this->priv->other_context = NULL;
322 gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
323 gst_gl_context_activate (this->priv->other_context, FALSE);
327 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
328 this->priv->other_context);
332 QtGLVideoItem::onSceneGraphInvalidated ()
334 GST_FIXME ("%p scene graph invalidated", this);
338 qt_item_init_winsys (QtGLVideoItem * widget)
340 g_return_val_if_fail (widget != NULL, FALSE);
342 g_mutex_lock (&widget->priv->lock);
344 if (widget->priv->display && widget->priv->qt_context
345 && widget->priv->other_context && widget->priv->context) {
346 /* already have the necessary state */
347 g_mutex_unlock (&widget->priv->lock);
351 if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
352 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
353 widget, widget->priv->display);
354 g_mutex_unlock (&widget->priv->lock);
358 if (!GST_GL_IS_CONTEXT (widget->priv->other_context)) {
359 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
360 widget->priv->other_context);
361 g_mutex_unlock (&widget->priv->lock);
365 widget->priv->context = gst_gl_context_new (widget->priv->display);
367 if (!widget->priv->context) {
368 g_mutex_unlock (&widget->priv->lock);
372 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
375 g_mutex_unlock (&widget->priv->lock);
380 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
383 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
384 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
386 this->priv->qt_context = NULL;
391 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
396 gint display_par_n, display_par_d;
397 guint display_ratio_num, display_ratio_den;
399 width = GST_VIDEO_INFO_WIDTH (info);
400 height = GST_VIDEO_INFO_HEIGHT (info);
402 par_n = GST_VIDEO_INFO_PAR_N (info);
403 par_d = GST_VIDEO_INFO_PAR_D (info);
408 /* get display's PAR */
409 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
410 display_par_n = widget->priv->par_n;
411 display_par_d = widget->priv->par_d;
417 ok = gst_video_calculate_display_ratio (&display_ratio_num,
418 &display_ratio_den, width, height, par_n, par_d, display_par_n,
424 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
426 if (height % display_ratio_den == 0) {
427 GST_DEBUG ("keeping video height");
428 widget->priv->display_width = (guint)
429 gst_util_uint64_scale_int (height, display_ratio_num,
431 widget->priv->display_height = height;
432 } else if (width % display_ratio_num == 0) {
433 GST_DEBUG ("keeping video width");
434 widget->priv->display_width = width;
435 widget->priv->display_height = (guint)
436 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
438 GST_DEBUG ("approximating while keeping video height");
439 widget->priv->display_width = (guint)
440 gst_util_uint64_scale_int (height, display_ratio_num,
442 widget->priv->display_height = height;
444 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
445 widget->priv->display_height);
451 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
455 g_return_val_if_fail (widget != NULL, FALSE);
456 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
457 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
459 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
462 if (!gst_video_info_from_caps (&v_info, caps))
465 g_mutex_lock (&widget->priv->lock);
469 gst_caps_replace (&widget->priv->caps, caps);
471 if (!_calculate_par (widget, &v_info)) {
472 g_mutex_unlock (&widget->priv->lock);
476 widget->priv->v_info = v_info;
477 widget->priv->negotiated = TRUE;
479 g_mutex_unlock (&widget->priv->lock);
485 qt_item_get_qt_context (QtGLVideoItem * qt_item)
487 g_return_val_if_fail (qt_item != NULL, NULL);
489 if (!qt_item->priv->other_context)
492 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
496 qt_item_get_context (QtGLVideoItem * qt_item)
498 g_return_val_if_fail (qt_item != NULL, NULL);
500 if (!qt_item->priv->context)
503 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
507 qt_item_get_display (QtGLVideoItem * qt_item)
509 g_return_val_if_fail (qt_item != NULL, NULL);
511 if (!qt_item->priv->display)
514 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);