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