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>
43 static struct imsgbuf *ibuf;
45 static int nextfd = -1;
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_setup(int bits, int rate, int channels)
80 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
81 bits, rate, channels);
89 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
90 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
91 bits, rate, channels);
95 if (par.bits != bits || par.pchan != channels) {
96 log_warnx("failed to set params");
100 /* TODO: check the sample rate? */
102 if (!sio_start(hdl)) {
103 log_warn("sio_start");
110 player_pendingimsg(void)
121 r = poll(&pfd, 1, 0);
128 player_enqueue(struct imsg *imsg)
131 fatalx("track already enqueued");
133 if ((nextfd = imsg->fd) == -1)
134 fatalx("%s: got invalid file descriptor", __func__);
135 log_debug("song enqueued");
138 /* process only one message */
140 player_dispatch(void)
149 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
152 fatalx("pipe closed");
154 if ((n = imsg_get(ibuf, &imsg)) == -1)
156 if (n == 0) /* no more messages */
157 fatalx("expected at least a message");
160 switch (imsg.hdr.type) {
162 player_enqueue(&imsg);
170 fatalx("unknown imsg %d", imsg.hdr.type);
179 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
186 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
191 player_playnext(void)
193 static char buf[512];
197 assert(nextfd != -1);
200 r = read(fd, buf, sizeof(buf));
202 /* 8 byte is the larger magic number */
204 log_warn("read failed");
208 if (lseek(fd, 0, SEEK_SET) == -1) {
209 log_warn("lseek failed");
213 if (memcmp(buf, "fLaC", 4) == 0)
214 return play_flac(fd);
215 if (memcmp(buf, "ID3", 3) == 0 ||
216 memcmp(buf, "\xFF\xFB", 2) == 0)
218 if (memmem(buf, r, "OpusHead", 8) != NULL)
219 return play_opus(fd);
220 if (memmem(buf, r, "OggS", 4) != NULL)
221 return play_oggvorbis(fd);
223 log_warnx("unknown file type");
234 r = player_dispatch();
235 return r == IMSG_RESUME;
239 player_shouldstop(void)
241 if (!player_pendingimsg())
244 switch (player_dispatch()) {
257 play(const void *buf, size_t len)
259 if (player_shouldstop())
261 sio_write(hdl, buf, len);
266 player(int debug, int verbose)
269 log_init(debug, LOG_DAEMON);
270 log_setverbose(verbose);
272 setproctitle("player");
273 log_procinit("player");
286 /* mark fd as blocking i/o mode */
287 if ((flags = fcntl(3, F_GETFL)) == -1)
288 fatal("fcntl(F_GETFL)");
289 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
290 fatal("fcntl F_SETFL O_NONBLOCK");
292 ibuf = xmalloc(sizeof(*ibuf));
295 signal(SIGINT, player_signal_handler);
296 signal(SIGTERM, player_signal_handler);
298 signal(SIGHUP, SIG_IGN);
299 signal(SIGPIPE, SIG_IGN);
301 if (pledge("stdio recvfd audio", NULL) == -1)
308 r = player_playnext();