chiark / gitweb /
Remove some obsolete code
[disorder] / server / speaker.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
5aff007d 3 * Copyright (C) 2005-2008 Richard Kettlewell
313acc77 4 * Portions (C) 2007 Mark Wooding
460b9539 5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 * USA
20 */
1674096e 21/** @file server/speaker.c
cf714d85 22 * @brief Speaker process
1674096e 23 *
24 * This program is responsible for transmitting a single coherent audio stream
25 * to its destination (over the network, to some sound API, to some
42829e58
RK
26 * subprocess). It receives connections from decoders (or rather from the
27 * process that is about to become disorder-normalize) and plays them in the
28 * right order.
1674096e 29 *
795192f4 30 * @b Encodings. For the <a href="http://www.alsa-project.org/">ALSA</a> API,
31 * 8- and 16- bit stereo and mono are supported, with any sample rate (within
32 * the limits that ALSA can deal with.)
1674096e 33 *
6d2d327c
RK
34 * Inbound data is expected to match @c config->sample_format. In normal use
35 * this is arranged by the @c disorder-normalize program (see @ref
36 * server/normalize.c).
1674096e 37 *
3fbdc96d 387 * @b Garbage @b Collection. This program deliberately does not use the
795192f4 39 * garbage collector even though it might be convenient to do so. This is for
40 * two reasons. Firstly some sound APIs use thread threads and we do not want
41 * to have to deal with potential interactions between threading and garbage
42 * collection. Secondly this process needs to be able to respond quickly and
43 * this is not compatible with the collector hanging the program even
44 * relatively briefly.
45 *
46 * @b Units. This program thinks at various times in three different units.
47 * Bytes are obvious. A sample is a single sample on a single channel. A
48 * frame is several samples on different channels at the same point in time.
49 * So (for instance) a 16-bit stereo frame is 4 bytes and consists of a pair of
50 * 2-byte samples.
1674096e 51 */
460b9539 52
53#include <config.h>
54#include "types.h"
55
56#include <getopt.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <locale.h>
60#include <syslog.h>
61#include <unistd.h>
62#include <errno.h>
63#include <ao/ao.h>
64#include <string.h>
65#include <assert.h>
66#include <sys/select.h>
9d5da576 67#include <sys/wait.h>
460b9539 68#include <time.h>
8023f60b 69#include <fcntl.h>
70#include <poll.h>
84aa9f93 71#include <sys/un.h>
a5f3ca1e 72#include <sys/stat.h>
460b9539 73
74#include "configuration.h"
75#include "syscalls.h"
76#include "log.h"
77#include "defs.h"
78#include "mem.h"
ea410ba1 79#include "speaker-protocol.h"
460b9539 80#include "user.h"
cf714d85 81#include "speaker.h"
85cb23d7 82#include "printf.h"
3fbdc96d 83#include "version.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 */
6d2d327c 92size_t bpf;
1c3f1e73 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
84aa9f93
RK
100/** @brief Listen socket */
101static int listenfd;
102
460b9539 103static time_t last_report; /* when we last reported */
104static int paused; /* pause status */
50ae38dd 105
5a7c42a8 106/** @brief The current device state */
107enum device_states device_state;
50ae38dd 108
55f35f2d 109/** @brief Set when idled
110 *
111 * This is set when the sound device is deliberately closed by idle().
55f35f2d 112 */
1c3f1e73 113int idled;
460b9539 114
29601377 115/** @brief Selected backend */
116static const struct speaker_backend *backend;
117
460b9539 118static const struct option options[] = {
119 { "help", no_argument, 0, 'h' },
120 { "version", no_argument, 0, 'V' },
121 { "config", required_argument, 0, 'c' },
122 { "debug", no_argument, 0, 'd' },
123 { "no-debug", no_argument, 0, 'D' },
0ca6d097
RK
124 { "syslog", no_argument, 0, 's' },
125 { "no-syslog", no_argument, 0, 'S' },
460b9539 126 { 0, 0, 0, 0 }
127};
128
129/* Display usage message and terminate. */
130static void help(void) {
131 xprintf("Usage:\n"
132 " disorder-speaker [OPTIONS]\n"
133 "Options:\n"
134 " --help, -h Display usage message\n"
135 " --version, -V Display version number\n"
136 " --config PATH, -c PATH Set configuration file\n"
137 " --debug, -d Turn on debugging\n"
0ca6d097 138 " --[no-]syslog Force logging\n"
460b9539 139 "\n"
140 "Speaker process for DisOrder. Not intended to be run\n"
141 "directly.\n");
142 xfclose(stdout);
143 exit(0);
144}
145
1674096e 146/** @brief Return the number of bytes per frame in @p format */
6d2d327c 147static size_t bytes_per_frame(const struct stream_header *format) {
460b9539 148 return format->channels * format->bits / 8;
149}
150
1674096e 151/** @brief Find track @p id, maybe creating it if not found */
460b9539 152static struct track *findtrack(const char *id, int create) {
153 struct track *t;
154
155 D(("findtrack %s %d", id, create));
156 for(t = tracks; t && strcmp(id, t->id); t = t->next)
157 ;
158 if(!t && create) {
159 t = xmalloc(sizeof *t);
160 t->next = tracks;
161 strcpy(t->id, id);
162 t->fd = -1;
163 tracks = t;
460b9539 164 }
165 return t;
166}
167
1674096e 168/** @brief Remove track @p id (but do not destroy it) */
460b9539 169static struct track *removetrack(const char *id) {
170 struct track *t, **tt;
171
172 D(("removetrack %s", id));
173 for(tt = &tracks; (t = *tt) && strcmp(id, t->id); tt = &t->next)
174 ;
175 if(t)
176 *tt = t->next;
177 return t;
178}
179
1674096e 180/** @brief Destroy a track */
460b9539 181static void destroy(struct track *t) {
182 D(("destroy %s", t->id));
183 if(t->fd != -1) xclose(t->fd);
460b9539 184 free(t);
185}
186
1674096e 187/** @brief Read data into a sample buffer
188 * @param t Pointer to track
189 * @return 0 on success, -1 on EOF
190 *
55f35f2d 191 * This is effectively the read callback on @c t->fd. It is called from the
192 * main loop whenever the track's file descriptor is readable, assuming the
193 * buffer has not reached the maximum allowed occupancy.
1674096e 194 */
f5a03f58 195static int speaker_fill(struct track *t) {
460b9539 196 size_t where, left;
197 int n;
198
6d2d327c
RK
199 D(("fill %s: eof=%d used=%zu",
200 t->id, t->eof, t->used));
460b9539 201 if(t->eof) return -1;
6d2d327c 202 if(t->used < sizeof t->buffer) {
460b9539 203 /* there is room left in the buffer */
6d2d327c
RK
204 where = (t->start + t->used) % sizeof t->buffer;
205 /* Get as much data as we can */
206 if(where >= t->start) left = (sizeof t->buffer) - where;
207 else left = t->start - where;
460b9539 208 do {
209 n = read(t->fd, t->buffer + where, left);
210 } while(n < 0 && errno == EINTR);
211 if(n < 0) {
212 if(errno != EAGAIN) fatal(errno, "error reading sample stream");
213 return 0;
214 }
215 if(n == 0) {
216 D(("fill %s: eof detected", t->id));
217 t->eof = 1;
f5a03f58 218 t->playable = 1;
460b9539 219 return -1;
220 }
221 t->used += n;
f5a03f58
RK
222 if(t->used == sizeof t->buffer)
223 t->playable = 1;
460b9539 224 }
225 return 0;
226}
227
55f35f2d 228/** @brief Close the sound device
229 *
230 * This is called to deactivate the output device when pausing, and also by the
231 * ALSA backend when changing encoding (in which case the sound device will be
232 * immediately reactivated).
233 */
460b9539 234static void idle(void) {
460b9539 235 D(("idle"));
5a7c42a8 236 if(backend->deactivate)
b5a99ad0 237 backend->deactivate();
5a7c42a8 238 else
239 device_state = device_closed;
e83d0967 240 idled = 1;
460b9539 241}
242
1674096e 243/** @brief Abandon the current track */
1c3f1e73 244void abandon(void) {
460b9539 245 struct speaker_message sm;
246
247 D(("abandon"));
248 memset(&sm, 0, sizeof sm);
249 sm.type = SM_FINISHED;
250 strcpy(sm.id, playing->id);
84aa9f93 251 speaker_send(1, &sm);
460b9539 252 removetrack(playing->id);
253 destroy(playing);
254 playing = 0;
1c6e6a61 255}
256
1674096e 257/** @brief Enable sound output
258 *
259 * Makes sure the sound device is open and has the right sample format. Return
260 * 0 on success and -1 on error.
261 */
5a7c42a8 262static void activate(void) {
6d2d327c 263 if(backend->activate)
5a7c42a8 264 backend->activate();
6d2d327c 265 else
5a7c42a8 266 device_state = device_open;
460b9539 267}
268
55f35f2d 269/** @brief Check whether the current track has finished
270 *
271 * The current track is determined to have finished either if the input stream
272 * eded before the format could be determined (i.e. it is malformed) or the
273 * input is at end of file and there is less than a frame left unplayed. (So
274 * it copes with decoders that crash mid-frame.)
275 */
460b9539 276static void maybe_finished(void) {
277 if(playing
278 && playing->eof
6d2d327c 279 && playing->used < bytes_per_frame(&config->sample_format))
460b9539 280 abandon();
281}
282
dac25ef9
RK
283/** @brief Return nonzero if we want to play some audio
284 *
285 * We want to play audio if there is a current track; and it is not paused; and
286 * it is playable according to the rules for @ref track::playable.
287 */
288static int playable(void) {
289 return playing
290 && !paused
291 && playing->playable;
292}
293
5a7c42a8 294/** @brief Play up to @p frames frames of audio
295 *
296 * It is always safe to call this function.
297 * - If @ref playing is 0 then it will just return
298 * - If @ref paused is non-0 then it will just return
299 * - If @ref device_state != @ref device_open then it will call activate() and
300 * return if it it fails.
301 * - If there is not enough audio to play then it play what is available.
302 *
303 * If there are not enough frames to play then whatever is available is played
dac25ef9
RK
304 * instead. It is up to mainloop() to ensure that speaker_play() is not called
305 * when unreasonably only an small amounts of data is available to play.
5a7c42a8 306 */
dac25ef9 307static void speaker_play(size_t frames) {
3c68b773 308 size_t avail_frames, avail_bytes, written_frames;
9d5da576 309 ssize_t written_bytes;
460b9539 310
dac25ef9
RK
311 /* Make sure there's a track to play and it is not paused */
312 if(!playable())
460b9539 313 return;
6d2d327c
RK
314 /* Make sure the output device is open */
315 if(device_state != device_open) {
5a7c42a8 316 activate();
317 if(device_state != device_open)
318 return;
460b9539 319 }
6d2d327c 320 D(("play: play %zu/%zu%s %dHz %db %dc", frames, playing->used / bpf,
460b9539 321 playing->eof ? " EOF" : "",
6d2d327c
RK
322 config->sample_format.rate,
323 config->sample_format.bits,
324 config->sample_format.channels));
460b9539 325 /* Figure out how many frames there are available to write */
6d2d327c 326 if(playing->start + playing->used > sizeof playing->buffer)
7f9d5847 327 /* The ring buffer is currently wrapped, only play up to the wrap point */
6d2d327c 328 avail_bytes = (sizeof playing->buffer) - playing->start;
460b9539 329 else
7f9d5847 330 /* The ring buffer is not wrapped, can play the lot */
460b9539 331 avail_bytes = playing->used;
6d2d327c 332 avail_frames = avail_bytes / bpf;
7f9d5847 333 /* Only play up to the requested amount */
334 if(avail_frames > frames)
335 avail_frames = frames;
336 if(!avail_frames)
337 return;
3c68b773 338 /* Play it, Sam */
339 written_frames = backend->play(avail_frames);
6d2d327c 340 written_bytes = written_frames * bpf;
e83d0967
RK
341 /* written_bytes and written_frames had better both be set and correct by
342 * this point */
460b9539 343 playing->start += written_bytes;
344 playing->used -= written_bytes;
345 playing->played += written_frames;
346 /* If the pointer is at the end of the buffer (or the buffer is completely
347 * empty) wrap it back to the start. */
6d2d327c 348 if(!playing->used || playing->start == (sizeof playing->buffer))
460b9539 349 playing->start = 0;
f5a03f58 350 /* If the buffer emptied out mark the track as unplayably */
3496051f 351 if(!playing->used && !playing->eof) {
f74fc096 352 error(0, "track buffer emptied");
f5a03f58 353 playing->playable = 0;
f74fc096 354 }
460b9539 355 frames -= written_frames;
5a7c42a8 356 return;
460b9539 357}
358
359/* Notify the server what we're up to. */
360static void report(void) {
361 struct speaker_message sm;
362
6d2d327c 363 if(playing) {
460b9539 364 memset(&sm, 0, sizeof sm);
365 sm.type = paused ? SM_PAUSED : SM_PLAYING;
366 strcpy(sm.id, playing->id);
6d2d327c 367 sm.data = playing->played / config->sample_format.rate;
84aa9f93 368 speaker_send(1, &sm);
460b9539 369 }
370 time(&last_report);
371}
372
9d5da576 373static void reap(int __attribute__((unused)) sig) {
e83d0967 374 pid_t cmdpid;
9d5da576 375 int st;
376
377 do
e83d0967
RK
378 cmdpid = waitpid(-1, &st, WNOHANG);
379 while(cmdpid > 0);
9d5da576 380 signal(SIGCHLD, reap);
381}
382
1c3f1e73 383int addfd(int fd, int events) {
460b9539 384 if(fdno < NFDS) {
385 fds[fdno].fd = fd;
386 fds[fdno].events = events;
387 return fdno++;
388 } else
389 return -1;
390}
391
572d74ba 392/** @brief Table of speaker backends */
1c3f1e73 393static const struct speaker_backend *backends[] = {
146e86fb 394#if HAVE_ALSA_ASOUNDLIB_H
1c3f1e73 395 &alsa_backend,
572d74ba 396#endif
1c3f1e73 397 &command_backend,
398 &network_backend,
937be4c0
RK
399#if HAVE_COREAUDIO_AUDIOHARDWARE_H
400 &coreaudio_backend,
401#endif
e99d42b1 402#if HAVE_SYS_SOUNDCARD_H
403 &oss_backend,
404#endif
1c3f1e73 405 0
572d74ba 406};
407
5a7c42a8 408/** @brief Main event loop */
55f35f2d 409static void mainloop(void) {
572d74ba 410 struct track *t;
411 struct speaker_message sm;
84aa9f93 412 int n, fd, stdin_slot, timeout, listen_slot;
460b9539 413
460b9539 414 while(getppid() != 1) {
415 fdno = 0;
5a7c42a8 416 /* By default we will wait up to a second before thinking about current
417 * state. */
418 timeout = 1000;
460b9539 419 /* Always ready for commands from the main server. */
420 stdin_slot = addfd(0, POLLIN);
84aa9f93
RK
421 /* Also always ready for inbound connections */
422 listen_slot = addfd(listenfd, POLLIN);
460b9539 423 /* Try to read sample data for the currently playing track if there is
424 * buffer space. */
84aa9f93
RK
425 if(playing
426 && playing->fd >= 0
427 && !playing->eof
428 && playing->used < (sizeof playing->buffer))
460b9539 429 playing->slot = addfd(playing->fd, POLLIN);
5a7c42a8 430 else if(playing)
460b9539 431 playing->slot = -1;
5a7c42a8 432 if(playable()) {
433 /* We want to play some audio. If the device is closed then we attempt
434 * to open it. */
435 if(device_state == device_closed)
436 activate();
437 /* If the device is (now) open then we will wait up until it is ready for
438 * more. If something went wrong then we should have device_error
439 * instead, but the post-poll code will cope even if it's
440 * device_closed. */
441 if(device_state == device_open)
e84fb5f0 442 backend->beforepoll(&timeout);
5a7c42a8 443 }
460b9539 444 /* If any other tracks don't have a full buffer, try to read sample data
5a7c42a8 445 * from them. We do this last of all, so that if we run out of slots,
446 * nothing important can't be monitored. */
460b9539 447 for(t = tracks; t; t = t->next)
448 if(t != playing) {
84aa9f93
RK
449 if(t->fd >= 0
450 && !t->eof
451 && t->used < sizeof t->buffer) {
9d5da576 452 t->slot = addfd(t->fd, POLLIN | POLLHUP);
460b9539 453 } else
454 t->slot = -1;
455 }
e83d0967
RK
456 /* Wait for something interesting to happen */
457 n = poll(fds, fdno, timeout);
460b9539 458 if(n < 0) {
459 if(errno == EINTR) continue;
460 fatal(errno, "error calling poll");
461 }
462 /* Play some sound before doing anything else */
5a7c42a8 463 if(playable()) {
464 /* We want to play some audio */
465 if(device_state == device_open) {
466 if(backend->ready())
dac25ef9 467 speaker_play(3 * FRAMES);
5a7c42a8 468 } else {
469 /* We must be in _closed or _error, and it should be the latter, but we
470 * cope with either.
471 *
dac25ef9
RK
472 * We most likely timed out, so now is a good time to retry.
473 * speaker_play() knows to re-activate the device if necessary.
5a7c42a8 474 */
dac25ef9 475 speaker_play(3 * FRAMES);
5a7c42a8 476 }
460b9539 477 }
84aa9f93
RK
478 /* Perhaps a connection has arrived */
479 if(fds[listen_slot].revents & POLLIN) {
480 struct sockaddr_un addr;
481 socklen_t addrlen = sizeof addr;
482 uint32_t l;
483 char id[24];
484
dc450d30 485 if((fd = accept(listenfd, (struct sockaddr *)&addr, &addrlen)) >= 0) {
937be4c0 486 blocking(fd);
84aa9f93
RK
487 if(read(fd, &l, sizeof l) < 4) {
488 error(errno, "reading length from inbound connection");
489 xclose(fd);
490 } else if(l >= sizeof id) {
491 error(0, "id length too long");
492 xclose(fd);
493 } else if(read(fd, id, l) < (ssize_t)l) {
494 error(errno, "reading id from inbound connection");
495 xclose(fd);
496 } else {
497 id[l] = 0;
498 D(("id %s fd %d", id, fd));
499 t = findtrack(id, 1/*create*/);
500 write(fd, "", 1); /* write an ack */
501 if(t->fd != -1) {
66bb2e02 502 error(0, "%s: already got a connection", id);
84aa9f93
RK
503 xclose(fd);
504 } else {
505 nonblock(fd);
506 t->fd = fd; /* yay */
507 }
508 }
509 } else
510 error(errno, "accept");
511 }
460b9539 512 /* Perhaps we have a command to process */
513 if(fds[stdin_slot].revents & POLLIN) {
5a7c42a8 514 /* There might (in theory) be several commands queued up, but in general
515 * this won't be the case, so we don't bother looping around to pick them
516 * all up. */
84aa9f93
RK
517 n = speaker_recv(0, &sm);
518 /* TODO */
460b9539 519 if(n > 0)
520 switch(sm.type) {
460b9539 521 case SM_PLAY:
460b9539 522 if(playing) fatal(0, "got SM_PLAY but already playing something");
523 t = findtrack(sm.id, 1);
84aa9f93
RK
524 D(("SM_PLAY %s fd %d", t->id, t->fd));
525 if(t->fd == -1)
526 error(0, "cannot play track because no connection arrived");
460b9539 527 playing = t;
5a7c42a8 528 /* We attempt to play straight away rather than going round the loop.
dac25ef9 529 * speaker_play() is clever enough to perform any activation that is
5a7c42a8 530 * required. */
dac25ef9 531 speaker_play(3 * FRAMES);
460b9539 532 report();
533 break;
534 case SM_PAUSE:
535 D(("SM_PAUSE"));
536 paused = 1;
537 report();
538 break;
539 case SM_RESUME:
540 D(("SM_RESUME"));
541 if(paused) {
542 paused = 0;
5a7c42a8 543 /* As for SM_PLAY we attempt to play straight away. */
460b9539 544 if(playing)
dac25ef9 545 speaker_play(3 * FRAMES);
460b9539 546 }
547 report();
548 break;
549 case SM_CANCEL:
819f5988 550 D(("SM_CANCEL %s", sm.id));
460b9539 551 t = removetrack(sm.id);
552 if(t) {
553 if(t == playing) {
819f5988 554 /* scratching the playing track */
460b9539 555 sm.type = SM_FINISHED;
460b9539 556 playing = 0;
819f5988
RK
557 } else {
558 /* Could be scratching the playing track before it's quite got
559 * going, or could be just removing a track from the queue. We
560 * log more because there's been a bug here recently than because
561 * it's particularly interesting; the log message will be removed
562 * if no further problems show up. */
563 info("SM_CANCEL for nonplaying track %s", sm.id);
564 sm.type = SM_STILLBORN;
460b9539 565 }
819f5988 566 strcpy(sm.id, t->id);
460b9539 567 destroy(t);
2b2a5fed 568 } else {
819f5988
RK
569 /* Probably scratching the playing track well before it's got
570 * going, but could indicate a bug, so we log this as an error. */
2b2a5fed 571 sm.type = SM_UNKNOWN;
460b9539 572 error(0, "SM_CANCEL for unknown track %s", sm.id);
2b2a5fed 573 }
819f5988 574 speaker_send(1, &sm);
460b9539 575 report();
576 break;
577 case SM_RELOAD:
578 D(("SM_RELOAD"));
c00fce3a 579 if(config_read(1)) error(0, "cannot read configuration");
460b9539 580 info("reloaded configuration");
581 break;
582 default:
583 error(0, "unknown message type %d", sm.type);
584 }
585 }
586 /* Read in any buffered data */
587 for(t = tracks; t; t = t->next)
84aa9f93
RK
588 if(t->fd != -1
589 && t->slot != -1
590 && (fds[t->slot].revents & (POLLIN | POLLHUP)))
f5a03f58 591 speaker_fill(t);
460b9539 592 /* Maybe we finished playing a track somewhere in the above */
593 maybe_finished();
594 /* If we don't need the sound device for now then close it for the benefit
595 * of anyone else who wants it. */
5a7c42a8 596 if((!playing || paused) && device_state == device_open)
460b9539 597 idle();
598 /* If we've not reported out state for a second do so now. */
599 if(time(0) > last_report)
600 report();
601 }
55f35f2d 602}
603
604int main(int argc, char **argv) {
0ca6d097 605 int n, logsyslog = !isatty(2);
84aa9f93
RK
606 struct sockaddr_un addr;
607 static const int one = 1;
937be4c0 608 struct speaker_message sm;
38b8221f 609 const char *d;
85cb23d7 610 char *dir;
55f35f2d 611
612 set_progname(argv);
613 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
0ca6d097 614 while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) {
55f35f2d 615 switch(n) {
616 case 'h': help();
3fbdc96d 617 case 'V': version("disorder-speaker");
55f35f2d 618 case 'c': configfile = optarg; break;
619 case 'd': debugging = 1; break;
620 case 'D': debugging = 0; break;
0ca6d097
RK
621 case 'S': logsyslog = 0; break;
622 case 's': logsyslog = 1; break;
55f35f2d 623 default: fatal(0, "invalid option");
624 }
625 }
38b8221f 626 if((d = getenv("DISORDER_DEBUG_SPEAKER"))) debugging = atoi(d);
0ca6d097 627 if(logsyslog) {
55f35f2d 628 openlog(progname, LOG_PID, LOG_DAEMON);
629 log_default = &log_syslog;
630 }
c00fce3a 631 if(config_read(1)) fatal(0, "cannot read configuration");
6d2d327c 632 bpf = bytes_per_frame(&config->sample_format);
55f35f2d 633 /* ignore SIGPIPE */
634 signal(SIGPIPE, SIG_IGN);
635 /* reap kids */
636 signal(SIGCHLD, reap);
637 /* set nice value */
638 xnice(config->nice_speaker);
639 /* change user */
640 become_mortal();
641 /* make sure we're not root, whatever the config says */
642 if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root");
643 /* identify the backend used to play */
1c3f1e73 644 for(n = 0; backends[n]; ++n)
bd8895a8 645 if(backends[n]->backend == config->api)
55f35f2d 646 break;
1c3f1e73 647 if(!backends[n])
bd8895a8 648 fatal(0, "unsupported api %d", config->api);
1c3f1e73 649 backend = backends[n];
55f35f2d 650 /* backend-specific initialization */
651 backend->init();
85cb23d7
RK
652 /* create the socket directory */
653 byte_xasprintf(&dir, "%s/speaker", config->home);
654 unlink(dir); /* might be a leftover socket */
a5f3ca1e 655 if(mkdir(dir, 0700) < 0 && errno != EEXIST)
85cb23d7 656 fatal(errno, "error creating %s", dir);
84aa9f93
RK
657 /* set up the listen socket */
658 listenfd = xsocket(PF_UNIX, SOCK_STREAM, 0);
659 memset(&addr, 0, sizeof addr);
660 addr.sun_family = AF_UNIX;
85cb23d7 661 snprintf(addr.sun_path, sizeof addr.sun_path, "%s/speaker/socket",
84aa9f93
RK
662 config->home);
663 if(unlink(addr.sun_path) < 0 && errno != ENOENT)
664 error(errno, "removing %s", addr.sun_path);
665 xsetsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
dc450d30 666 if(bind(listenfd, (const struct sockaddr *)&addr, sizeof addr) < 0)
84aa9f93
RK
667 fatal(errno, "error binding socket to %s", addr.sun_path);
668 xlisten(listenfd, 128);
669 nonblock(listenfd);
670 info("listening on %s", addr.sun_path);
937be4c0
RK
671 memset(&sm, 0, sizeof sm);
672 sm.type = SM_READY;
673 speaker_send(1, &sm);
55f35f2d 674 mainloop();
460b9539 675 info("stopped (parent terminated)");
676 exit(0);
677}
678
679/*
680Local Variables:
681c-basic-offset:2
682comment-column:40
683fill-column:79
684indent-tabs-mode:nil
685End:
686*/