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 /**
22 * SECTION:gstqtsink
23 *
24 * qmlglsink provides a way to render a video stream as a Qml object inside
25 * the Qml scene graph. This is achieved by providing the incoming OpenGL
26 * textures to Qt as a scene graph object.
27 *
28 * qmlglsink will attempt to retrieve the windowing system display connection
29 * that Qt is using (#GstGLDisplay). This may be different to any already
30 * existing window system display connection already in use in the pipeline for
31 * a number of reasons. A couple of examples of this are:
32 *
33 * 1. Adding qmlglsink to an already running pipeline
34 * 2. Not having any qmlglsink (or qmlgloverlay) element start up before any
35 * other OpenGL-based element in the pipeline.
36 *
37 * If one of these scenarios occurs, then there will be multiple OpenGL contexts
38 * in use in the pipeline. This means that either the pipeline will fail to
39 * start up correctly, a downstream element may reject buffers, or a complete
40 * GPU->System memory->GPU transfer is performed for every buffer.
41 *
42 * The requirement to avoid this is that all elements share the same
43 * #GstGLDisplay object and as Qt cannot currently share an existing window
44 * system display connection, GStreamer must use the window system display
45 * connection provided by Qt. This window system display connection can be
46 * retrieved by either a qmlglsink element or a qmlgloverlay element. The
47 * recommended usage is to have either element (qmlglsink or qmlgloverlay)
48 * be the first to propagate the #GstGLDisplay for the entire pipeline to use by
49 * setting either element to the READY element state before any other OpenGL
50 * element in the pipeline.
51 *
52 * In a dynamically adding qmlglsink (or qmlgloverlay) to a pipeline case,
53 * there are some considerations for ensuring that the window system display
54 * and OpenGL contexts are compatible with Qt. When the qmlgloverlay (or
55 * qmlglsink) element is added and brought up to READY, it will propagate it's
56 * own #GstGLDisplay using the #GstContext mechanism regardless of any existing
57 * #GstGLDisplay used by the pipeline previously. In order for the new
58 * #GstGLDisplay to be used, the application must then set the provided
59 * #GstGLDisplay containing #GstContext on the pipeline. This may effectively
60 * cause each OpenGL element to replace the window system display and also the
61 * OpenGL context it is using. As such this process may take a significant
62 * amount of time and resources as objects are recreated in the new OpenGL
63 * context.
64 *
65 * All instances of qmlglsink and qmlgloverlay will return the exact same
66 * #GstGLDisplay object while the pipeline is running regardless of whether
67 * any qmlglsink or qmlgloverlay elements are added or removed from the
68 * pipeline.
69 */
70
71 #ifdef HAVE_CONFIG_H
72 #include "config.h"
73 #endif
74
75 #include "gstqtelements.h"
76 #include "gstqtsink.h"
77 #include <QtGui/QGuiApplication>
78
79 #include <gst/gl/gstglfuncs.h>
80
81 #define GST_CAT_DEFAULT gst_debug_qt_gl_sink
82 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
83
84 static void gst_qt_sink_navigation_interface_init (GstNavigationInterface * iface);
85 static void gst_qt_sink_finalize (GObject * object);
86 static void gst_qt_sink_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * param_spec);
88 static void gst_qt_sink_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * param_spec);
90
91 static gboolean gst_qt_sink_stop (GstBaseSink * bsink);
92
93 static gboolean gst_qt_sink_query (GstBaseSink * bsink, GstQuery * query);
94
95 static GstStateChangeReturn
96 gst_qt_sink_change_state (GstElement * element, GstStateChange transition);
97
98 static void gst_qt_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
99 GstClockTime * start, GstClockTime * end);
100 static gboolean gst_qt_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
101 static GstFlowReturn gst_qt_sink_show_frame (GstVideoSink * bsink,
102 GstBuffer * buf);
103 static gboolean gst_qt_sink_propose_allocation (GstBaseSink * bsink,
104 GstQuery * query);
105
106 static GstStaticPadTemplate gst_qt_sink_template =
107 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_PAD_SINK,
109 GST_PAD_ALWAYS,
110 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), "
111 "format = (string) { RGB, RGBA }, "
112 "width = " GST_VIDEO_SIZE_RANGE ", "
113 "height = " GST_VIDEO_SIZE_RANGE ", "
114 "framerate = " GST_VIDEO_FPS_RANGE ", "
115 "texture-target = (string) 2D"));
116
117 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
118 #define DEFAULT_PAR_N 0
119 #define DEFAULT_PAR_D 1
120
121 enum
122 {
123 ARG_0,
124 PROP_WIDGET,
125 PROP_FORCE_ASPECT_RATIO,
126 PROP_PIXEL_ASPECT_RATIO,
127 };
128
129 enum
130 {
131 SIGNAL_0,
132 LAST_SIGNAL
133 };
134
135 #define gst_qt_sink_parent_class parent_class
136 G_DEFINE_TYPE_WITH_CODE (GstQtSink, gst_qt_sink,
137 GST_TYPE_VIDEO_SINK, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
138 "qtsink", 0, "Qt Video Sink");
139 G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
140 gst_qt_sink_navigation_interface_init));
141 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (qmlglsink, "qmlglsink",
142 GST_RANK_NONE, GST_TYPE_QT_SINK, qt5_element_init (plugin));
143
144 static void
gst_qt_sink_class_init(GstQtSinkClass * klass)145 gst_qt_sink_class_init (GstQtSinkClass * klass)
146 {
147 GObjectClass *gobject_class;
148 GstElementClass *gstelement_class;
149 GstBaseSinkClass *gstbasesink_class;
150 GstVideoSinkClass *gstvideosink_class;
151
152 gobject_class = (GObjectClass *) klass;
153 gstelement_class = (GstElementClass *) klass;
154 gstbasesink_class = (GstBaseSinkClass *) klass;
155 gstvideosink_class = (GstVideoSinkClass *) klass;
156
157 gobject_class->set_property = gst_qt_sink_set_property;
158 gobject_class->get_property = gst_qt_sink_get_property;
159
160 gst_element_class_set_metadata (gstelement_class, "Qt Video Sink",
161 "Sink/Video", "A video sink that renders to a QQuickItem",
162 "Matthew Waters <matthew@centricular.com>");
163
164 g_object_class_install_property (gobject_class, PROP_WIDGET,
165 g_param_spec_pointer ("widget", "QQuickItem",
166 "The QQuickItem to place in the object hierarchy",
167 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
168
169 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
170 g_param_spec_boolean ("force-aspect-ratio",
171 "Force aspect ratio",
172 "When enabled, scaling will respect original aspect ratio",
173 DEFAULT_FORCE_ASPECT_RATIO,
174 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
175
176 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
177 gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
178 "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D,
179 G_MAXINT, 1, 1, 1,
180 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
181
182 gst_element_class_add_static_pad_template (gstelement_class, &gst_qt_sink_template);
183
184 gobject_class->finalize = gst_qt_sink_finalize;
185
186 gstelement_class->change_state = gst_qt_sink_change_state;
187 gstbasesink_class->query = gst_qt_sink_query;
188 gstbasesink_class->set_caps = gst_qt_sink_set_caps;
189 gstbasesink_class->get_times = gst_qt_sink_get_times;
190 gstbasesink_class->propose_allocation = gst_qt_sink_propose_allocation;
191 gstbasesink_class->stop = gst_qt_sink_stop;
192
193 gstvideosink_class->show_frame = gst_qt_sink_show_frame;
194 }
195
196 static void
gst_qt_sink_init(GstQtSink * qt_sink)197 gst_qt_sink_init (GstQtSink * qt_sink)
198 {
199 qt_sink->widget = QSharedPointer<QtGLVideoItemInterface>();
200 if (qt_sink->widget)
201 qt_sink->widget->setSink (GST_ELEMENT_CAST (qt_sink));
202 }
203
204 static void
gst_qt_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)205 gst_qt_sink_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec)
207 {
208 GstQtSink *qt_sink = GST_QT_SINK (object);
209
210 switch (prop_id) {
211 case PROP_WIDGET: {
212 QtGLVideoItem *qt_item = static_cast<QtGLVideoItem *> (g_value_get_pointer (value));
213 if (qt_item) {
214 qt_sink->widget = qt_item->getInterface();
215 if (qt_sink->widget) {
216 qt_sink->widget->setSink (GST_ELEMENT_CAST (qt_sink));
217 }
218 } else {
219 qt_sink->widget.clear();
220 }
221 break;
222 }
223 case PROP_FORCE_ASPECT_RATIO:
224 g_return_if_fail (qt_sink->widget);
225 qt_sink->widget->setForceAspectRatio (g_value_get_boolean (value));
226 break;
227 case PROP_PIXEL_ASPECT_RATIO:
228 g_return_if_fail (qt_sink->widget);
229 qt_sink->widget->setDAR (gst_value_get_fraction_numerator (value),
230 gst_value_get_fraction_denominator (value));
231 break;
232 default:
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 break;
235 }
236 }
237
238 static void
_reset(GstQtSink * qt_sink)239 _reset (GstQtSink * qt_sink)
240 {
241 if (qt_sink->display) {
242 gst_object_unref (qt_sink->display);
243 qt_sink->display = NULL;
244 }
245
246 if (qt_sink->context) {
247 gst_object_unref (qt_sink->context);
248 qt_sink->context = NULL;
249 }
250
251 if (qt_sink->qt_context) {
252 gst_object_unref (qt_sink->qt_context);
253 qt_sink->qt_context = NULL;
254 }
255 }
256
257 static void
gst_qt_sink_finalize(GObject * object)258 gst_qt_sink_finalize (GObject * object)
259 {
260 GstQtSink *qt_sink = GST_QT_SINK (object);
261
262 _reset (qt_sink);
263
264 qt_sink->widget.clear();
265
266 G_OBJECT_CLASS (parent_class)->finalize (object);
267 }
268
269 static void
gst_qt_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)270 gst_qt_sink_get_property (GObject * object, guint prop_id,
271 GValue * value, GParamSpec * pspec)
272 {
273 GstQtSink *qt_sink = GST_QT_SINK (object);
274
275 switch (prop_id) {
276 case PROP_WIDGET:
277 /* This is not really safe - the app needs to be
278 * sure the widget is going to be kept alive or
279 * this can crash */
280 if (qt_sink->widget)
281 g_value_set_pointer (value, qt_sink->widget->videoItem());
282 else
283 g_value_set_pointer (value, NULL);
284 break;
285 case PROP_FORCE_ASPECT_RATIO:
286 if (qt_sink->widget)
287 g_value_set_boolean (value, qt_sink->widget->getForceAspectRatio ());
288 else
289 g_value_set_boolean (value, DEFAULT_FORCE_ASPECT_RATIO);
290 break;
291 case PROP_PIXEL_ASPECT_RATIO:
292 if (qt_sink->widget) {
293 gint num, den;
294 qt_sink->widget->getDAR (&num, &den);
295 gst_value_set_fraction (value, num, den);
296 } else {
297 gst_value_set_fraction (value, DEFAULT_PAR_N, DEFAULT_PAR_D);
298 }
299 break;
300 default:
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302 break;
303 }
304 }
305
306 static gboolean
gst_qt_sink_query(GstBaseSink * bsink,GstQuery * query)307 gst_qt_sink_query (GstBaseSink * bsink, GstQuery * query)
308 {
309 GstQtSink *qt_sink = GST_QT_SINK (bsink);
310 gboolean res = FALSE;
311
312 switch (GST_QUERY_TYPE (query)) {
313 case GST_QUERY_CONTEXT:
314 {
315 if (gst_gl_handle_context_query ((GstElement *) qt_sink, query,
316 qt_sink->display, qt_sink->context, qt_sink->qt_context))
317 return TRUE;
318
319 /* fallthrough */
320 }
321 default:
322 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
323 break;
324 }
325
326 return res;
327 }
328
329 static gboolean
gst_qt_sink_stop(GstBaseSink * bsink)330 gst_qt_sink_stop (GstBaseSink * bsink)
331 {
332 return TRUE;
333 }
334
335 static GstStateChangeReturn
gst_qt_sink_change_state(GstElement * element,GstStateChange transition)336 gst_qt_sink_change_state (GstElement * element, GstStateChange transition)
337 {
338 GstQtSink *qt_sink = GST_QT_SINK (element);
339 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
340 QGuiApplication *app;
341
342 GST_DEBUG ("changing state: %s => %s",
343 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
344 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
345
346 switch (transition) {
347 case GST_STATE_CHANGE_NULL_TO_READY:
348 app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
349 if (!app) {
350 GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
351 ("%s", "Failed to connect to Qt"),
352 ("%s", "Could not retrieve QGuiApplication instance"));
353 return GST_STATE_CHANGE_FAILURE;
354 }
355
356 if (!qt_sink->widget) {
357 GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
358 ("%s", "Required property \'widget\' not set"),
359 (NULL));
360 return GST_STATE_CHANGE_FAILURE;
361 }
362
363 if (!qt_sink->widget->initWinSys()) {
364 GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
365 ("%s", "Could not initialize window system"),
366 (NULL));
367 return GST_STATE_CHANGE_FAILURE;
368 }
369
370 qt_sink->display = qt_sink->widget->getDisplay();
371 qt_sink->context = qt_sink->widget->getContext();
372 qt_sink->qt_context = qt_sink->widget->getQtContext();
373
374 if (!qt_sink->display || !qt_sink->context || !qt_sink->qt_context) {
375 GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
376 ("%s", "Could not retrieve window system OpenGL configuration"),
377 (NULL));
378 return GST_STATE_CHANGE_FAILURE;
379 }
380
381 GST_OBJECT_LOCK (qt_sink->display);
382 gst_gl_display_add_context (qt_sink->display, qt_sink->context);
383 GST_OBJECT_UNLOCK (qt_sink->display);
384
385 gst_gl_element_propagate_display_context (GST_ELEMENT (qt_sink), qt_sink->display);
386
387 break;
388 case GST_STATE_CHANGE_READY_TO_PAUSED:
389 break;
390 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
391 break;
392 default:
393 break;
394 }
395
396 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
397 if (ret == GST_STATE_CHANGE_FAILURE)
398 return ret;
399
400 switch (transition) {
401 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
402 break;
403 case GST_STATE_CHANGE_PAUSED_TO_READY:
404 if (qt_sink->widget)
405 qt_sink->widget->setBuffer(NULL);
406 break;
407 case GST_STATE_CHANGE_READY_TO_NULL:
408 break;
409 default:
410 break;
411 }
412
413 return ret;
414 }
415
416 static void
gst_qt_sink_get_times(GstBaseSink * bsink,GstBuffer * buf,GstClockTime * start,GstClockTime * end)417 gst_qt_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
418 GstClockTime * start, GstClockTime * end)
419 {
420 GstQtSink *qt_sink;
421
422 qt_sink = GST_QT_SINK (bsink);
423
424 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
425 *start = GST_BUFFER_TIMESTAMP (buf);
426 if (GST_BUFFER_DURATION_IS_VALID (buf))
427 *end = *start + GST_BUFFER_DURATION (buf);
428 else {
429 if (GST_VIDEO_INFO_FPS_N (&qt_sink->v_info) > 0) {
430 *end = *start +
431 gst_util_uint64_scale_int (GST_SECOND,
432 GST_VIDEO_INFO_FPS_D (&qt_sink->v_info),
433 GST_VIDEO_INFO_FPS_N (&qt_sink->v_info));
434 }
435 }
436 }
437 }
438
439 gboolean
gst_qt_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)440 gst_qt_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
441 {
442 GstQtSink *qt_sink = GST_QT_SINK (bsink);
443
444 GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
445
446 if (!gst_video_info_from_caps (&qt_sink->v_info, caps))
447 return FALSE;
448
449 if (!qt_sink->widget)
450 return FALSE;
451
452 return qt_sink->widget->setCaps(caps);
453 }
454
455 static GstFlowReturn
gst_qt_sink_show_frame(GstVideoSink * vsink,GstBuffer * buf)456 gst_qt_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
457 {
458 GstQtSink *qt_sink;
459
460 GST_TRACE ("rendering buffer:%p", buf);
461
462 qt_sink = GST_QT_SINK (vsink);
463
464 if (qt_sink->widget)
465 qt_sink->widget->setBuffer(buf);
466
467 return GST_FLOW_OK;
468 }
469
470 static gboolean
gst_qt_sink_propose_allocation(GstBaseSink * bsink,GstQuery * query)471 gst_qt_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
472 {
473 GstQtSink *qt_sink = GST_QT_SINK (bsink);
474 GstBufferPool *pool;
475 GstStructure *config;
476 GstCaps *caps;
477 guint size;
478 gboolean need_pool;
479
480 if (!qt_sink->display || !qt_sink->context)
481 return FALSE;
482
483 gst_query_parse_allocation (query, &caps, &need_pool);
484
485 if (caps == NULL)
486 goto no_caps;
487
488 /* FIXME re-using buffer pool breaks renegotiation */
489 if ((pool = qt_sink->pool))
490 gst_object_ref (pool);
491
492 if (pool != NULL) {
493 GstCaps *pcaps;
494
495 /* we had a pool, check caps */
496 GST_DEBUG_OBJECT (qt_sink, "check existing pool caps");
497 config = gst_buffer_pool_get_config (pool);
498 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
499
500 if (!gst_caps_is_equal (caps, pcaps)) {
501 GST_DEBUG_OBJECT (qt_sink, "pool has different caps");
502 /* different caps, we can't use this pool */
503 gst_object_unref (pool);
504 pool = NULL;
505 }
506 gst_structure_free (config);
507 } else {
508 GstVideoInfo info;
509
510 if (!gst_video_info_from_caps (&info, caps))
511 goto invalid_caps;
512
513 /* the normal size of a frame */
514 size = info.size;
515 }
516
517 if (pool == NULL && need_pool) {
518
519 GST_DEBUG_OBJECT (qt_sink, "create new pool");
520 pool = gst_gl_buffer_pool_new (qt_sink->context);
521
522 config = gst_buffer_pool_get_config (pool);
523 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
524 if (!gst_buffer_pool_set_config (pool, config))
525 goto config_failed;
526 }
527
528 /* we need at least 2 buffer because we hold on to the last one */
529 gst_query_add_allocation_pool (query, pool, size, 2, 0);
530 if (pool)
531 gst_object_unref (pool);
532
533 /* we also support various metadata */
534 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
535
536 if (qt_sink->context->gl_vtable->FenceSync)
537 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
538
539 return TRUE;
540
541 /* ERRORS */
542 no_caps:
543 {
544 GST_DEBUG_OBJECT (bsink, "no caps specified");
545 return FALSE;
546 }
547 invalid_caps:
548 {
549 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
550 return FALSE;
551 }
552 config_failed:
553 {
554 GST_DEBUG_OBJECT (bsink, "failed setting config");
555 return FALSE;
556 }
557 }
558
559 static void
gst_qt_sink_navigation_send_event(GstNavigation * navigation,GstStructure * structure)560 gst_qt_sink_navigation_send_event (GstNavigation * navigation,
561 GstStructure * structure)
562 {
563 GstQtSink *qt_sink = GST_QT_SINK (navigation);
564 GstEvent *event;
565 GstPad *pad;
566
567 event = gst_event_new_navigation (structure);
568 pad = gst_pad_get_peer (GST_VIDEO_SINK_PAD (qt_sink));
569
570 GST_TRACE_OBJECT (qt_sink, "navigation event %" GST_PTR_FORMAT, structure);
571
572 if (GST_IS_PAD (pad) && GST_IS_EVENT (event)) {
573 if (!gst_pad_send_event (pad, gst_event_ref (event))) {
574 /* If upstream didn't handle the event we'll post a message with it
575 * for the application in case it wants to do something with it */
576 gst_element_post_message (GST_ELEMENT_CAST (qt_sink),
577 gst_navigation_message_new_event (GST_OBJECT_CAST (qt_sink), event));
578 }
579 gst_event_unref (event);
580 gst_object_unref (pad);
581 }
582 }
583
gst_qt_sink_navigation_interface_init(GstNavigationInterface * iface)584 static void gst_qt_sink_navigation_interface_init (GstNavigationInterface * iface)
585 {
586 iface->send_event = gst_qt_sink_navigation_send_event;
587 }
588