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