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 #define gst_gl_mixer_parent_class parent_class
40 G_DEFINE_ABSTRACT_TYPE (GstGLMixer, gst_gl_mixer, GST_TYPE_VIDEO_AGGREGATOR);
41 static gboolean gst_gl_mixer_do_bufferpool (GstGLMixer * mix,
45 #define GST_CAT_DEFAULT gst_gl_mixer_debug
46 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
48 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
49 GValue * value, GParamSpec * pspec);
50 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
51 const GValue * value, GParamSpec * pspec);
52 static void gst_gl_mixer_pad_finalize (GObject * object);
54 static void gst_gl_mixer_set_context (GstElement * element,
55 GstContext * context);
62 #define GST_GL_MIXER_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
65 struct _GstGLMixerPrivate
71 GstAllocator *allocator;
72 GstAllocationParams params;
75 gboolean gl_resource_ready;
76 GMutex gl_resource_lock;
77 GCond gl_resource_cond;
80 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_VIDEO_AGGREGATOR_PAD);
83 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
85 GObjectClass *gobject_class = (GObjectClass *) klass;
87 gobject_class->set_property = gst_gl_mixer_pad_set_property;
88 gobject_class->get_property = gst_gl_mixer_pad_get_property;
90 gobject_class->finalize = gst_gl_mixer_pad_finalize;
94 gst_gl_mixer_pad_finalize (GObject * object)
96 GstGLMixerPad *pad = GST_GL_MIXER_PAD (object);
99 gst_object_unref (pad->upload);
103 G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
107 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec)
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec)
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
131 GstGLMixer *mix = GST_GL_MIXER (vagg);
132 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
134 mix->priv->negotiated = ret;
136 gst_caps_replace (&mix->out_caps, caps);
142 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
143 GstQuery * decide_query, GstQuery * query)
146 GstStructure *config;
150 GError *error = NULL;
151 GstStructure *gl_context;
152 gchar *platform, *gl_apis;
154 GstAllocator *allocator = NULL;
155 GstAllocationParams params;
157 gst_query_parse_allocation (query, &caps, &need_pool);
162 if ((pool = mix->priv->pool))
163 gst_object_ref (pool);
168 /* we had a pool, check caps */
169 GST_DEBUG_OBJECT (mix, "check existing pool caps");
170 config = gst_buffer_pool_get_config (pool);
171 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
173 if (!gst_caps_is_equal (caps, pcaps)) {
174 GST_DEBUG_OBJECT (mix, "pool has different caps");
175 /* different caps, we can't use this pool */
176 gst_object_unref (pool);
179 gst_structure_free (config);
182 if (!gst_gl_ensure_display (mix, &mix->display))
186 mix->context = gst_gl_context_new (mix->display);
187 if (!gst_gl_context_create (mix->context, mix->other_context, &error))
191 if (pool == NULL && need_pool) {
194 if (!gst_video_info_from_caps (&info, caps))
197 GST_DEBUG_OBJECT (mix, "create new pool");
198 pool = gst_gl_buffer_pool_new (mix->context);
200 /* the normal size of a frame */
203 config = gst_buffer_pool_get_config (pool);
204 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
205 if (!gst_buffer_pool_set_config (pool, config))
210 gst_query_add_allocation_pool (query, pool, size, 1, 0);
211 gst_object_unref (pool);
214 /* we also support various metadata */
215 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
217 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
219 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
220 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
223 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
224 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
225 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
226 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
227 gst_query_add_allocation_meta (query,
228 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
232 gst_structure_free (gl_context);
234 gst_allocation_params_init (¶ms);
236 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
237 gst_query_add_allocation_param (query, allocator, ¶ms);
238 gst_object_unref (allocator);
245 GST_DEBUG_OBJECT (mix, "no caps specified");
250 GST_DEBUG_OBJECT (mix, "invalid caps specified");
255 GST_DEBUG_OBJECT (mix, "failed setting config");
260 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
267 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
270 gboolean ret = FALSE;
271 GstGLMixer *mix = GST_GL_MIXER (agg);
273 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
275 switch (GST_QUERY_TYPE (query)) {
276 case GST_QUERY_ALLOCATION:
278 GstQuery *decide_query = NULL;
280 GST_OBJECT_LOCK (mix);
281 if (G_UNLIKELY (!mix->priv->negotiated)) {
282 GST_DEBUG_OBJECT (mix,
283 "not negotiated yet, can't answer ALLOCATION query");
284 GST_OBJECT_UNLOCK (mix);
287 if ((decide_query = mix->priv->query))
288 gst_query_ref (decide_query);
289 GST_OBJECT_UNLOCK (mix);
291 GST_DEBUG_OBJECT (mix,
292 "calling propose allocation with query %" GST_PTR_FORMAT,
295 /* pass the query to the propose_allocation vmethod if any */
296 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
299 gst_query_unref (decide_query);
301 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
304 case GST_QUERY_CONTEXT:
306 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
311 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
319 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
323 /* GLMixer signals and args */
336 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
339 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
340 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
342 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
343 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
345 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
348 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
351 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
352 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
354 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
355 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
357 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
360 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
362 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
363 GstBuffer ** outbuf);
365 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
367 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
368 static gboolean gst_gl_mixer_start (GstAggregator * agg);
371 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
372 GstBuffer * outbuffer);
374 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
375 const GValue * value, GParamSpec * pspec);
376 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
377 GValue * value, GParamSpec * pspec);
379 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
381 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
382 GstBufferPool * pool, GstAllocator * allocator,
383 GstAllocationParams * params, GstQuery * query);
385 static void gst_gl_mixer_finalize (GObject * object);
388 gst_gl_mixer_class_init (GstGLMixerClass * klass)
390 GObjectClass *gobject_class;
391 GstElementClass *element_class;
393 GstVideoAggregatorClass *videoaggregator_class =
394 (GstVideoAggregatorClass *) klass;
395 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
397 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
399 gobject_class = (GObjectClass *) klass;
400 element_class = GST_ELEMENT_CLASS (klass);
402 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
404 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
406 gobject_class->get_property = gst_gl_mixer_get_property;
407 gobject_class->set_property = gst_gl_mixer_set_property;
409 g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
410 g_param_spec_object ("other-context",
411 "External OpenGL context",
412 "Give an external OpenGL context with which to share textures",
413 GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 gst_element_class_add_pad_template (element_class,
416 gst_static_pad_template_get (&src_factory));
417 gst_element_class_add_pad_template (element_class,
418 gst_static_pad_template_get (&sink_factory));
420 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
422 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
423 agg_class->sink_query = gst_gl_mixer_sink_query;
424 agg_class->src_query = gst_gl_mixer_src_query;
425 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
426 agg_class->stop = gst_gl_mixer_stop;
427 agg_class->start = gst_gl_mixer_start;
429 videoaggregator_class->disable_frame_conversion = TRUE;
430 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
431 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
432 videoaggregator_class->negotiated_caps = _negotiated_caps;
435 /* Register the pad class */
436 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
438 klass->set_caps = NULL;
443 gst_gl_mixer_reset (GstGLMixer * mix)
445 /* clean up collect data */
446 mix->priv->negotiated = FALSE;
450 gst_gl_mixer_init (GstGLMixer * mix)
452 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
453 mix->array_buffers = 0;
456 mix->depthbuffer = 0;
458 mix->priv->gl_resource_ready = FALSE;
459 g_mutex_init (&mix->priv->gl_resource_lock);
460 g_cond_init (&mix->priv->gl_resource_cond);
461 /* initialize variables */
462 gst_gl_mixer_reset (mix);
466 gst_gl_mixer_finalize (GObject * object)
468 GstGLMixer *mix = GST_GL_MIXER (object);
469 GstGLMixerPrivate *priv = mix->priv;
471 if (mix->other_context) {
472 gst_object_unref (mix->other_context);
473 mix->other_context = NULL;
476 g_mutex_clear (&priv->gl_resource_lock);
477 g_cond_clear (&priv->gl_resource_cond);
478 G_OBJECT_CLASS (parent_class)->finalize (object);
482 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
484 GstGLMixer *mix = GST_GL_MIXER (element);
486 gst_gl_handle_set_context (element, context, &mix->display);
490 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
492 gboolean result = TRUE;
495 if (!gst_gl_ensure_display (mix, &mix->display))
503 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
507 gboolean result = FALSE;
509 mix = GST_GL_MIXER (aggregator);
512 case GST_PAD_MODE_PUSH:
513 case GST_PAD_MODE_PULL:
514 result = gst_gl_mixer_activate (mix, active);
524 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
526 GstCaps *filter, *caps;
530 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
532 gst_query_parse_caps (query, &filter);
534 if (GST_VIDEO_INFO_FORMAT (&vagg->info) != GST_VIDEO_FORMAT_UNKNOWN) {
535 caps = gst_video_info_to_caps (&vagg->info);
537 caps = gst_pad_get_pad_template_caps (agg->srcpad);
540 caps = gst_caps_make_writable (caps);
542 n = gst_caps_get_size (caps) - 1;
543 for (; n >= 0; n--) {
544 s = gst_caps_get_structure (caps, n);
545 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
546 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
547 if (GST_VIDEO_INFO_FPS_D (&vagg->info) != 0) {
548 gst_structure_set (s,
549 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
554 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
556 gst_query_set_caps_result (query, caps);
557 gst_caps_unref (caps);
563 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
565 gboolean res = FALSE;
566 GstGLMixer *mix = GST_GL_MIXER (agg);
568 switch (GST_QUERY_TYPE (query)) {
569 case GST_QUERY_CONTEXT:
571 res = gst_gl_handle_context_query ((GstElement *) mix, query,
576 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
579 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
587 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
590 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
592 if (!mix->priv->pool_active) {
593 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
594 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
595 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
596 return GST_FLOW_ERROR;
598 mix->priv->pool_active = TRUE;
601 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
605 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
607 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
608 GstBufferPool *pool = NULL;
609 GstStructure *config;
611 guint min, max, size;
612 gboolean update_pool;
613 GError *error = NULL;
615 guint out_width, out_height;
616 GstGLContext *other_context = NULL;
617 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
619 if (!gst_gl_ensure_display (mix, &mix->display))
622 if (gst_query_find_allocation_meta (query,
623 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
624 GstGLContext *context;
625 const GstStructure *upload_meta_params;
630 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
631 if (upload_meta_params) {
632 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
633 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
634 GstGLContext *old = mix->context;
636 mix->context = context;
638 gst_object_unref (old);
639 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
640 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
641 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
643 GstGLPlatform platform;
646 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
649 platform = gst_gl_platform_from_string (type);
650 gl_apis = gst_gl_api_from_string (apis);
652 if (gl_apis && platform)
654 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
660 if (mix->other_context) {
661 if (!other_context) {
662 other_context = mix->other_context;
664 GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
665 ("%s", "Cannot share with more than one GL context"),
666 ("%s", "Cannot share with more than one GL context"));
671 mix->context = gst_gl_context_new (mix->display);
672 if (!gst_gl_context_create (mix->context, other_context, &error))
676 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
677 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
679 g_mutex_lock (&mix->priv->gl_resource_lock);
680 mix->priv->gl_resource_ready = FALSE;
682 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
684 mix->depthbuffer = 0;
687 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
688 &mix->fbo, &mix->depthbuffer)) {
689 g_cond_signal (&mix->priv->gl_resource_cond);
690 g_mutex_unlock (&mix->priv->gl_resource_lock);
695 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
696 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
697 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
699 gst_query_parse_allocation (query, &caps, NULL);
701 if (mixer_class->set_caps)
702 mixer_class->set_caps (mix, caps);
704 mix->priv->gl_resource_ready = TRUE;
705 g_cond_signal (&mix->priv->gl_resource_cond);
706 g_mutex_unlock (&mix->priv->gl_resource_lock);
708 if (gst_query_get_n_allocation_pools (query) > 0) {
709 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
715 gst_video_info_init (&vinfo);
716 gst_video_info_from_caps (&vinfo, caps);
723 pool = gst_gl_buffer_pool_new (mix->context);
725 config = gst_buffer_pool_get_config (pool);
726 gst_buffer_pool_config_set_params (config, caps, size, min, max);
728 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
730 gst_buffer_pool_set_config (pool, config);
733 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
735 gst_query_add_allocation_pool (query, pool, size, min, max);
737 gst_object_unref (pool);
743 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
749 /* takes ownership of the pool, allocator and query */
751 gst_gl_mixer_set_allocation (GstGLMixer * mix,
752 GstBufferPool * pool, GstAllocator * allocator,
753 GstAllocationParams * params, GstQuery * query)
755 GstAllocator *oldalloc;
756 GstBufferPool *oldpool;
758 GstGLMixerPrivate *priv = mix->priv;
760 GST_DEBUG ("storing allocation query");
762 GST_OBJECT_LOCK (mix);
763 oldpool = priv->pool;
765 priv->pool_active = FALSE;
767 oldalloc = priv->allocator;
768 priv->allocator = allocator;
770 oldquery = priv->query;
774 priv->params = *params;
776 gst_allocation_params_init (&priv->params);
777 GST_OBJECT_UNLOCK (mix);
780 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
781 gst_buffer_pool_set_active (oldpool, FALSE);
782 gst_object_unref (oldpool);
785 gst_object_unref (oldalloc);
788 gst_query_unref (oldquery);
794 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
797 gboolean result = TRUE;
798 GstBufferPool *pool = NULL;
799 GstAllocator *allocator;
800 GstAllocationParams params;
801 GstAggregator *agg = GST_AGGREGATOR (mix);
803 /* find a pool for the negotiated caps now */
804 GST_DEBUG_OBJECT (mix, "doing allocation query");
805 query = gst_query_new_allocation (outcaps, TRUE);
806 if (!gst_pad_peer_query (agg->srcpad, query)) {
807 /* not a problem, just debug a little */
808 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
811 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
812 result = gst_gl_mixer_decide_allocation (mix, query);
814 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
818 goto no_decide_allocation;
820 /* we got configuration from our peer or the decide_allocation method,
822 if (gst_query_get_n_allocation_params (query) > 0) {
823 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
826 gst_allocation_params_init (¶ms);
829 if (gst_query_get_n_allocation_pools (query) > 0)
830 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
833 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
838 no_decide_allocation:
840 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
841 gst_query_unref (query);
848 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
854 guint array_index = 0;
855 GstVideoFrame out_frame;
856 GstElement *element = GST_ELEMENT (mix);
857 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
858 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
859 GstGLMixerPrivate *priv = mix->priv;
860 gboolean to_download =
861 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
862 gst_caps_get_features (mix->out_caps, 0));
863 GstMapFlags out_map_flags = GST_MAP_WRITE;
865 GST_TRACE ("Processing buffers");
867 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
870 out_map_flags |= GST_MAP_GL;
872 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
877 out_tex = *(guint *) out_frame.data[0];
879 GST_INFO ("Output Buffer does not contain correct memory, "
880 "attempting to wrap for download");
883 mix->download = gst_gl_download_new (mix->context);
885 gst_gl_download_set_format (mix->download, &out_frame.info);
886 out_tex = mix->out_tex_id;
889 GST_OBJECT_LOCK (mix);
890 walk = element->sinkpads;
892 i = mix->frames->len;
893 g_ptr_array_set_size (mix->frames, element->numsinkpads);
894 for (; i < element->numsinkpads; i++)
895 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
897 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
898 GstVideoAggregatorPad *vaggpad = walk->data;
899 GstGLMixerFrameData *frame;
901 frame = g_ptr_array_index (mix->frames, array_index);
905 walk = g_list_next (walk);
907 if (vaggpad->buffer != NULL) {
911 pad->upload = gst_gl_upload_new (mix->context);
913 gst_gl_upload_set_format (pad->upload, &vaggpad->info);
916 if (!gst_gl_upload_perform_with_buffer (pad->upload,
917 vaggpad->buffer, &in_tex, NULL)) {
924 frame->texture = in_tex;
929 g_mutex_lock (&priv->gl_resource_lock);
930 if (!priv->gl_resource_ready)
931 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
933 if (!priv->gl_resource_ready) {
934 g_mutex_unlock (&priv->gl_resource_lock);
935 GST_ERROR_OBJECT (mix,
936 "fbo used to render can't be created, do not run process_textures");
941 mix_class->process_textures (mix, mix->frames, out_tex);
943 g_mutex_unlock (&priv->gl_resource_lock);
946 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
948 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
949 "Failed to download video frame"), (NULL));
957 walk = GST_ELEMENT (mix)->sinkpads;
959 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
962 gst_gl_upload_release_buffer (pad->upload);
965 walk = g_list_next (walk);
968 GST_OBJECT_UNLOCK (mix);
970 gst_video_frame_unmap (&out_frame);
976 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
979 guint i, array_index = 0;
980 GstElement *element = GST_ELEMENT (mix);
981 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
983 GST_OBJECT_LOCK (mix);
984 walk = GST_ELEMENT (mix)->sinkpads;
985 i = mix->frames->len;
986 g_ptr_array_set_size (mix->frames, element->numsinkpads);
987 for (; i < element->numsinkpads; i++)
988 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
989 while (walk) { /* We walk with this list because it's ordered */
990 GstVideoAggregatorPad *vaggpad = walk->data;
992 walk = g_list_next (walk);
994 if (vaggpad->buffer != NULL) {
995 /* put buffer into array */
996 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1000 GST_OBJECT_UNLOCK (mix);
1002 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1007 static GstFlowReturn
1008 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1010 gboolean res = FALSE;
1011 GstGLMixer *mix = GST_GL_MIXER (vagg);
1012 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1014 if (mix_class->process_buffers)
1015 res = gst_gl_mixer_process_buffers (mix, outbuf);
1016 else if (mix_class->process_textures)
1017 res = gst_gl_mixer_process_textures (mix, outbuf);
1019 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1023 gst_gl_mixer_get_property (GObject * object,
1024 guint prop_id, GValue * value, GParamSpec * pspec)
1026 GstGLMixer *mix = GST_GL_MIXER (object);
1029 case PROP_OTHER_CONTEXT:
1030 g_value_set_object (value, mix->other_context);
1033 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1039 gst_gl_mixer_set_property (GObject * object,
1040 guint prop_id, const GValue * value, GParamSpec * pspec)
1042 GstGLMixer *mix = GST_GL_MIXER (object);
1045 case PROP_OTHER_CONTEXT:
1047 if (mix->other_context)
1048 gst_object_unref (mix->other_context);
1049 mix->other_context = g_value_dup_object (value);
1053 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1059 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1061 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1064 gst_object_unref (pad->upload);
1072 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1074 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1078 gst_gl_mixer_start (GstAggregator * agg)
1081 GstGLMixer *mix = GST_GL_MIXER (agg);
1082 GstElement *element = GST_ELEMENT (agg);
1084 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1087 GST_OBJECT_LOCK (mix);
1088 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1089 (GDestroyNotify) _free_glmixer_frame_data);
1090 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1092 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1093 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1095 for (i = 0; i < element->numsinkpads; i++)
1096 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1098 GST_OBJECT_UNLOCK (mix);
1104 gst_gl_mixer_stop (GstAggregator * agg)
1106 GstGLMixer *mix = GST_GL_MIXER (agg);
1107 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1109 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1112 GST_OBJECT_LOCK (agg);
1113 g_ptr_array_free (mix->frames, TRUE);
1115 g_ptr_array_free (mix->array_buffers, TRUE);
1116 mix->array_buffers = NULL;
1117 GST_OBJECT_UNLOCK (agg);
1119 if (mixer_class->reset)
1120 mixer_class->reset (mix);
1122 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1124 mix->depthbuffer = 0;
1126 if (mix->download) {
1127 gst_object_unref (mix->download);
1128 mix->download = NULL;
1131 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1133 if (mix->priv->query) {
1134 gst_query_unref (mix->priv->query);
1135 mix->priv->query = NULL;
1138 if (mix->priv->pool) {
1139 gst_object_unref (mix->priv->pool);
1140 mix->priv->pool = NULL;
1144 gst_object_unref (mix->display);
1145 mix->display = NULL;
1149 gst_object_unref (mix->context);
1150 mix->context = NULL;
1153 gst_gl_mixer_reset (mix);