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