| 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 processs |
| 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 | #include <sys/socket.h> |
| 76 | #include <netdb.h> |
| 77 | #include <gcrypt.h> |
| 78 | #include <sys/uio.h> |
| 79 | |
| 80 | #include "configuration.h" |
| 81 | #include "syscalls.h" |
| 82 | #include "log.h" |
| 83 | #include "defs.h" |
| 84 | #include "mem.h" |
| 85 | #include "speaker.h" |
| 86 | #include "user.h" |
| 87 | #include "addr.h" |
| 88 | #include "timeval.h" |
| 89 | #include "rtp.h" |
| 90 | |
| 91 | #if API_ALSA |
| 92 | #include <alsa/asoundlib.h> |
| 93 | #endif |
| 94 | |
| 95 | #ifdef WORDS_BIGENDIAN |
| 96 | # define MACHINE_AO_FMT AO_FMT_BIG |
| 97 | #else |
| 98 | # define MACHINE_AO_FMT AO_FMT_LITTLE |
| 99 | #endif |
| 100 | |
| 101 | /** @brief How many seconds of input to buffer |
| 102 | * |
| 103 | * While any given connection has this much audio buffered, no more reads will |
| 104 | * be issued for that connection. The decoder will have to wait. |
| 105 | */ |
| 106 | #define BUFFER_SECONDS 5 |
| 107 | |
| 108 | /** @brief Frame batch size |
| 109 | * |
| 110 | * This controls how many frames are written in one go. |
| 111 | * |
| 112 | * For ALSA we request a buffer of three times this size and set the low |
| 113 | * watermark to this amount. The goal is then to keep between 1 and 3 times |
| 114 | * this many frames in play. |
| 115 | * |
| 116 | * For all backends we attempt to play up to three times this many frames per |
| 117 | * shot. In practice we will often only send much less than this. |
| 118 | */ |
| 119 | #define FRAMES 4096 |
| 120 | |
| 121 | /** @brief Bytes to send per network packet |
| 122 | * |
| 123 | * Don't make this too big or arithmetic will start to overflow. |
| 124 | */ |
| 125 | #define NETWORK_BYTES (1024+sizeof(struct rtp_header)) |
| 126 | |
| 127 | /** @brief Maximum RTP playahead (ms) */ |
| 128 | #define RTP_AHEAD_MS 1000 |
| 129 | |
| 130 | /** @brief Maximum number of FDs to poll for */ |
| 131 | #define NFDS 256 |
| 132 | |
| 133 | /** @brief Track structure |
| 134 | * |
| 135 | * Known tracks are kept in a linked list. Usually there will be at most two |
| 136 | * of these but rearranging the queue can cause there to be more. |
| 137 | */ |
| 138 | static struct track { |
| 139 | struct track *next; /* next track */ |
| 140 | int fd; /* input FD */ |
| 141 | char id[24]; /* ID */ |
| 142 | size_t start, used; /* start + bytes used */ |
| 143 | int eof; /* input is at EOF */ |
| 144 | int got_format; /* got format yet? */ |
| 145 | ao_sample_format format; /* sample format */ |
| 146 | unsigned long long played; /* number of frames played */ |
| 147 | char *buffer; /* sample buffer */ |
| 148 | size_t size; /* sample buffer size */ |
| 149 | int slot; /* poll array slot */ |
| 150 | } *tracks, *playing; /* all tracks + playing track */ |
| 151 | |
| 152 | static time_t last_report; /* when we last reported */ |
| 153 | static int paused; /* pause status */ |
| 154 | static size_t bpf; /* bytes per frame */ |
| 155 | static struct pollfd fds[NFDS]; /* if we need more than that */ |
| 156 | static int fdno; /* fd number */ |
| 157 | static size_t bufsize; /* buffer size */ |
| 158 | #if API_ALSA |
| 159 | /** @brief The current PCM handle */ |
| 160 | static snd_pcm_t *pcm; |
| 161 | static snd_pcm_uframes_t last_pcm_bufsize; /* last seen buffer size */ |
| 162 | static ao_sample_format pcm_format; /* current format if aodev != 0 */ |
| 163 | #endif |
| 164 | |
| 165 | /** @brief Ready to send audio |
| 166 | * |
| 167 | * This is set when the destination is ready to receive audio. Generally |
| 168 | * this implies that the sound device is open. In the ALSA backend it |
| 169 | * does @b not necessarily imply that is has the right sample format. |
| 170 | */ |
| 171 | static int ready; |
| 172 | |
| 173 | /** @brief Frames to force-play |
| 174 | * |
| 175 | * If this is nonzero, and playing is enabled, then the main loop will attempt |
| 176 | * to play this many frames without checking whether the output device is |
| 177 | * ready. |
| 178 | */ |
| 179 | static int forceplay; |
| 180 | |
| 181 | /** @brief Pipe to subprocess |
| 182 | * |
| 183 | * This is the file descriptor to write to for @ref BACKEND_COMMAND. |
| 184 | */ |
| 185 | static int cmdfd = -1; |
| 186 | |
| 187 | /** @brief Network socket |
| 188 | * |
| 189 | * This is the file descriptor to write to for @ref BACKEND_NETWORK. |
| 190 | */ |
| 191 | static int bfd = -1; |
| 192 | |
| 193 | /** @brief RTP timestamp |
| 194 | * |
| 195 | * This counts the number of samples played (NB not the number of frames |
| 196 | * played). |
| 197 | * |
| 198 | * The timestamp in the packet header is only 32 bits wide. With 44100Hz |
| 199 | * stereo, that only gives about half a day before wrapping, which is not |
| 200 | * particularly convenient for certain debugging purposes. Therefore the |
| 201 | * timestamp is maintained as a 64-bit integer, giving around six million years |
| 202 | * before wrapping, and truncated to 32 bits when transmitting. |
| 203 | */ |
| 204 | static uint64_t rtp_time; |
| 205 | |
| 206 | /** @brief RTP base timestamp |
| 207 | * |
| 208 | * This is the real time correspoding to an @ref rtp_time of 0. It is used |
| 209 | * to recalculate the timestamp after idle periods. |
| 210 | */ |
| 211 | static struct timeval rtp_time_0; |
| 212 | |
| 213 | /** @brief RTP packet sequence number */ |
| 214 | static uint16_t rtp_seq; |
| 215 | |
| 216 | /** @brief RTP SSRC */ |
| 217 | static uint32_t rtp_id; |
| 218 | |
| 219 | /** @brief Set when idled |
| 220 | * |
| 221 | * This is set when the sound device is deliberately closed by idle(). |
| 222 | * @ref ready is set to 0 at the same time. |
| 223 | */ |
| 224 | static int idled; /* set when idled */ |
| 225 | |
| 226 | /** @brief Error counter */ |
| 227 | static int audio_errors; |
| 228 | |
| 229 | /** @brief Structure of a backend */ |
| 230 | struct speaker_backend { |
| 231 | /** @brief Which backend this is |
| 232 | * |
| 233 | * @c -1 terminates the list. |
| 234 | */ |
| 235 | int backend; |
| 236 | |
| 237 | /** @brief Flags |
| 238 | * |
| 239 | * Possible values |
| 240 | * - @ref FIXED_FORMAT |
| 241 | */ |
| 242 | unsigned flags; |
| 243 | /** @brief Lock to configured sample format */ |
| 244 | #define FIXED_FORMAT 0x0001 |
| 245 | |
| 246 | /** @brief Initialization |
| 247 | * |
| 248 | * Called once at startup. This is responsible for one-time setup |
| 249 | * operations, for instance opening a network socket to transmit to. |
| 250 | * |
| 251 | * When writing to a native sound API this might @b not imply opening the |
| 252 | * native sound device - that might be done by @c activate below. |
| 253 | */ |
| 254 | void (*init)(void); |
| 255 | |
| 256 | /** @brief Activation |
| 257 | * @return 0 on success, non-0 on error |
| 258 | * |
| 259 | * Called to activate the output device. |
| 260 | * |
| 261 | * After this function succeeds, @ref ready should be non-0. As well as |
| 262 | * opening the audio device, this function is responsible for reconfiguring |
| 263 | * if it necessary to cope with different samples formats (for backends that |
| 264 | * don't demand a single fixed sample format for the lifetime of the server). |
| 265 | */ |
| 266 | int (*activate)(void); |
| 267 | |
| 268 | /** @brief Play sound |
| 269 | * @param frames Number of frames to play |
| 270 | * @return Number of frames actually played |
| 271 | */ |
| 272 | size_t (*play)(size_t frames); |
| 273 | |
| 274 | /** @brief Deactivation |
| 275 | * |
| 276 | * Called to deactivate the sound device. This is the inverse of |
| 277 | * @c activate above. |
| 278 | */ |
| 279 | void (*deactivate)(void); |
| 280 | |
| 281 | /** @brief Called before poll() |
| 282 | * |
| 283 | * Called before the call to poll(). Should call addfd() to update the FD |
| 284 | * array and stash the slot number somewhere safe. |
| 285 | */ |
| 286 | void (*beforepoll)(void); |
| 287 | |
| 288 | /** @brief Called after poll() |
| 289 | * @return 0 if we could play, non-0 if not |
| 290 | * |
| 291 | * Called after the call to poll(). Should arrange to play some audio if the |
| 292 | * output device is ready. |
| 293 | * |
| 294 | * The return value should be 0 if the device was ready to play, or nonzero |
| 295 | * if it was not. |
| 296 | */ |
| 297 | int (*afterpoll)(void); |
| 298 | }; |
| 299 | |
| 300 | /** @brief Selected backend */ |
| 301 | static const struct speaker_backend *backend; |
| 302 | |
| 303 | static const struct option options[] = { |
| 304 | { "help", no_argument, 0, 'h' }, |
| 305 | { "version", no_argument, 0, 'V' }, |
| 306 | { "config", required_argument, 0, 'c' }, |
| 307 | { "debug", no_argument, 0, 'd' }, |
| 308 | { "no-debug", no_argument, 0, 'D' }, |
| 309 | { 0, 0, 0, 0 } |
| 310 | }; |
| 311 | |
| 312 | /* Display usage message and terminate. */ |
| 313 | static void help(void) { |
| 314 | xprintf("Usage:\n" |
| 315 | " disorder-speaker [OPTIONS]\n" |
| 316 | "Options:\n" |
| 317 | " --help, -h Display usage message\n" |
| 318 | " --version, -V Display version number\n" |
| 319 | " --config PATH, -c PATH Set configuration file\n" |
| 320 | " --debug, -d Turn on debugging\n" |
| 321 | "\n" |
| 322 | "Speaker process for DisOrder. Not intended to be run\n" |
| 323 | "directly.\n"); |
| 324 | xfclose(stdout); |
| 325 | exit(0); |
| 326 | } |
| 327 | |
| 328 | /* Display version number and terminate. */ |
| 329 | static void version(void) { |
| 330 | xprintf("disorder-speaker version %s\n", disorder_version_string); |
| 331 | xfclose(stdout); |
| 332 | exit(0); |
| 333 | } |
| 334 | |
| 335 | /** @brief Return the number of bytes per frame in @p format */ |
| 336 | static size_t bytes_per_frame(const ao_sample_format *format) { |
| 337 | return format->channels * format->bits / 8; |
| 338 | } |
| 339 | |
| 340 | /** @brief Find track @p id, maybe creating it if not found */ |
| 341 | static struct track *findtrack(const char *id, int create) { |
| 342 | struct track *t; |
| 343 | |
| 344 | D(("findtrack %s %d", id, create)); |
| 345 | for(t = tracks; t && strcmp(id, t->id); t = t->next) |
| 346 | ; |
| 347 | if(!t && create) { |
| 348 | t = xmalloc(sizeof *t); |
| 349 | t->next = tracks; |
| 350 | strcpy(t->id, id); |
| 351 | t->fd = -1; |
| 352 | tracks = t; |
| 353 | /* The initial input buffer will be the sample format. */ |
| 354 | t->buffer = (void *)&t->format; |
| 355 | t->size = sizeof t->format; |
| 356 | } |
| 357 | return t; |
| 358 | } |
| 359 | |
| 360 | /** @brief Remove track @p id (but do not destroy it) */ |
| 361 | static struct track *removetrack(const char *id) { |
| 362 | struct track *t, **tt; |
| 363 | |
| 364 | D(("removetrack %s", id)); |
| 365 | for(tt = &tracks; (t = *tt) && strcmp(id, t->id); tt = &t->next) |
| 366 | ; |
| 367 | if(t) |
| 368 | *tt = t->next; |
| 369 | return t; |
| 370 | } |
| 371 | |
| 372 | /** @brief Destroy a track */ |
| 373 | static void destroy(struct track *t) { |
| 374 | D(("destroy %s", t->id)); |
| 375 | if(t->fd != -1) xclose(t->fd); |
| 376 | if(t->buffer != (void *)&t->format) free(t->buffer); |
| 377 | free(t); |
| 378 | } |
| 379 | |
| 380 | /** @brief Notice a new connection */ |
| 381 | static void acquire(struct track *t, int fd) { |
| 382 | D(("acquire %s %d", t->id, fd)); |
| 383 | if(t->fd != -1) |
| 384 | xclose(t->fd); |
| 385 | t->fd = fd; |
| 386 | nonblock(fd); |
| 387 | } |
| 388 | |
| 389 | /** @brief Return true if A and B denote identical libao formats, else false */ |
| 390 | static int formats_equal(const ao_sample_format *a, |
| 391 | const ao_sample_format *b) { |
| 392 | return (a->bits == b->bits |
| 393 | && a->rate == b->rate |
| 394 | && a->channels == b->channels |
| 395 | && a->byte_format == b->byte_format); |
| 396 | } |
| 397 | |
| 398 | /** @brief Compute arguments to sox */ |
| 399 | static void soxargs(const char ***pp, char **qq, ao_sample_format *ao) { |
| 400 | int n; |
| 401 | |
| 402 | *(*pp)++ = "-t.raw"; |
| 403 | *(*pp)++ = "-s"; |
| 404 | *(*pp)++ = *qq; n = sprintf(*qq, "-r%d", ao->rate); *qq += n + 1; |
| 405 | *(*pp)++ = *qq; n = sprintf(*qq, "-c%d", ao->channels); *qq += n + 1; |
| 406 | /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are |
| 407 | * deployed! */ |
| 408 | switch(config->sox_generation) { |
| 409 | case 0: |
| 410 | if(ao->bits != 8 |
| 411 | && ao->byte_format != AO_FMT_NATIVE |
| 412 | && ao->byte_format != MACHINE_AO_FMT) { |
| 413 | *(*pp)++ = "-x"; |
| 414 | } |
| 415 | switch(ao->bits) { |
| 416 | case 8: *(*pp)++ = "-b"; break; |
| 417 | case 16: *(*pp)++ = "-w"; break; |
| 418 | case 32: *(*pp)++ = "-l"; break; |
| 419 | case 64: *(*pp)++ = "-d"; break; |
| 420 | default: fatal(0, "cannot handle sample size %d", (int)ao->bits); |
| 421 | } |
| 422 | break; |
| 423 | case 1: |
| 424 | switch(ao->byte_format) { |
| 425 | case AO_FMT_NATIVE: break; |
| 426 | case AO_FMT_BIG: *(*pp)++ = "-B"; break; |
| 427 | case AO_FMT_LITTLE: *(*pp)++ = "-L"; break; |
| 428 | } |
| 429 | *(*pp)++ = *qq; n = sprintf(*qq, "-%d", ao->bits/8); *qq += n + 1; |
| 430 | break; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** @brief Enable format translation |
| 435 | * |
| 436 | * If necessary, replaces a tracks inbound file descriptor with one connected |
| 437 | * to a sox invocation, which performs the required translation. |
| 438 | */ |
| 439 | static void enable_translation(struct track *t) { |
| 440 | if((backend->flags & FIXED_FORMAT) |
| 441 | && !formats_equal(&t->format, &config->sample_format)) { |
| 442 | char argbuf[1024], *q = argbuf; |
| 443 | const char *av[18], **pp = av; |
| 444 | int soxpipe[2]; |
| 445 | pid_t soxkid; |
| 446 | |
| 447 | *pp++ = "sox"; |
| 448 | soxargs(&pp, &q, &t->format); |
| 449 | *pp++ = "-"; |
| 450 | soxargs(&pp, &q, &config->sample_format); |
| 451 | *pp++ = "-"; |
| 452 | *pp++ = 0; |
| 453 | if(debugging) { |
| 454 | for(pp = av; *pp; pp++) |
| 455 | D(("sox arg[%d] = %s", pp - av, *pp)); |
| 456 | D(("end args")); |
| 457 | } |
| 458 | xpipe(soxpipe); |
| 459 | soxkid = xfork(); |
| 460 | if(soxkid == 0) { |
| 461 | signal(SIGPIPE, SIG_DFL); |
| 462 | xdup2(t->fd, 0); |
| 463 | xdup2(soxpipe[1], 1); |
| 464 | fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK); |
| 465 | close(soxpipe[0]); |
| 466 | close(soxpipe[1]); |
| 467 | close(t->fd); |
| 468 | execvp("sox", (char **)av); |
| 469 | _exit(1); |
| 470 | } |
| 471 | D(("forking sox for format conversion (kid = %d)", soxkid)); |
| 472 | close(t->fd); |
| 473 | close(soxpipe[1]); |
| 474 | t->fd = soxpipe[0]; |
| 475 | t->format = config->sample_format; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** @brief Read data into a sample buffer |
| 480 | * @param t Pointer to track |
| 481 | * @return 0 on success, -1 on EOF |
| 482 | * |
| 483 | * This is effectively the read callback on @c t->fd. It is called from the |
| 484 | * main loop whenever the track's file descriptor is readable, assuming the |
| 485 | * buffer has not reached the maximum allowed occupancy. |
| 486 | */ |
| 487 | static int fill(struct track *t) { |
| 488 | size_t where, left; |
| 489 | int n; |
| 490 | |
| 491 | D(("fill %s: eof=%d used=%zu size=%zu got_format=%d", |
| 492 | t->id, t->eof, t->used, t->size, t->got_format)); |
| 493 | if(t->eof) return -1; |
| 494 | if(t->used < t->size) { |
| 495 | /* there is room left in the buffer */ |
| 496 | where = (t->start + t->used) % t->size; |
| 497 | if(t->got_format) { |
| 498 | /* We are reading audio data, get as much as we can */ |
| 499 | if(where >= t->start) left = t->size - where; |
| 500 | else left = t->start - where; |
| 501 | } else |
| 502 | /* We are still waiting for the format, only get that */ |
| 503 | left = sizeof (ao_sample_format) - t->used; |
| 504 | do { |
| 505 | n = read(t->fd, t->buffer + where, left); |
| 506 | } while(n < 0 && errno == EINTR); |
| 507 | if(n < 0) { |
| 508 | if(errno != EAGAIN) fatal(errno, "error reading sample stream"); |
| 509 | return 0; |
| 510 | } |
| 511 | if(n == 0) { |
| 512 | D(("fill %s: eof detected", t->id)); |
| 513 | t->eof = 1; |
| 514 | return -1; |
| 515 | } |
| 516 | t->used += n; |
| 517 | if(!t->got_format && t->used >= sizeof (ao_sample_format)) { |
| 518 | assert(t->used == sizeof (ao_sample_format)); |
| 519 | /* Check that our assumptions are met. */ |
| 520 | if(t->format.bits & 7) |
| 521 | fatal(0, "bits per sample not a multiple of 8"); |
| 522 | /* If the input format is unsuitable, arrange to translate it */ |
| 523 | enable_translation(t); |
| 524 | /* Make a new buffer for audio data. */ |
| 525 | t->size = bytes_per_frame(&t->format) * t->format.rate * BUFFER_SECONDS; |
| 526 | t->buffer = xmalloc(t->size); |
| 527 | t->used = 0; |
| 528 | t->got_format = 1; |
| 529 | D(("got format for %s", t->id)); |
| 530 | } |
| 531 | } |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /** @brief Close the sound device |
| 536 | * |
| 537 | * This is called to deactivate the output device when pausing, and also by the |
| 538 | * ALSA backend when changing encoding (in which case the sound device will be |
| 539 | * immediately reactivated). |
| 540 | */ |
| 541 | static void idle(void) { |
| 542 | D(("idle")); |
| 543 | if(backend->deactivate) |
| 544 | backend->deactivate(); |
| 545 | idled = 1; |
| 546 | ready = 0; |
| 547 | } |
| 548 | |
| 549 | /** @brief Abandon the current track */ |
| 550 | static void abandon(void) { |
| 551 | struct speaker_message sm; |
| 552 | |
| 553 | D(("abandon")); |
| 554 | memset(&sm, 0, sizeof sm); |
| 555 | sm.type = SM_FINISHED; |
| 556 | strcpy(sm.id, playing->id); |
| 557 | speaker_send(1, &sm, 0); |
| 558 | removetrack(playing->id); |
| 559 | destroy(playing); |
| 560 | playing = 0; |
| 561 | forceplay = 0; |
| 562 | } |
| 563 | |
| 564 | #if API_ALSA |
| 565 | /** @brief Log ALSA parameters */ |
| 566 | static void log_params(snd_pcm_hw_params_t *hwparams, |
| 567 | snd_pcm_sw_params_t *swparams) { |
| 568 | snd_pcm_uframes_t f; |
| 569 | unsigned u; |
| 570 | |
| 571 | return; /* too verbose */ |
| 572 | if(hwparams) { |
| 573 | /* TODO */ |
| 574 | } |
| 575 | if(swparams) { |
| 576 | snd_pcm_sw_params_get_silence_size(swparams, &f); |
| 577 | info("sw silence_size=%lu", (unsigned long)f); |
| 578 | snd_pcm_sw_params_get_silence_threshold(swparams, &f); |
| 579 | info("sw silence_threshold=%lu", (unsigned long)f); |
| 580 | snd_pcm_sw_params_get_sleep_min(swparams, &u); |
| 581 | info("sw sleep_min=%lu", (unsigned long)u); |
| 582 | snd_pcm_sw_params_get_start_threshold(swparams, &f); |
| 583 | info("sw start_threshold=%lu", (unsigned long)f); |
| 584 | snd_pcm_sw_params_get_stop_threshold(swparams, &f); |
| 585 | info("sw stop_threshold=%lu", (unsigned long)f); |
| 586 | snd_pcm_sw_params_get_xfer_align(swparams, &f); |
| 587 | info("sw xfer_align=%lu", (unsigned long)f); |
| 588 | } |
| 589 | } |
| 590 | #endif |
| 591 | |
| 592 | /** @brief Enable sound output |
| 593 | * |
| 594 | * Makes sure the sound device is open and has the right sample format. Return |
| 595 | * 0 on success and -1 on error. |
| 596 | */ |
| 597 | static int activate(void) { |
| 598 | /* If we don't know the format yet we cannot start. */ |
| 599 | if(!playing->got_format) { |
| 600 | D((" - not got format for %s", playing->id)); |
| 601 | return -1; |
| 602 | } |
| 603 | return backend->activate(); |
| 604 | } |
| 605 | |
| 606 | /** @brief Check whether the current track has finished |
| 607 | * |
| 608 | * The current track is determined to have finished either if the input stream |
| 609 | * eded before the format could be determined (i.e. it is malformed) or the |
| 610 | * input is at end of file and there is less than a frame left unplayed. (So |
| 611 | * it copes with decoders that crash mid-frame.) |
| 612 | */ |
| 613 | static void maybe_finished(void) { |
| 614 | if(playing |
| 615 | && playing->eof |
| 616 | && (!playing->got_format |
| 617 | || playing->used < bytes_per_frame(&playing->format))) |
| 618 | abandon(); |
| 619 | } |
| 620 | |
| 621 | /** @brief Start the subprocess for @ref BACKEND_COMMAND */ |
| 622 | static void fork_cmd(void) { |
| 623 | pid_t cmdpid; |
| 624 | int pfd[2]; |
| 625 | if(cmdfd != -1) close(cmdfd); |
| 626 | xpipe(pfd); |
| 627 | cmdpid = xfork(); |
| 628 | if(!cmdpid) { |
| 629 | signal(SIGPIPE, SIG_DFL); |
| 630 | xdup2(pfd[0], 0); |
| 631 | close(pfd[0]); |
| 632 | close(pfd[1]); |
| 633 | execl("/bin/sh", "sh", "-c", config->speaker_command, (char *)0); |
| 634 | fatal(errno, "error execing /bin/sh"); |
| 635 | } |
| 636 | close(pfd[0]); |
| 637 | cmdfd = pfd[1]; |
| 638 | D(("forked cmd %d, fd = %d", cmdpid, cmdfd)); |
| 639 | } |
| 640 | |
| 641 | /** @brief Play up to @p frames frames of audio */ |
| 642 | static void play(size_t frames) { |
| 643 | size_t avail_frames, avail_bytes, written_frames; |
| 644 | ssize_t written_bytes; |
| 645 | |
| 646 | /* Make sure the output device is activated */ |
| 647 | if(activate()) { |
| 648 | if(playing) |
| 649 | forceplay = frames; |
| 650 | else |
| 651 | forceplay = 0; /* Must have called abandon() */ |
| 652 | return; |
| 653 | } |
| 654 | D(("play: play %zu/%zu%s %dHz %db %dc", frames, playing->used / bpf, |
| 655 | playing->eof ? " EOF" : "", |
| 656 | playing->format.rate, |
| 657 | playing->format.bits, |
| 658 | playing->format.channels)); |
| 659 | /* If we haven't got enough bytes yet wait until we have. Exception: when |
| 660 | * we are at eof. */ |
| 661 | if(playing->used < frames * bpf && !playing->eof) { |
| 662 | forceplay = frames; |
| 663 | return; |
| 664 | } |
| 665 | /* We have got enough data so don't force play again */ |
| 666 | forceplay = 0; |
| 667 | /* Figure out how many frames there are available to write */ |
| 668 | if(playing->start + playing->used > playing->size) |
| 669 | /* The ring buffer is currently wrapped, only play up to the wrap point */ |
| 670 | avail_bytes = playing->size - playing->start; |
| 671 | else |
| 672 | /* The ring buffer is not wrapped, can play the lot */ |
| 673 | avail_bytes = playing->used; |
| 674 | avail_frames = avail_bytes / bpf; |
| 675 | /* Only play up to the requested amount */ |
| 676 | if(avail_frames > frames) |
| 677 | avail_frames = frames; |
| 678 | if(!avail_frames) |
| 679 | return; |
| 680 | /* Play it, Sam */ |
| 681 | written_frames = backend->play(avail_frames); |
| 682 | written_bytes = written_frames * bpf; |
| 683 | /* written_bytes and written_frames had better both be set and correct by |
| 684 | * this point */ |
| 685 | playing->start += written_bytes; |
| 686 | playing->used -= written_bytes; |
| 687 | playing->played += written_frames; |
| 688 | /* If the pointer is at the end of the buffer (or the buffer is completely |
| 689 | * empty) wrap it back to the start. */ |
| 690 | if(!playing->used || playing->start == playing->size) |
| 691 | playing->start = 0; |
| 692 | frames -= written_frames; |
| 693 | } |
| 694 | |
| 695 | /* Notify the server what we're up to. */ |
| 696 | static void report(void) { |
| 697 | struct speaker_message sm; |
| 698 | |
| 699 | if(playing && playing->buffer != (void *)&playing->format) { |
| 700 | memset(&sm, 0, sizeof sm); |
| 701 | sm.type = paused ? SM_PAUSED : SM_PLAYING; |
| 702 | strcpy(sm.id, playing->id); |
| 703 | sm.data = playing->played / playing->format.rate; |
| 704 | speaker_send(1, &sm, 0); |
| 705 | } |
| 706 | time(&last_report); |
| 707 | } |
| 708 | |
| 709 | static void reap(int __attribute__((unused)) sig) { |
| 710 | pid_t cmdpid; |
| 711 | int st; |
| 712 | |
| 713 | do |
| 714 | cmdpid = waitpid(-1, &st, WNOHANG); |
| 715 | while(cmdpid > 0); |
| 716 | signal(SIGCHLD, reap); |
| 717 | } |
| 718 | |
| 719 | static int addfd(int fd, int events) { |
| 720 | if(fdno < NFDS) { |
| 721 | fds[fdno].fd = fd; |
| 722 | fds[fdno].events = events; |
| 723 | return fdno++; |
| 724 | } else |
| 725 | return -1; |
| 726 | } |
| 727 | |
| 728 | #if API_ALSA |
| 729 | /** @brief ALSA backend initialization */ |
| 730 | static void alsa_init(void) { |
| 731 | info("selected ALSA backend"); |
| 732 | } |
| 733 | |
| 734 | /** @brief ALSA backend activation */ |
| 735 | static int alsa_activate(void) { |
| 736 | /* If we need to change format then close the current device. */ |
| 737 | if(pcm && !formats_equal(&playing->format, &pcm_format)) |
| 738 | idle(); |
| 739 | if(!pcm) { |
| 740 | snd_pcm_hw_params_t *hwparams; |
| 741 | snd_pcm_sw_params_t *swparams; |
| 742 | snd_pcm_uframes_t pcm_bufsize; |
| 743 | int err; |
| 744 | int sample_format = 0; |
| 745 | unsigned rate; |
| 746 | |
| 747 | D(("snd_pcm_open")); |
| 748 | if((err = snd_pcm_open(&pcm, |
| 749 | config->device, |
| 750 | SND_PCM_STREAM_PLAYBACK, |
| 751 | SND_PCM_NONBLOCK))) { |
| 752 | error(0, "error from snd_pcm_open: %d", err); |
| 753 | goto error; |
| 754 | } |
| 755 | snd_pcm_hw_params_alloca(&hwparams); |
| 756 | D(("set up hw params")); |
| 757 | if((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0) |
| 758 | fatal(0, "error from snd_pcm_hw_params_any: %d", err); |
| 759 | if((err = snd_pcm_hw_params_set_access(pcm, hwparams, |
| 760 | SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) |
| 761 | fatal(0, "error from snd_pcm_hw_params_set_access: %d", err); |
| 762 | switch(playing->format.bits) { |
| 763 | case 8: |
| 764 | sample_format = SND_PCM_FORMAT_S8; |
| 765 | break; |
| 766 | case 16: |
| 767 | switch(playing->format.byte_format) { |
| 768 | case AO_FMT_NATIVE: sample_format = SND_PCM_FORMAT_S16; break; |
| 769 | case AO_FMT_LITTLE: sample_format = SND_PCM_FORMAT_S16_LE; break; |
| 770 | case AO_FMT_BIG: sample_format = SND_PCM_FORMAT_S16_BE; break; |
| 771 | error(0, "unrecognized byte format %d", playing->format.byte_format); |
| 772 | goto fatal; |
| 773 | } |
| 774 | break; |
| 775 | default: |
| 776 | error(0, "unsupported sample size %d", playing->format.bits); |
| 777 | goto fatal; |
| 778 | } |
| 779 | if((err = snd_pcm_hw_params_set_format(pcm, hwparams, |
| 780 | sample_format)) < 0) { |
| 781 | error(0, "error from snd_pcm_hw_params_set_format (%d): %d", |
| 782 | sample_format, err); |
| 783 | goto fatal; |
| 784 | } |
| 785 | rate = playing->format.rate; |
| 786 | if((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0) { |
| 787 | error(0, "error from snd_pcm_hw_params_set_rate (%d): %d", |
| 788 | playing->format.rate, err); |
| 789 | goto fatal; |
| 790 | } |
| 791 | if(rate != (unsigned)playing->format.rate) |
| 792 | info("want rate %d, got %u", playing->format.rate, rate); |
| 793 | if((err = snd_pcm_hw_params_set_channels(pcm, hwparams, |
| 794 | playing->format.channels)) < 0) { |
| 795 | error(0, "error from snd_pcm_hw_params_set_channels (%d): %d", |
| 796 | playing->format.channels, err); |
| 797 | goto fatal; |
| 798 | } |
| 799 | bufsize = 3 * FRAMES; |
| 800 | pcm_bufsize = bufsize; |
| 801 | if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams, |
| 802 | &pcm_bufsize)) < 0) |
| 803 | fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d", |
| 804 | 3 * FRAMES, err); |
| 805 | if(pcm_bufsize != 3 * FRAMES && pcm_bufsize != last_pcm_bufsize) |
| 806 | info("asked for PCM buffer of %d frames, got %d", |
| 807 | 3 * FRAMES, (int)pcm_bufsize); |
| 808 | last_pcm_bufsize = pcm_bufsize; |
| 809 | if((err = snd_pcm_hw_params(pcm, hwparams)) < 0) |
| 810 | fatal(0, "error calling snd_pcm_hw_params: %d", err); |
| 811 | D(("set up sw params")); |
| 812 | snd_pcm_sw_params_alloca(&swparams); |
| 813 | if((err = snd_pcm_sw_params_current(pcm, swparams)) < 0) |
| 814 | fatal(0, "error calling snd_pcm_sw_params_current: %d", err); |
| 815 | if((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, FRAMES)) < 0) |
| 816 | fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d", |
| 817 | FRAMES, err); |
| 818 | if((err = snd_pcm_sw_params(pcm, swparams)) < 0) |
| 819 | fatal(0, "error calling snd_pcm_sw_params: %d", err); |
| 820 | pcm_format = playing->format; |
| 821 | bpf = bytes_per_frame(&pcm_format); |
| 822 | D(("acquired audio device")); |
| 823 | log_params(hwparams, swparams); |
| 824 | ready = 1; |
| 825 | } |
| 826 | return 0; |
| 827 | fatal: |
| 828 | abandon(); |
| 829 | error: |
| 830 | /* We assume the error is temporary and that we'll retry in a bit. */ |
| 831 | if(pcm) { |
| 832 | snd_pcm_close(pcm); |
| 833 | pcm = 0; |
| 834 | } |
| 835 | return -1; |
| 836 | } |
| 837 | |
| 838 | /** @brief Play via ALSA */ |
| 839 | static size_t alsa_play(size_t frames) { |
| 840 | snd_pcm_sframes_t pcm_written_frames; |
| 841 | int err; |
| 842 | |
| 843 | pcm_written_frames = snd_pcm_writei(pcm, |
| 844 | playing->buffer + playing->start, |
| 845 | frames); |
| 846 | D(("actually play %zu frames, wrote %d", |
| 847 | frames, (int)pcm_written_frames)); |
| 848 | if(pcm_written_frames < 0) { |
| 849 | switch(pcm_written_frames) { |
| 850 | case -EPIPE: /* underrun */ |
| 851 | error(0, "snd_pcm_writei reports underrun"); |
| 852 | if((err = snd_pcm_prepare(pcm)) < 0) |
| 853 | fatal(0, "error calling snd_pcm_prepare: %d", err); |
| 854 | return 0; |
| 855 | case -EAGAIN: |
| 856 | return 0; |
| 857 | default: |
| 858 | fatal(0, "error calling snd_pcm_writei: %d", |
| 859 | (int)pcm_written_frames); |
| 860 | } |
| 861 | } else |
| 862 | return pcm_written_frames; |
| 863 | } |
| 864 | |
| 865 | static int alsa_slots, alsa_nslots = -1; |
| 866 | |
| 867 | /** @brief Fill in poll fd array for ALSA */ |
| 868 | static void alsa_beforepoll(void) { |
| 869 | /* We send sample data to ALSA as fast as it can accept it, relying on |
| 870 | * the fact that it has a relatively small buffer to minimize pause |
| 871 | * latency. */ |
| 872 | int retry = 3, err; |
| 873 | |
| 874 | alsa_slots = fdno; |
| 875 | do { |
| 876 | retry = 0; |
| 877 | alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno); |
| 878 | if((alsa_nslots <= 0 |
| 879 | || !(fds[alsa_slots].events & POLLOUT)) |
| 880 | && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) { |
| 881 | error(0, "underrun detected after call to snd_pcm_poll_descriptors()"); |
| 882 | if((err = snd_pcm_prepare(pcm))) |
| 883 | fatal(0, "error calling snd_pcm_prepare: %d", err); |
| 884 | } else |
| 885 | break; |
| 886 | } while(retry-- > 0); |
| 887 | if(alsa_nslots >= 0) |
| 888 | fdno += alsa_nslots; |
| 889 | } |
| 890 | |
| 891 | /** @brief Process poll() results for ALSA */ |
| 892 | static int alsa_afterpoll(void) { |
| 893 | int err; |
| 894 | |
| 895 | if(alsa_slots != -1) { |
| 896 | unsigned short alsa_revents; |
| 897 | |
| 898 | if((err = snd_pcm_poll_descriptors_revents(pcm, |
| 899 | &fds[alsa_slots], |
| 900 | alsa_nslots, |
| 901 | &alsa_revents)) < 0) |
| 902 | fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err); |
| 903 | if(alsa_revents & (POLLOUT | POLLERR)) |
| 904 | play(3 * FRAMES); |
| 905 | return 0; |
| 906 | } else |
| 907 | return 1; |
| 908 | } |
| 909 | |
| 910 | /** @brief ALSA deactivation */ |
| 911 | static void alsa_deactivate(void) { |
| 912 | if(pcm) { |
| 913 | int err; |
| 914 | |
| 915 | if((err = snd_pcm_nonblock(pcm, 0)) < 0) |
| 916 | fatal(0, "error calling snd_pcm_nonblock: %d", err); |
| 917 | D(("draining pcm")); |
| 918 | snd_pcm_drain(pcm); |
| 919 | D(("closing pcm")); |
| 920 | snd_pcm_close(pcm); |
| 921 | pcm = 0; |
| 922 | forceplay = 0; |
| 923 | D(("released audio device")); |
| 924 | } |
| 925 | } |
| 926 | #endif |
| 927 | |
| 928 | /** @brief Command backend initialization */ |
| 929 | static void command_init(void) { |
| 930 | info("selected command backend"); |
| 931 | fork_cmd(); |
| 932 | } |
| 933 | |
| 934 | /** @brief Play to a subprocess */ |
| 935 | static size_t command_play(size_t frames) { |
| 936 | size_t bytes = frames * bpf; |
| 937 | int written_bytes; |
| 938 | |
| 939 | written_bytes = write(cmdfd, playing->buffer + playing->start, bytes); |
| 940 | D(("actually play %zu bytes, wrote %d", |
| 941 | bytes, written_bytes)); |
| 942 | if(written_bytes < 0) { |
| 943 | switch(errno) { |
| 944 | case EPIPE: |
| 945 | error(0, "hmm, command died; trying another"); |
| 946 | fork_cmd(); |
| 947 | return 0; |
| 948 | case EAGAIN: |
| 949 | return 0; |
| 950 | default: |
| 951 | fatal(errno, "error writing to subprocess"); |
| 952 | } |
| 953 | } else |
| 954 | return written_bytes / bpf; |
| 955 | } |
| 956 | |
| 957 | static int cmdfd_slot; |
| 958 | |
| 959 | /** @brief Update poll array for writing to subprocess */ |
| 960 | static void command_beforepoll(void) { |
| 961 | /* We send sample data to the subprocess as fast as it can accept it. |
| 962 | * This isn't ideal as pause latency can be very high as a result. */ |
| 963 | if(cmdfd >= 0) |
| 964 | cmdfd_slot = addfd(cmdfd, POLLOUT); |
| 965 | } |
| 966 | |
| 967 | /** @brief Process poll() results for subprocess play */ |
| 968 | static int command_afterpoll(void) { |
| 969 | if(cmdfd_slot != -1) { |
| 970 | if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR)) |
| 971 | play(3 * FRAMES); |
| 972 | return 0; |
| 973 | } else |
| 974 | return -1; |
| 975 | } |
| 976 | |
| 977 | /** @brief Command/network backend activation */ |
| 978 | static int generic_activate(void) { |
| 979 | if(!ready) { |
| 980 | bufsize = 3 * FRAMES; |
| 981 | bpf = bytes_per_frame(&config->sample_format); |
| 982 | D(("acquired audio device")); |
| 983 | ready = 1; |
| 984 | } |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | /** @brief Network backend initialization */ |
| 989 | static void network_init(void) { |
| 990 | struct addrinfo *res, *sres; |
| 991 | static const struct addrinfo pref = { |
| 992 | 0, |
| 993 | PF_INET, |
| 994 | SOCK_DGRAM, |
| 995 | IPPROTO_UDP, |
| 996 | 0, |
| 997 | 0, |
| 998 | 0, |
| 999 | 0 |
| 1000 | }; |
| 1001 | static const struct addrinfo prefbind = { |
| 1002 | AI_PASSIVE, |
| 1003 | PF_INET, |
| 1004 | SOCK_DGRAM, |
| 1005 | IPPROTO_UDP, |
| 1006 | 0, |
| 1007 | 0, |
| 1008 | 0, |
| 1009 | 0 |
| 1010 | }; |
| 1011 | static const int one = 1; |
| 1012 | int sndbuf, target_sndbuf = 131072; |
| 1013 | socklen_t len; |
| 1014 | char *sockname, *ssockname; |
| 1015 | |
| 1016 | res = get_address(&config->broadcast, &pref, &sockname); |
| 1017 | if(!res) exit(-1); |
| 1018 | if(config->broadcast_from.n) { |
| 1019 | sres = get_address(&config->broadcast_from, &prefbind, &ssockname); |
| 1020 | if(!sres) exit(-1); |
| 1021 | } else |
| 1022 | sres = 0; |
| 1023 | if((bfd = socket(res->ai_family, |
| 1024 | res->ai_socktype, |
| 1025 | res->ai_protocol)) < 0) |
| 1026 | fatal(errno, "error creating broadcast socket"); |
| 1027 | if(setsockopt(bfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0) |
| 1028 | fatal(errno, "error setting SO_BROADCAST on broadcast socket"); |
| 1029 | len = sizeof sndbuf; |
| 1030 | if(getsockopt(bfd, SOL_SOCKET, SO_SNDBUF, |
| 1031 | &sndbuf, &len) < 0) |
| 1032 | fatal(errno, "error getting SO_SNDBUF"); |
| 1033 | if(target_sndbuf > sndbuf) { |
| 1034 | if(setsockopt(bfd, SOL_SOCKET, SO_SNDBUF, |
| 1035 | &target_sndbuf, sizeof target_sndbuf) < 0) |
| 1036 | error(errno, "error setting SO_SNDBUF to %d", target_sndbuf); |
| 1037 | else |
| 1038 | info("changed socket send buffer size from %d to %d", |
| 1039 | sndbuf, target_sndbuf); |
| 1040 | } else |
| 1041 | info("default socket send buffer is %d", |
| 1042 | sndbuf); |
| 1043 | /* We might well want to set additional broadcast- or multicast-related |
| 1044 | * options here */ |
| 1045 | if(sres && bind(bfd, sres->ai_addr, sres->ai_addrlen) < 0) |
| 1046 | fatal(errno, "error binding broadcast socket to %s", ssockname); |
| 1047 | if(connect(bfd, res->ai_addr, res->ai_addrlen) < 0) |
| 1048 | fatal(errno, "error connecting broadcast socket to %s", sockname); |
| 1049 | /* Select an SSRC */ |
| 1050 | gcry_randomize(&rtp_id, sizeof rtp_id, GCRY_STRONG_RANDOM); |
| 1051 | info("selected network backend, sending to %s", sockname); |
| 1052 | if(config->sample_format.byte_format != AO_FMT_BIG) { |
| 1053 | info("forcing big-endian sample format"); |
| 1054 | config->sample_format.byte_format = AO_FMT_BIG; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /** @brief Play over the network */ |
| 1059 | static size_t network_play(size_t frames) { |
| 1060 | struct rtp_header header; |
| 1061 | struct iovec vec[2]; |
| 1062 | size_t bytes = frames * bpf, written_frames; |
| 1063 | int written_bytes; |
| 1064 | /* We transmit using RTP (RFC3550) and attempt to conform to the internet |
| 1065 | * AVT profile (RFC3551). */ |
| 1066 | |
| 1067 | if(idled) { |
| 1068 | /* There may have been a gap. Fix up the RTP time accordingly. */ |
| 1069 | struct timeval now; |
| 1070 | uint64_t delta; |
| 1071 | uint64_t target_rtp_time; |
| 1072 | |
| 1073 | /* Find the current time */ |
| 1074 | xgettimeofday(&now, 0); |
| 1075 | /* Find the number of microseconds elapsed since rtp_time=0 */ |
| 1076 | delta = tvsub_us(now, rtp_time_0); |
| 1077 | assert(delta <= UINT64_MAX / 88200); |
| 1078 | target_rtp_time = (delta * playing->format.rate |
| 1079 | * playing->format.channels) / 1000000; |
| 1080 | /* Overflows at ~6 years uptime with 44100Hz stereo */ |
| 1081 | |
| 1082 | /* rtp_time is the number of samples we've played. NB that we play |
| 1083 | * RTP_AHEAD_MS ahead of ourselves, so it may legitimately be ahead of |
| 1084 | * the value we deduce from time comparison. |
| 1085 | * |
| 1086 | * Suppose we have 1s track started at t=0, and another track begins to |
| 1087 | * play at t=2s. Suppose RTP_AHEAD_MS=1000 and 44100Hz stereo. In that |
| 1088 | * case we'll send 1s of audio as fast as we can, giving rtp_time=88200. |
| 1089 | * rtp_time stops at this point. |
| 1090 | * |
| 1091 | * At t=2s we'll have calculated target_rtp_time=176400. In this case we |
| 1092 | * set rtp_time=176400 and the player can correctly conclude that it |
| 1093 | * should leave 1s between the tracks. |
| 1094 | * |
| 1095 | * Suppose instead that the second track arrives at t=0.5s, and that |
| 1096 | * we've managed to transmit the whole of the first track already. We'll |
| 1097 | * have target_rtp_time=44100. |
| 1098 | * |
| 1099 | * The desired behaviour is to play the second track back to back with |
| 1100 | * first. In this case therefore we do not modify rtp_time. |
| 1101 | * |
| 1102 | * Is it ever right to reduce rtp_time? No; for that would imply |
| 1103 | * transmitting packets with overlapping timestamp ranges, which does not |
| 1104 | * make sense. |
| 1105 | */ |
| 1106 | if(target_rtp_time > rtp_time) { |
| 1107 | /* More time has elapsed than we've transmitted samples. That implies |
| 1108 | * we've been 'sending' silence. */ |
| 1109 | info("advancing rtp_time by %"PRIu64" samples", |
| 1110 | target_rtp_time - rtp_time); |
| 1111 | rtp_time = target_rtp_time; |
| 1112 | } else if(target_rtp_time < rtp_time) { |
| 1113 | const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS |
| 1114 | * config->sample_format.rate |
| 1115 | * config->sample_format.channels |
| 1116 | / 1000); |
| 1117 | |
| 1118 | if(target_rtp_time + samples_ahead < rtp_time) { |
| 1119 | info("reversing rtp_time by %"PRIu64" samples", |
| 1120 | rtp_time - target_rtp_time); |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */ |
| 1125 | header.seq = htons(rtp_seq++); |
| 1126 | header.timestamp = htonl((uint32_t)rtp_time); |
| 1127 | header.ssrc = rtp_id; |
| 1128 | header.mpt = (idled ? 0x80 : 0x00) | 10; |
| 1129 | /* 10 = L16 = 16-bit x 2 x 44100KHz. We ought to deduce this value from |
| 1130 | * the sample rate (in a library somewhere so that configuration.c can rule |
| 1131 | * out invalid rates). |
| 1132 | */ |
| 1133 | idled = 0; |
| 1134 | if(bytes > NETWORK_BYTES - sizeof header) { |
| 1135 | bytes = NETWORK_BYTES - sizeof header; |
| 1136 | /* Always send a whole number of frames */ |
| 1137 | bytes -= bytes % bpf; |
| 1138 | } |
| 1139 | /* "The RTP clock rate used for generating the RTP timestamp is independent |
| 1140 | * of the number of channels and the encoding; it equals the number of |
| 1141 | * sampling periods per second. For N-channel encodings, each sampling |
| 1142 | * period (say, 1/8000 of a second) generates N samples. (This terminology |
| 1143 | * is standard, but somewhat confusing, as the total number of samples |
| 1144 | * generated per second is then the sampling rate times the channel |
| 1145 | * count.)" |
| 1146 | */ |
| 1147 | vec[0].iov_base = (void *)&header; |
| 1148 | vec[0].iov_len = sizeof header; |
| 1149 | vec[1].iov_base = playing->buffer + playing->start; |
| 1150 | vec[1].iov_len = bytes; |
| 1151 | do { |
| 1152 | written_bytes = writev(bfd, vec, 2); |
| 1153 | } while(written_bytes < 0 && errno == EINTR); |
| 1154 | if(written_bytes < 0) { |
| 1155 | error(errno, "error transmitting audio data"); |
| 1156 | ++audio_errors; |
| 1157 | if(audio_errors == 10) |
| 1158 | fatal(0, "too many audio errors"); |
| 1159 | return 0; |
| 1160 | } else |
| 1161 | audio_errors /= 2; |
| 1162 | written_bytes -= sizeof (struct rtp_header); |
| 1163 | written_frames = written_bytes / bpf; |
| 1164 | /* Advance RTP's notion of the time */ |
| 1165 | rtp_time += written_frames * playing->format.channels; |
| 1166 | return written_frames; |
| 1167 | } |
| 1168 | |
| 1169 | static int bfd_slot; |
| 1170 | |
| 1171 | /** @brief Set up poll array for network play */ |
| 1172 | static void network_beforepoll(void) { |
| 1173 | struct timeval now; |
| 1174 | uint64_t target_us; |
| 1175 | uint64_t target_rtp_time; |
| 1176 | const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS |
| 1177 | * config->sample_format.rate |
| 1178 | * config->sample_format.channels |
| 1179 | / 1000); |
| 1180 | |
| 1181 | /* If we're starting then initialize the base time */ |
| 1182 | if(!rtp_time) |
| 1183 | xgettimeofday(&rtp_time_0, 0); |
| 1184 | /* We send audio data whenever we get RTP_AHEAD seconds or more |
| 1185 | * behind */ |
| 1186 | xgettimeofday(&now, 0); |
| 1187 | target_us = tvsub_us(now, rtp_time_0); |
| 1188 | assert(target_us <= UINT64_MAX / 88200); |
| 1189 | target_rtp_time = (target_us * config->sample_format.rate |
| 1190 | * config->sample_format.channels) |
| 1191 | / 1000000; |
| 1192 | if((int64_t)(rtp_time - target_rtp_time) < samples_ahead) |
| 1193 | bfd_slot = addfd(bfd, POLLOUT); |
| 1194 | } |
| 1195 | |
| 1196 | /** @brief Process poll() results for network play */ |
| 1197 | static int network_afterpoll(void) { |
| 1198 | if(bfd_slot != -1) { |
| 1199 | if(fds[bfd_slot].revents & (POLLOUT | POLLERR)) |
| 1200 | play(3 * FRAMES); |
| 1201 | return 0; |
| 1202 | } else |
| 1203 | return 1; |
| 1204 | } |
| 1205 | |
| 1206 | /** @brief Table of speaker backends */ |
| 1207 | static const struct speaker_backend backends[] = { |
| 1208 | #if API_ALSA |
| 1209 | { |
| 1210 | BACKEND_ALSA, |
| 1211 | 0, |
| 1212 | alsa_init, |
| 1213 | alsa_activate, |
| 1214 | alsa_play, |
| 1215 | alsa_deactivate, |
| 1216 | alsa_beforepoll, |
| 1217 | alsa_afterpoll |
| 1218 | }, |
| 1219 | #endif |
| 1220 | { |
| 1221 | BACKEND_COMMAND, |
| 1222 | FIXED_FORMAT, |
| 1223 | command_init, |
| 1224 | generic_activate, |
| 1225 | command_play, |
| 1226 | 0, /* deactivate */ |
| 1227 | command_beforepoll, |
| 1228 | command_afterpoll |
| 1229 | }, |
| 1230 | { |
| 1231 | BACKEND_NETWORK, |
| 1232 | FIXED_FORMAT, |
| 1233 | network_init, |
| 1234 | generic_activate, |
| 1235 | network_play, |
| 1236 | 0, /* deactivate */ |
| 1237 | network_beforepoll, |
| 1238 | network_afterpoll |
| 1239 | }, |
| 1240 | { -1, 0, 0, 0, 0, 0, 0, 0 } /* end of list */ |
| 1241 | }; |
| 1242 | |
| 1243 | /** @brief Main event loop |
| 1244 | * |
| 1245 | * This has grown in a rather bizarre and ad-hoc way is very sensitive to |
| 1246 | * changes... |
| 1247 | * |
| 1248 | * Firstly the loop is terminated when the parent process exits. Therefore the |
| 1249 | * speaker process has the same lifetime as the main server. This and the |
| 1250 | * reading of data from decoders is comprehensible enough. |
| 1251 | * |
| 1252 | * The playing of audio is more complicated however. |
| 1253 | * |
| 1254 | * On the first run through when a track is ready to be played, @ref ready and |
| 1255 | * @ref forceplay will both be zero. Therefore @c beforepoll is not called. |
| 1256 | * |
| 1257 | * @c afterpoll on the other hand @b is called and will return nonzero. The |
| 1258 | * result is that we call @c play(0). This will call activate(), setting |
| 1259 | * @ref ready nonzero, but otherwise has no immediate effect. |
| 1260 | * |
| 1261 | * We then deal with stdin and the decoders. |
| 1262 | * |
| 1263 | * We then reach the second place we might play some audio. @ref forceplay is |
| 1264 | * 0 so nothing happens here again. |
| 1265 | * |
| 1266 | * On the next iteration through however @ref ready is nonzero, and @ref |
| 1267 | * forceplay is 0, so we call @c beforepoll. After the @c poll() we call @c |
| 1268 | * afterpoll and actually get some audio played. |
| 1269 | * |
| 1270 | * This is surely @b far more complicated than it needs to be! |
| 1271 | * |
| 1272 | * If at any call to play(), activate() fails, or if there aren't enough bytes |
| 1273 | * in the buffer to satisfy the request, then @ref forceplay is set non-0. On |
| 1274 | * the next pass through the event loop @c beforepoll is not called. This |
| 1275 | * means that (if none of the other FDs trigger) the @c poll() call will block |
| 1276 | * for up to a second. @c afterpoll will return nonzero, since @c beforepoll |
| 1277 | * wasn't called, and consequently play() is called with @ref forceplay as its |
| 1278 | * argument. |
| 1279 | * |
| 1280 | * The effect is to attempt to restart playing audio - including the activate() |
| 1281 | * step, which may have failed at the previous attempt - at least once a second |
| 1282 | * after an error has disabled it. The delay prevents busy-waiting on whatever |
| 1283 | * condition has rendered the audio device uncooperative. |
| 1284 | */ |
| 1285 | static void mainloop(void) { |
| 1286 | struct track *t; |
| 1287 | struct speaker_message sm; |
| 1288 | int n, fd, stdin_slot, poke, timeout; |
| 1289 | |
| 1290 | while(getppid() != 1) { |
| 1291 | fdno = 0; |
| 1292 | /* Always ready for commands from the main server. */ |
| 1293 | stdin_slot = addfd(0, POLLIN); |
| 1294 | /* Try to read sample data for the currently playing track if there is |
| 1295 | * buffer space. */ |
| 1296 | if(playing && !playing->eof && playing->used < playing->size) { |
| 1297 | playing->slot = addfd(playing->fd, POLLIN); |
| 1298 | } else if(playing) |
| 1299 | playing->slot = -1; |
| 1300 | /* If forceplay is set then wait until it succeeds before waiting on the |
| 1301 | * sound device. */ |
| 1302 | #if API_ALSA |
| 1303 | alsa_slots = -1; |
| 1304 | #endif |
| 1305 | cmdfd_slot = -1; |
| 1306 | bfd_slot = -1; |
| 1307 | /* By default we will wait up to a second before thinking about current |
| 1308 | * state. */ |
| 1309 | timeout = 1000; |
| 1310 | /* We'll break the poll as soon as the underlying sound device is ready for |
| 1311 | * more data */ |
| 1312 | if(ready && !forceplay) |
| 1313 | backend->beforepoll(); |
| 1314 | /* If any other tracks don't have a full buffer, try to read sample data |
| 1315 | * from them. */ |
| 1316 | for(t = tracks; t; t = t->next) |
| 1317 | if(t != playing) { |
| 1318 | if(!t->eof && t->used < t->size) { |
| 1319 | t->slot = addfd(t->fd, POLLIN | POLLHUP); |
| 1320 | } else |
| 1321 | t->slot = -1; |
| 1322 | } |
| 1323 | /* Wait for something interesting to happen */ |
| 1324 | n = poll(fds, fdno, timeout); |
| 1325 | if(n < 0) { |
| 1326 | if(errno == EINTR) continue; |
| 1327 | fatal(errno, "error calling poll"); |
| 1328 | } |
| 1329 | /* Play some sound before doing anything else */ |
| 1330 | poke = backend->afterpoll(); |
| 1331 | if(poke) { |
| 1332 | /* Some attempt to play must have failed */ |
| 1333 | if(playing && !paused) |
| 1334 | play(forceplay); |
| 1335 | else |
| 1336 | forceplay = 0; /* just in case */ |
| 1337 | } |
| 1338 | /* Perhaps we have a command to process */ |
| 1339 | if(fds[stdin_slot].revents & POLLIN) { |
| 1340 | n = speaker_recv(0, &sm, &fd); |
| 1341 | if(n > 0) |
| 1342 | switch(sm.type) { |
| 1343 | case SM_PREPARE: |
| 1344 | D(("SM_PREPARE %s %d", sm.id, fd)); |
| 1345 | if(fd == -1) fatal(0, "got SM_PREPARE but no file descriptor"); |
| 1346 | t = findtrack(sm.id, 1); |
| 1347 | acquire(t, fd); |
| 1348 | break; |
| 1349 | case SM_PLAY: |
| 1350 | D(("SM_PLAY %s %d", sm.id, fd)); |
| 1351 | if(playing) fatal(0, "got SM_PLAY but already playing something"); |
| 1352 | t = findtrack(sm.id, 1); |
| 1353 | if(fd != -1) acquire(t, fd); |
| 1354 | playing = t; |
| 1355 | play(bufsize); |
| 1356 | report(); |
| 1357 | break; |
| 1358 | case SM_PAUSE: |
| 1359 | D(("SM_PAUSE")); |
| 1360 | paused = 1; |
| 1361 | report(); |
| 1362 | break; |
| 1363 | case SM_RESUME: |
| 1364 | D(("SM_RESUME")); |
| 1365 | if(paused) { |
| 1366 | paused = 0; |
| 1367 | if(playing) |
| 1368 | play(bufsize); |
| 1369 | } |
| 1370 | report(); |
| 1371 | break; |
| 1372 | case SM_CANCEL: |
| 1373 | D(("SM_CANCEL %s", sm.id)); |
| 1374 | t = removetrack(sm.id); |
| 1375 | if(t) { |
| 1376 | if(t == playing) { |
| 1377 | sm.type = SM_FINISHED; |
| 1378 | strcpy(sm.id, playing->id); |
| 1379 | speaker_send(1, &sm, 0); |
| 1380 | playing = 0; |
| 1381 | } |
| 1382 | destroy(t); |
| 1383 | } else |
| 1384 | error(0, "SM_CANCEL for unknown track %s", sm.id); |
| 1385 | report(); |
| 1386 | break; |
| 1387 | case SM_RELOAD: |
| 1388 | D(("SM_RELOAD")); |
| 1389 | if(config_read()) error(0, "cannot read configuration"); |
| 1390 | info("reloaded configuration"); |
| 1391 | break; |
| 1392 | default: |
| 1393 | error(0, "unknown message type %d", sm.type); |
| 1394 | } |
| 1395 | } |
| 1396 | /* Read in any buffered data */ |
| 1397 | for(t = tracks; t; t = t->next) |
| 1398 | if(t->slot != -1 && (fds[t->slot].revents & (POLLIN | POLLHUP))) |
| 1399 | fill(t); |
| 1400 | /* We might be able to play now */ |
| 1401 | if(ready && forceplay && playing && !paused) |
| 1402 | play(forceplay); |
| 1403 | /* Maybe we finished playing a track somewhere in the above */ |
| 1404 | maybe_finished(); |
| 1405 | /* If we don't need the sound device for now then close it for the benefit |
| 1406 | * of anyone else who wants it. */ |
| 1407 | if((!playing || paused) && ready) |
| 1408 | idle(); |
| 1409 | /* If we've not reported out state for a second do so now. */ |
| 1410 | if(time(0) > last_report) |
| 1411 | report(); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | int main(int argc, char **argv) { |
| 1416 | int n; |
| 1417 | |
| 1418 | set_progname(argv); |
| 1419 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 1420 | while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) { |
| 1421 | switch(n) { |
| 1422 | case 'h': help(); |
| 1423 | case 'V': version(); |
| 1424 | case 'c': configfile = optarg; break; |
| 1425 | case 'd': debugging = 1; break; |
| 1426 | case 'D': debugging = 0; break; |
| 1427 | default: fatal(0, "invalid option"); |
| 1428 | } |
| 1429 | } |
| 1430 | if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1; |
| 1431 | /* If stderr is a TTY then log there, otherwise to syslog. */ |
| 1432 | if(!isatty(2)) { |
| 1433 | openlog(progname, LOG_PID, LOG_DAEMON); |
| 1434 | log_default = &log_syslog; |
| 1435 | } |
| 1436 | if(config_read()) fatal(0, "cannot read configuration"); |
| 1437 | /* ignore SIGPIPE */ |
| 1438 | signal(SIGPIPE, SIG_IGN); |
| 1439 | /* reap kids */ |
| 1440 | signal(SIGCHLD, reap); |
| 1441 | /* set nice value */ |
| 1442 | xnice(config->nice_speaker); |
| 1443 | /* change user */ |
| 1444 | become_mortal(); |
| 1445 | /* make sure we're not root, whatever the config says */ |
| 1446 | if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root"); |
| 1447 | /* identify the backend used to play */ |
| 1448 | for(n = 0; backends[n].backend != -1; ++n) |
| 1449 | if(backends[n].backend == config->speaker_backend) |
| 1450 | break; |
| 1451 | if(backends[n].backend == -1) |
| 1452 | fatal(0, "unsupported backend %d", config->speaker_backend); |
| 1453 | backend = &backends[n]; |
| 1454 | /* backend-specific initialization */ |
| 1455 | backend->init(); |
| 1456 | mainloop(); |
| 1457 | info("stopped (parent terminated)"); |
| 1458 | exit(0); |
| 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | Local Variables: |
| 1463 | c-basic-offset:2 |
| 1464 | comment-column:40 |
| 1465 | fill-column:79 |
| 1466 | indent-tabs-mode:nil |
| 1467 | End: |
| 1468 | */ |