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