Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
5aff007d | 3 | * Copyright (C) 2004-2008 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
6d2d327c | 22 | #include "types.h" |
460b9539 | 23 | |
24 | #include <sys/types.h> | |
25 | #include <sys/time.h> | |
26 | #include <unistd.h> | |
27 | #include <errno.h> | |
28 | #include <fnmatch.h> | |
29 | #include <time.h> | |
30 | #include <signal.h> | |
31 | #include <stdlib.h> | |
32 | #include <assert.h> | |
33 | #include <sys/socket.h> | |
34 | #include <string.h> | |
35 | #include <stdio.h> | |
36 | #include <pcre.h> | |
37 | #include <ao/ao.h> | |
6d2d327c | 38 | #include <sys/wait.h> |
84aa9f93 | 39 | #include <sys/un.h> |
460b9539 | 40 | |
41 | #include "event.h" | |
42 | #include "log.h" | |
43 | #include "mem.h" | |
44 | #include "configuration.h" | |
45 | #include "queue.h" | |
f08c0859 | 46 | #include "server-queue.h" |
5df73aeb | 47 | #include "rights.h" |
460b9539 | 48 | #include "trackdb.h" |
49 | #include "play.h" | |
50 | #include "plugin.h" | |
51 | #include "wstat.h" | |
52 | #include "eventlog.h" | |
53 | #include "logfd.h" | |
54 | #include "syscalls.h" | |
ea410ba1 | 55 | #include "speaker-protocol.h" |
460b9539 | 56 | #include "disorder.h" |
57 | #include "signame.h" | |
58 | #include "hash.h" | |
59 | ||
60 | #define SPEAKER "disorder-speaker" | |
61 | ||
62 | struct queue_entry *playing; | |
63 | int paused; | |
64 | ||
65 | static void finished(ev_source *ev); | |
66 | ||
67 | static int speaker_fd = -1; | |
68 | static hash *player_pids; | |
69 | static int shutting_down; | |
70 | ||
71 | static void store_player_pid(const char *id, pid_t pid) { | |
72 | if(!player_pids) player_pids = hash_new(sizeof (pid_t)); | |
73 | hash_add(player_pids, id, &pid, HASH_INSERT_OR_REPLACE); | |
74 | } | |
75 | ||
76 | static pid_t find_player_pid(const char *id) { | |
77 | pid_t *pidp; | |
78 | ||
79 | if(player_pids && (pidp = hash_find(player_pids, id))) return *pidp; | |
80 | return -1; | |
81 | } | |
82 | ||
83 | static void forget_player_pid(const char *id) { | |
84 | if(player_pids) hash_remove(player_pids, id); | |
85 | } | |
86 | ||
87 | /* called when speaker process terminates */ | |
88 | static int speaker_terminated(ev_source attribute((unused)) *ev, | |
89 | pid_t attribute((unused)) pid, | |
90 | int attribute((unused)) status, | |
91 | const struct rusage attribute((unused)) *rusage, | |
92 | void attribute((unused)) *u) { | |
0213d16c | 93 | fatal(0, "speaker subprocess %s", |
2a6c7b79 | 94 | wstat(status)); |
460b9539 | 95 | } |
96 | ||
97 | /* called when speaker process has something to say */ | |
98 | static int speaker_readable(ev_source *ev, int fd, | |
99 | void attribute((unused)) *u) { | |
100 | struct speaker_message sm; | |
84aa9f93 | 101 | int ret = speaker_recv(fd, &sm); |
460b9539 | 102 | |
103 | if(ret < 0) return 0; /* EAGAIN */ | |
104 | if(!ret) { /* EOF */ | |
105 | ev_fd_cancel(ev, ev_read, fd); | |
106 | return 0; | |
107 | } | |
108 | switch(sm.type) { | |
109 | case SM_PAUSED: | |
110 | /* track ID is paused, DATA seconds played */ | |
111 | D(("SM_PAUSED %s %ld", sm.id, sm.data)); | |
112 | playing->sofar = sm.data; | |
113 | break; | |
819f5988 RK |
114 | case SM_FINISHED: /* scratched the playing track */ |
115 | case SM_STILLBORN: /* scratched too early */ | |
116 | case SM_UNKNOWN: /* scratched WAY too early */ | |
2b2a5fed RK |
117 | if(playing && !strcmp(sm.id, playing->id)) |
118 | finished(ev); | |
119 | break; | |
460b9539 | 120 | case SM_PLAYING: |
121 | /* track ID is playing, DATA seconds played */ | |
122 | D(("SM_PLAYING %s %ld", sm.id, sm.data)); | |
123 | playing->sofar = sm.data; | |
124 | break; | |
125 | default: | |
126 | error(0, "unknown message type %d", sm.type); | |
127 | } | |
128 | return 0; | |
129 | } | |
130 | ||
131 | void speaker_setup(ev_source *ev) { | |
4387f694 | 132 | int sp[2]; |
460b9539 | 133 | pid_t pid; |
937be4c0 | 134 | struct speaker_message sm; |
460b9539 | 135 | |
136 | if(socketpair(PF_UNIX, SOCK_DGRAM, 0, sp) < 0) | |
137 | fatal(errno, "error calling socketpair"); | |
460b9539 | 138 | if(!(pid = xfork())) { |
139 | exitfn = _exit; | |
140 | ev_signal_atfork(ev); | |
141 | xdup2(sp[0], 0); | |
142 | xdup2(sp[0], 1); | |
143 | xclose(sp[0]); | |
144 | xclose(sp[1]); | |
460b9539 | 145 | signal(SIGPIPE, SIG_DFL); |
146 | #if 0 | |
147 | execlp("valgrind", "valgrind", SPEAKER, "--config", configfile, | |
0ca6d097 RK |
148 | debugging ? "--debug" : "--no-debug", |
149 | log_default == &log_syslog ? "--syslog" : "--no-syslog", | |
150 | (char *)0); | |
460b9539 | 151 | #else |
152 | execlp(SPEAKER, SPEAKER, "--config", configfile, | |
0ca6d097 RK |
153 | debugging ? "--debug" : "--no-debug", |
154 | log_default == &log_syslog ? "--syslog" : "--no-syslog", | |
155 | (char *)0); | |
460b9539 | 156 | #endif |
157 | fatal(errno, "error invoking %s", SPEAKER); | |
158 | } | |
159 | ev_child(ev, pid, 0, speaker_terminated, 0); | |
160 | speaker_fd = sp[1]; | |
161 | xclose(sp[0]); | |
162 | cloexec(speaker_fd); | |
937be4c0 RK |
163 | /* Wait for the speaker to be ready */ |
164 | speaker_recv(speaker_fd, &sm); | |
165 | nonblock(speaker_fd); | |
e8c92ba7 | 166 | ev_fd(ev, ev_read, speaker_fd, speaker_readable, 0, "speaker read"); |
460b9539 | 167 | } |
168 | ||
169 | void speaker_reload(void) { | |
170 | struct speaker_message sm; | |
171 | ||
172 | memset(&sm, 0, sizeof sm); | |
173 | sm.type = SM_RELOAD; | |
84aa9f93 | 174 | speaker_send(speaker_fd, &sm); |
460b9539 | 175 | } |
176 | ||
460b9539 | 177 | /* Called when the currently playing track finishes playing. This |
178 | * might be because the player finished or because the speaker process | |
179 | * told us so. */ | |
180 | static void finished(ev_source *ev) { | |
181 | D(("finished playing=%p", (void *)playing)); | |
182 | if(!playing) | |
183 | return; | |
184 | if(playing->state != playing_scratched) | |
185 | notify_not_scratched(playing->track, playing->submitter); | |
186 | switch(playing->state) { | |
187 | case playing_ok: | |
188 | eventlog("completed", playing->track, (char *)0); | |
189 | break; | |
190 | case playing_scratched: | |
191 | eventlog("scratched", playing->track, playing->scratched, (char *)0); | |
192 | break; | |
193 | case playing_failed: | |
194 | eventlog("failed", playing->track, wstat(playing->wstat), (char *)0); | |
195 | break; | |
196 | default: | |
197 | break; | |
198 | } | |
199 | queue_played(playing); | |
200 | recent_write(); | |
201 | forget_player_pid(playing->id); | |
202 | playing = 0; | |
49a773eb RK |
203 | /* Try to play something else */ |
204 | /* TODO re-support config->gap? */ | |
205 | if(ev) | |
206 | play(ev); | |
460b9539 | 207 | } |
208 | ||
209 | /* Called when a player terminates. */ | |
210 | static int player_finished(ev_source *ev, | |
211 | pid_t pid, | |
212 | int status, | |
213 | const struct rusage attribute((unused)) *rusage, | |
214 | void *u) { | |
215 | struct queue_entry *q = u; | |
216 | ||
217 | D(("player_finished pid=%lu status=%#x", | |
218 | (unsigned long)pid, (unsigned)status)); | |
219 | /* Record that this PID is dead. If we killed the track we might know this | |
220 | * already, but also it might have exited or crashed. Either way we don't | |
221 | * want to end up signalling it. */ | |
222 | if(pid == find_player_pid(q->id)) | |
223 | forget_player_pid(q->id); | |
224 | switch(q->state) { | |
225 | case playing_unplayed: | |
226 | case playing_random: | |
84aa9f93 RK |
227 | /* If this was a pre-prepared track then either it failed or we |
228 | * deliberately stopped it because it was removed from the queue or moved | |
229 | * down it. So leave it state alone for future use. */ | |
460b9539 | 230 | break; |
231 | default: | |
232 | /* We actually started playing this track. */ | |
233 | if(status) { | |
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) | |
243 | error(0, "player for %s %s", q->track, wstat(status)); | |
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 | ||
256 | /* Find the player for Q */ | |
257 | static int find_player(const struct queue_entry *q) { | |
258 | int n; | |
259 | ||
260 | for(n = 0; n < config->player.n; ++n) | |
261 | if(fnmatch(config->player.s[n].s[0], q->track, 0) == 0) | |
262 | break; | |
263 | if(n >= config->player.n) | |
264 | return -1; | |
265 | else | |
266 | return n; | |
267 | } | |
268 | ||
269 | /* Return values from start() */ | |
3149c1e2 RK |
270 | #define START_OK 0 /**< @brief Succeeded. */ |
271 | #define START_HARDFAIL 1 /**< @brief Track is broken. */ | |
272 | #define START_SOFTFAIL 2 /**< @brief Track OK, system (temporarily?) broken */ | |
460b9539 | 273 | |
84aa9f93 RK |
274 | /** @brief Play or prepare @p q |
275 | * @param ev Event loop | |
276 | * @param q Track to play/prepare | |
277 | * @param prepare_only If true, only prepares track | |
3149c1e2 | 278 | * @return @ref START_OK, @ref START_HARDFAIL or @ref START_SOFTFAIL |
84aa9f93 | 279 | */ |
460b9539 | 280 | static int start(ev_source *ev, |
281 | struct queue_entry *q, | |
84aa9f93 | 282 | int prepare_only) { |
460b9539 | 283 | int n, lfd; |
284 | const char *p; | |
84aa9f93 | 285 | int np[2], sfd; |
460b9539 | 286 | struct speaker_message sm; |
287 | char buffer[64]; | |
288 | int optc; | |
289 | ao_sample_format format; | |
290 | ao_device *device; | |
291 | int retries; | |
292 | struct timespec ts; | |
293 | const char *waitdevice = 0; | |
294 | const char *const *optv; | |
6d2d327c | 295 | pid_t pid, npid; |
84aa9f93 RK |
296 | struct sockaddr_un addr; |
297 | uint32_t l; | |
460b9539 | 298 | |
299 | memset(&sm, 0, sizeof sm); | |
84aa9f93 | 300 | D(("start %s %d", q->id, prepare_only)); |
66bb2e02 RK |
301 | if(q->prepared) { |
302 | /* The track is alraedy prepared */ | |
303 | if(!prepare_only) { | |
304 | /* We want to run it, since it's prepared the answer is to tell the | |
305 | * speaker to set it off */ | |
306 | strcpy(sm.id, q->id); | |
307 | sm.type = SM_PLAY; | |
308 | speaker_send(speaker_fd, &sm); | |
309 | D(("sent SM_PLAY for %s", sm.id)); | |
310 | } | |
460b9539 | 311 | return START_OK; |
312 | } | |
313 | /* Find the player plugin. */ | |
314 | if((n = find_player(q)) < 0) return START_HARDFAIL; | |
315 | if(!(q->pl = open_plugin(config->player.s[n].s[1], 0))) | |
316 | return START_HARDFAIL; | |
317 | q->type = play_get_type(q->pl); | |
318 | /* Can't prepare non-raw tracks. */ | |
84aa9f93 | 319 | if(prepare_only |
460b9539 | 320 | && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) |
321 | return START_OK; | |
322 | /* Call the prefork function. */ | |
323 | p = trackdb_rawpath(q->track); | |
324 | if(q->type & DISORDER_PLAYER_PREFORK) | |
325 | if(!(q->data = play_prefork(q->pl, p))) { | |
326 | error(0, "prefork function for %s failed", q->track); | |
327 | return START_HARDFAIL; | |
328 | } | |
329 | /* Use the second arg as the tag if available (it's probably a command name), | |
330 | * otherwise the module name. */ | |
e99d42b1 | 331 | if(!isatty(2)) |
332 | lfd = logfd(ev, (config->player.s[n].s[2] | |
333 | ? config->player.s[n].s[2] : config->player.s[n].s[1])); | |
334 | else | |
335 | lfd = -1; | |
460b9539 | 336 | optc = config->player.s[n].n - 2; |
337 | optv = (void *)&config->player.s[n].s[2]; | |
338 | while(optc > 0 && optv[0][0] == '-') { | |
339 | if(!strcmp(optv[0], "--")) { | |
340 | ++optv; | |
341 | --optc; | |
342 | break; | |
343 | } | |
344 | if(!strcmp(optv[0], "--wait-for-device") | |
345 | || !strncmp(optv[0], "--wait-for-device=", 18)) { | |
346 | if((waitdevice = strchr(optv[0], '='))) { | |
347 | ++waitdevice; | |
348 | } else | |
349 | waitdevice = ""; /* use default */ | |
350 | ++optv; | |
351 | --optc; | |
352 | } else { | |
353 | error(0, "unknown option %s", optv[0]); | |
354 | return START_HARDFAIL; | |
355 | } | |
356 | } | |
357 | switch(pid = fork()) { | |
358 | case 0: /* child */ | |
359 | exitfn = _exit; | |
ba3d1d36 | 360 | progname = "disorderd-fork"; |
460b9539 | 361 | ev_signal_atfork(ev); |
362 | signal(SIGPIPE, SIG_DFL); | |
e99d42b1 | 363 | if(lfd != -1) { |
364 | xdup2(lfd, 1); | |
365 | xdup2(lfd, 2); | |
366 | xclose(lfd); /* tidy up */ | |
367 | } | |
460b9539 | 368 | setpgid(0, 0); |
369 | if((q->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) { | |
84aa9f93 RK |
370 | /* "Raw" format players always have their output send down a pipe |
371 | * to the disorder-normalize process. This will connect to the | |
372 | * speaker process to actually play the audio data. | |
6d2d327c RK |
373 | */ |
374 | /* np will be the pipe to disorder-normalize */ | |
375 | if(socketpair(PF_UNIX, SOCK_STREAM, 0, np) < 0) | |
376 | fatal(errno, "error calling socketpair"); | |
ba3d1d36 RK |
377 | /* Beware of the Leopard! On OS X 10.5.x, the order of the shutdown |
378 | * calls here DOES MATTER. If you do the SHUT_WR first then the SHUT_RD | |
af7a85b7 | 379 | * fails with "Socket is not connected". I think this is a bug but |
ba3d1d36 RK |
380 | * provided implementors either don't care about the order or all agree |
381 | * about the order, choosing the reliable order is an adequate | |
382 | * workaround. */ | |
6d2d327c | 383 | xshutdown(np[1], SHUT_RD); /* decoder writes to np[1] */ |
ba3d1d36 | 384 | xshutdown(np[0], SHUT_WR); /* normalize reads from np[0] */ |
937be4c0 RK |
385 | blocking(np[0]); |
386 | blocking(np[1]); | |
6d2d327c RK |
387 | /* Start disorder-normalize */ |
388 | if(!(npid = xfork())) { | |
389 | if(!xfork()) { | |
84aa9f93 RK |
390 | /* Connect to the speaker process */ |
391 | memset(&addr, 0, sizeof addr); | |
392 | addr.sun_family = AF_UNIX; | |
393 | snprintf(addr.sun_path, sizeof addr.sun_path, | |
85cb23d7 | 394 | "%s/speaker/socket", config->home); |
84aa9f93 | 395 | sfd = xsocket(PF_UNIX, SOCK_STREAM, 0); |
dc450d30 | 396 | if(connect(sfd, (const struct sockaddr *)&addr, sizeof addr) < 0) |
84aa9f93 RK |
397 | fatal(errno, "connecting to %s", addr.sun_path); |
398 | l = strlen(q->id); | |
399 | if(write(sfd, &l, sizeof l) < 0 | |
400 | || write(sfd, q->id, l) < 0) | |
401 | fatal(errno, "writing to %s", addr.sun_path); | |
402 | /* Await the ack */ | |
403 | read(sfd, &l, 1); | |
404 | /* Plumbing */ | |
6d2d327c | 405 | xdup2(np[0], 0); |
84aa9f93 | 406 | xdup2(sfd, 1); |
6d2d327c RK |
407 | xclose(np[0]); |
408 | xclose(np[1]); | |
84aa9f93 RK |
409 | xclose(sfd); |
410 | /* Ask the speaker to actually start playing the track; we do it here | |
411 | * so it's definitely after ack. */ | |
412 | if(!prepare_only) { | |
413 | strcpy(sm.id, q->id); | |
414 | sm.type = SM_PLAY; | |
415 | speaker_send(speaker_fd, &sm); | |
416 | D(("sent SM_PLAY for %s", sm.id)); | |
417 | } | |
4387f694 RK |
418 | /* TODO stderr shouldn't be redirected for disorder-normalize |
419 | * (but it should be for play_track() */ | |
b44ca625 RK |
420 | execlp("disorder-normalize", "disorder-normalize", |
421 | log_default == &log_syslog ? "--syslog" : "--no-syslog", | |
816e6088 | 422 | "--config", configfile, |
b44ca625 | 423 | (char *)0); |
6d2d327c | 424 | fatal(errno, "executing disorder-normalize"); |
84aa9f93 | 425 | /* end of the innermost fork */ |
6d2d327c RK |
426 | } |
427 | _exit(0); | |
84aa9f93 | 428 | /* end of the middle fork */ |
6d2d327c | 429 | } |
84aa9f93 RK |
430 | /* Wait for the middle fork to finish */ |
431 | while(waitpid(npid, &n, 0) < 0 && errno == EINTR) | |
432 | ; | |
460b9539 | 433 | /* Pass the file descriptor to the driver in an environment |
434 | * variable. */ | |
6d2d327c | 435 | snprintf(buffer, sizeof buffer, "DISORDER_RAW_FD=%d", np[1]); |
460b9539 | 436 | if(putenv(buffer) < 0) |
437 | fatal(errno, "error calling putenv"); | |
6d2d327c | 438 | /* Close all the FDs we don't need */ |
6d2d327c | 439 | xclose(np[0]); |
460b9539 | 440 | } |
441 | if(waitdevice) { | |
442 | ao_initialize(); | |
443 | if(*waitdevice) { | |
444 | n = ao_driver_id(waitdevice); | |
445 | if(n == -1) | |
446 | fatal(0, "invalid libao driver: %s", optv[0]); | |
447 | } else | |
448 | n = ao_default_driver_id(); | |
449 | /* Make up a format. */ | |
450 | memset(&format, 0, sizeof format); | |
451 | format.bits = 8; | |
452 | format.rate = 44100; | |
453 | format.channels = 1; | |
454 | format.byte_format = AO_FMT_NATIVE; | |
455 | retries = 20; | |
456 | ts.tv_sec = 0; | |
457 | ts.tv_nsec = 100000000; /* 0.1s */ | |
458 | while((device = ao_open_live(n, &format, 0)) == 0 && retries-- > 0) | |
459 | nanosleep(&ts, 0); | |
460 | if(device) | |
461 | ao_close(device); | |
462 | } | |
463 | play_track(q->pl, | |
464 | optv, optc, | |
465 | p, | |
466 | q->track); | |
467 | _exit(0); | |
468 | case -1: /* error */ | |
469 | error(errno, "error calling fork"); | |
470 | if(q->type & DISORDER_PLAYER_PREFORK) | |
471 | play_cleanup(q->pl, q->data); /* else would leak */ | |
e99d42b1 | 472 | if(lfd != -1) |
473 | xclose(lfd); | |
460b9539 | 474 | return START_SOFTFAIL; |
475 | } | |
476 | store_player_pid(q->id, pid); | |
66bb2e02 | 477 | q->prepared = 1; |
e99d42b1 | 478 | if(lfd != -1) |
479 | xclose(lfd); | |
460b9539 | 480 | setpgid(pid, pid); |
481 | ev_child(ev, pid, 0, player_finished, q); | |
482 | D(("player subprocess ID %lu", (unsigned long)pid)); | |
483 | return START_OK; | |
484 | } | |
485 | ||
486 | int prepare(ev_source *ev, | |
487 | struct queue_entry *q) { | |
488 | int n; | |
489 | ||
490 | /* Find the player plugin */ | |
491 | if(find_player_pid(q->id) > 0) return 0; /* Already going. */ | |
492 | if((n = find_player(q)) < 0) return -1; /* No player */ | |
493 | q->pl = open_plugin(config->player.s[n].s[1], 0); /* No player */ | |
494 | q->type = play_get_type(q->pl); | |
495 | if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) | |
496 | return 0; /* Not a raw player */ | |
84aa9f93 | 497 | return start(ev, q, 1/*prepare_only*/); /* Prepare it */ |
460b9539 | 498 | } |
499 | ||
500 | void abandon(ev_source attribute((unused)) *ev, | |
501 | struct queue_entry *q) { | |
502 | struct speaker_message sm; | |
503 | pid_t pid = find_player_pid(q->id); | |
504 | ||
505 | if(pid < 0) return; /* Not prepared. */ | |
506 | if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) | |
507 | return; /* Not a raw player. */ | |
508 | /* Terminate the player. */ | |
509 | kill(-pid, config->signal); | |
510 | forget_player_pid(q->id); | |
511 | /* Cancel the track. */ | |
512 | memset(&sm, 0, sizeof sm); | |
513 | sm.type = SM_CANCEL; | |
514 | strcpy(sm.id, q->id); | |
84aa9f93 | 515 | speaker_send(speaker_fd, &sm); |
460b9539 | 516 | } |
517 | ||
49a773eb RK |
518 | /** @brief Called with a new random track |
519 | * @param track Track name | |
520 | */ | |
521 | static void chosen_random_track(ev_source *ev, | |
522 | const char *track) { | |
523 | struct queue_entry *q; | |
524 | ||
525 | if(!track) | |
526 | return; | |
527 | /* Add the track to the queue */ | |
528 | q = queue_add(track, 0, WHERE_END); | |
529 | q->state = playing_random; | |
530 | D(("picked %p (%s) at random", (void *)q, q->track)); | |
531 | queue_write(); | |
532 | /* Maybe a track can now be played */ | |
533 | play(ev); | |
534 | } | |
535 | ||
536 | /** @brief Maybe add a randomly chosen track | |
537 | * @param ev Event loop | |
538 | */ | |
539 | void add_random_track(ev_source *ev) { | |
460b9539 | 540 | struct queue_entry *q; |
459d4402 | 541 | long qlen = 0; |
460b9539 | 542 | |
543 | /* If random play is not enabled then do nothing. */ | |
544 | if(shutting_down || !random_is_enabled()) | |
49a773eb | 545 | return; |
459d4402 | 546 | /* Count how big the queue is */ |
460b9539 | 547 | for(q = qhead.next; q != &qhead; q = q->next) |
459d4402 | 548 | ++qlen; |
49a773eb RK |
549 | /* If it's smaller than the desired size then add a track */ |
550 | if(qlen < config->queue_pad) | |
551 | trackdb_request_random(ev, chosen_random_track); | |
460b9539 | 552 | } |
553 | ||
554 | /* try to play a track */ | |
555 | void play(ev_source *ev) { | |
556 | struct queue_entry *q; | |
557 | int random_enabled = random_is_enabled(); | |
558 | ||
559 | D(("play playing=%p", (void *)playing)); | |
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); | |
567 | return; | |
460b9539 | 568 | } |
49a773eb | 569 | /* There must be at least one track in the queue. */ |
460b9539 | 570 | q = qhead.next; |
571 | /* If random play is disabled but the track is a random one then don't play | |
572 | * it. play() will be called again when random play is re-enabled. */ | |
573 | if(!random_enabled && q->state == playing_random) | |
574 | return; | |
575 | D(("taken %p (%s) from queue", (void *)q, q->track)); | |
576 | /* Try to start playing. */ | |
84aa9f93 | 577 | switch(start(ev, q, 0/*!prepare_only*/)) { |
460b9539 | 578 | case START_HARDFAIL: |
579 | if(q == qhead.next) { | |
580 | queue_remove(q, 0); /* Abandon this track. */ | |
581 | queue_played(q); | |
582 | recent_write(); | |
583 | } | |
49a773eb RK |
584 | /* Oh well, try the next one */ |
585 | play(ev); | |
460b9539 | 586 | break; |
587 | case START_SOFTFAIL: | |
49a773eb | 588 | /* We'll try the same track again shortly. */ |
460b9539 | 589 | break; |
590 | case START_OK: | |
591 | if(q == qhead.next) { | |
592 | queue_remove(q, 0); | |
593 | queue_write(); | |
594 | } | |
595 | playing = q; | |
596 | time(&playing->played); | |
597 | playing->state = playing_started; | |
598 | notify_play(playing->track, playing->submitter); | |
599 | eventlog("playing", playing->track, | |
600 | playing->submitter ? playing->submitter : (const char *)0, | |
601 | (const char *)0); | |
602 | /* Maybe add a random track. */ | |
49a773eb | 603 | add_random_track(ev); |
460b9539 | 604 | /* If there is another track in the queue prepare it now. This could |
605 | * potentially be a just-added random track. */ | |
606 | if(qhead.next != &qhead) | |
607 | prepare(ev, qhead.next); | |
608 | break; | |
609 | } | |
610 | } | |
611 | ||
612 | int playing_is_enabled(void) { | |
613 | const char *s = trackdb_get_global("playing"); | |
614 | ||
615 | return !s || !strcmp(s, "yes"); | |
616 | } | |
617 | ||
618 | void enable_playing(const char *who, ev_source *ev) { | |
619 | trackdb_set_global("playing", "yes", who); | |
620 | /* Add a random track if necessary. */ | |
49a773eb | 621 | add_random_track(ev); |
460b9539 | 622 | play(ev); |
623 | } | |
624 | ||
625 | void disable_playing(const char *who) { | |
626 | trackdb_set_global("playing", "no", who); | |
627 | } | |
628 | ||
629 | int random_is_enabled(void) { | |
630 | const char *s = trackdb_get_global("random-play"); | |
631 | ||
632 | return !s || !strcmp(s, "yes"); | |
633 | } | |
634 | ||
635 | void enable_random(const char *who, ev_source *ev) { | |
636 | trackdb_set_global("random-play", "yes", who); | |
49a773eb | 637 | add_random_track(ev); |
460b9539 | 638 | play(ev); |
639 | } | |
640 | ||
641 | void disable_random(const char *who) { | |
642 | trackdb_set_global("random-play", "no", who); | |
643 | } | |
644 | ||
645 | void scratch(const char *who, const char *id) { | |
646 | struct queue_entry *q; | |
647 | struct speaker_message sm; | |
648 | pid_t pid; | |
649 | ||
650 | D(("scratch playing=%p state=%d id=%s playing->id=%s", | |
651 | (void *)playing, | |
652 | playing ? playing->state : 0, | |
653 | id ? id : "(none)", | |
654 | playing ? playing->id : "(none)")); | |
655 | if(playing | |
656 | && (playing->state == playing_started | |
657 | || playing->state == playing_paused) | |
658 | && (!id | |
659 | || !strcmp(id, playing->id))) { | |
660 | playing->state = playing_scratched; | |
661 | playing->scratched = who ? xstrdup(who) : 0; | |
662 | if((pid = find_player_pid(playing->id)) > 0) { | |
663 | D(("kill -%d %lu", config->signal, (unsigned long)pid)); | |
664 | kill(-pid, config->signal); | |
665 | forget_player_pid(playing->id); | |
666 | } else | |
667 | error(0, "could not find PID for %s", playing->id); | |
668 | if((playing->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) { | |
669 | memset(&sm, 0, sizeof sm); | |
670 | sm.type = SM_CANCEL; | |
671 | strcpy(sm.id, playing->id); | |
84aa9f93 | 672 | speaker_send(speaker_fd, &sm); |
460b9539 | 673 | D(("sending SM_CANCEL for %s", playing->id)); |
674 | } | |
675 | /* put a scratch track onto the front of the queue (but don't | |
676 | * bother if playing is disabled) */ | |
677 | if(playing_is_enabled() && config->scratch.n) { | |
678 | int r = rand() * (double)config->scratch.n / (RAND_MAX + 1.0); | |
679 | q = queue_add(config->scratch.s[r], who, WHERE_START); | |
680 | q->state = playing_isscratch; | |
681 | } | |
682 | notify_scratch(playing->track, playing->submitter, who, | |
683 | time(0) - playing->played); | |
684 | } | |
685 | } | |
686 | ||
687 | void quitting(ev_source *ev) { | |
688 | struct queue_entry *q; | |
689 | pid_t pid; | |
690 | ||
691 | /* Don't start anything new */ | |
692 | shutting_down = 1; | |
693 | /* Shut down the current player */ | |
694 | if(playing) { | |
695 | if((pid = find_player_pid(playing->id)) > 0) { | |
696 | kill(-pid, config->signal); | |
697 | forget_player_pid(playing->id); | |
698 | } else | |
699 | error(0, "could not find PID for %s", playing->id); | |
700 | playing->state = playing_quitting; | |
701 | finished(0); | |
702 | } | |
703 | /* Zap any other players */ | |
704 | for(q = qhead.next; q != &qhead; q = q->next) | |
705 | if((pid = find_player_pid(q->id)) > 0) { | |
706 | D(("kill -%d %lu", config->signal, (unsigned long)pid)); | |
707 | kill(-pid, config->signal); | |
708 | forget_player_pid(q->id); | |
709 | } else | |
710 | error(0, "could not find PID for %s", q->id); | |
711 | /* Don't need the speaker any more */ | |
712 | ev_fd_cancel(ev, ev_read, speaker_fd); | |
713 | xclose(speaker_fd); | |
714 | } | |
715 | ||
716 | int pause_playing(const char *who) { | |
717 | struct speaker_message sm; | |
718 | long played; | |
719 | ||
720 | /* Can't pause if already paused or if nothing playing. */ | |
721 | if(!playing || paused) return 0; | |
722 | switch(playing->type & DISORDER_PLAYER_TYPEMASK) { | |
723 | case DISORDER_PLAYER_STANDALONE: | |
724 | if(!(playing->type & DISORDER_PLAYER_PAUSES)) { | |
725 | default: | |
726 | error(0, "cannot pause because player is not powerful enough"); | |
727 | return -1; | |
728 | } | |
729 | if(play_pause(playing->pl, &played, playing->data)) { | |
730 | error(0, "player indicates it cannot pause"); | |
731 | return -1; | |
732 | } | |
733 | time(&playing->lastpaused); | |
734 | playing->uptopause = played; | |
735 | playing->lastresumed = 0; | |
736 | break; | |
737 | case DISORDER_PLAYER_RAW: | |
738 | memset(&sm, 0, sizeof sm); | |
739 | sm.type = SM_PAUSE; | |
84aa9f93 | 740 | speaker_send(speaker_fd, &sm); |
460b9539 | 741 | break; |
742 | } | |
743 | if(who) info("paused by %s", who); | |
744 | notify_pause(playing->track, who); | |
745 | paused = 1; | |
746 | if(playing->state == playing_started) | |
747 | playing->state = playing_paused; | |
748 | eventlog("state", "pause", (char *)0); | |
749 | return 0; | |
750 | } | |
751 | ||
752 | void resume_playing(const char *who) { | |
753 | struct speaker_message sm; | |
754 | ||
755 | if(!paused) return; | |
756 | paused = 0; | |
757 | if(!playing) return; | |
758 | switch(playing->type & DISORDER_PLAYER_TYPEMASK) { | |
759 | case DISORDER_PLAYER_STANDALONE: | |
760 | if(!playing->type & DISORDER_PLAYER_PAUSES) { | |
761 | default: | |
762 | /* Shouldn't happen */ | |
763 | return; | |
764 | } | |
765 | play_resume(playing->pl, playing->data); | |
766 | time(&playing->lastresumed); | |
767 | break; | |
768 | case DISORDER_PLAYER_RAW: | |
769 | memset(&sm, 0, sizeof sm); | |
770 | sm.type = SM_RESUME; | |
84aa9f93 | 771 | speaker_send(speaker_fd, &sm); |
460b9539 | 772 | break; |
773 | } | |
774 | if(who) info("resumed by %s", who); | |
775 | notify_resume(playing->track, who); | |
776 | if(playing->state == playing_paused) | |
777 | playing->state = playing_started; | |
778 | eventlog("state", "resume", (char *)0); | |
779 | } | |
780 | ||
781 | /* | |
782 | Local Variables: | |
783 | c-basic-offset:2 | |
784 | comment-column:40 | |
785 | fill-column:79 | |
786 | End: | |
787 | */ |