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)
62 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
63 bits, rate, channels);
75 if (!sio_setpar(hdl, &par)) {
76 if (errno == EAGAIN) {
77 nfds = sio_pollfd(hdl, player_pfds + 1, POLLIN|POLLOUT);
78 if (poll(player_pfds + 1, nfds, INFTIM) == -1)
82 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
83 bits, rate, channels);
86 if (!sio_getpar(hdl, &par)) {
87 log_warnx("can't get params");
91 if (par.bits != bits || par.pchan != channels) {
92 log_warnx("failed to set params");
96 /* TODO: check the sample rate? */
98 if (!sio_start(hdl)) {
99 log_warn("sio_start");
106 /* process only one message */
108 player_dispatch(void)
119 if ((n = imsg_get(ibuf, &imsg)) == -1)
124 if (poll(&pfd, 1, INFTIM) == -1)
126 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
129 fatalx("pipe closed");
134 switch (imsg.hdr.type) {
137 fatalx("track already enqueued");
138 if ((nextfd = imsg.fd) == -1)
139 fatalx("%s: got invalid file descriptor", __func__);
140 log_debug("song enqueued");
148 fatalx("unknown imsg %d", imsg.hdr.type);
158 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
165 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
170 player_playnext(void)
172 static char buf[512];
176 assert(nextfd != -1);
179 r = read(fd, buf, sizeof(buf));
181 /* 8 byte is the larger magic number */
183 log_warn("read failed");
187 if (lseek(fd, 0, SEEK_SET) == -1) {
188 log_warn("lseek failed");
192 if (memcmp(buf, "fLaC", 4) == 0)
193 return play_flac(fd);
194 if (memcmp(buf, "ID3", 3) == 0 ||
195 memcmp(buf, "\xFF\xFB", 2) == 0)
197 if (memmem(buf, r, "OpusHead", 8) != NULL)
198 return play_opus(fd);
199 if (memmem(buf, r, "OggS", 4) != NULL)
200 return play_oggvorbis(fd);
202 log_warnx("unknown file type");
213 r = player_dispatch();
214 return r == IMSG_RESUME;
218 player_shouldstop(void)
220 switch (player_dispatch()) {
233 play(const void *buf, size_t len)
236 int nfds, revents, r;
239 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
240 r = poll(player_pfds, nfds + 1, INFTIM);
244 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
245 if (player_shouldstop()) {
252 revents = sio_revents(hdl, player_pfds + 1);
253 if (revents & POLLHUP)
254 fatalx("sndio hang-up");
255 if (revents & POLLOUT) {
256 w = sio_write(hdl, buf, len);
266 player(int debug, int verbose)
270 log_init(debug, LOG_DAEMON);
271 log_setverbose(verbose);
273 setproctitle("player");
274 log_procinit("player");
285 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
288 /* allocate one extra for imsg */
289 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
290 if (player_pfds == NULL)
293 player_pfds[0].events = POLLIN;
294 player_pfds[0].fd = 3;
296 ibuf = xmalloc(sizeof(*ibuf));
299 signal(SIGINT, player_signal_handler);
300 signal(SIGTERM, player_signal_handler);
302 signal(SIGHUP, SIG_IGN);
303 signal(SIGPIPE, SIG_IGN);
305 if (pledge("stdio recvfd audio", NULL) == -1)
312 r = player_playnext();