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