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;
41 /* static guint gst_element_factory_signals[LAST_SIGNAL] = { 0 }; */
44 gst_element_factory_get_type (void)
46 static GType elementfactory_type = 0;
48 if (!elementfactory_type) {
49 static const GTypeInfo elementfactory_info = {
50 sizeof (GstElementFactoryClass),
53 (GClassInitFunc) gst_element_factory_class_init,
56 sizeof (GstElementFactory),
58 (GInstanceInitFunc) gst_element_factory_init,
62 elementfactory_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
63 "GstElementFactory", &elementfactory_info, 0);
64 GST_DEBUG_CATEGORY_INIT (element_factory_debug, "GST_ELEMENT_FACTORY",
65 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
66 "element factories keep information about installed elements");
68 return elementfactory_type;
71 gst_element_factory_class_init (GstElementFactoryClass * klass)
73 GObjectClass *gobject_class;
74 GstObjectClass *gstobject_class;
75 GstPluginFeatureClass *gstpluginfeature_class;
77 gobject_class = (GObjectClass *) klass;
78 gstobject_class = (GstObjectClass *) klass;
79 gstpluginfeature_class = (GstPluginFeatureClass *) klass;
81 parent_class = g_type_class_peek_parent (klass);
83 gstpluginfeature_class->unload_thyself =
84 GST_DEBUG_FUNCPTR (gst_element_factory_unload_thyself);
87 gst_element_factory_init (GstElementFactory * factory)
89 factory->padtemplates = NULL;
90 factory->numpadtemplates = 0;
92 factory->uri_type = GST_URI_UNKNOWN;
93 factory->uri_protocols = NULL;
95 factory->interfaces = NULL;
99 * gst_element_factory_find:
100 * @name: name of factory to find
102 * Search for an element factory of the given name.
104 * Returns: #GstElementFactory if found, NULL otherwise
107 gst_element_factory_find (const gchar * name)
109 GstPluginFeature *feature;
111 g_return_val_if_fail (name != NULL, NULL);
113 feature = gst_registry_pool_find_feature (name, GST_TYPE_ELEMENT_FACTORY);
115 return GST_ELEMENT_FACTORY (feature);
117 /* this isn't an error, for instance when you query if an element factory is
119 GST_LOG ("no such element factory \"%s\"", name);
124 __gst_element_details_clear (GstElementDetails * dp)
126 g_free (dp->longname);
130 g_free (dp->description);
131 dp->description = NULL;
137 __gst_element_details_set (GstElementDetails * dest,
138 const GstElementDetails * src)
140 dest->longname = g_strdup (src->longname);
141 dest->klass = g_strdup (src->klass);
142 dest->description = g_strdup (src->description);
143 dest->author = g_strdup (src->author);
147 __gst_element_details_copy (GstElementDetails * dest,
148 const GstElementDetails * src)
150 __gst_element_details_clear (dest);
151 __gst_element_details_set (dest, src);
154 gst_element_factory_cleanup (GstElementFactory * factory)
156 __gst_element_details_clear (&factory->details);
158 g_type_class_unref (g_type_class_peek (factory->type));
162 g_list_foreach (factory->padtemplates, (GFunc) g_object_unref, NULL);
163 g_list_free (factory->padtemplates);
164 factory->padtemplates = NULL;
165 factory->numpadtemplates = 0;
166 factory->uri_type = GST_URI_UNKNOWN;
167 if (factory->uri_protocols) {
168 g_strfreev (factory->uri_protocols);
169 factory->uri_protocols = NULL;
172 g_list_foreach (factory->interfaces, (GFunc) g_free, NULL);
173 g_list_free (factory->interfaces);
174 factory->interfaces = NULL;
178 * gst_element_register:
180 * @name: name of elements of this type
181 * @rank: rank of element (higher rank means more importance when autoplugging)
182 * @type: GType of element to register
184 * Create a new elementfactory capable of insantiating objects of the
187 * Returns: TRUE, if the registering succeeded, FALSE on error
190 gst_element_register (GstPlugin * plugin, const gchar * name, guint rank,
193 GstElementFactory *factory;
195 guint n_interfaces, i;
196 GstElementClass *klass;
198 g_return_val_if_fail (name != NULL, FALSE);
199 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_ELEMENT), FALSE);
201 factory = gst_element_factory_find (name);
204 klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
206 GST_ELEMENT_FACTORY (g_object_new (GST_TYPE_ELEMENT_FACTORY, NULL));
207 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
208 GST_LOG_OBJECT (factory, "Created new elementfactory for type %s",
211 g_return_val_if_fail (factory->type == 0, FALSE);
212 klass = GST_ELEMENT_CLASS (g_type_class_ref (type));
213 gst_element_factory_cleanup (factory);
214 GST_LOG_OBJECT (factory, "Reuse existing elementfactory for type %s",
218 factory->type = type;
219 __gst_element_details_copy (&factory->details, &klass->details);
220 factory->padtemplates = g_list_copy (klass->padtemplates);
221 g_list_foreach (factory->padtemplates, (GFunc) g_object_ref, NULL);
222 factory->numpadtemplates = klass->numpadtemplates;
224 /* special stuff for URI handling */
225 if (g_type_is_a (type, GST_TYPE_URI_HANDLER)) {
226 GstURIHandlerInterface *iface = (GstURIHandlerInterface *)
227 g_type_interface_peek (klass, GST_TYPE_URI_HANDLER);
229 if (!iface || !iface->get_type || !iface->get_protocols)
231 factory->uri_type = iface->get_type ();
232 if (!GST_URI_TYPE_IS_VALID (factory->uri_type))
234 factory->uri_protocols = g_strdupv (iface->get_protocols ());
235 if (!factory->uri_protocols)
239 interfaces = g_type_interfaces (type, &n_interfaces);
240 for (i = 0; i < n_interfaces; i++) {
241 __gst_element_factory_add_interface (factory, g_type_name (interfaces[i]));
245 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
246 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
251 gst_element_factory_cleanup (factory);
256 * gst_element_factory_create:
257 * @factory: factory to instantiate
258 * @name: name of new element
260 * Create a new element of the type defined by the given elementfactory.
261 * It will be given the name supplied, since all elements require a name as
262 * their first argument.
264 * Returns: new #GstElement or NULL if the element couldn't be created
267 gst_element_factory_create (GstElementFactory * factory, const gchar * name)
270 GstElementClass *oclass;
272 g_return_val_if_fail (factory != NULL, NULL);
274 if (!gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
275 GST_INFO ("could not load element factory for element \"%s\"", name);
280 GST_INFO ("creating \"%s\" named \"%s\"", GST_PLUGIN_FEATURE_NAME (factory),
281 GST_STR_NULL (name));
283 GST_INFO ("creating \"%s\"", GST_PLUGIN_FEATURE_NAME (factory));
285 if (factory->type == 0) {
286 g_critical ("Factory for `%s' has no type",
287 GST_PLUGIN_FEATURE_NAME (factory));
291 oclass = GST_ELEMENT_CLASS (g_type_class_ref (factory->type));
292 if (oclass->elementfactory == NULL)
293 oclass->elementfactory = factory;
295 /* create an instance of the element */
296 element = GST_ELEMENT (g_object_new (factory->type, NULL));
297 g_assert (element != NULL);
299 g_type_class_unref (oclass);
301 gst_object_set_name (GST_OBJECT (element), name);
307 * gst_element_factory_make:
308 * @factoryname: a named factory to instantiate
309 * @name: name of new element
311 * Create a new element of the type defined by the given element factory.
312 * If name is NULL, then the element will receive a guaranteed unique name,
313 * consisting of the element factory name and a number.
314 * If name is given, it will be given the name supplied.
316 * Returns: new #GstElement or NULL if unable to create element
319 gst_element_factory_make (const gchar * factoryname, const gchar * name)
321 GstElementFactory *factory;
324 g_return_val_if_fail (factoryname != NULL, NULL);
326 GST_LOG ("gstelementfactory: make \"%s\" \"%s\"",
327 factoryname, GST_STR_NULL (name));
329 /* gst_plugin_load_element_factory (factoryname); */
330 factory = gst_element_factory_find (factoryname);
331 if (factory == NULL) {
332 GST_INFO ("no such element factory \"%s\"!", factoryname);
335 element = gst_element_factory_create (factory, name);
336 if (element == NULL) {
337 GST_INFO_OBJECT (factory, "couldn't create instance!");
345 __gst_element_factory_add_pad_template (GstElementFactory * factory,
346 GstPadTemplate * templ)
348 g_return_if_fail (factory != NULL);
349 g_return_if_fail (templ != NULL);
351 gst_object_ref (GST_OBJECT (templ));
352 gst_object_sink (GST_OBJECT (templ));
354 factory->padtemplates = g_list_append (factory->padtemplates, templ);
355 factory->numpadtemplates++;
359 * gst_element_factory_get_element_type:
360 * @factory: factory to get managed #GType from
362 * Get the #GType for elements managed by this factory
364 * Returns: the #GType for elements managed by this factory
367 gst_element_factory_get_element_type (GstElementFactory * factory)
369 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
371 return factory->type;
375 * gst_element_factory_get_longname:
376 * @factory: a #GstElementFactory
378 * Gets the longname for this factory
380 * Returns: the longname
382 G_CONST_RETURN gchar *
383 gst_element_factory_get_longname (GstElementFactory * factory)
385 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
387 return factory->details.longname;
391 * gst_element_factory_get_class:
392 * @factory: a #GstElementFactory
394 * Gets the class for this factory.
398 G_CONST_RETURN gchar *
399 gst_element_factory_get_klass (GstElementFactory * factory)
401 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
403 return factory->details.klass;
407 * gst_element_factory_get_description:
408 * @factory: a #GstElementFactory
410 * Gets the description for this factory.
412 * Returns: the description
414 G_CONST_RETURN gchar *
415 gst_element_factory_get_description (GstElementFactory * factory)
417 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
419 return factory->details.description;
423 * gst_element_factory_get_author:
424 * @factory: a #GstElementFactory
426 * Gets the author for this factory.
428 * Returns: the author
430 G_CONST_RETURN gchar *
431 gst_element_factory_get_author (GstElementFactory * factory)
433 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
435 return factory->details.author;
439 * gst_element_factory_get_num_pad_templates:
440 * @factory: a #GstElementFactory
442 * Gets the number of pad_templates in this factory.
444 * Returns: the number of pad_templates
447 gst_element_factory_get_num_pad_templates (GstElementFactory * factory)
449 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), 0);
451 return factory->numpadtemplates;
455 * __gst_element_factory_add_interface:
456 * @elementfactory: The elementfactory to add the interface to
457 * @interfacename: Name of the interface
459 * Adds the given interfacename to the list of implemented interfaces of the
463 __gst_element_factory_add_interface (GstElementFactory * elementfactory,
464 const gchar * interfacename)
466 g_return_if_fail (GST_IS_ELEMENT_FACTORY (elementfactory));
467 g_return_if_fail (interfacename != NULL);
468 g_return_if_fail (interfacename[0] != '\0'); /* no empty string */
470 elementfactory->interfaces =
471 g_list_prepend (elementfactory->interfaces, g_strdup (interfacename));
475 * gst_element_factory_get_pad_templates:
476 * @factory: a #GstElementFactory
478 * Gets the #GList of padtemplates for this factory.
480 * Returns: the padtemplates
482 G_CONST_RETURN GList *
483 gst_element_factory_get_pad_templates (GstElementFactory * factory)
485 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
487 return factory->padtemplates;
491 * gst_element_factory_get_uri_type:
492 * @factory: a #GstElementFactory
494 * Gets the type of URIs the element supports or GST_URI_UNKNOWN if none.
496 * Returns: type of URIs this element supports
499 gst_element_factory_get_uri_type (GstElementFactory * factory)
501 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), GST_URI_UNKNOWN);
503 return factory->uri_type;
507 * gst_element_factory_get_uri_protocols:
508 * @factory: a #GstElementFactory
510 * Gets a NULL-terminated array of protocols this element supports or NULL, if
511 * no protocols are supported. You may not change the contents of the returned
512 * array as it is still ownt by the element factory. Use g_strdupv() if you want to.
514 * Returns: the supported protocols or NULL
517 gst_element_factory_get_uri_protocols (GstElementFactory * factory)
519 g_return_val_if_fail (GST_IS_ELEMENT_FACTORY (factory), NULL);
521 return factory->uri_protocols;
525 * gst_element_factory_can_src_caps :
526 * @factory: factory to query
527 * @caps: the caps to check
529 * Checks if the factory can source the given capability.
531 * Returns: true if it can src the capabilities
534 gst_element_factory_can_src_caps (GstElementFactory * factory,
535 const GstCaps * caps)
539 g_return_val_if_fail (factory != NULL, FALSE);
540 g_return_val_if_fail (caps != NULL, FALSE);
542 templates = factory->padtemplates;
545 GstPadTemplate *template = (GstPadTemplate *) templates->data;
547 if (template->direction == GST_PAD_SRC) {
548 if (gst_caps_is_always_compatible (GST_PAD_TEMPLATE_CAPS (template),
552 templates = g_list_next (templates);
559 * gst_element_factory_can_sink_caps :
560 * @factory: factory to query
561 * @caps: the caps to check
563 * Checks if the factory can sink the given capability.
565 * Returns: true if it can sink the capabilities
568 gst_element_factory_can_sink_caps (GstElementFactory * factory,
569 const GstCaps * caps)
573 g_return_val_if_fail (factory != NULL, FALSE);
574 g_return_val_if_fail (caps != NULL, FALSE);
576 templates = factory->padtemplates;
579 GstPadTemplate *template = (GstPadTemplate *) templates->data;
581 if (template->direction == GST_PAD_SINK) {
582 if (gst_caps_is_always_compatible (caps,
583 GST_PAD_TEMPLATE_CAPS (template)))
586 templates = g_list_next (templates);
592 gst_element_factory_unload_thyself (GstPluginFeature * feature)
594 GstElementFactory *factory;
596 factory = GST_ELEMENT_FACTORY (feature);
599 g_type_class_unref (g_type_class_peek (factory->type));