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
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 ||
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;
}
.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,