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 _init_upload (GstGLMixer * mix, GstGLMixerPad * pad)
123 GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (pad);
126 GstCaps *in_caps = gst_pad_get_current_caps (GST_PAD (pad));
127 GstCaps *upload_caps = gst_caps_copy (in_caps);
128 GstCapsFeatures *gl_features =
129 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
131 pad->upload = gst_gl_upload_new (mix->context);
133 gst_caps_set_features (upload_caps, 0,
134 gst_caps_features_copy (gl_features));
135 gst_gl_upload_set_caps (pad->upload, in_caps, upload_caps);
136 gst_caps_unref (in_caps);
139 GstVideoInfo gl_info;
142 gst_video_info_set_format (&gl_info,
143 GST_VIDEO_FORMAT_RGBA,
144 GST_VIDEO_INFO_WIDTH (&vaggpad->info),
145 GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
146 gl_caps = gst_video_info_to_caps (&gl_info);
147 gst_caps_set_features (gl_caps, 0, gst_caps_features_copy (gl_features));
149 pad->convert = gst_gl_color_convert_new (mix->context);
151 gst_gl_color_convert_set_caps (pad->convert, upload_caps, gl_caps);
152 gst_caps_unref (gl_caps);
155 gst_caps_unref (upload_caps);
156 gst_caps_features_free (gl_features);
161 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
162 GValue * value, GParamSpec * pspec)
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
173 const GValue * value, GParamSpec * pspec)
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
185 GstGLMixer *mix = GST_GL_MIXER (vagg);
186 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
188 mix->priv->negotiated = ret;
190 gst_caps_replace (&mix->out_caps, caps);
196 gst_gl_mixer_propose_allocation (GstGLMixer * mix, GstGLMixerPad * pad,
197 GstQuery * decide_query, GstQuery * query)
199 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
201 GstStructure *config;
205 GError *error = NULL;
207 gst_query_parse_allocation (query, &caps, &need_pool);
212 if ((pool = mix->priv->pool))
213 gst_object_ref (pool);
218 /* we had a pool, check caps */
219 GST_DEBUG_OBJECT (mix, "check existing pool caps");
220 config = gst_buffer_pool_get_config (pool);
221 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
223 if (!gst_caps_is_equal (caps, pcaps)) {
224 GST_DEBUG_OBJECT (mix, "pool has different caps");
225 /* different caps, we can't use this pool */
226 gst_object_unref (pool);
229 gst_structure_free (config);
232 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
235 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
238 mix->context = gst_gl_context_new (mix->display);
239 if (!gst_gl_context_create (mix->context, mix->other_context, &error))
243 if (pool == NULL && need_pool) {
246 if (!gst_video_info_from_caps (&info, caps))
249 GST_DEBUG_OBJECT (mix, "create new pool");
250 pool = gst_gl_buffer_pool_new (mix->context);
252 /* the normal size of a frame */
255 config = gst_buffer_pool_get_config (pool);
256 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
257 if (!gst_buffer_pool_set_config (pool, config))
262 gst_query_add_allocation_pool (query, pool, size, 1, 0);
263 gst_object_unref (pool);
266 /* we also support various metadata */
267 if (mix->context->gl_vtable->FenceSync)
268 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
270 _init_upload (mix, pad);
272 gst_gl_upload_propose_allocation (pad->upload, decide_query, query);
279 GST_DEBUG_OBJECT (mix, "no caps specified");
284 GST_DEBUG_OBJECT (mix, "invalid caps specified");
289 GST_DEBUG_OBJECT (mix, "failed setting config");
294 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
301 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
305 GstCaps *template_caps;
307 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
309 template_caps = gst_pad_get_pad_template_caps (pad);
310 template_caps = gst_caps_make_writable (template_caps);
312 ret = gst_caps_can_intersect (caps, template_caps);
313 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
314 (ret ? "" : "not "), caps);
315 gst_caps_unref (template_caps);
321 gst_gl_mixer_set_caps_features (const GstCaps * caps,
322 const gchar * feature_name)
324 GstCaps *ret = gst_gl_caps_replace_all_caps_features (caps, feature_name);
325 gst_caps_set_simple (ret, "format", G_TYPE_STRING, "RGBA", NULL);
329 /* copies the given caps */
331 gst_gl_mixer_caps_remove_format_info (GstCaps * caps)
338 res = gst_caps_new_empty ();
340 n = gst_caps_get_size (caps);
341 for (i = 0; i < n; i++) {
342 st = gst_caps_get_structure (caps, i);
343 f = gst_caps_get_features (caps, i);
345 /* If this is already expressed by the existing caps
346 * skip this structure */
347 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
350 st = gst_structure_copy (st);
351 /* Only remove format info for the cases when we can actually convert */
352 if (!gst_caps_features_is_any (f)
353 && gst_caps_features_is_equal (f,
354 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
355 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
357 gst_structure_remove_fields (st, "width", "height", NULL);
359 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
366 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
368 GstCaps *result, *tmp, *gl_caps;
370 gl_caps = gst_caps_from_string ("video/x-raw(memory:GLMemory),format=RGBA");
373 gst_gl_color_convert_transform_caps (mix->context, GST_PAD_SRC, gl_caps,
376 GST_DEBUG_OBJECT (mix, "convert returned caps %" GST_PTR_FORMAT, tmp);
378 result = gst_gl_mixer_caps_remove_format_info (tmp);
379 gst_caps_unref (tmp);
381 GST_DEBUG_OBJECT (mix, "remove format returned caps %" GST_PTR_FORMAT, tmp);
383 result = gst_gl_upload_transform_caps (mix->context, GST_PAD_SRC, tmp, NULL);
384 gst_caps_unref (tmp);
386 GST_DEBUG_OBJECT (mix, "transfer returned caps %" GST_PTR_FORMAT, tmp);
392 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
395 GstCaps *template_caps;
396 GstCaps *filtered_caps;
397 GstCaps *returned_caps;
398 gboolean had_current_caps = TRUE;
400 template_caps = gst_pad_get_pad_template_caps (pad);
402 sinkcaps = gst_pad_get_current_caps (pad);
403 if (sinkcaps == NULL) {
404 had_current_caps = FALSE;
405 sinkcaps = template_caps;
408 gst_caps_merge (sinkcaps, gst_gl_mixer_update_caps (mix, sinkcaps));
411 filtered_caps = sinkcaps;
413 filtered_caps = gst_caps_intersect (sinkcaps, filter);
414 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
417 gst_caps_unref (filtered_caps);
418 if (had_current_caps)
419 gst_caps_unref (template_caps);
421 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
423 return returned_caps;
427 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
430 gboolean ret = FALSE;
431 GstGLMixer *mix = GST_GL_MIXER (agg);
432 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
433 GstGLMixerPad *pad = GST_GL_MIXER_PAD (bpad);
435 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
437 switch (GST_QUERY_TYPE (query)) {
440 GstCaps *filter, *caps;
442 gst_query_parse_caps (query, &filter);
443 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
444 gst_query_set_caps_result (query, caps);
445 gst_caps_unref (caps);
449 case GST_QUERY_ACCEPT_CAPS:
453 gst_query_parse_accept_caps (query, &caps);
454 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
455 gst_query_set_accept_caps_result (query, ret);
459 case GST_QUERY_ALLOCATION:
461 GstQuery *decide_query = NULL;
463 GST_OBJECT_LOCK (mix);
464 if (G_UNLIKELY (!mix->priv->negotiated)) {
465 GST_DEBUG_OBJECT (mix,
466 "not negotiated yet, can't answer ALLOCATION query");
467 GST_OBJECT_UNLOCK (mix);
470 if ((decide_query = mix->priv->query))
471 gst_query_ref (decide_query);
472 GST_OBJECT_UNLOCK (mix);
474 GST_DEBUG_OBJECT (mix,
475 "calling propose allocation with query %" GST_PTR_FORMAT,
478 /* pass the query to the propose_allocation vmethod if any */
479 ret = gst_gl_mixer_propose_allocation (mix, pad, decide_query, query);
482 gst_query_unref (decide_query);
484 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
487 case GST_QUERY_CONTEXT:
489 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
490 &mix->display, &mix->other_context);
492 gst_gl_display_filter_gl_api (mix->display,
493 mix_class->supported_gl_api);
497 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
505 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
509 /* GLMixer signals and args */
522 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
525 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
526 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
528 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
529 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
531 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
534 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
537 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
538 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
540 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
541 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
543 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
546 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
548 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
549 GstBuffer ** outbuf);
551 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
553 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
554 static gboolean gst_gl_mixer_start (GstAggregator * agg);
557 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
558 GstBuffer * outbuffer);
560 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
561 const GValue * value, GParamSpec * pspec);
562 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
563 GValue * value, GParamSpec * pspec);
565 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
567 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
568 GstBufferPool * pool, GstAllocator * allocator,
569 GstAllocationParams * params, GstQuery * query);
571 static void gst_gl_mixer_finalize (GObject * object);
574 gst_gl_mixer_class_init (GstGLMixerClass * klass)
576 GObjectClass *gobject_class;
577 GstElementClass *element_class;
579 GstVideoAggregatorClass *videoaggregator_class =
580 (GstVideoAggregatorClass *) klass;
581 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
583 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
585 gobject_class = (GObjectClass *) klass;
586 element_class = GST_ELEMENT_CLASS (klass);
588 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
590 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
592 gobject_class->get_property = gst_gl_mixer_get_property;
593 gobject_class->set_property = gst_gl_mixer_set_property;
595 gst_element_class_add_pad_template (element_class,
596 gst_static_pad_template_get (&src_factory));
597 gst_element_class_add_pad_template (element_class,
598 gst_static_pad_template_get (&sink_factory));
600 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
602 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
603 agg_class->sink_query = gst_gl_mixer_sink_query;
604 agg_class->src_query = gst_gl_mixer_src_query;
605 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
606 agg_class->stop = gst_gl_mixer_stop;
607 agg_class->start = gst_gl_mixer_start;
609 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
610 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
611 videoaggregator_class->negotiated_caps = _negotiated_caps;
612 videoaggregator_class->find_best_format = NULL;
614 g_object_class_install_property (gobject_class, PROP_CONTEXT,
615 g_param_spec_object ("context",
617 "Get OpenGL context",
618 GST_GL_TYPE_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
620 /* Register the pad class */
621 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
623 klass->set_caps = NULL;
624 klass->supported_gl_api = GST_GL_API_ANY;
628 gst_gl_mixer_reset (GstGLMixer * mix)
630 /* clean up collect data */
631 mix->priv->negotiated = FALSE;
635 gst_gl_mixer_init (GstGLMixer * mix)
637 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
638 mix->array_buffers = 0;
641 mix->depthbuffer = 0;
643 mix->priv->gl_resource_ready = FALSE;
644 g_mutex_init (&mix->priv->gl_resource_lock);
645 g_cond_init (&mix->priv->gl_resource_cond);
646 /* initialize variables */
647 gst_gl_mixer_reset (mix);
651 gst_gl_mixer_finalize (GObject * object)
653 GstGLMixer *mix = GST_GL_MIXER (object);
654 GstGLMixerPrivate *priv = mix->priv;
656 if (mix->other_context) {
657 gst_object_unref (mix->other_context);
658 mix->other_context = NULL;
661 g_mutex_clear (&priv->gl_resource_lock);
662 g_cond_clear (&priv->gl_resource_cond);
663 G_OBJECT_CLASS (parent_class)->finalize (object);
667 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
669 GstGLMixer *mix = GST_GL_MIXER (element);
670 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
672 gst_gl_handle_set_context (element, context, &mix->display,
673 &mix->other_context);
676 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
680 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
682 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
683 gboolean result = TRUE;
686 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
689 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
696 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
700 gboolean result = FALSE;
702 mix = GST_GL_MIXER (aggregator);
705 case GST_PAD_MODE_PUSH:
706 case GST_PAD_MODE_PULL:
707 result = gst_gl_mixer_activate (mix, active);
717 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
719 GstGLMixer *mix = GST_GL_MIXER (agg);
720 GstCaps *filter, *current_caps, *retcaps, *gl_caps;
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 /* convert from current caps to GLMemory caps */
730 gst_caps_merge (gst_caps_merge (gst_gl_mixer_set_caps_features
731 (current_caps, GST_CAPS_FEATURE_MEMORY_GL_MEMORY),
732 gst_gl_mixer_set_caps_features (current_caps,
733 GST_CAPS_FEATURE_MEMORY_EGL_IMAGE)),
734 gst_gl_mixer_set_caps_features (current_caps,
735 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META));
737 gst_gl_download_transform_caps (mix->context, GST_PAD_SINK, current_caps,
739 retcaps = gst_caps_merge (gl_caps, retcaps);
740 gst_caps_unref (current_caps);
741 current_caps = retcaps;
743 retcaps = gst_gl_mixer_caps_remove_format_info (current_caps);
744 gst_caps_unref (current_caps);
748 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
749 gst_caps_unref (retcaps);
750 retcaps = current_caps;
753 gst_query_set_caps_result (query, retcaps);
754 gst_caps_unref (retcaps);
760 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
762 gboolean res = FALSE;
763 GstGLMixer *mix = GST_GL_MIXER (agg);
764 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
766 switch (GST_QUERY_TYPE (query)) {
767 case GST_QUERY_CONTEXT:
769 res = gst_gl_handle_context_query ((GstElement *) mix, query,
770 &mix->display, &mix->other_context);
772 gst_gl_display_filter_gl_api (mix->display,
773 mix_class->supported_gl_api);
777 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
780 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
788 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
791 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
793 if (!mix->priv->pool_active) {
794 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
795 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
796 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
797 return GST_FLOW_ERROR;
799 mix->priv->pool_active = TRUE;
802 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
806 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
808 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
809 GstBufferPool *pool = NULL;
810 GstStructure *config;
812 guint min, max, size;
813 gboolean update_pool;
814 GError *error = NULL;
816 guint out_width, out_height;
817 GstGLContext *other_context = NULL;
818 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
819 gboolean same_downstream_gl_context = FALSE;
821 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
824 gst_gl_display_filter_gl_api (mix->display, mixer_class->supported_gl_api);
826 if (gst_query_find_allocation_meta (query,
827 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
828 GstGLContext *context;
829 const GstStructure *upload_meta_params;
834 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
835 if (upload_meta_params) {
836 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
837 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
838 GstGLContext *old = mix->context;
840 mix->context = context;
842 gst_object_unref (old);
843 same_downstream_gl_context = TRUE;
844 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
845 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
846 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
848 GstGLPlatform platform;
851 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
854 platform = gst_gl_platform_from_string (type);
855 gl_apis = gst_gl_api_from_string (apis);
857 if (gl_apis && platform)
859 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
865 if (mix->other_context) {
866 if (!other_context) {
867 other_context = mix->other_context;
869 GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
870 ("%s", "Cannot share with more than one GL context"),
871 ("%s", "Cannot share with more than one GL context"));
876 mix->context = gst_gl_context_new (mix->display);
877 if (!gst_gl_context_create (mix->context, other_context, &error))
881 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
882 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
884 g_mutex_lock (&mix->priv->gl_resource_lock);
885 mix->priv->gl_resource_ready = FALSE;
887 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
889 mix->depthbuffer = 0;
892 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
893 &mix->fbo, &mix->depthbuffer)) {
894 g_cond_signal (&mix->priv->gl_resource_cond);
895 g_mutex_unlock (&mix->priv->gl_resource_lock);
900 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
901 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
902 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
904 gst_query_parse_allocation (query, &caps, NULL);
906 if (mixer_class->set_caps)
907 mixer_class->set_caps (mix, caps);
909 mix->priv->gl_resource_ready = TRUE;
910 g_cond_signal (&mix->priv->gl_resource_cond);
911 g_mutex_unlock (&mix->priv->gl_resource_lock);
913 if (gst_query_get_n_allocation_pools (query) > 0) {
914 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
920 gst_video_info_init (&vinfo);
921 gst_video_info_from_caps (&vinfo, caps);
927 if (!pool || (!same_downstream_gl_context
928 && gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE,
930 && !gst_buffer_pool_has_option (pool,
931 GST_BUFFER_POOL_OPTION_GL_SYNC_META))) {
932 /* can't use this pool */
934 gst_object_unref (pool);
935 pool = gst_gl_buffer_pool_new (mix->context);
937 config = gst_buffer_pool_get_config (pool);
939 gst_buffer_pool_config_set_params (config, caps, size, min, max);
940 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
941 if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
942 gst_buffer_pool_config_add_option (config,
943 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
944 gst_buffer_pool_config_add_option (config,
945 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
947 gst_buffer_pool_set_config (pool, config);
950 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
952 gst_query_add_allocation_pool (query, pool, size, min, max);
954 gst_object_unref (pool);
960 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
966 /* takes ownership of the pool, allocator and query */
968 gst_gl_mixer_set_allocation (GstGLMixer * mix,
969 GstBufferPool * pool, GstAllocator * allocator,
970 GstAllocationParams * params, GstQuery * query)
972 GstAllocator *oldalloc;
973 GstBufferPool *oldpool;
975 GstGLMixerPrivate *priv = mix->priv;
977 GST_DEBUG ("storing allocation query");
979 GST_OBJECT_LOCK (mix);
980 oldpool = priv->pool;
982 priv->pool_active = FALSE;
984 oldalloc = priv->allocator;
985 priv->allocator = allocator;
987 oldquery = priv->query;
991 priv->params = *params;
993 gst_allocation_params_init (&priv->params);
994 GST_OBJECT_UNLOCK (mix);
997 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
998 gst_buffer_pool_set_active (oldpool, FALSE);
999 gst_object_unref (oldpool);
1002 gst_object_unref (oldalloc);
1005 gst_query_unref (oldquery);
1011 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
1014 gboolean result = TRUE;
1015 GstBufferPool *pool = NULL;
1016 GstAllocator *allocator;
1017 GstAllocationParams params;
1018 GstAggregator *agg = GST_AGGREGATOR (mix);
1020 /* find a pool for the negotiated caps now */
1021 GST_DEBUG_OBJECT (mix, "doing allocation query");
1022 query = gst_query_new_allocation (outcaps, TRUE);
1023 if (!gst_pad_peer_query (agg->srcpad, query)) {
1024 /* not a problem, just debug a little */
1025 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
1028 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
1029 result = gst_gl_mixer_decide_allocation (mix, query);
1031 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
1035 goto no_decide_allocation;
1037 /* we got configuration from our peer or the decide_allocation method,
1039 if (gst_query_get_n_allocation_params (query) > 0) {
1040 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
1043 gst_allocation_params_init (¶ms);
1046 if (gst_query_get_n_allocation_pools (query) > 0)
1047 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
1050 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
1055 no_decide_allocation:
1057 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
1058 gst_query_unref (query);
1065 _default_pad_upload_buffer (GstGLMixer * mix, GstGLMixerFrameData * frame,
1068 GstVideoAggregatorPad *vaggpad = GST_VIDEO_AGGREGATOR_PAD (frame->pad);
1069 GstGLMixerPad *pad = frame->pad;
1070 GstBuffer *uploaded_buf, *gl_buffer;
1071 GstVideoInfo gl_info;
1072 GstVideoFrame gl_frame;
1073 GstGLSyncMeta *sync_meta;
1075 gst_video_info_set_format (&gl_info,
1076 GST_VIDEO_FORMAT_RGBA,
1077 GST_VIDEO_INFO_WIDTH (&vaggpad->info),
1078 GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
1080 _init_upload (mix, pad);
1082 sync_meta = gst_buffer_get_gl_sync_meta (vaggpad->buffer);
1084 gst_gl_sync_meta_wait (sync_meta);
1086 if (gst_gl_upload_perform_with_buffer (pad->upload,
1087 vaggpad->buffer, &uploaded_buf) != GST_GL_UPLOAD_DONE) {
1091 if (!(gl_buffer = gst_gl_color_convert_perform (pad->convert, uploaded_buf))) {
1092 gst_buffer_unref (uploaded_buf);
1096 if (!gst_video_frame_map (&gl_frame, &gl_info, gl_buffer,
1097 GST_MAP_READ | GST_MAP_GL)) {
1098 gst_buffer_unref (uploaded_buf);
1099 gst_buffer_unref (gl_buffer);
1103 frame->texture = *(guint *) gl_frame.data[0];
1105 gst_buffer_unref (uploaded_buf);
1106 gst_video_frame_unmap (&gl_frame);
1112 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
1116 guint out_tex, out_tex_target;
1117 gboolean res = TRUE;
1118 guint array_index = 0;
1119 GstVideoFrame out_frame;
1120 GstElement *element = GST_ELEMENT (mix);
1121 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
1122 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1123 GstGLMixerPrivate *priv = mix->priv;
1124 gboolean to_download =
1125 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
1126 gst_caps_get_features (mix->out_caps, 0));
1127 GstMapFlags out_map_flags = GST_MAP_WRITE;
1129 GST_TRACE ("Processing buffers");
1131 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
1134 out_map_flags |= GST_MAP_GL;
1136 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
1141 out_tex = *(guint *) out_frame.data[0];
1143 ((GstGLMemory *) gst_buffer_peek_memory (outbuf, 0))->tex_target;
1145 GST_INFO ("Output Buffer does not contain correct memory, "
1146 "attempting to wrap for download");
1149 mix->download = gst_gl_download_new (mix->context);
1151 gst_gl_download_set_format (mix->download, &out_frame.info);
1152 out_tex = mix->out_tex_id;
1153 out_tex_target = GL_TEXTURE_2D;
1156 GST_OBJECT_LOCK (mix);
1157 walk = element->sinkpads;
1159 i = mix->frames->len;
1160 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1161 for (; i < element->numsinkpads; i++)
1162 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1164 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1165 GstGLMixerPadClass *pad_class = GST_GL_MIXER_PAD_GET_CLASS (pad);
1166 GstVideoAggregatorPad *vaggpad = walk->data;
1167 GstGLMixerFrameData *frame;
1169 frame = g_ptr_array_index (mix->frames, array_index);
1173 walk = g_list_next (walk);
1175 if (vaggpad->buffer != NULL) {
1176 g_assert (pad_class->upload_buffer);
1179 gst_buffer_unref (pad->gl_buffer);
1180 pad->gl_buffer = pad_class->upload_buffer (mix, frame, vaggpad->buffer);
1182 GST_DEBUG_OBJECT (pad,
1183 "uploaded buffer %" GST_PTR_FORMAT " from buffer %" GST_PTR_FORMAT,
1184 pad->gl_buffer, vaggpad->buffer);
1190 g_mutex_lock (&priv->gl_resource_lock);
1191 if (!priv->gl_resource_ready)
1192 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1194 if (!priv->gl_resource_ready) {
1195 g_mutex_unlock (&priv->gl_resource_lock);
1196 GST_ERROR_OBJECT (mix,
1197 "fbo used to render can't be created, do not run process_textures");
1202 mix_class->process_textures (mix, mix->frames, out_tex);
1204 g_mutex_unlock (&priv->gl_resource_lock);
1207 if (!gst_gl_download_perform_with_data (mix->download,
1208 out_tex, out_tex_target, out_frame.data)) {
1209 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1210 "Failed to download video frame"), (NULL));
1218 walk = GST_ELEMENT (mix)->sinkpads;
1220 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1223 gst_gl_upload_release_buffer (pad->upload);
1225 walk = g_list_next (walk);
1228 GST_OBJECT_UNLOCK (mix);
1230 gst_video_frame_unmap (&out_frame);
1236 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1239 guint i, array_index = 0;
1240 GstElement *element = GST_ELEMENT (mix);
1241 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1243 GST_OBJECT_LOCK (mix);
1244 walk = GST_ELEMENT (mix)->sinkpads;
1245 i = mix->frames->len;
1246 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1247 for (; i < element->numsinkpads; i++)
1248 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1249 while (walk) { /* We walk with this list because it's ordered */
1250 GstVideoAggregatorPad *vaggpad = walk->data;
1252 walk = g_list_next (walk);
1254 if (vaggpad->buffer != NULL) {
1255 /* put buffer into array */
1256 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1260 GST_OBJECT_UNLOCK (mix);
1262 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1267 static GstFlowReturn
1268 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1270 gboolean res = FALSE;
1271 GstGLMixer *mix = GST_GL_MIXER (vagg);
1272 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1273 GstGLSyncMeta *sync_meta;
1275 if (mix_class->process_buffers)
1276 res = gst_gl_mixer_process_buffers (mix, outbuf);
1277 else if (mix_class->process_textures)
1278 res = gst_gl_mixer_process_textures (mix, outbuf);
1280 sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
1282 gst_gl_sync_meta_set_sync_point (sync_meta, mix->context);
1284 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1288 gst_gl_mixer_get_property (GObject * object,
1289 guint prop_id, GValue * value, GParamSpec * pspec)
1291 GstGLMixer *mixer = GST_GL_MIXER (object);
1295 g_value_set_object (value, mixer->context);
1298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1304 gst_gl_mixer_set_property (GObject * object,
1305 guint prop_id, const GValue * value, GParamSpec * pspec)
1309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1315 _clean_upload (GstAggregator * agg, GstAggregatorPad * aggpad, gpointer udata)
1317 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1319 if (pad->gl_buffer) {
1320 gst_buffer_unref (pad->gl_buffer);
1321 pad->gl_buffer = NULL;
1325 gst_object_unref (pad->upload);
1330 gst_object_unref (pad->convert);
1331 pad->convert = NULL;
1338 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1340 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1344 gst_gl_mixer_start (GstAggregator * agg)
1347 GstGLMixer *mix = GST_GL_MIXER (agg);
1348 GstElement *element = GST_ELEMENT (agg);
1350 GST_OBJECT_LOCK (mix);
1351 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1352 (GDestroyNotify) _free_glmixer_frame_data);
1353 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1355 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1356 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1358 for (i = 0; i < element->numsinkpads; i++)
1359 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1361 GST_OBJECT_UNLOCK (mix);
1367 gst_gl_mixer_stop (GstAggregator * agg)
1369 GstGLMixer *mix = GST_GL_MIXER (agg);
1370 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1372 GST_OBJECT_LOCK (agg);
1373 g_ptr_array_free (mix->frames, TRUE);
1375 g_ptr_array_free (mix->array_buffers, TRUE);
1376 mix->array_buffers = NULL;
1377 GST_OBJECT_UNLOCK (agg);
1379 if (mixer_class->reset)
1380 mixer_class->reset (mix);
1382 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1384 mix->depthbuffer = 0;
1386 if (mix->download) {
1387 gst_object_unref (mix->download);
1388 mix->download = NULL;
1391 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1393 if (mix->priv->query) {
1394 gst_query_unref (mix->priv->query);
1395 mix->priv->query = NULL;
1398 if (mix->priv->pool) {
1399 gst_object_unref (mix->priv->pool);
1400 mix->priv->pool = NULL;
1404 gst_object_unref (mix->display);
1405 mix->display = NULL;
1409 gst_object_unref (mix->context);
1410 mix->context = NULL;
1413 gst_gl_mixer_reset (mix);