gstdeviceproviderfactory.c 17.2 KB
Newer Older
1 2 3 4 5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *                    2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
 *
6
 * gstdeviceproviderfactory.c: GstDeviceProviderFactory object, support routines
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
25 26 27
 * SECTION:gstdeviceproviderfactory
 * @short_description: Create GstDeviceProviders from a factory
 * @see_also: #GstDeviceProvider, #GstPlugin, #GstPluginFeature, #GstPadTemplate.
28
 *
29 30
 * #GstDeviceProviderFactory is used to create instances of device providers. A
 * GstDeviceProviderfactory can be added to a #GstPlugin as it is also a
31 32
 * #GstPluginFeature.
 *
33 34
 * Use the gst_device_provider_factory_find() and gst_device_provider_factory_create()
 * functions to create device provider instances or use gst_device_provider_factory_make() as a
35 36 37 38 39 40 41 42 43 44 45
 * convenient shortcut.
 *
 * Since: 1.4
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gst_private.h"

46
#include "gstdeviceproviderfactory.h"
47 48 49 50
#include "gst.h"

#include "glib-compat-private.h"

51 52
GST_DEBUG_CATEGORY_STATIC (device_provider_factory_debug);
#define GST_CAT_DEFAULT device_provider_factory_debug
53

54 55
static void gst_device_provider_factory_finalize (GObject * object);
static void gst_device_provider_factory_cleanup (GstDeviceProviderFactory *
56 57
    factory);

58
/* static guint gst_device_provider_factory_signals[LAST_SIGNAL] = { 0 }; */
59 60

/* this is defined in gstelement.c */
61
extern GQuark __gst_deviceproviderclass_factory;
62 63 64

#define _do_init \
{ \
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"); \
68 69
}

70
G_DEFINE_TYPE_WITH_CODE (GstDeviceProviderFactory, gst_device_provider_factory,
71 72 73
    GST_TYPE_PLUGIN_FEATURE, _do_init);

static void
74
gst_device_provider_factory_class_init (GstDeviceProviderFactoryClass * klass)
75 76 77
{
  GObjectClass *gobject_class = (GObjectClass *) klass;

78
  gobject_class->finalize = gst_device_provider_factory_finalize;
79 80 81
}

static void
82
gst_device_provider_factory_init (GstDeviceProviderFactory * factory)
83 84 85 86
{
}

static void
87
gst_device_provider_factory_finalize (GObject * object)
88
{
89 90
  GstDeviceProviderFactory *factory = GST_DEVICE_PROVIDER_FACTORY (object);
  GstDeviceProvider *provider;
91

92
  gst_device_provider_factory_cleanup (factory);
93

94 95 96
  provider = g_atomic_pointer_get (&factory->provider);
  if (provider)
    gst_object_unref (provider);
97

98
  G_OBJECT_CLASS (gst_device_provider_factory_parent_class)->finalize (object);
99 100 101
}

/**
102
 * gst_device_provider_factory_find:
103 104
 * @name: name of factory to find
 *
105 106
 * Search for an device provider factory of the given name. Refs the returned
 * device provider factory; caller is responsible for unreffing.
107
 *
108
 * Returns: (transfer full) (nullable): #GstDeviceProviderFactory if
109
 * found, %NULL otherwise
110 111 112
 *
 * Since: 1.4
 */
113 114
GstDeviceProviderFactory *
gst_device_provider_factory_find (const gchar * name)
115 116 117 118 119 120
{
  GstPluginFeature *feature;

  g_return_val_if_fail (name != NULL, NULL);

  feature = gst_registry_find_feature (gst_registry_get (), name,
121
      GST_TYPE_DEVICE_PROVIDER_FACTORY);
122
  if (feature)
123
    return GST_DEVICE_PROVIDER_FACTORY (feature);
124

125
  /* this isn't an error, for instance when you query if an device provider factory is
126
   * present */
127
  GST_LOG ("no such device provider factory \"%s\"", name);
128 129 130 131 132

  return NULL;
}

static void
133
gst_device_provider_factory_cleanup (GstDeviceProviderFactory * factory)
134 135 136 137 138 139 140 141 142 143 144 145
{
  if (factory->metadata) {
    gst_structure_free ((GstStructure *) factory->metadata);
    factory->metadata = NULL;
  }
  if (factory->type) {
    factory->type = G_TYPE_INVALID;
  }
}

#define CHECK_METADATA_FIELD(klass, name, key)                                 \
  G_STMT_START {                                                               \
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);    \
149 150 151 152 153
      goto detailserror;                                                       \
    } \
  } G_STMT_END;

/**
154 155 156 157 158 159 160 161
 * gst_device_provider_register:
 * @plugin: (allow-none): #GstPlugin to register the device provider with, or %NULL for
 *     a static device provider.
 * @name: name of device providers of this type
 * @rank: rank of device provider (higher rank means more importance when autoplugging)
 * @type: GType of device provider to register
 *
 * Create a new device providerfactory capable of instantiating objects of the
162 163
 * @type and add the factory to @plugin.
 *
164
 * Returns: %TRUE, if the registering succeeded, %FALSE on error
165 166 167 168
 *
 * Since: 1.4
 */
gboolean
169 170
gst_device_provider_register (GstPlugin * plugin, const gchar * name,
    guint rank, GType type)
171 172 173
{
  GstPluginFeature *existing_feature;
  GstRegistry *registry;
174 175
  GstDeviceProviderFactory *factory;
  GstDeviceProviderClass *klass;
176 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);
179 180 181 182 183 184 185 186 187 188 189

  registry = gst_registry_get ();

  /* check if feature already exists, if it exists there is no need to update it
   * when the registry is getting updated, outdated plugins and all their
   * features are removed and readded.
   */
  existing_feature = gst_registry_lookup_feature (registry, name);
  if (existing_feature) {
    GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
        existing_feature, name);
190
    factory = GST_DEVICE_PROVIDER_FACTORY_CAST (existing_feature);
191 192
    factory->type = type;
    existing_feature->loaded = TRUE;
193
    g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
194 195 196 197 198
    gst_object_unref (existing_feature);
    return TRUE;
  }

  factory =
199 200
      GST_DEVICE_PROVIDER_FACTORY_CAST (g_object_newv
      (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",
203 204 205
      g_type_name (type));

  /* provide info needed during class structure setup */
206 207
  g_type_set_qdata (type, __gst_deviceproviderclass_factory, factory);
  klass = GST_DEVICE_PROVIDER_CLASS (g_type_class_ref (type));
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_LONGNAME);
  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_KLASS);
  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_DESCRIPTION);
  CHECK_METADATA_FIELD (klass, name, GST_ELEMENT_METADATA_AUTHOR);

  factory->type = type;
  factory->metadata = gst_structure_copy ((GstStructure *) klass->metadata);

  if (plugin && plugin->desc.name) {
    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name;
    GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
    g_object_add_weak_pointer ((GObject *) plugin,
        (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
  } else {
    GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
    GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
  }
  gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
  GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;

  gst_registry_add_feature (registry, GST_PLUGIN_FEATURE_CAST (factory));

  return TRUE;

  /* ERRORS */
detailserror:
  {
236
    gst_device_provider_factory_cleanup (factory);
237 238 239 240 241
    return FALSE;
  }
}

/**
242
 * gst_device_provider_factory_get:
243 244
 * @factory: factory to instantiate
 *
245 246
 * Returns the device provider of the type defined by the given device
 * providerfactory.
247
 *
248 249
 * Returns: (transfer full) (nullable): the #GstDeviceProvider or %NULL
 * if the device provider couldn't be created
250 251 252
 *
 * Since: 1.4
 */
253 254
GstDeviceProvider *
gst_device_provider_factory_get (GstDeviceProviderFactory * factory)
255
{
256 257 258
  GstDeviceProvider *device_provider;
  GstDeviceProviderClass *oclass;
  GstDeviceProviderFactory *newfactory;
259 260 261 262

  g_return_val_if_fail (factory != NULL, NULL);

  newfactory =
263
      GST_DEVICE_PROVIDER_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
264 265 266 267 268 269 270
          (factory)));

  if (newfactory == NULL)
    goto load_failed;

  factory = newfactory;

271
  GST_INFO ("getting device provider \"%s\"", GST_OBJECT_NAME (factory));
272 273 274 275

  if (factory->type == 0)
    goto no_type;

276 277 278
  device_provider = g_atomic_pointer_get (&newfactory->provider);
  if (device_provider)
    return gst_object_ref (device_provider);
279

280
  /* create an instance of the device provider, cast so we don't assert on NULL
281 282
   * also set name as early as we can
   */
283
  device_provider = GST_DEVICE_PROVIDER_CAST (g_object_newv (factory->type, 0,
284
          NULL));
285 286
  if (G_UNLIKELY (device_provider == NULL))
    goto no_device_provider;
287

288
  /* fill in the pointer to the factory in the device provider class. The
289 290
   * class will not be unreffed currently.
   * Be thread safe as there might be 2 threads creating the first instance of
291
   * an device provider at the same moment
292
   */
293
  oclass = GST_DEVICE_PROVIDER_GET_CLASS (device_provider);
294 295 296
  if (!g_atomic_pointer_compare_and_exchange (&oclass->factory, NULL, factory))
    gst_object_unref (factory);

297
  gst_object_ref_sink (device_provider);
298 299

  /* We use an atomic to make sure we don't create two in parallel */
300 301 302
  if (!g_atomic_pointer_compare_and_exchange (&newfactory->provider, NULL,
          device_provider)) {
    gst_object_unref (device_provider);
303

304
    device_provider = g_atomic_pointer_get (&newfactory->provider);
305 306
  }

307
  GST_DEBUG ("created device provider \"%s\"", GST_OBJECT_NAME (factory));
308

309
  return gst_object_ref (device_provider);
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

  /* ERRORS */
load_failed:
  {
    GST_WARNING_OBJECT (factory,
        "loading plugin containing feature %s returned NULL!",
        GST_OBJECT_NAME (factory));
    return NULL;
  }
no_type:
  {
    GST_WARNING_OBJECT (factory, "factory has no type");
    gst_object_unref (factory);
    return NULL;
  }
325
no_device_provider:
326
  {
327
    GST_WARNING_OBJECT (factory, "could not create device provider");
328 329 330 331 332 333
    gst_object_unref (factory);
    return NULL;
  }
}

/**
334
 * gst_device_provider_factory_get_by_name:
335 336
 * @factoryname: a named factory to instantiate
 *
337 338
 * Returns the device provider of the type defined by the given device
 * provider factory.
339
 *
340 341
 * Returns: (transfer full) (nullable): a #GstDeviceProvider or %NULL
 * if unable to create device provider
342 343 344
 *
 * Since: 1.4
 */
345 346
GstDeviceProvider *
gst_device_provider_factory_get_by_name (const gchar * factoryname)
347
{
348 349
  GstDeviceProviderFactory *factory;
  GstDeviceProvider *device_provider;
350 351 352 353

  g_return_val_if_fail (factoryname != NULL, NULL);
  g_return_val_if_fail (gst_is_initialized (), NULL);

354
  GST_LOG ("gstdeviceproviderfactory: get_by_name \"%s\"", factoryname);
355

356
  factory = gst_device_provider_factory_find (factoryname);
357 358 359 360
  if (factory == NULL)
    goto no_factory;

  GST_LOG_OBJECT (factory, "found factory %p", factory);
361 362
  device_provider = gst_device_provider_factory_get (factory);
  if (device_provider == NULL)
363 364 365
    goto create_failed;

  gst_object_unref (factory);
366
  return device_provider;
367 368 369 370

  /* ERRORS */
no_factory:
  {
371
    GST_INFO ("no such device provider factory \"%s\"!", factoryname);
372 373 374 375 376 377 378 379 380 381 382
    return NULL;
  }
create_failed:
  {
    GST_INFO_OBJECT (factory, "couldn't create instance!");
    gst_object_unref (factory);
    return NULL;
  }
}

/**
383
 * gst_device_provider_factory_get_device_provider_type:
384 385
 * @factory: factory to get managed #GType from
 *
386 387
 * Get the #GType for device providers managed by this factory. The type can
 * only be retrieved if the device provider factory is loaded, which can be
388 389
 * assured with gst_plugin_feature_load().
 *
390
 * Returns: the #GType for device providers managed by this factory or 0 if
391 392 393 394 395
 * the factory is not loaded.
 *
 * Since: 1.4
 */
GType
396
gst_device_provider_factory_get_device_provider_type (GstDeviceProviderFactory *
397 398
    factory)
{
399
  g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), 0);
400 401 402 403 404

  return factory->type;
}

/**
405 406
 * gst_device_provider_factory_get_metadata:
 * @factory: a #GstDeviceProviderFactory
407 408 409 410
 * @key: a key
 *
 * Get the metadata on @factory with @key.
 *
411 412
 * Returns: (nullable): the metadata with @key on @factory or %NULL
 * when there was no metadata with the given @key.
413 414 415 416
 *
 * Since: 1.4
 */
const gchar *
417
gst_device_provider_factory_get_metadata (GstDeviceProviderFactory * factory,
418 419 420 421 422 423
    const gchar * key)
{
  return gst_structure_get_string ((GstStructure *) factory->metadata, key);
}

/**
424 425
 * gst_device_provider_factory_get_metadata_keys:
 * @factory: a #GstDeviceProviderFactory
426 427 428
 *
 * Get the available keys for the metadata on @factory.
 *
429
 * Returns: (transfer full) (element-type utf8) (array zero-terminated=1) (nullable):
430 431 432 433 434 435
 * a %NULL-terminated array of key strings, or %NULL when there is no
 * metadata. Free with g_strfreev() when no longer needed.
 *
 * Since: 1.4
 */
gchar **
436 437
gst_device_provider_factory_get_metadata_keys (GstDeviceProviderFactory *
    factory)
438 439 440 441 442
{
  GstStructure *metadata;
  gchar **arr;
  gint i, num;

443
  g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), NULL);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

  metadata = (GstStructure *) factory->metadata;
  if (metadata == NULL)
    return NULL;

  num = gst_structure_n_fields (metadata);
  if (num == 0)
    return NULL;

  arr = g_new (gchar *, num + 1);
  for (i = 0; i < num; ++i) {
    arr[i] = g_strdup (gst_structure_nth_field_name (metadata, i));
  }
  arr[i] = NULL;
  return arr;
}

/**
462 463
 * gst_device_provider_factory_has_classesv:
 * @factory: a #GstDeviceProviderFactory
464 465
 * @classes: (array zero-terminated=1): a %NULL terminated array of
 *   klasses to match, only match if all classes are matched
466
 *
467
 * Check if @factory matches all of the given classes
468
 *
469
 * Returns: %TRUE if @factory matches.
470 471 472 473
 *
 * Since: 1.4
 */
gboolean
474
gst_device_provider_factory_has_classesv (GstDeviceProviderFactory * factory,
475
    gchar ** classes)
476 477 478
{
  const gchar *klass;

479
  g_return_val_if_fail (GST_IS_DEVICE_PROVIDER_FACTORY (factory), FALSE);
480

481
  klass = gst_device_provider_factory_get_metadata (factory,
482 483 484 485
      GST_ELEMENT_METADATA_KLASS);

  if (klass == NULL) {
    GST_ERROR_OBJECT (factory,
486
        "device provider factory is missing klass identifiers");
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    return FALSE;
  }

  for (; classes[0]; classes++) {
    const gchar *found;
    guint len;

    if (classes[0] == '\0')
      continue;

    found = strstr (klass, classes[0]);

    if (!found)
      return FALSE;
    if (found != klass && *(found - 1) != '/')
      return FALSE;

    len = strlen (classes[0]);
    if (found[len] != 0 && found[len] != '/')
      return FALSE;
507 508
  }

509 510 511 512
  return TRUE;
}

/**
513 514
 * gst_device_provider_factory_has_classes:
 * @factory: a #GstDeviceProviderFactory
515 516 517 518 519 520 521 522 523 524
 * @classes: a "/" separate list of klasses to match, only match if all classes
 *  are matched
 *
 * Check if @factory matches all of the given @classes
 *
 * Returns: %TRUE if @factory matches.
 *
 * Since: 1.4
 */
gboolean
525
gst_device_provider_factory_has_classes (GstDeviceProviderFactory * factory,
526 527 528 529 530 531 532
    const gchar * classes)
{
  gchar **classesv;
  gboolean res;

  classesv = g_strsplit (classes, "/", 0);

533
  res = gst_device_provider_factory_has_classesv (factory, classesv);
534 535

  g_strfreev (classesv);
536 537 538 539

  return res;
}

540 541 542 543 544 545
typedef struct
{
  const char *classes;
  GstRank minrank;
} FilterData;

546
static gboolean
547
device_provider_filter (GstPluginFeature * feature, FilterData * data)
548 549 550
{
  gboolean res;

551 552
  /* we only care about device provider factories */
  if (G_UNLIKELY (!GST_IS_DEVICE_PROVIDER_FACTORY (feature)))
553 554 555
    return FALSE;

  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);
558 559 560 561 562

  return res;
}

/**
563
 * gst_device_provider_factory_list_get_device_providers:
564 565
 * @classes: a "/" separate list of klasses to match, only match if all classes
 *  are matched
566 567
 * @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.
571
 *
572 573
 * Returns: (transfer full) (element-type Gst.DeviceProviderFactory): a #GList of
 *     #GstDeviceProviderFactory device providers. Use gst_plugin_feature_list_free() after
574 575 576 577
 *     usage.
 *
 * Since: 1.4
 */
578
GList *gst_device_provider_factory_list_get_device_providers
579
    (const gchar * classes, GstRank minrank)
580 581 582 583 584
{
  GList *result;
  FilterData data;

  /* prepare type */
585
  data.classes = classes;
586 587 588 589
  data.minrank = minrank;

  /* get the feature list using the filter */
  result = gst_registry_feature_filter (gst_registry_get (),
590
      (GstPluginFeatureFilter) device_provider_filter, FALSE, &data);
591 592 593 594 595 596

  /* sort on rank and name */
  result = g_list_sort (result, gst_plugin_feature_rank_compare_func);

  return result;
}