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-libs/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;
91 gobject_class->set_property = gst_gl_mixer_pad_set_property;
92 gobject_class->get_property = gst_gl_mixer_pad_get_property;
94 gobject_class->finalize = gst_gl_mixer_pad_finalize;
98 gst_gl_mixer_pad_finalize (GObject * object)
100 GstGLMixerPad *pad = GST_GL_MIXER_PAD (object);
103 gst_object_unref (pad->upload);
107 G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
111 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec)
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
122 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec)
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
135 GstGLMixer *mix = GST_GL_MIXER (vagg);
136 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
138 mix->priv->negotiated = ret;
140 gst_caps_replace (&mix->out_caps, caps);
146 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
147 GstQuery * decide_query, GstQuery * query)
150 GstStructure *config;
154 GError *error = NULL;
155 GstStructure *gl_context;
156 gchar *platform, *gl_apis;
158 GstAllocator *allocator = NULL;
159 GstAllocationParams params;
161 gst_query_parse_allocation (query, &caps, &need_pool);
166 if ((pool = mix->priv->pool))
167 gst_object_ref (pool);
172 /* we had a pool, check caps */
173 GST_DEBUG_OBJECT (mix, "check existing pool caps");
174 config = gst_buffer_pool_get_config (pool);
175 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
177 if (!gst_caps_is_equal (caps, pcaps)) {
178 GST_DEBUG_OBJECT (mix, "pool has different caps");
179 /* different caps, we can't use this pool */
180 gst_object_unref (pool);
183 gst_structure_free (config);
186 if (!gst_gl_ensure_display (mix, &mix->display))
190 mix->context = gst_gl_context_new (mix->display);
191 if (!gst_gl_context_create (mix->context, mix->other_context, &error))
195 if (pool == NULL && need_pool) {
198 if (!gst_video_info_from_caps (&info, caps))
201 GST_DEBUG_OBJECT (mix, "create new pool");
202 pool = gst_gl_buffer_pool_new (mix->context);
204 /* the normal size of a frame */
207 config = gst_buffer_pool_get_config (pool);
208 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
209 if (!gst_buffer_pool_set_config (pool, config))
214 gst_query_add_allocation_pool (query, pool, size, 1, 0);
215 gst_object_unref (pool);
218 /* we also support various metadata */
219 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
221 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
223 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
224 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
227 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
228 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
229 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
230 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
231 gst_query_add_allocation_meta (query,
232 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
236 gst_structure_free (gl_context);
238 gst_allocation_params_init (¶ms);
240 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
241 gst_query_add_allocation_param (query, allocator, ¶ms);
242 gst_object_unref (allocator);
249 GST_DEBUG_OBJECT (mix, "no caps specified");
254 GST_DEBUG_OBJECT (mix, "invalid caps specified");
259 GST_DEBUG_OBJECT (mix, "failed setting config");
264 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
271 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
274 gboolean ret = FALSE;
275 GstGLMixer *mix = GST_GL_MIXER (agg);
277 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
279 switch (GST_QUERY_TYPE (query)) {
280 case GST_QUERY_ALLOCATION:
282 GstQuery *decide_query = NULL;
284 GST_OBJECT_LOCK (mix);
285 if (G_UNLIKELY (!mix->priv->negotiated)) {
286 GST_DEBUG_OBJECT (mix,
287 "not negotiated yet, can't answer ALLOCATION query");
288 GST_OBJECT_UNLOCK (mix);
291 if ((decide_query = mix->priv->query))
292 gst_query_ref (decide_query);
293 GST_OBJECT_UNLOCK (mix);
295 GST_DEBUG_OBJECT (mix,
296 "calling propose allocation with query %" GST_PTR_FORMAT,
299 /* pass the query to the propose_allocation vmethod if any */
300 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
303 gst_query_unref (decide_query);
305 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
308 case GST_QUERY_CONTEXT:
310 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
315 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
323 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
327 /* GLMixer signals and args */
340 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
343 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
344 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
346 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
347 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
349 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
352 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
355 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
356 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
358 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
359 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
361 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
364 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
366 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
367 GstBuffer ** outbuf);
369 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
371 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
372 static gboolean gst_gl_mixer_start (GstAggregator * agg);
375 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
376 GstBuffer * outbuffer);
378 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
379 const GValue * value, GParamSpec * pspec);
380 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
381 GValue * value, GParamSpec * pspec);
383 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
385 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
386 GstBufferPool * pool, GstAllocator * allocator,
387 GstAllocationParams * params, GstQuery * query);
389 static void gst_gl_mixer_finalize (GObject * object);
392 gst_gl_mixer_class_init (GstGLMixerClass * klass)
394 GObjectClass *gobject_class;
395 GstElementClass *element_class;
397 GstVideoAggregatorClass *videoaggregator_class =
398 (GstVideoAggregatorClass *) klass;
399 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
401 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
403 gobject_class = (GObjectClass *) klass;
404 element_class = GST_ELEMENT_CLASS (klass);
406 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
408 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
410 gobject_class->get_property = gst_gl_mixer_get_property;
411 gobject_class->set_property = gst_gl_mixer_set_property;
413 g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
414 g_param_spec_object ("other-context",
415 "External OpenGL context",
416 "Give an external OpenGL context with which to share textures",
417 GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 gst_element_class_add_pad_template (element_class,
420 gst_static_pad_template_get (&src_factory));
421 gst_element_class_add_pad_template (element_class,
422 gst_static_pad_template_get (&sink_factory));
424 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
426 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
427 agg_class->sink_query = gst_gl_mixer_sink_query;
428 agg_class->src_query = gst_gl_mixer_src_query;
429 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
430 agg_class->stop = gst_gl_mixer_stop;
431 agg_class->start = gst_gl_mixer_start;
433 videoaggregator_class->disable_frame_conversion = TRUE;
434 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
435 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
436 videoaggregator_class->negotiated_caps = _negotiated_caps;
439 /* Register the pad class */
440 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
442 klass->set_caps = NULL;
447 gst_gl_mixer_reset (GstGLMixer * mix)
449 /* clean up collect data */
450 mix->priv->negotiated = FALSE;
454 gst_gl_mixer_init (GstGLMixer * mix)
456 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
457 mix->array_buffers = 0;
460 mix->depthbuffer = 0;
462 mix->priv->gl_resource_ready = FALSE;
463 g_mutex_init (&mix->priv->gl_resource_lock);
464 g_cond_init (&mix->priv->gl_resource_cond);
465 /* initialize variables */
466 gst_gl_mixer_reset (mix);
470 gst_gl_mixer_finalize (GObject * object)
472 GstGLMixer *mix = GST_GL_MIXER (object);
473 GstGLMixerPrivate *priv = mix->priv;
475 if (mix->other_context) {
476 gst_object_unref (mix->other_context);
477 mix->other_context = NULL;
480 g_mutex_clear (&priv->gl_resource_lock);
481 g_cond_clear (&priv->gl_resource_cond);
482 G_OBJECT_CLASS (parent_class)->finalize (object);
486 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
488 GstGLMixer *mix = GST_GL_MIXER (element);
490 gst_gl_handle_set_context (element, context, &mix->display);
494 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
496 gboolean result = TRUE;
499 if (!gst_gl_ensure_display (mix, &mix->display))
507 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
511 gboolean result = FALSE;
513 mix = GST_GL_MIXER (aggregator);
516 case GST_PAD_MODE_PUSH:
517 case GST_PAD_MODE_PULL:
518 result = gst_gl_mixer_activate (mix, active);
528 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
530 GstCaps *filter, *caps;
534 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
536 gst_query_parse_caps (query, &filter);
538 if (GST_VIDEO_INFO_FORMAT (&vagg->info) != GST_VIDEO_FORMAT_UNKNOWN) {
539 caps = gst_video_info_to_caps (&vagg->info);
541 caps = gst_pad_get_pad_template_caps (agg->srcpad);
544 caps = gst_caps_make_writable (caps);
546 n = gst_caps_get_size (caps) - 1;
547 for (; n >= 0; n--) {
548 s = gst_caps_get_structure (caps, n);
549 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
550 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
551 if (GST_VIDEO_INFO_FPS_D (&vagg->info) != 0) {
552 gst_structure_set (s,
553 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
558 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
560 gst_query_set_caps_result (query, caps);
561 gst_caps_unref (caps);
567 gst_gl_mixer_set_caps_features (const GstCaps * caps,
568 const gchar * feature_name)
570 GstCaps *tmp = gst_caps_copy (caps);
571 guint n = gst_caps_get_size (tmp);
574 for (i = 0; i < n; i++) {
575 GstCapsFeatures *features = gst_caps_get_features (tmp, i);
577 guint n_f = gst_caps_features_get_size (features);
579 for (j = 0; j < n_f; j++) {
580 gst_caps_features_remove_id (features,
581 gst_caps_features_get_nth_id (features, j));
585 gst_caps_features_add (features, feature_name);
586 gst_caps_set_simple (tmp, "format", G_TYPE_STRING, "RGBA", NULL);
592 /* copies the given caps */
594 gst_gl_mixer_caps_remove_format_info (GstCaps * caps)
601 res = gst_caps_new_empty ();
603 n = gst_caps_get_size (caps);
604 for (i = 0; i < n; i++) {
605 st = gst_caps_get_structure (caps, i);
606 f = gst_caps_get_features (caps, i);
608 /* If this is already expressed by the existing caps
609 * skip this structure */
610 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
613 st = gst_structure_copy (st);
614 /* Only remove format info for the cases when we can actually convert */
615 if (!gst_caps_features_is_any (f)
616 && gst_caps_features_is_equal (f,
617 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
618 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
619 "width", "height", NULL);
621 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
628 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
630 GstCaps *result = NULL;
631 GstCaps *glcaps = gst_gl_mixer_set_caps_features (caps,
632 GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
633 #if GST_GL_HAVE_PLATFORM_EGL
634 GstCaps *eglcaps = gst_gl_mixer_set_caps_features (caps,
635 GST_CAPS_FEATURE_MEMORY_EGL_IMAGE);
637 GstCaps *uploadcaps = gst_gl_mixer_set_caps_features (caps,
638 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META);
640 gst_caps_from_string (GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS));
642 result = gst_caps_new_empty ();
644 result = gst_caps_merge (result, glcaps);
645 #if GST_GL_HAVE_PLATFORM_EGL
646 result = gst_caps_merge (result, eglcaps);
648 result = gst_caps_merge (result, uploadcaps);
649 result = gst_caps_merge (result, raw_caps);
651 result = gst_caps_merge (result, gst_gl_mixer_caps_remove_format_info (caps));
653 GST_DEBUG_OBJECT (mix, "returning %" GST_PTR_FORMAT, result);
659 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
661 gboolean res = FALSE;
662 GstGLMixer *mix = GST_GL_MIXER (agg);
664 switch (GST_QUERY_TYPE (query)) {
665 case GST_QUERY_CONTEXT:
667 res = gst_gl_handle_context_query ((GstElement *) mix, query,
672 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
675 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
683 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
686 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
688 if (!mix->priv->pool_active) {
689 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
690 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
691 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
692 return GST_FLOW_ERROR;
694 mix->priv->pool_active = TRUE;
697 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
701 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
703 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
704 GstBufferPool *pool = NULL;
705 GstStructure *config;
707 guint min, max, size;
708 gboolean update_pool;
709 GError *error = NULL;
711 guint out_width, out_height;
712 GstGLContext *other_context = NULL;
713 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
715 if (!gst_gl_ensure_display (mix, &mix->display))
718 if (gst_query_find_allocation_meta (query,
719 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
720 GstGLContext *context;
721 const GstStructure *upload_meta_params;
726 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
727 if (upload_meta_params) {
728 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
729 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
730 GstGLContext *old = mix->context;
732 mix->context = context;
734 gst_object_unref (old);
735 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
736 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
737 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
739 GstGLPlatform platform;
742 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
745 platform = gst_gl_platform_from_string (type);
746 gl_apis = gst_gl_api_from_string (apis);
748 if (gl_apis && platform)
750 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
756 if (mix->other_context) {
757 if (!other_context) {
758 other_context = mix->other_context;
760 GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
761 ("%s", "Cannot share with more than one GL context"),
762 ("%s", "Cannot share with more than one GL context"));
767 mix->context = gst_gl_context_new (mix->display);
768 if (!gst_gl_context_create (mix->context, other_context, &error))
772 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
773 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
775 g_mutex_lock (&mix->priv->gl_resource_lock);
776 mix->priv->gl_resource_ready = FALSE;
778 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
780 mix->depthbuffer = 0;
783 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
784 &mix->fbo, &mix->depthbuffer)) {
785 g_cond_signal (&mix->priv->gl_resource_cond);
786 g_mutex_unlock (&mix->priv->gl_resource_lock);
791 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
792 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
793 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
795 gst_query_parse_allocation (query, &caps, NULL);
797 if (mixer_class->set_caps)
798 mixer_class->set_caps (mix, caps);
800 mix->priv->gl_resource_ready = TRUE;
801 g_cond_signal (&mix->priv->gl_resource_cond);
802 g_mutex_unlock (&mix->priv->gl_resource_lock);
804 if (gst_query_get_n_allocation_pools (query) > 0) {
805 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
811 gst_video_info_init (&vinfo);
812 gst_video_info_from_caps (&vinfo, caps);
819 pool = gst_gl_buffer_pool_new (mix->context);
821 config = gst_buffer_pool_get_config (pool);
822 gst_buffer_pool_config_set_params (config, caps, size, min, max);
824 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
825 gst_buffer_pool_config_add_option (config,
826 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
828 gst_buffer_pool_set_config (pool, config);
831 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
833 gst_query_add_allocation_pool (query, pool, size, min, max);
835 gst_object_unref (pool);
841 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
847 /* takes ownership of the pool, allocator and query */
849 gst_gl_mixer_set_allocation (GstGLMixer * mix,
850 GstBufferPool * pool, GstAllocator * allocator,
851 GstAllocationParams * params, GstQuery * query)
853 GstAllocator *oldalloc;
854 GstBufferPool *oldpool;
856 GstGLMixerPrivate *priv = mix->priv;
858 GST_DEBUG ("storing allocation query");
860 GST_OBJECT_LOCK (mix);
861 oldpool = priv->pool;
863 priv->pool_active = FALSE;
865 oldalloc = priv->allocator;
866 priv->allocator = allocator;
868 oldquery = priv->query;
872 priv->params = *params;
874 gst_allocation_params_init (&priv->params);
875 GST_OBJECT_UNLOCK (mix);
878 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
879 gst_buffer_pool_set_active (oldpool, FALSE);
880 gst_object_unref (oldpool);
883 gst_object_unref (oldalloc);
886 gst_query_unref (oldquery);
892 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
895 gboolean result = TRUE;
896 GstBufferPool *pool = NULL;
897 GstAllocator *allocator;
898 GstAllocationParams params;
899 GstAggregator *agg = GST_AGGREGATOR (mix);
901 /* find a pool for the negotiated caps now */
902 GST_DEBUG_OBJECT (mix, "doing allocation query");
903 query = gst_query_new_allocation (outcaps, TRUE);
904 if (!gst_pad_peer_query (agg->srcpad, query)) {
905 /* not a problem, just debug a little */
906 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
909 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
910 result = gst_gl_mixer_decide_allocation (mix, query);
912 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
916 goto no_decide_allocation;
918 /* we got configuration from our peer or the decide_allocation method,
920 if (gst_query_get_n_allocation_params (query) > 0) {
921 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
924 gst_allocation_params_init (¶ms);
927 if (gst_query_get_n_allocation_pools (query) > 0)
928 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
931 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
936 no_decide_allocation:
938 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
939 gst_query_unref (query);
946 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
952 guint array_index = 0;
953 GstVideoFrame out_frame;
954 GstElement *element = GST_ELEMENT (mix);
955 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
956 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
957 GstGLMixerPrivate *priv = mix->priv;
958 gboolean to_download =
959 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
960 gst_caps_get_features (mix->out_caps, 0));
961 GstMapFlags out_map_flags = GST_MAP_WRITE;
963 GST_TRACE ("Processing buffers");
965 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
968 out_map_flags |= GST_MAP_GL;
970 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
975 out_tex = *(guint *) out_frame.data[0];
977 GST_INFO ("Output Buffer does not contain correct memory, "
978 "attempting to wrap for download");
981 mix->download = gst_gl_download_new (mix->context);
983 gst_gl_download_set_format (mix->download, &out_frame.info);
984 out_tex = mix->out_tex_id;
987 GST_OBJECT_LOCK (mix);
988 walk = element->sinkpads;
990 i = mix->frames->len;
991 g_ptr_array_set_size (mix->frames, element->numsinkpads);
992 for (; i < element->numsinkpads; i++)
993 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
995 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
996 GstVideoAggregatorPad *vaggpad = walk->data;
997 GstGLMixerFrameData *frame;
999 frame = g_ptr_array_index (mix->frames, array_index);
1003 walk = g_list_next (walk);
1005 if (vaggpad->buffer != NULL) {
1009 pad->upload = gst_gl_upload_new (mix->context);
1011 gst_gl_upload_set_format (pad->upload, &vaggpad->info);
1014 if (!gst_gl_upload_perform_with_buffer (pad->upload,
1015 vaggpad->buffer, &in_tex, NULL)) {
1017 pad->mapped = FALSE;
1022 frame->texture = in_tex;
1027 g_mutex_lock (&priv->gl_resource_lock);
1028 if (!priv->gl_resource_ready)
1029 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1031 if (!priv->gl_resource_ready) {
1032 g_mutex_unlock (&priv->gl_resource_lock);
1033 GST_ERROR_OBJECT (mix,
1034 "fbo used to render can't be created, do not run process_textures");
1039 mix_class->process_textures (mix, mix->frames, out_tex);
1041 g_mutex_unlock (&priv->gl_resource_lock);
1044 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
1046 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1047 "Failed to download video frame"), (NULL));
1055 walk = GST_ELEMENT (mix)->sinkpads;
1057 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1060 gst_gl_upload_release_buffer (pad->upload);
1062 pad->mapped = FALSE;
1063 walk = g_list_next (walk);
1066 GST_OBJECT_UNLOCK (mix);
1068 gst_video_frame_unmap (&out_frame);
1074 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1077 guint i, array_index = 0;
1078 GstElement *element = GST_ELEMENT (mix);
1079 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1081 GST_OBJECT_LOCK (mix);
1082 walk = GST_ELEMENT (mix)->sinkpads;
1083 i = mix->frames->len;
1084 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1085 for (; i < element->numsinkpads; i++)
1086 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1087 while (walk) { /* We walk with this list because it's ordered */
1088 GstVideoAggregatorPad *vaggpad = walk->data;
1090 walk = g_list_next (walk);
1092 if (vaggpad->buffer != NULL) {
1093 /* put buffer into array */
1094 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1098 GST_OBJECT_UNLOCK (mix);
1100 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1105 static GstFlowReturn
1106 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1108 gboolean res = FALSE;
1109 GstGLMixer *mix = GST_GL_MIXER (vagg);
1110 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1112 if (mix_class->process_buffers)
1113 res = gst_gl_mixer_process_buffers (mix, outbuf);
1114 else if (mix_class->process_textures)
1115 res = gst_gl_mixer_process_textures (mix, outbuf);
1117 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1121 gst_gl_mixer_get_property (GObject * object,
1122 guint prop_id, GValue * value, GParamSpec * pspec)
1124 GstGLMixer *mix = GST_GL_MIXER (object);
1127 case PROP_OTHER_CONTEXT:
1128 g_value_set_object (value, mix->other_context);
1131 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1137 gst_gl_mixer_set_property (GObject * object,
1138 guint prop_id, const GValue * value, GParamSpec * pspec)
1140 GstGLMixer *mix = GST_GL_MIXER (object);
1143 case PROP_OTHER_CONTEXT:
1145 if (mix->other_context)
1146 gst_object_unref (mix->other_context);
1147 mix->other_context = g_value_dup_object (value);
1151 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1157 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1159 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1162 gst_object_unref (pad->upload);
1170 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1172 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1176 gst_gl_mixer_start (GstAggregator * agg)
1179 GstGLMixer *mix = GST_GL_MIXER (agg);
1180 GstElement *element = GST_ELEMENT (agg);
1182 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1185 GST_OBJECT_LOCK (mix);
1186 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1187 (GDestroyNotify) _free_glmixer_frame_data);
1188 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1190 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1191 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1193 for (i = 0; i < element->numsinkpads; i++)
1194 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1196 GST_OBJECT_UNLOCK (mix);
1202 gst_gl_mixer_stop (GstAggregator * agg)
1204 GstGLMixer *mix = GST_GL_MIXER (agg);
1205 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1207 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1210 GST_OBJECT_LOCK (agg);
1211 g_ptr_array_free (mix->frames, TRUE);
1213 g_ptr_array_free (mix->array_buffers, TRUE);
1214 mix->array_buffers = NULL;
1215 GST_OBJECT_UNLOCK (agg);
1217 if (mixer_class->reset)
1218 mixer_class->reset (mix);
1220 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1222 mix->depthbuffer = 0;
1224 if (mix->download) {
1225 gst_object_unref (mix->download);
1226 mix->download = NULL;
1229 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1231 if (mix->priv->query) {
1232 gst_query_unref (mix->priv->query);
1233 mix->priv->query = NULL;
1236 if (mix->priv->pool) {
1237 gst_object_unref (mix->priv->pool);
1238 mix->priv->pool = NULL;
1242 gst_object_unref (mix->display);
1243 mix->display = NULL;
1247 gst_object_unref (mix->context);
1248 mix->context = NULL;
1251 gst_gl_mixer_reset (mix);