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