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 ();
73 struct udev_monitor *mon = udev_monitor_new_from_netlink (udev, "udev");
76 vlc_sd_probe_Add (probe, "v4l", N_("Capture devices"));
77 vlc_sd_probe_Add (probe, "disc", N_("Discs"));
78 udev_monitor_unref (mon);
81 return VLC_PROBE_CONTINUE;
86 dev_t devnum; /* must be first */
88 services_discovery_t *sd;
94 char * (*get_mrl) (struct udev_device *dev);
95 char * (*get_name) (struct udev_device *dev);
96 char * (*get_cat) (struct udev_device *dev);
100 struct services_discovery_sys_t
102 const struct subsys *subsys;
103 struct udev_monitor *monitor;
109 * Compares two devices (to support binary search).
111 static int cmpdev (const void *a, const void *b)
113 const dev_t *da = a, *db = b;
114 dev_t delta = *da - *db;
116 if (sizeof (delta) > sizeof (int))
117 return delta ? ((delta > 0) ? 1 : -1) : 0;
118 return (signed)delta;
121 static void DestroyDevice (void *data)
123 struct device *d = data;
126 services_discovery_RemoveItem (d->sd, d->item);
127 vlc_gc_decref (d->item);
131 static char *decode_property (struct udev_device *, const char *);
134 * Adds a udev device.
136 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
138 services_discovery_sys_t *p_sys = sd->p_sys;
140 char *mrl = p_sys->subsys->get_mrl (dev);
142 return 0; /* don't know if it was an error... */
143 char *name = p_sys->subsys->get_name (dev);
144 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
147 p_sys->subsys->item_type);
148 msg_Dbg (sd, "adding %s (%s)", mrl, name);
154 struct device *d = malloc (sizeof (*d));
157 vlc_gc_decref (item);
160 d->devnum = udev_device_get_devnum (dev);
164 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
165 if (dp == NULL) /* Out-of-memory */
170 if (*dp != d) /* Overwrite existing device */
176 name = p_sys->subsys->get_cat (dev);
177 services_discovery_AddItem (sd, item, name ? name : "Generic");
184 * Removes a udev device (if present).
186 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
188 services_discovery_sys_t *p_sys = sd->p_sys;
190 dev_t num = udev_device_get_devnum (dev);
191 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
195 struct device *d = *dp;
196 tdelete (d, &p_sys->root, cmpdev);
200 static void *Run (void *);
203 * Probes and initializes.
205 static int Open (vlc_object_t *obj, const struct subsys *subsys)
207 services_discovery_t *sd = (services_discovery_t *)obj;
208 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
213 p_sys->subsys = subsys;
216 struct udev_monitor *mon = NULL;
217 struct udev *udev = udev_new ();
221 mon = udev_monitor_new_from_netlink (udev, "udev");
223 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
226 p_sys->monitor = mon;
228 /* Enumerate existing devices */
229 struct udev_enumerate *devenum = udev_enumerate_new (udev);
232 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
234 udev_enumerate_unref (devenum);
238 udev_monitor_enable_receiving (mon);
239 /* Note that we enumerate _after_ monitoring is enabled so that we do not
240 * loose device events occuring while we are enumerating. We could still
241 * loose events if the Netlink socket receive buffer overflows. */
242 udev_enumerate_scan_devices (devenum);
243 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
244 struct udev_list_entry *deventry;
245 udev_list_entry_foreach (deventry, devlist)
247 const char *path = udev_list_entry_get_name (deventry);
248 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
250 udev_device_unref (dev);
252 udev_enumerate_unref (devenum);
254 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
255 { /* Fallback without thread */
256 udev_monitor_unref (mon);
258 p_sys->monitor = NULL;
264 udev_monitor_unref (mon);
274 static void Close (vlc_object_t *obj)
276 services_discovery_t *sd = (services_discovery_t *)obj;
277 services_discovery_sys_t *p_sys = sd->p_sys;
279 if (p_sys->monitor != NULL)
281 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
283 vlc_cancel (p_sys->thread);
284 vlc_join (p_sys->thread, NULL);
285 udev_monitor_unref (p_sys->monitor);
289 tdestroy (p_sys->root, DestroyDevice);
293 static void *Run (void *data)
295 services_discovery_t *sd = data;
296 services_discovery_sys_t *p_sys = sd->p_sys;
297 struct udev_monitor *mon = p_sys->monitor;
299 int fd = udev_monitor_get_fd (mon);
300 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
304 while (poll (&ufd, 1, -1) == -1)
308 int canc = vlc_savecancel ();
309 struct udev_device *dev = udev_monitor_receive_device (mon);
313 const char *action = udev_device_get_action (dev);
314 if (!strcmp (action, "add"))
316 else if (!strcmp (action, "remove"))
317 RemoveDevice (sd, dev);
318 else if (!strcmp (action, "change"))
320 RemoveDevice (sd, dev);
323 udev_device_unref (dev);
324 vlc_restorecancel (canc);
330 * Converts an hexadecimal digit to an integer.
332 static int hex (char c)
334 if (c >= '0' && c <= '9')
336 if (c >= 'A' && c <= 'F')
338 if (c >= 'a' && c <= 'f')
344 * Decodes a udev hexadecimal-encoded property.
346 static char *decode (const char *enc)
348 char *ret = enc ? strdup (enc) : NULL;
353 for (const char *in = ret; *in; out++)
357 if ((in[0] == '\\') && (in[1] == 'x')
358 && ((h1 = hex (in[2])) != -1)
359 && ((h2 = hex (in[3])) != -1))
361 *out = (h1 << 4) | h2;
374 static char *decode_property (struct udev_device *dev, const char *name)
376 return decode (udev_device_get_property_value (dev, name));
379 /*** Video4Linux support ***/
380 static bool is_v4l_legacy (struct udev_device *dev)
384 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
385 return version && !strcmp (version, "1");
388 static char *v4l_get_mrl (struct udev_device *dev)
390 /* Determine media location */
391 const char *scheme = "v4l2";
392 if (is_v4l_legacy (dev))
394 const char *node = udev_device_get_devnode (dev);
397 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
402 static char *v4l_get_name (struct udev_device *dev)
404 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
405 return prd ? strdup (prd) : NULL;
408 static char *v4l_get_cat (struct udev_device *dev)
410 return decode_property (dev, "ID_VENDOR_ENC");
413 int OpenV4L (vlc_object_t *obj)
415 static const struct subsys subsys = {
416 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
419 return Open (obj, &subsys);
422 /*** Discs support ***/
423 static char *disc_get_mrl (struct udev_device *dev)
427 val = udev_device_get_property_value (dev, "ID_CDROM");
429 return NULL; /* Ignore non-optical block devices */
431 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
432 if ((val == NULL) || !strcmp (val, "blank"))
433 return NULL; /* ignore empty drives and virgin recordable discs */
435 const char *scheme = "file";
436 val = udev_device_get_property_value (dev,
437 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
438 if (val && atoi (val))
439 scheme = "cdda"; /* Audio CD rather than file system */
440 #if 0 /* we can use file:// for DVDs */
441 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
442 if (val && atoi (val))
445 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
446 if (val && atoi (val))
449 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
450 if (val && atoi (val))
454 val = udev_device_get_devnode (dev);
457 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
462 static char *disc_get_name (struct udev_device *dev)
464 return decode_property (dev, "ID_FS_LABEL_ENC");
467 static char *disc_get_cat (struct udev_device *dev)
470 const char *cat = "Unknown";
472 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
473 if (val && atoi (val))
475 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
476 if (val && atoi (val))
478 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
479 if (val && atoi (val))
480 cat = "Blue-ray disc";
481 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
482 if (val && atoi (val))
488 int OpenDisc (vlc_object_t *obj)
490 static const struct subsys subsys = {
491 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
494 return Open (obj, &subsys);