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 */
71 char * (*get_mrl) (struct udev_device *dev);
72 char * (*get_name) (struct udev_device *dev);
73 char * (*get_cat) (struct udev_device *dev);
77 struct services_discovery_sys_t
79 const struct subsys *subsys;
80 struct udev_monitor *monitor;
86 * Compares two devices (to support binary search).
88 static int cmpdev (const void *a, const void *b)
90 const dev_t *da = a, *db = b;
91 dev_t delta = *da - *db;
93 if (sizeof (delta) > sizeof (int))
94 return delta ? ((delta > 0) ? 1 : -1) : 0;
98 static void DestroyDevice (void *data)
100 struct device *d = data;
102 vlc_gc_decref (d->item);
106 static char *decode_property (struct udev_device *, const char *);
109 * Adds a udev device.
111 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
113 services_discovery_sys_t *p_sys = sd->p_sys;
115 char *mrl = p_sys->subsys->get_mrl (dev);
117 return 0; /* don't know if it was an error... */
118 char *name = p_sys->subsys->get_name (dev);
119 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
122 p_sys->subsys->item_type);
123 msg_Dbg (sd, "adding %s (%s)", mrl, name);
129 struct device *d = malloc (sizeof (*d));
132 vlc_gc_decref (item);
136 d->devnum = udev_device_get_devnum (dev);
138 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
139 if (dp == NULL) /* Out-of-memory */
144 if (*dp != d) /* Overwrite existing device */
146 services_discovery_RemoveItem (sd, (*dp)->item);
151 name = p_sys->subsys->get_cat (dev);
152 services_discovery_AddItem (sd, item, name ? name : "Generic");
158 * Removes a udev device (if present).
160 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
162 services_discovery_sys_t *p_sys = sd->p_sys;
164 dev_t num = udev_device_get_devnum (dev);
165 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
169 struct device *d = *dp;
170 services_discovery_RemoveItem (sd, d->item);
171 tdelete (d, &p_sys->root, cmpdev);
175 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
179 RemoveDevice (sd, dev);
184 static void *Run (void *);
187 * Probes and initializes.
189 static int Open (vlc_object_t *obj, const struct subsys *subsys)
191 services_discovery_t *sd = (services_discovery_t *)obj;
192 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
197 p_sys->subsys = subsys;
200 struct udev_monitor *mon = NULL;
201 struct udev *udev = udev_new ();
205 mon = udev_monitor_new_from_netlink (udev, "udev");
207 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
210 p_sys->monitor = mon;
212 /* Enumerate existing devices */
213 struct udev_enumerate *devenum = udev_enumerate_new (udev);
216 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
218 udev_enumerate_unref (devenum);
222 udev_monitor_enable_receiving (mon);
223 /* Note that we enumerate _after_ monitoring is enabled so that we do not
224 * loose device events occuring while we are enumerating. We could still
225 * loose events if the Netlink socket receive buffer overflows. */
226 udev_enumerate_scan_devices (devenum);
227 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
228 struct udev_list_entry *deventry;
229 udev_list_entry_foreach (deventry, devlist)
231 const char *path = udev_list_entry_get_name (deventry);
232 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
233 HandleDevice (sd, dev, true);
234 udev_device_unref (dev);
236 udev_enumerate_unref (devenum);
238 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
239 { /* Fallback without thread */
240 udev_monitor_unref (mon);
242 p_sys->monitor = NULL;
248 udev_monitor_unref (mon);
258 static void Close (vlc_object_t *obj)
260 services_discovery_t *sd = (services_discovery_t *)obj;
261 services_discovery_sys_t *p_sys = sd->p_sys;
263 if (p_sys->monitor != NULL)
265 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
267 vlc_cancel (p_sys->thread);
268 vlc_join (p_sys->thread, NULL);
269 udev_monitor_unref (p_sys->monitor);
273 tdestroy (p_sys->root, DestroyDevice);
277 static void *Run (void *data)
279 services_discovery_t *sd = data;
280 services_discovery_sys_t *p_sys = sd->p_sys;
281 struct udev_monitor *mon = p_sys->monitor;
283 int fd = udev_monitor_get_fd (mon);
284 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
288 while (poll (&ufd, 1, -1) == -1)
292 struct udev_device *dev = udev_monitor_receive_device (mon);
296 /* FIXME: handle change, offline, online */
297 const char *action = udev_device_get_action (dev);
298 if (!strcmp (action, "add"))
299 HandleDevice (sd, dev, true);
300 else if (!strcmp (action, "remove"))
301 HandleDevice (sd, dev, false);
303 udev_device_unref (dev);
309 * Converts an hexadecimal digit to an integer.
311 static int hex (char c)
313 if (c >= '0' && c <= '9')
315 if (c >= 'A' && c <= 'F')
317 if (c >= 'a' && c <= 'f')
323 * Decodes a udev hexadecimal-encoded property.
325 static char *decode (const char *enc)
327 char *ret = enc ? strdup (enc) : NULL;
332 for (const char *in = ret; *in; out++)
336 if ((in[0] == '\\') && (in[1] == 'x')
337 && ((h1 = hex (in[2])) != -1)
338 && ((h2 = hex (in[3])) != -1))
340 *out = (h1 << 4) | h2;
353 static char *decode_property (struct udev_device *dev, const char *name)
355 return decode (udev_device_get_property_value (dev, name));
358 /*** Video4Linux support ***/
359 static bool is_v4l_legacy (struct udev_device *dev)
363 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
364 return version && !strcmp (version, "1");
367 static char *v4l_get_mrl (struct udev_device *dev)
369 /* Determine media location */
370 const char *scheme = "v4l2";
371 if (is_v4l_legacy (dev))
373 const char *node = udev_device_get_devnode (dev);
376 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
381 static char *v4l_get_name (struct udev_device *dev)
383 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
384 return prd ? strdup (prd) : NULL;
387 static char *v4l_get_cat (struct udev_device *dev)
389 return decode_property (dev, "ID_VENDOR_ENC");
392 int OpenV4L (vlc_object_t *obj)
394 static const struct subsys subsys = {
395 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
398 return Open (obj, &subsys);
401 /*** Discs support ***/
402 static char *disc_get_mrl (struct udev_device *dev)
406 val = udev_device_get_property_value (dev, "ID_CDROM");
408 return NULL; /* Ignore non-optical block devices */
410 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
411 if ((val == NULL) || !strcmp (val, "blank"))
412 return NULL; /* ignore empty drives and virgin recordable discs */
414 const char *scheme = "file";
415 val = udev_device_get_property_value (dev,
416 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
417 if (val && atoi (val))
418 scheme = "cdda"; /* Audio CD rather than file system */
419 #if 0 /* we can use file:// for DVDs */
420 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
421 if (val && atoi (val))
424 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
425 if (val && atoi (val))
428 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
429 if (val && atoi (val))
433 val = udev_device_get_devnode (dev);
436 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
441 static char *disc_get_name (struct udev_device *dev)
443 return decode_property (dev, "ID_FS_LABEL_ENC");
446 static char *disc_get_cat (struct udev_device *dev)
449 const char *cat = "Unknown";
451 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
452 if (val && atoi (val))
454 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
455 if (val && atoi (val))
457 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
458 if (val && atoi (val))
459 cat = "Blue-ray disc";
460 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
461 if (val && atoi (val))
467 int OpenDisc (vlc_object_t *obj)
469 static const struct subsys subsys = {
470 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
473 return Open (obj, &subsys);