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;
134 gst_caps_replace (&mix->out_caps, caps);
140 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
141 GstQuery * decide_query, GstQuery * query)
144 GstStructure *config;
148 GError *error = NULL;
149 GstStructure *gl_context;
150 gchar *platform, *gl_apis;
152 GstAllocator *allocator = NULL;
153 GstAllocationParams params;
155 gst_query_parse_allocation (query, &caps, &need_pool);
160 if ((pool = mix->priv->pool))
161 gst_object_ref (pool);
166 /* we had a pool, check caps */
167 GST_DEBUG_OBJECT (mix, "check existing pool caps");
168 config = gst_buffer_pool_get_config (pool);
169 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
171 if (!gst_caps_is_equal (caps, pcaps)) {
172 GST_DEBUG_OBJECT (mix, "pool has different caps");
173 /* different caps, we can't use this pool */
174 gst_object_unref (pool);
177 gst_structure_free (config);
180 if (!gst_gl_ensure_display (mix, &mix->display))
184 mix->context = gst_gl_context_new (mix->display);
185 if (!gst_gl_context_create (mix->context, NULL, &error))
189 if (pool == NULL && need_pool) {
192 if (!gst_video_info_from_caps (&info, caps))
195 GST_DEBUG_OBJECT (mix, "create new pool");
196 pool = gst_gl_buffer_pool_new (mix->context);
198 /* the normal size of a frame */
201 config = gst_buffer_pool_get_config (pool);
202 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
203 if (!gst_buffer_pool_set_config (pool, config))
208 gst_query_add_allocation_pool (query, pool, size, 1, 0);
209 gst_object_unref (pool);
212 /* we also support various metadata */
213 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
215 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
217 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
218 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
221 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
222 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
223 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
224 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
225 gst_query_add_allocation_meta (query,
226 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
230 gst_structure_free (gl_context);
232 gst_allocation_params_init (¶ms);
234 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
235 gst_query_add_allocation_param (query, allocator, ¶ms);
236 gst_object_unref (allocator);
243 GST_DEBUG_OBJECT (mix, "no caps specified");
248 GST_DEBUG_OBJECT (mix, "invalid caps specified");
253 GST_DEBUG_OBJECT (mix, "failed setting config");
258 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
265 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
268 gboolean ret = FALSE;
269 GstGLMixer *mix = GST_GL_MIXER (agg);
271 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
273 switch (GST_QUERY_TYPE (query)) {
274 case GST_QUERY_ALLOCATION:
276 GstQuery *decide_query = NULL;
278 GST_OBJECT_LOCK (mix);
279 if (G_UNLIKELY (!mix->priv->negotiated)) {
280 GST_DEBUG_OBJECT (mix,
281 "not negotiated yet, can't answer ALLOCATION query");
282 GST_OBJECT_UNLOCK (mix);
285 if ((decide_query = mix->priv->query))
286 gst_query_ref (decide_query);
287 GST_OBJECT_UNLOCK (mix);
289 GST_DEBUG_OBJECT (mix,
290 "calling propose allocation with query %" GST_PTR_FORMAT,
293 /* pass the query to the propose_allocation vmethod if any */
294 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
297 gst_query_unref (decide_query);
299 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
302 case GST_QUERY_CONTEXT:
304 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
309 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
317 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
321 /* GLMixer signals and args */
333 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
336 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
337 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
339 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
340 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
342 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
345 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
348 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
349 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
351 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
352 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
354 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
357 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
359 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
360 GstBuffer ** outbuf);
362 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
364 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
365 static gboolean gst_gl_mixer_start (GstAggregator * agg);
368 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
369 GstBuffer * outbuffer);
371 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
372 const GValue * value, GParamSpec * pspec);
373 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
374 GValue * value, GParamSpec * pspec);
376 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
378 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
379 GstBufferPool * pool, GstAllocator * allocator,
380 GstAllocationParams * params, GstQuery * query);
382 static void gst_gl_mixer_finalize (GObject * object);
385 gst_gl_mixer_class_init (GstGLMixerClass * klass)
387 GObjectClass *gobject_class;
388 GstElementClass *element_class;
390 GstVideoAggregatorClass *videoaggregator_class =
391 (GstVideoAggregatorClass *) klass;
392 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
394 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
396 gobject_class = (GObjectClass *) klass;
397 element_class = GST_ELEMENT_CLASS (klass);
399 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
401 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
403 gobject_class->get_property = gst_gl_mixer_get_property;
404 gobject_class->set_property = gst_gl_mixer_set_property;
406 gst_element_class_add_pad_template (element_class,
407 gst_static_pad_template_get (&src_factory));
408 gst_element_class_add_pad_template (element_class,
409 gst_static_pad_template_get (&sink_factory));
411 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
413 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
414 agg_class->sink_query = gst_gl_mixer_sink_query;
415 agg_class->src_query = gst_gl_mixer_src_query;
416 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
417 agg_class->stop = gst_gl_mixer_stop;
418 agg_class->start = gst_gl_mixer_start;
420 videoaggregator_class->disable_frame_conversion = TRUE;
421 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
422 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
423 videoaggregator_class->negotiated_caps = _negotiated_caps;
426 /* Register the pad class */
427 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
429 klass->set_caps = NULL;
434 gst_gl_mixer_reset (GstGLMixer * mix)
436 /* clean up collect data */
437 mix->priv->negotiated = FALSE;
441 gst_gl_mixer_init (GstGLMixer * mix)
443 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
444 mix->array_buffers = 0;
447 mix->depthbuffer = 0;
449 mix->priv->gl_resource_ready = FALSE;
450 g_mutex_init (&mix->priv->gl_resource_lock);
451 g_cond_init (&mix->priv->gl_resource_cond);
452 /* initialize variables */
453 gst_gl_mixer_reset (mix);
457 gst_gl_mixer_finalize (GObject * object)
459 GstGLMixerPrivate *priv = GST_GL_MIXER (object)->priv;
461 g_mutex_clear (&priv->gl_resource_lock);
462 g_cond_clear (&priv->gl_resource_cond);
463 G_OBJECT_CLASS (parent_class)->finalize (object);
467 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
469 GstGLMixer *mix = GST_GL_MIXER (element);
471 gst_gl_handle_set_context (element, context, &mix->display);
475 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
477 gboolean result = TRUE;
480 if (!gst_gl_ensure_display (mix, &mix->display))
488 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
492 gboolean result = FALSE;
494 mix = GST_GL_MIXER (aggregator);
497 case GST_PAD_MODE_PUSH:
498 case GST_PAD_MODE_PULL:
499 result = gst_gl_mixer_activate (mix, active);
509 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
511 GstCaps *filter, *caps;
515 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (agg);
517 gst_query_parse_caps (query, &filter);
519 if (GST_VIDEO_INFO_FORMAT (&vagg->info) != GST_VIDEO_FORMAT_UNKNOWN) {
520 caps = gst_video_info_to_caps (&vagg->info);
522 caps = gst_pad_get_pad_template_caps (agg->srcpad);
525 caps = gst_caps_make_writable (caps);
527 n = gst_caps_get_size (caps) - 1;
528 for (; n >= 0; n--) {
529 s = gst_caps_get_structure (caps, n);
530 gst_structure_set (s, "width", GST_TYPE_INT_RANGE, 1, G_MAXINT,
531 "height", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
532 if (GST_VIDEO_INFO_FPS_D (&vagg->info) != 0) {
533 gst_structure_set (s,
534 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
539 caps = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
541 gst_query_set_caps_result (query, caps);
542 gst_caps_unref (caps);
548 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
550 gboolean res = FALSE;
551 GstGLMixer *mix = GST_GL_MIXER (agg);
553 switch (GST_QUERY_TYPE (query)) {
554 case GST_QUERY_CONTEXT:
556 res = gst_gl_handle_context_query ((GstElement *) mix, query,
561 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
564 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
572 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
575 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
577 if (!mix->priv->pool_active) {
578 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
579 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
580 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
581 return GST_FLOW_ERROR;
583 mix->priv->pool_active = TRUE;
586 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
590 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
592 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
593 GstBufferPool *pool = NULL;
594 GstStructure *config;
596 guint min, max, size;
597 gboolean update_pool;
598 GError *error = NULL;
600 guint out_width, out_height;
601 GstGLContext *other_context = NULL;
602 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
604 if (!gst_gl_ensure_display (mix, &mix->display))
607 if (gst_query_find_allocation_meta (query,
608 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
609 GstGLContext *context;
610 const GstStructure *upload_meta_params;
615 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
616 if (upload_meta_params) {
617 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
618 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
619 GstGLContext *old = mix->context;
621 mix->context = context;
623 gst_object_unref (old);
624 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
625 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
626 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
628 GstGLPlatform platform;
631 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
634 platform = gst_gl_platform_from_string (type);
635 gl_apis = gst_gl_api_from_string (apis);
637 if (gl_apis && platform)
639 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
646 mix->context = gst_gl_context_new (mix->display);
647 if (!gst_gl_context_create (mix->context, other_context, &error))
651 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
652 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
654 g_mutex_lock (&mix->priv->gl_resource_lock);
655 mix->priv->gl_resource_ready = FALSE;
657 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
659 mix->depthbuffer = 0;
662 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
663 &mix->fbo, &mix->depthbuffer)) {
664 g_cond_signal (&mix->priv->gl_resource_cond);
665 g_mutex_unlock (&mix->priv->gl_resource_lock);
670 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
671 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
672 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
674 gst_query_parse_allocation (query, &caps, NULL);
676 if (mixer_class->set_caps)
677 mixer_class->set_caps (mix, caps);
679 mix->priv->gl_resource_ready = TRUE;
680 g_cond_signal (&mix->priv->gl_resource_cond);
681 g_mutex_unlock (&mix->priv->gl_resource_lock);
683 if (gst_query_get_n_allocation_pools (query) > 0) {
684 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
690 gst_video_info_init (&vinfo);
691 gst_video_info_from_caps (&vinfo, caps);
698 pool = gst_gl_buffer_pool_new (mix->context);
700 config = gst_buffer_pool_get_config (pool);
701 gst_buffer_pool_config_set_params (config, caps, size, min, max);
703 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
705 gst_buffer_pool_set_config (pool, config);
708 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
710 gst_query_add_allocation_pool (query, pool, size, min, max);
712 gst_object_unref (pool);
718 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
724 /* takes ownership of the pool, allocator and query */
726 gst_gl_mixer_set_allocation (GstGLMixer * mix,
727 GstBufferPool * pool, GstAllocator * allocator,
728 GstAllocationParams * params, GstQuery * query)
730 GstAllocator *oldalloc;
731 GstBufferPool *oldpool;
733 GstGLMixerPrivate *priv = mix->priv;
735 GST_DEBUG ("storing allocation query");
737 GST_OBJECT_LOCK (mix);
738 oldpool = priv->pool;
740 priv->pool_active = FALSE;
742 oldalloc = priv->allocator;
743 priv->allocator = allocator;
745 oldquery = priv->query;
749 priv->params = *params;
751 gst_allocation_params_init (&priv->params);
752 GST_OBJECT_UNLOCK (mix);
755 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
756 gst_buffer_pool_set_active (oldpool, FALSE);
757 gst_object_unref (oldpool);
760 gst_object_unref (oldalloc);
763 gst_query_unref (oldquery);
769 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
772 gboolean result = TRUE;
773 GstBufferPool *pool = NULL;
774 GstAllocator *allocator;
775 GstAllocationParams params;
776 GstAggregator *agg = GST_AGGREGATOR (mix);
778 /* find a pool for the negotiated caps now */
779 GST_DEBUG_OBJECT (mix, "doing allocation query");
780 query = gst_query_new_allocation (outcaps, TRUE);
781 if (!gst_pad_peer_query (agg->srcpad, query)) {
782 /* not a problem, just debug a little */
783 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
786 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
787 result = gst_gl_mixer_decide_allocation (mix, query);
789 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
793 goto no_decide_allocation;
795 /* we got configuration from our peer or the decide_allocation method,
797 if (gst_query_get_n_allocation_params (query) > 0) {
798 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
801 gst_allocation_params_init (¶ms);
804 if (gst_query_get_n_allocation_pools (query) > 0)
805 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
808 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
813 no_decide_allocation:
815 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
816 gst_query_unref (query);
823 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
829 guint array_index = 0;
830 GstVideoFrame out_frame;
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;
835 gboolean to_download =
836 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
837 gst_caps_get_features (mix->out_caps, 0));
838 GstMapFlags out_map_flags = GST_MAP_WRITE;
840 GST_TRACE ("Processing buffers");
842 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
845 out_map_flags |= GST_MAP_GL;
847 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
852 out_tex = *(guint *) out_frame.data[0];
854 GST_INFO ("Output Buffer does not contain correct memory, "
855 "attempting to wrap for download");
858 mix->download = gst_gl_download_new (mix->context);
860 gst_gl_download_set_format (mix->download, &out_frame.info);
861 out_tex = mix->out_tex_id;
864 GST_OBJECT_LOCK (mix);
865 walk = element->sinkpads;
867 i = mix->frames->len;
868 g_ptr_array_set_size (mix->frames, element->numsinkpads);
869 for (; i < element->numsinkpads; i++)
870 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
872 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
873 GstVideoAggregatorPad *vaggpad = walk->data;
874 GstGLMixerFrameData *frame;
876 frame = g_ptr_array_index (mix->frames, array_index);
880 walk = g_list_next (walk);
882 if (vaggpad->buffer != NULL) {
886 pad->upload = gst_gl_upload_new (mix->context);
888 gst_gl_upload_set_format (pad->upload, &vaggpad->info);
891 if (!gst_gl_upload_perform_with_buffer (pad->upload,
892 vaggpad->buffer, &in_tex, NULL)) {
899 frame->texture = in_tex;
904 g_mutex_lock (&priv->gl_resource_lock);
905 if (!priv->gl_resource_ready)
906 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
908 if (!priv->gl_resource_ready) {
909 g_mutex_unlock (&priv->gl_resource_lock);
910 GST_ERROR_OBJECT (mix,
911 "fbo used to render can't be created, do not run process_textures");
916 mix_class->process_textures (mix, mix->frames, out_tex);
918 g_mutex_unlock (&priv->gl_resource_lock);
921 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
923 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
924 "Failed to download video frame"), (NULL));
932 walk = GST_ELEMENT (mix)->sinkpads;
934 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
937 gst_gl_upload_release_buffer (pad->upload);
940 walk = g_list_next (walk);
943 GST_OBJECT_UNLOCK (mix);
945 gst_video_frame_unmap (&out_frame);
951 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
954 guint i, array_index = 0;
955 GstElement *element = GST_ELEMENT (mix);
956 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
958 GST_OBJECT_LOCK (mix);
959 walk = GST_ELEMENT (mix)->sinkpads;
960 i = mix->frames->len;
961 g_ptr_array_set_size (mix->frames, element->numsinkpads);
962 for (; i < element->numsinkpads; i++)
963 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
964 while (walk) { /* We walk with this list because it's ordered */
965 GstVideoAggregatorPad *vaggpad = walk->data;
967 walk = g_list_next (walk);
969 if (vaggpad->buffer != NULL) {
970 /* put buffer into array */
971 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
975 GST_OBJECT_UNLOCK (mix);
977 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
983 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
985 gboolean res = FALSE;
986 GstGLMixer *mix = GST_GL_MIXER (vagg);
987 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
989 if (mix_class->process_buffers)
990 res = gst_gl_mixer_process_buffers (mix, outbuf);
991 else if (mix_class->process_textures)
992 res = gst_gl_mixer_process_textures (mix, outbuf);
994 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
998 gst_gl_mixer_get_property (GObject * object,
999 guint prop_id, GValue * value, GParamSpec * pspec)
1003 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1009 gst_gl_mixer_set_property (GObject * object,
1010 guint prop_id, const GValue * value, GParamSpec * pspec)
1014 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1020 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1022 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1025 gst_object_unref (pad->upload);
1033 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1035 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1039 gst_gl_mixer_start (GstAggregator * agg)
1042 GstGLMixer *mix = GST_GL_MIXER (agg);
1043 GstElement *element = GST_ELEMENT (agg);
1045 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1048 GST_OBJECT_LOCK (mix);
1049 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1050 (GDestroyNotify) _free_glmixer_frame_data);
1051 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1053 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1054 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1056 for (i = 0; i < element->numsinkpads; i++)
1057 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1059 GST_OBJECT_UNLOCK (mix);
1065 gst_gl_mixer_stop (GstAggregator * agg)
1067 GstGLMixer *mix = GST_GL_MIXER (agg);
1068 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1070 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1073 GST_OBJECT_LOCK (agg);
1074 g_ptr_array_free (mix->frames, TRUE);
1076 g_ptr_array_free (mix->array_buffers, TRUE);
1077 mix->array_buffers = NULL;
1078 GST_OBJECT_UNLOCK (agg);
1080 if (mixer_class->reset)
1081 mixer_class->reset (mix);
1083 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1085 mix->depthbuffer = 0;
1087 if (mix->download) {
1088 gst_object_unref (mix->download);
1089 mix->download = NULL;
1092 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1094 if (mix->priv->query) {
1095 gst_query_unref (mix->priv->query);
1096 mix->priv->query = NULL;
1099 if (mix->priv->pool) {
1100 gst_object_unref (mix->priv->pool);
1101 mix->priv->pool = NULL;
1105 gst_object_unref (mix->display);
1106 mix->display = NULL;
1110 gst_object_unref (mix->context);
1111 mix->context = NULL;
1113 gst_gl_mixer_reset (mix);