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