Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
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.
7 3baa2617 2022-02-16 op *
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.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 f36fd90a 2022-07-09 op #include "config.h"
18 3baa2617 2022-02-16 op
19 3baa2617 2022-02-16 op #include <limits.h>
20 3baa2617 2022-02-16 op
21 3baa2617 2022-02-16 op #include <assert.h>
22 3baa2617 2022-02-16 op #include <errno.h>
23 3baa2617 2022-02-16 op #include <poll.h>
24 3baa2617 2022-02-16 op #include <signal.h>
25 3baa2617 2022-02-16 op #include <sndio.h>
26 14be015f 2022-02-17 op #include <stdio.h>
27 3baa2617 2022-02-16 op #include <stdlib.h>
28 3baa2617 2022-02-16 op #include <stdint.h>
29 3baa2617 2022-02-16 op #include <string.h>
30 3baa2617 2022-02-16 op #include <syslog.h>
31 3baa2617 2022-02-16 op #include <unistd.h>
32 3baa2617 2022-02-16 op
33 3baa2617 2022-02-16 op #include "amused.h"
34 3baa2617 2022-02-16 op #include "log.h"
35 3baa2617 2022-02-16 op #include "xmalloc.h"
36 3baa2617 2022-02-16 op
37 5a4b3030 2022-03-09 op struct sio_hdl *hdl;
38 ff06024f 2022-07-08 op struct sio_par par;
39 c0180200 2022-06-10 op struct pollfd *player_pfds;
40 5a4b3030 2022-03-09 op static struct imsgbuf *ibuf;
41 3baa2617 2022-02-16 op
42 c0180200 2022-06-10 op static int stopped = 1;
43 3baa2617 2022-02-16 op static int nextfd = -1;
44 ff06024f 2022-07-08 op static int64_t samples;
45 3baa2617 2022-02-16 op
46 3baa2617 2022-02-16 op volatile sig_atomic_t halted;
47 3baa2617 2022-02-16 op
48 ae335651 2022-07-08 op static void
49 3baa2617 2022-02-16 op player_signal_handler(int signo)
50 3baa2617 2022-02-16 op {
51 3baa2617 2022-02-16 op halted = 1;
52 3baa2617 2022-02-16 op }
53 3baa2617 2022-02-16 op
54 3baa2617 2022-02-16 op int
55 b9d2a697 2022-07-08 op player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
56 3baa2617 2022-02-16 op {
57 4d8a06d4 2022-06-10 op int nfds, fpct;
58 3baa2617 2022-02-16 op
59 a728254f 2022-02-23 op log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
60 a728254f 2022-02-23 op bits, rate, channels);
61 4d8a06d4 2022-06-10 op
62 4d8a06d4 2022-06-10 op fpct = (rate*5)/100;
63 4d8a06d4 2022-06-10 op
64 4d8a06d4 2022-06-10 op /* don't stop if the parameters are the same */
65 4d8a06d4 2022-06-10 op if (bits == par.bits && channels == par.pchan &&
66 f5237736 2022-06-10 op par.rate - fpct <= rate && rate <= par.rate + fpct) {
67 f5237736 2022-06-10 op if (stopped)
68 f5237736 2022-06-10 op goto start;
69 f5237736 2022-06-10 op return 0;
70 f5237736 2022-06-10 op }
71 3baa2617 2022-02-16 op
72 c0180200 2022-06-10 op again:
73 c0180200 2022-06-10 op if (!stopped) {
74 c0180200 2022-06-10 op sio_stop(hdl);
75 c0180200 2022-06-10 op stopped = 1;
76 c0180200 2022-06-10 op }
77 3baa2617 2022-02-16 op
78 3baa2617 2022-02-16 op sio_initpar(&par);
79 e24324f1 2022-02-21 op par.bits = bits;
80 3baa2617 2022-02-16 op par.rate = rate;
81 7fc831ea 2022-02-18 op par.pchan = channels;
82 c0180200 2022-06-10 op if (!sio_setpar(hdl, &par)) {
83 c0180200 2022-06-10 op if (errno == EAGAIN) {
84 463ce879 2022-06-10 op nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
85 c0180200 2022-06-10 op if (poll(player_pfds + 1, nfds, INFTIM) == -1)
86 c0180200 2022-06-10 op fatal("poll");
87 c0180200 2022-06-10 op goto again;
88 c0180200 2022-06-10 op }
89 f002c42a 2022-05-09 op log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
90 f002c42a 2022-05-09 op bits, rate, channels);
91 7fc831ea 2022-02-18 op return -1;
92 7fc831ea 2022-02-18 op }
93 c0180200 2022-06-10 op if (!sio_getpar(hdl, &par)) {
94 c0180200 2022-06-10 op log_warnx("can't get params");
95 c0180200 2022-06-10 op return -1;
96 c0180200 2022-06-10 op }
97 7fc831ea 2022-02-18 op
98 e24324f1 2022-02-21 op if (par.bits != bits || par.pchan != channels) {
99 7fc831ea 2022-02-18 op log_warnx("failed to set params");
100 3baa2617 2022-02-16 op return -1;
101 3baa2617 2022-02-16 op }
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op /* TODO: check the sample rate? */
104 3baa2617 2022-02-16 op
105 f5237736 2022-06-10 op start:
106 3baa2617 2022-02-16 op if (!sio_start(hdl)) {
107 3baa2617 2022-02-16 op log_warn("sio_start");
108 3baa2617 2022-02-16 op return -1;
109 3baa2617 2022-02-16 op }
110 c0180200 2022-06-10 op stopped = 0;
111 3baa2617 2022-02-16 op return 0;
112 ff06024f 2022-07-08 op }
113 ff06024f 2022-07-08 op
114 ff06024f 2022-07-08 op void
115 ff06024f 2022-07-08 op player_setduration(int64_t duration)
116 ff06024f 2022-07-08 op {
117 ff06024f 2022-07-08 op int64_t seconds;
118 ff06024f 2022-07-08 op
119 ff06024f 2022-07-08 op seconds = duration / par.rate;
120 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
121 ff06024f 2022-07-08 op imsg_flush(ibuf);
122 ff06024f 2022-07-08 op }
123 ff06024f 2022-07-08 op
124 ae335651 2022-07-08 op static void
125 ff06024f 2022-07-08 op player_onmove(void *arg, int delta)
126 ff06024f 2022-07-08 op {
127 ff06024f 2022-07-08 op static int64_t reported;
128 ff06024f 2022-07-08 op int64_t sec;
129 ff06024f 2022-07-08 op
130 ff06024f 2022-07-08 op samples += delta;
131 ff06024f 2022-07-08 op if (llabs(samples - reported) >= par.rate) {
132 ff06024f 2022-07-08 op reported = samples;
133 ff06024f 2022-07-08 op sec = samples / par.rate;
134 ff06024f 2022-07-08 op
135 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
136 ff06024f 2022-07-08 op imsg_flush(ibuf);
137 ff06024f 2022-07-08 op }
138 3baa2617 2022-02-16 op }
139 3baa2617 2022-02-16 op
140 791d3db3 2022-07-09 op void
141 791d3db3 2022-07-09 op player_setpos(int64_t pos)
142 791d3db3 2022-07-09 op {
143 791d3db3 2022-07-09 op samples = pos;
144 791d3db3 2022-07-09 op player_onmove(NULL, 0);
145 791d3db3 2022-07-09 op }
146 791d3db3 2022-07-09 op
147 3baa2617 2022-02-16 op /* process only one message */
148 ae335651 2022-07-08 op static int
149 c2f554ff 2022-07-09 op player_dispatch(int64_t *s, int wait)
150 3baa2617 2022-02-16 op {
151 791d3db3 2022-07-09 op struct player_seek seek;
152 fc4a38af 2022-05-19 op struct pollfd pfd;
153 3baa2617 2022-02-16 op struct imsg imsg;
154 3baa2617 2022-02-16 op ssize_t n;
155 3baa2617 2022-02-16 op int ret;
156 3baa2617 2022-02-16 op
157 3baa2617 2022-02-16 op if (halted != 0)
158 3baa2617 2022-02-16 op return IMSG_STOP;
159 fc4a38af 2022-05-19 op
160 926ee836 2022-05-19 op again:
161 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
162 3baa2617 2022-02-16 op fatal("imsg_get");
163 926ee836 2022-05-19 op if (n == 0) {
164 c2f554ff 2022-07-09 op if (!wait)
165 c2f554ff 2022-07-09 op return -1;
166 c2f554ff 2022-07-09 op
167 926ee836 2022-05-19 op pfd.fd = ibuf->fd;
168 926ee836 2022-05-19 op pfd.events = POLLIN;
169 926ee836 2022-05-19 op if (poll(&pfd, 1, INFTIM) == -1)
170 926ee836 2022-05-19 op fatal("poll");
171 926ee836 2022-05-19 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
172 926ee836 2022-05-19 op fatal("imsg_read");
173 926ee836 2022-05-19 op if (n == 0)
174 926ee836 2022-05-19 op fatalx("pipe closed");
175 926ee836 2022-05-19 op goto again;
176 926ee836 2022-05-19 op }
177 3baa2617 2022-02-16 op
178 3baa2617 2022-02-16 op ret = imsg.hdr.type;
179 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
180 3baa2617 2022-02-16 op case IMSG_PLAY:
181 6e4f8947 2022-06-09 op if (nextfd != -1)
182 6e4f8947 2022-06-09 op fatalx("track already enqueued");
183 6e4f8947 2022-06-09 op if ((nextfd = imsg.fd) == -1)
184 6e4f8947 2022-06-09 op fatalx("%s: got invalid file descriptor", __func__);
185 6e4f8947 2022-06-09 op log_debug("song enqueued");
186 3baa2617 2022-02-16 op ret = IMSG_STOP;
187 3baa2617 2022-02-16 op break;
188 3baa2617 2022-02-16 op case IMSG_RESUME:
189 3baa2617 2022-02-16 op case IMSG_PAUSE:
190 3baa2617 2022-02-16 op case IMSG_STOP:
191 3baa2617 2022-02-16 op break;
192 791d3db3 2022-07-09 op case IMSG_CTL_SEEK:
193 791d3db3 2022-07-09 op if (s == NULL)
194 791d3db3 2022-07-09 op break;
195 791d3db3 2022-07-09 op if (IMSG_DATA_SIZE(imsg) != sizeof(seek))
196 791d3db3 2022-07-09 op fatalx("wrong size for seek ctl");
197 791d3db3 2022-07-09 op memcpy(&seek, imsg.data, sizeof(seek));
198 791d3db3 2022-07-09 op *s = seek.offset * par.rate;
199 791d3db3 2022-07-09 op if (seek.relative)
200 791d3db3 2022-07-09 op *s += samples;
201 791d3db3 2022-07-09 op if (*s < 0)
202 791d3db3 2022-07-09 op *s = -1;
203 791d3db3 2022-07-09 op break;
204 3baa2617 2022-02-16 op default:
205 3baa2617 2022-02-16 op fatalx("unknown imsg %d", imsg.hdr.type);
206 3baa2617 2022-02-16 op }
207 3baa2617 2022-02-16 op
208 fd90976c 2022-06-09 op imsg_free(&imsg);
209 3baa2617 2022-02-16 op return ret;
210 3baa2617 2022-02-16 op }
211 3baa2617 2022-02-16 op
212 ae335651 2022-07-08 op static void
213 17ef54d6 2022-06-22 op player_senderr(const char *errstr)
214 3baa2617 2022-02-16 op {
215 17ef54d6 2022-06-22 op size_t len = 0;
216 17ef54d6 2022-06-22 op
217 17ef54d6 2022-06-22 op if (errstr != NULL)
218 17ef54d6 2022-06-22 op len = strlen(errstr) + 1;
219 17ef54d6 2022-06-22 op
220 17ef54d6 2022-06-22 op imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
221 3baa2617 2022-02-16 op imsg_flush(ibuf);
222 3baa2617 2022-02-16 op }
223 3baa2617 2022-02-16 op
224 ae335651 2022-07-08 op static void
225 3baa2617 2022-02-16 op player_sendeof(void)
226 3baa2617 2022-02-16 op {
227 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
228 3baa2617 2022-02-16 op imsg_flush(ibuf);
229 3baa2617 2022-02-16 op }
230 3baa2617 2022-02-16 op
231 ae335651 2022-07-08 op static int
232 17ef54d6 2022-06-22 op player_playnext(const char **errstr)
233 3baa2617 2022-02-16 op {
234 bf19b03e 2022-05-09 op static char buf[512];
235 bf19b03e 2022-05-09 op ssize_t r;
236 3baa2617 2022-02-16 op int fd = nextfd;
237 3baa2617 2022-02-16 op
238 3baa2617 2022-02-16 op assert(nextfd != -1);
239 3baa2617 2022-02-16 op nextfd = -1;
240 3baa2617 2022-02-16 op
241 ff06024f 2022-07-08 op /* reset samples and set position to zero */
242 ff06024f 2022-07-08 op samples = 0;
243 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
244 ff06024f 2022-07-08 op imsg_flush(ibuf);
245 ff06024f 2022-07-08 op
246 bf19b03e 2022-05-09 op r = read(fd, buf, sizeof(buf));
247 bf19b03e 2022-05-09 op
248 bf19b03e 2022-05-09 op /* 8 byte is the larger magic number */
249 bf19b03e 2022-05-09 op if (r < 8) {
250 17ef54d6 2022-06-22 op *errstr = "read failed";
251 bf19b03e 2022-05-09 op goto err;
252 bf19b03e 2022-05-09 op }
253 bf19b03e 2022-05-09 op
254 bf19b03e 2022-05-09 op if (lseek(fd, 0, SEEK_SET) == -1) {
255 17ef54d6 2022-06-22 op *errstr = "lseek failed";
256 bf19b03e 2022-05-09 op goto err;
257 bf19b03e 2022-05-09 op }
258 bf19b03e 2022-05-09 op
259 bf19b03e 2022-05-09 op if (memcmp(buf, "fLaC", 4) == 0)
260 17ef54d6 2022-06-22 op return play_flac(fd, errstr);
261 bf19b03e 2022-05-09 op if (memcmp(buf, "ID3", 3) == 0 ||
262 bf19b03e 2022-05-09 op memcmp(buf, "\xFF\xFB", 2) == 0)
263 17ef54d6 2022-06-22 op return play_mp3(fd, errstr);
264 bf19b03e 2022-05-09 op if (memmem(buf, r, "OpusHead", 8) != NULL)
265 17ef54d6 2022-06-22 op return play_opus(fd, errstr);
266 bf19b03e 2022-05-09 op if (memmem(buf, r, "OggS", 4) != NULL)
267 17ef54d6 2022-06-22 op return play_oggvorbis(fd, errstr);
268 25cb72fb 2022-02-22 op
269 17ef54d6 2022-06-22 op *errstr = "unknown file type";
270 bf19b03e 2022-05-09 op err:
271 184600a8 2022-05-09 op close(fd);
272 06412529 2022-05-09 op return -1;
273 3baa2617 2022-02-16 op }
274 3baa2617 2022-02-16 op
275 ae335651 2022-07-08 op static int
276 791d3db3 2022-07-09 op player_pause(int64_t *s)
277 3baa2617 2022-02-16 op {
278 3baa2617 2022-02-16 op int r;
279 3baa2617 2022-02-16 op
280 c2f554ff 2022-07-09 op r = player_dispatch(s, 1);
281 3ad8bae8 2022-07-09 op return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
282 3baa2617 2022-02-16 op }
283 3baa2617 2022-02-16 op
284 ae335651 2022-07-08 op static int
285 c2f554ff 2022-07-09 op player_shouldstop(int64_t *s, int wait)
286 3baa2617 2022-02-16 op {
287 c2f554ff 2022-07-09 op switch (player_dispatch(s, wait)) {
288 3baa2617 2022-02-16 op case IMSG_PAUSE:
289 791d3db3 2022-07-09 op if (player_pause(s))
290 3baa2617 2022-02-16 op break;
291 3baa2617 2022-02-16 op /* fallthrough */
292 3baa2617 2022-02-16 op case IMSG_STOP:
293 3baa2617 2022-02-16 op return 1;
294 3baa2617 2022-02-16 op }
295 3baa2617 2022-02-16 op
296 3baa2617 2022-02-16 op return 0;
297 3baa2617 2022-02-16 op }
298 3baa2617 2022-02-16 op
299 3baa2617 2022-02-16 op int
300 791d3db3 2022-07-09 op play(const void *buf, size_t len, int64_t *s)
301 2139c525 2022-03-09 op {
302 c0180200 2022-06-10 op size_t w;
303 c2f554ff 2022-07-09 op int nfds, revents, r, wait;
304 c0180200 2022-06-10 op
305 791d3db3 2022-07-09 op *s = -1;
306 c0180200 2022-06-10 op while (len != 0) {
307 c0180200 2022-06-10 op nfds = sio_pollfd(hdl, player_pfds + 1, POLLOUT);
308 c0180200 2022-06-10 op r = poll(player_pfds, nfds + 1, INFTIM);
309 c0180200 2022-06-10 op if (r == -1)
310 c0180200 2022-06-10 op fatal("poll");
311 c0180200 2022-06-10 op
312 c2f554ff 2022-07-09 op wait = player_pfds[0].revents & (POLLHUP|POLLIN);
313 c2f554ff 2022-07-09 op if (player_shouldstop(s, wait)) {
314 c2f554ff 2022-07-09 op sio_flush(hdl);
315 c2f554ff 2022-07-09 op stopped = 1;
316 c2f554ff 2022-07-09 op return 0;
317 c0180200 2022-06-10 op }
318 c0180200 2022-06-10 op
319 c0180200 2022-06-10 op revents = sio_revents(hdl, player_pfds + 1);
320 c0180200 2022-06-10 op if (revents & POLLHUP)
321 c0180200 2022-06-10 op fatalx("sndio hang-up");
322 c0180200 2022-06-10 op if (revents & POLLOUT) {
323 c0180200 2022-06-10 op w = sio_write(hdl, buf, len);
324 c0180200 2022-06-10 op len -= w;
325 c0180200 2022-06-10 op buf += w;
326 c0180200 2022-06-10 op }
327 c0180200 2022-06-10 op }
328 c0180200 2022-06-10 op
329 2139c525 2022-03-09 op return 1;
330 2139c525 2022-03-09 op }
331 2139c525 2022-03-09 op
332 2139c525 2022-03-09 op int
333 3baa2617 2022-02-16 op player(int debug, int verbose)
334 3baa2617 2022-02-16 op {
335 fc4a38af 2022-05-19 op int r;
336 fc4a38af 2022-05-19 op
337 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
338 3baa2617 2022-02-16 op log_setverbose(verbose);
339 3baa2617 2022-02-16 op
340 3baa2617 2022-02-16 op setproctitle("player");
341 3baa2617 2022-02-16 op log_procinit("player");
342 3baa2617 2022-02-16 op
343 3baa2617 2022-02-16 op #if 0
344 3baa2617 2022-02-16 op {
345 3baa2617 2022-02-16 op static int attached;
346 3baa2617 2022-02-16 op
347 3baa2617 2022-02-16 op while (!attached)
348 3baa2617 2022-02-16 op sleep(1);
349 3baa2617 2022-02-16 op }
350 3baa2617 2022-02-16 op #endif
351 3baa2617 2022-02-16 op
352 aecca17c 2022-06-10 op if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
353 aecca17c 2022-06-10 op fatal("sio_open");
354 3baa2617 2022-02-16 op
355 ff06024f 2022-07-08 op sio_onmove(hdl, player_onmove, NULL);
356 ff06024f 2022-07-08 op
357 c0180200 2022-06-10 op /* allocate one extra for imsg */
358 c0180200 2022-06-10 op player_pfds = calloc(sio_nfds(hdl) + 1, sizeof(*player_pfds));
359 c0180200 2022-06-10 op if (player_pfds == NULL)
360 c0180200 2022-06-10 op fatal("calloc");
361 c0180200 2022-06-10 op
362 c0180200 2022-06-10 op player_pfds[0].events = POLLIN;
363 c0180200 2022-06-10 op player_pfds[0].fd = 3;
364 c0180200 2022-06-10 op
365 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
366 3baa2617 2022-02-16 op imsg_init(ibuf, 3);
367 3baa2617 2022-02-16 op
368 3baa2617 2022-02-16 op signal(SIGINT, player_signal_handler);
369 3baa2617 2022-02-16 op signal(SIGTERM, player_signal_handler);
370 3baa2617 2022-02-16 op
371 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
372 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
373 3baa2617 2022-02-16 op
374 251e00ff 2022-03-10 op if (pledge("stdio recvfd audio", NULL) == -1)
375 3baa2617 2022-02-16 op fatal("pledge");
376 3baa2617 2022-02-16 op
377 3baa2617 2022-02-16 op while (!halted) {
378 17ef54d6 2022-06-22 op const char *errstr = NULL;
379 17ef54d6 2022-06-22 op
380 3baa2617 2022-02-16 op while (nextfd == -1)
381 c2f554ff 2022-07-09 op player_dispatch(NULL, 1);
382 06412529 2022-05-09 op
383 17ef54d6 2022-06-22 op r = player_playnext(&errstr);
384 239029b6 2022-05-09 op if (r == -1)
385 17ef54d6 2022-06-22 op player_senderr(errstr);
386 239029b6 2022-05-09 op if (r == 0)
387 06412529 2022-05-09 op player_sendeof();
388 3baa2617 2022-02-16 op }
389 3baa2617 2022-02-16 op
390 3baa2617 2022-02-16 op return 0;
391 3baa2617 2022-02-16 op }