| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2005, 2006, 2007 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | |
| 21 | /* This program deliberately does not use the garbage collector even though it |
| 22 | * might be convenient to do so. This is for two reasons. Firstly some libao |
| 23 | * drivers are implemented using threads and we do not want to have to deal |
| 24 | * with potential interactions between threading and garbage collection. |
| 25 | * Secondly this process needs to be able to respond quickly and this is not |
| 26 | * compatible with the collector hanging the program even relatively |
| 27 | * briefly. */ |
| 28 | |
| 29 | #include <config.h> |
| 30 | #include "types.h" |
| 31 | |
| 32 | #include <getopt.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <locale.h> |
| 36 | #include <syslog.h> |
| 37 | #include <unistd.h> |
| 38 | #include <errno.h> |
| 39 | #include <ao/ao.h> |
| 40 | #include <string.h> |
| 41 | #include <assert.h> |
| 42 | #include <sys/select.h> |
| 43 | #include <sys/wait.h> |
| 44 | #include <time.h> |
| 45 | |
| 46 | #include "configuration.h" |
| 47 | #include "syscalls.h" |
| 48 | #include "log.h" |
| 49 | #include "defs.h" |
| 50 | #include "mem.h" |
| 51 | #include "speaker.h" |
| 52 | #include "user.h" |
| 53 | |
| 54 | #if BUILD_SPEAKER |
| 55 | #include <alsa/asoundlib.h> |
| 56 | |
| 57 | #define BUFFER_SECONDS 5 /* How many seconds of input to |
| 58 | * buffer. */ |
| 59 | |
| 60 | #define FRAMES 4096 /* Frame batch size */ |
| 61 | |
| 62 | #define NFDS 256 /* Max FDs to poll for */ |
| 63 | |
| 64 | /* Known tracks are kept in a linked list. We don't normally to have |
| 65 | * more than two - maybe three at the outside. */ |
| 66 | static struct track { |
| 67 | struct track *next; /* next track */ |
| 68 | int fd; /* input FD */ |
| 69 | char id[24]; /* ID */ |
| 70 | size_t start, used; /* start + bytes used */ |
| 71 | int eof; /* input is at EOF */ |
| 72 | int got_format; /* got format yet? */ |
| 73 | ao_sample_format format; /* sample format */ |
| 74 | unsigned long long played; /* number of frames played */ |
| 75 | char *buffer; /* sample buffer */ |
| 76 | size_t size; /* sample buffer size */ |
| 77 | int slot; /* poll array slot */ |
| 78 | } *tracks, *playing; /* all tracks + playing track */ |
| 79 | |
| 80 | static time_t last_report; /* when we last reported */ |
| 81 | static int paused; /* pause status */ |
| 82 | static snd_pcm_t *pcm; /* current pcm handle */ |
| 83 | static ao_sample_format pcm_format; /* current format if aodev != 0 */ |
| 84 | static size_t bpf; /* bytes per frame */ |
| 85 | static struct pollfd fds[NFDS]; /* if we need more than that */ |
| 86 | static int fdno; /* fd number */ |
| 87 | static snd_pcm_uframes_t pcm_bufsize; /* buffer size */ |
| 88 | static snd_pcm_uframes_t last_pcm_bufsize; /* last seen buffer size */ |
| 89 | static int ready; /* ready to send audio */ |
| 90 | static int forceplay; /* frames to force play */ |
| 91 | static int kidfd = -1; /* child process input */ |
| 92 | |
| 93 | static const struct option options[] = { |
| 94 | { "help", no_argument, 0, 'h' }, |
| 95 | { "version", no_argument, 0, 'V' }, |
| 96 | { "config", required_argument, 0, 'c' }, |
| 97 | { "debug", no_argument, 0, 'd' }, |
| 98 | { "no-debug", no_argument, 0, 'D' }, |
| 99 | { 0, 0, 0, 0 } |
| 100 | }; |
| 101 | |
| 102 | /* Display usage message and terminate. */ |
| 103 | static void help(void) { |
| 104 | xprintf("Usage:\n" |
| 105 | " disorder-speaker [OPTIONS]\n" |
| 106 | "Options:\n" |
| 107 | " --help, -h Display usage message\n" |
| 108 | " --version, -V Display version number\n" |
| 109 | " --config PATH, -c PATH Set configuration file\n" |
| 110 | " --debug, -d Turn on debugging\n" |
| 111 | "\n" |
| 112 | "Speaker process for DisOrder. Not intended to be run\n" |
| 113 | "directly.\n"); |
| 114 | xfclose(stdout); |
| 115 | exit(0); |
| 116 | } |
| 117 | |
| 118 | /* Display version number and terminate. */ |
| 119 | static void version(void) { |
| 120 | xprintf("disorder-speaker version %s\n", disorder_version_string); |
| 121 | xfclose(stdout); |
| 122 | exit(0); |
| 123 | } |
| 124 | |
| 125 | /* Return the number of bytes per frame in FORMAT. */ |
| 126 | static size_t bytes_per_frame(const ao_sample_format *format) { |
| 127 | return format->channels * format->bits / 8; |
| 128 | } |
| 129 | |
| 130 | /* Find track ID, maybe creating it if not found. */ |
| 131 | static struct track *findtrack(const char *id, int create) { |
| 132 | struct track *t; |
| 133 | |
| 134 | D(("findtrack %s %d", id, create)); |
| 135 | for(t = tracks; t && strcmp(id, t->id); t = t->next) |
| 136 | ; |
| 137 | if(!t && create) { |
| 138 | t = xmalloc(sizeof *t); |
| 139 | t->next = tracks; |
| 140 | strcpy(t->id, id); |
| 141 | t->fd = -1; |
| 142 | tracks = t; |
| 143 | /* The initial input buffer will be the sample format. */ |
| 144 | t->buffer = (void *)&t->format; |
| 145 | t->size = sizeof t->format; |
| 146 | } |
| 147 | return t; |
| 148 | } |
| 149 | |
| 150 | /* Remove track ID (but do not destroy it). */ |
| 151 | static struct track *removetrack(const char *id) { |
| 152 | struct track *t, **tt; |
| 153 | |
| 154 | D(("removetrack %s", id)); |
| 155 | for(tt = &tracks; (t = *tt) && strcmp(id, t->id); tt = &t->next) |
| 156 | ; |
| 157 | if(t) |
| 158 | *tt = t->next; |
| 159 | return t; |
| 160 | } |
| 161 | |
| 162 | /* Destroy a track. */ |
| 163 | static void destroy(struct track *t) { |
| 164 | D(("destroy %s", t->id)); |
| 165 | if(t->fd != -1) xclose(t->fd); |
| 166 | if(t->buffer != (void *)&t->format) free(t->buffer); |
| 167 | free(t); |
| 168 | } |
| 169 | |
| 170 | /* Notice a new FD. */ |
| 171 | static void acquire(struct track *t, int fd) { |
| 172 | D(("acquire %s %d", t->id, fd)); |
| 173 | if(t->fd != -1) |
| 174 | xclose(t->fd); |
| 175 | t->fd = fd; |
| 176 | nonblock(fd); |
| 177 | } |
| 178 | |
| 179 | /* Read data into a sample buffer. Return 0 on success, -1 on EOF. */ |
| 180 | static int fill(struct track *t) { |
| 181 | size_t where, left; |
| 182 | int n; |
| 183 | |
| 184 | D(("fill %s: eof=%d used=%zu size=%zu got_format=%d", |
| 185 | t->id, t->eof, t->used, t->size, t->got_format)); |
| 186 | if(t->eof) return -1; |
| 187 | if(t->used < t->size) { |
| 188 | /* there is room left in the buffer */ |
| 189 | where = (t->start + t->used) % t->size; |
| 190 | if(t->got_format) { |
| 191 | /* We are reading audio data, get as much as we can */ |
| 192 | if(where >= t->start) left = t->size - where; |
| 193 | else left = t->start - where; |
| 194 | } else |
| 195 | /* We are still waiting for the format, only get that */ |
| 196 | left = sizeof (ao_sample_format) - t->used; |
| 197 | do { |
| 198 | n = read(t->fd, t->buffer + where, left); |
| 199 | } while(n < 0 && errno == EINTR); |
| 200 | if(n < 0) { |
| 201 | if(errno != EAGAIN) fatal(errno, "error reading sample stream"); |
| 202 | return 0; |
| 203 | } |
| 204 | if(n == 0) { |
| 205 | D(("fill %s: eof detected", t->id)); |
| 206 | t->eof = 1; |
| 207 | return -1; |
| 208 | } |
| 209 | t->used += n; |
| 210 | if(!t->got_format && t->used >= sizeof (ao_sample_format)) { |
| 211 | assert(t->used == sizeof (ao_sample_format)); |
| 212 | /* Check that our assumptions are met. */ |
| 213 | if(t->format.bits & 7) |
| 214 | fatal(0, "bits per sample not a multiple of 8"); |
| 215 | /* Make a new buffer for audio data. */ |
| 216 | t->size = bytes_per_frame(&t->format) * t->format.rate * BUFFER_SECONDS; |
| 217 | t->buffer = xmalloc(t->size); |
| 218 | t->used = 0; |
| 219 | t->got_format = 1; |
| 220 | D(("got format for %s", t->id)); |
| 221 | } |
| 222 | } |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* Return true if A and B denote identical libao formats, else false. */ |
| 227 | static int formats_equal(const ao_sample_format *a, |
| 228 | const ao_sample_format *b) { |
| 229 | return (a->bits == b->bits |
| 230 | && a->rate == b->rate |
| 231 | && a->channels == b->channels |
| 232 | && a->byte_format == b->byte_format); |
| 233 | } |
| 234 | |
| 235 | /* Close the sound device. */ |
| 236 | static void idle(void) { |
| 237 | int err; |
| 238 | |
| 239 | D(("idle")); |
| 240 | if(pcm) { |
| 241 | if((err = snd_pcm_nonblock(pcm, 0)) < 0) |
| 242 | fatal(0, "error calling snd_pcm_nonblock: %d", err); |
| 243 | D(("draining pcm")); |
| 244 | snd_pcm_drain(pcm); |
| 245 | D(("closing pcm")); |
| 246 | snd_pcm_close(pcm); |
| 247 | pcm = 0; |
| 248 | forceplay = 0; |
| 249 | D(("released audio device")); |
| 250 | } |
| 251 | ready = 0; |
| 252 | } |
| 253 | |
| 254 | /* Abandon the current track */ |
| 255 | static void abandon(void) { |
| 256 | struct speaker_message sm; |
| 257 | |
| 258 | D(("abandon")); |
| 259 | memset(&sm, 0, sizeof sm); |
| 260 | sm.type = SM_FINISHED; |
| 261 | strcpy(sm.id, playing->id); |
| 262 | speaker_send(1, &sm, 0); |
| 263 | removetrack(playing->id); |
| 264 | destroy(playing); |
| 265 | playing = 0; |
| 266 | forceplay = 0; |
| 267 | } |
| 268 | |
| 269 | static void log_params(snd_pcm_hw_params_t *hwparams, |
| 270 | snd_pcm_sw_params_t *swparams) { |
| 271 | snd_pcm_uframes_t f; |
| 272 | unsigned u; |
| 273 | |
| 274 | return; /* too verbose */ |
| 275 | if(hwparams) { |
| 276 | /* TODO */ |
| 277 | } |
| 278 | if(swparams) { |
| 279 | snd_pcm_sw_params_get_silence_size(swparams, &f); |
| 280 | info("sw silence_size=%lu", (unsigned long)f); |
| 281 | snd_pcm_sw_params_get_silence_threshold(swparams, &f); |
| 282 | info("sw silence_threshold=%lu", (unsigned long)f); |
| 283 | snd_pcm_sw_params_get_sleep_min(swparams, &u); |
| 284 | info("sw sleep_min=%lu", (unsigned long)u); |
| 285 | snd_pcm_sw_params_get_start_threshold(swparams, &f); |
| 286 | info("sw start_threshold=%lu", (unsigned long)f); |
| 287 | snd_pcm_sw_params_get_stop_threshold(swparams, &f); |
| 288 | info("sw stop_threshold=%lu", (unsigned long)f); |
| 289 | snd_pcm_sw_params_get_xfer_align(swparams, &f); |
| 290 | info("sw xfer_align=%lu", (unsigned long)f); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void soxargs(const char ***pp, char **qq, ao_sample_format *ao) |
| 295 | { |
| 296 | int n; |
| 297 | |
| 298 | *(*pp)++ = "-t.raw"; |
| 299 | *(*pp)++ = "-s"; |
| 300 | *(*pp)++ = *qq; n = sprintf(*qq, "-r%d", ao->rate); *qq += n + 1; |
| 301 | switch(ao->byte_format) { |
| 302 | case AO_FMT_NATIVE: break; |
| 303 | case AO_FMT_BIG: *(*pp)++ = "-B"; |
| 304 | case AO_FMT_LITTLE: *(*pp)++ = "-L"; |
| 305 | } |
| 306 | *(*pp)++ = *qq; n = sprintf(*qq, "-%d", ao->bits/8); *qq += n + 1; |
| 307 | *(*pp)++ = *qq; n = sprintf(*qq, "-c%d", ao->channels); *qq += n + 1; |
| 308 | } |
| 309 | |
| 310 | /* Make sure the sound device is open and has the right sample format. Return |
| 311 | * 0 on success and -1 on error. */ |
| 312 | static int activate(void) { |
| 313 | int err; |
| 314 | snd_pcm_hw_params_t *hwparams; |
| 315 | snd_pcm_sw_params_t *swparams; |
| 316 | int sample_format = 0; |
| 317 | unsigned rate; |
| 318 | |
| 319 | /* If we don't know the format yet we cannot start. */ |
| 320 | if(!playing->got_format) { |
| 321 | D((" - not got format for %s", playing->id)); |
| 322 | return -1; |
| 323 | } |
| 324 | if(kidfd >= 0) { |
| 325 | if(!formats_equal(&playing->format, &config->sample_format)) { |
| 326 | char argbuf[1024], *q = argbuf; |
| 327 | const char *av[18], **pp = av; |
| 328 | int soxpipe[2]; |
| 329 | pid_t soxkid; |
| 330 | *pp++ = "sox"; |
| 331 | soxargs(&pp, &q, &playing->format); |
| 332 | *pp++ = "-"; |
| 333 | soxargs(&pp, &q, &config->sample_format); |
| 334 | *pp++ = "-"; |
| 335 | *pp++ = 0; |
| 336 | if(debugging) { |
| 337 | for(pp = av; *pp; pp++) |
| 338 | D(("sox arg[%d] = %s", pp - av, *pp)); |
| 339 | D(("end args")); |
| 340 | } |
| 341 | xpipe(soxpipe); |
| 342 | soxkid = xfork(); |
| 343 | if(soxkid == 0) { |
| 344 | xdup2(playing->fd, 0); |
| 345 | xdup2(soxpipe[1], 1); |
| 346 | fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK); |
| 347 | close(soxpipe[0]); |
| 348 | close(soxpipe[1]); |
| 349 | close(playing->fd); |
| 350 | execvp("sox", (char **)av); |
| 351 | _exit(1); |
| 352 | } |
| 353 | D(("forking sox for format conversion (kid = %d)", soxkid)); |
| 354 | close(playing->fd); |
| 355 | close(soxpipe[1]); |
| 356 | playing->fd = soxpipe[0]; |
| 357 | playing->format = config->sample_format; |
| 358 | ready = 0; |
| 359 | } |
| 360 | if(!ready) { |
| 361 | pcm_format = config->sample_format; |
| 362 | pcm_bufsize = 3 * FRAMES; |
| 363 | bpf = bytes_per_frame(&config->sample_format); |
| 364 | D(("acquired audio device")); |
| 365 | ready = 1; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | /* If we need to change format then close the current device. */ |
| 370 | if(pcm && !formats_equal(&playing->format, &pcm_format)) |
| 371 | idle(); |
| 372 | if(!pcm) { |
| 373 | D(("snd_pcm_open")); |
| 374 | if((err = snd_pcm_open(&pcm, |
| 375 | config->device, |
| 376 | SND_PCM_STREAM_PLAYBACK, |
| 377 | SND_PCM_NONBLOCK))) { |
| 378 | error(0, "error from snd_pcm_open: %d", err); |
| 379 | goto error; |
| 380 | } |
| 381 | snd_pcm_hw_params_alloca(&hwparams); |
| 382 | D(("set up hw params")); |
| 383 | if((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0) |
| 384 | fatal(0, "error from snd_pcm_hw_params_any: %d", err); |
| 385 | if((err = snd_pcm_hw_params_set_access(pcm, hwparams, |
| 386 | SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) |
| 387 | fatal(0, "error from snd_pcm_hw_params_set_access: %d", err); |
| 388 | switch(playing->format.bits) { |
| 389 | case 8: |
| 390 | sample_format = SND_PCM_FORMAT_S8; |
| 391 | break; |
| 392 | case 16: |
| 393 | switch(playing->format.byte_format) { |
| 394 | case AO_FMT_NATIVE: sample_format = SND_PCM_FORMAT_S16; break; |
| 395 | case AO_FMT_LITTLE: sample_format = SND_PCM_FORMAT_S16_LE; break; |
| 396 | case AO_FMT_BIG: sample_format = SND_PCM_FORMAT_S16_BE; break; |
| 397 | error(0, "unrecognized byte format %d", playing->format.byte_format); |
| 398 | goto fatal; |
| 399 | } |
| 400 | break; |
| 401 | default: |
| 402 | error(0, "unsupported sample size %d", playing->format.bits); |
| 403 | goto fatal; |
| 404 | } |
| 405 | if((err = snd_pcm_hw_params_set_format(pcm, hwparams, |
| 406 | sample_format)) < 0) { |
| 407 | error(0, "error from snd_pcm_hw_params_set_format (%d): %d", |
| 408 | sample_format, err); |
| 409 | goto fatal; |
| 410 | } |
| 411 | rate = playing->format.rate; |
| 412 | if((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0) { |
| 413 | error(0, "error from snd_pcm_hw_params_set_rate (%d): %d", |
| 414 | playing->format.rate, err); |
| 415 | goto fatal; |
| 416 | } |
| 417 | if(rate != (unsigned)playing->format.rate) |
| 418 | info("want rate %d, got %u", playing->format.rate, rate); |
| 419 | if((err = snd_pcm_hw_params_set_channels(pcm, hwparams, |
| 420 | playing->format.channels)) < 0) { |
| 421 | error(0, "error from snd_pcm_hw_params_set_channels (%d): %d", |
| 422 | playing->format.channels, err); |
| 423 | goto fatal; |
| 424 | } |
| 425 | pcm_bufsize = 3 * FRAMES; |
| 426 | if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, |
| 427 | &pcm_bufsize)) < 0) |
| 428 | fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d", |
| 429 | 3 * FRAMES, err); |
| 430 | if(pcm_bufsize != 3 * FRAMES && pcm_bufsize != last_pcm_bufsize) |
| 431 | info("asked for PCM buffer of %d frames, got %d", |
| 432 | 3 * FRAMES, (int)pcm_bufsize); |
| 433 | last_pcm_bufsize = pcm_bufsize; |
| 434 | if((err = snd_pcm_hw_params(pcm, hwparams)) < 0) |
| 435 | fatal(0, "error calling snd_pcm_hw_params: %d", err); |
| 436 | D(("set up sw params")); |
| 437 | snd_pcm_sw_params_alloca(&swparams); |
| 438 | if((err = snd_pcm_sw_params_current(pcm, swparams)) < 0) |
| 439 | fatal(0, "error calling snd_pcm_sw_params_current: %d", err); |
| 440 | if((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, FRAMES)) < 0) |
| 441 | fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d", |
| 442 | FRAMES, err); |
| 443 | if((err = snd_pcm_sw_params(pcm, swparams)) < 0) |
| 444 | fatal(0, "error calling snd_pcm_sw_params: %d", err); |
| 445 | pcm_format = playing->format; |
| 446 | bpf = bytes_per_frame(&pcm_format); |
| 447 | D(("acquired audio device")); |
| 448 | log_params(hwparams, swparams); |
| 449 | ready = 1; |
| 450 | } |
| 451 | return 0; |
| 452 | fatal: |
| 453 | abandon(); |
| 454 | error: |
| 455 | /* We assume the error is temporary and that we'll retry in a bit. */ |
| 456 | if(pcm) { |
| 457 | snd_pcm_close(pcm); |
| 458 | pcm = 0; |
| 459 | } |
| 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | /* Check to see whether the current track has finished playing */ |
| 464 | static void maybe_finished(void) { |
| 465 | if(playing |
| 466 | && playing->eof |
| 467 | && (!playing->got_format |
| 468 | || playing->used < bytes_per_frame(&playing->format))) |
| 469 | abandon(); |
| 470 | } |
| 471 | |
| 472 | static void fork_kid(void) { |
| 473 | pid_t kid; |
| 474 | int pfd[2]; |
| 475 | if(kidfd != -1) close(kidfd); |
| 476 | xpipe(pfd); |
| 477 | kid = xfork(); |
| 478 | if(!kid) { |
| 479 | xdup2(pfd[0], 0); |
| 480 | close(pfd[0]); |
| 481 | close(pfd[1]); |
| 482 | execl("/bin/sh", "sh", "-c", config->speaker_command, (char *)0); |
| 483 | fatal(errno, "error execing /bin/sh"); |
| 484 | } |
| 485 | close(pfd[0]); |
| 486 | kidfd = pfd[1]; |
| 487 | D(("forked kid %d, fd = %d", kid, kidfd)); |
| 488 | } |
| 489 | |
| 490 | static void play(size_t frames) { |
| 491 | snd_pcm_sframes_t written_frames; |
| 492 | size_t avail_bytes, avail_frames; |
| 493 | ssize_t written_bytes; |
| 494 | int err; |
| 495 | |
| 496 | if(activate()) { |
| 497 | if(playing) |
| 498 | forceplay = frames; |
| 499 | else |
| 500 | forceplay = 0; /* Must have called abandon() */ |
| 501 | return; |
| 502 | } |
| 503 | D(("play: play %zu/%zu%s %dHz %db %dc", frames, playing->used / bpf, |
| 504 | playing->eof ? " EOF" : "", |
| 505 | playing->format.rate, |
| 506 | playing->format.bits, |
| 507 | playing->format.channels)); |
| 508 | /* If we haven't got enough bytes yet wait until we have. Exception: when |
| 509 | * we are at eof. */ |
| 510 | if(playing->used < frames * bpf && !playing->eof) { |
| 511 | forceplay = frames; |
| 512 | return; |
| 513 | } |
| 514 | /* We have got enough data so don't force play again */ |
| 515 | forceplay = 0; |
| 516 | /* Figure out how many frames there are available to write */ |
| 517 | if(playing->start + playing->used > playing->size) |
| 518 | avail_bytes = playing->size - playing->start; |
| 519 | else |
| 520 | avail_bytes = playing->used; |
| 521 | |
| 522 | if(kidfd == -1) { |
| 523 | avail_frames = avail_bytes / bpf; |
| 524 | if(avail_frames > frames) |
| 525 | avail_frames = frames; |
| 526 | if(!avail_frames) |
| 527 | return; |
| 528 | written_frames = snd_pcm_writei(pcm, |
| 529 | playing->buffer + playing->start, |
| 530 | avail_frames); |
| 531 | D(("actually play %zu frames, wrote %d", |
| 532 | avail_frames, (int)written_frames)); |
| 533 | if(written_frames < 0) { |
| 534 | switch(written_frames) { |
| 535 | case -EPIPE: /* underrun */ |
| 536 | error(0, "snd_pcm_writei reports underrun"); |
| 537 | if((err = snd_pcm_prepare(pcm)) < 0) |
| 538 | fatal(0, "error calling snd_pcm_prepare: %d", err); |
| 539 | return; |
| 540 | case -EAGAIN: |
| 541 | return; |
| 542 | default: |
| 543 | fatal(0, "error calling snd_pcm_writei: %d", (int)written_frames); |
| 544 | } |
| 545 | } |
| 546 | written_bytes = written_frames * bpf; |
| 547 | } else { |
| 548 | if(avail_bytes > frames * bpf) |
| 549 | avail_bytes = frames * bpf; |
| 550 | written_bytes = write(kidfd, playing->buffer + playing->start, |
| 551 | avail_bytes); |
| 552 | D(("actually play %zu bytes, wrote %d", |
| 553 | avail_bytes, (int)written_bytes)); |
| 554 | if(written_bytes < 0) { |
| 555 | switch(errno) { |
| 556 | case EPIPE: |
| 557 | error(0, "hmm, kid died; trying another"); |
| 558 | fork_kid(); |
| 559 | return; |
| 560 | case EAGAIN: |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | written_frames = written_bytes / bpf; /* good enough */ |
| 565 | } |
| 566 | playing->start += written_bytes; |
| 567 | playing->used -= written_bytes; |
| 568 | playing->played += written_frames; |
| 569 | /* If the pointer is at the end of the buffer (or the buffer is completely |
| 570 | * empty) wrap it back to the start. */ |
| 571 | if(!playing->used || playing->start == playing->size) |
| 572 | playing->start = 0; |
| 573 | frames -= written_frames; |
| 574 | } |
| 575 | |
| 576 | /* Notify the server what we're up to. */ |
| 577 | static void report(void) { |
| 578 | struct speaker_message sm; |
| 579 | |
| 580 | if(playing && playing->buffer != (void *)&playing->format) { |
| 581 | memset(&sm, 0, sizeof sm); |
| 582 | sm.type = paused ? SM_PAUSED : SM_PLAYING; |
| 583 | strcpy(sm.id, playing->id); |
| 584 | sm.data = playing->played / playing->format.rate; |
| 585 | speaker_send(1, &sm, 0); |
| 586 | } |
| 587 | time(&last_report); |
| 588 | } |
| 589 | |
| 590 | static void reap(int __attribute__((unused)) sig) { |
| 591 | pid_t kid; |
| 592 | int st; |
| 593 | |
| 594 | do |
| 595 | kid = waitpid(-1, &st, WNOHANG); |
| 596 | while(kid > 0); |
| 597 | signal(SIGCHLD, reap); |
| 598 | } |
| 599 | |
| 600 | static int addfd(int fd, int events) { |
| 601 | if(fdno < NFDS) { |
| 602 | fds[fdno].fd = fd; |
| 603 | fds[fdno].events = events; |
| 604 | return fdno++; |
| 605 | } else |
| 606 | return -1; |
| 607 | } |
| 608 | |
| 609 | int main(int argc, char **argv) { |
| 610 | int n, fd, stdin_slot, alsa_slots, alsa_nslots = -1, kid_slot, err; |
| 611 | unsigned short alsa_revents; |
| 612 | struct track *t; |
| 613 | struct speaker_message sm; |
| 614 | |
| 615 | set_progname(argv); |
| 616 | mem_init(0); |
| 617 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 618 | while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) { |
| 619 | switch(n) { |
| 620 | case 'h': help(); |
| 621 | case 'V': version(); |
| 622 | case 'c': configfile = optarg; break; |
| 623 | case 'd': debugging = 1; break; |
| 624 | case 'D': debugging = 0; break; |
| 625 | default: fatal(0, "invalid option"); |
| 626 | } |
| 627 | } |
| 628 | if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1; |
| 629 | /* If stderr is a TTY then log there, otherwise to syslog. */ |
| 630 | if(!isatty(2)) { |
| 631 | openlog(progname, LOG_PID, LOG_DAEMON); |
| 632 | log_default = &log_syslog; |
| 633 | } |
| 634 | if(config_read()) fatal(0, "cannot read configuration"); |
| 635 | /* ignore SIGPIPE */ |
| 636 | signal(SIGPIPE, SIG_IGN); |
| 637 | /* reap kids */ |
| 638 | signal(SIGCHLD, reap); |
| 639 | /* set nice value */ |
| 640 | xnice(config->nice_speaker); |
| 641 | /* change user */ |
| 642 | become_mortal(); |
| 643 | /* make sure we're not root, whatever the config says */ |
| 644 | if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root"); |
| 645 | info("started"); |
| 646 | if(config->speaker_command) fork_kid(); |
| 647 | while(getppid() != 1) { |
| 648 | fdno = 0; |
| 649 | /* Always ready for commands from the main server. */ |
| 650 | stdin_slot = addfd(0, POLLIN); |
| 651 | /* Try to read sample data for the currently playing track if there is |
| 652 | * buffer space. */ |
| 653 | if(playing && !playing->eof && playing->used < playing->size) { |
| 654 | playing->slot = addfd(playing->fd, POLLIN); |
| 655 | } else if(playing) |
| 656 | playing->slot = -1; |
| 657 | /* If forceplay is set then wait until it succeeds before waiting on the |
| 658 | * sound device. */ |
| 659 | alsa_slots = -1; |
| 660 | kid_slot = -1; |
| 661 | if(pcm && !forceplay) { |
| 662 | if(kidfd >= 0) |
| 663 | kid_slot = addfd(kidfd, POLLOUT); |
| 664 | else { |
| 665 | int retry = 3; |
| 666 | |
| 667 | alsa_slots = fdno; |
| 668 | do { |
| 669 | retry = 0; |
| 670 | alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno); |
| 671 | if((alsa_nslots <= 0 |
| 672 | || !(fds[alsa_slots].events & POLLOUT)) |
| 673 | && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) { |
| 674 | error(0, "underrun detected after call to snd_pcm_poll_descriptors()"); |
| 675 | if((err = snd_pcm_prepare(pcm))) |
| 676 | fatal(0, "error calling snd_pcm_prepare: %d", err); |
| 677 | } else |
| 678 | break; |
| 679 | } while(retry-- > 0); |
| 680 | if(alsa_nslots >= 0) |
| 681 | fdno += alsa_nslots; |
| 682 | } |
| 683 | } |
| 684 | /* If any other tracks don't have a full buffer, try to read sample data |
| 685 | * from them. */ |
| 686 | for(t = tracks; t; t = t->next) |
| 687 | if(t != playing) { |
| 688 | if(!t->eof && t->used < t->size) { |
| 689 | t->slot = addfd(t->fd, POLLIN | POLLHUP); |
| 690 | } else |
| 691 | t->slot = -1; |
| 692 | } |
| 693 | /* Wait up to a second before thinking about current state */ |
| 694 | n = poll(fds, fdno, 1000); |
| 695 | if(n < 0) { |
| 696 | if(errno == EINTR) continue; |
| 697 | fatal(errno, "error calling poll"); |
| 698 | } |
| 699 | /* Play some sound before doing anything else */ |
| 700 | if(alsa_slots != -1) { |
| 701 | if((err = snd_pcm_poll_descriptors_revents(pcm, |
| 702 | &fds[alsa_slots], |
| 703 | alsa_nslots, |
| 704 | &alsa_revents)) < 0) |
| 705 | fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err); |
| 706 | if(alsa_revents & (POLLOUT | POLLERR)) |
| 707 | play(3 * FRAMES); |
| 708 | } else if(kid_slot != -1) { |
| 709 | if(fds[kid_slot].revents & (POLLOUT | POLLERR)) |
| 710 | play(3 * FRAMES); |
| 711 | } else { |
| 712 | /* Some attempt to play must have failed */ |
| 713 | if(playing && !paused) |
| 714 | play(forceplay); |
| 715 | else |
| 716 | forceplay = 0; /* just in case */ |
| 717 | } |
| 718 | /* Perhaps we have a command to process */ |
| 719 | if(fds[stdin_slot].revents & POLLIN) { |
| 720 | n = speaker_recv(0, &sm, &fd); |
| 721 | if(n > 0) |
| 722 | switch(sm.type) { |
| 723 | case SM_PREPARE: |
| 724 | D(("SM_PREPARE %s %d", sm.id, fd)); |
| 725 | if(fd == -1) fatal(0, "got SM_PREPARE but no file descriptor"); |
| 726 | t = findtrack(sm.id, 1); |
| 727 | acquire(t, fd); |
| 728 | break; |
| 729 | case SM_PLAY: |
| 730 | D(("SM_PLAY %s %d", sm.id, fd)); |
| 731 | if(playing) fatal(0, "got SM_PLAY but already playing something"); |
| 732 | t = findtrack(sm.id, 1); |
| 733 | if(fd != -1) acquire(t, fd); |
| 734 | playing = t; |
| 735 | play(pcm_bufsize); |
| 736 | report(); |
| 737 | break; |
| 738 | case SM_PAUSE: |
| 739 | D(("SM_PAUSE")); |
| 740 | paused = 1; |
| 741 | report(); |
| 742 | break; |
| 743 | case SM_RESUME: |
| 744 | D(("SM_RESUME")); |
| 745 | if(paused) { |
| 746 | paused = 0; |
| 747 | if(playing) |
| 748 | play(pcm_bufsize); |
| 749 | } |
| 750 | report(); |
| 751 | break; |
| 752 | case SM_CANCEL: |
| 753 | D(("SM_CANCEL %s", sm.id)); |
| 754 | t = removetrack(sm.id); |
| 755 | if(t) { |
| 756 | if(t == playing) { |
| 757 | sm.type = SM_FINISHED; |
| 758 | strcpy(sm.id, playing->id); |
| 759 | speaker_send(1, &sm, 0); |
| 760 | playing = 0; |
| 761 | } |
| 762 | destroy(t); |
| 763 | } else |
| 764 | error(0, "SM_CANCEL for unknown track %s", sm.id); |
| 765 | report(); |
| 766 | break; |
| 767 | case SM_RELOAD: |
| 768 | D(("SM_RELOAD")); |
| 769 | if(config_read()) error(0, "cannot read configuration"); |
| 770 | info("reloaded configuration"); |
| 771 | break; |
| 772 | default: |
| 773 | error(0, "unknown message type %d", sm.type); |
| 774 | } |
| 775 | } |
| 776 | /* Read in any buffered data */ |
| 777 | for(t = tracks; t; t = t->next) |
| 778 | if(t->slot != -1 && (fds[t->slot].revents & (POLLIN | POLLHUP))) |
| 779 | fill(t); |
| 780 | /* We might be able to play now */ |
| 781 | if(ready && forceplay && playing && !paused) |
| 782 | play(forceplay); |
| 783 | /* Maybe we finished playing a track somewhere in the above */ |
| 784 | maybe_finished(); |
| 785 | /* If we don't need the sound device for now then close it for the benefit |
| 786 | * of anyone else who wants it. */ |
| 787 | if((!playing || paused) && ready) |
| 788 | idle(); |
| 789 | /* If we've not reported out state for a second do so now. */ |
| 790 | if(time(0) > last_report) |
| 791 | report(); |
| 792 | } |
| 793 | info("stopped (parent terminated)"); |
| 794 | exit(0); |
| 795 | } |
| 796 | #else |
| 797 | int main(int attribute((unused)) argc, char **argv) { |
| 798 | set_progname(argv); |
| 799 | mem_init(0); |
| 800 | fatal(0, "disorder-speaker not supported on this platform"); |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | /* |
| 805 | Local Variables: |
| 806 | c-basic-offset:2 |
| 807 | comment-column:40 |
| 808 | fill-column:79 |
| 809 | indent-tabs-mode:nil |
| 810 | End: |
| 811 | */ |