3 * @brief List of multimedia devices for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
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.
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.
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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_services_discovery.h>
37 #include <vlc_plugin.h>
39 # include <vlc_modules.h>
44 static int OpenV4L (vlc_object_t
*);
46 static int OpenALSA (vlc_object_t
*);
48 static int OpenDisc (vlc_object_t
*);
49 static void Close (vlc_object_t
*);
50 static int vlc_sd_probe_Open (vlc_object_t
*);
56 set_shortname (N_("Video capture"))
57 set_description (N_("Video capture (Video4Linux)"))
58 set_category (CAT_PLAYLIST
)
59 set_subcategory (SUBCAT_PLAYLIST_SD
)
60 set_capability ("services_discovery", 0)
61 set_callbacks (OpenV4L
, Close
)
62 add_shortcut ("v4l", "video")
65 set_shortname (N_("Audio capture"))
66 set_description (N_("Audio capture (ALSA)"))
67 set_category (CAT_PLAYLIST
)
68 set_subcategory (SUBCAT_PLAYLIST_SD
)
69 set_capability ("services_discovery", 0)
70 set_callbacks (OpenALSA
, Close
)
71 add_shortcut ("alsa", "audio")
74 set_shortname (N_("Discs"))
75 set_description (N_("Discs"))
76 set_category (CAT_PLAYLIST
)
77 set_subcategory (SUBCAT_PLAYLIST_SD
)
78 set_capability ("services_discovery", 0)
79 set_callbacks (OpenDisc
, Close
)
82 VLC_SD_PROBE_SUBMODULE
86 static int vlc_sd_probe_Open (vlc_object_t
*obj
)
88 vlc_probe_t
*probe
= (vlc_probe_t
*)obj
;
90 struct udev
*udev
= udev_new ();
92 return VLC_PROBE_CONTINUE
;
94 struct udev_monitor
*mon
= udev_monitor_new_from_netlink (udev
, "udev");
97 vlc_sd_probe_Add (probe
, "v4l", N_("Video capture"), SD_CAT_DEVICES
);
99 if (!module_exists ("pulselist"))
100 vlc_sd_probe_Add (probe
, "alsa", N_("Audio capture"),
103 vlc_sd_probe_Add (probe
, "disc", N_("Discs"), SD_CAT_DEVICES
);
104 udev_monitor_unref (mon
);
107 return VLC_PROBE_CONTINUE
;
112 dev_t devnum
; /* must be first */
114 services_discovery_t
*sd
;
120 const char *description
;
121 char * (*get_mrl
) (struct udev_device
*dev
);
122 char * (*get_name
) (struct udev_device
*dev
);
128 const struct subsys
*subsys
;
129 struct udev_monitor
*monitor
;
132 } services_discovery_sys_t
;
135 * Compares two devices (to support binary search).
137 static int cmpdev (const void *a
, const void *b
)
139 const dev_t
*da
= a
, *db
= b
;
148 static void DestroyDevice (void *data
)
150 struct device
*d
= data
;
153 services_discovery_RemoveItem (d
->sd
, d
->item
);
154 input_item_Release (d
->item
);
158 static char *decode_property (struct udev_device
*, const char *);
161 * Adds a udev device.
163 static int AddDevice (services_discovery_t
*sd
, struct udev_device
*dev
)
165 services_discovery_sys_t
*p_sys
= sd
->p_sys
;
167 char *mrl
= p_sys
->subsys
->get_mrl (dev
);
169 return 0; /* don't know if it was an error... */
170 char *name
= p_sys
->subsys
->get_name (dev
);
171 input_item_t
*item
= input_item_NewExt (mrl
, name
? name
: mrl
,
172 INPUT_DURATION_INDEFINITE
,
173 p_sys
->subsys
->item_type
, ITEM_LOCAL
);
174 msg_Dbg (sd
, "adding %s (%s)", mrl
, name
);
180 struct device
*d
= malloc (sizeof (*d
));
183 input_item_Release (item
);
186 d
->devnum
= udev_device_get_devnum (dev
);
190 void **dp
= tsearch (d
, &p_sys
->root
, cmpdev
);
191 if (dp
== NULL
) /* Out-of-memory */
196 if (*dp
!= d
) /* Overwrite existing device */
202 services_discovery_AddItem(sd
, item
);
208 * Removes a udev device (if present).
210 static void RemoveDevice (services_discovery_t
*sd
, struct udev_device
*dev
)
212 services_discovery_sys_t
*p_sys
= sd
->p_sys
;
214 dev_t num
= udev_device_get_devnum (dev
);
215 void **dp
= tfind (&(dev_t
){ num
}, &p_sys
->root
, cmpdev
);
219 struct device
*d
= *dp
;
220 tdelete (d
, &p_sys
->root
, cmpdev
);
224 static void *Run (void *);
227 * Probes and initializes.
229 static int Open (vlc_object_t
*obj
, const struct subsys
*subsys
)
231 services_discovery_t
*sd
= (services_discovery_t
*)obj
;
232 services_discovery_sys_t
*p_sys
= malloc (sizeof (*p_sys
));
237 sd
->description
= vlc_gettext(subsys
->description
);
239 p_sys
->subsys
= subsys
;
242 struct udev_monitor
*mon
= NULL
;
243 struct udev
*udev
= udev_new ();
247 mon
= udev_monitor_new_from_netlink (udev
, "udev");
249 || udev_monitor_filter_add_match_subsystem_devtype (mon
, subsys
->name
,
252 p_sys
->monitor
= mon
;
254 /* Enumerate existing devices */
255 struct udev_enumerate
*devenum
= udev_enumerate_new (udev
);
258 if (udev_enumerate_add_match_subsystem (devenum
, subsys
->name
))
260 udev_enumerate_unref (devenum
);
264 udev_monitor_enable_receiving (mon
);
265 /* Note that we enumerate _after_ monitoring is enabled so that we do not
266 * loose device events occuring while we are enumerating. We could still
267 * loose events if the Netlink socket receive buffer overflows. */
268 udev_enumerate_scan_devices (devenum
);
269 struct udev_list_entry
*devlist
= udev_enumerate_get_list_entry (devenum
);
270 struct udev_list_entry
*deventry
;
271 udev_list_entry_foreach (deventry
, devlist
)
273 const char *path
= udev_list_entry_get_name (deventry
);
274 struct udev_device
*dev
= udev_device_new_from_syspath (udev
, path
);
276 udev_device_unref (dev
);
278 udev_enumerate_unref (devenum
);
280 if (vlc_clone (&p_sys
->thread
, Run
, sd
, VLC_THREAD_PRIORITY_LOW
))
281 { /* Fallback without thread */
282 udev_monitor_unref (mon
);
284 p_sys
->monitor
= NULL
;
290 udev_monitor_unref (mon
);
300 static void Close (vlc_object_t
*obj
)
302 services_discovery_t
*sd
= (services_discovery_t
*)obj
;
303 services_discovery_sys_t
*p_sys
= sd
->p_sys
;
305 if (p_sys
->monitor
!= NULL
)
307 struct udev
*udev
= udev_monitor_get_udev (p_sys
->monitor
);
309 vlc_cancel (p_sys
->thread
);
310 vlc_join (p_sys
->thread
, NULL
);
311 udev_monitor_unref (p_sys
->monitor
);
315 tdestroy (p_sys
->root
, DestroyDevice
);
319 static void *Run (void *data
)
321 services_discovery_t
*sd
= data
;
322 services_discovery_sys_t
*p_sys
= sd
->p_sys
;
323 struct udev_monitor
*mon
= p_sys
->monitor
;
325 int fd
= udev_monitor_get_fd (mon
);
326 struct pollfd ufd
= { .fd
= fd
, .events
= POLLIN
, };
330 while (poll (&ufd
, 1, -1) == -1)
334 int canc
= vlc_savecancel ();
335 struct udev_device
*dev
= udev_monitor_receive_device (mon
);
339 const char *action
= udev_device_get_action (dev
);
340 if (!strcmp (action
, "add"))
342 else if (!strcmp (action
, "remove"))
343 RemoveDevice (sd
, dev
);
344 else if (!strcmp (action
, "change"))
346 RemoveDevice (sd
, dev
);
349 udev_device_unref (dev
);
350 vlc_restorecancel (canc
);
356 * Converts an hexadecimal digit to an integer.
358 static int hex (char c
)
360 if (c
>= '0' && c
<= '9')
362 if (c
>= 'A' && c
<= 'F')
364 if (c
>= 'a' && c
<= 'f')
370 * Decodes a udev hexadecimal-encoded property.
372 static char *decode (const char *enc
)
374 char *ret
= enc
? strdup (enc
) : NULL
;
379 for (const char *in
= ret
; *in
; out
++)
383 if ((in
[0] == '\\') && (in
[1] == 'x')
384 && ((h1
= hex (in
[2])) != -1)
385 && ((h2
= hex (in
[3])) != -1))
387 *out
= (h1
<< 4) | h2
;
400 static char *decode_property (struct udev_device
*dev
, const char *name
)
402 return decode (udev_device_get_property_value (dev
, name
));
406 /*** Video4Linux support ***/
407 static bool v4l_is_legacy (struct udev_device
*dev
)
411 version
= udev_device_get_property_value (dev
, "ID_V4L_VERSION");
412 return (version
!= NULL
) && !strcmp (version
, "1");
415 static bool v4l_can_capture (struct udev_device
*dev
)
419 caps
= udev_device_get_property_value (dev
, "ID_V4L_CAPABILITIES");
420 return (caps
!= NULL
) && (strstr (caps
, ":capture:") != NULL
);
423 static char *v4l_get_mrl (struct udev_device
*dev
)
425 /* Determine media location */
426 if (v4l_is_legacy (dev
) || !v4l_can_capture (dev
))
429 const char *node
= udev_device_get_devnode (dev
);
432 if (asprintf (&mrl
, "v4l2://%s", node
) == -1)
437 static char *v4l_get_name (struct udev_device
*dev
)
439 const char *prd
= udev_device_get_property_value (dev
, "ID_V4L_PRODUCT");
440 return prd
? strdup (prd
) : NULL
;
443 int OpenV4L (vlc_object_t
*obj
)
445 static const struct subsys subsys
= {
446 "video4linux", N_("Video capture"),
447 v4l_get_mrl
, v4l_get_name
, ITEM_TYPE_CARD
,
450 return Open (obj
, &subsys
);
455 /*** Advanced Linux Sound Architecture support ***/
456 #include <alsa/asoundlib.h>
458 static int alsa_get_device (struct udev_device
*dev
, unsigned *restrict pcard
,
459 unsigned *restrict pdevice
)
461 const char *node
= udev_device_get_devpath (dev
);
464 node
= strrchr (node
, '/');
467 if (sscanf (node
, "/pcmC%uD%u%c", pcard
, pdevice
, &type
) < 3)
475 static char *alsa_get_mrl (struct udev_device
*dev
)
477 /* Determine media location */
479 unsigned card
, device
;
481 if (alsa_get_device (dev
, &card
, &device
))
484 if (asprintf (&mrl
, "alsa://plughw:%u,%u", card
, device
) == -1)
489 static char *alsa_get_name (struct udev_device
*dev
)
492 unsigned card
, device
;
494 if (alsa_get_device (dev
, &card
, &device
))
497 char card_name
[4 + 3 * sizeof (int)];
498 snprintf (card_name
, sizeof (card_name
), "hw:%u", card
);
501 if (snd_ctl_open (&ctl
, card_name
, 0))
504 snd_pcm_info_t
*pcm_info
;
505 snd_pcm_info_alloca (&pcm_info
);
506 snd_pcm_info_set_device (pcm_info
, device
);
507 snd_pcm_info_set_subdevice (pcm_info
, 0);
508 snd_pcm_info_set_stream (pcm_info
, SND_PCM_STREAM_CAPTURE
);
509 if (snd_ctl_pcm_info (ctl
, pcm_info
))
512 name
= strdup (snd_pcm_info_get_name (pcm_info
));
518 int OpenALSA (vlc_object_t
*obj
)
520 static const struct subsys subsys
= {
521 "sound", N_("Audio capture"),
522 alsa_get_mrl
, alsa_get_name
, ITEM_TYPE_CARD
,
525 return Open (obj
, &subsys
);
527 #endif /* HAVE_ALSA */
530 /*** Discs support ***/
531 static char *disc_get_mrl (struct udev_device
*dev
)
533 const char *node
= udev_device_get_devnode (dev
);
536 val
= udev_device_get_property_value (dev
, "ID_CDROM");
538 return NULL
; /* Ignore non-optical block devices */
540 val
= udev_device_get_property_value (dev
, "ID_CDROM_MEDIA_STATE");
542 { /* Force probing of the disc in the drive if any. */
543 int fd
= vlc_open (node
, O_RDONLY
);
548 if (!strcmp (val
, "blank"))
549 return NULL
; /* ignore empty drives and virgin recordable discs */
551 const char *scheme
= NULL
;
552 val
= udev_device_get_property_value (dev
,
553 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
554 if (val
&& atoi (val
))
555 scheme
= "cdda"; /* Audio CD rather than file system */
556 val
= udev_device_get_property_value (dev
, "ID_CDROM_MEDIA_DVD");
557 if (val
&& atoi (val
))
560 val
= udev_device_get_property_value (dev
, "ID_CDROM_MEDIA_BD");
561 if (val
&& atoi (val
))
564 val
= udev_device_get_property_value (dev
, "ID_CDROM_MEDIA_HDDVD");
565 if (val
&& atoi (val
))
569 /* We didn't get any property that could tell we have optical disc
574 return vlc_path2uri (node
, scheme
);
577 static char *disc_get_name (struct udev_device
*dev
)
580 struct udev_list_entry
*list
, *entry
;
582 list
= udev_device_get_properties_list_entry (dev
);
583 if (unlikely(list
== NULL
))
586 const char *cat
= NULL
;
587 udev_list_entry_foreach (entry
, list
)
589 const char *propname
= udev_list_entry_get_name(entry
);
591 if (strncmp(propname
, "ID_CDROM_MEDIA_", 15))
593 if (!atoi (udev_list_entry_get_value (entry
)))
597 if (!strncmp(propname
, "CD", 2))
599 else if (!strncmp(propname
, "DVD", 3))
601 else if (!strncmp(propname
, "BD", 2))
603 else if (!strncmp(propname
, "HDDVD", 5))
611 cat
= N_("Unknown type");
613 char *label
= decode_property (dev
, "ID_FS_LABEL_ENC");
616 if (asprintf(&name
, "%s (%s)", label
, vlc_gettext(cat
)) < 0)
623 int OpenDisc (vlc_object_t
*obj
)
625 static const struct subsys subsys
= {
626 "block", N_("Discs"), disc_get_mrl
, disc_get_name
, ITEM_TYPE_DISC
,
629 return Open (obj
, &subsys
);