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 *Run (void *);
178 * Probes and initializes.
180 static int Open (vlc_object_t *obj, const struct subsys *subsys)
182 services_discovery_t *sd = (services_discovery_t *)obj;
183 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
188 p_sys->subsys = subsys;
191 struct udev_monitor *mon = NULL;
192 struct udev *udev = udev_new ();
196 mon = udev_monitor_new_from_netlink (udev, "udev");
198 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
201 p_sys->monitor = mon;
203 /* Enumerate existing devices */
204 struct udev_enumerate *devenum = udev_enumerate_new (udev);
207 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
209 udev_enumerate_unref (devenum);
213 udev_monitor_enable_receiving (mon);
214 /* Note that we enumerate _after_ monitoring is enabled so that we do not
215 * loose device events occuring while we are enumerating. We could still
216 * loose events if the Netlink socket receive buffer overflows. */
217 udev_enumerate_scan_devices (devenum);
218 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
219 struct udev_list_entry *deventry;
220 udev_list_entry_foreach (deventry, devlist)
222 const char *path = udev_list_entry_get_name (deventry);
223 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
225 udev_device_unref (dev);
227 udev_enumerate_unref (devenum);
229 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
230 { /* Fallback without thread */
231 udev_monitor_unref (mon);
233 p_sys->monitor = NULL;
239 udev_monitor_unref (mon);
249 static void Close (vlc_object_t *obj)
251 services_discovery_t *sd = (services_discovery_t *)obj;
252 services_discovery_sys_t *p_sys = sd->p_sys;
254 if (p_sys->monitor != NULL)
256 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
258 vlc_cancel (p_sys->thread);
259 vlc_join (p_sys->thread, NULL);
260 udev_monitor_unref (p_sys->monitor);
264 tdestroy (p_sys->root, DestroyDevice);
268 static void *Run (void *data)
270 services_discovery_t *sd = data;
271 services_discovery_sys_t *p_sys = sd->p_sys;
272 struct udev_monitor *mon = p_sys->monitor;
274 int fd = udev_monitor_get_fd (mon);
275 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
279 while (poll (&ufd, 1, -1) == -1)
283 int canc = vlc_savecancel ();
284 struct udev_device *dev = udev_monitor_receive_device (mon);
288 const char *action = udev_device_get_action (dev);
289 if (!strcmp (action, "add"))
291 else if (!strcmp (action, "remove"))
292 RemoveDevice (sd, dev);
293 else if (!strcmp (action, "change"))
295 RemoveDevice (sd, dev);
298 udev_device_unref (dev);
299 vlc_restorecancel (canc);
305 * Converts an hexadecimal digit to an integer.
307 static int hex (char c)
309 if (c >= '0' && c <= '9')
311 if (c >= 'A' && c <= 'F')
313 if (c >= 'a' && c <= 'f')
319 * Decodes a udev hexadecimal-encoded property.
321 static char *decode (const char *enc)
323 char *ret = enc ? strdup (enc) : NULL;
328 for (const char *in = ret; *in; out++)
332 if ((in[0] == '\\') && (in[1] == 'x')
333 && ((h1 = hex (in[2])) != -1)
334 && ((h2 = hex (in[3])) != -1))
336 *out = (h1 << 4) | h2;
349 static char *decode_property (struct udev_device *dev, const char *name)
351 return decode (udev_device_get_property_value (dev, name));
354 /*** Video4Linux support ***/
355 static bool is_v4l_legacy (struct udev_device *dev)
359 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
360 return version && !strcmp (version, "1");
363 static char *v4l_get_mrl (struct udev_device *dev)
365 /* Determine media location */
366 const char *scheme = "v4l2";
367 if (is_v4l_legacy (dev))
369 const char *node = udev_device_get_devnode (dev);
372 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
377 static char *v4l_get_name (struct udev_device *dev)
379 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
380 return prd ? strdup (prd) : NULL;
383 static char *v4l_get_cat (struct udev_device *dev)
385 return decode_property (dev, "ID_VENDOR_ENC");
388 int OpenV4L (vlc_object_t *obj)
390 static const struct subsys subsys = {
391 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
394 return Open (obj, &subsys);
397 /*** Discs support ***/
398 static char *disc_get_mrl (struct udev_device *dev)
402 val = udev_device_get_property_value (dev, "ID_CDROM");
404 return NULL; /* Ignore non-optical block devices */
406 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
407 if ((val == NULL) || !strcmp (val, "blank"))
408 return NULL; /* ignore empty drives and virgin recordable discs */
410 const char *scheme = "file";
411 val = udev_device_get_property_value (dev,
412 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
413 if (val && atoi (val))
414 scheme = "cdda"; /* Audio CD rather than file system */
415 #if 0 /* we can use file:// for DVDs */
416 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
417 if (val && atoi (val))
420 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
421 if (val && atoi (val))
424 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
425 if (val && atoi (val))
429 val = udev_device_get_devnode (dev);
432 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
437 static char *disc_get_name (struct udev_device *dev)
439 return decode_property (dev, "ID_FS_LABEL_ENC");
442 static char *disc_get_cat (struct udev_device *dev)
445 const char *cat = "Unknown";
447 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
448 if (val && atoi (val))
450 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
451 if (val && atoi (val))
453 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
454 if (val && atoi (val))
455 cat = "Blue-ray disc";
456 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
457 if (val && atoi (val))
463 int OpenDisc (vlc_object_t *obj)
465 static const struct subsys subsys = {
466 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
469 return Open (obj, &subsys);