]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
stream_filter/httplive.c: Append new segment any (HLS Live playback) when gap in...
[vlc] / modules / services_discovery / udev.c
1 /**
2  * @file udev.c
3  * @brief List of multimedia devices for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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.
12  *
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.
17  *
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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <libudev.h>
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30 #include <vlc_plugin.h>
31 #include <vlc_url.h>
32 #ifdef HAVE_SEARCH_H
33 # include <search.h>
34 #endif
35 #include <poll.h>
36 #include <errno.h>
37
38 static int OpenV4L (vlc_object_t *);
39 static int OpenALSA (vlc_object_t *);
40 static int OpenDisc (vlc_object_t *);
41 static void Close (vlc_object_t *);
42 static int vlc_sd_probe_Open (vlc_object_t *);
43
44 /*
45  * Module descriptor
46  */
47 vlc_module_begin ()
48     set_shortname (N_("Video capture"))
49     set_description (N_("Video capture (Video4Linux)"))
50     set_category (CAT_PLAYLIST)
51     set_subcategory (SUBCAT_PLAYLIST_SD)
52     set_capability ("services_discovery", 0)
53     set_callbacks (OpenV4L, Close)
54     add_shortcut ("v4l")
55
56     add_submodule ()
57     set_shortname (N_("Audio capture"))
58     set_description (N_("Audio capture (ALSA)"))
59     set_category (CAT_PLAYLIST)
60     set_subcategory (SUBCAT_PLAYLIST_SD)
61     set_capability ("services_discovery", 0)
62     set_callbacks (OpenALSA, Close)
63     add_shortcut ("alsa")
64
65     add_submodule ()
66     set_shortname (N_("Discs"))
67     set_description (N_("Discs"))
68     set_category (CAT_PLAYLIST)
69     set_subcategory (SUBCAT_PLAYLIST_SD)
70     set_capability ("services_discovery", 0)
71     set_callbacks (OpenDisc, Close)
72     add_shortcut ("disc")
73
74     VLC_SD_PROBE_SUBMODULE
75
76 vlc_module_end ()
77
78 static int vlc_sd_probe_Open (vlc_object_t *obj)
79 {
80     vlc_probe_t *probe = (vlc_probe_t *)obj;
81
82     struct udev *udev = udev_new ();
83     if (udev == NULL)
84         return VLC_PROBE_CONTINUE;
85
86     struct udev_monitor *mon = udev_monitor_new_from_netlink (udev, "udev");
87     if (mon != NULL)
88     {
89         vlc_sd_probe_Add (probe, "v4l{longname=\"Video capture\"}",
90                           N_("Video capture"), SD_CAT_DEVICES);
91         vlc_sd_probe_Add (probe, "alsa{longname=\"Audio capture\"}",
92                           N_("Audio capture"), SD_CAT_DEVICES);
93         vlc_sd_probe_Add (probe, "disc{longname=\"Discs\"}", N_("Discs"),
94                           SD_CAT_DEVICES);
95         udev_monitor_unref (mon);
96     }
97     udev_unref (udev);
98     return VLC_PROBE_CONTINUE;
99 }
100
101 struct device
102 {
103     dev_t devnum; /* must be first */
104     input_item_t *item;
105     services_discovery_t *sd;
106 };
107
108 struct subsys
109 {
110     const char *name;
111     char * (*get_mrl) (struct udev_device *dev);
112     char * (*get_name) (struct udev_device *dev);
113     char * (*get_cat) (struct udev_device *dev);
114     int item_type;
115 };
116
117 struct services_discovery_sys_t
118 {
119     const struct subsys *subsys;
120     struct udev_monitor *monitor;
121     vlc_thread_t         thread;
122     void                *root;
123 };
124
125 /**
126  * Compares two devices (to support binary search).
127  */
128 static int cmpdev (const void *a, const void *b)
129 {
130     const dev_t *da = a, *db = b;
131     dev_t delta = *da - *db;
132
133     if (sizeof (delta) > sizeof (int))
134         return delta ? ((delta > 0) ? 1 : -1) : 0;
135     return (signed)delta;
136 }
137
138 static void DestroyDevice (void *data)
139 {
140     struct device *d = data;
141
142     if (d->sd)
143         services_discovery_RemoveItem (d->sd, d->item);
144     vlc_gc_decref (d->item);
145     free (d);
146 }
147
148 static char *decode_property (struct udev_device *, const char *);
149
150 /**
151  * Adds a udev device.
152  */
153 static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
154 {
155     services_discovery_sys_t *p_sys = sd->p_sys;
156
157     char *mrl = p_sys->subsys->get_mrl (dev);
158     if (mrl == NULL)
159         return 0; /* don't know if it was an error... */
160     char *name = p_sys->subsys->get_name (dev);
161     input_item_t *item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
162                                                  name ? name : mrl,
163                                                  0, NULL, 0, -1,
164                                                  p_sys->subsys->item_type);
165     msg_Dbg (sd, "adding %s (%s)", mrl, name);
166     free (name);
167     free (mrl);
168     if (item == NULL)
169         return -1;
170
171     struct device *d = malloc (sizeof (*d));
172     if (d == NULL)
173     {
174         vlc_gc_decref (item);
175         return -1;
176     }
177     d->devnum = udev_device_get_devnum (dev);
178     d->item = item;
179     d->sd = NULL;
180
181     struct device **dp = tsearch (d, &p_sys->root, cmpdev);
182     if (dp == NULL) /* Out-of-memory */
183     {
184         DestroyDevice (d);
185         return -1;
186     }
187     if (*dp != d) /* Overwrite existing device */
188     {
189         DestroyDevice (*dp);
190         *dp = d;
191     }
192
193     name = p_sys->subsys->get_cat (dev);
194     services_discovery_AddItem (sd, item, name ? name : "Generic");
195     d->sd = sd;
196     free (name);
197     return 0;
198 }
199
200 /**
201  * Removes a udev device (if present).
202  */
203 static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
204 {
205     services_discovery_sys_t *p_sys = sd->p_sys;
206
207     dev_t num = udev_device_get_devnum (dev);
208     struct device **dp = tfind (&(dev_t){ num }, &p_sys->root, cmpdev);
209     if (dp == NULL)
210         return;
211
212     struct device *d = *dp;
213     tdelete (d, &p_sys->root, cmpdev);
214     DestroyDevice (d);
215 }
216
217 static void *Run (void *);
218
219 /**
220  * Probes and initializes.
221  */
222 static int Open (vlc_object_t *obj, const struct subsys *subsys)
223 {
224     services_discovery_t *sd = (services_discovery_t *)obj;
225     services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
226
227     if (p_sys == NULL)
228         return VLC_ENOMEM;
229     sd->p_sys = p_sys;
230     p_sys->subsys = subsys;
231     p_sys->root = NULL;
232
233     struct udev_monitor *mon = NULL;
234     struct udev *udev = udev_new ();
235     if (udev == NULL)
236         goto error;
237
238     mon = udev_monitor_new_from_netlink (udev, "udev");
239     if (mon == NULL
240      || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
241                                                          NULL))
242         goto error;
243     p_sys->monitor = mon;
244
245     /* Enumerate existing devices */
246     struct udev_enumerate *devenum = udev_enumerate_new (udev);
247     if (devenum == NULL)
248         goto error;
249     if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
250     {
251         udev_enumerate_unref (devenum);
252         goto error;
253     }
254
255     udev_monitor_enable_receiving (mon);
256     /* Note that we enumerate _after_ monitoring is enabled so that we do not
257      * loose device events occuring while we are enumerating. We could still
258      * loose events if the Netlink socket receive buffer overflows. */
259     udev_enumerate_scan_devices (devenum);
260     struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
261     struct udev_list_entry *deventry;
262     udev_list_entry_foreach (deventry, devlist)
263     {
264         const char *path = udev_list_entry_get_name (deventry);
265         struct udev_device *dev = udev_device_new_from_syspath (udev, path);
266         AddDevice (sd, dev);
267         udev_device_unref (dev);
268     }
269     udev_enumerate_unref (devenum);
270
271     if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
272     {   /* Fallback without thread */
273         udev_monitor_unref (mon);
274         udev_unref (udev);
275         p_sys->monitor = NULL;
276     }
277     return VLC_SUCCESS;
278
279 error:
280     if (mon != NULL)
281         udev_monitor_unref (mon);
282     if (udev != NULL)
283         udev_unref (udev);
284     free (p_sys);
285     return VLC_EGENERIC;
286 }
287
288 /**
289  * Releases resources
290  */
291 static void Close (vlc_object_t *obj)
292 {
293     services_discovery_t *sd = (services_discovery_t *)obj;
294     services_discovery_sys_t *p_sys = sd->p_sys;
295
296     if (p_sys->monitor != NULL)
297     {
298         struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
299
300         vlc_cancel (p_sys->thread);
301         vlc_join (p_sys->thread, NULL);
302         udev_monitor_unref (p_sys->monitor);
303         udev_unref (udev);
304     }
305
306     tdestroy (p_sys->root, DestroyDevice);
307     free (p_sys);
308 }
309
310 static void *Run (void *data)
311 {
312     services_discovery_t *sd = data;
313     services_discovery_sys_t *p_sys = sd->p_sys;
314     struct udev_monitor *mon = p_sys->monitor;
315
316     int fd = udev_monitor_get_fd (mon);
317     struct pollfd ufd = { .fd = fd, .events = POLLIN, };
318
319     for (;;)
320     {
321         while (poll (&ufd, 1, -1) == -1)
322             if (errno != EINTR)
323                 break;
324
325         int canc = vlc_savecancel ();
326         struct udev_device *dev = udev_monitor_receive_device (mon);
327         if (dev == NULL)
328             continue;
329
330         const char *action = udev_device_get_action (dev);
331         if (!strcmp (action, "add"))
332             AddDevice (sd, dev);
333         else if (!strcmp (action, "remove"))
334             RemoveDevice (sd, dev);
335         else if (!strcmp (action, "change"))
336         {
337             RemoveDevice (sd, dev);
338             AddDevice (sd, dev);
339         }
340         udev_device_unref (dev);
341         vlc_restorecancel (canc);
342     }
343     return NULL;
344 }
345
346 /**
347  * Converts an hexadecimal digit to an integer.
348  */
349 static int hex (char c)
350 {
351     if (c >= '0' && c <= '9')
352         return c - '0';
353     if (c >= 'A' && c <= 'F')
354         return c + 10 - 'A';
355     if (c >= 'a' && c <= 'f')
356         return c + 10 - 'a';
357     return -1;
358 }
359
360 /**
361  * Decodes a udev hexadecimal-encoded property.
362  */
363 static char *decode (const char *enc)
364 {
365     char *ret = enc ? strdup (enc) : NULL;
366     if (ret == NULL)
367         return NULL;
368
369     char *out = ret;
370     for (const char *in = ret; *in; out++)
371     {
372         int h1, h2;
373
374         if ((in[0] == '\\') && (in[1] == 'x')
375          && ((h1 = hex (in[2])) != -1)
376          && ((h2 = hex (in[3])) != -1))
377         {
378             *out = (h1 << 4) | h2;
379             in += 4;
380         }
381         else
382         {
383             *out = *in;
384             in++;
385         }
386     }
387     *out = 0;
388     return ret;
389 }
390
391 static char *decode_property (struct udev_device *dev, const char *name)
392 {
393     return decode (udev_device_get_property_value (dev, name));
394 }
395
396
397 /*** Video4Linux support ***/
398 static bool is_v4l_legacy (struct udev_device *dev)
399 {
400     const char *version;
401
402     version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
403     return version && !strcmp (version, "1");
404 }
405
406 static char *v4l_get_mrl (struct udev_device *dev)
407 {
408     /* Determine media location */
409     const char *scheme = "v4l2";
410     if (is_v4l_legacy (dev))
411         scheme = "v4l";
412     const char *node = udev_device_get_devnode (dev);
413     char *mrl;
414
415     if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
416         mrl = NULL;
417     return mrl;
418 }
419
420 static char *v4l_get_name (struct udev_device *dev)
421 {
422     const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
423     return prd ? strdup (prd) : NULL;
424 }
425
426 static char *v4l_get_cat (struct udev_device *dev)
427 {
428     return decode_property (dev, "ID_VENDOR_ENC");
429 }
430
431 int OpenV4L (vlc_object_t *obj)
432 {
433     static const struct subsys subsys = {
434         "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat, ITEM_TYPE_CARD,
435     };
436
437     return Open (obj, &subsys);
438 }
439
440
441 #ifdef HAVE_ALSA
442 /*** Advanced Linux Sound Architecture support ***/
443 #include <alsa/asoundlib.h>
444
445 static int alsa_get_device (struct udev_device *dev, unsigned *restrict pcard,
446                             unsigned *restrict pdevice)
447 {
448     const char *node = udev_device_get_devpath (dev);
449     char type;
450
451     node = strrchr (node, '/');
452     if (node == NULL)
453         return -1;
454     if (sscanf (node, "/pcmC%uD%u%c", pcard, pdevice, &type) < 3)
455         return -1;
456     if (type != 'c')
457         return -1;
458     return 0;
459 }
460
461
462 static char *alsa_get_mrl (struct udev_device *dev)
463 {
464     /* Determine media location */
465     char *mrl;
466     unsigned card, device;
467
468     if (alsa_get_device (dev, &card, &device))
469         return NULL;
470
471     if (asprintf (&mrl, "alsa://plughw:%u,%u", card, device) == -1)
472         mrl = NULL;
473     return mrl;
474 }
475
476 static char *alsa_get_name (struct udev_device *dev)
477 {
478     char *name = NULL;
479     unsigned card, device;
480
481     if (alsa_get_device (dev, &card, &device))
482         return NULL;
483
484     char card_name[4 + 3 * sizeof (int)];
485     snprintf (card_name, sizeof (card_name), "hw:%u", card);
486
487     snd_ctl_t *ctl;
488     if (snd_ctl_open (&ctl, card_name, 0))
489         return NULL;
490
491     snd_pcm_info_t *pcm_info;
492     snd_pcm_info_alloca (&pcm_info);
493     snd_pcm_info_set_device (pcm_info, device);
494     snd_pcm_info_set_subdevice (pcm_info, 0);
495     snd_pcm_info_set_stream (pcm_info, SND_PCM_STREAM_CAPTURE);
496     if (snd_ctl_pcm_info (ctl, pcm_info))
497         goto out;
498
499     name = strdup (snd_pcm_info_get_name (pcm_info));
500 out:
501     snd_ctl_close (ctl);
502     return name;
503 }
504
505 static char *alsa_get_cat (struct udev_device *dev)
506 {
507     const char *vnd;
508
509     dev = udev_device_get_parent (dev);
510     if (dev == NULL)
511         return NULL;
512
513     vnd = udev_device_get_property_value (dev, "ID_VENDOR_FROM_DATABASE");
514     if (vnd == NULL)
515         /* FIXME: USB may take time to settle... the parent device */
516         vnd = udev_device_get_property_value (dev, "ID_BUS");
517     return vnd ? strdup (vnd) : NULL;
518 }
519
520 int OpenALSA (vlc_object_t *obj)
521 {
522     static const struct subsys subsys = {
523         "sound", alsa_get_mrl, alsa_get_name, alsa_get_cat, ITEM_TYPE_CARD,
524     };
525
526     return Open (obj, &subsys);
527 }
528 #endif /* HAVE_ALSA */
529
530
531 /*** Discs support ***/
532 static char *disc_get_mrl (struct udev_device *dev)
533 {
534     const char *val;
535
536     val = udev_device_get_property_value (dev, "ID_CDROM");
537     if (val == NULL)
538         return NULL; /* Ignore non-optical block devices */
539
540     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
541     if (val && !strcmp (val, "blank"))
542         return NULL; /* ignore empty drives and virgin recordable discs */
543
544     const char *scheme = NULL;
545     val = udev_device_get_property_value (dev,
546                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
547     if (val && atoi (val))
548         scheme = "cdda"; /* Audio CD rather than file system */
549     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
550     if (val && atoi (val))
551         scheme = "dvd";
552
553     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
554     if (val && atoi (val))
555         scheme = "bd";
556 #ifdef LOL
557     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
558     if (val && atoi (val))
559         scheme = "hddvd";
560 #endif
561
562     /* We didn't get any property that could tell we have optical disc
563        that we can play */
564     if (scheme == NULL)
565         return NULL;
566
567     val = udev_device_get_devnode (dev);
568     return make_URI (val, scheme);
569 }
570
571 static char *disc_get_name (struct udev_device *dev)
572 {
573     return decode_property (dev, "ID_FS_LABEL_ENC");
574 }
575
576 static char *disc_get_cat (struct udev_device *dev)
577 {
578     struct udev_list_entry *list, *entry;
579
580     list = udev_device_get_properties_list_entry (dev);
581     if (unlikely(list == NULL))
582         return NULL;
583
584     const char *cat = NULL;
585     udev_list_entry_foreach (entry, list)
586     {
587         const char *name = udev_list_entry_get_name (entry);
588
589         if (strncmp (name, "ID_CDROM_MEDIA_", 15))
590             continue;
591         if (!atoi (udev_list_entry_get_value (entry)))
592             continue;
593         name += 15;
594
595         if (!strncmp (name, "CD", 2))
596             cat = N_("CD");
597         else if (!strncmp (name, "DVD", 3))
598             cat = N_("DVD");
599         else if (!strncmp (name, "BD", 2))
600             cat = N_("Blu-Ray");
601         else if (!strncmp (name, "HDDVD", 5))
602             cat = N_("HD DVD");
603
604         if (cat != NULL)
605             break;
606     }
607
608     if (cat == NULL)
609         cat = N_("Unknown type");
610     return strdup (vlc_gettext (cat));
611 }
612
613 int OpenDisc (vlc_object_t *obj)
614 {
615     static const struct subsys subsys = {
616         "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
617     };
618
619     return Open (obj, &subsys);
620 }