1 /* Generic video mixer plugin
4 * Copyright (C) 2015 Matthew Waters <matthew@centricular.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 "gstglbasemixer.h"
31 #define GST_CAT_DEFAULT gst_gl_base_mixer_debug
32 GST_DEBUG_CATEGORY (gst_gl_base_mixer_debug);
34 static void gst_gl_base_mixer_pad_get_property (GObject * object, guint prop_id,
35 GValue * value, GParamSpec * pspec);
36 static void gst_gl_base_mixer_pad_set_property (GObject * object, guint prop_id,
37 const GValue * value, GParamSpec * pspec);
39 static void gst_gl_base_mixer_set_context (GstElement * element,
40 GstContext * context);
41 static GstStateChangeReturn gst_gl_base_mixer_change_state (GstElement *
42 element, GstStateChange transition);
44 struct _GstGLBaseMixerPrivate
48 GstGLContext *other_context;
51 #define gst_gl_base_mixer_parent_class parent_class
52 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstGLBaseMixer, gst_gl_base_mixer,
53 GST_TYPE_VIDEO_AGGREGATOR);
55 G_DEFINE_TYPE (GstGLBaseMixerPad, gst_gl_base_mixer_pad,
56 GST_TYPE_VIDEO_AGGREGATOR_PAD);
59 gst_gl_base_mixer_pad_class_init (GstGLBaseMixerPadClass * klass)
61 GObjectClass *gobject_class = (GObjectClass *) klass;
62 GstVideoAggregatorPadClass *vaggpad_class =
63 (GstVideoAggregatorPadClass *) klass;
65 gobject_class->set_property = gst_gl_base_mixer_pad_set_property;
66 gobject_class->get_property = gst_gl_base_mixer_pad_get_property;
68 vaggpad_class->prepare_frame = NULL;
69 vaggpad_class->clean_frame = NULL;
73 gst_gl_base_mixer_pad_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec)
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 gst_gl_base_mixer_pad_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec)
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96 _find_local_gl_context (GstGLBaseMixer * mix)
98 if (gst_gl_query_local_gl_context (GST_ELEMENT (mix), GST_PAD_SRC,
101 if (gst_gl_query_local_gl_context (GST_ELEMENT (mix), GST_PAD_SINK,
108 _get_gl_context (GstGLBaseMixer * mix)
110 GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix);
111 GError *error = NULL;
113 if (!gst_gl_ensure_element_data (mix, &mix->display,
114 &mix->priv->other_context))
117 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
119 _find_local_gl_context (mix);
121 GST_OBJECT_LOCK (mix->display);
125 gst_object_unref (mix->context);
128 /* just get a GL context. we don't care */
130 gst_gl_display_get_gl_context_for_thread (mix->display, NULL);
132 if (!gst_gl_display_create_context (mix->display,
133 mix->priv->other_context, &mix->context, &error)) {
134 GST_OBJECT_UNLOCK (mix->display);
138 } while (!gst_gl_display_add_context (mix->display, mix->context));
140 GST_OBJECT_UNLOCK (mix->display);
143 GstGLAPI current_gl_api = gst_gl_context_get_gl_api (mix->context);
144 if ((current_gl_api & mix_class->supported_gl_api) == 0)
145 goto unsupported_gl_api;
152 GstGLAPI gl_api = gst_gl_context_get_gl_api (mix->context);
153 gchar *gl_api_str = gst_gl_api_to_string (gl_api);
154 gchar *supported_gl_api_str =
155 gst_gl_api_to_string (mix_class->supported_gl_api);
156 GST_ELEMENT_ERROR (mix, RESOURCE, BUSY,
157 ("GL API's not compatible context: %s supported: %s", gl_api_str,
158 supported_gl_api_str), (NULL));
160 g_free (supported_gl_api_str);
166 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("%s", error->message),
168 g_clear_error (&error);
174 gst_gl_base_mixer_propose_allocation (GstAggregator * agg,
175 GstAggregatorPad * aggpad, GstQuery * decide_query, GstQuery * query)
177 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (agg);
179 if (!_get_gl_context (mix))
186 gst_gl_base_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
189 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (agg);
191 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
193 switch (GST_QUERY_TYPE (query)) {
194 case GST_QUERY_CONTEXT:
196 if (gst_gl_handle_context_query ((GstElement *) mix, query,
197 mix->display, mix->context, mix->priv->other_context))
205 return GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);;
209 gst_gl_base_mixer_pad_init (GstGLBaseMixerPad * mixerpad)
213 /* GLBaseMixer signals and args */
226 static gboolean gst_gl_base_mixer_src_query (GstAggregator * agg,
230 gst_gl_base_mixer_src_activate_mode (GstAggregator * aggregator,
231 GstPadMode mode, gboolean active);
232 static gboolean gst_gl_base_mixer_stop (GstAggregator * agg);
233 static gboolean gst_gl_base_mixer_start (GstAggregator * agg);
235 static void gst_gl_base_mixer_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec);
237 static void gst_gl_base_mixer_get_property (GObject * object, guint prop_id,
238 GValue * value, GParamSpec * pspec);
240 static gboolean gst_gl_base_mixer_decide_allocation (GstAggregator * agg,
244 gst_gl_base_mixer_class_init (GstGLBaseMixerClass * klass)
246 GObjectClass *gobject_class;
247 GstElementClass *element_class;
248 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
250 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "opengl mixer");
252 gobject_class = (GObjectClass *) klass;
253 element_class = GST_ELEMENT_CLASS (klass);
255 gobject_class->get_property = gst_gl_base_mixer_get_property;
256 gobject_class->set_property = gst_gl_base_mixer_set_property;
258 element_class->set_context =
259 GST_DEBUG_FUNCPTR (gst_gl_base_mixer_set_context);
260 element_class->change_state = gst_gl_base_mixer_change_state;
262 agg_class->sink_query = gst_gl_base_mixer_sink_query;
263 agg_class->src_query = gst_gl_base_mixer_src_query;
264 agg_class->src_activate = gst_gl_base_mixer_src_activate_mode;
265 agg_class->stop = gst_gl_base_mixer_stop;
266 agg_class->start = gst_gl_base_mixer_start;
267 agg_class->decide_allocation = gst_gl_base_mixer_decide_allocation;
268 agg_class->propose_allocation = gst_gl_base_mixer_propose_allocation;
270 g_object_class_install_property (gobject_class, PROP_CONTEXT,
271 g_param_spec_object ("context",
273 "Get OpenGL context",
274 GST_TYPE_GL_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
276 /* Register the pad class */
277 g_type_class_ref (GST_TYPE_GL_BASE_MIXER_PAD);
279 klass->supported_gl_api = GST_GL_API_ANY;
283 gst_gl_base_mixer_init (GstGLBaseMixer * mix)
285 mix->priv = gst_gl_base_mixer_get_instance_private (mix);
289 gst_gl_base_mixer_set_context (GstElement * element, GstContext * context)
291 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (element);
292 GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix);
294 gst_gl_handle_set_context (element, context, &mix->display,
295 &mix->priv->other_context);
298 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
300 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
304 gst_gl_base_mixer_activate (GstGLBaseMixer * mix, gboolean active)
306 GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix);
307 gboolean result = TRUE;
310 if (!gst_gl_ensure_element_data (mix, &mix->display,
311 &mix->priv->other_context))
314 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
321 gst_gl_base_mixer_src_activate_mode (GstAggregator * aggregator,
322 GstPadMode mode, gboolean active)
325 gboolean result = FALSE;
327 mix = GST_GL_BASE_MIXER (aggregator);
330 case GST_PAD_MODE_PUSH:
331 case GST_PAD_MODE_PULL:
332 result = gst_gl_base_mixer_activate (mix, active);
342 gst_gl_base_mixer_src_query (GstAggregator * agg, GstQuery * query)
344 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (agg);
346 switch (GST_QUERY_TYPE (query)) {
347 case GST_QUERY_CONTEXT:
349 if (gst_gl_handle_context_query ((GstElement *) mix, query,
350 mix->display, mix->context, mix->priv->other_context))
358 return GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
362 gst_gl_base_mixer_decide_allocation (GstAggregator * agg, GstQuery * query)
364 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (agg);
366 if (!_get_gl_context (mix))
373 gst_gl_base_mixer_get_property (GObject * object,
374 guint prop_id, GValue * value, GParamSpec * pspec)
376 GstGLBaseMixer *mixer = GST_GL_BASE_MIXER (object);
380 g_value_set_object (value, mixer->context);
383 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
389 gst_gl_base_mixer_set_property (GObject * object,
390 guint prop_id, const GValue * value, GParamSpec * pspec)
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
400 gst_gl_base_mixer_start (GstAggregator * agg)
402 return GST_AGGREGATOR_CLASS (parent_class)->start (agg);;
406 gst_gl_base_mixer_stop (GstAggregator * agg)
408 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (agg);
411 gst_object_unref (mix->context);
418 static GstStateChangeReturn
419 gst_gl_base_mixer_change_state (GstElement * element, GstStateChange transition)
421 GstGLBaseMixer *mix = GST_GL_BASE_MIXER (element);
422 GstGLBaseMixerClass *mix_class = GST_GL_BASE_MIXER_GET_CLASS (mix);
423 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
425 GST_DEBUG_OBJECT (mix, "changing state: %s => %s",
426 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
427 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
429 switch (transition) {
430 case GST_STATE_CHANGE_NULL_TO_READY:
431 if (!gst_gl_ensure_element_data (element, &mix->display,
432 &mix->priv->other_context))
433 return GST_STATE_CHANGE_FAILURE;
435 gst_gl_display_filter_gl_api (mix->display, mix_class->supported_gl_api);
441 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
442 if (ret == GST_STATE_CHANGE_FAILURE)
445 switch (transition) {
446 case GST_STATE_CHANGE_READY_TO_NULL:
447 if (mix->priv->other_context) {
448 gst_object_unref (mix->priv->other_context);
449 mix->priv->other_context = NULL;
453 gst_object_unref (mix->display);