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>
31 #include "gstglmixer.h"
33 #define GST_CAT_DEFAULT gst_gl_mixer_debug
34 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
36 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
37 GValue * value, GParamSpec * pspec);
38 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
39 const GValue * value, GParamSpec * pspec);
40 static gboolean gst_gl_mixer_pad_prepare_frame (GstVideoAggregatorPad * vpad,
41 GstVideoAggregator * vagg, GstBuffer * buffer,
42 GstVideoFrame * prepared_frame);
43 static void gst_gl_mixer_pad_clean_frame (GstVideoAggregatorPad * vpad,
44 GstVideoAggregator * vagg, GstVideoFrame * prepared_frame);
51 struct _GstGLMixerPrivate
55 gboolean gl_resource_ready;
56 GMutex gl_resource_lock;
57 GCond gl_resource_cond;
60 #define gst_gl_mixer_parent_class parent_class
61 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstGLMixer, gst_gl_mixer,
62 GST_TYPE_GL_BASE_MIXER);
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 vaggpad_class->prepare_frame = gst_gl_mixer_pad_prepare_frame;
77 vaggpad_class->clean_frame = gst_gl_mixer_pad_clean_frame;
81 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec)
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec)
97 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103 gst_gl_mixer_pad_prepare_frame (GstVideoAggregatorPad * vpad,
104 GstVideoAggregator * vagg, GstBuffer * buffer,
105 GstVideoFrame * prepared_frame)
107 GstGLMixerPad *pad = GST_GL_MIXER_PAD (vpad);
108 GstGLMixer *mix = GST_GL_MIXER (vagg);
109 GstVideoInfo gl_info;
110 GstGLSyncMeta *sync_meta;
112 pad->current_texture = 0;
114 gst_video_info_set_format (&gl_info,
115 GST_VIDEO_FORMAT_RGBA,
116 GST_VIDEO_INFO_WIDTH (&vpad->info), GST_VIDEO_INFO_HEIGHT (&vpad->info));
118 sync_meta = gst_buffer_get_gl_sync_meta (buffer);
120 gst_gl_sync_meta_wait (sync_meta, GST_GL_BASE_MIXER (mix)->context);
122 if (!gst_video_frame_map (prepared_frame, &gl_info, buffer,
123 GST_MAP_READ | GST_MAP_GL)) {
124 GST_ERROR_OBJECT (pad, "Failed to map input frame");
128 pad->current_texture = *(guint *) prepared_frame->data[0];
134 gst_gl_mixer_pad_clean_frame (GstVideoAggregatorPad * vpad,
135 GstVideoAggregator * vagg, GstVideoFrame * prepared_frame)
137 GstGLMixerPad *pad = GST_GL_MIXER_PAD (vpad);
139 pad->current_texture = 0;
140 if (prepared_frame->buffer) {
141 gst_video_frame_unmap (prepared_frame);
142 memset (prepared_frame, 0, sizeof (GstVideoFrame));
147 _negotiated_caps (GstAggregator * agg, GstCaps * caps)
149 GstGLMixer *mix = GST_GL_MIXER (agg);
152 mix->priv->negotiated = TRUE;
154 gst_caps_replace (&mix->out_caps, caps);
156 ret = GST_AGGREGATOR_CLASS (parent_class)->negotiated_src_caps (agg, caps);
162 _find_best_format (GstVideoAggregator * vagg, GstCaps * downstream_caps,
163 GstVideoInfo * best_info, gboolean * at_least_one_alpha)
165 GstVideoInfo tmp_info;
167 GST_VIDEO_AGGREGATOR_CLASS (parent_class)->find_best_format (vagg,
168 downstream_caps, best_info, at_least_one_alpha);
170 gst_video_info_set_format (&tmp_info, GST_VIDEO_FORMAT_RGBA,
171 best_info->width, best_info->height);
172 tmp_info.par_n = best_info->par_n;
173 tmp_info.par_d = best_info->par_d;
174 tmp_info.fps_n = best_info->fps_n;
175 tmp_info.fps_d = best_info->fps_d;
176 tmp_info.flags = best_info->flags;
177 tmp_info.interlace_mode = best_info->interlace_mode;
178 *best_info = tmp_info;
182 gst_gl_mixer_propose_allocation (GstAggregator * agg,
183 GstAggregatorPad * agg_pad, GstQuery * decide_query, GstQuery * query)
185 GstGLMixer *mix = GST_GL_MIXER (agg);
186 GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
187 GstGLContext *context;
188 GstBufferPool *pool = NULL;
189 GstStructure *config;
195 if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->propose_allocation
196 (agg, agg_pad, decide_query, query))
199 context = base_mix->context;
201 gst_query_parse_allocation (query, &caps, &need_pool);
206 if (!gst_video_info_from_caps (&info, caps))
209 /* the normal size of a frame */
213 GST_DEBUG_OBJECT (mix, "create new pool");
214 pool = gst_gl_buffer_pool_new (context);
216 config = gst_buffer_pool_get_config (pool);
217 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
219 if (!gst_buffer_pool_set_config (pool, config)) {
220 g_object_unref (pool);
225 gst_query_add_allocation_pool (query, pool, size, 1, 0);
227 g_object_unref (pool);
229 /* we also support various metadata */
230 if (context->gl_vtable->FenceSync)
231 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
238 GST_DEBUG_OBJECT (mix, "no caps specified");
243 GST_DEBUG_OBJECT (mix, "invalid caps specified");
248 GST_DEBUG_OBJECT (mix, "failed setting config");
254 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
258 GstCaps *template_caps;
260 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
262 template_caps = gst_pad_get_pad_template_caps (pad);
263 template_caps = gst_caps_make_writable (template_caps);
265 ret = gst_caps_can_intersect (caps, template_caps);
266 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
267 (ret ? "" : "not "), caps);
268 gst_caps_unref (template_caps);
274 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
277 GstCaps *template_caps;
278 GstCaps *filtered_caps;
279 GstCaps *returned_caps;
281 template_caps = gst_pad_get_pad_template_caps (pad);
283 sinkcaps = gst_pad_get_current_caps (pad);
284 if (sinkcaps == NULL) {
285 sinkcaps = gst_caps_ref (template_caps);
287 sinkcaps = gst_caps_merge (sinkcaps, gst_caps_ref (template_caps));
291 filtered_caps = gst_caps_intersect (sinkcaps, filter);
292 gst_caps_unref (sinkcaps);
294 filtered_caps = sinkcaps; /* pass ownership */
297 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
299 gst_caps_unref (template_caps);
300 gst_caps_unref (filtered_caps);
302 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
304 return returned_caps;
308 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
311 gboolean ret = FALSE;
312 GstGLMixer *mix = GST_GL_MIXER (agg);
314 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
316 switch (GST_QUERY_TYPE (query)) {
319 GstCaps *filter, *caps;
321 gst_query_parse_caps (query, &filter);
322 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
323 gst_query_set_caps_result (query, caps);
324 gst_caps_unref (caps);
328 case GST_QUERY_ACCEPT_CAPS:
332 gst_query_parse_accept_caps (query, &caps);
333 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
334 gst_query_set_accept_caps_result (query, ret);
339 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
347 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
351 /* GLMixer signals and args */
363 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
366 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
367 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
371 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
374 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
375 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
379 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
380 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
381 static gboolean gst_gl_mixer_start (GstAggregator * agg);
384 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
385 GstBuffer * outbuffer);
387 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
388 const GValue * value, GParamSpec * pspec);
389 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
390 GValue * value, GParamSpec * pspec);
392 static gboolean gst_gl_mixer_decide_allocation (GstAggregator * agg,
395 static gboolean gst_gl_mixer_gl_start (GstGLBaseMixer * mix);
396 static void gst_gl_mixer_gl_stop (GstGLBaseMixer * mix);
398 static void gst_gl_mixer_finalize (GObject * object);
401 gst_gl_mixer_class_init (GstGLMixerClass * klass)
403 GObjectClass *gobject_class = (GObjectClass *) klass;
404 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
405 GstVideoAggregatorClass *videoaggregator_class =
406 (GstVideoAggregatorClass *) klass;
407 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
408 GstGLBaseMixerClass *base_class = (GstGLBaseMixerClass *) klass;
410 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "OpenGL mixer");
412 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
414 gobject_class->get_property = gst_gl_mixer_get_property;
415 gobject_class->set_property = gst_gl_mixer_set_property;
417 gst_element_class_add_static_pad_template_with_gtype (element_class,
418 &src_factory, GST_TYPE_AGGREGATOR_PAD);
419 gst_element_class_add_static_pad_template_with_gtype (element_class,
420 &sink_factory, GST_TYPE_GL_MIXER_PAD);
422 agg_class->sink_query = gst_gl_mixer_sink_query;
423 agg_class->src_query = gst_gl_mixer_src_query;
424 agg_class->stop = gst_gl_mixer_stop;
425 agg_class->start = gst_gl_mixer_start;
426 agg_class->negotiated_src_caps = _negotiated_caps;
427 agg_class->decide_allocation = gst_gl_mixer_decide_allocation;
428 agg_class->propose_allocation = gst_gl_mixer_propose_allocation;
430 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
431 videoaggregator_class->find_best_format = _find_best_format;
433 base_class->gl_start = gst_gl_mixer_gl_start;
434 base_class->gl_stop = gst_gl_mixer_gl_stop;
436 /* Register the pad class */
437 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
439 klass->set_caps = NULL;
441 gst_type_mark_as_plugin_api (GST_TYPE_GL_MIXER_PAD, 0);
445 gst_gl_mixer_reset (GstGLMixer * mix)
447 mix->priv->negotiated = FALSE;
451 gst_gl_mixer_init (GstGLMixer * mix)
453 mix->priv = gst_gl_mixer_get_instance_private (mix);
455 mix->priv->gl_resource_ready = FALSE;
456 g_mutex_init (&mix->priv->gl_resource_lock);
457 g_cond_init (&mix->priv->gl_resource_cond);
458 /* initialize variables */
459 gst_gl_mixer_reset (mix);
463 gst_gl_mixer_finalize (GObject * object)
465 GstGLMixer *mix = GST_GL_MIXER (object);
466 GstGLMixerPrivate *priv = mix->priv;
469 gst_caps_unref (mix->out_caps);
471 g_mutex_clear (&priv->gl_resource_lock);
472 g_cond_clear (&priv->gl_resource_cond);
473 G_OBJECT_CLASS (parent_class)->finalize (object);
477 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
479 GstCaps *filter, *current_caps, *retcaps, *template_caps;
481 gst_query_parse_caps (query, &filter);
483 template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
485 current_caps = gst_pad_get_current_caps (pad);
486 if (current_caps == NULL)
487 retcaps = gst_caps_ref (template_caps);
489 retcaps = gst_caps_merge (current_caps, template_caps);
490 template_caps = NULL;
495 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
496 gst_caps_unref (retcaps);
497 retcaps = current_caps;
500 gst_query_set_caps_result (query, retcaps);
501 gst_caps_unref (retcaps);
504 gst_caps_unref (template_caps);
510 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
512 gboolean res = FALSE;
514 switch (GST_QUERY_TYPE (query)) {
516 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
519 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
527 _mixer_create_fbo (GstGLContext * context, GstGLMixer * mix)
529 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
530 guint out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
531 guint out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
534 gst_gl_framebuffer_new_with_default_depth (context, out_width,
539 gst_gl_mixer_gl_start (GstGLBaseMixer * base_mix)
541 return GST_GL_BASE_MIXER_CLASS (parent_class)->gl_start (base_mix);
545 gst_gl_mixer_gl_stop (GstGLBaseMixer * base_mix)
547 GstGLMixer *mix = GST_GL_MIXER (base_mix);
548 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
550 if (mixer_class->reset)
551 mixer_class->reset (mix);
553 g_mutex_lock (&mix->priv->gl_resource_lock);
554 gst_clear_object (&mix->fbo);
555 g_mutex_unlock (&mix->priv->gl_resource_lock);
557 GST_GL_BASE_MIXER_CLASS (parent_class)->gl_stop (base_mix);
561 gst_gl_mixer_decide_allocation (GstAggregator * agg, GstQuery * query)
563 GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
564 GstGLMixer *mix = GST_GL_MIXER (base_mix);
565 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
566 GstGLContext *context;
567 GstBufferPool *pool = NULL;
568 GstStructure *config;
570 guint min, max, size;
571 gboolean update_pool;
573 if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->decide_allocation (agg,
577 context = gst_gl_base_mixer_get_gl_context (base_mix);
579 GST_WARNING_OBJECT (agg, "No OpenGL context");
583 g_mutex_lock (&mix->priv->gl_resource_lock);
584 mix->priv->gl_resource_ready = FALSE;
586 gst_object_unref (mix->fbo);
588 gst_gl_context_thread_add (context,
589 (GstGLContextThreadFunc) _mixer_create_fbo, mix);
591 g_cond_signal (&mix->priv->gl_resource_cond);
592 g_mutex_unlock (&mix->priv->gl_resource_lock);
596 if (mixer_class->set_caps)
597 mixer_class->set_caps (mix, mix->out_caps);
599 mix->priv->gl_resource_ready = TRUE;
600 g_cond_signal (&mix->priv->gl_resource_cond);
601 g_mutex_unlock (&mix->priv->gl_resource_lock);
603 gst_query_parse_allocation (query, &caps, NULL);
605 if (gst_query_get_n_allocation_pools (query) > 0) {
606 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
612 gst_video_info_init (&vinfo);
613 gst_video_info_from_caps (&vinfo, caps);
620 pool = gst_gl_buffer_pool_new (context);
621 config = gst_buffer_pool_get_config (pool);
623 gst_buffer_pool_config_set_params (config, caps, size, min, max);
624 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
626 gst_buffer_pool_set_config (pool, config);
629 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
631 gst_query_add_allocation_pool (query, pool, size, min, max);
633 gst_object_unref (pool);
635 gst_clear_object (&context);
641 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("Context error"), (NULL));
647 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
649 GstGLMemory *out_tex;
651 GstVideoFrame out_frame;
652 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
653 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
655 GST_TRACE ("Processing buffers");
657 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
658 GST_MAP_WRITE | GST_MAP_GL)) {
662 out_tex = (GstGLMemory *) out_frame.map[0].memory;
664 g_mutex_lock (&mix->priv->gl_resource_lock);
665 if (!mix->priv->gl_resource_ready)
666 g_cond_wait (&mix->priv->gl_resource_cond, &mix->priv->gl_resource_lock);
668 if (!mix->priv->gl_resource_ready) {
669 g_mutex_unlock (&mix->priv->gl_resource_lock);
670 GST_ERROR_OBJECT (mix,
671 "fbo used to render can't be created, do not run process_textures");
676 mix_class->process_textures (mix, out_tex);
678 g_mutex_unlock (&mix->priv->gl_resource_lock);
681 gst_video_frame_unmap (&out_frame);
687 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
689 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
691 return mix_class->process_buffers (mix, outbuf);
695 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
697 GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (vagg);
698 gboolean res = FALSE;
699 GstGLMixer *mix = GST_GL_MIXER (vagg);
700 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
701 GstGLContext *context = gst_gl_base_mixer_get_gl_context (base_mix);
702 GstGLSyncMeta *sync_meta;
705 GST_DEBUG_OBJECT (vagg, "No OpenGL context, try again later");
706 return GST_AGGREGATOR_FLOW_NEED_DATA;
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 gst_clear_object (&context);
720 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
724 gst_gl_mixer_get_property (GObject * object,
725 guint prop_id, GValue * value, GParamSpec * pspec)
729 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
735 gst_gl_mixer_set_property (GObject * object,
736 guint prop_id, const GValue * value, GParamSpec * pspec)
740 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
746 gst_gl_mixer_start (GstAggregator * agg)
748 return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
752 gst_gl_mixer_stop (GstAggregator * agg)
754 GstGLMixer *mix = GST_GL_MIXER (agg);
756 gst_gl_mixer_reset (mix);
758 return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);