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