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);
53 static void gst_gl_mixer_set_context (GstElement * element,
54 GstContext * context);
61 #define GST_GL_MIXER_GET_PRIVATE(obj) \
62 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
64 struct _GstGLMixerPrivate
70 GstAllocator *allocator;
71 GstAllocationParams params;
74 gboolean gl_resource_ready;
75 GMutex gl_resource_lock;
76 GCond gl_resource_cond;
79 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_VIDEO_AGGREGATOR_PAD);
82 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
84 GObjectClass *gobject_class = (GObjectClass *) klass;
86 gobject_class->set_property = gst_gl_mixer_pad_set_property;
87 gobject_class->get_property = gst_gl_mixer_pad_get_property;
91 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec)
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec)
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
115 GstGLMixer *mix = GST_GL_MIXER (vagg);
116 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
118 mix->priv->negotiated = ret;
124 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
125 GstQuery * decide_query, GstQuery * query)
128 GstStructure *config;
132 GError *error = NULL;
133 GstStructure *gl_context;
134 gchar *platform, *gl_apis;
136 GstAllocator *allocator = NULL;
137 GstAllocationParams params;
139 gst_query_parse_allocation (query, &caps, &need_pool);
144 if ((pool = mix->priv->pool))
145 gst_object_ref (pool);
150 /* we had a pool, check caps */
151 GST_DEBUG_OBJECT (mix, "check existing pool caps");
152 config = gst_buffer_pool_get_config (pool);
153 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
155 if (!gst_caps_is_equal (caps, pcaps)) {
156 GST_DEBUG_OBJECT (mix, "pool has different caps");
157 /* different caps, we can't use this pool */
158 gst_object_unref (pool);
161 gst_structure_free (config);
164 if (!gst_gl_ensure_display (mix, &mix->display))
168 mix->context = gst_gl_context_new (mix->display);
169 if (!gst_gl_context_create (mix->context, NULL, &error))
173 if (pool == NULL && need_pool) {
176 if (!gst_video_info_from_caps (&info, caps))
179 GST_DEBUG_OBJECT (mix, "create new pool");
180 pool = gst_gl_buffer_pool_new (mix->context);
182 /* the normal size of a frame */
185 config = gst_buffer_pool_get_config (pool);
186 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
187 if (!gst_buffer_pool_set_config (pool, config))
192 gst_query_add_allocation_pool (query, pool, size, 1, 0);
193 gst_object_unref (pool);
196 /* we also support various metadata */
197 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
199 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
201 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
202 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
205 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
206 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
207 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
208 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
209 gst_query_add_allocation_meta (query,
210 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
214 gst_structure_free (gl_context);
216 gst_allocation_params_init (¶ms);
218 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
219 gst_query_add_allocation_param (query, allocator, ¶ms);
220 gst_object_unref (allocator);
227 GST_DEBUG_OBJECT (mix, "no caps specified");
232 GST_DEBUG_OBJECT (mix, "invalid caps specified");
237 GST_DEBUG_OBJECT (mix, "failed setting config");
242 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
249 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
252 gboolean ret = FALSE;
253 GstGLMixer *mix = GST_GL_MIXER (agg);
255 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
257 switch (GST_QUERY_TYPE (query)) {
258 case GST_QUERY_ALLOCATION:
260 GstQuery *decide_query = NULL;
263 GST_OBJECT_LOCK (mix);
264 if (G_UNLIKELY (!(negotiated = mix->priv->negotiated))) {
265 GST_DEBUG_OBJECT (mix,
266 "not negotiated yet, can't answer ALLOCATION query");
267 GST_OBJECT_UNLOCK (mix);
270 if ((decide_query = mix->priv->query))
271 gst_query_ref (decide_query);
272 GST_OBJECT_UNLOCK (mix);
274 GST_DEBUG_OBJECT (mix,
275 "calling propose allocation with query %" GST_PTR_FORMAT,
278 /* pass the query to the propose_allocation vmethod if any */
279 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
282 gst_query_unref (decide_query);
284 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
287 case GST_QUERY_CONTEXT:
289 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
294 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
302 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
306 /* GLMixer signals and args */
318 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
321 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
322 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
324 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
325 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
327 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
330 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
333 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
334 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
336 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
337 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
339 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
342 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
344 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
345 GstBuffer ** outbuf);
347 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
349 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
350 static gboolean gst_gl_mixer_start (GstAggregator * agg);
353 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
354 GstBuffer * outbuffer);
356 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec);
358 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
359 GValue * value, GParamSpec * pspec);
361 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
363 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
364 GstBufferPool * pool, GstAllocator * allocator,
365 GstAllocationParams * params, GstQuery * query);
367 static void gst_gl_mixer_finalize (GObject * object);
370 gst_gl_mixer_class_init (GstGLMixerClass * klass)
372 GObjectClass *gobject_class;
373 GstElementClass *element_class;
375 GstVideoAggregatorClass *videoaggregator_class =
376 (GstVideoAggregatorClass *) klass;
377 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
379 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
381 gobject_class = (GObjectClass *) klass;
382 element_class = GST_ELEMENT_CLASS (klass);
384 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
386 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
388 gobject_class->get_property = gst_gl_mixer_get_property;
389 gobject_class->set_property = gst_gl_mixer_set_property;
391 gst_element_class_add_pad_template (element_class,
392 gst_static_pad_template_get (&src_factory));
393 gst_element_class_add_pad_template (element_class,
394 gst_static_pad_template_get (&sink_factory));
396 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
398 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
399 agg_class->sink_query = gst_gl_mixer_sink_query;
400 agg_class->src_query = gst_gl_mixer_src_query;
401 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
402 agg_class->stop = gst_gl_mixer_stop;
403 agg_class->start = gst_gl_mixer_start;
405 videoaggregator_class->disable_frame_conversion = TRUE;
406 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
407 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
408 videoaggregator_class->negotiated_caps = _negotiated_caps;
411 /* Register the pad class */
412 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
414 klass->set_caps = NULL;
419 gst_gl_mixer_reset (GstGLMixer * mix)
421 /* clean up collect data */
422 mix->priv->negotiated = FALSE;
426 gst_gl_mixer_init (GstGLMixer * mix)
428 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
429 mix->array_buffers = 0;
432 mix->depthbuffer = 0;
434 mix->priv->gl_resource_ready = FALSE;
435 g_mutex_init (&mix->priv->gl_resource_lock);
436 g_cond_init (&mix->priv->gl_resource_cond);
437 /* initialize variables */
438 gst_gl_mixer_reset (mix);
442 gst_gl_mixer_finalize (GObject * object)
444 GstGLMixerPrivate *priv = GST_GL_MIXER (object)->priv;
446 g_mutex_clear (&priv->gl_resource_lock);
447 g_cond_clear (&priv->gl_resource_cond);
448 G_OBJECT_CLASS (parent_class)->finalize (object);
452 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
454 GstGLMixer *mix = GST_GL_MIXER (element);
456 gst_gl_handle_set_context (element, context, &mix->display);
460 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
462 gboolean result = TRUE;
465 if (!gst_gl_ensure_display (mix, &mix->display))
473 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
477 gboolean result = FALSE;
479 mix = GST_GL_MIXER (aggregator);
482 case GST_PAD_MODE_PUSH:
483 case GST_PAD_MODE_PULL:
484 result = gst_gl_mixer_activate (mix, active);
494 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
496 GstCaps *filter, *caps;
500 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
502 gst_query_parse_caps (query, &filter);
504 if (GST_VIDEO_INFO_FORMAT (&vagg->info) != GST_VIDEO_FORMAT_UNKNOWN) {
505 caps = gst_video_info_to_caps (&vagg->info);
507 caps = gst_pad_get_pad_template_caps (agg->srcpad);
510 caps = gst_caps_make_writable (caps);
512 n = gst_caps_get_size (caps) - 1;
513 for (; n >= 0; n--) {
514 s = gst_caps_get_structure (caps, n);
515 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
516 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
517 if (GST_VIDEO_INFO_FPS_D (&vagg->info) != 0) {
518 gst_structure_set (s,
519 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
524 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
526 gst_query_set_caps_result (query, caps);
527 gst_caps_unref (caps);
533 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
535 gboolean res = FALSE;
536 GstGLMixer *mix = GST_GL_MIXER (agg);
538 switch (GST_QUERY_TYPE (query)) {
539 case GST_QUERY_CONTEXT:
541 res = gst_gl_handle_context_query ((GstElement *) mix, query,
546 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
549 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
557 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
560 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
562 if (!mix->priv->pool_active) {
563 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
564 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
565 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
566 return GST_FLOW_ERROR;
568 mix->priv->pool_active = TRUE;
571 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
575 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
577 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
578 GstBufferPool *pool = NULL;
579 GstStructure *config;
581 guint min, max, size;
582 gboolean update_pool;
583 GError *error = NULL;
585 guint out_width, out_height;
586 GstGLContext *other_context = NULL;
587 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
589 gst_query_parse_allocation (query, &caps, NULL);
591 if (gst_query_get_n_allocation_pools (query) > 0) {
592 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
598 gst_video_info_init (&vinfo);
599 gst_video_info_from_caps (&vinfo, caps);
605 if (!gst_gl_ensure_display (mix, &mix->display))
608 if (gst_query_find_allocation_meta (query,
609 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
610 GstGLContext *context;
611 const GstStructure *upload_meta_params;
616 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
617 if (upload_meta_params) {
618 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
619 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
620 GstGLContext *old = mix->context;
622 mix->context = context;
624 gst_object_unref (old);
625 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
626 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
627 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
629 GstGLPlatform platform = GST_GL_PLATFORM_NONE;
632 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
635 platform = gst_gl_platform_from_string (type);
636 gl_apis = gst_gl_api_from_string (apis);
638 if (gl_apis && platform)
640 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
647 mix->context = gst_gl_context_new (mix->display);
648 if (!gst_gl_context_create (mix->context, other_context, &error))
652 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
653 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
655 g_mutex_lock (&mix->priv->gl_resource_lock);
656 mix->priv->gl_resource_ready = FALSE;
658 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
660 mix->depthbuffer = 0;
663 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
664 &mix->fbo, &mix->depthbuffer)) {
665 g_cond_signal (&mix->priv->gl_resource_cond);
666 g_mutex_unlock (&mix->priv->gl_resource_lock);
671 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
672 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
673 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
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);
683 pool = gst_gl_buffer_pool_new (mix->context);
685 config = gst_buffer_pool_get_config (pool);
686 gst_buffer_pool_config_set_params (config, caps, size, min, max);
688 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
690 gst_buffer_pool_set_config (pool, config);
693 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
695 gst_query_add_allocation_pool (query, pool, size, min, max);
697 gst_object_unref (pool);
703 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
709 /* takes ownership of the pool, allocator and query */
711 gst_gl_mixer_set_allocation (GstGLMixer * mix,
712 GstBufferPool * pool, GstAllocator * allocator,
713 GstAllocationParams * params, GstQuery * query)
715 GstAllocator *oldalloc;
716 GstBufferPool *oldpool;
718 GstGLMixerPrivate *priv = mix->priv;
720 GST_DEBUG ("storing allocation query");
722 GST_OBJECT_LOCK (mix);
723 oldpool = priv->pool;
725 priv->pool_active = FALSE;
727 oldalloc = priv->allocator;
728 priv->allocator = allocator;
730 oldquery = priv->query;
734 priv->params = *params;
736 gst_allocation_params_init (&priv->params);
737 GST_OBJECT_UNLOCK (mix);
740 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
741 gst_buffer_pool_set_active (oldpool, FALSE);
742 gst_object_unref (oldpool);
745 gst_object_unref (oldalloc);
748 gst_query_unref (oldquery);
754 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
757 gboolean result = TRUE;
758 GstBufferPool *pool = NULL;
759 GstAllocator *allocator;
760 GstAllocationParams params;
761 GstAggregator *agg = GST_AGGREGATOR (mix);
763 /* find a pool for the negotiated caps now */
764 GST_DEBUG_OBJECT (mix, "doing allocation query");
765 query = gst_query_new_allocation (outcaps, TRUE);
766 if (!gst_pad_peer_query (agg->srcpad, query)) {
767 /* not a problem, just debug a little */
768 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
771 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
772 result = gst_gl_mixer_decide_allocation (mix, query);
774 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
778 goto no_decide_allocation;
780 /* we got configuration from our peer or the decide_allocation method,
782 if (gst_query_get_n_allocation_params (query) > 0) {
783 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
786 gst_allocation_params_init (¶ms);
789 if (gst_query_get_n_allocation_pools (query) > 0)
790 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
793 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
798 no_decide_allocation:
800 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
801 gst_query_unref (query);
808 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
814 guint array_index = 0;
815 GstVideoFrame out_frame;
816 gboolean out_gl_wrapped = FALSE;
817 GstElement *element = GST_ELEMENT (mix);
818 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
819 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
820 GstGLMixerPrivate *priv = mix->priv;
822 GST_TRACE ("Processing buffers");
824 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
825 GST_MAP_WRITE | GST_MAP_GL)) {
829 if (gst_is_gl_memory (out_frame.map[0].memory)) {
830 out_tex = *(guint *) out_frame.data[0];
832 GST_INFO ("Output Buffer does not contain correct memory, "
833 "attempting to wrap for download");
835 out_tex = mix->out_tex_id;;
838 mix->download = gst_gl_download_new (mix->context);
840 gst_gl_download_set_format (mix->download, &out_frame.info);
841 out_gl_wrapped = TRUE;
844 GST_OBJECT_LOCK (mix);
845 walk = element->sinkpads;
847 i = mix->frames->len;
848 g_ptr_array_set_size (mix->frames, element->numsinkpads);
849 for (; i < element->numsinkpads; i++)
850 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
852 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
853 GstVideoAggregatorPad *vaggpad = walk->data;
854 GstGLMixerFrameData *frame;
856 frame = g_ptr_array_index (mix->frames, array_index);
860 walk = g_list_next (walk);
862 if (vaggpad->buffer != NULL) {
866 pad->upload = gst_gl_upload_new (mix->context);
868 gst_gl_upload_set_format (pad->upload, &vaggpad->info);
871 if (!gst_gl_upload_perform_with_buffer (pad->upload,
872 vaggpad->buffer, &in_tex)) {
879 frame->texture = in_tex;
884 g_mutex_lock (&priv->gl_resource_lock);
885 if (!priv->gl_resource_ready)
886 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
888 if (!priv->gl_resource_ready) {
889 g_mutex_unlock (&priv->gl_resource_lock);
890 GST_ERROR_OBJECT (mix,
891 "fbo used to render can't be created, do not run process_textures");
896 mix_class->process_textures (mix, mix->frames, out_tex);
898 g_mutex_unlock (&priv->gl_resource_lock);
900 if (out_gl_wrapped) {
901 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
903 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
904 "Failed to download video frame"), (NULL));
912 walk = GST_ELEMENT (mix)->sinkpads;
914 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
917 gst_gl_upload_release_buffer (pad->upload);
920 walk = g_list_next (walk);
923 GST_OBJECT_UNLOCK (mix);
925 gst_video_frame_unmap (&out_frame);
931 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
934 guint i, array_index = 0;
935 GstElement *element = GST_ELEMENT (mix);
936 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
938 GST_OBJECT_LOCK (mix);
939 walk = GST_ELEMENT (mix)->sinkpads;
940 i = mix->frames->len;
941 g_ptr_array_set_size (mix->frames, element->numsinkpads);
942 for (; i < element->numsinkpads; i++)
943 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
944 while (walk) { /* We walk with this list because it's ordered */
945 GstVideoAggregatorPad *vaggpad = walk->data;
947 walk = g_list_next (walk);
949 if (vaggpad->buffer != NULL) {
950 /* put buffer into array */
951 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
955 GST_OBJECT_UNLOCK (mix);
957 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
963 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
965 gboolean res = FALSE;
966 GstGLMixer *mix = GST_GL_MIXER (vagg);
967 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
969 if (mix_class->process_buffers)
970 res = gst_gl_mixer_process_buffers (mix, outbuf);
971 else if (mix_class->process_textures)
972 res = gst_gl_mixer_process_textures (mix, outbuf);
974 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
978 gst_gl_mixer_get_property (GObject * object,
979 guint prop_id, GValue * value, GParamSpec * pspec)
983 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
989 gst_gl_mixer_set_property (GObject * object,
990 guint prop_id, const GValue * value, GParamSpec * pspec)
994 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1000 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1002 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1005 gst_object_unref (pad->upload);
1013 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1015 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1019 gst_gl_mixer_start (GstAggregator * agg)
1022 GstGLMixer *mix = GST_GL_MIXER (agg);
1023 GstElement *element = GST_ELEMENT (agg);
1025 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1028 GST_OBJECT_LOCK (mix);
1029 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1030 (GDestroyNotify) _free_glmixer_frame_data);
1031 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1033 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1034 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1036 for (i = 0; i < element->numsinkpads; i++)
1037 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1039 GST_OBJECT_UNLOCK (mix);
1045 gst_gl_mixer_stop (GstAggregator * agg)
1047 GstGLMixer *mix = GST_GL_MIXER (agg);
1048 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1050 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1053 GST_OBJECT_LOCK (agg);
1054 g_ptr_array_free (mix->frames, TRUE);
1056 g_ptr_array_free (mix->array_buffers, TRUE);
1057 mix->array_buffers = NULL;
1058 GST_OBJECT_UNLOCK (agg);
1060 if (mixer_class->reset)
1061 mixer_class->reset (mix);
1063 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1065 mix->depthbuffer = 0;
1067 if (mix->download) {
1068 gst_object_unref (mix->download);
1069 mix->download = NULL;
1072 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1074 if (mix->priv->query) {
1075 gst_query_unref (mix->priv->query);
1076 mix->priv->query = NULL;
1079 if (mix->priv->pool) {
1080 gst_object_unref (mix->priv->pool);
1081 mix->priv->pool = NULL;
1085 gst_object_unref (mix->display);
1086 mix->display = NULL;
1090 gst_object_unref (mix->context);
1091 mix->context = NULL;
1093 gst_gl_mixer_reset (mix);