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