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