1 /*	$OpenBSD$	*/
2 /*
3  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <unistd.h>
28 
29 #include "debug.h"
30 #include "sio_priv.h"
31 #include "bsd-compat.h"
32 
33 #define SIO_PAR_MAGIC	0x83b905a4
34 
35 void
sio_initpar(struct sio_par * par)36 sio_initpar(struct sio_par *par)
37 {
38 	memset(par, 0xff, sizeof(struct sio_par));
39 	par->__magic = SIO_PAR_MAGIC;
40 }
41 
42 struct sio_hdl *
sio_open(const char * str,unsigned int mode,int nbio)43 sio_open(const char *str, unsigned int mode, int nbio)
44 {
45 	static char devany[] = SIO_DEVANY;
46 	struct sio_hdl *hdl;
47 
48 #ifdef DEBUG
49 	_sndio_debug_init();
50 #endif
51 	if ((mode & (SIO_PLAY | SIO_REC)) == 0)
52 		return NULL;
53 	if (str == NULL) /* backward compat */
54 		str = devany;
55 	if (strcmp(str, devany) == 0 && !issetugid()) {
56 		if ((mode & SIO_PLAY) == 0)
57 			str = getenv("AUDIORECDEVICE");
58 		if ((mode & SIO_REC) == 0)
59 			str = getenv("AUDIOPLAYDEVICE");
60 		if (mode == (SIO_PLAY | SIO_REC) || str == NULL)
61 			str = getenv("AUDIODEVICE");
62 		if (str == NULL)
63 			str = devany;
64 	}
65 	if (strcmp(str, devany) == 0) {
66 		hdl = _sio_aucat_open("snd/0", mode, nbio);
67 		if (hdl != NULL)
68 			return hdl;
69 #if defined(USE_SUN)
70 		return _sio_sun_open("rsnd/0", mode, nbio);
71 #elif defined(USE_OSS)
72 		return _sio_oss_open("rsnd/default", mode, nbio);
73 #elif defined(USE_ALSA)
74 		return _sio_alsa_open("rsnd/default", mode, nbio);
75 #else
76 		return NULL;
77 #endif
78 	}
79 	if (_sndio_parsetype(str, "snd"))
80 		return _sio_aucat_open(str, mode, nbio);
81 	if (_sndio_parsetype(str, "rsnd"))
82 #if defined(USE_SUN)
83 		return _sio_sun_open(str, mode, nbio);
84 #elif defined(USE_OSS)
85 		return _sio_oss_open(str, mode, nbio);
86 #elif defined(USE_ALSA)
87 		return _sio_alsa_open(str, mode, nbio);
88 #else
89 		return NULL;
90 #endif
91 	DPRINTF("sio_open: %s: unknown device type\n", str);
92 	return NULL;
93 }
94 
95 void
_sio_create(struct sio_hdl * hdl,struct sio_ops * ops,unsigned int mode,int nbio)96 _sio_create(struct sio_hdl *hdl, struct sio_ops *ops,
97     unsigned int mode, int nbio)
98 {
99 	hdl->ops = ops;
100 	hdl->mode = mode;
101 	hdl->nbio = nbio;
102 	hdl->started = 0;
103 	hdl->eof = 0;
104 	hdl->move_cb = NULL;
105 	hdl->vol_cb = NULL;
106 }
107 
108 void
sio_close(struct sio_hdl * hdl)109 sio_close(struct sio_hdl *hdl)
110 {
111 	hdl->ops->close(hdl);
112 }
113 
114 int
sio_start(struct sio_hdl * hdl)115 sio_start(struct sio_hdl *hdl)
116 {
117 #ifdef DEBUG
118 	struct timespec ts;
119 #endif
120 
121 	if (hdl->eof) {
122 		DPRINTF("sio_start: eof\n");
123 		return 0;
124 	}
125 	if (hdl->started) {
126 		DPRINTF("sio_start: already started\n");
127 		hdl->eof = 1;
128 		return 0;
129 	}
130 	hdl->cpos = 0;
131 	hdl->rused = hdl->wused = 0;
132 	if (!sio_getpar(hdl, &hdl->par))
133 		return 0;
134 #ifdef DEBUG
135 	hdl->pollcnt = 0;
136 	clock_gettime(CLOCK_MONOTONIC, &ts);
137 	hdl->start_nsec = 1000000000LL * ts.tv_sec + ts.tv_nsec;
138 #endif
139 	hdl->rdrop = hdl->wsil = 0;
140 	if (!hdl->ops->start(hdl))
141 		return 0;
142 	hdl->started = 1;
143 	return 1;
144 }
145 
146 int
sio_stop(struct sio_hdl * hdl)147 sio_stop(struct sio_hdl *hdl)
148 {
149 	if (hdl->eof) {
150 		DPRINTF("sio_stop: eof\n");
151 		return 0;
152 	}
153 	if (!hdl->started) {
154 		DPRINTF("sio_stop: not started\n");
155 		hdl->eof = 1;
156 		return 0;
157 	}
158 	if (!hdl->ops->stop(hdl))
159 		return 0;
160 #ifdef DEBUG
161 	DPRINTFN(2, "libsndio: polls: %llu, samples = %llu\n",
162 	    hdl->pollcnt, hdl->cpos);
163 #endif
164 	hdl->started = 0;
165 	return 1;
166 }
167 
168 int
sio_setpar(struct sio_hdl * hdl,struct sio_par * par)169 sio_setpar(struct sio_hdl *hdl, struct sio_par *par)
170 {
171 	if (hdl->eof) {
172 		DPRINTF("sio_setpar: eof\n");
173 		return 0;
174 	}
175 	if (par->__magic != SIO_PAR_MAGIC) {
176 		DPRINTF("sio_setpar: uninitialized sio_par structure\n");
177 		hdl->eof = 1;
178 		return 0;
179 	}
180 	if (hdl->started) {
181 		DPRINTF("sio_setpar: already started\n");
182 		hdl->eof = 1;
183 		return 0;
184 	}
185 	if (par->bufsz != ~0U) {
186 		DPRINTF("sio_setpar: setting bufsz is deprecated\n");
187 		par->appbufsz = par->bufsz;
188 		par->bufsz = ~0U;
189 	}
190 	if (par->rate != ~0U && par->appbufsz == ~0U)
191 		par->appbufsz = par->rate * 200 / 1000;
192 	return hdl->ops->setpar(hdl, par);
193 }
194 
195 int
sio_getpar(struct sio_hdl * hdl,struct sio_par * par)196 sio_getpar(struct sio_hdl *hdl, struct sio_par *par)
197 {
198 	if (hdl->eof) {
199 		DPRINTF("sio_getpar: eof\n");
200 		return 0;
201 	}
202 	if (hdl->started) {
203 		DPRINTF("sio_getpar: already started\n");
204 		hdl->eof = 1;
205 		return 0;
206 	}
207 	if (!hdl->ops->getpar(hdl, par)) {
208 		par->__magic = 0;
209 		return 0;
210 	}
211 	par->__magic = 0;
212 	return 1;
213 }
214 
215 int
sio_getcap(struct sio_hdl * hdl,struct sio_cap * cap)216 sio_getcap(struct sio_hdl *hdl, struct sio_cap *cap)
217 {
218 	if (hdl->eof) {
219 		DPRINTF("sio_getcap: eof\n");
220 		return 0;
221 	}
222 	if (hdl->started) {
223 		DPRINTF("sio_getcap: already started\n");
224 		hdl->eof = 1;
225 		return 0;
226 	}
227 	return hdl->ops->getcap(hdl, cap);
228 }
229 
230 static int
sio_psleep(struct sio_hdl * hdl,int event)231 sio_psleep(struct sio_hdl *hdl, int event)
232 {
233 	struct pollfd pfd[SIO_MAXNFDS];
234 	int revents;
235 	int nfds;
236 
237 	nfds = sio_nfds(hdl);
238 	if (nfds > SIO_MAXNFDS) {
239 		DPRINTF("sio_psleep: %d: too many descriptors\n", nfds);
240 		hdl->eof = 1;
241 		return 0;
242 	}
243 	for (;;) {
244 		nfds = sio_pollfd(hdl, pfd, event);
245 		while (poll(pfd, nfds, -1) == -1) {
246 			if (errno == EINTR)
247 				continue;
248 			DPERROR("sio_psleep: poll");
249 			hdl->eof = 1;
250 			return 0;
251 		}
252 		revents = sio_revents(hdl, pfd);
253 		if (revents & POLLHUP) {
254 			DPRINTF("sio_psleep: hang-up\n");
255 			return 0;
256 		}
257 		if (revents & event)
258 			break;
259 	}
260 	return 1;
261 }
262 
263 static int
sio_rdrop(struct sio_hdl * hdl)264 sio_rdrop(struct sio_hdl *hdl)
265 {
266 #define DROP_NMAX 0x1000
267 	static char dummy[DROP_NMAX];
268 	ssize_t n, todo;
269 
270 	while (hdl->rdrop > 0) {
271 		todo = hdl->rdrop;
272 		if (todo > DROP_NMAX)
273 			todo = DROP_NMAX;
274 		n = hdl->ops->read(hdl, dummy, todo);
275 		if (n == 0)
276 			return 0;
277 		hdl->rdrop -= n;
278 		DPRINTF("sio_rdrop: dropped %zu bytes\n", n);
279 	}
280 	return 1;
281 }
282 
283 static int
sio_wsil(struct sio_hdl * hdl)284 sio_wsil(struct sio_hdl *hdl)
285 {
286 #define ZERO_NMAX 0x1000
287 	static char zero[ZERO_NMAX];
288 	ssize_t n, todo;
289 
290 	while (hdl->wsil > 0) {
291 		todo = hdl->wsil;
292 		if (todo > ZERO_NMAX)
293 			todo = ZERO_NMAX;
294 		n = hdl->ops->write(hdl, zero, todo);
295 		if (n == 0)
296 			return 0;
297 		hdl->wsil -= n;
298 		DPRINTF("sio_wsil: inserted %zu bytes\n", n);
299 	}
300 	return 1;
301 }
302 
303 size_t
sio_read(struct sio_hdl * hdl,void * buf,size_t len)304 sio_read(struct sio_hdl *hdl, void *buf, size_t len)
305 {
306 	unsigned int n;
307 	char *data = buf;
308 	size_t todo = len, maxread;
309 
310 	if (hdl->eof) {
311 		DPRINTF("sio_read: eof\n");
312 		return 0;
313 	}
314 	if (!hdl->started || !(hdl->mode & SIO_REC)) {
315 		DPRINTF("sio_read: recording not started\n");
316 		hdl->eof = 1;
317 		return 0;
318 	}
319 	while (todo > 0) {
320 		if (!sio_rdrop(hdl))
321 			return 0;
322 		maxread = hdl->rused;
323 		if (maxread > todo)
324 			maxread = todo;
325 		n = maxread > 0 ? hdl->ops->read(hdl, data, maxread) : 0;
326 		if (n == 0) {
327 			if (hdl->nbio || hdl->eof || todo < len)
328 				break;
329 			if (!sio_psleep(hdl, POLLIN))
330 				break;
331 			continue;
332 		}
333 		data += n;
334 		todo -= n;
335 		hdl->rused -= n;
336 	}
337 	return len - todo;
338 }
339 
340 size_t
sio_write(struct sio_hdl * hdl,const void * buf,size_t len)341 sio_write(struct sio_hdl *hdl, const void *buf, size_t len)
342 {
343 	unsigned int n;
344 	const unsigned char *data = buf;
345 	size_t todo = len, maxwrite;
346 
347 	if (hdl->eof) {
348 		DPRINTF("sio_write: eof\n");
349 		return 0;
350 	}
351 	if (!hdl->started || !(hdl->mode & SIO_PLAY)) {
352 		DPRINTF("sio_write: playback not started\n");
353 		hdl->eof = 1;
354 		return 0;
355 	}
356 	while (todo > 0) {
357 		if (!sio_wsil(hdl))
358 			return 0;
359 		maxwrite = hdl->par.bufsz * hdl->par.pchan * hdl->par.bps -
360 		    hdl->wused;
361 		if (maxwrite > todo)
362 			maxwrite = todo;
363 		n = maxwrite > 0 ? hdl->ops->write(hdl, data, maxwrite) : 0;
364 		if (n == 0) {
365 			if (hdl->nbio || hdl->eof)
366 				break;
367 			if (!sio_psleep(hdl, POLLOUT))
368 				break;
369 			continue;
370 		}
371 		data += n;
372 		todo -= n;
373 		hdl->wused += n;
374 	}
375 	return len - todo;
376 }
377 
378 int
sio_nfds(struct sio_hdl * hdl)379 sio_nfds(struct sio_hdl *hdl)
380 {
381 	return hdl->ops->nfds(hdl);
382 }
383 
384 int
sio_pollfd(struct sio_hdl * hdl,struct pollfd * pfd,int events)385 sio_pollfd(struct sio_hdl *hdl, struct pollfd *pfd, int events)
386 {
387 	if (hdl->eof)
388 		return 0;
389 	if (!hdl->started)
390 		events = 0;
391 	return hdl->ops->pollfd(hdl, pfd, events);
392 }
393 
394 int
sio_revents(struct sio_hdl * hdl,struct pollfd * pfd)395 sio_revents(struct sio_hdl *hdl, struct pollfd *pfd)
396 {
397 	int revents;
398 #ifdef DEBUG
399 	struct timespec ts0, ts1;
400 
401 	if (_sndio_debug >= 4)
402 		clock_gettime(CLOCK_MONOTONIC, &ts0);
403 #endif
404 	if (hdl->eof)
405 		return POLLHUP;
406 #ifdef DEBUG
407 	hdl->pollcnt++;
408 #endif
409 	revents = hdl->ops->revents(hdl, pfd);
410 	if (!hdl->started)
411 		return revents & POLLHUP;
412 #ifdef DEBUG
413 	if (_sndio_debug >= 4) {
414 		clock_gettime(CLOCK_MONOTONIC, &ts1);
415 		DPRINTF("%09lld: sio_revents: revents = 0x%x, took %lldns\n",
416 		    1000000000LL * ts0.tv_sec +
417 		    ts0.tv_nsec - hdl->start_nsec,
418 		    revents,
419 		    1000000000LL * (ts1.tv_sec - ts0.tv_sec) +
420 		    ts1.tv_nsec - ts0.tv_nsec);
421 	}
422 #endif
423 	if ((hdl->mode & SIO_PLAY) && !sio_wsil(hdl))
424 		revents &= ~POLLOUT;
425 	if ((hdl->mode & SIO_REC) && !sio_rdrop(hdl))
426 		revents &= ~POLLIN;
427 	return revents;
428 }
429 
430 int
sio_eof(struct sio_hdl * hdl)431 sio_eof(struct sio_hdl *hdl)
432 {
433 	return hdl->eof;
434 }
435 
436 void
sio_onmove(struct sio_hdl * hdl,void (* cb)(void *,int),void * addr)437 sio_onmove(struct sio_hdl *hdl, void (*cb)(void *, int), void *addr)
438 {
439 	if (hdl->started) {
440 		DPRINTF("sio_onmove: already started\n");
441 		hdl->eof = 1;
442 		return;
443 	}
444 	hdl->move_cb = cb;
445 	hdl->move_addr = addr;
446 }
447 
448 #ifdef DEBUG
449 void
_sio_printpos(struct sio_hdl * hdl)450 _sio_printpos(struct sio_hdl *hdl)
451 {
452 	struct timespec ts;
453 	long long rpos, rdiff;
454 	long long cpos, cdiff;
455 	long long wpos, wdiff;
456 	unsigned rbpf, wbpf, rround, wround;
457 
458 	clock_gettime(CLOCK_MONOTONIC, &ts);
459 	rbpf = (hdl->mode & SIO_REC) ? hdl->par.bps * hdl->par.rchan : 1;
460 	wbpf = (hdl->mode & SIO_PLAY) ? hdl->par.bps * hdl->par.pchan : 1;
461 	rround = hdl->par.round * rbpf;
462 	wround = hdl->par.round * wbpf;
463 
464 	rpos = (hdl->mode & SIO_REC) ?
465 	    hdl->cpos * rbpf - hdl->rused : 0;
466 	wpos = (hdl->mode & SIO_PLAY) ?
467 	    hdl->cpos * wbpf + hdl->wused : 0;
468 
469 	cdiff = hdl->cpos % hdl->par.round;
470 	cpos  = hdl->cpos / hdl->par.round;
471 	if (cdiff > hdl->par.round / 2) {
472 		cpos++;
473 		cdiff = cdiff - hdl->par.round;
474 	}
475 	rdiff = rpos % rround;
476 	rpos  = rpos / rround;
477 	if (rdiff > rround / 2) {
478 		rpos++;
479 		rdiff = rdiff - rround;
480 	}
481 	wdiff = wpos % wround;
482 	wpos  = wpos / wround;
483 	if (wdiff > wround / 2) {
484 		wpos++;
485 		wdiff = wdiff - wround;
486 	}
487 	DPRINTF("%011lld: "
488 	    "clk %+5lld%+5lld, wr %+5lld%+5lld rd: %+5lld%+5lld\n",
489 	    1000000000LL * ts.tv_sec + ts.tv_nsec - hdl->start_nsec,
490 	    cpos, cdiff, wpos, wdiff, rpos, rdiff);
491 }
492 #endif
493 
494 void
_sio_onmove_cb(struct sio_hdl * hdl,int delta)495 _sio_onmove_cb(struct sio_hdl *hdl, int delta)
496 {
497 	hdl->cpos += delta;
498 	if (hdl->mode & SIO_REC)
499 		hdl->rused += delta * (hdl->par.bps * hdl->par.rchan);
500 	if (hdl->mode & SIO_PLAY)
501 		hdl->wused -= delta * (hdl->par.bps * hdl->par.pchan);
502 #ifdef DEBUG
503 	if (_sndio_debug >= 3)
504 		_sio_printpos(hdl);
505 	if ((hdl->mode & SIO_PLAY) && hdl->wused < 0) {
506 		DPRINTFN(1, "sndio: h/w failure: negative buffer usage\n");
507 		hdl->eof = 1;
508 		return;
509 	}
510 #endif
511 	if (hdl->move_cb)
512 		hdl->move_cb(hdl->move_addr, delta);
513 }
514 
515 int
sio_setvol(struct sio_hdl * hdl,unsigned int ctl)516 sio_setvol(struct sio_hdl *hdl, unsigned int ctl)
517 {
518 	if (hdl->eof)
519 		return 0;
520 	if (!hdl->ops->setvol)
521 		return 1;
522 	if (!hdl->ops->setvol(hdl, ctl))
523 		return 0;
524 	hdl->ops->getvol(hdl);
525 	return 1;
526 }
527 
528 int
sio_onvol(struct sio_hdl * hdl,void (* cb)(void *,unsigned int),void * addr)529 sio_onvol(struct sio_hdl *hdl, void (*cb)(void *, unsigned int), void *addr)
530 {
531 	if (hdl->started) {
532 		DPRINTF("sio_onvol: already started\n");
533 		hdl->eof = 1;
534 		return 0;
535 	}
536 	if (!hdl->ops->setvol)
537 		return 0;
538 	hdl->vol_cb = cb;
539 	hdl->vol_addr = addr;
540 	hdl->ops->getvol(hdl);
541 	return 1;
542 }
543 
544 void
_sio_onvol_cb(struct sio_hdl * hdl,unsigned int ctl)545 _sio_onvol_cb(struct sio_hdl *hdl, unsigned int ctl)
546 {
547 	if (hdl->vol_cb)
548 		hdl->vol_cb(hdl->vol_addr, ctl);
549 }
550