From 7a9ad77510f34a51a1a1d008119d616cd101d4a0 Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Thu, 13 Feb 2014 13:04:02 +0100 Subject: [PATCH] use relative read/write positions for debug printfs --- libsndio/sio.c | 51 ++++++++++++++++++++++++++------------------- libsndio/sio_alsa.c | 10 ++++----- libsndio/sio_priv.h | 8 +++---- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/libsndio/sio.c b/libsndio/sio.c index 20333b9..88d09af 100644 --- a/libsndio/sio.c +++ b/libsndio/sio.c @@ -124,10 +124,12 @@ sio_start(struct sio_hdl *hdl) hdl->eof = 1; return 0; } -#ifdef DEBUG + hdl->cpos = 0; + hdl->rused = hdl->wused = 0; if (!sio_getpar(hdl, &hdl->par)) return 0; - hdl->pollcnt = hdl->wcnt = hdl->rcnt = hdl->cpos = 0; +#ifdef DEBUG + hdl->pollcnt = 0; clock_gettime(CLOCK_MONOTONIC, &ts); hdl->start_nsec = 1000000000LL * ts.tv_sec + ts.tv_nsec; #endif @@ -153,8 +155,8 @@ sio_stop(struct sio_hdl *hdl) if (!hdl->ops->stop(hdl)) return 0; #ifdef DEBUG - DPRINTFN(2, "libsndio: polls: %llu, written = %llu, read: %llu\n", - hdl->pollcnt, hdl->wcnt, hdl->rcnt); + DPRINTFN(2, "libsndio: polls: %llu, samples = %llu\n", + hdl->pollcnt, hdl->cpos); #endif hdl->started = 0; return 1; @@ -328,9 +330,7 @@ sio_read(struct sio_hdl *hdl, void *buf, size_t len) } data += n; todo -= n; -#ifdef DEBUG - hdl->rcnt += n; -#endif + hdl->rused -= n; } return len - todo; } @@ -368,9 +368,7 @@ sio_write(struct sio_hdl *hdl, const void *buf, size_t len) } data += n; todo -= n; -#ifdef DEBUG - hdl->wcnt += n; -#endif + hdl->wused += n; } return len - todo; } @@ -410,7 +408,7 @@ sio_revents(struct sio_hdl *hdl, struct pollfd *pfd) if (!hdl->started) return revents & POLLHUP; #ifdef DEBUG - if (_sndio_debug >= 3) { + if (_sndio_debug >= 4) { clock_gettime(CLOCK_MONOTONIC, &ts1); DPRINTF("%09lld: sio_revents: revents = 0x%x, took %lldns\n", 1000000000LL * ts0.tv_sec + @@ -453,13 +451,18 @@ _sio_printpos(struct sio_hdl *hdl) long long rpos, rdiff; long long cpos, cdiff; long long wpos, wdiff; + unsigned rbpf, wbpf, rround, wround; clock_gettime(CLOCK_MONOTONIC, &ts); + rbpf = hdl->par.bps * hdl->par.rchan; + wbpf = hdl->par.bps * hdl->par.pchan; + rround = hdl->par.round * rbpf; + wround = hdl->par.round * wbpf; rpos = (hdl->mode & SIO_REC) ? - hdl->rcnt / (hdl->par.bps * hdl->par.rchan) : 0; + hdl->cpos * rbpf - hdl->rused : 0; wpos = (hdl->mode & SIO_PLAY) ? - hdl->wcnt / (hdl->par.bps * hdl->par.pchan) : 0; + hdl->cpos * wbpf + hdl->wused : 0; cdiff = hdl->cpos % hdl->par.round; cpos = hdl->cpos / hdl->par.round; @@ -467,17 +470,17 @@ _sio_printpos(struct sio_hdl *hdl) cpos++; cdiff = cdiff - hdl->par.round; } - rdiff = rpos % hdl->par.round; - rpos = rpos / hdl->par.round; - if (rdiff > hdl->par.round / 2) { + rdiff = rpos % rround; + rpos = rpos / rround; + if (rdiff > rround / 2) { rpos++; - rdiff = rdiff - hdl->par.round; + rdiff = rdiff - rround; } - wdiff = wpos % hdl->par.round; - wpos = wpos / hdl->par.round; - if (wdiff > hdl->par.round / 2) { + wdiff = wpos % wround; + wpos = wpos / wround; + if (wdiff > wround / 2) { wpos++; - wdiff = wdiff - hdl->par.round; + wdiff = wdiff - wround; } DPRINTF("%011lld: " "clk %+5lld%+5lld, wr %+5lld%+5lld rd: %+5lld%+5lld\n", @@ -489,8 +492,12 @@ _sio_printpos(struct sio_hdl *hdl) void _sio_onmove_cb(struct sio_hdl *hdl, int delta) { -#ifdef DEBUG hdl->cpos += delta; + if (hdl->mode & SIO_REC) + hdl->rused += delta * (hdl->par.bps * hdl->par.rchan); + if (hdl->mode & SIO_PLAY) + hdl->wused -= delta * (hdl->par.bps * hdl->par.pchan); +#ifdef DEBUG if (_sndio_debug >= 3) _sio_printpos(hdl); #endif diff --git a/libsndio/sio_alsa.c b/libsndio/sio_alsa.c index 7b7074f..b153cd5 100644 --- a/libsndio/sio_alsa.c +++ b/libsndio/sio_alsa.c @@ -475,9 +475,9 @@ sio_alsa_xrun(struct sio_alsa_hdl *hdl) _sio_printpos(&hdl->sio); rpos = (hdl->sio.mode & SIO_REC) ? - hdl->sio.rcnt / hdl->ibpf : hdl->sio.cpos; + hdl->sio.cpos - hdl->sio.rused / hdl->ibpf : hdl->sio.cpos; wpos = (hdl->sio.mode & SIO_PLAY) ? - hdl->sio.wcnt / hdl->obpf : hdl->sio.cpos; + hdl->sio.cpos + hdl->sio.wused / hdl->obpf : hdl->sio.cpos; cdiff = hdl->par.round - (hdl->sio.cpos % hdl->par.round); if (cdiff == hdl->par.round) @@ -1081,11 +1081,11 @@ sio_alsa_pollfd(struct sio_hdl *sh, struct pollfd *pfd, int events) } } else hdl->infds = 0; - DPRINTFN(3, "sio_alsa_pollfd: events = %x, nfds = %d + %d\n", + DPRINTFN(4, "sio_alsa_pollfd: events = %x, nfds = %d + %d\n", events, hdl->onfds, hdl->infds); for (i = 0; i < hdl->onfds + hdl->infds; i++) { - DPRINTFN(3, "sio_alsa_pollfd: pfds[%d].events = %x\n", + DPRINTFN(4, "sio_alsa_pollfd: pfds[%d].events = %x\n", i, pfd[i].events); } return hdl->onfds + hdl->infds; @@ -1104,7 +1104,7 @@ sio_alsa_revents(struct sio_hdl *sh, struct pollfd *pfd) return POLLHUP; for (i = 0; i < hdl->onfds + hdl->infds; i++) { - DPRINTFN(3, "sio_alsa_revents: pfds[%d].revents = %x\n", + DPRINTFN(4, "sio_alsa_revents: pfds[%d].revents = %x\n", i, pfd[i].revents); } revents = nfds = 0; diff --git a/libsndio/sio_priv.h b/libsndio/sio_priv.h index c3a9675..e599448 100644 --- a/libsndio/sio_priv.h +++ b/libsndio/sio_priv.h @@ -37,13 +37,13 @@ struct sio_hdl { int eof; /* true if error occured */ int rdrop; /* recorded bytes to drop */ int wsil; /* silence to play */ + int rused; /* bytes used in read buffer */ + int wused; /* bytes used in write buffer */ + long long cpos; /* clock since start */ + struct sio_par par; #ifdef DEBUG unsigned long long pollcnt; /* times sio_revents was called */ - long long wcnt; /* bytes written with sio_write() */ - long long rcnt; /* bytes read with sio_read() */ - long long cpos; long long start_nsec; - struct sio_par par; #endif };