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 gst_device_provider_factory_create()
34 * functions to create device provider instances or use gst_device_provider_factory_make() as a
35 * convenient shortcut.
44 #include "gst_private.h"
46 #include "gstdeviceproviderfactory.h"
49 #include "glib-compat-private.h"
51 GST_DEBUG_CATEGORY_STATIC (device_provider_factory_debug);
52 #define GST_CAT_DEFAULT device_provider_factory_debug
54 static void gst_device_provider_factory_finalize (GObject * object);
55 static void gst_device_provider_factory_cleanup (GstDeviceProviderFactory *
58 /* static guint gst_device_provider_factory_signals[LAST_SIGNAL] = { 0 }; */
60 /* this is defined in gstelement.c */
61 extern GQuark __gst_deviceproviderclass_factory;
65 GST_DEBUG_CATEGORY_INIT (device_provider_factory_debug, "GST_DEVICE_PROVIDER_FACTORY", \
66 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, \
67 "device provider factories keep information about installed device providers"); \
70 G_DEFINE_TYPE_WITH_CODE (GstDeviceProviderFactory, gst_device_provider_factory,
71 GST_TYPE_PLUGIN_FEATURE, _do_init);
74 gst_device_provider_factory_class_init (GstDeviceProviderFactoryClass * klass)
76 GObjectClass *gobject_class = (GObjectClass *) klass;
78 gobject_class->finalize = gst_device_provider_factory_finalize;
82 gst_device_provider_factory_init (GstDeviceProviderFactory * factory)
87 gst_device_provider_factory_finalize (GObject * object)
89 GstDeviceProviderFactory *factory = GST_DEVICE_PROVIDER_FACTORY (object);
90 GstDeviceProvider *provider;
92 gst_device_provider_factory_cleanup (factory);
94 provider = g_atomic_pointer_get (&factory->provider);
96 gst_object_unref (provider);
98 G_OBJECT_CLASS (gst_device_provider_factory_parent_class)->finalize (object);
102 * gst_device_provider_factory_find:
103 * @name: name of factory to find
105 * Search for an device provider factory of the given name. Refs the returned
106 * device provider factory; caller is responsible for unreffing.
108 * Returns: (transfer full) (nullable): #GstDeviceProviderFactory if
109 * found, %NULL otherwise
113 GstDeviceProviderFactory *
114 gst_device_provider_factory_find (const gchar * name)
116 GstPluginFeature *feature;
118 g_return_val_if_fail (name != NULL, NULL);
120 feature = gst_registry_find_feature (gst_registry_get (), name,
121 GST_TYPE_DEVICE_PROVIDER_FACTORY);
123 return GST_DEVICE_PROVIDER_FACTORY (feature);
125 /* this isn't an error, for instance when you query if an device provider factory is
127 GST_LOG ("no such device provider factory \"%s\"", name);
133 gst_device_provider_factory_cleanup (GstDeviceProviderFactory * factory)
135 if (factory->metadata) {
136 gst_structure_free ((GstStructure *) factory->metadata);
137 factory->metadata = NULL;
140 factory->type = G_TYPE_INVALID;
144 #define CHECK_METADATA_FIELD(klass, name, key) \
146 const gchar *metafield = gst_device_provider_class_get_metadata (klass, key); \
147 if (G_UNLIKELY (metafield == NULL || *metafield == '\0')) { \
148 g_warning ("Device provider factory metadata for '%s' has no valid %s field", name, key); \
154 * gst_device_provider_register:
155 * @plugin: (allow-none): #GstPlugin to register the device provider with, or %NULL for
156 * a static device provider.
157 * @name: name of device providers of this type
158 * @rank: rank of device provider (higher rank means more importance when autoplugging)
159 * @type: GType of device provider to register
161 * Create a new device providerfactory capable of instantiating objects of the
162 * @type and add the factory to @plugin.
164 * Returns: %TRUE, if the registering succeeded, %FALSE on error
169 gst_device_provider_register (GstPlugin * plugin, const gchar * name,
170 guint rank, GType type)
172 GstPluginFeature *existing_feature;
173 GstRegistry *registry;
174 GstDeviceProviderFactory *factory;
175 GstDeviceProviderClass *klass;
177 g_return_val_if_fail (name != NULL, FALSE);
178 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_DEVICE_PROVIDER), FALSE);
180 registry = gst_registry_get ();
182 /* check if feature already exists, if it exists there is no need to update it
183 * when the registry is getting updated, outdated plugins and all their
184 * features are removed and readded.
186 existing_feature = gst_registry_lookup_feature (registry, name);
187 if (existing_feature) {
188 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
189 existing_feature, name);
190 factory = GST_DEVICE_PROVIDER_FACTORY_CAST (existing_feature);
191 factory->type = type;
192 existing_feature->loaded = TRUE;
193 g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
194 gst_object_unref (existing_feature);
199 GST_DEVICE_PROVIDER_FACTORY_CAST (g_object_newv
200 (GST_TYPE_DEVICE_PROVIDER_FACTORY, 0, NULL));
201 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
202 GST_LOG_OBJECT (factory, "Created new device providerfactory for type %s",
205 /* provide info needed during class structure setup */
206 g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
207 klass = GST_DEVICE_PROVIDER_CLASS (g_type_class_ref (type));
209 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
210 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
211 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
212 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);
214 factory->type = type;
215 factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
217 if (plugin && plugin->desc.name) {
218 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
219 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
220 g_object_add_weak_pointer ((GObject *) plugin,
221 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
223 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
224 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
226 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
227 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
229 gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
236 gst_device_provider_factory_cleanup (factory);
242 * gst_device_provider_factory_get:
243 * @factory: factory to instantiate
245 * Returns the device provider of the type defined by the given device
248 * Returns: (transfer full) (nullable): the #GstDeviceProvider or %NULL
249 * if the device provider couldn't be created
254 gst_device_provider_factory_get (GstDeviceProviderFactory * factory)
256 GstDeviceProvider *device_provider;
257 GstDeviceProviderClass *oclass;
258 GstDeviceProviderFactory *newfactory;
260 g_return_val_if_fail (factory != NULL, NULL);
263 GST_DEVICE_PROVIDER_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
266 if (newfactory == NULL)
269 factory = newfactory;
271 GST_INFO ("getting device provider \"%s\"", GST_OBJECT_NAME (factory));
273 if (factory->type == 0)
276 device_provider = g_atomic_pointer_get (&newfactory->provider);
278 return gst_object_ref (device_provider);
280 /* create an instance of the device provider, cast so we don't assert on NULL
281 * also set name as early as we can
283 device_provider = GST_DEVICE_PROVIDER_CAST (g_object_newv (factory->type, 0,
285 if (G_UNLIKELY (device_provider == NULL))
286 goto no_device_provider;
288 /* fill in the pointer to the factory in the device provider class. The
289 * class will not be unreffed currently.
290 * Be thread safe as there might be 2 threads creating the first instance of
291 * an device provider at the same moment
293 oclass = GST_DEVICE_PROVIDER_GET_CLASS (device_provider);
294 if (!g_atomic_pointer_compare_and_exchange (&oclass->factory, NULL, factory))
295 gst_object_unref (factory);
297 gst_object_ref_sink (device_provider);
299 /* We use an atomic to make sure we don't create two in parallel */
300 if (!g_atomic_pointer_compare_and_exchange (&newfactory->provider, NULL,
302 gst_object_unref (device_provider);
304 device_provider = g_atomic_pointer_get (&newfactory->provider);
307 GST_DEBUG ("created device provider \"%s\"", GST_OBJECT_NAME (factory));
309 return gst_object_ref (device_provider);
314 GST_WARNING_OBJECT (factory,
315 "loading plugin containing feature %s returned NULL!",
316 GST_OBJECT_NAME (factory));
321 GST_WARNING_OBJECT (factory, "factory has no type");
322 gst_object_unref (factory);
327 GST_WARNING_OBJECT (factory, "could not create device provider");
328 gst_object_unref (factory);
334 * gst_device_provider_factory_get_by_name:
335 * @factoryname: a named factory to instantiate
337 * Returns the device provider of the type defined by the given device
340 * Returns: (transfer full) (nullable): a #GstDeviceProvider or %NULL
341 * if unable to create device provider
346 gst_device_provider_factory_get_by_name (const gchar * factoryname)
348 GstDeviceProviderFactory *factory;
349 GstDeviceProvider *device_provider;
351 g_return_val_if_fail (factoryname != NULL, NULL);
352 g_return_val_if_fail (gst_is_initialized (), NULL);
354 GST_LOG ("gstdeviceproviderfactory: get_by_name \"%s\"", factoryname);
356 factory = gst_device_provider_factory_find (factoryname);
360 GST_LOG_OBJECT (factory, "found factory %p", factory);
361 device_provider = gst_device_provider_factory_get (factory);
362 if (device_provider == NULL)
365 gst_object_unref (factory);
366 return device_provider;
371 GST_INFO ("no such device provider factory \"%s\"!", factoryname);
376 GST_INFO_OBJECT (factory, "couldn't create instance!");
377 gst_object_unref (factory);
383 * gst_device_provider_factory_get_device_provider_type:
384 * @factory: factory to get managed #GType from
386 * Get the #GType for device providers managed by this factory. The type can
387 * only be retrieved if the device provider factory is loaded, which can be
388 * assured with gst_plugin_feature_load().
390 * Returns: the #GType for device providers managed by this factory or 0 if
391 * the factory is not loaded.
396 gst_device_provider_factory_get_device_provider_type (GstDeviceProviderFactory *
399 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), 0);
401 return factory->type;
405 * gst_device_provider_factory_get_metadata:
406 * @factory: a #GstDeviceProviderFactory
409 * Get the metadata on @factory with @key.
411 * Returns: (nullable): the metadata with @key on @factory or %NULL
412 * when there was no metadata with the given @key.
417 gst_device_provider_factory_get_metadata (GstDeviceProviderFactory * factory,
420 return gst_structure_get_string ((GstStructure *) factory->metadata, key);
424 * gst_device_provider_factory_get_metadata_keys:
425 * @factory: a #GstDeviceProviderFactory
427 * Get the available keys for the metadata on @factory.
429 * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
430 * a %NULL-terminated array of key strings, or %NULL when there is no
431 * metadata. Free with g_strfreev() when no longer needed.
436 gst_device_provider_factory_get_metadata_keys (GstDeviceProviderFactory *
439 GstStructure *metadata;
443 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), NULL);
445 metadata = (GstStructure *) factory->metadata;
446 if (metadata == NULL)
449 num = gst_structure_n_fields (metadata);
453 arr = g_new (gchar *, num + 1);
454 for (i = 0; i < num; ++i) {
455 arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
462 * gst_device_provider_factory_has_classesv:
463 * @factory: a #GstDeviceProviderFactory
464 * @classes: (array zero-terminated=1): a %NULL terminated array of
465 * klasses to match, only match if all classes are matched
467 * Check if @factory matches all of the given classes
469 * Returns: %TRUE if @factory matches.
474 gst_device_provider_factory_has_classesv (GstDeviceProviderFactory * factory,
479 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), FALSE);
481 klass = gst_device_provider_factory_get_metadata (factory,
482 GST_ELEMENT_METADATA_KLASS);
485 GST_ERROR_OBJECT (factory,
486 "device provider factory is missing klass identifiers");
490 for (; classes[0]; classes++) {
494 if (classes[0] == '\0')
497 found = strstr (klass, classes[0]);
501 if (found != klass && *(found - 1) != '/')
504 len = strlen (classes[0]);
505 if (found[len] != 0 && found[len] != '/')
513 * gst_device_provider_factory_has_classes:
514 * @factory: a #GstDeviceProviderFactory
515 * @classes: a "/" separate list of klasses to match, only match if all classes
518 * Check if @factory matches all of the given @classes
520 * Returns: %TRUE if @factory matches.
525 gst_device_provider_factory_has_classes (GstDeviceProviderFactory * factory,
526 const gchar * classes)
531 classesv = g_strsplit (classes, "/", 0);
533 res = gst_device_provider_factory_has_classesv (factory, classesv);
535 g_strfreev (classesv);
547 device_provider_filter (GstPluginFeature * feature, FilterData * data)
551 /* we only care about device provider factories */
552 if (G_UNLIKELY (!GST_IS_DEVICE_PROVIDER_FACTORY (feature)))
555 res = (gst_plugin_feature_get_rank (feature) >= data->minrank) &&
556 gst_device_provider_factory_has_classes (GST_DEVICE_PROVIDER_FACTORY_CAST
557 (feature), data->classes);
563 * gst_device_provider_factory_list_get_device_providers:
564 * @classes: a "/" separate list of klasses to match, only match if all classes
566 * @minrank: Minimum rank
568 * Get a list of factories that match all of the given @classes. Only
569 * device providers with a rank greater or equal to @minrank will be
570 * returned. The list of factories is returned by decreasing rank.
572 * Returns: (transfer full) (element-type Gst.DeviceProviderFactory): a #GList of
573 * #GstDeviceProviderFactory device providers. Use gst_plugin_feature_list_free() after
578 GList *gst_device_provider_factory_list_get_device_providers
579 (const gchar * classes, GstRank minrank)
585 data.classes = classes;
586 data.minrank = minrank;
588 /* get the feature list using the filter */
589 result = gst_registry_feature_filter (gst_registry_get (),
590 (GstPluginFeatureFilter) device_provider_filter, FALSE, &data);
592 /* sort on rank and name */
593 result = g_list_sort (result, gst_plugin_feature_rank_compare_func);