3 * @brief List of multimedia devices for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
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.
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.
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 ****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30 #include <vlc_plugin.h>
34 static int OpenV4L (vlc_object_t *);
35 static int OpenDisc (vlc_object_t *);
36 static void Close (vlc_object_t *);
42 set_shortname (N_("Devices"))
43 set_description (N_("Capture devices"))
44 set_category (CAT_PLAYLIST)
45 set_subcategory (SUBCAT_PLAYLIST_SD)
46 set_capability ("services_discovery", 0)
47 set_callbacks (OpenV4L, Close)
51 set_shortname (N_("Discs"))
52 set_description (N_("Discs"))
53 set_category (CAT_PLAYLIST)
54 set_subcategory (SUBCAT_PLAYLIST_SD)
55 set_capability ("services_discovery", 0)
56 set_callbacks (OpenDisc, Close)
64 char * (*get_mrl) (struct udev_device *dev);
65 char * (*get_name) (struct udev_device *dev);
66 char * (*get_cat) (struct udev_device *dev);
69 struct services_discovery_sys_t
71 const struct subsys *subsys;
72 struct udev_monitor *monitor;
78 static void *Run (void *);
79 static void HandleDevice (services_discovery_t *, struct udev_device *, bool);
82 * Probes and initializes.
84 static int Open (vlc_object_t *obj, const struct subsys *subsys)
86 services_discovery_t *sd = (services_discovery_t *)obj;
87 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
92 p_sys->subsys = subsys;
96 struct udev_monitor *mon = NULL;
97 struct udev *udev = udev_new ();
101 mon = udev_monitor_new_from_netlink (udev, "udev");
103 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
106 p_sys->monitor = mon;
108 /* Enumerate existing devices */
109 struct udev_enumerate *devenum = udev_enumerate_new (udev);
112 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
114 udev_enumerate_unref (devenum);
118 udev_monitor_enable_receiving (mon);
119 /* Note that we enumerate _after_ monitoring is enabled so that we do not
120 * loose device events occuring while we are enumerating. We could still
121 * loose events if the Netlink socket receive buffer overflows. */
122 udev_enumerate_scan_devices (devenum);
123 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
124 struct udev_list_entry *deventry;
125 udev_list_entry_foreach (deventry, devlist)
127 const char *path = udev_list_entry_get_name (deventry);
128 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
129 HandleDevice (sd, dev, true);
130 udev_device_unref (dev);
132 udev_enumerate_unref (devenum);
134 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
135 { /* Fallback without thread */
136 udev_monitor_unref (mon);
138 p_sys->monitor = NULL;
144 udev_monitor_unref (mon);
155 static void Close (vlc_object_t *obj)
157 services_discovery_t *sd = (services_discovery_t *)obj;
158 services_discovery_sys_t *p_sys = sd->p_sys;
160 if (p_sys->monitor != NULL)
162 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
164 vlc_cancel (p_sys->thread);
165 vlc_join (p_sys->thread, NULL);
166 udev_monitor_unref (p_sys->monitor);
170 for (size_t i = 0; i < p_sys->itemc; i++)
171 vlc_gc_decref (p_sys->itemv[i]);
177 static void *Run (void *data)
179 services_discovery_t *sd = data;
180 services_discovery_sys_t *p_sys = sd->p_sys;
181 struct udev_monitor *mon = p_sys->monitor;
183 int fd = udev_monitor_get_fd (mon);
184 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
188 while (poll (&ufd, 1, -1) == -1)
192 struct udev_device *dev = udev_monitor_receive_device (mon);
196 /* FIXME: handle change, offline, online */
197 const char *action = udev_device_get_action (dev);
198 if (!strcmp (action, "add"))
199 HandleDevice (sd, dev, true);
200 else if (!strcmp (action, "remove"))
201 HandleDevice (sd, dev, false);
203 udev_device_unref (dev);
208 static int hex (char c)
210 if (c >= '0' && c <= '9')
212 if (c >= 'A' && c <= 'F')
214 if (c >= 'a' && c <= 'f')
219 static char *decode (const char *enc)
221 char *ret = enc ? strdup (enc) : NULL;
226 for (const char *in = ret; *in; out++)
230 if ((in[0] == '\\') && (in[1] == 'x')
231 && ((h1 = hex (in[2])) != -1)
232 && ((h2 = hex (in[3])) != -1))
234 *out = (h1 << 4) | h2;
247 static char *decode_property (struct udev_device *dev, const char *name)
249 return decode (udev_device_get_property_value (dev, name));
252 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
255 services_discovery_sys_t *p_sys = sd->p_sys;
257 /* Determine media location */
258 char *mrl = p_sys->subsys->get_mrl (dev);
262 /* Find item in list */
263 input_item_t *item = NULL;
266 for (i = 0; i < p_sys->itemc; i++)
268 input_item_t *oitem = p_sys->itemv[i];
269 char *omrl = input_item_GetURI (oitem);
271 if (!strcmp (omrl, mrl))
278 /* Add/Remove old item */
279 if (add && (item == NULL))
281 char *name = p_sys->subsys->get_name (dev);
282 item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
283 name ? name : "Unnamed",
284 0, NULL, 0, -1, ITEM_TYPE_CARD);
288 msg_Dbg (sd, "adding %s", mrl);
289 name = p_sys->subsys->get_cat (dev);
290 services_discovery_AddItem (sd, item, name ? name : "Generic");
293 TAB_APPEND (p_sys->itemc, p_sys->itemv, item);
296 else if (!add && (item != NULL))
298 msg_Dbg (sd, "removing %s", mrl);
299 services_discovery_RemoveItem (sd, item);
300 vlc_gc_decref (item);
301 TAB_REMOVE (p_sys->itemc, p_sys->itemv, i);
306 /*** Video4Linux support ***/
307 static bool is_v4l_legacy (struct udev_device *dev)
311 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
312 return version && !strcmp (version, "1");
315 static char *v4l_get_mrl (struct udev_device *dev)
317 /* Determine media location */
318 const char *scheme = "v4l2";
319 if (is_v4l_legacy (dev))
321 const char *node = udev_device_get_devnode (dev);
324 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
329 static char *v4l_get_name (struct udev_device *dev)
331 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
332 return prd ? strdup (prd) : NULL;
335 static char *v4l_get_cat (struct udev_device *dev)
337 return decode_property (dev, "ID_VENDOR_ENC");
340 int OpenV4L (vlc_object_t *obj)
342 static const struct subsys subsys = {
343 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat,
346 return Open (obj, &subsys);
349 /*** Discs support ***/
350 static char *disc_get_mrl (struct udev_device *dev)
354 val = udev_device_get_property_value (dev, "ID_CDROM");
356 return NULL; /* Ignore non-optical block devices */
358 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_STATE");
359 if ((val == NULL) || !strcmp (val, "blank"))
360 return NULL; /* ignore empty drives and virgin recordable discs */
362 const char *scheme = "file";
363 val = udev_device_get_property_value (dev,
364 "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO");
365 if (val && atoi (val))
366 scheme = "cdda"; /* Audio CD rather than file system */
367 #if 0 /* we can use file:// for DVDs */
368 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
369 if (val && atoi (val))
372 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
373 if (val && atoi (val))
376 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
377 if (val && atoi (val))
381 val = udev_device_get_devnode (dev);
384 if (asprintf (&mrl, "%s://%s", scheme, val) == -1)
389 static char *disc_get_name (struct udev_device *dev)
391 return decode_property (dev, "ID_FS_LABEL_ENC");
394 static char *disc_get_cat (struct udev_device *dev)
397 const char *cat = "Unknown";
399 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_CD");
400 if (val && atoi (val))
402 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_DVD");
403 if (val && atoi (val))
405 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_BD");
406 if (val && atoi (val))
407 cat = "Blue-ray disc";
408 val = udev_device_get_property_value (dev, "ID_CDROM_MEDIA_HDDVD");
409 if (val && atoi (val))
415 int OpenDisc (vlc_object_t *obj)
417 static const struct subsys subsys = {
418 "block", disc_get_mrl, disc_get_name, disc_get_cat,
421 return Open (obj, &subsys);