chiark / gitweb /
Add a new 'origin' field to queue entries. This records the origin of
[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   q->state = playing_random;
496   D(("picked %p (%s) at random", (void *)q, q->track));
497   queue_write();
498   /* Maybe a track can now be played */
499   play(ev);
500 }
501
502 /** @brief Maybe add a randomly chosen track
503  * @param ev Event loop
504  */
505 void add_random_track(ev_source *ev) {
506   struct queue_entry *q;
507   long qlen = 0;
508
509   /* If random play is not enabled then do nothing. */
510   if(shutting_down || !random_is_enabled())
511     return;
512   /* Count how big the queue is */
513   for(q = qhead.next; q != &qhead; q = q->next)
514     ++qlen;
515   /* If it's smaller than the desired size then add a track */
516   if(qlen < config->queue_pad)
517     trackdb_request_random(ev, chosen_random_track);
518 }
519
520 /* try to play a track */
521 void play(ev_source *ev) {
522   struct queue_entry *q;
523   int random_enabled = random_is_enabled();
524
525   D(("play playing=%p", (void *)playing));
526   if(shutting_down || playing || !playing_is_enabled()) return;
527   /* See if there's anything to play */
528   if(qhead.next == &qhead) {
529     /* Queue is empty.  We could just wait around since there are periodic
530      * attempts to add a random track anyway.  However they are rarer than
531      * attempts to force a track so we initiate one now. */
532     add_random_track(ev);
533     return;
534   }
535   /* There must be at least one track in the queue. */
536   q = qhead.next;
537   /* If random play is disabled but the track is a non-adopted random one
538    * then don't play it.  play() will be called again when random play is
539    * re-enabled. */
540   if(!random_enabled && q->origin == origin_random)
541     return;
542   D(("taken %p (%s) from queue", (void *)q, q->track));
543   /* Try to start playing. */
544   switch(start(ev, q, 0/*!prepare_only*/)) {
545   case START_HARDFAIL:
546     if(q == qhead.next) {
547       queue_remove(q, 0);               /* Abandon this track. */
548       queue_played(q);
549       recent_write();
550     }
551     /* Oh well, try the next one */
552     play(ev);
553     break;
554   case START_SOFTFAIL:
555     /* We'll try the same track again shortly. */
556     break;
557   case START_OK:
558     if(q == qhead.next) {
559       queue_remove(q, 0);
560       queue_write();
561     }
562     playing = q;
563     time(&playing->played);
564     playing->state = playing_started;
565     notify_play(playing->track, playing->submitter);
566     eventlog("playing", playing->track,
567              playing->submitter ? playing->submitter : (const char *)0,
568              (const char *)0);
569     /* Maybe add a random track. */
570     add_random_track(ev);
571     /* If there is another track in the queue prepare it now.  This could
572      * potentially be a just-added random track. */
573     if(qhead.next != &qhead)
574       prepare(ev, qhead.next);
575     break;
576   }
577 }
578
579 int playing_is_enabled(void) {
580   const char *s = trackdb_get_global("playing");
581
582   return !s || !strcmp(s, "yes");
583 }
584
585 void enable_playing(const char *who, ev_source *ev) {
586   trackdb_set_global("playing", "yes", who);
587   /* Add a random track if necessary. */
588   add_random_track(ev);
589   play(ev);
590 }
591
592 void disable_playing(const char *who) {
593   trackdb_set_global("playing", "no", who);
594 }
595
596 int random_is_enabled(void) {
597   const char *s = trackdb_get_global("random-play");
598
599   return !s || !strcmp(s, "yes");
600 }
601
602 void enable_random(const char *who, ev_source *ev) {
603   trackdb_set_global("random-play", "yes", who);
604   add_random_track(ev);
605   play(ev);
606 }
607
608 void disable_random(const char *who) {
609   trackdb_set_global("random-play", "no", who);
610 }
611
612 void scratch(const char *who, const char *id) {
613   struct queue_entry *q;
614   struct speaker_message sm;
615   pid_t pid;
616
617   D(("scratch playing=%p state=%d id=%s playing->id=%s",
618      (void *)playing,
619      playing ? playing->state : 0,
620      id ? id : "(none)",
621      playing ? playing->id : "(none)"));
622   if(playing
623      && (playing->state == playing_started
624          || playing->state == playing_paused)
625      && (!id
626          || !strcmp(id, playing->id))) {
627     playing->state = playing_scratched;
628     playing->scratched = who ? xstrdup(who) : 0;
629     if((pid = find_player_pid(playing->id)) > 0) {
630       D(("kill -%d %lu", config->signal, (unsigned long)pid));
631       kill(-pid, config->signal);
632       forget_player_pid(playing->id);
633     } else
634       error(0, "could not find PID for %s", playing->id);
635     if((playing->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) {
636       memset(&sm, 0, sizeof sm);
637       sm.type = SM_CANCEL;
638       strcpy(sm.id, playing->id);
639       speaker_send(speaker_fd, &sm);
640       D(("sending SM_CANCEL for %s", playing->id));
641     }
642     /* put a scratch track onto the front of the queue (but don't
643      * bother if playing is disabled) */
644     if(playing_is_enabled() && config->scratch.n) {
645       int r = rand() * (double)config->scratch.n / (RAND_MAX + 1.0);
646       q = queue_add(config->scratch.s[r], who, WHERE_START, origin_scratch);
647       q->state = playing_isscratch;
648     }
649     notify_scratch(playing->track, playing->submitter, who,
650                    time(0) - playing->played);
651   }
652 }
653
654 void quitting(ev_source *ev) {
655   struct queue_entry *q;
656   pid_t pid;
657
658   /* Don't start anything new */
659   shutting_down = 1;
660   /* Shut down the current player */
661   if(playing) {
662     if((pid = find_player_pid(playing->id)) > 0) {
663       kill(-pid, config->signal);
664       forget_player_pid(playing->id);
665     } else
666       error(0, "could not find PID for %s", playing->id);
667     playing->state = playing_quitting;
668     finished(0);
669   }
670   /* Zap any other players */
671   for(q = qhead.next; q != &qhead; q = q->next)
672     if((pid = find_player_pid(q->id)) > 0) {
673       D(("kill -%d %lu", config->signal, (unsigned long)pid));
674       kill(-pid, config->signal);
675       forget_player_pid(q->id);
676     } else
677       error(0, "could not find PID for %s", q->id);
678   /* Don't need the speaker any more */
679   ev_fd_cancel(ev, ev_read, speaker_fd);
680   xclose(speaker_fd);
681 }
682
683 int pause_playing(const char *who) {
684   struct speaker_message sm;
685   long played;
686   
687   /* Can't pause if already paused or if nothing playing. */
688   if(!playing || paused) return 0;
689   switch(playing->type & DISORDER_PLAYER_TYPEMASK) {
690   case DISORDER_PLAYER_STANDALONE:
691     if(!(playing->type & DISORDER_PLAYER_PAUSES)) {
692     default:
693       error(0,  "cannot pause because player is not powerful enough");
694       return -1;
695     }
696     if(play_pause(playing->pl, &played, playing->data)) {
697       error(0, "player indicates it cannot pause");
698       return -1;
699     }
700     time(&playing->lastpaused);
701     playing->uptopause = played;
702     playing->lastresumed = 0;
703     break;
704   case DISORDER_PLAYER_RAW:
705     memset(&sm, 0, sizeof sm);
706     sm.type = SM_PAUSE;
707     speaker_send(speaker_fd, &sm);
708     break;
709   }
710   if(who) info("paused by %s", who);
711   notify_pause(playing->track, who);
712   paused = 1;
713   if(playing->state == playing_started)
714     playing->state = playing_paused;
715   eventlog("state", "pause", (char *)0);
716   return 0;
717 }
718
719 void resume_playing(const char *who) {
720   struct speaker_message sm;
721
722   if(!paused) return;
723   paused = 0;
724   if(!playing) return;
725   switch(playing->type & DISORDER_PLAYER_TYPEMASK) {
726   case DISORDER_PLAYER_STANDALONE:
727     if(!playing->type & DISORDER_PLAYER_PAUSES) {
728     default:
729       /* Shouldn't happen */
730       return;
731     }
732     play_resume(playing->pl, playing->data);
733     time(&playing->lastresumed);
734     break;
735   case DISORDER_PLAYER_RAW:
736     memset(&sm, 0, sizeof sm);
737     sm.type = SM_RESUME;
738     speaker_send(speaker_fd, &sm);
739     break;
740   }
741   if(who) info("resumed by %s", who);
742   notify_resume(playing->track, who);
743   if(playing->state == playing_paused)
744     playing->state = playing_started;
745   eventlog("state", "resume", (char *)0);
746 }
747
748 /*
749 Local Variables:
750 c-basic-offset:2
751 comment-column:40
752 fill-column:79
753 End:
754 */