Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
16fb2830 | 3 | * Copyright (C) 2004-2009 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
460b9539 | 8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
460b9539 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
460b9539 | 17 | */ |
132a5a4a RK |
18 | /** @file server/play.c |
19 | * @brief Playing tracks | |
5bac5b78 RK |
20 | * |
21 | * This file is rather badly organized. Sorry. It's better than it was... | |
132a5a4a | 22 | */ |
460b9539 | 23 | |
05b75f8d | 24 | #include "disorder-server.h" |
460b9539 | 25 | #include <ao/ao.h> |
460b9539 | 26 | |
27 | #define SPEAKER "disorder-speaker" | |
28 | ||
16fb2830 | 29 | /** @brief The current playing track or NULL */ |
460b9539 | 30 | struct queue_entry *playing; |
16fb2830 RK |
31 | |
32 | /** @brief Set when paused */ | |
460b9539 | 33 | int paused; |
34 | ||
35 | static void finished(ev_source *ev); | |
5bac5b78 RK |
36 | static int start_child(struct queue_entry *q, |
37 | const struct pbgc_params *params, | |
38 | void attribute((unused)) *bgdata); | |
39 | static int prepare_child(struct queue_entry *q, | |
40 | const struct pbgc_params *params, | |
41 | void attribute((unused)) *bgdata); | |
4857234e | 42 | static void ensure_next_scratch(ev_source *ev); |
460b9539 | 43 | |
16fb2830 | 44 | /** @brief File descriptor of our end of the socket to the speaker */ |
460b9539 | 45 | static int speaker_fd = -1; |
16fb2830 | 46 | |
16fb2830 | 47 | /** @brief Set when shutting down */ |
460b9539 | 48 | static int shutting_down; |
49 | ||
5bac5b78 RK |
50 | /* Speaker ------------------------------------------------------------------ */ |
51 | ||
16fb2830 RK |
52 | /** @brief Called when speaker process terminates |
53 | * | |
54 | * Currently kills of DisOrder completely. A future version could terminate | |
55 | * the speaker when nothing was going on, or recover from failures, though any | |
56 | * tracks with decoders already started would need to have them restarted. | |
57 | */ | |
460b9539 | 58 | static int speaker_terminated(ev_source attribute((unused)) *ev, |
59 | pid_t attribute((unused)) pid, | |
60 | int attribute((unused)) status, | |
61 | const struct rusage attribute((unused)) *rusage, | |
62 | void attribute((unused)) *u) { | |
2e9ba080 | 63 | disorder_fatal(0, "speaker subprocess %s", wstat(status)); |
460b9539 | 64 | } |
65 | ||
16fb2830 | 66 | /** @brief Called when we get a message from the speaker process */ |
460b9539 | 67 | static int speaker_readable(ev_source *ev, int fd, |
68 | void attribute((unused)) *u) { | |
69 | struct speaker_message sm; | |
84aa9f93 | 70 | int ret = speaker_recv(fd, &sm); |
460b9539 | 71 | |
72 | if(ret < 0) return 0; /* EAGAIN */ | |
73 | if(!ret) { /* EOF */ | |
74 | ev_fd_cancel(ev, ev_read, fd); | |
75 | return 0; | |
76 | } | |
77 | switch(sm.type) { | |
78 | case SM_PAUSED: | |
79 | /* track ID is paused, DATA seconds played */ | |
80 | D(("SM_PAUSED %s %ld", sm.id, sm.data)); | |
81 | playing->sofar = sm.data; | |
82 | break; | |
819f5988 RK |
83 | case SM_FINISHED: /* scratched the playing track */ |
84 | case SM_STILLBORN: /* scratched too early */ | |
85 | case SM_UNKNOWN: /* scratched WAY too early */ | |
f3102c22 RK |
86 | if(playing && !strcmp(sm.id, playing->id)) { |
87 | if((playing->state == playing_unplayed | |
88 | || playing->state == playing_started) | |
89 | && sm.type == SM_FINISHED) | |
90 | playing->state = playing_ok; | |
2b2a5fed | 91 | finished(ev); |
f3102c22 | 92 | } |
2b2a5fed | 93 | break; |
460b9539 | 94 | case SM_PLAYING: |
95 | /* track ID is playing, DATA seconds played */ | |
96 | D(("SM_PLAYING %s %ld", sm.id, sm.data)); | |
97 | playing->sofar = sm.data; | |
98 | break; | |
2a1c84fb RK |
99 | case SM_ARRIVED: { |
100 | /* track ID is now prepared */ | |
101 | struct queue_entry *q; | |
102 | for(q = qhead.next; q != &qhead && strcmp(q->id, sm.id); q = q->next) | |
103 | ; | |
104 | if(q && q->preparing) { | |
105 | q->preparing = 0; | |
106 | q->prepared = 1; | |
107 | /* We might be waiting to play the now-prepared track */ | |
108 | play(ev); | |
109 | } | |
110 | break; | |
111 | } | |
460b9539 | 112 | default: |
2e9ba080 | 113 | disorder_error(0, "unknown speaker message type %d", sm.type); |
460b9539 | 114 | } |
115 | return 0; | |
116 | } | |
117 | ||
16fb2830 | 118 | /** @brief Initialize the speaker process */ |
460b9539 | 119 | void speaker_setup(ev_source *ev) { |
4387f694 | 120 | int sp[2]; |
460b9539 | 121 | pid_t pid; |
937be4c0 | 122 | struct speaker_message sm; |
460b9539 | 123 | |
124 | if(socketpair(PF_UNIX, SOCK_DGRAM, 0, sp) < 0) | |
2e9ba080 | 125 | disorder_fatal(errno, "error calling socketpair"); |
460b9539 | 126 | if(!(pid = xfork())) { |
127 | exitfn = _exit; | |
128 | ev_signal_atfork(ev); | |
129 | xdup2(sp[0], 0); | |
130 | xdup2(sp[0], 1); | |
131 | xclose(sp[0]); | |
132 | xclose(sp[1]); | |
460b9539 | 133 | signal(SIGPIPE, SIG_DFL); |
134 | #if 0 | |
135 | execlp("valgrind", "valgrind", SPEAKER, "--config", configfile, | |
0ca6d097 RK |
136 | debugging ? "--debug" : "--no-debug", |
137 | log_default == &log_syslog ? "--syslog" : "--no-syslog", | |
138 | (char *)0); | |
460b9539 | 139 | #else |
140 | execlp(SPEAKER, SPEAKER, "--config", configfile, | |
0ca6d097 RK |
141 | debugging ? "--debug" : "--no-debug", |
142 | log_default == &log_syslog ? "--syslog" : "--no-syslog", | |
143 | (char *)0); | |
460b9539 | 144 | #endif |
2e9ba080 | 145 | disorder_fatal(errno, "error invoking %s", SPEAKER); |
460b9539 | 146 | } |
147 | ev_child(ev, pid, 0, speaker_terminated, 0); | |
148 | speaker_fd = sp[1]; | |
149 | xclose(sp[0]); | |
150 | cloexec(speaker_fd); | |
937be4c0 RK |
151 | /* Wait for the speaker to be ready */ |
152 | speaker_recv(speaker_fd, &sm); | |
153 | nonblock(speaker_fd); | |
31e2a93e | 154 | if(ev_fd(ev, ev_read, speaker_fd, speaker_readable, 0, "speaker read") < 0) |
2e9ba080 | 155 | disorder_fatal(0, "error registering speaker socket fd"); |
460b9539 | 156 | } |
157 | ||
16fb2830 | 158 | /** @brief Tell the speaker to reload its configuration */ |
460b9539 | 159 | void speaker_reload(void) { |
160 | struct speaker_message sm; | |
161 | ||
162 | memset(&sm, 0, sizeof sm); | |
163 | sm.type = SM_RELOAD; | |
84aa9f93 | 164 | speaker_send(speaker_fd, &sm); |
460b9539 | 165 | } |
166 | ||
5bac5b78 RK |
167 | /* Track termination -------------------------------------------------------- */ |
168 | ||
16fb2830 RK |
169 | /** @brief Called when the currently playing track finishes playing |
170 | * @param ev Event loop or NULL | |
171 | * | |
172 | * There are three places this is called from: | |
173 | * | |
174 | * 1) speaker_readable(), when the speaker tells us the playing track finished. | |
175 | * (Technically the speaker lies a little to arrange for gapless play.) | |
176 | * | |
177 | * 2) player_finished(), when the player for a non-raw track (i.e. one that | |
178 | * does not use the speaker) finishes. | |
179 | * | |
180 | * 3) quitting(), after signalling the decoder or player but possible before it | |
181 | * has actually terminated. In this case @p ev is NULL, inhibiting any further | |
182 | * attempt to play anything. | |
183 | */ | |
460b9539 | 184 | static void finished(ev_source *ev) { |
185 | D(("finished playing=%p", (void *)playing)); | |
186 | if(!playing) | |
187 | return; | |
188 | if(playing->state != playing_scratched) | |
189 | notify_not_scratched(playing->track, playing->submitter); | |
190 | switch(playing->state) { | |
191 | case playing_ok: | |
192 | eventlog("completed", playing->track, (char *)0); | |
193 | break; | |
194 | case playing_scratched: | |
195 | eventlog("scratched", playing->track, playing->scratched, (char *)0); | |
196 | break; | |
197 | case playing_failed: | |
198 | eventlog("failed", playing->track, wstat(playing->wstat), (char *)0); | |
199 | break; | |
200 | default: | |
201 | break; | |
202 | } | |
203 | queue_played(playing); | |
204 | recent_write(); | |
460b9539 | 205 | playing = 0; |
49a773eb | 206 | /* Try to play something else */ |
49a773eb RK |
207 | if(ev) |
208 | play(ev); | |
460b9539 | 209 | } |
210 | ||
16fb2830 RK |
211 | /** @brief Called when a player or decoder process terminates |
212 | * | |
213 | * This is called when a decoder process terminates (which might actually be | |
214 | * some time before the speaker reports it as finished) or when a non-raw | |
215 | * (i.e. non-speaker) player terminates. In the latter case it's imaginable | |
216 | * that the OS has buffered the last few samples. | |
4857234e RK |
217 | * |
218 | * NB. The finished track might NOT be in the queue (yet) - it might be a | |
219 | * pre-chosen scratch. | |
16fb2830 | 220 | */ |
460b9539 | 221 | static int player_finished(ev_source *ev, |
222 | pid_t pid, | |
223 | int status, | |
224 | const struct rusage attribute((unused)) *rusage, | |
225 | void *u) { | |
226 | struct queue_entry *q = u; | |
227 | ||
228 | D(("player_finished pid=%lu status=%#x", | |
229 | (unsigned long)pid, (unsigned)status)); | |
230 | /* Record that this PID is dead. If we killed the track we might know this | |
231 | * already, but also it might have exited or crashed. Either way we don't | |
232 | * want to end up signalling it. */ | |
90b8faa5 | 233 | q->pid = -1; |
460b9539 | 234 | switch(q->state) { |
235 | case playing_unplayed: | |
236 | case playing_random: | |
84aa9f93 | 237 | /* If this was a pre-prepared track then either it failed or we |
16fb2830 RK |
238 | * deliberately stopped it: it might have been removed from the queue, or |
239 | * moved down the queue, or the speaker might be on a break. So we leave | |
240 | * it state alone for future use. | |
241 | */ | |
460b9539 | 242 | break; |
243 | default: | |
244 | /* We actually started playing this track. */ | |
245 | if(status) { | |
16fb2830 | 246 | /* Don't override 'scratched' with 'failed'. */ |
460b9539 | 247 | if(q->state != playing_scratched) |
248 | q->state = playing_failed; | |
249 | } else | |
250 | q->state = playing_ok; | |
251 | break; | |
252 | } | |
253 | /* Regardless we always report and record the status and do cleanup for | |
254 | * prefork calls. */ | |
255 | if(status) | |
2e9ba080 | 256 | disorder_error(0, "player for %s %s", q->track, wstat(status)); |
460b9539 | 257 | if(q->type & DISORDER_PLAYER_PREFORK) |
258 | play_cleanup(q->pl, q->data); | |
259 | q->wstat = status; | |
260 | /* If this actually was the current track, and does not use the speaker | |
261 | * process, then it must have finished. For raw-output players we will get a | |
262 | * separate notification from the speaker process. */ | |
263 | if(q == playing | |
264 | && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) | |
265 | finished(ev); | |
266 | return 0; | |
267 | } | |
268 | ||
5bac5b78 RK |
269 | /* Track initiation --------------------------------------------------------- */ |
270 | ||
16fb2830 | 271 | /** @brief Find the player for @p q */ |
5bac5b78 | 272 | static const struct stringlist *find_player(const struct queue_entry *q) { |
460b9539 | 273 | int n; |
274 | ||
275 | for(n = 0; n < config->player.n; ++n) | |
276 | if(fnmatch(config->player.s[n].s[0], q->track, 0) == 0) | |
277 | break; | |
278 | if(n >= config->player.n) | |
5bac5b78 | 279 | return NULL; |
460b9539 | 280 | else |
5bac5b78 | 281 | return &config->player.s[n]; |
460b9539 | 282 | } |
283 | ||
5bac5b78 | 284 | /** @brief Start to play @p q |
84aa9f93 RK |
285 | * @param ev Event loop |
286 | * @param q Track to play/prepare | |
3149c1e2 | 287 | * @return @ref START_OK, @ref START_HARDFAIL or @ref START_SOFTFAIL |
16fb2830 | 288 | * |
5bac5b78 | 289 | * This makes @p actually start playing. It calls prepare() if necessary and |
c0f52b2c | 290 | * either sends an @ref SM_PLAY command or invokes the player itself in a |
5bac5b78 | 291 | * subprocess. |
16fb2830 | 292 | * |
5bac5b78 RK |
293 | * It's up to the caller to set @ref playing and @c playing->state (this might |
294 | * be changed in the future). | |
84aa9f93 | 295 | */ |
460b9539 | 296 | static int start(ev_source *ev, |
5bac5b78 RK |
297 | struct queue_entry *q) { |
298 | const struct stringlist *player; | |
299 | int rc; | |
460b9539 | 300 | |
5bac5b78 | 301 | D(("start %s", q->id)); |
460b9539 | 302 | /* Find the player plugin. */ |
8141805c | 303 | if(!(player = find_player(q))) |
5bac5b78 RK |
304 | return START_HARDFAIL; /* No player */ |
305 | if(!(q->pl = open_plugin(player->s[1], 0))) | |
460b9539 | 306 | return START_HARDFAIL; |
307 | q->type = play_get_type(q->pl); | |
5bac5b78 RK |
308 | /* Special handling for raw-format players */ |
309 | if((q->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) { | |
310 | /* Make sure that the track is prepared */ | |
311 | if((rc = prepare(ev, q))) | |
312 | return rc; | |
313 | /* Now we're sure it's prepared, start it playing */ | |
314 | /* TODO actually it might not be fully prepared yet - it's all happening in | |
315 | * a subprocess. See speaker.c for further discussion. */ | |
316 | struct speaker_message sm[1]; | |
317 | memset(sm, 0, sizeof sm); | |
318 | strcpy(sm->id, q->id); | |
319 | sm->type = SM_PLAY; | |
320 | speaker_send(speaker_fd, sm); | |
321 | D(("sent SM_PLAY for %s", sm->id)); | |
322 | /* Our caller will set playing and playing->state = playing_started */ | |
460b9539 | 323 | return START_OK; |
5bac5b78 RK |
324 | } else { |
325 | rc = play_background(ev, player, q, start_child, NULL); | |
326 | if(rc == START_OK) | |
327 | ev_child(ev, q->pid, 0, player_finished, q); | |
328 | /* Our caller will set playing and playing->state = playing_started */ | |
329 | return rc; | |
460b9539 | 330 | } |
5bac5b78 RK |
331 | } |
332 | ||
6ac9a215 RK |
333 | /** @brief Child-process half of start() |
334 | * @return Process exit code | |
335 | * | |
336 | * Called in subprocess to execute non-raw-format players (via plugin). | |
337 | */ | |
5bac5b78 RK |
338 | static int start_child(struct queue_entry *q, |
339 | const struct pbgc_params *params, | |
340 | void attribute((unused)) *bgdata) { | |
341 | int n; | |
342 | ||
343 | /* Wait for a device to clear. This ugliness is now deprecated and will | |
344 | * eventually be removed. */ | |
345 | if(params->waitdevice) { | |
346 | ao_initialize(); | |
347 | if(*params->waitdevice) { | |
348 | n = ao_driver_id(params->waitdevice); | |
349 | if(n == -1) | |
2e9ba080 | 350 | disorder_fatal(0, "invalid libao driver: %s", params->waitdevice); |
5bac5b78 RK |
351 | } else |
352 | n = ao_default_driver_id(); | |
353 | /* Make up a format. */ | |
354 | ao_sample_format format; | |
355 | memset(&format, 0, sizeof format); | |
356 | format.bits = 8; | |
357 | format.rate = 44100; | |
358 | format.channels = 1; | |
359 | format.byte_format = AO_FMT_NATIVE; | |
360 | int retries = 20; | |
361 | struct timespec ts; | |
362 | ts.tv_sec = 0; | |
363 | ts.tv_nsec = 100000000; /* 0.1s */ | |
364 | ao_device *device; | |
365 | while((device = ao_open_live(n, &format, 0)) == 0 && retries-- > 0) | |
366 | nanosleep(&ts, 0); | |
367 | if(device) | |
368 | ao_close(device); | |
460b9539 | 369 | } |
5bac5b78 RK |
370 | /* Play the track */ |
371 | play_track(q->pl, | |
372 | params->argv, params->argc, | |
373 | params->rawpath, | |
374 | q->track); | |
375 | return 0; | |
460b9539 | 376 | } |
377 | ||
16fb2830 | 378 | /** @brief Prepare a track for later play |
5bac5b78 | 379 | * @return @ref START_OK, @ref START_HARDFAIL or @ref START_SOFTFAIL |
16fb2830 | 380 | * |
6ac9a215 RK |
381 | * This can be called either when we want to play the track or slightly before |
382 | * so that some samples are decoded and available in a buffer. | |
383 | * | |
5bac5b78 RK |
384 | * Only applies to raw-format (i.e. speaker-using) players; everything else |
385 | * gets @c START_OK. | |
16fb2830 | 386 | */ |
460b9539 | 387 | int prepare(ev_source *ev, |
388 | struct queue_entry *q) { | |
5bac5b78 | 389 | const struct stringlist *player; |
460b9539 | 390 | |
90b8faa5 RK |
391 | /* If there's a decoder (or player!) going we do nothing */ |
392 | if(q->pid >= 0) | |
5bac5b78 RK |
393 | return START_OK; |
394 | /* If the track is already prepared, do nothing */ | |
2a1c84fb | 395 | if(q->prepared || q->preparing) |
5bac5b78 | 396 | return START_OK; |
460b9539 | 397 | /* Find the player plugin */ |
8141805c | 398 | if(!(player = find_player(q))) |
5bac5b78 RK |
399 | return START_HARDFAIL; /* No player */ |
400 | q->pl = open_plugin(player->s[1], 0); | |
460b9539 | 401 | q->type = play_get_type(q->pl); |
402 | if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) | |
5bac5b78 | 403 | return START_OK; /* Not a raw player */ |
2a1c84fb | 404 | int rc = play_background(ev, player, q, prepare_child, NULL); |
5bac5b78 RK |
405 | if(rc == START_OK) { |
406 | ev_child(ev, q->pid, 0, player_finished, q); | |
2a1c84fb RK |
407 | q->preparing = 1; |
408 | /* Actually the track is still "in flight" */ | |
409 | rc = START_SOFTFAIL; | |
5bac5b78 RK |
410 | } |
411 | return rc; | |
412 | } | |
413 | ||
6ac9a215 RK |
414 | /** @brief Child-process half of prepare() |
415 | * @return Process exit code | |
416 | * | |
417 | * Called in subprocess to execute the decoder for a raw-format player. | |
418 | * | |
419 | * @todo We currently run the normalizer from here in a double-fork. This is | |
420 | * unsatisfactory for many reasons: we can't prevent it outliving the main | |
421 | * server and we don't adequately report its exit status. | |
422 | */ | |
5bac5b78 RK |
423 | static int prepare_child(struct queue_entry *q, |
424 | const struct pbgc_params *params, | |
425 | void attribute((unused)) *bgdata) { | |
426 | /* np will be the pipe to disorder-normalize */ | |
427 | int np[2]; | |
428 | if(socketpair(PF_UNIX, SOCK_STREAM, 0, np) < 0) | |
2e9ba080 | 429 | disorder_fatal(errno, "error calling socketpair"); |
5bac5b78 RK |
430 | /* Beware of the Leopard! On OS X 10.5.x, the order of the shutdown |
431 | * calls here DOES MATTER. If you do the SHUT_WR first then the SHUT_RD | |
432 | * fails with "Socket is not connected". I think this is a bug but | |
433 | * provided implementors either don't care about the order or all agree | |
434 | * about the order, choosing the reliable order is an adequate | |
435 | * workaround. */ | |
436 | xshutdown(np[1], SHUT_RD); /* decoder writes to np[1] */ | |
437 | xshutdown(np[0], SHUT_WR); /* normalize reads from np[0] */ | |
438 | blocking(np[0]); | |
439 | blocking(np[1]); | |
440 | /* Start disorder-normalize. We double-fork so that nothing has to wait | |
441 | * for disorder-normalize. */ | |
442 | pid_t npid; | |
443 | if(!(npid = xfork())) { | |
444 | /* Grandchild of disorderd */ | |
445 | if(!xfork()) { | |
446 | /* Great-grandchild of disorderd */ | |
447 | /* Connect to the speaker process */ | |
448 | struct sockaddr_un addr; | |
449 | memset(&addr, 0, sizeof addr); | |
450 | addr.sun_family = AF_UNIX; | |
451 | snprintf(addr.sun_path, sizeof addr.sun_path, | |
452 | "%s/speaker/socket", config->home); | |
453 | int sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); | |
454 | if(connect(sfd, (const struct sockaddr *)&addr, sizeof addr) < 0) | |
2e9ba080 | 455 | disorder_fatal(errno, "connecting to %s", addr.sun_path); |
5bac5b78 RK |
456 | /* Send the ID, with a NATIVE-ENDIAN 32 bit length */ |
457 | uint32_t l = strlen(q->id); | |
458 | if(write(sfd, &l, sizeof l) < 0 | |
459 | || write(sfd, q->id, l) < 0) | |
2e9ba080 | 460 | disorder_fatal(errno, "writing to %s", addr.sun_path); |
5bac5b78 RK |
461 | /* Await the ack */ |
462 | if (read(sfd, &l, 1) < 0) | |
2e9ba080 | 463 | disorder_fatal(errno, "reading ack from %s", addr.sun_path); |
5bac5b78 RK |
464 | /* Plumbing */ |
465 | xdup2(np[0], 0); | |
466 | xdup2(sfd, 1); | |
467 | xclose(np[0]); | |
468 | xclose(np[1]); | |
469 | xclose(sfd); | |
470 | /* TODO stderr shouldn't be redirected for disorder-normalize | |
471 | * (but it should be for play_track() */ | |
472 | execlp("disorder-normalize", "disorder-normalize", | |
473 | log_default == &log_syslog ? "--syslog" : "--no-syslog", | |
474 | "--config", configfile, | |
475 | (char *)0); | |
2e9ba080 | 476 | disorder_fatal(errno, "executing disorder-normalize"); |
5bac5b78 RK |
477 | /* End of the great-grandchild of disorderd */ |
478 | } | |
479 | /* Back in the grandchild of disorderd */ | |
480 | _exit(0); | |
481 | /* End of the grandchild of disorderd */ | |
482 | } | |
483 | /* Back in the child of disorderd */ | |
484 | /* Wait for the grandchild of disordered to finish */ | |
485 | int n; | |
486 | while(waitpid(npid, &n, 0) < 0 && errno == EINTR) | |
487 | ; | |
488 | /* Pass the file descriptor to the driver in an environment | |
489 | * variable. */ | |
490 | char buffer[64]; | |
491 | snprintf(buffer, sizeof buffer, "DISORDER_RAW_FD=%d", np[1]); | |
492 | if(putenv(buffer) < 0) | |
2e9ba080 | 493 | disorder_fatal(errno, "error calling putenv"); |
5bac5b78 RK |
494 | /* Close all the FDs we don't need */ |
495 | xclose(np[0]); | |
496 | /* Start the decoder itself */ | |
497 | play_track(q->pl, | |
498 | params->argv, params->argc, | |
499 | params->rawpath, | |
500 | q->track); | |
501 | return 0; | |
460b9539 | 502 | } |
503 | ||
16fb2830 RK |
504 | /** @brief Abandon a queue entry |
505 | * | |
506 | * Called from c_remove() (but NOT when scratching a track). Only does | |
507 | * anything to raw-format tracks. Terminates the background decoder and tells | |
508 | * the speaker process to cancel the track. | |
509 | */ | |
460b9539 | 510 | void abandon(ev_source attribute((unused)) *ev, |
511 | struct queue_entry *q) { | |
512 | struct speaker_message sm; | |
460b9539 | 513 | |
90b8faa5 RK |
514 | if(q->pid < 0) |
515 | return; /* Not prepared. */ | |
460b9539 | 516 | if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) |
517 | return; /* Not a raw player. */ | |
518 | /* Terminate the player. */ | |
90b8faa5 | 519 | kill(-q->pid, config->signal); |
460b9539 | 520 | /* Cancel the track. */ |
521 | memset(&sm, 0, sizeof sm); | |
522 | sm.type = SM_CANCEL; | |
523 | strcpy(sm.id, q->id); | |
84aa9f93 | 524 | speaker_send(speaker_fd, &sm); |
460b9539 | 525 | } |
526 | ||
5bac5b78 RK |
527 | /* Random tracks ------------------------------------------------------------ */ |
528 | ||
49a773eb | 529 | /** @brief Called with a new random track |
59cf25c4 | 530 | * @param ev Event loop |
49a773eb RK |
531 | * @param track Track name |
532 | */ | |
533 | static void chosen_random_track(ev_source *ev, | |
534 | const char *track) { | |
535 | struct queue_entry *q; | |
536 | ||
537 | if(!track) | |
538 | return; | |
539 | /* Add the track to the queue */ | |
7a853280 | 540 | q = queue_add(track, 0, WHERE_END, NULL, origin_random); |
49a773eb RK |
541 | D(("picked %p (%s) at random", (void *)q, q->track)); |
542 | queue_write(); | |
543 | /* Maybe a track can now be played */ | |
544 | play(ev); | |
545 | } | |
546 | ||
547 | /** @brief Maybe add a randomly chosen track | |
548 | * @param ev Event loop | |
16fb2830 RK |
549 | * |
550 | * Picking can take some time so the track will only be added after this | |
551 | * function has returned. | |
49a773eb RK |
552 | */ |
553 | void add_random_track(ev_source *ev) { | |
460b9539 | 554 | struct queue_entry *q; |
459d4402 | 555 | long qlen = 0; |
460b9539 | 556 | |
557 | /* If random play is not enabled then do nothing. */ | |
558 | if(shutting_down || !random_is_enabled()) | |
49a773eb | 559 | return; |
459d4402 | 560 | /* Count how big the queue is */ |
460b9539 | 561 | for(q = qhead.next; q != &qhead; q = q->next) |
459d4402 | 562 | ++qlen; |
49a773eb RK |
563 | /* If it's smaller than the desired size then add a track */ |
564 | if(qlen < config->queue_pad) | |
565 | trackdb_request_random(ev, chosen_random_track); | |
460b9539 | 566 | } |
567 | ||
5bac5b78 RK |
568 | /* Track initiation (part 2) ------------------------------------------------ */ |
569 | ||
16fb2830 RK |
570 | /** @brief Attempt to play something |
571 | * | |
572 | * This is called from numerous locations - whenever it might conceivably have | |
573 | * become possible to play something. | |
574 | */ | |
460b9539 | 575 | void play(ev_source *ev) { |
576 | struct queue_entry *q; | |
577 | int random_enabled = random_is_enabled(); | |
578 | ||
579 | D(("play playing=%p", (void *)playing)); | |
16fb2830 RK |
580 | /* If we're shutting down, or there's something playing, or playing is not |
581 | * enabled, give up now */ | |
460b9539 | 582 | if(shutting_down || playing || !playing_is_enabled()) return; |
49a773eb | 583 | /* See if there's anything to play */ |
460b9539 | 584 | if(qhead.next == &qhead) { |
49a773eb RK |
585 | /* Queue is empty. We could just wait around since there are periodic |
586 | * attempts to add a random track anyway. However they are rarer than | |
587 | * attempts to force a track so we initiate one now. */ | |
588 | add_random_track(ev); | |
16fb2830 RK |
589 | /* chosen_random_track() will call play() when a new random track has been |
590 | * added to the queue. */ | |
49a773eb | 591 | return; |
460b9539 | 592 | } |
49a773eb | 593 | /* There must be at least one track in the queue. */ |
460b9539 | 594 | q = qhead.next; |
2dc2f478 RK |
595 | /* If random play is disabled but the track is a non-adopted random one |
596 | * then don't play it. play() will be called again when random play is | |
597 | * re-enabled. */ | |
598 | if(!random_enabled && q->origin == origin_random) | |
460b9539 | 599 | return; |
600 | D(("taken %p (%s) from queue", (void *)q, q->track)); | |
601 | /* Try to start playing. */ | |
5bac5b78 | 602 | switch(start(ev, q)) { |
460b9539 | 603 | case START_HARDFAIL: |
604 | if(q == qhead.next) { | |
605 | queue_remove(q, 0); /* Abandon this track. */ | |
606 | queue_played(q); | |
607 | recent_write(); | |
608 | } | |
49a773eb RK |
609 | /* Oh well, try the next one */ |
610 | play(ev); | |
460b9539 | 611 | break; |
612 | case START_SOFTFAIL: | |
49a773eb | 613 | /* We'll try the same track again shortly. */ |
460b9539 | 614 | break; |
615 | case START_OK: | |
16fb2830 | 616 | /* Remove from the queue */ |
460b9539 | 617 | if(q == qhead.next) { |
618 | queue_remove(q, 0); | |
619 | queue_write(); | |
620 | } | |
16fb2830 | 621 | /* It's become the playing track */ |
460b9539 | 622 | playing = q; |
4265e5d3 | 623 | xtime(&playing->played); |
460b9539 | 624 | playing->state = playing_started; |
625 | notify_play(playing->track, playing->submitter); | |
626 | eventlog("playing", playing->track, | |
627 | playing->submitter ? playing->submitter : (const char *)0, | |
628 | (const char *)0); | |
629 | /* Maybe add a random track. */ | |
49a773eb | 630 | add_random_track(ev); |
460b9539 | 631 | /* If there is another track in the queue prepare it now. This could |
632 | * potentially be a just-added random track. */ | |
633 | if(qhead.next != &qhead) | |
634 | prepare(ev, qhead.next); | |
4857234e RK |
635 | /* Make sure there is a prepared scratch */ |
636 | ensure_next_scratch(ev); | |
460b9539 | 637 | break; |
638 | } | |
639 | } | |
640 | ||
5bac5b78 RK |
641 | /* Miscelleneous ------------------------------------------------------------ */ |
642 | ||
9439cdab RK |
643 | int flag_enabled(const char *s) { |
644 | return !s || !strcmp(s, "yes"); | |
645 | } | |
646 | ||
16fb2830 | 647 | /** @brief Return true if play is enabled */ |
460b9539 | 648 | int playing_is_enabled(void) { |
9439cdab | 649 | return flag_enabled(trackdb_get_global("playing")); |
460b9539 | 650 | } |
651 | ||
16fb2830 | 652 | /** @brief Enable play */ |
460b9539 | 653 | void enable_playing(const char *who, ev_source *ev) { |
654 | trackdb_set_global("playing", "yes", who); | |
655 | /* Add a random track if necessary. */ | |
49a773eb | 656 | add_random_track(ev); |
460b9539 | 657 | play(ev); |
658 | } | |
659 | ||
16fb2830 | 660 | /** @brief Disable play */ |
9439cdab | 661 | void disable_playing(const char *who, ev_source attribute((unused)) *ev) { |
460b9539 | 662 | trackdb_set_global("playing", "no", who); |
663 | } | |
664 | ||
16fb2830 | 665 | /** @brief Return true if random play is enabled */ |
460b9539 | 666 | int random_is_enabled(void) { |
9439cdab | 667 | return flag_enabled(trackdb_get_global("random-play")); |
460b9539 | 668 | } |
669 | ||
16fb2830 | 670 | /** @brief Enable random play */ |
460b9539 | 671 | void enable_random(const char *who, ev_source *ev) { |
672 | trackdb_set_global("random-play", "yes", who); | |
49a773eb | 673 | add_random_track(ev); |
460b9539 | 674 | play(ev); |
675 | } | |
676 | ||
16fb2830 | 677 | /** @brief Disable random play */ |
9439cdab | 678 | void disable_random(const char *who, ev_source attribute((unused)) *ev) { |
460b9539 | 679 | trackdb_set_global("random-play", "no", who); |
680 | } | |
681 | ||
5bac5b78 RK |
682 | /* Scratching --------------------------------------------------------------- */ |
683 | ||
4857234e RK |
684 | /** @brief Track to play next time something is scratched */ |
685 | static struct queue_entry *next_scratch; | |
686 | ||
687 | /** @brief Ensure there isa prepared scratch */ | |
688 | static void ensure_next_scratch(ev_source *ev) { | |
689 | if(next_scratch) /* There's one already */ | |
690 | return; | |
691 | if(!config->scratch.n) /* There are no scratches */ | |
692 | return; | |
693 | int r = rand() * (double)config->scratch.n / (RAND_MAX + 1.0); | |
694 | next_scratch = queue_add(config->scratch.s[r], NULL, | |
695 | WHERE_NOWHERE, NULL, origin_scratch); | |
696 | if(ev) | |
697 | prepare(ev, next_scratch); | |
698 | } | |
699 | ||
16fb2830 | 700 | /** @brief Scratch a track |
c0f52b2c RK |
701 | * @param who User responsible (or NULL) |
702 | * @param id Track ID (or NULL for current) | |
16fb2830 | 703 | */ |
460b9539 | 704 | void scratch(const char *who, const char *id) { |
460b9539 | 705 | struct speaker_message sm; |
460b9539 | 706 | |
707 | D(("scratch playing=%p state=%d id=%s playing->id=%s", | |
708 | (void *)playing, | |
709 | playing ? playing->state : 0, | |
710 | id ? id : "(none)", | |
711 | playing ? playing->id : "(none)")); | |
16fb2830 RK |
712 | /* There must be a playing track; it must be in a scratchable state; if a |
713 | * specific ID was mentioned it must be that track. */ | |
460b9539 | 714 | if(playing |
715 | && (playing->state == playing_started | |
716 | || playing->state == playing_paused) | |
717 | && (!id | |
718 | || !strcmp(id, playing->id))) { | |
16fb2830 | 719 | /* Update state (for the benefit of the 'recent' list) */ |
460b9539 | 720 | playing->state = playing_scratched; |
721 | playing->scratched = who ? xstrdup(who) : 0; | |
16fb2830 | 722 | /* Find the player and kill the whole process group */ |
90b8faa5 RK |
723 | if(playing->pid >= 0) { |
724 | D(("kill -%d -%lu", config->signal, (unsigned long)playing->pid)); | |
725 | kill(-playing->pid, config->signal); | |
16fb2830 RK |
726 | } |
727 | /* Tell the speaker, if we think it'll care */ | |
460b9539 | 728 | if((playing->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) { |
729 | memset(&sm, 0, sizeof sm); | |
730 | sm.type = SM_CANCEL; | |
731 | strcpy(sm.id, playing->id); | |
84aa9f93 | 732 | speaker_send(speaker_fd, &sm); |
460b9539 | 733 | D(("sending SM_CANCEL for %s", playing->id)); |
734 | } | |
62416f14 RK |
735 | /* If playing is enabled then add a scratch to the queue. Having a scratch |
736 | * appear in the queue when further play is disabled is weird and | |
737 | * contradicts implicit assumptions made elsewhere, so we try to avoid | |
738 | * it. */ | |
739 | if(playing_is_enabled()) { | |
740 | /* Try to make sure there is a scratch */ | |
741 | ensure_next_scratch(NULL); | |
742 | /* Insert it at the head of the queue */ | |
743 | if(next_scratch){ | |
744 | next_scratch->submitter = who; | |
745 | queue_insert_entry(&qhead, next_scratch); | |
746 | eventlog_raw("queue", queue_marshall(next_scratch), (const char *)0); | |
747 | next_scratch = NULL; | |
748 | } | |
460b9539 | 749 | } |
750 | notify_scratch(playing->track, playing->submitter, who, | |
4265e5d3 | 751 | xtime(0) - playing->played); |
460b9539 | 752 | } |
753 | } | |
754 | ||
5bac5b78 RK |
755 | /* Server termination ------------------------------------------------------- */ |
756 | ||
757 | /** @brief Called from quit() to tear down everything belonging to this file */ | |
460b9539 | 758 | void quitting(ev_source *ev) { |
759 | struct queue_entry *q; | |
460b9539 | 760 | |
761 | /* Don't start anything new */ | |
762 | shutting_down = 1; | |
763 | /* Shut down the current player */ | |
764 | if(playing) { | |
90b8faa5 RK |
765 | if(playing->pid >= 0) |
766 | kill(-playing->pid, config->signal); | |
460b9539 | 767 | playing->state = playing_quitting; |
768 | finished(0); | |
769 | } | |
90b8faa5 | 770 | /* Zap any background decoders that are going */ |
460b9539 | 771 | for(q = qhead.next; q != &qhead; q = q->next) |
90b8faa5 RK |
772 | if(q->pid >= 0) { |
773 | D(("kill -%d %lu", config->signal, (unsigned long)q->pid)); | |
774 | kill(-q->pid, config->signal); | |
16fb2830 | 775 | } |
460b9539 | 776 | /* Don't need the speaker any more */ |
777 | ev_fd_cancel(ev, ev_read, speaker_fd); | |
778 | xclose(speaker_fd); | |
779 | } | |
780 | ||
5bac5b78 RK |
781 | /* Pause and resume --------------------------------------------------------- */ |
782 | ||
16fb2830 | 783 | /** @brief Pause the playing track */ |
460b9539 | 784 | int pause_playing(const char *who) { |
785 | struct speaker_message sm; | |
786 | long played; | |
787 | ||
788 | /* Can't pause if already paused or if nothing playing. */ | |
789 | if(!playing || paused) return 0; | |
790 | switch(playing->type & DISORDER_PLAYER_TYPEMASK) { | |
791 | case DISORDER_PLAYER_STANDALONE: | |
792 | if(!(playing->type & DISORDER_PLAYER_PAUSES)) { | |
793 | default: | |
2e9ba080 | 794 | disorder_error(0, "cannot pause because player is not powerful enough"); |
460b9539 | 795 | return -1; |
796 | } | |
797 | if(play_pause(playing->pl, &played, playing->data)) { | |
2e9ba080 | 798 | disorder_error(0, "player indicates it cannot pause"); |
460b9539 | 799 | return -1; |
800 | } | |
4265e5d3 | 801 | xtime(&playing->lastpaused); |
460b9539 | 802 | playing->uptopause = played; |
803 | playing->lastresumed = 0; | |
804 | break; | |
805 | case DISORDER_PLAYER_RAW: | |
806 | memset(&sm, 0, sizeof sm); | |
807 | sm.type = SM_PAUSE; | |
84aa9f93 | 808 | speaker_send(speaker_fd, &sm); |
460b9539 | 809 | break; |
810 | } | |
2e9ba080 RK |
811 | if(who) |
812 | disorder_info("paused by %s", who); | |
460b9539 | 813 | notify_pause(playing->track, who); |
814 | paused = 1; | |
815 | if(playing->state == playing_started) | |
816 | playing->state = playing_paused; | |
817 | eventlog("state", "pause", (char *)0); | |
818 | return 0; | |
819 | } | |
820 | ||
16fb2830 | 821 | /** @brief Resume playing after a pause */ |
460b9539 | 822 | void resume_playing(const char *who) { |
823 | struct speaker_message sm; | |
824 | ||
825 | if(!paused) return; | |
826 | paused = 0; | |
827 | if(!playing) return; | |
828 | switch(playing->type & DISORDER_PLAYER_TYPEMASK) { | |
829 | case DISORDER_PLAYER_STANDALONE: | |
c458dd3d | 830 | if(!(playing->type & DISORDER_PLAYER_PAUSES)) { |
460b9539 | 831 | default: |
832 | /* Shouldn't happen */ | |
833 | return; | |
834 | } | |
835 | play_resume(playing->pl, playing->data); | |
4265e5d3 | 836 | xtime(&playing->lastresumed); |
460b9539 | 837 | break; |
838 | case DISORDER_PLAYER_RAW: | |
839 | memset(&sm, 0, sizeof sm); | |
840 | sm.type = SM_RESUME; | |
84aa9f93 | 841 | speaker_send(speaker_fd, &sm); |
460b9539 | 842 | break; |
843 | } | |
2e9ba080 | 844 | if(who) disorder_info("resumed by %s", who); |
460b9539 | 845 | notify_resume(playing->track, who); |
846 | if(playing->state == playing_paused) | |
847 | playing->state = playing_started; | |
848 | eventlog("state", "resume", (char *)0); | |
849 | } | |
850 | ||
851 | /* | |
852 | Local Variables: | |
853 | c-basic-offset:2 | |
854 | comment-column:40 | |
855 | fill-column:79 | |
16fb2830 | 856 | indent-tabs-mode:nil |
460b9539 | 857 | End: |
858 | */ |