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