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"
30 #include "gstqtglutility.h"
32 #include <QtCore/QRunnable>
33 #include <QtCore/QMutexLocker>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QtQuick/QSGSimpleTextureNode>
39 * SECTION:gtkgstglwidget
40 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
41 * @see_also: #GtkGLArea, #GstBuffer
43 * #QtGLVideoItem is an #QQuickItem that renders GStreamer video buffers.
46 #define GST_CAT_DEFAULT qt_item_debug
47 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
49 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
50 #define DEFAULT_PAR_N 0
51 #define DEFAULT_PAR_D 1
56 PROP_FORCE_ASPECT_RATIO,
57 PROP_PIXEL_ASPECT_RATIO,
60 struct _QtGLVideoItemPrivate
65 gboolean force_aspect_ratio;
77 GstGLDisplay *display;
78 QOpenGLContext *qt_context;
79 GstGLContext *other_context;
80 GstGLContext *context;
83 class InitializeSceneGraph : public QRunnable
86 InitializeSceneGraph(QtGLVideoItem *item);
93 InitializeSceneGraph::InitializeSceneGraph(QtGLVideoItem *item) :
98 void InitializeSceneGraph::run()
100 item_->onSceneGraphInitialized();
103 QtGLVideoItem::QtGLVideoItem()
105 static volatile gsize _debug;
107 if (g_once_init_enter (&_debug)) {
108 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
109 g_once_init_leave (&_debug, 1);
111 this->m_openGlContextInitialized = false;
112 this->setFlag (QQuickItem::ItemHasContents, true);
114 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
116 this->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
117 this->priv->par_n = DEFAULT_PAR_N;
118 this->priv->par_d = DEFAULT_PAR_D;
120 g_mutex_init (&this->priv->lock);
122 this->priv->display = gst_qt_get_gl_display();
124 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
125 SLOT(handleWindowChanged(QQuickWindow*)));
127 this->proxy = QSharedPointer<QtGLVideoItemInterface>(new QtGLVideoItemInterface(this));
129 GST_DEBUG ("%p init Qt Video Item", this);
132 QtGLVideoItem::~QtGLVideoItem()
134 /* Before destroying the priv info, make sure
135 * no qmlglsink's will call in again, and that
136 * any ongoing calls are done by invalidating the proxy
138 GST_INFO ("Destroying QtGLVideoItem and invalidating the proxy");
139 proxy->invalidateRef();
142 g_mutex_clear (&this->priv->lock);
143 if (this->priv->context)
144 gst_object_unref(this->priv->context);
145 if (this->priv->other_context)
146 gst_object_unref(this->priv->other_context);
147 if (this->priv->display)
148 gst_object_unref(this->priv->display);
154 QtGLVideoItem::setDAR(gint num, gint den)
156 this->priv->par_n = num;
157 this->priv->par_d = den;
161 QtGLVideoItem::getDAR(gint * num, gint * den)
164 *num = this->priv->par_n;
166 *den = this->priv->par_d;
170 QtGLVideoItem::setForceAspectRatio(bool force_aspect_ratio)
172 this->priv->force_aspect_ratio = !!force_aspect_ratio;
176 QtGLVideoItem::getForceAspectRatio()
178 return this->priv->force_aspect_ratio;
182 QtGLVideoItem::itemInitialized()
184 return m_openGlContextInitialized;
188 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
189 UpdatePaintNodeData * updatePaintNodeData)
191 if (!m_openGlContextInitialized) {
195 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
196 GstVideoRectangle src, dst, result;
199 g_mutex_lock (&this->priv->lock);
200 gst_gl_context_activate (this->priv->other_context, TRUE);
202 GST_TRACE ("%p updatePaintNode", this);
204 if (!this->priv->caps) {
205 g_mutex_unlock (&this->priv->lock);
210 texNode = new QSGSimpleTextureNode ();
211 texNode->setOwnsTexture (true);
212 texNode->setTexture (new GstQSGTexture ());
215 tex = static_cast<GstQSGTexture *> (texNode->texture());
216 tex->setCaps (this->priv->caps);
217 tex->setBuffer (this->priv->buffer);
218 texNode->markDirty(QSGNode::DirtyMaterial);
220 if (this->priv->force_aspect_ratio) {
221 src.w = this->priv->display_width;
222 src.h = this->priv->display_height;
224 dst.x = boundingRect().x();
225 dst.y = boundingRect().y();
226 dst.w = boundingRect().width();
227 dst.h = boundingRect().height();
229 gst_video_sink_center_rect (src, dst, &result, TRUE);
231 result.x = boundingRect().x();
232 result.y = boundingRect().y();
233 result.w = boundingRect().width();
234 result.h = boundingRect().height();
237 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
239 gst_gl_context_activate (this->priv->other_context, FALSE);
240 g_mutex_unlock (&this->priv->lock);
246 _reset (QtGLVideoItem * qt_item)
248 gst_buffer_replace (&qt_item->priv->buffer, NULL);
250 gst_caps_replace (&qt_item->priv->caps, NULL);
252 qt_item->priv->negotiated = FALSE;
253 qt_item->priv->initted = FALSE;
257 QtGLVideoItemInterface::setBuffer (GstBuffer * buffer)
259 QMutexLocker locker(&lock);
264 if (!qt_item->priv->negotiated) {
265 GST_WARNING ("Got buffer on unnegotiated QtGLVideoItem. Dropping");
269 g_mutex_lock (&qt_item->priv->lock);
271 gst_buffer_replace (&qt_item->priv->buffer, buffer);
273 QMetaObject::invokeMethod(qt_item, "update", Qt::QueuedConnection);
275 g_mutex_unlock (&qt_item->priv->lock);
279 QtGLVideoItem::onSceneGraphInitialized ()
281 GST_DEBUG ("scene graph initialization with Qt GL context %p",
282 this->window()->openglContext ());
284 if (this->priv->qt_context == this->window()->openglContext ())
287 this->priv->qt_context = this->window()->openglContext ();
288 if (this->priv->qt_context == NULL) {
289 g_assert_not_reached ();
293 m_openGlContextInitialized = gst_qt_get_gl_wrapcontext (this->priv->display,
294 &this->priv->other_context, &this->priv->context);
296 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
297 this->priv->other_context);
299 emit itemInitializedChanged();
303 QtGLVideoItem::onSceneGraphInvalidated ()
305 GST_FIXME ("%p scene graph invalidated", this);
309 QtGLVideoItemInterface::initWinSys ()
311 QMutexLocker locker(&lock);
313 GError *error = NULL;
318 g_mutex_lock (&qt_item->priv->lock);
320 if (qt_item->priv->display && qt_item->priv->qt_context
321 && qt_item->priv->other_context && qt_item->priv->context) {
322 /* already have the necessary state */
323 g_mutex_unlock (&qt_item->priv->lock);
327 if (!GST_IS_GL_DISPLAY (qt_item->priv->display)) {
328 GST_ERROR ("%p failed to retrieve display connection %" GST_PTR_FORMAT,
329 qt_item, qt_item->priv->display);
330 g_mutex_unlock (&qt_item->priv->lock);
334 if (!GST_IS_GL_CONTEXT (qt_item->priv->other_context)) {
335 GST_ERROR ("%p failed to retrieve wrapped context %" GST_PTR_FORMAT, qt_item,
336 qt_item->priv->other_context);
337 g_mutex_unlock (&qt_item->priv->lock);
341 qt_item->priv->context = gst_gl_context_new (qt_item->priv->display);
343 if (!qt_item->priv->context) {
344 g_mutex_unlock (&qt_item->priv->lock);
348 if (!gst_gl_context_create (qt_item->priv->context, qt_item->priv->other_context,
350 GST_ERROR ("%s", error->message);
351 g_mutex_unlock (&qt_item->priv->lock);
355 g_mutex_unlock (&qt_item->priv->lock);
360 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
363 if (win->isSceneGraphInitialized())
364 win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage);
366 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
368 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
370 this->priv->qt_context = NULL;
375 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
380 gint display_par_n, display_par_d;
381 guint display_ratio_num, display_ratio_den;
383 width = GST_VIDEO_INFO_WIDTH (info);
384 height = GST_VIDEO_INFO_HEIGHT (info);
386 par_n = GST_VIDEO_INFO_PAR_N (info);
387 par_d = GST_VIDEO_INFO_PAR_D (info);
392 /* get display's PAR */
393 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
394 display_par_n = widget->priv->par_n;
395 display_par_d = widget->priv->par_d;
401 ok = gst_video_calculate_display_ratio (&display_ratio_num,
402 &display_ratio_den, width, height, par_n, par_d, display_par_n,
408 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
410 if (height % display_ratio_den == 0) {
411 GST_DEBUG ("keeping video height");
412 widget->priv->display_width = (guint)
413 gst_util_uint64_scale_int (height, display_ratio_num,
415 widget->priv->display_height = height;
416 } else if (width % display_ratio_num == 0) {
417 GST_DEBUG ("keeping video width");
418 widget->priv->display_width = width;
419 widget->priv->display_height = (guint)
420 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
422 GST_DEBUG ("approximating while keeping video height");
423 widget->priv->display_width = (guint)
424 gst_util_uint64_scale_int (height, display_ratio_num,
426 widget->priv->display_height = height;
428 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
429 widget->priv->display_height);
435 QtGLVideoItemInterface::setCaps (GstCaps * caps)
437 QMutexLocker locker(&lock);
440 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
441 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
446 if (qt_item->priv->caps && gst_caps_is_equal_fixed (qt_item->priv->caps, caps))
449 if (!gst_video_info_from_caps (&v_info, caps))
452 g_mutex_lock (&qt_item->priv->lock);
456 gst_caps_replace (&qt_item->priv->caps, caps);
458 if (!_calculate_par (qt_item, &v_info)) {
459 g_mutex_unlock (&qt_item->priv->lock);
463 qt_item->priv->v_info = v_info;
464 qt_item->priv->negotiated = TRUE;
466 g_mutex_unlock (&qt_item->priv->lock);
472 QtGLVideoItemInterface::getQtContext ()
474 QMutexLocker locker(&lock);
476 if (!qt_item || !qt_item->priv->other_context)
479 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
483 QtGLVideoItemInterface::getContext ()
485 QMutexLocker locker(&lock);
487 if (!qt_item || !qt_item->priv->context)
490 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
494 QtGLVideoItemInterface::getDisplay()
496 QMutexLocker locker(&lock);
498 if (!qt_item || !qt_item->priv->display)
501 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);
505 QtGLVideoItemInterface::setDAR(gint num, gint den)
507 QMutexLocker locker(&lock);
510 qt_item->setDAR(num, den);
514 QtGLVideoItemInterface::getDAR(gint * num, gint * den)
516 QMutexLocker locker(&lock);
519 qt_item->getDAR (num, den);
523 QtGLVideoItemInterface::setForceAspectRatio(bool force_aspect_ratio)
525 QMutexLocker locker(&lock);
528 qt_item->setForceAspectRatio(force_aspect_ratio);
532 QtGLVideoItemInterface::getForceAspectRatio()
534 QMutexLocker locker(&lock);
537 return qt_item->getForceAspectRatio();
541 QtGLVideoItemInterface::invalidateRef()
543 QMutexLocker locker(&lock);