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