1 /* Generic video mixer plugin
4 * Copyright (C) 2009 Julien Isorce <julien.isorce@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include <gst/base/gstcollectpads.h>
28 #include <gst/video/video.h>
37 #include "gstglmixer.h"
39 #if GST_GL_HAVE_PLATFORM_EGL
40 #include <gst/gl/egl/gsteglimagememory.h>
43 #define gst_gl_mixer_parent_class parent_class
44 G_DEFINE_ABSTRACT_TYPE (GstGLMixer, gst_gl_mixer, GST_TYPE_VIDEO_AGGREGATOR);
45 static gboolean gst_gl_mixer_do_bufferpool (GstGLMixer * mix,
49 #define GST_CAT_DEFAULT gst_gl_mixer_debug
50 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
52 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
53 GValue * value, GParamSpec * pspec);
54 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
55 const GValue * value, GParamSpec * pspec);
56 static void gst_gl_mixer_pad_finalize (GObject * object);
58 static void gst_gl_mixer_set_context (GstElement * element,
59 GstContext * context);
66 #define GST_GL_MIXER_GET_PRIVATE(obj) \
67 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
69 struct _GstGLMixerPrivate
75 GstAllocator *allocator;
76 GstAllocationParams params;
79 gboolean gl_resource_ready;
80 GMutex gl_resource_lock;
81 GCond gl_resource_cond;
84 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_VIDEO_AGGREGATOR_PAD);
87 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
89 GObjectClass *gobject_class = (GObjectClass *) klass;
91 gobject_class->set_property = gst_gl_mixer_pad_set_property;
92 gobject_class->get_property = gst_gl_mixer_pad_get_property;
94 gobject_class->finalize = gst_gl_mixer_pad_finalize;
98 gst_gl_mixer_pad_finalize (GObject * object)
100 GstGLMixerPad *pad = GST_GL_MIXER_PAD (object);
103 gst_object_unref (pad->upload);
107 G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
111 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec)
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
122 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec)
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
133 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
135 GstGLMixer *mix = GST_GL_MIXER (vagg);
136 gboolean ret = gst_gl_mixer_do_bufferpool (mix, caps);
138 mix->priv->negotiated = ret;
140 gst_caps_replace (&mix->out_caps, caps);
146 gst_gl_mixer_propose_allocation (GstGLMixer * mix,
147 GstQuery * decide_query, GstQuery * query)
150 GstStructure *config;
154 GError *error = NULL;
155 GstStructure *gl_context;
156 gchar *platform, *gl_apis;
158 GstAllocator *allocator = NULL;
159 GstAllocationParams params;
161 gst_query_parse_allocation (query, &caps, &need_pool);
166 if ((pool = mix->priv->pool))
167 gst_object_ref (pool);
172 /* we had a pool, check caps */
173 GST_DEBUG_OBJECT (mix, "check existing pool caps");
174 config = gst_buffer_pool_get_config (pool);
175 gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
177 if (!gst_caps_is_equal (caps, pcaps)) {
178 GST_DEBUG_OBJECT (mix, "pool has different caps");
179 /* different caps, we can't use this pool */
180 gst_object_unref (pool);
183 gst_structure_free (config);
186 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
190 mix->context = gst_gl_context_new (mix->display);
191 if (!gst_gl_context_create (mix->context, mix->other_context, &error))
195 if (pool == NULL && need_pool) {
198 if (!gst_video_info_from_caps (&info, caps))
201 GST_DEBUG_OBJECT (mix, "create new pool");
202 pool = gst_gl_buffer_pool_new (mix->context);
204 /* the normal size of a frame */
207 config = gst_buffer_pool_get_config (pool);
208 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
209 if (!gst_buffer_pool_set_config (pool, config))
214 gst_query_add_allocation_pool (query, pool, size, 1, 0);
215 gst_object_unref (pool);
218 /* we also support various metadata */
219 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);
221 gl_apis = gst_gl_api_to_string (gst_gl_context_get_gl_api (mix->context));
223 gst_gl_platform_to_string (gst_gl_context_get_gl_platform (mix->context));
224 handle = (gpointer) gst_gl_context_get_gl_context (mix->context);
227 gst_structure_new ("GstVideoGLTextureUploadMeta", "gst.gl.GstGLContext",
228 GST_GL_TYPE_CONTEXT, mix->context, "gst.gl.context.handle",
229 G_TYPE_POINTER, handle, "gst.gl.context.type", G_TYPE_STRING, platform,
230 "gst.gl.context.apis", G_TYPE_STRING, gl_apis, NULL);
231 gst_query_add_allocation_meta (query,
232 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, gl_context);
236 gst_structure_free (gl_context);
238 gst_allocation_params_init (¶ms);
240 allocator = gst_allocator_find (GST_GL_MEMORY_ALLOCATOR);
241 gst_query_add_allocation_param (query, allocator, ¶ms);
242 gst_object_unref (allocator);
249 GST_DEBUG_OBJECT (mix, "no caps specified");
254 GST_DEBUG_OBJECT (mix, "invalid caps specified");
259 GST_DEBUG_OBJECT (mix, "failed setting config");
264 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
271 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
275 GstCaps *template_caps;
277 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
279 template_caps = gst_pad_get_pad_template_caps (pad);
280 template_caps = gst_caps_make_writable (template_caps);
282 ret = gst_caps_can_intersect (caps, template_caps);
283 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
284 (ret ? "" : "not "), caps);
285 gst_caps_unref (template_caps);
291 gst_gl_mixer_set_caps_features (const GstCaps * caps,
292 const gchar * feature_name)
294 GstCaps *tmp = gst_caps_copy (caps);
295 guint n = gst_caps_get_size (tmp);
298 for (i = 0; i < n; i++) {
299 GstCapsFeatures *features = gst_caps_get_features (tmp, i);
301 guint n_f = gst_caps_features_get_size (features);
303 for (j = 0; j < n_f; j++) {
304 gst_caps_features_remove_id (features,
305 gst_caps_features_get_nth_id (features, j));
309 gst_caps_features_add (features, feature_name);
310 gst_caps_set_simple (tmp, "format", G_TYPE_STRING, "RGBA", NULL);
316 /* copies the given caps */
318 gst_gl_mixer_caps_remove_format_info (GstCaps * caps)
325 res = gst_caps_new_empty ();
327 n = gst_caps_get_size (caps);
328 for (i = 0; i < n; i++) {
329 st = gst_caps_get_structure (caps, i);
330 f = gst_caps_get_features (caps, i);
332 /* If this is already expressed by the existing caps
333 * skip this structure */
334 if (i > 0 && gst_caps_is_subset_structure_full (res, st, f))
337 st = gst_structure_copy (st);
338 /* Only remove format info for the cases when we can actually convert */
339 if (!gst_caps_features_is_any (f)
340 && gst_caps_features_is_equal (f,
341 GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY))
342 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site",
343 "width", "height", NULL);
345 gst_caps_append_structure_full (res, st, gst_caps_features_copy (f));
352 gst_gl_mixer_update_caps (GstGLMixer * mix, GstCaps * caps)
354 GstCaps *result = NULL;
355 GstCaps *glcaps = gst_gl_mixer_set_caps_features (caps,
356 GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
357 #if GST_GL_HAVE_PLATFORM_EGL
358 GstCaps *eglcaps = gst_gl_mixer_set_caps_features (caps,
359 GST_CAPS_FEATURE_MEMORY_EGL_IMAGE);
361 GstCaps *uploadcaps = gst_gl_mixer_set_caps_features (caps,
362 GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META);
364 gst_caps_from_string (GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS));
366 result = gst_caps_new_empty ();
368 result = gst_caps_merge (result, glcaps);
369 #if GST_GL_HAVE_PLATFORM_EGL
370 result = gst_caps_merge (result, eglcaps);
372 result = gst_caps_merge (result, uploadcaps);
373 result = gst_caps_merge (result, raw_caps);
375 result = gst_caps_merge (result, gst_gl_mixer_caps_remove_format_info (caps));
377 GST_DEBUG_OBJECT (mix, "returning %" GST_PTR_FORMAT, result);
383 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
386 GstCaps *template_caps;
387 GstCaps *filtered_caps;
388 GstCaps *returned_caps;
389 gboolean had_current_caps = TRUE;
391 template_caps = gst_pad_get_pad_template_caps (pad);
393 srccaps = gst_pad_get_current_caps (pad);
394 if (srccaps == NULL) {
395 had_current_caps = FALSE;
396 srccaps = template_caps;
398 srccaps = gst_caps_merge (srccaps, gst_gl_mixer_update_caps (mix, srccaps));
401 filtered_caps = srccaps;
403 filtered_caps = gst_caps_intersect (srccaps, filter);
404 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
407 gst_caps_unref (filtered_caps);
408 if (had_current_caps)
409 gst_caps_unref (template_caps);
411 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
413 return returned_caps;
417 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
420 gboolean ret = FALSE;
421 GstGLMixer *mix = GST_GL_MIXER (agg);
423 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
425 switch (GST_QUERY_TYPE (query)) {
428 GstCaps *filter, *caps;
430 gst_query_parse_caps (query, &filter);
431 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
432 gst_query_set_caps_result (query, caps);
433 gst_caps_unref (caps);
437 case GST_QUERY_ACCEPT_CAPS:
441 gst_query_parse_accept_caps (query, &caps);
442 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
443 gst_query_set_accept_caps_result (query, ret);
447 case GST_QUERY_ALLOCATION:
449 GstQuery *decide_query = NULL;
451 GST_OBJECT_LOCK (mix);
452 if (G_UNLIKELY (!mix->priv->negotiated)) {
453 GST_DEBUG_OBJECT (mix,
454 "not negotiated yet, can't answer ALLOCATION query");
455 GST_OBJECT_UNLOCK (mix);
458 if ((decide_query = mix->priv->query))
459 gst_query_ref (decide_query);
460 GST_OBJECT_UNLOCK (mix);
462 GST_DEBUG_OBJECT (mix,
463 "calling propose allocation with query %" GST_PTR_FORMAT,
466 /* pass the query to the propose_allocation vmethod if any */
467 ret = gst_gl_mixer_propose_allocation (mix, decide_query, query);
470 gst_query_unref (decide_query);
472 GST_DEBUG_OBJECT (mix, "ALLOCATION ret %d, %" GST_PTR_FORMAT, ret, query);
475 case GST_QUERY_CONTEXT:
477 ret = gst_gl_handle_context_query ((GstElement *) mix, query,
478 &mix->display, &mix->other_context);
482 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
490 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
494 /* GLMixer signals and args */
506 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
509 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
510 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
512 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
513 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
515 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
518 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
521 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
522 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
524 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
525 (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
527 "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
530 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
532 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
533 GstBuffer ** outbuf);
535 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
537 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
538 static gboolean gst_gl_mixer_start (GstAggregator * agg);
541 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
542 GstBuffer * outbuffer);
544 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
545 const GValue * value, GParamSpec * pspec);
546 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
547 GValue * value, GParamSpec * pspec);
549 static gboolean gst_gl_mixer_decide_allocation (GstGLMixer * mix,
551 static gboolean gst_gl_mixer_set_allocation (GstGLMixer * mix,
552 GstBufferPool * pool, GstAllocator * allocator,
553 GstAllocationParams * params, GstQuery * query);
555 static void gst_gl_mixer_finalize (GObject * object);
558 gst_gl_mixer_class_init (GstGLMixerClass * klass)
560 GObjectClass *gobject_class;
561 GstElementClass *element_class;
563 GstVideoAggregatorClass *videoaggregator_class =
564 (GstVideoAggregatorClass *) klass;
565 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
567 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
569 gobject_class = (GObjectClass *) klass;
570 element_class = GST_ELEMENT_CLASS (klass);
572 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
574 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
576 gobject_class->get_property = gst_gl_mixer_get_property;
577 gobject_class->set_property = gst_gl_mixer_set_property;
579 gst_element_class_add_pad_template (element_class,
580 gst_static_pad_template_get (&src_factory));
581 gst_element_class_add_pad_template (element_class,
582 gst_static_pad_template_get (&sink_factory));
584 element_class->set_context = GST_DEBUG_FUNCPTR (gst_gl_mixer_set_context);
586 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
587 agg_class->sink_query = gst_gl_mixer_sink_query;
588 agg_class->src_query = gst_gl_mixer_src_query;
589 agg_class->src_activate = gst_gl_mixer_src_activate_mode;
590 agg_class->stop = gst_gl_mixer_stop;
591 agg_class->start = gst_gl_mixer_start;
593 videoaggregator_class->disable_frame_conversion = TRUE;
594 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
595 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
596 videoaggregator_class->negotiated_caps = _negotiated_caps;
599 /* Register the pad class */
600 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
602 klass->set_caps = NULL;
607 gst_gl_mixer_reset (GstGLMixer * mix)
609 /* clean up collect data */
610 mix->priv->negotiated = FALSE;
614 gst_gl_mixer_init (GstGLMixer * mix)
616 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
617 mix->array_buffers = 0;
620 mix->depthbuffer = 0;
622 mix->priv->gl_resource_ready = FALSE;
623 g_mutex_init (&mix->priv->gl_resource_lock);
624 g_cond_init (&mix->priv->gl_resource_cond);
625 /* initialize variables */
626 gst_gl_mixer_reset (mix);
630 gst_gl_mixer_finalize (GObject * object)
632 GstGLMixer *mix = GST_GL_MIXER (object);
633 GstGLMixerPrivate *priv = mix->priv;
635 if (mix->other_context) {
636 gst_object_unref (mix->other_context);
637 mix->other_context = NULL;
640 g_mutex_clear (&priv->gl_resource_lock);
641 g_cond_clear (&priv->gl_resource_cond);
642 G_OBJECT_CLASS (parent_class)->finalize (object);
646 gst_gl_mixer_set_context (GstElement * element, GstContext * context)
648 GstGLMixer *mix = GST_GL_MIXER (element);
650 gst_gl_handle_set_context (element, context, &mix->display,
651 &mix->other_context);
655 gst_gl_mixer_activate (GstGLMixer * mix, gboolean active)
657 gboolean result = TRUE;
660 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
668 gst_gl_mixer_src_activate_mode (GstAggregator * aggregator, GstPadMode mode,
672 gboolean result = FALSE;
674 mix = GST_GL_MIXER (aggregator);
677 case GST_PAD_MODE_PUSH:
678 case GST_PAD_MODE_PULL:
679 result = gst_gl_mixer_activate (mix, active);
689 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
691 GstCaps *filter, *current_caps, *retcaps;
693 gst_query_parse_caps (query, &filter);
695 current_caps = gst_pad_get_current_caps (pad);
696 if (current_caps == NULL)
697 current_caps = gst_pad_get_pad_template_caps (agg->srcpad);
699 retcaps = gst_gl_mixer_caps_remove_format_info (current_caps);
700 gst_caps_unref (current_caps);
704 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
706 gst_query_set_caps_result (query, retcaps);
707 gst_caps_unref (retcaps);
713 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
715 gboolean res = FALSE;
716 GstGLMixer *mix = GST_GL_MIXER (agg);
718 switch (GST_QUERY_TYPE (query)) {
719 case GST_QUERY_CONTEXT:
721 res = gst_gl_handle_context_query ((GstElement *) mix, query,
722 &mix->display, &mix->other_context);
726 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
729 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
737 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
740 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
742 if (!mix->priv->pool_active) {
743 if (!gst_buffer_pool_set_active (mix->priv->pool, TRUE)) {
744 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
745 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
746 return GST_FLOW_ERROR;
748 mix->priv->pool_active = TRUE;
751 return gst_buffer_pool_acquire_buffer (mix->priv->pool, outbuf, NULL);
755 gst_gl_mixer_decide_allocation (GstGLMixer * mix, GstQuery * query)
757 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
758 GstBufferPool *pool = NULL;
759 GstStructure *config;
761 guint min, max, size;
762 gboolean update_pool;
763 GError *error = NULL;
765 guint out_width, out_height;
766 GstGLContext *other_context = NULL;
767 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
769 if (!gst_gl_ensure_element_data (mix, &mix->display, &mix->other_context))
772 if (gst_query_find_allocation_meta (query,
773 GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
774 GstGLContext *context;
775 const GstStructure *upload_meta_params;
780 gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
781 if (upload_meta_params) {
782 if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
783 GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
784 GstGLContext *old = mix->context;
786 mix->context = context;
788 gst_object_unref (old);
789 } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
790 G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
791 &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
793 GstGLPlatform platform;
796 GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
799 platform = gst_gl_platform_from_string (type);
800 gl_apis = gst_gl_api_from_string (apis);
802 if (gl_apis && platform)
804 gst_gl_context_new_wrapped (mix->display, (guintptr) handle,
810 if (mix->other_context) {
811 if (!other_context) {
812 other_context = mix->other_context;
814 GST_ELEMENT_WARNING (mix, LIBRARY, SETTINGS,
815 ("%s", "Cannot share with more than one GL context"),
816 ("%s", "Cannot share with more than one GL context"));
821 mix->context = gst_gl_context_new (mix->display);
822 if (!gst_gl_context_create (mix->context, other_context, &error))
826 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
827 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
829 g_mutex_lock (&mix->priv->gl_resource_lock);
830 mix->priv->gl_resource_ready = FALSE;
832 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
834 mix->depthbuffer = 0;
837 if (!gst_gl_context_gen_fbo (mix->context, out_width, out_height,
838 &mix->fbo, &mix->depthbuffer)) {
839 g_cond_signal (&mix->priv->gl_resource_cond);
840 g_mutex_unlock (&mix->priv->gl_resource_lock);
845 gst_gl_context_del_texture (mix->context, &mix->out_tex_id);
846 gst_gl_context_gen_texture (mix->context, &mix->out_tex_id,
847 GST_VIDEO_FORMAT_RGBA, out_width, out_height);
849 gst_query_parse_allocation (query, &caps, NULL);
851 if (mixer_class->set_caps)
852 mixer_class->set_caps (mix, caps);
854 mix->priv->gl_resource_ready = TRUE;
855 g_cond_signal (&mix->priv->gl_resource_cond);
856 g_mutex_unlock (&mix->priv->gl_resource_lock);
858 if (gst_query_get_n_allocation_pools (query) > 0) {
859 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
865 gst_video_info_init (&vinfo);
866 gst_video_info_from_caps (&vinfo, caps);
873 pool = gst_gl_buffer_pool_new (mix->context);
875 config = gst_buffer_pool_get_config (pool);
876 gst_buffer_pool_config_set_params (config, caps, size, min, max);
878 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
879 gst_buffer_pool_config_add_option (config,
880 GST_BUFFER_POOL_OPTION_VIDEO_GL_TEXTURE_UPLOAD_META);
882 gst_buffer_pool_set_config (pool, config);
885 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
887 gst_query_add_allocation_pool (query, pool, size, min, max);
889 gst_object_unref (pool);
895 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
901 /* takes ownership of the pool, allocator and query */
903 gst_gl_mixer_set_allocation (GstGLMixer * mix,
904 GstBufferPool * pool, GstAllocator * allocator,
905 GstAllocationParams * params, GstQuery * query)
907 GstAllocator *oldalloc;
908 GstBufferPool *oldpool;
910 GstGLMixerPrivate *priv = mix->priv;
912 GST_DEBUG ("storing allocation query");
914 GST_OBJECT_LOCK (mix);
915 oldpool = priv->pool;
917 priv->pool_active = FALSE;
919 oldalloc = priv->allocator;
920 priv->allocator = allocator;
922 oldquery = priv->query;
926 priv->params = *params;
928 gst_allocation_params_init (&priv->params);
929 GST_OBJECT_UNLOCK (mix);
932 GST_DEBUG_OBJECT (mix, "deactivating old pool %p", oldpool);
933 gst_buffer_pool_set_active (oldpool, FALSE);
934 gst_object_unref (oldpool);
937 gst_object_unref (oldalloc);
940 gst_query_unref (oldquery);
946 gst_gl_mixer_do_bufferpool (GstGLMixer * mix, GstCaps * outcaps)
949 gboolean result = TRUE;
950 GstBufferPool *pool = NULL;
951 GstAllocator *allocator;
952 GstAllocationParams params;
953 GstAggregator *agg = GST_AGGREGATOR (mix);
955 /* find a pool for the negotiated caps now */
956 GST_DEBUG_OBJECT (mix, "doing allocation query");
957 query = gst_query_new_allocation (outcaps, TRUE);
958 if (!gst_pad_peer_query (agg->srcpad, query)) {
959 /* not a problem, just debug a little */
960 GST_DEBUG_OBJECT (mix, "peer ALLOCATION query failed");
963 GST_DEBUG_OBJECT (mix, "calling decide_allocation");
964 result = gst_gl_mixer_decide_allocation (mix, query);
966 GST_DEBUG_OBJECT (mix, "ALLOCATION (%d) params: %" GST_PTR_FORMAT, result,
970 goto no_decide_allocation;
972 /* we got configuration from our peer or the decide_allocation method,
974 if (gst_query_get_n_allocation_params (query) > 0) {
975 gst_query_parse_nth_allocation_param (query, 0, &allocator, ¶ms);
978 gst_allocation_params_init (¶ms);
981 if (gst_query_get_n_allocation_pools (query) > 0)
982 gst_query_parse_nth_allocation_pool (query, 0, &pool, NULL, NULL, NULL);
985 result = gst_gl_mixer_set_allocation (mix, pool, allocator, ¶ms, query);
990 no_decide_allocation:
992 GST_WARNING_OBJECT (mix, "Failed to decide allocation");
993 gst_query_unref (query);
1000 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
1005 gboolean res = TRUE;
1006 guint array_index = 0;
1007 GstVideoFrame out_frame;
1008 GstElement *element = GST_ELEMENT (mix);
1009 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
1010 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1011 GstGLMixerPrivate *priv = mix->priv;
1012 gboolean to_download =
1013 gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
1014 gst_caps_get_features (mix->out_caps, 0));
1015 GstMapFlags out_map_flags = GST_MAP_WRITE;
1017 GST_TRACE ("Processing buffers");
1019 to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (outbuf, 0));
1022 out_map_flags |= GST_MAP_GL;
1024 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf, out_map_flags)) {
1029 out_tex = *(guint *) out_frame.data[0];
1031 GST_INFO ("Output Buffer does not contain correct memory, "
1032 "attempting to wrap for download");
1035 mix->download = gst_gl_download_new (mix->context);
1037 gst_gl_download_set_format (mix->download, &out_frame.info);
1038 out_tex = mix->out_tex_id;
1041 GST_OBJECT_LOCK (mix);
1042 walk = element->sinkpads;
1044 i = mix->frames->len;
1045 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1046 for (; i < element->numsinkpads; i++)
1047 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1049 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1050 GstVideoAggregatorPad *vaggpad = walk->data;
1051 GstGLMixerFrameData *frame;
1053 frame = g_ptr_array_index (mix->frames, array_index);
1057 walk = g_list_next (walk);
1059 if (vaggpad->buffer != NULL) {
1063 pad->upload = gst_gl_upload_new (mix->context);
1065 gst_gl_upload_set_format (pad->upload, &vaggpad->info);
1068 if (!gst_gl_upload_perform_with_buffer (pad->upload,
1069 vaggpad->buffer, &in_tex, NULL)) {
1071 pad->mapped = FALSE;
1076 frame->texture = in_tex;
1081 g_mutex_lock (&priv->gl_resource_lock);
1082 if (!priv->gl_resource_ready)
1083 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
1085 if (!priv->gl_resource_ready) {
1086 g_mutex_unlock (&priv->gl_resource_lock);
1087 GST_ERROR_OBJECT (mix,
1088 "fbo used to render can't be created, do not run process_textures");
1093 mix_class->process_textures (mix, mix->frames, out_tex);
1095 g_mutex_unlock (&priv->gl_resource_lock);
1098 if (!gst_gl_download_perform_with_data (mix->download, out_tex,
1100 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s",
1101 "Failed to download video frame"), (NULL));
1109 walk = GST_ELEMENT (mix)->sinkpads;
1111 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
1114 gst_gl_upload_release_buffer (pad->upload);
1116 pad->mapped = FALSE;
1117 walk = g_list_next (walk);
1120 GST_OBJECT_UNLOCK (mix);
1122 gst_video_frame_unmap (&out_frame);
1128 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
1131 guint i, array_index = 0;
1132 GstElement *element = GST_ELEMENT (mix);
1133 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
1135 GST_OBJECT_LOCK (mix);
1136 walk = GST_ELEMENT (mix)->sinkpads;
1137 i = mix->frames->len;
1138 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1139 for (; i < element->numsinkpads; i++)
1140 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1141 while (walk) { /* We walk with this list because it's ordered */
1142 GstVideoAggregatorPad *vaggpad = walk->data;
1144 walk = g_list_next (walk);
1146 if (vaggpad->buffer != NULL) {
1147 /* put buffer into array */
1148 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
1152 GST_OBJECT_UNLOCK (mix);
1154 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
1159 static GstFlowReturn
1160 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
1162 gboolean res = FALSE;
1163 GstGLMixer *mix = GST_GL_MIXER (vagg);
1164 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
1166 if (mix_class->process_buffers)
1167 res = gst_gl_mixer_process_buffers (mix, outbuf);
1168 else if (mix_class->process_textures)
1169 res = gst_gl_mixer_process_textures (mix, outbuf);
1171 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
1175 gst_gl_mixer_get_property (GObject * object,
1176 guint prop_id, GValue * value, GParamSpec * pspec)
1180 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1186 gst_gl_mixer_set_property (GObject * object,
1187 guint prop_id, const GValue * value, GParamSpec * pspec)
1191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1197 _clean_upload (GstAggregator * agg, GstPad * aggpad, gpointer udata)
1199 GstGLMixerPad *pad = GST_GL_MIXER_PAD (aggpad);
1202 gst_object_unref (pad->upload);
1210 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
1212 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
1216 gst_gl_mixer_start (GstAggregator * agg)
1219 GstGLMixer *mix = GST_GL_MIXER (agg);
1220 GstElement *element = GST_ELEMENT (agg);
1222 if (!GST_AGGREGATOR_CLASS (parent_class)->start (agg))
1225 GST_OBJECT_LOCK (mix);
1226 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
1227 (GDestroyNotify) _free_glmixer_frame_data);
1228 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
1230 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
1231 g_ptr_array_set_size (mix->frames, element->numsinkpads);
1233 for (i = 0; i < element->numsinkpads; i++)
1234 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
1236 GST_OBJECT_UNLOCK (mix);
1242 gst_gl_mixer_stop (GstAggregator * agg)
1244 GstGLMixer *mix = GST_GL_MIXER (agg);
1245 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
1247 if (!GST_AGGREGATOR_CLASS (parent_class)->stop (agg))
1250 GST_OBJECT_LOCK (agg);
1251 g_ptr_array_free (mix->frames, TRUE);
1253 g_ptr_array_free (mix->array_buffers, TRUE);
1254 mix->array_buffers = NULL;
1255 GST_OBJECT_UNLOCK (agg);
1257 if (mixer_class->reset)
1258 mixer_class->reset (mix);
1260 gst_gl_context_del_fbo (mix->context, mix->fbo, mix->depthbuffer);
1262 mix->depthbuffer = 0;
1264 if (mix->download) {
1265 gst_object_unref (mix->download);
1266 mix->download = NULL;
1269 gst_aggregator_iterate_sinkpads (GST_AGGREGATOR (mix), _clean_upload, NULL);
1271 if (mix->priv->query) {
1272 gst_query_unref (mix->priv->query);
1273 mix->priv->query = NULL;
1276 if (mix->priv->pool) {
1277 gst_object_unref (mix->priv->pool);
1278 mix->priv->pool = NULL;
1282 gst_object_unref (mix->display);
1283 mix->display = NULL;
1287 gst_object_unref (mix->context);
1288 mix->context = NULL;
1291 gst_gl_mixer_reset (mix);