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