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 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 (OpenV4L, Close)
54 char * (*get_mrl) (struct udev_device *dev);
55 char * (*get_name) (struct udev_device *dev);
56 char * (*get_cat) (struct udev_device *dev);
59 struct services_discovery_sys_t
61 const struct subsys *subsys;
62 struct udev_monitor *monitor;
68 static void *Run (void *);
69 static void HandleDevice (services_discovery_t *, struct udev_device *, bool);
72 * Probes and initializes.
74 static int Open (vlc_object_t *obj, const struct subsys *subsys)
76 services_discovery_t *sd = (services_discovery_t *)obj;
77 services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
82 p_sys->subsys = subsys;
86 struct udev_monitor *mon = NULL;
87 struct udev *udev = udev_new ();
91 mon = udev_monitor_new_from_netlink (udev, "udev");
93 || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys->name,
98 /* Enumerate existing devices */
99 struct udev_enumerate *devenum = udev_enumerate_new (udev);
102 if (udev_enumerate_add_match_subsystem (devenum, subsys->name))
104 udev_enumerate_unref (devenum);
108 udev_monitor_enable_receiving (mon);
109 /* Note that we enumerate _after_ monitoring is enabled so that we do not
110 * loose device events occuring while we are enumerating. We could still
111 * loose events if the Netlink socket receive buffer overflows. */
112 udev_enumerate_scan_devices (devenum);
113 struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
114 struct udev_list_entry *deventry;
115 udev_list_entry_foreach (deventry, devlist)
117 const char *path = udev_list_entry_get_name (deventry);
118 struct udev_device *dev = udev_device_new_from_syspath (udev, path);
119 HandleDevice (sd, dev, true);
120 udev_device_unref (dev);
122 udev_enumerate_unref (devenum);
124 if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
125 { /* Fallback without thread */
126 udev_monitor_unref (mon);
128 p_sys->monitor = NULL;
134 udev_monitor_unref (mon);
145 static void Close (vlc_object_t *obj)
147 services_discovery_t *sd = (services_discovery_t *)obj;
148 services_discovery_sys_t *p_sys = sd->p_sys;
150 if (p_sys->monitor != NULL)
152 struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
154 vlc_cancel (p_sys->thread);
155 vlc_join (p_sys->thread, NULL);
156 udev_monitor_unref (p_sys->monitor);
160 for (size_t i = 0; i < p_sys->itemc; i++)
161 vlc_gc_decref (p_sys->itemv[i]);
167 static void *Run (void *data)
169 services_discovery_t *sd = data;
170 services_discovery_sys_t *p_sys = sd->p_sys;
171 struct udev_monitor *mon = p_sys->monitor;
173 int fd = udev_monitor_get_fd (mon);
174 struct pollfd ufd = { .fd = fd, .events = POLLIN, };
178 while (poll (&ufd, 1, -1) == -1)
182 struct udev_device *dev = udev_monitor_receive_device (mon);
186 /* FIXME: handle change, offline, online */
187 const char *action = udev_device_get_action (dev);
188 if (!strcmp (action, "add"))
189 HandleDevice (sd, dev, true);
190 else if (!strcmp (action, "remove"))
191 HandleDevice (sd, dev, false);
193 udev_device_unref (dev);
198 static int hex (char c)
200 if (c >= '0' && c <= '9')
202 if (c >= 'A' && c <= 'F')
204 if (c >= 'a' && c <= 'f')
209 static char *decode (const char *enc)
211 char *ret = enc ? strdup (enc) : NULL;
216 for (const char *in = ret; *in; out++)
220 if ((in[0] == '\\') && (in[1] == 'x')
221 && ((h1 = hex (in[2])) != -1)
222 && ((h2 = hex (in[3])) != -1))
224 *out = (h1 << 4) | h2;
237 static char *decode_property (struct udev_device *dev, const char *name)
239 return decode (udev_device_get_property_value (dev, name));
242 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
245 services_discovery_sys_t *p_sys = sd->p_sys;
247 /* Determine media location */
248 char *mrl = p_sys->subsys->get_mrl (dev);
252 /* Find item in list */
253 input_item_t *item = NULL;
256 for (i = 0; i < p_sys->itemc; i++)
258 input_item_t *oitem = p_sys->itemv[i];
259 char *omrl = input_item_GetURI (oitem);
261 if (!strcmp (omrl, mrl))
268 /* Add/Remove old item */
269 if (add && (item == NULL))
271 char *name = p_sys->subsys->get_name (dev);
272 item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
273 name ? name : "Unnamed",
274 0, NULL, 0, -1, ITEM_TYPE_CARD);
278 msg_Dbg (sd, "adding %s", mrl);
279 name = p_sys->subsys->get_cat (dev);
280 services_discovery_AddItem (sd, item, name ? name : "Generic");
283 TAB_APPEND (p_sys->itemc, p_sys->itemv, item);
286 else if (!add && (item != NULL))
288 msg_Dbg (sd, "removing %s", mrl);
289 services_discovery_RemoveItem (sd, item);
290 vlc_gc_decref (item);
291 TAB_REMOVE (p_sys->itemc, p_sys->itemv, i);
296 /*** Video4Linux support ***/
297 static bool is_v4l_legacy (struct udev_device *dev)
301 version = udev_device_get_property_value (dev, "ID_V4L_VERSION");
302 return version && !strcmp (version, "1");
305 static char *v4l_get_mrl (struct udev_device *dev)
307 /* Determine media location */
308 const char *scheme = "v4l2";
309 if (is_v4l_legacy (dev))
311 const char *node = udev_device_get_devnode (dev);
314 if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
319 static char *v4l_get_name (struct udev_device *dev)
321 const char *prd = udev_device_get_property_value (dev, "ID_V4L_PRODUCT");
322 return prd ? strdup (prd) : NULL;
325 static char *v4l_get_cat (struct udev_device *dev)
327 return decode_property (dev, "ID_VENDOR_ENC");
330 int OpenV4L (vlc_object_t *obj)
332 static const struct subsys subsys = {
333 "video4linux", v4l_get_mrl, v4l_get_name, v4l_get_cat,
336 return Open (obj, &subsys);