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 * @title: GstDeviceProviderFactory
27 * @short_description: Create GstDeviceProviders from a factory
28 * @see_also: #GstDeviceProvider, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
30 * #GstDeviceProviderFactory is used to create instances of device providers. A
31 * GstDeviceProviderfactory can be added to a #GstPlugin as it is also a
34 * Use the gst_device_provider_factory_find() and
35 * gst_device_provider_factory_get() functions to create device
36 * provider instances or use gst_device_provider_factory_get_by_name() as a
37 * convenient shortcut.
46 #include "gst_private.h"
48 #include "gstdeviceproviderfactory.h"
51 #include "glib-compat-private.h"
53 GST_DEBUG_CATEGORY_STATIC (device_provider_factory_debug);
54 #define GST_CAT_DEFAULT device_provider_factory_debug
56 static void gst_device_provider_factory_finalize (GObject * object);
57 static void gst_device_provider_factory_cleanup (GstDeviceProviderFactory *
60 /* static guint gst_device_provider_factory_signals[LAST_SIGNAL] = { 0 }; */
62 /* this is defined in gstelement.c */
63 extern GQuark __gst_deviceproviderclass_factory;
67 GST_DEBUG_CATEGORY_INIT (device_provider_factory_debug, "GST_DEVICE_PROVIDER_FACTORY", \
68 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, \
69 "device provider factories keep information about installed device providers"); \
72 G_DEFINE_TYPE_WITH_CODE (GstDeviceProviderFactory, gst_device_provider_factory,
73 GST_TYPE_PLUGIN_FEATURE, _do_init);
76 gst_device_provider_factory_class_init (GstDeviceProviderFactoryClass * klass)
78 GObjectClass *gobject_class = (GObjectClass *) klass;
80 gobject_class->finalize = gst_device_provider_factory_finalize;
84 gst_device_provider_factory_init (GstDeviceProviderFactory * factory)
89 gst_device_provider_factory_finalize (GObject * object)
91 GstDeviceProviderFactory *factory = GST_DEVICE_PROVIDER_FACTORY (object);
92 GstDeviceProvider *provider;
94 gst_device_provider_factory_cleanup (factory);
96 provider = g_atomic_pointer_get (&factory->provider);
98 gst_object_unref (provider);
100 G_OBJECT_CLASS (gst_device_provider_factory_parent_class)->finalize (object);
104 * gst_device_provider_factory_find:
105 * @name: name of factory to find
107 * Search for an device provider factory of the given name. Refs the returned
108 * device provider factory; caller is responsible for unreffing.
110 * Returns: (transfer full) (nullable): #GstDeviceProviderFactory if
111 * found, %NULL otherwise
115 GstDeviceProviderFactory *
116 gst_device_provider_factory_find (const gchar * name)
118 GstPluginFeature *feature;
120 g_return_val_if_fail (name != NULL, NULL);
122 feature = gst_registry_find_feature (gst_registry_get (), name,
123 GST_TYPE_DEVICE_PROVIDER_FACTORY);
125 return GST_DEVICE_PROVIDER_FACTORY (feature);
127 /* this isn't an error, for instance when you query if an device provider factory is
129 GST_LOG ("no such device provider factory \"%s\"", name);
135 gst_device_provider_factory_cleanup (GstDeviceProviderFactory * factory)
137 if (factory->metadata) {
138 gst_structure_free ((GstStructure *) factory->metadata);
139 factory->metadata = NULL;
142 factory->type = G_TYPE_INVALID;
146 #define CHECK_METADATA_FIELD(klass, name, key) \
148 const gchar *metafield = gst_device_provider_class_get_metadata (klass, key); \
149 if (G_UNLIKELY (metafield == NULL || *metafield == '\0')) { \
150 g_warning ("Device provider factory metadata for '%s' has no valid %s field", name, key); \
156 * gst_device_provider_register:
157 * @plugin: (allow-none): #GstPlugin to register the device provider with, or %NULL for
158 * a static device provider.
159 * @name: name of device providers of this type
160 * @rank: rank of device provider (higher rank means more importance when autoplugging)
161 * @type: GType of device provider to register
163 * Create a new device providerfactory capable of instantiating objects of the
164 * @type and add the factory to @plugin.
166 * Returns: %TRUE, if the registering succeeded, %FALSE on error
171 gst_device_provider_register (GstPlugin * plugin, const gchar * name,
172 guint rank, GType type)
174 GstPluginFeature *existing_feature;
175 GstRegistry *registry;
176 GstDeviceProviderFactory *factory;
177 GstDeviceProviderClass *klass;
179 g_return_val_if_fail (name != NULL, FALSE);
180 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_DEVICE_PROVIDER), FALSE);
182 registry = gst_registry_get ();
184 /* check if feature already exists, if it exists there is no need to update it
185 * when the registry is getting updated, outdated plugins and all their
186 * features are removed and readded.
188 existing_feature = gst_registry_lookup_feature (registry, name);
189 if (existing_feature) {
190 GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
191 existing_feature, name);
192 factory = GST_DEVICE_PROVIDER_FACTORY_CAST (existing_feature);
193 factory->type = type;
194 existing_feature->loaded = TRUE;
195 g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
196 gst_object_unref (existing_feature);
201 GST_DEVICE_PROVIDER_FACTORY_CAST (g_object_newv
202 (GST_TYPE_DEVICE_PROVIDER_FACTORY, 0, NULL));
203 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
204 GST_LOG_OBJECT (factory, "Created new device providerfactory for type %s",
207 /* provide info needed during class structure setup */
208 g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
209 klass = GST_DEVICE_PROVIDER_CLASS (g_type_class_ref (type));
211 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
212 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
213 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
214 CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);
216 factory->type = type;
217 factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
219 if (plugin && plugin->desc.name) {
220 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
221 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
222 g_object_add_weak_pointer ((GObject *) plugin,
223 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
225 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
226 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
228 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
229 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
231 gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
238 gst_device_provider_factory_cleanup (factory);
244 * gst_device_provider_factory_get:
245 * @factory: factory to instantiate
247 * Returns the device provider of the type defined by the given device
250 * Returns: (transfer full) (nullable): the #GstDeviceProvider or %NULL
251 * if the device provider couldn't be created
256 gst_device_provider_factory_get (GstDeviceProviderFactory * factory)
258 GstDeviceProvider *device_provider;
259 GstDeviceProviderClass *oclass;
260 GstDeviceProviderFactory *newfactory;
262 g_return_val_if_fail (factory != NULL, NULL);
265 GST_DEVICE_PROVIDER_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
268 if (newfactory == NULL)
271 factory = newfactory;
273 GST_INFO ("getting device provider \"%s\"", GST_OBJECT_NAME (factory));
275 if (factory->type == 0)
278 device_provider = g_atomic_pointer_get (&newfactory->provider);
279 if (device_provider) {
280 gst_object_unref (factory);
281 return gst_object_ref (device_provider);
284 /* create an instance of the device provider, cast so we don't assert on NULL
285 * also set name as early as we can
287 device_provider = GST_DEVICE_PROVIDER_CAST (g_object_newv (factory->type, 0,
289 if (G_UNLIKELY (device_provider == NULL))
290 goto no_device_provider;
292 /* fill in the pointer to the factory in the device provider class. The
293 * class will not be unreffed currently.
294 * Be thread safe as there might be 2 threads creating the first instance of
295 * an device provider at the same moment
297 oclass = GST_DEVICE_PROVIDER_GET_CLASS (device_provider);
298 if (!g_atomic_pointer_compare_and_exchange (&oclass->factory, NULL, factory)) {
299 gst_object_unref (factory);
301 /* This ref will never be dropped as the class is never destroyed */
302 GST_OBJECT_FLAG_SET (factory, GST_OBJECT_FLAG_MAY_BE_LEAKED);
305 gst_object_ref_sink (device_provider);
307 /* We use an atomic to make sure we don't create two in parallel */
308 if (!g_atomic_pointer_compare_and_exchange (&newfactory->provider, NULL,
310 gst_object_unref (device_provider);
312 device_provider = g_atomic_pointer_get (&newfactory->provider);
315 GST_DEBUG ("created device provider \"%s\"", GST_OBJECT_NAME (factory));
317 return gst_object_ref (device_provider);
322 GST_WARNING_OBJECT (factory,
323 "loading plugin containing feature %s returned NULL!",
324 GST_OBJECT_NAME (factory));
329 GST_WARNING_OBJECT (factory, "factory has no type");
330 gst_object_unref (factory);
335 GST_WARNING_OBJECT (factory, "could not create device provider");
336 gst_object_unref (factory);
342 * gst_device_provider_factory_get_by_name:
343 * @factoryname: a named factory to instantiate
345 * Returns the device provider of the type defined by the given device
348 * Returns: (transfer full) (nullable): a #GstDeviceProvider or %NULL
349 * if unable to create device provider
354 gst_device_provider_factory_get_by_name (const gchar * factoryname)
356 GstDeviceProviderFactory *factory;
357 GstDeviceProvider *device_provider;
359 g_return_val_if_fail (factoryname != NULL, NULL);
360 g_return_val_if_fail (gst_is_initialized (), NULL);
362 GST_LOG ("gstdeviceproviderfactory: get_by_name \"%s\"", factoryname);
364 factory = gst_device_provider_factory_find (factoryname);
368 GST_LOG_OBJECT (factory, "found factory %p", factory);
369 device_provider = gst_device_provider_factory_get (factory);
370 if (device_provider == NULL)
373 gst_object_unref (factory);
374 return device_provider;
379 GST_INFO ("no such device provider factory \"%s\"!", factoryname);
384 GST_INFO_OBJECT (factory, "couldn't create instance!");
385 gst_object_unref (factory);
391 * gst_device_provider_factory_get_device_provider_type:
392 * @factory: factory to get managed #GType from
394 * Get the #GType for device providers managed by this factory. The type can
395 * only be retrieved if the device provider factory is loaded, which can be
396 * assured with gst_plugin_feature_load().
398 * Returns: the #GType for device providers managed by this factory.
403 gst_device_provider_factory_get_device_provider_type (GstDeviceProviderFactory *
406 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory),
409 return factory->type;
413 * gst_device_provider_factory_get_metadata:
414 * @factory: a #GstDeviceProviderFactory
417 * Get the metadata on @factory with @key.
419 * Returns: (nullable): the metadata with @key on @factory or %NULL
420 * when there was no metadata with the given @key.
425 gst_device_provider_factory_get_metadata (GstDeviceProviderFactory * factory,
428 return gst_structure_get_string ((GstStructure *) factory->metadata, key);
432 * gst_device_provider_factory_get_metadata_keys:
433 * @factory: a #GstDeviceProviderFactory
435 * Get the available keys for the metadata on @factory.
437 * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
438 * a %NULL-terminated array of key strings, or %NULL when there is no
439 * metadata. Free with g_strfreev() when no longer needed.
444 gst_device_provider_factory_get_metadata_keys (GstDeviceProviderFactory *
447 GstStructure *metadata;
451 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), NULL);
453 metadata = (GstStructure *) factory->metadata;
454 if (metadata == NULL)
457 num = gst_structure_n_fields (metadata);
461 arr = g_new (gchar *, num + 1);
462 for (i = 0; i < num; ++i) {
463 arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
470 * gst_device_provider_factory_has_classesv:
471 * @factory: a #GstDeviceProviderFactory
472 * @classes: (array zero-terminated=1) (allow-none): a %NULL terminated array
473 * of classes to match, only match if all classes are matched
475 * Check if @factory matches all of the given classes
477 * Returns: %TRUE if @factory matches.
482 gst_device_provider_factory_has_classesv (GstDeviceProviderFactory * factory,
487 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), FALSE);
489 klass = gst_device_provider_factory_get_metadata (factory,
490 GST_ELEMENT_METADATA_KLASS);
493 GST_ERROR_OBJECT (factory,
494 "device provider factory is missing klass identifiers");
498 for (; classes != NULL && classes[0] != NULL; classes++) {
502 if (classes[0] == '\0')
505 found = strstr (klass, classes[0]);
509 if (found != klass && *(found - 1) != '/')
512 len = strlen (classes[0]);
513 if (found[len] != 0 && found[len] != '/')
521 * gst_device_provider_factory_has_classes:
522 * @factory: a #GstDeviceProviderFactory
523 * @classes: (allow-none): a "/" separate list of classes to match, only match
524 * if all classes are matched
526 * Check if @factory matches all of the given @classes
528 * Returns: %TRUE if @factory matches or if @classes is %NULL.
533 gst_device_provider_factory_has_classes (GstDeviceProviderFactory * factory,
534 const gchar * classes)
542 classesv = g_strsplit (classes, "/", 0);
544 res = gst_device_provider_factory_has_classesv (factory, classesv);
546 g_strfreev (classesv);
552 device_provider_filter (GstPluginFeature * feature, GstRank * minrank)
554 /* we only care about device provider factories */
555 if (G_UNLIKELY (!GST_IS_DEVICE_PROVIDER_FACTORY (feature)))
558 return (gst_plugin_feature_get_rank (feature) >= *minrank);
562 * gst_device_provider_factory_list_get_device_providers:
563 * @minrank: Minimum rank
565 * Get a list of factories with a rank greater or equal to @minrank.
566 * The list of factories is returned by decreasing rank.
568 * Returns: (transfer full) (element-type Gst.DeviceProviderFactory):
569 * a #GList of #GstDeviceProviderFactory device providers. Use
570 * gst_plugin_feature_list_free() after usage.
575 gst_device_provider_factory_list_get_device_providers (GstRank minrank)
579 /* get the feature list using the filter */
580 result = gst_registry_feature_filter (gst_registry_get (),
581 (GstPluginFeatureFilter) device_provider_filter, FALSE, &minrank);
583 /* sort on rank and name */
584 result = g_list_sort (result, gst_plugin_feature_rank_compare_func);