2020-01-10 04:59:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Alexandre Ratchov <alex@caoua.org>
|
|
|
|
*
|
|
|
|
* This program is made available under an ISC-style license. See the
|
|
|
|
* accompanying file LICENSE for details.
|
|
|
|
*/
|
2022-08-05 07:28:17 +00:00
|
|
|
#include "cubeb-internal.h"
|
|
|
|
#include "cubeb/cubeb.h"
|
2023-11-24 11:14:39 +00:00
|
|
|
#include "cubeb_tracing.h"
|
2022-08-05 07:28:17 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <dlfcn.h>
|
2020-01-10 04:59:53 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sndio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2022-08-05 07:28:17 +00:00
|
|
|
#include <stdlib.h>
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
#if defined(CUBEB_SNDIO_DEBUG)
|
|
|
|
#define DPR(...) fprintf(stderr, __VA_ARGS__);
|
|
|
|
#else
|
2022-08-05 07:28:17 +00:00
|
|
|
#define DPR(...) \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
2020-01-10 04:59:53 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef DISABLE_LIBSNDIO_DLOPEN
|
|
|
|
#define WRAP(x) x
|
|
|
|
#else
|
2022-08-05 07:28:17 +00:00
|
|
|
#define WRAP(x) (*cubeb_##x)
|
|
|
|
#define LIBSNDIO_API_VISIT(X) \
|
|
|
|
X(sio_close) \
|
|
|
|
X(sio_eof) \
|
|
|
|
X(sio_getpar) \
|
|
|
|
X(sio_initpar) \
|
|
|
|
X(sio_nfds) \
|
|
|
|
X(sio_onmove) \
|
|
|
|
X(sio_open) \
|
|
|
|
X(sio_pollfd) \
|
|
|
|
X(sio_read) \
|
|
|
|
X(sio_revents) \
|
|
|
|
X(sio_setpar) \
|
|
|
|
X(sio_start) \
|
|
|
|
X(sio_stop) \
|
|
|
|
X(sio_write)
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
#define MAKE_TYPEDEF(x) static typeof(x) * cubeb_##x;
|
|
|
|
LIBSNDIO_API_VISIT(MAKE_TYPEDEF);
|
|
|
|
#undef MAKE_TYPEDEF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct cubeb_ops const sndio_ops;
|
|
|
|
|
|
|
|
struct cubeb {
|
|
|
|
struct cubeb_ops const * ops;
|
|
|
|
void * libsndio;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cubeb_stream {
|
|
|
|
/* Note: Must match cubeb_stream layout in cubeb.c. */
|
|
|
|
cubeb * context;
|
2022-08-05 07:28:17 +00:00
|
|
|
void * arg; /* user arg to {data,state}_cb */
|
2020-01-10 04:59:53 +00:00
|
|
|
/**/
|
2022-08-05 07:28:17 +00:00
|
|
|
pthread_t th; /* to run real-time audio i/o */
|
|
|
|
pthread_mutex_t mtx; /* protects hdl and pos */
|
|
|
|
struct sio_hdl * hdl; /* link us to sndio */
|
|
|
|
int mode; /* bitmap of SIO_{PLAY,REC} */
|
|
|
|
int active; /* cubec_start() called */
|
2023-11-24 11:14:39 +00:00
|
|
|
int conv; /* need float->s24 conversion */
|
2022-08-05 07:28:17 +00:00
|
|
|
unsigned char * rbuf; /* rec data consumed from here */
|
|
|
|
unsigned char * pbuf; /* play data is prepared here */
|
|
|
|
unsigned int nfr; /* number of frames in ibuf and obuf */
|
|
|
|
unsigned int rbpf; /* rec bytes per frame */
|
|
|
|
unsigned int pbpf; /* play bytes per frame */
|
|
|
|
unsigned int rchan; /* number of rec channels */
|
|
|
|
unsigned int pchan; /* number of play channels */
|
|
|
|
unsigned int nblks; /* number of blocks in the buffer */
|
|
|
|
uint64_t hwpos; /* frame number Joe hears right now */
|
|
|
|
uint64_t swpos; /* number of frames produced/consumed */
|
|
|
|
cubeb_data_callback data_cb; /* cb to preapare data */
|
|
|
|
cubeb_state_callback state_cb; /* cb to notify about state changes */
|
|
|
|
float volume; /* current volume */
|
2020-01-10 04:59:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2022-08-05 07:28:17 +00:00
|
|
|
s16_setvol(void * ptr, long nsamp, float volume)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2022-08-05 07:28:17 +00:00
|
|
|
int16_t * dst = ptr;
|
2020-01-10 04:59:53 +00:00
|
|
|
int32_t mult = volume * 32768;
|
|
|
|
int32_t s;
|
|
|
|
|
|
|
|
while (nsamp-- > 0) {
|
|
|
|
s = *dst;
|
|
|
|
s = (s * mult) >> 15;
|
|
|
|
*(dst++) = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-11-24 11:14:39 +00:00
|
|
|
float_to_s24(void * ptr, long nsamp, float volume)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2023-11-24 11:14:39 +00:00
|
|
|
int32_t * dst = ptr;
|
2022-08-05 07:28:17 +00:00
|
|
|
float * src = ptr;
|
2023-11-24 11:14:39 +00:00
|
|
|
float mult = volume * 8388608;
|
2020-01-10 04:59:53 +00:00
|
|
|
int s;
|
|
|
|
|
|
|
|
while (nsamp-- > 0) {
|
|
|
|
s = lrintf(*(src++) * mult);
|
2023-11-24 11:14:39 +00:00
|
|
|
if (s < -8388608)
|
|
|
|
s = -8388608;
|
|
|
|
else if (s > 8388607)
|
|
|
|
s = 8388607;
|
2020-01-10 04:59:53 +00:00
|
|
|
*(dst++) = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-11-24 11:14:39 +00:00
|
|
|
s24_to_float(void * ptr, long nsamp)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2023-11-24 11:14:39 +00:00
|
|
|
int32_t * src = ptr;
|
2022-08-05 07:28:17 +00:00
|
|
|
float * dst = ptr;
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
src += nsamp;
|
|
|
|
dst += nsamp;
|
|
|
|
while (nsamp-- > 0)
|
2023-11-24 11:14:39 +00:00
|
|
|
*(--dst) = (1. / 8388608) * *(--src);
|
2020-01-10 04:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
sndio_get_device()
|
|
|
|
{
|
2020-10-24 11:05:37 +00:00
|
|
|
#ifdef __linux__
|
2020-01-10 04:59:53 +00:00
|
|
|
/*
|
|
|
|
* On other platforms default to sndio devices,
|
|
|
|
* so cubebs other backends can be used instead.
|
|
|
|
*/
|
2022-08-05 07:28:17 +00:00
|
|
|
const char * dev = getenv("AUDIODEVICE");
|
2020-01-10 04:59:53 +00:00
|
|
|
if (dev == NULL || *dev == '\0')
|
2022-08-05 07:28:17 +00:00
|
|
|
return "snd/0";
|
2020-01-10 04:59:53 +00:00
|
|
|
return dev;
|
|
|
|
#else
|
|
|
|
return SIO_DEVANY;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_onmove(void * arg, int delta)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2022-08-05 07:28:17 +00:00
|
|
|
cubeb_stream * s = (cubeb_stream *)arg;
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
s->hwpos += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_mainloop(void * arg)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2022-08-05 07:28:17 +00:00
|
|
|
struct pollfd * pfds;
|
|
|
|
cubeb_stream * s = arg;
|
2020-01-10 04:59:53 +00:00
|
|
|
int n, eof = 0, prime, nfds, events, revents, state = CUBEB_STATE_STARTED;
|
|
|
|
size_t pstart = 0, pend = 0, rstart = 0, rend = 0;
|
|
|
|
long nfr;
|
|
|
|
|
2023-11-24 11:14:39 +00:00
|
|
|
CUBEB_REGISTER_THREAD("cubeb rendering thread");
|
|
|
|
|
2020-01-10 04:59:53 +00:00
|
|
|
nfds = WRAP(sio_nfds)(s->hdl);
|
2022-08-05 07:28:17 +00:00
|
|
|
pfds = calloc(nfds, sizeof(struct pollfd));
|
2023-11-24 11:14:39 +00:00
|
|
|
if (pfds == NULL) {
|
|
|
|
CUBEB_UNREGISTER_THREAD();
|
2022-08-05 07:28:17 +00:00
|
|
|
return NULL;
|
2023-11-24 11:14:39 +00:00
|
|
|
}
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
DPR("sndio_mainloop()\n");
|
|
|
|
s->state_cb(s, s->arg, CUBEB_STATE_STARTED);
|
|
|
|
pthread_mutex_lock(&s->mtx);
|
|
|
|
if (!WRAP(sio_start)(s->hdl)) {
|
|
|
|
pthread_mutex_unlock(&s->mtx);
|
|
|
|
free(pfds);
|
2023-11-24 11:14:39 +00:00
|
|
|
CUBEB_UNREGISTER_THREAD();
|
2020-01-10 04:59:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
DPR("sndio_mainloop(), started\n");
|
|
|
|
|
|
|
|
if (s->mode & SIO_PLAY) {
|
|
|
|
pstart = pend = s->nfr * s->pbpf;
|
|
|
|
prime = s->nblks;
|
|
|
|
if (s->mode & SIO_REC) {
|
|
|
|
memset(s->rbuf, 0, s->nfr * s->rbpf);
|
|
|
|
rstart = rend = s->nfr * s->rbpf;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prime = 0;
|
|
|
|
rstart = 0;
|
|
|
|
rend = s->nfr * s->rbpf;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!s->active) {
|
|
|
|
DPR("sndio_mainloop() stopped\n");
|
|
|
|
state = CUBEB_STATE_STOPPED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do we have a complete block? */
|
|
|
|
if ((!(s->mode & SIO_PLAY) || pstart == pend) &&
|
2022-08-05 07:28:17 +00:00
|
|
|
(!(s->mode & SIO_REC) || rstart == rend)) {
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
if (eof) {
|
|
|
|
DPR("sndio_mainloop() drained\n");
|
|
|
|
state = CUBEB_STATE_DRAINED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s->mode & SIO_REC) && s->conv)
|
2023-11-24 11:14:39 +00:00
|
|
|
s24_to_float(s->rbuf, s->nfr * s->rchan);
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
/* invoke call-back, it returns less that s->nfr if done */
|
|
|
|
pthread_mutex_unlock(&s->mtx);
|
|
|
|
nfr = s->data_cb(s, s->arg, s->rbuf, s->pbuf, s->nfr);
|
|
|
|
pthread_mutex_lock(&s->mtx);
|
|
|
|
if (nfr < 0) {
|
|
|
|
DPR("sndio_mainloop() cb err\n");
|
|
|
|
state = CUBEB_STATE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->swpos += nfr;
|
|
|
|
|
|
|
|
/* was this last call-back invocation (aka end-of-stream) ? */
|
|
|
|
if (nfr < s->nfr) {
|
|
|
|
|
|
|
|
if (!(s->mode & SIO_PLAY) || nfr == 0) {
|
|
|
|
state = CUBEB_STATE_DRAINED;
|
|
|
|
break;
|
2022-08-05 07:28:17 +00:00
|
|
|
}
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
/* need to write (aka drain) the partial play block we got */
|
|
|
|
pend = nfr * s->pbpf;
|
|
|
|
eof = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prime > 0)
|
|
|
|
prime--;
|
|
|
|
|
|
|
|
if (s->mode & SIO_PLAY) {
|
|
|
|
if (s->conv)
|
2023-11-24 11:14:39 +00:00
|
|
|
float_to_s24(s->pbuf, nfr * s->pchan, s->volume);
|
2020-01-10 04:59:53 +00:00
|
|
|
else
|
|
|
|
s16_setvol(s->pbuf, nfr * s->pchan, s->volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->mode & SIO_REC)
|
|
|
|
rstart = 0;
|
|
|
|
if (s->mode & SIO_PLAY)
|
|
|
|
pstart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
events = 0;
|
|
|
|
if ((s->mode & SIO_REC) && rstart < rend && prime == 0)
|
|
|
|
events |= POLLIN;
|
|
|
|
if ((s->mode & SIO_PLAY) && pstart < pend)
|
|
|
|
events |= POLLOUT;
|
|
|
|
nfds = WRAP(sio_pollfd)(s->hdl, pfds, events);
|
|
|
|
|
|
|
|
if (nfds > 0) {
|
|
|
|
pthread_mutex_unlock(&s->mtx);
|
|
|
|
n = poll(pfds, nfds, -1);
|
|
|
|
pthread_mutex_lock(&s->mtx);
|
|
|
|
if (n < 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
revents = WRAP(sio_revents)(s->hdl, pfds);
|
|
|
|
|
|
|
|
if (revents & POLLHUP) {
|
|
|
|
state = CUBEB_STATE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (revents & POLLOUT) {
|
|
|
|
n = WRAP(sio_write)(s->hdl, s->pbuf + pstart, pend - pstart);
|
|
|
|
if (n == 0 && WRAP(sio_eof)(s->hdl)) {
|
|
|
|
DPR("sndio_mainloop() werr\n");
|
|
|
|
state = CUBEB_STATE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pstart += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (revents & POLLIN) {
|
|
|
|
n = WRAP(sio_read)(s->hdl, s->rbuf + rstart, rend - rstart);
|
|
|
|
if (n == 0 && WRAP(sio_eof)(s->hdl)) {
|
|
|
|
DPR("sndio_mainloop() rerr\n");
|
|
|
|
state = CUBEB_STATE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rstart += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip rec block, if not recording (yet) */
|
|
|
|
if (prime > 0 && (s->mode & SIO_REC))
|
|
|
|
rstart = rend;
|
|
|
|
}
|
|
|
|
WRAP(sio_stop)(s->hdl);
|
|
|
|
s->hwpos = s->swpos;
|
|
|
|
pthread_mutex_unlock(&s->mtx);
|
|
|
|
s->state_cb(s, s->arg, state);
|
|
|
|
free(pfds);
|
2023-11-24 11:14:39 +00:00
|
|
|
CUBEB_UNREGISTER_THREAD();
|
2020-01-10 04:59:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*static*/ int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_init(cubeb ** context, char const * context_name)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
void * libsndio = NULL;
|
2022-08-05 07:28:17 +00:00
|
|
|
struct sio_hdl * hdl;
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
assert(context);
|
|
|
|
|
|
|
|
#ifndef DISABLE_LIBSNDIO_DLOPEN
|
|
|
|
libsndio = dlopen("libsndio.so.7.0", RTLD_LAZY);
|
|
|
|
if (!libsndio) {
|
|
|
|
libsndio = dlopen("libsndio.so", RTLD_LAZY);
|
|
|
|
if (!libsndio) {
|
|
|
|
DPR("sndio_init(%s) failed dlopen(libsndio.so)\n", context_name);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:28:17 +00:00
|
|
|
#define LOAD(x) \
|
|
|
|
{ \
|
|
|
|
cubeb_##x = dlsym(libsndio, #x); \
|
|
|
|
if (!cubeb_##x) { \
|
|
|
|
DPR("sndio_init(%s) failed dlsym(%s)\n", context_name, #x); \
|
|
|
|
dlclose(libsndio); \
|
|
|
|
return CUBEB_ERROR; \
|
|
|
|
} \
|
2020-01-10 04:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LIBSNDIO_API_VISIT(LOAD);
|
|
|
|
#undef LOAD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* test if sndio works */
|
|
|
|
hdl = WRAP(sio_open)(sndio_get_device(), SIO_PLAY, 1);
|
|
|
|
if (hdl == NULL) {
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
WRAP(sio_close)(hdl);
|
|
|
|
|
|
|
|
DPR("sndio_init(%s)\n", context_name);
|
|
|
|
*context = malloc(sizeof(**context));
|
|
|
|
if (*context == NULL)
|
2022-08-05 07:28:17 +00:00
|
|
|
return CUBEB_ERROR;
|
2020-01-10 04:59:53 +00:00
|
|
|
(*context)->libsndio = libsndio;
|
|
|
|
(*context)->ops = &sndio_ops;
|
|
|
|
(void)context_name;
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char const *
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_get_backend_id(cubeb * context)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
return "sndio";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_destroy(cubeb * context)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
DPR("sndio_destroy()\n");
|
2023-11-24 11:14:39 +00:00
|
|
|
#ifndef DISABLE_LIBSNDIO_DLOPEN
|
2020-01-10 04:59:53 +00:00
|
|
|
if (context->libsndio)
|
|
|
|
dlclose(context->libsndio);
|
2023-11-24 11:14:39 +00:00
|
|
|
#endif
|
2020-01-10 04:59:53 +00:00
|
|
|
free(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_stream_init(cubeb * context, cubeb_stream ** stream,
|
|
|
|
char const * stream_name, cubeb_devid input_device,
|
2020-01-10 04:59:53 +00:00
|
|
|
cubeb_stream_params * input_stream_params,
|
|
|
|
cubeb_devid output_device,
|
|
|
|
cubeb_stream_params * output_stream_params,
|
|
|
|
unsigned int latency_frames,
|
|
|
|
cubeb_data_callback data_callback,
|
2022-08-05 07:28:17 +00:00
|
|
|
cubeb_state_callback state_callback, void * user_ptr)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2022-08-05 07:28:17 +00:00
|
|
|
cubeb_stream * s;
|
2020-01-10 04:59:53 +00:00
|
|
|
struct sio_par wpar, rpar;
|
|
|
|
cubeb_sample_format format;
|
|
|
|
int rate;
|
|
|
|
size_t bps;
|
|
|
|
|
|
|
|
DPR("sndio_stream_init(%s)\n", stream_name);
|
|
|
|
|
|
|
|
s = malloc(sizeof(cubeb_stream));
|
|
|
|
if (s == NULL)
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
memset(s, 0, sizeof(cubeb_stream));
|
|
|
|
s->mode = 0;
|
|
|
|
if (input_stream_params) {
|
|
|
|
if (input_stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK) {
|
|
|
|
DPR("sndio_stream_init(), loopback not supported\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
s->mode |= SIO_REC;
|
|
|
|
format = input_stream_params->format;
|
|
|
|
rate = input_stream_params->rate;
|
|
|
|
}
|
|
|
|
if (output_stream_params) {
|
|
|
|
if (output_stream_params->prefs & CUBEB_STREAM_PREF_LOOPBACK) {
|
|
|
|
DPR("sndio_stream_init(), loopback not supported\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
s->mode |= SIO_PLAY;
|
|
|
|
format = output_stream_params->format;
|
|
|
|
rate = output_stream_params->rate;
|
|
|
|
}
|
|
|
|
if (s->mode == 0) {
|
|
|
|
DPR("sndio_stream_init(), neither playing nor recording\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
s->context = context;
|
|
|
|
s->hdl = WRAP(sio_open)(sndio_get_device(), s->mode, 1);
|
|
|
|
if (s->hdl == NULL) {
|
|
|
|
DPR("sndio_stream_init(), sio_open() failed\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
WRAP(sio_initpar)(&wpar);
|
|
|
|
wpar.sig = 1;
|
|
|
|
switch (format) {
|
|
|
|
case CUBEB_SAMPLE_S16LE:
|
|
|
|
wpar.le = 1;
|
2023-11-24 11:14:39 +00:00
|
|
|
wpar.bits = 16;
|
2020-01-10 04:59:53 +00:00
|
|
|
break;
|
|
|
|
case CUBEB_SAMPLE_S16BE:
|
|
|
|
wpar.le = 0;
|
2023-11-24 11:14:39 +00:00
|
|
|
wpar.bits = 16;
|
2020-01-10 04:59:53 +00:00
|
|
|
break;
|
|
|
|
case CUBEB_SAMPLE_FLOAT32NE:
|
|
|
|
wpar.le = SIO_LE_NATIVE;
|
2023-11-24 11:14:39 +00:00
|
|
|
wpar.bits = 24;
|
|
|
|
wpar.msb = 0;
|
2020-01-10 04:59:53 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DPR("sndio_stream_init() unsupported format\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2023-11-24 11:14:39 +00:00
|
|
|
wpar.bps = SIO_BPS(wpar.bits);
|
2020-01-10 04:59:53 +00:00
|
|
|
wpar.rate = rate;
|
|
|
|
if (s->mode & SIO_REC)
|
|
|
|
wpar.rchan = input_stream_params->channels;
|
|
|
|
if (s->mode & SIO_PLAY)
|
|
|
|
wpar.pchan = output_stream_params->channels;
|
|
|
|
wpar.appbufsz = latency_frames;
|
|
|
|
if (!WRAP(sio_setpar)(s->hdl, &wpar) || !WRAP(sio_getpar)(s->hdl, &rpar)) {
|
|
|
|
DPR("sndio_stream_init(), sio_setpar() failed\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2022-08-05 07:28:17 +00:00
|
|
|
if (rpar.bits != wpar.bits || rpar.le != wpar.le || rpar.sig != wpar.sig ||
|
2023-11-24 11:14:39 +00:00
|
|
|
rpar.bps != wpar.bps ||
|
|
|
|
(wpar.bits < 8 * wpar.bps && rpar.msb != wpar.msb) ||
|
2022-08-05 07:28:17 +00:00
|
|
|
rpar.rate != wpar.rate ||
|
2020-01-10 04:59:53 +00:00
|
|
|
((s->mode & SIO_REC) && rpar.rchan != wpar.rchan) ||
|
|
|
|
((s->mode & SIO_PLAY) && rpar.pchan != wpar.pchan)) {
|
|
|
|
DPR("sndio_stream_init() unsupported params\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
WRAP(sio_onmove)(s->hdl, sndio_onmove, s);
|
|
|
|
s->active = 0;
|
|
|
|
s->nfr = rpar.round;
|
|
|
|
s->rbpf = rpar.bps * rpar.rchan;
|
|
|
|
s->pbpf = rpar.bps * rpar.pchan;
|
|
|
|
s->rchan = rpar.rchan;
|
|
|
|
s->pchan = rpar.pchan;
|
|
|
|
s->nblks = rpar.bufsz / rpar.round;
|
|
|
|
s->data_cb = data_callback;
|
|
|
|
s->state_cb = state_callback;
|
|
|
|
s->arg = user_ptr;
|
|
|
|
s->mtx = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
s->hwpos = s->swpos = 0;
|
|
|
|
if (format == CUBEB_SAMPLE_FLOAT32LE) {
|
|
|
|
s->conv = 1;
|
|
|
|
bps = sizeof(float);
|
|
|
|
} else {
|
|
|
|
s->conv = 0;
|
|
|
|
bps = rpar.bps;
|
|
|
|
}
|
|
|
|
if (s->mode & SIO_PLAY) {
|
|
|
|
s->pbuf = malloc(bps * rpar.pchan * rpar.round);
|
|
|
|
if (s->pbuf == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (s->mode & SIO_REC) {
|
|
|
|
s->rbuf = malloc(bps * rpar.rchan * rpar.round);
|
|
|
|
if (s->rbuf == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
s->volume = 1.;
|
|
|
|
*stream = s;
|
|
|
|
DPR("sndio_stream_init() end, ok\n");
|
|
|
|
(void)context;
|
|
|
|
(void)stream_name;
|
|
|
|
return CUBEB_OK;
|
|
|
|
err:
|
|
|
|
if (s->hdl)
|
|
|
|
WRAP(sio_close)(s->hdl);
|
|
|
|
if (s->pbuf)
|
|
|
|
free(s->pbuf);
|
|
|
|
if (s->rbuf)
|
|
|
|
free(s->pbuf);
|
|
|
|
free(s);
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sndio_get_max_channel_count(cubeb * ctx, uint32_t * max_channels)
|
|
|
|
{
|
|
|
|
assert(ctx && max_channels);
|
|
|
|
|
|
|
|
*max_channels = 8;
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sndio_get_preferred_sample_rate(cubeb * ctx, uint32_t * rate)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We've no device-independent prefered rate; any rate will work if
|
|
|
|
* sndiod is running. If it isn't, 48kHz is what is most likely to
|
|
|
|
* work as most (but not all) devices support it.
|
|
|
|
*/
|
|
|
|
*rate = 48000;
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_get_min_latency(cubeb * ctx, cubeb_stream_params params,
|
|
|
|
uint32_t * latency_frames)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We've no device-independent minimum latency.
|
|
|
|
*/
|
|
|
|
*latency_frames = 2048;
|
|
|
|
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_stream_destroy(cubeb_stream * s)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
DPR("sndio_stream_destroy()\n");
|
|
|
|
WRAP(sio_close)(s->hdl);
|
|
|
|
if (s->mode & SIO_PLAY)
|
|
|
|
free(s->pbuf);
|
|
|
|
if (s->mode & SIO_REC)
|
|
|
|
free(s->rbuf);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_stream_start(cubeb_stream * s)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
DPR("sndio_stream_start()\n");
|
|
|
|
s->active = 1;
|
|
|
|
err = pthread_create(&s->th, NULL, sndio_mainloop, s);
|
|
|
|
if (err) {
|
|
|
|
s->active = 0;
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
}
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_stream_stop(cubeb_stream * s)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
2022-08-05 07:28:17 +00:00
|
|
|
void * dummy;
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
DPR("sndio_stream_stop()\n");
|
|
|
|
if (s->active) {
|
|
|
|
s->active = 0;
|
|
|
|
pthread_join(s->th, &dummy);
|
|
|
|
}
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_stream_get_position(cubeb_stream * s, uint64_t * p)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&s->mtx);
|
|
|
|
DPR("sndio_stream_get_position() %" PRId64 "\n", s->hwpos);
|
|
|
|
*p = s->hwpos;
|
|
|
|
pthread_mutex_unlock(&s->mtx);
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_stream_set_volume(cubeb_stream * s, float volume)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
DPR("sndio_stream_set_volume(%f)\n", volume);
|
|
|
|
pthread_mutex_lock(&s->mtx);
|
|
|
|
if (volume < 0.)
|
|
|
|
volume = 0.;
|
|
|
|
else if (volume > 1.0)
|
|
|
|
volume = 1.;
|
|
|
|
s->volume = volume;
|
|
|
|
pthread_mutex_unlock(&s->mtx);
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sndio_stream_get_latency(cubeb_stream * stm, uint32_t * latency)
|
|
|
|
{
|
|
|
|
// http://www.openbsd.org/cgi-bin/man.cgi?query=sio_open
|
|
|
|
// in the "Measuring the latency and buffers usage" paragraph.
|
|
|
|
*latency = stm->swpos - stm->hwpos;
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-08-05 07:28:17 +00:00
|
|
|
sndio_enumerate_devices(cubeb * context, cubeb_device_type type,
|
|
|
|
cubeb_device_collection * collection)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
static char dev[] = SIO_DEVANY;
|
2022-08-05 07:28:17 +00:00
|
|
|
cubeb_device_info * device;
|
2020-01-10 04:59:53 +00:00
|
|
|
|
|
|
|
device = malloc(sizeof(cubeb_device_info));
|
|
|
|
if (device == NULL)
|
|
|
|
return CUBEB_ERROR;
|
|
|
|
|
2022-08-05 07:28:17 +00:00
|
|
|
device->devid = dev; /* passed to stream_init() */
|
|
|
|
device->device_id = dev; /* printable in UI */
|
|
|
|
device->friendly_name = dev; /* same, but friendly */
|
|
|
|
device->group_id = dev; /* actual device if full-duplex */
|
|
|
|
device->vendor_name = NULL; /* may be NULL */
|
|
|
|
device->type = type; /* Input/Output */
|
2020-01-10 04:59:53 +00:00
|
|
|
device->state = CUBEB_DEVICE_STATE_ENABLED;
|
|
|
|
device->preferred = CUBEB_DEVICE_PREF_ALL;
|
|
|
|
device->format = CUBEB_DEVICE_FMT_S16NE;
|
|
|
|
device->default_format = CUBEB_DEVICE_FMT_S16NE;
|
2022-08-05 07:28:17 +00:00
|
|
|
device->max_channels = (type == CUBEB_DEVICE_TYPE_INPUT) ? 2 : 8;
|
2020-01-10 04:59:53 +00:00
|
|
|
device->default_rate = 48000;
|
|
|
|
device->min_rate = 4000;
|
|
|
|
device->max_rate = 192000;
|
|
|
|
device->latency_lo = 480;
|
|
|
|
device->latency_hi = 9600;
|
|
|
|
collection->device = device;
|
|
|
|
collection->count = 1;
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
sndio_device_collection_destroy(cubeb * context,
|
2022-08-05 07:28:17 +00:00
|
|
|
cubeb_device_collection * collection)
|
2020-01-10 04:59:53 +00:00
|
|
|
{
|
|
|
|
free(collection->device);
|
|
|
|
return CUBEB_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct cubeb_ops const sndio_ops = {
|
2022-08-05 07:28:17 +00:00
|
|
|
.init = sndio_init,
|
|
|
|
.get_backend_id = sndio_get_backend_id,
|
|
|
|
.get_max_channel_count = sndio_get_max_channel_count,
|
|
|
|
.get_min_latency = sndio_get_min_latency,
|
|
|
|
.get_preferred_sample_rate = sndio_get_preferred_sample_rate,
|
2024-05-12 07:10:00 +00:00
|
|
|
.get_supported_input_processing_params = NULL,
|
2022-08-05 07:28:17 +00:00
|
|
|
.enumerate_devices = sndio_enumerate_devices,
|
|
|
|
.device_collection_destroy = sndio_device_collection_destroy,
|
|
|
|
.destroy = sndio_destroy,
|
|
|
|
.stream_init = sndio_stream_init,
|
|
|
|
.stream_destroy = sndio_stream_destroy,
|
|
|
|
.stream_start = sndio_stream_start,
|
|
|
|
.stream_stop = sndio_stream_stop,
|
|
|
|
.stream_get_position = sndio_stream_get_position,
|
|
|
|
.stream_get_latency = sndio_stream_get_latency,
|
|
|
|
.stream_set_volume = sndio_stream_set_volume,
|
|
|
|
.stream_set_name = NULL,
|
|
|
|
.stream_get_current_device = NULL,
|
2024-05-12 07:10:00 +00:00
|
|
|
.stream_set_input_mute = NULL,
|
|
|
|
.stream_set_input_processing_params = NULL,
|
2022-08-05 07:28:17 +00:00
|
|
|
.stream_device_destroy = NULL,
|
|
|
|
.stream_register_device_changed_callback = NULL,
|
|
|
|
.register_device_collection_changed = NULL};
|