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