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