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