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