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);
117 player_enqueue(struct imsg *imsg)
120 fatalx("track already enqueued");
122 if ((nextfd = imsg->fd) == -1)
123 fatalx("%s: got invalid file descriptor", __func__);
124 log_debug("song enqueued");
127 /* process only one message */
129 player_dispatch(void)
140 if ((n = imsg_get(ibuf, &imsg)) == -1)
145 if (poll(&pfd, 1, INFTIM) == -1)
147 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
150 fatalx("pipe closed");
155 switch (imsg.hdr.type) {
157 player_enqueue(&imsg);
165 fatalx("unknown imsg %d", imsg.hdr.type);
174 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
181 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
186 player_playnext(void)
188 static char buf[512];
192 assert(nextfd != -1);
195 r = read(fd, buf, sizeof(buf));
197 /* 8 byte is the larger magic number */
199 log_warn("read failed");
203 if (lseek(fd, 0, SEEK_SET) == -1) {
204 log_warn("lseek failed");
208 if (memcmp(buf, "fLaC", 4) == 0)
209 return play_flac(fd);
210 if (memcmp(buf, "ID3", 3) == 0 ||
211 memcmp(buf, "\xFF\xFB", 2) == 0)
213 if (memmem(buf, r, "OpusHead", 8) != NULL)
214 return play_opus(fd);
215 if (memmem(buf, r, "OggS", 4) != NULL)
216 return play_oggvorbis(fd);
218 log_warnx("unknown file type");
229 r = player_dispatch();
230 return r == IMSG_RESUME;
234 player_shouldstop(void)
236 if (!player_pendingimsg())
239 switch (player_dispatch()) {
252 play(const void *buf, size_t len)
254 if (player_shouldstop())
256 sio_write(hdl, buf, len);
261 player(int debug, int verbose)
265 log_init(debug, LOG_DAEMON);
266 log_setverbose(verbose);
268 setproctitle("player");
269 log_procinit("player");
282 ibuf = xmalloc(sizeof(*ibuf));
285 signal(SIGINT, player_signal_handler);
286 signal(SIGTERM, player_signal_handler);
288 signal(SIGHUP, SIG_IGN);
289 signal(SIGPIPE, SIG_IGN);
291 if (pledge("stdio recvfd audio", NULL) == -1)
298 r = player_playnext();