Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <limits.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <poll.h>
27 #include <signal.h>
28 #include <sndio.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <imsg.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <unistd.h>
37 #include "amused.h"
38 #include "log.h"
39 #include "xmalloc.h"
41 struct sio_hdl *hdl;
42 struct sio_par par;
43 struct pollfd *player_pfds;
44 static struct imsgbuf *ibuf;
46 static int stopped = 1;
47 static int nextfd = -1;
48 static int64_t samples;
50 volatile sig_atomic_t halted;
52 void
53 player_signal_handler(int signo)
54 {
55 halted = 1;
56 }
58 int
59 player_setup(int bits, int rate, int channels)
60 {
61 int nfds, fpct;
63 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
64 bits, rate, channels);
66 fpct = (rate*5)/100;
68 /* don't stop if the parameters are the same */
69 if (bits == par.bits && channels == par.pchan &&
70 par.rate - fpct <= rate && rate <= par.rate + fpct) {
71 if (stopped)
72 goto start;
73 return 0;
74 }
76 again:
77 if (!stopped) {
78 sio_stop(hdl);
79 stopped = 1;
80 }
82 sio_initpar(&par);
83 par.bits = bits;
84 par.rate = rate;
85 par.pchan = channels;
86 if (!sio_setpar(hdl, &par)) {
87 if (errno == EAGAIN) {
88 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
89 if (poll(player_pfds + 1, nfds, INFTIM) == -1)
90 fatal("poll");
91 goto again;
92 }
93 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
94 bits, rate, channels);
95 return -1;
96 }
97 if (!sio_getpar(hdl, &par)) {
98 log_warnx("can't get params");
99 return -1;
102 if (par.bits != bits || par.pchan != channels) {
103 log_warnx("failed to set params");
104 return -1;
107 /* TODO: check the sample rate? */
109 start:
110 if (!sio_start(hdl)) {
111 log_warn("sio_start");
112 return -1;
114 stopped = 0;
115 return 0;
118 void
119 player_setduration(int64_t duration)
121 int64_t seconds;
123 seconds = duration / par.rate;
124 imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
125 imsg_flush(ibuf);
128 void
129 player_onmove(void *arg, int delta)
131 static int64_t reported;
132 int64_t sec;
134 samples += delta;
135 if (llabs(samples - reported) >= par.rate) {
136 reported = samples;
137 sec = samples / par.rate;
139 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
140 imsg_flush(ibuf);
144 /* process only one message */
145 int
146 player_dispatch(void)
148 struct pollfd pfd;
149 struct imsg imsg;
150 ssize_t n;
151 int ret;
153 if (halted != 0)
154 return IMSG_STOP;
156 again:
157 if ((n = imsg_get(ibuf, &imsg)) == -1)
158 fatal("imsg_get");
159 if (n == 0) {
160 pfd.fd = ibuf->fd;
161 pfd.events = POLLIN;
162 if (poll(&pfd, 1, INFTIM) == -1)
163 fatal("poll");
164 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
165 fatal("imsg_read");
166 if (n == 0)
167 fatalx("pipe closed");
168 goto again;
171 ret = imsg.hdr.type;
172 switch (imsg.hdr.type) {
173 case IMSG_PLAY:
174 if (nextfd != -1)
175 fatalx("track already enqueued");
176 if ((nextfd = imsg.fd) == -1)
177 fatalx("%s: got invalid file descriptor", __func__);
178 log_debug("song enqueued");
179 ret = IMSG_STOP;
180 break;
181 case IMSG_RESUME:
182 case IMSG_PAUSE:
183 case IMSG_STOP:
184 break;
185 default:
186 fatalx("unknown imsg %d", imsg.hdr.type);
189 imsg_free(&imsg);
190 return ret;
193 void
194 player_senderr(const char *errstr)
196 size_t len = 0;
198 if (errstr != NULL)
199 len = strlen(errstr) + 1;
201 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
202 imsg_flush(ibuf);
205 void
206 player_sendeof(void)
208 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
209 imsg_flush(ibuf);
212 int
213 player_playnext(const char **errstr)
215 static char buf[512];
216 ssize_t r;
217 int fd = nextfd;
219 assert(nextfd != -1);
220 nextfd = -1;
222 /* reset samples and set position to zero */
223 samples = 0;
224 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
225 imsg_flush(ibuf);
227 r = read(fd, buf, sizeof(buf));
229 /* 8 byte is the larger magic number */
230 if (r < 8) {
231 *errstr = "read failed";
232 goto err;
235 if (lseek(fd, 0, SEEK_SET) == -1) {
236 *errstr = "lseek failed";
237 goto err;
240 if (memcmp(buf, "fLaC", 4) == 0)
241 return play_flac(fd, errstr);
242 if (memcmp(buf, "ID3", 3) == 0 ||
243 memcmp(buf, "\xFF\xFB", 2) == 0)
244 return play_mp3(fd, errstr);
245 if (memmem(buf, r, "OpusHead", 8) != NULL)
246 return play_opus(fd, errstr);
247 if (memmem(buf, r, "OggS", 4) != NULL)
248 return play_oggvorbis(fd, errstr);
250 *errstr = "unknown file type";
251 err:
252 close(fd);
253 return -1;
256 int
257 player_pause(void)
259 int r;
261 r = player_dispatch();
262 return r == IMSG_RESUME;
265 int
266 player_shouldstop(void)
268 switch (player_dispatch()) {
269 case IMSG_PAUSE:
270 if (player_pause())
271 break;
272 /* fallthrough */
273 case IMSG_STOP:
274 return 1;
277 return 0;
280 int
281 play(const void *buf, size_t len)
283 size_t w;
284 int nfds, revents, r;
286 while (len != 0) {
287 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
288 r = poll(player_pfds, nfds + 1, INFTIM);
289 if (r == -1)
290 fatal("poll");
292 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
293 if (player_shouldstop()) {
294 sio_flush(hdl);
295 stopped = 1;
296 return 0;
300 revents = sio_revents(hdl, player_pfds + 1);
301 if (revents & POLLHUP)
302 fatalx("sndio hang-up");
303 if (revents & POLLOUT) {
304 w = sio_write(hdl, buf, len);
305 len -= w;
306 buf += w;
310 return 1;
313 int
314 player(int debug, int verbose)
316 int r;
318 log_init(debug, LOG_DAEMON);
319 log_setverbose(verbose);
321 setproctitle("player");
322 log_procinit("player");
324 #if 0
326 static int attached;
328 while (!attached)
329 sleep(1);
331 #endif
333 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
334 fatal("sio_open");
336 sio_onmove(hdl, player_onmove, NULL);
338 /* allocate one extra for imsg */
339 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
340 if (player_pfds == NULL)
341 fatal("calloc");
343 player_pfds[0].events = POLLIN;
344 player_pfds[0].fd = 3;
346 ibuf = xmalloc(sizeof(*ibuf));
347 imsg_init(ibuf, 3);
349 signal(SIGINT, player_signal_handler);
350 signal(SIGTERM, player_signal_handler);
352 signal(SIGHUP, SIG_IGN);
353 signal(SIGPIPE, SIG_IGN);
355 if (pledge("stdio recvfd audio", NULL) == -1)
356 fatal("pledge");
358 while (!halted) {
359 const char *errstr = NULL;
361 while (nextfd == -1)
362 player_dispatch();
364 r = player_playnext(&errstr);
365 if (r == -1)
366 player_senderr(errstr);
367 if (r == 0)
368 player_sendeof();
371 return 0;