2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/queue.h>
19 3baa2617 2022-02-16 op #include <sys/uio.h>
21 3baa2617 2022-02-16 op #include <limits.h>
23 3baa2617 2022-02-16 op #include <assert.h>
24 3baa2617 2022-02-16 op #include <errno.h>
25 3baa2617 2022-02-16 op #include <event.h>
26 3baa2617 2022-02-16 op #include <poll.h>
27 3baa2617 2022-02-16 op #include <signal.h>
28 3baa2617 2022-02-16 op #include <sndio.h>
29 14be015f 2022-02-17 op #include <stdio.h>
30 3baa2617 2022-02-16 op #include <stdlib.h>
31 3baa2617 2022-02-16 op #include <stdint.h>
32 3baa2617 2022-02-16 op #include <imsg.h>
33 3baa2617 2022-02-16 op #include <string.h>
34 3baa2617 2022-02-16 op #include <syslog.h>
35 3baa2617 2022-02-16 op #include <unistd.h>
37 3baa2617 2022-02-16 op #include "amused.h"
38 3baa2617 2022-02-16 op #include "log.h"
39 3baa2617 2022-02-16 op #include "xmalloc.h"
41 5a4b3030 2022-03-09 op struct sio_hdl *hdl;
42 ff06024f 2022-07-08 op struct sio_par par;
43 c0180200 2022-06-10 op struct pollfd *player_pfds;
44 5a4b3030 2022-03-09 op static struct imsgbuf *ibuf;
46 c0180200 2022-06-10 op static int stopped = 1;
47 3baa2617 2022-02-16 op static int nextfd = -1;
48 ff06024f 2022-07-08 op static int64_t samples;
50 3baa2617 2022-02-16 op volatile sig_atomic_t halted;
53 3baa2617 2022-02-16 op player_signal_handler(int signo)
59 e24324f1 2022-02-21 op player_setup(int bits, int rate, int channels)
61 4d8a06d4 2022-06-10 op int nfds, fpct;
63 a728254f 2022-02-23 op log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
64 a728254f 2022-02-23 op bits, rate, channels);
66 4d8a06d4 2022-06-10 op fpct = (rate*5)/100;
68 4d8a06d4 2022-06-10 op /* don't stop if the parameters are the same */
69 4d8a06d4 2022-06-10 op if (bits == par.bits && channels == par.pchan &&
70 f5237736 2022-06-10 op par.rate - fpct <= rate && rate <= par.rate + fpct) {
77 c0180200 2022-06-10 op if (!stopped) {
78 c0180200 2022-06-10 op sio_stop(hdl);
82 3baa2617 2022-02-16 op sio_initpar(&par);
83 e24324f1 2022-02-21 op par.bits = bits;
84 3baa2617 2022-02-16 op par.rate = rate;
85 7fc831ea 2022-02-18 op par.pchan = channels;
86 c0180200 2022-06-10 op if (!sio_setpar(hdl, &par)) {
87 c0180200 2022-06-10 op if (errno == EAGAIN) {
88 463ce879 2022-06-10 op nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
89 c0180200 2022-06-10 op if (poll(player_pfds + 1, nfds, INFTIM) == -1)
90 c0180200 2022-06-10 op fatal("poll");
93 f002c42a 2022-05-09 op log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
94 f002c42a 2022-05-09 op bits, rate, channels);
97 c0180200 2022-06-10 op if (!sio_getpar(hdl, &par)) {
98 c0180200 2022-06-10 op log_warnx("can't get params");
102 e24324f1 2022-02-21 op if (par.bits != bits || par.pchan != channels) {
103 7fc831ea 2022-02-18 op log_warnx("failed to set params");
107 3baa2617 2022-02-16 op /* TODO: check the sample rate? */
110 3baa2617 2022-02-16 op if (!sio_start(hdl)) {
111 3baa2617 2022-02-16 op log_warn("sio_start");
119 ff06024f 2022-07-08 op player_setduration(int64_t duration)
121 ff06024f 2022-07-08 op int64_t seconds;
123 ff06024f 2022-07-08 op seconds = duration / par.rate;
124 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
125 ff06024f 2022-07-08 op imsg_flush(ibuf);
129 ff06024f 2022-07-08 op player_onmove(void *arg, int delta)
131 ff06024f 2022-07-08 op static int64_t reported;
134 ff06024f 2022-07-08 op samples += delta;
135 ff06024f 2022-07-08 op if (llabs(samples - reported) >= par.rate) {
136 ff06024f 2022-07-08 op reported = samples;
137 ff06024f 2022-07-08 op sec = samples / par.rate;
139 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
140 ff06024f 2022-07-08 op imsg_flush(ibuf);
144 3baa2617 2022-02-16 op /* process only one message */
146 3baa2617 2022-02-16 op player_dispatch(void)
148 fc4a38af 2022-05-19 op struct pollfd pfd;
149 3baa2617 2022-02-16 op struct imsg imsg;
153 3baa2617 2022-02-16 op if (halted != 0)
154 3baa2617 2022-02-16 op return IMSG_STOP;
157 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
158 3baa2617 2022-02-16 op fatal("imsg_get");
159 926ee836 2022-05-19 op if (n == 0) {
160 926ee836 2022-05-19 op pfd.fd = ibuf->fd;
161 926ee836 2022-05-19 op pfd.events = POLLIN;
162 926ee836 2022-05-19 op if (poll(&pfd, 1, INFTIM) == -1)
163 926ee836 2022-05-19 op fatal("poll");
164 926ee836 2022-05-19 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
165 926ee836 2022-05-19 op fatal("imsg_read");
167 926ee836 2022-05-19 op fatalx("pipe closed");
171 3baa2617 2022-02-16 op ret = imsg.hdr.type;
172 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
173 3baa2617 2022-02-16 op case IMSG_PLAY:
174 6e4f8947 2022-06-09 op if (nextfd != -1)
175 6e4f8947 2022-06-09 op fatalx("track already enqueued");
176 6e4f8947 2022-06-09 op if ((nextfd = imsg.fd) == -1)
177 6e4f8947 2022-06-09 op fatalx("%s: got invalid file descriptor", __func__);
178 6e4f8947 2022-06-09 op log_debug("song enqueued");
179 3baa2617 2022-02-16 op ret = IMSG_STOP;
181 3baa2617 2022-02-16 op case IMSG_RESUME:
182 3baa2617 2022-02-16 op case IMSG_PAUSE:
183 3baa2617 2022-02-16 op case IMSG_STOP:
186 3baa2617 2022-02-16 op fatalx("unknown imsg %d", imsg.hdr.type);
189 fd90976c 2022-06-09 op imsg_free(&imsg);
194 17ef54d6 2022-06-22 op player_senderr(const char *errstr)
196 17ef54d6 2022-06-22 op size_t len = 0;
198 17ef54d6 2022-06-22 op if (errstr != NULL)
199 17ef54d6 2022-06-22 op len = strlen(errstr) + 1;
201 17ef54d6 2022-06-22 op imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
202 3baa2617 2022-02-16 op imsg_flush(ibuf);
206 3baa2617 2022-02-16 op player_sendeof(void)
208 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
209 3baa2617 2022-02-16 op imsg_flush(ibuf);
213 17ef54d6 2022-06-22 op player_playnext(const char **errstr)
215 bf19b03e 2022-05-09 op static char buf[512];
217 3baa2617 2022-02-16 op int fd = nextfd;
219 3baa2617 2022-02-16 op assert(nextfd != -1);
222 ff06024f 2022-07-08 op /* reset samples and set position to zero */
224 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
225 ff06024f 2022-07-08 op imsg_flush(ibuf);
227 bf19b03e 2022-05-09 op r = read(fd, buf, sizeof(buf));
229 bf19b03e 2022-05-09 op /* 8 byte is the larger magic number */
231 17ef54d6 2022-06-22 op *errstr = "read failed";
235 bf19b03e 2022-05-09 op if (lseek(fd, 0, SEEK_SET) == -1) {
236 17ef54d6 2022-06-22 op *errstr = "lseek failed";
240 bf19b03e 2022-05-09 op if (memcmp(buf, "fLaC", 4) == 0)
241 17ef54d6 2022-06-22 op return play_flac(fd, errstr);
242 bf19b03e 2022-05-09 op if (memcmp(buf, "ID3", 3) == 0 ||
243 bf19b03e 2022-05-09 op memcmp(buf, "\xFF\xFB", 2) == 0)
244 17ef54d6 2022-06-22 op return play_mp3(fd, errstr);
245 bf19b03e 2022-05-09 op if (memmem(buf, r, "OpusHead", 8) != NULL)
246 17ef54d6 2022-06-22 op return play_opus(fd, errstr);
247 bf19b03e 2022-05-09 op if (memmem(buf, r, "OggS", 4) != NULL)
248 17ef54d6 2022-06-22 op return play_oggvorbis(fd, errstr);
250 17ef54d6 2022-06-22 op *errstr = "unknown file type";
257 3baa2617 2022-02-16 op player_pause(void)
261 3baa2617 2022-02-16 op r = player_dispatch();
262 3baa2617 2022-02-16 op return r == IMSG_RESUME;
266 3baa2617 2022-02-16 op player_shouldstop(void)
268 3baa2617 2022-02-16 op switch (player_dispatch()) {
269 3baa2617 2022-02-16 op case IMSG_PAUSE:
270 3baa2617 2022-02-16 op if (player_pause())
272 3baa2617 2022-02-16 op /* fallthrough */
273 3baa2617 2022-02-16 op case IMSG_STOP:
281 2139c525 2022-03-09 op play(const void *buf, size_t len)
284 c0180200 2022-06-10 op int nfds, revents, r;
286 c0180200 2022-06-10 op while (len != 0) {
287 c0180200 2022-06-10 op nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
288 c0180200 2022-06-10 op r = poll(player_pfds, nfds + 1, INFTIM);
290 c0180200 2022-06-10 op fatal("poll");
292 c0180200 2022-06-10 op if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
293 c0180200 2022-06-10 op if (player_shouldstop()) {
294 c0180200 2022-06-10 op sio_flush(hdl);
300 c0180200 2022-06-10 op revents = sio_revents(hdl, player_pfds + 1);
301 c0180200 2022-06-10 op if (revents & POLLHUP)
302 c0180200 2022-06-10 op fatalx("sndio hang-up");
303 c0180200 2022-06-10 op if (revents & POLLOUT) {
304 c0180200 2022-06-10 op w = sio_write(hdl, buf, len);
314 3baa2617 2022-02-16 op player(int debug, int verbose)
318 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
319 3baa2617 2022-02-16 op log_setverbose(verbose);
321 3baa2617 2022-02-16 op setproctitle("player");
322 3baa2617 2022-02-16 op log_procinit("player");
326 3baa2617 2022-02-16 op static int attached;
328 3baa2617 2022-02-16 op while (!attached)
333 aecca17c 2022-06-10 op if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
334 aecca17c 2022-06-10 op fatal("sio_open");
336 ff06024f 2022-07-08 op sio_onmove(hdl, player_onmove, NULL);
338 c0180200 2022-06-10 op /* allocate one extra for imsg */
339 c0180200 2022-06-10 op player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
340 c0180200 2022-06-10 op if (player_pfds == NULL)
341 c0180200 2022-06-10 op fatal("calloc");
343 c0180200 2022-06-10 op player_pfds[0].events = POLLIN;
344 c0180200 2022-06-10 op player_pfds[0].fd = 3;
346 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
347 3baa2617 2022-02-16 op imsg_init(ibuf, 3);
349 3baa2617 2022-02-16 op signal(SIGINT, player_signal_handler);
350 3baa2617 2022-02-16 op signal(SIGTERM, player_signal_handler);
352 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
353 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
355 251e00ff 2022-03-10 op if (pledge("stdio recvfd audio", NULL) == -1)
356 3baa2617 2022-02-16 op fatal("pledge");
358 3baa2617 2022-02-16 op while (!halted) {
359 17ef54d6 2022-06-22 op const char *errstr = NULL;
361 3baa2617 2022-02-16 op while (nextfd == -1)
362 1489a53d 2022-02-16 op player_dispatch();
364 17ef54d6 2022-06-22 op r = player_playnext(&errstr);
366 17ef54d6 2022-06-22 op player_senderr(errstr);
368 06412529 2022-05-09 op player_sendeof();