gstdevicemonitor.c 22.9 KB
Newer Older
1 2 3
/* GStreamer
 * Copyright (C) 2013 Olivier Crete <olivier.crete@collabora.com>
 *
4
 * gstdevicemonitor.c: device monitor
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 */
21 22

/**
23
 * SECTION:gstdevicemonitor
24
 * @title: GstDeviceMonitor
25
 * @short_description: A device monitor and prober
26
 * @see_also: #GstDevice, #GstDeviceProvider
27
 *
28
 * Applications should create a #GstDeviceMonitor when they want
29
 * to probe, list and monitor devices of a specific type. The
30
 * #GstDeviceMonitor will create the appropriate
31
 * #GstDeviceProvider objects and manage them. It will then post
32 33 34
 * messages on its #GstBus for devices that have been added and
 * removed.
 *
35 36 37
 * The device monitor will monitor all devices matching the filters that
 * the application has set.
 *
38
 * The basic use pattern of a device monitor is as follows:
39 40 41 42 43
 * |[
 *   static gboolean
 *   my_bus_func (GstBus * bus, GstMessage * message, gpointer user_data)
 *   {
 *      GstDevice *device;
44
 *      gchar *name;
45 46 47 48 49 50 51
 *
 *      switch (GST_MESSAGE_TYPE (message)) {
 *        case GST_MESSAGE_DEVICE_ADDED:
 *          gst_message_parse_device_added (message, &device);
 *          name = gst_device_get_display_name (device);
 *          g_print("Device added: %s\n", name);
 *          g_free (name);
52
 *          gst_object_unref (device);
53 54 55 56 57 58
 *          break;
 *        case GST_MESSAGE_DEVICE_REMOVED:
 *          gst_message_parse_device_removed (message, &device);
 *          name = gst_device_get_display_name (device);
 *          g_print("Device removed: %s\n", name);
 *          g_free (name);
59
 *          gst_object_unref (device);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 *          break;
 *        default:
 *          break;
 *      }
 *
 *      return G_SOURCE_CONTINUE;
 *   }
 *
 *   GstDeviceMonitor *
 *   setup_raw_video_source_device_monitor (void) {
 *      GstDeviceMonitor *monitor;
 *      GstBus *bus;
 *      GstCaps *caps;
 *
 *      monitor = gst_device_monitor_new ();
 *
 *      bus = gst_device_monitor_get_bus (monitor);
 *      gst_bus_add_watch (bus, my_bus_func, NULL);
 *      gst_object_unref (bus);
 *
80
 *      caps = gst_caps_new_empty_simple ("video/x-raw");
81 82 83 84 85 86 87 88 89
 *      gst_device_monitor_add_filter (monitor, "Video/Source", caps);
 *      gst_caps_unref (caps);
 *
 *      gst_device_monitor_start (monitor);
 *
 *      return monitor;
 *   }
 * ]|
 *
90 91 92 93
 * Since: 1.4
 */


94 95 96 97
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

98
#include "gst_private.h"
99
#include "gstdevicemonitor.h"
100

101
struct _GstDeviceMonitorPrivate
102 103 104 105 106
{
  gboolean started;

  GstBus *bus;

107
  GPtrArray *providers;
108 109
  guint cookie;

110 111 112
  GPtrArray *filters;

  guint last_id;
113 114
  GList *hidden;
  gboolean show_all;
115 116
};

117 118 119 120 121 122
#define DEFAULT_SHOW_ALL        FALSE

enum
{
  PROP_SHOW_ALL = 1,
};
123

124
G_DEFINE_TYPE (GstDeviceMonitor, gst_device_monitor, GST_TYPE_OBJECT);
125

126
static void gst_device_monitor_dispose (GObject * object);
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
struct DeviceFilter
{
  guint id;

  gchar **classesv;
  GstCaps *caps;
};

static void
device_filter_free (struct DeviceFilter *filter)
{
  g_strfreev (filter->classesv);
  gst_caps_unref (filter->caps);

  g_slice_free (struct DeviceFilter, filter);
}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static void
gst_device_monitor_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);

  switch (prop_id) {
    case PROP_SHOW_ALL:
      g_value_set_boolean (value,
          gst_device_monitor_get_show_all_devices (monitor));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_device_monitor_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstDeviceMonitor *monitor = GST_DEVICE_MONITOR (object);

  switch (prop_id) {
    case PROP_SHOW_ALL:
      gst_device_monitor_set_show_all_devices (monitor,
          g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}


180
static void
181
gst_device_monitor_class_init (GstDeviceMonitorClass * klass)
182 183 184
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

185
  g_type_class_add_private (klass, sizeof (GstDeviceMonitorPrivate));
186

187 188
  object_class->get_property = gst_device_monitor_get_property;
  object_class->set_property = gst_device_monitor_set_property;
189
  object_class->dispose = gst_device_monitor_dispose;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  g_object_class_install_property (object_class, PROP_SHOW_ALL,
      g_param_spec_boolean ("show-all", "Show All",
          "Show all devices, even those from hidden providers",
          DEFAULT_SHOW_ALL, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));
}

/* must be called with monitor lock */
static gboolean
is_provider_hidden (GstDeviceMonitor * monitor, GList * hidden,
    GstDeviceProvider * provider)
{
  GstDeviceProviderFactory *factory;

  if (monitor->priv->show_all)
    return FALSE;

  factory = gst_device_provider_get_factory (provider);
  if (g_list_find_custom (hidden, GST_OBJECT_NAME (factory),
          (GCompareFunc) g_strcmp0))
    return TRUE;

  return FALSE;
}

/* must be called with monitor lock */
static void
update_hidden_providers_list (GList ** hidden, GstDeviceProvider * provider)
{
  gchar **obs;

  obs = gst_device_provider_get_hidden_providers (provider);
  if (obs) {
    gint i;

    for (i = 0; obs[i]; i++)
      *hidden = g_list_prepend (*hidden, obs[i]);

    g_free (obs);
  }
230 231 232 233
}

static void
bus_sync_message (GstBus * bus, GstMessage * message,
234
    GstDeviceMonitor * monitor)
235
{
236
  GstMessageType type = GST_MESSAGE_TYPE (message);
237

238
  if (type == GST_MESSAGE_DEVICE_ADDED || type == GST_MESSAGE_DEVICE_REMOVED) {
239
    gboolean matches;
240
    GstDevice *device;
241
    GstDeviceProvider *provider;
242

243
    if (type == GST_MESSAGE_DEVICE_ADDED)
244 245 246 247 248
      gst_message_parse_device_added (message, &device);
    else
      gst_message_parse_device_removed (message, &device);

    GST_OBJECT_LOCK (monitor);
249 250 251 252 253
    provider =
        GST_DEVICE_PROVIDER (gst_object_get_parent (GST_OBJECT (device)));
    if (is_provider_hidden (monitor, monitor->priv->hidden, provider)) {
      matches = FALSE;
    } else if (monitor->priv->filters->len) {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
      guint i;

      for (i = 0; i < monitor->priv->filters->len; i++) {
        struct DeviceFilter *filter =
            g_ptr_array_index (monitor->priv->filters, i);
        GstCaps *caps;

        caps = gst_device_get_caps (device);
        matches = gst_caps_can_intersect (filter->caps, caps) &&
            gst_device_has_classesv (device, filter->classesv);
        gst_caps_unref (caps);
        if (matches)
          break;
      }
    } else {
      matches = TRUE;
    }
271 272
    GST_OBJECT_UNLOCK (monitor);

Wim Taymans's avatar
Wim Taymans committed
273
    gst_object_unref (provider);
274 275
    gst_object_unref (device);

276
    if (matches)
277 278 279 280 281 282
      gst_bus_post (monitor->priv->bus, gst_message_ref (message));
  }
}


static void
283
gst_device_monitor_init (GstDeviceMonitor * self)
284 285
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
286
      GST_TYPE_DEVICE_MONITOR, GstDeviceMonitorPrivate);
287

288 289
  self->priv->show_all = DEFAULT_SHOW_ALL;

290 291 292
  self->priv->bus = gst_bus_new ();
  gst_bus_set_flushing (self->priv->bus, TRUE);

293
  self->priv->providers = g_ptr_array_new ();
294 295
  self->priv->filters = g_ptr_array_new_with_free_func (
      (GDestroyNotify) device_filter_free);
296

297
  self->priv->last_id = 1;
298 299 300 301
}


static void
302
gst_device_monitor_remove (GstDeviceMonitor * self, guint i)
303
{
304
  GstDeviceProvider *provider = g_ptr_array_index (self->priv->providers, i);
305 306
  GstBus *bus;

307
  g_ptr_array_remove_index (self->priv->providers, i);
308

309
  bus = gst_device_provider_get_bus (provider);
310 311 312
  g_signal_handlers_disconnect_by_func (bus, bus_sync_message, self);
  gst_object_unref (bus);

313
  gst_object_unref (provider);
314 315 316
}

static void
317
gst_device_monitor_dispose (GObject * object)
318
{
319
  GstDeviceMonitor *self = GST_DEVICE_MONITOR (object);
320

321
  g_return_if_fail (!self->priv->started);
322

323 324
  if (self->priv->providers) {
    while (self->priv->providers->len)
325
      gst_device_monitor_remove (self, self->priv->providers->len - 1);
326 327
    g_ptr_array_unref (self->priv->providers);
    self->priv->providers = NULL;
328 329
  }

330 331 332 333
  if (self->priv->filters) {
    g_ptr_array_unref (self->priv->filters);
    self->priv->filters = NULL;
  }
334

335 336
  gst_object_replace ((GstObject **) & self->priv->bus, NULL);

337
  G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
338 339 340
}

/**
341
 * gst_device_monitor_get_devices:
342
 * @monitor: A #GstDeviceProvider
343 344
 *
 * Gets a list of devices from all of the relevant monitors. This may actually
345
 * probe the hardware if the monitor is not currently started.
346 347 348
 *
 * Returns: (transfer full) (element-type GstDevice): a #GList of
 *   #GstDevice
349 350
 *
 * Since: 1.4
351 352 353
 */

GList *
354
gst_device_monitor_get_devices (GstDeviceMonitor * monitor)
355
{
356
  GList *devices = NULL, *hidden = NULL;
357 358 359
  guint i;
  guint cookie;

360
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
361

362
  GST_OBJECT_LOCK (monitor);
363

364 365 366 367 368 369 370 371 372 373 374 375
  if (monitor->priv->filters->len == 0) {
    GST_OBJECT_UNLOCK (monitor);
    GST_WARNING_OBJECT (monitor, "No filters have been set");
    return FALSE;
  }

  if (monitor->priv->providers->len == 0) {
    GST_OBJECT_UNLOCK (monitor);
    GST_WARNING_OBJECT (monitor, "No providers match the current filters");
    return FALSE;
  }

376 377 378
again:

  g_list_free_full (devices, gst_object_unref);
379
  g_list_free_full (hidden, g_free);
380
  devices = NULL;
381
  hidden = NULL;
382

383
  cookie = monitor->priv->cookie;
384

385
  for (i = 0; i < monitor->priv->providers->len; i++) {
386
    GList *tmpdev;
387 388
    GstDeviceProvider *provider =
        gst_object_ref (g_ptr_array_index (monitor->priv->providers, i));
389 390
    GList *item;

391 392
    if (!is_provider_hidden (monitor, hidden, provider)) {
      GST_OBJECT_UNLOCK (monitor);
393

394 395 396 397 398 399 400
      tmpdev = gst_device_provider_get_devices (provider);

      GST_OBJECT_LOCK (monitor);
      update_hidden_providers_list (&hidden, provider);
    } else {
      tmpdev = NULL;
    }
401

402

403 404 405
    for (item = tmpdev; item; item = item->next) {
      GstDevice *dev = GST_DEVICE (item->data);
      GstCaps *caps = gst_device_get_caps (dev);
406 407 408 409 410
      guint j;

      for (j = 0; j < monitor->priv->filters->len; j++) {
        struct DeviceFilter *filter =
            g_ptr_array_index (monitor->priv->filters, j);
411

412 413 414 415 416 417
        if (gst_caps_can_intersect (filter->caps, caps) &&
            gst_device_has_classesv (dev, filter->classesv)) {
          devices = g_list_prepend (devices, gst_object_ref (dev));
          break;
        }
      }
418 419 420 421
      gst_caps_unref (caps);
    }

    g_list_free_full (tmpdev, gst_object_unref);
422
    gst_object_unref (provider);
423

424
    if (monitor->priv->cookie != cookie)
425 426
      goto again;
  }
427
  g_list_free_full (hidden, g_free);
428

429
  GST_OBJECT_UNLOCK (monitor);
430

431
  return g_list_reverse (devices);
432 433 434
}

/**
435 436
 * gst_device_monitor_start:
 * @monitor: A #GstDeviceMonitor
437 438
 *
 * Starts monitoring the devices, one this has succeeded, the
439 440
 * %GST_MESSAGE_DEVICE_ADDED and %GST_MESSAGE_DEVICE_REMOVED messages
 * will be emitted on the bus when the list of devices changes.
441 442
 *
 * Returns: %TRUE if the device monitoring could be started
443 444
 *
 * Since: 1.4
445 446 447
 */

gboolean
448
gst_device_monitor_start (GstDeviceMonitor * monitor)
449
{
450 451
  guint cookie, i;
  GList *pending = NULL, *started = NULL, *removed = NULL;
452

453
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
454

455
  GST_OBJECT_LOCK (monitor);
456

457 458
  if (monitor->priv->filters->len == 0) {
    GST_OBJECT_UNLOCK (monitor);
459 460 461 462
    GST_WARNING_OBJECT (monitor, "No filters have been set, will expose all "
        "devices found");
    gst_device_monitor_add_filter (monitor, NULL, NULL);
    GST_OBJECT_LOCK (monitor);
463 464
  }

465
  if (monitor->priv->providers->len == 0) {
466
    GST_OBJECT_UNLOCK (monitor);
467
    GST_WARNING_OBJECT (monitor, "No providers match the current filters");
468 469 470
    return FALSE;
  }

471
  gst_bus_set_flushing (monitor->priv->bus, FALSE);
472

473 474 475 476 477 478 479 480
again:
  cookie = monitor->priv->cookie;

  g_list_free_full (pending, gst_object_unref);
  pending = NULL;
  removed = started;
  started = NULL;

481
  for (i = 0; i < monitor->priv->providers->len; i++) {
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    GstDeviceProvider *provider;
    GList *find;

    provider = g_ptr_array_index (monitor->priv->providers, i);

    find = g_list_find (removed, provider);
    if (find) {
      /* this was already started, move to started list */
      removed = g_list_remove_link (removed, find);
      started = g_list_concat (started, find);
    } else {
      /* not started, add to pending list */
      pending = g_list_append (pending, gst_object_ref (provider));
    }
  }
  g_list_free_full (removed, gst_object_unref);
  removed = NULL;

  while (pending) {
    GstDeviceProvider *provider = pending->data;
502 503

    if (gst_device_provider_can_monitor (provider)) {
504
      GST_OBJECT_UNLOCK (monitor);
505

506 507
      if (!gst_device_provider_start (provider))
        goto start_failed;
508

509
      GST_OBJECT_LOCK (monitor);
510
    }
511 512
    started = g_list_prepend (started, provider);
    pending = g_list_delete_link (pending, pending);
513

514 515 516
    if (monitor->priv->cookie != cookie)
      goto again;
  }
517 518
  monitor->priv->started = TRUE;
  GST_OBJECT_UNLOCK (monitor);
519

520 521
  g_list_free_full (started, gst_object_unref);

522
  return TRUE;
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

start_failed:
  {
    GST_OBJECT_LOCK (monitor);
    gst_bus_set_flushing (monitor->priv->bus, TRUE);
    GST_OBJECT_UNLOCK (monitor);

    while (started) {
      GstDeviceProvider *provider = started->data;

      gst_device_provider_stop (provider);
      gst_object_unref (provider);

      started = g_list_delete_link (started, started);
    }
    return FALSE;
  }
540 541 542
}

/**
543
 * gst_device_monitor_stop:
544
 * @monitor: A #GstDeviceProvider
545 546
 *
 * Stops monitoring the devices.
547 548
 *
 * Since: 1.4
549 550
 */
void
551
gst_device_monitor_stop (GstDeviceMonitor * monitor)
552 553
{
  guint i;
554
  GList *started = NULL;
555

556
  g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
557

558
  gst_bus_set_flushing (monitor->priv->bus, TRUE);
559

560
  GST_OBJECT_LOCK (monitor);
561 562 563 564
  for (i = 0; i < monitor->priv->providers->len; i++) {
    GstDeviceProvider *provider =
        g_ptr_array_index (monitor->priv->providers, i);

565 566 567 568 569 570 571
    started = g_list_prepend (started, gst_object_ref (provider));
  }
  GST_OBJECT_UNLOCK (monitor);

  while (started) {
    GstDeviceProvider *provider = started->data;

572 573
    if (gst_device_provider_can_monitor (provider))
      gst_device_provider_stop (provider);
574 575 576

    started = g_list_delete_link (started, started);
    gst_object_unref (provider);
577
  }
578 579

  GST_OBJECT_LOCK (monitor);
580 581
  monitor->priv->started = FALSE;
  GST_OBJECT_UNLOCK (monitor);
582 583 584

}

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
static void
provider_hidden (GstDeviceProvider * provider, const gchar * hidden,
    GstDeviceMonitor * monitor)
{
  GST_OBJECT_LOCK (monitor);
  monitor->priv->hidden =
      g_list_prepend (monitor->priv->hidden, g_strdup (hidden));
  GST_OBJECT_UNLOCK (monitor);
}

static void
provider_unhidden (GstDeviceProvider * provider, const gchar * hidden,
    GstDeviceMonitor * monitor)
{
  GList *find;

  GST_OBJECT_LOCK (monitor);
  find =
      g_list_find_custom (monitor->priv->hidden, hidden,
      (GCompareFunc) g_strcmp0);
  if (find) {
    g_free (find->data);
    monitor->priv->hidden = g_list_delete_link (monitor->priv->hidden, find);
  }
  GST_OBJECT_UNLOCK (monitor);
}

612
/**
613 614
 * gst_device_monitor_add_filter:
 * @monitor: a device monitor
615
 * @classes: (allow-none): device classes to use as filter or %NULL for any class
616
 * @caps: (allow-none): the #GstCaps to filter or %NULL for ANY
617
 *
618
 * Adds a filter for which #GstDevice will be monitored, any device that matches
619 620 621 622 623 624 625 626
 * all these classes and the #GstCaps will be returned.
 *
 * If this function is called multiple times to add more filters, each will be
 * matched independently. That is, adding more filters will not further restrict
 * what devices are matched.
 *
 * The #GstCaps supported by the device as returned by gst_device_get_caps() are
 * not intersected with caps filters added using this function.
627 628 629
 *
 * Filters must be added before the #GstDeviceMonitor is started.
 *
Nicolas Dufresne's avatar
Nicolas Dufresne committed
630
 * Returns: The id of the new filter or 0 if no provider matched the filter's
631
 *  classes.
632 633 634
 *
 * Since: 1.4
 */
635 636 637
guint
gst_device_monitor_add_filter (GstDeviceMonitor * monitor,
    const gchar * classes, GstCaps * caps)
638 639
{
  GList *factories = NULL;
640 641 642
  struct DeviceFilter *filter;
  guint id = 0;
  gboolean matched = FALSE;
643

644 645
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), 0);
  g_return_val_if_fail (!monitor->priv->started, 0);
646

647
  GST_OBJECT_LOCK (monitor);
648

649 650 651 652 653 654 655 656
  filter = g_slice_new0 (struct DeviceFilter);
  filter->id = monitor->priv->last_id++;
  if (caps)
    filter->caps = gst_caps_ref (caps);
  else
    filter->caps = gst_caps_new_any ();
  if (classes)
    filter->classesv = g_strsplit (classes, "/", 0);
657

658
  factories = gst_device_provider_factory_list_get_device_providers (1);
659

660 661
  while (factories) {
    GstDeviceProviderFactory *factory = factories->data;
662

663 664
    if (gst_device_provider_factory_has_classesv (factory, filter->classesv)) {
      GstDeviceProvider *provider;
665

666
      provider = gst_device_provider_factory_get (factory);
667

668 669
      if (provider) {
        guint i;
670

671 672 673 674 675 676 677 678 679
        for (i = 0; i < monitor->priv->providers->len; i++) {
          if (g_ptr_array_index (monitor->priv->providers, i) == provider) {
            gst_object_unref (provider);
            provider = NULL;
            matched = TRUE;
            break;
          }
        }
      }
680

681 682
      if (provider) {
        GstBus *bus = gst_device_provider_get_bus (provider);
683

684 685 686 687 688 689
        update_hidden_providers_list (&monitor->priv->hidden, provider);
        g_signal_connect (provider, "provider-hidden",
            (GCallback) provider_hidden, monitor);
        g_signal_connect (provider, "provider-unhidden",
            (GCallback) provider_unhidden, monitor);

690 691 692 693 694 695 696 697
        matched = TRUE;
        gst_bus_enable_sync_message_emission (bus);
        g_signal_connect (bus, "sync-message",
            G_CALLBACK (bus_sync_message), monitor);
        gst_object_unref (bus);
        g_ptr_array_add (monitor->priv->providers, provider);
        monitor->priv->cookie++;
      }
698 699
    }

700
    factories = g_list_remove (factories, factory);
701 702 703
    gst_object_unref (factory);
  }

704 705
  /* Ensure there is no leak here */
  g_assert (factories == NULL);
706

707
  if (matched)
708
    id = filter->id;
709
  g_ptr_array_add (monitor->priv->filters, filter);
710

711
  GST_OBJECT_UNLOCK (monitor);
712

713
  return id;
714 715
}

716
/**
717
 * gst_device_monitor_remove_filter:
718
 * @monitor: a device monitor
719
 * @filter_id: the id of the filter
720
 *
721 722
 * Removes a filter from the #GstDeviceMonitor using the id that was returned
 * by gst_device_monitor_add_filter().
723
 *
724
 * Returns: %TRUE of the filter id was valid, %FALSE otherwise
725 726 727
 *
 * Since: 1.4
 */
728 729
gboolean
gst_device_monitor_remove_filter (GstDeviceMonitor * monitor, guint filter_id)
730
{
731 732
  guint i, j;
  gboolean removed = FALSE;
733

734 735 736
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
  g_return_val_if_fail (!monitor->priv->started, FALSE);
  g_return_val_if_fail (filter_id > 0, FALSE);
737

738
  GST_OBJECT_LOCK (monitor);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
  for (i = 0; i < monitor->priv->filters->len; i++) {
    struct DeviceFilter *filter = g_ptr_array_index (monitor->priv->filters, i);

    if (filter->id == filter_id) {
      g_ptr_array_remove_index (monitor->priv->filters, i);
      removed = TRUE;
      break;
    }
  }

  if (removed) {
    for (i = 0; i < monitor->priv->providers->len; i++) {
      GstDeviceProvider *provider =
          g_ptr_array_index (monitor->priv->providers, i);
      GstDeviceProviderFactory *factory =
          gst_device_provider_get_factory (provider);
      gboolean valid = FALSE;

      for (j = 0; j < monitor->priv->filters->len; j++) {
        struct DeviceFilter *filter =
            g_ptr_array_index (monitor->priv->filters, j);

        if (gst_device_provider_factory_has_classesv (factory,
                filter->classesv)) {
          valid = TRUE;
          break;
        }
      }

      if (!valid) {
        monitor->priv->cookie++;
        gst_device_monitor_remove (monitor, i);
        i--;
      }
    }
  }

776
  GST_OBJECT_UNLOCK (monitor);
777

778
  return removed;
779 780
}

781 782


783
/**
784
 * gst_device_monitor_new:
785
 *
786
 * Create a new #GstDeviceMonitor
787
 *
788
 * Returns: (transfer full): a new device monitor.
789 790 791
 *
 * Since: 1.4
 */
792 793
GstDeviceMonitor *
gst_device_monitor_new (void)
794
{
795
  return g_object_new (GST_TYPE_DEVICE_MONITOR, NULL);
796 797 798
}

/**
799
 * gst_device_monitor_get_bus:
800
 * @monitor: a #GstDeviceProvider
801
 *
802
 * Gets the #GstBus of this #GstDeviceMonitor
803 804
 *
 * Returns: (transfer full): a #GstBus
805 806
 *
 * Since: 1.4
807 808
 */
GstBus *
809
gst_device_monitor_get_bus (GstDeviceMonitor * monitor)
810
{
811
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
812 813 814

  return gst_object_ref (monitor->priv->bus);
}
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

/**
 * gst_device_monitor_get_providers:
 * @monitor: a #GstDeviceMonitor
 *
 * Get a list of the currently selected device provider factories.
 *
 * This
 *
 * Returns: (transfer full) (array zero-terminated=1) (element-type gchar*):
 *     A list of device provider factory names that are currently being
 *     monitored by @monitor or %NULL when nothing is being monitored.
 *
 * Since: 1.6
 */
gchar **
gst_device_monitor_get_providers (GstDeviceMonitor * monitor)
{
  guint i, len;
  gchar **res = NULL;

  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);

  GST_OBJECT_LOCK (monitor);
  len = monitor->priv->providers->len;
  if (len == 0)
    goto done;

  res = g_new (gchar *, len + 1);

  for (i = 0; i < len; i++) {
    GstDeviceProvider *provider =
        g_ptr_array_index (monitor->priv->providers, i);
    GstDeviceProviderFactory *factory =
        gst_device_provider_get_factory (provider);

    res[i] = g_strdup (GST_OBJECT_NAME (factory));
  }
  res[i] = NULL;

done:
  GST_OBJECT_UNLOCK (monitor);

  return res;
}
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

/**
 * gst_device_monitor_set_show_all_devices:
 * @monitor: a #GstDeviceMonitor
 * @show_all: show all devices
 *
 * Set if all devices should be visible, even those devices from hidden
 * providers. Setting @show_all to true might show some devices multiple times.
 *
 * Since: 1.6
 */
void
gst_device_monitor_set_show_all_devices (GstDeviceMonitor * monitor,
    gboolean show_all)
{
  g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));

  GST_OBJECT_LOCK (monitor);
  monitor->priv->show_all = show_all;
  GST_OBJECT_UNLOCK (monitor);
}

/**
 * gst_device_monitor_get_show_all_devices:
 * @monitor: a #GstDeviceMonitor
 *
 * Get if @monitor is curretly showing all devices, even those from hidden
 * providers.
 *
 * Returns: %TRUE when all devices will be shown.
 *
 * Since: 1.6
 */
gboolean
gst_device_monitor_get_show_all_devices (GstDeviceMonitor * monitor)
{
  gboolean res;

  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);

  GST_OBJECT_LOCK (monitor);
  res = monitor->priv->show_all;
  GST_OBJECT_UNLOCK (monitor);

  return res;
}