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