1 /*
2 * GStreamer
3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include <gst/video/video.h>
28 #include "qtitem.h"
29 #include "gstqsgtexture.h"
30 #include "gstqtglutility.h"
31
32 #include <QtCore/QRunnable>
33 #include <QtCore/QMutexLocker>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QtQuick/QSGSimpleTextureNode>
37
38 /**
39 * SECTION:gtkgstglwidget
40 * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
41 * @see_also: #GtkGLArea, #GstBuffer
42 *
43 * #QtGLVideoItem is an #QQuickItem that renders GStreamer video buffers.
44 */
45
46 #define GST_CAT_DEFAULT qt_item_debug
47 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
48
49 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
50 #define DEFAULT_PAR_N 0
51 #define DEFAULT_PAR_D 1
52
53 enum
54 {
55 PROP_0,
56 PROP_FORCE_ASPECT_RATIO,
57 PROP_PIXEL_ASPECT_RATIO,
58 };
59
60 struct _QtGLVideoItemPrivate
61 {
62 GMutex lock;
63
64 /* properties */
65 gboolean force_aspect_ratio;
66 gint par_n, par_d;
67
68 gint display_width;
69 gint display_height;
70
71 gboolean negotiated;
72 GstBuffer *buffer;
73 GstCaps *caps;
74 GstVideoInfo v_info;
75
76 gboolean initted;
77 GstGLDisplay *display;
78 QOpenGLContext *qt_context;
79 GstGLContext *other_context;
80 GstGLContext *context;
81 };
82
83 class InitializeSceneGraph : public QRunnable
84 {
85 public:
86 InitializeSceneGraph(QtGLVideoItem *item);
87 void run();
88
89 private:
90 QtGLVideoItem *item_;
91 };
92
InitializeSceneGraph(QtGLVideoItem * item)93 InitializeSceneGraph::InitializeSceneGraph(QtGLVideoItem *item) :
94 item_(item)
95 {
96 }
97
run()98 void InitializeSceneGraph::run()
99 {
100 item_->onSceneGraphInitialized();
101 }
102
QtGLVideoItem()103 QtGLVideoItem::QtGLVideoItem()
104 {
105 static volatile gsize _debug;
106
107 if (g_once_init_enter ((unsigned long *)&_debug)) {
108 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwidget", 0, "Qt GL Widget");
109 g_once_init_leave (&_debug, 1);
110 }
111 this->m_openGlContextInitialized = false;
112 this->setFlag (QQuickItem::ItemHasContents, true);
113
114 this->priv = g_new0 (QtGLVideoItemPrivate, 1);
115
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;
119
120 g_mutex_init (&this->priv->lock);
121
122 this->priv->display = gst_qt_get_gl_display();
123
124 connect(this, SIGNAL(windowChanged(QQuickWindow*)), this,
125 SLOT(handleWindowChanged(QQuickWindow*)));
126
127 this->proxy = QSharedPointer<QtGLVideoItemInterface>(new QtGLVideoItemInterface(this));
128
129 GST_DEBUG ("%p init Qt Video Item", this);
130 }
131
~QtGLVideoItem()132 QtGLVideoItem::~QtGLVideoItem()
133 {
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
137 * pointer */
138 GST_INFO ("Destroying QtGLVideoItem and invalidating the proxy");
139 proxy->invalidateRef();
140 proxy.clear();
141
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);
149 g_free (this->priv);
150 this->priv = NULL;
151 }
152
153 void
setDAR(gint num,gint den)154 QtGLVideoItem::setDAR(gint num, gint den)
155 {
156 this->priv->par_n = num;
157 this->priv->par_d = den;
158 }
159
160 void
getDAR(gint * num,gint * den)161 QtGLVideoItem::getDAR(gint * num, gint * den)
162 {
163 if (num)
164 *num = this->priv->par_n;
165 if (den)
166 *den = this->priv->par_d;
167 }
168
169 void
setForceAspectRatio(bool force_aspect_ratio)170 QtGLVideoItem::setForceAspectRatio(bool force_aspect_ratio)
171 {
172 this->priv->force_aspect_ratio = !!force_aspect_ratio;
173 }
174
175 bool
getForceAspectRatio()176 QtGLVideoItem::getForceAspectRatio()
177 {
178 return this->priv->force_aspect_ratio;
179 }
180
181 bool
itemInitialized()182 QtGLVideoItem::itemInitialized()
183 {
184 return m_openGlContextInitialized;
185 }
186
187 QSGNode *
updatePaintNode(QSGNode * oldNode,UpdatePaintNodeData * updatePaintNodeData)188 QtGLVideoItem::updatePaintNode(QSGNode * oldNode,
189 UpdatePaintNodeData * updatePaintNodeData)
190 {
191 if (!m_openGlContextInitialized) {
192 return oldNode;
193 }
194
195 QSGSimpleTextureNode *texNode = static_cast<QSGSimpleTextureNode *> (oldNode);
196 GstVideoRectangle src, dst, result;
197 GstQSGTexture *tex;
198
199 g_mutex_lock (&this->priv->lock);
200 gst_gl_context_activate (this->priv->other_context, TRUE);
201
202 GST_TRACE ("%p updatePaintNode", this);
203
204 if (!this->priv->caps) {
205 g_mutex_unlock (&this->priv->lock);
206 return NULL;
207 }
208
209 if (!texNode) {
210 texNode = new QSGSimpleTextureNode ();
211 texNode->setOwnsTexture (true);
212 texNode->setTexture (new GstQSGTexture ());
213 }
214
215 tex = static_cast<GstQSGTexture *> (texNode->texture());
216 tex->setCaps (this->priv->caps);
217 tex->setBuffer (this->priv->buffer);
218 texNode->markDirty(QSGNode::DirtyMaterial);
219
220 if (this->priv->force_aspect_ratio) {
221 src.w = this->priv->display_width;
222 src.h = this->priv->display_height;
223
224 dst.x = boundingRect().x();
225 dst.y = boundingRect().y();
226 dst.w = boundingRect().width();
227 dst.h = boundingRect().height();
228
229 gst_video_sink_center_rect (src, dst, &result, TRUE);
230 } else {
231 result.x = boundingRect().x();
232 result.y = boundingRect().y();
233 result.w = boundingRect().width();
234 result.h = boundingRect().height();
235 }
236
237 texNode->setRect (QRectF (result.x, result.y, result.w, result.h));
238
239 gst_gl_context_activate (this->priv->other_context, FALSE);
240 g_mutex_unlock (&this->priv->lock);
241
242 return texNode;
243 }
244
245 static void
_reset(QtGLVideoItem * qt_item)246 _reset (QtGLVideoItem * qt_item)
247 {
248 gst_buffer_replace (&qt_item->priv->buffer, NULL);
249
250 gst_caps_replace (&qt_item->priv->caps, NULL);
251
252 qt_item->priv->negotiated = FALSE;
253 qt_item->priv->initted = FALSE;
254 }
255
256 void
setBuffer(GstBuffer * buffer)257 QtGLVideoItemInterface::setBuffer (GstBuffer * buffer)
258 {
259 QMutexLocker locker(&lock);
260
261 if (qt_item == NULL)
262 return;
263
264 if (!qt_item->priv->negotiated) {
265 GST_WARNING ("Got buffer on unnegotiated QtGLVideoItem. Dropping");
266 return;
267 }
268
269 g_mutex_lock (&qt_item->priv->lock);
270
271 gst_buffer_replace (&qt_item->priv->buffer, buffer);
272
273 QMetaObject::invokeMethod(qt_item, "update", Qt::QueuedConnection);
274
275 g_mutex_unlock (&qt_item->priv->lock);
276 }
277
278 void
onSceneGraphInitialized()279 QtGLVideoItem::onSceneGraphInitialized ()
280 {
281 GST_DEBUG ("scene graph initialization with Qt GL context %p",
282 this->window()->openglContext ());
283
284 if (this->priv->qt_context == this->window()->openglContext ())
285 return;
286
287 this->priv->qt_context = this->window()->openglContext ();
288 if (this->priv->qt_context == NULL) {
289 g_assert_not_reached ();
290 return;
291 }
292
293 m_openGlContextInitialized = gst_qt_get_gl_wrapcontext (this->priv->display,
294 &this->priv->other_context, &this->priv->context);
295
296 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
297 this->priv->other_context);
298
299 emit itemInitializedChanged();
300 }
301
302 void
onSceneGraphInvalidated()303 QtGLVideoItem::onSceneGraphInvalidated ()
304 {
305 GST_FIXME ("%p scene graph invalidated", this);
306 }
307
308 gboolean
initWinSys()309 QtGLVideoItemInterface::initWinSys ()
310 {
311 QMutexLocker locker(&lock);
312
313 GError *error = NULL;
314
315 if (qt_item == NULL)
316 return FALSE;
317
318 g_mutex_lock (&qt_item->priv->lock);
319
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);
324 return TRUE;
325 }
326
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);
331 return FALSE;
332 }
333
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);
338 return FALSE;
339 }
340
341 qt_item->priv->context = gst_gl_context_new (qt_item->priv->display);
342
343 if (!qt_item->priv->context) {
344 g_mutex_unlock (&qt_item->priv->lock);
345 return FALSE;
346 }
347
348 if (!gst_gl_context_create (qt_item->priv->context, qt_item->priv->other_context,
349 &error)) {
350 GST_ERROR ("%s", error->message);
351 g_mutex_unlock (&qt_item->priv->lock);
352 return FALSE;
353 }
354
355 g_mutex_unlock (&qt_item->priv->lock);
356 return TRUE;
357 }
358
359 void
handleWindowChanged(QQuickWindow * win)360 QtGLVideoItem::handleWindowChanged(QQuickWindow *win)
361 {
362 if (win) {
363 if (win->isSceneGraphInitialized())
364 win->scheduleRenderJob(new InitializeSceneGraph(this), QQuickWindow::BeforeSynchronizingStage);
365 else
366 connect(win, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
367
368 connect(win, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
369 } else {
370 this->priv->qt_context = NULL;
371 }
372 }
373
374 static gboolean
_calculate_par(QtGLVideoItem * widget,GstVideoInfo * info)375 _calculate_par (QtGLVideoItem * widget, GstVideoInfo * info)
376 {
377 gboolean ok;
378 gint width, height;
379 gint par_n, par_d;
380 gint display_par_n, display_par_d;
381 guint display_ratio_num, display_ratio_den;
382
383 width = GST_VIDEO_INFO_WIDTH (info);
384 height = GST_VIDEO_INFO_HEIGHT (info);
385
386 par_n = GST_VIDEO_INFO_PAR_N (info);
387 par_d = GST_VIDEO_INFO_PAR_D (info);
388
389 if (!par_n)
390 par_n = 1;
391
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;
396 } else {
397 display_par_n = 1;
398 display_par_d = 1;
399 }
400
401 ok = gst_video_calculate_display_ratio (&display_ratio_num,
402 &display_ratio_den, width, height, par_n, par_d, display_par_n,
403 display_par_d);
404
405 if (!ok)
406 return FALSE;
407
408 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
409
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,
414 display_ratio_den);
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);
421 } else {
422 GST_DEBUG ("approximating while keeping video height");
423 widget->priv->display_width = (guint)
424 gst_util_uint64_scale_int (height, display_ratio_num,
425 display_ratio_den);
426 widget->priv->display_height = height;
427 }
428 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
429 widget->priv->display_height);
430
431 return TRUE;
432 }
433
434 gboolean
setCaps(GstCaps * caps)435 QtGLVideoItemInterface::setCaps (GstCaps * caps)
436 {
437 QMutexLocker locker(&lock);
438 GstVideoInfo v_info;
439
440 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
441 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
442
443 if (qt_item == NULL)
444 return FALSE;
445
446 if (qt_item->priv->caps && gst_caps_is_equal_fixed (qt_item->priv->caps, caps))
447 return TRUE;
448
449 if (!gst_video_info_from_caps (&v_info, caps))
450 return FALSE;
451
452 g_mutex_lock (&qt_item->priv->lock);
453
454 _reset (qt_item);
455
456 gst_caps_replace (&qt_item->priv->caps, caps);
457
458 if (!_calculate_par (qt_item, &v_info)) {
459 g_mutex_unlock (&qt_item->priv->lock);
460 return FALSE;
461 }
462
463 qt_item->priv->v_info = v_info;
464 qt_item->priv->negotiated = TRUE;
465
466 g_mutex_unlock (&qt_item->priv->lock);
467
468 return TRUE;
469 }
470
471 GstGLContext *
getQtContext()472 QtGLVideoItemInterface::getQtContext ()
473 {
474 QMutexLocker locker(&lock);
475
476 if (!qt_item || !qt_item->priv->other_context)
477 return NULL;
478
479 return (GstGLContext *) gst_object_ref (qt_item->priv->other_context);
480 }
481
482 GstGLContext *
getContext()483 QtGLVideoItemInterface::getContext ()
484 {
485 QMutexLocker locker(&lock);
486
487 if (!qt_item || !qt_item->priv->context)
488 return NULL;
489
490 return (GstGLContext *) gst_object_ref (qt_item->priv->context);
491 }
492
493 GstGLDisplay *
getDisplay()494 QtGLVideoItemInterface::getDisplay()
495 {
496 QMutexLocker locker(&lock);
497
498 if (!qt_item || !qt_item->priv->display)
499 return NULL;
500
501 return (GstGLDisplay *) gst_object_ref (qt_item->priv->display);
502 }
503
504 void
setDAR(gint num,gint den)505 QtGLVideoItemInterface::setDAR(gint num, gint den)
506 {
507 QMutexLocker locker(&lock);
508 if (!qt_item)
509 return;
510 qt_item->setDAR(num, den);
511 }
512
513 void
getDAR(gint * num,gint * den)514 QtGLVideoItemInterface::getDAR(gint * num, gint * den)
515 {
516 QMutexLocker locker(&lock);
517 if (!qt_item)
518 return;
519 qt_item->getDAR (num, den);
520 }
521
522 void
setForceAspectRatio(bool force_aspect_ratio)523 QtGLVideoItemInterface::setForceAspectRatio(bool force_aspect_ratio)
524 {
525 QMutexLocker locker(&lock);
526 if (!qt_item)
527 return;
528 qt_item->setForceAspectRatio(force_aspect_ratio);
529 }
530
531 bool
getForceAspectRatio()532 QtGLVideoItemInterface::getForceAspectRatio()
533 {
534 QMutexLocker locker(&lock);
535 if (!qt_item)
536 return FALSE;
537 return qt_item->getForceAspectRatio();
538 }
539
540 void
invalidateRef()541 QtGLVideoItemInterface::invalidateRef()
542 {
543 QMutexLocker locker(&lock);
544 qt_item = NULL;
545 }
546
547