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);
57 static GstBuffer *_default_pad_upload_buffer (GstGLMixer * mix,
58 GstGLMixerFrameData * frame, GstBuffer * buffer);
60 static void gst_gl_mixer_set_context (GstElement * element,
61 GstContext * context);
68 #define GST_GL_MIXER_GET_PRIVATE(obj) \
69 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
71 struct _GstGLMixerPrivate
77 GstAllocator *allocator;
78 GstAllocationParams params;
81 gboolean gl_resource_ready;
82 GMutex gl_resource_lock;
83 GCond gl_resource_cond;
86 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_VIDEO_AGGREGATOR_PAD);
89 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
91 GObjectClass *gobject_class = (GObjectClass *) klass;
92 GstVideoAggregatorPadClass *vaggpad_class =
93 (GstVideoAggregatorPadClass *) klass;
95 gobject_class->set_property = gst_gl_mixer_pad_set_property;
96 gobject_class->get_property = gst_gl_mixer_pad_get_property;
98 gobject_class->finalize = gst_gl_mixer_pad_finalize;
100 vaggpad_class->set_info = NULL;
101 vaggpad_class->prepare_frame = NULL;
102 vaggpad_class->clean_frame = NULL;
104 klass->upload_buffer = _default_pad_upload_buffer;
108 gst_gl_mixer_pad_finalize (GObject * object)
110 GstGLMixerPad *pad = GST_GL_MIXER_PAD (object);
113 gst_object_unref (pad->upload);
117 G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
121 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec)
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec)
137 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
145 GstGLMixer *mix = GST_GL_MIXER (vagg);
146 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
148 mix->priv->negotiated = ret;
150 gst_caps_replace (&mix->out_caps, caps);
156 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
157 GstQuery * decide_query, GstQuery * query)
159 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
161 GstStructure *config;
165 GError *error = NULL;
166 GstStructure *gl_context;
167 gchar *platform, *gl_apis;
169 GstAllocator *allocator = NULL;
170 GstAllocationParams params;
172 gst_query_parse_allocation (query, &caps, &need_pool);
177 if ((pool = mix->priv->pool))
178 gst_object_ref (pool);
183 /* we had a pool, check caps */
184 GST_DEBUG_OBJECT (mix, "check existing pool caps");
185 config = gst_buffer_pool_get_config (pool);
186 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
188 if (!gst_caps_is_equal (caps, pcaps)) {
189 GST_DEBUG_OBJECT (mix, "pool has different caps");
190 /* different caps, we can't use this pool */
191 gst_object_unref (pool);
194 gst_structure_free (config);
197 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
200 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
203 mix->context = gst_gl_context_new (mix->display);
204 if (!gst_gl_context_create (mix->context, mix->other_context, &error))
208 if (pool == NULL && need_pool) {
211 if (!gst_video_info_from_caps (&info, caps))
214 GST_DEBUG_OBJECT (mix, "create new pool");
215 pool = gst_gl_buffer_pool_new (mix->context);
217 /* the normal size of a frame */
220 config = gst_buffer_pool_get_config (pool);
221 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
222 if (!gst_buffer_pool_set_config (pool, config))
227 gst_query_add_allocation_pool (query, pool, size, 1, 0);
228 gst_object_unref (pool);
231 /* we also support various metadata */
232 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
233 if (mix->context->gl_vtable->FenceSync)
234 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
236 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
238 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
239 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
242 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
243 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
244 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
245 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
246 gst_query_add_allocation_meta (query,
247 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
251 gst_structure_free (gl_context);
253 gst_allocation_params_init (¶ms);
255 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
256 gst_query_add_allocation_param (query, allocator, ¶ms);
257 gst_object_unref (allocator);
264 GST_DEBUG_OBJECT (mix, "no caps specified");
269 GST_DEBUG_OBJECT (mix, "invalid caps specified");
274 GST_DEBUG_OBJECT (mix, "failed setting config");
279 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
286 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
290 GstCaps *template_caps;
292 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
294 template_caps = gst_pad_get_pad_template_caps (pad);
295 template_caps = gst_caps_make_writable (template_caps);
297 ret = gst_caps_can_intersect (caps, template_caps);
298 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
299 (ret ? "" : "not "), caps);
300 gst_caps_unref (template_caps);
306 gst_gl_mixer_set_caps_features (const GstCaps * caps,
307 const gchar * feature_name)
309 GstCaps *tmp = gst_caps_copy (caps);
310 guint n = gst_caps_get_size (tmp);
313 for (i = 0; i < n; i++) {
314 GstCapsFeatures *features = gst_caps_get_features (tmp, i);
316 guint n_f = gst_caps_features_get_size (features);
318 for (j = 0; j < n_f; j++) {
319 gst_caps_features_remove_id (features,
320 gst_caps_features_get_nth_id (features, j));
324 gst_caps_features_add (features, feature_name);
325 gst_caps_set_simple (tmp, "format", G_TYPE_STRING, "RGBA", NULL);
331 /* copies the given caps */
333 gst_gl_mixer_caps_remove_format_info (GstCaps * caps)
340 res = gst_caps_new_empty ();
342 n = gst_caps_get_size (caps);
343 for (i = 0; i < n; i++) {
344 st = gst_caps_get_structure (caps, i);
345 f = gst_caps_get_features (caps, i);
347 /* If this is already expressed by the existing caps
348 * skip this structure */
349 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
352 st = gst_structure_copy (st);
353 /* Only remove format info for the cases when we can actually convert */
354 if (!gst_caps_features_is_any (f)
355 && gst_caps_features_is_equal (f,
356 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
357 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
359 gst_structure_remove_fields (st, "width", "height", NULL);
361 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
368 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
370 GstCaps *result = NULL;
371 GstCaps *glcaps = gst_gl_mixer_set_caps_features (caps,
372 GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
373 #if GST_GL_HAVE_PLATFORM_EGL
374 GstCaps *eglcaps = gst_gl_mixer_set_caps_features (caps,
375 GST_CAPS_FEATURE_MEMORY_EGL_IMAGE);
377 GstCaps *uploadcaps = gst_gl_mixer_set_caps_features (caps,
378 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META);
380 gst_caps_from_string (GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS));
382 result = gst_caps_new_empty ();
384 result = gst_caps_merge (result, glcaps);
385 #if GST_GL_HAVE_PLATFORM_EGL
386 result = gst_caps_merge (result, eglcaps);
388 result = gst_caps_merge (result, uploadcaps);
389 result = gst_caps_merge (result, raw_caps);
391 result = gst_caps_merge (result, gst_gl_mixer_caps_remove_format_info (caps));
393 GST_DEBUG_OBJECT (mix, "returning %" GST_PTR_FORMAT, result);
399 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
402 GstCaps *template_caps;
403 GstCaps *filtered_caps;
404 GstCaps *returned_caps;
405 gboolean had_current_caps = TRUE;
407 template_caps = gst_pad_get_pad_template_caps (pad);
409 srccaps = gst_pad_get_current_caps (pad);
410 if (srccaps == NULL) {
411 had_current_caps = FALSE;
412 srccaps = template_caps;
414 srccaps = gst_caps_merge (srccaps, gst_gl_mixer_update_caps (mix, srccaps));
417 filtered_caps = srccaps;
419 filtered_caps = gst_caps_intersect (srccaps, filter);
420 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
423 gst_caps_unref (filtered_caps);
424 if (had_current_caps)
425 gst_caps_unref (template_caps);
427 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
429 return returned_caps;
433 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
436 gboolean ret = FALSE;
437 GstGLMixer *mix = GST_GL_MIXER (agg);
438 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
440 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
442 switch (GST_QUERY_TYPE (query)) {
445 GstCaps *filter, *caps;
447 gst_query_parse_caps (query, &filter);
448 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
449 gst_query_set_caps_result (query, caps);
450 gst_caps_unref (caps);
454 case GST_QUERY_ACCEPT_CAPS:
458 gst_query_parse_accept_caps (query, &caps);
459 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
460 gst_query_set_accept_caps_result (query, ret);
464 case GST_QUERY_ALLOCATION:
466 GstQuery *decide_query = NULL;
468 GST_OBJECT_LOCK (mix);
469 if (G_UNLIKELY (!mix->priv->negotiated)) {
470 GST_DEBUG_OBJECT (mix,
471 "not negotiated yet, can't answer ALLOCATION query");
472 GST_OBJECT_UNLOCK (mix);
475 if ((decide_query = mix->priv->query))
476 gst_query_ref (decide_query);
477 GST_OBJECT_UNLOCK (mix);
479 GST_DEBUG_OBJECT (mix,
480 "calling propose allocation with query %" GST_PTR_FORMAT,
483 /* pass the query to the propose_allocation vmethod if any */
484 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
487 gst_query_unref (decide_query);
489 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
492 case GST_QUERY_CONTEXT:
494 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
495 &mix->display, &mix->other_context);
497 gst_gl_display_filter_gl_api (mix->display,
498 mix_class->supported_gl_api);
502 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
510 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
514 /* GLMixer signals and args */
527 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
530 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
531 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
533 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
534 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
536 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
539 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
542 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
543 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
545 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
546 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
548 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
551 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
553 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
554 GstBuffer ** outbuf);
556 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
558 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
559 static gboolean gst_gl_mixer_start (GstAggregator * agg);
562 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
563 GstBuffer * outbuffer);
565 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
566 const GValue * value, GParamSpec * pspec);
567 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
568 GValue * value, GParamSpec * pspec);
570 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
572 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
573 GstBufferPool * pool, GstAllocator * allocator,
574 GstAllocationParams * params, GstQuery * query);
576 static void gst_gl_mixer_finalize (GObject * object);
579 gst_gl_mixer_class_init (GstGLMixerClass * klass)
581 GObjectClass *gobject_class;
582 GstElementClass *element_class;
584 GstVideoAggregatorClass *videoaggregator_class =
585 (GstVideoAggregatorClass *) klass;
586 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
588 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
590 gobject_class = (GObjectClass *) klass;
591 element_class = GST_ELEMENT_CLASS (klass);
593 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
595 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
597 gobject_class->get_property = gst_gl_mixer_get_property;
598 gobject_class->set_property = gst_gl_mixer_set_property;
600 gst_element_class_add_pad_template (element_class,
601 gst_static_pad_template_get (&src_factory));
602 gst_element_class_add_pad_template (element_class,
603 gst_static_pad_template_get (&sink_factory));
605 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
607 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
608 agg_class->sink_query = gst_gl_mixer_sink_query;
609 agg_class->src_query = gst_gl_mixer_src_query;
610 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
611 agg_class->stop = gst_gl_mixer_stop;
612 agg_class->start = gst_gl_mixer_start;
614 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
615 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
616 videoaggregator_class->negotiated_caps = _negotiated_caps;
617 videoaggregator_class->find_best_format = NULL;
619 g_object_class_install_property (gobject_class, PROP_CONTEXT,
620 g_param_spec_object ("context",
622 "Get OpenGL context",
623 GST_GL_TYPE_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
625 /* Register the pad class */
626 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
628 klass->set_caps = NULL;
629 klass->supported_gl_api = GST_GL_API_ANY;
633 gst_gl_mixer_reset (GstGLMixer * mix)
635 /* clean up collect data */
636 mix->priv->negotiated = FALSE;
640 gst_gl_mixer_init (GstGLMixer * mix)
642 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
643 mix->array_buffers = 0;
646 mix->depthbuffer = 0;
648 mix->priv->gl_resource_ready = FALSE;
649 g_mutex_init (&mix->priv->gl_resource_lock);
650 g_cond_init (&mix->priv->gl_resource_cond);
651 /* initialize variables */
652 gst_gl_mixer_reset (mix);
656 gst_gl_mixer_finalize (GObject * object)
658 GstGLMixer *mix = GST_GL_MIXER (object);
659 GstGLMixerPrivate *priv = mix->priv;
661 if (mix->other_context) {
662 gst_object_unref (mix->other_context);
663 mix->other_context = NULL;
666 g_mutex_clear (&priv->gl_resource_lock);
667 g_cond_clear (&priv->gl_resource_cond);
668 G_OBJECT_CLASS (parent_class)->finalize (object);
672 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
674 GstGLMixer *mix = GST_GL_MIXER (element);
675 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
677 gst_gl_handle_set_context (element, context, &mix->display,
678 &mix->other_context);
681 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
685 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
687 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
688 gboolean result = TRUE;
691 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
694 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
701 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
705 gboolean result = FALSE;
707 mix = GST_GL_MIXER (aggregator);
710 case GST_PAD_MODE_PUSH:
711 case GST_PAD_MODE_PULL:
712 result = gst_gl_mixer_activate (mix, active);
722 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
724 GstCaps *filter, *current_caps, *retcaps;
726 gst_query_parse_caps (query, &filter);
728 current_caps = gst_pad_get_current_caps (pad);
729 if (current_caps == NULL)
730 current_caps = gst_pad_get_pad_template_caps (agg->srcpad);
732 retcaps = gst_gl_mixer_caps_remove_format_info (current_caps);
733 gst_caps_unref (current_caps);
737 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
739 gst_query_set_caps_result (query, retcaps);
740 gst_caps_unref (retcaps);
746 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
748 gboolean res = FALSE;
749 GstGLMixer *mix = GST_GL_MIXER (agg);
750 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
752 switch (GST_QUERY_TYPE (query)) {
753 case GST_QUERY_CONTEXT:
755 res = gst_gl_handle_context_query ((GstElement *) mix, query,
756 &mix->display, &mix->other_context);
758 gst_gl_display_filter_gl_api (mix->display,
759 mix_class->supported_gl_api);
763 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
766 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
774 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
777 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
779 if (!mix->priv->pool_active) {
780 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
781 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
782 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
783 return GST_FLOW_ERROR;
785 mix->priv->pool_active = TRUE;
788 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
792 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
794 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
795 GstBufferPool *pool = NULL;
796 GstStructure *config;
798 guint min, max, size;
799 gboolean update_pool;
800 GError *error = NULL;
802 guint out_width, out_height;
803 GstGLContext *other_context = NULL;
804 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
805 gboolean same_downstream_gl_context = FALSE;
807 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
810 gst_gl_display_filter_gl_api (mix->display, mixer_class->supported_gl_api);
812 if (gst_query_find_allocation_meta (query,
813 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
814 GstGLContext *context;
815 const GstStructure *upload_meta_params;
820 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
821 if (upload_meta_params) {
822 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
823 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
824 GstGLContext *old = mix->context;
826 mix->context = context;
828 gst_object_unref (old);
829 same_downstream_gl_context = TRUE;
830 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
831 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
832 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
834 GstGLPlatform platform;
837 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
840 platform = gst_gl_platform_from_string (type);
841 gl_apis = gst_gl_api_from_string (apis);
843 if (gl_apis && platform)
845 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
851 if (mix->other_context) {
852 if (!other_context) {
853 other_context = mix->other_context;
855 GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
856 ("%s", "Cannot share with more than one GL context"),
857 ("%s", "Cannot share with more than one GL context"));
862 mix->context = gst_gl_context_new (mix->display);
863 if (!gst_gl_context_create (mix->context, other_context, &error))
867 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
868 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
870 g_mutex_lock (&mix->priv->gl_resource_lock);
871 mix->priv->gl_resource_ready = FALSE;
873 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
875 mix->depthbuffer = 0;
878 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
879 &mix->fbo, &mix->depthbuffer)) {
880 g_cond_signal (&mix->priv->gl_resource_cond);
881 g_mutex_unlock (&mix->priv->gl_resource_lock);
886 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
887 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
888 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
890 gst_query_parse_allocation (query, &caps, NULL);
892 if (mixer_class->set_caps)
893 mixer_class->set_caps (mix, caps);
895 mix->priv->gl_resource_ready = TRUE;
896 g_cond_signal (&mix->priv->gl_resource_cond);
897 g_mutex_unlock (&mix->priv->gl_resource_lock);
899 if (gst_query_get_n_allocation_pools (query) > 0) {
900 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
906 gst_video_info_init (&vinfo);
907 gst_video_info_from_caps (&vinfo, caps);
913 if (!pool || (!same_downstream_gl_context
914 && gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE,
916 && !gst_buffer_pool_has_option (pool,
917 GST_BUFFER_POOL_OPTION_GL_SYNC_META))) {
918 /* can't use this pool */
920 gst_object_unref (pool);
921 pool = gst_gl_buffer_pool_new (mix->context);
923 config = gst_buffer_pool_get_config (pool);
925 gst_buffer_pool_config_set_params (config, caps, size, min, max);
926 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
927 if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
928 gst_buffer_pool_config_add_option (config,
929 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
930 gst_buffer_pool_config_add_option (config,
931 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
933 gst_buffer_pool_set_config (pool, config);
936 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
938 gst_query_add_allocation_pool (query, pool, size, min, max);
940 gst_object_unref (pool);
946 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
952 /* takes ownership of the pool, allocator and query */
954 gst_gl_mixer_set_allocation (GstGLMixer * mix,
955 GstBufferPool * pool, GstAllocator * allocator,
956 GstAllocationParams * params, GstQuery * query)
958 GstAllocator *oldalloc;
959 GstBufferPool *oldpool;
961 GstGLMixerPrivate *priv = mix->priv;
963 GST_DEBUG ("storing allocation query");
965 GST_OBJECT_LOCK (mix);
966 oldpool = priv->pool;
968 priv->pool_active = FALSE;
970 oldalloc = priv->allocator;
971 priv->allocator = allocator;
973 oldquery = priv->query;
977 priv->params = *params;
979 gst_allocation_params_init (&priv->params);
980 GST_OBJECT_UNLOCK (mix);
983 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
984 gst_buffer_pool_set_active (oldpool, FALSE);
985 gst_object_unref (oldpool);
988 gst_object_unref (oldalloc);
991 gst_query_unref (oldquery);
997 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
1000 gboolean result = TRUE;
1001 GstBufferPool *pool = NULL;
1002 GstAllocator *allocator;
1003 GstAllocationParams params;
1004 GstAggregator *agg = GST_AGGREGATOR (mix);
1006 /* find a pool for the negotiated caps now */
1007 GST_DEBUG_OBJECT (mix, "doing allocation query");
1008 query = gst_query_new_allocation (outcaps, TRUE);
1009 if (!gst_pad_peer_query (agg->srcpad, query)) {
1010 /* not a problem, just debug a little */
1011 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
1014 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
1015 result = gst_gl_mixer_decide_allocation (mix, query);
1017 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
1021 goto no_decide_allocation;
1023 /* we got configuration from our peer or the decide_allocation method,
1025 if (gst_query_get_n_allocation_params (query) > 0) {
1026 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1029 gst_allocation_params_init (¶ms);
1032 if (gst_query_get_n_allocation_pools (query) > 0)
1033 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
1036 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
1041 no_decide_allocation:
1043 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
1044 gst_query_unref (query);
1051 _default_pad_upload_buffer (GstGLMixer * mix, GstGLMixerFrameData * frame,
1054 GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (frame->pad);
1055 GstGLMixerPad *pad = frame->pad;
1056 GstBuffer *uploaded_buf, *gl_buffer;
1058 GstCapsFeatures *gl_features;
1059 GstVideoInfo gl_info;
1060 GstVideoFrame gl_frame;
1061 GstGLSyncMeta *sync_meta;
1063 gst_video_info_set_format (&gl_info,
1064 GST_VIDEO_FORMAT_RGBA,
1065 GST_VIDEO_INFO_WIDTH (&vaggpad->info),
1066 GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
1068 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
1070 gl_caps = gst_video_info_to_caps (&gl_info);
1071 gst_caps_set_features (gl_caps, 0, gst_caps_features_copy (gl_features));
1074 GstCaps *in_caps = gst_pad_get_current_caps (GST_PAD (pad));
1075 GstCaps *upload_caps = gst_caps_copy (in_caps);
1077 pad->upload = gst_gl_upload_new (mix->context);
1079 gst_caps_set_features (upload_caps, 0,
1080 gst_caps_features_copy (gl_features));
1081 gst_gl_upload_set_caps (pad->upload, in_caps, upload_caps);
1083 if (!pad->convert) {
1084 pad->convert = gst_gl_color_convert_new (mix->context);
1086 gst_gl_color_convert_set_caps (pad->convert, upload_caps, gl_caps);
1089 gst_caps_unref (upload_caps);
1090 gst_caps_unref (in_caps);
1093 gst_caps_features_free (gl_features);
1094 gst_caps_unref (gl_caps);
1096 sync_meta = gst_buffer_get_gl_sync_meta (vaggpad->buffer);
1098 gst_gl_sync_meta_wait (sync_meta);
1100 if (gst_gl_upload_perform_with_buffer (pad->upload,
1101 vaggpad->buffer, &uploaded_buf) != GST_GL_UPLOAD_DONE) {
1105 if (!(gl_buffer = gst_gl_color_convert_perform (pad->convert, uploaded_buf))) {
1106 gst_buffer_unref (uploaded_buf);
1110 if (!gst_video_frame_map (&gl_frame, &gl_info, gl_buffer,
1111 GST_MAP_READ | GST_MAP_GL)) {
1112 gst_buffer_unref (uploaded_buf);
1113 gst_buffer_unref (gl_buffer);
1117 frame->texture = *(guint *) gl_frame.data[0];
1119 gst_buffer_unref (uploaded_buf);
1120 gst_video_frame_unmap (&gl_frame);
1126 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
1130 guint out_tex, out_tex_target;
1131 gboolean res = TRUE;
1132 guint array_index = 0;
1133 GstVideoFrame out_frame;
1134 GstElement *element = GST_ELEMENT (mix);
1135 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
1136 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1137 GstGLMixerPrivate *priv = mix->priv;
1138 gboolean to_download =
1139 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
1140 gst_caps_get_features (mix->out_caps, 0));
1141 GstMapFlags out_map_flags = GST_MAP_WRITE;
1143 GST_TRACE ("Processing buffers");
1145 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
1148 out_map_flags |= GST_MAP_GL;
1150 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
1155 out_tex = *(guint *) out_frame.data[0];
1157 ((GstGLMemory *) gst_buffer_peek_memory (outbuf, 0))->tex_target;
1159 GST_INFO ("Output Buffer does not contain correct memory, "
1160 "attempting to wrap for download");
1163 mix->download = gst_gl_download_new (mix->context);
1165 gst_gl_download_set_format (mix->download, &out_frame.info);
1166 out_tex = mix->out_tex_id;
1167 out_tex_target = GL_TEXTURE_2D;
1170 GST_OBJECT_LOCK (mix);
1171 walk = element->sinkpads;
1173 i = mix->frames->len;
1174 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1175 for (; i < element->numsinkpads; i++)
1176 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1178 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1179 GstGLMixerPadClass *pad_class = GST_GL_MIXER_PAD_GET_CLASS (pad);
1180 GstVideoAggregatorPad *vaggpad = walk->data;
1181 GstGLMixerFrameData *frame;
1183 frame = g_ptr_array_index (mix->frames, array_index);
1187 walk = g_list_next (walk);
1189 if (vaggpad->buffer != NULL) {
1190 g_assert (pad_class->upload_buffer);
1193 gst_buffer_unref (pad->gl_buffer);
1194 pad->gl_buffer = pad_class->upload_buffer (mix, frame, vaggpad->buffer);
1196 GST_DEBUG_OBJECT (pad,
1197 "uploaded buffer %" GST_PTR_FORMAT " from buffer %" GST_PTR_FORMAT,
1198 pad->gl_buffer, vaggpad->buffer);
1204 g_mutex_lock (&priv->gl_resource_lock);
1205 if (!priv->gl_resource_ready)
1206 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1208 if (!priv->gl_resource_ready) {
1209 g_mutex_unlock (&priv->gl_resource_lock);
1210 GST_ERROR_OBJECT (mix,
1211 "fbo used to render can't be created, do not run process_textures");
1216 mix_class->process_textures (mix, mix->frames, out_tex);
1218 g_mutex_unlock (&priv->gl_resource_lock);
1221 if (!gst_gl_download_perform_with_data (mix->download,
1222 out_tex, out_tex_target, out_frame.data)) {
1223 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1224 "Failed to download video frame"), (NULL));
1232 walk = GST_ELEMENT (mix)->sinkpads;
1234 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1237 gst_gl_upload_release_buffer (pad->upload);
1239 walk = g_list_next (walk);
1242 GST_OBJECT_UNLOCK (mix);
1244 gst_video_frame_unmap (&out_frame);
1250 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1253 guint i, array_index = 0;
1254 GstElement *element = GST_ELEMENT (mix);
1255 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1257 GST_OBJECT_LOCK (mix);
1258 walk = GST_ELEMENT (mix)->sinkpads;
1259 i = mix->frames->len;
1260 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1261 for (; i < element->numsinkpads; i++)
1262 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1263 while (walk) { /* We walk with this list because it's ordered */
1264 GstVideoAggregatorPad *vaggpad = walk->data;
1266 walk = g_list_next (walk);
1268 if (vaggpad->buffer != NULL) {
1269 /* put buffer into array */
1270 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1274 GST_OBJECT_UNLOCK (mix);
1276 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1281 static GstFlowReturn
1282 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1284 gboolean res = FALSE;
1285 GstGLMixer *mix = GST_GL_MIXER (vagg);
1286 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1287 GstGLSyncMeta *sync_meta;
1289 if (mix_class->process_buffers)
1290 res = gst_gl_mixer_process_buffers (mix, outbuf);
1291 else if (mix_class->process_textures)
1292 res = gst_gl_mixer_process_textures (mix, outbuf);
1294 sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
1296 gst_gl_sync_meta_set_sync_point (sync_meta, mix->context);
1298 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1302 gst_gl_mixer_get_property (GObject * object,
1303 guint prop_id, GValue * value, GParamSpec * pspec)
1305 GstGLMixer *mixer = GST_GL_MIXER (object);
1309 g_value_set_object (value, mixer->context);
1312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1318 gst_gl_mixer_set_property (GObject * object,
1319 guint prop_id, const GValue * value, GParamSpec * pspec)
1323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1329 _clean_upload (GstAggregator * agg, GstAggregatorPad * aggpad, gpointer udata)
1331 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1333 if (pad->gl_buffer) {
1334 gst_buffer_unref (pad->gl_buffer);
1335 pad->gl_buffer = NULL;
1339 gst_object_unref (pad->upload);
1344 gst_object_unref (pad->convert);
1345 pad->convert = NULL;
1352 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1354 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1358 gst_gl_mixer_start (GstAggregator * agg)
1361 GstGLMixer *mix = GST_GL_MIXER (agg);
1362 GstElement *element = GST_ELEMENT (agg);
1364 GST_OBJECT_LOCK (mix);
1365 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1366 (GDestroyNotify) _free_glmixer_frame_data);
1367 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1369 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1370 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1372 for (i = 0; i < element->numsinkpads; i++)
1373 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1375 GST_OBJECT_UNLOCK (mix);
1381 gst_gl_mixer_stop (GstAggregator * agg)
1383 GstGLMixer *mix = GST_GL_MIXER (agg);
1384 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1386 GST_OBJECT_LOCK (agg);
1387 g_ptr_array_free (mix->frames, TRUE);
1389 g_ptr_array_free (mix->array_buffers, TRUE);
1390 mix->array_buffers = NULL;
1391 GST_OBJECT_UNLOCK (agg);
1393 if (mixer_class->reset)
1394 mixer_class->reset (mix);
1396 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1398 mix->depthbuffer = 0;
1400 if (mix->download) {
1401 gst_object_unref (mix->download);
1402 mix->download = NULL;
1405 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1407 if (mix->priv->query) {
1408 gst_query_unref (mix->priv->query);
1409 mix->priv->query = NULL;
1412 if (mix->priv->pool) {
1413 gst_object_unref (mix->priv->pool);
1414 mix->priv->pool = NULL;
1418 gst_object_unref (mix->display);
1419 mix->display = NULL;
1423 gst_object_unref (mix->context);
1424 mix->context = NULL;
1427 gst_gl_mixer_reset (mix);