demux: ts: only seek on pcr for current program
[vlc.git] / modules / services_discovery / udev.c
blob8ac528b89d359242fceca7881d094c7a251141c9
1 /**
2 * @file udev.c
3 * @brief List of multimedia devices for VLC media player
4 */
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <errno.h>
28 #include <search.h>
29 #include <poll.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
33 #include <libudev.h>
35 #include <vlc_common.h>
36 #include <vlc_services_discovery.h>
37 #include <vlc_plugin.h>
38 #ifdef HAVE_ALSA
39 # include <vlc_modules.h>
40 #endif
41 #include <vlc_fs.h>
42 #include <vlc_url.h>
44 static int OpenV4L (vlc_object_t *);
45 #ifdef HAVE_ALSA
46 static int OpenALSA (vlc_object_t *);
47 #endif
48 static int OpenDisc (vlc_object_t *);
49 static void Close (vlc_object_t *);
50 static int vlc_sd_probe_Open (vlc_object_t *);
53 * Module descriptor
55 vlc_module_begin ()
56 set_shortname (N_("Video capture"))
57 set_description (N_("Video capture (Video4Linux)"))
58 set_category (CAT_PLAYLIST)
59 set_subcategory (SUBCAT_PLAYLIST_SD)
60 set_capability ("services_discovery", 0)
61 set_callbacks (OpenV4L, Close)
62 add_shortcut ("v4l", "video")
63 #ifdef HAVE_ALSA
64 add_submodule ()
65 set_shortname (N_("Audio capture"))
66 set_description (N_("Audio capture (ALSA)"))
67 set_category (CAT_PLAYLIST)
68 set_subcategory (SUBCAT_PLAYLIST_SD)
69 set_capability ("services_discovery", 0)
70 set_callbacks (OpenALSA, Close)
71 add_shortcut ("alsa", "audio")
72 #endif
73 add_submodule ()
74 set_shortname (N_("Discs"))
75 set_description (N_("Discs"))
76 set_category (CAT_PLAYLIST)
77 set_subcategory (SUBCAT_PLAYLIST_SD)
78 set_capability ("services_discovery", 0)
79 set_callbacks (OpenDisc, Close)
80 add_shortcut ("disc")
82 VLC_SD_PROBE_SUBMODULE
84 vlc_module_end ()
86 static int vlc_sd_probe_Open (vlc_object_t *obj)
88 vlc_probe_t *probe = (vlc_probe_t *)obj;
90 struct udev *udev = udev_new ();
91 if (udev == NULL)
92 return VLC_PROBE_CONTINUE;
94 struct udev_monitor *mon = udev_monitor_new_from_netlink (udev, "udev");
95 if (mon != NULL)
97 vlc_sd_probe_Add (probe, "v4l", N_("Video capture"), SD_CAT_DEVICES);
98 #ifdef HAVE_ALSA
99 if (!module_exists ("pulselist"))
100 vlc_sd_probe_Add (probe, "alsa", N_("Audio capture"),
101 SD_CAT_DEVICES);
102 #endif
103 vlc_sd_probe_Add (probe, "disc", N_("Discs"), SD_CAT_DEVICES);
104 udev_monitor_unref (mon);
106 udev_unref (udev);
107 return VLC_PROBE_CONTINUE;
110 struct device
112 dev_t devnum; /* must be first */
113 input_item_t *item;
114 services_discovery_t *sd;
117 struct subsys
119 const char *name;
120 const char *description;
121 char * (*get_mrl) (struct udev_device *dev);
122 char * (*get_name) (struct udev_device *dev);
123 int item_type;
126 struct services_discovery_sys_t
128 const struct subsys *subsys;
129 struct udev_monitor *monitor;
130 vlc_thread_t thread;
131 void *root;
135 * Compares two devices (to support binary search).
137 static int cmpdev (const void *a, const void *b)
139 const dev_t *da = a, *db = b;
141 if (*da > *db)
142 return +1;
143 if (*da < *db)
144 return -1;
145 return 0;
148 static void DestroyDevice (void *data)
150 struct device *d = data;
152 if (d->sd)
153 services_discovery_RemoveItem (d->sd, d->item);
154 input_item_Release (d->item);
155 free (d);
158 static char *decode_property (struct udev_device *, const char *);
161 * Adds a udev device.
163 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
165 services_discovery_sys_t *p_sys = sd->p_sys;
167 char *mrl = p_sys->subsys->get_mrl (dev);
168 if (mrl == NULL)
169 return 0; /* don't know if it was an error... */
170 char *name = p_sys->subsys->get_name (dev);
171 input_item_t *item = input_item_NewExt (mrl, name ? name : mrl, -1,
172 p_sys->subsys->item_type, ITEM_LOCAL);
173 msg_Dbg (sd, "adding %s (%s)", mrl, name);
174 free (name);
175 free (mrl);
176 if (item == NULL)
177 return -1;
179 struct device *d = malloc (sizeof (*d));
180 if (d == NULL)
182 input_item_Release (item);
183 return -1;
185 d->devnum = udev_device_get_devnum (dev);
186 d->item = item;
187 d->sd = NULL;
189 struct device **dp = tsearch (d, &p_sys->root, cmpdev);
190 if (dp == NULL) /* Out-of-memory */
192 DestroyDevice (d);
193 return -1;
195 if (*dp != d) /* Overwrite existing device */
197 DestroyDevice (*dp);
198 *dp = d;
201 services_discovery_AddItem(sd, item);
202 d->sd = sd;
203 return 0;
207 * Removes a udev device (if present).
209 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
211 services_discovery_sys_t *p_sys = sd->p_sys;
213 dev_t num = udev_device_get_devnum (dev);
214 struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
215 if (dp == NULL)
216 return;
218 struct device *d = *dp;
219 tdelete (d, &p_sys->root, cmpdev);
220 DestroyDevice (d);
223 static void *Run (void *);
226 * Probes and initializes.
228 static int Open (vlc_object_t *obj, const struct subsys *subsys)
230 services_discovery_t *sd = (services_discovery_t *)obj;
231 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
233 if (p_sys == NULL)
234 return VLC_ENOMEM;
236 sd->description = vlc_gettext(subsys->description);
237 sd->p_sys = p_sys;
238 p_sys->subsys = subsys;
239 p_sys->root = NULL;
241 struct udev_monitor *mon = NULL;
242 struct udev *udev = udev_new ();
243 if (udev == NULL)
244 goto error;
246 mon = udev_monitor_new_from_netlink (udev, "udev");
247 if (mon == NULL
248 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
249 NULL))
250 goto error;
251 p_sys->monitor = mon;
253 /* Enumerate existing devices */
254 struct udev_enumerate *devenum = udev_enumerate_new (udev);
255 if (devenum == NULL)
256 goto error;
257 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
259 udev_enumerate_unref (devenum);
260 goto error;
263 udev_monitor_enable_receiving (mon);
264 /* Note that we enumerate _after_ monitoring is enabled so that we do not
265 * loose device events occuring while we are enumerating. We could still
266 * loose events if the Netlink socket receive buffer overflows. */
267 udev_enumerate_scan_devices (devenum);
268 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
269 struct udev_list_entry *deventry;
270 udev_list_entry_foreach (deventry, devlist)
272 const char *path = udev_list_entry_get_name (deventry);
273 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
274 AddDevice (sd, dev);
275 udev_device_unref (dev);
277 udev_enumerate_unref (devenum);
279 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
280 { /* Fallback without thread */
281 udev_monitor_unref (mon);
282 udev_unref (udev);
283 p_sys->monitor = NULL;
285 return VLC_SUCCESS;
287 error:
288 if (mon != NULL)
289 udev_monitor_unref (mon);
290 if (udev != NULL)
291 udev_unref (udev);
292 free (p_sys);
293 return VLC_EGENERIC;
297 * Releases resources
299 static void Close (vlc_object_t *obj)
301 services_discovery_t *sd = (services_discovery_t *)obj;
302 services_discovery_sys_t *p_sys = sd->p_sys;
304 if (p_sys->monitor != NULL)
306 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
308 vlc_cancel (p_sys->thread);
309 vlc_join (p_sys->thread, NULL);
310 udev_monitor_unref (p_sys->monitor);
311 udev_unref (udev);
314 tdestroy (p_sys->root, DestroyDevice);
315 free (p_sys);
318 static void *Run (void *data)
320 services_discovery_t *sd = data;
321 services_discovery_sys_t *p_sys = sd->p_sys;
322 struct udev_monitor *mon = p_sys->monitor;
324 int fd = udev_monitor_get_fd (mon);
325 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
327 for (;;)
329 while (poll (&ufd, 1, -1) == -1)
330 if (errno != EINTR)
331 break;
333 int canc = vlc_savecancel ();
334 struct udev_device *dev = udev_monitor_receive_device (mon);
335 if (dev == NULL)
336 continue;
338 const char *action = udev_device_get_action (dev);
339 if (!strcmp (action, "add"))
340 AddDevice (sd, dev);
341 else if (!strcmp (action, "remove"))
342 RemoveDevice (sd, dev);
343 else if (!strcmp (action, "change"))
345 RemoveDevice (sd, dev);
346 AddDevice (sd, dev);
348 udev_device_unref (dev);
349 vlc_restorecancel (canc);
351 return NULL;
355 * Converts an hexadecimal digit to an integer.
357 static int hex (char c)
359 if (c >= '0' && c <= '9')
360 return c - '0';
361 if (c >= 'A' && c <= 'F')
362 return c + 10 - 'A';
363 if (c >= 'a' && c <= 'f')
364 return c + 10 - 'a';
365 return -1;
369 * Decodes a udev hexadecimal-encoded property.
371 static char *decode (const char *enc)
373 char *ret = enc ? strdup (enc) : NULL;
374 if (ret == NULL)
375 return NULL;
377 char *out = ret;
378 for (const char *in = ret; *in; out++)
380 int h1, h2;
382 if ((in[0] == '\\') && (in[1] == 'x')
383 && ((h1 = hex (in[2])) != -1)
384 && ((h2 = hex (in[3])) != -1))
386 *out = (h1 << 4) | h2;
387 in += 4;
389 else
391 *out = *in;
392 in++;
395 *out = 0;
396 return ret;
399 static char *decode_property (struct udev_device *dev, const char *name)
401 return decode (udev_device_get_property_value (dev, name));
405 /*** Video4Linux support ***/
406 static bool v4l_is_legacy (struct udev_device *dev)
408 const char *version;
410 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
411 return (version != NULL) && !strcmp (version, "1");
414 static bool v4l_can_capture (struct udev_device *dev)
416 const char *caps;
418 caps = udev_device_get_property_value (dev, "ID_V4L_CAPABILITIES");
419 return (caps != NULL) && (strstr (caps, ":capture:") != NULL);
422 static char *v4l_get_mrl (struct udev_device *dev)
424 /* Determine media location */
425 if (v4l_is_legacy (dev) || !v4l_can_capture (dev))
426 return NULL;
428 const char *node = udev_device_get_devnode (dev);
429 char *mrl;
431 if (asprintf (&mrl, "v4l2://%s", node) == -1)
432 mrl = NULL;
433 return mrl;
436 static char *v4l_get_name (struct udev_device *dev)
438 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
439 return prd ? strdup (prd) : NULL;
442 int OpenV4L (vlc_object_t *obj)
444 static const struct subsys subsys = {
445 "video4linux", N_("Video capture"),
446 v4l_get_mrl, v4l_get_name, ITEM_TYPE_CARD,
449 return Open (obj, &subsys);
453 #ifdef HAVE_ALSA
454 /*** Advanced Linux Sound Architecture support ***/
455 #include <alsa/asoundlib.h>
457 static int alsa_get_device (struct udev_device *dev, unsigned *restrict pcard,
458 unsigned *restrict pdevice)
460 const char *node = udev_device_get_devpath (dev);
461 char type;
463 node = strrchr (node, '/');
464 if (node == NULL)
465 return -1;
466 if (sscanf (node, "/pcmC%uD%u%c", pcard, pdevice, &type) < 3)
467 return -1;
468 if (type != 'c')
469 return -1;
470 return 0;
474 static char *alsa_get_mrl (struct udev_device *dev)
476 /* Determine media location */
477 char *mrl;
478 unsigned card, device;
480 if (alsa_get_device (dev, &card, &device))
481 return NULL;
483 if (asprintf (&mrl, "alsa://plughw:%u,%u", card, device) == -1)
484 mrl = NULL;
485 return mrl;
488 static char *alsa_get_name (struct udev_device *dev)
490 char *name = NULL;
491 unsigned card, device;
493 if (alsa_get_device (dev, &card, &device))
494 return NULL;
496 char card_name[4 + 3 * sizeof (int)];
497 snprintf (card_name, sizeof (card_name), "hw:%u", card);
499 snd_ctl_t *ctl;
500 if (snd_ctl_open (&ctl, card_name, 0))
501 return NULL;
503 snd_pcm_info_t *pcm_info;
504 snd_pcm_info_alloca (&pcm_info);
505 snd_pcm_info_set_device (pcm_info, device);
506 snd_pcm_info_set_subdevice (pcm_info, 0);
507 snd_pcm_info_set_stream (pcm_info, SND_PCM_STREAM_CAPTURE);
508 if (snd_ctl_pcm_info (ctl, pcm_info))
509 goto out;
511 name = strdup (snd_pcm_info_get_name (pcm_info));
512 out:
513 snd_ctl_close (ctl);
514 return name;
517 int OpenALSA (vlc_object_t *obj)
519 static const struct subsys subsys = {
520 "sound", N_("Audio capture"),
521 alsa_get_mrl, alsa_get_name, ITEM_TYPE_CARD,
524 return Open (obj, &subsys);
526 #endif /* HAVE_ALSA */
529 /*** Discs support ***/
530 static char *disc_get_mrl (struct udev_device *dev)
532 const char *node = udev_device_get_devnode (dev);
533 const char *val;
535 val = udev_device_get_property_value (dev, "ID_CDROM");
536 if (val == NULL)
537 return NULL; /* Ignore non-optical block devices */
539 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
540 if (val == NULL)
541 { /* Force probing of the disc in the drive if any. */
542 int fd = vlc_open (node, O_RDONLY);
543 if (fd != -1)
544 vlc_close (fd);
545 return NULL;
547 if (!strcmp (val, "blank"))
548 return NULL; /* ignore empty drives and virgin recordable discs */
550 const char *scheme = NULL;
551 val = udev_device_get_property_value (dev,
552 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
553 if (val && atoi (val))
554 scheme = "cdda"; /* Audio CD rather than file system */
555 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
556 if (val && atoi (val))
557 scheme = "dvd";
559 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
560 if (val && atoi (val))
561 scheme = "bluray";
562 #ifdef LOL
563 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
564 if (val && atoi (val))
565 scheme = "hddvd";
566 #endif
568 /* We didn't get any property that could tell we have optical disc
569 that we can play */
570 if (scheme == NULL)
571 return NULL;
573 return vlc_path2uri (node, scheme);
576 static char *disc_get_name (struct udev_device *dev)
578 char *name = NULL;
579 struct udev_list_entry *list, *entry;
581 list = udev_device_get_properties_list_entry (dev);
582 if (unlikely(list == NULL))
583 return NULL;
585 const char *cat = NULL;
586 udev_list_entry_foreach (entry, list)
588 const char *propname = udev_list_entry_get_name(entry);
590 if (strncmp(propname, "ID_CDROM_MEDIA_", 15))
591 continue;
592 if (!atoi (udev_list_entry_get_value (entry)))
593 continue;
594 propname += 15;
596 if (!strncmp(propname, "CD", 2))
597 cat = N_("CD");
598 else if (!strncmp(propname, "DVD", 3))
599 cat = N_("DVD");
600 else if (!strncmp(propname, "BD", 2))
601 cat = N_("Blu-ray");
602 else if (!strncmp(propname, "HDDVD", 5))
603 cat = N_("HD DVD");
605 if (cat != NULL)
606 break;
609 if (cat == NULL)
610 cat = N_("Unknown type");
612 char *label = decode_property (dev, "ID_FS_LABEL_ENC");
614 if (label)
615 if (asprintf(&name, "%s (%s)", label, vlc_gettext(cat)) < 0)
616 name = NULL;
617 free(label);
619 return name;
622 int OpenDisc (vlc_object_t *obj)
624 static const struct subsys subsys = {
625 "block", N_("Discs"), disc_get_mrl, disc_get_name, ITEM_TYPE_DISC,
628 return Open (obj, &subsys);