]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
95b6b1a8288cce931af8cdb04218600bbcea9d2d
[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_DEVICES);
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 #ifdef HAVE_ALSA
439 /*** Advanced Linux Sound Architecture support ***/
440 #include <alsa/asoundlib.h>
441
442 static int alsa_get_device (struct udev_device *dev, unsigned *restrict pcard,
443                             unsigned *restrict pdevice)
444 {
445     const char *node = udev_device_get_devpath (dev);
446     char type;
447
448     node = strrchr (node, '/');
449     if (node == NULL)
450         return -1;
451     if (sscanf (node, "/pcmC%uD%u%c", pcard, pdevice, &type) < 3)
452         return -1;
453     if (type != 'c')
454         return -1;
455     return 0;
456 }
457
458
459 static char *alsa_get_mrl (struct udev_device *dev)
460 {
461     /* Determine media location */
462     char *mrl;
463     unsigned card, device;
464
465     if (alsa_get_device (dev, &card, &device))
466         return NULL;
467
468     if (asprintf (&mrl, "alsa://plughw:%u,%u", card, device) == -1)
469         mrl = NULL;
470     return mrl;
471 }
472
473 static char *alsa_get_name (struct udev_device *dev)
474 {
475     char *name = NULL;
476     unsigned card, device;
477
478     if (alsa_get_device (dev, &card, &device))
479         return NULL;
480
481     char card_name[4 + 3 * sizeof (int)];
482     snprintf (card_name, sizeof (card_name), "hw:%u", card);
483
484     snd_ctl_t *ctl;
485     if (snd_ctl_open (&ctl, card_name, 0))
486         return NULL;
487
488     snd_pcm_info_t *pcm_info;
489     snd_pcm_info_alloca (&pcm_info);
490     snd_pcm_info_set_device (pcm_info, device);
491     snd_pcm_info_set_subdevice (pcm_info, 0);
492     snd_pcm_info_set_stream (pcm_info, SND_PCM_STREAM_CAPTURE);
493     if (snd_ctl_pcm_info (ctl, pcm_info))
494         goto out;
495
496     name = strdup (snd_pcm_info_get_name (pcm_info));
497 out:
498     snd_ctl_close (ctl);
499     return name;
500 }
501
502 static char *alsa_get_cat (struct udev_device *dev)
503 {
504     const char *vnd;
505
506     dev = udev_device_get_parent (dev);
507     if (dev == NULL)
508         return NULL;
509
510     vnd = udev_device_get_property_value (dev, "ID_VENDOR_FROM_DATABASE");
511     if (vnd == NULL)
512         /* FIXME: USB may take time to settle... the parent device */
513         vnd = udev_device_get_property_value (dev, "ID_BUS");
514     return vnd ? strdup (vnd) : NULL;
515 }
516
517 int OpenALSA (vlc_object_t *obj)
518 {
519     static const struct subsys subsys = {
520         "sound", alsa_get_mrl, alsa_get_name, alsa_get_cat, ITEM_TYPE_CARD,
521     };
522
523     return Open (obj, &subsys);
524 }
525 #endif /* HAVE_ALSA */
526
527
528 /*** Discs support ***/
529 static char *disc_get_mrl (struct udev_device *dev)
530 {
531     const char *val;
532
533     val = udev_device_get_property_value (dev, "ID_CDROM");
534     if (val == NULL)
535         return NULL; /* Ignore non-optical block devices */
536
537     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
538     if (val && !strcmp (val, "blank"))
539         return NULL; /* ignore empty drives and virgin recordable discs */
540
541     const char *scheme = NULL;
542     val = udev_device_get_property_value (dev,
543                                           "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
544     if (val && atoi (val))
545         scheme = "cdda"; /* Audio CD rather than file system */
546     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
547     if (val && atoi (val))
548         scheme = "dvd";
549
550     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
551     if (val && atoi (val))
552         scheme = "bd";
553 #ifdef LOL
554     val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
555     if (val && atoi (val))
556         scheme = "hddvd";
557 #endif
558
559     /* We didn't get any property that could tell we have optical disc
560        that we can play */
561     if (scheme == NULL)
562         return NULL;
563
564     val = udev_device_get_devnode (dev);
565     char *mrl;
566
567     if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
568         mrl = NULL;
569     return mrl;
570 }
571
572 static char *disc_get_name (struct udev_device *dev)
573 {
574     return decode_property (dev, "ID_FS_LABEL_ENC");
575 }
576
577 static char *disc_get_cat (struct udev_device *dev)
578 {
579     struct udev_list_entry *list, *entry;
580
581     list = udev_device_get_properties_list_entry (dev);
582     if (unlikely(list == NULL))
583         return NULL;
584
585     const char *cat = NULL;
586     udev_list_entry_foreach (entry, list)
587     {
588         const char *name = udev_list_entry_get_name (entry);
589
590         if (strncmp (name, "ID_CDROM_MEDIA_", 15))
591             continue;
592         if (!atoi (udev_list_entry_get_value (entry)))
593             continue;
594         name += 15;
595
596         if (!strncmp (name, "CD", 2))
597             cat = N_("CD");
598         else if (!strncmp (name, "DVD", 3))
599             cat = N_("DVD");
600         else if (!strncmp (name, "BD", 2))
601             cat = N_("Blu-Ray");
602         else if (!strncmp (name, "HDDVD", 5))
603             cat = N_("HD DVD");
604
605         if (cat != NULL)
606             break;
607     }
608
609     if (cat == NULL)
610         cat = N_("Unknown type");
611     return strdup (vlc_gettext (cat));
612 }
613
614 int OpenDisc (vlc_object_t *obj)
615 {
616     static const struct subsys subsys = {
617         "block", disc_get_mrl, disc_get_name, disc_get_cat, ITEM_TYPE_DISC,
618     };
619
620     return Open (obj, &subsys);
621 }