Writing a new plugin for ALSA
Hi,
I am trying to write a new ALSA plugin, like dmix.
My intend is to give priority to streams, i will not do up sample and down sample as we do in the case of dmix.
If we do up sample and down sample then I think it will take lot of cpu.
In my case I have three streams audio, bluetooth sco and Navigation sound data.
priority is audio < sco < Nav
audio rate is 44100hz, sco rate is 8000hz
when sco is up audio should pause playback and audio should continue when sco is finished. I dont want to do software mixing. Thats why I am trying to write this new plugin.
Assume that audio is active first, then sco comes to active state. So what i will do is in the library i will put audio app in sleep(), and re-configure the pcm for sco. Once sco is finished i will re-configure pcm for audio again like wise.
so my question is there any api in the alsa-lib to take a copy of pcm structures, so when audio comes back gain i can use these saved pcm conf to that.
Is there any api in the alsa-lib to reset the pcm structure to initialized state, means state just after open.
Thanks
Nobin Mathew
At Mon, 9 Jul 2007 17:29:17 +0530,
Quoted 7 lines · Nobin Mathew
Hi,
I am trying to write a new ALSA plugin, like dmix.
My intend is to give priority to streams, i will not do up sample and down sample as we do in the case of dmix.
A small correction: dmix won't do any up/down-sampling but requires the fixed sample rate. The up/downsampling is the job of rate plugin.
Quoted 13 lines
If we do up sample and down sample then I think it will take lot of cpu.
In my case I have three streams audio, bluetooth sco and Navigation sound data.
priority is audio < sco < Nav
audio rate is 44100hz, sco rate is 8000hz
when sco is up audio should pause playback and audio should continue when sco is finished. I dont want to do software mixing. Thats why I am trying to write this new plugin.
Quoted 4 lines
Assume that audio is active first, then sco comes to active state. So what i will do is in the library i will put audio app in
sleep(), and re-configure the pcm for sco. Once sco is finished i will re-configure pcm for audio again like wise.
It should depend on the running mode. For example, sleeping in the plugin is bad for apps that require non-blocking access. OTOH, for blocking mode, sleeping until the stream gets ready again is reasonable behavior. And, I don't know whether the pausing is really the right choice. When it's paused, the application has no way to know what's going on there.
Maybe, returning -ESTRPIPE, which means suspend, would be a better choice. Then the app is supposed to do a proper work for resuming (calling snd_pcm_resume(), etc).
Quoted 3 lines
so my question is there any api in the alsa-lib to take a copy of pcm structures, so when audio comes back gain i can use these saved pcm conf to that.
The "copy" of PCM handle won't help for your scenario, I guess. Because the status of PCM stream is changed, the old copy won't match with the latest state.
Quoted 2 lines
Is there any api in the alsa-lib to reset the pcm structure to initialized state, means state just after open.
I feel this is a kind of things that needs a tight connection with the kernel-side implementation. For example, the plugin controlled from multiple processes isn't easy to implement. dmix uses SYSV IPC for some parts, but it's to be avoided as much as possible.
Takashi
Read through this, and you can ask more to this guy. This guy implemented alsa-lib and lot of other things in alsa.
Please copy [email protected] when you mail him, so that others in mailing list will also benefit.
---------- Forwarded message ----------
From: Nobin Mathew <[email protected]>
Date: Jul 9, 2007 5:29 PM
Subject: Writing a new plugin for ALSA
To: Takashi Iwai <[email protected]>
Cc: ALSA devel <[email protected]>
Hi,
I am trying to write a new ALSA plugin, like dmix.
My intend is to give priority to streams, i will not do up sample and down sample as we do in the case of dmix.
If we do up sample and down sample then I think it will take lot of cpu.
In my case I have three streams audio, bluetooth sco and Navigation sound data.
priority is audio < sco < Nav
audio rate is 44100hz, sco rate is 8000hz
when sco is up audio should pause playback and audio should continue when sco is finished. I dont want to do software mixing. Thats why I am trying to write this new plugin.
Assume that audio is active first, then sco comes to active state. So what i will do is in the library i will put audio app in sleep(), and re-configure the pcm for sco. Once sco is finished i will re-configure pcm for audio again like wise.
so my question is there any api in the alsa-lib to take a copy of pcm structures, so when audio comes back gain i can use these saved pcm conf to that.
Is there any api in the alsa-lib to reset the pcm structure to initialized state, means state just after open.
Thanks
Nobin Mathew
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): s5hbqekl9mb.wl%[email protected]
Quoted 43 lines · Takashi Iwai
At Tue, 10 Jul 2007 17:45:03 +0530,
Quoted 9 lines · Sudhanshu Saxena
Hi Takashi,
I am working on this issue along with Nobin, for multiple multiple streams (at different rates) playback based on a priority.
I attempted suspend and resume returning
-ESTRPIPE, but it has two issues
1) All applications may need a change in Suspend logic specific to wait for higher priority stream to run to completion.Basically every app is _required_ to handle of suspend/resume properly.
Quoted 5 lines
2) I have to still find out a mechanism to reset the hardware settings are reconfigure it for second stream.
snd_pcm_hw_resethas an access to reset using IOCTL "SNDRV_PCM_IOCTL_RESET" , I hope that can help, I am not
very sure on this.Well, I don't think the plugin itself requires the reset.
Quoted 11 lines
As mentioned by Nobin we want to skip any upsampling caused when we use the dmix (rate) plugin.
I was trying to find the way out, with a plugin with following changes:
1) All streams will open pcm with a priority. (Pre decided for the system)
2) Plug-in will have shared memory to hold the priority list of all applications which are accessing sound.
3) At any instance only one stream will be active.
4) All applications will hold the context of HW params and SW params (accessible through apis plugin will support).
5) If an application with a higher priority callssnd_pcm_openshared memory with the priority list will be updated andQuoted 8 lines
when the currently active stream attempts to re-write the data it will check for
priority list. This will cause saving of params, drain of stream and reset of hw as mentioned using above IOCTL and the active stream would be changed to currently highest in the priority list.
6) The higher priority stream can then start writing data, on completion of access similar sequence of drain, hw reset will be caused and the active stream will be changed to next in the priority list.
7) On resume of lower priority stream the hw params will be set again.
Quoted 8 lines
The question is how the application decides which stream to use. If it can open a PCM with a different name (to
snd_pcm_opencall), and if you have three individual PCM devices, the whole switching can be done inside the kernel driver. That is, when an SCO stream is opened during audio stream(s) is running, the driver triggers SUSPEND mode for the audio stream. Then the app will stop and restart the stream by itself. This means you don't need any change in user-space side but needs some additions in the driver side.
We thought about implementing this in the kernel driver, but after some investigation, we found that we are serializing the driver so much by providing three interfaces (audio, sco and Nav). say pcmC0D0p/c, pcmC0D1p/c and pcmC0D2p/c.
Consider the case of two apps audio and sco.
We have two interfaces for this pcmC0D0p/c and pcmC0D1p/c priority is audio < sco
so interface priority is pcmC0D0p/c < pcmC0D1p/c.
Assume that audio is already opened.
When we open sco interface audio interface can be in any state, it i can be in paused state or it is about to close etc. So we will have so
many states of application in the kernel driver.
we need to maintain all these states of application in the kernel driver. Say tomorrow one more application came in to picture, then this complexity will increase.
Locking of states of streams is also required in this case.
correct me if I am going wrong
Quoted 3 lines
BTW, any reason to drop Cc to alsa-devel ML? Such an open discussion is very important for opensource development.
I think Sudhanshu missed it, I am copying to alsa-devel
Quoted 4 lines
Takashi
thanks
Nobin mathew
At Tue, 10 Jul 2007 18:24:18 +0530,
Quoted 63 lines · Nobin Mathew
Quoted 43 lines · Takashi Iwai
At Tue, 10 Jul 2007 17:45:03 +0530,
Quoted 9 lines · Sudhanshu Saxena
Hi Takashi,
I am working on this issue along with Nobin, for multiple multiple streams (at different rates) playback based on a priority.
I attempted suspend and resume returning
-ESTRPIPE, but it has two issues
1) All applications may need a change in Suspend logic specific to wait for higher priority stream to run to completion.Basically every app is _required_ to handle of suspend/resume properly.
Quoted 5 lines
2) I have to still find out a mechanism to reset the hardware settings are reconfigure it for second stream.
snd_pcm_hw_resethas an access to reset using IOCTL "SNDRV_PCM_IOCTL_RESET" , I hope that can help, I am not
very sure on this.Well, I don't think the plugin itself requires the reset.
Quoted 11 lines
As mentioned by Nobin we want to skip any upsampling caused when we use the dmix (rate) plugin.
I was trying to find the way out, with a plugin with following changes:
1) All streams will open pcm with a priority. (Pre decided for the system)
2) Plug-in will have shared memory to hold the priority list of all applications which are accessing sound.
3) At any instance only one stream will be active.
4) All applications will hold the context of HW params and SW params (accessible through apis plugin will support).
5) If an application with a higher priority callssnd_pcm_openshared memory with the priority list will be updated andQuoted 8 lines
when the currently active stream attempts to re-write the data it will check for
priority list. This will cause saving of params, drain of stream and reset of hw as mentioned using above IOCTL and the active stream would be changed to currently highest in the priority list.
6) The higher priority stream can then start writing data, on completion of access similar sequence of drain, hw reset will be caused and the active stream will be changed to next in the priority list.
7) On resume of lower priority stream the hw params will be set again.Quoted 8 lines
The question is how the application decides which stream to use. If it can open a PCM with a different name (to
snd_pcm_opencall), and if you have three individual PCM devices, the whole switching can be done inside the kernel driver. That is, when an SCO stream is opened during audio stream(s) is running, the driver triggers SUSPEND mode for the audio stream. Then the app will stop and restart the stream by itself. This means you don't need any change in user-space side but needs some additions in the driver side.We thought about implementing this in the kernel driver, but after some investigation, we found that we are serializing the driver so much by providing three interfaces (audio, sco and Nav). say pcmC0D0p/c, pcmC0D1p/c and pcmC0D2p/c.
Consider the case of two apps audio and sco.
We have two interfaces for this pcmC0D0p/c and pcmC0D1p/c priority is audio < sco
Quoted 6 lines
so interface priority is pcmC0D0p/c < pcmC0D1p/c.
Assume that audio is already opened.
When we open sco interface audio interface can be in any state, it i can be in paused state or it is about to close etc. So we will have so
many states of application in the kernel driver.
Well, but this is same for user-space solution.
How the exclusiveness is handled in the driver? What exactly happens if you open two different streams (e.g. audio and sco) at the same time? Does it return an error?
Quoted 5 lines
we need to maintain all these states of application in the kernel driver. Say tomorrow one more application came in to picture, then this complexity will increase.
Locking of states of streams is also required in this case.
It's needed in any case if the concurrent access is possible.
Takashi
If I open both sco and audio simultaneously, where I will push the data from both streams, because I have only one codec, i.e. single DAC/ADC pair.
So that needs stopping one of the stream inside the kernel.
When we try to stop one stream from kernel itself, it will lead to problems I mentioned in the earlier mail.
Thanks
Nobin Mathew
Quoted 89 lines · Takashi Iwai
At Tue, 10 Jul 2007 18:24:18 +0530,
Quoted 63 lines · Nobin Mathew
Quoted 43 lines · Takashi Iwai
At Tue, 10 Jul 2007 17:45:03 +0530,
Quoted 9 lines · Sudhanshu Saxena
Hi Takashi,
I am working on this issue along with Nobin, for multiple multiple streams (at different rates) playback based on a priority.
I attempted suspend and resume returning
-ESTRPIPE, but it has two issues
1) All applications may need a change in Suspend logic specific to wait for higher priority stream to run to completion.Basically every app is _required_ to handle of suspend/resume properly.
Quoted 5 lines
2) I have to still find out a mechanism to reset the hardware settings are reconfigure it for second stream.
snd_pcm_hw_resethas an access to reset using IOCTL "SNDRV_PCM_IOCTL_RESET" , I hope that can help, I am not
very sure on this.Well, I don't think the plugin itself requires the reset.
Quoted 11 lines
As mentioned by Nobin we want to skip any upsampling caused when we use the dmix (rate) plugin.
I was trying to find the way out, with a plugin with following changes:
1) All streams will open pcm with a priority. (Pre decided for the system)
2) Plug-in will have shared memory to hold the priority list of all applications which are accessing sound.
3) At any instance only one stream will be active.
4) All applications will hold the context of HW params and SW params (accessible through apis plugin will support).
5) If an application with a higher priority callssnd_pcm_openshared memory with the priority list will be updated andQuoted 8 lines
when the currently active stream attempts to re-write the data it will check for
priority list. This will cause saving of params, drain of stream and reset of hw as mentioned using above IOCTL and the active stream would be changed to currently highest in the priority list.
6) The higher priority stream can then start writing data, on completion of access similar sequence of drain, hw reset will be caused and the active stream will be changed to next in the priority list.
7) On resume of lower priority stream the hw params will be set again.Quoted 8 lines
The question is how the application decides which stream to use. If it can open a PCM with a different name (to
snd_pcm_opencall), and if you have three individual PCM devices, the whole switching can be done inside the kernel driver. That is, when an SCO stream is opened during audio stream(s) is running, the driver triggers SUSPEND mode for the audio stream. Then the app will stop and restart the stream by itself. This means you don't need any change in user-space side but needs some additions in the driver side.We thought about implementing this in the kernel driver, but after some investigation, we found that we are serializing the driver so much by providing three interfaces (audio, sco and Nav). say pcmC0D0p/c, pcmC0D1p/c and pcmC0D2p/c.
Consider the case of two apps audio and sco.
We have two interfaces for this pcmC0D0p/c and pcmC0D1p/c priority is audio < scoQuoted 6 lines
so interface priority is pcmC0D0p/c < pcmC0D1p/c.
Assume that audio is already opened.
When we open sco interface audio interface can be in any state, it i can be in paused state or it is about to close etc. So we will have so
many states of application in the kernel driver.Well, but this is same for user-space solution.
How the exclusiveness is handled in the driver? What exactly happens if you open two different streams (e.g. audio and sco) at the same time? Does it return an error?
Quoted 5 lines
we need to maintain all these states of application in the kernel driver. Say tomorrow one more application came in to picture, then this complexity will increase.
Locking of states of streams is also required in this case.
It's needed in any case if the concurrent access is possible.
Takashi
I think if we can a find/develop a solution inside the ALSA framework, then that will be usefull for lot of people. Because most of the new embedded devices are coming with GPS, bluetooth and Audio capabilities.
Thanks
Nobin Mathew
Quoted 103 lines · Nobin Mathew
If I open both sco and audio simultaneously, where I will push the data from both streams, because I have only one codec, i.e. single DAC/ADC pair.
So that needs stopping one of the stream inside the kernel.
When we try to stop one stream from kernel itself, it will lead to problems I mentioned in the earlier mail.
Thanks
Nobin MathewQuoted 89 lines · Takashi Iwai
At Tue, 10 Jul 2007 18:24:18 +0530,
Quoted 63 lines · Nobin Mathew
Quoted 43 lines · Takashi Iwai
At Tue, 10 Jul 2007 17:45:03 +0530,
Quoted 9 lines · Sudhanshu Saxena
Hi Takashi,
I am working on this issue along with Nobin, for multiple multiple streams (at different rates) playback based on a priority.
I attempted suspend and resume returning
-ESTRPIPE, but it has two issues
1) All applications may need a change in Suspend logic specific to wait for higher priority stream to run to completion.Basically every app is _required_ to handle of suspend/resume properly.
Quoted 5 lines
2) I have to still find out a mechanism to reset the hardware settings are reconfigure it for second stream.
snd_pcm_hw_resethas an access to reset using IOCTL "SNDRV_PCM_IOCTL_RESET" , I hope that can help, I am not
very sure on this.Well, I don't think the plugin itself requires the reset.
Quoted 11 lines
As mentioned by Nobin we want to skip any upsampling caused when we use the dmix (rate) plugin.
I was trying to find the way out, with a plugin with following changes:
1) All streams will open pcm with a priority. (Pre decided for the system)
2) Plug-in will have shared memory to hold the priority list of all applications which are accessing sound.
3) At any instance only one stream will be active.
4) All applications will hold the context of HW params and SW params (accessible through apis plugin will support).
5) If an application with a higher priority callssnd_pcm_openshared memory with the priority list will be updated andQuoted 8 lines
when the currently active stream attempts to re-write the data it will check for
priority list. This will cause saving of params, drain of stream and reset of hw as mentioned using above IOCTL and the active stream would be changed to currently highest in the priority list.
6) The higher priority stream can then start writing data, on completion of access similar sequence of drain, hw reset will be caused and the active stream will be changed to next in the priority list.
7) On resume of lower priority stream the hw params will be set again.Quoted 8 lines
The question is how the application decides which stream to use. If it can open a PCM with a different name (to
snd_pcm_opencall), and if you have three individual PCM devices, the whole switching can be done inside the kernel driver. That is, when an SCO stream is opened during audio stream(s) is running, the driver triggers SUSPEND mode for the audio stream. Then the app will stop and restart the stream by itself. This means you don't need any change in user-space side but needs some additions in the driver side.We thought about implementing this in the kernel driver, but after some investigation, we found that we are serializing the driver so much by providing three interfaces (audio, sco and Nav). say pcmC0D0p/c, pcmC0D1p/c and pcmC0D2p/c.
Consider the case of two apps audio and sco.
We have two interfaces for this pcmC0D0p/c and pcmC0D1p/c priority is audio < scoQuoted 6 lines
so interface priority is pcmC0D0p/c < pcmC0D1p/c.
Assume that audio is already opened.
When we open sco interface audio interface can be in any state, it i can be in paused state or it is about to close etc. So we will have so
many states of application in the kernel driver.Well, but this is same for user-space solution.
How the exclusiveness is handled in the driver? What exactly happens if you open two different streams (e.g. audio and sco) at the same time? Does it return an error?
Quoted 5 lines
we need to maintain all these states of application in the kernel driver. Say tomorrow one more application came in to picture, then this complexity will increase.
Locking of states of streams is also required in this case.
It's needed in any case if the concurrent access is possible.
Takashi