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