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>
42 static struct imsgbuf *ibuf;
44 static int nextfd = -1;
46 volatile sig_atomic_t halted;
49 player_signal_handler(int signo)
57 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
65 player_setup(int bits, int rate, int channels)
69 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
70 bits, rate, channels);
78 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
79 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
80 bits, rate, channels);
84 if (par.bits != bits || par.pchan != channels) {
85 log_warnx("failed to set params");
89 /* TODO: check the sample rate? */
91 if (!sio_start(hdl)) {
92 log_warn("sio_start");
99 player_pendingimsg(void)
110 r = poll(&pfd, 1, 0);
116 /* process only one message */
118 player_dispatch(void)
129 if ((n = imsg_get(ibuf, &imsg)) == -1)
134 if (poll(&pfd, 1, INFTIM) == -1)
136 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
139 fatalx("pipe closed");
144 switch (imsg.hdr.type) {
147 fatalx("track already enqueued");
148 if ((nextfd = imsg.fd) == -1)
149 fatalx("%s: got invalid file descriptor", __func__);
150 log_debug("song enqueued");
158 fatalx("unknown imsg %d", imsg.hdr.type);
168 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
175 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
180 player_playnext(void)
182 static char buf[512];
186 assert(nextfd != -1);
189 r = read(fd, buf, sizeof(buf));
191 /* 8 byte is the larger magic number */
193 log_warn("read failed");
197 if (lseek(fd, 0, SEEK_SET) == -1) {
198 log_warn("lseek failed");
202 if (memcmp(buf, "fLaC", 4) == 0)
203 return play_flac(fd);
204 if (memcmp(buf, "ID3", 3) == 0 ||
205 memcmp(buf, "\xFF\xFB", 2) == 0)
207 if (memmem(buf, r, "OpusHead", 8) != NULL)
208 return play_opus(fd);
209 if (memmem(buf, r, "OggS", 4) != NULL)
210 return play_oggvorbis(fd);
212 log_warnx("unknown file type");
223 r = player_dispatch();
224 return r == IMSG_RESUME;
228 player_shouldstop(void)
230 if (!player_pendingimsg())
233 switch (player_dispatch()) {
246 play(const void *buf, size_t len)
248 if (player_shouldstop())
250 sio_write(hdl, buf, len);
255 player(int debug, int verbose)
259 log_init(debug, LOG_DAEMON);
260 log_setverbose(verbose);
262 setproctitle("player");
263 log_procinit("player");
276 ibuf = xmalloc(sizeof(*ibuf));
279 signal(SIGINT, player_signal_handler);
280 signal(SIGTERM, player_signal_handler);
282 signal(SIGHUP, SIG_IGN);
283 signal(SIGPIPE, SIG_IGN);
285 if (pledge("stdio recvfd audio", NULL) == -1)
292 r = player_playnext();