2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
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.
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.
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
23 #include <sys/types.h>
32 #include <sys/socket.h>
41 #include "configuration.h"
55 #define SPEAKER "disorder-speaker"
57 struct queue_entry
*playing
;
60 static void finished(ev_source
*ev
);
62 static int speaker_fd
= -1;
63 static hash
*player_pids
;
64 static int shutting_down
;
66 static void store_player_pid(const char *id
, pid_t pid
) {
67 if(!player_pids
) player_pids
= hash_new(sizeof (pid_t
));
68 hash_add(player_pids
, id
, &pid
, HASH_INSERT_OR_REPLACE
);
71 static pid_t
find_player_pid(const char *id
) {
74 if(player_pids
&& (pidp
= hash_find(player_pids
, id
))) return *pidp
;
78 static void forget_player_pid(const char *id
) {
79 if(player_pids
) hash_remove(player_pids
, id
);
82 /* called when speaker process terminates */
83 static int speaker_terminated(ev_source
attribute((unused
)) *ev
,
84 pid_t
attribute((unused
)) pid
,
85 int attribute((unused
)) status
,
86 const struct rusage
attribute((unused
)) *rusage
,
87 void attribute((unused
)) *u
) {
89 error(0, "speaker subprocess terminated with status %s",
94 /* called when speaker process has something to say */
95 static int speaker_readable(ev_source
*ev
, int fd
,
96 void attribute((unused
)) *u
) {
97 struct speaker_message sm
;
98 int ret
= speaker_recv(fd
, &sm
, 0);
100 if(ret
< 0) return 0; /* EAGAIN */
102 ev_fd_cancel(ev
, ev_read
, fd
);
107 /* track ID is paused, DATA seconds played */
108 D(("SM_PAUSED %s %ld", sm
.id
, sm
.data
));
109 playing
->sofar
= sm
.data
;
112 /* the playing track finished */
113 D(("SM_FINISHED %s", sm
.id
));
117 /* track ID is playing, DATA seconds played */
118 D(("SM_PLAYING %s %ld", sm
.id
, sm
.data
));
119 playing
->sofar
= sm
.data
;
122 error(0, "unknown message type %d", sm
.type
);
127 void speaker_setup(ev_source
*ev
) {
131 if(socketpair(PF_UNIX
, SOCK_DGRAM
, 0, sp
) < 0)
132 fatal(errno
, "error calling socketpair");
134 lfd
= logfd(ev
, SPEAKER
);
137 if(!(pid
= xfork())) {
139 ev_signal_atfork(ev
);
148 signal(SIGPIPE
, SIG_DFL
);
150 execlp("valgrind", "valgrind", SPEAKER
, "--config", configfile
,
151 debugging ?
"--debug" : "--no-debug", (char *)0);
153 execlp(SPEAKER
, SPEAKER
, "--config", configfile
,
154 debugging ?
"--debug" : "--no-debug", (char *)0);
156 fatal(errno
, "error invoking %s", SPEAKER
);
158 ev_child(ev
, pid
, 0, speaker_terminated
, 0);
162 /* Don't need to make speaker_fd nonblocking because speaker_recv() uses
164 ev_fd(ev
, ev_read
, speaker_fd
, speaker_readable
, 0);
167 void speaker_reload(void) {
168 struct speaker_message sm
;
170 memset(&sm
, 0, sizeof sm
);
172 speaker_send(speaker_fd
, &sm
, -1);
175 /* timeout for play retry */
176 static int play_again(ev_source
*ev
,
177 const struct timeval
attribute((unused
)) *now
,
178 void attribute((unused
)) *u
) {
184 /* try calling play() again after @offset@ seconds */
185 static void retry_play(ev_source
*ev
, int offset
) {
188 D(("retry_play(%d)", offset
));
191 ev_timeout(ev
, 0, &w
, play_again
, 0);
194 /* Called when the currently playing track finishes playing. This
195 * might be because the player finished or because the speaker process
197 static void finished(ev_source
*ev
) {
198 D(("finished playing=%p", (void *)playing
));
201 if(playing
->state
!= playing_scratched
)
202 notify_not_scratched(playing
->track
, playing
->submitter
);
203 switch(playing
->state
) {
205 eventlog("completed", playing
->track
, (char *)0);
207 case playing_scratched
:
208 eventlog("scratched", playing
->track
, playing
->scratched
, (char *)0);
211 eventlog("failed", playing
->track
, wstat(playing
->wstat
), (char *)0);
216 queue_played(playing
);
218 forget_player_pid(playing
->id
);
220 if(ev
) retry_play(ev
, config
->gap
);
223 /* Called when a player terminates. */
224 static int player_finished(ev_source
*ev
,
227 const struct rusage
attribute((unused
)) *rusage
,
229 struct queue_entry
*q
= u
;
231 D(("player_finished pid=%lu status=%#x",
232 (unsigned long)pid
, (unsigned)status
));
233 /* Record that this PID is dead. If we killed the track we might know this
234 * already, but also it might have exited or crashed. Either way we don't
235 * want to end up signalling it. */
236 if(pid
== find_player_pid(q
->id
))
237 forget_player_pid(q
->id
);
239 case playing_unplayed
:
241 /* If this was an SM_PREPARE track then either it failed or we deliberately
242 * stopped it because it was removed from the queue or moved down it. So
243 * leave it state alone for future use. */
246 /* We actually started playing this track. */
248 if(q
->state
!= playing_scratched
)
249 q
->state
= playing_failed
;
251 q
->state
= playing_ok
;
254 /* Regardless we always report and record the status and do cleanup for
257 error(0, "player for %s %s", q
->track
, wstat(status
));
258 if(q
->type
& DISORDER_PLAYER_PREFORK
)
259 play_cleanup(q
->pl
, q
->data
);
261 /* If this actually was the current track, and does not use the speaker
262 * process, then it must have finished. For raw-output players we will get a
263 * separate notification from the speaker process. */
265 && (q
->type
& DISORDER_PLAYER_TYPEMASK
) != DISORDER_PLAYER_RAW
)
270 /* Find the player for Q */
271 static int find_player(const struct queue_entry
*q
) {
274 for(n
= 0; n
< config
->player
.n
; ++n
)
275 if(fnmatch(config
->player
.s
[n
].s
[0], q
->track
, 0) == 0)
277 if(n
>= config
->player
.n
)
283 /* Return values from start() */
284 #define START_OK 0 /* Succeeded. */
285 #define START_HARDFAIL 1 /* Track is broken. */
286 #define START_SOFTFAIL 2 /* Track OK, system (temporarily?) broken */
288 /* Play or prepare Q */
289 static int start(ev_source
*ev
,
290 struct queue_entry
*q
,
295 struct speaker_message sm
;
298 ao_sample_format format
;
302 const char *waitdevice
= 0;
303 const char *const *optv
;
306 memset(&sm
, 0, sizeof sm
);
307 if(find_player_pid(q
->id
) > 0) {
308 if(smop
== SM_PREPARE
) return START_OK
;
309 /* We have already sent an SM_PREPARE for this track so we just need to
310 * tell the speaker process to start actually playing the queued up audio
312 strcpy(sm
.id
, q
->id
);
314 speaker_send(speaker_fd
, &sm
, -1);
317 /* Find the player plugin. */
318 if((n
= find_player(q
)) < 0) return START_HARDFAIL
;
319 if(!(q
->pl
= open_plugin(config
->player
.s
[n
].s
[1], 0)))
320 return START_HARDFAIL
;
321 q
->type
= play_get_type(q
->pl
);
322 /* Can't prepare non-raw tracks. */
323 if(smop
== SM_PREPARE
324 && (q
->type
& DISORDER_PLAYER_TYPEMASK
) != DISORDER_PLAYER_RAW
)
326 /* Call the prefork function. */
327 p
= trackdb_rawpath(q
->track
);
328 if(q
->type
& DISORDER_PLAYER_PREFORK
)
329 if(!(q
->data
= play_prefork(q
->pl
, p
))) {
330 error(0, "prefork function for %s failed", q
->track
);
331 return START_HARDFAIL
;
333 /* Use the second arg as the tag if available (it's probably a command name),
334 * otherwise the module name. */
335 lfd
= logfd(ev
, (config
->player
.s
[n
].s
[2]
336 ? config
->player
.s
[n
].s
[2] : config
->player
.s
[n
].s
[1]));
337 optc
= config
->player
.s
[n
].n
- 2;
338 optv
= (void *)&config
->player
.s
[n
].s
[2];
339 while(optc
> 0 && optv
[0][0] == '-') {
340 if(!strcmp(optv
[0], "--")) {
345 if(!strcmp(optv
[0], "--wait-for-device")
346 || !strncmp(optv
[0], "--wait-for-device=", 18)) {
347 if((waitdevice
= strchr(optv
[0], '='))) {
350 waitdevice
= ""; /* use default */
354 error(0, "unknown option %s", optv
[0]);
355 return START_HARDFAIL
;
358 switch(pid
= fork()) {
361 ev_signal_atfork(ev
);
362 signal(SIGPIPE
, SIG_DFL
);
365 xclose(lfd
); /* tidy up */
367 if((q
->type
& DISORDER_PLAYER_TYPEMASK
) == DISORDER_PLAYER_RAW
) {
368 /* Raw format players write down a pipe (in fact a socket) to
369 * the speaker process. */
371 strcpy(sm
.id
, q
->id
);
372 if(socketpair(PF_UNIX
, SOCK_STREAM
, 0, sp
) < 0)
373 fatal(errno
, "error calling socketpair");
374 xshutdown(sp
[0], SHUT_WR
);
375 xshutdown(sp
[1], SHUT_RD
);
376 speaker_send(speaker_fd
, &sm
, sp
[0]);
377 /* Pass the file descriptor to the driver in an environment
379 snprintf(buffer
, sizeof buffer
, "DISORDER_RAW_FD=%d", sp
[1]);
380 if(putenv(buffer
) < 0)
381 fatal(errno
, "error calling putenv");
387 n
= ao_driver_id(waitdevice
);
389 fatal(0, "invalid libao driver: %s", optv
[0]);
391 n
= ao_default_driver_id();
392 /* Make up a format. */
393 memset(&format
, 0, sizeof format
);
397 format
.byte_format
= AO_FMT_NATIVE
;
400 ts
.tv_nsec
= 100000000; /* 0.1s */
401 while((device
= ao_open_live(n
, &format
, 0)) == 0 && retries
-- > 0)
412 error(errno
, "error calling fork");
413 if(q
->type
& DISORDER_PLAYER_PREFORK
)
414 play_cleanup(q
->pl
, q
->data
); /* else would leak */
416 return START_SOFTFAIL
;
418 store_player_pid(q
->id
, pid
);
421 ev_child(ev
, pid
, 0, player_finished
, q
);
422 D(("player subprocess ID %lu", (unsigned long)pid
));
426 int prepare(ev_source
*ev
,
427 struct queue_entry
*q
) {
430 /* Find the player plugin */
431 if(find_player_pid(q
->id
) > 0) return 0; /* Already going. */
432 if((n
= find_player(q
)) < 0) return -1; /* No player */
433 q
->pl
= open_plugin(config
->player
.s
[n
].s
[1], 0); /* No player */
434 q
->type
= play_get_type(q
->pl
);
435 if((q
->type
& DISORDER_PLAYER_TYPEMASK
) != DISORDER_PLAYER_RAW
)
436 return 0; /* Not a raw player */
437 return start(ev
, q
, SM_PREPARE
); /* Prepare it */
440 void abandon(ev_source
attribute((unused
)) *ev
,
441 struct queue_entry
*q
) {
442 struct speaker_message sm
;
443 pid_t pid
= find_player_pid(q
->id
);
445 if(pid
< 0) return; /* Not prepared. */
446 if((q
->type
& DISORDER_PLAYER_TYPEMASK
) != DISORDER_PLAYER_RAW
)
447 return; /* Not a raw player. */
448 /* Terminate the player. */
449 kill(-pid
, config
->signal
);
450 forget_player_pid(q
->id
);
451 /* Cancel the track. */
452 memset(&sm
, 0, sizeof sm
);
454 strcpy(sm
.id
, q
->id
);
455 speaker_send(speaker_fd
, &sm
, -1);
458 int add_random_track(void) {
459 struct queue_entry
*q
;
462 /* If random play is not enabled then do nothing. */
463 if(shutting_down
|| !random_is_enabled())
465 /* If there is already a random track, do nothing. */
466 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
)
467 if(q
->state
== playing_random
)
469 /* Try to pick a random track */
470 if(!(p
= trackdb_random(16)))
472 /* Add it to the end of the queue. */
473 q
= queue_add(p
, 0, WHERE_END
);
474 q
->state
= playing_random
;
475 /* Commit the queue */
477 D(("picked %p (%s) at random", (void *)q
, q
->track
));
481 /* try to play a track */
482 void play(ev_source
*ev
) {
483 struct queue_entry
*q
;
484 int random_enabled
= random_is_enabled();
486 D(("play playing=%p", (void *)playing
));
487 if(shutting_down
|| playing
|| !playing_is_enabled()) return;
488 /* If the queue is empty then add a random track. */
489 if(qhead
.next
== &qhead
) {
492 if(add_random_track()) {
493 /* On error, try again in 10s. */
497 /* Now there must be at least one track in the queue. */
500 /* If random play is disabled but the track is a random one then don't play
501 * it. play() will be called again when random play is re-enabled. */
502 if(!random_enabled
&& q
->state
== playing_random
)
504 D(("taken %p (%s) from queue", (void *)q
, q
->track
));
505 /* Try to start playing. */
506 switch(start(ev
, q
, SM_PLAY
)) {
508 if(q
== qhead
.next
) {
509 queue_remove(q
, 0); /* Abandon this track. */
513 if(qhead
.next
== &qhead
)
514 /* Queue is empty, wait a bit before trying something else (so we don't
515 * sit there looping madly in the presence of persistent problem). Note
516 * that we might not reliably get a random track lookahead in this case,
517 * but if we get here then really there are bigger problems. */
520 /* More in queue, try again now. */
524 /* Try same track again in a bit. */
528 if(q
== qhead
.next
) {
533 time(&playing
->played
);
534 playing
->state
= playing_started
;
535 notify_play(playing
->track
, playing
->submitter
);
536 eventlog("playing", playing
->track
,
537 playing
->submitter ? playing
->submitter
: (const char *)0,
539 /* Maybe add a random track. */
541 /* If there is another track in the queue prepare it now. This could
542 * potentially be a just-added random track. */
543 if(qhead
.next
!= &qhead
)
544 prepare(ev
, qhead
.next
);
549 int playing_is_enabled(void) {
550 const char *s
= trackdb_get_global("playing");
552 return !s
|| !strcmp(s
, "yes");
555 void enable_playing(const char *who
, ev_source
*ev
) {
556 trackdb_set_global("playing", "yes", who
);
557 /* Add a random track if necessary. */
562 void disable_playing(const char *who
) {
563 trackdb_set_global("playing", "no", who
);
566 int random_is_enabled(void) {
567 const char *s
= trackdb_get_global("random-play");
569 return !s
|| !strcmp(s
, "yes");
572 void enable_random(const char *who
, ev_source
*ev
) {
573 trackdb_set_global("random-play", "yes", who
);
578 void disable_random(const char *who
) {
579 trackdb_set_global("random-play", "no", who
);
582 void scratch(const char *who
, const char *id
) {
583 struct queue_entry
*q
;
584 struct speaker_message sm
;
587 D(("scratch playing=%p state=%d id=%s playing->id=%s",
589 playing ? playing
->state
: 0,
591 playing ? playing
->id
: "(none)"));
593 && (playing
->state
== playing_started
594 || playing
->state
== playing_paused
)
596 || !strcmp(id
, playing
->id
))) {
597 playing
->state
= playing_scratched
;
598 playing
->scratched
= who ?
xstrdup(who
) : 0;
599 if((pid
= find_player_pid(playing
->id
)) > 0) {
600 D(("kill -%d %lu", config
->signal
, (unsigned long)pid
));
601 kill(-pid
, config
->signal
);
602 forget_player_pid(playing
->id
);
604 error(0, "could not find PID for %s", playing
->id
);
605 if((playing
->type
& DISORDER_PLAYER_TYPEMASK
) == DISORDER_PLAYER_RAW
) {
606 memset(&sm
, 0, sizeof sm
);
608 strcpy(sm
.id
, playing
->id
);
609 speaker_send(speaker_fd
, &sm
, -1);
610 D(("sending SM_CANCEL for %s", playing
->id
));
612 /* put a scratch track onto the front of the queue (but don't
613 * bother if playing is disabled) */
614 if(playing_is_enabled() && config
->scratch
.n
) {
615 int r
= rand() * (double)config
->scratch
.n
/ (RAND_MAX
+ 1.0);
616 q
= queue_add(config
->scratch
.s
[r
], who
, WHERE_START
);
617 q
->state
= playing_isscratch
;
619 notify_scratch(playing
->track
, playing
->submitter
, who
,
620 time(0) - playing
->played
);
624 void quitting(ev_source
*ev
) {
625 struct queue_entry
*q
;
628 /* Don't start anything new */
630 /* Shut down the current player */
632 if((pid
= find_player_pid(playing
->id
)) > 0) {
633 kill(-pid
, config
->signal
);
634 forget_player_pid(playing
->id
);
636 error(0, "could not find PID for %s", playing
->id
);
637 playing
->state
= playing_quitting
;
640 /* Zap any other players */
641 for(q
= qhead
.next
; q
!= &qhead
; q
= q
->next
)
642 if((pid
= find_player_pid(q
->id
)) > 0) {
643 D(("kill -%d %lu", config
->signal
, (unsigned long)pid
));
644 kill(-pid
, config
->signal
);
645 forget_player_pid(q
->id
);
647 error(0, "could not find PID for %s", q
->id
);
648 /* Don't need the speaker any more */
649 ev_fd_cancel(ev
, ev_read
, speaker_fd
);
653 int pause_playing(const char *who
) {
654 struct speaker_message sm
;
657 /* Can't pause if already paused or if nothing playing. */
658 if(!playing
|| paused
) return 0;
659 switch(playing
->type
& DISORDER_PLAYER_TYPEMASK
) {
660 case DISORDER_PLAYER_STANDALONE
:
661 if(!(playing
->type
& DISORDER_PLAYER_PAUSES
)) {
663 error(0, "cannot pause because player is not powerful enough");
666 if(play_pause(playing
->pl
, &played
, playing
->data
)) {
667 error(0, "player indicates it cannot pause");
670 time(&playing
->lastpaused
);
671 playing
->uptopause
= played
;
672 playing
->lastresumed
= 0;
674 case DISORDER_PLAYER_RAW
:
675 memset(&sm
, 0, sizeof sm
);
677 speaker_send(speaker_fd
, &sm
, -1);
680 if(who
) info("paused by %s", who
);
681 notify_pause(playing
->track
, who
);
683 if(playing
->state
== playing_started
)
684 playing
->state
= playing_paused
;
685 eventlog("state", "pause", (char *)0);
689 void resume_playing(const char *who
) {
690 struct speaker_message sm
;
695 switch(playing
->type
& DISORDER_PLAYER_TYPEMASK
) {
696 case DISORDER_PLAYER_STANDALONE
:
697 if(!playing
->type
& DISORDER_PLAYER_PAUSES
) {
699 /* Shouldn't happen */
702 play_resume(playing
->pl
, playing
->data
);
703 time(&playing
->lastresumed
);
705 case DISORDER_PLAYER_RAW
:
706 memset(&sm
, 0, sizeof sm
);
708 speaker_send(speaker_fd
, &sm
, -1);
711 if(who
) info("resumed by %s", who
);
712 notify_resume(playing
->track
, who
);
713 if(playing
->state
== playing_paused
)
714 playing
->state
= playing_started
;
715 eventlog("state", "resume", (char *)0);