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 struct pollfd *player_pfds;
43 static struct imsgbuf *ibuf;
45 static int stopped = 1;
46 static int nextfd = -1;
48 volatile sig_atomic_t halted;
51 player_signal_handler(int signo)
57 player_setup(int bits, int rate, int channels)
59 static struct sio_par par;
62 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
63 bits, rate, channels);
67 /* don't stop if the parameters are the same */
68 if (bits == par.bits && channels == par.pchan &&
69 par.rate - fpct <= rate && rate <= par.rate + fpct) {
85 if (!sio_setpar(hdl, &par)) {
86 if (errno == EAGAIN) {
87 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
88 if (poll(player_pfds + 1, nfds, INFTIM) == -1)
92 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
93 bits, rate, channels);
96 if (!sio_getpar(hdl, &par)) {
97 log_warnx("can't get params");
101 if (par.bits != bits || par.pchan != channels) {
102 log_warnx("failed to set params");
106 /* TODO: check the sample rate? */
109 if (!sio_start(hdl)) {
110 log_warn("sio_start");
117 /* process only one message */
119 player_dispatch(void)
130 if ((n = imsg_get(ibuf, &imsg)) == -1)
135 if (poll(&pfd, 1, INFTIM) == -1)
137 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
140 fatalx("pipe closed");
145 switch (imsg.hdr.type) {
148 fatalx("track already enqueued");
149 if ((nextfd = imsg.fd) == -1)
150 fatalx("%s: got invalid file descriptor", __func__);
151 log_debug("song enqueued");
159 fatalx("unknown imsg %d", imsg.hdr.type);
167 player_senderr(const char *errstr)
172 len = strlen(errstr) + 1;
174 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
181 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
186 player_playnext(const char **errstr)
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 *errstr = "read failed";
203 if (lseek(fd, 0, SEEK_SET) == -1) {
204 *errstr = "lseek failed";
208 if (memcmp(buf, "fLaC", 4) == 0)
209 return play_flac(fd, errstr);
210 if (memcmp(buf, "ID3", 3) == 0 ||
211 memcmp(buf, "\xFF\xFB", 2) == 0)
212 return play_mp3(fd, errstr);
213 if (memmem(buf, r, "OpusHead", 8) != NULL)
214 return play_opus(fd, errstr);
215 if (memmem(buf, r, "OggS", 4) != NULL)
216 return play_oggvorbis(fd, errstr);
218 *errstr = "unknown file type";
229 r = player_dispatch();
230 return r == IMSG_RESUME;
234 player_shouldstop(void)
236 switch (player_dispatch()) {
249 play(const void *buf, size_t len)
252 int nfds, revents, r;
255 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
256 r = poll(player_pfds, nfds + 1, INFTIM);
260 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
261 if (player_shouldstop()) {
268 revents = sio_revents(hdl, player_pfds + 1);
269 if (revents & POLLHUP)
270 fatalx("sndio hang-up");
271 if (revents & POLLOUT) {
272 w = sio_write(hdl, buf, len);
282 player(int debug, int verbose)
286 log_init(debug, LOG_DAEMON);
287 log_setverbose(verbose);
289 setproctitle("player");
290 log_procinit("player");
301 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
304 /* allocate one extra for imsg */
305 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
306 if (player_pfds == NULL)
309 player_pfds[0].events = POLLIN;
310 player_pfds[0].fd = 3;
312 ibuf = xmalloc(sizeof(*ibuf));
315 signal(SIGINT, player_signal_handler);
316 signal(SIGTERM, player_signal_handler);
318 signal(SIGHUP, SIG_IGN);
319 signal(SIGPIPE, SIG_IGN);
321 if (pledge("stdio recvfd audio", NULL) == -1)
325 const char *errstr = NULL;
330 r = player_playnext(&errstr);
332 player_senderr(errstr);