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 *);
43 set_shortname (N_("Devices"))
44 set_description (N_("Capture devices"))
45 set_category (CAT_PLAYLIST)
46 set_subcategory (SUBCAT_PLAYLIST_SD)
47 set_capability ("services_discovery", 0)
48 set_callbacks (OpenV4L, Close)
52 set_shortname (N_("Discs"))
53 set_description (N_("Discs"))
54 set_category (CAT_PLAYLIST)
55 set_subcategory (SUBCAT_PLAYLIST_SD)
56 set_capability ("services_discovery", 0)
57 set_callbacks (OpenDisc, Close)
64 dev_t devnum; /* must be first */
66 services_discovery_t *sd;
72 char * (*get_mrl) (struct udev_device *dev);
73 char * (*get_name) (struct udev_device *dev);
74 char * (*get_cat) (struct udev_device *dev);
78 struct services_discovery_sys_t
80 const struct subsys *subsys;
81 struct udev_monitor *monitor;
87 * Compares two devices (to support binary search).
89 static int cmpdev (const void *a, const void *b)
91 const dev_t *da = a, *db = b;
92 dev_t delta = *da - *db;
94 if (sizeof (delta) > sizeof (int))
95 return delta ? ((delta > 0) ? 1 : -1) : 0;
99 static void DestroyDevice (void *data)
101 struct device *d = data;
104 services_discovery_RemoveItem (d->sd, d->item);
105 vlc_gc_decref (d->item);
109 static char *decode_property (struct udev_device *, const char *);
112 * Adds a udev device.
114 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
116 services_discovery_sys_t *p_sys = sd->p_sys;
118 char *mrl = p_sys->subsys->get_mrl (dev);
120 return 0; /* don't know if it was an error... */
121 char *name = p_sys->subsys->get_name (dev);
122 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
125 p_sys->subsys->item_type);
126 msg_Dbg (sd, "adding %s (%s)", mrl, name);
132 struct device *d = malloc (sizeof (*d));
135 vlc_gc_decref (item);
138 d->devnum = udev_device_get_devnum (dev);
142 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
143 if (dp == NULL) /* Out-of-memory */
148 if (*dp != d) /* Overwrite existing device */
154 name = p_sys->subsys->get_cat (dev);
155 services_discovery_AddItem (sd, item, name ? name : "Generic");
162 * Removes a udev device (if present).
164 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
166 services_discovery_sys_t *p_sys = sd->p_sys;
168 dev_t num = udev_device_get_devnum (dev);
169 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
173 struct device *d = *dp;
174 tdelete (d, &p_sys->root, cmpdev);
178 static void *Run (void *);
181 * Probes and initializes.
183 static int Open (vlc_object_t *obj, const struct subsys *subsys)
185 services_discovery_t *sd = (services_discovery_t *)obj;
186 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
191 p_sys->subsys = subsys;
194 struct udev_monitor *mon = NULL;
195 struct udev *udev = udev_new ();
199 mon = udev_monitor_new_from_netlink (udev, "udev");
201 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
204 p_sys->monitor = mon;
206 /* Enumerate existing devices */
207 struct udev_enumerate *devenum = udev_enumerate_new (udev);
210 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
212 udev_enumerate_unref (devenum);
216 udev_monitor_enable_receiving (mon);
217 /* Note that we enumerate _after_ monitoring is enabled so that we do not
218 * loose device events occuring while we are enumerating. We could still
219 * loose events if the Netlink socket receive buffer overflows. */
220 udev_enumerate_scan_devices (devenum);
221 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
222 struct udev_list_entry *deventry;
223 udev_list_entry_foreach (deventry, devlist)
225 const char *path = udev_list_entry_get_name (deventry);
226 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
228 udev_device_unref (dev);
230 udev_enumerate_unref (devenum);
232 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
233 { /* Fallback without thread */
234 udev_monitor_unref (mon);
236 p_sys->monitor = NULL;
242 udev_monitor_unref (mon);
252 static void Close (vlc_object_t *obj)
254 services_discovery_t *sd = (services_discovery_t *)obj;
255 services_discovery_sys_t *p_sys = sd->p_sys;
257 if (p_sys->monitor != NULL)
259 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
261 vlc_cancel (p_sys->thread);
262 vlc_join (p_sys->thread, NULL);
263 udev_monitor_unref (p_sys->monitor);
267 tdestroy (p_sys->root, DestroyDevice);
271 static void *Run (void *data)
273 services_discovery_t *sd = data;
274 services_discovery_sys_t *p_sys = sd->p_sys;
275 struct udev_monitor *mon = p_sys->monitor;
277 int fd = udev_monitor_get_fd (mon);
278 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
282 while (poll (&ufd, 1, -1) == -1)
286 int canc = vlc_savecancel ();
287 struct udev_device *dev = udev_monitor_receive_device (mon);
291 const char *action = udev_device_get_action (dev);
292 if (!strcmp (action, "add"))
294 else if (!strcmp (action, "remove"))
295 RemoveDevice (sd, dev);
296 else if (!strcmp (action, "change"))
298 RemoveDevice (sd, dev);
301 udev_device_unref (dev);
302 vlc_restorecancel (canc);
308 * Converts an hexadecimal digit to an integer.
310 static int hex (char c)
312 if (c >= '0' && c <= '9')
314 if (c >= 'A' && c <= 'F')
316 if (c >= 'a' && c <= 'f')
322 * Decodes a udev hexadecimal-encoded property.
324 static char *decode (const char *enc)
326 char *ret = enc ? strdup (enc) : NULL;
331 for (const char *in = ret; *in; out++)
335 if ((in[0] == '\\') && (in[1] == 'x')
336 && ((h1 = hex (in[2])) != -1)
337 && ((h2 = hex (in[3])) != -1))
339 *out = (h1 << 4) | h2;
352 static char *decode_property (struct udev_device *dev, const char *name)
354 return decode (udev_device_get_property_value (dev, name));
357 /*** Video4Linux support ***/
358 static bool is_v4l_legacy (struct udev_device *dev)
362 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
363 return version && !strcmp (version, "1");
366 static char *v4l_get_mrl (struct udev_device *dev)
368 /* Determine media location */
369 const char *scheme = "v4l2";
370 if (is_v4l_legacy (dev))
372 const char *node = udev_device_get_devnode (dev);
375 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
380 static char *v4l_get_name (struct udev_device *dev)
382 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
383 return prd ? strdup (prd) : NULL;
386 static char *v4l_get_cat (struct udev_device *dev)
388 return decode_property (dev, "ID_VENDOR_ENC");
391 int OpenV4L (vlc_object_t *obj)
393 static const struct subsys subsys = {
394 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
397 return Open (obj, &subsys);
400 /*** Discs support ***/
401 static char *disc_get_mrl (struct udev_device *dev)
405 val = udev_device_get_property_value (dev, "ID_CDROM");
407 return NULL; /* Ignore non-optical block devices */
409 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
410 if ((val == NULL) || !strcmp (val, "blank"))
411 return NULL; /* ignore empty drives and virgin recordable discs */
413 const char *scheme = "file";
414 val = udev_device_get_property_value (dev,
415 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
416 if (val && atoi (val))
417 scheme = "cdda"; /* Audio CD rather than file system */
418 #if 0 /* we can use file:// for DVDs */
419 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
420 if (val && atoi (val))
423 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
424 if (val && atoi (val))
427 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
428 if (val && atoi (val))
432 val = udev_device_get_devnode (dev);
435 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
440 static char *disc_get_name (struct udev_device *dev)
442 return decode_property (dev, "ID_FS_LABEL_ENC");
445 static char *disc_get_cat (struct udev_device *dev)
448 const char *cat = "Unknown";
450 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
451 if (val && atoi (val))
453 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
454 if (val && atoi (val))
456 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
457 if (val && atoi (val))
458 cat = "Blue-ray disc";
459 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
460 if (val && atoi (val))
466 int OpenDisc (vlc_object_t *obj)
468 static const struct subsys subsys = {
469 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
472 return Open (obj, &subsys);