2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file server/speaker.c
21 * @brief Speaker process
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.
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.)
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.
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.
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
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
70 #include <sys/select.h>
76 #include "configuration.h"
81 #include "speaker-protocol.h"
85 /** @brief Linked list of all prepared tracks */
88 /** @brief Playing track, or NULL */
89 struct track
*playing
;
91 /** @brief Number of bytes pre frame */
94 /** @brief Array of file descriptors for poll() */
95 struct pollfd fds
[NFDS
];
97 /** @brief Next free slot in @ref fds */
100 static time_t last_report
; /* when we last reported */
101 static int paused
; /* pause status */
103 /** @brief The current device state */
104 enum device_states device_state
;
106 /** @brief The current device sample format
108 * Only meaningful if @ref device_state = @ref device_open or perhaps @ref
109 * device_error. For @ref FIXED_FORMAT backends, this should always match @c
110 * config->sample_format.
112 ao_sample_format device_format
;
114 /** @brief Set when idled
116 * This is set when the sound device is deliberately closed by idle().
120 /** @brief Selected backend */
121 static const struct speaker_backend
*backend
;
123 static const struct option options
[] = {
124 { "help", no_argument
, 0, 'h' },
125 { "version", no_argument
, 0, 'V' },
126 { "config", required_argument
, 0, 'c' },
127 { "debug", no_argument
, 0, 'd' },
128 { "no-debug", no_argument
, 0, 'D' },
132 /* Display usage message and terminate. */
133 static void help(void) {
135 " disorder-speaker [OPTIONS]\n"
137 " --help, -h Display usage message\n"
138 " --version, -V Display version number\n"
139 " --config PATH, -c PATH Set configuration file\n"
140 " --debug, -d Turn on debugging\n"
142 "Speaker process for DisOrder. Not intended to be run\n"
148 /* Display version number and terminate. */
149 static void version(void) {
150 xprintf("disorder-speaker version %s\n", disorder_version_string
);
155 /** @brief Return the number of bytes per frame in @p format */
156 static size_t bytes_per_frame(const ao_sample_format
*format
) {
157 return format
->channels
* format
->bits
/ 8;
160 /** @brief Find track @p id, maybe creating it if not found */
161 static struct track
*findtrack(const char *id
, int create
) {
164 D(("findtrack %s %d", id
, create
));
165 for(t
= tracks
; t
&& strcmp(id
, t
->id
); t
= t
->next
)
168 t
= xmalloc(sizeof *t
);
173 /* The initial input buffer will be the sample format. */
174 t
->buffer
= (void *)&t
->format
;
175 t
->size
= sizeof t
->format
;
180 /** @brief Remove track @p id (but do not destroy it) */
181 static struct track
*removetrack(const char *id
) {
182 struct track
*t
, **tt
;
184 D(("removetrack %s", id
));
185 for(tt
= &tracks
; (t
= *tt
) && strcmp(id
, t
->id
); tt
= &t
->next
)
192 /** @brief Destroy a track */
193 static void destroy(struct track
*t
) {
194 D(("destroy %s", t
->id
));
195 if(t
->fd
!= -1) xclose(t
->fd
);
196 if(t
->buffer
!= (void *)&t
->format
) free(t
->buffer
);
200 /** @brief Notice a new connection */
201 static void acquire(struct track
*t
, int fd
) {
202 D(("acquire %s %d", t
->id
, fd
));
209 /** @brief Return true if A and B denote identical libao formats, else false */
210 int formats_equal(const ao_sample_format
*a
,
211 const ao_sample_format
*b
) {
212 return (a
->bits
== b
->bits
213 && a
->rate
== b
->rate
214 && a
->channels
== b
->channels
215 && a
->byte_format
== b
->byte_format
);
218 /** @brief Compute arguments to sox */
219 static void soxargs(const char ***pp
, char **qq
, ao_sample_format
*ao
) {
224 *(*pp
)++ = *qq
; n
= sprintf(*qq
, "-r%d", ao
->rate
); *qq
+= n
+ 1;
225 *(*pp
)++ = *qq
; n
= sprintf(*qq
, "-c%d", ao
->channels
); *qq
+= n
+ 1;
226 /* sox 12.17.9 insists on -b etc; CVS sox insists on -<n> etc; both are
228 switch(config
->sox_generation
) {
231 && ao
->byte_format
!= AO_FMT_NATIVE
232 && ao
->byte_format
!= MACHINE_AO_FMT
) {
236 case 8: *(*pp
)++ = "-b"; break;
237 case 16: *(*pp
)++ = "-w"; break;
238 case 32: *(*pp
)++ = "-l"; break;
239 case 64: *(*pp
)++ = "-d"; break;
240 default: fatal(0, "cannot handle sample size %d", (int)ao
->bits
);
244 switch(ao
->byte_format
) {
245 case AO_FMT_NATIVE
: break;
246 case AO_FMT_BIG
: *(*pp
)++ = "-B"; break;
247 case AO_FMT_LITTLE
: *(*pp
)++ = "-L"; break;
249 *(*pp
)++ = *qq
; n
= sprintf(*qq
, "-%d", ao
->bits
/8); *qq
+= n
+ 1;
254 /** @brief Enable format translation
256 * If necessary, replaces a tracks inbound file descriptor with one connected
257 * to a sox invocation, which performs the required translation.
259 static void enable_translation(struct track
*t
) {
260 if((backend
->flags
& FIXED_FORMAT
)
261 && !formats_equal(&t
->format
, &config
->sample_format
)) {
262 char argbuf
[1024], *q
= argbuf
;
263 const char *av
[18], **pp
= av
;
268 soxargs(&pp
, &q
, &t
->format
);
270 soxargs(&pp
, &q
, &config
->sample_format
);
274 for(pp
= av
; *pp
; pp
++)
275 D(("sox arg[%d] = %s", pp
- av
, *pp
));
281 signal(SIGPIPE
, SIG_DFL
);
283 xdup2(soxpipe
[1], 1);
284 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) & ~O_NONBLOCK
);
288 execvp("sox", (char **)av
);
291 D(("forking sox for format conversion (kid = %d)", soxkid
));
295 t
->format
= config
->sample_format
;
299 /** @brief Read data into a sample buffer
300 * @param t Pointer to track
301 * @return 0 on success, -1 on EOF
303 * This is effectively the read callback on @c t->fd. It is called from the
304 * main loop whenever the track's file descriptor is readable, assuming the
305 * buffer has not reached the maximum allowed occupancy.
307 static int fill(struct track
*t
) {
311 D(("fill %s: eof=%d used=%zu size=%zu got_format=%d",
312 t
->id
, t
->eof
, t
->used
, t
->size
, t
->got_format
));
313 if(t
->eof
) return -1;
314 if(t
->used
< t
->size
) {
315 /* there is room left in the buffer */
316 where
= (t
->start
+ t
->used
) % t
->size
;
318 /* We are reading audio data, get as much as we can */
319 if(where
>= t
->start
) left
= t
->size
- where
;
320 else left
= t
->start
- where
;
322 /* We are still waiting for the format, only get that */
323 left
= sizeof (ao_sample_format
) - t
->used
;
325 n
= read(t
->fd
, t
->buffer
+ where
, left
);
326 } while(n
< 0 && errno
== EINTR
);
328 if(errno
!= EAGAIN
) fatal(errno
, "error reading sample stream");
332 D(("fill %s: eof detected", t
->id
));
337 if(!t
->got_format
&& t
->used
>= sizeof (ao_sample_format
)) {
338 assert(t
->used
== sizeof (ao_sample_format
));
339 /* Check that our assumptions are met. */
340 if(t
->format
.bits
& 7)
341 fatal(0, "bits per sample not a multiple of 8");
342 /* If the input format is unsuitable, arrange to translate it */
343 enable_translation(t
);
344 /* Make a new buffer for audio data. */
345 t
->size
= bytes_per_frame(&t
->format
) * t
->format
.rate
* BUFFER_SECONDS
;
346 t
->buffer
= xmalloc(t
->size
);
349 D(("got format for %s", t
->id
));
355 /** @brief Close the sound device
357 * This is called to deactivate the output device when pausing, and also by the
358 * ALSA backend when changing encoding (in which case the sound device will be
359 * immediately reactivated).
361 static void idle(void) {
363 if(backend
->deactivate
)
364 backend
->deactivate();
366 device_state
= device_closed
;
370 /** @brief Abandon the current track */
372 struct speaker_message sm
;
375 memset(&sm
, 0, sizeof sm
);
376 sm
.type
= SM_FINISHED
;
377 strcpy(sm
.id
, playing
->id
);
378 speaker_send(1, &sm
, 0);
379 removetrack(playing
->id
);
384 /** @brief Enable sound output
386 * Makes sure the sound device is open and has the right sample format. Return
387 * 0 on success and -1 on error.
389 static void activate(void) {
390 /* If we don't know the format yet we cannot start. */
391 if(!playing
->got_format
) {
392 D((" - not got format for %s", playing
->id
));
395 if(backend
->flags
& FIXED_FORMAT
)
396 device_format
= config
->sample_format
;
397 if(backend
->activate
) {
400 assert(backend
->flags
& FIXED_FORMAT
);
401 /* ...otherwise device_format not set */
402 device_state
= device_open
;
404 if(device_state
== device_open
)
405 device_bpf
= bytes_per_frame(&device_format
);
408 /** @brief Check whether the current track has finished
410 * The current track is determined to have finished either if the input stream
411 * eded before the format could be determined (i.e. it is malformed) or the
412 * input is at end of file and there is less than a frame left unplayed. (So
413 * it copes with decoders that crash mid-frame.)
415 static void maybe_finished(void) {
418 && (!playing
->got_format
419 || playing
->used
< bytes_per_frame(&playing
->format
)))
423 /** @brief Play up to @p frames frames of audio
425 * It is always safe to call this function.
426 * - If @ref playing is 0 then it will just return
427 * - If @ref paused is non-0 then it will just return
428 * - If @ref device_state != @ref device_open then it will call activate() and
429 * return if it it fails.
430 * - If there is not enough audio to play then it play what is available.
432 * If there are not enough frames to play then whatever is available is played
433 * instead. It is up to mainloop() to ensure that play() is not called when
434 * unreasonably only an small amounts of data is available to play.
436 static void play(size_t frames
) {
437 size_t avail_frames
, avail_bytes
, written_frames
;
438 ssize_t written_bytes
;
440 /* Make sure there's a track to play and it is not pasued */
441 if(!playing
|| paused
)
443 /* Make sure the output device is open and has the right sample format */
444 if(device_state
!= device_open
445 || !formats_equal(&device_format
, &playing
->format
)) {
447 if(device_state
!= device_open
)
450 D(("play: play %zu/%zu%s %dHz %db %dc", frames
, playing
->used
/ device_bpf
,
451 playing
->eof ?
" EOF" : "",
452 playing
->format
.rate
,
453 playing
->format
.bits
,
454 playing
->format
.channels
));
455 /* Figure out how many frames there are available to write */
456 if(playing
->start
+ playing
->used
> playing
->size
)
457 /* The ring buffer is currently wrapped, only play up to the wrap point */
458 avail_bytes
= playing
->size
- playing
->start
;
460 /* The ring buffer is not wrapped, can play the lot */
461 avail_bytes
= playing
->used
;
462 avail_frames
= avail_bytes
/ device_bpf
;
463 /* Only play up to the requested amount */
464 if(avail_frames
> frames
)
465 avail_frames
= frames
;
469 written_frames
= backend
->play(avail_frames
);
470 written_bytes
= written_frames
* device_bpf
;
471 /* written_bytes and written_frames had better both be set and correct by
473 playing
->start
+= written_bytes
;
474 playing
->used
-= written_bytes
;
475 playing
->played
+= written_frames
;
476 /* If the pointer is at the end of the buffer (or the buffer is completely
477 * empty) wrap it back to the start. */
478 if(!playing
->used
|| playing
->start
== playing
->size
)
480 frames
-= written_frames
;
484 /* Notify the server what we're up to. */
485 static void report(void) {
486 struct speaker_message sm
;
488 if(playing
&& playing
->buffer
!= (void *)&playing
->format
) {
489 memset(&sm
, 0, sizeof sm
);
490 sm
.type
= paused ? SM_PAUSED
: SM_PLAYING
;
491 strcpy(sm
.id
, playing
->id
);
492 sm
.data
= playing
->played
/ playing
->format
.rate
;
493 speaker_send(1, &sm
, 0);
498 static void reap(int __attribute__((unused
)) sig
) {
503 cmdpid
= waitpid(-1, &st
, WNOHANG
);
505 signal(SIGCHLD
, reap
);
508 int addfd(int fd
, int events
) {
511 fds
[fdno
].events
= events
;
517 /** @brief Table of speaker backends */
518 static const struct speaker_backend
*backends
[] = {
527 /** @brief Return nonzero if we want to play some audio
529 * We want to play audio if there is a current track; and it is not paused; and
530 * there are at least @ref FRAMES frames of audio to play, or we are in sight
531 * of the end of the current track.
533 static int playable(void) {
536 && (playing
->used
>= FRAMES
|| playing
->eof
);
539 /** @brief Main event loop */
540 static void mainloop(void) {
542 struct speaker_message sm
;
543 int n
, fd
, stdin_slot
, timeout
;
545 while(getppid() != 1) {
547 /* By default we will wait up to a second before thinking about current
550 /* Always ready for commands from the main server. */
551 stdin_slot
= addfd(0, POLLIN
);
552 /* Try to read sample data for the currently playing track if there is
554 if(playing
&& !playing
->eof
&& playing
->used
< playing
->size
)
555 playing
->slot
= addfd(playing
->fd
, POLLIN
);
559 /* We want to play some audio. If the device is closed then we attempt
561 if(device_state
== device_closed
)
563 /* If the device is (now) open then we will wait up until it is ready for
564 * more. If something went wrong then we should have device_error
565 * instead, but the post-poll code will cope even if it's
567 if(device_state
== device_open
)
568 backend
->beforepoll();
570 /* If any other tracks don't have a full buffer, try to read sample data
571 * from them. We do this last of all, so that if we run out of slots,
572 * nothing important can't be monitored. */
573 for(t
= tracks
; t
; t
= t
->next
)
575 if(!t
->eof
&& t
->used
< t
->size
) {
576 t
->slot
= addfd(t
->fd
, POLLIN
| POLLHUP
);
580 /* Wait for something interesting to happen */
581 n
= poll(fds
, fdno
, timeout
);
583 if(errno
== EINTR
) continue;
584 fatal(errno
, "error calling poll");
586 /* Play some sound before doing anything else */
588 /* We want to play some audio */
589 if(device_state
== device_open
) {
593 /* We must be in _closed or _error, and it should be the latter, but we
596 * We most likely timed out, so now is a good time to retry. play()
597 * knows to re-activate the device if necessary.
602 /* Perhaps we have a command to process */
603 if(fds
[stdin_slot
].revents
& POLLIN
) {
604 /* There might (in theory) be several commands queued up, but in general
605 * this won't be the case, so we don't bother looping around to pick them
607 n
= speaker_recv(0, &sm
, &fd
);
611 D(("SM_PREPARE %s %d", sm
.id
, fd
));
612 if(fd
== -1) fatal(0, "got SM_PREPARE but no file descriptor");
613 t
= findtrack(sm
.id
, 1);
617 D(("SM_PLAY %s %d", sm
.id
, fd
));
618 if(playing
) fatal(0, "got SM_PLAY but already playing something");
619 t
= findtrack(sm
.id
, 1);
620 if(fd
!= -1) acquire(t
, fd
);
622 /* We attempt to play straight away rather than going round the loop.
623 * play() is clever enough to perform any activation that is
637 /* As for SM_PLAY we attempt to play straight away. */
644 D(("SM_CANCEL %s", sm
.id
));
645 t
= removetrack(sm
.id
);
648 sm
.type
= SM_FINISHED
;
649 strcpy(sm
.id
, playing
->id
);
650 speaker_send(1, &sm
, 0);
655 error(0, "SM_CANCEL for unknown track %s", sm
.id
);
660 if(config_read()) error(0, "cannot read configuration");
661 info("reloaded configuration");
664 error(0, "unknown message type %d", sm
.type
);
667 /* Read in any buffered data */
668 for(t
= tracks
; t
; t
= t
->next
)
669 if(t
->slot
!= -1 && (fds
[t
->slot
].revents
& (POLLIN
| POLLHUP
)))
671 /* Maybe we finished playing a track somewhere in the above */
673 /* If we don't need the sound device for now then close it for the benefit
674 * of anyone else who wants it. */
675 if((!playing
|| paused
) && device_state
== device_open
)
677 /* If we've not reported out state for a second do so now. */
678 if(time(0) > last_report
)
683 int main(int argc
, char **argv
) {
687 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
688 while((n
= getopt_long(argc
, argv
, "hVc:dD", options
, 0)) >= 0) {
692 case 'c': configfile
= optarg
; break;
693 case 'd': debugging
= 1; break;
694 case 'D': debugging
= 0; break;
695 default: fatal(0, "invalid option");
698 if(getenv("DISORDER_DEBUG_SPEAKER")) debugging
= 1;
699 /* If stderr is a TTY then log there, otherwise to syslog. */
701 openlog(progname
, LOG_PID
, LOG_DAEMON
);
702 log_default
= &log_syslog
;
704 if(config_read()) fatal(0, "cannot read configuration");
706 signal(SIGPIPE
, SIG_IGN
);
708 signal(SIGCHLD
, reap
);
710 xnice(config
->nice_speaker
);
713 /* make sure we're not root, whatever the config says */
714 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
715 /* identify the backend used to play */
716 for(n
= 0; backends
[n
]; ++n
)
717 if(backends
[n
]->backend
== config
->speaker_backend
)
720 fatal(0, "unsupported backend %d", config
->speaker_backend
);
721 backend
= backends
[n
];
722 /* backend-specific initialization */
725 info("stopped (parent terminated)");