ALSA: pcm - introduce soc_delay
PATCH 1/1 ALSA: pcm - introduce soc_delay
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when hw_ptr reaches app_ptr value, though DSP may still have some buffered data.
This patch tries to add a new field "soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
--
Once we are okay with this approach, I will send a follow up patch which adds this notion in ASoC and uses this to compute cpu_dai delay. The codec_dai delay along with FIFO delay from cpu_dai should be added and reresented by today's notion of delay.
--- include/sound/pcm.h | 1 + sound/core/pcm_lib.c | 14 +++++++++++--- sound/core/pcm_native.c | 6 +++--- 3 files changed, 15 insertions(+), 6 deletions(-)
include/sound/pcm.h +1 -0
@@ -281,6 +281,7 @@ struct snd_pcm_runtime {unsigned long hw_ptr_jiffies;/* Time when hw_ptr is updated */ unsigned long hw_ptr_buffer_jiffies;/* buffer time in jiffies */ snd_pcm_sframes_t delay;/* extra delay; typically FIFO size */ +snd_pcm_sframes_t soc_delay;/* extra delay; typically delay incurred in soc */ /* -- HW params -- */ snd_pcm_access_t access;/* access mode */
sound/core/pcm_lib.c +11 -3
@@ -292,7 +292,15 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream,return -EPIPE ; } }else { -if (avail >= runtime->stop_threshold) { +snd_pcm_uframes_t actual_avail; +if (avail < runtime->soc_delay) + actual_avail = avail; +else + actual_avail = avail - runtime->soc_delay; +if (actual_avail >= runtime->stop_threshold) { +snd_printd (KERN_ERR "avail > stop_threshold!!\n" ); +snd_printd (KERN_ERR "actual_avail %ld, avail %ld, soc_delay %ld!!\n" , + actual_avail, avail, runtime->soc_delay);xrun (substream);return -EPIPE ; }
@@ -440,9 +448,9 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,if (runtime->hw.info &SNDRV_PCM_INFO_BATCH ) goto no_jiffies_check; hdelta = delta; -if (hdelta < runtime->delay) +if (hdelta < (runtime->delay + runtime->soc_delay)) goto no_jiffies_check; - hdelta -= runtime->delay; + hdelta -= (runtime->delay + runtime->soc_delay); jdelta = curr_jiffies - runtime->hw_ptr_jiffies;if (((hdelta *HZ ) / runtime->rate) > jdelta +HZ /100 ) { delta = jdelta /
sound/core/pcm_native.c +3 -3
@@ -606,13 +606,13 @@ int snd_pcm_status(struct snd_pcm_substream *substream,if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING || runtime->status->state ==SNDRV_PCM_STATE_DRAINING ) { status->delay = runtime->buffer_size - status->avail; - status->delay += runtime->delay; + status->delay += runtime->delay + runtime->soc_delay; }else status->delay =0 ; }else { status->avail =snd_pcm_capture_avail (runtime);if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING ) - status->delay = status->avail + runtime->delay; + status->delay = status->avail + runtime->delay + runtime->soc_delay;else status->delay =0 ; }
@@ -2442,7 +2442,7 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream, n =snd_pcm_playback_hw_avail (runtime); else n =snd_pcm_capture_avail (runtime); - n += runtime->delay; + n += runtime->delay + runtime->soc_delay;break ;case SNDRV_PCM_STATE_XRUN : err = -EPIPE ;
Signature · 1 line
1.7.0.4
Quoted 1 line · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we
This isn't particularly new, we've had SRAM buffers in SoCs for ages.
Quoted 2 lines
have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.
It's not immediately clear to me that we shouldn't be flagging an overrun here - obviously if there's just a pure delay introduced by the DSP then we will underrun if we catch up with the input, it's just that there will be a delay in the user hearing it. It seems like the thing we need to say here is more that the DSP or DMA knows where the application pointer is and can halt the DMA when it underruns, together with the fact that we have some buffering beyond the application visible DMA which means that an underrun isn't (yet) a user audible problem. We'd also want some other way of saying that that these further buffers did overrun.
Quoted 2 lines
This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
soc_delay seems like a very unclear name for this.
Quoted 9 lines · Mark Brown
Quoted 1 line · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we
This isn't particularly new, we've had SRAM buffers in SoCs for ages.
Quoted 2 lines
have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.
Quoted 10 lines
It's not immediately clear to me that we shouldn't be flagging an overrun here - obviously if there's just a pure delay introduced by the DSP then we will underrun if we catch up with the input, it's just that there will be a delay in the user hearing it. It seems like the thing we need to say here is more that the DSP or DMA knows where the application pointer is and can halt the DMA when it underruns, together with the fact that we have some buffering beyond the application visible DMA which means that an underrun isn't (yet) a user audible problem. We'd also want some other way of saying that that these further buffers did overrun.
having DMA know where app_pointer helps in detection of overrun in dsp. But that's a separate topic.
Take the case of a DSP which employs DMA on input and output (of the DSP)
At start, DSP would buffer some data, assuming a period. And assume app has filed one period only. So at start as we buffered a period, ALSA threw up wrong xrun, as it wasn't aware that this buffer is not yet rendered.
Using this notion, ALSA knows that there is some buffer in DSP and used that for xrun as well as delay calculations.
Quoted 4 lines
Quoted 2 lines
This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
soc_delayseems like a very unclear name for this.
I am not good with names, perhaps soc_buffer is good name :-) ?
Signature · 1 line
~Vinod
Quoted 12 lines · Vinod Koul
Quoted 2 lines · Mark Brown
It's not immediately clear to me that we shouldn't be flagging an overrun here - obviously if there's just a pure delay introduced by the
Take the case of a DSP which employs DMA on input and output (of the DSP)
At start, DSP would buffer some data, assuming a period. And assume app has filed one period only. So at start as we buffered a period, ALSA threw up wrong xrun, as it wasn't aware that this buffer is not yet rendered.
Quoted 2 lines
Using this notion, ALSA knows that there is some buffer in DSP and used that for xrun as well as delay calculations.
I'm having a hard time relating this to what I was saying. The point here is that if the device keeps marching on consuming data (as most cyclic DMAs would) then there's still going to be an underrun even if there's a buffer that causes a delay in the user hearing it.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
soc_delayseems like a very unclear name for this.I am not good with names, perhaps
soc_bufferis good name :-) ?
The "soc" is the problem here - this shouldn't be specific to some sort of hardware.
Quoted 13 lines · Mark Brown
Quoted 11 lines · Vinod Koul
Quoted 2 lines · Mark Brown
It's not immediately clear to me that we shouldn't be flagging an overrun here - obviously if there's just a pure delay introduced by the
Take the case of a DSP which employs DMA on input and output (of the DSP)
At start, DSP would buffer some data, assuming a period. And assume app has filed one period only. So at start as we buffered a period, ALSA threw up wrong xrun, as it wasn't aware that this buffer is not yet rendered.
Quoted 7 lines
Quoted 2 lines
Using this notion, ALSA knows that there is some buffer in DSP and used that for xrun as well as delay calculations.
I'm having a hard time relating this to what I was saying. The point here is that if the device keeps marching on consuming data (as most cyclic DMAs would) then there's still going to be an underrun even if there's a buffer that causes a delay in the user hearing it.
yes there would be overrun but that is correct one. At that point I can see that the DSP would have rendered everything it has (all buffers exhausted). At this point app_pointer = hw_pointer and soc_delay would be zero.
I am assuming dsp driver reports the soc_delay dynamically as it would do for hw_pointer.
This doesn't stop all underruns, they would still be reported.
This only prevents false underrun when DSP still has something to process and give to output DMA.
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
soc_delayseems like a very unclear name for this.I am not good with names, perhaps
soc_bufferis good name :-) ?The "soc" is the problem here - this shouldn't be specific to some sort of hardware.
Hmmm, yes this can happen outside soc's as well. dsp_buffer is my next candidate :)
Signature · 1 line
~Vinod
Quoted 7 lines · Vinod Koul
Quoted 4 lines · Mark Brown
I'm having a hard time relating this to what I was saying. The point here is that if the device keeps marching on consuming data (as most cyclic DMAs would) then there's still going to be an underrun even if there's a buffer that causes a delay in the user hearing it.
Quoted 7 lines
yes there would be overrun but that is correct one. At that point I can see that the DSP would have rendered everything it has (all buffers exhausted). At this point
app_pointer=hw_pointerandsoc_delaywould be zero.I am assuming dsp driver reports the
soc_delaydynamically as it would do forhw_pointer.
This is sounding exactly like the existing delay parameter and how it's handled in ASoC.
Quoted 4 lines
This doesn't stop all underruns, they would still be reported.
This only prevents false underrun when DSP still has something to process and give to output DMA.
I don't think you're quite getting my point here. The DSP still has something to process but unless whatever is feeding the data to the DSP stops sending data to the DSP when it catches up with where the application is writing then there will still be a glitch.
Reporting the delay is useful anyway but it's not the whole picture as far as recovering from and masking the underrun is concerned.
Quoted 4 lines · Mark Brown
I'm having a hard time relating this to what I was saying. The point here is that if the device keeps marching on consuming data (as most cyclic DMAs would) then there's still going to be an underrun even if there's a buffer that causes a delay in the user hearing it
Not necessarily. The DMA between system memory and DSP buffers need not work at a rate linked to the serial bit clock, they can be much faster, and actually they should to help put the system in a low power state rather than reading continuously from memory... what Vinod is trying to explain is that due to the bursty nature of data transfers inside the soc, we need to modify how the accounting is done.
Quoted 1 line · Pierre-Louis Bossart
On 7/23/2012 5:47 AM, Mark Brown wrote:
Don't drop CCs from replies.
Quoted 9 lines
Quoted 4 lines
I'm having a hard time relating this to what I was saying. The point here is that if the device keeps marching on consuming data (as most cyclic DMAs would) then there's still going to be an underrun even if there's a buffer that causes a delay in the user hearing it
Not necessarily. The DMA between system memory and DSP buffers need not work at a rate linked to the serial bit clock, they can be much faster, and actually they should to help put the system in a low power state rather than reading continuously from memory... what
I'm aware of this, thanks.
Quoted 3 lines
Vinod is trying to explain is that due to the bursty nature of data transfers inside the soc, we need to modify how the accounting is done.
Right, but this depends on the ability of the device to pause reading data when it reads up to the point where the application has written. This is a separate capability to any latency that's been added by the buffering, and most of the systems that have the buffering don't have this capability but instead either don't report the buffer or rely on the application being a full buffer ahead of the hardware.
Quoted 1 line
Don't drop CCs from replies.
Sorry.
Quoted 4 lines
Quoted 4 lines
Vinod is trying to explain is that due to the bursty nature of data transfers inside the soc, we need to modify how the accounting is done.
Quoted 8 lines
Right, but this depends on the ability of the device to pause reading data when it reads up to the point where the application has written. This is a separate capability to any latency that's been added by the buffering, and most of the systems that have the buffering don't have this capability but instead either don't report the buffer or rely on the application being a full buffer ahead of the hardware.
We don't have such fancy hardware (and I don't think anyone has). This can happen even with simple IP that has an embedded SRAM and bursty DMA, if the IP buffer amounts to the period size to avoid partial wakes or transfers, and the application cannot provide more than one period initially you get an underflow that isn't a true one. -Pierre
Quoted 10 lines · Pierre-Louis Bossart
Quoted 6 lines
Right, but this depends on the ability of the device to pause reading data when it reads up to the point where the application has written. This is a separate capability to any latency that's been added by the buffering, and most of the systems that have the buffering don't have this capability but instead either don't report the buffer or rely on the application being a full buffer ahead of the hardware.
We don't have such fancy hardware (and I don't think anyone has). This can happen even with simple IP that has an embedded SRAM and bursty DMA, if the IP buffer amounts to the period size to avoid
The usual way of representing this is to have the pointers in the buffer represent where the input to the hardware pipeline is at (ie, where the actual DMA is reading) and handle underflow on that normally.
Quoted 3 lines
partial wakes or transfers, and the application cannot provide more than one period initially you get an underflow that isn't a true one.
Normally this is handled by requiring that the application has at least one period ready for the hardware at all times, otherwise as you get down towards the end of the buffer things get racier and racier. It's not really about the delay, either - it's about when the DMA wants to read the memory which isn't quite the same thing. It could be that it does this with half the buffer remaining, it could be that it waits until it's got some much smaller amount of data left.
I think what's really needed here is to represent the buffer in the hardware and how it's filled up to the stack more directly. It's not the fact that there is data buffered that we need to tell the stack about (we can already do that), what we need to tell it is that the trigger level for refilling that may not be what's expected.
Quoted 7 lines · Vinod Koul
Take the case of a DSP which employs DMA on input and output (of the DSP)
At start, DSP would buffer some data, assuming a period. And assume app has filed one period only. So at start as we buffered a period, ALSA threw up wrong xrun, as it wasn't aware that this buffer is not yet rendered.
I thought the data was supposed to leave the ring-buffer only _after_ the TRIGGER_START call ?
Quoted 7 lines · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
Couldn't we employ snd_pcm_hw_params.fifo_size for the purpose ?
Quoted 9 lines · Jassi Brar
Quoted 7 lines · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.Couldn't we employ snd_pcm_hw_params.fifo_size for the purpose ?
Nope, that should be used to represent the delay in samples being output not the sample which are buffered and processed in a DSP
Signature · 1 line
~Vinod
Quoted 6 lines · Vinod Koul
Quoted 5 lines · Jassi Brar
Quoted 4 lines · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.Couldn't we employ snd_pcm_hw_params.fifo_size for the purpose ?
Nope, that should be used to represent the delay in samples being output not the sample which are buffered and processed in a DSP
After leaving alsa ring buffer, don't the samples incur extra delay of your internal buffer ?
Quoted 7 lines · Jassi Brar
Quoted 2 lines
Quoted 1 line
Couldn't we employ snd_pcm_hw_params.fifo_size for the purpose ?
Nope, that should be used to represent the delay in samples being
output
Quoted 2 lines
not the sample which are buffered and processed in a DSP
After leaving alsa ring buffer, don't the samples incur extra delay of your internal buffer ?
Yes they do.
And they are two types.
a) constant delay in DMA FIFO, aptly represented by delay
b) dynamic buffering on input DMA at DSP and during post-processing. This is before the circular DMA at output, and this patch attempts to represent this buffering.
I guess soc_delay is not a best of name and is causing quite a churn!
Signature · 1 line
~Vinod
Quoted 4 lines · Vinod Koul
a) constant delay in DMA FIFO, aptly represented by delay
b) dynamic buffering on input DMA at DSP and during post-processing. This is before the circular DMA at output, and this patch attempts to represent this buffering.
The delay in the runtime can vary too, in ASoC it's currently updated every time we call pointer().
Quoted 8 lines · Vinod Koul
Quoted 7 lines · Jassi Brar
Quoted 2 lines
Quoted 1 line
Couldn't we employ snd_pcm_hw_params.fifo_size for the purpose ?
Nope, that should be used to represent the delay in samples being
output
Quoted 2 lines
not the sample which are buffered and processed in a DSP
After leaving alsa ring buffer, don't the samples incur extra delay of your internal buffer ?
Quoted 8 lines
Yes they do.
And they are two types.
a) constant delay in DMA FIFO, aptly represented by delay
b) dynamic buffering on input DMA at DSP and during post-processing. This is before the circular DMA at output, and this patch attempts to represent this buffering.
Correct, but I think (a) and (b) are not that different.
I too might like to buy some time when the ring buffer is empty but my I2S controller's "deep fifo" still has a few millisecs of data 'buffered'. I don't see how your scenario is effectively different from mine.
And I can't see how it matters if the the "DSP buffer" is before or
after the ring-buffer. So perhaps we should abstract out any data outside of the ring-buffer to be in "fifo" and start taking fifo_size into account before warning XRUNs ?
At Mon, 23 Jul 2012 15:36:37 +0530,
Quoted 8 lines · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.
Quoted 7 lines
Signed-off-by Vinod Koul <[email protected]>--
Once we are okay with this approach, I will send a follow up patch which adds this notion in ASoC and uses this to computecpu_daidelay. Thecodec_daidelay along with FIFO delay fromcpu_daishould be added and reresented by today's notion of delay.
Hmm, it's confusing to have both delay and soc_delay fields.
And, if the XRUN detection is the only problem, we can provide a flag to correct avail with runtime->delay.
Quoted 26 lines
--- include/sound/pcm.h | 1 + sound/core/pcm_lib.c | 14 +++++++++++--- sound/core/pcm_native.c | 6 +++--- 3 files changed, 15 insertions(+), 6 deletions(-)include/sound/pcm.h +1
-0@@ -281,6 +281,7 @@ struct snd_pcm_runtime {unsigned long hw_ptr_jiffies;/* Time when hw_ptr is updated */ unsigned long hw_ptr_buffer_jiffies;/* buffer time in jiffies */ snd_pcm_sframes_t delay;/* extra delay; typically FIFO size */ +snd_pcm_sframes_t soc_delay;/* extra delay; typically delay incurred in soc */ /* -- HW params -- */ snd_pcm_access_t access;/* access mode */ sound/core/pcm_lib.c +0
-0@@ -292,7 +292,15 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream,return -EPIPE ; } }else {
sound/core/pcm_lib.c int snd_pcm_update_state(struct snd_pcm_substream *substream, · line 303
Quoted 10 lines
sound/core/pcm_lib.c +9
-1-if (avail >= runtime->stop_threshold) { +snd_pcm_uframes_t actual_avail; +if (avail < runtime->soc_delay) + actual_avail = avail; +else + actual_avail = avail - runtime->soc_delay; +if (actual_avail >= runtime->stop_threshold) { +snd_printd (KERN_ERR "avail > stop_threshold!!\n" ); +snd_printd (KERN_ERR "actual_avail %ld, avail %ld, soc_delay %ld!!\n" , + actual_avail, avail, runtime->soc_delay);
Don't add debug prints here. This is no kernel error, and XRUN is already informed in xrun() function.
thanks,
Takashi
Quoted 47 lines
sound/core/pcm_lib.c +2
-2xrun (substream);return -EPIPE ; }@@ -440,9 +448,9 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,if (runtime->hw.info &SNDRV_PCM_INFO_BATCH ) goto no_jiffies_check; hdelta = delta; -if (hdelta < runtime->delay) +if (hdelta < (runtime->delay + runtime->soc_delay)) goto no_jiffies_check; - hdelta -= runtime->delay; + hdelta -= (runtime->delay + runtime->soc_delay); jdelta = curr_jiffies - runtime->hw_ptr_jiffies;if (((hdelta *HZ ) / runtime->rate) > jdelta +HZ /100 ) { delta = jdelta /sound/core/pcm_native.c +3
-3@@ -606,13 +606,13 @@ int snd_pcm_status(struct snd_pcm_substream *substream,if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING || runtime->status->state ==SNDRV_PCM_STATE_DRAINING ) { status->delay = runtime->buffer_size - status->avail; - status->delay += runtime->delay; + status->delay += runtime->delay + runtime->soc_delay; }else status->delay =0 ; }else { status->avail =snd_pcm_capture_avail (runtime);if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING ) - status->delay = status->avail + runtime->delay; + status->delay = status->avail + runtime->delay + runtime->soc_delay;else status->delay =0 ; }@@ -2442,7 +2442,7 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream, n =snd_pcm_playback_hw_avail (runtime); else n =snd_pcm_capture_avail (runtime); - n += runtime->delay; + n += runtime->delay + runtime->soc_delay;break ;case SNDRV_PCM_STATE_XRUN : err = -EPIPE ;Signature · 1 line
1.7.0.4
Jaroslav Kysela
maintainerQuoted 18 lines · Takashi Iwai
At Mon, 23 Jul 2012 15:36:37 +0530,
Quoted 8 lines · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.Quoted 7 lines
Signed-off-by Vinod Koul <[email protected]>--
Once we are okay with this approach, I will send a follow up patch which adds this notion in ASoC and uses this to computecpu_daidelay. Thecodec_daidelay along with FIFO delay fromcpu_daishould be added and reresented by today's notion of delay.
Quoted 4 lines
Hmm, it's confusing to have both delay and
soc_delayfields.And, if the XRUN detection is the only problem, we can provide a flag to correct avail with
runtime->delay.
I agree. The runtime->delay variable should handle all situations for queued samples.
But the point is - we have data in some FIFO, but the DMA ring buffer is empty. If you use the delay value in the snd_pcm_update_state(), you'll probably allow to transfer invalid samples to the FIFO (probably previous samples from the ring buffer, because application has not provided new ones). That was the purpose of the check.
Of course, if you stop DMA according the appl_ptr and wait on application to provide new data in the "FIFO time window", then the check should be updated for this type of hw.
Jaroslav
Signature · 3 lines
Jaroslav Kysela <[email protected]> Linux Kernel Sound Maintainer ALSA Project; Red Hat, Inc.
Quoted 9 lines · Jaroslav Kysela
mm, it's confusing to have both delay and
soc_delayfields.Quoted 3 lines
And, if the XRUN detection is the only problem, we can provide a flag to correct avail with
runtime->delay.I agree. The
runtime->delayvariable should handle all situations for queued samples.
But the point is - we have data in some FIFO, but the DMA ring buffer is empty.
In a DSP you can have input buffer (from the ring buffer), which is post processed and sent to output DMA buffer. This buffering is dynamic in nature and since input buffer is memory copy, it would buffer ahead of time unlike the output buffer which will run at PCM rate. The samples at input DMA and various modules doing DSP processing are still before output circular DMA and should be considered when we do xrun calculation.
Quoted 4 lines
If you use the delay value in the
snd_pcm_update_state(), you'll probably allow to transfer invalid samples to the FIFO (probably previous samples from the ring buffer, because application has not provided new ones). That was the purpose of the check.
Yes and the right one. But what about samples still there before DMA, this patch just attempts to represent those. not the ones at and after DMA, they are correctly represented today in runtime->delay.
Quoted 7 lines
Of course, if you stop DMA according the
appl_ptrand wait on application to provide new data in the "FIFO time window", then the check should be updated for this type of hw.Jaroslav
Signature · 1 line
~Vinod
Quoted 12 lines · Takashi Iwai
At Mon, 23 Jul 2012 15:36:37 +0530,
Quoted 10 lines · Vinod Koul
In many modern SoCs the audio DSP can buffer the PCM ring buffer data. Today we have no means to represent this buffering and ALSA wrongly detects an overrun when
hw_ptrreachesapp_ptrvalue, though DSP may still have some buffered data.This patch tries to add a new field "
soc_delay" to represent buffering done in DSPs. This value is also used for the xrun calculations in ALSA.Signed-off-by Vinod Koul <[email protected]>
Quoted 7 lines
Quoted 5 lines
--
Once we are okay with this approach, I will send a follow up patch which adds this notion in ASoC and uses this to computecpu_daidelay. Thecodec_daidelay along with FIFO delay fromcpu_daishould be added and reresented by today's notion of delay.Hmm, it's confusing to have both delay and
soc_delayfields.
Yes, I agree but delay doesn't represent this and we should still keep delay for knowing the delay in samples getting rendered. This should be sum of output DMA FIFO delay and codec delay
Quoted 3 lines
And, if the XRUN detection is the only problem, we can provide a flag to correct avail with
runtime->delay.
For start yes, but from a correctness perspective we should tell ALSA how much data is buffered.
It also helps when we would like to do rewind and forward (not handled in this patch yet)
Quoted 94 lines
Quoted 28 lines
--- include/sound/pcm.h | 1 + sound/core/pcm_lib.c | 14 +++++++++++--- sound/core/pcm_native.c | 6 +++--- 3 files changed, 15 insertions(+), 6 deletions(-)include/sound/pcm.h +1
-0@@ -281,6 +281,7 @@ struct snd_pcm_runtime {unsigned long hw_ptr_jiffies;/* Time when hw_ptr is updated */ unsigned long hw_ptr_buffer_jiffies;/* buffer time in jiffies */ snd_pcm_sframes_t delay;/* extra delay; typically FIFO size */ +snd_pcm_sframes_t soc_delay;/* extra delay; typically delay incurred in soc */ /* -- HW params -- */ snd_pcm_access_t access;/* access mode */ sound/core/pcm_lib.c +0
-0@@ -292,7 +292,15 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream,return -EPIPE ; } }else {sound/core/pcm_lib.c int snd_pcm_update_state(struct snd_pcm_substream *substream, · line 303
Quoted 10 lines
sound/core/pcm_lib.c +9
-1-if (avail >= runtime->stop_threshold) { +snd_pcm_uframes_t actual_avail; +if (avail < runtime->soc_delay) + actual_avail = avail; +else + actual_avail = avail - runtime->soc_delay; +if (actual_avail >= runtime->stop_threshold) { +snd_printd (KERN_ERR "avail > stop_threshold!!\n" ); +snd_printd (KERN_ERR "actual_avail %ld, avail %ld, soc_delay %ld!!\n" , + actual_avail, avail, runtime->soc_delay);Don't add debug prints here. This is no kernel error, and XRUN is already informed in
xrun()function.thanks,
Takashi
Quoted 47 lines
sound/core/pcm_lib.c +2
-2xrun (substream);return -EPIPE ; }@@ -440,9 +448,9 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,if (runtime->hw.info &SNDRV_PCM_INFO_BATCH ) goto no_jiffies_check; hdelta = delta; -if (hdelta < runtime->delay) +if (hdelta < (runtime->delay + runtime->soc_delay)) goto no_jiffies_check; - hdelta -= runtime->delay; + hdelta -= (runtime->delay + runtime->soc_delay); jdelta = curr_jiffies - runtime->hw_ptr_jiffies;if (((hdelta *HZ ) / runtime->rate) > jdelta +HZ /100 ) { delta = jdelta /sound/core/pcm_native.c +3
-3@@ -606,13 +606,13 @@ int snd_pcm_status(struct snd_pcm_substream *substream,if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING || runtime->status->state ==SNDRV_PCM_STATE_DRAINING ) { status->delay = runtime->buffer_size - status->avail; - status->delay += runtime->delay; + status->delay += runtime->delay + runtime->soc_delay; }else status->delay =0 ; }else { status->avail =snd_pcm_capture_avail (runtime);if (runtime->status->state ==SNDRV_PCM_STATE_RUNNING ) - status->delay = status->avail + runtime->delay; + status->delay = status->avail + runtime->delay + runtime->soc_delay;else status->delay =0 ; }@@ -2442,7 +2442,7 @@ static int snd_pcm_delay(struct snd_pcm_substream *substream, n =snd_pcm_playback_hw_avail (runtime); else n =snd_pcm_capture_avail (runtime); - n += runtime->delay; + n += runtime->delay + runtime->soc_delay;break ;case SNDRV_PCM_STATE_XRUN : err = -EPIPE ;Signature · 1 line
1.7.0.4
Signature · 1 line
~Vinod