2010-08-19 16:00:06 -05:00
|
|
|
/* $OpenBSD$ */
|
2010-08-19 15:38:45 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-04-04 09:07:14 -05:00
|
|
|
#include <time.h>
|
2010-08-19 15:38:45 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-04-16 04:12:41 -05:00
|
|
|
#include "debug.h"
|
2011-03-31 04:52:54 -05:00
|
|
|
#include "sio_priv.h"
|
2010-08-19 16:00:06 -05:00
|
|
|
#include "bsd-compat.h"
|
2010-08-19 15:38:45 -05:00
|
|
|
|
|
|
|
#define SIO_PAR_MAGIC 0x83b905a4
|
|
|
|
|
|
|
|
void
|
|
|
|
sio_initpar(struct sio_par *par)
|
|
|
|
{
|
|
|
|
memset(par, 0xff, sizeof(struct sio_par));
|
|
|
|
par->__magic = SIO_PAR_MAGIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sio_hdl *
|
2012-04-11 02:21:29 -05:00
|
|
|
sio_open(const char *str, unsigned int mode, int nbio)
|
2010-08-19 15:38:45 -05:00
|
|
|
{
|
2012-05-23 14:26:54 -05:00
|
|
|
static char devany[] = SIO_DEVANY;
|
2010-08-19 15:38:45 -05:00
|
|
|
struct sio_hdl *hdl;
|
|
|
|
|
2011-04-16 04:12:41 -05:00
|
|
|
#ifdef DEBUG
|
2013-11-12 02:49:28 -06:00
|
|
|
_sndio_debug_init();
|
2010-08-19 15:38:45 -05:00
|
|
|
#endif
|
|
|
|
if ((mode & (SIO_PLAY | SIO_REC)) == 0)
|
|
|
|
return NULL;
|
2012-05-23 14:26:54 -05:00
|
|
|
if (str == NULL) /* backward compat */
|
|
|
|
str = devany;
|
|
|
|
if (strcmp(str, devany) == 0 && !issetugid()) {
|
2010-08-19 15:38:45 -05:00
|
|
|
str = getenv("AUDIODEVICE");
|
2012-05-23 14:26:54 -05:00
|
|
|
if (str == NULL)
|
|
|
|
str = devany;
|
|
|
|
}
|
|
|
|
if (strcmp(str, devany) == 0) {
|
2015-12-09 04:13:11 -06:00
|
|
|
hdl = _sio_aucat_open("snd/0", mode, nbio);
|
2011-11-01 07:02:13 -05:00
|
|
|
if (hdl != NULL)
|
|
|
|
return hdl;
|
|
|
|
#if defined(USE_SUN)
|
2018-02-01 01:53:20 -06:00
|
|
|
return _sio_sun_open("rsnd/default", mode, nbio);
|
2016-11-03 02:04:47 -05:00
|
|
|
#elif defined(USE_OSS)
|
2018-02-01 01:53:20 -06:00
|
|
|
return _sio_oss_open("rsnd/default", mode, nbio);
|
2011-11-01 07:02:13 -05:00
|
|
|
#elif defined(USE_ALSA)
|
2018-02-01 01:53:20 -06:00
|
|
|
return _sio_alsa_open("rsnd/default", mode, nbio);
|
2011-11-01 07:02:13 -05:00
|
|
|
#else
|
2010-08-19 16:00:06 -05:00
|
|
|
return NULL;
|
2011-11-01 07:02:13 -05:00
|
|
|
#endif
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
2015-12-09 04:13:11 -06:00
|
|
|
if (_sndio_parsetype(str, "snd"))
|
|
|
|
return _sio_aucat_open(str, mode, nbio);
|
|
|
|
if (_sndio_parsetype(str, "rsnd"))
|
2011-11-01 07:02:13 -05:00
|
|
|
#if defined(USE_SUN)
|
2015-12-09 04:13:11 -06:00
|
|
|
return _sio_sun_open(str, mode, nbio);
|
2016-11-03 02:04:47 -05:00
|
|
|
#elif defined(USE_OSS)
|
|
|
|
return _sio_oss_open(str, mode, nbio);
|
2011-11-01 07:02:13 -05:00
|
|
|
#elif defined(USE_ALSA)
|
2015-12-09 04:13:11 -06:00
|
|
|
return _sio_alsa_open(str, mode, nbio);
|
|
|
|
#else
|
|
|
|
return NULL;
|
2011-11-01 07:02:13 -05:00
|
|
|
#endif
|
2010-08-19 15:38:45 -05:00
|
|
|
DPRINTF("sio_open: %s: unknown device type\n", str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-12 02:49:28 -06:00
|
|
|
_sio_create(struct sio_hdl *hdl, struct sio_ops *ops,
|
2012-04-11 02:21:29 -05:00
|
|
|
unsigned int mode, int nbio)
|
2010-08-19 15:38:45 -05:00
|
|
|
{
|
|
|
|
hdl->ops = ops;
|
|
|
|
hdl->mode = mode;
|
|
|
|
hdl->nbio = nbio;
|
|
|
|
hdl->started = 0;
|
|
|
|
hdl->eof = 0;
|
|
|
|
hdl->move_cb = NULL;
|
|
|
|
hdl->vol_cb = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sio_close(struct sio_hdl *hdl)
|
|
|
|
{
|
|
|
|
hdl->ops->close(hdl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_start(struct sio_hdl *hdl)
|
|
|
|
{
|
2013-04-04 09:07:14 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
struct timespec ts;
|
|
|
|
#endif
|
|
|
|
|
2010-08-19 15:38:45 -05:00
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_start: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (hdl->started) {
|
|
|
|
DPRINTF("sio_start: already started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-13 06:04:02 -06:00
|
|
|
hdl->cpos = 0;
|
|
|
|
hdl->rused = hdl->wused = 0;
|
2010-08-19 15:38:45 -05:00
|
|
|
if (!sio_getpar(hdl, &hdl->par))
|
|
|
|
return 0;
|
2014-02-13 06:04:02 -06:00
|
|
|
#ifdef DEBUG
|
|
|
|
hdl->pollcnt = 0;
|
2013-04-04 09:07:14 -05:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
hdl->start_nsec = 1000000000LL * ts.tv_sec + ts.tv_nsec;
|
2010-08-19 15:38:45 -05:00
|
|
|
#endif
|
2013-04-12 09:14:53 -05:00
|
|
|
hdl->rdrop = hdl->wsil = 0;
|
2010-08-19 15:38:45 -05:00
|
|
|
if (!hdl->ops->start(hdl))
|
|
|
|
return 0;
|
|
|
|
hdl->started = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_stop(struct sio_hdl *hdl)
|
|
|
|
{
|
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_stop: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!hdl->started) {
|
|
|
|
DPRINTF("sio_stop: not started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!hdl->ops->stop(hdl))
|
|
|
|
return 0;
|
|
|
|
#ifdef DEBUG
|
2014-02-13 06:04:02 -06:00
|
|
|
DPRINTFN(2, "libsndio: polls: %llu, samples = %llu\n",
|
|
|
|
hdl->pollcnt, hdl->cpos);
|
2010-08-19 15:38:45 -05:00
|
|
|
#endif
|
|
|
|
hdl->started = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_setpar(struct sio_hdl *hdl, struct sio_par *par)
|
|
|
|
{
|
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_setpar: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (par->__magic != SIO_PAR_MAGIC) {
|
2016-01-08 14:51:12 -06:00
|
|
|
DPRINTF("sio_setpar: uninitialized sio_par structure\n");
|
2010-08-19 15:38:45 -05:00
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (hdl->started) {
|
|
|
|
DPRINTF("sio_setpar: already started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (par->bufsz != ~0U) {
|
|
|
|
DPRINTF("sio_setpar: setting bufsz is deprecated\n");
|
|
|
|
par->appbufsz = par->bufsz;
|
2012-11-13 08:08:28 -06:00
|
|
|
par->bufsz = ~0U;
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
|
|
|
if (par->rate != ~0U && par->appbufsz == ~0U)
|
|
|
|
par->appbufsz = par->rate * 200 / 1000;
|
|
|
|
return hdl->ops->setpar(hdl, par);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_getpar(struct sio_hdl *hdl, struct sio_par *par)
|
|
|
|
{
|
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_getpar: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (hdl->started) {
|
|
|
|
DPRINTF("sio_getpar: already started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!hdl->ops->getpar(hdl, par)) {
|
|
|
|
par->__magic = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
par->__magic = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_getcap(struct sio_hdl *hdl, struct sio_cap *cap)
|
|
|
|
{
|
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_getcap: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (hdl->started) {
|
|
|
|
DPRINTF("sio_getcap: already started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return hdl->ops->getcap(hdl, cap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sio_psleep(struct sio_hdl *hdl, int event)
|
|
|
|
{
|
2011-05-06 13:04:34 -05:00
|
|
|
struct pollfd pfd[SIO_MAXNFDS];
|
2010-08-19 15:38:45 -05:00
|
|
|
int revents;
|
2012-10-06 05:25:25 -05:00
|
|
|
int nfds;
|
2010-08-19 15:38:45 -05:00
|
|
|
|
2011-05-06 13:04:34 -05:00
|
|
|
nfds = sio_nfds(hdl);
|
2012-10-05 10:07:18 -05:00
|
|
|
if (nfds > SIO_MAXNFDS) {
|
|
|
|
DPRINTF("sio_psleep: %d: too many descriptors\n", nfds);
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-08-19 15:38:45 -05:00
|
|
|
for (;;) {
|
2012-10-05 10:07:18 -05:00
|
|
|
nfds = sio_pollfd(hdl, pfd, event);
|
2011-05-06 13:04:34 -05:00
|
|
|
while (poll(pfd, nfds, -1) < 0) {
|
2010-08-19 15:38:45 -05:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
DPERROR("sio_psleep: poll");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-06 13:04:34 -05:00
|
|
|
revents = sio_revents(hdl, pfd);
|
2010-08-19 15:38:45 -05:00
|
|
|
if (revents & POLLHUP) {
|
|
|
|
DPRINTF("sio_psleep: hang-up\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (revents & event)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-04-12 09:14:53 -05:00
|
|
|
static int
|
|
|
|
sio_rdrop(struct sio_hdl *hdl)
|
|
|
|
{
|
|
|
|
#define DROP_NMAX 0x1000
|
|
|
|
static char dummy[DROP_NMAX];
|
|
|
|
ssize_t n, todo;
|
|
|
|
|
|
|
|
while (hdl->rdrop > 0) {
|
|
|
|
todo = hdl->rdrop;
|
|
|
|
if (todo > DROP_NMAX)
|
|
|
|
todo = DROP_NMAX;
|
|
|
|
n = hdl->ops->read(hdl, dummy, todo);
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
hdl->rdrop -= n;
|
2013-05-14 04:42:32 -05:00
|
|
|
DPRINTF("sio_rdrop: dropped %zu bytes\n", n);
|
2013-04-12 09:14:53 -05:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sio_wsil(struct sio_hdl *hdl)
|
|
|
|
{
|
|
|
|
#define ZERO_NMAX 0x1000
|
|
|
|
static char zero[ZERO_NMAX];
|
|
|
|
ssize_t n, todo;
|
|
|
|
|
|
|
|
while (hdl->wsil > 0) {
|
|
|
|
todo = hdl->wsil;
|
|
|
|
if (todo > ZERO_NMAX)
|
|
|
|
todo = ZERO_NMAX;
|
|
|
|
n = hdl->ops->write(hdl, zero, todo);
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
hdl->wsil -= n;
|
2013-05-14 04:42:32 -05:00
|
|
|
DPRINTF("sio_wsil: inserted %zu bytes\n", n);
|
2013-04-12 09:14:53 -05:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-19 15:38:45 -05:00
|
|
|
size_t
|
|
|
|
sio_read(struct sio_hdl *hdl, void *buf, size_t len)
|
|
|
|
{
|
2012-04-11 02:21:29 -05:00
|
|
|
unsigned int n;
|
2010-08-19 15:38:45 -05:00
|
|
|
char *data = buf;
|
2014-02-21 03:58:43 -06:00
|
|
|
size_t todo = len, maxread;
|
2010-08-19 15:38:45 -05:00
|
|
|
|
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_read: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!hdl->started || !(hdl->mode & SIO_REC)) {
|
|
|
|
DPRINTF("sio_read: recording not started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (todo == 0) {
|
|
|
|
DPRINTF("sio_read: zero length read ignored\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (todo > 0) {
|
2014-02-21 03:58:43 -06:00
|
|
|
if (!sio_rdrop(hdl))
|
|
|
|
return 0;
|
|
|
|
maxread = hdl->rused;
|
|
|
|
if (maxread > todo)
|
|
|
|
maxread = todo;
|
|
|
|
n = maxread > 0 ? hdl->ops->read(hdl, data, maxread) : 0;
|
2010-08-19 15:38:45 -05:00
|
|
|
if (n == 0) {
|
|
|
|
if (hdl->nbio || hdl->eof || todo < len)
|
|
|
|
break;
|
|
|
|
if (!sio_psleep(hdl, POLLIN))
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
data += n;
|
|
|
|
todo -= n;
|
2014-02-13 06:04:02 -06:00
|
|
|
hdl->rused -= n;
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
|
|
|
return len - todo;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
sio_write(struct sio_hdl *hdl, const void *buf, size_t len)
|
|
|
|
{
|
2012-04-11 02:21:29 -05:00
|
|
|
unsigned int n;
|
2010-08-19 15:38:45 -05:00
|
|
|
const unsigned char *data = buf;
|
2014-02-21 03:58:43 -06:00
|
|
|
size_t todo = len, maxwrite;
|
2010-08-19 15:38:45 -05:00
|
|
|
|
|
|
|
if (hdl->eof) {
|
|
|
|
DPRINTF("sio_write: eof\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!hdl->started || !(hdl->mode & SIO_PLAY)) {
|
|
|
|
DPRINTF("sio_write: playback not started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (todo == 0) {
|
|
|
|
DPRINTF("sio_write: zero length write ignored\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (todo > 0) {
|
2014-02-21 03:58:43 -06:00
|
|
|
if (!sio_wsil(hdl))
|
|
|
|
return 0;
|
|
|
|
maxwrite = hdl->par.bufsz * hdl->par.pchan * hdl->par.bps -
|
|
|
|
hdl->wused;
|
|
|
|
if (maxwrite > todo)
|
|
|
|
maxwrite = todo;
|
|
|
|
n = maxwrite > 0 ? hdl->ops->write(hdl, data, maxwrite) : 0;
|
2010-08-19 15:38:45 -05:00
|
|
|
if (n == 0) {
|
|
|
|
if (hdl->nbio || hdl->eof)
|
|
|
|
break;
|
|
|
|
if (!sio_psleep(hdl, POLLOUT))
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
data += n;
|
|
|
|
todo -= n;
|
2014-02-13 06:04:02 -06:00
|
|
|
hdl->wused += n;
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
|
|
|
return len - todo;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_nfds(struct sio_hdl *hdl)
|
|
|
|
{
|
2011-03-31 06:08:07 -05:00
|
|
|
return hdl->ops->nfds(hdl);
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_pollfd(struct sio_hdl *hdl, struct pollfd *pfd, int events)
|
|
|
|
{
|
|
|
|
if (hdl->eof)
|
|
|
|
return 0;
|
|
|
|
if (!hdl->started)
|
|
|
|
events = 0;
|
|
|
|
return hdl->ops->pollfd(hdl, pfd, events);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_revents(struct sio_hdl *hdl, struct pollfd *pfd)
|
|
|
|
{
|
|
|
|
int revents;
|
|
|
|
#ifdef DEBUG
|
2013-04-04 09:07:14 -05:00
|
|
|
struct timespec ts0, ts1;
|
2010-08-19 15:38:45 -05:00
|
|
|
|
2014-08-19 09:05:29 -05:00
|
|
|
if (_sndio_debug >= 4)
|
2013-04-04 09:07:14 -05:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts0);
|
2010-08-19 15:38:45 -05:00
|
|
|
#endif
|
|
|
|
if (hdl->eof)
|
|
|
|
return POLLHUP;
|
|
|
|
#ifdef DEBUG
|
|
|
|
hdl->pollcnt++;
|
|
|
|
#endif
|
|
|
|
revents = hdl->ops->revents(hdl, pfd);
|
|
|
|
if (!hdl->started)
|
|
|
|
return revents & POLLHUP;
|
|
|
|
#ifdef DEBUG
|
2014-02-13 06:04:02 -06:00
|
|
|
if (_sndio_debug >= 4) {
|
2013-04-04 09:07:14 -05:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
|
|
|
DPRINTF("%09lld: sio_revents: revents = 0x%x, took %lldns\n",
|
|
|
|
1000000000LL * ts0.tv_sec +
|
|
|
|
ts0.tv_nsec - hdl->start_nsec,
|
|
|
|
revents,
|
|
|
|
1000000000LL * (ts1.tv_sec - ts0.tv_sec) +
|
|
|
|
ts1.tv_nsec - ts0.tv_nsec);
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
|
|
|
#endif
|
2013-04-12 09:14:53 -05:00
|
|
|
if ((hdl->mode & SIO_PLAY) && !sio_wsil(hdl))
|
|
|
|
revents &= ~POLLOUT;
|
|
|
|
if ((hdl->mode & SIO_REC) && !sio_rdrop(hdl))
|
|
|
|
revents &= ~POLLIN;
|
2010-08-19 15:38:45 -05:00
|
|
|
return revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sio_eof(struct sio_hdl *hdl)
|
|
|
|
{
|
|
|
|
return hdl->eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sio_onmove(struct sio_hdl *hdl, void (*cb)(void *, int), void *addr)
|
|
|
|
{
|
|
|
|
if (hdl->started) {
|
|
|
|
DPRINTF("sio_onmove: already started\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hdl->move_cb = cb;
|
|
|
|
hdl->move_addr = addr;
|
|
|
|
}
|
|
|
|
|
2013-04-04 09:07:14 -05:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
2013-11-12 02:49:28 -06:00
|
|
|
_sio_printpos(struct sio_hdl *hdl)
|
2016-01-08 14:51:12 -06:00
|
|
|
{
|
2013-04-04 09:07:14 -05:00
|
|
|
struct timespec ts;
|
|
|
|
long long rpos, rdiff;
|
|
|
|
long long cpos, cdiff;
|
|
|
|
long long wpos, wdiff;
|
2014-02-13 06:04:02 -06:00
|
|
|
unsigned rbpf, wbpf, rround, wround;
|
2013-04-04 09:07:14 -05:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2014-02-13 08:10:56 -06:00
|
|
|
rbpf = (hdl->mode & SIO_REC) ? hdl->par.bps * hdl->par.rchan : 1;
|
|
|
|
wbpf = (hdl->mode & SIO_PLAY) ? hdl->par.bps * hdl->par.pchan : 1;
|
2014-02-13 06:04:02 -06:00
|
|
|
rround = hdl->par.round * rbpf;
|
|
|
|
wround = hdl->par.round * wbpf;
|
2013-04-04 09:07:14 -05:00
|
|
|
|
2016-01-08 14:51:12 -06:00
|
|
|
rpos = (hdl->mode & SIO_REC) ?
|
2014-02-13 06:04:02 -06:00
|
|
|
hdl->cpos * rbpf - hdl->rused : 0;
|
2013-04-04 09:07:14 -05:00
|
|
|
wpos = (hdl->mode & SIO_PLAY) ?
|
2014-02-13 06:04:02 -06:00
|
|
|
hdl->cpos * wbpf + hdl->wused : 0;
|
2013-04-04 09:07:14 -05:00
|
|
|
|
|
|
|
cdiff = hdl->cpos % hdl->par.round;
|
|
|
|
cpos = hdl->cpos / hdl->par.round;
|
|
|
|
if (cdiff > hdl->par.round / 2) {
|
|
|
|
cpos++;
|
|
|
|
cdiff = cdiff - hdl->par.round;
|
|
|
|
}
|
2014-02-13 06:04:02 -06:00
|
|
|
rdiff = rpos % rround;
|
|
|
|
rpos = rpos / rround;
|
|
|
|
if (rdiff > rround / 2) {
|
2013-04-04 09:07:14 -05:00
|
|
|
rpos++;
|
2014-02-13 06:04:02 -06:00
|
|
|
rdiff = rdiff - rround;
|
2013-04-04 09:07:14 -05:00
|
|
|
}
|
2014-02-13 06:04:02 -06:00
|
|
|
wdiff = wpos % wround;
|
|
|
|
wpos = wpos / wround;
|
|
|
|
if (wdiff > wround / 2) {
|
2013-04-04 09:07:14 -05:00
|
|
|
wpos++;
|
2014-02-13 06:04:02 -06:00
|
|
|
wdiff = wdiff - wround;
|
2013-04-04 09:07:14 -05:00
|
|
|
}
|
|
|
|
DPRINTF("%011lld: "
|
|
|
|
"clk %+5lld%+5lld, wr %+5lld%+5lld rd: %+5lld%+5lld\n",
|
|
|
|
1000000000LL * ts.tv_sec + ts.tv_nsec - hdl->start_nsec,
|
|
|
|
cpos, cdiff, wpos, wdiff, rpos, rdiff);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-19 15:38:45 -05:00
|
|
|
void
|
2013-11-12 02:49:28 -06:00
|
|
|
_sio_onmove_cb(struct sio_hdl *hdl, int delta)
|
2010-08-19 15:38:45 -05:00
|
|
|
{
|
2013-04-04 09:07:14 -05:00
|
|
|
hdl->cpos += delta;
|
2014-02-13 06:04:02 -06:00
|
|
|
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
|
2013-12-10 13:23:11 -06:00
|
|
|
if (_sndio_debug >= 3)
|
2013-11-12 02:49:28 -06:00
|
|
|
_sio_printpos(hdl);
|
2014-02-21 03:58:43 -06:00
|
|
|
if ((hdl->mode & SIO_PLAY) && hdl->wused < 0) {
|
|
|
|
DPRINTFN(1, "sndio: h/w failure: negative buffer usage\n");
|
|
|
|
hdl->eof = 1;
|
|
|
|
return;
|
|
|
|
}
|
2010-08-19 15:38:45 -05:00
|
|
|
#endif
|
|
|
|
if (hdl->move_cb)
|
|
|
|
hdl->move_cb(hdl->move_addr, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-04-11 02:21:29 -05:00
|
|
|
sio_setvol(struct sio_hdl *hdl, unsigned int ctl)
|
2010-08-19 15:38:45 -05:00
|
|
|
{
|
|
|
|
if (hdl->eof)
|
|
|
|
return 0;
|
2010-10-25 02:45:16 -05:00
|
|
|
if (!hdl->ops->setvol)
|
|
|
|
return 1;
|
2010-08-19 15:38:45 -05:00
|
|
|
if (!hdl->ops->setvol(hdl, ctl))
|
|
|
|
return 0;
|
|
|
|
hdl->ops->getvol(hdl);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-10-25 02:45:16 -05:00
|
|
|
int
|
2012-04-11 02:21:29 -05:00
|
|
|
sio_onvol(struct sio_hdl *hdl, void (*cb)(void *, unsigned int), void *addr)
|
2010-08-19 15:38:45 -05:00
|
|
|
{
|
|
|
|
if (hdl->started) {
|
|
|
|
DPRINTF("sio_onvol: already started\n");
|
|
|
|
hdl->eof = 1;
|
2010-10-25 02:45:16 -05:00
|
|
|
return 0;
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
2010-10-25 02:45:16 -05:00
|
|
|
if (!hdl->ops->setvol)
|
|
|
|
return 0;
|
2010-08-19 15:38:45 -05:00
|
|
|
hdl->vol_cb = cb;
|
|
|
|
hdl->vol_addr = addr;
|
2010-10-30 07:46:11 -05:00
|
|
|
hdl->ops->getvol(hdl);
|
2010-10-25 02:45:16 -05:00
|
|
|
return 1;
|
2010-08-19 15:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-12 02:49:28 -06:00
|
|
|
_sio_onvol_cb(struct sio_hdl *hdl, unsigned int ctl)
|
2010-08-19 15:38:45 -05:00
|
|
|
{
|
|
|
|
if (hdl->vol_cb)
|
|
|
|
hdl->vol_cb(hdl->vol_addr, ctl);
|
|
|
|
}
|