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