ALSA: pcm: rewrite snd_pcm_playback_silence()
Subsystems (2)
PATCH v2 1/2 ALSA: pcm: rewrite snd_pcm_playback_silence()
The auto-silencer supports two modes: "thresholded" to fill up "just enough", and "top-up" to fill up "as much as possible". The two modes used rather distinct code paths, which this patch unifies. The only remaining distinction is how much we actually want to fill.
This fixes a bug in thresholded mode, where we failed to use new_hw_ptr, resulting in under-fill.
Top-up mode is now more well-behaved and much easier to understand in corner cases.
This also updates comments in the proximity of silencing-related data structures.
---
v2:
- removed useless boundary check
- got rid of casts by using a signed type for deltas.
i did not adjust the style of the conditionals, because it's not clear whether the hangup was actually over that, or merely over the casts.
- dropped use of C99 comments where the surroundings suggest it. (in the case of the interspersed multi-line comments, that doesn't look like an improvement to me at all ...)
- swapped the added and hw_avail calculation blocks to reduce subsequent churn. it's more logical that way anyway.
--- .../kernel-api/writing-an-alsa-driver.rst | 17 ++-- include/sound/pcm.h | 14 +-- include/uapi/sound/asound.h | 11 ++- sound/core/pcm_lib.c | 86 ++++++++----------- sound/core/pcm_local.h | 3 +- sound/core/pcm_native.c | 6 +- 6 files changed, 66 insertions(+), 71 deletions(-)
Documentation/sound/kernel-api/writing-an-alsa-driver.rst +11 -6
@@ -1577,14 +1577,19 @@ are the contents of this file: unsigned int period_step; unsigned int sleep_min; /* min ticks to sleep */ snd_pcm_uframes_t start_threshold; - snd_pcm_uframes_t stop_threshold; - snd_pcm_uframes_t silence_threshold; /* Silence filling happens when - noise is nearest than this */ - snd_pcm_uframes_t silence_size; /* Silence filling size */ + /* + * The following two thresholds alleviate playback buffer underruns; when + * hw_avail drops below the threshold, the respective action is triggered: + */ + snd_pcm_uframes_t stop_threshold; /* - stop playback */ + snd_pcm_uframes_t silence_threshold; /* - pre-fill buffer with silence */ + snd_pcm_uframes_t silence_size; /* max size of silence pre-fill; when >= boundary, + * fill played area with silence immediately */ snd_pcm_uframes_t boundary; /* pointers wrap point */ - snd_pcm_uframes_t silenced_start; - snd_pcm_uframes_t silenced_size; + /* internal data of auto-silencer */ + snd_pcm_uframes_t silence_start; /* starting pointer to silence area */ + snd_pcm_uframes_t silence_filled; /* size filled with silence */ snd_pcm_sync_id_t sync; /* hardware synchronization ID */
include/sound/pcm.h +7 -7
@@ -378,18 +378,18 @@ struct snd_pcm_runtime {unsigned int rate_den; unsigned int no_period_wakeup: 1; -/* -- SW params -- */ -int tstamp_mode;/* mmap timestamp is updated */ +/* -- SW params; see struct snd_pcm_sw_params for comments -- */ +int tstamp_mode;unsigned int period_step;snd_pcm_uframes_t start_threshold;snd_pcm_uframes_t stop_threshold; -snd_pcm_uframes_t silence_threshold;/* Silence filling happens when -noise is nearest than this */ -snd_pcm_uframes_t silence_size;/* Silence filling size */ -snd_pcm_uframes_t boundary;/* pointers wrap point */ +snd_pcm_uframes_t silence_threshold; +snd_pcm_uframes_t silence_size; +snd_pcm_uframes_t boundary; +/* internal data of auto-silencer */ snd_pcm_uframes_t silence_start;/* starting pointer to silence area */ -snd_pcm_uframes_t silence_filled;/* size filled with silence */ +snd_pcm_uframes_t silence_filled;/* already filled part of silence area */ union snd_pcm_sync_id sync;/* hardware synchronization ID */
include/uapi/sound/asound.h +8 -3
@@ -429,9 +429,14 @@ struct snd_pcm_sw_params {snd_pcm_uframes_t avail_min;/* min avail frames for wakeup */ snd_pcm_uframes_t xfer_align;/* obsolete: xfer size need to be a multiple */ snd_pcm_uframes_t start_threshold;/* min hw_avail frames for automatic start */ -snd_pcm_uframes_t stop_threshold;/* min avail frames for automatic stop */ -snd_pcm_uframes_t silence_threshold;/* min distance from noise for silence filling */ -snd_pcm_uframes_t silence_size;/* silence block size */ +/* +* The following two thresholds alleviate playback buffer underruns; when +* hw_avail drops below the threshold, the respective action is triggered: +*/ +snd_pcm_uframes_t stop_threshold;/* - stop playback */ +snd_pcm_uframes_t silence_threshold;/* - pre-fill buffer with silence */ +snd_pcm_uframes_t silence_size;/* max size of silence pre-fill; when >= boundary, +* fill played area with silence immediately */ snd_pcm_uframes_t boundary;/* pointers wrap point */ unsigned int proto;/* protocol version */ unsigned int tstamp_type;/* timestamp type (req. proto >= 2.0.12) */
sound/core/pcm_lib.c +36 -50
122 lines not on this page · raw message
sound/core/pcm_local.h +1 -2
@@ -29,8 +29,7 @@ int snd_pcm_update_state(struct snd_pcm_substream *substream, struct snd_pcm_runtime *runtime);int snd_pcm_update_hw_ptr (struct snd_pcm_substream *substream); -void snd_pcm_playback_silence (struct snd_pcm_substream *substream, -snd_pcm_uframes_t new_hw_ptr); +void snd_pcm_playback_silence (struct snd_pcm_substream *substream);static inline snd_pcm_uframes_t snd_pcm_avail (struct snd_pcm_substream *substream)
sound/core/pcm_native.c +3 -3
@@ -958,7 +958,7 @@ static int snd_pcm_sw_params(struct snd_pcm_substream *substream,if (snd_pcm_running (substream)) {if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK && runtime->silence_size >0 ) -snd_pcm_playback_silence (substream,ULONG_MAX ); +snd_pcm_playback_silence (substream); err =snd_pcm_update_state (substream, runtime); }snd_pcm_stream_unlock_irq (substream);
@@ -1455,7 +1455,7 @@ static void snd_pcm_post_start(struct snd_pcm_substream *substream,__snd_pcm_set_state (runtime, state);if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK && runtime->silence_size >0 ) -snd_pcm_playback_silence (substream,ULONG_MAX ); +snd_pcm_playback_silence (substream);snd_pcm_timer_notify (substream,SNDRV_TIMER_EVENT_MSTART ); }
@@ -1916,7 +1916,7 @@ static void snd_pcm_post_reset(struct snd_pcm_substream *substream, runtime->control->appl_ptr = runtime->status->hw_ptr;if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK && runtime->silence_size >0 ) -snd_pcm_playback_silence (substream,ULONG_MAX ); +snd_pcm_playback_silence (substream);snd_pcm_stream_unlock_irq (substream); }
Signature · 1 line
2.40.0.152.g15d061e6df
PATCH v2 2/2 ALSA: pcm: auto-fill buffer with silence when draining playback
Draining will always playback somewhat beyond the end of the filled buffer. This would produce artifacts if the user did not set up the auto-silencing machinery, which is an extremely easy mistake to make, as the API strongly suggests convenient fire-and-forget semantics. This patch makes it work out of the box.
Applying this unconditionally is uncontroversial for RW_ACCESS, as the buffer is fully controlled by the kernel in this case, which a) makes failure to set up silencing even more likely and b) no detrimental effects on user space are possible.
MMAP_ACCESS is a different matter:
- It can be argued that the user can be expected to know that the buffer needs to be padded one way or another. I dispute that; of the numerous resources I surveyed, only one mentioned this. Draining is a convenience function also in the mmap case - an application that wants to control things finely would just use start/stop and manage the timing itself.
- It can be argued that it's a bad thing to overwrite a buffer the user has access to without them explicitly requesting it. While technically true, I think that's only a hypothetical issue - applications can be expected to treat consumed samples as undefined data:
- The cases where playing back the same samples would be even useful and practical are extremely limited.
- Most user code uses cross-platform/-API abstractions, which makes it even less likely that they would get the idea that it's OK to re-use buffered samples.
So I think the trade-off between fixing numerous applications and potentially breaking some is skewed towards the former to the point that it's not even a question.
We do the filling even if the driver supports exact draining (SNDRV_PCM_TRIGGER_DRAIN), because a) the cost of filling two periods from time to time is negligible, so it's not worth complicating the code and b) so the behavior is consistent between drivers, so hypothetical problems with the mmap case would be easier to reproduce.
It would be possible to add an opt-in API to the kernel and leave actually enabling it to alsa-lib. However, this would add significant overall complexity, for no obvious gain.
---
v2:
- fill only up to two periods, to avoid undue load with big buffers
- added discussion to commit message
--- sound/core/pcm_lib.c | 47 +++++++++++++++++++++++++---------------- sound/core/pcm_native.c | 3 ++- 2 files changed, 31 insertions(+), 19 deletions(-)
sound/core/pcm_lib.c +29 -18
@@ -61,24 +61,35 @@ void snd_pcm_playback_silence(struct snd_pcm_substream *substream) runtime->silence_start = appl_ptr; } -// This will "legitimately" turn negative on underrun, and will be mangled -// into a huge number by the boundary crossing handling. The initial state -// might also be not quite sane. The code below MUST account for these cases. - hw_avail = appl_ptr - runtime->status->hw_ptr; -if (hw_avail <0 ) - hw_avail += runtime->boundary; +if (runtime->state ==SNDRV_PCM_STATE_DRAINING ) { +// We actually need only the next period boundary plus the FIFO size +// plus some slack for IRQ delays, but it's not worth calculating that. + frames = runtime->period_size *2 - runtime->silence_filled; +if (frames <=0 ) +return ; +// Impossible, unless the buffer has only one period. +if (frames > runtime->buffer_size) + frames = runtime->buffer_size; + }else { +// This will "legitimately" turn negative on underrun, and will be mangled +// into a huge number by the boundary crossing handling. The initial state +// might also be not quite sane. The code below MUST account for these cases. + hw_avail = appl_ptr - runtime->status->hw_ptr; +if (hw_avail <0 ) + hw_avail += runtime->boundary; - noise_dist = hw_avail + runtime->silence_filled; -if (runtime->silence_size < runtime->boundary) { - frames = runtime->silence_threshold - noise_dist; -if (frames <=0 ) -return ; -if (frames > runtime->silence_size) - frames = runtime->silence_size; - }else { - frames = runtime->buffer_size - noise_dist; -if (frames <=0 ) -return ; + noise_dist = hw_avail + runtime->silence_filled; +if (runtime->silence_size < runtime->boundary) { + frames = runtime->silence_threshold - noise_dist; +if (frames <=0 ) +return ; +if (frames > runtime->silence_size) + frames = runtime->silence_size; + }else { + frames = runtime->buffer_size - noise_dist; +if (frames <=0 ) +return ; + } }if (snd_BUG_ON (frames > runtime->buffer_size))
@@ -443,7 +454,7 @@ static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream, }if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK && - runtime->silence_size >0 ) + (runtime->silence_size >0 || runtime->state ==SNDRV_PCM_STATE_DRAINING ))snd_pcm_playback_silence (substream);update_audio_tstamp (substream, &curr_tstamp, &audio_tstamp);
sound/core/pcm_native.c +2 -1
@@ -1454,7 +1454,7 @@ static void snd_pcm_post_start(struct snd_pcm_substream *substream, runtime->rate;__snd_pcm_set_state (runtime, state);if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK && - runtime->silence_size >0 ) + (runtime->silence_size >0 || state ==SNDRV_PCM_STATE_DRAINING ))snd_pcm_playback_silence (substream);snd_pcm_timer_notify (substream,SNDRV_TIMER_EVENT_MSTART ); }
@@ -2045,6 +2045,7 @@ static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream,break ;case SNDRV_PCM_STATE_RUNNING :__snd_pcm_set_state (runtime,SNDRV_PCM_STATE_DRAINING ); +snd_pcm_playback_silence (substream);break ;case SNDRV_PCM_STATE_XRUN :__snd_pcm_set_state (runtime,SNDRV_PCM_STATE_SETUP );
Signature · 1 line
2.40.0.152.g15d061e6df
Jaroslav Kysela
maintainerRe: [PATCH v2 2/2] ALSA: pcm: auto-fill buffer with silence when draining playback
Quoted 5 lines · Oswald Buddenhagen
Draining will always playback somewhat beyond the end of the filled buffer. This would produce artifacts if the user did not set up the auto-silencing machinery, which is an extremely easy mistake to make, as the API strongly suggests convenient fire-and-forget semantics. This patch makes it work out of the box.
NACK. The initial implementation should be put to alsa-lib as discussed.
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Re: [PATCH v2 2/2] ALSA: pcm: auto-fill buffer with silence when draining playback
Quoted 9 lines · Jaroslav Kysela
Quoted 5 lines · Oswald Buddenhagen
Draining will always playback somewhat beyond the end of the filled buffer. This would produce artifacts if the user did not set up the auto-silencing machinery, which is an extremely easy mistake to make, as the API strongly suggests convenient fire-and-forget semantics. This patch makes it work out of the box.
NACK. The initial implementation should be put to alsa-lib as discussed.
as discussed, a user-space only implementation based on the current kernel api is not reasonable:
it could either enable auto-silencing on device open (which would be unreasonably expensive) or it could enable it on drain (and disable it once draining is done, which would be unreasonably complex due to needing to handle asynchronous draining completion).
so we would at least need a kernel api to enable silence-on-drain, which user space could apply on device open.
this would be easy enough to do, but i really don't see a point in adding that complexity, given that it should be always enabled, lest it won't have much of a real-world impact.
fwiw, i just realized that the argument against touching the mmap'd buffer is even weaker with the updated implementation, as it only clears as much as user space would have to clear anyway (pedantically, i could round it down to the end of a period rather than filling two whole periods, if that's the bit that convinces you).
regards
Jaroslav Kysela
maintainerRe: [PATCH v2 2/2] ALSA: pcm: auto-fill buffer with silence when draining playback
Quoted 8 lines · Oswald Buddenhagen
Quoted 7 lines · Jaroslav Kysela
Quoted 5 lines · Oswald Buddenhagen
Draining will always playback somewhat beyond the end of the filled buffer. This would produce artifacts if the user did not set up the auto-silencing machinery, which is an extremely easy mistake to make, as the API strongly suggests convenient fire-and-forget semantics. This patch makes it work out of the box.
Quoted 8 lines
Quoted 2 lines
NACK. The initial implementation should be put to alsa-lib as discussed.
as discussed, a user-space only implementation based on the current kernel api is not reasonable:
it could either enable auto-silencing on device open (which would be unreasonably expensive) or it could enable it on drain (and disable it once draining is done, which would be unreasonably complex due to needing to handle asynchronous draining completion).
I doubt. We should consider all solutions. The drain ends with the SETUP state, thus the application must call prepare again. We can restore the sw_params there for all types of i/o access (if the app does not reset sw_params itself). We can just set the silence_size (sw_params) in snd_pcm_hw_drain() and it's all.
Also, an interrupt can be "lost" or "merged" only for the small periods where the system is not able to handle the fast interrupts. For large periods, we should not assume that any of the interrupt is lost. Otherwise, it would break many things and the driver is really broken in this case. So the drain fill size should be updated for the big periods like "fill_align_to_last_period + 100ms" or so.
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Quoted 8 lines · Oswald Buddenhagen
The auto-silencer supports two modes: "thresholded" to fill up "just enough", and "top-up" to fill up "as much as possible". The two modes used rather distinct code paths, which this patch unifies. The only remaining distinction is how much we actually want to fill.
This fixes a bug in thresholded mode, where we failed to use
new_hw_ptr, resulting in under-fill.
Quoted 7 lines
Top-up mode is now more well-behaved and much easier to understand in corner cases.
This also updates comments in the proximity of silencing-related data structures.
Signed-off-by Oswald Buddenhagen <[email protected]>
Looks much better. Thanks.
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Quoted 17 lines · Oswald Buddenhagen
The auto-silencer supports two modes: "thresholded" to fill up "just enough", and "top-up" to fill up "as much as possible". The two modes used rather distinct code paths, which this patch unifies. The only remaining distinction is how much we actually want to fill.
This fixes a bug in thresholded mode, where we failed to use
new_hw_ptr, resulting in under-fill.Top-up mode is now more well-behaved and much easier to understand in corner cases.
This also updates comments in the proximity of silencing-related data structures.
Signed-off-by Oswald Buddenhagen <[email protected]>
Quoted 11 lines
---
v2:
- removed useless boundary check
- got rid of casts by using a signed type for deltas.
i did not adjust the style of the conditionals, because it's not clear whether the hangup was actually over that, or merely over the casts.
- dropped use of C99 comments where the surroundings suggest it. (in the case of the interspersed multi-line comments, that doesn't look like an improvement to me at all ...)
- swapped theaddedandhw_availcalculation blocks to reduce subsequent churn. it's more logical that way anyway.
Applied this one. For the second patch, let's discuss further.
thanks,
Takashi