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