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