1 /* Generic video mixer plugin
2  *
3  * GStreamer
4  * Copyright (C) 2009 Julien Isorce <julien.isorce@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/gst.h>
27 #include <gst/video/video.h>
28 
29 #include <string.h>
30 
31 #include "gstglmixer.h"
32 
33 #define GST_CAT_DEFAULT gst_gl_mixer_debug
34 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
35 
36 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
37     GValue * value, GParamSpec * pspec);
38 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
39     const GValue * value, GParamSpec * pspec);
40 static gboolean gst_gl_mixer_pad_prepare_frame (GstVideoAggregatorPad * vpad,
41     GstVideoAggregator * vagg, GstBuffer * buffer,
42     GstVideoFrame * prepared_frame);
43 static void gst_gl_mixer_pad_clean_frame (GstVideoAggregatorPad * vpad,
44     GstVideoAggregator * vagg, GstVideoFrame * prepared_frame);
45 
46 enum
47 {
48   PROP_PAD_0
49 };
50 
51 struct _GstGLMixerPrivate
52 {
53   gboolean negotiated;
54 
55   gboolean gl_resource_ready;
56   GMutex gl_resource_lock;
57   GCond gl_resource_cond;
58 };
59 
60 #define gst_gl_mixer_parent_class parent_class
61 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstGLMixer, gst_gl_mixer,
62     GST_TYPE_GL_BASE_MIXER);
63 
64 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_GL_BASE_MIXER_PAD);
65 
66 static void
gst_gl_mixer_pad_class_init(GstGLMixerPadClass * klass)67 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
68 {
69   GObjectClass *gobject_class = (GObjectClass *) klass;
70   GstVideoAggregatorPadClass *vaggpad_class =
71       (GstVideoAggregatorPadClass *) klass;
72 
73   gobject_class->set_property = gst_gl_mixer_pad_set_property;
74   gobject_class->get_property = gst_gl_mixer_pad_get_property;
75 
76   vaggpad_class->prepare_frame = gst_gl_mixer_pad_prepare_frame;
77   vaggpad_class->clean_frame = gst_gl_mixer_pad_clean_frame;
78 }
79 
80 static void
gst_gl_mixer_pad_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)81 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
82     GValue * value, GParamSpec * pspec)
83 {
84   switch (prop_id) {
85     default:
86       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87       break;
88   }
89 }
90 
91 static void
gst_gl_mixer_pad_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)92 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
93     const GValue * value, GParamSpec * pspec)
94 {
95   switch (prop_id) {
96     default:
97       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98       break;
99   }
100 }
101 
102 static gboolean
gst_gl_mixer_pad_prepare_frame(GstVideoAggregatorPad * vpad,GstVideoAggregator * vagg,GstBuffer * buffer,GstVideoFrame * prepared_frame)103 gst_gl_mixer_pad_prepare_frame (GstVideoAggregatorPad * vpad,
104     GstVideoAggregator * vagg, GstBuffer * buffer,
105     GstVideoFrame * prepared_frame)
106 {
107   GstGLMixerPad *pad = GST_GL_MIXER_PAD (vpad);
108   GstGLMixer *mix = GST_GL_MIXER (vagg);
109   GstVideoInfo gl_info;
110   GstGLSyncMeta *sync_meta;
111 
112   pad->current_texture = 0;
113 
114   gst_video_info_set_format (&gl_info,
115       GST_VIDEO_FORMAT_RGBA,
116       GST_VIDEO_INFO_WIDTH (&vpad->info), GST_VIDEO_INFO_HEIGHT (&vpad->info));
117 
118   sync_meta = gst_buffer_get_gl_sync_meta (buffer);
119   if (sync_meta)
120     gst_gl_sync_meta_wait (sync_meta, GST_GL_BASE_MIXER (mix)->context);
121 
122   if (!gst_video_frame_map (prepared_frame, &gl_info, buffer,
123           GST_MAP_READ | GST_MAP_GL)) {
124     GST_ERROR_OBJECT (pad, "Failed to map input frame");
125     return FALSE;
126   }
127 
128   pad->current_texture = *(guint *) prepared_frame->data[0];
129 
130   return TRUE;
131 }
132 
133 static void
gst_gl_mixer_pad_clean_frame(GstVideoAggregatorPad * vpad,GstVideoAggregator * vagg,GstVideoFrame * prepared_frame)134 gst_gl_mixer_pad_clean_frame (GstVideoAggregatorPad * vpad,
135     GstVideoAggregator * vagg, GstVideoFrame * prepared_frame)
136 {
137   GstGLMixerPad *pad = GST_GL_MIXER_PAD (vpad);
138 
139   pad->current_texture = 0;
140   if (prepared_frame->buffer) {
141     gst_video_frame_unmap (prepared_frame);
142     memset (prepared_frame, 0, sizeof (GstVideoFrame));
143   }
144 }
145 
146 static gboolean
_negotiated_caps(GstAggregator * agg,GstCaps * caps)147 _negotiated_caps (GstAggregator * agg, GstCaps * caps)
148 {
149   GstGLMixer *mix = GST_GL_MIXER (agg);
150   gboolean ret;
151 
152   mix->priv->negotiated = TRUE;
153 
154   gst_caps_replace (&mix->out_caps, caps);
155 
156   ret = GST_AGGREGATOR_CLASS (parent_class)->negotiated_src_caps (agg, caps);
157 
158   return ret;
159 }
160 
161 static void
_find_best_format(GstVideoAggregator * vagg,GstCaps * downstream_caps,GstVideoInfo * best_info,gboolean * at_least_one_alpha)162 _find_best_format (GstVideoAggregator * vagg, GstCaps * downstream_caps,
163     GstVideoInfo * best_info, gboolean * at_least_one_alpha)
164 {
165   GstVideoInfo tmp_info;
166 
167   GST_VIDEO_AGGREGATOR_CLASS (parent_class)->find_best_format (vagg,
168       downstream_caps, best_info, at_least_one_alpha);
169 
170   gst_video_info_set_format (&tmp_info, GST_VIDEO_FORMAT_RGBA,
171       best_info->width, best_info->height);
172   tmp_info.par_n = best_info->par_n;
173   tmp_info.par_d = best_info->par_d;
174   tmp_info.fps_n = best_info->fps_n;
175   tmp_info.fps_d = best_info->fps_d;
176   tmp_info.flags = best_info->flags;
177   tmp_info.interlace_mode = best_info->interlace_mode;
178   *best_info = tmp_info;
179 }
180 
181 static gboolean
gst_gl_mixer_propose_allocation(GstAggregator * agg,GstAggregatorPad * agg_pad,GstQuery * decide_query,GstQuery * query)182 gst_gl_mixer_propose_allocation (GstAggregator * agg,
183     GstAggregatorPad * agg_pad, GstQuery * decide_query, GstQuery * query)
184 {
185   GstGLMixer *mix = GST_GL_MIXER (agg);
186   GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
187   GstGLContext *context;
188   GstBufferPool *pool = NULL;
189   GstStructure *config;
190   GstCaps *caps;
191   GstVideoInfo info;
192   guint size = 0;
193   gboolean need_pool;
194 
195   if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->propose_allocation
196       (agg, agg_pad, decide_query, query))
197     return FALSE;
198 
199   context = base_mix->context;
200 
201   gst_query_parse_allocation (query, &caps, &need_pool);
202 
203   if (caps == NULL)
204     goto no_caps;
205 
206   if (!gst_video_info_from_caps (&info, caps))
207     goto invalid_caps;
208 
209   /* the normal size of a frame */
210   size = info.size;
211 
212   if (need_pool) {
213     GST_DEBUG_OBJECT (mix, "create new pool");
214     pool = gst_gl_buffer_pool_new (context);
215 
216     config = gst_buffer_pool_get_config (pool);
217     gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
218 
219     if (!gst_buffer_pool_set_config (pool, config)) {
220       g_object_unref (pool);
221       goto config_failed;
222     }
223   }
224 
225   gst_query_add_allocation_pool (query, pool, size, 1, 0);
226   if (pool)
227     g_object_unref (pool);
228 
229   /* we also support various metadata */
230   if (context->gl_vtable->FenceSync)
231     gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
232 
233   return TRUE;
234 
235   /* ERRORS */
236 no_caps:
237   {
238     GST_DEBUG_OBJECT (mix, "no caps specified");
239     return FALSE;
240   }
241 invalid_caps:
242   {
243     GST_DEBUG_OBJECT (mix, "invalid caps specified");
244     return FALSE;
245   }
246 config_failed:
247   {
248     GST_DEBUG_OBJECT (mix, "failed setting config");
249     return FALSE;
250   }
251 }
252 
253 static gboolean
gst_gl_mixer_pad_sink_acceptcaps(GstPad * pad,GstGLMixer * mix,GstCaps * caps)254 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
255     GstCaps * caps)
256 {
257   gboolean ret;
258   GstCaps *template_caps;
259 
260   GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
261 
262   template_caps = gst_pad_get_pad_template_caps (pad);
263   template_caps = gst_caps_make_writable (template_caps);
264 
265   ret = gst_caps_can_intersect (caps, template_caps);
266   GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
267       (ret ? "" : "not "), caps);
268   gst_caps_unref (template_caps);
269 
270   return ret;
271 }
272 
273 static GstCaps *
gst_gl_mixer_pad_sink_getcaps(GstPad * pad,GstGLMixer * mix,GstCaps * filter)274 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
275 {
276   GstCaps *sinkcaps;
277   GstCaps *template_caps;
278   GstCaps *filtered_caps;
279   GstCaps *returned_caps;
280 
281   template_caps = gst_pad_get_pad_template_caps (pad);
282 
283   sinkcaps = gst_pad_get_current_caps (pad);
284   if (sinkcaps == NULL) {
285     sinkcaps = gst_caps_ref (template_caps);
286   } else {
287     sinkcaps = gst_caps_merge (sinkcaps, gst_caps_ref (template_caps));
288   }
289 
290   if (filter) {
291     filtered_caps = gst_caps_intersect (sinkcaps, filter);
292     gst_caps_unref (sinkcaps);
293   } else {
294     filtered_caps = sinkcaps;   /* pass ownership */
295   }
296 
297   returned_caps = gst_caps_intersect (filtered_caps, template_caps);
298 
299   gst_caps_unref (template_caps);
300   gst_caps_unref (filtered_caps);
301 
302   GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
303 
304   return returned_caps;
305 }
306 
307 static gboolean
gst_gl_mixer_sink_query(GstAggregator * agg,GstAggregatorPad * bpad,GstQuery * query)308 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
309     GstQuery * query)
310 {
311   gboolean ret = FALSE;
312   GstGLMixer *mix = GST_GL_MIXER (agg);
313 
314   GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
315 
316   switch (GST_QUERY_TYPE (query)) {
317     case GST_QUERY_CAPS:
318     {
319       GstCaps *filter, *caps;
320 
321       gst_query_parse_caps (query, &filter);
322       caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
323       gst_query_set_caps_result (query, caps);
324       gst_caps_unref (caps);
325       ret = TRUE;
326       break;
327     }
328     case GST_QUERY_ACCEPT_CAPS:
329     {
330       GstCaps *caps;
331 
332       gst_query_parse_accept_caps (query, &caps);
333       ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
334       gst_query_set_accept_caps_result (query, ret);
335       ret = TRUE;
336       break;
337     }
338     default:
339       ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
340       break;
341   }
342 
343   return ret;
344 }
345 
346 static void
gst_gl_mixer_pad_init(GstGLMixerPad * mixerpad)347 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
348 {
349 }
350 
351 /* GLMixer signals and args */
352 enum
353 {
354   /* FILL ME */
355   LAST_SIGNAL
356 };
357 
358 enum
359 {
360   PROP_0,
361 };
362 
363 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
364     GST_PAD_SRC,
365     GST_PAD_ALWAYS,
366     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
367         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
368             "RGBA"))
369     );
370 
371 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
372     GST_PAD_SINK,
373     GST_PAD_REQUEST,
374     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
375         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
376             "RGBA"))
377     );
378 
379 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
380 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
381 static gboolean gst_gl_mixer_start (GstAggregator * agg);
382 
383 static GstFlowReturn
384 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
385     GstBuffer * outbuffer);
386 
387 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
388     const GValue * value, GParamSpec * pspec);
389 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
390     GValue * value, GParamSpec * pspec);
391 
392 static gboolean gst_gl_mixer_decide_allocation (GstAggregator * agg,
393     GstQuery * query);
394 
395 static void gst_gl_mixer_finalize (GObject * object);
396 
397 static void
gst_gl_mixer_class_init(GstGLMixerClass * klass)398 gst_gl_mixer_class_init (GstGLMixerClass * klass)
399 {
400   GObjectClass *gobject_class = (GObjectClass *) klass;
401   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
402   GstVideoAggregatorClass *videoaggregator_class =
403       (GstVideoAggregatorClass *) klass;
404   GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
405 
406   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "OpenGL mixer");
407 
408   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
409 
410   gobject_class->get_property = gst_gl_mixer_get_property;
411   gobject_class->set_property = gst_gl_mixer_set_property;
412 
413   gst_element_class_add_static_pad_template_with_gtype (element_class,
414       &src_factory, GST_TYPE_AGGREGATOR_PAD);
415   gst_element_class_add_static_pad_template_with_gtype (element_class,
416       &sink_factory, GST_TYPE_GL_MIXER_PAD);
417 
418   agg_class->sink_query = gst_gl_mixer_sink_query;
419   agg_class->src_query = gst_gl_mixer_src_query;
420   agg_class->stop = gst_gl_mixer_stop;
421   agg_class->start = gst_gl_mixer_start;
422   agg_class->negotiated_src_caps = _negotiated_caps;
423   agg_class->decide_allocation = gst_gl_mixer_decide_allocation;
424   agg_class->propose_allocation = gst_gl_mixer_propose_allocation;
425 
426   videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
427   videoaggregator_class->find_best_format = _find_best_format;
428 
429 
430   /* Register the pad class */
431   g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
432 
433   klass->set_caps = NULL;
434 }
435 
436 static void
gst_gl_mixer_reset(GstGLMixer * mix)437 gst_gl_mixer_reset (GstGLMixer * mix)
438 {
439   mix->priv->negotiated = FALSE;
440 }
441 
442 static void
gst_gl_mixer_init(GstGLMixer * mix)443 gst_gl_mixer_init (GstGLMixer * mix)
444 {
445   mix->priv = gst_gl_mixer_get_instance_private (mix);
446 
447   mix->priv->gl_resource_ready = FALSE;
448   g_mutex_init (&mix->priv->gl_resource_lock);
449   g_cond_init (&mix->priv->gl_resource_cond);
450   /* initialize variables */
451   gst_gl_mixer_reset (mix);
452 }
453 
454 static void
gst_gl_mixer_finalize(GObject * object)455 gst_gl_mixer_finalize (GObject * object)
456 {
457   GstGLMixer *mix = GST_GL_MIXER (object);
458   GstGLMixerPrivate *priv = mix->priv;
459 
460   if (mix->out_caps)
461     gst_caps_unref (mix->out_caps);
462 
463   g_mutex_clear (&priv->gl_resource_lock);
464   g_cond_clear (&priv->gl_resource_cond);
465   G_OBJECT_CLASS (parent_class)->finalize (object);
466 }
467 
468 static gboolean
gst_gl_mixer_query_caps(GstPad * pad,GstAggregator * agg,GstQuery * query)469 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
470 {
471   GstCaps *filter, *current_caps, *retcaps, *template_caps;
472 
473   gst_query_parse_caps (query, &filter);
474 
475   template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
476 
477   current_caps = gst_pad_get_current_caps (pad);
478   if (current_caps == NULL)
479     retcaps = gst_caps_ref (template_caps);
480   else {
481     retcaps = gst_caps_merge (current_caps, template_caps);
482     template_caps = NULL;
483   }
484 
485   if (filter) {
486     current_caps =
487         gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
488     gst_caps_unref (retcaps);
489     retcaps = current_caps;
490   }
491 
492   gst_query_set_caps_result (query, retcaps);
493   gst_caps_unref (retcaps);
494 
495   if (template_caps)
496     gst_caps_unref (template_caps);
497 
498   return TRUE;
499 }
500 
501 static gboolean
gst_gl_mixer_src_query(GstAggregator * agg,GstQuery * query)502 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
503 {
504   gboolean res = FALSE;
505 
506   switch (GST_QUERY_TYPE (query)) {
507     case GST_QUERY_CAPS:
508       res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
509       break;
510     default:
511       res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
512       break;
513   }
514 
515   return res;
516 }
517 
518 static void
_mixer_create_fbo(GstGLContext * context,GstGLMixer * mix)519 _mixer_create_fbo (GstGLContext * context, GstGLMixer * mix)
520 {
521   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
522   guint out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
523   guint out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
524 
525   mix->fbo =
526       gst_gl_framebuffer_new_with_default_depth (context, out_width,
527       out_height);
528 }
529 
530 static gboolean
gst_gl_mixer_decide_allocation(GstAggregator * agg,GstQuery * query)531 gst_gl_mixer_decide_allocation (GstAggregator * agg, GstQuery * query)
532 {
533   GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
534   GstGLMixer *mix = GST_GL_MIXER (base_mix);
535   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
536   GstGLContext *context;
537   GstBufferPool *pool = NULL;
538   GstStructure *config;
539   GstCaps *caps;
540   guint min, max, size;
541   gboolean update_pool;
542 
543   if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->decide_allocation (agg,
544           query))
545     return FALSE;
546 
547   context = base_mix->context;
548 
549   g_mutex_lock (&mix->priv->gl_resource_lock);
550   mix->priv->gl_resource_ready = FALSE;
551   if (mix->fbo)
552     gst_object_unref (mix->fbo);
553 
554   gst_gl_context_thread_add (context,
555       (GstGLContextThreadFunc) _mixer_create_fbo, mix);
556   if (!mix->fbo) {
557     g_cond_signal (&mix->priv->gl_resource_cond);
558     g_mutex_unlock (&mix->priv->gl_resource_lock);
559     goto context_error;
560   }
561 
562   gst_query_parse_allocation (query, &caps, NULL);
563 
564   if (mixer_class->set_caps)
565     mixer_class->set_caps (mix, caps);
566 
567   mix->priv->gl_resource_ready = TRUE;
568   g_cond_signal (&mix->priv->gl_resource_cond);
569   g_mutex_unlock (&mix->priv->gl_resource_lock);
570 
571   if (gst_query_get_n_allocation_pools (query) > 0) {
572     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
573 
574     update_pool = TRUE;
575   } else {
576     GstVideoInfo vinfo;
577 
578     gst_video_info_init (&vinfo);
579     gst_video_info_from_caps (&vinfo, caps);
580     size = vinfo.size;
581     min = max = 0;
582     update_pool = FALSE;
583   }
584 
585   if (!pool)
586     pool = gst_gl_buffer_pool_new (context);
587   config = gst_buffer_pool_get_config (pool);
588 
589   gst_buffer_pool_config_set_params (config, caps, size, min, max);
590   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
591 
592   gst_buffer_pool_set_config (pool, config);
593 
594   if (update_pool)
595     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
596   else
597     gst_query_add_allocation_pool (query, pool, size, min, max);
598 
599   gst_object_unref (pool);
600 
601   return TRUE;
602 
603 context_error:
604   {
605     GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("Context error"), (NULL));
606     return FALSE;
607   }
608 }
609 
610 gboolean
gst_gl_mixer_process_textures(GstGLMixer * mix,GstBuffer * outbuf)611 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
612 {
613   GstGLMemory *out_tex;
614   gboolean res = TRUE;
615   GstVideoFrame out_frame;
616   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
617   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
618   GstGLMixerPrivate *priv = mix->priv;
619 
620   GST_TRACE ("Processing buffers");
621 
622   if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
623           GST_MAP_WRITE | GST_MAP_GL)) {
624     return FALSE;
625   }
626 
627   out_tex = (GstGLMemory *) out_frame.map[0].memory;
628 
629   g_mutex_lock (&priv->gl_resource_lock);
630   if (!priv->gl_resource_ready)
631     g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
632 
633   if (!priv->gl_resource_ready) {
634     g_mutex_unlock (&priv->gl_resource_lock);
635     GST_ERROR_OBJECT (mix,
636         "fbo used to render can't be created, do not run process_textures");
637     res = FALSE;
638     goto out;
639   }
640 
641   mix_class->process_textures (mix, out_tex);
642 
643   g_mutex_unlock (&priv->gl_resource_lock);
644 
645 out:
646   gst_video_frame_unmap (&out_frame);
647 
648   return res;
649 }
650 
651 static gboolean
gst_gl_mixer_process_buffers(GstGLMixer * mix,GstBuffer * outbuf)652 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
653 {
654   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
655 
656   return mix_class->process_buffers (mix, outbuf);
657 }
658 
659 static GstFlowReturn
gst_gl_mixer_aggregate_frames(GstVideoAggregator * vagg,GstBuffer * outbuf)660 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
661 {
662   gboolean res = FALSE;
663   GstGLMixer *mix = GST_GL_MIXER (vagg);
664   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
665   GstGLContext *context = GST_GL_BASE_MIXER (mix)->context;
666   GstGLSyncMeta *sync_meta;
667 
668   if (mix_class->process_buffers)
669     res = gst_gl_mixer_process_buffers (mix, outbuf);
670   else if (mix_class->process_textures)
671     res = gst_gl_mixer_process_textures (mix, outbuf);
672 
673   sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
674   if (sync_meta)
675     gst_gl_sync_meta_set_sync_point (sync_meta, context);
676 
677   return res ? GST_FLOW_OK : GST_FLOW_ERROR;
678 }
679 
680 static void
gst_gl_mixer_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)681 gst_gl_mixer_get_property (GObject * object,
682     guint prop_id, GValue * value, GParamSpec * pspec)
683 {
684   switch (prop_id) {
685     default:
686       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
687       break;
688   }
689 }
690 
691 static void
gst_gl_mixer_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)692 gst_gl_mixer_set_property (GObject * object,
693     guint prop_id, const GValue * value, GParamSpec * pspec)
694 {
695   switch (prop_id) {
696     default:
697       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
698       break;
699   }
700 }
701 
702 static gboolean
gst_gl_mixer_start(GstAggregator * agg)703 gst_gl_mixer_start (GstAggregator * agg)
704 {
705   return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
706 }
707 
708 static gboolean
gst_gl_mixer_stop(GstAggregator * agg)709 gst_gl_mixer_stop (GstAggregator * agg)
710 {
711   GstGLMixer *mix = GST_GL_MIXER (agg);
712   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
713 
714   if (mixer_class->reset)
715     mixer_class->reset (mix);
716 
717   if (mix->fbo) {
718     gst_object_unref (mix->fbo);
719     mix->fbo = NULL;
720   }
721 
722   gst_gl_mixer_reset (mix);
723 
724   return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);
725 }
726