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