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