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