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