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