1
0
mirror of https://github.com/ericonr/sndio.git synced 2024-02-18 04:45:21 -06:00

rework xrun recovery code

This commit is contained in:
Alexandre Ratchov 2012-11-08 16:58:46 +01:00
parent 4ee0a617be
commit 2d4a336e1d

View File

@ -330,6 +330,9 @@ sio_alsa_printpos(struct sio_alsa_hdl *hdl)
long long cpos, cdiff; long long cpos, cdiff;
long long wpos, wdiff; long long wpos, wdiff;
rpos = hdl->rpos + hdl->idrop;
wpos = hdl->wpos + hdl->osil;
cdiff = hdl->cpos % hdl->par.round; cdiff = hdl->cpos % hdl->par.round;
cpos = hdl->cpos / hdl->par.round; cpos = hdl->cpos / hdl->par.round;
if (cdiff > hdl->par.round / 2) { if (cdiff > hdl->par.round / 2) {
@ -337,20 +340,22 @@ sio_alsa_printpos(struct sio_alsa_hdl *hdl)
cdiff = cdiff - hdl->par.round; cdiff = cdiff - hdl->par.round;
} }
rdiff = hdl->rpos % hdl->par.round; rdiff = rpos % hdl->par.round;
rpos = hdl->rpos / hdl->par.round; rpos = rpos / hdl->par.round;
if (rdiff > hdl->par.round / 2) { if (rdiff > hdl->par.round / 2) {
rpos++; rpos++;
rdiff = rdiff - hdl->par.round; rdiff = rdiff - hdl->par.round;
} }
wdiff = hdl->wpos % hdl->par.round; wdiff = wpos % hdl->par.round;
wpos = hdl->wpos / hdl->par.round; wpos = wpos / hdl->par.round;
if (wdiff > hdl->par.round / 2) { if (wdiff > hdl->par.round / 2) {
wpos++; wpos++;
wdiff = wdiff - hdl->par.round; wdiff = wdiff - hdl->par.round;
} }
//DPRINTF("iused=%d idelta=%d oused=%d, odelta=%d\n",
// hdl->iused, hdl->idelta, hdl->oused, hdl->odelta);
DPRINTF("clk: %+4lld %+4lld, wr %+4lld %+4lld rd: %+4lld %+4lld\n", DPRINTF("clk: %+4lld %+4lld, wr %+4lld %+4lld rd: %+4lld %+4lld\n",
cpos, cdiff, wpos, wdiff, rpos, rdiff); cpos, cdiff, wpos, wdiff, rpos, rdiff);
} }
@ -444,68 +449,57 @@ sio_alsa_stop(struct sio_hdl *sh)
return 0; return 0;
} }
} }
DPRINTF("stopped"); DPRINTF("sio_alsa_stop: stopped\n");
return 1; return 1;
} }
static int static int
sio_alsa_xrun(struct sio_alsa_hdl *hdl) sio_alsa_xrun(struct sio_alsa_hdl *hdl)
{ {
int wdiff, cdiff, rdiff, offs; long long wpos, rpos;
int wdiff, cdiff, rdiff;
int wsil, rdrop, cmove;
DPRINTF("- - - - - - - - - - - - - - - - - - - - - xrun begin\n"); DPRINTF("- - - - - - - - - - - - - - - - - - - - - xrun begin\n");
DPRINTF("osil = %d, odrop = %d, odelta = %d, idelta = %d\n", hdl->osil, hdl->idrop, hdl->odelta, hdl->idelta);
sio_alsa_printpos(hdl); sio_alsa_printpos(hdl);
rpos = (hdl->sio.mode & SIO_REC) ? hdl->rpos : hdl->cpos;
wpos = (hdl->sio.mode & SIO_PLAY) ? hdl->wpos : hdl->cpos;
cdiff = hdl->par.round - (hdl->cpos % hdl->par.round); cdiff = hdl->par.round - (hdl->cpos % hdl->par.round);
if (cdiff == hdl->par.round) if (cdiff == hdl->par.round)
cdiff = 0; cdiff = 0;
if (cdiff > 0) {
hdl->cpos += cdiff;
sio_onmove_cb(&hdl->sio, cdiff);
}
DPRINTF("xrun: tick "); rdiff = rpos % hdl->par.round;
sio_alsa_printpos(hdl);
wdiff = hdl->par.round - (wpos % hdl->par.round);
if (hdl->sio.mode & SIO_PLAY) { if (wdiff == hdl->par.round)
wdiff = hdl->wpos - hdl->cpos; wdiff = 0;
if (hdl->sio.mode & SIO_REC)
offs = hdl->wpos - hdl->rpos; DPRINTF("rdiff = %d, cdiff = %d, wdiff = %d\n", rdiff, cdiff, wdiff);
} else {
rdiff = hdl->par.bufsz - (hdl->cpos - hdl->rpos); wsil = rdiff + wpos - rpos;
} rdrop = rdiff;
cmove = -(rdiff + hdl->cpos - rpos);
DPRINTF("wsil = %d, cmove = %d, rdrop = %d\n", wsil, cmove, rdrop);
if (!sio_alsa_stop(&hdl->sio)) if (!sio_alsa_stop(&hdl->sio))
return 0; return 0;
if (!sio_alsa_start(&hdl->sio)) if (!sio_alsa_start(&hdl->sio))
return 0; return 0;
hdl->running = 1;
if (hdl->sio.mode & SIO_PLAY) { if (hdl->sio.mode & SIO_PLAY) {
if (hdl->sio.mode & SIO_REC) { hdl->osil = wsil;
while (wdiff < offs) { hdl->odelta -= cmove;
wdiff += hdl->par.round; }
hdl->odelta -= hdl->par.round; if (hdl->sio.mode & SIO_REC) {
hdl->idelta -= hdl->par.round; hdl->idrop = rdrop;
hdl->cpos += hdl->par.round; hdl->idelta -= rdiff;
}
}
DPRINTF("xrun: inserting silence: %d\n", wdiff);
hdl->osil = wdiff;
if (hdl->sio.mode & SIO_REC)
hdl->idrop = wdiff - offs;
} else {
while (rdiff > (int)hdl->par.bufsz) {
rdiff -= hdl->par.round;
hdl->idelta -= hdl->par.round;
hdl->cpos += hdl->par.round;
}
DPRINTF("xrun: dropping: %d\n", rdiff);
hdl->idrop = rdiff;
} }
DPRINTF("xrun: corrected\n"); DPRINTF("xrun: corrected\n");
DPRINTF("osil = %d, odrop = %d, odelta = %d, idelta = %d\n", hdl->osil, hdl->idrop, hdl->odelta, hdl->idelta); DPRINTF("osil = %d, odrop = %d, odelta = %d, idelta = %d\n",
hdl->osil, hdl->idrop, hdl->odelta, hdl->idelta);
sio_alsa_printpos(hdl); sio_alsa_printpos(hdl);
DPRINTF("- - - - - - - - - - - - - - - - - - - - - xrun end\n"); DPRINTF("- - - - - - - - - - - - - - - - - - - - - xrun end\n");
return 1; return 1;
@ -947,6 +941,9 @@ sio_alsa_rdrop(struct sio_alsa_hdl *hdl)
static char buf[DROP_NMAX]; static char buf[DROP_NMAX];
ssize_t n, todo, max; ssize_t n, todo, max;
if (hdl->idrop == 0)
return 1;
max = DROP_NMAX / hdl->ibpf; max = DROP_NMAX / hdl->ibpf;
while (hdl->idrop > 0) { while (hdl->idrop > 0) {
todo = hdl->idrop; todo = hdl->idrop;
@ -974,10 +971,18 @@ sio_alsa_rdrop(struct sio_alsa_hdl *hdl)
#ifdef DEBUG #ifdef DEBUG
hdl->rpos += n; hdl->rpos += n;
#endif #endif
hdl->idelta += n; /*
* dropping samples is clock-wise neutral, so we
* should not bump hdl->idelta += n; but since
* we dropped samples, kernel buffer usage changed
* and we have to take this into account here, to
* prevent sio_alsa_revents() interpretin iused
* change as clock tick
*/
hdl->iused -= n;
DPRINTF("sio_alsa_rdrop: dropped %zu/%zu frames\n", n, todo); DPRINTF("sio_alsa_rdrop: dropped %zu/%zu frames\n", n, todo);
} }
return 1; return 0;
} }
static size_t static size_t
@ -1020,10 +1025,13 @@ sio_alsa_read(struct sio_hdl *sh, void *buf, size_t len)
static int static int
sio_alsa_wsil(struct sio_alsa_hdl *hdl) sio_alsa_wsil(struct sio_alsa_hdl *hdl)
{ {
#define ZERO_NMAX 0x1000 #define ZERO_NMAX 0x10000
static char zero[ZERO_NMAX]; static char zero[ZERO_NMAX];
ssize_t n, todo, max; ssize_t n, todo, max;
if (hdl->osil == 0)
return 1;
max = ZERO_NMAX / hdl->obpf; max = ZERO_NMAX / hdl->obpf;
while (hdl->osil > 0) { while (hdl->osil > 0) {
todo = hdl->osil; todo = hdl->osil;
@ -1045,11 +1053,14 @@ sio_alsa_wsil(struct sio_alsa_hdl *hdl)
#ifdef DEBUG #ifdef DEBUG
hdl->wpos += n; hdl->wpos += n;
#endif #endif
hdl->odelta += n; /*
* be clock-wise neutral, see end of sio_alsa_wsil()
*/
hdl->oused += n;
hdl->osil -= n; hdl->osil -= n;
DPRINTF("sio_alsa_wsil: inserted %zu/%zu frames\n", n, todo); DPRINTF("sio_alsa_wsil: inserted %zu/%zu frames\n", n, todo);
} }
return 1; return 0;
} }
static size_t static size_t
@ -1058,8 +1069,11 @@ sio_alsa_write(struct sio_hdl *sh, const void *buf, size_t len)
struct sio_alsa_hdl *hdl = (struct sio_alsa_hdl *)sh; struct sio_alsa_hdl *hdl = (struct sio_alsa_hdl *)sh;
ssize_t n, todo; ssize_t n, todo;
if (!sio_alsa_wsil(hdl)) if (hdl->osil) {
if (!sio_alsa_wsil(hdl))
return 0;
return 0; return 0;
}
if (len < hdl->obpf) { if (len < hdl->obpf) {
/* /*
* we can't just return, because sio_write() will loop * we can't just return, because sio_write() will loop
@ -1094,6 +1108,38 @@ sio_alsa_write(struct sio_hdl *sh, const void *buf, size_t len)
return n; return n;
} }
void
sio_alsa_onmove(struct sio_alsa_hdl *hdl)
{
int delta;
switch (hdl->sio.mode & (SIO_PLAY | SIO_REC)) {
case SIO_PLAY:
delta = hdl->odelta;
break;
case SIO_REC:
delta = hdl->idelta;
break;
case SIO_PLAY | SIO_REC:
delta = hdl->odelta > hdl->idelta ? hdl->odelta : hdl->idelta;
break;
}
if (delta < 0)
return;
if (delta == 0 && hdl->running)
return;
#ifdef DEBUG
hdl->cpos += delta;
if (sndio_debug >= 1)
sio_alsa_printpos(hdl);
#endif
sio_onmove_cb(&hdl->sio, delta);
if (hdl->sio.mode & SIO_PLAY)
hdl->odelta -= delta;
if (hdl->sio.mode & SIO_REC)
hdl->idelta -= delta;
}
static int static int
sio_alsa_nfds(struct sio_hdl *sh) sio_alsa_nfds(struct sio_hdl *sh)
{ {
@ -1113,11 +1159,12 @@ sio_alsa_pollfd(struct sio_hdl *sh, struct pollfd *pfd, int events)
hdl->events = events; hdl->events = events;
memset(pfd, 0, sizeof(struct pollfd) * hdl->nfds); memset(pfd, 0, sizeof(struct pollfd) * hdl->nfds);
if ((events & POLLOUT) && (hdl->sio.mode & SIO_PLAY) && hdl->sio.started) { if ((events & POLLOUT) && (hdl->sio.mode & SIO_PLAY) &&
hdl->sio.started) {
if (!hdl->running && if (!hdl->running &&
snd_pcm_state(hdl->opcm) == SND_PCM_STATE_RUNNING) { snd_pcm_state(hdl->opcm) == SND_PCM_STATE_RUNNING) {
sio_alsa_onmove(hdl);
hdl->running = 1; hdl->running = 1;
sio_onmove_cb(&hdl->sio, 0);
} }
hdl->onfds = snd_pcm_poll_descriptors(hdl->opcm, hdl->onfds = snd_pcm_poll_descriptors(hdl->opcm,
pfd, hdl->nfds); pfd, hdl->nfds);
@ -1128,11 +1175,12 @@ sio_alsa_pollfd(struct sio_hdl *sh, struct pollfd *pfd, int events)
} }
} else } else
hdl->onfds = 0; hdl->onfds = 0;
if ((events & POLLIN) && (hdl->sio.mode & SIO_REC) && hdl->sio.started) { if ((events & POLLIN) && (hdl->sio.mode & SIO_REC) &&
hdl->sio.started) {
if (!hdl->running && if (!hdl->running &&
snd_pcm_state(hdl->ipcm) == SND_PCM_STATE_RUNNING) { snd_pcm_state(hdl->ipcm) == SND_PCM_STATE_RUNNING) {
sio_alsa_onmove(hdl);
hdl->running = 1; hdl->running = 1;
sio_onmove_cb(&hdl->sio, 0);
} }
hdl->infds = snd_pcm_poll_descriptors(hdl->ipcm, hdl->infds = snd_pcm_poll_descriptors(hdl->ipcm,
pfd + hdl->onfds, hdl->nfds - hdl->onfds); pfd + hdl->onfds, hdl->nfds - hdl->onfds);
@ -1159,10 +1207,8 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
struct sio_alsa_hdl *hdl = (struct sio_alsa_hdl *)sh; struct sio_alsa_hdl *hdl = (struct sio_alsa_hdl *)sh;
snd_pcm_sframes_t iused, oavail, oused; snd_pcm_sframes_t iused, oavail, oused;
snd_pcm_state_t istate, ostate; snd_pcm_state_t istate, ostate;
int nfds;
unsigned short revents, r; unsigned short revents, r;
int delta, err; int nfds, err, i;
int i;
if (hdl->sio.eof) if (hdl->sio.eof)
return POLLHUP; return POLLHUP;
@ -1171,7 +1217,6 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
DPRINTFN(3, "sio_alsa_revents: pfds[%d].events = %x\n", DPRINTFN(3, "sio_alsa_revents: pfds[%d].events = %x\n",
i, pfd[i].revents); i, pfd[i].revents);
} }
revents = nfds = 0; revents = nfds = 0;
if ((hdl->events & POLLOUT) && (hdl->sio.mode & SIO_PLAY)) { if ((hdl->events & POLLOUT) && (hdl->sio.mode & SIO_PLAY)) {
ostate = snd_pcm_state(hdl->opcm); ostate = snd_pcm_state(hdl->opcm);
@ -1180,7 +1225,8 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
return POLLHUP; return POLLHUP;
return 0; return 0;
} }
err = snd_pcm_poll_descriptors_revents(hdl->opcm, pfd, hdl->onfds, &r); err = snd_pcm_poll_descriptors_revents(hdl->opcm,
pfd, hdl->onfds, &r);
if (err < 0) { if (err < 0) {
DALSA("couldn't get play events", err); DALSA("couldn't get play events", err);
hdl->sio.eof = 1; hdl->sio.eof = 1;
@ -1188,6 +1234,7 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
} }
revents |= r; revents |= r;
nfds += hdl->onfds; nfds += hdl->onfds;
} }
if ((hdl->events & POLLIN) && (hdl->sio.mode & SIO_REC)) { if ((hdl->events & POLLIN) && (hdl->sio.mode & SIO_REC)) {
istate = snd_pcm_state(hdl->ipcm); istate = snd_pcm_state(hdl->ipcm);
@ -1196,7 +1243,8 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
return POLLHUP; return POLLHUP;
return 0; return 0;
} }
err = snd_pcm_poll_descriptors_revents(hdl->ipcm, pfd + nfds, hdl->infds, &r); err = snd_pcm_poll_descriptors_revents(hdl->ipcm,
pfd + nfds, hdl->infds, &r);
if (err < 0) { if (err < 0) {
DALSA("couldn't get rec events", err); DALSA("couldn't get rec events", err);
hdl->sio.eof = 1; hdl->sio.eof = 1;
@ -1217,7 +1265,7 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
return POLLHUP; return POLLHUP;
return 0; return 0;
} }
DALSA("couldn't get play buffer pointer", oavail); DALSA("couldn't get play buffer ptr", oavail);
hdl->sio.eof = 1; hdl->sio.eof = 1;
return POLLHUP; return POLLHUP;
} }
@ -1235,26 +1283,14 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd)
return POLLHUP; return POLLHUP;
return 0; return 0;
} }
DALSA("couldn't get rec buffer pointer", iused); DALSA("couldn't get rec buffer ptr", iused);
hdl->sio.eof = 1; hdl->sio.eof = 1;
return POLLHUP; return POLLHUP;
} }
hdl->idelta += iused - hdl->iused; hdl->idelta += iused - hdl->iused;
hdl->iused = iused; hdl->iused = iused;
} }
delta = hdl->odelta > hdl->idelta ? hdl->odelta : hdl->idelta; sio_alsa_onmove(hdl);
if (delta > 0 && hdl->running) {
sio_onmove_cb(&hdl->sio, delta);
if (hdl->sio.mode & SIO_PLAY)
hdl->odelta -= delta;
if (hdl->sio.mode & SIO_REC)
hdl->idelta -= delta;
#ifdef DEBUG
hdl->cpos += delta;
if (sndio_debug >= 2)
sio_alsa_printpos(hdl);
#endif
}
} }
if ((hdl->sio.mode & SIO_PLAY) && !sio_alsa_wsil(hdl)) if ((hdl->sio.mode & SIO_PLAY) && !sio_alsa_wsil(hdl))
revents &= ~POLLOUT; revents &= ~POLLOUT;