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