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);
105 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
106 GValue * value, GParamSpec * pspec)
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
117 const GValue * value, GParamSpec * pspec)
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
129 GstGLMixer *mix = GST_GL_MIXER (vagg);
130 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
132 mix->priv->negotiated = ret;
138 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
139 GstQuery * decide_query, GstQuery * query)
142 GstStructure *config;
146 GError *error = NULL;
147 GstStructure *gl_context;
148 gchar *platform, *gl_apis;
150 GstAllocator *allocator = NULL;
151 GstAllocationParams params;
153 gst_query_parse_allocation (query, &caps, &need_pool);
158 if ((pool = mix->priv->pool))
159 gst_object_ref (pool);
164 /* we had a pool, check caps */
165 GST_DEBUG_OBJECT (mix, "check existing pool caps");
166 config = gst_buffer_pool_get_config (pool);
167 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
169 if (!gst_caps_is_equal (caps, pcaps)) {
170 GST_DEBUG_OBJECT (mix, "pool has different caps");
171 /* different caps, we can't use this pool */
172 gst_object_unref (pool);
175 gst_structure_free (config);
178 if (!gst_gl_ensure_display (mix, &mix->display))
182 mix->context = gst_gl_context_new (mix->display);
183 if (!gst_gl_context_create (mix->context, NULL, &error))
187 if (pool == NULL && need_pool) {
190 if (!gst_video_info_from_caps (&info, caps))
193 GST_DEBUG_OBJECT (mix, "create new pool");
194 pool = gst_gl_buffer_pool_new (mix->context);
196 /* the normal size of a frame */
199 config = gst_buffer_pool_get_config (pool);
200 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
201 if (!gst_buffer_pool_set_config (pool, config))
206 gst_query_add_allocation_pool (query, pool, size, 1, 0);
207 gst_object_unref (pool);
210 /* we also support various metadata */
211 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
213 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
215 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
216 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
219 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
220 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
221 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
222 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
223 gst_query_add_allocation_meta (query,
224 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
228 gst_structure_free (gl_context);
230 gst_allocation_params_init (¶ms);
232 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
233 gst_query_add_allocation_param (query, allocator, ¶ms);
234 gst_object_unref (allocator);
241 GST_DEBUG_OBJECT (mix, "no caps specified");
246 GST_DEBUG_OBJECT (mix, "invalid caps specified");
251 GST_DEBUG_OBJECT (mix, "failed setting config");
256 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
263 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
266 gboolean ret = FALSE;
267 GstGLMixer *mix = GST_GL_MIXER (agg);
269 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
271 switch (GST_QUERY_TYPE (query)) {
272 case GST_QUERY_ALLOCATION:
274 GstQuery *decide_query = NULL;
277 GST_OBJECT_LOCK (mix);
278 if (G_UNLIKELY (!(negotiated = mix->priv->negotiated))) {
279 GST_DEBUG_OBJECT (mix,
280 "not negotiated yet, can't answer ALLOCATION query");
281 GST_OBJECT_UNLOCK (mix);
284 if ((decide_query = mix->priv->query))
285 gst_query_ref (decide_query);
286 GST_OBJECT_UNLOCK (mix);
288 GST_DEBUG_OBJECT (mix,
289 "calling propose allocation with query %" GST_PTR_FORMAT,
292 /* pass the query to the propose_allocation vmethod if any */
293 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
296 gst_query_unref (decide_query);
298 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
301 case GST_QUERY_CONTEXT:
303 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
308 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
316 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
320 /* GLMixer signals and args */
332 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
335 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
336 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
338 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
339 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
341 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
344 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
347 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
348 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
350 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
351 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
353 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
356 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
358 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
359 GstBuffer ** outbuf);
361 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
363 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
364 static gboolean gst_gl_mixer_start (GstAggregator * agg);
367 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
368 GstBuffer * outbuffer);
370 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
371 const GValue * value, GParamSpec * pspec);
372 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
373 GValue * value, GParamSpec * pspec);
375 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
377 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
378 GstBufferPool * pool, GstAllocator * allocator,
379 GstAllocationParams * params, GstQuery * query);
381 static void gst_gl_mixer_finalize (GObject * object);
384 gst_gl_mixer_class_init (GstGLMixerClass * klass)
386 GObjectClass *gobject_class;
387 GstElementClass *element_class;
389 GstVideoAggregatorClass *videoaggregator_class =
390 (GstVideoAggregatorClass *) klass;
391 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
393 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
395 gobject_class = (GObjectClass *) klass;
396 element_class = GST_ELEMENT_CLASS (klass);
398 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
400 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
402 gobject_class->get_property = gst_gl_mixer_get_property;
403 gobject_class->set_property = gst_gl_mixer_set_property;
405 gst_element_class_add_pad_template (element_class,
406 gst_static_pad_template_get (&src_factory));
407 gst_element_class_add_pad_template (element_class,
408 gst_static_pad_template_get (&sink_factory));
410 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
412 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
413 agg_class->sink_query = gst_gl_mixer_sink_query;
414 agg_class->src_query = gst_gl_mixer_src_query;
415 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
416 agg_class->stop = gst_gl_mixer_stop;
417 agg_class->start = gst_gl_mixer_start;
419 videoaggregator_class->disable_frame_conversion = TRUE;
420 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
421 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
422 videoaggregator_class->negotiated_caps = _negotiated_caps;
425 /* Register the pad class */
426 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
428 klass->set_caps = NULL;
433 gst_gl_mixer_reset (GstGLMixer * mix)
435 /* clean up collect data */
436 mix->priv->negotiated = FALSE;
440 gst_gl_mixer_init (GstGLMixer * mix)
442 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
443 mix->array_buffers = 0;
446 mix->depthbuffer = 0;
448 mix->priv->gl_resource_ready = FALSE;
449 g_mutex_init (&mix->priv->gl_resource_lock);
450 g_cond_init (&mix->priv->gl_resource_cond);
451 /* initialize variables */
452 gst_gl_mixer_reset (mix);
456 gst_gl_mixer_finalize (GObject * object)
458 GstGLMixerPrivate *priv = GST_GL_MIXER (object)->priv;
460 g_mutex_clear (&priv->gl_resource_lock);
461 g_cond_clear (&priv->gl_resource_cond);
462 G_OBJECT_CLASS (parent_class)->finalize (object);
466 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
468 GstGLMixer *mix = GST_GL_MIXER (element);
470 gst_gl_handle_set_context (element, context, &mix->display);
474 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
476 gboolean result = TRUE;
479 if (!gst_gl_ensure_display (mix, &mix->display))
487 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
491 gboolean result = FALSE;
493 mix = GST_GL_MIXER (aggregator);
496 case GST_PAD_MODE_PUSH:
497 case GST_PAD_MODE_PULL:
498 result = gst_gl_mixer_activate (mix, active);
508 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
510 GstCaps *filter, *caps;
514 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
516 gst_query_parse_caps (query, &filter);
518 if (GST_VIDEO_INFO_FORMAT (&vagg->info) != GST_VIDEO_FORMAT_UNKNOWN) {
519 caps = gst_video_info_to_caps (&vagg->info);
521 caps = gst_pad_get_pad_template_caps (agg->srcpad);
524 caps = gst_caps_make_writable (caps);
526 n = gst_caps_get_size (caps) - 1;
527 for (; n >= 0; n--) {
528 s = gst_caps_get_structure (caps, n);
529 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
530 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
531 if (GST_VIDEO_INFO_FPS_D (&vagg->info) != 0) {
532 gst_structure_set (s,
533 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
538 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
540 gst_query_set_caps_result (query, caps);
541 gst_caps_unref (caps);
547 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
549 gboolean res = FALSE;
550 GstGLMixer *mix = GST_GL_MIXER (agg);
552 switch (GST_QUERY_TYPE (query)) {
553 case GST_QUERY_CONTEXT:
555 res = gst_gl_handle_context_query ((GstElement *) mix, query,
560 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
563 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
571 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
574 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
576 if (!mix->priv->pool_active) {
577 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
578 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
579 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
580 return GST_FLOW_ERROR;
582 mix->priv->pool_active = TRUE;
585 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
589 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
591 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
592 GstBufferPool *pool = NULL;
593 GstStructure *config;
595 guint min, max, size;
596 gboolean update_pool;
597 GError *error = NULL;
599 guint out_width, out_height;
600 GstGLContext *other_context = NULL;
601 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
603 if (!gst_gl_ensure_display (mix, &mix->display))
606 if (gst_query_find_allocation_meta (query,
607 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
608 GstGLContext *context;
609 const GstStructure *upload_meta_params;
614 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
615 if (upload_meta_params) {
616 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
617 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
618 GstGLContext *old = mix->context;
620 mix->context = context;
622 gst_object_unref (old);
623 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
624 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
625 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
627 GstGLPlatform platform = GST_GL_PLATFORM_NONE;
630 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
633 platform = gst_gl_platform_from_string (type);
634 gl_apis = gst_gl_api_from_string (apis);
636 if (gl_apis && platform)
638 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
645 mix->context = gst_gl_context_new (mix->display);
646 if (!gst_gl_context_create (mix->context, other_context, &error))
650 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
651 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
653 g_mutex_lock (&mix->priv->gl_resource_lock);
654 mix->priv->gl_resource_ready = FALSE;
656 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
658 mix->depthbuffer = 0;
661 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
662 &mix->fbo, &mix->depthbuffer)) {
663 g_cond_signal (&mix->priv->gl_resource_cond);
664 g_mutex_unlock (&mix->priv->gl_resource_lock);
669 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
670 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
671 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
673 gst_query_parse_allocation (query, &caps, NULL);
675 if (mixer_class->set_caps)
676 mixer_class->set_caps (mix, caps);
678 mix->priv->gl_resource_ready = TRUE;
679 g_cond_signal (&mix->priv->gl_resource_cond);
680 g_mutex_unlock (&mix->priv->gl_resource_lock);
682 if (gst_query_get_n_allocation_pools (query) > 0) {
683 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
689 gst_video_info_init (&vinfo);
690 gst_video_info_from_caps (&vinfo, caps);
697 pool = gst_gl_buffer_pool_new (mix->context);
699 config = gst_buffer_pool_get_config (pool);
700 gst_buffer_pool_config_set_params (config, caps, size, min, max);
702 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
704 gst_buffer_pool_set_config (pool, config);
707 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
709 gst_query_add_allocation_pool (query, pool, size, min, max);
711 gst_object_unref (pool);
717 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
723 /* takes ownership of the pool, allocator and query */
725 gst_gl_mixer_set_allocation (GstGLMixer * mix,
726 GstBufferPool * pool, GstAllocator * allocator,
727 GstAllocationParams * params, GstQuery * query)
729 GstAllocator *oldalloc;
730 GstBufferPool *oldpool;
732 GstGLMixerPrivate *priv = mix->priv;
734 GST_DEBUG ("storing allocation query");
736 GST_OBJECT_LOCK (mix);
737 oldpool = priv->pool;
739 priv->pool_active = FALSE;
741 oldalloc = priv->allocator;
742 priv->allocator = allocator;
744 oldquery = priv->query;
748 priv->params = *params;
750 gst_allocation_params_init (&priv->params);
751 GST_OBJECT_UNLOCK (mix);
754 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
755 gst_buffer_pool_set_active (oldpool, FALSE);
756 gst_object_unref (oldpool);
759 gst_object_unref (oldalloc);
762 gst_query_unref (oldquery);
768 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
771 gboolean result = TRUE;
772 GstBufferPool *pool = NULL;
773 GstAllocator *allocator;
774 GstAllocationParams params;
775 GstAggregator *agg = GST_AGGREGATOR (mix);
777 /* find a pool for the negotiated caps now */
778 GST_DEBUG_OBJECT (mix, "doing allocation query");
779 query = gst_query_new_allocation (outcaps, TRUE);
780 if (!gst_pad_peer_query (agg->srcpad, query)) {
781 /* not a problem, just debug a little */
782 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
785 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
786 result = gst_gl_mixer_decide_allocation (mix, query);
788 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
792 goto no_decide_allocation;
794 /* we got configuration from our peer or the decide_allocation method,
796 if (gst_query_get_n_allocation_params (query) > 0) {
797 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
800 gst_allocation_params_init (¶ms);
803 if (gst_query_get_n_allocation_pools (query) > 0)
804 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
807 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
812 no_decide_allocation:
814 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
815 gst_query_unref (query);
822 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
828 guint array_index = 0;
829 GstVideoFrame out_frame;
830 gboolean out_gl_wrapped = FALSE;
831 GstElement *element = GST_ELEMENT (mix);
832 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
833 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
834 GstGLMixerPrivate *priv = mix->priv;
836 GST_TRACE ("Processing buffers");
838 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
839 GST_MAP_WRITE | GST_MAP_GL)) {
843 if (gst_is_gl_memory (out_frame.map[0].memory)) {
844 out_tex = *(guint *) out_frame.data[0];
846 GST_INFO ("Output Buffer does not contain correct memory, "
847 "attempting to wrap for download");
849 out_tex = mix->out_tex_id;;
852 mix->download = gst_gl_download_new (mix->context);
854 gst_gl_download_set_format (mix->download, &out_frame.info);
855 out_gl_wrapped = TRUE;
858 GST_OBJECT_LOCK (mix);
859 walk = element->sinkpads;
861 i = mix->frames->len;
862 g_ptr_array_set_size (mix->frames, element->numsinkpads);
863 for (; i < element->numsinkpads; i++)
864 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
866 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
867 GstVideoAggregatorPad *vaggpad = walk->data;
868 GstGLMixerFrameData *frame;
870 frame = g_ptr_array_index (mix->frames, array_index);
874 walk = g_list_next (walk);
876 if (vaggpad->buffer != NULL) {
880 pad->upload = gst_gl_upload_new (mix->context);
882 gst_gl_upload_set_format (pad->upload, &vaggpad->info);
885 if (!gst_gl_upload_perform_with_buffer (pad->upload,
886 vaggpad->buffer, &in_tex)) {
893 frame->texture = in_tex;
898 g_mutex_lock (&priv->gl_resource_lock);
899 if (!priv->gl_resource_ready)
900 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
902 if (!priv->gl_resource_ready) {
903 g_mutex_unlock (&priv->gl_resource_lock);
904 GST_ERROR_OBJECT (mix,
905 "fbo used to render can't be created, do not run process_textures");
910 mix_class->process_textures (mix, mix->frames, out_tex);
912 g_mutex_unlock (&priv->gl_resource_lock);
914 if (out_gl_wrapped) {
915 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
917 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
918 "Failed to download video frame"), (NULL));
926 walk = GST_ELEMENT (mix)->sinkpads;
928 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
931 gst_gl_upload_release_buffer (pad->upload);
934 walk = g_list_next (walk);
937 GST_OBJECT_UNLOCK (mix);
939 gst_video_frame_unmap (&out_frame);
945 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
948 guint i, array_index = 0;
949 GstElement *element = GST_ELEMENT (mix);
950 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
952 GST_OBJECT_LOCK (mix);
953 walk = GST_ELEMENT (mix)->sinkpads;
954 i = mix->frames->len;
955 g_ptr_array_set_size (mix->frames, element->numsinkpads);
956 for (; i < element->numsinkpads; i++)
957 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
958 while (walk) { /* We walk with this list because it's ordered */
959 GstVideoAggregatorPad *vaggpad = walk->data;
961 walk = g_list_next (walk);
963 if (vaggpad->buffer != NULL) {
964 /* put buffer into array */
965 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
969 GST_OBJECT_UNLOCK (mix);
971 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
977 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
979 gboolean res = FALSE;
980 GstGLMixer *mix = GST_GL_MIXER (vagg);
981 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
983 if (mix_class->process_buffers)
984 res = gst_gl_mixer_process_buffers (mix, outbuf);
985 else if (mix_class->process_textures)
986 res = gst_gl_mixer_process_textures (mix, outbuf);
988 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
992 gst_gl_mixer_get_property (GObject * object,
993 guint prop_id, GValue * value, GParamSpec * pspec)
997 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1003 gst_gl_mixer_set_property (GObject * object,
1004 guint prop_id, const GValue * value, GParamSpec * pspec)
1008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1014 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1016 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1019 gst_object_unref (pad->upload);
1027 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1029 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1033 gst_gl_mixer_start (GstAggregator * agg)
1036 GstGLMixer *mix = GST_GL_MIXER (agg);
1037 GstElement *element = GST_ELEMENT (agg);
1039 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1042 GST_OBJECT_LOCK (mix);
1043 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1044 (GDestroyNotify) _free_glmixer_frame_data);
1045 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1047 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1048 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1050 for (i = 0; i < element->numsinkpads; i++)
1051 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1053 GST_OBJECT_UNLOCK (mix);
1059 gst_gl_mixer_stop (GstAggregator * agg)
1061 GstGLMixer *mix = GST_GL_MIXER (agg);
1062 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1064 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1067 GST_OBJECT_LOCK (agg);
1068 g_ptr_array_free (mix->frames, TRUE);
1070 g_ptr_array_free (mix->array_buffers, TRUE);
1071 mix->array_buffers = NULL;
1072 GST_OBJECT_UNLOCK (agg);
1074 if (mixer_class->reset)
1075 mixer_class->reset (mix);
1077 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1079 mix->depthbuffer = 0;
1081 if (mix->download) {
1082 gst_object_unref (mix->download);
1083 mix->download = NULL;
1086 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1088 if (mix->priv->query) {
1089 gst_query_unref (mix->priv->query);
1090 mix->priv->query = NULL;
1093 if (mix->priv->pool) {
1094 gst_object_unref (mix->priv->pool);
1095 mix->priv->pool = NULL;
1099 gst_object_unref (mix->display);
1100 mix->display = NULL;
1104 gst_object_unref (mix->context);
1105 mix->context = NULL;
1107 gst_gl_mixer_reset (mix);