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