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