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