460b9539 |
1 | /* |
2 | * This file is part of DisOrder. |
3 | * Copyright (C) 2004, 2005, 2006 Richard Kettlewell |
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> |
22 | |
23 | #include <sys/types.h> |
24 | #include <sys/time.h> |
25 | #include <unistd.h> |
26 | #include <errno.h> |
27 | #include <fnmatch.h> |
28 | #include <time.h> |
29 | #include <signal.h> |
30 | #include <stdlib.h> |
31 | #include <assert.h> |
32 | #include <sys/socket.h> |
33 | #include <string.h> |
34 | #include <stdio.h> |
35 | #include <pcre.h> |
36 | #include <ao/ao.h> |
37 | |
38 | #include "event.h" |
39 | #include "log.h" |
40 | #include "mem.h" |
41 | #include "configuration.h" |
42 | #include "queue.h" |
43 | #include "trackdb.h" |
44 | #include "play.h" |
45 | #include "plugin.h" |
46 | #include "wstat.h" |
47 | #include "eventlog.h" |
48 | #include "logfd.h" |
49 | #include "syscalls.h" |
50 | #include "speaker.h" |
51 | #include "disorder.h" |
52 | #include "signame.h" |
53 | #include "hash.h" |
54 | |
55 | #define SPEAKER "disorder-speaker" |
56 | |
57 | struct queue_entry *playing; |
58 | int paused; |
59 | |
60 | static void finished(ev_source *ev); |
61 | |
62 | static int speaker_fd = -1; |
63 | static hash *player_pids; |
64 | static int shutting_down; |
65 | |
66 | static void store_player_pid(const char *id, pid_t pid) { |
67 | if(!player_pids) player_pids = hash_new(sizeof (pid_t)); |
68 | hash_add(player_pids, id, &pid, HASH_INSERT_OR_REPLACE); |
69 | } |
70 | |
71 | static pid_t find_player_pid(const char *id) { |
72 | pid_t *pidp; |
73 | |
74 | if(player_pids && (pidp = hash_find(player_pids, id))) return *pidp; |
75 | return -1; |
76 | } |
77 | |
78 | static void forget_player_pid(const char *id) { |
79 | if(player_pids) hash_remove(player_pids, id); |
80 | } |
81 | |
82 | /* called when speaker process terminates */ |
83 | static int speaker_terminated(ev_source attribute((unused)) *ev, |
84 | pid_t attribute((unused)) pid, |
85 | int attribute((unused)) status, |
86 | const struct rusage attribute((unused)) *rusage, |
87 | void attribute((unused)) *u) { |
88 | if(status) |
89 | error(0, "speaker subprocess terminated with status %s", |
90 | wstat(status)); |
91 | return 0; |
92 | } |
93 | |
94 | /* called when speaker process has something to say */ |
95 | static int speaker_readable(ev_source *ev, int fd, |
96 | void attribute((unused)) *u) { |
97 | struct speaker_message sm; |
98 | int ret = speaker_recv(fd, &sm, 0); |
99 | |
100 | if(ret < 0) return 0; /* EAGAIN */ |
101 | if(!ret) { /* EOF */ |
102 | ev_fd_cancel(ev, ev_read, fd); |
103 | return 0; |
104 | } |
105 | switch(sm.type) { |
106 | case SM_PAUSED: |
107 | /* track ID is paused, DATA seconds played */ |
108 | D(("SM_PAUSED %s %ld", sm.id, sm.data)); |
109 | playing->sofar = sm.data; |
110 | break; |
111 | case SM_FINISHED: |
112 | /* the playing track finished */ |
113 | D(("SM_FINISHED %s", sm.id)); |
114 | finished(ev); |
115 | break; |
116 | case SM_PLAYING: |
117 | /* track ID is playing, DATA seconds played */ |
118 | D(("SM_PLAYING %s %ld", sm.id, sm.data)); |
119 | playing->sofar = sm.data; |
120 | break; |
121 | default: |
122 | error(0, "unknown message type %d", sm.type); |
123 | } |
124 | return 0; |
125 | } |
126 | |
127 | void speaker_setup(ev_source *ev) { |
128 | int sp[2], lfd; |
129 | pid_t pid; |
130 | |
131 | if(socketpair(PF_UNIX, SOCK_DGRAM, 0, sp) < 0) |
132 | fatal(errno, "error calling socketpair"); |
133 | if(!isatty(2)) |
134 | lfd = logfd(ev, SPEAKER); |
135 | else |
136 | lfd = -1; |
137 | if(!(pid = xfork())) { |
138 | exitfn = _exit; |
139 | ev_signal_atfork(ev); |
140 | xdup2(sp[0], 0); |
141 | xdup2(sp[0], 1); |
142 | xclose(sp[0]); |
143 | xclose(sp[1]); |
144 | if(lfd != -1) { |
145 | xdup2(lfd, 2); |
146 | xclose(lfd); |
147 | } |
148 | signal(SIGPIPE, SIG_DFL); |
149 | #if 0 |
150 | execlp("valgrind", "valgrind", SPEAKER, "--config", configfile, |
151 | debugging ? "--debug" : "--no-debug", (char *)0); |
152 | #else |
153 | execlp(SPEAKER, SPEAKER, "--config", configfile, |
154 | debugging ? "--debug" : "--no-debug", (char *)0); |
155 | #endif |
156 | fatal(errno, "error invoking %s", SPEAKER); |
157 | } |
158 | ev_child(ev, pid, 0, speaker_terminated, 0); |
159 | speaker_fd = sp[1]; |
160 | xclose(sp[0]); |
161 | cloexec(speaker_fd); |
162 | /* Don't need to make speaker_fd nonblocking because speaker_recv() uses |
163 | * MSG_DONTWAIT. */ |
164 | ev_fd(ev, ev_read, speaker_fd, speaker_readable, 0); |
165 | } |
166 | |
167 | void speaker_reload(void) { |
168 | struct speaker_message sm; |
169 | |
170 | memset(&sm, 0, sizeof sm); |
171 | sm.type = SM_RELOAD; |
172 | speaker_send(speaker_fd, &sm, -1); |
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: |
241 | /* If this was an SM_PREPARE track then either it failed or we deliberately |
242 | * stopped it because it was removed from the queue or moved down it. So |
243 | * leave it state alone for future use. */ |
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() */ |
284 | #define START_OK 0 /* Succeeded. */ |
285 | #define START_HARDFAIL 1 /* Track is broken. */ |
286 | #define START_SOFTFAIL 2 /* Track OK, system (temporarily?) broken */ |
287 | |
288 | /* Play or prepare Q */ |
289 | static int start(ev_source *ev, |
290 | struct queue_entry *q, |
291 | int smop) { |
292 | int n, lfd; |
293 | const char *p; |
294 | int sp[2]; |
295 | struct speaker_message sm; |
296 | char buffer[64]; |
297 | int optc; |
298 | ao_sample_format format; |
299 | ao_device *device; |
300 | int retries; |
301 | struct timespec ts; |
302 | const char *waitdevice = 0; |
303 | const char *const *optv; |
304 | pid_t pid; |
305 | |
306 | memset(&sm, 0, sizeof sm); |
307 | if(find_player_pid(q->id) > 0) { |
308 | if(smop == SM_PREPARE) return START_OK; |
309 | /* We have already sent an SM_PREPARE for this track so we just need to |
310 | * tell the speaker process to start actually playing the queued up audio |
311 | * data */ |
312 | strcpy(sm.id, q->id); |
313 | sm.type = SM_PLAY; |
314 | speaker_send(speaker_fd, &sm, -1); |
315 | return START_OK; |
316 | } |
317 | /* Find the player plugin. */ |
318 | if((n = find_player(q)) < 0) return START_HARDFAIL; |
319 | if(!(q->pl = open_plugin(config->player.s[n].s[1], 0))) |
320 | return START_HARDFAIL; |
321 | q->type = play_get_type(q->pl); |
322 | /* Can't prepare non-raw tracks. */ |
323 | if(smop == SM_PREPARE |
324 | && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) |
325 | return START_OK; |
326 | /* Call the prefork function. */ |
327 | p = trackdb_rawpath(q->track); |
328 | if(q->type & DISORDER_PLAYER_PREFORK) |
329 | if(!(q->data = play_prefork(q->pl, p))) { |
330 | error(0, "prefork function for %s failed", q->track); |
331 | return START_HARDFAIL; |
332 | } |
333 | /* Use the second arg as the tag if available (it's probably a command name), |
334 | * otherwise the module name. */ |
335 | lfd = logfd(ev, (config->player.s[n].s[2] |
336 | ? config->player.s[n].s[2] : config->player.s[n].s[1])); |
337 | optc = config->player.s[n].n - 2; |
338 | optv = (void *)&config->player.s[n].s[2]; |
339 | while(optc > 0 && optv[0][0] == '-') { |
340 | if(!strcmp(optv[0], "--")) { |
341 | ++optv; |
342 | --optc; |
343 | break; |
344 | } |
345 | if(!strcmp(optv[0], "--wait-for-device") |
346 | || !strncmp(optv[0], "--wait-for-device=", 18)) { |
347 | if((waitdevice = strchr(optv[0], '='))) { |
348 | ++waitdevice; |
349 | } else |
350 | waitdevice = ""; /* use default */ |
351 | ++optv; |
352 | --optc; |
353 | } else { |
354 | error(0, "unknown option %s", optv[0]); |
355 | return START_HARDFAIL; |
356 | } |
357 | } |
358 | switch(pid = fork()) { |
359 | case 0: /* child */ |
360 | exitfn = _exit; |
361 | ev_signal_atfork(ev); |
362 | signal(SIGPIPE, SIG_DFL); |
363 | xdup2(lfd, 1); |
364 | xdup2(lfd, 2); |
365 | xclose(lfd); /* tidy up */ |
366 | setpgid(0, 0); |
367 | if((q->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) { |
368 | /* Raw format players write down a pipe (in fact a socket) to |
369 | * the speaker process. */ |
370 | sm.type = smop; |
371 | strcpy(sm.id, q->id); |
372 | if(socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) |
373 | fatal(errno, "error calling socketpair"); |
374 | xshutdown(sp[0], SHUT_WR); |
375 | xshutdown(sp[1], SHUT_RD); |
376 | speaker_send(speaker_fd, &sm, sp[0]); |
377 | /* Pass the file descriptor to the driver in an environment |
378 | * variable. */ |
379 | snprintf(buffer, sizeof buffer, "DISORDER_RAW_FD=%d", sp[1]); |
380 | if(putenv(buffer) < 0) |
381 | fatal(errno, "error calling putenv"); |
382 | xclose(sp[0]); |
383 | } |
384 | if(waitdevice) { |
385 | ao_initialize(); |
386 | if(*waitdevice) { |
387 | n = ao_driver_id(waitdevice); |
388 | if(n == -1) |
389 | fatal(0, "invalid libao driver: %s", optv[0]); |
390 | } else |
391 | n = ao_default_driver_id(); |
392 | /* Make up a format. */ |
393 | memset(&format, 0, sizeof format); |
394 | format.bits = 8; |
395 | format.rate = 44100; |
396 | format.channels = 1; |
397 | format.byte_format = AO_FMT_NATIVE; |
398 | retries = 20; |
399 | ts.tv_sec = 0; |
400 | ts.tv_nsec = 100000000; /* 0.1s */ |
401 | while((device = ao_open_live(n, &format, 0)) == 0 && retries-- > 0) |
402 | nanosleep(&ts, 0); |
403 | if(device) |
404 | ao_close(device); |
405 | } |
406 | play_track(q->pl, |
407 | optv, optc, |
408 | p, |
409 | q->track); |
410 | _exit(0); |
411 | case -1: /* error */ |
412 | error(errno, "error calling fork"); |
413 | if(q->type & DISORDER_PLAYER_PREFORK) |
414 | play_cleanup(q->pl, q->data); /* else would leak */ |
415 | xclose(lfd); |
416 | return START_SOFTFAIL; |
417 | } |
418 | store_player_pid(q->id, pid); |
419 | xclose(lfd); |
420 | setpgid(pid, pid); |
421 | ev_child(ev, pid, 0, player_finished, q); |
422 | D(("player subprocess ID %lu", (unsigned long)pid)); |
423 | return START_OK; |
424 | } |
425 | |
426 | int prepare(ev_source *ev, |
427 | struct queue_entry *q) { |
428 | int n; |
429 | |
430 | /* Find the player plugin */ |
431 | if(find_player_pid(q->id) > 0) return 0; /* Already going. */ |
432 | if((n = find_player(q)) < 0) return -1; /* No player */ |
433 | q->pl = open_plugin(config->player.s[n].s[1], 0); /* No player */ |
434 | q->type = play_get_type(q->pl); |
435 | if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) |
436 | return 0; /* Not a raw player */ |
437 | return start(ev, q, SM_PREPARE); /* Prepare it */ |
438 | } |
439 | |
440 | void abandon(ev_source attribute((unused)) *ev, |
441 | struct queue_entry *q) { |
442 | struct speaker_message sm; |
443 | pid_t pid = find_player_pid(q->id); |
444 | |
445 | if(pid < 0) return; /* Not prepared. */ |
446 | if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) |
447 | return; /* Not a raw player. */ |
448 | /* Terminate the player. */ |
449 | kill(-pid, config->signal); |
450 | forget_player_pid(q->id); |
451 | /* Cancel the track. */ |
452 | memset(&sm, 0, sizeof sm); |
453 | sm.type = SM_CANCEL; |
454 | strcpy(sm.id, q->id); |
455 | speaker_send(speaker_fd, &sm, -1); |
456 | } |
457 | |
458 | int add_random_track(void) { |
459 | struct queue_entry *q; |
460 | const char *p; |
459d4402 |
461 | long qlen = 0; |
462 | int rc = 0; |
460b9539 |
463 | |
464 | /* If random play is not enabled then do nothing. */ |
465 | if(shutting_down || !random_is_enabled()) |
466 | return 0; |
459d4402 |
467 | /* Count how big the queue is */ |
460b9539 |
468 | for(q = qhead.next; q != &qhead; q = q->next) |
459d4402 |
469 | ++qlen; |
470 | /* Add random tracks until the queue is at the right size */ |
471 | while(qlen < config->queue_pad) { |
472 | /* Try to pick a random track */ |
473 | if(!(p = trackdb_random(16))) { |
474 | rc = -1; |
475 | break; |
476 | } |
477 | /* Add it to the end of the queue. */ |
478 | q = queue_add(p, 0, WHERE_END); |
479 | q->state = playing_random; |
480 | D(("picked %p (%s) at random", (void *)q, q->track)); |
481 | ++qlen; |
482 | } |
460b9539 |
483 | /* Commit the queue */ |
484 | queue_write(); |
459d4402 |
485 | return rc; |
460b9539 |
486 | } |
487 | |
488 | /* try to play a track */ |
489 | void play(ev_source *ev) { |
490 | struct queue_entry *q; |
491 | int random_enabled = random_is_enabled(); |
492 | |
493 | D(("play playing=%p", (void *)playing)); |
494 | if(shutting_down || playing || !playing_is_enabled()) return; |
495 | /* If the queue is empty then add a random track. */ |
496 | if(qhead.next == &qhead) { |
497 | if(!random_enabled) |
498 | return; |
499 | if(add_random_track()) { |
500 | /* On error, try again in 10s. */ |
501 | retry_play(ev, 10); |
502 | return; |
503 | } |
504 | /* Now there must be at least one track in the queue. */ |
505 | } |
506 | q = qhead.next; |
507 | /* If random play is disabled but the track is a random one then don't play |
508 | * it. play() will be called again when random play is re-enabled. */ |
509 | if(!random_enabled && q->state == playing_random) |
510 | return; |
511 | D(("taken %p (%s) from queue", (void *)q, q->track)); |
512 | /* Try to start playing. */ |
513 | switch(start(ev, q, SM_PLAY)) { |
514 | case START_HARDFAIL: |
515 | if(q == qhead.next) { |
516 | queue_remove(q, 0); /* Abandon this track. */ |
517 | queue_played(q); |
518 | recent_write(); |
519 | } |
520 | if(qhead.next == &qhead) |
521 | /* Queue is empty, wait a bit before trying something else (so we don't |
522 | * sit there looping madly in the presence of persistent problem). Note |
523 | * that we might not reliably get a random track lookahead in this case, |
524 | * but if we get here then really there are bigger problems. */ |
525 | retry_play(ev, 1); |
526 | else |
527 | /* More in queue, try again now. */ |
528 | play(ev); |
529 | break; |
530 | case START_SOFTFAIL: |
531 | /* Try same track again in a bit. */ |
532 | retry_play(ev, 10); |
533 | break; |
534 | case START_OK: |
535 | if(q == qhead.next) { |
536 | queue_remove(q, 0); |
537 | queue_write(); |
538 | } |
539 | playing = q; |
540 | time(&playing->played); |
541 | playing->state = playing_started; |
542 | notify_play(playing->track, playing->submitter); |
543 | eventlog("playing", playing->track, |
544 | playing->submitter ? playing->submitter : (const char *)0, |
545 | (const char *)0); |
546 | /* Maybe add a random track. */ |
547 | add_random_track(); |
548 | /* If there is another track in the queue prepare it now. This could |
549 | * potentially be a just-added random track. */ |
550 | if(qhead.next != &qhead) |
551 | prepare(ev, qhead.next); |
552 | break; |
553 | } |
554 | } |
555 | |
556 | int playing_is_enabled(void) { |
557 | const char *s = trackdb_get_global("playing"); |
558 | |
559 | return !s || !strcmp(s, "yes"); |
560 | } |
561 | |
562 | void enable_playing(const char *who, ev_source *ev) { |
563 | trackdb_set_global("playing", "yes", who); |
564 | /* Add a random track if necessary. */ |
565 | add_random_track(); |
566 | play(ev); |
567 | } |
568 | |
569 | void disable_playing(const char *who) { |
570 | trackdb_set_global("playing", "no", who); |
571 | } |
572 | |
573 | int random_is_enabled(void) { |
574 | const char *s = trackdb_get_global("random-play"); |
575 | |
576 | return !s || !strcmp(s, "yes"); |
577 | } |
578 | |
579 | void enable_random(const char *who, ev_source *ev) { |
580 | trackdb_set_global("random-play", "yes", who); |
581 | add_random_track(); |
582 | play(ev); |
583 | } |
584 | |
585 | void disable_random(const char *who) { |
586 | trackdb_set_global("random-play", "no", who); |
587 | } |
588 | |
589 | void scratch(const char *who, const char *id) { |
590 | struct queue_entry *q; |
591 | struct speaker_message sm; |
592 | pid_t pid; |
593 | |
594 | D(("scratch playing=%p state=%d id=%s playing->id=%s", |
595 | (void *)playing, |
596 | playing ? playing->state : 0, |
597 | id ? id : "(none)", |
598 | playing ? playing->id : "(none)")); |
599 | if(playing |
600 | && (playing->state == playing_started |
601 | || playing->state == playing_paused) |
602 | && (!id |
603 | || !strcmp(id, playing->id))) { |
604 | playing->state = playing_scratched; |
605 | playing->scratched = who ? xstrdup(who) : 0; |
606 | if((pid = find_player_pid(playing->id)) > 0) { |
607 | D(("kill -%d %lu", config->signal, (unsigned long)pid)); |
608 | kill(-pid, config->signal); |
609 | forget_player_pid(playing->id); |
610 | } else |
611 | error(0, "could not find PID for %s", playing->id); |
612 | if((playing->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) { |
613 | memset(&sm, 0, sizeof sm); |
614 | sm.type = SM_CANCEL; |
615 | strcpy(sm.id, playing->id); |
616 | speaker_send(speaker_fd, &sm, -1); |
617 | D(("sending SM_CANCEL for %s", playing->id)); |
618 | } |
619 | /* put a scratch track onto the front of the queue (but don't |
620 | * bother if playing is disabled) */ |
621 | if(playing_is_enabled() && config->scratch.n) { |
622 | int r = rand() * (double)config->scratch.n / (RAND_MAX + 1.0); |
623 | q = queue_add(config->scratch.s[r], who, WHERE_START); |
624 | q->state = playing_isscratch; |
625 | } |
626 | notify_scratch(playing->track, playing->submitter, who, |
627 | time(0) - playing->played); |
628 | } |
629 | } |
630 | |
631 | void quitting(ev_source *ev) { |
632 | struct queue_entry *q; |
633 | pid_t pid; |
634 | |
635 | /* Don't start anything new */ |
636 | shutting_down = 1; |
637 | /* Shut down the current player */ |
638 | if(playing) { |
639 | if((pid = find_player_pid(playing->id)) > 0) { |
640 | kill(-pid, config->signal); |
641 | forget_player_pid(playing->id); |
642 | } else |
643 | error(0, "could not find PID for %s", playing->id); |
644 | playing->state = playing_quitting; |
645 | finished(0); |
646 | } |
647 | /* Zap any other players */ |
648 | for(q = qhead.next; q != &qhead; q = q->next) |
649 | if((pid = find_player_pid(q->id)) > 0) { |
650 | D(("kill -%d %lu", config->signal, (unsigned long)pid)); |
651 | kill(-pid, config->signal); |
652 | forget_player_pid(q->id); |
653 | } else |
654 | error(0, "could not find PID for %s", q->id); |
655 | /* Don't need the speaker any more */ |
656 | ev_fd_cancel(ev, ev_read, speaker_fd); |
657 | xclose(speaker_fd); |
658 | } |
659 | |
660 | int pause_playing(const char *who) { |
661 | struct speaker_message sm; |
662 | long played; |
663 | |
664 | /* Can't pause if already paused or if nothing playing. */ |
665 | if(!playing || paused) return 0; |
666 | switch(playing->type & DISORDER_PLAYER_TYPEMASK) { |
667 | case DISORDER_PLAYER_STANDALONE: |
668 | if(!(playing->type & DISORDER_PLAYER_PAUSES)) { |
669 | default: |
670 | error(0, "cannot pause because player is not powerful enough"); |
671 | return -1; |
672 | } |
673 | if(play_pause(playing->pl, &played, playing->data)) { |
674 | error(0, "player indicates it cannot pause"); |
675 | return -1; |
676 | } |
677 | time(&playing->lastpaused); |
678 | playing->uptopause = played; |
679 | playing->lastresumed = 0; |
680 | break; |
681 | case DISORDER_PLAYER_RAW: |
682 | memset(&sm, 0, sizeof sm); |
683 | sm.type = SM_PAUSE; |
684 | speaker_send(speaker_fd, &sm, -1); |
685 | break; |
686 | } |
687 | if(who) info("paused by %s", who); |
688 | notify_pause(playing->track, who); |
689 | paused = 1; |
690 | if(playing->state == playing_started) |
691 | playing->state = playing_paused; |
692 | eventlog("state", "pause", (char *)0); |
693 | return 0; |
694 | } |
695 | |
696 | void resume_playing(const char *who) { |
697 | struct speaker_message sm; |
698 | |
699 | if(!paused) return; |
700 | paused = 0; |
701 | if(!playing) return; |
702 | switch(playing->type & DISORDER_PLAYER_TYPEMASK) { |
703 | case DISORDER_PLAYER_STANDALONE: |
704 | if(!playing->type & DISORDER_PLAYER_PAUSES) { |
705 | default: |
706 | /* Shouldn't happen */ |
707 | return; |
708 | } |
709 | play_resume(playing->pl, playing->data); |
710 | time(&playing->lastresumed); |
711 | break; |
712 | case DISORDER_PLAYER_RAW: |
713 | memset(&sm, 0, sizeof sm); |
714 | sm.type = SM_RESUME; |
715 | speaker_send(speaker_fd, &sm, -1); |
716 | break; |
717 | } |
718 | if(who) info("resumed by %s", who); |
719 | notify_resume(playing->track, who); |
720 | if(playing->state == playing_paused) |
721 | playing->state = playing_started; |
722 | eventlog("state", "resume", (char *)0); |
723 | } |
724 | |
725 | /* |
726 | Local Variables: |
727 | c-basic-offset:2 |
728 | comment-column:40 |
729 | fill-column:79 |
730 | End: |
731 | */ |