Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
de38ab9b | 3 | * Copyright (C) 2005-2010 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 | * |
b50cfb8a RK |
28 | * @b Model. mainloop() implements a select loop awaiting commands from the |
29 | * main server, new connections to the speaker socket, and audio data on those | |
30 | * connections. Each connection starts with a queue ID (with a 32-bit | |
31 | * native-endian length word), allowing it to be referred to in commands from | |
32 | * the server. | |
33 | * | |
34 | * Data read on connections is buffered, up to a limit (currently 1Mbyte per | |
35 | * track). No attempt is made here to limit the number of tracks, it is | |
36 | * assumed that the main server won't start outrageously many decoders. | |
37 | * | |
38 | * Audio is supplied from this buffer to the uaudio play callback. Playback is | |
39 | * enabled when a track is to be played and disabled when the its last bytes | |
40 | * have been return by the callback; pause and resume is implemneted the | |
41 | * obvious way. If the callback finds itself required to play when there is no | |
42 | * playing track it returns dead air. | |
43 | * | |
888b8031 RK |
44 | * To implement gapless playback, the server is notified that a track has |
45 | * finished slightly early. @ref SM_PLAY is therefore allowed to arrive while | |
46 | * the previous track is still playing provided an early @ref SM_FINISHED has | |
47 | * been sent for it. | |
48 | * | |
b50cfb8a RK |
49 | * @b Encodings. The encodings supported depend entirely on the uaudio backend |
50 | * chosen. See @ref uaudio.h, etc. | |
1674096e | 51 | * |
6d2d327c RK |
52 | * Inbound data is expected to match @c config->sample_format. In normal use |
53 | * this is arranged by the @c disorder-normalize program (see @ref | |
54 | * server/normalize.c). | |
1674096e | 55 | * |
5b7a22c6 | 56 | * @b Garbage @b Collection. This program deliberately does not use the |
795192f4 | 57 | * garbage collector even though it might be convenient to do so. This is for |
58 | * two reasons. Firstly some sound APIs use thread threads and we do not want | |
59 | * to have to deal with potential interactions between threading and garbage | |
60 | * collection. Secondly this process needs to be able to respond quickly and | |
61 | * this is not compatible with the collector hanging the program even | |
62 | * relatively briefly. | |
63 | * | |
64 | * @b Units. This program thinks at various times in three different units. | |
65 | * Bytes are obvious. A sample is a single sample on a single channel. A | |
66 | * frame is several samples on different channels at the same point in time. | |
67 | * So (for instance) a 16-bit stereo frame is 4 bytes and consists of a pair of | |
68 | * 2-byte samples. | |
1674096e | 69 | */ |
460b9539 | 70 | |
05b75f8d | 71 | #include "common.h" |
460b9539 | 72 | |
73 | #include <getopt.h> | |
460b9539 | 74 | #include <locale.h> |
75 | #include <syslog.h> | |
76 | #include <unistd.h> | |
77 | #include <errno.h> | |
78 | #include <ao/ao.h> | |
460b9539 | 79 | #include <sys/select.h> |
9d5da576 | 80 | #include <sys/wait.h> |
460b9539 | 81 | #include <time.h> |
8023f60b | 82 | #include <fcntl.h> |
83 | #include <poll.h> | |
84aa9f93 | 84 | #include <sys/un.h> |
a5f3ca1e | 85 | #include <sys/stat.h> |
b50cfb8a | 86 | #include <pthread.h> |
adeb58a0 | 87 | #include <sys/resource.h> |
5bf7c546 | 88 | #include <gcrypt.h> |
460b9539 | 89 | |
90 | #include "configuration.h" | |
91 | #include "syscalls.h" | |
92 | #include "log.h" | |
93 | #include "defs.h" | |
94 | #include "mem.h" | |
ea410ba1 | 95 | #include "speaker-protocol.h" |
460b9539 | 96 | #include "user.h" |
85cb23d7 | 97 | #include "printf.h" |
3fbdc96d | 98 | #include "version.h" |
b50cfb8a | 99 | #include "uaudio.h" |
460b9539 | 100 | |
b50cfb8a RK |
101 | /** @brief Maximum number of FDs to poll for */ |
102 | #define NFDS 1024 | |
e83d0967 | 103 | |
888b8031 RK |
104 | /** @brief Number of bytes before end of track to send SM_FINISHED |
105 | * | |
106 | * Generally set to 1 second. | |
107 | */ | |
108 | static size_t early_finish; | |
109 | ||
b50cfb8a RK |
110 | /** @brief Track structure |
111 | * | |
112 | * Known tracks are kept in a linked list. Usually there will be at most two | |
113 | * of these but rearranging the queue can cause there to be more. | |
114 | */ | |
115 | struct track { | |
116 | /** @brief Next track */ | |
117 | struct track *next; | |
118 | ||
119 | /** @brief Input file descriptor */ | |
120 | int fd; /* input FD */ | |
121 | ||
122 | /** @brief Track ID */ | |
123 | char id[24]; | |
124 | ||
125 | /** @brief Start position of data in buffer */ | |
126 | size_t start; | |
127 | ||
128 | /** @brief Number of bytes of data in buffer */ | |
129 | size_t used; | |
130 | ||
131 | /** @brief Set @c fd is at EOF */ | |
132 | int eof; | |
133 | ||
134 | /** @brief Total number of samples played */ | |
135 | unsigned long long played; | |
136 | ||
137 | /** @brief Slot in @ref fds */ | |
138 | int slot; | |
139 | ||
140 | /** @brief Set when playable | |
141 | * | |
142 | * A track becomes playable whenever it fills its buffer or reaches EOF; it | |
143 | * stops being playable when it entirely empties its buffer. Tracks start | |
144 | * out life not playable. | |
145 | */ | |
146 | int playable; | |
888b8031 RK |
147 | |
148 | /** @brief Set when finished | |
149 | * | |
150 | * This is set when we've notified the server that the track is finished. | |
151 | * Once this has happened (typically very late in the track's lifetime) the | |
152 | * track cannot be paused or cancelled. | |
153 | */ | |
154 | int finished; | |
b50cfb8a RK |
155 | |
156 | /** @brief Input buffer | |
157 | * | |
158 | * 1Mbyte is enough for nearly 6s of 44100Hz 16-bit stereo | |
159 | */ | |
160 | char buffer[1048576]; | |
161 | }; | |
460b9539 | 162 | |
b50cfb8a RK |
163 | /** @brief Lock protecting data structures |
164 | * | |
165 | * This lock protects values shared between the main thread and the callback. | |
89156c8c RK |
166 | * |
167 | * It is held 'all' the time by the main thread, the exceptions being when | |
168 | * called activate/deactivate callbacks and when calling (potentially) slow | |
169 | * system calls (in particular poll(), where in fact the main thread will spend | |
170 | * most of its time blocked). | |
171 | * | |
172 | * The callback holds it when it's running. | |
b50cfb8a RK |
173 | */ |
174 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; | |
175 | ||
176 | /** @brief Linked list of all prepared tracks */ | |
177 | static struct track *tracks; | |
178 | ||
179 | /** @brief Playing track, or NULL | |
180 | * | |
888b8031 RK |
181 | * This means the track the speaker process intends to play. It does not |
182 | * reflect any other state (e.g. activation of uaudio backend). | |
b50cfb8a RK |
183 | */ |
184 | static struct track *playing; | |
1c3f1e73 | 185 | |
888b8031 RK |
186 | /** @brief Pending playing track, or NULL |
187 | * | |
188 | * This means the track the server wants the speaker to play. | |
189 | */ | |
190 | static struct track *pending_playing; | |
191 | ||
1c3f1e73 | 192 | /** @brief Array of file descriptors for poll() */ |
b50cfb8a | 193 | static struct pollfd fds[NFDS]; |
1c3f1e73 | 194 | |
195 | /** @brief Next free slot in @ref fds */ | |
b50cfb8a | 196 | static int fdno; |
1c3f1e73 | 197 | |
84aa9f93 RK |
198 | /** @brief Listen socket */ |
199 | static int listenfd; | |
200 | ||
b50cfb8a RK |
201 | /** @brief Timestamp of last potential report to server */ |
202 | static time_t last_report; | |
50ae38dd | 203 | |
b50cfb8a RK |
204 | /** @brief Set when paused */ |
205 | static int paused; | |
50ae38dd | 206 | |
b50cfb8a RK |
207 | /** @brief Set when back end activated */ |
208 | static int activated; | |
209 | ||
210 | /** @brief Signal pipe back into the poll() loop */ | |
211 | static int sigpipe[2]; | |
460b9539 | 212 | |
29601377 | 213 | /** @brief Selected backend */ |
b50cfb8a | 214 | static const struct uaudio *backend; |
29601377 | 215 | |
460b9539 | 216 | static const struct option options[] = { |
217 | { "help", no_argument, 0, 'h' }, | |
218 | { "version", no_argument, 0, 'V' }, | |
219 | { "config", required_argument, 0, 'c' }, | |
220 | { "debug", no_argument, 0, 'd' }, | |
221 | { "no-debug", no_argument, 0, 'D' }, | |
0ca6d097 RK |
222 | { "syslog", no_argument, 0, 's' }, |
223 | { "no-syslog", no_argument, 0, 'S' }, | |
460b9539 | 224 | { 0, 0, 0, 0 } |
225 | }; | |
226 | ||
227 | /* Display usage message and terminate. */ | |
228 | static void help(void) { | |
229 | xprintf("Usage:\n" | |
230 | " disorder-speaker [OPTIONS]\n" | |
231 | "Options:\n" | |
232 | " --help, -h Display usage message\n" | |
233 | " --version, -V Display version number\n" | |
234 | " --config PATH, -c PATH Set configuration file\n" | |
235 | " --debug, -d Turn on debugging\n" | |
0ca6d097 | 236 | " --[no-]syslog Force logging\n" |
460b9539 | 237 | "\n" |
238 | "Speaker process for DisOrder. Not intended to be run\n" | |
239 | "directly.\n"); | |
240 | xfclose(stdout); | |
241 | exit(0); | |
242 | } | |
243 | ||
b50cfb8a RK |
244 | /** @brief Find track @p id, maybe creating it if not found |
245 | * @param id Track ID to find | |
246 | * @param create If nonzero, create track structure of @p id not found | |
247 | * @return Pointer to track structure or NULL | |
248 | */ | |
460b9539 | 249 | static struct track *findtrack(const char *id, int create) { |
250 | struct track *t; | |
251 | ||
252 | D(("findtrack %s %d", id, create)); | |
253 | for(t = tracks; t && strcmp(id, t->id); t = t->next) | |
254 | ; | |
255 | if(!t && create) { | |
256 | t = xmalloc(sizeof *t); | |
257 | t->next = tracks; | |
258 | strcpy(t->id, id); | |
259 | t->fd = -1; | |
260 | tracks = t; | |
460b9539 | 261 | } |
262 | return t; | |
263 | } | |
264 | ||
b50cfb8a RK |
265 | /** @brief Remove track @p id (but do not destroy it) |
266 | * @param id Track ID to remove | |
267 | * @return Track structure or NULL if not found | |
268 | */ | |
460b9539 | 269 | static struct track *removetrack(const char *id) { |
270 | struct track *t, **tt; | |
271 | ||
272 | D(("removetrack %s", id)); | |
273 | for(tt = &tracks; (t = *tt) && strcmp(id, t->id); tt = &t->next) | |
274 | ; | |
275 | if(t) | |
276 | *tt = t->next; | |
277 | return t; | |
278 | } | |
279 | ||
b50cfb8a RK |
280 | /** @brief Destroy a track |
281 | * @param t Track structure | |
282 | */ | |
460b9539 | 283 | static void destroy(struct track *t) { |
284 | D(("destroy %s", t->id)); | |
b50cfb8a RK |
285 | if(t->fd != -1) |
286 | xclose(t->fd); | |
460b9539 | 287 | free(t); |
288 | } | |
289 | ||
1674096e | 290 | /** @brief Read data into a sample buffer |
291 | * @param t Pointer to track | |
292 | * @return 0 on success, -1 on EOF | |
293 | * | |
55f35f2d | 294 | * This is effectively the read callback on @c t->fd. It is called from the |
295 | * main loop whenever the track's file descriptor is readable, assuming the | |
296 | * buffer has not reached the maximum allowed occupancy. | |
1674096e | 297 | */ |
f5a03f58 | 298 | static int speaker_fill(struct track *t) { |
460b9539 | 299 | size_t where, left; |
b50cfb8a | 300 | int n, rc; |
460b9539 | 301 | |
6d2d327c RK |
302 | D(("fill %s: eof=%d used=%zu", |
303 | t->id, t->eof, t->used)); | |
b50cfb8a RK |
304 | if(t->eof) |
305 | return -1; | |
6d2d327c | 306 | if(t->used < sizeof t->buffer) { |
460b9539 | 307 | /* there is room left in the buffer */ |
6d2d327c RK |
308 | where = (t->start + t->used) % sizeof t->buffer; |
309 | /* Get as much data as we can */ | |
b50cfb8a RK |
310 | if(where >= t->start) |
311 | left = (sizeof t->buffer) - where; | |
312 | else | |
313 | left = t->start - where; | |
314 | pthread_mutex_unlock(&lock); | |
460b9539 | 315 | do { |
316 | n = read(t->fd, t->buffer + where, left); | |
317 | } while(n < 0 && errno == EINTR); | |
b50cfb8a | 318 | pthread_mutex_lock(&lock); |
de38ab9b RK |
319 | if(n < 0 && errno == EAGAIN) { |
320 | /* EAGAIN means more later */ | |
b50cfb8a | 321 | rc = 0; |
de38ab9b RK |
322 | } else if(n <= 0) { |
323 | /* n=0 means EOF. n<0 means some error occurred. We log the error but | |
324 | * otherwise treat it as identical to EOF. */ | |
325 | if(n < 0) | |
326 | disorder_error(errno, "error reading sample stream for %s", t->id); | |
327 | else | |
328 | D(("fill %s: eof detected", t->id)); | |
460b9539 | 329 | t->eof = 1; |
b50cfb8a RK |
330 | /* A track always becomes playable at EOF; we're not going to see any |
331 | * more data. */ | |
f5a03f58 | 332 | t->playable = 1; |
b50cfb8a RK |
333 | rc = -1; |
334 | } else { | |
335 | t->used += n; | |
336 | /* A track becomes playable when it (first) fills its buffer. For | |
337 | * 44.1KHz 16-bit stereo this is ~6s of audio data. The latency will | |
338 | * depend how long that takes to decode (hopefuly not very!) */ | |
339 | if(t->used == sizeof t->buffer) | |
340 | t->playable = 1; | |
341 | rc = 0; | |
460b9539 | 342 | } |
9efa107e RK |
343 | } else |
344 | rc = 0; | |
b50cfb8a | 345 | return rc; |
460b9539 | 346 | } |
347 | ||
dac25ef9 RK |
348 | /** @brief Return nonzero if we want to play some audio |
349 | * | |
350 | * We want to play audio if there is a current track; and it is not paused; and | |
351 | * it is playable according to the rules for @ref track::playable. | |
888b8031 RK |
352 | * |
353 | * We don't allow tracks to be paused if we've already told the server we've | |
354 | * finished them; that would cause such tracks to survive much longer than the | |
355 | * few samples they're supposed to, with report() remaining silent for the | |
356 | * duration. | |
dac25ef9 RK |
357 | */ |
358 | static int playable(void) { | |
359 | return playing | |
888b8031 | 360 | && (!paused || playing->finished) |
dac25ef9 RK |
361 | && playing->playable; |
362 | } | |
363 | ||
b50cfb8a | 364 | /** @brief Notify the server what we're up to */ |
460b9539 | 365 | static void report(void) { |
366 | struct speaker_message sm; | |
367 | ||
6d2d327c | 368 | if(playing) { |
888b8031 RK |
369 | /* Had better not send a report for a track that the server thinks has |
370 | * finished, that would be confusing. */ | |
371 | if(playing->finished) | |
372 | return; | |
460b9539 | 373 | memset(&sm, 0, sizeof sm); |
374 | sm.type = paused ? SM_PAUSED : SM_PLAYING; | |
375 | strcpy(sm.id, playing->id); | |
b50cfb8a | 376 | sm.data = playing->played / (uaudio_rate * uaudio_channels); |
84aa9f93 | 377 | speaker_send(1, &sm); |
4265e5d3 | 378 | xtime(&last_report); |
460b9539 | 379 | } |
460b9539 | 380 | } |
381 | ||
b50cfb8a RK |
382 | /** @brief Add a file descriptor to the set to poll() for |
383 | * @param fd File descriptor | |
384 | * @param events Events to wait for e.g. @c POLLIN | |
385 | * @return Slot number | |
386 | */ | |
387 | static int addfd(int fd, int events) { | |
460b9539 | 388 | if(fdno < NFDS) { |
389 | fds[fdno].fd = fd; | |
390 | fds[fdno].events = events; | |
391 | return fdno++; | |
392 | } else | |
393 | return -1; | |
394 | } | |
395 | ||
b50cfb8a RK |
396 | /** @brief Callback to return some sampled data |
397 | * @param buffer Where to put sample data | |
398 | * @param max_samples How many samples to return | |
399 | * @param userdata User data | |
400 | * @return Number of samples written | |
401 | * | |
402 | * See uaudio_callback(). | |
403 | */ | |
404 | static size_t speaker_callback(void *buffer, | |
405 | size_t max_samples, | |
406 | void attribute((unused)) *userdata) { | |
407 | const size_t max_bytes = max_samples * uaudio_sample_size; | |
408 | size_t provided_samples = 0; | |
409 | ||
410 | pthread_mutex_lock(&lock); | |
411 | /* TODO perhaps we should immediately go silent if we've been asked to pause | |
412 | * or cancel the playing track (maybe block in the cancel case and see what | |
413 | * else turns up?) */ | |
414 | if(playing) { | |
415 | if(playing->used > 0) { | |
416 | size_t bytes; | |
417 | /* Compute size of largest contiguous chunk. We get called as often as | |
418 | * necessary so there's no need for cleverness here. */ | |
419 | if(playing->start + playing->used > sizeof playing->buffer) | |
420 | bytes = sizeof playing->buffer - playing->start; | |
421 | else | |
422 | bytes = playing->used; | |
423 | /* Limit to what we were asked for */ | |
424 | if(bytes > max_bytes) | |
425 | bytes = max_bytes; | |
426 | /* Provide it */ | |
427 | memcpy(buffer, playing->buffer + playing->start, bytes); | |
428 | playing->start += bytes; | |
429 | playing->used -= bytes; | |
430 | /* Wrap around to start of buffer */ | |
431 | if(playing->start == sizeof playing->buffer) | |
432 | playing->start = 0; | |
433 | /* See if we've reached the end of the track */ | |
784744a8 RK |
434 | if(playing->used == 0 && playing->eof) { |
435 | int ignored = write(sigpipe[1], "", 1); | |
436 | (void) ignored; | |
437 | } | |
b50cfb8a RK |
438 | provided_samples = bytes / uaudio_sample_size; |
439 | playing->played += provided_samples; | |
440 | } | |
441 | } | |
442 | /* If we couldn't provide anything at all, play dead air */ | |
443 | /* TODO maybe it would be better to block, in some cases? */ | |
444 | if(!provided_samples) { | |
445 | memset(buffer, 0, max_bytes); | |
446 | provided_samples = max_samples; | |
89156c8c | 447 | if(playing) |
2e9ba080 RK |
448 | disorder_info("%zu samples silence, playing->used=%zu", |
449 | provided_samples, playing->used); | |
89156c8c | 450 | else |
2e9ba080 | 451 | disorder_info("%zu samples silence, playing=NULL", provided_samples); |
b50cfb8a RK |
452 | } |
453 | pthread_mutex_unlock(&lock); | |
454 | return provided_samples; | |
455 | } | |
572d74ba | 456 | |
5a7c42a8 | 457 | /** @brief Main event loop */ |
55f35f2d | 458 | static void mainloop(void) { |
572d74ba | 459 | struct track *t; |
460 | struct speaker_message sm; | |
b50cfb8a | 461 | int n, fd, stdin_slot, timeout, listen_slot, sigpipe_slot; |
460b9539 | 462 | |
b50cfb8a | 463 | /* Keep going while our parent process is alive */ |
89156c8c | 464 | pthread_mutex_lock(&lock); |
460b9539 | 465 | while(getppid() != 1) { |
b50cfb8a RK |
466 | int force_report = 0; |
467 | ||
460b9539 | 468 | fdno = 0; |
888b8031 RK |
469 | /* By default we will wait up to half a second before thinking about |
470 | * current state. */ | |
471 | timeout = 500; | |
460b9539 | 472 | /* Always ready for commands from the main server. */ |
473 | stdin_slot = addfd(0, POLLIN); | |
84aa9f93 RK |
474 | /* Also always ready for inbound connections */ |
475 | listen_slot = addfd(listenfd, POLLIN); | |
460b9539 | 476 | /* Try to read sample data for the currently playing track if there is |
477 | * buffer space. */ | |
84aa9f93 RK |
478 | if(playing |
479 | && playing->fd >= 0 | |
480 | && !playing->eof | |
481 | && playing->used < (sizeof playing->buffer)) | |
460b9539 | 482 | playing->slot = addfd(playing->fd, POLLIN); |
5a7c42a8 | 483 | else if(playing) |
460b9539 | 484 | playing->slot = -1; |
460b9539 | 485 | /* If any other tracks don't have a full buffer, try to read sample data |
5a7c42a8 | 486 | * from them. We do this last of all, so that if we run out of slots, |
487 | * nothing important can't be monitored. */ | |
460b9539 | 488 | for(t = tracks; t; t = t->next) |
489 | if(t != playing) { | |
84aa9f93 RK |
490 | if(t->fd >= 0 |
491 | && !t->eof | |
492 | && t->used < sizeof t->buffer) { | |
9d5da576 | 493 | t->slot = addfd(t->fd, POLLIN | POLLHUP); |
460b9539 | 494 | } else |
495 | t->slot = -1; | |
496 | } | |
89156c8c | 497 | sigpipe_slot = addfd(sigpipe[0], POLLIN); |
e83d0967 | 498 | /* Wait for something interesting to happen */ |
89156c8c | 499 | pthread_mutex_unlock(&lock); |
e83d0967 | 500 | n = poll(fds, fdno, timeout); |
89156c8c | 501 | pthread_mutex_lock(&lock); |
460b9539 | 502 | if(n < 0) { |
503 | if(errno == EINTR) continue; | |
2e9ba080 | 504 | disorder_fatal(errno, "error calling poll"); |
460b9539 | 505 | } |
84aa9f93 RK |
506 | /* Perhaps a connection has arrived */ |
507 | if(fds[listen_slot].revents & POLLIN) { | |
508 | struct sockaddr_un addr; | |
509 | socklen_t addrlen = sizeof addr; | |
510 | uint32_t l; | |
511 | char id[24]; | |
512 | ||
dc450d30 | 513 | if((fd = accept(listenfd, (struct sockaddr *)&addr, &addrlen)) >= 0) { |
937be4c0 | 514 | blocking(fd); |
84aa9f93 | 515 | if(read(fd, &l, sizeof l) < 4) { |
2e9ba080 | 516 | disorder_error(errno, "reading length from inbound connection"); |
84aa9f93 RK |
517 | xclose(fd); |
518 | } else if(l >= sizeof id) { | |
2e9ba080 | 519 | disorder_error(0, "id length too long"); |
84aa9f93 RK |
520 | xclose(fd); |
521 | } else if(read(fd, id, l) < (ssize_t)l) { | |
2e9ba080 | 522 | disorder_error(errno, "reading id from inbound connection"); |
84aa9f93 RK |
523 | xclose(fd); |
524 | } else { | |
525 | id[l] = 0; | |
526 | D(("id %s fd %d", id, fd)); | |
527 | t = findtrack(id, 1/*create*/); | |
918393ff | 528 | if (write(fd, "", 1) < 0) /* write an ack */ |
de38ab9b RK |
529 | disorder_error(errno, "writing ack to inbound connection for %s", |
530 | id); | |
84aa9f93 | 531 | if(t->fd != -1) { |
2e9ba080 | 532 | disorder_error(0, "%s: already got a connection", id); |
84aa9f93 RK |
533 | xclose(fd); |
534 | } else { | |
535 | nonblock(fd); | |
536 | t->fd = fd; /* yay */ | |
537 | } | |
2a1c84fb RK |
538 | /* Notify the server that the connection arrived */ |
539 | sm.type = SM_ARRIVED; | |
540 | strcpy(sm.id, id); | |
541 | speaker_send(1, &sm); | |
84aa9f93 RK |
542 | } |
543 | } else | |
2e9ba080 | 544 | disorder_error(errno, "accept"); |
84aa9f93 | 545 | } |
460b9539 | 546 | /* Perhaps we have a command to process */ |
547 | if(fds[stdin_slot].revents & POLLIN) { | |
5a7c42a8 | 548 | /* There might (in theory) be several commands queued up, but in general |
549 | * this won't be the case, so we don't bother looping around to pick them | |
550 | * all up. */ | |
84aa9f93 | 551 | n = speaker_recv(0, &sm); |
460b9539 | 552 | if(n > 0) |
888b8031 RK |
553 | /* As a rule we don't send success replies to most commands - we just |
554 | * force the regular status update to be sent immediately rather than | |
555 | * on schedule. */ | |
460b9539 | 556 | switch(sm.type) { |
460b9539 | 557 | case SM_PLAY: |
888b8031 RK |
558 | /* SM_PLAY is only allowed if the server reasonably believes that |
559 | * nothing is playing */ | |
560 | if(playing) { | |
561 | /* If finished isn't set then the server can't believe that this | |
562 | * track has finished */ | |
563 | if(!playing->finished) | |
2e9ba080 | 564 | disorder_fatal(0, "got SM_PLAY but already playing something"); |
888b8031 RK |
565 | /* If pending_playing is set then the server must believe that that |
566 | * is playing */ | |
567 | if(pending_playing) | |
2e9ba080 | 568 | disorder_fatal(0, "got SM_PLAY but have a pending playing track"); |
888b8031 | 569 | } |
460b9539 | 570 | t = findtrack(sm.id, 1); |
84aa9f93 RK |
571 | D(("SM_PLAY %s fd %d", t->id, t->fd)); |
572 | if(t->fd == -1) | |
2e9ba080 RK |
573 | disorder_error(0, |
574 | "cannot play track because no connection arrived"); | |
5bac5b78 RK |
575 | /* TODO as things stand we often report this error message but then |
576 | * appear to proceed successfully. Understanding why requires a look | |
577 | * at play.c: we call prepare() which makes the connection in a child | |
578 | * process, and then sends the SM_PLAY in the parent process. The | |
579 | * latter may well be faster. As it happens this is harmless; we'll | |
580 | * just sit around sending silence until the decoder connects and | |
581 | * starts sending some sample data. But is is annoying and ought to | |
582 | * be fixed. */ | |
888b8031 RK |
583 | pending_playing = t; |
584 | /* If nothing is currently playing then we'll switch to the pending | |
585 | * track below so there's no point distinguishing the situations | |
586 | * here. */ | |
460b9539 | 587 | break; |
588 | case SM_PAUSE: | |
589 | D(("SM_PAUSE")); | |
590 | paused = 1; | |
b50cfb8a | 591 | force_report = 1; |
460b9539 | 592 | break; |
593 | case SM_RESUME: | |
594 | D(("SM_RESUME")); | |
b50cfb8a RK |
595 | paused = 0; |
596 | force_report = 1; | |
460b9539 | 597 | break; |
598 | case SM_CANCEL: | |
819f5988 | 599 | D(("SM_CANCEL %s", sm.id)); |
460b9539 | 600 | t = removetrack(sm.id); |
601 | if(t) { | |
888b8031 RK |
602 | if(t == playing || t == pending_playing) { |
603 | /* Scratching the track that the server believes is playing, | |
604 | * which might either be the actual playing track or a pending | |
605 | * playing track */ | |
460b9539 | 606 | sm.type = SM_FINISHED; |
888b8031 RK |
607 | if(t == playing) |
608 | playing = 0; | |
609 | else | |
610 | pending_playing = 0; | |
819f5988 RK |
611 | } else { |
612 | /* Could be scratching the playing track before it's quite got | |
613 | * going, or could be just removing a track from the queue. We | |
614 | * log more because there's been a bug here recently than because | |
615 | * it's particularly interesting; the log message will be removed | |
616 | * if no further problems show up. */ | |
2e9ba080 | 617 | disorder_info("SM_CANCEL for nonplaying track %s", sm.id); |
819f5988 | 618 | sm.type = SM_STILLBORN; |
460b9539 | 619 | } |
819f5988 | 620 | strcpy(sm.id, t->id); |
460b9539 | 621 | destroy(t); |
2b2a5fed | 622 | } else { |
819f5988 RK |
623 | /* Probably scratching the playing track well before it's got |
624 | * going, but could indicate a bug, so we log this as an error. */ | |
2b2a5fed | 625 | sm.type = SM_UNKNOWN; |
2e9ba080 | 626 | disorder_error(0, "SM_CANCEL for unknown track %s", sm.id); |
2b2a5fed | 627 | } |
819f5988 | 628 | speaker_send(1, &sm); |
b50cfb8a | 629 | force_report = 1; |
460b9539 | 630 | break; |
631 | case SM_RELOAD: | |
632 | D(("SM_RELOAD")); | |
02ba7921 | 633 | if(config_read(1, NULL)) |
2e9ba080 RK |
634 | disorder_error(0, "cannot read configuration"); |
635 | disorder_info("reloaded configuration"); | |
460b9539 | 636 | break; |
637 | default: | |
2e9ba080 | 638 | disorder_error(0, "unknown message type %d", sm.type); |
460b9539 | 639 | } |
640 | } | |
641 | /* Read in any buffered data */ | |
642 | for(t = tracks; t; t = t->next) | |
84aa9f93 RK |
643 | if(t->fd != -1 |
644 | && t->slot != -1 | |
645 | && (fds[t->slot].revents & (POLLIN | POLLHUP))) | |
f5a03f58 | 646 | speaker_fill(t); |
b50cfb8a RK |
647 | /* Drain the signal pipe. We don't care about its contents, merely that it |
648 | * interrupted poll(). */ | |
649 | if(fds[sigpipe_slot].revents & POLLIN) { | |
650 | char buffer[64]; | |
784744a8 | 651 | int ignored; (void)ignored; |
b50cfb8a | 652 | |
784744a8 | 653 | ignored = read(sigpipe[0], buffer, sizeof buffer); |
b50cfb8a | 654 | } |
888b8031 RK |
655 | /* Send SM_FINISHED when we're near the end of the track. |
656 | * | |
657 | * This is how we implement gapless play; we hope that the SM_PLAY from the | |
658 | * server arrives before the remaining bytes of the track play out. | |
659 | */ | |
660 | if(playing | |
661 | && playing->eof | |
662 | && !playing->finished | |
663 | && playing->used <= early_finish) { | |
b50cfb8a RK |
664 | memset(&sm, 0, sizeof sm); |
665 | sm.type = SM_FINISHED; | |
666 | strcpy(sm.id, playing->id); | |
667 | speaker_send(1, &sm); | |
888b8031 RK |
668 | playing->finished = 1; |
669 | } | |
670 | /* When the track is actually finished, deconfigure it */ | |
671 | if(playing && playing->eof && !playing->used) { | |
888b8031 | 672 | removetrack(playing->id); |
b50cfb8a RK |
673 | destroy(playing); |
674 | playing = 0; | |
888b8031 RK |
675 | } |
676 | /* Act on the pending SM_PLAY */ | |
9c55e9e4 | 677 | if(!playing && pending_playing) { |
888b8031 RK |
678 | playing = pending_playing; |
679 | pending_playing = 0; | |
888b8031 | 680 | force_report = 1; |
b50cfb8a RK |
681 | } |
682 | /* Impose any state change required by the above */ | |
683 | if(playable()) { | |
684 | if(!activated) { | |
685 | activated = 1; | |
89156c8c | 686 | pthread_mutex_unlock(&lock); |
b50cfb8a | 687 | backend->activate(); |
89156c8c | 688 | pthread_mutex_lock(&lock); |
b50cfb8a RK |
689 | } |
690 | } else { | |
691 | if(activated) { | |
692 | activated = 0; | |
89156c8c | 693 | pthread_mutex_unlock(&lock); |
b50cfb8a | 694 | backend->deactivate(); |
89156c8c | 695 | pthread_mutex_lock(&lock); |
b50cfb8a RK |
696 | } |
697 | } | |
698 | /* If we've not reported our state for a second do so now. */ | |
4265e5d3 | 699 | if(force_report || xtime(0) > last_report) |
460b9539 | 700 | report(); |
701 | } | |
55f35f2d | 702 | } |
703 | ||
704 | int main(int argc, char **argv) { | |
0ca6d097 | 705 | int n, logsyslog = !isatty(2); |
84aa9f93 RK |
706 | struct sockaddr_un addr; |
707 | static const int one = 1; | |
937be4c0 | 708 | struct speaker_message sm; |
38b8221f | 709 | const char *d; |
85cb23d7 | 710 | char *dir; |
b50cfb8a | 711 | struct rlimit rl[1]; |
55f35f2d | 712 | |
713 | set_progname(argv); | |
2e9ba080 | 714 | if(!setlocale(LC_CTYPE, "")) disorder_fatal(errno, "error calling setlocale"); |
0ca6d097 | 715 | while((n = getopt_long(argc, argv, "hVc:dDSs", options, 0)) >= 0) { |
55f35f2d | 716 | switch(n) { |
717 | case 'h': help(); | |
3fbdc96d | 718 | case 'V': version("disorder-speaker"); |
55f35f2d | 719 | case 'c': configfile = optarg; break; |
720 | case 'd': debugging = 1; break; | |
721 | case 'D': debugging = 0; break; | |
0ca6d097 RK |
722 | case 'S': logsyslog = 0; break; |
723 | case 's': logsyslog = 1; break; | |
2e9ba080 | 724 | default: disorder_fatal(0, "invalid option"); |
55f35f2d | 725 | } |
726 | } | |
38b8221f | 727 | if((d = getenv("DISORDER_DEBUG_SPEAKER"))) debugging = atoi(d); |
0ca6d097 | 728 | if(logsyslog) { |
55f35f2d | 729 | openlog(progname, LOG_PID, LOG_DAEMON); |
730 | log_default = &log_syslog; | |
731 | } | |
b50cfb8a | 732 | config_uaudio_apis = uaudio_apis; |
2e9ba080 | 733 | if(config_read(1, NULL)) disorder_fatal(0, "cannot read configuration"); |
55f35f2d | 734 | /* ignore SIGPIPE */ |
735 | signal(SIGPIPE, SIG_IGN); | |
55f35f2d | 736 | /* set nice value */ |
737 | xnice(config->nice_speaker); | |
738 | /* change user */ | |
739 | become_mortal(); | |
740 | /* make sure we're not root, whatever the config says */ | |
b50cfb8a | 741 | if(getuid() == 0 || geteuid() == 0) |
2e9ba080 | 742 | disorder_fatal(0, "do not run as root"); |
b50cfb8a RK |
743 | /* Make sure we can't have more than NFDS files open (it would bust our |
744 | * poll() array) */ | |
745 | if(getrlimit(RLIMIT_NOFILE, rl) < 0) | |
2e9ba080 | 746 | disorder_fatal(errno, "getrlimit RLIMIT_NOFILE"); |
b50cfb8a RK |
747 | if(rl->rlim_cur > NFDS) { |
748 | rl->rlim_cur = NFDS; | |
749 | if(setrlimit(RLIMIT_NOFILE, rl) < 0) | |
2e9ba080 | 750 | disorder_fatal(errno, "setrlimit to reduce RLIMIT_NOFILE to %lu", |
b50cfb8a | 751 | (unsigned long)rl->rlim_cur); |
2e9ba080 | 752 | disorder_info("set RLIM_NOFILE to %lu", (unsigned long)rl->rlim_cur); |
b50cfb8a | 753 | } else |
2e9ba080 | 754 | disorder_info("RLIM_NOFILE is %lu", (unsigned long)rl->rlim_cur); |
5bf7c546 RK |
755 | /* gcrypt initialization */ |
756 | if(!gcry_check_version(NULL)) | |
757 | disorder_fatal(0, "gcry_check_version failed"); | |
758 | gcry_control(GCRYCTL_INIT_SECMEM, 0); | |
759 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | |
b50cfb8a RK |
760 | /* create a pipe between the backend callback and the poll() loop */ |
761 | xpipe(sigpipe); | |
762 | nonblock(sigpipe[0]); | |
763 | /* set up audio backend */ | |
764 | uaudio_set_format(config->sample_format.rate, | |
765 | config->sample_format.channels, | |
766 | config->sample_format.bits, | |
767 | config->sample_format.bits != 8); | |
888b8031 | 768 | early_finish = uaudio_sample_size * uaudio_channels * uaudio_rate; |
b50cfb8a RK |
769 | /* TODO other parameters! */ |
770 | backend = uaudio_find(config->api); | |
55f35f2d | 771 | /* backend-specific initialization */ |
ba70caca RK |
772 | if(backend->configure) |
773 | backend->configure(); | |
b50cfb8a | 774 | backend->start(speaker_callback, NULL); |
85cb23d7 RK |
775 | /* create the socket directory */ |
776 | byte_xasprintf(&dir, "%s/speaker", config->home); | |
777 | unlink(dir); /* might be a leftover socket */ | |
a5f3ca1e | 778 | if(mkdir(dir, 0700) < 0 && errno != EEXIST) |
2e9ba080 | 779 | disorder_fatal(errno, "error creating %s", dir); |
84aa9f93 RK |
780 | /* set up the listen socket */ |
781 | listenfd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
782 | memset(&addr, 0, sizeof addr); | |
783 | addr.sun_family = AF_UNIX; | |
85cb23d7 | 784 | snprintf(addr.sun_path, sizeof addr.sun_path, "%s/speaker/socket", |
84aa9f93 RK |
785 | config->home); |
786 | if(unlink(addr.sun_path) < 0 && errno != ENOENT) | |
2e9ba080 | 787 | disorder_error(errno, "removing %s", addr.sun_path); |
84aa9f93 | 788 | xsetsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); |
dc450d30 | 789 | if(bind(listenfd, (const struct sockaddr *)&addr, sizeof addr) < 0) |
2e9ba080 | 790 | disorder_fatal(errno, "error binding socket to %s", addr.sun_path); |
84aa9f93 RK |
791 | xlisten(listenfd, 128); |
792 | nonblock(listenfd); | |
2e9ba080 | 793 | disorder_info("listening on %s", addr.sun_path); |
937be4c0 RK |
794 | memset(&sm, 0, sizeof sm); |
795 | sm.type = SM_READY; | |
796 | speaker_send(1, &sm); | |
55f35f2d | 797 | mainloop(); |
2e9ba080 | 798 | disorder_info("stopped (parent terminated)"); |
460b9539 | 799 | exit(0); |
800 | } | |
801 | ||
802 | /* | |
803 | Local Variables: | |
804 | c-basic-offset:2 | |
805 | comment-column:40 | |
806 | fill-column:79 | |
807 | indent-tabs-mode:nil | |
808 | End: | |
809 | */ |