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 static void
53 player_signal_handler(int signo)
54 {
55 halted = 1;
56 }
58 int
59 player_setup(unsigned int bits, unsigned int rate, unsigned 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 static 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 void
145 player_setpos(int64_t pos)
147 samples = pos;
148 player_onmove(NULL, 0);
151 /* process only one message */
152 static int
153 player_dispatch(int64_t *s)
155 struct player_seek seek;
156 struct pollfd pfd;
157 struct imsg imsg;
158 ssize_t n;
159 int ret;
161 if (halted != 0)
162 return IMSG_STOP;
164 again:
165 if ((n = imsg_get(ibuf, &imsg)) == -1)
166 fatal("imsg_get");
167 if (n == 0) {
168 pfd.fd = ibuf->fd;
169 pfd.events = POLLIN;
170 if (poll(&pfd, 1, INFTIM) == -1)
171 fatal("poll");
172 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
173 fatal("imsg_read");
174 if (n == 0)
175 fatalx("pipe closed");
176 goto again;
179 ret = imsg.hdr.type;
180 switch (imsg.hdr.type) {
181 case IMSG_PLAY:
182 if (nextfd != -1)
183 fatalx("track already enqueued");
184 if ((nextfd = imsg.fd) == -1)
185 fatalx("%s: got invalid file descriptor", __func__);
186 log_debug("song enqueued");
187 ret = IMSG_STOP;
188 break;
189 case IMSG_RESUME:
190 case IMSG_PAUSE:
191 case IMSG_STOP:
192 break;
193 case IMSG_CTL_SEEK:
194 if (s == NULL)
195 break;
196 if (IMSG_DATA_SIZE(imsg) != sizeof(seek))
197 fatalx("wrong size for seek ctl");
198 memcpy(&seek, imsg.data, sizeof(seek));
199 log_debug("got to seek: {%lld, %d}", seek.offset,
200 seek.relative);
201 *s = seek.offset * par.rate;
202 if (seek.relative)
203 *s += samples;
204 if (*s < 0)
205 *s = -1;
206 break;
207 default:
208 fatalx("unknown imsg %d", imsg.hdr.type);
211 imsg_free(&imsg);
212 return ret;
215 static void
216 player_senderr(const char *errstr)
218 size_t len = 0;
220 if (errstr != NULL)
221 len = strlen(errstr) + 1;
223 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
224 imsg_flush(ibuf);
227 static void
228 player_sendeof(void)
230 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
231 imsg_flush(ibuf);
234 static int
235 player_playnext(const char **errstr)
237 static char buf[512];
238 ssize_t r;
239 int fd = nextfd;
241 assert(nextfd != -1);
242 nextfd = -1;
244 /* reset samples and set position to zero */
245 samples = 0;
246 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
247 imsg_flush(ibuf);
249 r = read(fd, buf, sizeof(buf));
251 /* 8 byte is the larger magic number */
252 if (r < 8) {
253 *errstr = "read failed";
254 goto err;
257 if (lseek(fd, 0, SEEK_SET) == -1) {
258 *errstr = "lseek failed";
259 goto err;
262 if (memcmp(buf, "fLaC", 4) == 0)
263 return play_flac(fd, errstr);
264 if (memcmp(buf, "ID3", 3) == 0 ||
265 memcmp(buf, "\xFF\xFB", 2) == 0)
266 return play_mp3(fd, errstr);
267 if (memmem(buf, r, "OpusHead", 8) != NULL)
268 return play_opus(fd, errstr);
269 if (memmem(buf, r, "OggS", 4) != NULL)
270 return play_oggvorbis(fd, errstr);
272 *errstr = "unknown file type";
273 err:
274 close(fd);
275 return -1;
278 static int
279 player_pause(int64_t *s)
281 int r;
283 r = player_dispatch(s);
284 return r == IMSG_RESUME;
287 static int
288 player_shouldstop(int64_t *s)
290 switch (player_dispatch(s)) {
291 case IMSG_PAUSE:
292 if (player_pause(s))
293 break;
294 /* fallthrough */
295 case IMSG_STOP:
296 return 1;
299 return 0;
302 int
303 play(const void *buf, size_t len, int64_t *s)
305 size_t w;
306 int nfds, revents, r;
308 *s = -1;
309 while (len != 0) {
310 nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
311 r = poll(player_pfds, nfds + 1, INFTIM);
312 if (r == -1)
313 fatal("poll");
315 if (player_pfds[0].revents & (POLLHUP|POLLIN)) {
316 if (player_shouldstop(s)) {
317 sio_flush(hdl);
318 stopped = 1;
319 return 0;
323 revents = sio_revents(hdl, player_pfds + 1);
324 if (revents & POLLHUP)
325 fatalx("sndio hang-up");
326 if (revents & POLLOUT) {
327 w = sio_write(hdl, buf, len);
328 len -= w;
329 buf += w;
333 return 1;
336 int
337 player(int debug, int verbose)
339 int r;
341 log_init(debug, LOG_DAEMON);
342 log_setverbose(verbose);
344 setproctitle("player");
345 log_procinit("player");
347 #if 0
349 static int attached;
351 while (!attached)
352 sleep(1);
354 #endif
356 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
357 fatal("sio_open");
359 sio_onmove(hdl, player_onmove, NULL);
361 /* allocate one extra for imsg */
362 player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
363 if (player_pfds == NULL)
364 fatal("calloc");
366 player_pfds[0].events = POLLIN;
367 player_pfds[0].fd = 3;
369 ibuf = xmalloc(sizeof(*ibuf));
370 imsg_init(ibuf, 3);
372 signal(SIGINT, player_signal_handler);
373 signal(SIGTERM, player_signal_handler);
375 signal(SIGHUP, SIG_IGN);
376 signal(SIGPIPE, SIG_IGN);
378 if (pledge("stdio recvfd audio", NULL) == -1)
379 fatal("pledge");
381 while (!halted) {
382 const char *errstr = NULL;
384 while (nextfd == -1)
385 player_dispatch(NULL);
387 r = player_playnext(&errstr);
388 if (r == -1)
389 player_senderr(errstr);
390 if (r == 0)
391 player_sendeof();
394 return 0;