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