Correct stopping capture and playback substreams?

Jaroslav Kysela perex at perex.cz
Mon Jan 3 10:10:00 CET 2022


On 03. 01. 22 9:22, Pavel Hofman wrote:
> 
> Dne 23. 12. 21 v 9:18 Pavel Hofman napsal(a):
>> Hi Takashi,
>>
>> I am working on stopping alsa streams of audio USB gadget when USB host
>> stops capture/playback/USB cable unplugged.
>>
>> For capture I used code from AK4114 SPDIF receiver
>> https://elixir.bootlin.com/linux/latest/source/sound/i2c/other/ak4114.c#L590:
>>
>>
>> static void stop_substream(struct uac_rtd_params *prm)
>> {
>>       unsigned long _flags;
>>       struct snd_pcm_substream *substream;
>>
>>       substream = prm->ss;
>>       if (substream) {
>>           snd_pcm_stream_lock_irqsave(substream, _flags);
>>           if (snd_pcm_running(substream))
>>               // TODO - correct handling for playback substream?
>>               snd_pcm_stop(substream, SNDRV_PCM_STATE_DRAINING);
>>           snd_pcm_stream_unlock_irqrestore(substream, _flags);
>>       }
>> }
>>
>> For setup I found calling snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP)
>> (https://elixir.bootlin.com/linux/latest/source/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c#L63)
>>    Or for both capture and playback using SNDRV_PCM_STATE_DISCONNECTED
>> (https://elixir.bootlin.com/linux/latest/source/sound/core/pcm.c#L1103).
>>
>> Or perhaps using snd_pcm_dev_disconnect(dev) or snd_pcm_drop(substream)?
>>
>> Please what is the recommended way?
>>
> 
> Please can I ask for expert view on this issue? E.g. in SoX stopping the
> stream with SNDRV_PCM_STATE_SETUP/SNDRV_PCM_STATE_DRAINING does not stop
> the application, while with SNDRV_PCM_STATE_DISCONNECTED SoX exits with
> non-recoverable status. I am considering implementing both methods and
> letting users choose their suitable snd_pcm_stop operation (none
> (default)/SETUP-DRAINING/DISCONNECTED) for the two events (host
> playback/capture stop, cable disconnection) with a configfs param. Would
> this make sense?

The disconnection state is unrecoverable. It's expected that the device will 
be freed and cannot be reused.

If you expect to keep the PCM device, we should probably introduce a new 
function which puts the device to the SNDRV_PCM_STATE_OPEN state. In this 
state, all I/O routines will return -EBADFD for the applications, so they 
should close or re-initialize the PCM device completely.

https://elixir.bootlin.com/linux/latest/source/sound/core/pcm_native.c#L794

					Jaroslav

-- 
Jaroslav Kysela <perex at perex.cz>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.


More information about the Alsa-devel mailing list