]> git.alsa-project.org Git - alsa-lib.git/commitdiff
pcm: mmap_emul: account for pending frames in delay and guard avail
authorJaroslav Kysela <perex@perex.cz>
Mon, 30 Nov 2026 20:38:06 +0000 (21:38 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 30 Nov 2026 20:41:19 +0000 (21:41 +0100)
Add a delay op that adds the pending frames to the slave delay, so snd_pcm_delay()
and snd_pcm_status() agree after a short write.

Guard the status->avail subtraction against unsigned underflow if the slave
reports an inconsistent avail.

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
src/pcm/pcm_mmap_emul.c

index af942b35e84b70f755ef5a6418300454df796a5e..0fed4f114291e694c71d8192fbad80c114bb2b7c 100644 (file)
@@ -340,6 +340,26 @@ static snd_pcm_sframes_t snd_pcm_mmap_emul_avail_update(snd_pcm_t *pcm)
        return snd_pcm_mmap_avail(pcm);
 }
 
+/*
+ * Frames committed to the mmap_emul PCM but not yet written to the slave.
+ *
+ * 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. Report nothing pending then.
+ */
+static snd_pcm_sframes_t snd_pcm_mmap_emul_pending(snd_pcm_t *pcm,
+                                                  snd_pcm_uframes_t slave_appl_ptr)
+{
+       mmap_emul_t *map = pcm->private_data;
+       snd_pcm_sframes_t pending;
+
+       pending = pcm_frame_diff(map->appl_ptr, slave_appl_ptr, pcm->boundary);
+       if (pending > (snd_pcm_sframes_t)pcm->buffer_size)
+               pending = 0;
+       return pending;
+}
+
 /*
  * 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
@@ -360,16 +380,7 @@ static int snd_pcm_mmap_emul_status(snd_pcm_t *pcm, snd_pcm_status_t *status)
        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;
+       pending = snd_pcm_mmap_emul_pending(pcm, status->appl_ptr);
        status->appl_ptr = map->appl_ptr;
        /* The kernel status reports delay only when running or draining. */
        if (status->state == SND_PCM_STATE_RUNNING ||
@@ -377,9 +388,41 @@ static int snd_pcm_mmap_emul_status(snd_pcm_t *pcm, snd_pcm_status_t *status)
                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.
+        * less pending, so the slave avail is at least pending. Guard against
+        * underflow anyway, as status->avail is unsigned.
         */
-       status->avail -= pending;
+       if ((snd_pcm_uframes_t)pending > status->avail)
+               status->avail = 0;
+       else
+               status->avail -= pending;
+       return 0;
+}
+
+/*
+ * Match snd_pcm_mmap_emul_status(): snd_pcm_generic_delay() reports only the
+ * slave delay, so the frames not yet written to the slave have to be added
+ * here too, or snd_pcm_delay() and snd_pcm_status() disagree. As in the
+ * status op the kernel reports a delay only while running or draining.
+ */
+static int snd_pcm_mmap_emul_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
+{
+       mmap_emul_t *map = pcm->private_data;
+       snd_pcm_t *slave = map->gen.slave;
+       int err;
+
+       err = snd_pcm_delay(slave, delayp);
+       if (err < 0)
+               return err;
+       if (!map->mmap_emul || pcm->stream != SND_PCM_STREAM_PLAYBACK)
+               return 0;
+       switch (snd_pcm_state(slave)) {
+       case SND_PCM_STATE_RUNNING:
+       case SND_PCM_STATE_DRAINING:
+               *delayp += snd_pcm_mmap_emul_pending(pcm, *slave->appl.ptr);
+               break;
+       default:
+               break;
+       }
        return 0;
 }
 
@@ -418,7 +461,7 @@ static const snd_pcm_fast_ops_t snd_pcm_mmap_emul_fast_ops = {
        .status = snd_pcm_mmap_emul_status,
        .state = snd_pcm_generic_state,
        .hwsync = snd_pcm_generic_hwsync,
-       .delay = snd_pcm_generic_delay,
+       .delay = snd_pcm_mmap_emul_delay,
        .prepare = snd_pcm_mmap_emul_prepare,
        .reset = snd_pcm_mmap_emul_reset,
        .start = snd_pcm_generic_start,