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>
35 #include <qpa/qplatformnativeinterface.h>
37 #if GST_GL_HAVE_WINDOW_X11 && GST_GL_HAVE_PLATFORM_GLX && defined (HAVE_QT_X11)
39 #include <gst/gl/x11/gstgldisplay_x11.h>
40 #include <gst/gl/x11/gstglcontext_glx.h>
43 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
44 #include <gst/gl/wayland/gstgldisplay_wayland.h>
47 #if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
48 #include <gst/gl/egl/gstgldisplay_egl.h>
49 #include <gst/gl/egl/gstglcontext_egl.h>
52 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
53 #include <gst/gl/coaoa/gstgldisplay_cocoa.h>
57 * SECTION:gtkgstglwidget
58 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
59 * @see_also: #GtkGLArea, #GstBuffer
61 * #QtGLVideoItem is an #GtkWidget that renders GStreamer video buffers.
64 #define GST_CAT_DEFAULT qt_item_debug
65 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
67 #define GTK_GST_GL_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
68 GTK_TYPE_GST_GL_WIDGET, QtGLVideoItemPrivate))
70 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
71 #define DEFAULT_PAR_N 0
72 #define DEFAULT_PAR_D 1
77 PROP_FORCE_ASPECT_RATIO,
78 PROP_PIXEL_ASPECT_RATIO,
81 struct _QtGLVideoItemPrivate
86 gboolean force_aspect_ratio;
98 GstGLDisplay *display;
99 QOpenGLContext *qt_context;
100 GstGLContext *other_context;
101 GstGLContext *context;
104 class InitializeSceneGraph : public QRunnable
107 InitializeSceneGraph(QtGLVideoItem *item);
111 QtGLVideoItem *item_;
114 InitializeSceneGraph::InitializeSceneGraph(QtGLVideoItem *item) :
119 void InitializeSceneGraph::run()
121 item_->onSceneGraphInitialized();
124 QtGLVideoItem::QtGLVideoItem()
126 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
127 static volatile gsize _debug;
129 g_assert (app != NULL);
131 if (g_once_init_enter (&_debug)) {
132 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
133 g_once_init_leave (&_debug, 1);
135 this->m_openGlContextInitialized = false;
136 this->setFlag (QQuickItem::ItemHasContents, true);
138 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
140 this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
141 this->priv->par_n = DEFAULT_PAR_N;
142 this->priv->par_d = DEFAULT_PAR_D;
144 g_mutex_init (&this->priv->lock);
146 GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
148 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
149 if (QString::fromUtf8 ("xcb") == app->platformName())
150 this->priv->display = (GstGLDisplay *)
151 gst_gl_display_x11_new_with_display (QX11Info::display ());
153 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
154 if (QString::fromUtf8 ("wayland") == app->platformName()
155 || QString::fromUtf8 ("wayland-egl") == app->platformName()){
156 struct wl_display * wayland_display;
157 QPlatformNativeInterface *native =
158 QGuiApplication::platformNativeInterface();
159 wayland_display = (struct wl_display *)
160 native->nativeResourceForWindow("display", NULL);
161 this->priv->display = (GstGLDisplay *)
162 gst_gl_display_wayland_new_with_display (wayland_display);
165 #if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
166 if (QString::fromUtf8 ("android") == app->platformName())
167 this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
168 #elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
169 if (QString::fromUtf8("eglfs") == app->platformName())
170 this->priv->display = (GstGLDisplay *) gst_gl_display_egl_new ();
173 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
174 if (QString::fromUtf8 ("cocoa") == app->platformName())
175 this->priv->display = (GstGLDisplay *) gst_gl_display_cocoa_new ();
177 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
178 if (QString::fromUtf8 ("ios") == app->platformName())
179 this->priv->display = gst_gl_display_new ();
182 if (!this->priv->display)
183 this->priv->display = gst_gl_display_new ();
185 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
186 SLOT(handleWindowChanged(QQuickWindow*)));
188 GST_DEBUG ("%p init Qt Video Item", this);
191 QtGLVideoItem::~QtGLVideoItem()
193 g_mutex_clear (&this->priv->lock);
194 if (this->priv->context)
195 gst_object_unref(this->priv->context);
196 if (this->priv->other_context)
197 gst_object_unref(this->priv->other_context);
203 QtGLVideoItem::setDAR(gint num, gint den)
205 this->priv->par_n = num;
206 this->priv->par_d = den;
210 QtGLVideoItem::getDAR(gint * num, gint * den)
213 *num = this->priv->par_n;
215 *den = this->priv->par_d;
219 QtGLVideoItem::setForceAspectRatio(bool far)
221 this->priv->force_aspect_ratio = far;
225 QtGLVideoItem::getForceAspectRatio()
227 return this->priv->force_aspect_ratio;
231 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
232 UpdatePaintNodeData * updatePaintNodeData)
234 if (!m_openGlContextInitialized) {
238 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
239 GstVideoRectangle src, dst, result;
242 g_mutex_lock (&this->priv->lock);
243 gst_gl_context_activate (this->priv->other_context, TRUE);
245 GST_TRACE ("%p updatePaintNode", this);
247 if (!this->priv->caps) {
248 g_mutex_unlock (&this->priv->lock);
253 texNode = new QSGSimpleTextureNode ();
254 texNode->setOwnsTexture (true);
255 texNode->setTexture (new GstQSGTexture ());
258 tex = static_cast<GstQSGTexture *> (texNode->texture());
259 tex->setCaps (this->priv->caps);
260 tex->setBuffer (this->priv->buffer);
261 texNode->markDirty(QSGNode::DirtyMaterial);
263 if (this->priv->force_aspect_ratio) {
264 src.w = this->priv->display_width;
265 src.h = this->priv->display_height;
267 dst.x = boundingRect().x();
268 dst.y = boundingRect().y();
269 dst.w = boundingRect().width();
270 dst.h = boundingRect().height();
272 gst_video_sink_center_rect (src, dst, &result, TRUE);
274 result.x = boundingRect().x();
275 result.y = boundingRect().y();
276 result.w = boundingRect().width();
277 result.h = boundingRect().height();
280 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
282 gst_gl_context_activate (this->priv->other_context, FALSE);
283 g_mutex_unlock (&this->priv->lock);
289 _reset (QtGLVideoItem * qt_item)
291 gst_buffer_replace (&qt_item->priv->buffer, NULL);
293 gst_caps_replace (&qt_item->priv->caps, NULL);
295 qt_item->priv->negotiated = FALSE;
296 qt_item->priv->initted = FALSE;
300 qt_item_set_buffer (QtGLVideoItem * widget, GstBuffer * buffer)
302 g_return_if_fail (widget != NULL);
303 g_return_if_fail (widget->priv->negotiated);
305 g_mutex_lock (&widget->priv->lock);
307 gst_buffer_replace (&widget->priv->buffer, buffer);
309 QMetaObject::invokeMethod(widget, "update", Qt::QueuedConnection);
311 g_mutex_unlock (&widget->priv->lock);
315 QtGLVideoItem::onSceneGraphInitialized ()
317 GstGLPlatform platform;
321 GST_DEBUG ("scene graph initialization with Qt GL context %p",
322 this->window()->openglContext ());
324 if (this->priv->qt_context == this->window()->openglContext ())
327 this->priv->qt_context = this->window()->openglContext ();
328 if (this->priv->qt_context == NULL) {
329 g_assert_not_reached ();
333 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
334 if (GST_IS_GL_DISPLAY_X11 (this->priv->display)) {
335 platform = GST_GL_PLATFORM_GLX;
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_WAYLAND && defined (HAVE_QT_WAYLAND)
345 if (GST_IS_GL_DISPLAY_WAYLAND (this->priv->display)) {
346 platform = GST_GL_PLATFORM_EGL;
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,
356 #if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
357 if (GST_IS_GL_DISPLAY_EGL (this->priv->display)) {
358 platform = GST_GL_PLATFORM_EGL;
359 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
360 gl_handle = gst_gl_context_get_current_gl_context (platform);
362 this->priv->other_context =
363 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
368 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_COCOA && defined (HAVE_QT_MAC)
369 if (this->priv->display) {
370 platform = GST_GL_PLATFORM_CGL;
371 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
372 gl_handle = gst_gl_context_get_current_gl_context (platform);
374 this->priv->other_context =
375 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
379 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
380 if (this->priv->display) {
381 platform = GST_GL_PLATFORM_EAGL;
382 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
383 gl_handle = gst_gl_context_get_current_gl_context (platform);
385 this->priv->other_context =
386 gst_gl_context_new_wrapped (this->priv->display, gl_handle,
395 if (this->priv->other_context) {
396 GError *error = NULL;
398 gst_gl_context_activate (this->priv->other_context, TRUE);
399 if (!gst_gl_context_fill_info (this->priv->other_context, &error)) {
400 GST_ERROR ("%p failed to retrieve qt context info: %s", this, error->message);
401 g_object_unref (this->priv->other_context);
402 this->priv->other_context = NULL;
404 gst_gl_display_filter_gl_api (this->priv->display, gst_gl_context_get_gl_api (this->priv->other_context));
405 gst_gl_context_activate (this->priv->other_context, FALSE);
406 m_openGlContextInitialized = true;
410 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
411 this->priv->other_context);
415 QtGLVideoItem::onSceneGraphInvalidated ()
417 GST_FIXME ("%p scene graph invalidated", this);
421 qt_item_init_winsys (QtGLVideoItem * widget)
423 GError *error = NULL;
425 g_return_val_if_fail (widget != NULL, FALSE);
427 g_mutex_lock (&widget->priv->lock);
429 if (widget->priv->display && widget->priv->qt_context
430 && widget->priv->other_context && widget->priv->context) {
431 /* already have the necessary state */
432 g_mutex_unlock (&widget->priv->lock);
436 if (!GST_IS_GL_DISPLAY (widget->priv->display)) {
437 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
438 widget, widget->priv->display);
439 g_mutex_unlock (&widget->priv->lock);
443 if (!GST_IS_GL_CONTEXT (widget->priv->other_context)) {
444 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, widget,
445 widget->priv->other_context);
446 g_mutex_unlock (&widget->priv->lock);
450 widget->priv->context = gst_gl_context_new (widget->priv->display);
452 if (!widget->priv->context) {
453 g_mutex_unlock (&widget->priv->lock);
457 if (!gst_gl_context_create (widget->priv->context, widget->priv->other_context,
459 GST_ERROR ("%s", error->message);
460 g_mutex_unlock (&widget->priv->lock);
464 g_mutex_unlock (&widget->priv->lock);
469 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
472 if (win->isSceneGraphInitialized())
473 win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage);
475 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
477 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
479 this->priv->qt_context = NULL;
484 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
489 gint display_par_n, display_par_d;
490 guint display_ratio_num, display_ratio_den;
492 width = GST_VIDEO_INFO_WIDTH (info);
493 height = GST_VIDEO_INFO_HEIGHT (info);
495 par_n = GST_VIDEO_INFO_PAR_N (info);
496 par_d = GST_VIDEO_INFO_PAR_D (info);
501 /* get display's PAR */
502 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
503 display_par_n = widget->priv->par_n;
504 display_par_d = widget->priv->par_d;
510 ok = gst_video_calculate_display_ratio (&display_ratio_num,
511 &display_ratio_den, width, height, par_n, par_d, display_par_n,
517 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
519 if (height % display_ratio_den == 0) {
520 GST_DEBUG ("keeping video height");
521 widget->priv->display_width = (guint)
522 gst_util_uint64_scale_int (height, display_ratio_num,
524 widget->priv->display_height = height;
525 } else if (width % display_ratio_num == 0) {
526 GST_DEBUG ("keeping video width");
527 widget->priv->display_width = width;
528 widget->priv->display_height = (guint)
529 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
531 GST_DEBUG ("approximating while keeping video height");
532 widget->priv->display_width = (guint)
533 gst_util_uint64_scale_int (height, display_ratio_num,
535 widget->priv->display_height = height;
537 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
538 widget->priv->display_height);
544 qt_item_set_caps (QtGLVideoItem * widget, GstCaps * caps)
548 g_return_val_if_fail (widget != NULL, FALSE);
549 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
550 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
552 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
555 if (!gst_video_info_from_caps (&v_info, caps))
558 g_mutex_lock (&widget->priv->lock);
562 gst_caps_replace (&widget->priv->caps, caps);
564 if (!_calculate_par (widget, &v_info)) {
565 g_mutex_unlock (&widget->priv->lock);
569 widget->priv->v_info = v_info;
570 widget->priv->negotiated = TRUE;
572 g_mutex_unlock (&widget->priv->lock);
578 qt_item_get_qt_context (QtGLVideoItem * qt_item)
580 g_return_val_if_fail (qt_item != NULL, NULL);
582 if (!qt_item->priv->other_context)
585 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
589 qt_item_get_context (QtGLVideoItem * qt_item)
591 g_return_val_if_fail (qt_item != NULL, NULL);
593 if (!qt_item->priv->context)
596 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
600 qt_item_get_display (QtGLVideoItem * qt_item)
602 g_return_val_if_fail (qt_item != NULL, NULL);
604 if (!qt_item->priv->display)
607 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);