qmlglsrc: sync on the streaming thread
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / ext / qt / gstqtsrc.cc
1 /*
2  * GStreamer
3  * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
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:qmlglsrc
23  *
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "gstqtelements.h"
31 #include "gstqtsrc.h"
32 #include <QtGui/QGuiApplication>
33
34 #define GST_CAT_DEFAULT gst_debug_qt_gl_src
35 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
36
37 #define DEFAULT_IS_LIVE TRUE
38
39 static void gst_qt_src_set_property (GObject * object, guint prop_id,
40     const GValue * value, GParamSpec * pspec);
41 static void gst_qt_src_get_property (GObject * object, guint prop_id,
42     GValue * value, GParamSpec * pspec);
43
44 static void gst_qt_src_finalize (GObject * object);
45
46 static gboolean gst_qt_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
47 static GstCaps *gst_qt_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter);
48 static gboolean gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query);
49
50 static gboolean gst_qt_src_decide_allocation (GstBaseSrc * bsrc,
51     GstQuery * query);
52 static GstFlowReturn gst_qt_src_fill (GstPushSrc * psrc, GstBuffer * buffer);
53 static GstStateChangeReturn gst_qt_src_change_state (GstElement * element,
54     GstStateChange transition);
55 static gboolean gst_qt_src_start (GstBaseSrc * basesrc);
56 static gboolean gst_qt_src_stop (GstBaseSrc * basesrc);
57 static gboolean gst_qt_src_unlock(GstBaseSrc *basesrc);
58
59 static GstStaticPadTemplate gst_qt_src_template =
60 GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), "
64         "format = (string) RGBA, "
65         "width = " GST_VIDEO_SIZE_RANGE ", "
66         "height = " GST_VIDEO_SIZE_RANGE ", "
67         "framerate = " GST_VIDEO_FPS_RANGE ", "
68         "texture-target = (string) 2D"));
69
70 enum
71 {
72   ARG_0,
73   PROP_WINDOW,
74   PROP_DEFAULT_FBO
75 };
76
77 #define gst_qt_src_parent_class parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstQtSrc, gst_qt_src,
79     GST_TYPE_PUSH_SRC, GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
80         "qtsrc", 0, "Qt Video Src"));
81 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (qmlglsrc, "qmlglsrc",
82     GST_RANK_NONE, GST_TYPE_QT_SRC, qt5_element_init (plugin));
83
84 static const gfloat vertical_flip_matrix[] = {
85   1.0f, 0.0f, 0.0f, 0.0f,
86   0.0f, -1.0f, 0.0f, 0.0f,
87   0.0f, 0.0f, 1.0f, 0.0f,
88   0.0f, 1.0f, 0.0f, 1.0f,
89 };
90
91 static void
92 gst_qt_src_class_init (GstQtSrcClass * klass)
93 {
94   GObjectClass *gobject_class = (GObjectClass *) klass;
95   GstElementClass *gstelement_class = (GstElementClass *) klass;
96   GstBaseSrcClass *gstbasesrc_class = (GstBaseSrcClass *) klass;
97   GstPushSrcClass *gstpushsrc_class = (GstPushSrcClass *) klass;
98
99   gobject_class->set_property = gst_qt_src_set_property;
100   gobject_class->get_property = gst_qt_src_get_property;
101   gobject_class->finalize = gst_qt_src_finalize;
102
103   gst_element_class_set_metadata (gstelement_class, "Qt Video Source",
104       "Source/Video", "A video src that captures a window from a QML view",
105       "Multimedia Team <shmmmw@freescale.com>");
106
107   g_object_class_install_property (gobject_class, PROP_WINDOW,
108       g_param_spec_pointer ("window", "QQuickWindow",
109           "The QQuickWindow to place in the object hierarchy",
110           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
111
112   g_object_class_install_property (gobject_class, PROP_DEFAULT_FBO,
113       g_param_spec_boolean ("use-default-fbo",
114           "Whether to use default FBO",
115           "When set it will not create a new FBO for the QML render thread",
116           FALSE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
117
118   gst_element_class_add_pad_template (gstelement_class,
119       gst_static_pad_template_get (&gst_qt_src_template));
120
121   gstelement_class->change_state = gst_qt_src_change_state;
122   gstbasesrc_class->set_caps = gst_qt_src_setcaps;
123   gstbasesrc_class->get_caps = gst_qt_src_get_caps;
124   gstbasesrc_class->query = gst_qt_src_query;
125   gstbasesrc_class->start = gst_qt_src_start;
126   gstbasesrc_class->stop = gst_qt_src_stop;
127   gstbasesrc_class->unlock = gst_qt_src_unlock;
128   gstbasesrc_class->decide_allocation = gst_qt_src_decide_allocation;
129
130   gstpushsrc_class->fill = gst_qt_src_fill;
131 }
132
133 static void
134 gst_qt_src_init (GstQtSrc * src)
135 {
136   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
137   gst_base_src_set_live (GST_BASE_SRC (src), DEFAULT_IS_LIVE);
138   src->default_fbo = FALSE;
139   src->pending_image_orientation = TRUE;
140 }
141
142 static void
143 gst_qt_src_set_property (GObject * object, guint prop_id,
144     const GValue * value, GParamSpec * pspec)
145 {
146   GstQtSrc *qt_src = GST_QT_SRC (object);
147
148   switch (prop_id) {
149     case PROP_WINDOW:{
150       qt_src->qwindow =
151           static_cast < QQuickWindow * >(g_value_get_pointer (value));
152
153       if (qt_src->window) {
154         delete qt_src->window;
155         qt_src->window = NULL;
156       }
157
158       if (qt_src->qwindow)
159         qt_src->window = new QtGLWindow (NULL, qt_src->qwindow);
160
161       break;
162     }
163     case PROP_DEFAULT_FBO:
164       qt_src->default_fbo = g_value_get_boolean (value);
165       if (qt_src->window)
166         qt_window_use_default_fbo (qt_src->window, qt_src->default_fbo);
167       break;
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173
174 static void
175 gst_qt_src_get_property (GObject * object, guint prop_id,
176     GValue * value, GParamSpec * pspec)
177 {
178   GstQtSrc *qt_src = GST_QT_SRC (object);
179
180   switch (prop_id) {
181     case PROP_WINDOW:
182       g_value_set_pointer (value, qt_src->qwindow);
183       break;
184     case PROP_DEFAULT_FBO:
185       g_value_set_boolean (value, qt_src->default_fbo);
186       break;
187     default:
188       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189       break;
190   }
191 }
192
193 static void
194 gst_qt_src_finalize (GObject * object)
195 {
196   GstQtSrc *qt_src = GST_QT_SRC (object);
197
198   GST_DEBUG ("qmlglsrc finalize");
199   if (qt_src->context)
200     gst_object_unref (qt_src->context);
201   qt_src->context = NULL;
202
203   if (qt_src->qt_context)
204     gst_object_unref (qt_src->qt_context);
205   qt_src->qt_context = NULL;
206
207   if (qt_src->display)
208     gst_object_unref (qt_src->display);
209   qt_src->display = NULL;
210
211   if (qt_src->window)
212     delete qt_src->window;
213
214   G_OBJECT_CLASS (parent_class)->finalize (object);
215 }
216
217 static gboolean
218 gst_qt_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
219 {
220   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
221
222   GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
223
224   if (!gst_video_info_from_caps (&qt_src->v_info, caps))
225     return FALSE;
226
227   if (!qt_window_set_caps (qt_src->window, caps))
228     return FALSE;
229
230   return TRUE;
231 }
232
233 static GstCaps *
234 gst_qt_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
235 {
236   GstCaps *caps = NULL, *temp = NULL;
237   GstPadTemplate *pad_template;
238   GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc);
239   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
240   guint i;
241   gint width, height;
242
243   if (qt_src->window) {
244     qt_src->window->getGeometry (&width, &height);
245   }
246
247   pad_template =
248       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
249   if (pad_template != NULL)
250     caps = gst_pad_template_get_caps (pad_template);
251
252   if (qt_src->window) {
253     temp = gst_caps_copy (caps);
254     guint n_caps = gst_caps_get_size (caps);
255
256     for (i = 0; i < n_caps; i++) {
257       GstStructure *s = gst_caps_get_structure (temp, i);
258       gst_structure_set (s, "width", G_TYPE_INT, width, NULL);
259       gst_structure_set (s, "height", G_TYPE_INT, height, NULL);
260       /* because the framerate is unknown */
261       gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
262       gst_structure_set (s, "pixel-aspect-ratio",
263           GST_TYPE_FRACTION, 1, 1, NULL);
264     }
265
266     gst_caps_unref (caps);
267     caps = temp;
268   }
269
270   if (filter) {
271     GstCaps *intersection;
272
273     intersection =
274         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
275     gst_caps_unref (caps);
276     caps = intersection;
277   }
278
279   return caps;
280 }
281
282 static gboolean
283 gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query)
284 {
285   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
286   gboolean res = FALSE;
287
288   switch (GST_QUERY_TYPE (query)) {
289     case GST_QUERY_CONTEXT:
290     {
291       if (!qt_window_is_scenegraph_initialized (qt_src->window))
292         return FALSE;
293
294       if (!qt_src->display && !qt_src->qt_context) {
295         if (!qt_src->display)
296           qt_src->display = qt_window_get_display (qt_src->window);
297         if (!qt_src->qt_context)
298           qt_src->qt_context = qt_window_get_qt_context (qt_src->window);
299         if (!qt_src->context)
300           qt_src->context = qt_window_get_context (qt_src->window);
301       }
302
303       if (gst_gl_handle_context_query ((GstElement *) qt_src, query,
304           qt_src->display, qt_src->context, qt_src->qt_context))
305         return TRUE;
306
307       /* fallthrough */
308     }
309     default:
310       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
311       break;
312   }
313
314   return res;
315 }
316
317 static gboolean
318 _find_local_gl_context (GstQtSrc * qt_src)
319 {
320   if (gst_gl_query_local_gl_context (GST_ELEMENT (qt_src), GST_PAD_SRC,
321       &qt_src->context))
322     return TRUE;
323   return FALSE;
324 }
325
326 static gboolean
327 gst_qt_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
328 {
329   GstBufferPool *pool = NULL;
330   GstStructure *config;
331   GstCaps *caps;
332   guint min, max, size, n, i;
333   gboolean update_pool, update_allocator;
334   GstAllocator *allocator;
335   GstAllocationParams params;
336   GstGLVideoAllocationParams *glparams;
337   GstVideoInfo vinfo;
338   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
339
340   if (gst_query_find_allocation_meta (query,
341           GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, NULL)) {
342     qt_src->downstream_supports_affine_meta = TRUE;
343   } else {
344     qt_src->downstream_supports_affine_meta = FALSE;
345   }
346
347   gst_query_parse_allocation (query, &caps, NULL);
348   if (!caps)
349     return FALSE;
350
351   gst_video_info_from_caps (&vinfo, caps);
352
353   n = gst_query_get_n_allocation_pools (query);
354   if (n > 0) {
355     update_pool = TRUE;
356     for (i = 0; i < n; i++) {
357       gst_query_parse_nth_allocation_pool (query, i, &pool, &size, &min, &max);
358
359       if (!pool || !GST_IS_GL_BUFFER_POOL (pool)) {
360         if (pool)
361           gst_object_unref (pool);
362         pool = NULL;
363       }
364     }
365   }
366
367   if (!pool) {
368     size = vinfo.size;
369     min = max = 0;
370     update_pool = FALSE;
371   }
372
373   if (!qt_src->context && !_find_local_gl_context (qt_src))
374     return FALSE;
375
376   if (!qt_window_set_context (qt_src->window, qt_src->context))
377     return FALSE;
378
379   if (!pool) {
380     if (!qt_src->context || !GST_IS_GL_CONTEXT (qt_src->context))
381       return FALSE;
382
383     pool = gst_gl_buffer_pool_new (qt_src->context);
384     GST_INFO_OBJECT (qt_src, "No pool, create one ourself %p", pool);
385   }
386
387   config = gst_buffer_pool_get_config (pool);
388
389   gst_buffer_pool_config_set_params (config, caps, size, min, max);
390   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
391   if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
392     gst_buffer_pool_config_add_option (config,
393         GST_BUFFER_POOL_OPTION_GL_SYNC_META);
394
395   if (gst_query_get_n_allocation_params (query) > 0) {
396     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
397     gst_buffer_pool_config_set_allocator (config, allocator, &params);
398     GST_INFO_OBJECT (qt_src, "got allocator %p", allocator);
399     update_allocator = TRUE;
400   } else {
401     allocator = NULL;
402     gst_allocation_params_init (&params);
403     update_allocator = FALSE;
404   }
405
406   glparams =
407       gst_gl_video_allocation_params_new (qt_src->context, &params, &vinfo, 0,
408       NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
409   gst_buffer_pool_config_set_gl_allocation_params (config,
410       (GstGLAllocationParams *) glparams);
411   gst_gl_allocation_params_free ((GstGLAllocationParams *) glparams);
412
413   if (!gst_buffer_pool_set_config (pool, config))
414     GST_WARNING_OBJECT (qt_src, "Failed to set buffer pool config");
415
416   if (update_allocator)
417     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
418   else
419     gst_query_add_allocation_param (query, allocator, &params);
420   if (allocator)
421     gst_object_unref (allocator);
422
423   if (update_pool)
424     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
425   else
426     gst_query_add_allocation_pool (query, pool, size, min, max);
427   gst_object_unref (pool);
428
429   GST_INFO_OBJECT (qt_src, "successfully decide_allocation");
430   return TRUE;
431 }
432
433 static GstFlowReturn
434 gst_qt_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
435 {
436   GstQtSrc *qt_src = GST_QT_SRC (psrc);
437   GstGLContext* context = qt_src->context;
438   GstGLSyncMeta *sync_meta;
439
440   GST_DEBUG_OBJECT (qt_src, "setting buffer %p", buffer);
441
442   if (!qt_window_set_buffer (qt_src->window, buffer)) {
443     GST_ERROR_OBJECT (qt_src, "failed to fill buffer %p", buffer);
444     return GST_FLOW_ERROR;
445   }
446
447   sync_meta = gst_buffer_get_gl_sync_meta(buffer);
448   if (sync_meta)
449       gst_gl_sync_meta_wait(sync_meta, context);
450
451   if (!qt_src->downstream_supports_affine_meta) {
452     if (qt_src->pending_image_orientation) {
453       /* let downstream know the image orientation is vertical filp */
454       GstTagList *image_orientation_tag =
455           gst_tag_list_new (GST_TAG_IMAGE_ORIENTATION, "flip-rotate-180", NULL);
456
457       gst_pad_push_event (GST_BASE_SRC_PAD (psrc),
458           gst_event_new_tag (image_orientation_tag));
459
460       qt_src->pending_image_orientation = FALSE;
461     }
462   } else {
463     GstVideoAffineTransformationMeta *trans_meta;
464     trans_meta = gst_buffer_add_video_affine_transformation_meta (buffer);
465     gst_video_affine_transformation_meta_apply_matrix (trans_meta,
466         vertical_flip_matrix);
467   }
468
469   GST_DEBUG_OBJECT (qt_src, "buffer fill done %p", buffer);
470
471   return GST_FLOW_OK;
472 }
473
474 static GstStateChangeReturn
475 gst_qt_src_change_state (GstElement * element, GstStateChange transition)
476 {
477   GstQtSrc *qt_src = GST_QT_SRC (element);
478   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
479   QGuiApplication *app;
480
481   GST_DEBUG ("changing state: %s => %s",
482       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
483       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
484
485   switch (transition) {
486     case GST_STATE_CHANGE_NULL_TO_READY:
487       app = static_cast < QGuiApplication * >(QCoreApplication::instance ());
488       if (!app) {
489         GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
490             ("%s", "Failed to connect to Qt"),
491             ("%s", "Could not retrieve QGuiApplication instance"));
492         return GST_STATE_CHANGE_FAILURE;
493       }
494
495       if (!qt_src->window) {
496         GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
497             ("%s", "Required property \'window\' not set"), (NULL));
498         return GST_STATE_CHANGE_FAILURE;
499       }
500
501       if (!qt_window_is_scenegraph_initialized (qt_src->window)) {
502         GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
503             ("%s", "Could not initialize window system"), (NULL));
504         return GST_STATE_CHANGE_FAILURE;
505       }
506
507       qt_window_use_default_fbo (qt_src->window, qt_src->default_fbo);
508
509       break;
510     case GST_STATE_CHANGE_READY_TO_PAUSED:
511       break;
512     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
513       break;
514     default:
515       break;
516   }
517
518   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
519   if (ret == GST_STATE_CHANGE_FAILURE)
520     return ret;
521
522   switch (transition) {
523     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
524       break;
525     case GST_STATE_CHANGE_PAUSED_TO_READY:
526       break;
527     case GST_STATE_CHANGE_READY_TO_NULL:
528       break;
529     default:
530       break;
531   }
532
533   return ret;
534 }
535
536 static gboolean
537 gst_qt_src_start (GstBaseSrc * basesrc)
538 {
539   GstQtSrc *qt_src = GST_QT_SRC (basesrc);
540
541   /* already has get OpenGL configuration from qt */
542   if (qt_src->display && qt_src->qt_context)
543     return TRUE;
544
545   if (!qt_window_is_scenegraph_initialized (qt_src->window))
546     return FALSE;
547
548   qt_src->display = qt_window_get_display (qt_src->window);
549   qt_src->qt_context = qt_window_get_qt_context (qt_src->window);
550   qt_src->context = qt_window_get_context (qt_src->window);
551
552   if (!qt_src->display || !qt_src->qt_context) {
553     GST_ERROR_OBJECT (qt_src,
554         "Could not retrieve window system OpenGL configuration");
555     return FALSE;
556   }
557
558   GST_DEBUG_OBJECT (qt_src, "Got qt display %p and qt gl context %p",
559       qt_src->display, qt_src->qt_context);
560   return TRUE;
561 }
562
563 static gboolean
564 gst_qt_src_unlock(GstBaseSrc *basesrc)
565 {
566   GstQtSrc *qt_src = GST_QT_SRC (basesrc);
567   if (qt_src->window)
568     qt_window_stop (qt_src->window);
569   return TRUE;
570 }
571
572 static gboolean
573 gst_qt_src_stop (GstBaseSrc * basesrc)
574 {
575   return TRUE;
576 }