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
gst_qt_src_class_init(GstQtSrcClass * klass)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 that captures a 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 hierarchy",
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           "Whether to use default FBO",
111           "When set it will not create a new FBO for the 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
gst_qt_src_init(GstQtSrc * src)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
gst_qt_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)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         qt_src->window = NULL;
151       }
152 
153       if (qt_src->qwindow)
154         qt_src->window = new QtGLWindow (NULL, qt_src->qwindow);
155 
156       break;
157     }
158     case PROP_DEFAULT_FBO:
159       qt_src->default_fbo = g_value_get_boolean (value);
160       if (qt_src->window)
161         qt_window_use_default_fbo (qt_src->window, qt_src->default_fbo);
162       break;
163     default:
164       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165       break;
166   }
167 }
168 
169 static void
gst_qt_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)170 gst_qt_src_get_property (GObject * object, guint prop_id,
171     GValue * value, GParamSpec * pspec)
172 {
173   GstQtSrc *qt_src = GST_QT_SRC (object);
174 
175   switch (prop_id) {
176     case PROP_WINDOW:
177       g_value_set_pointer (value, qt_src->qwindow);
178       break;
179     case PROP_DEFAULT_FBO:
180       g_value_set_boolean (value, qt_src->default_fbo);
181       break;
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185   }
186 }
187 
188 static void
gst_qt_src_finalize(GObject * object)189 gst_qt_src_finalize (GObject * object)
190 {
191   GstQtSrc *qt_src = GST_QT_SRC (object);
192 
193   GST_DEBUG ("qmlglsrc finalize");
194   if (qt_src->context)
195     gst_object_unref (qt_src->context);
196   qt_src->context = NULL;
197 
198   if (qt_src->qt_context)
199     gst_object_unref (qt_src->qt_context);
200   qt_src->qt_context = NULL;
201 
202   if (qt_src->display)
203     gst_object_unref (qt_src->display);
204   qt_src->display = NULL;
205 
206   if (qt_src->window)
207     delete qt_src->window;
208 
209   G_OBJECT_CLASS (parent_class)->finalize (object);
210 }
211 
212 static gboolean
gst_qt_src_setcaps(GstBaseSrc * bsrc,GstCaps * caps)213 gst_qt_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
214 {
215   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
216 
217   GST_DEBUG ("set caps with %" GST_PTR_FORMAT, caps);
218 
219   if (!gst_video_info_from_caps (&qt_src->v_info, caps))
220     return FALSE;
221 
222   if (!qt_window_set_caps (qt_src->window, caps))
223     return FALSE;
224 
225   return TRUE;
226 }
227 
228 static GstCaps *
gst_qt_src_get_caps(GstBaseSrc * bsrc,GstCaps * filter)229 gst_qt_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
230 {
231   GstCaps *caps = NULL, *temp = NULL;
232   GstPadTemplate *pad_template;
233   GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc);
234   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
235   guint i;
236   gint width, height;
237 
238   if (qt_src->window) {
239     qt_src->window->getGeometry (&width, &height);
240   }
241 
242   pad_template =
243       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
244   if (pad_template != NULL)
245     caps = gst_pad_template_get_caps (pad_template);
246 
247   if (qt_src->window) {
248     temp = gst_caps_copy (caps);
249     guint n_caps = gst_caps_get_size (caps);
250 
251     for (i = 0; i < n_caps; i++) {
252       GstStructure *s = gst_caps_get_structure (temp, i);
253       gst_structure_set (s, "width", G_TYPE_INT, width, NULL);
254       gst_structure_set (s, "height", G_TYPE_INT, height, NULL);
255       /* because the framerate is unknown */
256       gst_structure_set (s, "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
257       gst_structure_set (s, "pixel-aspect-ratio",
258           GST_TYPE_FRACTION, 1, 1, NULL);
259     }
260 
261     gst_caps_unref (caps);
262     caps = temp;
263   }
264 
265   if (filter) {
266     GstCaps *intersection;
267 
268     intersection =
269         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
270     gst_caps_unref (caps);
271     caps = intersection;
272   }
273 
274   return caps;
275 }
276 
277 static gboolean
gst_qt_src_query(GstBaseSrc * bsrc,GstQuery * query)278 gst_qt_src_query (GstBaseSrc * bsrc, GstQuery * query)
279 {
280   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
281   gboolean res = FALSE;
282 
283   switch (GST_QUERY_TYPE (query)) {
284     case GST_QUERY_CONTEXT:
285     {
286       if (!qt_window_is_scenegraph_initialized (qt_src->window))
287         return FALSE;
288 
289       if (!qt_src->display && !qt_src->qt_context) {
290         qt_src->display = qt_window_get_display (qt_src->window);
291         qt_src->qt_context = qt_window_get_qt_context (qt_src->window);
292       }
293 
294       if (gst_gl_handle_context_query ((GstElement *) qt_src, query,
295           qt_src->display, qt_src->context, qt_src->qt_context))
296         return TRUE;
297 
298       /* fallthrough */
299     }
300     default:
301       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
302       break;
303   }
304 
305   return res;
306 }
307 
308 static gboolean
_find_local_gl_context(GstQtSrc * qt_src)309 _find_local_gl_context (GstQtSrc * qt_src)
310 {
311   if (gst_gl_query_local_gl_context (GST_ELEMENT (qt_src), GST_PAD_SRC,
312       &qt_src->context))
313     return TRUE;
314   return FALSE;
315 }
316 
317 static gboolean
gst_qt_src_decide_allocation(GstBaseSrc * bsrc,GstQuery * query)318 gst_qt_src_decide_allocation (GstBaseSrc * bsrc, GstQuery * query)
319 {
320   GstBufferPool *pool = NULL;
321   GstStructure *config;
322   GstCaps *caps;
323   guint min, max, size, n, i;
324   gboolean update_pool, update_allocator;
325   GstAllocator *allocator;
326   GstAllocationParams params;
327   GstGLVideoAllocationParams *glparams;
328   GstVideoInfo vinfo;
329   GstQtSrc *qt_src = GST_QT_SRC (bsrc);
330 
331   if (gst_query_find_allocation_meta (query,
332           GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, NULL)) {
333     qt_src->downstream_supports_affine_meta = TRUE;
334   } else {
335     qt_src->downstream_supports_affine_meta = FALSE;
336   }
337 
338   gst_query_parse_allocation (query, &caps, NULL);
339   if (!caps)
340     return FALSE;
341 
342   gst_video_info_from_caps (&vinfo, caps);
343 
344   n = gst_query_get_n_allocation_pools (query);
345   if (n > 0) {
346     update_pool = TRUE;
347     for (i = 0; i < n; i++) {
348       gst_query_parse_nth_allocation_pool (query, i, &pool, &size, &min, &max);
349 
350       if (!pool || !GST_IS_GL_BUFFER_POOL (pool)) {
351         if (pool)
352           gst_object_unref (pool);
353         pool = NULL;
354       }
355     }
356   }
357 
358   if (!pool) {
359     size = vinfo.size;
360     min = max = 0;
361     update_pool = FALSE;
362   }
363 
364   if (!qt_src->context && !_find_local_gl_context (qt_src))
365     return FALSE;
366 
367   if (!pool) {
368     if (!qt_src->context || !GST_IS_GL_CONTEXT (qt_src->context))
369       return FALSE;
370 
371     pool = gst_gl_buffer_pool_new (qt_src->context);
372     GST_INFO_OBJECT (qt_src, "No pool, create one ourself %p", pool);
373   }
374 
375   config = gst_buffer_pool_get_config (pool);
376 
377   gst_buffer_pool_config_set_params (config, caps, size, min, max);
378   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
379   if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
380     gst_buffer_pool_config_add_option (config,
381         GST_BUFFER_POOL_OPTION_GL_SYNC_META);
382 
383   if (gst_query_get_n_allocation_params (query) > 0) {
384     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
385     gst_buffer_pool_config_set_allocator (config, allocator, &params);
386     GST_INFO_OBJECT (qt_src, "got allocator %p", allocator);
387     update_allocator = TRUE;
388   } else {
389     allocator = NULL;
390     gst_allocation_params_init (&params);
391     update_allocator = FALSE;
392   }
393 
394   glparams =
395       gst_gl_video_allocation_params_new (qt_src->context, &params, &vinfo, 0,
396       NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA);
397   gst_buffer_pool_config_set_gl_allocation_params (config,
398       (GstGLAllocationParams *) glparams);
399   gst_gl_allocation_params_free ((GstGLAllocationParams *) glparams);
400 
401   if (!gst_buffer_pool_set_config (pool, config))
402     GST_WARNING_OBJECT (qt_src, "Failed to set buffer pool config");
403 
404   if (update_allocator)
405     gst_query_set_nth_allocation_param (query, 0, allocator, &params);
406   else
407     gst_query_add_allocation_param (query, allocator, &params);
408   if (allocator)
409     gst_object_unref (allocator);
410 
411   if (update_pool)
412     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
413   else
414     gst_query_add_allocation_pool (query, pool, size, min, max);
415   gst_object_unref (pool);
416 
417   GST_INFO_OBJECT (qt_src, "successfully decide_allocation");
418   return TRUE;
419 }
420 
421 static GstFlowReturn
gst_qt_src_fill(GstPushSrc * psrc,GstBuffer * buffer)422 gst_qt_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
423 {
424   GstQtSrc *qt_src = GST_QT_SRC (psrc);
425 
426   GST_DEBUG_OBJECT (qt_src, "setting buffer %p", buffer);
427 
428   if (!qt_window_set_buffer (qt_src->window, buffer)) {
429     GST_ERROR_OBJECT (qt_src, "failed to fill buffer %p", buffer);
430     return GST_FLOW_ERROR;
431   }
432 
433   if (!qt_src->downstream_supports_affine_meta) {
434     if (qt_src->pending_image_orientation) {
435       /* let downstream know the image orientation is vertical filp */
436       GstTagList *image_orientation_tag =
437           gst_tag_list_new (GST_TAG_IMAGE_ORIENTATION, "flip-rotate-180", NULL);
438 
439       gst_pad_push_event (GST_BASE_SRC_PAD (psrc),
440           gst_event_new_tag (image_orientation_tag));
441 
442       qt_src->pending_image_orientation = FALSE;
443     }
444   } else {
445     GstVideoAffineTransformationMeta *trans_meta;
446     trans_meta = gst_buffer_add_video_affine_transformation_meta (buffer);
447     gst_video_affine_transformation_meta_apply_matrix (trans_meta,
448         vertical_flip_matrix);
449   }
450 
451   GST_DEBUG_OBJECT (qt_src, "buffer fill done %p", buffer);
452 
453   return GST_FLOW_OK;
454 }
455 
456 static GstStateChangeReturn
gst_qt_src_change_state(GstElement * element,GstStateChange transition)457 gst_qt_src_change_state (GstElement * element, GstStateChange transition)
458 {
459   GstQtSrc *qt_src = GST_QT_SRC (element);
460   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
461   QGuiApplication *app;
462 
463   GST_DEBUG ("changing state: %s => %s",
464       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
465       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
466 
467   switch (transition) {
468     case GST_STATE_CHANGE_NULL_TO_READY:
469       app = static_cast < QGuiApplication * >(QCoreApplication::instance ());
470       if (!app) {
471         GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
472             ("%s", "Failed to connect to Qt"),
473             ("%s", "Could not retrieve QGuiApplication instance"));
474         return GST_STATE_CHANGE_FAILURE;
475       }
476 
477       if (!qt_src->window) {
478         GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
479             ("%s", "Required property \'window\' not set"), (NULL));
480         return GST_STATE_CHANGE_FAILURE;
481       }
482 
483       if (!qt_window_is_scenegraph_initialized (qt_src->window)) {
484         GST_ELEMENT_ERROR (element, RESOURCE, NOT_FOUND,
485             ("%s", "Could not initialize window system"), (NULL));
486         return GST_STATE_CHANGE_FAILURE;
487       }
488 
489       qt_window_use_default_fbo (qt_src->window, qt_src->default_fbo);
490 
491       break;
492     case GST_STATE_CHANGE_READY_TO_PAUSED:
493       break;
494     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
495       break;
496     default:
497       break;
498   }
499 
500   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
501   if (ret == GST_STATE_CHANGE_FAILURE)
502     return ret;
503 
504   switch (transition) {
505     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
506       break;
507     case GST_STATE_CHANGE_PAUSED_TO_READY:
508       break;
509     case GST_STATE_CHANGE_READY_TO_NULL:
510       break;
511     default:
512       break;
513   }
514 
515   return ret;
516 }
517 
518 static gboolean
gst_qt_src_start(GstBaseSrc * basesrc)519 gst_qt_src_start (GstBaseSrc * basesrc)
520 {
521   GstQtSrc *qt_src = GST_QT_SRC (basesrc);
522 
523   /* already has get OpenGL configuration from qt */
524   if (qt_src->display && qt_src->qt_context)
525     return TRUE;
526 
527   if (!qt_window_is_scenegraph_initialized (qt_src->window))
528     return FALSE;
529 
530   qt_src->display = qt_window_get_display (qt_src->window);
531   qt_src->qt_context = qt_window_get_qt_context (qt_src->window);
532 
533   if (!qt_src->display || !qt_src->qt_context) {
534     GST_ERROR_OBJECT (qt_src,
535         "Could not retrieve window system OpenGL configuration");
536     return FALSE;
537   }
538 
539   GST_DEBUG_OBJECT (qt_src, "Got qt display %p and qt gl context %p",
540       qt_src->display, qt_src->qt_context);
541   return TRUE;
542 }
543 
544 static gboolean
gst_qt_src_stop(GstBaseSrc * basesrc)545 gst_qt_src_stop (GstBaseSrc * basesrc)
546 {
547   return TRUE;
548 }
549