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);
167 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
174 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
179 player_playnext(void)
181 static char buf[512];
185 assert(nextfd != -1);
188 r = read(fd, buf, sizeof(buf));
190 /* 8 byte is the larger magic number */
192 log_warn("read failed");
196 if (lseek(fd, 0, SEEK_SET) == -1) {
197 log_warn("lseek failed");
201 if (memcmp(buf, "fLaC", 4) == 0)
202 return play_flac(fd);
203 if (memcmp(buf, "ID3", 3) == 0 ||
204 memcmp(buf, "\xFF\xFB", 2) == 0)
206 if (memmem(buf, r, "OpusHead", 8) != NULL)
207 return play_opus(fd);
208 if (memmem(buf, r, "OggS", 4) != NULL)
209 return play_oggvorbis(fd);
211 log_warnx("unknown file type");
222 r = player_dispatch();
223 return r == IMSG_RESUME;
227 player_shouldstop(void)
229 if (!player_pendingimsg())
232 switch (player_dispatch()) {
245 play(const void *buf, size_t len)
247 if (player_shouldstop())
249 sio_write(hdl, buf, len);
254 player(int debug, int verbose)
258 log_init(debug, LOG_DAEMON);
259 log_setverbose(verbose);
261 setproctitle("player");
262 log_procinit("player");
275 ibuf = xmalloc(sizeof(*ibuf));
278 signal(SIGINT, player_signal_handler);
279 signal(SIGTERM, player_signal_handler);
281 signal(SIGHUP, SIG_IGN);
282 signal(SIGPIPE, SIG_IGN);
284 if (pledge("stdio recvfd audio", NULL) == -1)
291 r = player_playnext();