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