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 Open (vlc_object_t *);
35 static void Close (vlc_object_t *);
41 set_shortname (N_("Devices"))
42 set_description (N_("Capture devices"))
43 set_category (CAT_PLAYLIST)
44 set_subcategory (SUBCAT_PLAYLIST_SD)
45 set_capability ("services_discovery", 0)
46 set_callbacks (Open, Close)
51 struct services_discovery_sys_t
53 struct udev_monitor *monitor;
57 static void *Run (void *);
58 static void HandleDevice (services_discovery_t *, struct udev_device *, bool);
61 * Probes and initializes.
63 static int Open (vlc_object_t *obj)
65 const char subsys[] = "video4linux";
66 services_discovery_t *sd = (services_discovery_t *)obj;
67 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
73 struct udev_monitor *mon = NULL;
74 struct udev *udev = udev_new ();
78 mon = udev_monitor_new_from_netlink (udev, "udev");
80 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys, NULL))
84 /* Enumerate existing devices */
85 struct udev_enumerate *devenum = udev_enumerate_new (udev);
88 if (udev_enumerate_add_match_subsystem (devenum, subsys))
90 udev_enumerate_unref (devenum);
94 udev_monitor_enable_receiving (mon);
95 /* Note that we enumerate _after_ monitoring is enabled so that we do not
96 * loose device events occuring while we are enumerating. We could still
97 * loose events if the Netlink socket receive buffer overflows. */
98 udev_enumerate_scan_devices (devenum);
99 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
100 struct udev_list_entry *deventry;
101 udev_list_entry_foreach (deventry, devlist)
103 const char *path = udev_list_entry_get_name (deventry);
104 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
105 HandleDevice (sd, dev, true);
106 udev_device_unref (dev);
108 udev_enumerate_unref (devenum);
110 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
116 udev_monitor_unref (mon);
127 static void Close (vlc_object_t *obj)
129 services_discovery_t *sd = (services_discovery_t *)obj;
130 services_discovery_sys_t *p_sys = sd->p_sys;
131 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
133 vlc_cancel (p_sys->thread);
134 vlc_join (p_sys->thread, NULL);
135 udev_monitor_unref (p_sys->monitor);
141 static void *Run (void *data)
143 services_discovery_t *sd = data;
144 services_discovery_sys_t *p_sys = sd->p_sys;
145 struct udev_monitor *mon = p_sys->monitor;
147 int fd = udev_monitor_get_fd (mon);
148 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
152 while (poll (&ufd, 1, -1) == -1)
156 struct udev_device *dev = udev_monitor_receive_device (mon);
160 /* FIXME: handle change, offline, online */
161 if (!strcmp (udev_device_get_action (dev), "add"))
162 HandleDevice (sd, dev, true);
163 else if (!strcmp (udev_device_get_action (dev), "remove"))
164 HandleDevice (sd, dev, false);
166 udev_device_unref (dev);
171 static int hex (char c)
173 if (c >= '0' && c <= '9')
175 if (c >= 'A' && c <= 'F')
177 if (c >= 'a' && c <= 'f')
182 static char *decode (const char *enc)
184 char *ret = enc ? strdup (enc) : NULL;
189 for (const char *in = ret; *in; out++)
193 if ((in[0] == '\\') && (in[1] == 'x')
194 && ((h1 = hex (in[2])) != -1)
195 && ((h2 = hex (in[3])) != -1))
197 *out = (h1 << 4) | h2;
210 static char *decode_property (struct udev_device *dev, const char *name)
212 return decode (udev_device_get_property_value (dev, name));
215 static bool is_v4l_legacy (struct udev_device *dev)
219 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
220 return version && !strcmp (version, "1");
223 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
226 //services_discovery_sys_t *p_sys = sd->p_sys;
229 msg_Err (sd, "FIXME: removing device not implemented!");
233 const char *scheme = "v4l2";
234 if (is_v4l_legacy (dev))
236 const char *node = udev_device_get_devnode (dev);
237 char *vnd = decode_property (dev, "ID_VENDOR_ENC");
238 const char *name = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
241 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
244 /* FIXME: check for duplicates (race between monitor starting to receive
245 * and initial enumeration). */
247 item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
248 name ? name : "Unnamed",
249 0, NULL, 0, -1, ITEM_TYPE_CARD);
250 msg_Dbg (sd, "adding %s", mrl);
255 services_discovery_AddItem (sd, item, vnd ? vnd : "Generic");
256 vlc_gc_decref (item);