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