]> code.delx.au - pulseaudio/blobdiff - src/modules/alsa/alsa-util.c
alsa: Use card description in default sink/source prefix when available
[pulseaudio] / src / modules / alsa / alsa-util.c
index 602e9a366f6321f8f21af694ed4e33b8651bb8f4..dca1f2e4c54d43554a15cfd3f9fffad58305e305 100644 (file)
 #include "alsa-util.h"
 #include "alsa-mixer.h"
 
-#ifdef HAVE_HAL
-#include "hal-util.h"
-#endif
-
 #ifdef HAVE_UDEV
 #include "udev-util.h"
 #endif
@@ -195,17 +191,17 @@ int pa_alsa_set_hw_params(
         snd_pcm_uframes_t *period_size,
         snd_pcm_uframes_t *buffer_size,
         snd_pcm_uframes_t tsched_size,
-        pa_bool_t *use_mmap,
-        pa_bool_t *use_tsched,
-        pa_bool_t require_exact_channel_number) {
+        bool *use_mmap,
+        bool *use_tsched,
+        bool require_exact_channel_number) {
 
     int ret = -1;
     snd_pcm_hw_params_t *hwparams, *hwparams_copy;
     int dir;
     snd_pcm_uframes_t _period_size = period_size ? *period_size : 0;
     snd_pcm_uframes_t _buffer_size = buffer_size ? *buffer_size : 0;
-    pa_bool_t _use_mmap = use_mmap && *use_mmap;
-    pa_bool_t _use_tsched = use_tsched && *use_tsched;
+    bool _use_mmap = use_mmap && *use_mmap;
+    bool _use_tsched = use_tsched && *use_tsched;
     pa_sample_spec _ss = *ss;
 
     pa_assert(pcm_handle);
@@ -235,7 +231,7 @@ int pa_alsa_set_hw_params(
                 goto finish;
             }
 
-            _use_mmap = FALSE;
+            _use_mmap = false;
         }
 
     } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
@@ -244,10 +240,16 @@ int pa_alsa_set_hw_params(
     }
 
     if (!_use_mmap)
-        _use_tsched = FALSE;
+        _use_tsched = false;
 
     if (!pa_alsa_pcm_is_hw(pcm_handle))
-        _use_tsched = FALSE;
+        _use_tsched = false;
+
+    /* The PCM pointer is only updated with period granularity */
+    if (snd_pcm_hw_params_is_batch(hwparams)) {
+        pa_log_info("Disabling tsched mode since BATCH flag is set");
+        _use_tsched = false;
+    }
 
 #if (SND_LIB_VERSION >= ((1<<16)|(0<<8)|24)) /* API additions in 1.0.24 */
     if (_use_tsched) {
@@ -255,7 +257,7 @@ int pa_alsa_set_hw_params(
         /* try to disable period wakeups if hardware can do so */
         if (snd_pcm_hw_params_can_disable_period_wakeup(hwparams)) {
 
-            if (snd_pcm_hw_params_set_period_wakeup(pcm_handle, hwparams, FALSE) < 0)
+            if ((ret = snd_pcm_hw_params_set_period_wakeup(pcm_handle, hwparams, false)) < 0)
                 /* don't bail, keep going with default mode with period wakeups */
                 pa_log_debug("snd_pcm_hw_params_set_period_wakeup() failed: %s", pa_alsa_strerror(ret));
             else
@@ -310,8 +312,8 @@ int pa_alsa_set_hw_params(
             pa_log_debug("Maximum hw buffer size is %lu ms", (long unsigned) (max_frames * PA_MSEC_PER_SEC / _ss.rate));
 
         /* Some ALSA drivers really don't like if we set the buffer
-         * size first and the number of periods second. (which would
-         * make a lot more sense to me) So, try a few combinations
+         * size first and the number of periods second (which would
+         * make a lot more sense to me). So, try a few combinations
          * before we give up. */
 
         if (_buffer_size > 0 && _period_size > 0) {
@@ -430,7 +432,7 @@ finish:
     return ret;
 }
 
-int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min, pa_bool_t period_event) {
+int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min, bool period_event) {
     snd_pcm_sw_params_t *swparams;
     snd_pcm_uframes_t boundary;
     int err;
@@ -491,8 +493,8 @@ snd_pcm_t *pa_alsa_open_by_device_id_auto(
         snd_pcm_uframes_t *period_size,
         snd_pcm_uframes_t *buffer_size,
         snd_pcm_uframes_t tsched_size,
-        pa_bool_t *use_mmap,
-        pa_bool_t *use_tsched,
+        bool *use_mmap,
+        bool *use_tsched,
         pa_alsa_profile_set *ps,
         pa_alsa_mapping **mapping) {
 
@@ -582,7 +584,7 @@ snd_pcm_t *pa_alsa_open_by_device_id_auto(
             tsched_size,
             use_mmap,
             use_tsched,
-            FALSE);
+            false);
     pa_xfree(d);
 
     if (pcm_handle && mapping)
@@ -600,8 +602,8 @@ snd_pcm_t *pa_alsa_open_by_device_id_mapping(
         snd_pcm_uframes_t *period_size,
         snd_pcm_uframes_t *buffer_size,
         snd_pcm_uframes_t tsched_size,
-        pa_bool_t *use_mmap,
-        pa_bool_t *use_tsched,
+        bool *use_mmap,
+        bool *use_tsched,
         pa_alsa_mapping *m) {
 
     snd_pcm_t *pcm_handle;
@@ -631,7 +633,7 @@ snd_pcm_t *pa_alsa_open_by_device_id_mapping(
             tsched_size,
             use_mmap,
             use_tsched,
-            TRUE);
+            pa_channel_map_valid(&m->channel_map) /* Query the channel count if we don't know what we want */);
 
     if (!pcm_handle)
         return NULL;
@@ -652,14 +654,14 @@ snd_pcm_t *pa_alsa_open_by_device_string(
         snd_pcm_uframes_t *period_size,
         snd_pcm_uframes_t *buffer_size,
         snd_pcm_uframes_t tsched_size,
-        pa_bool_t *use_mmap,
-        pa_bool_t *use_tsched,
-        pa_bool_t require_exact_channel_number) {
+        bool *use_mmap,
+        bool *use_tsched,
+        bool require_exact_channel_number) {
 
     int err;
     char *d;
     snd_pcm_t *pcm_handle;
-    pa_bool_t reformat = FALSE;
+    bool reformat = false;
 
     pa_assert(device);
     pa_assert(ss);
@@ -692,7 +694,7 @@ snd_pcm_t *pa_alsa_open_by_device_string(
                      require_exact_channel_number)) < 0) {
 
             if (!reformat) {
-                reformat = TRUE;
+                reformat = true;
 
                 snd_pcm_close(pcm_handle);
                 continue;
@@ -706,7 +708,7 @@ snd_pcm_t *pa_alsa_open_by_device_string(
                 pa_xfree(d);
                 d = t;
 
-                reformat = FALSE;
+                reformat = false;
 
                 snd_pcm_close(pcm_handle);
                 continue;
@@ -745,9 +747,9 @@ snd_pcm_t *pa_alsa_open_by_template(
         snd_pcm_uframes_t *period_size,
         snd_pcm_uframes_t *buffer_size,
         snd_pcm_uframes_t tsched_size,
-        pa_bool_t *use_mmap,
-        pa_bool_t *use_tsched,
-        pa_bool_t require_exact_channel_number) {
+        bool *use_mmap,
+        bool *use_tsched,
+        bool require_exact_channel_number) {
 
     snd_pcm_t *pcm_handle;
     char **i;
@@ -819,12 +821,12 @@ void pa_alsa_dump_status(snd_pcm_t *pcm) {
     }
 
     if ((err = snd_pcm_status_dump(status, out)) < 0) {
-        pa_log_debug("snd_pcm_dump(): %s", pa_alsa_strerror(err));
+        pa_log_debug("snd_pcm_status_dump(): %s", pa_alsa_strerror(err));
         goto finish;
     }
 
     snd_output_buffer_string(out, &s);
-    pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s));
+    pa_log_debug("snd_pcm_status_dump():\n%s", pa_strnull(s));
 
 finish:
 
@@ -866,27 +868,27 @@ void pa_alsa_refcnt_dec(void) {
     }
 }
 
-pa_bool_t pa_alsa_init_description(pa_proplist *p) {
+bool pa_alsa_init_description(pa_proplist *p, pa_card *card) {
     const char *d, *k;
     pa_assert(p);
 
-    if (pa_device_init_description(p))
-        return TRUE;
+    if (pa_device_init_description(p, card))
+        return true;
 
     if (!(d = pa_proplist_gets(p, "alsa.card_name")))
         d = pa_proplist_gets(p, "alsa.name");
 
     if (!d)
-        return FALSE;
+        return false;
 
     k = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_DESCRIPTION);
 
     if (d && k)
-        pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, _("%s %s"), d, k);
+        pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, "%s %s", d, k);
     else if (d)
         pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, d);
 
-    return FALSE;
+    return false;
 }
 
 void pa_alsa_init_proplist_card(pa_core *c, pa_proplist *p, int card) {
@@ -915,10 +917,6 @@ void pa_alsa_init_proplist_card(pa_core *c, pa_proplist *p, int card) {
 #ifdef HAVE_UDEV
     pa_udev_get_info(card, p);
 #endif
-
-#ifdef HAVE_HAL
-    pa_hal_get_info(c, p, card);
-#endif
 }
 
 void pa_alsa_init_proplist_pcm_info(pa_core *c, pa_proplist *p, snd_pcm_info_t *pcm_info) {
@@ -1149,10 +1147,11 @@ snd_pcm_sframes_t pa_alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const pa
     return n;
 }
 
-int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_size, const pa_sample_spec *ss, pa_bool_t capture) {
+int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_status_t *status, snd_pcm_sframes_t *delay, size_t hwbuf_size, const pa_sample_spec *ss,
+                       bool capture) {
     ssize_t k;
     size_t abs_k;
-    int r;
+    int err;
     snd_pcm_sframes_t avail = 0;
 
     pa_assert(pcm);
@@ -1162,10 +1161,16 @@ int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_si
 
     /* Some ALSA driver expose weird bugs, let's inform the user about
      * what is going on. We're going to get both the avail and delay values so
-     * that we can compare and check them for capture */
+     * that we can compare and check them for capture.
+     * This is done with snd_pcm_status() which provides
+     * avail, delay and timestamp values in a single kernel call to improve
+     * timer-based scheduling */
 
-    if ((r = snd_pcm_avail_delay(pcm, &avail, delay)) < 0)
-        return r;
+    if ((err = snd_pcm_status(pcm, status)) < 0)
+        return err;
+
+    avail = snd_pcm_status_get_avail(status);
+    *delay = snd_pcm_status_get_delay(status);
 
     k = (ssize_t) *delay * (ssize_t) pa_frame_size(ss);
 
@@ -1327,26 +1332,77 @@ char *pa_alsa_get_reserve_name(const char *device) {
     return pa_sprintf_malloc("Audio%i", i);
 }
 
-pa_bool_t pa_alsa_pcm_is_hw(snd_pcm_t *pcm) {
+unsigned int *pa_alsa_get_supported_rates(snd_pcm_t *pcm, unsigned int fallback_rate) {
+    static unsigned int all_rates[] = { 8000, 11025, 12000,
+                                        16000, 22050, 24000,
+                                        32000, 44100, 48000,
+                                        64000, 88200, 96000,
+                                        128000, 176400, 192000,
+                                        384000 };
+    bool supported[PA_ELEMENTSOF(all_rates)] = { false, };
+    snd_pcm_hw_params_t *hwparams;
+    unsigned int i, j, n, *rates = NULL;
+    int ret;
+
+    snd_pcm_hw_params_alloca(&hwparams);
+
+    if ((ret = snd_pcm_hw_params_any(pcm, hwparams)) < 0) {
+        pa_log_debug("snd_pcm_hw_params_any() failed: %s", pa_alsa_strerror(ret));
+        return NULL;
+    }
+
+    for (i = 0, n = 0; i < PA_ELEMENTSOF(all_rates); i++) {
+        if (snd_pcm_hw_params_test_rate(pcm, hwparams, all_rates[i], 0) == 0) {
+            supported[i] = true;
+            n++;
+        }
+    }
+
+    if (n > 0) {
+        rates = pa_xnew(unsigned int, n + 1);
+
+        for (i = 0, j = 0; i < PA_ELEMENTSOF(all_rates); i++) {
+            if (supported[i])
+                rates[j++] = all_rates[i];
+        }
+
+        rates[j] = 0;
+    } else {
+        rates = pa_xnew(unsigned int, 2);
+
+        rates[0] = fallback_rate;
+        if ((ret = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rates[0], NULL)) < 0) {
+            pa_log_debug("snd_pcm_hw_params_set_rate_near() failed: %s", pa_alsa_strerror(ret));
+            pa_xfree(rates);
+            return NULL;
+        }
+
+        rates[1] = 0;
+    }
+
+    return rates;
+}
+
+bool pa_alsa_pcm_is_hw(snd_pcm_t *pcm) {
     snd_pcm_info_t* info;
     snd_pcm_info_alloca(&info);
 
     pa_assert(pcm);
 
     if (snd_pcm_info(pcm, info) < 0)
-        return FALSE;
+        return false;
 
     return snd_pcm_info_get_card(info) >= 0;
 }
 
-pa_bool_t pa_alsa_pcm_is_modem(snd_pcm_t *pcm) {
+bool pa_alsa_pcm_is_modem(snd_pcm_t *pcm) {
     snd_pcm_info_t* info;
     snd_pcm_info_alloca(&info);
 
     pa_assert(pcm);
 
     if (snd_pcm_info(pcm, info) < 0)
-        return FALSE;
+        return false;
 
     return snd_pcm_info_get_class(info) == SND_PCM_CLASS_MODEM;
 }
@@ -1378,24 +1434,215 @@ const char* pa_alsa_strerror(int errnum) {
     return translated;
 }
 
-pa_bool_t pa_alsa_may_tsched(pa_bool_t want) {
+bool pa_alsa_may_tsched(bool want) {
 
     if (!want)
-        return FALSE;
+        return false;
 
     if (!pa_rtclock_hrtimer()) {
         /* We cannot depend on being woken up in time when the timers
         are inaccurate, so let's fallback to classic IO based playback
         then. */
         pa_log_notice("Disabling timer-based scheduling because high-resolution timers are not available from the kernel.");
-        return FALSE; }
+        return false; }
 
     if (pa_running_in_vm()) {
         /* We cannot depend on being woken up when we ask for in a VM,
          * so let's fallback to classic IO based playback then. */
         pa_log_notice("Disabling timer-based scheduling because running inside a VM.");
-        return FALSE;
+        return false;
+    }
+
+    return true;
+}
+
+snd_hctl_elem_t* pa_alsa_find_jack(snd_hctl_t *hctl, const char* jack_name) {
+    snd_ctl_elem_id_t *id;
+
+    snd_ctl_elem_id_alloca(&id);
+    snd_ctl_elem_id_clear(id);
+    snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_CARD);
+    snd_ctl_elem_id_set_name(id, jack_name);
+
+    return snd_hctl_find_elem(hctl, id);
+}
+
+snd_hctl_elem_t* pa_alsa_find_eld_ctl(snd_hctl_t *hctl, int device) {
+    snd_ctl_elem_id_t *id;
+
+    /* See if we can find the ELD control */
+    snd_ctl_elem_id_alloca(&id);
+    snd_ctl_elem_id_clear(id);
+    snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_PCM);
+    snd_ctl_elem_id_set_name(id, "ELD");
+    snd_ctl_elem_id_set_device(id, device);
+
+    return snd_hctl_find_elem(hctl, id);
+}
+
+static int prepare_mixer(snd_mixer_t *mixer, const char *dev, snd_hctl_t **hctl) {
+    int err;
+
+    pa_assert(mixer);
+    pa_assert(dev);
+
+    if ((err = snd_mixer_attach(mixer, dev)) < 0) {
+        pa_log_info("Unable to attach to mixer %s: %s", dev, pa_alsa_strerror(err));
+        return -1;
+    }
+
+    /* Note: The hctl handle returned should not be freed.
+       It is closed/freed by alsa-lib on snd_mixer_close/free */
+    if (hctl && (err = snd_mixer_get_hctl(mixer, dev, hctl)) < 0) {
+        pa_log_info("Unable to get hctl of mixer %s: %s", dev, pa_alsa_strerror(err));
+        return -1;
+    }
+
+    if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) {
+        pa_log_warn("Unable to register mixer: %s", pa_alsa_strerror(err));
+        return -1;
+    }
+
+    if ((err = snd_mixer_load(mixer)) < 0) {
+        pa_log_warn("Unable to load mixer: %s", pa_alsa_strerror(err));
+        return -1;
+    }
+
+    pa_log_info("Successfully attached to mixer '%s'", dev);
+    return 0;
+}
+
+snd_mixer_t *pa_alsa_open_mixer(int alsa_card_index, char **ctl_device, snd_hctl_t **hctl) {
+    int err;
+    snd_mixer_t *m;
+    char *md;
+    snd_pcm_info_t* info;
+    snd_pcm_info_alloca(&info);
+
+    if ((err = snd_mixer_open(&m, 0)) < 0) {
+        pa_log("Error opening mixer: %s", pa_alsa_strerror(err));
+        return NULL;
+    }
+
+    /* Then, try by card index */
+    md = pa_sprintf_malloc("hw:%i", alsa_card_index);
+    if (prepare_mixer(m, md, hctl) >= 0) {
+
+        if (ctl_device)
+            *ctl_device = md;
+        else
+            pa_xfree(md);
+
+        return m;
+    }
+
+    pa_xfree(md);
+
+    snd_mixer_close(m);
+    return NULL;
+}
+
+snd_mixer_t *pa_alsa_open_mixer_for_pcm(snd_pcm_t *pcm, char **ctl_device, snd_hctl_t **hctl) {
+    int err;
+    snd_mixer_t *m;
+    const char *dev;
+    snd_pcm_info_t* info;
+    snd_pcm_info_alloca(&info);
+
+    pa_assert(pcm);
+
+    if ((err = snd_mixer_open(&m, 0)) < 0) {
+        pa_log("Error opening mixer: %s", pa_alsa_strerror(err));
+        return NULL;
+    }
+
+    /* First, try by name */
+    if ((dev = snd_pcm_name(pcm)))
+        if (prepare_mixer(m, dev, hctl) >= 0) {
+            if (ctl_device)
+                *ctl_device = pa_xstrdup(dev);
+
+            return m;
+        }
+
+    /* Then, try by card index */
+    if (snd_pcm_info(pcm, info) >= 0) {
+        char *md;
+        int card_idx;
+
+        if ((card_idx = snd_pcm_info_get_card(info)) >= 0) {
+
+            md = pa_sprintf_malloc("hw:%i", card_idx);
+
+            if (!dev || !pa_streq(dev, md))
+                if (prepare_mixer(m, md, hctl) >= 0) {
+
+                    if (ctl_device)
+                        *ctl_device = md;
+                    else
+                        pa_xfree(md);
+
+                    return m;
+                }
+
+            pa_xfree(md);
+        }
+    }
+
+    snd_mixer_close(m);
+    return NULL;
+}
+
+int pa_alsa_get_hdmi_eld(snd_hctl_t *hctl, int device, pa_hdmi_eld *eld) {
+
+    /* The ELD format is specific to HDA Intel sound cards and defined in the
+       HDA specification: http://www.intel.com/content/www/us/en/standards/high-definition-audio-specification.html */
+    int err;
+    snd_hctl_elem_t *elem;
+    snd_ctl_elem_info_t *info;
+    snd_ctl_elem_value_t *value;
+    uint8_t *elddata;
+    unsigned int eldsize, mnl;
+
+    pa_assert(eld != NULL);
+
+    /* See if we can find the ELD control */
+    elem = pa_alsa_find_eld_ctl(hctl, device);
+    if (elem == NULL) {
+        pa_log_debug("No ELD info control found (for device=%d)", device);
+        return -1;
+    }
+
+    /* Does it have any contents? */
+    snd_ctl_elem_info_alloca(&info);
+    snd_ctl_elem_value_alloca(&value);
+    if ((err = snd_hctl_elem_info(elem, info)) < 0 ||
+       (err = snd_hctl_elem_read(elem, value)) < 0) {
+        pa_log_warn("Accessing ELD control failed with error %s", snd_strerror(err));
+        return -1;
+    }
+
+    eldsize = snd_ctl_elem_info_get_count(info);
+    elddata = (unsigned char *) snd_ctl_elem_value_get_bytes(value);
+    if (elddata == NULL || eldsize == 0) {
+        pa_log_debug("ELD info empty (for device=%d)", device);
+        return -1;
+    }
+    if (eldsize < 20 || eldsize > 256) {
+        pa_log_debug("ELD info has wrong size (for device=%d)", device);
+        return -1;
     }
 
-    return TRUE;
+    /* Try to fetch monitor name */
+    mnl = elddata[4] & 0x1f;
+    if (mnl == 0 || mnl > 16 || 20 + mnl > eldsize) {
+        pa_log_debug("No monitor name in ELD info (for device=%d)", device);
+        mnl = 0;
+    }
+    memcpy(eld->monitor_name, &elddata[20], mnl);
+    eld->monitor_name[mnl] = '\0';
+    if (mnl)
+        pa_log_debug("Monitor name in ELD info is '%s' (for device=%d)", eld->monitor_name, device);
+
+    return 0;
 }