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