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