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)
59 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
64 player_setup(int bits, int rate, int channels)
69 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
70 bits, rate, channels);
82 if (!sio_setpar(hdl, &par)) {
83 if (errno == EAGAIN) {
84 nfds = sio_pollfd(hdl, player_pfds + 1, POLLIN|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? */
105 if (!sio_start(hdl)) {
106 log_warn("sio_start");
113 /* process only one message */
115 player_dispatch(void)
126 if ((n = imsg_get(ibuf, &imsg)) == -1)
131 if (poll(&pfd, 1, INFTIM) == -1)
133 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
136 fatalx("pipe closed");
141 switch (imsg.hdr.type) {
144 fatalx("track already enqueued");
145 if ((nextfd = imsg.fd) == -1)
146 fatalx("%s: got invalid file descriptor", __func__);
147 log_debug("song enqueued");
155 fatalx("unknown imsg %d", imsg.hdr.type);
165 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
172 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
177 player_playnext(void)
179 static char buf[512];
183 assert(nextfd != -1);
186 r = read(fd, buf, sizeof(buf));
188 /* 8 byte is the larger magic number */
190 log_warn("read failed");
194 if (lseek(fd, 0, SEEK_SET) == -1) {
195 log_warn("lseek failed");
199 if (memcmp(buf, "fLaC", 4) == 0)
200 return play_flac(fd);
201 if (memcmp(buf, "ID3", 3) == 0 ||
202 memcmp(buf, "\xFF\xFB", 2) == 0)
204 if (memmem(buf, r, "OpusHead", 8) != NULL)
205 return play_opus(fd);
206 if (memmem(buf, r, "OggS", 4) != NULL)
207 return play_oggvorbis(fd);
209 log_warnx("unknown file type");
220 r = player_dispatch();
221 return r == IMSG_RESUME;
225 player_shouldstop(void)
227 switch (player_dispatch()) {
240 play(const void *buf, size_t len)
243 int nfds, revents, r;
246 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
247 r = poll(player_pfds, nfds + 1, INFTIM);
251 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
252 if (player_shouldstop()) {
259 revents = sio_revents(hdl, player_pfds + 1);
260 if (revents & POLLHUP)
261 fatalx("sndio hang-up");
262 if (revents & POLLOUT) {
263 w = sio_write(hdl, buf, len);
273 player(int debug, int verbose)
277 log_init(debug, LOG_DAEMON);
278 log_setverbose(verbose);
280 setproctitle("player");
281 log_procinit("player");
294 /* allocate one extra for imsg */
295 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
296 if (player_pfds == NULL)
299 player_pfds[0].events = POLLIN;
300 player_pfds[0].fd = 3;
302 ibuf = xmalloc(sizeof(*ibuf));
305 signal(SIGINT, player_signal_handler);
306 signal(SIGTERM, player_signal_handler);
308 signal(SIGHUP, SIG_IGN);
309 signal(SIGPIPE, SIG_IGN);
311 if (pledge("stdio recvfd audio", NULL) == -1)
318 r = player_playnext();