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;
43 static int nextfd = -1;
44 static char nextpath[PATH_MAX];
46 volatile sig_atomic_t halted;
49 player_signal_handler(int signo)
59 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
64 par.appbufsz = 50 * par.rate / 1000;
66 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
67 fatal("couldn't set audio params");
68 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
69 fatalx("unsupported audio params");
75 player_setrate(int rate)
79 log_debug("switching to sample rate %d", rate);
85 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
86 log_warnx("invalid params");
90 /* TODO: check the sample rate? */
92 if (!sio_start(hdl)) {
93 log_warn("sio_start");
100 player_pendingimsg(void)
111 r = poll(&pfd, 1, 0);
118 player_enqueue(struct imsg *imsg)
123 fatalx("track already enqueued");
125 datalen = IMSG_DATA_SIZE(*imsg);
126 if (datalen != sizeof(nextpath))
127 fatalx("%s: size mismatch", __func__);
128 memcpy(nextpath, imsg->data, sizeof(nextpath));
129 if (nextpath[datalen-1] != '\0')
130 fatalx("%s: corrupted path", __func__);
131 if ((nextfd = imsg->fd) == -1)
132 fatalx("%s: got invalid file descriptor", __func__);
133 log_debug("enqueued %s", nextpath);
136 /* process only one message */
138 player_dispatch(void)
147 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
150 fatalx("pipe closed");
152 if ((n = imsg_get(ibuf, &imsg)) == -1)
154 if (n == 0) /* no more messages */
155 fatalx("expected at least a message");
158 switch (imsg.hdr.type) {
160 player_enqueue(&imsg);
168 fatalx("unknown imsg %d", imsg.hdr.type);
177 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
184 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
189 player_playnext(void)
193 assert(nextfd != -1);
196 /* XXX: use magic(5) for this, not file extensions */
197 if (strstr(nextpath, ".ogg") != NULL)
199 else if (strstr(nextpath, ".mp3") != NULL)
201 else if (strstr(nextpath, ".flac") != NULL)
203 else if (strstr(nextpath, ".opus") != NULL)
206 log_warnx("unknown file type for %s", nextpath);
217 r = player_dispatch();
218 return r == IMSG_RESUME;
222 player_shouldstop(void)
224 if (!player_pendingimsg())
227 switch (player_dispatch()) {
240 player(int debug, int verbose)
243 log_init(debug, LOG_DAEMON);
244 log_setverbose(verbose);
246 setproctitle("player");
247 log_procinit("player");
260 /* mark fd as blocking i/o mode */
261 if ((flags = fcntl(3, F_GETFL)) == -1)
262 fatal("fcntl(F_GETFL)");
263 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
264 fatal("fcntl F_SETFL O_NONBLOCK");
266 ibuf = xmalloc(sizeof(*ibuf));
269 signal(SIGINT, player_signal_handler);
270 signal(SIGTERM, player_signal_handler);
272 signal(SIGHUP, SIG_IGN);
273 signal(SIGPIPE, SIG_IGN);
275 if (pledge("stdio recvfd", NULL) == -1)