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