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 struct udev_device *dev = udev_monitor_receive_device (mon);
287 const char *action = udev_device_get_action (dev);
288 if (!strcmp (action, "add"))
290 else if (!strcmp (action, "remove"))
291 RemoveDevice (sd, dev);
292 else if (!strcmp (action, "change"))
294 RemoveDevice (sd, dev);
297 udev_device_unref (dev);
303 * Converts an hexadecimal digit to an integer.
305 static int hex (char c)
307 if (c >= '0' && c <= '9')
309 if (c >= 'A' && c <= 'F')
311 if (c >= 'a' && c <= 'f')
317 * Decodes a udev hexadecimal-encoded property.
319 static char *decode (const char *enc)
321 char *ret = enc ? strdup (enc) : NULL;
326 for (const char *in = ret; *in; out++)
330 if ((in[0] == '\\') && (in[1] == 'x')
331 && ((h1 = hex (in[2])) != -1)
332 && ((h2 = hex (in[3])) != -1))
334 *out = (h1 << 4) | h2;
347 static char *decode_property (struct udev_device *dev, const char *name)
349 return decode (udev_device_get_property_value (dev, name));
352 /*** Video4Linux support ***/
353 static bool is_v4l_legacy (struct udev_device *dev)
357 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
358 return version && !strcmp (version, "1");
361 static char *v4l_get_mrl (struct udev_device *dev)
363 /* Determine media location */
364 const char *scheme = "v4l2";
365 if (is_v4l_legacy (dev))
367 const char *node = udev_device_get_devnode (dev);
370 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
375 static char *v4l_get_name (struct udev_device *dev)
377 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
378 return prd ? strdup (prd) : NULL;
381 static char *v4l_get_cat (struct udev_device *dev)
383 return decode_property (dev, "ID_VENDOR_ENC");
386 int OpenV4L (vlc_object_t *obj)
388 static const struct subsys subsys = {
389 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
392 return Open (obj, &subsys);
395 /*** Discs support ***/
396 static char *disc_get_mrl (struct udev_device *dev)
400 val = udev_device_get_property_value (dev, "ID_CDROM");
402 return NULL; /* Ignore non-optical block devices */
404 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
405 if ((val == NULL) || !strcmp (val, "blank"))
406 return NULL; /* ignore empty drives and virgin recordable discs */
408 const char *scheme = "file";
409 val = udev_device_get_property_value (dev,
410 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
411 if (val && atoi (val))
412 scheme = "cdda"; /* Audio CD rather than file system */
413 #if 0 /* we can use file:// for DVDs */
414 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
415 if (val && atoi (val))
418 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
419 if (val && atoi (val))
422 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
423 if (val && atoi (val))
427 val = udev_device_get_devnode (dev);
430 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
435 static char *disc_get_name (struct udev_device *dev)
437 return decode_property (dev, "ID_FS_LABEL_ENC");
440 static char *disc_get_cat (struct udev_device *dev)
443 const char *cat = "Unknown";
445 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
446 if (val && atoi (val))
448 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
449 if (val && atoi (val))
451 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
452 if (val && atoi (val))
453 cat = "Blue-ray disc";
454 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
455 if (val && atoi (val))
461 int OpenDisc (vlc_object_t *obj)
463 static const struct subsys subsys = {
464 "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
467 return Open (obj, &subsys);