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;
46 static char nextpath[PATH_MAX];
48 volatile sig_atomic_t halted;
51 player_signal_handler(int signo)
61 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
66 par.appbufsz = 50 * par.rate / 1000;
68 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
69 fatal("couldn't set audio params");
70 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
71 fatalx("unsupported audio params");
77 player_setup(int bits, int rate, int channels)
81 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
82 bits, rate, channels);
90 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
91 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
92 bits, rate, channels);
96 if (par.bits != bits || par.pchan != channels) {
97 log_warnx("failed to set params");
101 /* TODO: check the sample rate? */
103 if (!sio_start(hdl)) {
104 log_warn("sio_start");
111 player_pendingimsg(void)
122 r = poll(&pfd, 1, 0);
129 player_enqueue(struct imsg *imsg)
134 fatalx("track already enqueued");
136 datalen = IMSG_DATA_SIZE(*imsg);
137 if (datalen != sizeof(nextpath))
138 fatalx("%s: size mismatch", __func__);
139 memcpy(nextpath, imsg->data, sizeof(nextpath));
140 if (nextpath[datalen-1] != '\0')
141 fatalx("%s: corrupted path", __func__);
142 if ((nextfd = imsg->fd) == -1)
143 fatalx("%s: got invalid file descriptor", __func__);
144 log_debug("enqueued %s", nextpath);
147 /* process only one message */
149 player_dispatch(void)
158 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
161 fatalx("pipe closed");
163 if ((n = imsg_get(ibuf, &imsg)) == -1)
165 if (n == 0) /* no more messages */
166 fatalx("expected at least a message");
169 switch (imsg.hdr.type) {
171 player_enqueue(&imsg);
179 fatalx("unknown imsg %d", imsg.hdr.type);
188 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
195 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
200 player_playnext(void)
202 static char buf[512];
206 assert(nextfd != -1);
209 r = read(fd, buf, sizeof(buf));
211 /* 8 byte is the larger magic number */
213 log_warn("failed to read %s", nextpath);
217 if (lseek(fd, 0, SEEK_SET) == -1) {
218 log_warn("lseek failed");
222 if (memcmp(buf, "fLaC", 4) == 0)
223 return play_flac(fd);
224 if (memcmp(buf, "ID3", 3) == 0 ||
225 memcmp(buf, "\xFF\xFB", 2) == 0)
227 if (memmem(buf, r, "OpusHead", 8) != NULL)
228 return play_opus(fd);
229 if (memmem(buf, r, "OggS", 4) != NULL)
230 return play_oggvorbis(fd);
232 log_warnx("unknown file type for %s", nextpath);
243 r = player_dispatch();
244 return r == IMSG_RESUME;
248 player_shouldstop(void)
250 if (!player_pendingimsg())
253 switch (player_dispatch()) {
266 play(const void *buf, size_t len)
268 if (player_shouldstop())
270 sio_write(hdl, buf, len);
275 player(int debug, int verbose)
278 log_init(debug, LOG_DAEMON);
279 log_setverbose(verbose);
281 setproctitle("player");
282 log_procinit("player");
295 /* mark fd as blocking i/o mode */
296 if ((flags = fcntl(3, F_GETFL)) == -1)
297 fatal("fcntl(F_GETFL)");
298 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
299 fatal("fcntl F_SETFL O_NONBLOCK");
301 ibuf = xmalloc(sizeof(*ibuf));
304 signal(SIGINT, player_signal_handler);
305 signal(SIGTERM, player_signal_handler);
307 signal(SIGHUP, SIG_IGN);
308 signal(SIGPIPE, SIG_IGN);
310 if (pledge("stdio recvfd audio", NULL) == -1)
317 if (player_playnext() == -1)