2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
39 struct pollfd *player_pfds;
40 static struct imsgbuf *ibuf;
42 static int stopped = 1;
43 static int nextfd = -1;
44 static int64_t samples;
45 static int64_t duration;
47 volatile sig_atomic_t halted;
50 player_signal_handler(int signo)
56 player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
60 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
61 bits, rate, channels);
65 /* don't stop if the parameters are the same */
66 if (bits == par.bits && channels == par.pchan &&
67 par.rate - fpct <= rate && rate <= par.rate + fpct) {
83 if (!sio_setpar(hdl, &par)) {
84 if (errno == EAGAIN) {
85 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
86 if (poll(player_pfds + 1, nfds, INFTIM) == -1)
90 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
91 bits, rate, channels);
94 if (!sio_getpar(hdl, &par)) {
95 log_warnx("can't get params");
99 if (par.bits != bits || par.pchan != channels) {
100 log_warnx("failed to set params");
104 /* TODO: check the sample rate? */
107 if (!sio_start(hdl)) {
108 log_warn("sio_start");
116 player_setduration(int64_t d)
121 seconds = duration / par.rate;
122 imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
127 player_onmove(void *arg, int delta)
129 static int64_t reported;
133 if (llabs(samples - reported) >= par.rate) {
135 sec = samples / par.rate;
137 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
143 player_setpos(int64_t pos)
146 player_onmove(NULL, 0);
149 /* process only one message */
151 player_dispatch(int64_t *s, int wait)
153 struct player_seek seek;
163 if ((n = imsg_get(ibuf, &imsg)) == -1)
171 if (poll(&pfd, 1, INFTIM) == -1)
173 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
176 fatalx("pipe closed");
181 switch (imsg.hdr.type) {
184 fatalx("track already enqueued");
185 if ((nextfd = imsg.fd) == -1)
186 fatalx("%s: got invalid file descriptor", __func__);
187 log_debug("song enqueued");
197 if (IMSG_DATA_SIZE(imsg) != sizeof(seek))
198 fatalx("wrong size for seek ctl");
199 memcpy(&seek, imsg.data, sizeof(seek));
201 *s = (double)seek.offset * (double)duration / 100.0;
203 *s = seek.offset * par.rate;
211 fatalx("unknown imsg %d", imsg.hdr.type);
219 player_senderr(const char *errstr)
224 len = strlen(errstr) + 1;
226 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
233 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
238 player_playnext(const char **errstr)
240 static char buf[512];
244 assert(nextfd != -1);
247 /* reset samples and set position to zero */
249 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
252 r = read(fd, buf, sizeof(buf));
254 /* 8 byte is the larger magic number */
256 *errstr = "read failed";
260 if (lseek(fd, 0, SEEK_SET) == -1) {
261 *errstr = "lseek failed";
265 if (memcmp(buf, "fLaC", 4) == 0)
266 return play_flac(fd, errstr);
267 if (memcmp(buf, "ID3", 3) == 0 ||
268 memcmp(buf, "\xFF\xFB", 2) == 0)
269 return play_mp3(fd, errstr);
270 if (memmem(buf, r, "OpusHead", 8) != NULL)
271 return play_opus(fd, errstr);
272 if (memmem(buf, r, "OggS", 4) != NULL)
273 return play_oggvorbis(fd, errstr);
275 *errstr = "unknown file type";
282 player_pause(int64_t *s)
286 r = player_dispatch(s, 1);
287 return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
291 player_shouldstop(int64_t *s, int wait)
293 switch (player_dispatch(s, wait)) {
306 play(const void *buf, size_t len, int64_t *s)
309 int nfds, revents, r, wait;
313 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
314 r = poll(player_pfds, nfds + 1, INFTIM);
318 wait = player_pfds[0].revents & (POLLHUP|POLLIN);
319 if (player_shouldstop(s, wait)) {
325 revents = sio_revents(hdl, player_pfds + 1);
326 if (revents & POLLHUP)
327 fatalx("sndio hang-up");
328 if (revents & POLLOUT) {
329 w = sio_write(hdl, buf, len);
339 player(int debug, int verbose)
343 log_init(debug, LOG_DAEMON);
344 log_setverbose(verbose);
346 setproctitle("player");
347 log_procinit("player");
358 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
361 sio_onmove(hdl, player_onmove, NULL);
363 /* allocate one extra for imsg */
364 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
365 if (player_pfds == NULL)
368 player_pfds[0].events = POLLIN;
369 player_pfds[0].fd = 3;
371 ibuf = xmalloc(sizeof(*ibuf));
374 signal(SIGINT, player_signal_handler);
375 signal(SIGTERM, player_signal_handler);
377 signal(SIGHUP, SIG_IGN);
378 signal(SIGPIPE, SIG_IGN);
380 if (pledge("stdio recvfd audio", NULL) == -1)
384 const char *errstr = NULL;
387 player_dispatch(NULL, 1);
389 r = player_playnext(&errstr);
391 player_senderr(errstr);