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);
76 struct services_discovery_sys_t
78 const struct subsys *subsys;
79 struct udev_monitor *monitor;
85 * Compares two devices (to support binary search).
87 static int cmpdev (const void *a, const void *b)
89 const dev_t *da = a, *db = b;
90 dev_t delta = *da - *db;
92 if (sizeof (delta) > sizeof (int))
93 return delta ? ((delta > 0) ? 1 : -1) : 0;
97 static void DestroyDevice (void *data)
99 struct device *d = data;
101 vlc_gc_decref (d->item);
105 static char *decode_property (struct udev_device *, const char *);
108 * Adds a udev device.
110 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
112 services_discovery_sys_t *p_sys = sd->p_sys;
114 char *mrl = p_sys->subsys->get_mrl (dev);
116 return 0; /* don't know if it was an error... */
117 char *name = p_sys->subsys->get_name (dev);
118 input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
120 0, NULL, 0, -1, ITEM_TYPE_CARD);
121 msg_Dbg (sd, "adding %s (%s)", mrl, name);
127 struct device *d = malloc (sizeof (*d));
130 vlc_gc_decref (item);
134 d->devnum = udev_device_get_devnum (dev);
136 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
137 if (dp == NULL) /* Out-of-memory */
142 if (*dp != d) /* Overwrite existing device */
144 services_discovery_RemoveItem (sd, (*dp)->item);
149 name = p_sys->subsys->get_cat (dev);
150 services_discovery_AddItem (sd, item, name ? name : "Generic");
156 * Removes a udev device (if present).
158 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
160 services_discovery_sys_t *p_sys = sd->p_sys;
162 dev_t num = udev_device_get_devnum (dev);
163 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
167 struct device *d = *dp;
168 services_discovery_RemoveItem (sd, d->item);
169 tdelete (d, &p_sys->root, cmpdev);
173 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
177 RemoveDevice (sd, dev);
182 static void *Run (void *);
185 * Probes and initializes.
187 static int Open (vlc_object_t *obj, const struct subsys *subsys)
189 services_discovery_t *sd = (services_discovery_t *)obj;
190 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
195 p_sys->subsys = subsys;
198 struct udev_monitor *mon = NULL;
199 struct udev *udev = udev_new ();
203 mon = udev_monitor_new_from_netlink (udev, "udev");
205 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
208 p_sys->monitor = mon;
210 /* Enumerate existing devices */
211 struct udev_enumerate *devenum = udev_enumerate_new (udev);
214 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
216 udev_enumerate_unref (devenum);
220 udev_monitor_enable_receiving (mon);
221 /* Note that we enumerate _after_ monitoring is enabled so that we do not
222 * loose device events occuring while we are enumerating. We could still
223 * loose events if the Netlink socket receive buffer overflows. */
224 udev_enumerate_scan_devices (devenum);
225 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
226 struct udev_list_entry *deventry;
227 udev_list_entry_foreach (deventry, devlist)
229 const char *path = udev_list_entry_get_name (deventry);
230 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
231 HandleDevice (sd, dev, true);
232 udev_device_unref (dev);
234 udev_enumerate_unref (devenum);
236 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
237 { /* Fallback without thread */
238 udev_monitor_unref (mon);
240 p_sys->monitor = NULL;
246 udev_monitor_unref (mon);
256 static void Close (vlc_object_t *obj)
258 services_discovery_t *sd = (services_discovery_t *)obj;
259 services_discovery_sys_t *p_sys = sd->p_sys;
261 if (p_sys->monitor != NULL)
263 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
265 vlc_cancel (p_sys->thread);
266 vlc_join (p_sys->thread, NULL);
267 udev_monitor_unref (p_sys->monitor);
271 tdestroy (p_sys->root, DestroyDevice);
275 static void *Run (void *data)
277 services_discovery_t *sd = data;
278 services_discovery_sys_t *p_sys = sd->p_sys;
279 struct udev_monitor *mon = p_sys->monitor;
281 int fd = udev_monitor_get_fd (mon);
282 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
286 while (poll (&ufd, 1, -1) == -1)
290 struct udev_device *dev = udev_monitor_receive_device (mon);
294 /* FIXME: handle change, offline, online */
295 const char *action = udev_device_get_action (dev);
296 if (!strcmp (action, "add"))
297 HandleDevice (sd, dev, true);
298 else if (!strcmp (action, "remove"))
299 HandleDevice (sd, dev, false);
301 udev_device_unref (dev);
307 * Converts an hexadecimal digit to an integer.
309 static int hex (char c)
311 if (c >= '0' && c <= '9')
313 if (c >= 'A' && c <= 'F')
315 if (c >= 'a' && c <= 'f')
321 * Decodes a udev hexadecimal-encoded property.
323 static char *decode (const char *enc)
325 char *ret = enc ? strdup (enc) : NULL;
330 for (const char *in = ret; *in; out++)
334 if ((in[0] == '\\') && (in[1] == 'x')
335 && ((h1 = hex (in[2])) != -1)
336 && ((h2 = hex (in[3])) != -1))
338 *out = (h1 << 4) | h2;
351 static char *decode_property (struct udev_device *dev, const char *name)
353 return decode (udev_device_get_property_value (dev, name));
356 /*** Video4Linux support ***/
357 static bool is_v4l_legacy (struct udev_device *dev)
361 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
362 return version && !strcmp (version, "1");
365 static char *v4l_get_mrl (struct udev_device *dev)
367 /* Determine media location */
368 const char *scheme = "v4l2";
369 if (is_v4l_legacy (dev))
371 const char *node = udev_device_get_devnode (dev);
374 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
379 static char *v4l_get_name (struct udev_device *dev)
381 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
382 return prd ? strdup (prd) : NULL;
385 static char *v4l_get_cat (struct udev_device *dev)
387 return decode_property (dev, "ID_VENDOR_ENC");
390 int OpenV4L (vlc_object_t *obj)
392 static const struct subsys subsys = {
393 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat,
396 return Open (obj, &subsys);
399 /*** Discs support ***/
400 static char *disc_get_mrl (struct udev_device *dev)
404 val = udev_device_get_property_value (dev, "ID_CDROM");
406 return NULL; /* Ignore non-optical block devices */
408 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
409 if ((val == NULL) || !strcmp (val, "blank"))
410 return NULL; /* ignore empty drives and virgin recordable discs */
412 const char *scheme = "file";
413 val = udev_device_get_property_value (dev,
414 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
415 if (val && atoi (val))
416 scheme = "cdda"; /* Audio CD rather than file system */
417 #if 0 /* we can use file:// for DVDs */
418 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
419 if (val && atoi (val))
422 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
423 if (val && atoi (val))
426 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
427 if (val && atoi (val))
431 val = udev_device_get_devnode (dev);
434 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
439 static char *disc_get_name (struct udev_device *dev)
441 return decode_property (dev, "ID_FS_LABEL_ENC");
444 static char *disc_get_cat (struct udev_device *dev)
447 const char *cat = "Unknown";
449 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
450 if (val && atoi (val))
452 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
453 if (val && atoi (val))
455 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
456 if (val && atoi (val))
457 cat = "Blue-ray disc";
458 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
459 if (val && atoi (val))
465 int OpenDisc (vlc_object_t *obj)
467 static const struct subsys subsys = {
468 "block", disc_get_mrl, disc_get_name, disc_get_cat,
471 return Open (obj, &subsys);