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