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