chiark / gitweb /
never reverse rtp_time. leave a comment explaining
[disorder] / server / speaker.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
dea8f8aa 3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
460b9539 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 */
1674096e 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 * For the <a href="http://www.alsa-project.org/">ALSA</a> API, 8- and 16- bit
29 * stereo and mono are supported, with any sample rate (within the limits that
30 * 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 * This program deliberately does not use the garbage collector even though it
43 * might be convenient to do so. This is for two reasons. Firstly some sound
44 * APIs use thread threads and we do not want to have to deal with potential
45 * interactions between threading and garbage collection. Secondly this
46 * process needs to be able to respond quickly and this is not compatible with
47 * the collector hanging the program even relatively briefly.
48 */
460b9539 49
50#include <config.h>
51#include "types.h"
52
53#include <getopt.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <locale.h>
57#include <syslog.h>
58#include <unistd.h>
59#include <errno.h>
60#include <ao/ao.h>
61#include <string.h>
62#include <assert.h>
63#include <sys/select.h>
9d5da576 64#include <sys/wait.h>
460b9539 65#include <time.h>
8023f60b 66#include <fcntl.h>
67#include <poll.h>
e83d0967
RK
68#include <sys/socket.h>
69#include <netdb.h>
70#include <gcrypt.h>
71#include <sys/uio.h>
460b9539 72
73#include "configuration.h"
74#include "syscalls.h"
75#include "log.h"
76#include "defs.h"
77#include "mem.h"
78#include "speaker.h"
79#include "user.h"
e83d0967
RK
80#include "addr.h"
81#include "timeval.h"
82#include "rtp.h"
460b9539 83
8023f60b 84#if API_ALSA
dea8f8aa 85#include <alsa/asoundlib.h>
8023f60b 86#endif
dea8f8aa 87
5330d674 88#ifdef WORDS_BIGENDIAN
89# define MACHINE_AO_FMT AO_FMT_BIG
90#else
91# define MACHINE_AO_FMT AO_FMT_LITTLE
92#endif
93
1674096e 94/** @brief How many seconds of input to buffer
95 *
96 * While any given connection has this much audio buffered, no more reads will
97 * be issued for that connection. The decoder will have to wait.
98 */
99#define BUFFER_SECONDS 5
460b9539 100
101#define FRAMES 4096 /* Frame batch size */
102
1674096e 103/** @brief Bytes to send per network packet
104 *
105 * Don't make this too big or arithmetic will start to overflow.
106 */
8d2482ec 107#define NETWORK_BYTES (1024+sizeof(struct rtp_header))
e83d0967 108
508acf7a
RK
109/** @brief Maximum RTP playahead (ms) */
110#define RTP_AHEAD_MS 1000
e83d0967 111
1674096e 112/** @brief Maximum number of FDs to poll for */
113#define NFDS 256
460b9539 114
1674096e 115/** @brief Track structure
116 *
117 * Known tracks are kept in a linked list. Usually there will be at most two
118 * of these but rearranging the queue can cause there to be more.
119 */
460b9539 120static struct track {
121 struct track *next; /* next track */
122 int fd; /* input FD */
123 char id[24]; /* ID */
124 size_t start, used; /* start + bytes used */
125 int eof; /* input is at EOF */
126 int got_format; /* got format yet? */
127 ao_sample_format format; /* sample format */
128 unsigned long long played; /* number of frames played */
129 char *buffer; /* sample buffer */
130 size_t size; /* sample buffer size */
131 int slot; /* poll array slot */
132} *tracks, *playing; /* all tracks + playing track */
133
134static time_t last_report; /* when we last reported */
135static int paused; /* pause status */
460b9539 136static ao_sample_format pcm_format; /* current format if aodev != 0 */
137static size_t bpf; /* bytes per frame */
138static struct pollfd fds[NFDS]; /* if we need more than that */
139static int fdno; /* fd number */
8023f60b 140static size_t bufsize; /* buffer size */
141#if API_ALSA
142static snd_pcm_t *pcm; /* current pcm handle */
0c207c37 143static snd_pcm_uframes_t last_pcm_bufsize; /* last seen buffer size */
8023f60b 144#endif
9d5da576 145static int ready; /* ready to send audio */
460b9539 146static int forceplay; /* frames to force play */
e83d0967
RK
147static int cmdfd = -1; /* child process input */
148static int bfd = -1; /* broadcast FD */
7aa087a7
RK
149
150/** @brief RTP timestamp
151 *
152 * This counts the number of samples played (NB not the number of frames
153 * played).
154 *
155 * The timestamp in the packet header is only 32 bits wide. With 44100Hz
156 * stereo, that only gives about half a day before wrapping, which is not
157 * particularly convenient for certain debugging purposes. Therefore the
158 * timestamp is maintained as a 64-bit integer, giving around six million years
159 * before wrapping, and truncated to 32 bits when transmitting.
160 */
161static uint64_t rtp_time;
162
163/** @brief RTP base timestamp
164 *
165 * This is the real time correspoding to an @ref rtp_time of 0. It is used
166 * to recalculate the timestamp after idle periods.
167 */
168static struct timeval rtp_time_0;
169
e83d0967
RK
170static uint16_t rtp_seq; /* frame sequence number */
171static uint32_t rtp_id; /* RTP SSRC */
172static int idled; /* set when idled */
173static int audio_errors; /* audio error counter */
460b9539 174
175static const struct option options[] = {
176 { "help", no_argument, 0, 'h' },
177 { "version", no_argument, 0, 'V' },
178 { "config", required_argument, 0, 'c' },
179 { "debug", no_argument, 0, 'd' },
180 { "no-debug", no_argument, 0, 'D' },
181 { 0, 0, 0, 0 }
182};
183
184/* Display usage message and terminate. */
185static void help(void) {
186 xprintf("Usage:\n"
187 " disorder-speaker [OPTIONS]\n"
188 "Options:\n"
189 " --help, -h Display usage message\n"
190 " --version, -V Display version number\n"
191 " --config PATH, -c PATH Set configuration file\n"
192 " --debug, -d Turn on debugging\n"
193 "\n"
194 "Speaker process for DisOrder. Not intended to be run\n"
195 "directly.\n");
196 xfclose(stdout);
197 exit(0);
198}
199
200/* Display version number and terminate. */
201static void version(void) {
202 xprintf("disorder-speaker version %s\n", disorder_version_string);
203 xfclose(stdout);
204 exit(0);
205}
206
1674096e 207/** @brief Return the number of bytes per frame in @p format */
460b9539 208static size_t bytes_per_frame(const ao_sample_format *format) {
209 return format->channels * format->bits / 8;
210}
211
1674096e 212/** @brief Find track @p id, maybe creating it if not found */
460b9539 213static struct track *findtrack(const char *id, int create) {
214 struct track *t;
215
216 D(("findtrack %s %d", id, create));
217 for(t = tracks; t && strcmp(id, t->id); t = t->next)
218 ;
219 if(!t && create) {
220 t = xmalloc(sizeof *t);
221 t->next = tracks;
222 strcpy(t->id, id);
223 t->fd = -1;
224 tracks = t;
225 /* The initial input buffer will be the sample format. */
226 t->buffer = (void *)&t->format;
227 t->size = sizeof t->format;
228 }
229 return t;
230}
231
1674096e 232/** @brief Remove track @p id (but do not destroy it) */
460b9539 233static struct track *removetrack(const char *id) {
234 struct track *t, **tt;
235
236 D(("removetrack %s", id));
237 for(tt = &tracks; (t = *tt) && strcmp(id, t->id); tt = &t->next)
238 ;
239 if(t)
240 *tt = t->next;
241 return t;
242}
243
1674096e 244/** @brief Destroy a track */
460b9539 245static void destroy(struct track *t) {
246 D(("destroy %s", t->id));
247 if(t->fd != -1) xclose(t->fd);
248 if(t->buffer != (void *)&t->format) free(t->buffer);
249 free(t);
250}
251
1674096e 252/** @brief Notice a new connection */
460b9539 253static void acquire(struct track *t, int fd) {
254 D(("acquire %s %d", t->id, fd));
255 if(t->fd != -1)
256 xclose(t->fd);
257 t->fd = fd;
258 nonblock(fd);
259}
260
1674096e 261/** @brief Return true if A and B denote identical libao formats, else false */
262static int formats_equal(const ao_sample_format *a,
263 const ao_sample_format *b) {
264 return (a->bits == b->bits
265 && a->rate == b->rate
266 && a->channels == b->channels
267 && a->byte_format == b->byte_format);
268}
269
270/** @brief Compute arguments to sox */
271static void soxargs(const char ***pp, char **qq, ao_sample_format *ao) {
272 int n;
273
274 *(*pp)++ = "-t.raw";
275 *(*pp)++ = "-s";
276 *(*pp)++ = *qq; n = sprintf(*qq, "-r%d", ao->rate); *qq += n + 1;
277 *(*pp)++ = *qq; n = sprintf(*qq, "-c%d", ao->channels); *qq += n + 1;
278 /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are
279 * deployed! */
280 switch(config->sox_generation) {
281 case 0:
282 if(ao->bits != 8
283 && ao->byte_format != AO_FMT_NATIVE
284 && ao->byte_format != MACHINE_AO_FMT) {
285 *(*pp)++ = "-x";
286 }
287 switch(ao->bits) {
288 case 8: *(*pp)++ = "-b"; break;
289 case 16: *(*pp)++ = "-w"; break;
290 case 32: *(*pp)++ = "-l"; break;
291 case 64: *(*pp)++ = "-d"; break;
292 default: fatal(0, "cannot handle sample size %d", (int)ao->bits);
293 }
294 break;
295 case 1:
296 switch(ao->byte_format) {
297 case AO_FMT_NATIVE: break;
298 case AO_FMT_BIG: *(*pp)++ = "-B"; break;
299 case AO_FMT_LITTLE: *(*pp)++ = "-L"; break;
300 }
301 *(*pp)++ = *qq; n = sprintf(*qq, "-%d", ao->bits/8); *qq += n + 1;
302 break;
303 }
304}
305
306/** @brief Enable format translation
307 *
308 * If necessary, replaces a tracks inbound file descriptor with one connected
309 * to a sox invocation, which performs the required translation.
310 */
311static void enable_translation(struct track *t) {
312 switch(config->speaker_backend) {
313 case BACKEND_COMMAND:
314 case BACKEND_NETWORK:
315 /* These backends need a specific sample format */
316 break;
317 case BACKEND_ALSA:
318 /* ALSA can cope */
319 return;
320 }
321 if(!formats_equal(&t->format, &config->sample_format)) {
322 char argbuf[1024], *q = argbuf;
323 const char *av[18], **pp = av;
324 int soxpipe[2];
325 pid_t soxkid;
326
327 *pp++ = "sox";
328 soxargs(&pp, &q, &t->format);
329 *pp++ = "-";
330 soxargs(&pp, &q, &config->sample_format);
331 *pp++ = "-";
332 *pp++ = 0;
333 if(debugging) {
334 for(pp = av; *pp; pp++)
335 D(("sox arg[%d] = %s", pp - av, *pp));
336 D(("end args"));
337 }
338 xpipe(soxpipe);
339 soxkid = xfork();
340 if(soxkid == 0) {
341 signal(SIGPIPE, SIG_DFL);
342 xdup2(t->fd, 0);
343 xdup2(soxpipe[1], 1);
344 fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK);
345 close(soxpipe[0]);
346 close(soxpipe[1]);
347 close(t->fd);
348 execvp("sox", (char **)av);
349 _exit(1);
350 }
351 D(("forking sox for format conversion (kid = %d)", soxkid));
352 close(t->fd);
353 close(soxpipe[1]);
354 t->fd = soxpipe[0];
355 t->format = config->sample_format;
356 ready = 0;
357 }
358}
359
360/** @brief Read data into a sample buffer
361 * @param t Pointer to track
362 * @return 0 on success, -1 on EOF
363 *
364 * This is effectively the read callback on @c t->fd.
365 */
460b9539 366static int fill(struct track *t) {
367 size_t where, left;
368 int n;
369
370 D(("fill %s: eof=%d used=%zu size=%zu got_format=%d",
371 t->id, t->eof, t->used, t->size, t->got_format));
372 if(t->eof) return -1;
373 if(t->used < t->size) {
374 /* there is room left in the buffer */
375 where = (t->start + t->used) % t->size;
376 if(t->got_format) {
377 /* We are reading audio data, get as much as we can */
378 if(where >= t->start) left = t->size - where;
379 else left = t->start - where;
380 } else
381 /* We are still waiting for the format, only get that */
382 left = sizeof (ao_sample_format) - t->used;
383 do {
384 n = read(t->fd, t->buffer + where, left);
385 } while(n < 0 && errno == EINTR);
386 if(n < 0) {
387 if(errno != EAGAIN) fatal(errno, "error reading sample stream");
388 return 0;
389 }
390 if(n == 0) {
391 D(("fill %s: eof detected", t->id));
392 t->eof = 1;
393 return -1;
394 }
395 t->used += n;
396 if(!t->got_format && t->used >= sizeof (ao_sample_format)) {
397 assert(t->used == sizeof (ao_sample_format));
398 /* Check that our assumptions are met. */
399 if(t->format.bits & 7)
400 fatal(0, "bits per sample not a multiple of 8");
1674096e 401 /* If the input format is unsuitable, arrange to translate it */
402 enable_translation(t);
460b9539 403 /* Make a new buffer for audio data. */
404 t->size = bytes_per_frame(&t->format) * t->format.rate * BUFFER_SECONDS;
405 t->buffer = xmalloc(t->size);
406 t->used = 0;
407 t->got_format = 1;
408 D(("got format for %s", t->id));
409 }
410 }
411 return 0;
412}
413
1674096e 414/** @brief Close the sound device */
460b9539 415static void idle(void) {
460b9539 416 D(("idle"));
8023f60b 417#if API_ALSA
e83d0967 418 if(config->speaker_backend == BACKEND_ALSA && pcm) {
8023f60b 419 int err;
420
460b9539 421 if((err = snd_pcm_nonblock(pcm, 0)) < 0)
422 fatal(0, "error calling snd_pcm_nonblock: %d", err);
423 D(("draining pcm"));
424 snd_pcm_drain(pcm);
425 D(("closing pcm"));
426 snd_pcm_close(pcm);
427 pcm = 0;
428 forceplay = 0;
429 D(("released audio device"));
430 }
8023f60b 431#endif
e83d0967 432 idled = 1;
9d5da576 433 ready = 0;
460b9539 434}
435
1674096e 436/** @brief Abandon the current track */
460b9539 437static void abandon(void) {
438 struct speaker_message sm;
439
440 D(("abandon"));
441 memset(&sm, 0, sizeof sm);
442 sm.type = SM_FINISHED;
443 strcpy(sm.id, playing->id);
444 speaker_send(1, &sm, 0);
445 removetrack(playing->id);
446 destroy(playing);
447 playing = 0;
448 forceplay = 0;
449}
450
8023f60b 451#if API_ALSA
1674096e 452/** @brief Log ALSA parameters */
1c6e6a61 453static void log_params(snd_pcm_hw_params_t *hwparams,
454 snd_pcm_sw_params_t *swparams) {
455 snd_pcm_uframes_t f;
456 unsigned u;
457
0c207c37 458 return; /* too verbose */
1c6e6a61 459 if(hwparams) {
460 /* TODO */
461 }
462 if(swparams) {
463 snd_pcm_sw_params_get_silence_size(swparams, &f);
464 info("sw silence_size=%lu", (unsigned long)f);
465 snd_pcm_sw_params_get_silence_threshold(swparams, &f);
466 info("sw silence_threshold=%lu", (unsigned long)f);
467 snd_pcm_sw_params_get_sleep_min(swparams, &u);
468 info("sw sleep_min=%lu", (unsigned long)u);
469 snd_pcm_sw_params_get_start_threshold(swparams, &f);
470 info("sw start_threshold=%lu", (unsigned long)f);
471 snd_pcm_sw_params_get_stop_threshold(swparams, &f);
472 info("sw stop_threshold=%lu", (unsigned long)f);
473 snd_pcm_sw_params_get_xfer_align(swparams, &f);
474 info("sw xfer_align=%lu", (unsigned long)f);
475 }
476}
8023f60b 477#endif
1c6e6a61 478
1674096e 479/** @brief Enable sound output
480 *
481 * Makes sure the sound device is open and has the right sample format. Return
482 * 0 on success and -1 on error.
483 */
460b9539 484static int activate(void) {
460b9539 485 /* If we don't know the format yet we cannot start. */
486 if(!playing->got_format) {
487 D((" - not got format for %s", playing->id));
488 return -1;
489 }
e83d0967
RK
490 switch(config->speaker_backend) {
491 case BACKEND_COMMAND:
492 case BACKEND_NETWORK:
9d5da576 493 if(!ready) {
494 pcm_format = config->sample_format;
8023f60b 495 bufsize = 3 * FRAMES;
9d5da576 496 bpf = bytes_per_frame(&config->sample_format);
497 D(("acquired audio device"));
498 ready = 1;
499 }
500 return 0;
e83d0967 501 case BACKEND_ALSA:
8023f60b 502#if API_ALSA
e83d0967
RK
503 /* If we need to change format then close the current device. */
504 if(pcm && !formats_equal(&playing->format, &pcm_format))
505 idle();
506 if(!pcm) {
507 snd_pcm_hw_params_t *hwparams;
508 snd_pcm_sw_params_t *swparams;
509 snd_pcm_uframes_t pcm_bufsize;
510 int err;
511 int sample_format = 0;
512 unsigned rate;
513
514 D(("snd_pcm_open"));
515 if((err = snd_pcm_open(&pcm,
516 config->device,
517 SND_PCM_STREAM_PLAYBACK,
518 SND_PCM_NONBLOCK))) {
519 error(0, "error from snd_pcm_open: %d", err);
520 goto error;
521 }
522 snd_pcm_hw_params_alloca(&hwparams);
523 D(("set up hw params"));
524 if((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
525 fatal(0, "error from snd_pcm_hw_params_any: %d", err);
526 if((err = snd_pcm_hw_params_set_access(pcm, hwparams,
527 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
528 fatal(0, "error from snd_pcm_hw_params_set_access: %d", err);
529 switch(playing->format.bits) {
530 case 8:
531 sample_format = SND_PCM_FORMAT_S8;
532 break;
533 case 16:
534 switch(playing->format.byte_format) {
535 case AO_FMT_NATIVE: sample_format = SND_PCM_FORMAT_S16; break;
536 case AO_FMT_LITTLE: sample_format = SND_PCM_FORMAT_S16_LE; break;
537 case AO_FMT_BIG: sample_format = SND_PCM_FORMAT_S16_BE; break;
538 error(0, "unrecognized byte format %d", playing->format.byte_format);
539 goto fatal;
540 }
541 break;
542 default:
543 error(0, "unsupported sample size %d", playing->format.bits);
460b9539 544 goto fatal;
545 }
e83d0967
RK
546 if((err = snd_pcm_hw_params_set_format(pcm, hwparams,
547 sample_format)) < 0) {
548 error(0, "error from snd_pcm_hw_params_set_format (%d): %d",
549 sample_format, err);
550 goto fatal;
551 }
552 rate = playing->format.rate;
553 if((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0) {
554 error(0, "error from snd_pcm_hw_params_set_rate (%d): %d",
555 playing->format.rate, err);
556 goto fatal;
557 }
558 if(rate != (unsigned)playing->format.rate)
559 info("want rate %d, got %u", playing->format.rate, rate);
560 if((err = snd_pcm_hw_params_set_channels(pcm, hwparams,
561 playing->format.channels)) < 0) {
562 error(0, "error from snd_pcm_hw_params_set_channels (%d): %d",
563 playing->format.channels, err);
564 goto fatal;
565 }
566 bufsize = 3 * FRAMES;
567 pcm_bufsize = bufsize;
568 if((err = snd_pcm_hw_params_set_buffer_size_near(pcm, hwparams,
569 &pcm_bufsize)) < 0)
570 fatal(0, "error from snd_pcm_hw_params_set_buffer_size (%d): %d",
571 3 * FRAMES, err);
572 if(pcm_bufsize != 3 * FRAMES && pcm_bufsize != last_pcm_bufsize)
573 info("asked for PCM buffer of %d frames, got %d",
574 3 * FRAMES, (int)pcm_bufsize);
575 last_pcm_bufsize = pcm_bufsize;
576 if((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
577 fatal(0, "error calling snd_pcm_hw_params: %d", err);
578 D(("set up sw params"));
579 snd_pcm_sw_params_alloca(&swparams);
580 if((err = snd_pcm_sw_params_current(pcm, swparams)) < 0)
581 fatal(0, "error calling snd_pcm_sw_params_current: %d", err);
582 if((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, FRAMES)) < 0)
583 fatal(0, "error calling snd_pcm_sw_params_set_avail_min %d: %d",
584 FRAMES, err);
585 if((err = snd_pcm_sw_params(pcm, swparams)) < 0)
586 fatal(0, "error calling snd_pcm_sw_params: %d", err);
587 pcm_format = playing->format;
588 bpf = bytes_per_frame(&pcm_format);
589 D(("acquired audio device"));
590 log_params(hwparams, swparams);
591 ready = 1;
460b9539 592 }
e83d0967
RK
593 return 0;
594 fatal:
595 abandon();
596 error:
597 /* We assume the error is temporary and that we'll retry in a bit. */
598 if(pcm) {
599 snd_pcm_close(pcm);
600 pcm = 0;
460b9539 601 }
e83d0967 602 return -1;
8023f60b 603#endif
e83d0967
RK
604 default:
605 assert(!"reached");
606 }
460b9539 607}
608
609/* Check to see whether the current track has finished playing */
610static void maybe_finished(void) {
611 if(playing
612 && playing->eof
613 && (!playing->got_format
614 || playing->used < bytes_per_frame(&playing->format)))
615 abandon();
616}
617
e83d0967
RK
618static void fork_cmd(void) {
619 pid_t cmdpid;
9d5da576 620 int pfd[2];
e83d0967 621 if(cmdfd != -1) close(cmdfd);
9d5da576 622 xpipe(pfd);
e83d0967
RK
623 cmdpid = xfork();
624 if(!cmdpid) {
1674096e 625 signal(SIGPIPE, SIG_DFL);
9d5da576 626 xdup2(pfd[0], 0);
627 close(pfd[0]);
628 close(pfd[1]);
629 execl("/bin/sh", "sh", "-c", config->speaker_command, (char *)0);
630 fatal(errno, "error execing /bin/sh");
631 }
632 close(pfd[0]);
e83d0967
RK
633 cmdfd = pfd[1];
634 D(("forked cmd %d, fd = %d", cmdpid, cmdfd));
9d5da576 635}
636
460b9539 637static void play(size_t frames) {
ceb044f4 638 size_t avail_bytes, write_bytes, written_frames;
9d5da576 639 ssize_t written_bytes;
0b75463f 640 struct rtp_header header;
e83d0967 641 struct iovec vec[2];
460b9539 642
643 if(activate()) {
644 if(playing)
645 forceplay = frames;
646 else
647 forceplay = 0; /* Must have called abandon() */
648 return;
649 }
650 D(("play: play %zu/%zu%s %dHz %db %dc", frames, playing->used / bpf,
651 playing->eof ? " EOF" : "",
652 playing->format.rate,
653 playing->format.bits,
654 playing->format.channels));
655 /* If we haven't got enough bytes yet wait until we have. Exception: when
656 * we are at eof. */
657 if(playing->used < frames * bpf && !playing->eof) {
658 forceplay = frames;
659 return;
660 }
661 /* We have got enough data so don't force play again */
662 forceplay = 0;
663 /* Figure out how many frames there are available to write */
664 if(playing->start + playing->used > playing->size)
665 avail_bytes = playing->size - playing->start;
666 else
667 avail_bytes = playing->used;
9d5da576 668
e83d0967 669 switch(config->speaker_backend) {
8023f60b 670#if API_ALSA
3a3c7bb9 671 case BACKEND_ALSA: {
8023f60b 672 snd_pcm_sframes_t pcm_written_frames;
673 size_t avail_frames;
674 int err;
675
9d5da576 676 avail_frames = avail_bytes / bpf;
677 if(avail_frames > frames)
678 avail_frames = frames;
679 if(!avail_frames)
460b9539 680 return;
8023f60b 681 pcm_written_frames = snd_pcm_writei(pcm,
682 playing->buffer + playing->start,
683 avail_frames);
9d5da576 684 D(("actually play %zu frames, wrote %d",
8023f60b 685 avail_frames, (int)pcm_written_frames));
686 if(pcm_written_frames < 0) {
687 switch(pcm_written_frames) {
9d5da576 688 case -EPIPE: /* underrun */
689 error(0, "snd_pcm_writei reports underrun");
690 if((err = snd_pcm_prepare(pcm)) < 0)
691 fatal(0, "error calling snd_pcm_prepare: %d", err);
692 return;
693 case -EAGAIN:
694 return;
695 default:
8023f60b 696 fatal(0, "error calling snd_pcm_writei: %d",
697 (int)pcm_written_frames);
9d5da576 698 }
699 }
8023f60b 700 written_frames = pcm_written_frames;
9d5da576 701 written_bytes = written_frames * bpf;
e83d0967 702 break;
3a3c7bb9 703 }
8023f60b 704#endif
e83d0967 705 case BACKEND_COMMAND:
9d5da576 706 if(avail_bytes > frames * bpf)
707 avail_bytes = frames * bpf;
e83d0967 708 written_bytes = write(cmdfd, playing->buffer + playing->start,
9d5da576 709 avail_bytes);
710 D(("actually play %zu bytes, wrote %d",
711 avail_bytes, (int)written_bytes));
712 if(written_bytes < 0) {
713 switch(errno) {
714 case EPIPE:
e83d0967
RK
715 error(0, "hmm, command died; trying another");
716 fork_cmd();
9d5da576 717 return;
718 case EAGAIN:
719 return;
720 }
460b9539 721 }
9d5da576 722 written_frames = written_bytes / bpf; /* good enough */
e83d0967
RK
723 break;
724 case BACKEND_NETWORK:
725 /* We transmit using RTP (RFC3550) and attempt to conform to the internet
726 * AVT profile (RFC3551). */
7aa087a7 727
e83d0967 728 if(idled) {
451169fc 729 /* There may have been a gap. Fix up the RTP time accordingly. */
e83d0967 730 struct timeval now;
7aa087a7
RK
731 uint64_t delta;
732 uint64_t target_rtp_time;
733
734 /* Find the current time */
e83d0967 735 xgettimeofday(&now, 0);
7aa087a7
RK
736 /* Find the number of microseconds elapsed since rtp_time=0 */
737 delta = tvsub_us(now, rtp_time_0);
738 assert(delta <= UINT64_MAX / 88200);
739 target_rtp_time = (delta * playing->format.rate
740 * playing->format.channels) / 1000000;
741 /* Overflows at ~6 years uptime with 44100Hz stereo */
451169fc
RK
742
743 /* rtp_time is the number of samples we've played. NB that we play
744 * RTP_AHEAD_MS ahead of ourselves, so it may legitimately be ahead of
745 * the value we deduce from time comparison.
746 *
747 * Suppose we have 1s track started at t=0, and another track begins to
748 * play at t=2s. Suppose RTP_AHEAD_MS=1000 and 44100Hz stereo. In that
749 * case we'll send 1s of audio as fast as we can, giving rtp_time=88200.
750 * rtp_time stops at this point.
751 *
752 * At t=2s we'll have calculated target_rtp_time=176400. In this case we
753 * set rtp_time=176400 and the player can correctly conclude that it
754 * should leave 1s between the tracks.
755 *
756 * Suppose instead that the second track arrives at t=0.5s, and that
757 * we've managed to transmit the whole of the first track already. We'll
758 * have target_rtp_time=44100.
759 *
760 * The desired behaviour is to play the second track back to back with
761 * first. In this case therefore we do not modify rtp_time.
762 *
763 * Is it ever right to reduce rtp_time? No; for that would imply
764 * transmitting packets with overlapping timestamp ranges, which does not
765 * make sense.
766 */
767 if(target_rtp_time > rtp_time) {
768 /* More time has elapsed than we've transmitted samples. That implies
769 * we've been 'sending' silence. */
7aa087a7
RK
770 info("advancing rtp_time by %"PRIu64" samples",
771 target_rtp_time - rtp_time);
451169fc
RK
772 rtp_time = target_rtp_time;
773 } else if(target_rtp_time < rtp_time) {
774 const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS
775 * config->sample_format.rate
776 * config->sample_format.channels
777 / 1000);
778
779 if(target_rtp_time + samples_ahead < rtp_time) {
780 info("reversing rtp_time by %"PRIu64" samples",
781 rtp_time - target_rtp_time);
782 }
783 }
e83d0967
RK
784 }
785 header.vpxcc = 2 << 6; /* V=2, P=0, X=0, CC=0 */
786 header.seq = htons(rtp_seq++);
7aa087a7 787 header.timestamp = htonl((uint32_t)rtp_time);
e83d0967
RK
788 header.ssrc = rtp_id;
789 header.mpt = (idled ? 0x80 : 0x00) | 10;
790 /* 10 = L16 = 16-bit x 2 x 44100KHz. We ought to deduce this value from
791 * the sample rate (in a library somewhere so that configuration.c can rule
792 * out invalid rates).
793 */
794 idled = 0;
795 if(avail_bytes > NETWORK_BYTES - sizeof header) {
796 avail_bytes = NETWORK_BYTES - sizeof header;
7aa087a7 797 /* Always send a whole number of frames */
e83d0967
RK
798 avail_bytes -= avail_bytes % bpf;
799 }
800 /* "The RTP clock rate used for generating the RTP timestamp is independent
801 * of the number of channels and the encoding; it equals the number of
802 * sampling periods per second. For N-channel encodings, each sampling
803 * period (say, 1/8000 of a second) generates N samples. (This terminology
804 * is standard, but somewhat confusing, as the total number of samples
805 * generated per second is then the sampling rate times the channel
806 * count.)"
807 */
ceb044f4 808 write_bytes = avail_bytes;
ceb044f4
RK
809 if(write_bytes) {
810 vec[0].iov_base = (void *)&header;
811 vec[0].iov_len = sizeof header;
812 vec[1].iov_base = playing->buffer + playing->start;
813 vec[1].iov_len = avail_bytes;
ceb044f4
RK
814 do {
815 written_bytes = writev(bfd,
816 vec,
817 2);
818 } while(written_bytes < 0 && errno == EINTR);
819 if(written_bytes < 0) {
820 error(errno, "error transmitting audio data");
821 ++audio_errors;
822 if(audio_errors == 10)
823 fatal(0, "too many audio errors");
e83d0967 824 return;
ceb044f4
RK
825 }
826 } else
e83d0967
RK
827 audio_errors /= 2;
828 written_bytes = avail_bytes;
829 written_frames = written_bytes / bpf;
830 /* Advance RTP's notion of the time */
831 rtp_time += written_frames * playing->format.channels;
e83d0967
RK
832 break;
833 default:
834 assert(!"reached");
460b9539 835 }
e83d0967
RK
836 /* written_bytes and written_frames had better both be set and correct by
837 * this point */
460b9539 838 playing->start += written_bytes;
839 playing->used -= written_bytes;
840 playing->played += written_frames;
841 /* If the pointer is at the end of the buffer (or the buffer is completely
842 * empty) wrap it back to the start. */
843 if(!playing->used || playing->start == playing->size)
844 playing->start = 0;
845 frames -= written_frames;
846}
847
848/* Notify the server what we're up to. */
849static void report(void) {
850 struct speaker_message sm;
851
852 if(playing && playing->buffer != (void *)&playing->format) {
853 memset(&sm, 0, sizeof sm);
854 sm.type = paused ? SM_PAUSED : SM_PLAYING;
855 strcpy(sm.id, playing->id);
856 sm.data = playing->played / playing->format.rate;
857 speaker_send(1, &sm, 0);
858 }
859 time(&last_report);
860}
861
9d5da576 862static void reap(int __attribute__((unused)) sig) {
e83d0967 863 pid_t cmdpid;
9d5da576 864 int st;
865
866 do
e83d0967
RK
867 cmdpid = waitpid(-1, &st, WNOHANG);
868 while(cmdpid > 0);
9d5da576 869 signal(SIGCHLD, reap);
870}
871
460b9539 872static int addfd(int fd, int events) {
873 if(fdno < NFDS) {
874 fds[fdno].fd = fd;
875 fds[fdno].events = events;
876 return fdno++;
877 } else
878 return -1;
879}
880
881int main(int argc, char **argv) {
e83d0967 882 int n, fd, stdin_slot, alsa_slots, cmdfd_slot, bfd_slot, poke, timeout;
460b9539 883 struct track *t;
884 struct speaker_message sm;
e83d0967
RK
885 struct addrinfo *res, *sres;
886 static const struct addrinfo pref = {
887 0,
888 PF_INET,
889 SOCK_DGRAM,
890 IPPROTO_UDP,
891 0,
892 0,
893 0,
894 0
895 };
896 static const struct addrinfo prefbind = {
897 AI_PASSIVE,
898 PF_INET,
899 SOCK_DGRAM,
900 IPPROTO_UDP,
901 0,
902 0,
903 0,
904 0
905 };
906 static const int one = 1;
24d0936b
RK
907 int sndbuf, target_sndbuf = 131072;
908 socklen_t len;
e83d0967 909 char *sockname, *ssockname;
8023f60b 910#if API_ALSA
911 int alsa_nslots = -1, err;
912#endif
460b9539 913
914 set_progname(argv);
460b9539 915 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
916 while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) {
917 switch(n) {
918 case 'h': help();
919 case 'V': version();
920 case 'c': configfile = optarg; break;
921 case 'd': debugging = 1; break;
922 case 'D': debugging = 0; break;
923 default: fatal(0, "invalid option");
924 }
925 }
926 if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1;
927 /* If stderr is a TTY then log there, otherwise to syslog. */
928 if(!isatty(2)) {
929 openlog(progname, LOG_PID, LOG_DAEMON);
930 log_default = &log_syslog;
931 }
932 if(config_read()) fatal(0, "cannot read configuration");
933 /* ignore SIGPIPE */
934 signal(SIGPIPE, SIG_IGN);
9d5da576 935 /* reap kids */
936 signal(SIGCHLD, reap);
460b9539 937 /* set nice value */
938 xnice(config->nice_speaker);
939 /* change user */
940 become_mortal();
941 /* make sure we're not root, whatever the config says */
942 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
e83d0967
RK
943 switch(config->speaker_backend) {
944 case BACKEND_ALSA:
945 info("selected ALSA backend");
946 case BACKEND_COMMAND:
947 info("selected command backend");
948 fork_cmd();
949 break;
950 case BACKEND_NETWORK:
951 res = get_address(&config->broadcast, &pref, &sockname);
952 if(!res) return -1;
953 if(config->broadcast_from.n) {
954 sres = get_address(&config->broadcast_from, &prefbind, &ssockname);
955 if(!sres) return -1;
956 } else
957 sres = 0;
958 if((bfd = socket(res->ai_family,
959 res->ai_socktype,
960 res->ai_protocol)) < 0)
961 fatal(errno, "error creating broadcast socket");
962 if(setsockopt(bfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0)
24d0936b
RK
963 fatal(errno, "error setting SO_BROADCAST on broadcast socket");
964 len = sizeof sndbuf;
965 if(getsockopt(bfd, SOL_SOCKET, SO_SNDBUF,
966 &sndbuf, &len) < 0)
967 fatal(errno, "error getting SO_SNDBUF");
968 if(setsockopt(bfd, SOL_SOCKET, SO_SNDBUF,
969 &target_sndbuf, sizeof target_sndbuf) < 0)
970 error(errno, "error setting SO_SNDBUF to %d", target_sndbuf);
971 else
972 info("changed socket send buffer size from %d to %d",
973 sndbuf, target_sndbuf);
e83d0967
RK
974 /* We might well want to set additional broadcast- or multicast-related
975 * options here */
976 if(sres && bind(bfd, sres->ai_addr, sres->ai_addrlen) < 0)
977 fatal(errno, "error binding broadcast socket to %s", ssockname);
978 if(connect(bfd, res->ai_addr, res->ai_addrlen) < 0)
979 fatal(errno, "error connecting broadcast socket to %s", sockname);
980 /* Select an SSRC */
981 gcry_randomize(&rtp_id, sizeof rtp_id, GCRY_STRONG_RANDOM);
982 info("selected network backend, sending to %s", sockname);
983 if(config->sample_format.byte_format != AO_FMT_BIG) {
984 info("forcing big-endian sample format");
985 config->sample_format.byte_format = AO_FMT_BIG;
986 }
987 break;
988 default:
989 fatal(0, "unknown backend %d", config->speaker_backend);
8023f60b 990 }
460b9539 991 while(getppid() != 1) {
992 fdno = 0;
993 /* Always ready for commands from the main server. */
994 stdin_slot = addfd(0, POLLIN);
995 /* Try to read sample data for the currently playing track if there is
996 * buffer space. */
997 if(playing && !playing->eof && playing->used < playing->size) {
998 playing->slot = addfd(playing->fd, POLLIN);
999 } else if(playing)
1000 playing->slot = -1;
1001 /* If forceplay is set then wait until it succeeds before waiting on the
1002 * sound device. */
9d5da576 1003 alsa_slots = -1;
e83d0967
RK
1004 cmdfd_slot = -1;
1005 bfd_slot = -1;
1006 /* By default we will wait up to a second before thinking about current
1007 * state. */
1008 timeout = 1000;
8023f60b 1009 if(ready && !forceplay) {
e83d0967
RK
1010 switch(config->speaker_backend) {
1011 case BACKEND_COMMAND:
1012 /* We send sample data to the subprocess as fast as it can accept it.
1013 * This isn't ideal as pause latency can be very high as a result. */
1014 if(cmdfd >= 0)
1015 cmdfd_slot = addfd(cmdfd, POLLOUT);
1016 break;
7aa087a7
RK
1017 case BACKEND_NETWORK: {
1018 struct timeval now;
1019 uint64_t target_us;
1020 uint64_t target_rtp_time;
508acf7a
RK
1021 const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS
1022 * config->sample_format.rate
1023 * config->sample_format.channels
1024 / 1000);
ae5b28b9 1025#if 0
7aa087a7 1026 static unsigned logit;
ae5b28b9 1027#endif
7aa087a7
RK
1028
1029 /* If we're starting then initialize the base time */
1030 if(!rtp_time)
1031 xgettimeofday(&rtp_time_0, 0);
1032 /* We send audio data whenever we get RTP_AHEAD seconds or more
1033 * behind */
e83d0967 1034 xgettimeofday(&now, 0);
7aa087a7
RK
1035 target_us = tvsub_us(now, rtp_time_0);
1036 assert(target_us <= UINT64_MAX / 88200);
1037 target_rtp_time = (target_us * config->sample_format.rate
1038 * config->sample_format.channels)
1039
1040 / 1000000;
ae5b28b9 1041#if 0
7aa087a7
RK
1042 /* TODO remove logging guff */
1043 if(!(logit++ & 1023))
1044 info("rtp_time %llu target %llu difference %lld [%lld]",
1045 rtp_time, target_rtp_time,
1046 rtp_time - target_rtp_time,
189e9830
RK
1047 samples_ahead);
1048#endif
508acf7a 1049 if((int64_t)(rtp_time - target_rtp_time) < samples_ahead)
e83d0967 1050 bfd_slot = addfd(bfd, POLLOUT);
e83d0967 1051 break;
7aa087a7 1052 }
8023f60b 1053#if API_ALSA
3a3c7bb9 1054 case BACKEND_ALSA: {
e83d0967
RK
1055 /* We send sample data to ALSA as fast as it can accept it, relying on
1056 * the fact that it has a relatively small buffer to minimize pause
1057 * latency. */
9d5da576 1058 int retry = 3;
1059
1060 alsa_slots = fdno;
1061 do {
1062 retry = 0;
1063 alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno);
1064 if((alsa_nslots <= 0
1065 || !(fds[alsa_slots].events & POLLOUT))
1066 && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) {
1067 error(0, "underrun detected after call to snd_pcm_poll_descriptors()");
1068 if((err = snd_pcm_prepare(pcm)))
1069 fatal(0, "error calling snd_pcm_prepare: %d", err);
1070 } else
1071 break;
1072 } while(retry-- > 0);
1073 if(alsa_nslots >= 0)
1074 fdno += alsa_nslots;
e83d0967 1075 break;
3a3c7bb9 1076 }
8023f60b 1077#endif
e83d0967
RK
1078 default:
1079 assert(!"unknown backend");
9d5da576 1080 }
1081 }
460b9539 1082 /* If any other tracks don't have a full buffer, try to read sample data
1083 * from them. */
1084 for(t = tracks; t; t = t->next)
1085 if(t != playing) {
1086 if(!t->eof && t->used < t->size) {
9d5da576 1087 t->slot = addfd(t->fd, POLLIN | POLLHUP);
460b9539 1088 } else
1089 t->slot = -1;
1090 }
e83d0967
RK
1091 /* Wait for something interesting to happen */
1092 n = poll(fds, fdno, timeout);
460b9539 1093 if(n < 0) {
1094 if(errno == EINTR) continue;
1095 fatal(errno, "error calling poll");
1096 }
1097 /* Play some sound before doing anything else */
e83d0967
RK
1098 poke = 0;
1099 switch(config->speaker_backend) {
8023f60b 1100#if API_ALSA
e83d0967
RK
1101 case BACKEND_ALSA:
1102 if(alsa_slots != -1) {
1103 unsigned short alsa_revents;
1104
1105 if((err = snd_pcm_poll_descriptors_revents(pcm,
1106 &fds[alsa_slots],
1107 alsa_nslots,
1108 &alsa_revents)) < 0)
1109 fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err);
1110 if(alsa_revents & (POLLOUT | POLLERR))
1111 play(3 * FRAMES);
1112 } else
1113 poke = 1;
1114 break;
8023f60b 1115#endif
e83d0967
RK
1116 case BACKEND_COMMAND:
1117 if(cmdfd_slot != -1) {
1118 if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR))
1119 play(3 * FRAMES);
1120 } else
1121 poke = 1;
1122 break;
1123 case BACKEND_NETWORK:
1124 if(bfd_slot != -1) {
1125 if(fds[bfd_slot].revents & (POLLOUT | POLLERR))
1126 play(3 * FRAMES);
1127 } else
1128 poke = 1;
1129 break;
1130 }
1131 if(poke) {
460b9539 1132 /* Some attempt to play must have failed */
1133 if(playing && !paused)
1134 play(forceplay);
1135 else
1136 forceplay = 0; /* just in case */
1137 }
1138 /* Perhaps we have a command to process */
1139 if(fds[stdin_slot].revents & POLLIN) {
1140 n = speaker_recv(0, &sm, &fd);
1141 if(n > 0)
1142 switch(sm.type) {
1143 case SM_PREPARE:
1144 D(("SM_PREPARE %s %d", sm.id, fd));
1145 if(fd == -1) fatal(0, "got SM_PREPARE but no file descriptor");
1146 t = findtrack(sm.id, 1);
1147 acquire(t, fd);
1148 break;
1149 case SM_PLAY:
1150 D(("SM_PLAY %s %d", sm.id, fd));
1151 if(playing) fatal(0, "got SM_PLAY but already playing something");
1152 t = findtrack(sm.id, 1);
1153 if(fd != -1) acquire(t, fd);
1154 playing = t;
8023f60b 1155 play(bufsize);
460b9539 1156 report();
1157 break;
1158 case SM_PAUSE:
1159 D(("SM_PAUSE"));
1160 paused = 1;
1161 report();
1162 break;
1163 case SM_RESUME:
1164 D(("SM_RESUME"));
1165 if(paused) {
1166 paused = 0;
1167 if(playing)
8023f60b 1168 play(bufsize);
460b9539 1169 }
1170 report();
1171 break;
1172 case SM_CANCEL:
1173 D(("SM_CANCEL %s", sm.id));
1174 t = removetrack(sm.id);
1175 if(t) {
1176 if(t == playing) {
1177 sm.type = SM_FINISHED;
1178 strcpy(sm.id, playing->id);
1179 speaker_send(1, &sm, 0);
1180 playing = 0;
1181 }
1182 destroy(t);
1183 } else
1184 error(0, "SM_CANCEL for unknown track %s", sm.id);
1185 report();
1186 break;
1187 case SM_RELOAD:
1188 D(("SM_RELOAD"));
1189 if(config_read()) error(0, "cannot read configuration");
1190 info("reloaded configuration");
1191 break;
1192 default:
1193 error(0, "unknown message type %d", sm.type);
1194 }
1195 }
1196 /* Read in any buffered data */
1197 for(t = tracks; t; t = t->next)
9d5da576 1198 if(t->slot != -1 && (fds[t->slot].revents & (POLLIN | POLLHUP)))
460b9539 1199 fill(t);
1200 /* We might be able to play now */
9d5da576 1201 if(ready && forceplay && playing && !paused)
460b9539 1202 play(forceplay);
1203 /* Maybe we finished playing a track somewhere in the above */
1204 maybe_finished();
1205 /* If we don't need the sound device for now then close it for the benefit
1206 * of anyone else who wants it. */
9d5da576 1207 if((!playing || paused) && ready)
460b9539 1208 idle();
1209 /* If we've not reported out state for a second do so now. */
1210 if(time(0) > last_report)
1211 report();
1212 }
1213 info("stopped (parent terminated)");
1214 exit(0);
1215}
1216
1217/*
1218Local Variables:
1219c-basic-offset:2
1220comment-column:40
1221fill-column:79
1222indent-tabs-mode:nil
1223End:
1224*/