| 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 | /** @file server/speaker.c |
| 21 | * @brief Speaker process |
| 22 | * |
| 23 | * This program is responsible for transmitting a single coherent audio stream |
| 24 | * to its destination (over the network, to some sound API, to some |
| 25 | * subprocess). It receives connections from decoders via file descriptor |
| 26 | * passing from the main server and plays them in the right order. |
| 27 | * |
| 28 | * @b Encodings. For the <a href="http://www.alsa-project.org/">ALSA</a> API, |
| 29 | * 8- and 16- bit stereo and mono are supported, with any sample rate (within |
| 30 | * the limits that ALSA can deal with.) |
| 31 | * |
| 32 | * When communicating with a subprocess, <a |
| 33 | * href="http://sox.sourceforge.net/">sox</a> is invoked to convert the inbound |
| 34 | * data to a single consistent format. The same applies for network (RTP) |
| 35 | * play, though in that case currently only 44.1KHz 16-bit stereo is supported. |
| 36 | * |
| 37 | * The inbound data starts with a structure defining the data format. Note |
| 38 | * that this is NOT portable between different platforms or even necessarily |
| 39 | * between versions; the speaker is assumed to be built from the same source |
| 40 | * and run on the same host as the main server. |
| 41 | * |
| 42 | * @b Garbage @b Collection. This program deliberately does not use the |
| 43 | * garbage collector even though it might be convenient to do so. This is for |
| 44 | * two reasons. Firstly some sound APIs use thread threads and we do not want |
| 45 | * to have to deal with potential interactions between threading and garbage |
| 46 | * collection. Secondly this process needs to be able to respond quickly and |
| 47 | * this is not compatible with the collector hanging the program even |
| 48 | * relatively briefly. |
| 49 | * |
| 50 | * @b Units. This program thinks at various times in three different units. |
| 51 | * Bytes are obvious. A sample is a single sample on a single channel. A |
| 52 | * frame is several samples on different channels at the same point in time. |
| 53 | * So (for instance) a 16-bit stereo frame is 4 bytes and consists of a pair of |
| 54 | * 2-byte samples. |
| 55 | */ |
| 56 | |
| 57 | #include <config.h> |
| 58 | #include "types.h" |
| 59 | |
| 60 | #include <getopt.h> |
| 61 | #include <stdio.h> |
| 62 | #include <stdlib.h> |
| 63 | #include <locale.h> |
| 64 | #include <syslog.h> |
| 65 | #include <unistd.h> |
| 66 | #include <errno.h> |
| 67 | #include <ao/ao.h> |
| 68 | #include <string.h> |
| 69 | #include <assert.h> |
| 70 | #include <sys/select.h> |
| 71 | #include <sys/wait.h> |
| 72 | #include <time.h> |
| 73 | #include <fcntl.h> |
| 74 | #include <poll.h> |
| 75 | |
| 76 | #include "configuration.h" |
| 77 | #include "syscalls.h" |
| 78 | #include "log.h" |
| 79 | #include "defs.h" |
| 80 | #include "mem.h" |
| 81 | #include "speaker-protocol.h" |
| 82 | #include "user.h" |
| 83 | #include "speaker.h" |
| 84 | |
| 85 | /** @brief Linked list of all prepared tracks */ |
| 86 | struct track *tracks; |
| 87 | |
| 88 | /** @brief Playing track, or NULL */ |
| 89 | struct track *playing; |
| 90 | |
| 91 | /** @brief Number of bytes pre frame */ |
| 92 | size_t device_bpf; |
| 93 | |
| 94 | /** @brief Array of file descriptors for poll() */ |
| 95 | struct pollfd fds[NFDS]; |
| 96 | |
| 97 | /** @brief Next free slot in @ref fds */ |
| 98 | int fdno; |
| 99 | |
| 100 | static time_t last_report; /* when we last reported */ |
| 101 | static int paused; /* pause status */ |
| 102 | |
| 103 | /** @brief The current device state */ |
| 104 | enum device_states device_state; |
| 105 | |
| 106 | /** @brief The current device sample format |
| 107 | * |
| 108 | * Only meaningful if @ref device_state = @ref device_open or perhaps @ref |
| 109 | * device_error. For @ref FIXED_FORMAT backends, this should always match @c |
| 110 | * config->sample_format. |
| 111 | */ |
| 112 | ao_sample_format device_format; |
| 113 | |
| 114 | /** @brief Set when idled |
| 115 | * |
| 116 | * This is set when the sound device is deliberately closed by idle(). |
| 117 | */ |
| 118 | int idled; |
| 119 | |
| 120 | /** @brief Selected backend */ |
| 121 | static const struct speaker_backend *backend; |
| 122 | |
| 123 | static const struct option options[] = { |
| 124 | { "help", no_argument, 0, 'h' }, |
| 125 | { "version", no_argument, 0, 'V' }, |
| 126 | { "config", required_argument, 0, 'c' }, |
| 127 | { "debug", no_argument, 0, 'd' }, |
| 128 | { "no-debug", no_argument, 0, 'D' }, |
| 129 | { 0, 0, 0, 0 } |
| 130 | }; |
| 131 | |
| 132 | /* Display usage message and terminate. */ |
| 133 | static void help(void) { |
| 134 | xprintf("Usage:\n" |
| 135 | " disorder-speaker [OPTIONS]\n" |
| 136 | "Options:\n" |
| 137 | " --help, -h Display usage message\n" |
| 138 | " --version, -V Display version number\n" |
| 139 | " --config PATH, -c PATH Set configuration file\n" |
| 140 | " --debug, -d Turn on debugging\n" |
| 141 | "\n" |
| 142 | "Speaker process for DisOrder. Not intended to be run\n" |
| 143 | "directly.\n"); |
| 144 | xfclose(stdout); |
| 145 | exit(0); |
| 146 | } |
| 147 | |
| 148 | /* Display version number and terminate. */ |
| 149 | static void version(void) { |
| 150 | xprintf("disorder-speaker version %s\n", disorder_version_string); |
| 151 | xfclose(stdout); |
| 152 | exit(0); |
| 153 | } |
| 154 | |
| 155 | /** @brief Return the number of bytes per frame in @p format */ |
| 156 | static size_t bytes_per_frame(const ao_sample_format *format) { |
| 157 | return format->channels * format->bits / 8; |
| 158 | } |
| 159 | |
| 160 | /** @brief Find track @p id, maybe creating it if not found */ |
| 161 | static struct track *findtrack(const char *id, int create) { |
| 162 | struct track *t; |
| 163 | |
| 164 | D(("findtrack %s %d", id, create)); |
| 165 | for(t = tracks; t && strcmp(id, t->id); t = t->next) |
| 166 | ; |
| 167 | if(!t && create) { |
| 168 | t = xmalloc(sizeof *t); |
| 169 | t->next = tracks; |
| 170 | strcpy(t->id, id); |
| 171 | t->fd = -1; |
| 172 | tracks = t; |
| 173 | /* The initial input buffer will be the sample format. */ |
| 174 | t->buffer = (void *)&t->format; |
| 175 | t->size = sizeof t->format; |
| 176 | } |
| 177 | return t; |
| 178 | } |
| 179 | |
| 180 | /** @brief Remove track @p id (but do not destroy it) */ |
| 181 | static struct track *removetrack(const char *id) { |
| 182 | struct track *t, **tt; |
| 183 | |
| 184 | D(("removetrack %s", id)); |
| 185 | for(tt = &tracks; (t = *tt) && strcmp(id, t->id); tt = &t->next) |
| 186 | ; |
| 187 | if(t) |
| 188 | *tt = t->next; |
| 189 | return t; |
| 190 | } |
| 191 | |
| 192 | /** @brief Destroy a track */ |
| 193 | static void destroy(struct track *t) { |
| 194 | D(("destroy %s", t->id)); |
| 195 | if(t->fd != -1) xclose(t->fd); |
| 196 | if(t->buffer != (void *)&t->format) free(t->buffer); |
| 197 | free(t); |
| 198 | } |
| 199 | |
| 200 | /** @brief Notice a new connection */ |
| 201 | static void acquire(struct track *t, int fd) { |
| 202 | D(("acquire %s %d", t->id, fd)); |
| 203 | if(t->fd != -1) |
| 204 | xclose(t->fd); |
| 205 | t->fd = fd; |
| 206 | nonblock(fd); |
| 207 | } |
| 208 | |
| 209 | /** @brief Return true if A and B denote identical libao formats, else false */ |
| 210 | int formats_equal(const ao_sample_format *a, |
| 211 | const ao_sample_format *b) { |
| 212 | return (a->bits == b->bits |
| 213 | && a->rate == b->rate |
| 214 | && a->channels == b->channels |
| 215 | && a->byte_format == b->byte_format); |
| 216 | } |
| 217 | |
| 218 | /** @brief Compute arguments to sox */ |
| 219 | static void soxargs(const char ***pp, char **qq, ao_sample_format *ao) { |
| 220 | int n; |
| 221 | |
| 222 | *(*pp)++ = "-t.raw"; |
| 223 | *(*pp)++ = "-s"; |
| 224 | *(*pp)++ = *qq; n = sprintf(*qq, "-r%d", ao->rate); *qq += n + 1; |
| 225 | *(*pp)++ = *qq; n = sprintf(*qq, "-c%d", ao->channels); *qq += n + 1; |
| 226 | /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are |
| 227 | * deployed! */ |
| 228 | switch(config->sox_generation) { |
| 229 | case 0: |
| 230 | if(ao->bits != 8 |
| 231 | && ao->byte_format != AO_FMT_NATIVE |
| 232 | && ao->byte_format != MACHINE_AO_FMT) { |
| 233 | *(*pp)++ = "-x"; |
| 234 | } |
| 235 | switch(ao->bits) { |
| 236 | case 8: *(*pp)++ = "-b"; break; |
| 237 | case 16: *(*pp)++ = "-w"; break; |
| 238 | case 32: *(*pp)++ = "-l"; break; |
| 239 | case 64: *(*pp)++ = "-d"; break; |
| 240 | default: fatal(0, "cannot handle sample size %d", (int)ao->bits); |
| 241 | } |
| 242 | break; |
| 243 | case 1: |
| 244 | switch(ao->byte_format) { |
| 245 | case AO_FMT_NATIVE: break; |
| 246 | case AO_FMT_BIG: *(*pp)++ = "-B"; break; |
| 247 | case AO_FMT_LITTLE: *(*pp)++ = "-L"; break; |
| 248 | } |
| 249 | *(*pp)++ = *qq; n = sprintf(*qq, "-%d", ao->bits/8); *qq += n + 1; |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** @brief Enable format translation |
| 255 | * |
| 256 | * If necessary, replaces a tracks inbound file descriptor with one connected |
| 257 | * to a sox invocation, which performs the required translation. |
| 258 | */ |
| 259 | static void enable_translation(struct track *t) { |
| 260 | if((backend->flags & FIXED_FORMAT) |
| 261 | && !formats_equal(&t->format, &config->sample_format)) { |
| 262 | char argbuf[1024], *q = argbuf; |
| 263 | const char *av[18], **pp = av; |
| 264 | int soxpipe[2]; |
| 265 | pid_t soxkid; |
| 266 | |
| 267 | *pp++ = "sox"; |
| 268 | soxargs(&pp, &q, &t->format); |
| 269 | *pp++ = "-"; |
| 270 | soxargs(&pp, &q, &config->sample_format); |
| 271 | *pp++ = "-"; |
| 272 | *pp++ = 0; |
| 273 | if(debugging) { |
| 274 | for(pp = av; *pp; pp++) |
| 275 | D(("sox arg[%d] = %s", pp - av, *pp)); |
| 276 | D(("end args")); |
| 277 | } |
| 278 | xpipe(soxpipe); |
| 279 | soxkid = xfork(); |
| 280 | if(soxkid == 0) { |
| 281 | signal(SIGPIPE, SIG_DFL); |
| 282 | xdup2(t->fd, 0); |
| 283 | xdup2(soxpipe[1], 1); |
| 284 | fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK); |
| 285 | close(soxpipe[0]); |
| 286 | close(soxpipe[1]); |
| 287 | close(t->fd); |
| 288 | execvp("sox", (char **)av); |
| 289 | _exit(1); |
| 290 | } |
| 291 | D(("forking sox for format conversion (kid = %d)", soxkid)); |
| 292 | close(t->fd); |
| 293 | close(soxpipe[1]); |
| 294 | t->fd = soxpipe[0]; |
| 295 | t->format = config->sample_format; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** @brief Read data into a sample buffer |
| 300 | * @param t Pointer to track |
| 301 | * @return 0 on success, -1 on EOF |
| 302 | * |
| 303 | * This is effectively the read callback on @c t->fd. It is called from the |
| 304 | * main loop whenever the track's file descriptor is readable, assuming the |
| 305 | * buffer has not reached the maximum allowed occupancy. |
| 306 | */ |
| 307 | static int fill(struct track *t) { |
| 308 | size_t where, left; |
| 309 | int n; |
| 310 | |
| 311 | D(("fill %s: eof=%d used=%zu size=%zu got_format=%d", |
| 312 | t->id, t->eof, t->used, t->size, t->got_format)); |
| 313 | if(t->eof) return -1; |
| 314 | if(t->used < t->size) { |
| 315 | /* there is room left in the buffer */ |
| 316 | where = (t->start + t->used) % t->size; |
| 317 | if(t->got_format) { |
| 318 | /* We are reading audio data, get as much as we can */ |
| 319 | if(where >= t->start) left = t->size - where; |
| 320 | else left = t->start - where; |
| 321 | } else |
| 322 | /* We are still waiting for the format, only get that */ |
| 323 | left = sizeof (ao_sample_format) - t->used; |
| 324 | do { |
| 325 | n = read(t->fd, t->buffer + where, left); |
| 326 | } while(n < 0 && errno == EINTR); |
| 327 | if(n < 0) { |
| 328 | if(errno != EAGAIN) fatal(errno, "error reading sample stream"); |
| 329 | return 0; |
| 330 | } |
| 331 | if(n == 0) { |
| 332 | D(("fill %s: eof detected", t->id)); |
| 333 | t->eof = 1; |
| 334 | return -1; |
| 335 | } |
| 336 | t->used += n; |
| 337 | if(!t->got_format && t->used >= sizeof (ao_sample_format)) { |
| 338 | assert(t->used == sizeof (ao_sample_format)); |
| 339 | /* Check that our assumptions are met. */ |
| 340 | if(t->format.bits & 7) |
| 341 | fatal(0, "bits per sample not a multiple of 8"); |
| 342 | /* If the input format is unsuitable, arrange to translate it */ |
| 343 | enable_translation(t); |
| 344 | /* Make a new buffer for audio data. */ |
| 345 | t->size = bytes_per_frame(&t->format) * t->format.rate * BUFFER_SECONDS; |
| 346 | t->buffer = xmalloc(t->size); |
| 347 | t->used = 0; |
| 348 | t->got_format = 1; |
| 349 | D(("got format for %s", t->id)); |
| 350 | } |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /** @brief Close the sound device |
| 356 | * |
| 357 | * This is called to deactivate the output device when pausing, and also by the |
| 358 | * ALSA backend when changing encoding (in which case the sound device will be |
| 359 | * immediately reactivated). |
| 360 | */ |
| 361 | static void idle(void) { |
| 362 | D(("idle")); |
| 363 | if(backend->deactivate) |
| 364 | backend->deactivate(); |
| 365 | else |
| 366 | device_state = device_closed; |
| 367 | idled = 1; |
| 368 | } |
| 369 | |
| 370 | /** @brief Abandon the current track */ |
| 371 | void abandon(void) { |
| 372 | struct speaker_message sm; |
| 373 | |
| 374 | D(("abandon")); |
| 375 | memset(&sm, 0, sizeof sm); |
| 376 | sm.type = SM_FINISHED; |
| 377 | strcpy(sm.id, playing->id); |
| 378 | speaker_send(1, &sm, 0); |
| 379 | removetrack(playing->id); |
| 380 | destroy(playing); |
| 381 | playing = 0; |
| 382 | } |
| 383 | |
| 384 | /** @brief Enable sound output |
| 385 | * |
| 386 | * Makes sure the sound device is open and has the right sample format. Return |
| 387 | * 0 on success and -1 on error. |
| 388 | */ |
| 389 | static void activate(void) { |
| 390 | /* If we don't know the format yet we cannot start. */ |
| 391 | if(!playing->got_format) { |
| 392 | D((" - not got format for %s", playing->id)); |
| 393 | return; |
| 394 | } |
| 395 | if(backend->flags & FIXED_FORMAT) |
| 396 | device_format = config->sample_format; |
| 397 | if(backend->activate) { |
| 398 | backend->activate(); |
| 399 | } else { |
| 400 | assert(backend->flags & FIXED_FORMAT); |
| 401 | /* ...otherwise device_format not set */ |
| 402 | device_state = device_open; |
| 403 | } |
| 404 | if(device_state == device_open) |
| 405 | device_bpf = bytes_per_frame(&device_format); |
| 406 | } |
| 407 | |
| 408 | /** @brief Check whether the current track has finished |
| 409 | * |
| 410 | * The current track is determined to have finished either if the input stream |
| 411 | * eded before the format could be determined (i.e. it is malformed) or the |
| 412 | * input is at end of file and there is less than a frame left unplayed. (So |
| 413 | * it copes with decoders that crash mid-frame.) |
| 414 | */ |
| 415 | static void maybe_finished(void) { |
| 416 | if(playing |
| 417 | && playing->eof |
| 418 | && (!playing->got_format |
| 419 | || playing->used < bytes_per_frame(&playing->format))) |
| 420 | abandon(); |
| 421 | } |
| 422 | |
| 423 | /** @brief Play up to @p frames frames of audio |
| 424 | * |
| 425 | * It is always safe to call this function. |
| 426 | * - If @ref playing is 0 then it will just return |
| 427 | * - If @ref paused is non-0 then it will just return |
| 428 | * - If @ref device_state != @ref device_open then it will call activate() and |
| 429 | * return if it it fails. |
| 430 | * - If there is not enough audio to play then it play what is available. |
| 431 | * |
| 432 | * If there are not enough frames to play then whatever is available is played |
| 433 | * instead. It is up to mainloop() to ensure that play() is not called when |
| 434 | * unreasonably only an small amounts of data is available to play. |
| 435 | */ |
| 436 | static void play(size_t frames) { |
| 437 | size_t avail_frames, avail_bytes, written_frames; |
| 438 | ssize_t written_bytes; |
| 439 | |
| 440 | /* Make sure there's a track to play and it is not pasued */ |
| 441 | if(!playing || paused) |
| 442 | return; |
| 443 | /* Make sure the output device is open and has the right sample format */ |
| 444 | if(device_state != device_open |
| 445 | || !formats_equal(&device_format, &playing->format)) { |
| 446 | activate(); |
| 447 | if(device_state != device_open) |
| 448 | return; |
| 449 | } |
| 450 | D(("play: play %zu/%zu%s %dHz %db %dc", frames, playing->used / device_bpf, |
| 451 | playing->eof ? " EOF" : "", |
| 452 | playing->format.rate, |
| 453 | playing->format.bits, |
| 454 | playing->format.channels)); |
| 455 | /* Figure out how many frames there are available to write */ |
| 456 | if(playing->start + playing->used > playing->size) |
| 457 | /* The ring buffer is currently wrapped, only play up to the wrap point */ |
| 458 | avail_bytes = playing->size - playing->start; |
| 459 | else |
| 460 | /* The ring buffer is not wrapped, can play the lot */ |
| 461 | avail_bytes = playing->used; |
| 462 | avail_frames = avail_bytes / device_bpf; |
| 463 | /* Only play up to the requested amount */ |
| 464 | if(avail_frames > frames) |
| 465 | avail_frames = frames; |
| 466 | if(!avail_frames) |
| 467 | return; |
| 468 | /* Play it, Sam */ |
| 469 | written_frames = backend->play(avail_frames); |
| 470 | written_bytes = written_frames * device_bpf; |
| 471 | /* written_bytes and written_frames had better both be set and correct by |
| 472 | * this point */ |
| 473 | playing->start += written_bytes; |
| 474 | playing->used -= written_bytes; |
| 475 | playing->played += written_frames; |
| 476 | /* If the pointer is at the end of the buffer (or the buffer is completely |
| 477 | * empty) wrap it back to the start. */ |
| 478 | if(!playing->used || playing->start == playing->size) |
| 479 | playing->start = 0; |
| 480 | frames -= written_frames; |
| 481 | return; |
| 482 | } |
| 483 | |
| 484 | /* Notify the server what we're up to. */ |
| 485 | static void report(void) { |
| 486 | struct speaker_message sm; |
| 487 | |
| 488 | if(playing && playing->buffer != (void *)&playing->format) { |
| 489 | memset(&sm, 0, sizeof sm); |
| 490 | sm.type = paused ? SM_PAUSED : SM_PLAYING; |
| 491 | strcpy(sm.id, playing->id); |
| 492 | sm.data = playing->played / playing->format.rate; |
| 493 | speaker_send(1, &sm, 0); |
| 494 | } |
| 495 | time(&last_report); |
| 496 | } |
| 497 | |
| 498 | static void reap(int __attribute__((unused)) sig) { |
| 499 | pid_t cmdpid; |
| 500 | int st; |
| 501 | |
| 502 | do |
| 503 | cmdpid = waitpid(-1, &st, WNOHANG); |
| 504 | while(cmdpid > 0); |
| 505 | signal(SIGCHLD, reap); |
| 506 | } |
| 507 | |
| 508 | int addfd(int fd, int events) { |
| 509 | if(fdno < NFDS) { |
| 510 | fds[fdno].fd = fd; |
| 511 | fds[fdno].events = events; |
| 512 | return fdno++; |
| 513 | } else |
| 514 | return -1; |
| 515 | } |
| 516 | |
| 517 | /** @brief Table of speaker backends */ |
| 518 | static const struct speaker_backend *backends[] = { |
| 519 | #if API_ALSA |
| 520 | &alsa_backend, |
| 521 | #endif |
| 522 | &command_backend, |
| 523 | &network_backend, |
| 524 | 0 |
| 525 | }; |
| 526 | |
| 527 | /** @brief Return nonzero if we want to play some audio |
| 528 | * |
| 529 | * We want to play audio if there is a current track; and it is not paused; and |
| 530 | * there are at least @ref FRAMES frames of audio to play, or we are in sight |
| 531 | * of the end of the current track. |
| 532 | */ |
| 533 | static int playable(void) { |
| 534 | return playing |
| 535 | && !paused |
| 536 | && (playing->used >= FRAMES || playing->eof); |
| 537 | } |
| 538 | |
| 539 | /** @brief Main event loop */ |
| 540 | static void mainloop(void) { |
| 541 | struct track *t; |
| 542 | struct speaker_message sm; |
| 543 | int n, fd, stdin_slot, timeout; |
| 544 | |
| 545 | while(getppid() != 1) { |
| 546 | fdno = 0; |
| 547 | /* By default we will wait up to a second before thinking about current |
| 548 | * state. */ |
| 549 | timeout = 1000; |
| 550 | /* Always ready for commands from the main server. */ |
| 551 | stdin_slot = addfd(0, POLLIN); |
| 552 | /* Try to read sample data for the currently playing track if there is |
| 553 | * buffer space. */ |
| 554 | if(playing && !playing->eof && playing->used < playing->size) |
| 555 | playing->slot = addfd(playing->fd, POLLIN); |
| 556 | else if(playing) |
| 557 | playing->slot = -1; |
| 558 | if(playable()) { |
| 559 | /* We want to play some audio. If the device is closed then we attempt |
| 560 | * to open it. */ |
| 561 | if(device_state == device_closed) |
| 562 | activate(); |
| 563 | /* If the device is (now) open then we will wait up until it is ready for |
| 564 | * more. If something went wrong then we should have device_error |
| 565 | * instead, but the post-poll code will cope even if it's |
| 566 | * device_closed. */ |
| 567 | if(device_state == device_open) |
| 568 | backend->beforepoll(); |
| 569 | } |
| 570 | /* If any other tracks don't have a full buffer, try to read sample data |
| 571 | * from them. We do this last of all, so that if we run out of slots, |
| 572 | * nothing important can't be monitored. */ |
| 573 | for(t = tracks; t; t = t->next) |
| 574 | if(t != playing) { |
| 575 | if(!t->eof && t->used < t->size) { |
| 576 | t->slot = addfd(t->fd, POLLIN | POLLHUP); |
| 577 | } else |
| 578 | t->slot = -1; |
| 579 | } |
| 580 | /* Wait for something interesting to happen */ |
| 581 | n = poll(fds, fdno, timeout); |
| 582 | if(n < 0) { |
| 583 | if(errno == EINTR) continue; |
| 584 | fatal(errno, "error calling poll"); |
| 585 | } |
| 586 | /* Play some sound before doing anything else */ |
| 587 | if(playable()) { |
| 588 | /* We want to play some audio */ |
| 589 | if(device_state == device_open) { |
| 590 | if(backend->ready()) |
| 591 | play(3 * FRAMES); |
| 592 | } else { |
| 593 | /* We must be in _closed or _error, and it should be the latter, but we |
| 594 | * cope with either. |
| 595 | * |
| 596 | * We most likely timed out, so now is a good time to retry. play() |
| 597 | * knows to re-activate the device if necessary. |
| 598 | */ |
| 599 | play(3 * FRAMES); |
| 600 | } |
| 601 | } |
| 602 | /* Perhaps we have a command to process */ |
| 603 | if(fds[stdin_slot].revents & POLLIN) { |
| 604 | /* There might (in theory) be several commands queued up, but in general |
| 605 | * this won't be the case, so we don't bother looping around to pick them |
| 606 | * all up. */ |
| 607 | n = speaker_recv(0, &sm, &fd); |
| 608 | if(n > 0) |
| 609 | switch(sm.type) { |
| 610 | case SM_PREPARE: |
| 611 | D(("SM_PREPARE %s %d", sm.id, fd)); |
| 612 | if(fd == -1) fatal(0, "got SM_PREPARE but no file descriptor"); |
| 613 | t = findtrack(sm.id, 1); |
| 614 | acquire(t, fd); |
| 615 | break; |
| 616 | case SM_PLAY: |
| 617 | D(("SM_PLAY %s %d", sm.id, fd)); |
| 618 | if(playing) fatal(0, "got SM_PLAY but already playing something"); |
| 619 | t = findtrack(sm.id, 1); |
| 620 | if(fd != -1) acquire(t, fd); |
| 621 | playing = t; |
| 622 | /* We attempt to play straight away rather than going round the loop. |
| 623 | * play() is clever enough to perform any activation that is |
| 624 | * required. */ |
| 625 | play(3 * FRAMES); |
| 626 | report(); |
| 627 | break; |
| 628 | case SM_PAUSE: |
| 629 | D(("SM_PAUSE")); |
| 630 | paused = 1; |
| 631 | report(); |
| 632 | break; |
| 633 | case SM_RESUME: |
| 634 | D(("SM_RESUME")); |
| 635 | if(paused) { |
| 636 | paused = 0; |
| 637 | /* As for SM_PLAY we attempt to play straight away. */ |
| 638 | if(playing) |
| 639 | play(3 * FRAMES); |
| 640 | } |
| 641 | report(); |
| 642 | break; |
| 643 | case SM_CANCEL: |
| 644 | D(("SM_CANCEL %s", sm.id)); |
| 645 | t = removetrack(sm.id); |
| 646 | if(t) { |
| 647 | if(t == playing) { |
| 648 | sm.type = SM_FINISHED; |
| 649 | strcpy(sm.id, playing->id); |
| 650 | speaker_send(1, &sm, 0); |
| 651 | playing = 0; |
| 652 | } |
| 653 | destroy(t); |
| 654 | } else |
| 655 | error(0, "SM_CANCEL for unknown track %s", sm.id); |
| 656 | report(); |
| 657 | break; |
| 658 | case SM_RELOAD: |
| 659 | D(("SM_RELOAD")); |
| 660 | if(config_read()) error(0, "cannot read configuration"); |
| 661 | info("reloaded configuration"); |
| 662 | break; |
| 663 | default: |
| 664 | error(0, "unknown message type %d", sm.type); |
| 665 | } |
| 666 | } |
| 667 | /* Read in any buffered data */ |
| 668 | for(t = tracks; t; t = t->next) |
| 669 | if(t->slot != -1 && (fds[t->slot].revents & (POLLIN | POLLHUP))) |
| 670 | fill(t); |
| 671 | /* Maybe we finished playing a track somewhere in the above */ |
| 672 | maybe_finished(); |
| 673 | /* If we don't need the sound device for now then close it for the benefit |
| 674 | * of anyone else who wants it. */ |
| 675 | if((!playing || paused) && device_state == device_open) |
| 676 | idle(); |
| 677 | /* If we've not reported out state for a second do so now. */ |
| 678 | if(time(0) > last_report) |
| 679 | report(); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | int main(int argc, char **argv) { |
| 684 | int n; |
| 685 | |
| 686 | set_progname(argv); |
| 687 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 688 | while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) { |
| 689 | switch(n) { |
| 690 | case 'h': help(); |
| 691 | case 'V': version(); |
| 692 | case 'c': configfile = optarg; break; |
| 693 | case 'd': debugging = 1; break; |
| 694 | case 'D': debugging = 0; break; |
| 695 | default: fatal(0, "invalid option"); |
| 696 | } |
| 697 | } |
| 698 | if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1; |
| 699 | /* If stderr is a TTY then log there, otherwise to syslog. */ |
| 700 | if(!isatty(2)) { |
| 701 | openlog(progname, LOG_PID, LOG_DAEMON); |
| 702 | log_default = &log_syslog; |
| 703 | } |
| 704 | if(config_read()) fatal(0, "cannot read configuration"); |
| 705 | /* ignore SIGPIPE */ |
| 706 | signal(SIGPIPE, SIG_IGN); |
| 707 | /* reap kids */ |
| 708 | signal(SIGCHLD, reap); |
| 709 | /* set nice value */ |
| 710 | xnice(config->nice_speaker); |
| 711 | /* change user */ |
| 712 | become_mortal(); |
| 713 | /* make sure we're not root, whatever the config says */ |
| 714 | if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root"); |
| 715 | /* identify the backend used to play */ |
| 716 | for(n = 0; backends[n]; ++n) |
| 717 | if(backends[n]->backend == config->speaker_backend) |
| 718 | break; |
| 719 | if(!backends[n]) |
| 720 | fatal(0, "unsupported backend %d", config->speaker_backend); |
| 721 | backend = backends[n]; |
| 722 | /* backend-specific initialization */ |
| 723 | backend->init(); |
| 724 | mainloop(); |
| 725 | info("stopped (parent terminated)"); |
| 726 | exit(0); |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | Local Variables: |
| 731 | c-basic-offset:2 |
| 732 | comment-column:40 |
| 733 | fill-column:79 |
| 734 | indent-tabs-mode:nil |
| 735 | End: |
| 736 | */ |