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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
41 static struct imsgbuf *ibuf;
44 static int nextfd = -1;
45 static char nextpath[PATH_MAX];
47 volatile sig_atomic_t halted;
50 player_signal_handler(int signo)
60 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
65 par.appbufsz = 50 * par.rate / 1000;
67 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
68 fatal("couldn't set audio params");
69 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
70 fatalx("unsupported audio params");
76 player_setrate(int rate)
80 log_debug("switching to sample rate %d", rate);
86 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
87 log_warnx("invalid params");
91 /* TODO: check the sample rate? */
93 if (!sio_start(hdl)) {
94 log_warn("sio_start");
101 player_pendingimsg(void)
112 r = poll(&pfd, 1, 0);
119 player_enqueue(struct imsg *imsg)
124 fatalx("track already enqueued");
126 datalen = IMSG_DATA_SIZE(*imsg);
127 if (datalen != sizeof(nextpath))
128 fatalx("%s: size mismatch", __func__);
129 memcpy(nextpath, imsg->data, sizeof(nextpath));
130 if (nextpath[datalen-1] != '\0')
131 fatalx("%s: corrupted path", __func__);
132 if ((nextfd = imsg->fd) == -1)
133 fatalx("%s: got invalid file descriptor", __func__);
134 log_debug("enqueued %s", nextpath);
137 /* process only one message */
139 player_dispatch(void)
148 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
151 fatalx("pipe closed");
153 if ((n = imsg_get(ibuf, &imsg)) == -1)
155 if (n == 0) /* no more messages */
156 fatalx("expected at least a message");
159 if (ret == IMSG_STOP)
161 switch (imsg.hdr.type) {
163 player_enqueue(&imsg);
171 fatalx("unknown imsg %d", imsg.hdr.type);
180 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
187 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
192 player_playnext(void)
196 assert(nextfd != -1);
199 /* XXX: use magic(5) for this, not file extensions */
200 if (strstr(nextpath, ".ogg") != NULL)
202 else if (strstr(nextpath, ".mp3") != NULL)
204 else if (strstr(nextpath, ".flac") != NULL)
206 else if (strstr(nextpath, ".opus") != NULL)
209 log_warnx("unknown file type for %s", nextpath);
220 r = player_dispatch();
221 return r == IMSG_RESUME;
225 player_shouldstop(void)
227 if (!player_pendingimsg())
230 switch (player_dispatch()) {
243 player(int debug, int verbose)
246 log_init(debug, LOG_DAEMON);
247 log_setverbose(verbose);
249 setproctitle("player");
250 log_procinit("player");
263 /* mark fd as blocking i/o mode */
264 if ((flags = fcntl(3, F_GETFL)) == -1)
265 fatal("fcntl(F_GETFL)");
266 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
267 fatal("fcntl F_SETFL O_NONBLOCK");
269 ibuf = xmalloc(sizeof(*ibuf));
272 signal(SIGINT, player_signal_handler);
273 signal(SIGTERM, player_signal_handler);
275 signal(SIGHUP, SIG_IGN);
276 signal(SIGPIPE, SIG_IGN);
278 if (pledge("stdio recvfd", NULL) == -1)