chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / server / play.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2012 Richard Kettlewell
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 3 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,
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 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18/** @file server/play.c
19 * @brief Playing tracks
20 *
21 * This file is rather badly organized. Sorry. It's better than it was...
22 */
23
24#include "disorder-server.h"
25
26#define SPEAKER "disorder-speaker"
27
28/** @brief The current playing track or NULL */
29struct queue_entry *playing;
30
31/** @brief Set when paused */
32int paused;
33
34static void finished(ev_source *ev);
35static int start_child(struct queue_entry *q,
36 const struct pbgc_params *params,
37 void attribute((unused)) *bgdata);
38static int prepare_child(struct queue_entry *q,
39 const struct pbgc_params *params,
40 void attribute((unused)) *bgdata);
41static void ensure_next_scratch(ev_source *ev);
42
43/** @brief File descriptor of our end of the socket to the speaker */
44static int speaker_fd = -1;
45
46/** @brief Set when shutting down */
47static int shutting_down;
48
49/* Speaker ------------------------------------------------------------------ */
50
51/** @brief Called when speaker process terminates
52 *
53 * Currently kills of DisOrder completely. A future version could terminate
54 * the speaker when nothing was going on, or recover from failures, though any
55 * tracks with decoders already started would need to have them restarted.
56 */
57static int speaker_terminated(ev_source attribute((unused)) *ev,
58 pid_t attribute((unused)) pid,
59 int attribute((unused)) status,
60 const struct rusage attribute((unused)) *rusage,
61 void attribute((unused)) *u) {
62 disorder_fatal(0, "speaker subprocess %s", wstat(status));
63}
64
65/** @brief Called when we get a message from the speaker process */
66static int speaker_readable(ev_source *ev, int fd,
67 void attribute((unused)) *u) {
68 struct speaker_message sm;
69 int ret = speaker_recv(fd, &sm);
70
71 if(ret < 0) return 0; /* EAGAIN */
72 if(!ret) { /* EOF */
73 ev_fd_cancel(ev, ev_read, fd);
74 return 0;
75 }
76 switch(sm.type) {
77 case SM_PAUSED:
78 /* track ID is paused, DATA seconds played */
79 D(("SM_PAUSED %s %ld", sm.u.id, sm.data));
80 playing->sofar = sm.data;
81 break;
82 case SM_FINISHED: /* scratched the playing track */
83 case SM_STILLBORN: /* scratched too early */
84 case SM_UNKNOWN: /* scratched WAY too early */
85 if(playing && !strcmp(sm.u.id, playing->id)) {
86 if((playing->state == playing_unplayed
87 || playing->state == playing_started)
88 && sm.type == SM_FINISHED)
89 playing->state = playing_ok;
90 finished(ev);
91 }
92 break;
93 case SM_PLAYING:
94 /* track ID is playing, DATA seconds played */
95 D(("SM_PLAYING %s %ld", sm.u.id, sm.data));
96 playing->sofar = sm.data;
97 break;
98 case SM_ARRIVED: {
99 /* track ID is now prepared */
100 struct queue_entry *q;
101 for(q = qhead.next; q != &qhead && strcmp(q->id, sm.u.id); q = q->next)
102 ;
103 if(q && q->preparing) {
104 q->preparing = 0;
105 q->prepared = 1;
106 /* We might be waiting to play the now-prepared track */
107 play(ev);
108 }
109 break;
110 }
111 default:
112 disorder_error(0, "unknown speaker message type %d", sm.type);
113 }
114 return 0;
115}
116
117/** @brief Initialize the speaker process */
118void speaker_setup(ev_source *ev) {
119 int sp[2];
120 pid_t pid;
121 struct speaker_message sm;
122
123 if(socketpair(PF_UNIX, SOCK_DGRAM, 0, sp) < 0)
124 disorder_fatal(errno, "error calling socketpair");
125 if(!(pid = xfork())) {
126 exitfn = _exit;
127 ev_signal_atfork(ev);
128 xdup2(sp[0], 0);
129 xdup2(sp[0], 1);
130 xclose(sp[0]);
131 xclose(sp[1]);
132 signal(SIGPIPE, SIG_DFL);
133#if 0
134 execlp("valgrind", "valgrind", SPEAKER, "--config", configfile,
135 debugging ? "--debug" : "--no-debug",
136 log_default == &log_syslog ? "--syslog" : "--no-syslog",
137 (char *)0);
138#else
139 execlp(SPEAKER, SPEAKER, "--config", configfile,
140 debugging ? "--debug" : "--no-debug",
141 log_default == &log_syslog ? "--syslog" : "--no-syslog",
142 (char *)0);
143#endif
144 disorder_fatal(errno, "error invoking %s", SPEAKER);
145 }
146 ev_child(ev, pid, 0, speaker_terminated, 0);
147 speaker_fd = sp[1];
148 xclose(sp[0]);
149 cloexec(speaker_fd);
150 /* Wait for the speaker to be ready */
151 speaker_recv(speaker_fd, &sm);
152 nonblock(speaker_fd);
153 if(ev_fd(ev, ev_read, speaker_fd, speaker_readable, 0, "speaker read") < 0)
154 disorder_fatal(0, "error registering speaker socket fd");
155}
156
157/** @brief Tell the speaker to reload its configuration */
158void speaker_reload(void) {
159 struct speaker_message sm;
160
161 memset(&sm, 0, sizeof sm);
162 sm.type = SM_RELOAD;
163 speaker_send(speaker_fd, &sm);
164}
165
166/* Track termination -------------------------------------------------------- */
167
168/** @brief Called when the currently playing track finishes playing
169 * @param ev Event loop or NULL
170 *
171 * There are three places this is called from:
172 *
173 * 1) speaker_readable(), when the speaker tells us the playing track finished.
174 * (Technically the speaker lies a little to arrange for gapless play.)
175 *
176 * 2) player_finished(), when the player for a non-raw track (i.e. one that
177 * does not use the speaker) finishes.
178 *
179 * 3) quitting(), after signalling the decoder or player but possible before it
180 * has actually terminated. In this case @p ev is NULL, inhibiting any further
181 * attempt to play anything.
182 */
183static void finished(ev_source *ev) {
184 D(("finished playing=%p", (void *)playing));
185 if(!playing)
186 return;
187 if(playing->state != playing_scratched)
188 notify_not_scratched(playing->track, playing->submitter);
189 switch(playing->state) {
190 case playing_ok:
191 eventlog("completed", playing->track, (char *)0);
192 break;
193 case playing_scratched:
194 eventlog("scratched", playing->track, playing->scratched, (char *)0);
195 break;
196 case playing_failed:
197 eventlog("failed", playing->track, wstat(playing->wstat), (char *)0);
198 break;
199 default:
200 break;
201 }
202 queue_played(playing);
203 recent_write();
204 playing = 0;
205 /* Try to play something else */
206 if(ev)
207 play(ev);
208}
209
210/** @brief Called when a player or decoder process terminates
211 *
212 * This is called when a decoder process terminates (which might actually be
213 * some time before the speaker reports it as finished) or when a non-raw
214 * (i.e. non-speaker) player terminates. In the latter case it's imaginable
215 * that the OS has buffered the last few samples.
216 *
217 * NB. The finished track might NOT be in the queue (yet) - it might be a
218 * pre-chosen scratch.
219 */
220static int player_finished(ev_source *ev,
221 pid_t pid,
222 int status,
223 const struct rusage attribute((unused)) *rusage,
224 void *u) {
225 struct queue_entry *q = u;
226
227 D(("player_finished pid=%lu status=%#x",
228 (unsigned long)pid, (unsigned)status));
229 /* Record that this PID is dead. If we killed the track we might know this
230 * already, but also it might have exited or crashed. Either way we don't
231 * want to end up signalling it. */
232 q->pid = -1;
233 switch(q->state) {
234 case playing_unplayed:
235 case playing_random:
236 /* If this was a pre-prepared track then either it failed or we
237 * deliberately stopped it: it might have been removed from the queue, or
238 * moved down the queue, or the speaker might be on a break. So we leave
239 * it state alone for future use.
240 */
241 break;
242 default:
243 /* We actually started playing this track. */
244 if(status) {
245 /* Don't override 'scratched' with 'failed'. */
246 if(q->state != playing_scratched)
247 q->state = playing_failed;
248 } else
249 q->state = playing_ok;
250 break;
251 }
252 /* Report the status unless we killed it */
253 if(status) {
254 if(!(q->killed && WIFSIGNALED(status) && WTERMSIG(status) == q->killed))
255 disorder_error(0, "player for %s %s", q->track, wstat(status));
256 }
257 /* Clean up any prefork calls */
258 if(q->type & DISORDER_PLAYER_PREFORK)
259 play_cleanup(q->pl, q->data);
260 q->wstat = status;
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. */
264 if(q == playing
265 && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW)
266 finished(ev);
267 return 0;
268}
269
270/* Track initiation --------------------------------------------------------- */
271
272/** @brief Find the player for @p q */
273static const struct stringlist *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 NULL;
281 else
282 return &config->player.s[n];
283}
284
285/** @brief Start to play @p q
286 * @param ev Event loop
287 * @param q Track to play/prepare
288 * @return @ref START_OK, @ref START_HARDFAIL or @ref START_SOFTFAIL
289 *
290 * This makes @p actually start playing. It calls prepare() if necessary and
291 * either sends an @ref SM_PLAY command or invokes the player itself in a
292 * subprocess.
293 *
294 * It's up to the caller to set @ref playing and @c playing->state (this might
295 * be changed in the future).
296 */
297static int start(ev_source *ev,
298 struct queue_entry *q) {
299 const struct stringlist *player;
300 int rc;
301
302 D(("start %s", q->id));
303 /* Find the player plugin. */
304 if(!(player = find_player(q)))
305 return START_HARDFAIL; /* No player */
306 if(!(q->pl = open_plugin(player->s[1], 0)))
307 return START_HARDFAIL;
308 q->type = play_get_type(q->pl);
309 /* Special handling for raw-format players */
310 if((q->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) {
311 /* Make sure that the track is prepared */
312 if((rc = prepare(ev, q)))
313 return rc;
314 /* Now we're sure it's prepared, start it playing */
315 /* TODO actually it might not be fully prepared yet - it's all happening in
316 * a subprocess. See speaker.c for further discussion. */
317 struct speaker_message sm[1];
318 memset(sm, 0, sizeof sm);
319 strcpy(sm->u.id, q->id);
320 sm->type = SM_PLAY;
321 speaker_send(speaker_fd, sm);
322 D(("sent SM_PLAY for %s", sm->u.id));
323 /* Our caller will set playing and playing->state = playing_started */
324 return START_OK;
325 } else {
326 rc = play_background(ev, player, q, start_child, NULL);
327 if(rc == START_OK)
328 ev_child(ev, q->pid, 0, player_finished, q);
329 /* Our caller will set playing and playing->state = playing_started */
330 return rc;
331 }
332}
333
334/** @brief Child-process half of start()
335 * @return Process exit code
336 *
337 * Called in subprocess to execute non-raw-format players (via plugin).
338 */
339static int start_child(struct queue_entry *q,
340 const struct pbgc_params *params,
341 void attribute((unused)) *bgdata) {
342 /* Play the track */
343 play_track(q->pl,
344 params->argv, params->argc,
345 params->rawpath,
346 q->track);
347 return 0;
348}
349
350/** @brief Prepare a track for later play
351 * @return @ref START_OK, @ref START_HARDFAIL or @ref START_SOFTFAIL
352 *
353 * This can be called either when we want to play the track or slightly before
354 * so that some samples are decoded and available in a buffer.
355 *
356 * Only applies to raw-format (i.e. speaker-using) players; everything else
357 * gets @c START_OK.
358 */
359int prepare(ev_source *ev,
360 struct queue_entry *q) {
361 const struct stringlist *player;
362
363 /* If there's a decoder (or player!) going we do nothing */
364 if(q->pid >= 0)
365 return START_OK;
366 /* If the track is already prepared, do nothing */
367 if(q->prepared || q->preparing)
368 return START_OK;
369 /* Find the player plugin */
370 if(!(player = find_player(q)))
371 return START_HARDFAIL; /* No player */
372 q->pl = open_plugin(player->s[1], 0);
373 q->type = play_get_type(q->pl);
374 if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW)
375 return START_OK; /* Not a raw player */
376 int rc = play_background(ev, player, q, prepare_child, NULL);
377 if(rc == START_OK) {
378 ev_child(ev, q->pid, 0, player_finished, q);
379 q->preparing = 1;
380 /* Actually the track is still "in flight" */
381 rc = START_SOFTFAIL;
382 }
383 return rc;
384}
385
386/** @brief Child-process half of prepare()
387 * @return Process exit code
388 *
389 * Called in subprocess to execute the decoder for a raw-format player.
390 *
391 * @todo We currently run the normalizer from here in a double-fork. This is
392 * unsatisfactory for many reasons: we can't prevent it outliving the main
393 * server and we don't adequately report its exit status.
394 */
395static int prepare_child(struct queue_entry *q,
396 const struct pbgc_params *params,
397 void attribute((unused)) *bgdata) {
398 /* np will be the pipe to disorder-normalize */
399 int np[2];
400 if(socketpair(PF_UNIX, SOCK_STREAM, 0, np) < 0)
401 disorder_fatal(errno, "error calling socketpair");
402 /* Beware of the Leopard! On OS X 10.5.x, the order of the shutdown
403 * calls here DOES MATTER. If you do the SHUT_WR first then the SHUT_RD
404 * fails with "Socket is not connected". I think this is a bug but
405 * provided implementors either don't care about the order or all agree
406 * about the order, choosing the reliable order is an adequate
407 * workaround. */
408 xshutdown(np[1], SHUT_RD); /* decoder writes to np[1] */
409 xshutdown(np[0], SHUT_WR); /* normalize reads from np[0] */
410 blocking(np[0]);
411 blocking(np[1]);
412 /* Start disorder-normalize. We double-fork so that nothing has to wait
413 * for disorder-normalize. */
414 pid_t npid;
415 if(!(npid = xfork())) {
416 /* Grandchild of disorderd */
417 if(!xfork()) {
418 /* Great-grandchild of disorderd */
419 /* Connect to the speaker process */
420 struct sockaddr_un addr;
421 memset(&addr, 0, sizeof addr);
422 addr.sun_family = AF_UNIX;
423 snprintf(addr.sun_path, sizeof addr.sun_path,
424 "%s/private/speaker", config->home);
425 int sfd = xsocket(PF_UNIX, SOCK_STREAM, 0);
426 if(connect(sfd, (const struct sockaddr *)&addr, sizeof addr) < 0)
427 disorder_fatal(errno, "connecting to %s", addr.sun_path);
428 /* Send the ID, with a NATIVE-ENDIAN 32 bit length */
429 uint32_t l = strlen(q->id);
430 if(write(sfd, &l, sizeof l) < 0
431 || write(sfd, q->id, l) < 0)
432 disorder_fatal(errno, "writing to %s", addr.sun_path);
433 /* Await the ack */
434 if (read(sfd, &l, 1) < 0)
435 disorder_fatal(errno, "reading ack from %s", addr.sun_path);
436 /* Plumbing */
437 xdup2(np[0], 0);
438 xdup2(sfd, 1);
439 xclose(np[0]);
440 xclose(np[1]);
441 xclose(sfd);
442 /* TODO stderr shouldn't be redirected for disorder-normalize
443 * (but it should be for play_track() */
444 execlp("disorder-normalize", "disorder-normalize",
445 log_default == &log_syslog ? "--syslog" : "--no-syslog",
446 "--config", configfile,
447 (char *)0);
448 disorder_fatal(errno, "executing disorder-normalize");
449 /* End of the great-grandchild of disorderd */
450 }
451 /* Back in the grandchild of disorderd */
452 _exit(0);
453 /* End of the grandchild of disorderd */
454 }
455 /* Back in the child of disorderd */
456 /* Wait for the grandchild of disordered to finish */
457 int n;
458 while(waitpid(npid, &n, 0) < 0 && errno == EINTR)
459 ;
460 /* Pass the file descriptor to the driver in an environment
461 * variable. */
462 char buffer[64];
463 snprintf(buffer, sizeof buffer, "DISORDER_RAW_FD=%d", np[1]);
464 if(putenv(buffer) < 0)
465 disorder_fatal(errno, "error calling putenv");
466 /* Close all the FDs we don't need */
467 xclose(np[0]);
468 /* Start the decoder itself */
469 play_track(q->pl,
470 params->argv, params->argc,
471 params->rawpath,
472 q->track);
473 return 0;
474}
475
476/** @brief Kill a player
477 * @param q Queue entry corresponding to player
478 */
479static void kill_player(struct queue_entry *q) {
480 if(q->pid >= 0)
481 kill(-q->pid, config->signal);
482 q->killed = config->signal;
483}
484
485/** @brief Abandon a queue entry
486 *
487 * Called from c_remove() (but NOT when scratching a track). Only does
488 * anything to raw-format tracks. Terminates the background decoder and tells
489 * the speaker process to cancel the track.
490 */
491void abandon(ev_source attribute((unused)) *ev,
492 struct queue_entry *q) {
493 struct speaker_message sm;
494
495 if(q->pid < 0)
496 return; /* Not prepared. */
497 if((q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW)
498 return; /* Not a raw player. */
499 /* Terminate the player. */
500 kill_player(q);
501 /* Cancel the track. */
502 memset(&sm, 0, sizeof sm);
503 sm.type = SM_CANCEL;
504 strcpy(sm.u.id, q->id);
505 speaker_send(speaker_fd, &sm);
506}
507
508/* Random tracks ------------------------------------------------------------ */
509
510/** @brief Called with a new random track
511 * @param ev Event loop
512 * @param track Track name
513 */
514static void chosen_random_track(ev_source *ev,
515 const char *track) {
516 struct queue_entry *q;
517
518 if(!track)
519 return;
520 /* Add the track to the queue */
521 q = queue_add(track, 0, WHERE_END, NULL, origin_random);
522 D(("picked %p (%s) at random", (void *)q, q->track));
523 queue_write();
524 /* Maybe a track can now be played */
525 play(ev);
526}
527
528/** @brief Maybe add a randomly chosen track
529 * @param ev Event loop
530 *
531 * Picking can take some time so the track will only be added after this
532 * function has returned.
533 */
534void add_random_track(ev_source *ev) {
535 struct queue_entry *q;
536 long qlen = 0;
537
538 /* If random play is not enabled then do nothing. */
539 if(shutting_down || !random_is_enabled())
540 return;
541 /* Count how big the queue is */
542 for(q = qhead.next; q != &qhead; q = q->next)
543 ++qlen;
544 /* If it's smaller than the desired size then add a track */
545 if(qlen < config->queue_pad)
546 trackdb_request_random(ev, chosen_random_track);
547}
548
549/* Track initiation (part 2) ------------------------------------------------ */
550
551/** @brief Attempt to play something
552 *
553 * This is called from numerous locations - whenever it might conceivably have
554 * become possible to play something.
555 */
556void play(ev_source *ev) {
557 struct queue_entry *q;
558 int random_enabled = random_is_enabled();
559
560 D(("play playing=%p", (void *)playing));
561 /* If we're shutting down, or there's something playing, or playing is not
562 * enabled, give up now */
563 if(shutting_down || playing || !playing_is_enabled()) return;
564 /* See if there's anything to play */
565 if(qhead.next == &qhead) {
566 /* Queue is empty. We could just wait around since there are periodic
567 * attempts to add a random track anyway. However they are rarer than
568 * attempts to force a track so we initiate one now. */
569 add_random_track(ev);
570 /* chosen_random_track() will call play() when a new random track has been
571 * added to the queue. */
572 return;
573 }
574 /* There must be at least one track in the queue. */
575 q = qhead.next;
576 /* If random play is disabled but the track is a non-adopted random one
577 * then don't play it. play() will be called again when random play is
578 * re-enabled. */
579 if(!random_enabled && q->origin == origin_random)
580 return;
581 D(("taken %p (%s) from queue", (void *)q, q->track));
582 /* Try to start playing. */
583 switch(start(ev, q)) {
584 case START_HARDFAIL:
585 if(q == qhead.next) {
586 queue_remove(q, 0); /* Abandon this track. */
587 queue_played(q);
588 recent_write();
589 }
590 /* Oh well, try the next one */
591 play(ev);
592 break;
593 case START_SOFTFAIL:
594 /* We'll try the same track again shortly. */
595 break;
596 case START_OK:
597 /* Remove from the queue */
598 if(q == qhead.next) {
599 queue_remove(q, 0);
600 queue_write();
601 }
602 /* It's become the playing track */
603 playing = q;
604 xtime(&playing->played);
605 playing->state = playing_started;
606 notify_play(playing->track, playing->submitter);
607 eventlog("playing", playing->track,
608 playing->submitter ? playing->submitter : (const char *)0,
609 (const char *)0);
610 /* Maybe add a random track. */
611 add_random_track(ev);
612 /* If there is another track in the queue prepare it now. This could
613 * potentially be a just-added random track. */
614 if(qhead.next != &qhead)
615 prepare(ev, qhead.next);
616 /* Make sure there is a prepared scratch */
617 ensure_next_scratch(ev);
618 break;
619 }
620}
621
622/* Miscelleneous ------------------------------------------------------------ */
623
624int flag_enabled(const char *s) {
625 return !s || !strcmp(s, "yes");
626}
627
628/** @brief Return true if play is enabled */
629int playing_is_enabled(void) {
630 return flag_enabled(trackdb_get_global("playing"));
631}
632
633/** @brief Enable play */
634void enable_playing(const char *who, ev_source *ev) {
635 trackdb_set_global("playing", "yes", who);
636 /* Add a random track if necessary. */
637 add_random_track(ev);
638 play(ev);
639}
640
641/** @brief Disable play */
642void disable_playing(const char *who, ev_source attribute((unused)) *ev) {
643 trackdb_set_global("playing", "no", who);
644}
645
646/** @brief Return true if random play is enabled */
647int random_is_enabled(void) {
648 return flag_enabled(trackdb_get_global("random-play"));
649}
650
651/** @brief Enable random play */
652void enable_random(const char *who, ev_source *ev) {
653 trackdb_set_global("random-play", "yes", who);
654 add_random_track(ev);
655 play(ev);
656}
657
658/** @brief Disable random play */
659void disable_random(const char *who, ev_source attribute((unused)) *ev) {
660 trackdb_set_global("random-play", "no", who);
661}
662
663/* Scratching --------------------------------------------------------------- */
664
665/** @brief Track to play next time something is scratched */
666static struct queue_entry *next_scratch;
667
668/** @brief Ensure there isa prepared scratch */
669static void ensure_next_scratch(ev_source *ev) {
670 if(next_scratch) /* There's one already */
671 return;
672 if(!config->scratch.n) /* There are no scratches */
673 return;
674 int r = rand() * (double)config->scratch.n / (RAND_MAX + 1.0);
675 next_scratch = queue_add(config->scratch.s[r], NULL,
676 WHERE_NOWHERE, NULL, origin_scratch);
677 if(ev)
678 prepare(ev, next_scratch);
679}
680
681/** @brief Scratch a track
682 * @param who User responsible (or NULL)
683 * @param id Track ID (or NULL for current)
684 */
685void scratch(const char *who, const char *id) {
686 struct speaker_message sm;
687
688 D(("scratch playing=%p state=%d id=%s playing->id=%s",
689 (void *)playing,
690 playing ? playing->state : 0,
691 id ? id : "(none)",
692 playing ? playing->id : "(none)"));
693 /* There must be a playing track; it must be in a scratchable state; if a
694 * specific ID was mentioned it must be that track. */
695 if(playing
696 && (playing->state == playing_started
697 || playing->state == playing_paused)
698 && (!id
699 || !strcmp(id, playing->id))) {
700 /* Update state (for the benefit of the 'recent' list) */
701 playing->state = playing_scratched;
702 playing->scratched = who ? xstrdup(who) : 0;
703 /* Find the player and kill the whole process group */
704 if(playing->pid >= 0)
705 kill_player(playing);
706 /* Tell the speaker, if we think it'll care */
707 if((playing->type & DISORDER_PLAYER_TYPEMASK) == DISORDER_PLAYER_RAW) {
708 memset(&sm, 0, sizeof sm);
709 sm.type = SM_CANCEL;
710 strcpy(sm.u.id, playing->id);
711 speaker_send(speaker_fd, &sm);
712 D(("sending SM_CANCEL for %s", playing->id));
713 }
714 /* If playing is enabled then add a scratch to the queue. Having a scratch
715 * appear in the queue when further play is disabled is weird and
716 * contradicts implicit assumptions made elsewhere, so we try to avoid
717 * it. */
718 if(playing_is_enabled()) {
719 /* Try to make sure there is a scratch */
720 ensure_next_scratch(NULL);
721 /* Insert it at the head of the queue */
722 if(next_scratch){
723 next_scratch->submitter = who;
724 queue_insert_entry(&qhead, next_scratch);
725 eventlog_raw("queue", queue_marshall(next_scratch), (const char *)0);
726 next_scratch = NULL;
727 }
728 }
729 notify_scratch(playing->track, playing->submitter, who,
730 xtime(0) - playing->played);
731 }
732}
733
734/* Server termination ------------------------------------------------------- */
735
736/** @brief Called from quit() to tear down everything belonging to this file */
737void quitting(ev_source *ev) {
738 struct queue_entry *q;
739
740 /* Don't start anything new */
741 shutting_down = 1;
742 /* Shut down the current player */
743 if(playing) {
744 kill_player(playing);
745 playing->state = playing_quitting;
746 finished(0);
747 }
748 /* Zap any background decoders that are going */
749 for(q = qhead.next; q != &qhead; q = q->next)
750 kill_player(q);
751 /* Don't need the speaker any more */
752 ev_fd_cancel(ev, ev_read, speaker_fd);
753 xclose(speaker_fd);
754}
755
756/* Pause and resume --------------------------------------------------------- */
757
758/** @brief Pause the playing track */
759int pause_playing(const char *who) {
760 struct speaker_message sm;
761 long played;
762
763 /* Can't pause if already paused or if nothing playing. */
764 if(!playing || paused) return 0;
765 switch(playing->type & DISORDER_PLAYER_TYPEMASK) {
766 case DISORDER_PLAYER_STANDALONE:
767 if(!(playing->type & DISORDER_PLAYER_PAUSES)) {
768 default:
769 disorder_error(0, "cannot pause because player is not powerful enough");
770 return -1;
771 }
772 if(play_pause(playing->pl, &played, playing->data)) {
773 disorder_error(0, "player indicates it cannot pause");
774 return -1;
775 }
776 xtime(&playing->lastpaused);
777 playing->uptopause = played;
778 playing->lastresumed = 0;
779 break;
780 case DISORDER_PLAYER_RAW:
781 memset(&sm, 0, sizeof sm);
782 sm.type = SM_PAUSE;
783 speaker_send(speaker_fd, &sm);
784 break;
785 }
786 if(who)
787 disorder_info("paused by %s", who);
788 notify_pause(playing->track, who);
789 paused = 1;
790 if(playing->state == playing_started)
791 playing->state = playing_paused;
792 eventlog("state", "pause", (char *)0);
793 return 0;
794}
795
796/** @brief Resume playing after a pause */
797void resume_playing(const char *who) {
798 struct speaker_message sm;
799
800 if(!paused) return;
801 paused = 0;
802 if(!playing) return;
803 switch(playing->type & DISORDER_PLAYER_TYPEMASK) {
804 case DISORDER_PLAYER_STANDALONE:
805 if(!(playing->type & DISORDER_PLAYER_PAUSES)) {
806 default:
807 /* Shouldn't happen */
808 return;
809 }
810 play_resume(playing->pl, playing->data);
811 xtime(&playing->lastresumed);
812 break;
813 case DISORDER_PLAYER_RAW:
814 memset(&sm, 0, sizeof sm);
815 sm.type = SM_RESUME;
816 speaker_send(speaker_fd, &sm);
817 break;
818 }
819 if(who) disorder_info("resumed by %s", who);
820 notify_resume(playing->track, who);
821 if(playing->state == playing_paused)
822 playing->state = playing_started;
823 eventlog("state", "resume", (char *)0);
824}
825
826/** @brief Request an RTP stream */
827void rtp_request(const struct sockaddr_storage *sa) {
828 struct speaker_message sm;
829 memset(&sm, 0, sizeof sm);
830 sm.type = SM_RTP_REQUEST;
831 sm.u.address = *sa;
832 speaker_send(speaker_fd, &sm);
833}
834
835/** @brief Cancel an RTP stream */
836void rtp_request_cancel(const struct sockaddr_storage *sa) {
837 struct speaker_message sm;
838 memset(&sm, 0, sizeof sm);
839 sm.type = SM_RTP_CANCEL;
840 sm.u.address = *sa;
841 speaker_send(speaker_fd, &sm);
842}
843
844/*
845Local Variables:
846c-basic-offset:2
847comment-column:40
848fill-column:79
849indent-tabs-mode:nil
850End:
851*/