From: Takashi Iwai Subject: Re: quit snd_pcm_readi, retrieve pending frames Date: Mon, 15 May 2006 12:06:47 +0200 Message-ID: References: <4465BDCB.7010900@gmx.net> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Return-path: In-Reply-To: <4465BDCB.7010900@gmx.net> Sender: alsa-devel-admin@lists.sourceforge.net Errors-To: alsa-devel-admin@lists.sourceforge.net List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , List-Archive: To: Gerald Grabner Cc: alsa-devel@lists.sourceforge.net List-Id: alsa-devel@alsa-project.org At Sat, 13 May 2006 13:06:51 +0200, Gerald Grabner wrote: > > Hi, > > I'm experimenting with the ALSA PCM API and was wondering whether > there is a simple way to exit a record loop by stopping the pcm, but > without loosing pending frames. > > Initially, I was thinking of something like this, where I would call > snd_pcm_drop(pcm) from some other thread: > > while ( true ) > { > r = snd_pcm_readi (pcm, data, frames); > fwrite (data, 2, 2*r, file); > if ( r != frames ) > break; > } > > However, snd_pcm_drain(pcm) doesn't work here; the loop continues. > snd_pcm_drop(pcm) breaks the loop, but pending frames are lost, and > r=-EBADFD. > > Is there an easy way to stop snd_pcm_readi in a way that I can > retrieve the residual frames? Do I need to set any parameters for > that purpose? If you're using hw or plughw, it's likely a bug in alsa-driver. Try the patch below. dsnoop plugin should work as it is, though, and dsnoop is used for many drivers as default. Check your configuration at first. Takashi diff -r d1f5192b7abb core/pcm_native.c --- a/core/pcm_native.c Thu May 11 18:12:23 2006 +0200 +++ b/core/pcm_native.c Mon May 15 12:00:27 2006 +0200 @@ -1406,6 +1406,18 @@ struct drain_rec { static int snd_pcm_drop(struct snd_pcm_substream *substream); +static int pcms_still_draining(struct snd_pcm_substream *substream) +{ + struct list_head *pos; + snd_pcm_group_for_each(pos, substream) { + struct snd_pcm_substream *s; + s = snd_pcm_group_substream_entry(pos); + if (s->runtime->status->state == SNDRV_PCM_STATE_DRAINING) + return 1; + } + return 0; /* yes, all drained */ +} + /* * Drain the stream(s). * When the substream is linked, sync until the draining of all playback streams @@ -1469,8 +1481,6 @@ static int snd_pcm_drain(struct snd_pcm_ } } up_read(&snd_pcm_link_rwsem); - if (! num_drecs) - goto _error; snd_pcm_stream_lock_irq(substream); /* resume pause */ @@ -1484,21 +1494,12 @@ static int snd_pcm_drain(struct snd_pcm_ goto _error; } - for (;;) { + while (pcms_still_draining(substream)) { long tout; if (signal_pending(current)) { result = -ERESTARTSYS; break; } - /* all finished? */ - for (i = 0; i < num_drecs; i++) { - runtime = drec[i].substream->runtime; - if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) - break; - } - if (i == num_drecs) - break; /* yes, all drained */ - set_current_state(TASK_INTERRUPTIBLE); snd_pcm_stream_unlock_irq(substream); snd_power_unlock(card); ------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642