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);
169 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
176 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
181 player_playnext(void)
183 static char buf[512];
187 assert(nextfd != -1);
190 r = read(fd, buf, sizeof(buf));
192 /* 8 byte is the larger magic number */
194 log_warn("read failed");
198 if (lseek(fd, 0, SEEK_SET) == -1) {
199 log_warn("lseek failed");
203 if (memcmp(buf, "fLaC", 4) == 0)
204 return play_flac(fd);
205 if (memcmp(buf, "ID3", 3) == 0 ||
206 memcmp(buf, "\xFF\xFB", 2) == 0)
208 if (memmem(buf, r, "OpusHead", 8) != NULL)
209 return play_opus(fd);
210 if (memmem(buf, r, "OggS", 4) != NULL)
211 return play_oggvorbis(fd);
213 log_warnx("unknown file type");
224 r = player_dispatch();
225 return r == IMSG_RESUME;
229 player_shouldstop(void)
231 switch (player_dispatch()) {
244 play(const void *buf, size_t len)
247 int nfds, revents, r;
250 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
251 r = poll(player_pfds, nfds + 1, INFTIM);
255 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
256 if (player_shouldstop()) {
263 revents = sio_revents(hdl, player_pfds + 1);
264 if (revents & POLLHUP)
265 fatalx("sndio hang-up");
266 if (revents & POLLOUT) {
267 w = sio_write(hdl, buf, len);
277 player(int debug, int verbose)
281 log_init(debug, LOG_DAEMON);
282 log_setverbose(verbose);
284 setproctitle("player");
285 log_procinit("player");
296 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
299 /* allocate one extra for imsg */
300 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
301 if (player_pfds == NULL)
304 player_pfds[0].events = POLLIN;
305 player_pfds[0].fd = 3;
307 ibuf = xmalloc(sizeof(*ibuf));
310 signal(SIGINT, player_signal_handler);
311 signal(SIGTERM, player_signal_handler);
313 signal(SIGHUP, SIG_IGN);
314 signal(SIGPIPE, SIG_IGN);
316 if (pledge("stdio recvfd audio", NULL) == -1)
323 r = player_playnext();