2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
6 * gstelementfactory.c: GstElementFactory object, support routines
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 * SECTION:gstelementfactory
25 * @short_description: Create GstElements from a factory
26 * @see_also: #GstElement, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
28 * GstElementFactory is used to create instances of elements. A GstElementfactory
29 * can be added to a #GstPlugin as it is also a #GstPluginFeature.
31 * Use gst_element_factory_new() to create a new factory which can be added to a
32 * plugin with gst_plugin_add_feature().
34 * gst_element_factory_add_pad_template() is used to add a padtemplate to the factory.
35 * This function will enable the application to query for elementfactories that handle
36 * a specific media type.
38 * Use the gst_element_factory_find() and gst_element_factory_create() functions
39 * to create element instances or use gst_element_factory_make() as a convenient
42 * The following code example shows you how to create a GstFileSrc element.
45 * <title>Using an element factory</title>
46 * <programlisting language="c">
47 * #include <gst/gst.h>
50 * GstElementFactory *srcfactory;
52 * gst_init(&argc,&argv);
54 * srcfactory = gst_element_factory_find("filesrc");
55 * g_return_if_fail(srcfactory != NULL);
57 * src = gst_element_factory_create(srcfactory,"src");
58 * g_return_if_fail(src != NULL);
63 * An elementfactory can be assigned a rank with gst_element_factory_set_rank()
64 * so that the autopluggers can select a plugin more appropriatly
67 #include "gst_private.h"
69 #include "gstelement.h"
72 #include "gstregistry.h"
74 GST_DEBUG_CATEGORY_STATIC (element_factory_debug);
75 #define GST_CAT_DEFAULT element_factory_debug
77 static void gst_element_factory_class_init (GstElementFactoryClass * klass);
78 static void gst_element_factory_init (GstElementFactory * factory);
79 static void gst_element_factory_finalize (GObject * object);
80 void __gst_element_details_clear (GstElementDetails * dp);
81 static void gst_element_factory_cleanup (GstElementFactory * factory);
83 static GstPluginFeatureClass *parent_class = NULL;
85 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
88 gst_element_factory_get_type (void)
90 static GType elementfactory_type = 0;
92 if (!elementfactory_type) {
93 static const GTypeInfo elementfactory_info = {
94 sizeof (GstElementFactoryClass),
97 (GClassInitFunc) gst_element_factory_class_init,
100 sizeof (GstElementFactory),
102 (GInstanceInitFunc) gst_element_factory_init,
106 elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
107 "GstElementFactory", &elementfactory_info, 0);
108 GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY",
109 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
110 "element factories keep information about installed elements");
112 return elementfactory_type;
115 gst_element_factory_class_init (GstElementFactoryClass * klass)
117 GObjectClass *gobject_class;
118 GstObjectClass *gstobject_class;
119 GstPluginFeatureClass *gstpluginfeature_class;
121 gobject_class = (GObjectClass *) klass;
122 gstobject_class = (GstObjectClass *) klass;
123 gstpluginfeature_class = (GstPluginFeatureClass *) klass;
125 parent_class = g_type_class_peek_parent (klass);
127 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_factory_finalize);
131 gst_element_factory_init (GstElementFactory * factory)
133 factory->staticpadtemplates = NULL;
134 factory->numpadtemplates = 0;
136 factory->uri_type = GST_URI_UNKNOWN;
137 factory->uri_protocols = NULL;
139 factory->interfaces = NULL;
143 gst_element_factory_finalize (GObject * object)
145 GstElementFactory *factory = GST_ELEMENT_FACTORY (object);
147 gst_element_factory_cleanup (factory);
148 G_OBJECT_CLASS (parent_class)->finalize (object);
152 * gst_element_factory_find:
153 * @name: name of factory to find
155 * Search for an element factory of the given name. Refs the returned
156 * element factory; caller is responsible for unreffing.
158 * Returns: #GstElementFactory if found, NULL otherwise
161 gst_element_factory_find (const gchar * name)
163 GstPluginFeature *feature;
165 g_return_val_if_fail (name != NULL, NULL);
167 feature = gst_registry_find_feature (gst_registry_get_default (), name,
168 GST_TYPE_ELEMENT_FACTORY);
170 return GST_ELEMENT_FACTORY (feature);
172 /* this isn't an error, for instance when you query if an element factory is
174 GST_LOG ("no such element factory \"%s\"", name);
179 __gst_element_details_clear (GstElementDetails * dp)
181 g_free (dp->longname);
185 g_free (dp->description);
186 dp->description = NULL;
191 #define VALIDATE_SET(__dest, __src, __entry) \
193 if (g_utf8_validate (__src->__entry, -1, NULL)) { \
194 __dest->__entry = g_strdup (__src->__entry); \
196 g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s", \
198 __dest->__entry = g_strdup ("[ERROR: invalid UTF-8]"); \
203 __gst_element_details_set (GstElementDetails * dest,
204 const GstElementDetails * src)
206 VALIDATE_SET (dest, src, longname);
207 VALIDATE_SET (dest, src, klass);
208 VALIDATE_SET (dest, src, description);
209 VALIDATE_SET (dest, src, author);
213 __gst_element_details_copy (GstElementDetails * dest,
214 const GstElementDetails * src)
216 __gst_element_details_clear (dest);
217 __gst_element_details_set (dest, src);
221 gst_element_factory_cleanup (GstElementFactory * factory)
225 __gst_element_details_clear (&factory->details);
227 g_type_class_unref (g_type_class_peek (factory->type));
231 for (item = factory->staticpadtemplates; item; item = item->next) {
232 GstStaticPadTemplate *templ = item->data;
234 g_free (templ->name_template);
235 g_free ((gchar *) templ->static_caps.string);
236 memset (&(templ->static_caps), 0, sizeof (GstStaticCaps));
239 g_list_free (factory->staticpadtemplates);
240 factory->staticpadtemplates = NULL;
241 factory->numpadtemplates = 0;
242 factory->uri_type = GST_URI_UNKNOWN;
243 if (factory->uri_protocols) {
244 g_strfreev (factory->uri_protocols);
245 factory->uri_protocols = NULL;
248 g_list_foreach (factory->interfaces, (GFunc) g_free, NULL);
249 g_list_free (factory->interfaces);
250 factory->interfaces = NULL;
254 * gst_element_register:
255 * @plugin: #GstPlugin to register the element with
256 * @name: name of elements of this type
257 * @rank: rank of element (higher rank means more importance when autoplugging)
258 * @type: GType of element to register
260 * Create a new elementfactory capable of instantiating objects of the
263 * Returns: TRUE, if the registering succeeded, FALSE on error
266 gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
269 GstElementFactory *factory;
271 guint n_interfaces, i;
272 GstElementClass *klass;
275 g_return_val_if_fail (name != NULL, FALSE);
276 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
278 factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
279 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
280 GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
283 klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
284 factory->type = type;
285 __gst_element_details_copy (&factory->details, &klass->details);
286 for (item = klass->padtemplates; item; item = item->next) {
287 GstPadTemplate *templ = item->data;
288 GstStaticPadTemplate *newt;
290 newt = g_new0 (GstStaticPadTemplate, 1);
291 newt->name_template = g_strdup (templ->name_template);
292 newt->direction = templ->direction;
293 newt->presence = templ->presence;
294 newt->static_caps.string = gst_caps_to_string (templ->caps);
295 factory->staticpadtemplates =
296 g_list_append (factory->staticpadtemplates, newt);
298 factory->numpadtemplates = klass->numpadtemplates;
299 klass->elementfactory = factory;
301 /* special stuff for URI handling */
302 if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
303 GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
304 g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
306 if (!iface || !iface->get_type || !iface->get_protocols)
308 factory->uri_type = iface->get_type ();
309 if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
311 factory->uri_protocols = g_strdupv (iface->get_protocols ());
312 if (!factory->uri_protocols)
316 interfaces = g_type_interfaces (type, &n_interfaces);
317 for (i = 0; i < n_interfaces; i++) {
318 __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
322 GST_PLUGIN_FEATURE (factory)->plugin_name = g_strdup (plugin->desc.name);
323 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
324 GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
326 gst_registry_add_feature (gst_registry_get_default (),
327 GST_PLUGIN_FEATURE (factory));
332 gst_element_factory_cleanup (factory);
337 * gst_element_factory_create:
338 * @factory: factory to instantiate
339 * @name: name of new element
341 * Create a new element of the type defined by the given elementfactory.
342 * It will be given the name supplied, since all elements require a name as
343 * their first argument.
345 * Returns: new #GstElement or NULL if the element couldn't be created
348 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
351 GstElementClass *oclass;
352 GstElementFactory *newfactory;
354 g_return_val_if_fail (factory != NULL, NULL);
356 gst_object_ref (factory);
359 GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
361 if (newfactory == NULL) {
362 GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
365 gst_object_unref (factory);
366 factory = newfactory;
370 GST_INFO ("creating element \"%s\" named \"%s\"",
371 GST_PLUGIN_FEATURE_NAME (factory), GST_STR_NULL (name));
373 GST_INFO ("creating element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
376 if (factory->type == 0) {
377 g_critical ("Plugin didn't set object type in feature.");
383 /* FIXME: the object class gets a pointer to the factory that might
384 * be disposed at the end of this call if it was newly loaded;
385 * to fix that, we should ref and then unref in an object class finalize,
386 * which we don't have currently. */
387 oclass = GST_ELEMENT_CLASS (g_type_class_ref (factory->type));
388 if (oclass->elementfactory == NULL)
389 oclass->elementfactory = factory;
391 /* create an instance of the element */
392 element = GST_ELEMENT (g_object_new (factory->type, NULL));
393 g_assert (element != NULL);
395 g_type_class_unref (oclass);
398 gst_object_set_name (GST_OBJECT (element), name);
400 GST_DEBUG ("created element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
406 * gst_element_factory_make:
407 * @factoryname: a named factory to instantiate
408 * @name: name of new element
410 * Create a new element of the type defined by the given element factory.
411 * If name is NULL, then the element will receive a guaranteed unique name,
412 * consisting of the element factory name and a number.
413 * If name is given, it will be given the name supplied.
415 * Returns: new #GstElement or NULL if unable to create element
418 gst_element_factory_make (const gchar * factoryname, const gchar * name)
420 GstElementFactory *factory;
423 g_return_val_if_fail (factoryname != NULL, NULL);
425 GST_LOG ("gstelementfactory: make \"%s\" \"%s\"",
426 factoryname, GST_STR_NULL (name));
428 /* gst_plugin_load_element_factory (factoryname); */
429 factory = gst_element_factory_find (factoryname);
430 if (factory == NULL) {
431 GST_INFO ("no such element factory \"%s\"!", factoryname);
434 GST_LOG ("gstelementfactory: found factory %p", factory);
435 element = gst_element_factory_create (factory, name);
436 gst_object_unref (factory);
437 if (element == NULL) {
438 GST_INFO_OBJECT (factory, "couldn't create instance!");
446 __gst_element_factory_add_static_pad_template (GstElementFactory * factory,
447 GstStaticPadTemplate * templ)
449 g_return_if_fail (factory != NULL);
450 g_return_if_fail (templ != NULL);
452 factory->staticpadtemplates =
453 g_list_append (factory->staticpadtemplates, templ);
454 factory->numpadtemplates++;
458 * gst_element_factory_get_element_type:
459 * @factory: factory to get managed #GType from
461 * Get the #GType for elements managed by this factory
463 * Returns: the #GType for elements managed by this factory
466 gst_element_factory_get_element_type (GstElementFactory * factory)
468 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
470 return factory->type;
474 * gst_element_factory_get_longname:
475 * @factory: a #GstElementFactory
477 * Gets the longname for this factory
479 * Returns: the longname
481 G_CONST_RETURN gchar *
482 gst_element_factory_get_longname (GstElementFactory * factory)
484 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
486 return factory->details.longname;
490 * gst_element_factory_get_klass:
491 * @factory: a #GstElementFactory
493 * Gets the class for this factory.
497 G_CONST_RETURN gchar *
498 gst_element_factory_get_klass (GstElementFactory * factory)
500 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
502 return factory->details.klass;
506 * gst_element_factory_get_description:
507 * @factory: a #GstElementFactory
509 * Gets the description for this factory.
511 * Returns: the description
513 G_CONST_RETURN gchar *
514 gst_element_factory_get_description (GstElementFactory * factory)
516 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
518 return factory->details.description;
522 * gst_element_factory_get_author:
523 * @factory: a #GstElementFactory
525 * Gets the author for this factory.
527 * Returns: the author
529 G_CONST_RETURN gchar *
530 gst_element_factory_get_author (GstElementFactory * factory)
532 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
534 return factory->details.author;
538 * gst_element_factory_get_num_pad_templates:
539 * @factory: a #GstElementFactory
541 * Gets the number of pad_templates in this factory.
543 * Returns: the number of pad_templates
546 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
548 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
550 return factory->numpadtemplates;
554 * __gst_element_factory_add_interface:
555 * @elementfactory: The elementfactory to add the interface to
556 * @interfacename: Name of the interface
558 * Adds the given interfacename to the list of implemented interfaces of the
562 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
563 const gchar * interfacename)
565 g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
566 g_return_if_fail (interfacename != NULL);
567 g_return_if_fail (interfacename[0] != '\0'); /* no empty string */
569 elementfactory->interfaces =
570 g_list_prepend (elementfactory->interfaces, g_strdup (interfacename));
574 * gst_element_factory_get_static_pad_templates:
575 * @factory: a #GstElementFactory
577 * Gets the #GList of padtemplates for this factory.
579 * Returns: the padtemplates
581 G_CONST_RETURN GList *
582 gst_element_factory_get_static_pad_templates (GstElementFactory * factory)
584 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
586 return factory->staticpadtemplates;
590 * gst_element_factory_get_uri_type:
591 * @factory: a #GstElementFactory
593 * Gets the type of URIs the element supports or GST_URI_UNKNOWN if none.
595 * Returns: type of URIs this element supports
598 gst_element_factory_get_uri_type (GstElementFactory * factory)
600 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
602 return factory->uri_type;
606 * gst_element_factory_get_uri_protocols:
607 * @factory: a #GstElementFactory
609 * Gets a NULL-terminated array of protocols this element supports or NULL, if
610 * no protocols are supported. You may not change the contents of the returned
611 * array as it is still ownt by the element factory. Use g_strdupv() if you want to.
613 * Returns: the supported protocols or NULL
616 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
618 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
620 return factory->uri_protocols;