2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstelementfactory.c: GstElementFactory object, support routines
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 /* #define DEBUG_ENABLED */
24 #include "gst_private.h"
26 #include "gstelement.h"
27 #include "gstregistry.h"
30 static void gst_element_factory_class_init (GstElementFactoryClass *klass);
31 static void gst_element_factory_init (GstElementFactory *factory);
33 static void gst_element_factory_unload_thyself (GstPluginFeature *feature);
35 static GstPluginFeatureClass *parent_class = NULL;
36 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
39 gst_element_factory_get_type (void)
41 static GType elementfactory_type = 0;
43 if (!elementfactory_type) {
44 static const GTypeInfo elementfactory_info = {
45 sizeof (GstElementFactoryClass),
48 (GClassInitFunc) gst_element_factory_class_init,
51 sizeof(GstElementFactory),
53 (GInstanceInitFunc) gst_element_factory_init,
56 elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
57 "GstElementFactory", &elementfactory_info, 0);
59 return elementfactory_type;
63 gst_element_factory_class_init (GstElementFactoryClass *klass)
65 GObjectClass *gobject_class;
66 GstObjectClass *gstobject_class;
67 GstPluginFeatureClass *gstpluginfeature_class;
69 gobject_class = (GObjectClass*)klass;
70 gstobject_class = (GstObjectClass*)klass;
71 gstpluginfeature_class = (GstPluginFeatureClass*) klass;
73 parent_class = g_type_class_ref (GST_TYPE_PLUGIN_FEATURE);
75 gstpluginfeature_class->unload_thyself = GST_DEBUG_FUNCPTR (gst_element_factory_unload_thyself);
80 gst_element_factory_init (GstElementFactory *factory)
82 factory->padtemplates = NULL;
83 factory->numpadtemplates = 0;
87 * gst_element_factory_find:
88 * @name: name of factory to find
90 * Search for an element factory of the given name.
92 * Returns: #GstElementFactory if found, NULL otherwise
95 gst_element_factory_find (const gchar *name)
97 GstPluginFeature *feature;
99 g_return_val_if_fail(name != NULL, NULL);
101 feature = gst_registry_pool_find_feature (name, GST_TYPE_ELEMENT_FACTORY);
103 return GST_ELEMENT_FACTORY (feature);
105 /* this should be an ERROR */
106 GST_DEBUG (GST_CAT_ELEMENT_FACTORY,"no such elementfactory \"%s\"", name);
111 gst_element_details_free (GstElementDetails *dp)
113 g_free (dp->longname);
115 g_free (dp->description);
116 g_free (dp->version);
118 g_free (dp->copyright);
123 gst_element_factory_cleanup (GstElementFactory *factory)
127 if (factory->details_dynamic) {
128 gst_element_details_free (factory->details);
129 factory->details_dynamic = FALSE;
132 padtemplates = factory->padtemplates;
134 while (padtemplates) {
135 GstPadTemplate *oldtempl = GST_PAD_TEMPLATE (padtemplates->data);
137 gst_object_unref (GST_OBJECT (oldtempl));
139 padtemplates = g_list_next (padtemplates);
141 g_list_free (factory->padtemplates);
143 factory->padtemplates = NULL;
144 factory->numpadtemplates = 0;
146 g_free (GST_PLUGIN_FEATURE (factory)->name);
150 * gst_element_factory_new:
151 * @name: name of new elementfactory
152 * @type: GType of new element
153 * @details: #GstElementDetails structure with element details
155 * Create a new elementfactory capable of insantiating objects of the
158 * Returns: new elementfactory
161 gst_element_factory_new (const gchar *name, GType type,
162 GstElementDetails *details)
164 GstElementFactory *factory;
166 g_return_val_if_fail (name != NULL, NULL);
167 g_return_val_if_fail (type, NULL);
168 g_return_val_if_fail (details, NULL);
170 factory = gst_element_factory_find (name);
173 factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
175 gst_element_factory_cleanup (factory);
178 factory->details = details;
179 factory->details_dynamic = FALSE;
182 factory->type = type;
183 else if (factory->type != type)
184 g_critical ("`%s' requested type change (!)", name);
186 GST_PLUGIN_FEATURE (factory)->name = g_strdup (name);
192 * gst_element_factory_create:
193 * @factory: factory to instantiate
194 * @name: name of new element
196 * Create a new element of the type defined by the given elementfactory.
197 * It will be given the name supplied, since all elements require a name as
198 * their first argument.
200 * Returns: new #GstElement
203 gst_element_factory_create (GstElementFactory *factory,
207 GstElementClass *oclass;
209 g_return_val_if_fail (factory != NULL, NULL);
211 if (!gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory)))
214 GST_DEBUG (GST_CAT_ELEMENT_FACTORY,
215 "creating element from factory \"%s\" (name \"%s\", type %d)",
216 GST_PLUGIN_FEATURE_NAME (factory), name, (gint) factory->type);
218 if (factory->type == 0) {
219 g_critical ("Factory for `%s' has no type",
220 GST_PLUGIN_FEATURE_NAME (factory));
224 /* create an instance of the element */
225 element = GST_ELEMENT (g_object_new (factory->type, NULL));
226 g_assert (element != NULL);
228 /* attempt to set the elemenfactory class pointer if necessary */
229 oclass = GST_ELEMENT_CLASS (G_OBJECT_GET_CLASS (element));
230 if (oclass->elementfactory == NULL) {
231 GST_DEBUG (GST_CAT_ELEMENT_FACTORY, "class %s", GST_PLUGIN_FEATURE_NAME (factory));
232 oclass->elementfactory = factory;
234 /* copy pad template pointers to the element class,
235 * allow for custom padtemplates */
236 oclass->padtemplates = g_list_concat (oclass->padtemplates,
237 g_list_copy (factory->padtemplates));
238 oclass->numpadtemplates += factory->numpadtemplates;
241 gst_object_set_name (GST_OBJECT (element), name);
247 * gst_element_factory_make:
248 * @factoryname: a named factory to instantiate
249 * @name: name of new element
251 * Create a new element of the type defined by the given element factory.
252 * If name is NULL, then the element will receive a guaranteed unique name,
253 * consisting of the element factory name and a number.
254 * If name is given, it will be given the name supplied.
256 * Returns: new #GstElement (or NULL if unable to create element)
259 gst_element_factory_make (const gchar *factoryname, const gchar *name)
261 GstElementFactory *factory;
264 g_return_val_if_fail (factoryname != NULL, NULL);
266 GST_DEBUG (GST_CAT_ELEMENT_FACTORY, "gstelementfactory: make \"%s\" \"%s\"",
269 /* gst_plugin_load_element_factory (factoryname); */
270 factory = gst_element_factory_find (factoryname);
271 if (factory == NULL) {
272 GST_INFO (GST_CAT_ELEMENT_FACTORY,"no such element factory \"%s\"!",
276 element = gst_element_factory_create (factory, name);
277 if (element == NULL) {
278 GST_INFO (GST_CAT_ELEMENT_FACTORY,
279 "couldn't create instance of element factory \"%s\"!",
288 * gst_element_factory_make_or_warn:
289 * @factoryname: a named factory to instantiate
290 * @name: name of new element
292 * Create a new element of the type defined by the given element factory
293 * using #gst_element_factory_make.
294 * Will use g_warning if the element could not be created.
296 * Returns: new #GstElement (or NULL if unable to create element)
299 gst_element_factory_make_or_warn (const gchar *factoryname, const gchar *name)
303 element = gst_element_factory_make (factoryname, name);
306 g_warning ("Could not create element from factory %s !\n", factoryname);
312 * gst_element_factory_add_pad_template :
313 * @elementfactory: factory to add the src id to
314 * @templ: the padtemplate to add
316 * Add the given padtemplate to this elementfactory.
319 gst_element_factory_add_pad_template (GstElementFactory *factory,
320 GstPadTemplate *templ)
322 g_return_if_fail (factory != NULL);
323 g_return_if_fail (templ != NULL);
325 gst_object_ref (GST_OBJECT (templ));
326 gst_object_sink (GST_OBJECT (templ));
328 factory->padtemplates = g_list_append (factory->padtemplates, templ);
329 factory->numpadtemplates++;
333 * gst_element_factory_can_src_caps :
334 * @factory: factory to query
335 * @caps: the caps to check
337 * Checks if the factory can source the given capability.
339 * Returns: true if it can src the capabilities
342 gst_element_factory_can_src_caps (GstElementFactory *factory,
347 g_return_val_if_fail(factory != NULL, FALSE);
348 g_return_val_if_fail(caps != NULL, FALSE);
350 templates = factory->padtemplates;
353 GstPadTemplate *template = (GstPadTemplate *)templates->data;
355 if (template->direction == GST_PAD_SRC) {
356 if (gst_caps_is_always_compatible (GST_PAD_TEMPLATE_CAPS (template), caps))
359 templates = g_list_next (templates);
366 * gst_element_factory_can_sink_caps :
367 * @factory: factory to query
368 * @caps: the caps to check
370 * Checks if the factory can sink the given capability.
372 * Returns: true if it can sink the capabilities
375 gst_element_factory_can_sink_caps (GstElementFactory *factory,
380 g_return_val_if_fail(factory != NULL, FALSE);
381 g_return_val_if_fail(caps != NULL, FALSE);
383 templates = factory->padtemplates;
386 GstPadTemplate *template = (GstPadTemplate *)templates->data;
388 if (template->direction == GST_PAD_SINK) {
389 if (gst_caps_is_always_compatible (caps, GST_PAD_TEMPLATE_CAPS (template)))
392 templates = g_list_next (templates);
399 * gst_element_factory_set_rank :
400 * @factory: factory to rank
401 * @rank: rank value - higher number means more priority rank
403 * Specifies a rank for the element so that
404 * autoplugging uses the most appropriate elements.
408 gst_element_factory_set_rank (GstElementFactory *factory, guint16 rank)
410 g_return_if_fail (factory != NULL);
411 factory->rank = rank;
415 gst_element_factory_unload_thyself (GstPluginFeature *feature)
417 GstElementFactory *factory;
419 factory = GST_ELEMENT_FACTORY (feature);