]> git.sesse.net Git - vlc/blobdiff - modules/services_discovery/udev.c
udev: rudimentary ALSA capture support
[vlc] / modules / services_discovery / udev.c
index b443a832221a12ee174133d7423fa00280ebe7ef..7168684b4ceec29dfa7c015cf23534811dec0e9c 100644 (file)
 #include <errno.h>
 
 static int OpenV4L (vlc_object_t *);
+static int OpenALSA (vlc_object_t *);
 static int OpenDisc (vlc_object_t *);
 static void Close (vlc_object_t *);
+static int vlc_sd_probe_Open (vlc_object_t *);
 
 /*
  * Module descriptor
  */
 vlc_module_begin ()
-    set_shortname (N_("Devices"))
-    set_description (N_("Capture devices"))
+    set_shortname (N_("Video capture"))
+    set_description (N_("Video capture (Video4Linux)"))
     set_category (CAT_PLAYLIST)
     set_subcategory (SUBCAT_PLAYLIST_SD)
     set_capability ("services_discovery", 0)
     set_callbacks (OpenV4L, Close)
     add_shortcut ("v4l")
 
+    add_submodule ()
+    set_shortname (N_("Audio capture"))
+    set_description (N_("Audio capture (ALSA)"))
+    set_category (CAT_PLAYLIST)
+    set_subcategory (SUBCAT_PLAYLIST_SD)
+    set_capability ("services_discovery", 0)
+    set_callbacks (OpenALSA, Close)
+    add_shortcut ("alsa")
+
     add_submodule ()
     set_shortname (N_("Discs"))
     set_description (N_("Discs"))
@@ -57,12 +68,38 @@ vlc_module_begin ()
     set_callbacks (OpenDisc, Close)
     add_shortcut ("disc")
 
+    VLC_SD_PROBE_SUBMODULE
+
 vlc_module_end ()
 
+static int vlc_sd_probe_Open (vlc_object_t *obj)
+{
+    vlc_probe_t *probe = (vlc_probe_t *)obj;
+
+    struct udev *udev = udev_new ();
+    if (udev == NULL)
+        return VLC_PROBE_CONTINUE;
+
+    struct udev_monitor *mon = udev_monitor_new_from_netlink (udev, "udev");
+    if (mon != NULL)
+    {
+        vlc_sd_probe_Add (probe, "v4l{longname=\"Video capture\"}",
+                          N_("Video capture"), SD_CAT_DEVICES);
+        vlc_sd_probe_Add (probe, "alsa{longname=\"Audio capture\"}",
+                          N_("Audio capture"), SD_CAT_DEVICES);
+        vlc_sd_probe_Add (probe, "disc{longname=\"Discs\"}", N_("Discs"),
+                          SD_CAT_MYCOMPUTER);
+        udev_monitor_unref (mon);
+    }
+    udev_unref (udev);
+    return VLC_PROBE_CONTINUE;
+}
+
 struct device
 {
     dev_t devnum; /* must be first */
     input_item_t *item;
+    services_discovery_t *sd;
 };
 
 struct subsys
@@ -99,6 +136,8 @@ static void DestroyDevice (void *data)
 {
     struct device *d = data;
 
+    if (d->sd)
+        services_discovery_RemoveItem (d->sd, d->item);
     vlc_gc_decref (d->item);
     free (d);
 }
@@ -132,8 +171,9 @@ static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
         vlc_gc_decref (item);
         return -1;
     }
-    d->item = item;
     d->devnum = udev_device_get_devnum (dev);
+    d->item = item;
+    d->sd = NULL;
 
     struct device **dp = tsearch (d, &p_sys->root, cmpdev);
     if (dp == NULL) /* Out-of-memory */
@@ -143,13 +183,13 @@ static int AddDevice (services_discovery_t *sd, struct udev_device *dev)
     }
     if (*dp != d) /* Overwrite existing device */
     {
-        services_discovery_RemoveItem (sd, (*dp)->item);
         DestroyDevice (*dp);
         *dp = d;
     }
 
     name = p_sys->subsys->get_cat (dev);
     services_discovery_AddItem (sd, item, name ? name : "Generic");
+    d->sd = sd;
     free (name);
     return 0;
 }
@@ -167,20 +207,10 @@ static void RemoveDevice (services_discovery_t *sd, struct udev_device *dev)
         return;
 
     struct device *d = *dp;
-    services_discovery_RemoveItem (sd, d->item);
     tdelete (d, &p_sys->root, cmpdev);
     DestroyDevice (d);
 }
 
-static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
-                          bool add)
-{
-    if (!add)
-        RemoveDevice (sd, dev);
-    else
-        AddDevice (sd, dev);
-}
-
 static void *Run (void *);
 
 /**
@@ -230,7 +260,7 @@ static int Open (vlc_object_t *obj, const struct subsys *subsys)
     {
         const char *path = udev_list_entry_get_name (deventry);
         struct udev_device *dev = udev_device_new_from_syspath (udev, path);
-        HandleDevice (sd, dev, true);
+        AddDevice (sd, dev);
         udev_device_unref (dev);
     }
     udev_enumerate_unref (devenum);
@@ -289,18 +319,23 @@ static void *Run (void *data)
             if (errno != EINTR)
                 break;
 
+        int canc = vlc_savecancel ();
         struct udev_device *dev = udev_monitor_receive_device (mon);
         if (dev == NULL)
             continue;
 
-        /* FIXME: handle change, offline, online */
         const char *action = udev_device_get_action (dev);
         if (!strcmp (action, "add"))
-            HandleDevice (sd, dev, true);
+            AddDevice (sd, dev);
         else if (!strcmp (action, "remove"))
-            HandleDevice (sd, dev, false);
-
+            RemoveDevice (sd, dev);
+        else if (!strcmp (action, "change"))
+        {
+            RemoveDevice (sd, dev);
+            AddDevice (sd, dev);
+        }
         udev_device_unref (dev);
+        vlc_restorecancel (canc);
     }
     return NULL;
 }
@@ -355,6 +390,7 @@ static char *decode_property (struct udev_device *dev, const char *name)
     return decode (udev_device_get_property_value (dev, name));
 }
 
+
 /*** Video4Linux support ***/
 static bool is_v4l_legacy (struct udev_device *dev)
 {
@@ -398,6 +434,74 @@ int OpenV4L (vlc_object_t *obj)
     return Open (obj, &subsys);
 }
 
+
+/*** Advanced Linux Sound Architecture support ***/
+static int alsa_get_device (struct udev_device *dev, unsigned *restrict pcard,
+                            unsigned *restrict pdevice)
+{
+    const char *node = udev_device_get_devnode (dev);
+    char type;
+
+    if (node == NULL)
+        return -1;
+    if (sscanf (node, "/dev/snd/pcmC%uD%u%c", pcard, pdevice, &type) < 3)
+        return -1;
+    if (type != 'c')
+        return -1;
+    return 0;
+}
+
+
+static char *alsa_get_mrl (struct udev_device *dev)
+{
+    /* Determine media location */
+    char *mrl;
+    unsigned card, device;
+
+    if (alsa_get_device (dev, &card, &device))
+        return NULL;
+
+    if (asprintf (&mrl, "alsa://plughw:%u,%u", card, device) == -1)
+        mrl = NULL;
+    return mrl;
+}
+
+static char *alsa_get_name (struct udev_device *dev)
+{
+    char *name;
+    unsigned card, device;
+
+    if (alsa_get_device (dev, &card, &device))
+        return NULL;
+
+    if (asprintf (&name, _("Device %u"), device) == -1)
+        name = NULL;
+    return name;
+}
+
+static char *alsa_get_cat (struct udev_device *dev)
+{
+    char *name;
+    unsigned card, device;
+
+    if (alsa_get_device (dev, &card, &device))
+        return NULL;
+
+    if (asprintf (&name, _("Card %u"), card) == -1)
+        name = NULL;
+    return name;
+}
+
+int OpenALSA (vlc_object_t *obj)
+{
+    static const struct subsys subsys = {
+        "sound", alsa_get_mrl, alsa_get_name, alsa_get_cat, ITEM_TYPE_CARD,
+    };
+
+    return Open (obj, &subsys);
+}
+
+
 /*** Discs support ***/
 static char *disc_get_mrl (struct udev_device *dev)
 {