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 #include "gst_private.h"
26 #include "gstelement.h"
27 #include "gstregistrypool.h"
31 GST_DEBUG_CATEGORY_STATIC (element_factory_debug);
32 #define GST_CAT_DEFAULT element_factory_debug
34 static void gst_element_factory_class_init (GstElementFactoryClass *klass);
35 static void gst_element_factory_init (GstElementFactory *factory);
37 static void gst_element_factory_unload_thyself (GstPluginFeature *feature);
39 static GstPluginFeatureClass *parent_class = NULL;
40 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
43 gst_element_factory_get_type (void)
45 static GType elementfactory_type = 0;
47 if (!elementfactory_type) {
48 static const GTypeInfo elementfactory_info = {
49 sizeof (GstElementFactoryClass),
52 (GClassInitFunc) gst_element_factory_class_init,
55 sizeof(GstElementFactory),
57 (GInstanceInitFunc) gst_element_factory_init,
60 elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
61 "GstElementFactory", &elementfactory_info, 0);
62 GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY",
63 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
64 "element factories keep information about installed elements");
66 return elementfactory_type;
69 gst_element_factory_class_init (GstElementFactoryClass *klass)
71 GObjectClass *gobject_class;
72 GstObjectClass *gstobject_class;
73 GstPluginFeatureClass *gstpluginfeature_class;
75 gobject_class = (GObjectClass*)klass;
76 gstobject_class = (GstObjectClass*)klass;
77 gstpluginfeature_class = (GstPluginFeatureClass*) klass;
79 parent_class = g_type_class_peek_parent (klass);
81 gstpluginfeature_class->unload_thyself = GST_DEBUG_FUNCPTR (gst_element_factory_unload_thyself);
84 gst_element_factory_init (GstElementFactory *factory)
86 factory->padtemplates = NULL;
87 factory->numpadtemplates = 0;
89 factory->uri_type = GST_URI_UNKNOWN;
90 factory->uri_protocols = NULL;
92 factory->interfaces = NULL;
95 * gst_element_factory_find:
96 * @name: name of factory to find
98 * Search for an element factory of the given name.
100 * Returns: #GstElementFactory if found, NULL otherwise
103 gst_element_factory_find (const gchar *name)
105 GstPluginFeature *feature;
107 g_return_val_if_fail(name != NULL, NULL);
109 feature = gst_registry_pool_find_feature (name, GST_TYPE_ELEMENT_FACTORY);
111 return GST_ELEMENT_FACTORY (feature);
113 /* this should be an ERROR */
114 GST_DEBUG ("no such elementfactory \"%s\"", name);
119 __gst_element_details_clear (GstElementDetails *dp)
121 g_free (dp->longname);
125 g_free (dp->description);
126 dp->description = NULL;
131 __gst_element_details_set (GstElementDetails *dest, const GstElementDetails *src)
133 dest->longname = g_strdup (src->longname);
134 dest->klass = g_strdup (src->klass);
135 dest->description = g_strdup (src->description);
136 dest->author = g_strdup (src->author);
139 __gst_element_details_copy (GstElementDetails *dest, const GstElementDetails *src)
141 __gst_element_details_clear (dest);
142 __gst_element_details_set (dest, src);
145 gst_element_factory_cleanup (GstElementFactory *factory)
147 __gst_element_details_clear (&factory->details);
149 g_type_class_unref (g_type_class_peek (factory->type));
153 g_list_foreach (factory->padtemplates, (GFunc) g_object_unref, NULL);
154 g_list_free (factory->padtemplates);
155 factory->padtemplates = NULL;
156 factory->numpadtemplates = 0;
157 factory->uri_type = GST_URI_UNKNOWN;
158 if (factory->uri_protocols) {
159 g_strfreev (factory->uri_protocols);
160 factory->uri_protocols = NULL;
163 g_list_foreach (factory->interfaces, (GFunc) g_free, NULL);
164 g_list_free (factory->interfaces);
165 factory->interfaces = NULL;
168 * gst_element_register:
170 * @name: name of elements of this type
171 * @rank: rank of element (higher rank means more importance when autoplugging)
172 * @type: GType of element to register
174 * Create a new elementfactory capable of insantiating objects of the
177 * Returns: TRUE, if the registering succeeded, FALSE on error
180 gst_element_register (GstPlugin *plugin, const gchar *name, guint rank, GType type)
182 GstElementFactory *factory;
184 guint n_interfaces, i;
185 GstElementClass *klass;
187 g_return_val_if_fail (name != NULL, FALSE);
188 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
190 factory = gst_element_factory_find (name);
193 klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
194 factory = GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
195 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
196 GST_LOG_OBJECT (factory, "Created new elementfactory for type %s", g_type_name (type));
198 g_return_val_if_fail (factory->type == 0, FALSE);
199 klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
200 gst_element_factory_cleanup (factory);
201 GST_LOG_OBJECT (factory, "Reuse existing elementfactory for type %s", g_type_name (type));
204 factory->type = type;
205 __gst_element_details_copy (&factory->details, &klass->details);
206 factory->padtemplates = g_list_copy (klass->padtemplates);
207 g_list_foreach (factory->padtemplates, (GFunc) g_object_ref, NULL);
208 factory->numpadtemplates = klass->numpadtemplates;
210 /* special stuff for URI handling */
211 if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
212 GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
213 g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
214 if (!iface || !iface->get_type || !iface->get_protocols)
216 factory->uri_type = iface->get_type ();
217 if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
219 factory->uri_protocols = g_strdupv (iface->get_protocols ());
220 if (!factory->uri_protocols)
224 interfaces = g_type_interfaces (type, &n_interfaces);
225 for (i = 0; i < n_interfaces; i++) {
226 __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
230 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
231 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
236 gst_element_factory_cleanup (factory);
240 * gst_element_factory_create:
241 * @factory: factory to instantiate
242 * @name: name of new element
244 * Create a new element of the type defined by the given elementfactory.
245 * It will be given the name supplied, since all elements require a name as
246 * their first argument.
248 * Returns: new #GstElement or NULL if the element couldn't be created
251 gst_element_factory_create (GstElementFactory *factory,
255 GstElementClass *oclass;
257 g_return_val_if_fail (factory != NULL, NULL);
259 if (!gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
260 GST_INFO ("could not load element factory for element \"%s\"", name);
264 GST_LOG_OBJECT (factory, "creating element (name \"%s\", type %d)",
265 GST_STR_NULL (name), (gint) factory->type);
267 if (factory->type == 0) {
268 g_critical ("Factory for `%s' has no type",
269 GST_PLUGIN_FEATURE_NAME (factory));
273 oclass = GST_ELEMENT_CLASS (g_type_class_ref (factory->type));
274 if (oclass->elementfactory == NULL) {
275 GST_DEBUG ("class %s", GST_PLUGIN_FEATURE_NAME (factory));
276 oclass->elementfactory = factory;
279 /* create an instance of the element */
280 element = GST_ELEMENT (g_object_new (factory->type, NULL));
281 g_assert (element != NULL);
283 g_type_class_unref (oclass);
285 gst_object_set_name (GST_OBJECT (element), name);
290 * gst_element_factory_make:
291 * @factoryname: a named factory to instantiate
292 * @name: name of new element
294 * Create a new element of the type defined by the given element factory.
295 * If name is NULL, then the element will receive a guaranteed unique name,
296 * consisting of the element factory name and a number.
297 * If name is given, it will be given the name supplied.
299 * Returns: new #GstElement or NULL if unable to create element
302 gst_element_factory_make (const gchar *factoryname, const gchar *name)
304 GstElementFactory *factory;
307 g_return_val_if_fail (factoryname != NULL, NULL);
309 GST_LOG ("gstelementfactory: make \"%s\" \"%s\"",
310 factoryname, GST_STR_NULL (name));
312 /* gst_plugin_load_element_factory (factoryname); */
313 factory = gst_element_factory_find (factoryname);
314 if (factory == NULL) {
315 GST_INFO ("no such element factory \"%s\"!",
319 element = gst_element_factory_create (factory, name);
320 if (element == NULL) {
321 GST_INFO_OBJECT (factory, "couldn't create instance!");
328 __gst_element_factory_add_pad_template (GstElementFactory *factory,
329 GstPadTemplate *templ)
331 g_return_if_fail (factory != NULL);
332 g_return_if_fail (templ != NULL);
334 gst_object_ref (GST_OBJECT (templ));
335 gst_object_sink (GST_OBJECT (templ));
337 factory->padtemplates = g_list_append (factory->padtemplates, templ);
338 factory->numpadtemplates++;
341 * gst_element_factory_get_element_type:
342 * @factory: factory to get managed #GType from
344 * Get the #GType for elements managed by this factory
346 * Returns: the #GType for elements managed by this factory
349 gst_element_factory_get_element_type (GstElementFactory *factory)
351 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
353 return factory->type;
356 * gst_element_factory_get_longname:
357 * @factory: a #GstElementFactory
359 * Gets the longname for this factory
361 * Returns: the longname
363 G_CONST_RETURN gchar *
364 gst_element_factory_get_longname (GstElementFactory *factory)
366 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
368 return factory->details.longname;
371 * gst_element_factory_get_class:
372 * @factory: a #GstElementFactory
374 * Gets the class for this factory.
378 G_CONST_RETURN gchar *
379 gst_element_factory_get_klass (GstElementFactory *factory)
381 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
383 return factory->details.klass;
386 * gst_element_factory_get_description:
387 * @factory: a #GstElementFactory
389 * Gets the description for this factory.
391 * Returns: the description
393 G_CONST_RETURN gchar *
394 gst_element_factory_get_description (GstElementFactory *factory)
396 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
398 return factory->details.description;
401 * gst_element_factory_get_author:
402 * @factory: a #GstElementFactory
404 * Gets the author for this factory.
406 * Returns: the author
408 G_CONST_RETURN gchar *
409 gst_element_factory_get_author (GstElementFactory *factory)
411 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
413 return factory->details.author;
416 * gst_element_factory_get_num_pad_templates:
417 * @factory: a #GstElementFactory
419 * Gets the number of pad_templates in this factory.
421 * Returns: the number of pad_templates
424 gst_element_factory_get_num_pad_templates (GstElementFactory *factory)
426 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
428 return factory->numpadtemplates;
431 * __gst_element_factory_add_interface:
432 * @elementfactory: The elementfactory to add the interface to
433 * @interfacename: Name of the interface
435 * Adds the given interfacename to the list of implemented interfaces of the
439 __gst_element_factory_add_interface (GstElementFactory *elementfactory, const gchar *interfacename)
441 g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
442 g_return_if_fail (interfacename != NULL);
443 g_return_if_fail (interfacename[0] != '\0'); /* no empty string */
445 elementfactory->interfaces = g_list_prepend (elementfactory->interfaces, g_strdup (interfacename));
448 * gst_element_factory_get_pad_templates:
449 * @factory: a #GstElementFactory
451 * Gets the #GList of padtemplates for this factory.
453 * Returns: the padtemplates
455 G_CONST_RETURN GList *
456 gst_element_factory_get_pad_templates (GstElementFactory *factory)
458 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
460 return factory->padtemplates;
463 * gst_element_factory_get_uri_type:
464 * @factory: a #GstElementFactory
466 * Gets the type of URIs the element supports or GST_URI_UNKNOWN if none.
468 * Returns: type of URIs this element supports
471 gst_element_factory_get_uri_type (GstElementFactory *factory)
473 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
475 return factory->uri_type;
478 * gst_element_factory_get_uri_protocols:
479 * @factory: a #GstElementFactory
481 * Gets a NULL-terminated array of protocols this element supports or NULL, if
482 * no protocols are supported. You may not change the contents of the returned
483 * array as it is still ownt by the element factory. Use g_strdupv() if you want to.
485 * Returns: the supported protocols or NULL
488 gst_element_factory_get_uri_protocols (GstElementFactory *factory)
490 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
492 return factory->uri_protocols;
495 * gst_element_factory_can_src_caps :
496 * @factory: factory to query
497 * @caps: the caps to check
499 * Checks if the factory can source the given capability.
501 * Returns: true if it can src the capabilities
504 gst_element_factory_can_src_caps (GstElementFactory *factory,
509 g_return_val_if_fail(factory != NULL, FALSE);
510 g_return_val_if_fail(caps != NULL, FALSE);
512 templates = factory->padtemplates;
515 GstPadTemplate *template = (GstPadTemplate *)templates->data;
517 if (template->direction == GST_PAD_SRC) {
518 if (gst_caps_is_always_compatible (GST_PAD_TEMPLATE_CAPS (template), caps))
521 templates = g_list_next (templates);
527 * gst_element_factory_can_sink_caps :
528 * @factory: factory to query
529 * @caps: the caps to check
531 * Checks if the factory can sink the given capability.
533 * Returns: true if it can sink the capabilities
536 gst_element_factory_can_sink_caps (GstElementFactory *factory,
541 g_return_val_if_fail(factory != NULL, FALSE);
542 g_return_val_if_fail(caps != NULL, FALSE);
544 templates = factory->padtemplates;
547 GstPadTemplate *template = (GstPadTemplate *)templates->data;
549 if (template->direction == GST_PAD_SINK) {
550 if (gst_caps_is_always_compatible (caps, GST_PAD_TEMPLATE_CAPS (template)))
553 templates = g_list_next (templates);
559 gst_element_factory_unload_thyself (GstPluginFeature *feature)
561 GstElementFactory *factory;
563 factory = GST_ELEMENT_FACTORY (feature);
566 g_type_class_unref (g_type_class_peek (factory->type));