cfa42c1b545ec00b311502976fd1a5d5699d5075
[platform/upstream/gstreamer.git] / ext / gl / gstglmixer.c
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 "gstglmixer.h"
30
31 #if GST_GL_HAVE_PLATFORM_EGL
32 #include <gst/gl/egl/gsteglimagememory.h>
33 #endif
34
35 #define gst_gl_mixer_parent_class parent_class
36 G_DEFINE_ABSTRACT_TYPE (GstGLMixer, gst_gl_mixer, GST_TYPE_GL_BASE_MIXER);
37
38 #define GST_CAT_DEFAULT gst_gl_mixer_debug
39 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
40
41 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
42     GValue * value, GParamSpec * pspec);
43 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
44     const GValue * value, GParamSpec * pspec);
45
46 enum
47 {
48   PROP_PAD_0
49 };
50
51 #define GST_GL_MIXER_GET_PRIVATE(obj)  \
52     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
53
54 struct _GstGLMixerPrivate
55 {
56   gboolean negotiated;
57
58   gboolean gl_resource_ready;
59   GMutex gl_resource_lock;
60   GCond gl_resource_cond;
61 };
62
63 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_GL_BASE_MIXER_PAD);
64
65 static void
66 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
67 {
68   GObjectClass *gobject_class = (GObjectClass *) klass;
69   GstVideoAggregatorPadClass *vaggpad_class =
70       (GstVideoAggregatorPadClass *) klass;
71
72   gobject_class->set_property = gst_gl_mixer_pad_set_property;
73   gobject_class->get_property = gst_gl_mixer_pad_get_property;
74
75   vaggpad_class->set_info = NULL;
76   vaggpad_class->prepare_frame = NULL;
77   vaggpad_class->clean_frame = NULL;
78 }
79
80 static void
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
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
103 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
104 {
105   GstGLMixer *mix = GST_GL_MIXER (vagg);
106   gboolean ret;
107
108   mix->priv->negotiated = TRUE;
109
110   gst_caps_replace (&mix->out_caps, caps);
111
112   ret = GST_VIDEO_AGGREGATOR_CLASS (parent_class)->negotiated_caps (vagg, caps);
113
114   return ret;
115 }
116
117 static void
118 _find_best_format (GstVideoAggregator * vagg, GstCaps * downstream_caps,
119     GstVideoInfo * best_info, gboolean * at_least_one_alpha)
120 {
121   GstVideoInfo tmp_info;
122
123   GST_VIDEO_AGGREGATOR_CLASS (parent_class)->find_best_format (vagg,
124       downstream_caps, best_info, at_least_one_alpha);
125
126   gst_video_info_set_format (&tmp_info, GST_VIDEO_FORMAT_RGBA,
127       best_info->width, best_info->height);
128   tmp_info.par_n = best_info->par_n;
129   tmp_info.par_d = best_info->par_d;
130   tmp_info.fps_n = best_info->fps_n;
131   tmp_info.fps_d = best_info->fps_d;
132   tmp_info.flags = best_info->flags;
133   tmp_info.interlace_mode = best_info->interlace_mode;
134   *best_info = tmp_info;
135 }
136
137 static gboolean
138 gst_gl_mixer_propose_allocation (GstGLBaseMixer * base_mix,
139     GstGLBaseMixerPad * base_pad, GstQuery * decide_query, GstQuery * query)
140 {
141   GstGLMixer *mix = GST_GL_MIXER (base_mix);
142   GstGLContext *context = base_mix->context;
143   GstBufferPool *pool = NULL;
144   GstStructure *config;
145   GstCaps *caps;
146   guint size = 0;
147   gboolean need_pool;
148
149   gst_query_parse_allocation (query, &caps, &need_pool);
150
151   if (caps == NULL)
152     goto no_caps;
153
154   if (need_pool) {
155     GstVideoInfo info;
156
157     if (!gst_video_info_from_caps (&info, caps))
158       goto invalid_caps;
159
160     GST_DEBUG_OBJECT (mix, "create new pool");
161     pool = gst_gl_buffer_pool_new (context);
162
163     /* the normal size of a frame */
164     size = info.size;
165
166     config = gst_buffer_pool_get_config (pool);
167     gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
168
169     if (!gst_buffer_pool_set_config (pool, config)) {
170       g_object_unref (pool);
171       goto config_failed;
172     }
173
174     gst_query_add_allocation_pool (query, pool, size, 1, 0);
175     g_object_unref (pool);
176   }
177
178   /* we also support various metadata */
179   if (context->gl_vtable->FenceSync)
180     gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
181
182   return TRUE;
183
184   /* ERRORS */
185 no_caps:
186   {
187     GST_DEBUG_OBJECT (mix, "no caps specified");
188     return FALSE;
189   }
190 invalid_caps:
191   {
192     GST_DEBUG_OBJECT (mix, "invalid caps specified");
193     return FALSE;
194   }
195 config_failed:
196   {
197     GST_DEBUG_OBJECT (mix, "failed setting config");
198     return FALSE;
199   }
200 }
201
202 static gboolean
203 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
204     GstCaps * caps)
205 {
206   gboolean ret;
207   GstCaps *template_caps;
208
209   GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
210
211   template_caps = gst_pad_get_pad_template_caps (pad);
212   template_caps = gst_caps_make_writable (template_caps);
213
214   ret = gst_caps_can_intersect (caps, template_caps);
215   GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
216       (ret ? "" : "not "), caps);
217   gst_caps_unref (template_caps);
218
219   return ret;
220 }
221
222 /* copies the given caps */
223 static GstCaps *
224 _update_caps (GstVideoAggregator * vagg, GstCaps * caps, GstCaps * filter)
225 {
226   GstCaps *tmp;
227
228   if (filter) {
229     tmp = gst_caps_intersect (caps, filter);
230   } else {
231     tmp = caps;
232   }
233
234   return gst_gl_caps_replace_all_caps_features (tmp,
235       GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
236 }
237
238 static GstCaps *
239 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
240 {
241   GstCaps *sinkcaps;
242   GstCaps *template_caps;
243   GstCaps *filtered_caps;
244   GstCaps *returned_caps;
245
246   template_caps = gst_pad_get_pad_template_caps (pad);
247
248   sinkcaps = gst_pad_get_current_caps (pad);
249   if (sinkcaps == NULL) {
250     sinkcaps = gst_caps_ref (template_caps);
251   } else {
252     sinkcaps = gst_caps_merge (sinkcaps, gst_caps_ref (template_caps));
253   }
254
255   if (filter) {
256     filtered_caps = gst_caps_intersect (sinkcaps, filter);
257     gst_caps_unref (sinkcaps);
258   } else {
259     filtered_caps = sinkcaps;   /* pass ownership */
260   }
261
262   returned_caps = gst_caps_intersect (filtered_caps, template_caps);
263
264   gst_caps_unref (template_caps);
265   gst_caps_unref (filtered_caps);
266
267   GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
268
269   return returned_caps;
270 }
271
272 static gboolean
273 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
274     GstQuery * query)
275 {
276   gboolean ret = FALSE;
277   GstGLMixer *mix = GST_GL_MIXER (agg);
278
279   GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
280
281   switch (GST_QUERY_TYPE (query)) {
282     case GST_QUERY_CAPS:
283     {
284       GstCaps *filter, *caps;
285
286       gst_query_parse_caps (query, &filter);
287       caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
288       gst_query_set_caps_result (query, caps);
289       gst_caps_unref (caps);
290       ret = TRUE;
291       break;
292     }
293     case GST_QUERY_ACCEPT_CAPS:
294     {
295       GstCaps *caps;
296
297       gst_query_parse_accept_caps (query, &caps);
298       ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
299       gst_query_set_accept_caps_result (query, ret);
300       ret = TRUE;
301       break;
302     }
303     default:
304       ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
305       break;
306   }
307
308   return ret;
309 }
310
311 static void
312 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
313 {
314 }
315
316 /* GLMixer signals and args */
317 enum
318 {
319   /* FILL ME */
320   LAST_SIGNAL
321 };
322
323 enum
324 {
325   PROP_0,
326 };
327
328 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
329     GST_PAD_SRC,
330     GST_PAD_ALWAYS,
331     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
332         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
333             "RGBA"))
334     );
335
336 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
337     GST_PAD_SINK,
338     GST_PAD_REQUEST,
339     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
340         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
341             "RGBA"))
342     );
343
344 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
345 static GstFlowReturn gst_gl_mixer_get_output_buffer (GstVideoAggregator *
346     videoaggregator, GstBuffer ** outbuf);
347 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
348 static gboolean gst_gl_mixer_start (GstAggregator * agg);
349
350 static GstFlowReturn
351 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
352     GstBuffer * outbuffer);
353
354 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
355     const GValue * value, GParamSpec * pspec);
356 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
357     GValue * value, GParamSpec * pspec);
358
359 static gboolean gst_gl_mixer_decide_allocation (GstGLBaseMixer * mix,
360     GstQuery * query);
361
362 static void gst_gl_mixer_finalize (GObject * object);
363
364 static void
365 gst_gl_mixer_class_init (GstGLMixerClass * klass)
366 {
367   GObjectClass *gobject_class = (GObjectClass *) klass;
368   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
369   GstVideoAggregatorClass *videoaggregator_class =
370       (GstVideoAggregatorClass *) klass;
371   GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
372   GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_CLASS (klass);;
373
374   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "OpenGL mixer");
375
376   g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
377
378   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
379
380   gobject_class->get_property = gst_gl_mixer_get_property;
381   gobject_class->set_property = gst_gl_mixer_set_property;
382
383   gst_element_class_add_pad_template (element_class,
384       gst_static_pad_template_get (&src_factory));
385   gst_element_class_add_pad_template (element_class,
386       gst_static_pad_template_get (&sink_factory));
387
388   agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
389   agg_class->sink_query = gst_gl_mixer_sink_query;
390   agg_class->src_query = gst_gl_mixer_src_query;
391   agg_class->stop = gst_gl_mixer_stop;
392   agg_class->start = gst_gl_mixer_start;
393
394   videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
395   videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
396   videoaggregator_class->negotiated_caps = _negotiated_caps;
397   videoaggregator_class->update_caps = _update_caps;
398   videoaggregator_class->find_best_format = _find_best_format;
399
400   mix_class->propose_allocation = gst_gl_mixer_propose_allocation;
401   mix_class->decide_allocation = gst_gl_mixer_decide_allocation;
402
403   /* Register the pad class */
404   g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
405
406   klass->set_caps = NULL;
407 }
408
409 static void
410 gst_gl_mixer_reset (GstGLMixer * mix)
411 {
412   mix->priv->negotiated = FALSE;
413 }
414
415 static void
416 gst_gl_mixer_init (GstGLMixer * mix)
417 {
418   mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
419   mix->array_buffers = 0;
420   mix->fbo = 0;
421   mix->depthbuffer = 0;
422
423   mix->priv->gl_resource_ready = FALSE;
424   g_mutex_init (&mix->priv->gl_resource_lock);
425   g_cond_init (&mix->priv->gl_resource_cond);
426   /* initialize variables */
427   gst_gl_mixer_reset (mix);
428 }
429
430 static void
431 gst_gl_mixer_finalize (GObject * object)
432 {
433   GstGLMixer *mix = GST_GL_MIXER (object);
434   GstGLMixerPrivate *priv = mix->priv;
435
436   if (mix->out_caps)
437     gst_caps_unref (mix->out_caps);
438
439   g_mutex_clear (&priv->gl_resource_lock);
440   g_cond_clear (&priv->gl_resource_cond);
441   G_OBJECT_CLASS (parent_class)->finalize (object);
442 }
443
444 static gboolean
445 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
446 {
447   GstCaps *filter, *current_caps, *retcaps, *template_caps;
448
449   gst_query_parse_caps (query, &filter);
450
451   template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
452
453   current_caps = gst_pad_get_current_caps (pad);
454   if (current_caps == NULL)
455     retcaps = gst_caps_ref (template_caps);
456   else {
457     retcaps = gst_caps_merge (current_caps, template_caps);
458     template_caps = NULL;
459   }
460
461   if (filter) {
462     current_caps =
463         gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
464     gst_caps_unref (retcaps);
465     retcaps = current_caps;
466   }
467
468   gst_query_set_caps_result (query, retcaps);
469   gst_caps_unref (retcaps);
470
471   if (template_caps)
472     gst_caps_unref (template_caps);
473
474   return TRUE;
475 }
476
477 static gboolean
478 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
479 {
480   gboolean res = FALSE;
481
482   switch (GST_QUERY_TYPE (query)) {
483     case GST_QUERY_CAPS:
484       res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
485       break;
486     default:
487       res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
488       break;
489   }
490
491   return res;
492 }
493
494 static GstFlowReturn
495 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
496     GstBuffer ** outbuf)
497 {
498   GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
499   GstBufferPool *pool;
500   GstFlowReturn ret;
501
502   pool =
503       gst_gl_base_mixer_get_buffer_pool (GST_GL_BASE_MIXER (videoaggregator));
504
505   if (!pool)
506     return GST_FLOW_NOT_NEGOTIATED;
507
508   if (!gst_buffer_pool_is_active (pool)) {
509     if (!gst_buffer_pool_set_active (pool, TRUE)) {
510       GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
511           ("failed to activate bufferpool"), ("failed to activate bufferpool"));
512       return GST_FLOW_ERROR;
513     }
514   }
515
516   ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
517   gst_object_unref (pool);
518
519   return ret;
520 }
521
522 static gboolean
523 gst_gl_mixer_decide_allocation (GstGLBaseMixer * base_mix, GstQuery * query)
524 {
525   GstGLMixer *mix = GST_GL_MIXER (base_mix);
526   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
527   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
528   GstGLContext *context = base_mix->context;
529   GstBufferPool *pool = NULL;
530   GstStructure *config;
531   GstCaps *caps;
532   guint min, max, size;
533   gboolean update_pool;
534   guint out_width, out_height;
535
536   out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
537   out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
538
539   g_mutex_lock (&mix->priv->gl_resource_lock);
540   mix->priv->gl_resource_ready = FALSE;
541   if (mix->fbo) {
542     gst_gl_context_del_fbo (context, mix->fbo, mix->depthbuffer);
543     mix->fbo = 0;
544     mix->depthbuffer = 0;
545   }
546
547   if (!gst_gl_context_gen_fbo (context, out_width, out_height,
548           &mix->fbo, &mix->depthbuffer)) {
549     g_cond_signal (&mix->priv->gl_resource_cond);
550     g_mutex_unlock (&mix->priv->gl_resource_lock);
551     goto context_error;
552   }
553
554   gst_query_parse_allocation (query, &caps, NULL);
555
556   if (mixer_class->set_caps)
557     mixer_class->set_caps (mix, caps);
558
559   mix->priv->gl_resource_ready = TRUE;
560   g_cond_signal (&mix->priv->gl_resource_cond);
561   g_mutex_unlock (&mix->priv->gl_resource_lock);
562
563   if (gst_query_get_n_allocation_pools (query) > 0) {
564     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
565
566     update_pool = TRUE;
567   } else {
568     GstVideoInfo vinfo;
569
570     gst_video_info_init (&vinfo);
571     gst_video_info_from_caps (&vinfo, caps);
572     size = vinfo.size;
573     min = max = 0;
574     update_pool = FALSE;
575   }
576
577   if (!pool)
578     pool = gst_gl_buffer_pool_new (context);
579   config = gst_buffer_pool_get_config (pool);
580
581   gst_buffer_pool_config_set_params (config, caps, size, min, max);
582   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
583
584   gst_buffer_pool_set_config (pool, config);
585
586   if (update_pool)
587     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
588   else
589     gst_query_add_allocation_pool (query, pool, size, min, max);
590
591   gst_object_unref (pool);
592
593   return TRUE;
594
595 context_error:
596   {
597     GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("Context error"), (NULL));
598     return FALSE;
599   }
600 }
601
602 static gboolean
603 _upload_frames (GstAggregator * agg, GstAggregatorPad * agg_pad,
604     gpointer user_data)
605 {
606   GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (agg_pad);
607   GstGLMixerPad *pad = GST_GL_MIXER_PAD (agg_pad);
608   GstElement *element = GST_ELEMENT (agg);
609   GstGLMixer *mix = GST_GL_MIXER (agg);
610   GstGLMixerFrameData *frame;
611   guint *array_index, i;
612
613   array_index = (guint *) user_data;
614
615   GST_OBJECT_LOCK (agg);
616   /* make sure the frames array is big enough */
617   i = mix->frames->len;
618   g_ptr_array_set_size (mix->frames, element->numsinkpads);
619   for (; i < element->numsinkpads; i++)
620     mix->frames->pdata[i] = g_new0 (GstGLMixerFrameData, 1);
621
622   frame = g_ptr_array_index (mix->frames, *array_index);
623   frame->pad = pad;
624   frame->texture = 0;
625   GST_OBJECT_UNLOCK (agg);
626
627   if (vaggpad->buffer != NULL) {
628     GstVideoInfo gl_info;
629     GstVideoFrame gl_frame;
630     GstGLSyncMeta *sync_meta;
631
632     gst_video_info_set_format (&gl_info,
633         GST_VIDEO_FORMAT_RGBA,
634         GST_VIDEO_INFO_WIDTH (&vaggpad->info),
635         GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
636
637     sync_meta = gst_buffer_get_gl_sync_meta (vaggpad->buffer);
638     if (sync_meta)
639       gst_gl_sync_meta_wait (sync_meta, GST_GL_BASE_MIXER (mix)->context);
640
641     if (!gst_video_frame_map (&gl_frame, &gl_info, vaggpad->buffer,
642             GST_MAP_READ | GST_MAP_GL)) {
643       GST_ERROR_OBJECT (agg_pad, "Failed to map input frame");
644       return FALSE;
645     }
646
647     frame->texture = *(guint *) gl_frame.data[0];
648     gst_video_frame_unmap (&gl_frame);
649   }
650
651   (*array_index)++;
652
653   return TRUE;
654 }
655
656 gboolean
657 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
658 {
659   guint out_tex;
660   gboolean res = TRUE;
661   guint array_index = 0;
662   GstVideoFrame out_frame;
663   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
664   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
665   GstGLMixerPrivate *priv = mix->priv;
666
667   GST_TRACE ("Processing buffers");
668
669   if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
670           GST_MAP_WRITE | GST_MAP_GL)) {
671     return FALSE;
672   }
673
674   out_tex = *(guint *) out_frame.data[0];
675
676   if (!gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix),
677           (GstAggregatorPadForeachFunc) _upload_frames, &array_index))
678     return FALSE;
679
680   g_mutex_lock (&priv->gl_resource_lock);
681   if (!priv->gl_resource_ready)
682     g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
683
684   if (!priv->gl_resource_ready) {
685     g_mutex_unlock (&priv->gl_resource_lock);
686     GST_ERROR_OBJECT (mix,
687         "fbo used to render can't be created, do not run process_textures");
688     res = FALSE;
689     goto out;
690   }
691
692   mix_class->process_textures (mix, mix->frames, out_tex);
693
694   g_mutex_unlock (&priv->gl_resource_lock);
695
696 out:
697   gst_video_frame_unmap (&out_frame);
698
699   return res;
700 }
701
702 static gboolean
703 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
704 {
705   GList *walk;
706   guint i, array_index = 0;
707   GstElement *element = GST_ELEMENT (mix);
708   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
709
710   GST_OBJECT_LOCK (mix);
711   walk = GST_ELEMENT (mix)->sinkpads;
712   i = mix->frames->len;
713   g_ptr_array_set_size (mix->frames, element->numsinkpads);
714   for (; i < element->numsinkpads; i++)
715     mix->frames->pdata[i] = g_new0 (GstGLMixerFrameData, 1);
716   while (walk) {                /* We walk with this list because it's ordered */
717     GstVideoAggregatorPad *vaggpad = walk->data;
718
719     walk = g_list_next (walk);
720
721     if (vaggpad->buffer != NULL) {
722       /* put buffer into array */
723       mix->array_buffers->pdata[array_index] = vaggpad->buffer;
724     }
725     ++array_index;
726   }
727   GST_OBJECT_UNLOCK (mix);
728
729   return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
730 }
731
732 static GstFlowReturn
733 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
734 {
735   gboolean res = FALSE;
736   GstGLMixer *mix = GST_GL_MIXER (vagg);
737   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
738   GstGLContext *context = GST_GL_BASE_MIXER (mix)->context;
739   GstGLSyncMeta *sync_meta;
740
741   if (mix_class->process_buffers)
742     res = gst_gl_mixer_process_buffers (mix, outbuf);
743   else if (mix_class->process_textures)
744     res = gst_gl_mixer_process_textures (mix, outbuf);
745
746   sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
747   if (sync_meta)
748     gst_gl_sync_meta_set_sync_point (sync_meta, context);
749
750   return res ? GST_FLOW_OK : GST_FLOW_ERROR;
751 }
752
753 static void
754 gst_gl_mixer_get_property (GObject * object,
755     guint prop_id, GValue * value, GParamSpec * pspec)
756 {
757   switch (prop_id) {
758     default:
759       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
760       break;
761   }
762 }
763
764 static void
765 gst_gl_mixer_set_property (GObject * object,
766     guint prop_id, const GValue * value, GParamSpec * pspec)
767 {
768   switch (prop_id) {
769     default:
770       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771       break;
772   }
773 }
774
775 static gboolean
776 gst_gl_mixer_start (GstAggregator * agg)
777 {
778   guint i;
779   GstGLMixer *mix = GST_GL_MIXER (agg);
780   GstElement *element = GST_ELEMENT (agg);
781
782   GST_OBJECT_LOCK (mix);
783   mix->array_buffers = g_ptr_array_new_full (element->numsinkpads, NULL);
784   mix->frames = g_ptr_array_new_full (element->numsinkpads,
785       (GDestroyNotify) g_free);
786
787   g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
788   g_ptr_array_set_size (mix->frames, element->numsinkpads);
789
790   for (i = 0; i < element->numsinkpads; i++)
791     mix->frames->pdata[i] = g_new0 (GstGLMixerFrameData, 1);
792
793   GST_OBJECT_UNLOCK (mix);
794
795   return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
796 }
797
798 static gboolean
799 gst_gl_mixer_stop (GstAggregator * agg)
800 {
801   GstGLMixer *mix = GST_GL_MIXER (agg);
802   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
803   GstGLContext *context = GST_GL_BASE_MIXER (mix)->context;
804
805   GST_OBJECT_LOCK (agg);
806   g_ptr_array_free (mix->frames, TRUE);
807   mix->frames = NULL;
808   g_ptr_array_free (mix->array_buffers, TRUE);
809   mix->array_buffers = NULL;
810   GST_OBJECT_UNLOCK (agg);
811
812   if (mixer_class->reset)
813     mixer_class->reset (mix);
814   if (mix->fbo) {
815     gst_gl_context_del_fbo (context, mix->fbo, mix->depthbuffer);
816     mix->fbo = 0;
817     mix->depthbuffer = 0;
818   }
819
820   gst_gl_mixer_reset (mix);
821
822   return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);
823 }