3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "gstglsrcbin.h"
27 GST_DEBUG_CATEGORY (gst_debug_gl_src_bin);
28 #define GST_CAT_DEFAULT gst_debug_gl_src_bin
30 static void gst_gl_src_bin_finalize (GObject * object);
31 static void gst_gl_src_bin_set_property (GObject * object, guint prop_id,
32 const GValue * value, GParamSpec * param_spec);
33 static void gst_gl_src_bin_get_property (GObject * object, guint prop_id,
34 GValue * value, GParamSpec * param_spec);
36 static GstStateChangeReturn gst_gl_src_bin_change_state (GstElement * element,
37 GstStateChange transition);
39 static GstStaticPadTemplate gst_gl_src_bin_template =
40 GST_STATIC_PAD_TEMPLATE ("src",
43 GST_STATIC_CAPS ("video/x-raw(ANY)"));
54 SIGNAL_CREATE_ELEMENT,
58 static guint gst_gl_src_bin_signals[SIGNAL_LAST] = { 0, };
60 #define gst_gl_src_bin_parent_class parent_class
61 G_DEFINE_TYPE_WITH_CODE (GstGLSrcBin, gst_gl_src_bin,
63 GST_DEBUG_CATEGORY_INIT (gst_debug_gl_src_bin, "glsrcbin", 0,
64 "OpenGL Video Src Bin"));
67 gst_gl_src_bin_class_init (GstGLSrcBinClass * klass)
69 GObjectClass *gobject_class;
70 GstElementClass *element_class;
72 gobject_class = (GObjectClass *) klass;
73 element_class = GST_ELEMENT_CLASS (klass);
75 element_class->change_state = gst_gl_src_bin_change_state;
77 gobject_class->set_property = gst_gl_src_bin_set_property;
78 gobject_class->get_property = gst_gl_src_bin_get_property;
79 gobject_class->finalize = gst_gl_src_bin_finalize;
81 g_object_class_install_property (gobject_class, PROP_SRC,
82 g_param_spec_object ("src",
84 "The GL src chain to use",
86 GST_PARAM_MUTABLE_READY | G_PARAM_READWRITE |
87 G_PARAM_STATIC_STRINGS));
90 * GstGLSrcBin::create-element:
91 * @object: the #GstGLSrcBin
93 * Will be emitted when we need the processing element/s that this bin will use
95 * Returns: a new #GstElement
97 gst_gl_src_bin_signals[SIGNAL_CREATE_ELEMENT] =
98 g_signal_new ("create-element", G_TYPE_FROM_CLASS (klass),
99 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_ELEMENT, 0);
101 gst_element_class_set_metadata (element_class,
102 "GL Src Bin", "Src/Video",
103 "Infrastructure to process GL textures",
104 "Matthew Waters <matthew@centricular.com>");
106 gst_element_class_add_static_pad_template (element_class,
107 &gst_gl_src_bin_template);
111 gst_gl_src_bin_init (GstGLSrcBin * self)
116 self->download = gst_element_factory_make ("gldownload", NULL);
117 self->convert = gst_element_factory_make ("glcolorconvert", NULL);
119 res &= gst_bin_add (GST_BIN (self), self->download);
120 res &= gst_bin_add (GST_BIN (self), self->convert);
122 res &= gst_element_link_pads (self->convert, "src", self->download, "sink");
124 pad = gst_element_get_static_pad (self->download, "src");
128 GST_DEBUG_OBJECT (self, "setting target src pad %" GST_PTR_FORMAT, pad);
129 self->srcpad = gst_ghost_pad_new ("src", pad);
130 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
131 gst_object_unref (pad);
135 GST_WARNING_OBJECT (self, "Failed to add/connect the necessary machinery");
140 gst_gl_src_bin_finalize (GObject * object)
142 GstGLSrcBin *self = GST_GL_SRC_BIN (object);
145 gst_object_unref (self->src);
147 G_OBJECT_CLASS (parent_class)->finalize (object);
151 _connect_src_element (GstGLSrcBin * self)
155 gst_object_set_name (GST_OBJECT (self->src), "src");
156 res &= gst_bin_add (GST_BIN (self), self->src);
158 res &= gst_element_link_pads (self->src, "src", self->convert, "sink");
161 GST_ERROR_OBJECT (self, "Failed to link src element into the pipeline");
167 * @src: (transfer floating):
170 gst_gl_src_bin_set_src (GstGLSrcBin * self, GstElement * src)
172 g_return_val_if_fail (GST_IS_ELEMENT (src), FALSE);
175 gst_element_set_locked_state (self->src, TRUE);
176 gst_bin_remove (GST_BIN (self), self->src);
177 gst_element_set_state (self->src, GST_STATE_NULL);
178 gst_object_unref (self->src);
183 gst_object_ref_sink (src);
185 if (src && !_connect_src_element (self)) {
186 gst_object_unref (self->src);
195 gst_gl_src_bin_finish_init_with_element (GstGLSrcBin * self,
196 GstElement * element)
198 gst_gl_src_bin_set_src (self, element);
202 gst_gl_src_bin_finish_init (GstGLSrcBin * self)
204 GstGLSrcBinClass *klass = GST_GL_SRC_BIN_GET_CLASS (self);
205 GstElement *element = NULL;
207 if (klass->create_element)
208 element = klass->create_element ();
211 gst_gl_src_bin_finish_init_with_element (self, element);
215 gst_gl_src_bin_set_property (GObject * object, guint prop_id,
216 const GValue * value, GParamSpec * pspec)
218 GstGLSrcBin *self = GST_GL_SRC_BIN (object);
222 gst_gl_src_bin_set_src (self, g_value_get_object (value));
226 g_object_set_property (G_OBJECT (self->src), pspec->name, value);
232 gst_gl_src_bin_get_property (GObject * object, guint prop_id,
233 GValue * value, GParamSpec * pspec)
235 GstGLSrcBin *self = GST_GL_SRC_BIN (object);
239 g_value_set_object (value, self->src);
243 g_object_get_property (G_OBJECT (self->src), pspec->name, value);
248 static GstStateChangeReturn
249 gst_gl_src_bin_change_state (GstElement * element, GstStateChange transition)
251 GstGLSrcBin *self = GST_GL_SRC_BIN (element);
252 GstGLSrcBinClass *klass = GST_GL_SRC_BIN_GET_CLASS (self);
253 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
255 GST_DEBUG ("changing state: %s => %s",
256 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
257 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
259 switch (transition) {
260 case GST_STATE_CHANGE_NULL_TO_READY:
262 if (klass->create_element)
263 self->src = klass->create_element ();
266 g_signal_emit (element,
267 gst_gl_src_bin_signals[SIGNAL_CREATE_ELEMENT], 0, &self->src);
268 if (self->src && g_object_is_floating (self->src))
269 gst_object_ref_sink (self->src);
273 GST_ERROR_OBJECT (element, "Failed to retrieve element");
274 return GST_STATE_CHANGE_FAILURE;
276 if (!_connect_src_element (self))
277 return GST_STATE_CHANGE_FAILURE;
284 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
285 if (ret == GST_STATE_CHANGE_FAILURE)
288 switch (transition) {