[pulseaudio-commits] [Git][pulseaudio/pulseaudio][master] 2 commits: alsa: mixer - more clever alias cache implementation

PulseAudio Marge Bot (@pulseaudio-merge-bot) gitlab at gitlab.freedesktop.org
Wed Dec 29 16:16:15 UTC 2021



PulseAudio Marge Bot pushed to branch master at PulseAudio / pulseaudio


Commits:
f5c8b82c by Jaroslav Kysela at 2021-12-29T16:13:43+00:00
alsa: mixer - more clever alias cache implementation

The hw: device can be addressed using the card index (hw:0)
or the card identifier (ASCII string - hw:Loopback). Both
mixers are equal.

The previous code was fine for the mixers without the UCM
private prefixes (_ucmXXXX). Make code more robust, create
two aliased mixer structures in the mixers array.

Signed-off-by: Jaroslav Kysela <perex at perex.cz>
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/673>

- - - - -
bc3a07dd by Jaroslav Kysela at 2021-12-29T16:13:43+00:00
alsa: ucm - use possible mixer private device prefix for ELD controls

If UCM defines the private alsa-lib configuration, the ELD controls
are expected to use this device configuration too.

With this change:

  I: [pulseaudio] alsa-util.c: Successfully attached to mixer '_ucm0009.hw:Loopback'

Without:

  I: [pulseaudio] alsa-util.c: Successfully attached to mixer '_ucm0009.hw:Loopback'
  I: [pulseaudio] alsa-util.c: Successfully attached to mixer 'hw:4'

Signed-off-by: Jaroslav Kysela <perex at perex.cz>
Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/673>

- - - - -


4 changed files:

- src/modules/alsa/alsa-mixer.h
- src/modules/alsa/alsa-ucm.c
- src/modules/alsa/alsa-ucm.h
- src/modules/alsa/alsa-util.c


Changes:

=====================================
src/modules/alsa/alsa-mixer.h
=====================================
@@ -103,8 +103,8 @@ struct pa_alsa_setting {
 
 /* An entry for one ALSA mixer */
 struct pa_alsa_mixer {
+    struct pa_alsa_mixer *alias;
     snd_mixer_t *mixer_handle;
-    int card_index;
     pa_alsa_fdlist *fdl;
     bool used_for_probe_only:1;
 };


=====================================
src/modules/alsa/alsa-ucm.c
=====================================
@@ -794,6 +794,14 @@ int pa_alsa_ucm_query_profiles(pa_alsa_ucm_config *ucm, int card_index) {
 
     pa_log_info("UCM available for card %s", card_name);
 
+    if (snd_use_case_get(ucm->ucm_mgr, "_alibpref", &value) == 0) {
+        if (value[0]) {
+            ucm->alib_prefix = pa_xstrdup(value);
+            pa_log_debug("UCM _alibpref=%s", ucm->alib_prefix);
+        }
+        free((void *)value);
+    }
+
     /* get a list of all UCM verbs (profiles) for this card */
     num_verbs = snd_use_case_verb_list(ucm->ucm_mgr, &verb_list);
     if (num_verbs < 0) {
@@ -1554,15 +1562,11 @@ static pa_alsa_mapping* ucm_alsa_mapping_get(pa_alsa_ucm_config *ucm, pa_alsa_pr
     pa_alsa_mapping *m;
     char *mapping_name;
     size_t ucm_alibpref_len = 0;
-    const char *value;
 
     /* find private alsa-lib's configuration device prefix */
-    if (snd_use_case_get(ucm->ucm_mgr, "_alibpref", &value) == 0) {
-        if (value[0] && pa_startswith(device_str, value))
-            ucm_alibpref_len = strlen(value);
 
-        free((void *)value);
-    }
+    if (ucm->alib_prefix && pa_startswith(device_str, ucm->alib_prefix))
+        ucm_alibpref_len = strlen(ucm->alib_prefix);
 
     mapping_name = pa_sprintf_malloc("Mapping %s: %s: %s", verb_name, device_str + ucm_alibpref_len, is_sink ? "sink" : "source");
 
@@ -1884,7 +1888,7 @@ static void mapping_init_eld(pa_alsa_mapping *m, snd_pcm_t *pcm)
     pa_alsa_ucm_mapping_context *context = &m->ucm_context;
     pa_alsa_ucm_device *dev;
     uint32_t idx;
-    char *mdev;
+    char *mdev, *alib_prefix;
     snd_pcm_info_t *info;
     int pcm_card, pcm_device;
 
@@ -1897,8 +1901,10 @@ static void mapping_init_eld(pa_alsa_mapping *m, snd_pcm_t *pcm)
     if ((pcm_device = snd_pcm_info_get_device(info)) < 0)
         return;
 
+    alib_prefix = context->ucm->alib_prefix;
+
     PA_IDXSET_FOREACH(dev, context->ucm_devices, idx) {
-       mdev = pa_sprintf_malloc("hw:%i", pcm_card);
+       mdev = pa_sprintf_malloc("%shw:%i", alib_prefix ? alib_prefix : "", pcm_card);
        if (mdev == NULL)
            continue;
        dev->eld_mixer_device_name = mdev;
@@ -2164,6 +2170,8 @@ void pa_alsa_ucm_free(pa_alsa_ucm_config *ucm) {
         snd_use_case_mgr_close(ucm->ucm_mgr);
         ucm->ucm_mgr = NULL;
     }
+    pa_xfree(ucm->alib_prefix);
+    ucm->alib_prefix = NULL;
 }
 
 void pa_alsa_ucm_mapping_context_free(pa_alsa_ucm_mapping_context *context) {


=====================================
src/modules/alsa/alsa-ucm.h
=====================================
@@ -253,6 +253,7 @@ struct pa_alsa_ucm_config {
     pa_core *core;
     snd_use_case_mgr_t *ucm_mgr;
     pa_alsa_ucm_verb *active_verb;
+    char *alib_prefix;
 
     pa_hashmap *mixers;
     PA_LLIST_HEAD(pa_alsa_ucm_verb, verbs);


=====================================
src/modules/alsa/alsa-util.c
=====================================
@@ -1687,14 +1687,14 @@ static int mixer_class_event(snd_mixer_class_t *class, unsigned int mask,
     return 0;
 }
 
-static int prepare_mixer(snd_mixer_t *mixer, const char *dev) {
+static int prepare_mixer(snd_mixer_t *mixer, const char *dev, snd_hctl_t *hctl) {
     int err;
     snd_mixer_class_t *class;
 
     pa_assert(mixer);
     pa_assert(dev);
 
-    if ((err = snd_mixer_attach(mixer, dev)) < 0) {
+    if ((err = snd_mixer_attach_hctl(mixer, hctl)) < 0) {
         pa_log_info("Unable to attach to mixer %s: %s", dev, pa_alsa_strerror(err));
         return -1;
     }
@@ -1733,37 +1733,29 @@ snd_mixer_t *pa_alsa_open_mixer(pa_hashmap *mixers, int alsa_card_index, bool pr
     return m;
 }
 
+pa_alsa_mixer *pa_alsa_create_mixer(pa_hashmap *mixers, const char *dev, snd_mixer_t *m, bool probe) {
+    pa_alsa_mixer *pm;
+
+    pm = pa_xnew0(pa_alsa_mixer, 1);
+    if (pm == NULL)
+        return NULL;
+
+    pm->used_for_probe_only = probe;
+    pm->mixer_handle = m;
+    pa_hashmap_put(mixers, pa_xstrdup(dev), pm);
+    return pm;
+}
+
 snd_mixer_t *pa_alsa_open_mixer_by_name(pa_hashmap *mixers, const char *dev, bool probe) {
     int err;
     snd_mixer_t *m;
+    snd_hctl_t *hctl;
     pa_alsa_mixer *pm;
-    char *dev2;
-    void *state;
 
     pa_assert(mixers);
     pa_assert(dev);
 
     pm = pa_hashmap_get(mixers, dev);
-
-    /* The quick card number/index lookup (hw:#)
-     * We already know the card number/index, thus use the mixer
-     * from the cache at first.
-     */
-    if (!pm && pa_strneq(dev, "hw:", 3)) {
-        const char *s = dev + 3;
-        int card_index;
-        while (*s && *s >= '0' && *s <= '9') s++;
-        if (*s == '\0' && pa_atoi(dev + 3, &card_index) >= 0) {
-            PA_HASHMAP_FOREACH_KV(dev2, pm, mixers, state) {
-                if (pm->card_index == card_index) {
-                    dev = dev2;
-                    pm = pa_hashmap_get(mixers, dev);
-                    break;
-                }
-            }
-        }
-    }
-
     if (pm) {
         if (!probe)
             pm->used_for_probe_only = false;
@@ -1775,27 +1767,55 @@ snd_mixer_t *pa_alsa_open_mixer_by_name(pa_hashmap *mixers, const char *dev, boo
         return NULL;
     }
 
-    if (prepare_mixer(m, dev) >= 0) {
-        pm = pa_xnew0(pa_alsa_mixer, 1);
-        if (pm) {
-            snd_hctl_t *hctl;
-            pm->card_index = -1;
-            /* determine the ALSA card number (index) and store it to card_index */
-            err = snd_mixer_get_hctl(m, dev, &hctl);
-            if (err >= 0) {
-                snd_ctl_card_info_t *info;
-                snd_ctl_card_info_alloca(&info);
-                err = snd_ctl_card_info(snd_hctl_ctl(hctl), info);
-                if (err >= 0)
-                    pm->card_index = snd_ctl_card_info_get_card(info);
+    err = snd_hctl_open(&hctl, dev, 0);
+    if (err < 0) {
+        pa_log("Error opening hctl device: %s", pa_alsa_strerror(err));
+        goto __close;
+    }
+
+    if (prepare_mixer(m, dev, hctl) >= 0) {
+        /* get the ALSA card number (index) and ID (alias) and create two identical mixers */
+        char *p, *dev2, *dev_idx, *dev_id;
+        snd_ctl_card_info_t *info;
+        snd_ctl_card_info_alloca(&info);
+        err = snd_ctl_card_info(snd_hctl_ctl(hctl), info);
+        if (err < 0)
+            goto __std;
+        dev2 = pa_xstrdup(dev);
+        if (dev2 == NULL)
+            goto __close;
+        p = strchr(dev2, ':');
+        /* sanity check - only hw: devices */
+        if (p == NULL || (p - dev2) < 2 || !pa_strneq(p - 2, "hw:", 3)) {
+            pa_xfree(dev2);
+            goto __std;
+        }
+        *p = '\0';
+        dev_idx = pa_sprintf_malloc("%s:%u", dev2, snd_ctl_card_info_get_card(info));
+        dev_id = pa_sprintf_malloc("%s:%s", dev2, snd_ctl_card_info_get_id(info));
+        pa_log_debug("ALSA alias mixers: %s = %s", dev_idx, dev_id);
+        if (dev_idx && dev_id && (strcmp(dev, dev_idx) == 0 || strcmp(dev, dev_id) == 0)) {
+            pm = pa_alsa_create_mixer(mixers, dev_idx, m, probe);
+            if (pm) {
+                pa_alsa_mixer *pm2;
+                pm2 = pa_alsa_create_mixer(mixers, dev_id, m, probe);
+                if (pm2) {
+                    pm->alias = pm2;
+                    pm2->alias = pm;
+                }
             }
-            pm->used_for_probe_only = probe;
-            pm->mixer_handle = m;
-            pa_hashmap_put(mixers, pa_xstrdup(dev), pm);
-            return m;
         }
+        pa_xfree(dev_id);
+        pa_xfree(dev_idx);
+        pa_xfree(dev2);
+   __std:
+        if (pm == NULL)
+            pm = pa_alsa_create_mixer(mixers, dev, m, probe);
+        if (pm)
+            return m;
     }
 
+__close:
     snd_mixer_close(m);
     return NULL;
 }
@@ -1836,8 +1856,10 @@ void pa_alsa_mixer_free(pa_alsa_mixer *mixer)
 {
     if (mixer->fdl)
         pa_alsa_fdlist_free(mixer->fdl);
-    if (mixer->mixer_handle)
+    if (mixer->mixer_handle && mixer->alias == NULL)
         snd_mixer_close(mixer->mixer_handle);
+    if (mixer->alias)
+        mixer->alias->alias = NULL;
     pa_xfree(mixer);
 }
 



View it on GitLab: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/compare/1883355f1b4a00c44dc80f149e0ca6a1af4e9eac...bc3a07dd4f4cd62166c214085cced42d5a2ae2a8

-- 
View it on GitLab: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/compare/1883355f1b4a00c44dc80f149e0ca6a1af4e9eac...bc3a07dd4f4cd62166c214085cced42d5a2ae2a8
You're receiving this email because of your account on gitlab.freedesktop.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/pulseaudio-commits/attachments/20211229/4393d9a9/attachment-0001.htm>


More information about the pulseaudio-commits mailing list