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