@@ -169,6 +169,12 @@ struct snd_pcm_ioplug_callback {
int (*prepare)(snd_pcm_ioplug_t *io);
/**
* drain; optional
+ * This function should never block. It should return -EAGAIN in case of
+ * it is not yet done. In this case it will be called again by the ALSA
+ * library (in blocking mode) or by the user (in non blocking mode).
+ * In case of -EAGAIN the poll_descriptors have to be used to inform the
+ * ALSA library that draining is possibly done and this callback has to
+ * be called again.
*/
int (*drain)(snd_pcm_ioplug_t *io);
/**
@@ -492,17 +492,37 @@ static int snd_pcm_ioplug_drop(snd_pcm_t *pcm)
static int snd_pcm_ioplug_drain(snd_pcm_t *pcm)
{
ioplug_priv_t *io = pcm->private_data;
- int err;
+ int err = 0;
if (io->data->state == SND_PCM_STATE_OPEN)
return -EBADFD;
io->data->state = SND_PCM_STATE_DRAINING;
- if (io->data->callback->drain)
- io->data->callback->drain(io->data);
- snd_pcm_lock(pcm);
- err = snd_pcm_ioplug_drop(pcm);
- snd_pcm_unlock(pcm);
+ if (io->data->callback->drain) {
+ err = io->data->callback->drain(io->data);
+
+ /* in blocking mode wait unti draining is done or
+ * an issue was detected.
+ */
+ if (!io->data->nonblock) {
+ while (err == -EAGAIN) {
+ snd_pcm_lock(pcm);
+ err = snd_pcm_wait_nocheck(pcm, -1);
+ snd_pcm_unlock(pcm);
+ if (err < 0)
+ break;
+ err = io->data->callback->drain(io->data);
+ }
+ }
+ }
+
+ /* only drop if draining is done or there was an issue */
+ if (err != -EAGAIN) {
+ snd_pcm_lock(pcm);
+ err = snd_pcm_ioplug_drop(pcm);
+ snd_pcm_unlock(pcm);
+ }
+
return err;
}