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)
59 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
64 par.appbufsz = 50 * par.rate / 1000;
66 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
67 fatal("couldn't set audio params");
68 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
69 fatalx("unsupported audio params");
75 player_setup(int bits, int rate, int channels)
79 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
80 bits, rate, channels);
88 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
89 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
90 bits, rate, channels);
94 if (par.bits != bits || par.pchan != channels) {
95 log_warnx("failed to set params");
99 /* TODO: check the sample rate? */
101 if (!sio_start(hdl)) {
102 log_warn("sio_start");
109 player_pendingimsg(void)
120 r = poll(&pfd, 1, 0);
127 player_enqueue(struct imsg *imsg)
130 fatalx("track already enqueued");
132 if ((nextfd = imsg->fd) == -1)
133 fatalx("%s: got invalid file descriptor", __func__);
134 log_debug("song enqueued");
137 /* process only one message */
139 player_dispatch(void)
150 if ((n = imsg_get(ibuf, &imsg)) == -1)
155 if (poll(&pfd, 1, INFTIM) == -1)
157 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
160 fatalx("pipe closed");
165 switch (imsg.hdr.type) {
167 player_enqueue(&imsg);
175 fatalx("unknown imsg %d", imsg.hdr.type);
184 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
191 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
196 player_playnext(void)
198 static char buf[512];
202 assert(nextfd != -1);
205 r = read(fd, buf, sizeof(buf));
207 /* 8 byte is the larger magic number */
209 log_warn("read failed");
213 if (lseek(fd, 0, SEEK_SET) == -1) {
214 log_warn("lseek failed");
218 if (memcmp(buf, "fLaC", 4) == 0)
219 return play_flac(fd);
220 if (memcmp(buf, "ID3", 3) == 0 ||
221 memcmp(buf, "\xFF\xFB", 2) == 0)
223 if (memmem(buf, r, "OpusHead", 8) != NULL)
224 return play_opus(fd);
225 if (memmem(buf, r, "OggS", 4) != NULL)
226 return play_oggvorbis(fd);
228 log_warnx("unknown file type");
239 r = player_dispatch();
240 return r == IMSG_RESUME;
244 player_shouldstop(void)
246 if (!player_pendingimsg())
249 switch (player_dispatch()) {
262 play(const void *buf, size_t len)
264 if (player_shouldstop())
266 sio_write(hdl, buf, len);
271 player(int debug, int verbose)
275 log_init(debug, LOG_DAEMON);
276 log_setverbose(verbose);
278 setproctitle("player");
279 log_procinit("player");
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();