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 * gstdeviceproviderfactory.c: GstDeviceProviderFactory 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:gstdeviceproviderfactory
26 * @short_description: Create GstDeviceProviders from a factory
27 * @see_also: #GstDeviceProvider, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
29 * #GstDeviceProviderFactory is used to create instances of device providers. A
30 * GstDeviceProviderfactory can be added to a #GstPlugin as it is also a
33 * Use the gst_device_provider_factory_find() and
34 * gst_device_provider_factory_get() functions to create device
35 * provider instances or use gst_device_provider_factory_get_by_name() as a
36 * convenient shortcut.
45 #include "gst_private.h"
47 #include "gstdeviceproviderfactory.h"
50 #include "glib-compat-private.h"
52 GST_DEBUG_CATEGORY_STATIC (device_provider_factory_debug);
53 #define GST_CAT_DEFAULT device_provider_factory_debug
55 static void gst_device_provider_factory_finalize (GObject * object);
56 static void gst_device_provider_factory_cleanup (GstDeviceProviderFactory *
59 /* static guint gst_device_provider_factory_signals[LAST_SIGNAL] = { 0 }; */
61 /* this is defined in gstelement.c */
62 extern GQuark __gst_deviceproviderclass_factory;
66 GST_DEBUG_CATEGORY_INIT (device_provider_factory_debug, "GST_DEVICE_PROVIDER_FACTORY", \
67 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, \
68 "device provider factories keep information about installed device providers"); \
71 G_DEFINE_TYPE_WITH_CODE (GstDeviceProviderFactory, gst_device_provider_factory,
72 GST_TYPE_PLUGIN_FEATURE, _do_init);
75 gst_device_provider_factory_class_init (GstDeviceProviderFactoryClass * klass)
77 GObjectClass *gobject_class = (GObjectClass *) klass;
79 gobject_class->finalize = gst_device_provider_factory_finalize;
83 gst_device_provider_factory_init (GstDeviceProviderFactory * factory)
88 gst_device_provider_factory_finalize (GObject * object)
90 GstDeviceProviderFactory *factory = GST_DEVICE_PROVIDER_FACTORY (object);
91 GstDeviceProvider *provider;
93 gst_device_provider_factory_cleanup (factory);
95 provider = g_atomic_pointer_get (&factory->provider);
97 gst_object_unref (provider);
99 G_OBJECT_CLASS (gst_device_provider_factory_parent_class)->finalize (object);
103 * gst_device_provider_factory_find:
104 * @name: name of factory to find
106 * Search for an device provider factory of the given name. Refs the returned
107 * device provider factory; caller is responsible for unreffing.
109 * Returns: (transfer full) (nullable): #GstDeviceProviderFactory if
110 * found, %NULL otherwise
114 GstDeviceProviderFactory *
115 gst_device_provider_factory_find (const gchar * name)
117 GstPluginFeature *feature;
119 g_return_val_if_fail (name != NULL, NULL);
121 feature = gst_registry_find_feature (gst_registry_get (), name,
122 GST_TYPE_DEVICE_PROVIDER_FACTORY);
124 return GST_DEVICE_PROVIDER_FACTORY (feature);
126 /* this isn't an error, for instance when you query if an device provider factory is
128 GST_LOG ("no such device provider factory \"%s\"", name);
134 gst_device_provider_factory_cleanup (GstDeviceProviderFactory * factory)
136 if (factory->metadata) {
137 gst_structure_free ((GstStructure *) factory->metadata);
138 factory->metadata = NULL;
141 factory->type = G_TYPE_INVALID;
145 #define CHECK_METADATA_FIELD(klass, name, key) \
147 const gchar *metafield = gst_device_provider_class_get_metadata (klass, key); \
148 if (G_UNLIKELY (metafield == NULL || *metafield == '\0')) { \
149 g_warning ("Device provider factory metadata for '%s' has no valid %s field", name, key); \
155 * gst_device_provider_register:
156 * @plugin: (allow-none): #GstPlugin to register the device provider with, or %NULL for
157 * a static device provider.
158 * @name: name of device providers of this type
159 * @rank: rank of device provider (higher rank means more importance when autoplugging)
160 * @type: GType of device provider to register
162 * Create a new device providerfactory capable of instantiating objects of the
163 * @type and add the factory to @plugin.
165 * Returns: %TRUE, if the registering succeeded, %FALSE on error
170 gst_device_provider_register (GstPlugin * plugin, const gchar * name,
171 guint rank, GType type)
173 GstPluginFeature *existing_feature;
174 GstRegistry *registry;
175 GstDeviceProviderFactory *factory;
176 GstDeviceProviderClass *klass;
178 g_return_val_if_fail (name != NULL, FALSE);
179 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_DEVICE_PROVIDER), FALSE);
181 registry = gst_registry_get ();
183 /* check if feature already exists, if it exists there is no need to update it
184 * when the registry is getting updated, outdated plugins and all their
185 * features are removed and readded.
187 existing_feature = gst_registry_lookup_feature (registry, name);
188 if (existing_feature) {
189 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
190 existing_feature, name);
191 factory = GST_DEVICE_PROVIDER_FACTORY_CAST (existing_feature);
192 factory->type = type;
193 existing_feature->loaded = TRUE;
194 g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
195 gst_object_unref (existing_feature);
200 GST_DEVICE_PROVIDER_FACTORY_CAST (g_object_newv
201 (GST_TYPE_DEVICE_PROVIDER_FACTORY, 0, NULL));
202 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
203 GST_LOG_OBJECT (factory, "Created new device providerfactory for type %s",
206 /* provide info needed during class structure setup */
207 g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
208 klass = GST_DEVICE_PROVIDER_CLASS (g_type_class_ref (type));
210 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
211 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
212 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
213 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);
215 factory->type = type;
216 factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
218 if (plugin && plugin->desc.name) {
219 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
220 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
221 g_object_add_weak_pointer ((GObject *) plugin,
222 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
224 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
225 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
227 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
228 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
230 gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
237 gst_device_provider_factory_cleanup (factory);
243 * gst_device_provider_factory_get:
244 * @factory: factory to instantiate
246 * Returns the device provider of the type defined by the given device
249 * Returns: (transfer full) (nullable): the #GstDeviceProvider or %NULL
250 * if the device provider couldn't be created
255 gst_device_provider_factory_get (GstDeviceProviderFactory * factory)
257 GstDeviceProvider *device_provider;
258 GstDeviceProviderClass *oclass;
259 GstDeviceProviderFactory *newfactory;
261 g_return_val_if_fail (factory != NULL, NULL);
264 GST_DEVICE_PROVIDER_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
267 if (newfactory == NULL)
270 factory = newfactory;
272 GST_INFO ("getting device provider \"%s\"", GST_OBJECT_NAME (factory));
274 if (factory->type == 0)
277 device_provider = g_atomic_pointer_get (&newfactory->provider);
278 if (device_provider) {
279 gst_object_unref (factory);
280 return gst_object_ref (device_provider);
283 /* create an instance of the device provider, cast so we don't assert on NULL
284 * also set name as early as we can
286 device_provider = GST_DEVICE_PROVIDER_CAST (g_object_newv (factory->type, 0,
288 if (G_UNLIKELY (device_provider == NULL))
289 goto no_device_provider;
291 /* fill in the pointer to the factory in the device provider class. The
292 * class will not be unreffed currently.
293 * Be thread safe as there might be 2 threads creating the first instance of
294 * an device provider at the same moment
296 oclass = GST_DEVICE_PROVIDER_GET_CLASS (device_provider);
297 if (!g_atomic_pointer_compare_and_exchange (&oclass->factory, NULL, factory)) {
298 gst_object_unref (factory);
300 /* This ref will never be dropped as the class is never destroyed */
301 GST_OBJECT_FLAG_SET (factory, GST_OBJECT_FLAG_MAY_BE_LEAKED);
304 gst_object_ref_sink (device_provider);
306 /* We use an atomic to make sure we don't create two in parallel */
307 if (!g_atomic_pointer_compare_and_exchange (&newfactory->provider, NULL,
309 gst_object_unref (device_provider);
311 device_provider = g_atomic_pointer_get (&newfactory->provider);
314 GST_DEBUG ("created device provider \"%s\"", GST_OBJECT_NAME (factory));
316 return gst_object_ref (device_provider);
321 GST_WARNING_OBJECT (factory,
322 "loading plugin containing feature %s returned NULL!",
323 GST_OBJECT_NAME (factory));
328 GST_WARNING_OBJECT (factory, "factory has no type");
329 gst_object_unref (factory);
334 GST_WARNING_OBJECT (factory, "could not create device provider");
335 gst_object_unref (factory);
341 * gst_device_provider_factory_get_by_name:
342 * @factoryname: a named factory to instantiate
344 * Returns the device provider of the type defined by the given device
347 * Returns: (transfer full) (nullable): a #GstDeviceProvider or %NULL
348 * if unable to create device provider
353 gst_device_provider_factory_get_by_name (const gchar * factoryname)
355 GstDeviceProviderFactory *factory;
356 GstDeviceProvider *device_provider;
358 g_return_val_if_fail (factoryname != NULL, NULL);
359 g_return_val_if_fail (gst_is_initialized (), NULL);
361 GST_LOG ("gstdeviceproviderfactory: get_by_name \"%s\"", factoryname);
363 factory = gst_device_provider_factory_find (factoryname);
367 GST_LOG_OBJECT (factory, "found factory %p", factory);
368 device_provider = gst_device_provider_factory_get (factory);
369 if (device_provider == NULL)
372 gst_object_unref (factory);
373 return device_provider;
378 GST_INFO ("no such device provider factory \"%s\"!", factoryname);
383 GST_INFO_OBJECT (factory, "couldn't create instance!");
384 gst_object_unref (factory);
390 * gst_device_provider_factory_get_device_provider_type:
391 * @factory: factory to get managed #GType from
393 * Get the #GType for device providers managed by this factory. The type can
394 * only be retrieved if the device provider factory is loaded, which can be
395 * assured with gst_plugin_feature_load().
397 * Returns: the #GType for device providers managed by this factory.
402 gst_device_provider_factory_get_device_provider_type (GstDeviceProviderFactory *
405 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory),
408 return factory->type;
412 * gst_device_provider_factory_get_metadata:
413 * @factory: a #GstDeviceProviderFactory
416 * Get the metadata on @factory with @key.
418 * Returns: (nullable): the metadata with @key on @factory or %NULL
419 * when there was no metadata with the given @key.
424 gst_device_provider_factory_get_metadata (GstDeviceProviderFactory * factory,
427 return gst_structure_get_string ((GstStructure *) factory->metadata, key);
431 * gst_device_provider_factory_get_metadata_keys:
432 * @factory: a #GstDeviceProviderFactory
434 * Get the available keys for the metadata on @factory.
436 * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
437 * a %NULL-terminated array of key strings, or %NULL when there is no
438 * metadata. Free with g_strfreev() when no longer needed.
443 gst_device_provider_factory_get_metadata_keys (GstDeviceProviderFactory *
446 GstStructure *metadata;
450 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), NULL);
452 metadata = (GstStructure *) factory->metadata;
453 if (metadata == NULL)
456 num = gst_structure_n_fields (metadata);
460 arr = g_new (gchar *, num + 1);
461 for (i = 0; i < num; ++i) {
462 arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
469 * gst_device_provider_factory_has_classesv:
470 * @factory: a #GstDeviceProviderFactory
471 * @classes: (array zero-terminated=1) (allow-none): a %NULL terminated array
472 * of classes to match, only match if all classes are matched
474 * Check if @factory matches all of the given classes
476 * Returns: %TRUE if @factory matches.
481 gst_device_provider_factory_has_classesv (GstDeviceProviderFactory * factory,
486 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), FALSE);
488 klass = gst_device_provider_factory_get_metadata (factory,
489 GST_ELEMENT_METADATA_KLASS);
492 GST_ERROR_OBJECT (factory,
493 "device provider factory is missing klass identifiers");
497 for (; classes != NULL && classes[0] != NULL; classes++) {
501 if (classes[0] == '\0')
504 found = strstr (klass, classes[0]);
508 if (found != klass && *(found - 1) != '/')
511 len = strlen (classes[0]);
512 if (found[len] != 0 && found[len] != '/')
520 * gst_device_provider_factory_has_classes:
521 * @factory: a #GstDeviceProviderFactory
522 * @classes: (allow-none): a "/" separate list of classes to match, only match
523 * if all classes are matched
525 * Check if @factory matches all of the given @classes
527 * Returns: %TRUE if @factory matches or if @classes is %NULL.
532 gst_device_provider_factory_has_classes (GstDeviceProviderFactory * factory,
533 const gchar * classes)
541 classesv = g_strsplit (classes, "/", 0);
543 res = gst_device_provider_factory_has_classesv (factory, classesv);
545 g_strfreev (classesv);
551 device_provider_filter (GstPluginFeature * feature, GstRank * minrank)
553 /* we only care about device provider factories */
554 if (G_UNLIKELY (!GST_IS_DEVICE_PROVIDER_FACTORY (feature)))
557 return (gst_plugin_feature_get_rank (feature) >= *minrank);
561 * gst_device_provider_factory_list_get_device_providers:
562 * @minrank: Minimum rank
564 * Get a list of factories with a rank greater or equal to @minrank.
565 * The list of factories is returned by decreasing rank.
567 * Returns: (transfer full) (element-type Gst.DeviceProviderFactory):
568 * a #GList of #GstDeviceProviderFactory device providers. Use
569 * gst_plugin_feature_list_free() after usage.
574 gst_device_provider_factory_list_get_device_providers (GstRank minrank)
578 /* get the feature list using the filter */
579 result = gst_registry_feature_filter (gst_registry_get (),
580 (GstPluginFeatureFilter) device_provider_filter, FALSE, &minrank);
582 /* sort on rank and name */
583 result = g_list_sort (result, gst_plugin_feature_rank_compare_func);