videoaggregator: operate on caps rather than video info
[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-libs/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_display (mix, &mix->display))
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);
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   PROP_OTHER_CONTEXT
338 };
339
340 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
341     GST_PAD_SRC,
342     GST_PAD_ALWAYS,
343     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
344         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
345             "RGBA") "; "
346         GST_VIDEO_CAPS_MAKE_WITH_FEATURES
347         (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
348             "RGBA")
349         "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
350     );
351
352 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
353     GST_PAD_SINK,
354     GST_PAD_REQUEST,
355     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
356         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
357             "RGBA") "; "
358         GST_VIDEO_CAPS_MAKE_WITH_FEATURES
359         (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
360             "RGBA")
361         "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
362     );
363
364 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
365 static GstFlowReturn
366 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
367     GstBuffer ** outbuf);
368 static gboolean
369 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
370     gboolean active);
371 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
372 static gboolean gst_gl_mixer_start (GstAggregator * agg);
373
374 static GstFlowReturn
375 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
376     GstBuffer * outbuffer);
377
378 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
379     const GValue * value, GParamSpec * pspec);
380 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
381     GValue * value, GParamSpec * pspec);
382
383 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
384     GstQuery * query);
385 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
386     GstBufferPool * pool, GstAllocator * allocator,
387     GstAllocationParams * params, GstQuery * query);
388
389 static void gst_gl_mixer_finalize (GObject * object);
390
391 static void
392 gst_gl_mixer_class_init (GstGLMixerClass * klass)
393 {
394   GObjectClass *gobject_class;
395   GstElementClass *element_class;
396
397   GstVideoAggregatorClass *videoaggregator_class =
398       (GstVideoAggregatorClass *) klass;
399   GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
400
401   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
402
403   gobject_class = (GObjectClass *) klass;
404   element_class = GST_ELEMENT_CLASS (klass);
405
406   g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
407
408   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
409
410   gobject_class->get_property = gst_gl_mixer_get_property;
411   gobject_class->set_property = gst_gl_mixer_set_property;
412
413   g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
414       g_param_spec_object ("other-context",
415           "External OpenGL context",
416           "Give an external OpenGL context with which to share textures",
417           GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418
419   gst_element_class_add_pad_template (element_class,
420       gst_static_pad_template_get (&src_factory));
421   gst_element_class_add_pad_template (element_class,
422       gst_static_pad_template_get (&sink_factory));
423
424   element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
425
426   agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
427   agg_class->sink_query = gst_gl_mixer_sink_query;
428   agg_class->src_query = gst_gl_mixer_src_query;
429   agg_class->src_activate = gst_gl_mixer_src_activate_mode;
430   agg_class->stop = gst_gl_mixer_stop;
431   agg_class->start = gst_gl_mixer_start;
432
433   videoaggregator_class->disable_frame_conversion = TRUE;
434   videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
435   videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
436   videoaggregator_class->negotiated_caps = _negotiated_caps;
437
438
439   /* Register the pad class */
440   g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
441
442   klass->set_caps = NULL;
443
444 }
445
446 static void
447 gst_gl_mixer_reset (GstGLMixer * mix)
448 {
449   /* clean up collect data */
450   mix->priv->negotiated = FALSE;
451 }
452
453 static void
454 gst_gl_mixer_init (GstGLMixer * mix)
455 {
456   mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
457   mix->array_buffers = 0;
458   mix->display = NULL;
459   mix->fbo = 0;
460   mix->depthbuffer = 0;
461
462   mix->priv->gl_resource_ready = FALSE;
463   g_mutex_init (&mix->priv->gl_resource_lock);
464   g_cond_init (&mix->priv->gl_resource_cond);
465   /* initialize variables */
466   gst_gl_mixer_reset (mix);
467 }
468
469 static void
470 gst_gl_mixer_finalize (GObject * object)
471 {
472   GstGLMixer *mix = GST_GL_MIXER (object);
473   GstGLMixerPrivate *priv = mix->priv;
474
475   if (mix->other_context) {
476     gst_object_unref (mix->other_context);
477     mix->other_context = NULL;
478   }
479
480   g_mutex_clear (&priv->gl_resource_lock);
481   g_cond_clear (&priv->gl_resource_cond);
482   G_OBJECT_CLASS (parent_class)->finalize (object);
483 }
484
485 static void
486 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
487 {
488   GstGLMixer *mix = GST_GL_MIXER (element);
489
490   gst_gl_handle_set_context (element, context, &mix->display);
491 }
492
493 static gboolean
494 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
495 {
496   gboolean result = TRUE;
497
498   if (active) {
499     if (!gst_gl_ensure_display (mix, &mix->display))
500       result = FALSE;
501   }
502
503   return result;
504 }
505
506 static gboolean
507 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
508     gboolean active)
509 {
510   GstGLMixer *mix;
511   gboolean result = FALSE;
512
513   mix = GST_GL_MIXER (aggregator);
514
515   switch (mode) {
516     case GST_PAD_MODE_PUSH:
517     case GST_PAD_MODE_PULL:
518       result = gst_gl_mixer_activate (mix, active);
519       break;
520     default:
521       result = TRUE;
522       break;
523   }
524   return result;
525 }
526
527 static gboolean
528 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
529 {
530   GstCaps *filter, *caps;
531   GstStructure *s;
532   gint n;
533
534   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
535
536   gst_query_parse_caps (query, &filter);
537
538   if (GST_VIDEO_INFO_FORMAT (&vagg->info) != GST_VIDEO_FORMAT_UNKNOWN) {
539     caps = gst_video_info_to_caps (&vagg->info);
540   } else {
541     caps = gst_pad_get_pad_template_caps (agg->srcpad);
542   }
543
544   caps = gst_caps_make_writable (caps);
545
546   n = gst_caps_get_size (caps) - 1;
547   for (; n >= 0; n--) {
548     s = gst_caps_get_structure (caps, n);
549     gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
550         "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
551     if (GST_VIDEO_INFO_FPS_D (&vagg->info) != 0) {
552       gst_structure_set (s,
553           "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
554     }
555   }
556
557   if (filter)
558     caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
559
560   gst_query_set_caps_result (query, caps);
561   gst_caps_unref (caps);
562
563   return TRUE;
564 }
565
566 static GstCaps *
567 gst_gl_mixer_set_caps_features (const GstCaps * caps,
568     const gchar * feature_name)
569 {
570   GstCaps *tmp = gst_caps_copy (caps);
571   guint n = gst_caps_get_size (tmp);
572   guint i = 0;
573
574   for (i = 0; i < n; i++) {
575     GstCapsFeatures *features = gst_caps_get_features (tmp, i);
576     if (features) {
577       guint n_f = gst_caps_features_get_size (features);
578       guint j = 0;
579       for (j = 0; j < n_f; j++) {
580         gst_caps_features_remove_id (features,
581             gst_caps_features_get_nth_id (features, j));
582       }
583     }
584
585     gst_caps_features_add (features, feature_name);
586     gst_caps_set_simple (tmp, "format", G_TYPE_STRING, "RGBA", NULL);
587   }
588
589   return tmp;
590 }
591
592 /* copies the given caps */
593 static GstCaps *
594 gst_gl_mixer_caps_remove_format_info (GstCaps * caps)
595 {
596   GstStructure *st;
597   GstCapsFeatures *f;
598   gint i, n;
599   GstCaps *res;
600
601   res = gst_caps_new_empty ();
602
603   n = gst_caps_get_size (caps);
604   for (i = 0; i < n; i++) {
605     st = gst_caps_get_structure (caps, i);
606     f = gst_caps_get_features (caps, i);
607
608     /* If this is already expressed by the existing caps
609      * skip this structure */
610     if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
611       continue;
612
613     st = gst_structure_copy (st);
614     /* Only remove format info for the cases when we can actually convert */
615     if (!gst_caps_features_is_any (f)
616         && gst_caps_features_is_equal (f,
617             GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
618       gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
619           "width", "height", NULL);
620
621     gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
622   }
623
624   return res;
625 }
626
627 GstCaps *
628 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
629 {
630   GstCaps *result = NULL;
631   GstCaps *glcaps = gst_gl_mixer_set_caps_features (caps,
632       GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
633 #if GST_GL_HAVE_PLATFORM_EGL
634   GstCaps *eglcaps = gst_gl_mixer_set_caps_features (caps,
635       GST_CAPS_FEATURE_MEMORY_EGL_IMAGE);
636 #endif
637   GstCaps *uploadcaps = gst_gl_mixer_set_caps_features (caps,
638       GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META);
639   GstCaps *raw_caps =
640       gst_caps_from_string (GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS));
641
642   result = gst_caps_new_empty ();
643
644   result = gst_caps_merge (result, glcaps);
645 #if GST_GL_HAVE_PLATFORM_EGL
646   result = gst_caps_merge (result, eglcaps);
647 #endif
648   result = gst_caps_merge (result, uploadcaps);
649   result = gst_caps_merge (result, raw_caps);
650
651   result = gst_caps_merge (result, gst_gl_mixer_caps_remove_format_info (caps));
652
653   GST_DEBUG_OBJECT (mix, "returning %" GST_PTR_FORMAT, result);
654
655   return result;
656 }
657
658 static gboolean
659 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
660 {
661   gboolean res = FALSE;
662   GstGLMixer *mix = GST_GL_MIXER (agg);
663
664   switch (GST_QUERY_TYPE (query)) {
665     case GST_QUERY_CONTEXT:
666     {
667       res = gst_gl_handle_context_query ((GstElement *) mix, query,
668           &mix->display);
669       break;
670     }
671     case GST_QUERY_CAPS:
672       res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
673       break;
674     default:
675       res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
676       break;
677   }
678
679   return res;
680 }
681
682 static GstFlowReturn
683 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
684     GstBuffer ** outbuf)
685 {
686   GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
687
688   if (!mix->priv->pool_active) {
689     if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
690       GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
691           ("failed to activate bufferpool"), ("failed to activate bufferpool"));
692       return GST_FLOW_ERROR;
693     }
694     mix->priv->pool_active = TRUE;
695   }
696
697   return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
698 }
699
700 static gboolean
701 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
702 {
703   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
704   GstBufferPool *pool = NULL;
705   GstStructure *config;
706   GstCaps *caps;
707   guint min, max, size;
708   gboolean update_pool;
709   GError *error = NULL;
710   guint idx;
711   guint out_width, out_height;
712   GstGLContext *other_context = NULL;
713   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
714
715   if (!gst_gl_ensure_display (mix, &mix->display))
716     return FALSE;
717
718   if (gst_query_find_allocation_meta (query,
719           GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
720     GstGLContext *context;
721     const GstStructure *upload_meta_params;
722     gpointer handle;
723     gchar *type;
724     gchar *apis;
725
726     gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
727     if (upload_meta_params) {
728       if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
729               GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
730         GstGLContext *old = mix->context;
731
732         mix->context = context;
733         if (old)
734           gst_object_unref (old);
735       } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
736               G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
737               &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
738           && handle) {
739         GstGLPlatform platform;
740         GstGLAPI gl_apis;
741
742         GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
743             handle, type, apis);
744
745         platform = gst_gl_platform_from_string (type);
746         gl_apis = gst_gl_api_from_string (apis);
747
748         if (gl_apis && platform)
749           other_context =
750               gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
751               platform, gl_apis);
752       }
753     }
754   }
755
756   if (mix->other_context) {
757     if (!other_context) {
758       other_context = mix->other_context;
759     } else {
760       GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
761           ("%s", "Cannot share with more than one GL context"),
762           ("%s", "Cannot share with more than one GL context"));
763     }
764   }
765
766   if (!mix->context) {
767     mix->context = gst_gl_context_new (mix->display);
768     if (!gst_gl_context_create (mix->context, other_context, &error))
769       goto context_error;
770   }
771
772   out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
773   out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
774
775   g_mutex_lock (&mix->priv->gl_resource_lock);
776   mix->priv->gl_resource_ready = FALSE;
777   if (mix->fbo) {
778     gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
779     mix->fbo = 0;
780     mix->depthbuffer = 0;
781   }
782
783   if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
784           &mix->fbo, &mix->depthbuffer)) {
785     g_cond_signal (&mix->priv->gl_resource_cond);
786     g_mutex_unlock (&mix->priv->gl_resource_lock);
787     goto context_error;
788   }
789
790   if (mix->out_tex_id)
791     gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
792   gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
793       GST_VIDEO_FORMAT_RGBA, out_width, out_height);
794
795   gst_query_parse_allocation (query, &caps, NULL);
796
797   if (mixer_class->set_caps)
798     mixer_class->set_caps (mix, caps);
799
800   mix->priv->gl_resource_ready = TRUE;
801   g_cond_signal (&mix->priv->gl_resource_cond);
802   g_mutex_unlock (&mix->priv->gl_resource_lock);
803
804   if (gst_query_get_n_allocation_pools (query) > 0) {
805     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
806
807     update_pool = TRUE;
808   } else {
809     GstVideoInfo vinfo;
810
811     gst_video_info_init (&vinfo);
812     gst_video_info_from_caps (&vinfo, caps);
813     size = vinfo.size;
814     min = max = 0;
815     update_pool = FALSE;
816   }
817
818   if (!pool)
819     pool = gst_gl_buffer_pool_new (mix->context);
820
821   config = gst_buffer_pool_get_config (pool);
822   gst_buffer_pool_config_set_params (config, caps, size, min, max);
823
824   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
825
826   gst_buffer_pool_set_config (pool, config);
827
828   if (update_pool)
829     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
830   else
831     gst_query_add_allocation_pool (query, pool, size, min, max);
832
833   gst_object_unref (pool);
834
835   return TRUE;
836
837 context_error:
838   {
839     GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
840         (NULL));
841     return FALSE;
842   }
843 }
844
845 /* takes ownership of the pool, allocator and query */
846 static gboolean
847 gst_gl_mixer_set_allocation (GstGLMixer * mix,
848     GstBufferPool * pool, GstAllocator * allocator,
849     GstAllocationParams * params, GstQuery * query)
850 {
851   GstAllocator *oldalloc;
852   GstBufferPool *oldpool;
853   GstQuery *oldquery;
854   GstGLMixerPrivate *priv = mix->priv;
855
856   GST_DEBUG ("storing allocation query");
857
858   GST_OBJECT_LOCK (mix);
859   oldpool = priv->pool;
860   priv->pool = pool;
861   priv->pool_active = FALSE;
862
863   oldalloc = priv->allocator;
864   priv->allocator = allocator;
865
866   oldquery = priv->query;
867   priv->query = query;
868
869   if (params)
870     priv->params = *params;
871   else
872     gst_allocation_params_init (&priv->params);
873   GST_OBJECT_UNLOCK (mix);
874
875   if (oldpool) {
876     GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
877     gst_buffer_pool_set_active (oldpool, FALSE);
878     gst_object_unref (oldpool);
879   }
880   if (oldalloc) {
881     gst_object_unref (oldalloc);
882   }
883   if (oldquery) {
884     gst_query_unref (oldquery);
885   }
886   return TRUE;
887 }
888
889 static gboolean
890 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
891 {
892   GstQuery *query;
893   gboolean result = TRUE;
894   GstBufferPool *pool = NULL;
895   GstAllocator *allocator;
896   GstAllocationParams params;
897   GstAggregator *agg = GST_AGGREGATOR (mix);
898
899   /* find a pool for the negotiated caps now */
900   GST_DEBUG_OBJECT (mix, "doing allocation query");
901   query = gst_query_new_allocation (outcaps, TRUE);
902   if (!gst_pad_peer_query (agg->srcpad, query)) {
903     /* not a problem, just debug a little */
904     GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
905   }
906
907   GST_DEBUG_OBJECT (mix, "calling decide_allocation");
908   result = gst_gl_mixer_decide_allocation (mix, query);
909
910   GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
911       query);
912
913   if (!result)
914     goto no_decide_allocation;
915
916   /* we got configuration from our peer or the decide_allocation method,
917    * parse them */
918   if (gst_query_get_n_allocation_params (query) > 0) {
919     gst_query_parse_nth_allocation_param (query, 0, &allocator, &params);
920   } else {
921     allocator = NULL;
922     gst_allocation_params_init (&params);
923   }
924
925   if (gst_query_get_n_allocation_pools (query) > 0)
926     gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
927
928   /* now store */
929   result = gst_gl_mixer_set_allocation (mix, pool, allocator, &params, query);
930
931   return result;
932
933   /* Errors */
934 no_decide_allocation:
935   {
936     GST_WARNING_OBJECT (mix, "Failed to decide allocation");
937     gst_query_unref (query);
938
939     return result;
940   }
941 }
942
943 gboolean
944 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
945 {
946   guint i;
947   GList *walk;
948   guint out_tex;
949   gboolean res = TRUE;
950   guint array_index = 0;
951   GstVideoFrame out_frame;
952   GstElement *element = GST_ELEMENT (mix);
953   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
954   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
955   GstGLMixerPrivate *priv = mix->priv;
956   gboolean to_download =
957       gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
958       gst_caps_get_features (mix->out_caps, 0));
959   GstMapFlags out_map_flags = GST_MAP_WRITE;
960
961   GST_TRACE ("Processing buffers");
962
963   to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
964
965   if (!to_download)
966     out_map_flags |= GST_MAP_GL;
967
968   if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
969     return FALSE;
970   }
971
972   if (!to_download) {
973     out_tex = *(guint *) out_frame.data[0];
974   } else {
975     GST_INFO ("Output Buffer does not contain correct memory, "
976         "attempting to wrap for download");
977
978     if (!mix->download)
979       mix->download = gst_gl_download_new (mix->context);
980
981     gst_gl_download_set_format (mix->download, &out_frame.info);
982     out_tex = mix->out_tex_id;
983   }
984
985   GST_OBJECT_LOCK (mix);
986   walk = element->sinkpads;
987
988   i = mix->frames->len;
989   g_ptr_array_set_size (mix->frames, element->numsinkpads);
990   for (; i < element->numsinkpads; i++)
991     mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
992   while (walk) {
993     GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
994     GstVideoAggregatorPad *vaggpad = walk->data;
995     GstGLMixerFrameData *frame;
996
997     frame = g_ptr_array_index (mix->frames, array_index);
998     frame->pad = pad;
999     frame->texture = 0;
1000
1001     walk = g_list_next (walk);
1002
1003     if (vaggpad->buffer != NULL) {
1004       guint in_tex;
1005
1006       if (!pad->upload) {
1007         pad->upload = gst_gl_upload_new (mix->context);
1008
1009         gst_gl_upload_set_format (pad->upload, &vaggpad->info);
1010       }
1011
1012       if (!gst_gl_upload_perform_with_buffer (pad->upload,
1013               vaggpad->buffer, &in_tex, NULL)) {
1014         ++array_index;
1015         pad->mapped = FALSE;
1016         continue;
1017       }
1018       pad->mapped = TRUE;
1019
1020       frame->texture = in_tex;
1021     }
1022     ++array_index;
1023   }
1024
1025   g_mutex_lock (&priv->gl_resource_lock);
1026   if (!priv->gl_resource_ready)
1027     g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1028
1029   if (!priv->gl_resource_ready) {
1030     g_mutex_unlock (&priv->gl_resource_lock);
1031     GST_ERROR_OBJECT (mix,
1032         "fbo used to render can't be created, do not run process_textures");
1033     res = FALSE;
1034     goto out;
1035   }
1036
1037   mix_class->process_textures (mix, mix->frames, out_tex);
1038
1039   g_mutex_unlock (&priv->gl_resource_lock);
1040
1041   if (to_download) {
1042     if (!gst_gl_download_perform_with_data (mix->download, out_tex,
1043             out_frame.data)) {
1044       GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1045               "Failed to download video frame"), (NULL));
1046       res = FALSE;
1047       goto out;
1048     }
1049   }
1050
1051 out:
1052   i = 0;
1053   walk = GST_ELEMENT (mix)->sinkpads;
1054   while (walk) {
1055     GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1056
1057     if (pad->mapped)
1058       gst_gl_upload_release_buffer (pad->upload);
1059
1060     pad->mapped = FALSE;
1061     walk = g_list_next (walk);
1062     i++;
1063   }
1064   GST_OBJECT_UNLOCK (mix);
1065
1066   gst_video_frame_unmap (&out_frame);
1067
1068   return res;
1069 }
1070
1071 static gboolean
1072 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1073 {
1074   GList *walk;
1075   guint i, array_index = 0;
1076   GstElement *element = GST_ELEMENT (mix);
1077   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1078
1079   GST_OBJECT_LOCK (mix);
1080   walk = GST_ELEMENT (mix)->sinkpads;
1081   i = mix->frames->len;
1082   g_ptr_array_set_size (mix->frames, element->numsinkpads);
1083   for (; i < element->numsinkpads; i++)
1084     mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1085   while (walk) {                /* We walk with this list because it's ordered */
1086     GstVideoAggregatorPad *vaggpad = walk->data;
1087
1088     walk = g_list_next (walk);
1089
1090     if (vaggpad->buffer != NULL) {
1091       /* put buffer into array */
1092       mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1093     }
1094     ++array_index;
1095   }
1096   GST_OBJECT_UNLOCK (mix);
1097
1098   return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1099 }
1100
1101
1102
1103 static GstFlowReturn
1104 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1105 {
1106   gboolean res = FALSE;
1107   GstGLMixer *mix = GST_GL_MIXER (vagg);
1108   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1109
1110   if (mix_class->process_buffers)
1111     res = gst_gl_mixer_process_buffers (mix, outbuf);
1112   else if (mix_class->process_textures)
1113     res = gst_gl_mixer_process_textures (mix, outbuf);
1114
1115   return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1116 }
1117
1118 static void
1119 gst_gl_mixer_get_property (GObject * object,
1120     guint prop_id, GValue * value, GParamSpec * pspec)
1121 {
1122   GstGLMixer *mix = GST_GL_MIXER (object);
1123
1124   switch (prop_id) {
1125     case PROP_OTHER_CONTEXT:
1126       g_value_set_object (value, mix->other_context);
1127       break;
1128     default:
1129       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1130       break;
1131   }
1132 }
1133
1134 static void
1135 gst_gl_mixer_set_property (GObject * object,
1136     guint prop_id, const GValue * value, GParamSpec * pspec)
1137 {
1138   GstGLMixer *mix = GST_GL_MIXER (object);
1139
1140   switch (prop_id) {
1141     case PROP_OTHER_CONTEXT:
1142     {
1143       if (mix->other_context)
1144         gst_object_unref (mix->other_context);
1145       mix->other_context = g_value_dup_object (value);
1146       break;
1147     }
1148     default:
1149       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1150       break;
1151   }
1152 }
1153
1154 static gboolean
1155 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1156 {
1157   GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1158
1159   if (pad->upload) {
1160     gst_object_unref (pad->upload);
1161     pad->upload = NULL;
1162   }
1163
1164   return TRUE;
1165 }
1166
1167 static void
1168 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1169 {
1170   g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1171 }
1172
1173 static gboolean
1174 gst_gl_mixer_start (GstAggregator * agg)
1175 {
1176   guint i;
1177   GstGLMixer *mix = GST_GL_MIXER (agg);
1178   GstElement *element = GST_ELEMENT (agg);
1179
1180   if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1181     return FALSE;
1182
1183   GST_OBJECT_LOCK (mix);
1184   mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1185       (GDestroyNotify) _free_glmixer_frame_data);
1186   mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1187
1188   g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1189   g_ptr_array_set_size (mix->frames, element->numsinkpads);
1190
1191   for (i = 0; i < element->numsinkpads; i++)
1192     mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1193
1194   GST_OBJECT_UNLOCK (mix);
1195
1196   return TRUE;
1197 }
1198
1199 static gboolean
1200 gst_gl_mixer_stop (GstAggregator * agg)
1201 {
1202   GstGLMixer *mix = GST_GL_MIXER (agg);
1203   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1204
1205   if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1206     return FALSE;
1207
1208   GST_OBJECT_LOCK (agg);
1209   g_ptr_array_free (mix->frames, TRUE);
1210   mix->frames = NULL;
1211   g_ptr_array_free (mix->array_buffers, TRUE);
1212   mix->array_buffers = NULL;
1213   GST_OBJECT_UNLOCK (agg);
1214
1215   if (mixer_class->reset)
1216     mixer_class->reset (mix);
1217   if (mix->fbo) {
1218     gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1219     mix->fbo = 0;
1220     mix->depthbuffer = 0;
1221   }
1222   if (mix->download) {
1223     gst_object_unref (mix->download);
1224     mix->download = NULL;
1225   }
1226
1227   gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1228
1229   if (mix->priv->query) {
1230     gst_query_unref (mix->priv->query);
1231     mix->priv->query = NULL;
1232   }
1233
1234   if (mix->priv->pool) {
1235     gst_object_unref (mix->priv->pool);
1236     mix->priv->pool = NULL;
1237   }
1238
1239   if (mix->display) {
1240     gst_object_unref (mix->display);
1241     mix->display = NULL;
1242   }
1243
1244   if (mix->context) {
1245     gst_object_unref (mix->context);
1246     mix->context = NULL;
1247   }
1248
1249   gst_gl_mixer_reset (mix);
1250
1251   return TRUE;
1252 }