2 * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 struct pollfd *player_pfds;
38 static struct imsgbuf *imsgbuf;
40 static int nextfd = -1;
41 static int64_t samples;
42 static int64_t duration;
43 static unsigned int current_rate;
45 volatile sig_atomic_t halted;
48 player_signal_handler(int signo)
54 player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
56 log_debug("%s: bits=%u, rate=%u, channels=%u", __func__,
57 bits, rate, channels);
60 return audio_setup(bits, rate, channels, player_pfds + 1, player_nfds);
64 player_setduration(int64_t d)
69 seconds = duration / current_rate;
70 imsg_compose(imsgbuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
75 player_onmove(void *arg, int delta)
77 static int64_t reported;
81 if (llabs(samples - reported) >= current_rate) {
83 sec = samples / current_rate;
85 imsg_compose(imsgbuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
91 player_setpos(int64_t pos)
94 player_onmove(NULL, 0);
97 /* process only one message */
99 player_dispatch(int64_t *s, int wait)
101 struct player_seek seek;
111 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
117 pfd.fd = imsgbuf->fd;
119 if (poll(&pfd, 1, INFTIM) == -1)
121 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
124 fatalx("pipe closed");
128 ret = imsg_get_type(&imsg);
132 fatalx("track already enqueued");
133 if ((nextfd = imsg_get_fd(&imsg)) == -1)
134 fatalx("%s: got invalid file descriptor", __func__);
135 log_debug("song enqueued");
145 if (imsg_get_data(&imsg, &seek, sizeof(seek)) == -1)
146 fatalx("wrong size for seek ctl");
148 *s = (double)seek.offset * (double)duration / 100.0;
150 *s = seek.offset * current_rate;
157 fatalx("unknown imsg %d", ret);
165 player_senderr(const char *errstr)
170 len = strlen(errstr) + 1;
172 imsg_compose(imsgbuf, IMSG_ERR, 0, 0, -1, errstr, len);
179 imsg_compose(imsgbuf, IMSG_EOF, 0, 0, -1, NULL, 0);
184 player_playnext(const char **errstr)
186 static char buf[512];
190 assert(nextfd != -1);
193 /* reset samples and set position to zero */
195 imsg_compose(imsgbuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
198 r = read(fd, buf, sizeof(buf));
200 /* 8 byte is the larger magic number */
202 *errstr = "read failed";
206 if (lseek(fd, 0, SEEK_SET) == -1) {
207 *errstr = "lseek failed";
211 if (memcmp(buf, "fLaC", 4) == 0)
212 return play_flac(fd, errstr);
213 if (memcmp(buf, "ID3", 3) == 0 ||
214 memcmp(buf, "\xFF\xFB", 2) == 0)
215 return play_mp3(fd, errstr);
216 if (memmem(buf, r, "OpusHead", 8) != NULL)
217 return play_opus(fd, errstr);
218 if (memmem(buf, r, "OggS", 4) != NULL)
219 return play_oggvorbis(fd, errstr);
221 *errstr = "unknown file type";
228 player_pause(int64_t *s)
232 r = player_dispatch(s, 1);
233 return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
237 player_shouldstop(int64_t *s, int wait)
239 switch (player_dispatch(s, wait)) {
252 play(const void *buf, size_t len, int64_t *s)
255 int revents, r, wait;
259 audio_pollfd(player_pfds + 1, player_nfds, POLLOUT);
260 r = poll(player_pfds, player_nfds + 1, INFTIM);
264 wait = player_pfds[0].revents & (POLLHUP|POLLIN);
265 if (player_shouldstop(s, wait)) {
270 revents = audio_revents(player_pfds + 1, player_nfds);
271 if (revents & POLLHUP) {
274 fatal("audio hang-up");
276 if (revents & POLLOUT) {
277 w = audio_write(buf, len);
287 player(int debug, int verbose)
291 log_init(debug, LOG_DAEMON);
292 log_setverbose(verbose);
294 setproctitle("player");
295 log_procinit("player");
306 if (audio_open(player_onmove) == -1)
309 if ((player_nfds = audio_nfds()) <= 0)
310 fatal("audio_nfds: invalid number of file descriptors: %d",
313 /* allocate one extra for imsg */
314 player_pfds = calloc(player_nfds + 1, sizeof(*player_pfds));
315 if (player_pfds == NULL)
318 player_pfds[0].events = POLLIN;
319 player_pfds[0].fd = 3;
321 imsgbuf = xmalloc(sizeof(*imsgbuf));
322 imsg_init(imsgbuf, 3);
324 signal(SIGINT, player_signal_handler);
325 signal(SIGTERM, player_signal_handler);
327 signal(SIGHUP, SIG_IGN);
328 signal(SIGPIPE, SIG_IGN);
330 if (pledge("stdio recvfd audio", NULL) == -1)
334 const char *errstr = NULL;
337 player_dispatch(NULL, 1);
339 r = player_playnext(&errstr);
341 player_senderr(errstr);