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