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