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 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>
20 3baa2617 2022-02-16 op
21 3baa2617 2022-02-16 op #include <limits.h>
22 3baa2617 2022-02-16 op
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 <fcntl.h>
27 3baa2617 2022-02-16 op #include <poll.h>
28 3baa2617 2022-02-16 op #include <signal.h>
29 3baa2617 2022-02-16 op #include <sndio.h>
30 14be015f 2022-02-17 op #include <stdio.h>
31 3baa2617 2022-02-16 op #include <stdlib.h>
32 3baa2617 2022-02-16 op #include <stdint.h>
33 3baa2617 2022-02-16 op #include <imsg.h>
34 3baa2617 2022-02-16 op #include <string.h>
35 3baa2617 2022-02-16 op #include <syslog.h>
36 3baa2617 2022-02-16 op #include <unistd.h>
37 3baa2617 2022-02-16 op
38 3baa2617 2022-02-16 op #include "amused.h"
39 3baa2617 2022-02-16 op #include "log.h"
40 3baa2617 2022-02-16 op #include "xmalloc.h"
41 3baa2617 2022-02-16 op
42 5a4b3030 2022-03-09 op struct sio_hdl *hdl;
43 5a4b3030 2022-03-09 op static struct imsgbuf *ibuf;
44 3baa2617 2022-02-16 op
45 3baa2617 2022-02-16 op static int nextfd = -1;
46 3baa2617 2022-02-16 op static char nextpath[PATH_MAX];
47 3baa2617 2022-02-16 op
48 3baa2617 2022-02-16 op volatile sig_atomic_t halted;
49 3baa2617 2022-02-16 op
50 3baa2617 2022-02-16 op static void
51 3baa2617 2022-02-16 op player_signal_handler(int signo)
52 3baa2617 2022-02-16 op {
53 3baa2617 2022-02-16 op halted = 1;
54 3baa2617 2022-02-16 op }
55 3baa2617 2022-02-16 op
56 3baa2617 2022-02-16 op static void
57 3baa2617 2022-02-16 op audio_init(void)
58 3baa2617 2022-02-16 op {
59 3baa2617 2022-02-16 op struct sio_par par;
60 3baa2617 2022-02-16 op
61 3baa2617 2022-02-16 op if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
62 3baa2617 2022-02-16 op fatal("sio_open");
63 3baa2617 2022-02-16 op
64 3baa2617 2022-02-16 op sio_initpar(&par);
65 3baa2617 2022-02-16 op par.bits = 16;
66 3baa2617 2022-02-16 op par.appbufsz = 50 * par.rate / 1000;
67 3baa2617 2022-02-16 op par.pchan = 2;
68 3baa2617 2022-02-16 op if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
69 3baa2617 2022-02-16 op fatal("couldn't set audio params");
70 3baa2617 2022-02-16 op if (par.bits != 16 || par.le != SIO_LE_NATIVE)
71 3baa2617 2022-02-16 op fatalx("unsupported audio params");
72 3baa2617 2022-02-16 op if (!sio_start(hdl))
73 3baa2617 2022-02-16 op fatal("sio_start");
74 3baa2617 2022-02-16 op }
75 3baa2617 2022-02-16 op
76 3baa2617 2022-02-16 op int
77 e24324f1 2022-02-21 op player_setup(int bits, int rate, int channels)
78 3baa2617 2022-02-16 op {
79 3baa2617 2022-02-16 op struct sio_par par;
80 3baa2617 2022-02-16 op
81 a728254f 2022-02-23 op log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
82 a728254f 2022-02-23 op bits, rate, channels);
83 3baa2617 2022-02-16 op
84 3baa2617 2022-02-16 op sio_stop(hdl);
85 3baa2617 2022-02-16 op
86 3baa2617 2022-02-16 op sio_initpar(&par);
87 e24324f1 2022-02-21 op par.bits = bits;
88 3baa2617 2022-02-16 op par.rate = rate;
89 7fc831ea 2022-02-18 op par.pchan = channels;
90 3baa2617 2022-02-16 op if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
91 f002c42a 2022-05-09 op log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
92 f002c42a 2022-05-09 op bits, rate, channels);
93 7fc831ea 2022-02-18 op return -1;
94 7fc831ea 2022-02-18 op }
95 7fc831ea 2022-02-18 op
96 e24324f1 2022-02-21 op if (par.bits != bits || par.pchan != channels) {
97 7fc831ea 2022-02-18 op log_warnx("failed to set params");
98 3baa2617 2022-02-16 op return -1;
99 3baa2617 2022-02-16 op }
100 3baa2617 2022-02-16 op
101 3baa2617 2022-02-16 op /* TODO: check the sample rate? */
102 3baa2617 2022-02-16 op
103 3baa2617 2022-02-16 op if (!sio_start(hdl)) {
104 3baa2617 2022-02-16 op log_warn("sio_start");
105 3baa2617 2022-02-16 op return -1;
106 3baa2617 2022-02-16 op }
107 3baa2617 2022-02-16 op return 0;
108 3baa2617 2022-02-16 op }
109 3baa2617 2022-02-16 op
110 3baa2617 2022-02-16 op int
111 3baa2617 2022-02-16 op player_pendingimsg(void)
112 3baa2617 2022-02-16 op {
113 3baa2617 2022-02-16 op struct pollfd pfd;
114 3baa2617 2022-02-16 op int r;
115 3baa2617 2022-02-16 op
116 3baa2617 2022-02-16 op if (halted != 0)
117 3baa2617 2022-02-16 op return 1;
118 3baa2617 2022-02-16 op
119 3baa2617 2022-02-16 op pfd.fd = ibuf->fd;
120 9fdc3eb6 2022-02-16 op pfd.events = POLLIN;
121 3baa2617 2022-02-16 op
122 3baa2617 2022-02-16 op r = poll(&pfd, 1, 0);
123 3baa2617 2022-02-16 op if (r == -1)
124 3baa2617 2022-02-16 op fatal("poll");
125 3baa2617 2022-02-16 op return r;
126 3baa2617 2022-02-16 op }
127 3baa2617 2022-02-16 op
128 3baa2617 2022-02-16 op void
129 3baa2617 2022-02-16 op player_enqueue(struct imsg *imsg)
130 3baa2617 2022-02-16 op {
131 3baa2617 2022-02-16 op size_t datalen;
132 3baa2617 2022-02-16 op
133 3baa2617 2022-02-16 op if (nextfd != -1)
134 3baa2617 2022-02-16 op fatalx("track already enqueued");
135 3baa2617 2022-02-16 op
136 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
137 3baa2617 2022-02-16 op if (datalen != sizeof(nextpath))
138 3baa2617 2022-02-16 op fatalx("%s: size mismatch", __func__);
139 3baa2617 2022-02-16 op memcpy(nextpath, imsg->data, sizeof(nextpath));
140 3baa2617 2022-02-16 op if (nextpath[datalen-1] != '\0')
141 3baa2617 2022-02-16 op fatalx("%s: corrupted path", __func__);
142 3baa2617 2022-02-16 op if ((nextfd = imsg->fd) == -1)
143 3baa2617 2022-02-16 op fatalx("%s: got invalid file descriptor", __func__);
144 3baa2617 2022-02-16 op log_debug("enqueued %s", nextpath);
145 3baa2617 2022-02-16 op }
146 3baa2617 2022-02-16 op
147 3baa2617 2022-02-16 op /* process only one message */
148 3baa2617 2022-02-16 op int
149 3baa2617 2022-02-16 op player_dispatch(void)
150 3baa2617 2022-02-16 op {
151 3baa2617 2022-02-16 op struct imsg imsg;
152 3baa2617 2022-02-16 op ssize_t n;
153 3baa2617 2022-02-16 op int ret;
154 3baa2617 2022-02-16 op
155 3baa2617 2022-02-16 op if (halted != 0)
156 3baa2617 2022-02-16 op return IMSG_STOP;
157 3baa2617 2022-02-16 op
158 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
159 3baa2617 2022-02-16 op fatalx("imsg_read");
160 3baa2617 2022-02-16 op if (n == 0)
161 3baa2617 2022-02-16 op fatalx("pipe closed");
162 3baa2617 2022-02-16 op
163 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
164 3baa2617 2022-02-16 op fatal("imsg_get");
165 3baa2617 2022-02-16 op if (n == 0) /* no more messages */
166 3baa2617 2022-02-16 op fatalx("expected at least a message");
167 3baa2617 2022-02-16 op
168 3baa2617 2022-02-16 op ret = imsg.hdr.type;
169 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
170 3baa2617 2022-02-16 op case IMSG_PLAY:
171 3baa2617 2022-02-16 op player_enqueue(&imsg);
172 3baa2617 2022-02-16 op ret = IMSG_STOP;
173 3baa2617 2022-02-16 op break;
174 3baa2617 2022-02-16 op case IMSG_RESUME:
175 3baa2617 2022-02-16 op case IMSG_PAUSE:
176 3baa2617 2022-02-16 op case IMSG_STOP:
177 3baa2617 2022-02-16 op break;
178 3baa2617 2022-02-16 op default:
179 3baa2617 2022-02-16 op fatalx("unknown imsg %d", imsg.hdr.type);
180 3baa2617 2022-02-16 op }
181 3baa2617 2022-02-16 op
182 3baa2617 2022-02-16 op return ret;
183 3baa2617 2022-02-16 op }
184 3baa2617 2022-02-16 op
185 3baa2617 2022-02-16 op void
186 3baa2617 2022-02-16 op player_senderr(void)
187 3baa2617 2022-02-16 op {
188 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
189 3baa2617 2022-02-16 op imsg_flush(ibuf);
190 3baa2617 2022-02-16 op }
191 3baa2617 2022-02-16 op
192 3baa2617 2022-02-16 op void
193 3baa2617 2022-02-16 op player_sendeof(void)
194 3baa2617 2022-02-16 op {
195 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
196 3baa2617 2022-02-16 op imsg_flush(ibuf);
197 3baa2617 2022-02-16 op }
198 3baa2617 2022-02-16 op
199 06412529 2022-05-09 op int
200 3baa2617 2022-02-16 op player_playnext(void)
201 3baa2617 2022-02-16 op {
202 bf19b03e 2022-05-09 op static char buf[512];
203 bf19b03e 2022-05-09 op ssize_t r;
204 3baa2617 2022-02-16 op int fd = nextfd;
205 3baa2617 2022-02-16 op
206 3baa2617 2022-02-16 op assert(nextfd != -1);
207 3baa2617 2022-02-16 op nextfd = -1;
208 3baa2617 2022-02-16 op
209 bf19b03e 2022-05-09 op r = read(fd, buf, sizeof(buf));
210 bf19b03e 2022-05-09 op
211 bf19b03e 2022-05-09 op /* 8 byte is the larger magic number */
212 bf19b03e 2022-05-09 op if (r < 8) {
213 bf19b03e 2022-05-09 op log_warn("failed to read %s", nextpath);
214 bf19b03e 2022-05-09 op goto err;
215 bf19b03e 2022-05-09 op }
216 bf19b03e 2022-05-09 op
217 bf19b03e 2022-05-09 op if (lseek(fd, 0, SEEK_SET) == -1) {
218 bf19b03e 2022-05-09 op log_warn("lseek failed");
219 bf19b03e 2022-05-09 op goto err;
220 bf19b03e 2022-05-09 op }
221 bf19b03e 2022-05-09 op
222 bf19b03e 2022-05-09 op if (memcmp(buf, "fLaC", 4) == 0)
223 bf19b03e 2022-05-09 op return play_flac(fd);
224 bf19b03e 2022-05-09 op if (memcmp(buf, "ID3", 3) == 0 ||
225 bf19b03e 2022-05-09 op memcmp(buf, "\xFF\xFB", 2) == 0)
226 bf19b03e 2022-05-09 op return play_mp3(fd);
227 bf19b03e 2022-05-09 op if (memmem(buf, r, "OpusHead", 8) != NULL)
228 06412529 2022-05-09 op return play_opus(fd);
229 bf19b03e 2022-05-09 op if (memmem(buf, r, "OggS", 4) != NULL)
230 bf19b03e 2022-05-09 op return play_oggvorbis(fd);
231 25cb72fb 2022-02-22 op
232 06412529 2022-05-09 op log_warnx("unknown file type for %s", nextpath);
233 bf19b03e 2022-05-09 op err:
234 184600a8 2022-05-09 op close(fd);
235 06412529 2022-05-09 op return -1;
236 3baa2617 2022-02-16 op }
237 3baa2617 2022-02-16 op
238 3baa2617 2022-02-16 op int
239 3baa2617 2022-02-16 op player_pause(void)
240 3baa2617 2022-02-16 op {
241 3baa2617 2022-02-16 op int r;
242 3baa2617 2022-02-16 op
243 3baa2617 2022-02-16 op r = player_dispatch();
244 3baa2617 2022-02-16 op return r == IMSG_RESUME;
245 3baa2617 2022-02-16 op }
246 3baa2617 2022-02-16 op
247 3baa2617 2022-02-16 op int
248 3baa2617 2022-02-16 op player_shouldstop(void)
249 3baa2617 2022-02-16 op {
250 3baa2617 2022-02-16 op if (!player_pendingimsg())
251 3baa2617 2022-02-16 op return 0;
252 3baa2617 2022-02-16 op
253 3baa2617 2022-02-16 op switch (player_dispatch()) {
254 3baa2617 2022-02-16 op case IMSG_PAUSE:
255 3baa2617 2022-02-16 op if (player_pause())
256 3baa2617 2022-02-16 op break;
257 3baa2617 2022-02-16 op /* fallthrough */
258 3baa2617 2022-02-16 op case IMSG_STOP:
259 3baa2617 2022-02-16 op return 1;
260 3baa2617 2022-02-16 op }
261 3baa2617 2022-02-16 op
262 3baa2617 2022-02-16 op return 0;
263 3baa2617 2022-02-16 op }
264 3baa2617 2022-02-16 op
265 3baa2617 2022-02-16 op int
266 2139c525 2022-03-09 op play(const void *buf, size_t len)
267 2139c525 2022-03-09 op {
268 2139c525 2022-03-09 op if (player_shouldstop())
269 2139c525 2022-03-09 op return 0;
270 2139c525 2022-03-09 op sio_write(hdl, buf, len);
271 2139c525 2022-03-09 op return 1;
272 2139c525 2022-03-09 op }
273 2139c525 2022-03-09 op
274 2139c525 2022-03-09 op int
275 3baa2617 2022-02-16 op player(int debug, int verbose)
276 3baa2617 2022-02-16 op {
277 3baa2617 2022-02-16 op int flags;
278 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
279 3baa2617 2022-02-16 op log_setverbose(verbose);
280 3baa2617 2022-02-16 op
281 3baa2617 2022-02-16 op setproctitle("player");
282 3baa2617 2022-02-16 op log_procinit("player");
283 3baa2617 2022-02-16 op
284 3baa2617 2022-02-16 op #if 0
285 3baa2617 2022-02-16 op {
286 3baa2617 2022-02-16 op static int attached;
287 3baa2617 2022-02-16 op
288 3baa2617 2022-02-16 op while (!attached)
289 3baa2617 2022-02-16 op sleep(1);
290 3baa2617 2022-02-16 op }
291 3baa2617 2022-02-16 op #endif
292 3baa2617 2022-02-16 op
293 3baa2617 2022-02-16 op audio_init();
294 3baa2617 2022-02-16 op
295 3baa2617 2022-02-16 op /* mark fd as blocking i/o mode */
296 3baa2617 2022-02-16 op if ((flags = fcntl(3, F_GETFL)) == -1)
297 3baa2617 2022-02-16 op fatal("fcntl(F_GETFL)");
298 3baa2617 2022-02-16 op if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
299 3baa2617 2022-02-16 op fatal("fcntl F_SETFL O_NONBLOCK");
300 3baa2617 2022-02-16 op
301 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
302 3baa2617 2022-02-16 op imsg_init(ibuf, 3);
303 3baa2617 2022-02-16 op
304 3baa2617 2022-02-16 op signal(SIGINT, player_signal_handler);
305 3baa2617 2022-02-16 op signal(SIGTERM, player_signal_handler);
306 3baa2617 2022-02-16 op
307 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
308 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
309 3baa2617 2022-02-16 op
310 251e00ff 2022-03-10 op if (pledge("stdio recvfd audio", NULL) == -1)
311 3baa2617 2022-02-16 op fatal("pledge");
312 3baa2617 2022-02-16 op
313 3baa2617 2022-02-16 op while (!halted) {
314 3baa2617 2022-02-16 op while (nextfd == -1)
315 1489a53d 2022-02-16 op player_dispatch();
316 06412529 2022-05-09 op
317 06412529 2022-05-09 op if (player_playnext() == -1)
318 06412529 2022-05-09 op player_senderr();
319 06412529 2022-05-09 op else
320 06412529 2022-05-09 op player_sendeof();
321 3baa2617 2022-02-16 op }
322 3baa2617 2022-02-16 op
323 3baa2617 2022-02-16 op return 0;
324 3baa2617 2022-02-16 op }