gstdevicemonitor.c 12.3 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 24
 * SECTION:gstdevicemonitor
 * @short_description: A device monitor and prober
25
 * @see_also: #GstDevice, #GstDeviceProvider
26
 *
27
 * Applications should create a #GstDeviceMonitor when they want
28
 * to probe, list and monitor devices of a specific type. The
29
 * #GstDeviceMonitor will create the appropriate
30
 * #GstDeviceProvider objects and manage them. It will then post
31 32 33 34 35 36 37
 * messages on its #GstBus for devices that have been added and
 * removed.
 *
 * Since: 1.4
 */


38 39 40 41
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

42
#include "gst_private.h"
43
#include "gstdevicemonitor.h"
44

45
struct _GstDeviceMonitorPrivate
46 47 48 49 50
{
  gboolean started;

  GstBus *bus;

51
  GPtrArray *providers;
52 53 54
  guint cookie;

  GstCaps *caps;
55
  gchar *classes;
56 57 58
};


59
G_DEFINE_TYPE (GstDeviceMonitor, gst_device_monitor, GST_TYPE_OBJECT);
60

61
static void gst_device_monitor_dispose (GObject * object);
62 63

static void
64
gst_device_monitor_class_init (GstDeviceMonitorClass * klass)
65 66 67
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

68
  g_type_class_add_private (klass, sizeof (GstDeviceMonitorPrivate));
69

70
  object_class->dispose = gst_device_monitor_dispose;
71 72 73 74
}

static void
bus_sync_message (GstBus * bus, GstMessage * message,
75
    GstDeviceMonitor * monitor)
76
{
77
  GstMessageType type = GST_MESSAGE_TYPE (message);
78

79
  if (type == GST_MESSAGE_DEVICE_ADDED || type == GST_MESSAGE_DEVICE_REMOVED) {
80
    gboolean matches;
81 82 83
    GstCaps *caps;
    GstDevice *device;

84
    if (type == GST_MESSAGE_DEVICE_ADDED)
85 86 87 88 89 90
      gst_message_parse_device_added (message, &device);
    else
      gst_message_parse_device_removed (message, &device);

    GST_OBJECT_LOCK (monitor);
    caps = gst_device_get_caps (device);
91 92
    matches = gst_caps_can_intersect (monitor->priv->caps, caps) &&
        gst_device_has_classes (device, monitor->priv->classes);
93 94 95
    gst_caps_unref (caps);
    GST_OBJECT_UNLOCK (monitor);

96
    if (matches)
97 98 99 100 101 102
      gst_bus_post (monitor->priv->bus, gst_message_ref (message));
  }
}


static void
103
gst_device_monitor_init (GstDeviceMonitor * self)
104 105 106 107
{
  GList *factories;

  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
108
      GST_TYPE_DEVICE_MONITOR, GstDeviceMonitorPrivate);
109 110 111 112

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

113
  self->priv->providers = g_ptr_array_new ();
114
  self->priv->caps = gst_caps_new_any ();
115
  self->priv->classes = g_strdup ("");
116 117

  factories =
118 119
      gst_device_provider_factory_list_get_device_providers (self->
      priv->classes, 1);
120 121

  while (factories) {
122 123
    GstDeviceProviderFactory *factory = factories->data;
    GstDeviceProvider *provider;
124 125 126

    factories = g_list_remove (factories, factory);

127 128 129
    provider = gst_device_provider_factory_get (factory);
    if (provider) {
      GstBus *bus = gst_device_provider_get_bus (provider);
130 131

      gst_bus_enable_sync_message_emission (bus);
132
      g_signal_connect (bus, "sync-message",
133
          G_CALLBACK (bus_sync_message), self);
134
      g_ptr_array_add (self->priv->providers, provider);
135 136 137 138 139 140 141 142
    }

    gst_object_unref (factory);
  }
}


static void
143
gst_device_monitor_remove (GstDeviceMonitor * self, guint i)
144
{
145
  GstDeviceProvider *provider = g_ptr_array_index (self->priv->providers, i);
146 147
  GstBus *bus;

148
  g_ptr_array_remove_index_fast (self->priv->providers, i);
149

150
  bus = gst_device_provider_get_bus (provider);
151 152 153
  g_signal_handlers_disconnect_by_func (bus, bus_sync_message, self);
  gst_object_unref (bus);

154
  gst_object_unref (provider);
155 156 157
}

static void
158
gst_device_monitor_dispose (GObject * object)
159
{
160
  GstDeviceMonitor *self = GST_DEVICE_MONITOR (object);
161 162 163

  g_return_if_fail (self->priv->started == FALSE);

164 165
  if (self->priv->providers) {
    while (self->priv->providers->len)
166
      gst_device_monitor_remove (self, self->priv->providers->len - 1);
167 168
    g_ptr_array_unref (self->priv->providers);
    self->priv->providers = NULL;
169 170 171
  }

  gst_caps_replace (&self->priv->caps, NULL);
172
  g_free (self->priv->classes);
173 174
  gst_object_replace ((GstObject **) & self->priv->bus, NULL);

175
  G_OBJECT_CLASS (gst_device_monitor_parent_class)->dispose (object);
176 177 178
}

/**
179
 * gst_device_monitor_get_devices:
180
 * @monitor: A #GstDeviceProvider
181 182
 *
 * Gets a list of devices from all of the relevant monitors. This may actually
183
 * probe the hardware if the monitor is not currently started.
184 185 186
 *
 * Returns: (transfer full) (element-type GstDevice): a #GList of
 *   #GstDevice
187 188
 *
 * Since: 1.4
189 190 191
 */

GList *
192
gst_device_monitor_get_devices (GstDeviceMonitor * monitor)
193 194 195 196 197
{
  GList *devices = NULL;
  guint i;
  guint cookie;

198
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
199

200
  GST_OBJECT_LOCK (monitor);
201 202 203 204 205 206

again:

  g_list_free_full (devices, gst_object_unref);
  devices = NULL;

207
  cookie = monitor->priv->cookie;
208

209
  for (i = 0; i < monitor->priv->providers->len; i++) {
210
    GList *tmpdev;
211 212
    GstDeviceProvider *provider =
        gst_object_ref (g_ptr_array_index (monitor->priv->providers, i));
213 214
    GList *item;

215
    GST_OBJECT_UNLOCK (monitor);
216

217
    tmpdev = gst_device_provider_get_devices (provider);
218 219 220 221 222

    for (item = tmpdev; item; item = item->next) {
      GstDevice *dev = GST_DEVICE (item->data);
      GstCaps *caps = gst_device_get_caps (dev);

223 224
      if (gst_caps_can_intersect (monitor->priv->caps, caps) &&
          gst_device_has_classes (dev, monitor->priv->classes))
225 226 227 228 229
        devices = g_list_prepend (devices, gst_object_ref (dev));
      gst_caps_unref (caps);
    }

    g_list_free_full (tmpdev, gst_object_unref);
230
    gst_object_unref (provider);
231

232
    GST_OBJECT_LOCK (monitor);
233

234
    if (monitor->priv->cookie != cookie)
235 236 237
      goto again;
  }

238
  GST_OBJECT_UNLOCK (monitor);
239 240 241 242 243

  return devices;
}

/**
244 245
 * gst_device_monitor_start:
 * @monitor: A #GstDeviceMonitor
246 247
 *
 * Starts monitoring the devices, one this has succeeded, the
248 249
 * %GST_MESSAGE_DEVICE_ADDED and %GST_MESSAGE_DEVICE_REMOVED messages
 * will be emitted on the bus when the list of devices changes.
250 251
 *
 * Returns: %TRUE if the device monitoring could be started
252 253
 *
 * Since: 1.4
254 255 256
 */

gboolean
257
gst_device_monitor_start (GstDeviceMonitor * monitor)
258 259 260
{
  guint i;

261
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), FALSE);
262

263
  GST_OBJECT_LOCK (monitor);
264

265
  if (monitor->priv->providers->len == 0) {
266
    GST_OBJECT_UNLOCK (monitor);
267 268 269
    return FALSE;
  }

270
  gst_bus_set_flushing (monitor->priv->bus, FALSE);
271

272 273
  for (i = 0; i < monitor->priv->providers->len; i++) {
    if (!gst_device_provider_start (g_ptr_array_index (monitor->priv->providers,
274 275
                i))) {
      gst_bus_set_flushing (monitor->priv->bus, TRUE);
276 277

      for (; i != 0; i--)
278
        gst_device_provider_stop (g_ptr_array_index (monitor->priv->providers,
279 280
                i - 1));

281
      GST_OBJECT_UNLOCK (monitor);
282 283 284 285
      return FALSE;
    }
  }

286 287
  monitor->priv->started = TRUE;
  GST_OBJECT_UNLOCK (monitor);
288 289 290 291 292

  return TRUE;
}

/**
293
 * gst_device_monitor_stop:
294
 * @monitor: A #GstDeviceProvider
295 296
 *
 * Stops monitoring the devices.
297 298
 *
 * Since: 1.4
299 300
 */
void
301
gst_device_monitor_stop (GstDeviceMonitor * monitor)
302 303 304
{
  guint i;

305
  g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
306

307
  gst_bus_set_flushing (monitor->priv->bus, TRUE);
308

309
  GST_OBJECT_LOCK (monitor);
310 311
  for (i = 0; i < monitor->priv->providers->len; i++)
    gst_device_provider_stop (g_ptr_array_index (monitor->priv->providers, i));
312 313
  monitor->priv->started = FALSE;
  GST_OBJECT_UNLOCK (monitor);
314 315 316

}

317
/**
318 319
 * gst_device_monitor_set_classes_filter:
 * @monitor: the device monitor
320 321 322 323 324 325 326 327
 * @classes: device classes to use as filter
 *
 * Filter devices monitored by device class, e.g. in case you are only
 * interested in a certain type of device like audio devices or
 * video sources.
 *
 * Since: 1.4
 */
328
void
329
gst_device_monitor_set_classes_filter (GstDeviceMonitor * monitor,
330
    const gchar * classes)
331 332 333 334
{
  GList *factories = NULL;
  guint i;

335
  g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
336
  g_return_if_fail (!monitor->priv->started);
337

338 339 340
  GST_OBJECT_LOCK (monitor);
  if (!strcmp (monitor->priv->classes, classes)) {
    GST_OBJECT_UNLOCK (monitor);
341 342 343
    return;
  }

344 345
  g_free (monitor->priv->classes);
  monitor->priv->classes = g_strdup (classes);
346

347 348
  factories = gst_device_provider_factory_list_get_device_providers (classes,
      1);
349

350 351 352
  for (i = 0; i < monitor->priv->providers->len; i++) {
    GstDeviceProvider *provider;
    GstDeviceProviderFactory *f;
353 354
    GList *item;

355 356
    provider = g_ptr_array_index (monitor->priv->providers, i);
    f = gst_device_provider_get_factory (provider);
357

358 359 360 361 362 363 364 365 366
    item = g_list_find (factories, f);

    if (item) {
      /* If the item is in our list, then remove it from the list of factories,
       * we don't have it to re-create it later
       */
      factories = g_list_remove_link (factories, item);
      gst_object_unref (f);
    } else {
367
      /* If it's not in our list, them remove it from the list of providers.
368 369
       */

370
      monitor->priv->cookie++;
371
      gst_device_monitor_remove (monitor, i);
372 373 374 375 376
      i--;
    }
  }

  while (factories) {
377 378
    GstDeviceProviderFactory *factory = factories->data;
    GstDeviceProvider *provider;
379 380 381

    factories = g_list_remove (factories, factory);

382 383 384
    provider = gst_device_provider_factory_get (factory);
    if (provider) {
      GstBus *bus = gst_device_provider_get_bus (provider);
385 386

      gst_bus_enable_sync_message_emission (bus);
387
      g_signal_connect (bus, "sync-message",
388
          G_CALLBACK (bus_sync_message), monitor);
389
      gst_object_unref (bus);
390
      g_ptr_array_add (monitor->priv->providers, provider);
391
      monitor->priv->cookie++;
392 393 394 395 396
    }

    gst_object_unref (factory);
  }

397
  GST_OBJECT_UNLOCK (monitor);
398 399
}

400
/**
401 402
 * gst_device_monitor_get_classes_filter:
 * @monitor: the device monitor
403 404 405 406 407 408 409
 *
 * Return the type (device classes) filter active for device filtering.
 *
 * Returns: string of device classes that are being filtered.
 *
 * Since: 1.4
 */
410
gchar *
411
gst_device_monitor_get_classes_filter (GstDeviceMonitor * monitor)
412
{
413
  gchar *res;
414

415
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), 0);
416

417 418 419
  GST_OBJECT_LOCK (monitor);
  res = g_strdup (monitor->priv->classes);
  GST_OBJECT_UNLOCK (monitor);
420 421 422 423

  return res;
}

424
/**
425 426
 * gst_device_monitor_set_caps_filter:
 * @monitor: the device monitor
427 428 429 430 431 432 433
 * @caps: caps to filter
 *
 * Set caps to use as filter for devices. By default ANY caps are used,
 * meaning no caps filter is active.
 *
 * Since: 1.4
 */
434
void
435
gst_device_monitor_set_caps_filter (GstDeviceMonitor * monitor, GstCaps * caps)
436
{
437
  g_return_if_fail (GST_IS_DEVICE_MONITOR (monitor));
438 439
  g_return_if_fail (GST_IS_CAPS (caps));

440 441 442
  GST_OBJECT_LOCK (monitor);
  gst_caps_replace (&monitor->priv->caps, caps);
  GST_OBJECT_UNLOCK (monitor);
443 444
}

445
/**
446 447
 * gst_device_monitor_get_caps_filter:
 * @monitor: a device monitor
448
 *
449
 * Get the #GstCaps filter set by gst_device_monitor_set_caps_filter().
450
 *
451 452 453 454
 * Returns: (transfer full): the filter caps that are active (or ANY caps)
 *
 * Since: 1.4
 */
455
GstCaps *
456
gst_device_monitor_get_caps_filter (GstDeviceMonitor * monitor)
457 458 459
{
  GstCaps *res;

460
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
461

462 463 464
  GST_OBJECT_LOCK (monitor);
  res = gst_caps_ref (monitor->priv->caps);
  GST_OBJECT_UNLOCK (monitor);
465 466 467 468

  return res;
}

469
/**
470
 * gst_device_monitor_new:
471
 *
472
 * Create a new #GstDeviceMonitor
473
 *
474
 * Returns: (transfer full): a new device monitor.
475 476 477
 *
 * Since: 1.4
 */
478 479
GstDeviceMonitor *
gst_device_monitor_new (void)
480
{
481
  return g_object_new (GST_TYPE_DEVICE_MONITOR, NULL);
482 483 484
}

/**
485
 * gst_device_monitor_get_bus:
486
 * @monitor: a #GstDeviceProvider
487
 *
488
 * Gets the #GstBus of this #GstDeviceMonitor
489 490
 *
 * Returns: (transfer full): a #GstBus
491 492
 *
 * Since: 1.4
493 494
 */
GstBus *
495
gst_device_monitor_get_bus (GstDeviceMonitor * monitor)
496
{
497
  g_return_val_if_fail (GST_IS_DEVICE_MONITOR (monitor), NULL);
498 499 500

  return gst_object_ref (monitor->priv->bus);
}