Re: [OT] ALSA userspace API complexity

From: Heikki Orsila
Date: Thu Jan 05 2006 - 09:01:02 EST


Alistair John Strachan <s0348365@xxxxxxxxxxxx> wrote:
> On Tuesday 03 January 2006 23:10, Tomasz K?oczko wrote:
> 2) ALSA API is to complicated: most applications opens single sound
> stream.

> FUD and nonsense. I've written many DSP applications and very often I can
> recycle the same code for use in them.

It's not FUD and it's very true. I've written ALSA support for many
programs and I still can't remember the tricks that are needed to get
even basic things working.

Just look at error handling of libao-0.8.6 for a great example how
complicated it is to write code for ALSA. The following code is directly
from the source:

/* try to write the entire buffer at once */
err = internal->writei(internal->pcm_handle, ptr, len);

/* it's possible that no data was transferred */
if (err == -EAGAIN) {
continue;
}

if (err < 0) {
/* this might be an error, or an exception */
err = alsa_error_recovery(internal, err);
if (err < 0) {
fprintf(stderr,"ALSA write error: %s\n",
snd_strerror(err));
return 0;
}

/* abandon the rest of the buffer */
break;
}

alsa_error_recovery() expands to 30 lines of more logic. This is pretty
insane considering that libao _only_ writes data to device and does
nothing else. If ALSA was done properly, the main loop would simply be:

err = internal->writei(internal->pcm_handle, ptr, len);

/* it's possible that no data was transferred */
if (err == -EAGAIN || err == -EINTR) {
continue;
}

if (err < 0) {
/* BAD BAD */
}

When I originally ran into this signal handling brain damage of ALSA, I
had to actually look into other programs how they handle signals because
ALSA documentation is so bad. The core problem is of course the broken
API, not the bad documentation.

A small note, libao can not handle EINTR properly. A patch has been
submitted already.

I've long ago stopped using ALSA API because it is broken. But if you
wanted to make ALSA usable by real people you might considering adding 3
functions (there are ~300 already so not much loss):

err = alsa_simple_pcm_open(nchannels, sampleformat, samplingrate, frames_in_period /* 0 for automated default */ );
err = alsa_simple_writei(); /* handless signal brokeness automagically */
alsa_simple_close();

Basically ogg123/mpg123 like applications would only need 3 alsa calls.
Now everyone reimplementing their own buggy versions of simple mechanisms.

--
Heikki Orsila Barbie's law:
heikki.orsila@xxxxxx "Math is hard, let's go shopping!"
http://www.iki.fi/shd
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/