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