From a2251fad43410ae4cd9b515173f024adb99f3455 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Thu, 1 Sep 2005 10:58:13 +0000 Subject: [PATCH] Fix suspend/resume with aoss Fixed suspend/resume with aoss. Clean up error handling codes. --- alsa/pcm.c | 67 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/alsa/pcm.c b/alsa/pcm.c index 9e63adc..93ffd50 100644 --- a/alsa/pcm.c +++ b/alsa/pcm.c @@ -617,6 +617,31 @@ static int oss_dsp_open(int card, int device, int oflag, mode_t mode ATTRIBUTE_U return -1; } +static int xrun(snd_pcm_t *pcm) +{ + switch (snd_pcm_state(pcm)) { + case SND_PCM_STATE_XRUN: + return snd_pcm_prepare(pcm); + case SND_PCM_STATE_DRAINING: + if (snd_pcm_stream(pcm) == SND_PCM_STREAM_CAPTURE) + return snd_pcm_prepare(pcm); + break; + default: + break; + } + return -EIO; +} + +static int resume(snd_pcm_t *pcm) +{ + int res; + while ((res = snd_pcm_resume(pcm)) == -EAGAIN) + sleep(1); + if (! res) + return 0; + return snd_pcm_prepare(pcm); +} + ssize_t lib_oss_pcm_write(int fd, const void *buf, size_t n) { ssize_t result; @@ -640,25 +665,20 @@ ssize_t lib_oss_pcm_write(int fd, const void *buf, size_t n) frames = n / str->frame_bytes; _again: result = snd_pcm_writei(pcm, buf, frames); - if (result == -EPIPE && - snd_pcm_state(pcm) == SND_PCM_STATE_XRUN && - (result = snd_pcm_prepare(pcm)) == 0) - goto _again; - if (result == -EPIPE && - snd_pcm_state(pcm) == SND_PCM_STATE_SUSPENDED) { - while ((result = snd_pcm_resume(pcm)) == -EAGAIN) - sleep(1); - if (result < 0 && (result = snd_pcm_prepare(pcm)) == 0) - goto _again; + if (result == -EPIPE) { + if (! (result = xrun(pcm))) + goto _again; + } else if (result == -ESTRPIPE) { + if (! (result = resume(pcm))) + goto _again; } if (result < 0) { errno = -result; result = -1; goto _end; - } else { - str->alsa.appl_ptr += result; - str->alsa.appl_ptr %= str->alsa.boundary; } + str->alsa.appl_ptr += result; + str->alsa.appl_ptr %= str->alsa.boundary; result *= str->frame_bytes; str->oss.bytes += result; _end: @@ -693,25 +713,20 @@ ssize_t lib_oss_pcm_read(int fd, void *buf, size_t n) frames = n / str->frame_bytes; _again: result = snd_pcm_readi(pcm, buf, frames); - if (result == -EPIPE && - snd_pcm_state(pcm) == SND_PCM_STATE_XRUN && - (result = snd_pcm_prepare(pcm)) == 0) - goto _again; - if (result == -EPIPE && - snd_pcm_state(pcm) == SND_PCM_STATE_SUSPENDED) { - while ((result = snd_pcm_resume(pcm)) == -EAGAIN) - sleep(1); - if (result < 0 && (result = snd_pcm_prepare(pcm)) == 0) - goto _again; + if (result == -EPIPE) { + if (! (result = xrun(pcm))) + goto _again; + } else if (result == -ESTRPIPE) { + if (! (result = resume(pcm))) + goto _again; } if (result < 0) { errno = -result; result = -1; goto _end; - } else { - str->alsa.appl_ptr += result; - str->alsa.appl_ptr %= str->alsa.boundary; } + str->alsa.appl_ptr += result; + str->alsa.appl_ptr %= str->alsa.boundary; result *= str->frame_bytes; str->oss.bytes += result; _end: -- 2.47.1