#endif
#include <sys/types.h>
-#include <limits.h>
#include <asoundlib.h>
#include <pulse/sample.h>
#include <pulse/xmalloc.h>
#include <pulse/timeval.h>
#include <pulse/util.h>
-#include <pulse/i18n.h>
#include <pulse/utf8.h>
+#include <pulsecore/i18n.h>
#include <pulsecore/log.h>
#include <pulsecore/macro.h>
#include <pulsecore/core-util.h>
#include <pulsecore/atomic.h>
#include <pulsecore/core-error.h>
-#include <pulsecore/once.h>
#include <pulsecore/thread.h>
#include <pulsecore/conf-parser.h>
#include <pulsecore/core-rtclock.h>
#include "alsa-util.h"
#include "alsa-mixer.h"
-#ifdef HAVE_HAL
-#include "hal-util.h"
-#endif
-
#ifdef HAVE_UDEV
#include "udev-util.h"
#endif
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);
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) {
}
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) {
+
+ /* try to disable period wakeups if hardware can do so */
+ if (snd_pcm_hw_params_can_disable_period_wakeup(hwparams)) {
+
+ 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
+ pa_log_info("Trying to disable ALSA period wakeups, using timers only");
+ } else
+ pa_log_info("cannot disable ALSA period wakeups");
+ }
+#endif
if ((ret = set_format(pcm_handle, hwparams, &_ss.format)) < 0)
goto finish;
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) {
goto success;
}
+ snd_pcm_hw_params_copy(hwparams_copy, hwparams);
/* Second try: set period size first, followed by buffer size */
if (set_period_size(pcm_handle, hwparams_copy, _period_size) >= 0 &&
set_buffer_size(pcm_handle, hwparams_copy, _buffer_size) >= 0 &&
pa_log_debug("Set neither period nor buffer size.");
/* Last chance, set nothing */
- if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
+ if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
pa_log_info("snd_pcm_hw_params failed: %s", pa_alsa_strerror(ret));
goto finish;
}
goto finish;
}
+#if (SND_LIB_VERSION >= ((1<<16)|(0<<8)|24)) /* API additions in 1.0.24 */
+ if (_use_tsched) {
+ unsigned int no_wakeup;
+ /* see if period wakeups were disabled */
+ snd_pcm_hw_params_get_period_wakeup(pcm_handle, hwparams, &no_wakeup);
+ if (no_wakeup == 0)
+ pa_log_info("ALSA period wakeups disabled");
+ else
+ pa_log_info("ALSA period wakeups were not disabled");
+ }
+#endif
+
ss->rate = _ss.rate;
ss->channels = _ss.channels;
ss->format = _ss.format;
ret = 0;
- snd_pcm_nonblock(pcm_handle, 1);
-
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;
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) {
tsched_size,
use_mmap,
use_tsched,
- FALSE);
+ false);
pa_xfree(d);
if (pcm_handle && 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;
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;
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);
require_exact_channel_number)) < 0) {
if (!reformat) {
- reformat = TRUE;
+ reformat = true;
snd_pcm_close(pcm_handle);
continue;
pa_xfree(d);
d = t;
- reformat = FALSE;
+ reformat = false;
snd_pcm_close(pcm_handle);
continue;
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;
}
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:
}
}
-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) {
pa_proplist_setf(p, "alsa.card", "%i", card);
if (snd_card_get_name(card, &cn) >= 0) {
- pa_proplist_sets(p, "alsa.card_name", cn);
+ pa_proplist_sets(p, "alsa.card_name", pa_strip(cn));
free(cn);
}
if (snd_card_get_longname(card, &lcn) >= 0) {
- pa_proplist_sets(p, "alsa.long_card_name", lcn);
+ pa_proplist_sets(p, "alsa.long_card_name", pa_strip(lcn));
free(lcn);
}
#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) {
if (alsa_subclass_table[subclass])
pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]);
- if ((n = snd_pcm_info_get_name(pcm_info)))
- pa_proplist_sets(p, "alsa.name", n);
+ if ((n = snd_pcm_info_get_name(pcm_info))) {
+ char *t = pa_xstrdup(n);
+ pa_proplist_sets(p, "alsa.name", pa_strip(t));
+ pa_xfree(t);
+ }
if ((id = snd_pcm_info_get_id(pcm_info)))
pa_proplist_sets(p, "alsa.id", id);
k = (size_t) n * pa_frame_size(ss);
- if (k >= hwbuf_size * 5 ||
- k >= pa_bytes_per_second(ss)*10) {
+ if (PA_UNLIKELY(k >= hwbuf_size * 5 ||
+ k >= pa_bytes_per_second(ss)*10)) {
PA_ONCE_BEGIN {
char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
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) {
+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);
pa_assert(delay);
pa_assert(ss);
/* Some ALSA driver expose weird bugs, let's inform the user about
- * what is going on */
+ * 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.
+ * 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_delay(pcm, 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);
abs_k = k >= 0 ? (size_t) k : (size_t) -k;
- if (abs_k >= hwbuf_size * 5 ||
- abs_k >= pa_bytes_per_second(ss)*10) {
+ if (PA_UNLIKELY(abs_k >= hwbuf_size * 5 ||
+ abs_k >= pa_bytes_per_second(ss)*10)) {
PA_ONCE_BEGIN {
char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
*delay = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss));
}
+ if (capture) {
+ abs_k = (size_t) avail * pa_frame_size(ss);
+
+ if (PA_UNLIKELY(abs_k >= hwbuf_size * 5 ||
+ abs_k >= pa_bytes_per_second(ss)*10)) {
+
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_avail() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (unsigned long) k,
+ (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC),
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ /* Mhmm, let's try not to fail completely */
+ avail = (snd_pcm_sframes_t) (hwbuf_size / pa_frame_size(ss));
+ }
+
+ if (PA_UNLIKELY(*delay < avail)) {
+ PA_ONCE_BEGIN {
+ char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
+ pa_log(_("snd_pcm_avail_delay() returned strange values: delay %lu is less than avail %lu.\n"
+ "Most likely this is a bug in the ALSA driver '%s'. Please report this issue to the ALSA developers."),
+ (unsigned long) *delay,
+ (unsigned long) avail,
+ pa_strnull(dn));
+ pa_xfree(dn);
+ pa_alsa_dump(PA_LOG_ERROR, pcm);
+ } PA_ONCE_END;
+
+ /* try to fixup */
+ *delay = avail;
+ }
+ }
+
return 0;
}
k = (size_t) *frames * pa_frame_size(ss);
- if (*frames > before ||
- k >= hwbuf_size * 3 ||
- k >= pa_bytes_per_second(ss)*10)
-
+ if (PA_UNLIKELY(*frames > before ||
+ k >= hwbuf_size * 3 ||
+ k >= pa_bytes_per_second(ss)*10))
PA_ONCE_BEGIN {
char *dn = pa_alsa_get_driver_name_by_pcm(pcm);
pa_log(_("snd_pcm_mmap_begin() returned a value that is exceptionally large: %lu bytes (%lu ms).\n"
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;
}
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;
- return TRUE;
+ 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;
+ }
+
+ /* 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;
}