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/video/video.h>
29 #include "gstglmixer.h"
31 #if GST_GL_HAVE_PLATFORM_EGL
32 #include <gst/gl/egl/gsteglimagememory.h>
35 #define gst_gl_mixer_parent_class parent_class
36 G_DEFINE_ABSTRACT_TYPE (GstGLMixer, gst_gl_mixer, GST_TYPE_GL_BASE_MIXER);
38 #define GST_CAT_DEFAULT gst_gl_mixer_debug
39 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
41 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
42 GValue * value, GParamSpec * pspec);
43 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
44 const GValue * value, GParamSpec * pspec);
45 static void gst_gl_mixer_pad_finalize (GObject * object);
52 #define GST_GL_MIXER_GET_PRIVATE(obj) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_GL_MIXER, GstGLMixerPrivate))
55 struct _GstGLMixerPrivate
59 gboolean gl_resource_ready;
60 GMutex gl_resource_lock;
61 GCond gl_resource_cond;
64 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_GL_BASE_MIXER_PAD);
67 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
69 GObjectClass *gobject_class = (GObjectClass *) klass;
70 GstVideoAggregatorPadClass *vaggpad_class =
71 (GstVideoAggregatorPadClass *) klass;
73 gobject_class->set_property = gst_gl_mixer_pad_set_property;
74 gobject_class->get_property = gst_gl_mixer_pad_get_property;
76 gobject_class->finalize = gst_gl_mixer_pad_finalize;
78 vaggpad_class->set_info = NULL;
79 vaggpad_class->prepare_frame = NULL;
80 vaggpad_class->clean_frame = NULL;
84 gst_gl_mixer_pad_finalize (GObject * object)
86 G_OBJECT_CLASS (gst_gl_mixer_pad_parent_class)->finalize (object);
90 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec)
95 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
101 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec)
106 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 _negotiated_caps (GstVideoAggregator * vagg, GstCaps * caps)
114 GstGLMixer *mix = GST_GL_MIXER (vagg);
117 mix->priv->negotiated = TRUE;
119 gst_caps_replace (&mix->out_caps, caps);
121 ret = GST_VIDEO_AGGREGATOR_CLASS (parent_class)->negotiated_caps (vagg, caps);
127 gst_gl_mixer_propose_allocation (GstGLBaseMixer * base_mix,
128 GstGLBaseMixerPad * base_pad, GstQuery * decide_query, GstQuery * query)
130 GstGLMixer *mix = GST_GL_MIXER (base_mix);
131 GstGLContext *context = base_mix->context;
132 GstBufferPool *pool = NULL;
133 GstStructure *config;
138 gst_query_parse_allocation (query, &caps, &need_pool);
146 if (!gst_video_info_from_caps (&info, caps))
149 GST_DEBUG_OBJECT (mix, "create new pool");
150 pool = gst_gl_buffer_pool_new (context);
152 /* the normal size of a frame */
155 config = gst_buffer_pool_get_config (pool);
156 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
157 if (!gst_buffer_pool_set_config (pool, config))
162 gst_query_add_allocation_pool (query, pool, size, 1, 0);
163 gst_object_unref (pool);
166 /* we also support various metadata */
167 if (context->gl_vtable->FenceSync)
168 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
175 GST_DEBUG_OBJECT (mix, "no caps specified");
180 GST_DEBUG_OBJECT (mix, "invalid caps specified");
185 GST_DEBUG_OBJECT (mix, "failed setting config");
191 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
195 GstCaps *template_caps;
197 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
199 template_caps = gst_pad_get_pad_template_caps (pad);
200 template_caps = gst_caps_make_writable (template_caps);
202 ret = gst_caps_can_intersect (caps, template_caps);
203 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
204 (ret ? "" : "not "), caps);
205 gst_caps_unref (template_caps);
210 /* copies the given caps */
212 _update_caps (GstVideoAggregator * vagg, GstCaps * caps)
214 return gst_gl_caps_replace_all_caps_features (caps,
215 GST_CAPS_FEATURE_MEMORY_GL_MEMORY);
219 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
222 GstCaps *template_caps;
223 GstCaps *filtered_caps;
224 GstCaps *returned_caps;
225 gboolean had_current_caps = TRUE;
227 template_caps = gst_pad_get_pad_template_caps (pad);
229 sinkcaps = gst_pad_get_current_caps (pad);
230 if (sinkcaps == NULL) {
231 had_current_caps = FALSE;
232 sinkcaps = template_caps;
234 sinkcaps = gst_caps_merge (sinkcaps, template_caps);
237 filtered_caps = sinkcaps;
239 filtered_caps = gst_caps_intersect (sinkcaps, filter);
240 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
243 gst_caps_unref (filtered_caps);
244 if (had_current_caps)
245 gst_caps_unref (template_caps);
247 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
249 return returned_caps;
253 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
256 gboolean ret = FALSE;
257 GstGLMixer *mix = GST_GL_MIXER (agg);
259 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
261 switch (GST_QUERY_TYPE (query)) {
264 GstCaps *filter, *caps;
266 gst_query_parse_caps (query, &filter);
267 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
268 gst_query_set_caps_result (query, caps);
269 gst_caps_unref (caps);
273 case GST_QUERY_ACCEPT_CAPS:
277 gst_query_parse_accept_caps (query, &caps);
278 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
279 gst_query_set_accept_caps_result (query, ret);
284 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
292 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
296 /* GLMixer signals and args */
308 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
311 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
312 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
316 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
319 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
320 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
324 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
325 static GstFlowReturn gst_gl_mixer_get_output_buffer (GstVideoAggregator *
326 videoaggregator, GstBuffer ** outbuf);
327 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
328 static gboolean gst_gl_mixer_start (GstAggregator * agg);
331 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
332 GstBuffer * outbuffer);
334 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
335 const GValue * value, GParamSpec * pspec);
336 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
337 GValue * value, GParamSpec * pspec);
339 static gboolean gst_gl_mixer_decide_allocation (GstGLBaseMixer * mix,
342 static void gst_gl_mixer_finalize (GObject * object);
345 gst_gl_mixer_class_init (GstGLMixerClass * klass)
347 GObjectClass *gobject_class = (GObjectClass *) klass;
348 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
349 GstVideoAggregatorClass *videoaggregator_class =
350 (GstVideoAggregatorClass *) klass;
351 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
352 GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_CLASS (klass);;
354 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "OpenGL mixer");
356 g_type_class_add_private (klass, sizeof (GstGLMixerPrivate));
358 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
360 gobject_class->get_property = gst_gl_mixer_get_property;
361 gobject_class->set_property = gst_gl_mixer_set_property;
363 gst_element_class_add_pad_template (element_class,
364 gst_static_pad_template_get (&src_factory));
365 gst_element_class_add_pad_template (element_class,
366 gst_static_pad_template_get (&sink_factory));
368 agg_class->sinkpads_type = GST_TYPE_GL_MIXER_PAD;
369 agg_class->sink_query = gst_gl_mixer_sink_query;
370 agg_class->src_query = gst_gl_mixer_src_query;
371 agg_class->stop = gst_gl_mixer_stop;
372 agg_class->start = gst_gl_mixer_start;
374 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
375 videoaggregator_class->get_output_buffer = gst_gl_mixer_get_output_buffer;
376 videoaggregator_class->negotiated_caps = _negotiated_caps;
377 videoaggregator_class->update_caps = _update_caps;
378 videoaggregator_class->find_best_format = NULL;
380 mix_class->propose_allocation = gst_gl_mixer_propose_allocation;
381 mix_class->decide_allocation = gst_gl_mixer_decide_allocation;
383 /* Register the pad class */
384 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
386 klass->set_caps = NULL;
390 gst_gl_mixer_reset (GstGLMixer * mix)
392 mix->priv->negotiated = FALSE;
396 gst_gl_mixer_init (GstGLMixer * mix)
398 mix->priv = GST_GL_MIXER_GET_PRIVATE (mix);
399 mix->array_buffers = 0;
401 mix->depthbuffer = 0;
403 mix->priv->gl_resource_ready = FALSE;
404 g_mutex_init (&mix->priv->gl_resource_lock);
405 g_cond_init (&mix->priv->gl_resource_cond);
406 /* initialize variables */
407 gst_gl_mixer_reset (mix);
411 gst_gl_mixer_finalize (GObject * object)
413 GstGLMixer *mix = GST_GL_MIXER (object);
414 GstGLMixerPrivate *priv = mix->priv;
416 g_mutex_clear (&priv->gl_resource_lock);
417 g_cond_clear (&priv->gl_resource_cond);
418 G_OBJECT_CLASS (parent_class)->finalize (object);
422 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
424 GstCaps *filter, *current_caps, *retcaps, *template_caps;
426 gst_query_parse_caps (query, &filter);
428 template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
430 current_caps = gst_pad_get_current_caps (pad);
431 if (current_caps == NULL)
432 retcaps = gst_caps_ref (template_caps);
434 retcaps = gst_caps_merge (current_caps, template_caps);
435 template_caps = NULL;
440 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
441 gst_caps_unref (retcaps);
442 retcaps = current_caps;
445 gst_query_set_caps_result (query, retcaps);
446 gst_caps_unref (retcaps);
449 gst_caps_unref (template_caps);
455 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
457 gboolean res = FALSE;
459 switch (GST_QUERY_TYPE (query)) {
461 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
464 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
472 gst_gl_mixer_get_output_buffer (GstVideoAggregator * videoaggregator,
475 GstGLMixer *mix = GST_GL_MIXER (videoaggregator);
480 gst_gl_base_mixer_get_buffer_pool (GST_GL_BASE_MIXER (videoaggregator));
483 return GST_FLOW_NOT_NEGOTIATED;
485 if (!gst_buffer_pool_is_active (pool)) {
486 if (!gst_buffer_pool_set_active (pool, TRUE)) {
487 GST_ELEMENT_ERROR (mix, RESOURCE, SETTINGS,
488 ("failed to activate bufferpool"), ("failed to activate bufferpool"));
489 return GST_FLOW_ERROR;
493 ret = gst_buffer_pool_acquire_buffer (pool, outbuf, NULL);
494 gst_object_unref (pool);
500 gst_gl_mixer_decide_allocation (GstGLBaseMixer * base_mix, GstQuery * query)
502 GstGLMixer *mix = GST_GL_MIXER (base_mix);
503 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
504 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
505 GstGLContext *context = base_mix->context;
506 GstBufferPool *pool = NULL;
507 GstStructure *config;
509 guint min, max, size;
510 gboolean update_pool;
511 GError *error = NULL;
512 guint out_width, out_height;
514 out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
515 out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
517 g_mutex_lock (&mix->priv->gl_resource_lock);
518 mix->priv->gl_resource_ready = FALSE;
520 gst_gl_context_del_fbo (context, mix->fbo, mix->depthbuffer);
522 mix->depthbuffer = 0;
525 if (!gst_gl_context_gen_fbo (context, out_width, out_height,
526 &mix->fbo, &mix->depthbuffer)) {
527 g_cond_signal (&mix->priv->gl_resource_cond);
528 g_mutex_unlock (&mix->priv->gl_resource_lock);
532 gst_query_parse_allocation (query, &caps, NULL);
534 if (mixer_class->set_caps)
535 mixer_class->set_caps (mix, caps);
537 mix->priv->gl_resource_ready = TRUE;
538 g_cond_signal (&mix->priv->gl_resource_cond);
539 g_mutex_unlock (&mix->priv->gl_resource_lock);
541 if (gst_query_get_n_allocation_pools (query) > 0) {
542 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
548 gst_video_info_init (&vinfo);
549 gst_video_info_from_caps (&vinfo, caps);
556 pool = gst_gl_buffer_pool_new (context);
557 config = gst_buffer_pool_get_config (pool);
559 gst_buffer_pool_config_set_params (config, caps, size, min, max);
560 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
562 gst_buffer_pool_set_config (pool, config);
565 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
567 gst_query_add_allocation_pool (query, pool, size, min, max);
569 gst_object_unref (pool);
575 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
582 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
588 guint array_index = 0;
589 GstVideoFrame out_frame;
590 GstElement *element = GST_ELEMENT (mix);
591 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
592 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
593 GstGLMixerPrivate *priv = mix->priv;
595 GST_TRACE ("Processing buffers");
597 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
598 GST_MAP_WRITE | GST_MAP_GL)) {
602 out_tex = *(guint *) out_frame.data[0];
604 GST_OBJECT_LOCK (mix);
605 walk = element->sinkpads;
607 i = mix->frames->len;
608 g_ptr_array_set_size (mix->frames, element->numsinkpads);
609 for (; i < element->numsinkpads; i++)
610 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
612 GstGLMixerPad *pad = GST_GL_MIXER_PAD (walk->data);
613 GstVideoAggregatorPad *vaggpad = walk->data;
614 GstGLMixerFrameData *frame;
616 frame = g_ptr_array_index (mix->frames, array_index);
620 walk = g_list_next (walk);
622 if (vaggpad->buffer != NULL) {
623 GstVideoInfo gl_info;
624 GstVideoFrame gl_frame;
625 GstGLSyncMeta *sync_meta;
627 gst_video_info_set_format (&gl_info,
628 GST_VIDEO_FORMAT_RGBA,
629 GST_VIDEO_INFO_WIDTH (&vaggpad->info),
630 GST_VIDEO_INFO_HEIGHT (&vaggpad->info));
632 sync_meta = gst_buffer_get_gl_sync_meta (vaggpad->buffer);
634 gst_gl_sync_meta_wait (sync_meta);
636 if (gst_video_frame_map (&gl_frame, &gl_info, vaggpad->buffer,
637 GST_MAP_READ | GST_MAP_GL)) {
638 frame->texture = *(guint *) gl_frame.data[0];
639 gst_video_frame_unmap (&gl_frame);
646 g_mutex_lock (&priv->gl_resource_lock);
647 if (!priv->gl_resource_ready)
648 g_cond_wait (&priv->gl_resource_cond, &priv->gl_resource_lock);
650 if (!priv->gl_resource_ready) {
651 g_mutex_unlock (&priv->gl_resource_lock);
652 GST_ERROR_OBJECT (mix,
653 "fbo used to render can't be created, do not run process_textures");
658 mix_class->process_textures (mix, mix->frames, out_tex);
660 g_mutex_unlock (&priv->gl_resource_lock);
663 GST_OBJECT_UNLOCK (mix);
665 gst_video_frame_unmap (&out_frame);
671 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
674 guint i, array_index = 0;
675 GstElement *element = GST_ELEMENT (mix);
676 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
678 GST_OBJECT_LOCK (mix);
679 walk = GST_ELEMENT (mix)->sinkpads;
680 i = mix->frames->len;
681 g_ptr_array_set_size (mix->frames, element->numsinkpads);
682 for (; i < element->numsinkpads; i++)
683 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
684 while (walk) { /* We walk with this list because it's ordered */
685 GstVideoAggregatorPad *vaggpad = walk->data;
687 walk = g_list_next (walk);
689 if (vaggpad->buffer != NULL) {
690 /* put buffer into array */
691 mix->array_buffers->pdata[array_index] = vaggpad->buffer;
695 GST_OBJECT_UNLOCK (mix);
697 return mix_class->process_buffers (mix, mix->array_buffers, outbuf);
701 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
703 gboolean res = FALSE;
704 GstGLMixer *mix = GST_GL_MIXER (vagg);
705 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
706 GstGLContext *context = GST_GL_BASE_MIXER (mix)->context;
707 GstGLSyncMeta *sync_meta;
709 if (mix_class->process_buffers)
710 res = gst_gl_mixer_process_buffers (mix, outbuf);
711 else if (mix_class->process_textures)
712 res = gst_gl_mixer_process_textures (mix, outbuf);
714 sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
716 gst_gl_sync_meta_set_sync_point (sync_meta, context);
718 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
722 gst_gl_mixer_get_property (GObject * object,
723 guint prop_id, GValue * value, GParamSpec * pspec)
727 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
733 gst_gl_mixer_set_property (GObject * object,
734 guint prop_id, const GValue * value, GParamSpec * pspec)
738 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
744 _free_glmixer_frame_data (GstGLMixerFrameData * frame)
746 g_slice_free1 (sizeof (GstGLMixerFrameData), frame);
750 gst_gl_mixer_start (GstAggregator * agg)
753 GstGLMixer *mix = GST_GL_MIXER (agg);
754 GstElement *element = GST_ELEMENT (agg);
756 GST_OBJECT_LOCK (mix);
757 mix->array_buffers = g_ptr_array_new_full (element->numsinkpads,
758 (GDestroyNotify) _free_glmixer_frame_data);
759 mix->frames = g_ptr_array_new_full (element->numsinkpads, NULL);
761 g_ptr_array_set_size (mix->array_buffers, element->numsinkpads);
762 g_ptr_array_set_size (mix->frames, element->numsinkpads);
764 for (i = 0; i < element->numsinkpads; i++)
765 mix->frames->pdata[i] = g_slice_new0 (GstGLMixerFrameData);
767 GST_OBJECT_UNLOCK (mix);
769 return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
773 gst_gl_mixer_stop (GstAggregator * agg)
775 GstGLMixer *mix = GST_GL_MIXER (agg);
776 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
777 GstGLContext *context = GST_GL_BASE_MIXER (mix)->context;
779 GST_OBJECT_LOCK (agg);
780 g_ptr_array_free (mix->frames, TRUE);
782 g_ptr_array_free (mix->array_buffers, TRUE);
783 mix->array_buffers = NULL;
784 GST_OBJECT_UNLOCK (agg);
786 if (mixer_class->reset)
787 mixer_class->reset (mix);
789 gst_gl_context_del_fbo (context, mix->fbo, mix->depthbuffer);
791 mix->depthbuffer = 0;
794 gst_gl_mixer_reset (mix);
796 return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);