From 665b61baa76055e999046df51e59b0fe2da34826 Mon Sep 17 00:00:00 2001 From: Andy Saul Date: Thu, 5 Nov 2026 14:17:10 +0000 Subject: [PATCH] pcm: mmap_emul: report own appl_ptr in status In mmap_emul mode the mmap_emul PCM keeps its own appl_ptr apart from the slave. A playback commit forwards it and then writes the new frames to the slave with sync_slave_write(). The commit returns the full size even when that write fails or comes up short, for example after an xrun. The status op was snd_pcm_generic_status(), which returns the slave appl_ptr. When a plugin such as softvol sits above mmap_emul, its appl_ptr follows the mmap_emul one. After a short write the two differ and snd_pcm_plugin_status() aborts on pcm_plugin.c: snd_pcm_plugin_status: Assertion `status->appl_ptr == *pcm->appl.ptr' failed. This happens with any hw PCM whose driver lacks mmap support when it is used through plug with softvol above it, on the first xrun followed by snd_pcm_status(). We hit it on an LG SoC whose ALSA driver only offers RW access, where mpv aborts on the first xrun. It also reproduces without hardware, with a linear PCM over mmap_emul over an ioplug slave that offers only RW_INTERLEAVED access and fails one transfer. Whether that transfer returns -EPIPE (state XRUN) or -EIO, the next snd_pcm_status() on the linear PCM aborts. Add a status op that reports the mmap_emul appl_ptr in emulation mode for playback. It takes the frames not yet written to the slave out of avail, and adds them to delay only while the stream is running or draining, because the kernel status reports no delay in other states. A count above the buffer size means the two appl_ptrs are out of sync, as after snd_pcm_reset() on a running stream, and is ignored. Non-emulated and capture streams are unchanged. Signed-off-by: Andy Saul --- src/pcm/pcm_mmap_emul.c | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/pcm/pcm_mmap_emul.c b/src/pcm/pcm_mmap_emul.c index 40821974b..af942b35e 100644 --- a/src/pcm/pcm_mmap_emul.c +++ b/src/pcm/pcm_mmap_emul.c @@ -340,6 +340,49 @@ static snd_pcm_sframes_t snd_pcm_mmap_emul_avail_update(snd_pcm_t *pcm) return snd_pcm_mmap_avail(pcm); } +/* + * In mmap_emul mode the appl_ptr of this PCM and of the slave are separate. + * A playback commit forwards our appl_ptr even when the write to the slave + * comes up short, for example after an xrun. Report our own appl_ptr so + * that plugins above us see the same value they track, and count the + * frames not yet written to the slave as queued. + */ +static int snd_pcm_mmap_emul_status(snd_pcm_t *pcm, snd_pcm_status_t *status) +{ + mmap_emul_t *map = pcm->private_data; + snd_pcm_t *slave = map->gen.slave; + snd_pcm_sframes_t pending; + int err; + + err = snd_pcm_status(slave, status); + if (err < 0) + return err; + if (!map->mmap_emul || pcm->stream != SND_PCM_STREAM_PLAYBACK) + return 0; + + pending = pcm_frame_diff(map->appl_ptr, status->appl_ptr, + pcm->boundary); + /* + * Our appl_ptr is never behind the slave one or more than a buffer + * ahead of it. snd_pcm_reset() on a running stream breaks that, + * because the slave keeps appl_ptr at hw_ptr % buffer_size while + * snd_pcm_mmap_emul_reset() sets ours to 0. Count nothing then. + */ + if (pending > (snd_pcm_sframes_t)pcm->buffer_size) + pending = 0; + status->appl_ptr = map->appl_ptr; + /* The kernel status reports delay only when running or draining. */ + if (status->state == SND_PCM_STATE_RUNNING || + status->state == SND_PCM_STATE_DRAINING) + status->delay += pending; + /* + * We never commit more than our own avail, which is the slave avail + * less pending, so the slave avail is at least pending. + */ + status->avail -= pending; + return 0; +} + static void snd_pcm_mmap_emul_dump(snd_pcm_t *pcm, snd_output_t *out) { mmap_emul_t *map = pcm->private_data; @@ -372,7 +415,7 @@ static const snd_pcm_ops_t snd_pcm_mmap_emul_ops = { }; static const snd_pcm_fast_ops_t snd_pcm_mmap_emul_fast_ops = { - .status = snd_pcm_generic_status, + .status = snd_pcm_mmap_emul_status, .state = snd_pcm_generic_state, .hwsync = snd_pcm_generic_hwsync, .delay = snd_pcm_generic_delay,