How to handle return code of ALSA API snd_pcm_writei()

4.3k views Asked by At

I am using ASLA Audio API for playing sound in my embedded linux application. Am observing at times that the snd_pcm_writei API returns –EPIPE error and when that error happens I call snd_pcm_prepare (that basically prepares PCM for re-use).

if ((err = snd_pcm_writei (playback_handle, buf, nframes)) < 0) 
{
    if ((err = snd_pcm_prepare (playback_handle)) < 0)
    return err;
}

I found some suggestions that instead of snd_pcm_prepare using snd_pcm_recover should be the correct approach in this instance. Before trying the fix I just wanted to know if anybody can help me out in understanding how we to set the PCM device to recover from the underrun or other errors that can happen during write.

1

There are 1 answers

11
CL. On BEST ANSWER

The snd_pcm_recover() function handles more error codes, so you should use it. But it ends up calling snd_pcm_prepare() anyway (see the source code).

There's nothing really special about snd_pcm_prepare(); it's just the simplest way of reinitializing the stream.