gl: Remove format info in glcolorconvert's transform_caps function instead of on...
[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/base/gstcollectpads.h>
28 #include <gst/video/video.h>
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36
37 #include "gstglmixer.h"
38
39 #if GST_GL_HAVE_PLATFORM_EGL
40 #include <gst/gl/egl/gsteglimagememory.h>
41 #endif
42
43 #define gst_gl_mixer_parent_class parent_class
44 G_DEFINE_ABSTRACT_TYPE (GstGLMixer, gst_gl_mixer, GST_TYPE_VIDEO_AGGREGATOR);
45 static gboolean gst_gl_mixer_do_bufferpool (GstGLMixer * mix,
46     GstCaps * outcaps);
47
48
49 #define GST_CAT_DEFAULT gst_gl_mixer_debug
50 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
51
52 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
53     GValue * value, GParamSpec * pspec);
54 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
55     const GValue * value, GParamSpec * pspec);
56 static void gst_gl_mixer_pad_finalize (GObject * object);
57 static GstBuffer *_default_pad_upload_buffer (GstGLMixer * mix,
58     GstGLMixerFrameData * frame, GstBuffer * buffer);
59
60 static void gst_gl_mixer_set_context (GstElement * element,
61     GstContext * context);
62
63 enum
64 {
65   PROP_PAD_0
66 };
67
68 #define GST_GL_MIXER_GET_PRIVATE(obj)  \
69     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
70
71 struct _GstGLMixerPrivate
72 {
73   gboolean negotiated;
74
75   GstBufferPool *pool;
76   gboolean pool_active;
77   GstAllocator *allocator;
78   GstAllocationParams params;
79   GstQuery *query;
80
81   gboolean gl_resource_ready;
82   GMutex gl_resource_lock;
83   GCond gl_resource_cond;
84 };
85
86 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_VIDEO_AGGREGATOR_PAD);
87
88 static void
89 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
90 {
91   GObjectClass *gobject_class = (GObjectClass *) klass;
92   GstVideoAggregatorPadClass *vaggpad_class =
93       (GstVideoAggregatorPadClass *) klass;
94
95   gobject_class->set_property = gst_gl_mixer_pad_set_property;
96   gobject_class->get_property = gst_gl_mixer_pad_get_property;
97
98   gobject_class->finalize = gst_gl_mixer_pad_finalize;
99
100   vaggpad_class->set_info = NULL;
101   vaggpad_class->prepare_frame = NULL;
102   vaggpad_class->clean_frame = NULL;
103
104   klass->upload_buffer = _default_pad_upload_buffer;
105 }
106
107 static void
108 gst_gl_mixer_pad_finalize (GObject * object)
109 {
110   GstGLMixerPad *pad = GST_GL_MIXER_PAD (object);
111
112   if (pad->upload) {
113     gst_object_unref (pad->upload);
114     pad->upload = NULL;
115   }
116
117   G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
118 }
119
120 static void
121 _init_upload (GstGLMixer * mix, GstGLMixerPad * pad)
122 {
123   GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (pad);
124
125   if (!pad->upload) {
126     GstCaps *in_caps = gst_pad_get_current_caps (GST_PAD (pad));
127     GstCaps *upload_caps = gst_caps_copy (in_caps);
128     GstCapsFeatures *gl_features =
129         gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
130
131     pad->upload = gst_gl_upload_new (mix->context);
132
133     gst_caps_set_features (upload_caps, 0,
134         gst_caps_features_copy (gl_features));
135     gst_gl_upload_set_caps (pad->upload, in_caps, upload_caps);
136     gst_caps_unref (in_caps);
137
138     if (!pad->convert) {
139       GstVideoInfo gl_info;
140       GstCaps *gl_caps;
141
142       gst_video_info_set_format (&gl_info,
143           GST_VIDEO_FORMAT_RGBA,
144           GST_VIDEO_INFO_WIDTH (&vaggpad->info),
145           GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
146       gl_caps = gst_video_info_to_caps (&gl_info);
147       gst_caps_set_features (gl_caps, 0, gst_caps_features_copy (gl_features));
148
149       pad->convert = gst_gl_color_convert_new (mix->context);
150
151       gst_gl_color_convert_set_caps (pad->convert, upload_caps, gl_caps);
152       gst_caps_unref (gl_caps);
153     }
154
155     gst_caps_unref (upload_caps);
156     gst_caps_features_free (gl_features);
157   }
158 }
159
160 static void
161 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
162     GValue * value, GParamSpec * pspec)
163 {
164   switch (prop_id) {
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167       break;
168   }
169 }
170
171 static void
172 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec)
174 {
175   switch (prop_id) {
176     default:
177       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178       break;
179   }
180 }
181
182 static gboolean
183 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
184 {
185   GstGLMixer *mix = GST_GL_MIXER (vagg);
186   gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
187
188   mix->priv->negotiated = ret;
189
190   gst_caps_replace (&mix->out_caps, caps);
191
192   return ret;
193 }
194
195 static gboolean
196 gst_gl_mixer_propose_allocation (GstGLMixer * mix, GstGLMixerPad * pad,
197     GstQuery * decide_query, GstQuery * query)
198 {
199   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
200   GstBufferPool *pool;
201   GstStructure *config;
202   GstCaps *caps;
203   guint size = 0;
204   gboolean need_pool;
205   GError *error = NULL;
206
207   gst_query_parse_allocation (query, &caps, &need_pool);
208
209   if (caps == NULL)
210     goto no_caps;
211
212   if ((pool = mix->priv->pool))
213     gst_object_ref (pool);
214
215   if (pool != NULL) {
216     GstCaps *pcaps;
217
218     /* we had a pool, check caps */
219     GST_DEBUG_OBJECT (mix, "check existing pool caps");
220     config = gst_buffer_pool_get_config (pool);
221     gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
222
223     if (!gst_caps_is_equal (caps, pcaps)) {
224       GST_DEBUG_OBJECT (mix, "pool has different caps");
225       /* different caps, we can't use this pool */
226       gst_object_unref (pool);
227       pool = NULL;
228     }
229     gst_structure_free (config);
230   }
231
232   if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
233     return FALSE;
234
235   gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
236
237   if (!mix->context) {
238     mix->context = gst_gl_context_new (mix->display);
239     if (!gst_gl_context_create (mix->context, mix->other_context, &error))
240       goto context_error;
241   }
242
243   if (pool == NULL && need_pool) {
244     GstVideoInfo info;
245
246     if (!gst_video_info_from_caps (&info, caps))
247       goto invalid_caps;
248
249     GST_DEBUG_OBJECT (mix, "create new pool");
250     pool = gst_gl_buffer_pool_new (mix->context);
251
252     /* the normal size of a frame */
253     size = info.size;
254
255     config = gst_buffer_pool_get_config (pool);
256     gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
257     if (!gst_buffer_pool_set_config (pool, config))
258       goto config_failed;
259   }
260
261   if (pool) {
262     gst_query_add_allocation_pool (query, pool, size, 1, 0);
263     gst_object_unref (pool);
264   }
265
266   /* we also support various metadata */
267   if (mix->context->gl_vtable->FenceSync)
268     gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
269
270   _init_upload (mix, pad);
271
272   gst_gl_upload_propose_allocation (pad->upload, decide_query, query);
273
274   return TRUE;
275
276   /* ERRORS */
277 no_caps:
278   {
279     GST_DEBUG_OBJECT (mix, "no caps specified");
280     return FALSE;
281   }
282 invalid_caps:
283   {
284     GST_DEBUG_OBJECT (mix, "invalid caps specified");
285     return FALSE;
286   }
287 config_failed:
288   {
289     GST_DEBUG_OBJECT (mix, "failed setting config");
290     return FALSE;
291   }
292 context_error:
293   {
294     GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
295         (NULL));
296     return FALSE;
297   }
298 }
299
300 static gboolean
301 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
302     GstCaps * caps)
303 {
304   gboolean ret;
305   GstCaps *template_caps;
306
307   GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
308
309   template_caps = gst_pad_get_pad_template_caps (pad);
310   template_caps = gst_caps_make_writable (template_caps);
311
312   ret = gst_caps_can_intersect (caps, template_caps);
313   GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
314       (ret ? "" : "not "), caps);
315   gst_caps_unref (template_caps);
316
317   return ret;
318 }
319
320 static GstCaps *
321 gst_gl_mixer_set_caps_features (const GstCaps * caps,
322     const gchar * feature_name)
323 {
324   GstCaps *ret = gst_gl_caps_replace_all_caps_features (caps, feature_name);
325   gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
326   return ret;
327 }
328
329 GstCaps *
330 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
331 {
332   GstCaps *result, *tmp;
333
334   result =
335       gst_gl_color_convert_transform_caps (mix->context, GST_PAD_SRC, caps,
336       NULL);
337   tmp = result;
338   GST_DEBUG_OBJECT (mix, "convert returned caps %" GST_PTR_FORMAT, tmp);
339
340   result = gst_gl_upload_transform_caps (mix->context, GST_PAD_SRC, tmp, NULL);
341   gst_caps_unref (tmp);
342   tmp = result;
343   GST_DEBUG_OBJECT (mix, "transfer returned caps %" GST_PTR_FORMAT, tmp);
344
345   return result;
346 }
347
348 static GstCaps *
349 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
350 {
351   GstCaps *sinkcaps;
352   GstCaps *template_caps;
353   GstCaps *filtered_caps;
354   GstCaps *returned_caps;
355   gboolean had_current_caps = TRUE;
356
357   template_caps = gst_pad_get_pad_template_caps (pad);
358
359   sinkcaps = gst_pad_get_current_caps (pad);
360   if (sinkcaps == NULL) {
361     had_current_caps = FALSE;
362     sinkcaps = template_caps;
363   } else {
364     sinkcaps =
365         gst_caps_merge (sinkcaps, gst_gl_mixer_update_caps (mix, sinkcaps));
366   }
367
368   filtered_caps = sinkcaps;
369   if (filter)
370     filtered_caps = gst_caps_intersect (sinkcaps, filter);
371   returned_caps = gst_caps_intersect (filtered_caps, template_caps);
372
373   if (filter)
374     gst_caps_unref (filtered_caps);
375   if (had_current_caps)
376     gst_caps_unref (template_caps);
377
378   GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
379
380   return returned_caps;
381 }
382
383 static gboolean
384 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
385     GstQuery * query)
386 {
387   gboolean ret = FALSE;
388   GstGLMixer *mix = GST_GL_MIXER (agg);
389   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
390   GstGLMixerPad *pad = GST_GL_MIXER_PAD (bpad);
391
392   GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
393
394   switch (GST_QUERY_TYPE (query)) {
395     case GST_QUERY_CAPS:
396     {
397       GstCaps *filter, *caps;
398
399       gst_query_parse_caps (query, &filter);
400       caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
401       gst_query_set_caps_result (query, caps);
402       gst_caps_unref (caps);
403       ret = TRUE;
404       break;
405     }
406     case GST_QUERY_ACCEPT_CAPS:
407     {
408       GstCaps *caps;
409
410       gst_query_parse_accept_caps (query, &caps);
411       ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
412       gst_query_set_accept_caps_result (query, ret);
413       ret = TRUE;
414       break;
415     }
416     case GST_QUERY_ALLOCATION:
417     {
418       GstQuery *decide_query = NULL;
419
420       GST_OBJECT_LOCK (mix);
421       if (G_UNLIKELY (!mix->priv->negotiated)) {
422         GST_DEBUG_OBJECT (mix,
423             "not negotiated yet, can't answer ALLOCATION query");
424         GST_OBJECT_UNLOCK (mix);
425         return FALSE;
426       }
427       if ((decide_query = mix->priv->query))
428         gst_query_ref (decide_query);
429       GST_OBJECT_UNLOCK (mix);
430
431       GST_DEBUG_OBJECT (mix,
432           "calling propose allocation with query %" GST_PTR_FORMAT,
433           decide_query);
434
435       /* pass the query to the propose_allocation vmethod if any */
436       ret = gst_gl_mixer_propose_allocation (mix, pad, decide_query, query);
437
438       if (decide_query)
439         gst_query_unref (decide_query);
440
441       GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
442       break;
443     }
444     case GST_QUERY_CONTEXT:
445     {
446       ret = gst_gl_handle_context_query ((GstElement *) mix, query,
447           &mix->display, &mix->other_context);
448       if (mix->display)
449         gst_gl_display_filter_gl_api (mix->display,
450             mix_class->supported_gl_api);
451       break;
452     }
453     default:
454       ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
455       break;
456   }
457
458   return ret;
459 }
460
461 static void
462 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
463 {
464 }
465
466 /* GLMixer signals and args */
467 enum
468 {
469   /* FILL ME */
470   LAST_SIGNAL
471 };
472
473 enum
474 {
475   PROP_0,
476   PROP_CONTEXT
477 };
478
479 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
480     GST_PAD_SRC,
481     GST_PAD_ALWAYS,
482     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
483         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
484             "RGBA") "; "
485         GST_VIDEO_CAPS_MAKE_WITH_FEATURES
486         (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
487             "RGBA")
488         "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
489     );
490
491 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
492     GST_PAD_SINK,
493     GST_PAD_REQUEST,
494     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
495         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
496             "RGBA") "; "
497 #if GST_GL_HAVE_PLATFORM_EGL
498         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_EGL_IMAGE,
499             "RGBA") "; "
500 #endif
501         GST_VIDEO_CAPS_MAKE_WITH_FEATURES
502         (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
503             "RGBA")
504         "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
505     );
506
507 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
508 static GstFlowReturn
509 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
510     GstBuffer ** outbuf);
511 static gboolean
512 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
513     gboolean active);
514 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
515 static gboolean gst_gl_mixer_start (GstAggregator * agg);
516
517 static GstFlowReturn
518 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
519     GstBuffer * outbuffer);
520
521 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
522     const GValue * value, GParamSpec * pspec);
523 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
524     GValue * value, GParamSpec * pspec);
525
526 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
527     GstQuery * query);
528 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
529     GstBufferPool * pool, GstAllocator * allocator,
530     GstAllocationParams * params, GstQuery * query);
531
532 static void gst_gl_mixer_finalize (GObject * object);
533
534 static void
535 gst_gl_mixer_class_init (GstGLMixerClass * klass)
536 {
537   GObjectClass *gobject_class;
538   GstElementClass *element_class;
539
540   GstVideoAggregatorClass *videoaggregator_class =
541       (GstVideoAggregatorClass *) klass;
542   GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
543
544   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
545
546   gobject_class = (GObjectClass *) klass;
547   element_class = GST_ELEMENT_CLASS (klass);
548
549   g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
550
551   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
552
553   gobject_class->get_property = gst_gl_mixer_get_property;
554   gobject_class->set_property = gst_gl_mixer_set_property;
555
556   gst_element_class_add_pad_template (element_class,
557       gst_static_pad_template_get (&src_factory));
558   gst_element_class_add_pad_template (element_class,
559       gst_static_pad_template_get (&sink_factory));
560
561   element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
562
563   agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
564   agg_class->sink_query = gst_gl_mixer_sink_query;
565   agg_class->src_query = gst_gl_mixer_src_query;
566   agg_class->src_activate = gst_gl_mixer_src_activate_mode;
567   agg_class->stop = gst_gl_mixer_stop;
568   agg_class->start = gst_gl_mixer_start;
569
570   videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
571   videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
572   videoaggregator_class->negotiated_caps = _negotiated_caps;
573   videoaggregator_class->find_best_format = NULL;
574
575   g_object_class_install_property (gobject_class, PROP_CONTEXT,
576       g_param_spec_object ("context",
577           "OpenGL context",
578           "Get OpenGL context",
579           GST_GL_TYPE_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
580
581   /* Register the pad class */
582   g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
583
584   klass->set_caps = NULL;
585   klass->supported_gl_api = GST_GL_API_ANY;
586 }
587
588 static void
589 gst_gl_mixer_reset (GstGLMixer * mix)
590 {
591   /* clean up collect data */
592   mix->priv->negotiated = FALSE;
593 }
594
595 static void
596 gst_gl_mixer_init (GstGLMixer * mix)
597 {
598   mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
599   mix->array_buffers = 0;
600   mix->display = NULL;
601   mix->fbo = 0;
602   mix->depthbuffer = 0;
603
604   mix->priv->gl_resource_ready = FALSE;
605   g_mutex_init (&mix->priv->gl_resource_lock);
606   g_cond_init (&mix->priv->gl_resource_cond);
607   /* initialize variables */
608   gst_gl_mixer_reset (mix);
609 }
610
611 static void
612 gst_gl_mixer_finalize (GObject * object)
613 {
614   GstGLMixer *mix = GST_GL_MIXER (object);
615   GstGLMixerPrivate *priv = mix->priv;
616
617   if (mix->other_context) {
618     gst_object_unref (mix->other_context);
619     mix->other_context = NULL;
620   }
621
622   g_mutex_clear (&priv->gl_resource_lock);
623   g_cond_clear (&priv->gl_resource_cond);
624   G_OBJECT_CLASS (parent_class)->finalize (object);
625 }
626
627 static void
628 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
629 {
630   GstGLMixer *mix = GST_GL_MIXER (element);
631   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
632
633   gst_gl_handle_set_context (element, context, &mix->display,
634       &mix->other_context);
635
636   if (mix->display)
637     gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
638 }
639
640 static gboolean
641 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
642 {
643   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
644   gboolean result = TRUE;
645
646   if (active) {
647     if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
648       return FALSE;
649
650     gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
651   }
652
653   return result;
654 }
655
656 static gboolean
657 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
658     gboolean active)
659 {
660   GstGLMixer *mix;
661   gboolean result = FALSE;
662
663   mix = GST_GL_MIXER (aggregator);
664
665   switch (mode) {
666     case GST_PAD_MODE_PUSH:
667     case GST_PAD_MODE_PULL:
668       result = gst_gl_mixer_activate (mix, active);
669       break;
670     default:
671       result = TRUE;
672       break;
673   }
674   return result;
675 }
676
677 static gboolean
678 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
679 {
680   GstGLMixer *mix = GST_GL_MIXER (agg);
681   GstCaps *filter, *current_caps, *retcaps, *gl_caps;
682
683   gst_query_parse_caps (query, &filter);
684
685   current_caps = gst_pad_get_current_caps (pad);
686   if (current_caps == NULL)
687     current_caps = gst_pad_get_pad_template_caps (agg->srcpad);
688
689   /* convert from current caps to GLMemory caps */
690   gl_caps =
691       gst_caps_merge (gst_gl_mixer_set_caps_features
692       (current_caps, GST_CAPS_FEATURE_MEMORY_GL_MEMORY),
693       gst_gl_mixer_set_caps_features (current_caps,
694           GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META));
695   retcaps =
696       gst_gl_download_transform_caps (mix->context, GST_PAD_SINK, current_caps,
697       NULL);
698   retcaps = gst_caps_merge (gl_caps, retcaps);
699   gst_caps_unref (current_caps);
700
701   if (filter) {
702     current_caps =
703         gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
704     gst_caps_unref (retcaps);
705     retcaps = current_caps;
706   }
707
708   gst_query_set_caps_result (query, retcaps);
709   gst_caps_unref (retcaps);
710
711   return TRUE;
712 }
713
714 static gboolean
715 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
716 {
717   gboolean res = FALSE;
718   GstGLMixer *mix = GST_GL_MIXER (agg);
719   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
720
721   switch (GST_QUERY_TYPE (query)) {
722     case GST_QUERY_CONTEXT:
723     {
724       res = gst_gl_handle_context_query ((GstElement *) mix, query,
725           &mix->display, &mix->other_context);
726       if (mix->display)
727         gst_gl_display_filter_gl_api (mix->display,
728             mix_class->supported_gl_api);
729       break;
730     }
731     case GST_QUERY_CAPS:
732       res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
733       break;
734     default:
735       res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
736       break;
737   }
738
739   return res;
740 }
741
742 static GstFlowReturn
743 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
744     GstBuffer ** outbuf)
745 {
746   GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
747
748   if (!mix->priv->pool_active) {
749     if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
750       GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
751           ("failed to activate bufferpool"), ("failed to activate bufferpool"));
752       return GST_FLOW_ERROR;
753     }
754     mix->priv->pool_active = TRUE;
755   }
756
757   return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
758 }
759
760 static gboolean
761 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
762 {
763   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
764   GstBufferPool *pool = NULL;
765   GstStructure *config;
766   GstCaps *caps;
767   guint min, max, size;
768   gboolean update_pool;
769   GError *error = NULL;
770   guint idx;
771   guint out_width, out_height;
772   GstGLContext *other_context = NULL;
773   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
774   gboolean same_downstream_gl_context = FALSE;
775
776   if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
777     return FALSE;
778
779   gst_gl_display_filter_gl_api (mix->display, mixer_class->supported_gl_api);
780
781   if (gst_query_find_allocation_meta (query,
782           GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
783     GstGLContext *context;
784     const GstStructure *upload_meta_params;
785     gpointer handle;
786     gchar *type;
787     gchar *apis;
788
789     gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
790     if (upload_meta_params) {
791       if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
792               GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
793         GstGLContext *old = mix->context;
794
795         mix->context = context;
796         if (old)
797           gst_object_unref (old);
798         same_downstream_gl_context = TRUE;
799       } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
800               G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
801               &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
802           && handle) {
803         GstGLPlatform platform;
804         GstGLAPI gl_apis;
805
806         GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
807             handle, type, apis);
808
809         platform = gst_gl_platform_from_string (type);
810         gl_apis = gst_gl_api_from_string (apis);
811
812         if (gl_apis && platform)
813           other_context =
814               gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
815               platform, gl_apis);
816       }
817     }
818   }
819
820   if (mix->other_context) {
821     if (!other_context) {
822       other_context = mix->other_context;
823     } else {
824       GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
825           ("%s", "Cannot share with more than one GL context"),
826           ("%s", "Cannot share with more than one GL context"));
827     }
828   }
829
830   if (!mix->context) {
831     mix->context = gst_gl_context_new (mix->display);
832     if (!gst_gl_context_create (mix->context, other_context, &error))
833       goto context_error;
834   }
835
836   out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
837   out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
838
839   g_mutex_lock (&mix->priv->gl_resource_lock);
840   mix->priv->gl_resource_ready = FALSE;
841   if (mix->fbo) {
842     gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
843     mix->fbo = 0;
844     mix->depthbuffer = 0;
845   }
846
847   if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
848           &mix->fbo, &mix->depthbuffer)) {
849     g_cond_signal (&mix->priv->gl_resource_cond);
850     g_mutex_unlock (&mix->priv->gl_resource_lock);
851     goto context_error;
852   }
853
854   if (mix->out_tex_id)
855     gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
856   gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
857       GST_VIDEO_FORMAT_RGBA, out_width, out_height);
858
859   gst_query_parse_allocation (query, &caps, NULL);
860
861   if (mixer_class->set_caps)
862     mixer_class->set_caps (mix, caps);
863
864   mix->priv->gl_resource_ready = TRUE;
865   g_cond_signal (&mix->priv->gl_resource_cond);
866   g_mutex_unlock (&mix->priv->gl_resource_lock);
867
868   if (gst_query_get_n_allocation_pools (query) > 0) {
869     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
870
871     update_pool = TRUE;
872   } else {
873     GstVideoInfo vinfo;
874
875     gst_video_info_init (&vinfo);
876     gst_video_info_from_caps (&vinfo, caps);
877     size = vinfo.size;
878     min = max = 0;
879     update_pool = FALSE;
880   }
881
882   if (!pool || (!same_downstream_gl_context
883           && gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE,
884               NULL)
885           && !gst_buffer_pool_has_option (pool,
886               GST_BUFFER_POOL_OPTION_GL_SYNC_META))) {
887     /* can't use this pool */
888     if (pool)
889       gst_object_unref (pool);
890     pool = gst_gl_buffer_pool_new (mix->context);
891   }
892   config = gst_buffer_pool_get_config (pool);
893
894   gst_buffer_pool_config_set_params (config, caps, size, min, max);
895   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
896   if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
897     gst_buffer_pool_config_add_option (config,
898         GST_BUFFER_POOL_OPTION_GL_SYNC_META);
899   gst_buffer_pool_config_add_option (config,
900       GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
901
902   gst_buffer_pool_set_config (pool, config);
903
904   if (update_pool)
905     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
906   else
907     gst_query_add_allocation_pool (query, pool, size, min, max);
908
909   gst_object_unref (pool);
910
911   return TRUE;
912
913 context_error:
914   {
915     GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
916         (NULL));
917     return FALSE;
918   }
919 }
920
921 /* takes ownership of the pool, allocator and query */
922 static gboolean
923 gst_gl_mixer_set_allocation (GstGLMixer * mix,
924     GstBufferPool * pool, GstAllocator * allocator,
925     GstAllocationParams * params, GstQuery * query)
926 {
927   GstAllocator *oldalloc;
928   GstBufferPool *oldpool;
929   GstQuery *oldquery;
930   GstGLMixerPrivate *priv = mix->priv;
931
932   GST_DEBUG ("storing allocation query");
933
934   GST_OBJECT_LOCK (mix);
935   oldpool = priv->pool;
936   priv->pool = pool;
937   priv->pool_active = FALSE;
938
939   oldalloc = priv->allocator;
940   priv->allocator = allocator;
941
942   oldquery = priv->query;
943   priv->query = query;
944
945   if (params)
946     priv->params = *params;
947   else
948     gst_allocation_params_init (&priv->params);
949   GST_OBJECT_UNLOCK (mix);
950
951   if (oldpool) {
952     GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
953     gst_buffer_pool_set_active (oldpool, FALSE);
954     gst_object_unref (oldpool);
955   }
956   if (oldalloc) {
957     gst_object_unref (oldalloc);
958   }
959   if (oldquery) {
960     gst_query_unref (oldquery);
961   }
962   return TRUE;
963 }
964
965 static gboolean
966 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
967 {
968   GstQuery *query;
969   gboolean result = TRUE;
970   GstBufferPool *pool = NULL;
971   GstAllocator *allocator;
972   GstAllocationParams params;
973   GstAggregator *agg = GST_AGGREGATOR (mix);
974
975   /* find a pool for the negotiated caps now */
976   GST_DEBUG_OBJECT (mix, "doing allocation query");
977   query = gst_query_new_allocation (outcaps, TRUE);
978   if (!gst_pad_peer_query (agg->srcpad, query)) {
979     /* not a problem, just debug a little */
980     GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
981   }
982
983   GST_DEBUG_OBJECT (mix, "calling decide_allocation");
984   result = gst_gl_mixer_decide_allocation (mix, query);
985
986   GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
987       query);
988
989   if (!result)
990     goto no_decide_allocation;
991
992   /* we got configuration from our peer or the decide_allocation method,
993    * parse them */
994   if (gst_query_get_n_allocation_params (query) > 0) {
995     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
996   } else {
997     allocator = NULL;
998     gst_allocation_params_init (&params);
999   }
1000
1001   if (gst_query_get_n_allocation_pools (query) > 0)
1002     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
1003
1004   /* now store */
1005   result = gst_gl_mixer_set_allocation (mix, pool, allocator, &params, query);
1006
1007   return result;
1008
1009   /* Errors */
1010 no_decide_allocation:
1011   {
1012     GST_WARNING_OBJECT (mix, "Failed to decide allocation");
1013     gst_query_unref (query);
1014
1015     return result;
1016   }
1017 }
1018
1019 static GstBuffer *
1020 _default_pad_upload_buffer (GstGLMixer * mix, GstGLMixerFrameData * frame,
1021     GstBuffer * buffer)
1022 {
1023   GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (frame->pad);
1024   GstGLMixerPad *pad = frame->pad;
1025   GstBuffer *uploaded_buf, *gl_buffer;
1026   GstVideoInfo gl_info;
1027   GstVideoFrame gl_frame;
1028   GstGLSyncMeta *sync_meta;
1029
1030   gst_video_info_set_format (&gl_info,
1031       GST_VIDEO_FORMAT_RGBA,
1032       GST_VIDEO_INFO_WIDTH (&vaggpad->info),
1033       GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
1034
1035   _init_upload (mix, pad);
1036
1037   sync_meta = gst_buffer_get_gl_sync_meta (vaggpad->buffer);
1038   if (sync_meta)
1039     gst_gl_sync_meta_wait (sync_meta);
1040
1041   if (gst_gl_upload_perform_with_buffer (pad->upload,
1042           vaggpad->buffer, &uploaded_buf) != GST_GL_UPLOAD_DONE) {
1043     return NULL;
1044   }
1045
1046   if (!(gl_buffer = gst_gl_color_convert_perform (pad->convert, uploaded_buf))) {
1047     gst_buffer_unref (uploaded_buf);
1048     return NULL;
1049   }
1050
1051   if (!gst_video_frame_map (&gl_frame, &gl_info, gl_buffer,
1052           GST_MAP_READ | GST_MAP_GL)) {
1053     gst_buffer_unref (uploaded_buf);
1054     gst_buffer_unref (gl_buffer);
1055     return NULL;
1056   }
1057
1058   frame->texture = *(guint *) gl_frame.data[0];
1059
1060   gst_buffer_unref (uploaded_buf);
1061   gst_video_frame_unmap (&gl_frame);
1062
1063   return gl_buffer;
1064 }
1065
1066 gboolean
1067 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
1068 {
1069   guint i;
1070   GList *walk;
1071   guint out_tex, out_tex_target;
1072   gboolean res = TRUE;
1073   guint array_index = 0;
1074   GstVideoFrame out_frame;
1075   GstElement *element = GST_ELEMENT (mix);
1076   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
1077   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1078   GstGLMixerPrivate *priv = mix->priv;
1079   gboolean to_download =
1080       gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
1081       gst_caps_get_features (mix->out_caps, 0));
1082   GstMapFlags out_map_flags = GST_MAP_WRITE;
1083
1084   GST_TRACE ("Processing buffers");
1085
1086   to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
1087
1088   if (!to_download)
1089     out_map_flags |= GST_MAP_GL;
1090
1091   if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
1092     return FALSE;
1093   }
1094
1095   if (!to_download) {
1096     out_tex = *(guint *) out_frame.data[0];
1097     out_tex_target =
1098         ((GstGLMemory *) gst_buffer_peek_memory (outbuf, 0))->tex_target;
1099   } else {
1100     GST_INFO ("Output Buffer does not contain correct memory, "
1101         "attempting to wrap for download");
1102
1103     if (!mix->download)
1104       mix->download = gst_gl_download_new (mix->context);
1105
1106     gst_gl_download_set_format (mix->download, &out_frame.info);
1107     out_tex = mix->out_tex_id;
1108     out_tex_target = GL_TEXTURE_2D;
1109   }
1110
1111   GST_OBJECT_LOCK (mix);
1112   walk = element->sinkpads;
1113
1114   i = mix->frames->len;
1115   g_ptr_array_set_size (mix->frames, element->numsinkpads);
1116   for (; i < element->numsinkpads; i++)
1117     mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1118   while (walk) {
1119     GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1120     GstGLMixerPadClass *pad_class = GST_GL_MIXER_PAD_GET_CLASS (pad);
1121     GstVideoAggregatorPad *vaggpad = walk->data;
1122     GstGLMixerFrameData *frame;
1123
1124     frame = g_ptr_array_index (mix->frames, array_index);
1125     frame->pad = pad;
1126     frame->texture = 0;
1127
1128     walk = g_list_next (walk);
1129
1130     if (vaggpad->buffer != NULL) {
1131       g_assert (pad_class->upload_buffer);
1132
1133       if (pad->gl_buffer)
1134         gst_buffer_unref (pad->gl_buffer);
1135       pad->gl_buffer = pad_class->upload_buffer (mix, frame, vaggpad->buffer);
1136
1137       GST_DEBUG_OBJECT (pad,
1138           "uploaded buffer %" GST_PTR_FORMAT " from buffer %" GST_PTR_FORMAT,
1139           pad->gl_buffer, vaggpad->buffer);
1140     }
1141
1142     ++array_index;
1143   }
1144
1145   g_mutex_lock (&priv->gl_resource_lock);
1146   if (!priv->gl_resource_ready)
1147     g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1148
1149   if (!priv->gl_resource_ready) {
1150     g_mutex_unlock (&priv->gl_resource_lock);
1151     GST_ERROR_OBJECT (mix,
1152         "fbo used to render can't be created, do not run process_textures");
1153     res = FALSE;
1154     goto out;
1155   }
1156
1157   mix_class->process_textures (mix, mix->frames, out_tex);
1158
1159   g_mutex_unlock (&priv->gl_resource_lock);
1160
1161   if (to_download) {
1162     if (!gst_gl_download_perform_with_data (mix->download,
1163             out_tex, out_tex_target, out_frame.data)) {
1164       GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1165               "Failed to download video frame"), (NULL));
1166       res = FALSE;
1167       goto out;
1168     }
1169   }
1170
1171 out:
1172   i = 0;
1173   walk = GST_ELEMENT (mix)->sinkpads;
1174   while (walk) {
1175     GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1176
1177     if (pad->upload)
1178       gst_gl_upload_release_buffer (pad->upload);
1179
1180     walk = g_list_next (walk);
1181     i++;
1182   }
1183   GST_OBJECT_UNLOCK (mix);
1184
1185   gst_video_frame_unmap (&out_frame);
1186
1187   return res;
1188 }
1189
1190 static gboolean
1191 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1192 {
1193   GList *walk;
1194   guint i, array_index = 0;
1195   GstElement *element = GST_ELEMENT (mix);
1196   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1197
1198   GST_OBJECT_LOCK (mix);
1199   walk = GST_ELEMENT (mix)->sinkpads;
1200   i = mix->frames->len;
1201   g_ptr_array_set_size (mix->frames, element->numsinkpads);
1202   for (; i < element->numsinkpads; i++)
1203     mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1204   while (walk) {                /* We walk with this list because it's ordered */
1205     GstVideoAggregatorPad *vaggpad = walk->data;
1206
1207     walk = g_list_next (walk);
1208
1209     if (vaggpad->buffer != NULL) {
1210       /* put buffer into array */
1211       mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1212     }
1213     ++array_index;
1214   }
1215   GST_OBJECT_UNLOCK (mix);
1216
1217   return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1218 }
1219
1220
1221
1222 static GstFlowReturn
1223 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1224 {
1225   gboolean res = FALSE;
1226   GstGLMixer *mix = GST_GL_MIXER (vagg);
1227   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1228   GstGLSyncMeta *sync_meta;
1229
1230   if (mix_class->process_buffers)
1231     res = gst_gl_mixer_process_buffers (mix, outbuf);
1232   else if (mix_class->process_textures)
1233     res = gst_gl_mixer_process_textures (mix, outbuf);
1234
1235   sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
1236   if (sync_meta)
1237     gst_gl_sync_meta_set_sync_point (sync_meta, mix->context);
1238
1239   return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1240 }
1241
1242 static void
1243 gst_gl_mixer_get_property (GObject * object,
1244     guint prop_id, GValue * value, GParamSpec * pspec)
1245 {
1246   GstGLMixer *mixer = GST_GL_MIXER (object);
1247
1248   switch (prop_id) {
1249     case PROP_CONTEXT:
1250       g_value_set_object (value, mixer->context);
1251       break;
1252     default:
1253       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1254       break;
1255   }
1256 }
1257
1258 static void
1259 gst_gl_mixer_set_property (GObject * object,
1260     guint prop_id, const GValue * value, GParamSpec * pspec)
1261 {
1262   switch (prop_id) {
1263     default:
1264       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1265       break;
1266   }
1267 }
1268
1269 static gboolean
1270 _clean_upload (GstAggregator * agg, GstAggregatorPad * aggpad, gpointer udata)
1271 {
1272   GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1273
1274   if (pad->gl_buffer) {
1275     gst_buffer_unref (pad->gl_buffer);
1276     pad->gl_buffer = NULL;
1277   }
1278
1279   if (pad->upload) {
1280     gst_object_unref (pad->upload);
1281     pad->upload = NULL;
1282   }
1283
1284   if (pad->convert) {
1285     gst_object_unref (pad->convert);
1286     pad->convert = NULL;
1287   }
1288
1289   return TRUE;
1290 }
1291
1292 static void
1293 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1294 {
1295   g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1296 }
1297
1298 static gboolean
1299 gst_gl_mixer_start (GstAggregator * agg)
1300 {
1301   guint i;
1302   GstGLMixer *mix = GST_GL_MIXER (agg);
1303   GstElement *element = GST_ELEMENT (agg);
1304
1305   GST_OBJECT_LOCK (mix);
1306   mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1307       (GDestroyNotify) _free_glmixer_frame_data);
1308   mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1309
1310   g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1311   g_ptr_array_set_size (mix->frames, element->numsinkpads);
1312
1313   for (i = 0; i < element->numsinkpads; i++)
1314     mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1315
1316   GST_OBJECT_UNLOCK (mix);
1317
1318   return TRUE;
1319 }
1320
1321 static gboolean
1322 gst_gl_mixer_stop (GstAggregator * agg)
1323 {
1324   GstGLMixer *mix = GST_GL_MIXER (agg);
1325   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1326
1327   GST_OBJECT_LOCK (agg);
1328   g_ptr_array_free (mix->frames, TRUE);
1329   mix->frames = NULL;
1330   g_ptr_array_free (mix->array_buffers, TRUE);
1331   mix->array_buffers = NULL;
1332   GST_OBJECT_UNLOCK (agg);
1333
1334   if (mixer_class->reset)
1335     mixer_class->reset (mix);
1336   if (mix->fbo) {
1337     gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1338     mix->fbo = 0;
1339     mix->depthbuffer = 0;
1340   }
1341   if (mix->download) {
1342     gst_object_unref (mix->download);
1343     mix->download = NULL;
1344   }
1345
1346   gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1347
1348   if (mix->priv->query) {
1349     gst_query_unref (mix->priv->query);
1350     mix->priv->query = NULL;
1351   }
1352
1353   if (mix->priv->pool) {
1354     gst_object_unref (mix->priv->pool);
1355     mix->priv->pool = NULL;
1356   }
1357
1358   if (mix->display) {
1359     gst_object_unref (mix->display);
1360     mix->display = NULL;
1361   }
1362
1363   if (mix->context) {
1364     gst_object_unref (mix->context);
1365     mix->context = NULL;
1366   }
1367
1368   gst_gl_mixer_reset (mix);
1369
1370   return TRUE;
1371 }