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 <QtCore/QRunnable>
32 #include <QtGui/QGuiApplication>
33 #include <QtQuick/QQuickWindow>
34 #include <QtQuick/QSGSimpleTextureNode>
36 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (HAVE_QT_X11)
38 #include <gst/gl/x11/gstgldisplay_x11.h>
39 #include <gst/gl/x11/gstglcontext_glx.h>
42 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
43 #include <gst/gl/wayland/gstgldisplay_wayland.h>
46 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
47 #include <gst/gl/egl/gstgldisplay_egl.h>
48 #include <gst/gl/egl/gstglcontext_egl.h>
51 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
52 #include <gst/gl/coaoa/gstgldisplay_cocoa.h>
56 * SECTION:gtkgstglwidget
57 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
58 * @see_also: #GtkGLArea, #GstBuffer
60 * #QtGLVideoItem is an #GtkWidget that renders GStreamer video buffers.
63 #define GST_CAT_DEFAULT qt_item_debug
64 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
66 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
67 GTK_TYPE_GST_GL_WIDGET, QtGLVideoItemPrivate))
69 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
70 #define DEFAULT_PAR_N 0
71 #define DEFAULT_PAR_D 1
76 PROP_FORCE_ASPECT_RATIO,
77 PROP_PIXEL_ASPECT_RATIO,
80 struct _QtGLVideoItemPrivate
85 gboolean force_aspect_ratio;
97 GstGLDisplay *display;
98 QOpenGLContext *qt_context;
99 GstGLContext *other_context;
100 GstGLContext *context;
103 class InitializeSceneGraph : public QRunnable
106 InitializeSceneGraph(QtGLVideoItem *item);
110 QtGLVideoItem *item_;
113 InitializeSceneGraph::InitializeSceneGraph(QtGLVideoItem *item) :
118 void InitializeSceneGraph::run()
120 item_->onSceneGraphInitialized();
123 QtGLVideoItem::QtGLVideoItem()
125 QGuiApplication *app = dynamic_cast<QGuiApplication *> (QCoreApplication::instance ());
126 static volatile gsize _debug;
128 g_assert (app != NULL);
130 if (g_once_init_enter (&_debug)) {
131 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
132 g_once_init_leave (&_debug, 1);
134 this->m_openGlContextInitialized = false;
135 this->setFlag (QQuickItem::ItemHasContents, true);
137 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
139 this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
140 this->priv->par_n = DEFAULT_PAR_N;
141 this->priv->par_d = DEFAULT_PAR_D;
143 g_mutex_init (&this->priv->lock);
145 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
146 if (QString::fromUtf8 ("xcb") == app->platformName())
147 this->priv->display = (GstGLDisplay *)
148 gst_gl_display_x11_new_with_display (QX11Info::display ());
150 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
151 if (QString::fromUtf8 ("android") == app->platformName())
152 this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
154 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
155 if (QString::fromUtf8 ("cocoa") == app->platformName())
156 this->priv->display = (GstGLDisplay *) gst_gl_display_cocoa_new ();
158 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
159 if (QString::fromUtf8 ("ios") == app->platformName())
160 this->priv->display = gst_gl_display_new ();
163 if (!this->priv->display)
164 this->priv->display = gst_gl_display_new ();
166 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
167 SLOT(handleWindowChanged(QQuickWindow*)));
169 GST_DEBUG ("%p init Qt Video Item", this);
172 QtGLVideoItem::~QtGLVideoItem()
174 g_mutex_clear (&this->priv->lock);
181 QtGLVideoItem::setDAR(gint num, gint den)
183 this->priv->par_n = num;
184 this->priv->par_d = den;
188 QtGLVideoItem::getDAR(gint * num, gint * den)
191 *num = this->priv->par_n;
193 *den = this->priv->par_d;
197 QtGLVideoItem::setForceAspectRatio(bool far)
199 this->priv->force_aspect_ratio = far;
203 QtGLVideoItem::getForceAspectRatio()
205 return this->priv->force_aspect_ratio;
209 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
210 UpdatePaintNodeData * updatePaintNodeData)
212 if (!m_openGlContextInitialized) {
216 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
217 GstVideoRectangle src, dst, result;
220 g_mutex_lock (&this->priv->lock);
221 gst_gl_context_activate (this->priv->other_context, TRUE);
223 GST_TRACE ("%p updatePaintNode", this);
225 if (!this->priv->caps) {
226 g_mutex_unlock (&this->priv->lock);
231 texNode = new QSGSimpleTextureNode ();
232 texNode->setOwnsTexture (true);
233 texNode->setTexture (new GstQSGTexture ());
236 tex = static_cast<GstQSGTexture *> (texNode->texture());
237 tex->setCaps (this->priv->caps);
238 tex->setBuffer (this->priv->buffer);
239 texNode->markDirty(QSGNode::DirtyMaterial);
241 if (this->priv->force_aspect_ratio) {
242 src.w = this->priv->display_width;
243 src.h = this->priv->display_height;
245 dst.x = boundingRect().x();
246 dst.y = boundingRect().y();
247 dst.w = boundingRect().width();
248 dst.h = boundingRect().height();
250 gst_video_sink_center_rect (src, dst, &result, TRUE);
252 result.x = boundingRect().x();
253 result.y = boundingRect().y();
254 result.w = boundingRect().width();
255 result.h = boundingRect().height();
258 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
260 gst_gl_context_activate (this->priv->other_context, FALSE);
261 g_mutex_unlock (&this->priv->lock);
267 _reset (QtGLVideoItem * qt_item)
269 gst_buffer_replace (&qt_item->priv->buffer, NULL);
271 gst_caps_replace (&qt_item->priv->caps, NULL);
273 qt_item->priv->negotiated = FALSE;
274 qt_item->priv->initted = FALSE;
278 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
280 g_return_if_fail (widget != NULL);
281 g_return_if_fail (widget->priv->negotiated);
283 g_mutex_lock (&widget->priv->lock);
285 gst_buffer_replace (&widget->priv->buffer, buffer);
287 QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
289 g_mutex_unlock (&widget->priv->lock);
293 QtGLVideoItem::onSceneGraphInitialized ()
295 GstGLPlatform platform;
299 GST_DEBUG ("scene graph initialization with Qt GL context %p",
300 this->window()->openglContext ());
302 if (this->priv->qt_context == this->window()->openglContext ())
305 this->priv->qt_context = this->window()->openglContext ();
306 if (this->priv->qt_context == NULL) {
307 g_assert_not_reached ();
311 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
312 if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
313 platform = GST_GL_PLATFORM_GLX;
314 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
315 gl_handle = gst_gl_context_get_current_gl_context (platform);
317 this->priv->other_context =
318 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
322 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
323 if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
324 platform = GST_GL_PLATFORM_EGL;
325 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
326 gl_handle = gst_gl_context_get_current_gl_context (platform);
328 this->priv->other_context =
329 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
333 #if GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
334 if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
335 platform = GST_GL_PLATFORM_EGL;
336 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
337 gl_handle = gst_gl_context_get_current_gl_context (platform);
339 this->priv->other_context =
340 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
344 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
345 if (this->priv->display) {
346 platform = GST_GL_PLATFORM_CGL;
347 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
348 gl_handle = gst_gl_context_get_current_gl_context (platform);
350 this->priv->other_context =
351 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
355 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
356 if (this->priv->display) {
357 platform = GST_GL_PLATFORM_EAGL;
358 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
359 gl_handle = gst_gl_context_get_current_gl_context (platform);
361 this->priv->other_context =
362 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
371 if (this->priv->other_context) {
372 GError *error = NULL;
374 gst_gl_context_activate (this->priv->other_context, TRUE);
375 if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
376 GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
377 g_object_unref (this->priv->other_context);
378 this->priv->other_context = NULL;
380 gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
381 gst_gl_context_activate (this->priv->other_context, FALSE);
382 m_openGlContextInitialized = true;
386 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
387 this->priv->other_context);
391 QtGLVideoItem::onSceneGraphInvalidated ()
393 GST_FIXME ("%p scene graph invalidated", this);
397 qt_item_init_winsys (QtGLVideoItem * widget)
399 g_return_val_if_fail (widget != NULL, FALSE);
401 g_mutex_lock (&widget->priv->lock);
403 if (widget->priv->display && widget->priv->qt_context
404 && widget->priv->other_context && widget->priv->context) {
405 /* already have the necessary state */
406 g_mutex_unlock (&widget->priv->lock);
410 if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
411 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
412 widget, widget->priv->display);
413 g_mutex_unlock (&widget->priv->lock);
417 if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
418 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
419 widget->priv->other_context);
420 g_mutex_unlock (&widget->priv->lock);
424 widget->priv->context = gst_gl_context_new (widget->priv->display);
426 if (!widget->priv->context) {
427 g_mutex_unlock (&widget->priv->lock);
431 gst_gl_context_create (widget->priv->context, widget->priv->other_context,
434 g_mutex_unlock (&widget->priv->lock);
439 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
442 if (win->isSceneGraphInitialized())
443 win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage);
445 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
447 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
449 this->priv->qt_context = NULL;
454 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
459 gint display_par_n, display_par_d;
460 guint display_ratio_num, display_ratio_den;
462 width = GST_VIDEO_INFO_WIDTH (info);
463 height = GST_VIDEO_INFO_HEIGHT (info);
465 par_n = GST_VIDEO_INFO_PAR_N (info);
466 par_d = GST_VIDEO_INFO_PAR_D (info);
471 /* get display's PAR */
472 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
473 display_par_n = widget->priv->par_n;
474 display_par_d = widget->priv->par_d;
480 ok = gst_video_calculate_display_ratio (&display_ratio_num,
481 &display_ratio_den, width, height, par_n, par_d, display_par_n,
487 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
489 if (height % display_ratio_den == 0) {
490 GST_DEBUG ("keeping video height");
491 widget->priv->display_width = (guint)
492 gst_util_uint64_scale_int (height, display_ratio_num,
494 widget->priv->display_height = height;
495 } else if (width % display_ratio_num == 0) {
496 GST_DEBUG ("keeping video width");
497 widget->priv->display_width = width;
498 widget->priv->display_height = (guint)
499 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
501 GST_DEBUG ("approximating while keeping video height");
502 widget->priv->display_width = (guint)
503 gst_util_uint64_scale_int (height, display_ratio_num,
505 widget->priv->display_height = height;
507 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
508 widget->priv->display_height);
514 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
518 g_return_val_if_fail (widget != NULL, FALSE);
519 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
520 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
522 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
525 if (!gst_video_info_from_caps (&v_info, caps))
528 g_mutex_lock (&widget->priv->lock);
532 gst_caps_replace (&widget->priv->caps, caps);
534 if (!_calculate_par (widget, &v_info)) {
535 g_mutex_unlock (&widget->priv->lock);
539 widget->priv->v_info = v_info;
540 widget->priv->negotiated = TRUE;
542 g_mutex_unlock (&widget->priv->lock);
548 qt_item_get_qt_context (QtGLVideoItem * qt_item)
550 g_return_val_if_fail (qt_item != NULL, NULL);
552 if (!qt_item->priv->other_context)
555 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
559 qt_item_get_context (QtGLVideoItem * qt_item)
561 g_return_val_if_fail (qt_item != NULL, NULL);
563 if (!qt_item->priv->context)
566 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
570 qt_item_get_display (QtGLVideoItem * qt_item)
572 g_return_val_if_fail (qt_item != NULL, NULL);
574 if (!qt_item->priv->display)
577 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);