1 /* Generic video mixer plugin
4 * Copyright (C) 2009 Julien Isorce <julien.isorce@gmail.com>
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.
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.
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.
27 #include <gst/base/gstcollectpads.h>
28 #include <gst/video/video.h>
37 #include "gstglmixer.h"
39 #if GST_GL_HAVE_PLATFORM_EGL
40 #include <gst/gl/egl/gsteglimagememory.h>
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,
49 #define GST_CAT_DEFAULT gst_gl_mixer_debug
50 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
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);
58 static void gst_gl_mixer_set_context (GstElement * element,
59 GstContext * context);
66 #define GST_GL_MIXER_GET_PRIVATE(obj) \
67 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
69 struct _GstGLMixerPrivate
75 GstAllocator *allocator;
76 GstAllocationParams params;
79 gboolean gl_resource_ready;
80 GMutex gl_resource_lock;
81 GCond gl_resource_cond;
84 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_VIDEO_AGGREGATOR_PAD);
87 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
89 GObjectClass *gobject_class = (GObjectClass *) klass;
90 GstVideoAggregatorPadClass *vaggpad_class =
91 (GstVideoAggregatorPadClass *) klass;
93 gobject_class->set_property = gst_gl_mixer_pad_set_property;
94 gobject_class->get_property = gst_gl_mixer_pad_get_property;
96 gobject_class->finalize = gst_gl_mixer_pad_finalize;
98 vaggpad_class->set_info = NULL;
99 vaggpad_class->prepare_frame = NULL;
100 vaggpad_class->clean_frame = NULL;
104 gst_gl_mixer_pad_finalize (GObject * object)
106 GstGLMixerPad *pad = GST_GL_MIXER_PAD (object);
109 gst_object_unref (pad->upload);
113 G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
117 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
118 GValue * value, GParamSpec * pspec)
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
129 const GValue * value, GParamSpec * pspec)
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
139 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
141 GstGLMixer *mix = GST_GL_MIXER (vagg);
142 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
144 mix->priv->negotiated = ret;
146 gst_caps_replace (&mix->out_caps, caps);
152 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
153 GstQuery * decide_query, GstQuery * query)
155 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
157 GstStructure *config;
161 GError *error = NULL;
162 GstStructure *gl_context;
163 gchar *platform, *gl_apis;
165 GstAllocator *allocator = NULL;
166 GstAllocationParams params;
168 gst_query_parse_allocation (query, &caps, &need_pool);
173 if ((pool = mix->priv->pool))
174 gst_object_ref (pool);
179 /* we had a pool, check caps */
180 GST_DEBUG_OBJECT (mix, "check existing pool caps");
181 config = gst_buffer_pool_get_config (pool);
182 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
184 if (!gst_caps_is_equal (caps, pcaps)) {
185 GST_DEBUG_OBJECT (mix, "pool has different caps");
186 /* different caps, we can't use this pool */
187 gst_object_unref (pool);
190 gst_structure_free (config);
193 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
196 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
199 mix->context = gst_gl_context_new (mix->display);
200 if (!gst_gl_context_create (mix->context, mix->other_context, &error))
204 if (pool == NULL && need_pool) {
207 if (!gst_video_info_from_caps (&info, caps))
210 GST_DEBUG_OBJECT (mix, "create new pool");
211 pool = gst_gl_buffer_pool_new (mix->context);
213 /* the normal size of a frame */
216 config = gst_buffer_pool_get_config (pool);
217 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
218 if (!gst_buffer_pool_set_config (pool, config))
223 gst_query_add_allocation_pool (query, pool, size, 1, 0);
224 gst_object_unref (pool);
227 /* we also support various metadata */
228 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
229 if (mix->context->gl_vtable->FenceSync)
230 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
232 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
234 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
235 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
238 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
239 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
240 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
241 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
242 gst_query_add_allocation_meta (query,
243 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
247 gst_structure_free (gl_context);
249 gst_allocation_params_init (¶ms);
251 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
252 gst_query_add_allocation_param (query, allocator, ¶ms);
253 gst_object_unref (allocator);
260 GST_DEBUG_OBJECT (mix, "no caps specified");
265 GST_DEBUG_OBJECT (mix, "invalid caps specified");
270 GST_DEBUG_OBJECT (mix, "failed setting config");
275 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
282 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
286 GstCaps *template_caps;
288 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
290 template_caps = gst_pad_get_pad_template_caps (pad);
291 template_caps = gst_caps_make_writable (template_caps);
293 ret = gst_caps_can_intersect (caps, template_caps);
294 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
295 (ret ? "" : "not "), caps);
296 gst_caps_unref (template_caps);
302 gst_gl_mixer_set_caps_features (const GstCaps * caps,
303 const gchar * feature_name)
305 GstCaps *tmp = gst_caps_copy (caps);
306 guint n = gst_caps_get_size (tmp);
309 for (i = 0; i < n; i++) {
310 GstCapsFeatures *features = gst_caps_get_features (tmp, i);
312 guint n_f = gst_caps_features_get_size (features);
314 for (j = 0; j < n_f; j++) {
315 gst_caps_features_remove_id (features,
316 gst_caps_features_get_nth_id (features, j));
320 gst_caps_features_add (features, feature_name);
321 gst_caps_set_simple (tmp, "format", G_TYPE_STRING, "RGBA", NULL);
327 /* copies the given caps */
329 gst_gl_mixer_caps_remove_format_info (GstCaps * caps)
336 res = gst_caps_new_empty ();
338 n = gst_caps_get_size (caps);
339 for (i = 0; i < n; i++) {
340 st = gst_caps_get_structure (caps, i);
341 f = gst_caps_get_features (caps, i);
343 /* If this is already expressed by the existing caps
344 * skip this structure */
345 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
348 st = gst_structure_copy (st);
349 /* Only remove format info for the cases when we can actually convert */
350 if (!gst_caps_features_is_any (f)
351 && gst_caps_features_is_equal (f,
352 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
353 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
355 gst_structure_remove_fields (st, "width", "height", NULL);
357 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
364 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
366 GstCaps *result = NULL;
367 GstCaps *glcaps = gst_gl_mixer_set_caps_features (caps,
368 GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
369 #if GST_GL_HAVE_PLATFORM_EGL
370 GstCaps *eglcaps = gst_gl_mixer_set_caps_features (caps,
371 GST_CAPS_FEATURE_MEMORY_EGL_IMAGE);
373 GstCaps *uploadcaps = gst_gl_mixer_set_caps_features (caps,
374 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META);
376 gst_caps_from_string (GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS));
378 result = gst_caps_new_empty ();
380 result = gst_caps_merge (result, glcaps);
381 #if GST_GL_HAVE_PLATFORM_EGL
382 result = gst_caps_merge (result, eglcaps);
384 result = gst_caps_merge (result, uploadcaps);
385 result = gst_caps_merge (result, raw_caps);
387 result = gst_caps_merge (result, gst_gl_mixer_caps_remove_format_info (caps));
389 GST_DEBUG_OBJECT (mix, "returning %" GST_PTR_FORMAT, result);
395 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
398 GstCaps *template_caps;
399 GstCaps *filtered_caps;
400 GstCaps *returned_caps;
401 gboolean had_current_caps = TRUE;
403 template_caps = gst_pad_get_pad_template_caps (pad);
405 srccaps = gst_pad_get_current_caps (pad);
406 if (srccaps == NULL) {
407 had_current_caps = FALSE;
408 srccaps = template_caps;
410 srccaps = gst_caps_merge (srccaps, gst_gl_mixer_update_caps (mix, srccaps));
413 filtered_caps = srccaps;
415 filtered_caps = gst_caps_intersect (srccaps, filter);
416 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
419 gst_caps_unref (filtered_caps);
420 if (had_current_caps)
421 gst_caps_unref (template_caps);
423 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
425 return returned_caps;
429 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
432 gboolean ret = FALSE;
433 GstGLMixer *mix = GST_GL_MIXER (agg);
434 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
436 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
438 switch (GST_QUERY_TYPE (query)) {
441 GstCaps *filter, *caps;
443 gst_query_parse_caps (query, &filter);
444 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
445 gst_query_set_caps_result (query, caps);
446 gst_caps_unref (caps);
450 case GST_QUERY_ACCEPT_CAPS:
454 gst_query_parse_accept_caps (query, &caps);
455 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
456 gst_query_set_accept_caps_result (query, ret);
460 case GST_QUERY_ALLOCATION:
462 GstQuery *decide_query = NULL;
464 GST_OBJECT_LOCK (mix);
465 if (G_UNLIKELY (!mix->priv->negotiated)) {
466 GST_DEBUG_OBJECT (mix,
467 "not negotiated yet, can't answer ALLOCATION query");
468 GST_OBJECT_UNLOCK (mix);
471 if ((decide_query = mix->priv->query))
472 gst_query_ref (decide_query);
473 GST_OBJECT_UNLOCK (mix);
475 GST_DEBUG_OBJECT (mix,
476 "calling propose allocation with query %" GST_PTR_FORMAT,
479 /* pass the query to the propose_allocation vmethod if any */
480 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
483 gst_query_unref (decide_query);
485 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
488 case GST_QUERY_CONTEXT:
490 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
491 &mix->display, &mix->other_context);
493 gst_gl_display_filter_gl_api (mix->display,
494 mix_class->supported_gl_api);
498 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
506 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
510 /* GLMixer signals and args */
523 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
526 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
527 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
529 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
530 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
532 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
535 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
538 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
539 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
541 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
542 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
544 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
547 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
549 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
550 GstBuffer ** outbuf);
552 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
554 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
555 static gboolean gst_gl_mixer_start (GstAggregator * agg);
558 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
559 GstBuffer * outbuffer);
561 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
562 const GValue * value, GParamSpec * pspec);
563 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
564 GValue * value, GParamSpec * pspec);
566 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
568 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
569 GstBufferPool * pool, GstAllocator * allocator,
570 GstAllocationParams * params, GstQuery * query);
572 static void gst_gl_mixer_finalize (GObject * object);
575 gst_gl_mixer_class_init (GstGLMixerClass * klass)
577 GObjectClass *gobject_class;
578 GstElementClass *element_class;
580 GstVideoAggregatorClass *videoaggregator_class =
581 (GstVideoAggregatorClass *) klass;
582 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
584 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
586 gobject_class = (GObjectClass *) klass;
587 element_class = GST_ELEMENT_CLASS (klass);
589 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
591 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
593 gobject_class->get_property = gst_gl_mixer_get_property;
594 gobject_class->set_property = gst_gl_mixer_set_property;
596 gst_element_class_add_pad_template (element_class,
597 gst_static_pad_template_get (&src_factory));
598 gst_element_class_add_pad_template (element_class,
599 gst_static_pad_template_get (&sink_factory));
601 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
603 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
604 agg_class->sink_query = gst_gl_mixer_sink_query;
605 agg_class->src_query = gst_gl_mixer_src_query;
606 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
607 agg_class->stop = gst_gl_mixer_stop;
608 agg_class->start = gst_gl_mixer_start;
610 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
611 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
612 videoaggregator_class->negotiated_caps = _negotiated_caps;
613 videoaggregator_class->find_best_format = NULL;
615 g_object_class_install_property (gobject_class, PROP_CONTEXT,
616 g_param_spec_object ("context",
618 "Get OpenGL context",
619 GST_GL_TYPE_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
621 /* Register the pad class */
622 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
624 klass->set_caps = NULL;
625 klass->supported_gl_api = GST_GL_API_ANY;
629 gst_gl_mixer_reset (GstGLMixer * mix)
631 /* clean up collect data */
632 mix->priv->negotiated = FALSE;
636 gst_gl_mixer_init (GstGLMixer * mix)
638 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
639 mix->array_buffers = 0;
642 mix->depthbuffer = 0;
644 mix->priv->gl_resource_ready = FALSE;
645 g_mutex_init (&mix->priv->gl_resource_lock);
646 g_cond_init (&mix->priv->gl_resource_cond);
647 /* initialize variables */
648 gst_gl_mixer_reset (mix);
652 gst_gl_mixer_finalize (GObject * object)
654 GstGLMixer *mix = GST_GL_MIXER (object);
655 GstGLMixerPrivate *priv = mix->priv;
657 if (mix->other_context) {
658 gst_object_unref (mix->other_context);
659 mix->other_context = NULL;
662 g_mutex_clear (&priv->gl_resource_lock);
663 g_cond_clear (&priv->gl_resource_cond);
664 G_OBJECT_CLASS (parent_class)->finalize (object);
668 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
670 GstGLMixer *mix = GST_GL_MIXER (element);
671 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
673 gst_gl_handle_set_context (element, context, &mix->display,
674 &mix->other_context);
677 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
681 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
683 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
684 gboolean result = TRUE;
687 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
690 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
697 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
701 gboolean result = FALSE;
703 mix = GST_GL_MIXER (aggregator);
706 case GST_PAD_MODE_PUSH:
707 case GST_PAD_MODE_PULL:
708 result = gst_gl_mixer_activate (mix, active);
718 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
720 GstCaps *filter, *current_caps, *retcaps;
722 gst_query_parse_caps (query, &filter);
724 current_caps = gst_pad_get_current_caps (pad);
725 if (current_caps == NULL)
726 current_caps = gst_pad_get_pad_template_caps (agg->srcpad);
728 retcaps = gst_gl_mixer_caps_remove_format_info (current_caps);
729 gst_caps_unref (current_caps);
733 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
735 gst_query_set_caps_result (query, retcaps);
736 gst_caps_unref (retcaps);
742 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
744 gboolean res = FALSE;
745 GstGLMixer *mix = GST_GL_MIXER (agg);
746 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
748 switch (GST_QUERY_TYPE (query)) {
749 case GST_QUERY_CONTEXT:
751 res = gst_gl_handle_context_query ((GstElement *) mix, query,
752 &mix->display, &mix->other_context);
754 gst_gl_display_filter_gl_api (mix->display,
755 mix_class->supported_gl_api);
759 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
762 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
770 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
773 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
775 if (!mix->priv->pool_active) {
776 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
777 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
778 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
779 return GST_FLOW_ERROR;
781 mix->priv->pool_active = TRUE;
784 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
788 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
790 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
791 GstBufferPool *pool = NULL;
792 GstStructure *config;
794 guint min, max, size;
795 gboolean update_pool;
796 GError *error = NULL;
798 guint out_width, out_height;
799 GstGLContext *other_context = NULL;
800 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
801 gboolean same_downstream_gl_context = FALSE;
803 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
806 gst_gl_display_filter_gl_api (mix->display, mixer_class->supported_gl_api);
808 if (gst_query_find_allocation_meta (query,
809 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
810 GstGLContext *context;
811 const GstStructure *upload_meta_params;
816 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
817 if (upload_meta_params) {
818 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
819 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
820 GstGLContext *old = mix->context;
822 mix->context = context;
824 gst_object_unref (old);
825 same_downstream_gl_context = TRUE;
826 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
827 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
828 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
830 GstGLPlatform platform;
833 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
836 platform = gst_gl_platform_from_string (type);
837 gl_apis = gst_gl_api_from_string (apis);
839 if (gl_apis && platform)
841 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
847 if (mix->other_context) {
848 if (!other_context) {
849 other_context = mix->other_context;
851 GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
852 ("%s", "Cannot share with more than one GL context"),
853 ("%s", "Cannot share with more than one GL context"));
858 mix->context = gst_gl_context_new (mix->display);
859 if (!gst_gl_context_create (mix->context, other_context, &error))
863 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
864 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
866 g_mutex_lock (&mix->priv->gl_resource_lock);
867 mix->priv->gl_resource_ready = FALSE;
869 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
871 mix->depthbuffer = 0;
874 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
875 &mix->fbo, &mix->depthbuffer)) {
876 g_cond_signal (&mix->priv->gl_resource_cond);
877 g_mutex_unlock (&mix->priv->gl_resource_lock);
882 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
883 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
884 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
886 gst_query_parse_allocation (query, &caps, NULL);
888 if (mixer_class->set_caps)
889 mixer_class->set_caps (mix, caps);
891 mix->priv->gl_resource_ready = TRUE;
892 g_cond_signal (&mix->priv->gl_resource_cond);
893 g_mutex_unlock (&mix->priv->gl_resource_lock);
895 if (gst_query_get_n_allocation_pools (query) > 0) {
896 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
902 gst_video_info_init (&vinfo);
903 gst_video_info_from_caps (&vinfo, caps);
909 if (!pool || (!same_downstream_gl_context
910 && gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE,
912 && !gst_buffer_pool_has_option (pool,
913 GST_BUFFER_POOL_OPTION_GL_SYNC_META))) {
914 /* can't use this pool */
916 gst_object_unref (pool);
917 pool = gst_gl_buffer_pool_new (mix->context);
919 config = gst_buffer_pool_get_config (pool);
921 gst_buffer_pool_config_set_params (config, caps, size, min, max);
922 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
923 if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
924 gst_buffer_pool_config_add_option (config,
925 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
926 gst_buffer_pool_config_add_option (config,
927 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
929 gst_buffer_pool_set_config (pool, config);
932 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
934 gst_query_add_allocation_pool (query, pool, size, min, max);
936 gst_object_unref (pool);
942 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
948 /* takes ownership of the pool, allocator and query */
950 gst_gl_mixer_set_allocation (GstGLMixer * mix,
951 GstBufferPool * pool, GstAllocator * allocator,
952 GstAllocationParams * params, GstQuery * query)
954 GstAllocator *oldalloc;
955 GstBufferPool *oldpool;
957 GstGLMixerPrivate *priv = mix->priv;
959 GST_DEBUG ("storing allocation query");
961 GST_OBJECT_LOCK (mix);
962 oldpool = priv->pool;
964 priv->pool_active = FALSE;
966 oldalloc = priv->allocator;
967 priv->allocator = allocator;
969 oldquery = priv->query;
973 priv->params = *params;
975 gst_allocation_params_init (&priv->params);
976 GST_OBJECT_UNLOCK (mix);
979 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
980 gst_buffer_pool_set_active (oldpool, FALSE);
981 gst_object_unref (oldpool);
984 gst_object_unref (oldalloc);
987 gst_query_unref (oldquery);
993 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
996 gboolean result = TRUE;
997 GstBufferPool *pool = NULL;
998 GstAllocator *allocator;
999 GstAllocationParams params;
1000 GstAggregator *agg = GST_AGGREGATOR (mix);
1002 /* find a pool for the negotiated caps now */
1003 GST_DEBUG_OBJECT (mix, "doing allocation query");
1004 query = gst_query_new_allocation (outcaps, TRUE);
1005 if (!gst_pad_peer_query (agg->srcpad, query)) {
1006 /* not a problem, just debug a little */
1007 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
1010 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
1011 result = gst_gl_mixer_decide_allocation (mix, query);
1013 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
1017 goto no_decide_allocation;
1019 /* we got configuration from our peer or the decide_allocation method,
1021 if (gst_query_get_n_allocation_params (query) > 0) {
1022 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1025 gst_allocation_params_init (¶ms);
1028 if (gst_query_get_n_allocation_pools (query) > 0)
1029 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
1032 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
1037 no_decide_allocation:
1039 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
1040 gst_query_unref (query);
1047 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
1052 gboolean res = TRUE;
1053 guint array_index = 0;
1054 GstVideoFrame out_frame;
1055 GstElement *element = GST_ELEMENT (mix);
1056 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
1057 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1058 GstGLMixerPrivate *priv = mix->priv;
1059 gboolean to_download =
1060 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
1061 gst_caps_get_features (mix->out_caps, 0));
1062 GstMapFlags out_map_flags = GST_MAP_WRITE;
1064 GST_TRACE ("Processing buffers");
1066 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
1069 out_map_flags |= GST_MAP_GL;
1071 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
1076 out_tex = *(guint *) out_frame.data[0];
1078 GST_INFO ("Output Buffer does not contain correct memory, "
1079 "attempting to wrap for download");
1082 mix->download = gst_gl_download_new (mix->context);
1084 gst_gl_download_set_format (mix->download, &out_frame.info);
1085 out_tex = mix->out_tex_id;
1088 GST_OBJECT_LOCK (mix);
1089 walk = element->sinkpads;
1091 i = mix->frames->len;
1092 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1093 for (; i < element->numsinkpads; i++)
1094 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1096 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1097 GstVideoAggregatorPad *vaggpad = walk->data;
1098 GstGLMixerFrameData *frame;
1100 frame = g_ptr_array_index (mix->frames, array_index);
1104 walk = g_list_next (walk);
1106 if (vaggpad->buffer != NULL) {
1109 GstCapsFeatures *gl_features;
1110 GstVideoInfo gl_info;
1111 GstVideoFrame gl_frame;
1112 GstGLSyncMeta *sync_meta;
1114 gst_video_info_set_format (&gl_info,
1115 GST_VIDEO_FORMAT_RGBA,
1116 GST_VIDEO_INFO_WIDTH (&vaggpad->info),
1117 GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
1119 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
1121 gl_caps = gst_video_info_to_caps (&gl_info);
1122 gst_caps_set_features (gl_caps, 0, gl_features);
1125 GstCaps *in_caps = gst_pad_get_current_caps (GST_PAD (pad));
1127 pad->upload = gst_gl_upload_new (mix->context);
1129 gst_gl_upload_set_caps (pad->upload, in_caps, gl_caps);
1131 gst_caps_unref (in_caps);
1134 sync_meta = gst_buffer_get_gl_sync_meta (vaggpad->buffer);
1136 gst_gl_sync_meta_wait (sync_meta);
1138 if (!gst_gl_upload_perform_with_buffer (pad->upload,
1139 vaggpad->buffer, &gl_buf)) {
1141 pad->mapped = FALSE;
1142 gst_caps_unref (gl_caps);
1146 if (!gst_video_frame_map (&gl_frame, &gl_info, gl_buf,
1147 GST_MAP_READ | GST_MAP_GL)) {
1149 pad->mapped = FALSE;
1150 gst_buffer_unref (gl_buf);
1151 gst_caps_unref (gl_caps);
1156 frame->texture = *(guint *) gl_frame.data[0];
1158 gst_caps_unref (gl_caps);
1159 gst_video_frame_unmap (&gl_frame);
1160 gst_buffer_unref (gl_buf);
1165 g_mutex_lock (&priv->gl_resource_lock);
1166 if (!priv->gl_resource_ready)
1167 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1169 if (!priv->gl_resource_ready) {
1170 g_mutex_unlock (&priv->gl_resource_lock);
1171 GST_ERROR_OBJECT (mix,
1172 "fbo used to render can't be created, do not run process_textures");
1177 mix_class->process_textures (mix, mix->frames, out_tex);
1179 g_mutex_unlock (&priv->gl_resource_lock);
1182 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
1184 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1185 "Failed to download video frame"), (NULL));
1193 walk = GST_ELEMENT (mix)->sinkpads;
1195 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1198 gst_gl_upload_release_buffer (pad->upload);
1200 pad->mapped = FALSE;
1201 walk = g_list_next (walk);
1204 GST_OBJECT_UNLOCK (mix);
1206 gst_video_frame_unmap (&out_frame);
1212 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1215 guint i, array_index = 0;
1216 GstElement *element = GST_ELEMENT (mix);
1217 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1219 GST_OBJECT_LOCK (mix);
1220 walk = GST_ELEMENT (mix)->sinkpads;
1221 i = mix->frames->len;
1222 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1223 for (; i < element->numsinkpads; i++)
1224 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1225 while (walk) { /* We walk with this list because it's ordered */
1226 GstVideoAggregatorPad *vaggpad = walk->data;
1228 walk = g_list_next (walk);
1230 if (vaggpad->buffer != NULL) {
1231 /* put buffer into array */
1232 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1236 GST_OBJECT_UNLOCK (mix);
1238 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1243 static GstFlowReturn
1244 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1246 gboolean res = FALSE;
1247 GstGLMixer *mix = GST_GL_MIXER (vagg);
1248 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1249 GstGLSyncMeta *sync_meta;
1251 if (mix_class->process_buffers)
1252 res = gst_gl_mixer_process_buffers (mix, outbuf);
1253 else if (mix_class->process_textures)
1254 res = gst_gl_mixer_process_textures (mix, outbuf);
1256 sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
1258 gst_gl_sync_meta_set_sync_point (sync_meta, mix->context);
1260 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1264 gst_gl_mixer_get_property (GObject * object,
1265 guint prop_id, GValue * value, GParamSpec * pspec)
1267 GstGLMixer *mixer = GST_GL_MIXER (object);
1271 g_value_set_object (value, mixer->context);
1274 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1280 gst_gl_mixer_set_property (GObject * object,
1281 guint prop_id, const GValue * value, GParamSpec * pspec)
1285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1291 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1293 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1296 gst_object_unref (pad->upload);
1304 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1306 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1310 gst_gl_mixer_start (GstAggregator * agg)
1313 GstGLMixer *mix = GST_GL_MIXER (agg);
1314 GstElement *element = GST_ELEMENT (agg);
1316 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1319 GST_OBJECT_LOCK (mix);
1320 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1321 (GDestroyNotify) _free_glmixer_frame_data);
1322 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1324 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1325 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1327 for (i = 0; i < element->numsinkpads; i++)
1328 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1330 GST_OBJECT_UNLOCK (mix);
1336 gst_gl_mixer_stop (GstAggregator * agg)
1338 GstGLMixer *mix = GST_GL_MIXER (agg);
1339 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1341 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1344 GST_OBJECT_LOCK (agg);
1345 g_ptr_array_free (mix->frames, TRUE);
1347 g_ptr_array_free (mix->array_buffers, TRUE);
1348 mix->array_buffers = NULL;
1349 GST_OBJECT_UNLOCK (agg);
1351 if (mixer_class->reset)
1352 mixer_class->reset (mix);
1354 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1356 mix->depthbuffer = 0;
1358 if (mix->download) {
1359 gst_object_unref (mix->download);
1360 mix->download = NULL;
1363 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1365 if (mix->priv->query) {
1366 gst_query_unref (mix->priv->query);
1367 mix->priv->query = NULL;
1370 if (mix->priv->pool) {
1371 gst_object_unref (mix->priv->pool);
1372 mix->priv->pool = NULL;
1376 gst_object_unref (mix->display);
1377 mix->display = NULL;
1381 gst_object_unref (mix->context);
1382 mix->context = NULL;
1385 gst_gl_mixer_reset (mix);