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)
82 if (!sio_setpar(hdl, &par)) {
83 if (errno == EAGAIN) {
84 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
85 if (poll(player_pfds + 1, nfds, INFTIM) == -1)
89 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
90 bits, rate, channels);
93 if (!sio_getpar(hdl, &par)) {
94 log_warnx("can't get params");
98 if (par.bits != bits || par.pchan != channels) {
99 log_warnx("failed to set params");
103 /* TODO: check the sample rate? */
106 if (!sio_start(hdl)) {
107 log_warn("sio_start");
114 /* process only one message */
116 player_dispatch(void)
127 if ((n = imsg_get(ibuf, &imsg)) == -1)
132 if (poll(&pfd, 1, INFTIM) == -1)
134 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
137 fatalx("pipe closed");
142 switch (imsg.hdr.type) {
145 fatalx("track already enqueued");
146 if ((nextfd = imsg.fd) == -1)
147 fatalx("%s: got invalid file descriptor", __func__);
148 log_debug("song enqueued");
156 fatalx("unknown imsg %d", imsg.hdr.type);
166 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
173 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
178 player_playnext(void)
180 static char buf[512];
184 assert(nextfd != -1);
187 r = read(fd, buf, sizeof(buf));
189 /* 8 byte is the larger magic number */
191 log_warn("read failed");
195 if (lseek(fd, 0, SEEK_SET) == -1) {
196 log_warn("lseek failed");
200 if (memcmp(buf, "fLaC", 4) == 0)
201 return play_flac(fd);
202 if (memcmp(buf, "ID3", 3) == 0 ||
203 memcmp(buf, "\xFF\xFB", 2) == 0)
205 if (memmem(buf, r, "OpusHead", 8) != NULL)
206 return play_opus(fd);
207 if (memmem(buf, r, "OggS", 4) != NULL)
208 return play_oggvorbis(fd);
210 log_warnx("unknown file type");
221 r = player_dispatch();
222 return r == IMSG_RESUME;
226 player_shouldstop(void)
228 switch (player_dispatch()) {
241 play(const void *buf, size_t len)
244 int nfds, revents, r;
247 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
248 r = poll(player_pfds, nfds + 1, INFTIM);
252 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
253 if (player_shouldstop()) {
260 revents = sio_revents(hdl, player_pfds + 1);
261 if (revents & POLLHUP)
262 fatalx("sndio hang-up");
263 if (revents & POLLOUT) {
264 w = sio_write(hdl, buf, len);
274 player(int debug, int verbose)
278 log_init(debug, LOG_DAEMON);
279 log_setverbose(verbose);
281 setproctitle("player");
282 log_procinit("player");
293 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
296 /* allocate one extra for imsg */
297 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
298 if (player_pfds == NULL)
301 player_pfds[0].events = POLLIN;
302 player_pfds[0].fd = 3;
304 ibuf = xmalloc(sizeof(*ibuf));
307 signal(SIGINT, player_signal_handler);
308 signal(SIGTERM, player_signal_handler);
310 signal(SIGHUP, SIG_IGN);
311 signal(SIGPIPE, SIG_IGN);
313 if (pledge("stdio recvfd audio", NULL) == -1)
320 r = player_playnext();