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