3 * @brief List of multimedia devices for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30 #include <vlc_plugin.h>
35 static int OpenV4L (vlc_object_t *);
36 static int OpenDisc (vlc_object_t *);
37 static void Close (vlc_object_t *);
38 static int vlc_sd_probe_Open (vlc_object_t *);
44 set_shortname (N_("Devices"))
45 set_description (N_("Capture devices"))
46 set_category (CAT_PLAYLIST)
47 set_subcategory (SUBCAT_PLAYLIST_SD)
48 set_capability ("services_discovery", 0)
49 set_callbacks (OpenV4L, Close)
53 set_shortname (N_("Discs"))
54 set_description (N_("Discs"))
55 set_category (CAT_PLAYLIST)
56 set_subcategory (SUBCAT_PLAYLIST_SD)
57 set_capability ("services_discovery", 0)
58 set_callbacks (OpenDisc, Close)
61 VLC_SD_PROBE_SUBMODULE
65 static int vlc_sd_probe_Open (vlc_object_t *obj)
67 vlc_probe_t *probe = (vlc_probe_t *)obj;
69 struct udev *udev = udev_new ();
71 return VLC_PROBE_CONTINUE;
73 struct udev_monitor *mon = udev_monitor_new_from_netlink (udev, "udev");
76 vlc_sd_probe_Add (probe, "v4l{longname=\"Video capture\"}",
77 N_("Video capture"), SD_CAT_DEVICES);
78 vlc_sd_probe_Add (probe, "disc{longname=\"Discs\"}", N_("Discs"), SD_CAT_MYCOMPUTER);
79 udev_monitor_unref (mon);
82 return VLC_PROBE_CONTINUE;
87 dev_t devnum; /* must be first */
89 services_discovery_t *sd;
95 char * (*get_mrl) (struct udev_device *dev);
96 char * (*get_name) (struct udev_device *dev);
97 char * (*get_cat) (struct udev_device *dev);
101 struct services_discovery_sys_t
103 const struct subsys *subsys;
104 struct udev_monitor *monitor;
110 * Compares two devices (to support binary search).
112 static int cmpdev (const void *a, const void *b)
114 const dev_t *da = a, *db = b;
115 dev_t delta = *da - *db;
117 if (sizeof (delta) > sizeof (int))
118 return delta ? ((delta > 0) ? 1 : -1) : 0;
119 return (signed)delta;
122 static void DestroyDevice (void *data)
124 struct device *d = data;
127 services_discovery_RemoveItem (d->sd, d->item);
128 vlc_gc_decref (d->item);
132 static char *decode_property (struct udev_device *, const char *);
135 * Adds a udev device.
137 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
139 services_discovery_sys_t *p_sys = sd->p_sys;
141 char *mrl = p_sys->subsys->get_mrl (dev);
143 return 0; /* don't know if it was an error... */
144 char *name = p_sys->subsys->get_name (dev);
145 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
148 p_sys->subsys->item_type);
149 msg_Dbg (sd, "adding %s (%s)", mrl, name);
155 struct device *d = malloc (sizeof (*d));
158 vlc_gc_decref (item);
161 d->devnum = udev_device_get_devnum (dev);
165 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
166 if (dp == NULL) /* Out-of-memory */
171 if (*dp != d) /* Overwrite existing device */
177 name = p_sys->subsys->get_cat (dev);
178 services_discovery_AddItem (sd, item, name ? name : "Generic");
185 * Removes a udev device (if present).
187 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
189 services_discovery_sys_t *p_sys = sd->p_sys;
191 dev_t num = udev_device_get_devnum (dev);
192 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
196 struct device *d = *dp;
197 tdelete (d, &p_sys->root, cmpdev);
201 static void *Run (void *);
204 * Probes and initializes.
206 static int Open (vlc_object_t *obj, const struct subsys *subsys)
208 services_discovery_t *sd = (services_discovery_t *)obj;
209 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
214 p_sys->subsys = subsys;
217 struct udev_monitor *mon = NULL;
218 struct udev *udev = udev_new ();
222 mon = udev_monitor_new_from_netlink (udev, "udev");
224 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
227 p_sys->monitor = mon;
229 /* Enumerate existing devices */
230 struct udev_enumerate *devenum = udev_enumerate_new (udev);
233 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
235 udev_enumerate_unref (devenum);
239 udev_monitor_enable_receiving (mon);
240 /* Note that we enumerate _after_ monitoring is enabled so that we do not
241 * loose device events occuring while we are enumerating. We could still
242 * loose events if the Netlink socket receive buffer overflows. */
243 udev_enumerate_scan_devices (devenum);
244 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
245 struct udev_list_entry *deventry;
246 udev_list_entry_foreach (deventry, devlist)
248 const char *path = udev_list_entry_get_name (deventry);
249 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
251 udev_device_unref (dev);
253 udev_enumerate_unref (devenum);
255 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
256 { /* Fallback without thread */
257 udev_monitor_unref (mon);
259 p_sys->monitor = NULL;
265 udev_monitor_unref (mon);
275 static void Close (vlc_object_t *obj)
277 services_discovery_t *sd = (services_discovery_t *)obj;
278 services_discovery_sys_t *p_sys = sd->p_sys;
280 if (p_sys->monitor != NULL)
282 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
284 vlc_cancel (p_sys->thread);
285 vlc_join (p_sys->thread, NULL);
286 udev_monitor_unref (p_sys->monitor);
290 tdestroy (p_sys->root, DestroyDevice);
294 static void *Run (void *data)
296 services_discovery_t *sd = data;
297 services_discovery_sys_t *p_sys = sd->p_sys;
298 struct udev_monitor *mon = p_sys->monitor;
300 int fd = udev_monitor_get_fd (mon);
301 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
305 while (poll (&ufd, 1, -1) == -1)
309 int canc = vlc_savecancel ();
310 struct udev_device *dev = udev_monitor_receive_device (mon);
314 const char *action = udev_device_get_action (dev);
315 if (!strcmp (action, "add"))
317 else if (!strcmp (action, "remove"))
318 RemoveDevice (sd, dev);
319 else if (!strcmp (action, "change"))
321 RemoveDevice (sd, dev);
324 udev_device_unref (dev);
325 vlc_restorecancel (canc);
331 * Converts an hexadecimal digit to an integer.
333 static int hex (char c)
335 if (c >= '0' && c <= '9')
337 if (c >= 'A' && c <= 'F')
339 if (c >= 'a' && c <= 'f')
345 * Decodes a udev hexadecimal-encoded property.
347 static char *decode (const char *enc)
349 char *ret = enc ? strdup (enc) : NULL;
354 for (const char *in = ret; *in; out++)
358 if ((in[0] == '\\') && (in[1] == 'x')
359 && ((h1 = hex (in[2])) != -1)
360 && ((h2 = hex (in[3])) != -1))
362 *out = (h1 << 4) | h2;
375 static char *decode_property (struct udev_device *dev, const char *name)
377 return decode (udev_device_get_property_value (dev, name));
380 /*** Video4Linux support ***/
381 static bool is_v4l_legacy (struct udev_device *dev)
385 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
386 return version && !strcmp (version, "1");
389 static char *v4l_get_mrl (struct udev_device *dev)
391 /* Determine media location */
392 const char *scheme = "v4l2";
393 if (is_v4l_legacy (dev))
395 const char *node = udev_device_get_devnode (dev);
398 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
403 static char *v4l_get_name (struct udev_device *dev)
405 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
406 return prd ? strdup (prd) : NULL;
409 static char *v4l_get_cat (struct udev_device *dev)
411 return decode_property (dev, "ID_VENDOR_ENC");
414 int OpenV4L (vlc_object_t *obj)
416 static const struct subsys subsys = {
417 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
420 return Open (obj, &subsys);
423 /*** Discs support ***/
424 static char *disc_get_mrl (struct udev_device *dev)
428 val = udev_device_get_property_value (dev, "ID_CDROM");
430 return NULL; /* Ignore non-optical block devices */
432 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
433 if ((val == NULL) || !strcmp (val, "blank"))
434 return NULL; /* ignore empty drives and virgin recordable discs */
436 const char *scheme = "file";
437 val = udev_device_get_property_value (dev,
438 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
439 if (val && atoi (val))
440 scheme = "cdda"; /* Audio CD rather than file system */
441 #if 0 /* we can use file:// for DVDs */
442 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
443 if (val && atoi (val))
446 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
447 if (val && atoi (val))
450 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
451 if (val && atoi (val))
455 val = udev_device_get_devnode (dev);
458 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
463 static char *disc_get_name (struct udev_device *dev)
465 return decode_property (dev, "ID_FS_LABEL_ENC");
468 static char *disc_get_cat (struct udev_device *dev)
471 const char *cat = "Unknown";
473 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
474 if (val && atoi (val))
476 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
477 if (val && atoi (val))
479 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
480 if (val && atoi (val))
481 cat = "Blue-ray disc";
482 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
483 if (val && atoi (val))
489 int OpenDisc (vlc_object_t *obj)
491 static const struct subsys subsys = {
492 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
495 return Open (obj, &subsys);