1 /* GStreamer
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>
5 *
6 * gstdeviceproviderfactory.c: GstDeviceProviderFactory object, support routines
7 *
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.
12 *
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.
17 *
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.
22 */
23
24 /**
25 * SECTION:gstdeviceproviderfactory
26 * @title: GstDeviceProviderFactory
27 * @short_description: Create GstDeviceProviders from a factory
28 * @see_also: #GstDeviceProvider, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
29 *
30 * #GstDeviceProviderFactory is used to create instances of device providers. A
31 * GstDeviceProviderfactory can be added to a #GstPlugin as it is also a
32 * #GstPluginFeature.
33 *
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.
38 *
39 * Since: 1.4
40 */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "gst_private.h"
47
48 #include "gstdeviceproviderfactory.h"
49 #include "gst.h"
50
51 #include "glib-compat-private.h"
52
53 GST_DEBUG_CATEGORY_STATIC (device_provider_factory_debug);
54 #define GST_CAT_DEFAULT device_provider_factory_debug
55
56 static void gst_device_provider_factory_finalize (GObject * object);
57 static void gst_device_provider_factory_cleanup (GstDeviceProviderFactory *
58 factory);
59
60 /* static guint gst_device_provider_factory_signals[LAST_SIGNAL] = { 0 }; */
61
62 /* this is defined in gstelement.c */
63 extern GQuark __gst_deviceproviderclass_factory;
64
65 #define _do_init \
66 { \
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"); \
70 }
71
72 G_DEFINE_TYPE_WITH_CODE (GstDeviceProviderFactory, gst_device_provider_factory,
73 GST_TYPE_PLUGIN_FEATURE, _do_init);
74
75 static void
gst_device_provider_factory_class_init(GstDeviceProviderFactoryClass * klass)76 gst_device_provider_factory_class_init (GstDeviceProviderFactoryClass * klass)
77 {
78 GObjectClass *gobject_class = (GObjectClass *) klass;
79
80 gobject_class->finalize = gst_device_provider_factory_finalize;
81 }
82
83 static void
gst_device_provider_factory_init(GstDeviceProviderFactory * factory)84 gst_device_provider_factory_init (GstDeviceProviderFactory * factory)
85 {
86 }
87
88 static void
gst_device_provider_factory_finalize(GObject * object)89 gst_device_provider_factory_finalize (GObject * object)
90 {
91 GstDeviceProviderFactory *factory = GST_DEVICE_PROVIDER_FACTORY (object);
92 GstDeviceProvider *provider;
93
94 gst_device_provider_factory_cleanup (factory);
95
96 provider = g_atomic_pointer_get (&factory->provider);
97 if (provider)
98 gst_object_unref (provider);
99
100 G_OBJECT_CLASS (gst_device_provider_factory_parent_class)->finalize (object);
101 }
102
103 /**
104 * gst_device_provider_factory_find:
105 * @name: name of factory to find
106 *
107 * Search for an device provider factory of the given name. Refs the returned
108 * device provider factory; caller is responsible for unreffing.
109 *
110 * Returns: (transfer full) (nullable): #GstDeviceProviderFactory if
111 * found, %NULL otherwise
112 *
113 * Since: 1.4
114 */
115 GstDeviceProviderFactory *
gst_device_provider_factory_find(const gchar * name)116 gst_device_provider_factory_find (const gchar * name)
117 {
118 GstPluginFeature *feature;
119
120 g_return_val_if_fail (name != NULL, NULL);
121
122 feature = gst_registry_find_feature (gst_registry_get (), name,
123 GST_TYPE_DEVICE_PROVIDER_FACTORY);
124 if (feature)
125 return GST_DEVICE_PROVIDER_FACTORY (feature);
126
127 /* this isn't an error, for instance when you query if an device provider factory is
128 * present */
129 GST_LOG ("no such device provider factory \"%s\"", name);
130
131 return NULL;
132 }
133
134 static void
gst_device_provider_factory_cleanup(GstDeviceProviderFactory * factory)135 gst_device_provider_factory_cleanup (GstDeviceProviderFactory * factory)
136 {
137 if (factory->metadata) {
138 gst_structure_free ((GstStructure *) factory->metadata);
139 factory->metadata = NULL;
140 }
141 if (factory->type) {
142 factory->type = G_TYPE_INVALID;
143 }
144 }
145
146 #define CHECK_METADATA_FIELD(klass, name, key) \
147 G_STMT_START { \
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); \
151 goto detailserror; \
152 } \
153 } G_STMT_END;
154
155 /**
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
162 *
163 * Create a new device providerfactory capable of instantiating objects of the
164 * @type and add the factory to @plugin.
165 *
166 * Returns: %TRUE, if the registering succeeded, %FALSE on error
167 *
168 * Since: 1.4
169 */
170 gboolean
gst_device_provider_register(GstPlugin * plugin,const gchar * name,guint rank,GType type)171 gst_device_provider_register (GstPlugin * plugin, const gchar * name,
172 guint rank, GType type)
173 {
174 GstPluginFeature *existing_feature;
175 GstRegistry *registry;
176 GstDeviceProviderFactory *factory;
177 GstDeviceProviderClass *klass;
178
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);
181
182 registry = gst_registry_get ();
183
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.
187 */
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);
197 return TRUE;
198 }
199
200 factory = g_object_new (GST_TYPE_DEVICE_PROVIDER_FACTORY, 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",
203 g_type_name (type));
204
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));
208
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);
213
214 factory->type = type;
215 factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);
216
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);
222 } else {
223 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
224 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
225 }
226 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
227 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
228
229 gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));
230
231 return TRUE;
232
233 /* ERRORS */
234 detailserror:
235 {
236 gst_device_provider_factory_cleanup (factory);
237 return FALSE;
238 }
239 }
240
241 /**
242 * gst_device_provider_factory_get:
243 * @factory: factory to instantiate
244 *
245 * Returns the device provider of the type defined by the given device
246 * providerfactory.
247 *
248 * Returns: (transfer full) (nullable): the #GstDeviceProvider or %NULL
249 * if the device provider couldn't be created
250 *
251 * Since: 1.4
252 */
253 GstDeviceProvider *
gst_device_provider_factory_get(GstDeviceProviderFactory * factory)254 gst_device_provider_factory_get (GstDeviceProviderFactory * factory)
255 {
256 GstDeviceProvider *device_provider;
257 GstDeviceProviderClass *oclass;
258 GstDeviceProviderFactory *newfactory;
259
260 g_return_val_if_fail (factory != NULL, NULL);
261
262 newfactory =
263 GST_DEVICE_PROVIDER_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
264 (factory)));
265
266 if (newfactory == NULL)
267 goto load_failed;
268
269 factory = newfactory;
270
271 GST_INFO ("getting device provider \"%s\"", GST_OBJECT_NAME (factory));
272
273 if (factory->type == 0)
274 goto no_type;
275
276 device_provider = g_atomic_pointer_get (&newfactory->provider);
277 if (device_provider) {
278 gst_object_unref (factory);
279 return gst_object_ref (device_provider);
280 }
281
282 /* create an instance of the device provider, cast so we don't assert on NULL
283 * also set name as early as we can
284 */
285 device_provider = g_object_new (factory->type, NULL);
286 if (G_UNLIKELY (device_provider == NULL))
287 goto no_device_provider;
288
289 /* fill in the pointer to the factory in the device provider class. The
290 * class will not be unreffed currently.
291 * Be thread safe as there might be 2 threads creating the first instance of
292 * an device provider at the same moment
293 */
294 oclass = GST_DEVICE_PROVIDER_GET_CLASS (device_provider);
295 if (!g_atomic_pointer_compare_and_exchange (&oclass->factory,
296 (GstDeviceProviderFactory *) NULL, factory)) {
297 gst_object_unref (factory);
298 } else {
299 /* This ref will never be dropped as the class is never destroyed */
300 GST_OBJECT_FLAG_SET (factory, GST_OBJECT_FLAG_MAY_BE_LEAKED);
301 }
302
303 gst_object_ref_sink (device_provider);
304
305 /* We use an atomic to make sure we don't create two in parallel */
306 if (!g_atomic_pointer_compare_and_exchange (&newfactory->provider,
307 (GstDeviceProvider *) NULL, device_provider)) {
308 gst_object_unref (device_provider);
309
310 device_provider = g_atomic_pointer_get (&newfactory->provider);
311 }
312
313 GST_DEBUG ("created device provider \"%s\"", GST_OBJECT_NAME (factory));
314
315 return gst_object_ref (device_provider);
316
317 /* ERRORS */
318 load_failed:
319 {
320 GST_WARNING_OBJECT (factory,
321 "loading plugin containing feature %s returned NULL!",
322 GST_OBJECT_NAME (factory));
323 return NULL;
324 }
325 no_type:
326 {
327 GST_WARNING_OBJECT (factory, "factory has no type");
328 gst_object_unref (factory);
329 return NULL;
330 }
331 no_device_provider:
332 {
333 GST_WARNING_OBJECT (factory, "could not create device provider");
334 gst_object_unref (factory);
335 return NULL;
336 }
337 }
338
339 /**
340 * gst_device_provider_factory_get_by_name:
341 * @factoryname: a named factory to instantiate
342 *
343 * Returns the device provider of the type defined by the given device
344 * provider factory.
345 *
346 * Returns: (transfer full) (nullable): a #GstDeviceProvider or %NULL
347 * if unable to create device provider
348 *
349 * Since: 1.4
350 */
351 GstDeviceProvider *
gst_device_provider_factory_get_by_name(const gchar * factoryname)352 gst_device_provider_factory_get_by_name (const gchar * factoryname)
353 {
354 GstDeviceProviderFactory *factory;
355 GstDeviceProvider *device_provider;
356
357 g_return_val_if_fail (factoryname != NULL, NULL);
358 g_return_val_if_fail (gst_is_initialized (), NULL);
359
360 GST_LOG ("gstdeviceproviderfactory: get_by_name \"%s\"", factoryname);
361
362 factory = gst_device_provider_factory_find (factoryname);
363 if (factory == NULL)
364 goto no_factory;
365
366 GST_LOG_OBJECT (factory, "found factory %p", factory);
367 device_provider = gst_device_provider_factory_get (factory);
368 if (device_provider == NULL)
369 goto create_failed;
370
371 gst_object_unref (factory);
372 return device_provider;
373
374 /* ERRORS */
375 no_factory:
376 {
377 GST_INFO ("no such device provider factory \"%s\"!", factoryname);
378 return NULL;
379 }
380 create_failed:
381 {
382 GST_INFO_OBJECT (factory, "couldn't create instance!");
383 gst_object_unref (factory);
384 return NULL;
385 }
386 }
387
388 /**
389 * gst_device_provider_factory_get_device_provider_type:
390 * @factory: factory to get managed #GType from
391 *
392 * Get the #GType for device providers managed by this factory. The type can
393 * only be retrieved if the device provider factory is loaded, which can be
394 * assured with gst_plugin_feature_load().
395 *
396 * Returns: the #GType for device providers managed by this factory.
397 *
398 * Since: 1.4
399 */
400 GType
gst_device_provider_factory_get_device_provider_type(GstDeviceProviderFactory * factory)401 gst_device_provider_factory_get_device_provider_type (GstDeviceProviderFactory *
402 factory)
403 {
404 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory),
405 G_TYPE_INVALID);
406
407 return factory->type;
408 }
409
410 /**
411 * gst_device_provider_factory_get_metadata:
412 * @factory: a #GstDeviceProviderFactory
413 * @key: a key
414 *
415 * Get the metadata on @factory with @key.
416 *
417 * Returns: (nullable): the metadata with @key on @factory or %NULL
418 * when there was no metadata with the given @key.
419 *
420 * Since: 1.4
421 */
422 const gchar *
gst_device_provider_factory_get_metadata(GstDeviceProviderFactory * factory,const gchar * key)423 gst_device_provider_factory_get_metadata (GstDeviceProviderFactory * factory,
424 const gchar * key)
425 {
426 return gst_structure_get_string ((GstStructure *) factory->metadata, key);
427 }
428
429 /**
430 * gst_device_provider_factory_get_metadata_keys:
431 * @factory: a #GstDeviceProviderFactory
432 *
433 * Get the available keys for the metadata on @factory.
434 *
435 * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
436 * a %NULL-terminated array of key strings, or %NULL when there is no
437 * metadata. Free with g_strfreev() when no longer needed.
438 *
439 * Since: 1.4
440 */
441 gchar **
gst_device_provider_factory_get_metadata_keys(GstDeviceProviderFactory * factory)442 gst_device_provider_factory_get_metadata_keys (GstDeviceProviderFactory *
443 factory)
444 {
445 GstStructure *metadata;
446 gchar **arr;
447 gint i, num;
448
449 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), NULL);
450
451 metadata = (GstStructure *) factory->metadata;
452 if (metadata == NULL)
453 return NULL;
454
455 num = gst_structure_n_fields (metadata);
456 if (num == 0)
457 return NULL;
458
459 arr = g_new (gchar *, num + 1);
460 for (i = 0; i < num; ++i) {
461 arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
462 }
463 arr[i] = NULL;
464 return arr;
465 }
466
467 /**
468 * gst_device_provider_factory_has_classesv:
469 * @factory: a #GstDeviceProviderFactory
470 * @classes: (array zero-terminated=1) (allow-none): a %NULL terminated array
471 * of classes to match, only match if all classes are matched
472 *
473 * Check if @factory matches all of the given classes
474 *
475 * Returns: %TRUE if @factory matches.
476 *
477 * Since: 1.4
478 */
479 gboolean
gst_device_provider_factory_has_classesv(GstDeviceProviderFactory * factory,gchar ** classes)480 gst_device_provider_factory_has_classesv (GstDeviceProviderFactory * factory,
481 gchar ** classes)
482 {
483 const gchar *klass;
484
485 g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), FALSE);
486
487 klass = gst_device_provider_factory_get_metadata (factory,
488 GST_ELEMENT_METADATA_KLASS);
489
490 if (klass == NULL) {
491 GST_ERROR_OBJECT (factory,
492 "device provider factory is missing klass identifiers");
493 return FALSE;
494 }
495
496 for (; classes != NULL && classes[0] != NULL; classes++) {
497 const gchar *found;
498 guint len;
499
500 if (classes[0][0] == '\0')
501 continue;
502
503 found = strstr (klass, classes[0]);
504
505 if (!found)
506 return FALSE;
507 if (found != klass && *(found - 1) != '/')
508 return FALSE;
509
510 len = strlen (classes[0]);
511 if (found[len] != 0 && found[len] != '/')
512 return FALSE;
513 }
514
515 return TRUE;
516 }
517
518 /**
519 * gst_device_provider_factory_has_classes:
520 * @factory: a #GstDeviceProviderFactory
521 * @classes: (allow-none): a "/" separate list of classes to match, only match
522 * if all classes are matched
523 *
524 * Check if @factory matches all of the given @classes
525 *
526 * Returns: %TRUE if @factory matches or if @classes is %NULL.
527 *
528 * Since: 1.4
529 */
530 gboolean
gst_device_provider_factory_has_classes(GstDeviceProviderFactory * factory,const gchar * classes)531 gst_device_provider_factory_has_classes (GstDeviceProviderFactory * factory,
532 const gchar * classes)
533 {
534 gchar **classesv;
535 gboolean res;
536
537 if (classes == NULL)
538 return TRUE;
539
540 classesv = g_strsplit (classes, "/", 0);
541
542 res = gst_device_provider_factory_has_classesv (factory, classesv);
543
544 g_strfreev (classesv);
545
546 return res;
547 }
548
549 static gboolean
device_provider_filter(GstPluginFeature * feature,GstRank * minrank)550 device_provider_filter (GstPluginFeature * feature, GstRank * minrank)
551 {
552 /* we only care about device provider factories */
553 if (G_UNLIKELY (!GST_IS_DEVICE_PROVIDER_FACTORY (feature)))
554 return FALSE;
555
556 return (gst_plugin_feature_get_rank (feature) >= *minrank);
557 }
558
559 /**
560 * gst_device_provider_factory_list_get_device_providers:
561 * @minrank: Minimum rank
562 *
563 * Get a list of factories with a rank greater or equal to @minrank.
564 * The list of factories is returned by decreasing rank.
565 *
566 * Returns: (transfer full) (element-type Gst.DeviceProviderFactory):
567 * a #GList of #GstDeviceProviderFactory device providers. Use
568 * gst_plugin_feature_list_free() after usage.
569 *
570 * Since: 1.4
571 */
572 GList *
gst_device_provider_factory_list_get_device_providers(GstRank minrank)573 gst_device_provider_factory_list_get_device_providers (GstRank minrank)
574 {
575 GList *result;
576
577 /* get the feature list using the filter */
578 result = gst_registry_feature_filter (gst_registry_get (),
579 (GstPluginFeatureFilter) device_provider_filter, FALSE, &minrank);
580
581 /* sort on rank and name */
582 result = g_list_sort (result, gst_plugin_feature_rank_compare_func);
583
584 return result;
585 }
586