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