summaryrefslogtreecommitdiff
path: root/lib/libsndio/sio_aucat.c
blob: a3f9472993d8d7592a7e2c00e29180d37a497f3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*	$OpenBSD: sio_aucat.c,v 1.7 2011/06/03 18:09:25 ratchov Exp $	*/
/*
 * 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/socket.h>
#include <sys/un.h>

#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "aucat.h"
#include "debug.h"
#include "sio_priv.h"

struct sio_aucat_hdl {
	struct sio_hdl sio;
	struct aucat aucat;
	unsigned rbpf, wbpf;		/* read and write bytes-per-frame */
	int maxwrite;			/* latency constraint */
	int events;			/* events the user requested */
	unsigned curvol, reqvol;	/* current and requested volume */
	int delta;			/* some of received deltas */
#define PSTATE_INIT	0
#define PSTATE_RUN	1
	int pstate;
};

static void sio_aucat_close(struct sio_hdl *);
static int sio_aucat_start(struct sio_hdl *);
static int sio_aucat_stop(struct sio_hdl *);
static int sio_aucat_setpar(struct sio_hdl *, struct sio_par *);
static int sio_aucat_getpar(struct sio_hdl *, struct sio_par *);
static int sio_aucat_getcap(struct sio_hdl *, struct sio_cap *);
static size_t sio_aucat_read(struct sio_hdl *, void *, size_t);
static size_t sio_aucat_write(struct sio_hdl *, const void *, size_t);
static int sio_aucat_nfds(struct sio_hdl *);
static int sio_aucat_pollfd(struct sio_hdl *, struct pollfd *, int);
static int sio_aucat_revents(struct sio_hdl *, struct pollfd *);
static int sio_aucat_setvol(struct sio_hdl *, unsigned);
static void sio_aucat_getvol(struct sio_hdl *);

static struct sio_ops sio_aucat_ops = {
	sio_aucat_close,
	sio_aucat_setpar,
	sio_aucat_getpar,
	sio_aucat_getcap,
	sio_aucat_write,
	sio_aucat_read,
	sio_aucat_start,
	sio_aucat_stop,
	sio_aucat_nfds,
	sio_aucat_pollfd,
	sio_aucat_revents,
	sio_aucat_setvol,
	sio_aucat_getvol
};

/*
 * execute the next message, return 0 if blocked
 */
static int
sio_aucat_runmsg(struct sio_aucat_hdl *hdl)
{
	int delta;
	unsigned size, ctl;

	if (!aucat_rmsg(&hdl->aucat, &hdl->sio.eof))
		return 0;
	switch (ntohl(hdl->aucat.rmsg.cmd)) {
	case AMSG_DATA:
		size = ntohl(hdl->aucat.rmsg.u.data.size);
		if (size == 0 || size % hdl->rbpf) {
			DPRINTF("sio_aucat_runmsg: bad data message\n");
			hdl->sio.eof = 1;
			return 0;
		}
		return 1;
	case AMSG_POS:
		delta = ntohl(hdl->aucat.rmsg.u.ts.delta);
		hdl->maxwrite += delta * (int)hdl->wbpf;
		DPRINTF("aucat: pos = %d, maxwrite = %d\n",
		    delta, hdl->maxwrite);
		hdl->delta = delta;
		if (hdl->delta >= 0) {
			sio_onmove_cb(&hdl->sio, hdl->delta);
			hdl->delta = 0;
		}
		break;
	case AMSG_MOVE:
		delta = ntohl(hdl->aucat.rmsg.u.ts.delta);
		hdl->maxwrite += delta * hdl->wbpf;
		hdl->delta += delta;
		DPRINTF("aucat: move = %d, delta = %d, maxwrite = %d\n",
		    delta, hdl->delta, hdl->maxwrite);
		if (hdl->delta >= 0) {
			sio_onmove_cb(&hdl->sio, hdl->delta);
			hdl->delta = 0;
		}
		break;
	case AMSG_SETVOL:
		ctl = ntohl(hdl->aucat.rmsg.u.vol.ctl);
		hdl->curvol = hdl->reqvol = ctl;
		sio_onvol_cb(&hdl->sio, ctl);
		break;
	case AMSG_STOP:
		hdl->pstate = PSTATE_INIT;
		break;
	default:
		DPRINTF("sio_aucat_runmsg: unhandled message %u\n", hdl->aucat.rmsg.cmd);
		hdl->sio.eof = 1;
		return 0;
	}
	hdl->aucat.rstate = RSTATE_MSG;
	hdl->aucat.rtodo = sizeof(struct amsg);
	return 1;
}

static int
sio_aucat_buildmsg(struct sio_aucat_hdl *hdl)
{
	if (hdl->curvol != hdl->reqvol) {
		hdl->aucat.wstate = WSTATE_MSG;
		hdl->aucat.wtodo = sizeof(struct amsg);
		hdl->aucat.wmsg.cmd = htonl(AMSG_SETVOL);
		hdl->aucat.wmsg.u.vol.ctl = htonl(hdl->reqvol);
		hdl->curvol = hdl->reqvol;
		return aucat_wmsg(&hdl->aucat, &hdl->sio.eof);
	}
	return 0;
}

struct sio_hdl *
sio_aucat_open(const char *str, unsigned mode, int nbio)
{
	struct sio_aucat_hdl *hdl;

	hdl = malloc(sizeof(struct sio_aucat_hdl));
	if (hdl == NULL)
		return NULL;
	if (!aucat_open(&hdl->aucat, str, mode, 1)) {
		free(hdl);
		return NULL;
	}
	sio_create(&hdl->sio, &sio_aucat_ops, mode, nbio);
	hdl->curvol = SIO_MAXVOL;
	hdl->reqvol = SIO_MAXVOL;
	hdl->pstate = PSTATE_INIT;
	return (struct sio_hdl *)hdl;
}

static void
sio_aucat_close(struct sio_hdl *sh)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	if (!hdl->sio.eof && hdl->sio.started)
		(void)sio_aucat_stop(&hdl->sio);
	aucat_close(&hdl->aucat, hdl->sio.eof);
	free(hdl);
}

static int
sio_aucat_start(struct sio_hdl *sh)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;
	struct sio_par par;

	/*
	 * save bpf
	 */
	if (!sio_getpar(&hdl->sio, &par))
		return 0;
	hdl->wbpf = par.bps * par.pchan;
	hdl->rbpf = par.bps * par.rchan;
	hdl->maxwrite = hdl->wbpf * par.bufsz;
	hdl->delta = 0;
	DPRINTF("aucat: start, maxwrite = %d\n", hdl->maxwrite);

	AMSG_INIT(&hdl->aucat.wmsg);
	hdl->aucat.wmsg.cmd = htonl(AMSG_START);
	hdl->aucat.wtodo = sizeof(struct amsg);
	if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof))
		return 0;
	hdl->aucat.rstate = RSTATE_MSG;
	hdl->aucat.rtodo = sizeof(struct amsg);
	if (!aucat_setfl(&hdl->aucat, 1, &hdl->sio.eof))
		return 0;
	hdl->pstate = PSTATE_RUN;
	return 1;
}

static int
sio_aucat_stop(struct sio_hdl *sh)
{
#define ZERO_MAX 0x400
	static unsigned char zero[ZERO_MAX];
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;
	unsigned n, count;

	if (!aucat_setfl(&hdl->aucat, 0, &hdl->sio.eof))
		return 0;
	/*
	 * complete message or data block in progress
	 */
	if (hdl->aucat.wstate == WSTATE_MSG) {
		if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof))
			return 0;
	}
	if (hdl->aucat.wstate == WSTATE_DATA) {
		hdl->maxwrite = hdl->aucat.wtodo;
		while (hdl->aucat.wstate != WSTATE_IDLE) {
			count = hdl->aucat.wtodo;
			if (count > ZERO_MAX)
				count = ZERO_MAX;
			n = sio_aucat_write(&hdl->sio, zero, count);
			if (n == 0)
				return 0;
		}
	}

	/*
	 * send stop message
	 */
	AMSG_INIT(&hdl->aucat.wmsg);
	hdl->aucat.wmsg.cmd = htonl(AMSG_STOP);
	hdl->aucat.wtodo = sizeof(struct amsg);
	if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof))
		return 0;

	/*
	 * wait for the STOP ACK
	 */
	while (hdl->pstate != PSTATE_INIT) {
		switch (hdl->aucat.rstate) {
		case RSTATE_MSG:
			if (!sio_aucat_runmsg(hdl))
				return 0;
			break;
		case RSTATE_DATA:
			if (!sio_aucat_read(&hdl->sio, zero, ZERO_MAX))
				return 0;
			break;
		}
	}
	return 1;
}

static int
sio_aucat_setpar(struct sio_hdl *sh, struct sio_par *par)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	AMSG_INIT(&hdl->aucat.wmsg);
	hdl->aucat.wmsg.cmd = htonl(AMSG_SETPAR);
	hdl->aucat.wmsg.u.par.bits = par->bits;
	hdl->aucat.wmsg.u.par.bps = par->bps;
	hdl->aucat.wmsg.u.par.sig = par->sig;
	hdl->aucat.wmsg.u.par.le = par->le;
	hdl->aucat.wmsg.u.par.msb = par->msb;
	hdl->aucat.wmsg.u.par.rate = htonl(par->rate);
	hdl->aucat.wmsg.u.par.appbufsz = htonl(par->appbufsz);
	hdl->aucat.wmsg.u.par.xrun = par->xrun;
	if (hdl->sio.mode & SIO_REC)
		hdl->aucat.wmsg.u.par.rchan = htons(par->rchan);
	if (hdl->sio.mode & SIO_PLAY)
		hdl->aucat.wmsg.u.par.pchan = htons(par->pchan);
	hdl->aucat.wtodo = sizeof(struct amsg);
	if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof))
		return 0;
	return 1;
}

static int
sio_aucat_getpar(struct sio_hdl *sh, struct sio_par *par)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	AMSG_INIT(&hdl->aucat.wmsg);
	hdl->aucat.wmsg.cmd = htonl(AMSG_GETPAR);
	hdl->aucat.wtodo = sizeof(struct amsg);
	if (!aucat_wmsg(&hdl->aucat, &hdl->sio.eof))
		return 0;
	hdl->aucat.rtodo = sizeof(struct amsg);
	if (!aucat_rmsg(&hdl->aucat, &hdl->sio.eof))
		return 0;
	if (ntohl(hdl->aucat.rmsg.cmd) != AMSG_GETPAR) {
		DPRINTF("sio_aucat_getpar: protocol err\n");
		hdl->sio.eof = 1;
		return 0;
	}
	par->bits = hdl->aucat.rmsg.u.par.bits;
	par->bps = hdl->aucat.rmsg.u.par.bps;
	par->sig = hdl->aucat.rmsg.u.par.sig;
	par->le = hdl->aucat.rmsg.u.par.le;
	par->msb = hdl->aucat.rmsg.u.par.msb;
	par->rate = ntohl(hdl->aucat.rmsg.u.par.rate);
	par->bufsz = ntohl(hdl->aucat.rmsg.u.par.bufsz);
	par->appbufsz = ntohl(hdl->aucat.rmsg.u.par.appbufsz);
	par->xrun = hdl->aucat.rmsg.u.par.xrun;
	par->round = ntohl(hdl->aucat.rmsg.u.par.round);
	if (hdl->sio.mode & SIO_PLAY)
		par->pchan = ntohs(hdl->aucat.rmsg.u.par.pchan);
	if (hdl->sio.mode & SIO_REC)
		par->rchan = ntohs(hdl->aucat.rmsg.u.par.rchan);
	return 1;
}

static int
sio_aucat_getcap(struct sio_hdl *sh, struct sio_cap *cap)
{
	unsigned i, bps, le, sig, chan, rindex, rmult;
	static unsigned rates[] = { 8000, 11025, 12000 };

	bps = 1;
	sig = le = 0;
	cap->confs[0].enc = 0;
	for (i = 0; i < SIO_NENC; i++) {
		if (bps > 4)
			break;
		cap->confs[0].enc |= 1 << i;
		cap->enc[i].bits = bps == 4 ? 24 : bps * 8;
		cap->enc[i].bps = bps;
		cap->enc[i].sig = sig ^ 1;
		cap->enc[i].le = bps > 1 ? le : SIO_LE_NATIVE;
		cap->enc[i].msb = 1;
		le++;
		if (le > 1 || bps == 1) {
			le = 0;
			sig++;
		}
		if (sig > 1 || (le == 0 && bps > 1)) {
			sig = 0;
			bps++;
		}
	}
	chan = 1;
	cap->confs[0].rchan = 0;
	for (i = 0; i < SIO_NCHAN; i++) {
		if (chan > 16)
			break;
		cap->confs[0].rchan |= 1 << i;
		cap->rchan[i] = chan;
		if (chan >= 12) {
			chan += 4;
		} else if (chan >= 2) {
			chan += 2;
		} else
			chan++;
	}
	chan = 1;
	cap->confs[0].pchan = 0;
	for (i = 0; i < SIO_NCHAN; i++) {
		if (chan > 16)
			break;
		cap->confs[0].pchan |= 1 << i;
		cap->pchan[i] = chan;
		if (chan >= 12) {
			chan += 4;
		} else if (chan >= 2) {
			chan += 2;
		} else
			chan++;
	}
	rindex = 0;
	rmult = 1;
	cap->confs[0].rate = 0;
	for (i = 0; i < SIO_NRATE; i++) {
		if (rmult >= 32)
			break;
		cap->rate[i] = rates[rindex] * rmult;
		cap->confs[0].rate |= 1 << i;
		rindex++;
		if (rindex == sizeof(rates) / sizeof(unsigned)) {
			rindex = 0;
			rmult *= 2;
		}
	}
	cap->nconf = 1;
	return 1;
}

static size_t
sio_aucat_read(struct sio_hdl *sh, void *buf, size_t len)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	while (hdl->aucat.rstate == RSTATE_MSG) {
		if (!sio_aucat_runmsg(hdl))
			return 0;
	}
	return aucat_rdata(&hdl->aucat, buf, len, &hdl->sio.eof);
}

static size_t
sio_aucat_write(struct sio_hdl *sh, const void *buf, size_t len)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;
	size_t n;

	while (hdl->aucat.wstate == WSTATE_IDLE) {
		if (!sio_aucat_buildmsg(hdl))
			break;
	}
	if (len <= 0 || hdl->maxwrite <= 0)
		return 0;
	if (len > hdl->maxwrite)
		len = hdl->maxwrite;
	n = aucat_wdata(&hdl->aucat, buf, len, hdl->wbpf, &hdl->sio.eof);
	hdl->maxwrite -= n;
	return n;
}

static int
sio_aucat_nfds(struct sio_hdl *hdl)
{
	return 1;
 }

static int
sio_aucat_pollfd(struct sio_hdl *sh, struct pollfd *pfd, int events)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	hdl->events = events;
	if (hdl->maxwrite <= 0)
		events &= ~POLLOUT;
	return aucat_pollfd(&hdl->aucat, pfd, events);
}

static int
sio_aucat_revents(struct sio_hdl *sh, struct pollfd *pfd)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;
	int revents = pfd->revents;

	if (revents & POLLIN) {
		while (hdl->aucat.rstate == RSTATE_MSG) {
			if (!sio_aucat_runmsg(hdl))
				break;
		}
		if (hdl->aucat.rstate != RSTATE_DATA)
			revents &= ~POLLIN;
	}
	if (revents & POLLOUT) {
		if (hdl->maxwrite <= 0)
			revents &= ~POLLOUT;
	}
	if (hdl->sio.eof)
		return POLLHUP;
	DPRINTF("sio_aucat_revents: %x\n", revents & hdl->events);
	return revents & (hdl->events | POLLHUP);
}

static int
sio_aucat_setvol(struct sio_hdl *sh, unsigned vol)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	hdl->reqvol = vol;
	return 1;
}

static void
sio_aucat_getvol(struct sio_hdl *sh)
{
	struct sio_aucat_hdl *hdl = (struct sio_aucat_hdl *)sh;

	sio_onvol_cb(&hdl->sio, hdl->reqvol);
	return;
}