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.
25 * SECTION:gstelementfactory
26 * @short_description: Create GstElements from a factory
27 * @see_also: #GstElement, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
29 * #GstElementFactory is used to create instances of elements. A
30 * GstElementfactory can be added to a #GstPlugin as it is also a
33 * Use the gst_element_factory_find() and gst_element_factory_create()
34 * functions to create element instances or use gst_element_factory_make() as a
35 * convenient shortcut.
37 * The following code example shows you how to create a GstFileSrc element.
40 * <title>Using an element factory</title>
41 * <programlisting language="c">
42 * #include <gst/gst.h>
44 * GstElementFactory *srcfactory;
45 * gst_init(&argc,&argv);
46 * srcfactory = gst_element_factory_find("filesrc");
47 * g_return_if_fail(srcfactory != NULL);
48 * src = gst_element_factory_create(srcfactory,"src");
49 * g_return_if_fail(src != NULL);
54 * Last reviewed on 2005-11-23 (0.9.5)
57 #include "gst_private.h"
59 #include "gstelement.h"
62 #include "gstregistry.h"
64 GST_DEBUG_CATEGORY_STATIC (element_factory_debug);
65 #define GST_CAT_DEFAULT element_factory_debug
67 static void gst_element_factory_class_init (GstElementFactoryClass * klass);
68 static void gst_element_factory_init (GstElementFactory * factory);
69 static void gst_element_factory_finalize (GObject * object);
70 void __gst_element_details_clear (GstElementDetails * dp);
71 static void gst_element_factory_cleanup (GstElementFactory * factory);
73 static GstPluginFeatureClass *parent_class = NULL;
75 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
78 gst_element_factory_get_type (void)
80 static GType elementfactory_type = 0;
82 if (G_UNLIKELY (elementfactory_type == 0)) {
83 static const GTypeInfo elementfactory_info = {
84 sizeof (GstElementFactoryClass),
87 (GClassInitFunc) gst_element_factory_class_init,
90 sizeof (GstElementFactory),
92 (GInstanceInitFunc) gst_element_factory_init,
96 elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
97 "GstElementFactory", &elementfactory_info, 0);
98 GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY",
99 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
100 "element factories keep information about installed elements");
102 return elementfactory_type;
105 gst_element_factory_class_init (GstElementFactoryClass * klass)
107 GObjectClass *gobject_class;
108 GstObjectClass *gstobject_class;
109 GstPluginFeatureClass *gstpluginfeature_class;
111 gobject_class = (GObjectClass *) klass;
112 gstobject_class = (GstObjectClass *) klass;
113 gstpluginfeature_class = (GstPluginFeatureClass *) klass;
115 parent_class = g_type_class_peek_parent (klass);
117 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_element_factory_finalize);
121 gst_element_factory_init (GstElementFactory * factory)
123 factory->staticpadtemplates = NULL;
124 factory->numpadtemplates = 0;
126 factory->uri_type = GST_URI_UNKNOWN;
127 factory->uri_protocols = NULL;
129 factory->interfaces = NULL;
133 gst_element_factory_finalize (GObject * object)
135 GstElementFactory *factory = GST_ELEMENT_FACTORY (object);
137 gst_element_factory_cleanup (factory);
138 G_OBJECT_CLASS (parent_class)->finalize (object);
142 * gst_element_factory_find:
143 * @name: name of factory to find
145 * Search for an element factory of the given name. Refs the returned
146 * element factory; caller is responsible for unreffing.
148 * Returns: #GstElementFactory if found, NULL otherwise
151 gst_element_factory_find (const gchar * name)
153 GstPluginFeature *feature;
155 g_return_val_if_fail (name != NULL, NULL);
157 feature = gst_registry_find_feature (gst_registry_get_default (), name,
158 GST_TYPE_ELEMENT_FACTORY);
160 return GST_ELEMENT_FACTORY (feature);
162 /* this isn't an error, for instance when you query if an element factory is
164 GST_LOG ("no such element factory \"%s\"", name);
169 __gst_element_details_clear (GstElementDetails * dp)
171 g_free (dp->longname);
175 g_free (dp->description);
176 dp->description = NULL;
181 #define VALIDATE_SET(__dest, __src, __entry) \
183 if (g_utf8_validate (__src->__entry, -1, NULL)) { \
184 __dest->__entry = g_strdup (__src->__entry); \
186 g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s", \
188 __dest->__entry = g_strdup ("[ERROR: invalid UTF-8]"); \
193 __gst_element_details_set (GstElementDetails * dest,
194 const GstElementDetails * src)
196 VALIDATE_SET (dest, src, longname);
197 VALIDATE_SET (dest, src, klass);
198 VALIDATE_SET (dest, src, description);
199 VALIDATE_SET (dest, src, author);
203 __gst_element_details_copy (GstElementDetails * dest,
204 const GstElementDetails * src)
206 __gst_element_details_clear (dest);
207 __gst_element_details_set (dest, src);
211 gst_element_factory_cleanup (GstElementFactory * factory)
215 __gst_element_details_clear (&factory->details);
217 g_type_class_unref (g_type_class_peek (factory->type));
221 for (item = factory->staticpadtemplates; item; item = item->next) {
222 GstStaticPadTemplate *templ = item->data;
224 g_free (templ->name_template);
225 g_free ((gchar *) templ->static_caps.string);
226 memset (&(templ->static_caps), 0, sizeof (GstStaticCaps));
229 g_list_free (factory->staticpadtemplates);
230 factory->staticpadtemplates = NULL;
231 factory->numpadtemplates = 0;
232 factory->uri_type = GST_URI_UNKNOWN;
233 if (factory->uri_protocols) {
234 g_strfreev (factory->uri_protocols);
235 factory->uri_protocols = NULL;
238 g_list_foreach (factory->interfaces, (GFunc) g_free, NULL);
239 g_list_free (factory->interfaces);
240 factory->interfaces = NULL;
244 * gst_element_register:
245 * @plugin: #GstPlugin to register the element with
246 * @name: name of elements of this type
247 * @rank: rank of element (higher rank means more importance when autoplugging)
248 * @type: GType of element to register
250 * Create a new elementfactory capable of instantiating objects of the
251 * @type and add the factory to @plugin.
253 * Returns: TRUE, if the registering succeeded, FALSE on error
256 gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
259 GstElementFactory *factory;
261 guint n_interfaces, i;
262 GstElementClass *klass;
265 g_return_val_if_fail (name != NULL, FALSE);
266 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
268 factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
269 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
270 GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
273 klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
274 factory->type = type;
275 __gst_element_details_copy (&factory->details, &klass->details);
276 for (item = klass->padtemplates; item; item = item->next) {
277 GstPadTemplate *templ = item->data;
278 GstStaticPadTemplate *newt;
280 newt = g_new0 (GstStaticPadTemplate, 1);
281 newt->name_template = g_strdup (templ->name_template);
282 newt->direction = templ->direction;
283 newt->presence = templ->presence;
284 newt->static_caps.string = gst_caps_to_string (templ->caps);
285 factory->staticpadtemplates =
286 g_list_append (factory->staticpadtemplates, newt);
288 factory->numpadtemplates = klass->numpadtemplates;
289 klass->elementfactory = factory;
291 /* special stuff for URI handling */
292 if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
293 GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
294 g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
296 if (!iface || !iface->get_type || !iface->get_protocols)
298 factory->uri_type = iface->get_type ();
299 if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
301 factory->uri_protocols = g_strdupv (iface->get_protocols ());
302 if (!factory->uri_protocols)
306 interfaces = g_type_interfaces (type, &n_interfaces);
307 for (i = 0; i < n_interfaces; i++) {
308 __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
312 GST_PLUGIN_FEATURE (factory)->plugin_name = g_strdup (plugin->desc.name);
313 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
314 GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
316 gst_registry_add_feature (gst_registry_get_default (),
317 GST_PLUGIN_FEATURE (factory));
324 GST_WARNING_OBJECT (factory, "error with uri handler!");
325 gst_element_factory_cleanup (factory);
331 * gst_element_factory_create:
332 * @factory: factory to instantiate
333 * @name: name of new element
335 * Create a new element of the type defined by the given elementfactory.
336 * It will be given the name supplied, since all elements require a name as
337 * their first argument.
339 * Returns: new #GstElement or NULL if the element couldn't be created
342 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
345 GstElementClass *oclass;
346 GstElementFactory *newfactory;
348 g_return_val_if_fail (factory != NULL, NULL);
350 gst_object_ref (factory);
353 GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
355 if (newfactory == NULL)
358 gst_object_unref (factory);
359 factory = newfactory;
362 GST_INFO ("creating element \"%s\" named \"%s\"",
363 GST_PLUGIN_FEATURE_NAME (factory), GST_STR_NULL (name));
365 GST_INFO ("creating element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
367 if (factory->type == 0)
370 /* create an instance of the element, cast so we don't assert on NULL */
371 element = GST_ELEMENT_CAST (g_object_new (factory->type, NULL));
375 /* fill in the pointer to the factory in the element class. The
376 * class will not be unreffed currently. */
377 oclass = GST_ELEMENT_GET_CLASS (element);
378 if (oclass->elementfactory == NULL)
379 oclass->elementfactory = factory;
382 gst_object_set_name (GST_OBJECT (element), name);
384 GST_DEBUG ("created element \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
391 GST_WARNING_OBJECT (factory, "loading plugin returned NULL!");
396 GST_WARNING_OBJECT (factory, "factory has no type");
401 GST_WARNING_OBJECT (factory, "could not create element");
407 * gst_element_factory_make:
408 * @factoryname: a named factory to instantiate
409 * @name: name of new element
411 * Create a new element of the type defined by the given element factory.
412 * If name is NULL, then the element will receive a guaranteed unique name,
413 * consisting of the element factory name and a number.
414 * If name is given, it will be given the name supplied.
416 * Returns: new #GstElement or NULL if unable to create element
419 gst_element_factory_make (const gchar * factoryname, const gchar * name)
421 GstElementFactory *factory;
424 g_return_val_if_fail (factoryname != NULL, NULL);
426 GST_LOG ("gstelementfactory: make \"%s\" \"%s\"",
427 factoryname, GST_STR_NULL (name));
429 factory = gst_element_factory_find (factoryname);
433 GST_LOG_OBJECT (factory, "found factory %p", factory);
434 element = gst_element_factory_create (factory, name);
435 gst_object_unref (factory);
444 GST_INFO ("no such element factory \"%s\"!", factoryname);
449 GST_INFO_OBJECT (factory, "couldn't create instance!");
455 __gst_element_factory_add_static_pad_template (GstElementFactory * factory,
456 GstStaticPadTemplate * templ)
458 g_return_if_fail (factory != NULL);
459 g_return_if_fail (templ != NULL);
461 factory->staticpadtemplates =
462 g_list_append (factory->staticpadtemplates, templ);
463 factory->numpadtemplates++;
467 * gst_element_factory_get_element_type:
468 * @factory: factory to get managed #GType from
470 * Get the #GType for elements managed by this factory. The type can
471 * only be retrieved if the element factory is loaded, which can be
472 * assured with gst_plugin_feature_load().
474 * Returns: the #GType for elements managed by this factory or 0 if
475 * the factory is not loaded.
478 gst_element_factory_get_element_type (GstElementFactory * factory)
480 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
482 return factory->type;
486 * gst_element_factory_get_longname:
487 * @factory: a #GstElementFactory
489 * Gets the longname for this factory
491 * Returns: the longname
493 G_CONST_RETURN gchar *
494 gst_element_factory_get_longname (GstElementFactory * factory)
496 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
498 return factory->details.longname;
502 * gst_element_factory_get_klass:
503 * @factory: a #GstElementFactory
505 * Gets the class for this factory.
509 G_CONST_RETURN gchar *
510 gst_element_factory_get_klass (GstElementFactory * factory)
512 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
514 return factory->details.klass;
518 * gst_element_factory_get_description:
519 * @factory: a #GstElementFactory
521 * Gets the description for this factory.
523 * Returns: the description
525 G_CONST_RETURN gchar *
526 gst_element_factory_get_description (GstElementFactory * factory)
528 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
530 return factory->details.description;
534 * gst_element_factory_get_author:
535 * @factory: a #GstElementFactory
537 * Gets the author for this factory.
539 * Returns: the author
541 G_CONST_RETURN gchar *
542 gst_element_factory_get_author (GstElementFactory * factory)
544 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
546 return factory->details.author;
550 * gst_element_factory_get_num_pad_templates:
551 * @factory: a #GstElementFactory
553 * Gets the number of pad_templates in this factory.
555 * Returns: the number of pad_templates
558 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
560 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
562 return factory->numpadtemplates;
566 * __gst_element_factory_add_interface:
567 * @elementfactory: The elementfactory to add the interface to
568 * @interfacename: Name of the interface
570 * Adds the given interfacename to the list of implemented interfaces of the
574 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
575 const gchar * interfacename)
577 g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
578 g_return_if_fail (interfacename != NULL);
579 g_return_if_fail (interfacename[0] != '\0'); /* no empty string */
581 elementfactory->interfaces =
582 g_list_prepend (elementfactory->interfaces, g_strdup (interfacename));
586 * gst_element_factory_get_static_pad_templates:
587 * @factory: a #GstElementFactory
589 * Gets the #GList of padtemplates for this factory.
591 * Returns: the padtemplates
593 G_CONST_RETURN GList *
594 gst_element_factory_get_static_pad_templates (GstElementFactory * factory)
596 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
598 return factory->staticpadtemplates;
602 * gst_element_factory_get_uri_type:
603 * @factory: a #GstElementFactory
605 * Gets the type of URIs the element supports or GST_URI_UNKNOWN if none.
607 * Returns: type of URIs this element supports
610 gst_element_factory_get_uri_type (GstElementFactory * factory)
612 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
614 return factory->uri_type;
618 * gst_element_factory_get_uri_protocols:
619 * @factory: a #GstElementFactory
621 * Gets a NULL-terminated array of protocols this element supports or NULL, if
622 * no protocols are supported. You may not change the contents of the returned
623 * array as it is still ownt by the element factory. Use g_strdupv() if you want to.
625 * Returns: the supported protocols or NULL
628 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
630 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
632 return factory->uri_protocols;