2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
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>
21 3baa2617 2022-02-16 op #include <limits.h>
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 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>
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"
41 3baa2617 2022-02-16 op static struct imsgbuf *ibuf;
43 63f5223a 2022-02-17 op static int got_stop;
44 3baa2617 2022-02-16 op static int nextfd = -1;
45 3baa2617 2022-02-16 op static char nextpath[PATH_MAX];
47 3baa2617 2022-02-16 op volatile sig_atomic_t halted;
50 3baa2617 2022-02-16 op player_signal_handler(int signo)
56 3baa2617 2022-02-16 op audio_init(void)
58 3baa2617 2022-02-16 op struct sio_par par;
60 3baa2617 2022-02-16 op if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
61 3baa2617 2022-02-16 op fatal("sio_open");
63 3baa2617 2022-02-16 op sio_initpar(&par);
64 3baa2617 2022-02-16 op par.bits = 16;
65 3baa2617 2022-02-16 op par.appbufsz = 50 * par.rate / 1000;
66 3baa2617 2022-02-16 op par.pchan = 2;
67 3baa2617 2022-02-16 op if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
68 3baa2617 2022-02-16 op fatal("couldn't set audio params");
69 3baa2617 2022-02-16 op if (par.bits != 16 || par.le != SIO_LE_NATIVE)
70 3baa2617 2022-02-16 op fatalx("unsupported audio params");
71 3baa2617 2022-02-16 op if (!sio_start(hdl))
72 3baa2617 2022-02-16 op fatal("sio_start");
76 3baa2617 2022-02-16 op player_setrate(int rate)
78 3baa2617 2022-02-16 op struct sio_par par;
80 3baa2617 2022-02-16 op log_debug("switching to sample rate %d", rate);
82 3baa2617 2022-02-16 op sio_stop(hdl);
84 3baa2617 2022-02-16 op sio_initpar(&par);
85 3baa2617 2022-02-16 op par.rate = rate;
86 3baa2617 2022-02-16 op if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
87 3baa2617 2022-02-16 op log_warnx("invalid params");
91 3baa2617 2022-02-16 op /* TODO: check the sample rate? */
93 3baa2617 2022-02-16 op if (!sio_start(hdl)) {
94 3baa2617 2022-02-16 op log_warn("sio_start");
101 3baa2617 2022-02-16 op player_pendingimsg(void)
103 3baa2617 2022-02-16 op struct pollfd pfd;
106 3baa2617 2022-02-16 op if (halted != 0)
109 3baa2617 2022-02-16 op pfd.fd = ibuf->fd;
110 9fdc3eb6 2022-02-16 op pfd.events = POLLIN;
112 3baa2617 2022-02-16 op r = poll(&pfd, 1, 0);
114 3baa2617 2022-02-16 op fatal("poll");
119 3baa2617 2022-02-16 op player_enqueue(struct imsg *imsg)
121 3baa2617 2022-02-16 op size_t datalen;
123 3baa2617 2022-02-16 op if (nextfd != -1)
124 3baa2617 2022-02-16 op fatalx("track already enqueued");
126 3baa2617 2022-02-16 op datalen = IMSG_DATA_SIZE(*imsg);
127 3baa2617 2022-02-16 op if (datalen != sizeof(nextpath))
128 3baa2617 2022-02-16 op fatalx("%s: size mismatch", __func__);
129 3baa2617 2022-02-16 op memcpy(nextpath, imsg->data, sizeof(nextpath));
130 3baa2617 2022-02-16 op if (nextpath[datalen-1] != '\0')
131 3baa2617 2022-02-16 op fatalx("%s: corrupted path", __func__);
132 3baa2617 2022-02-16 op if ((nextfd = imsg->fd) == -1)
133 3baa2617 2022-02-16 op fatalx("%s: got invalid file descriptor", __func__);
134 3baa2617 2022-02-16 op log_debug("enqueued %s", nextpath);
137 3baa2617 2022-02-16 op /* process only one message */
139 3baa2617 2022-02-16 op player_dispatch(void)
141 3baa2617 2022-02-16 op struct imsg imsg;
145 3baa2617 2022-02-16 op if (halted != 0)
146 3baa2617 2022-02-16 op return IMSG_STOP;
148 3baa2617 2022-02-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
149 3baa2617 2022-02-16 op fatalx("imsg_read");
151 3baa2617 2022-02-16 op fatalx("pipe closed");
153 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
154 3baa2617 2022-02-16 op fatal("imsg_get");
155 3baa2617 2022-02-16 op if (n == 0) /* no more messages */
156 3baa2617 2022-02-16 op fatalx("expected at least a message");
158 3baa2617 2022-02-16 op ret = imsg.hdr.type;
159 63f5223a 2022-02-17 op if (ret == IMSG_STOP)
160 63f5223a 2022-02-17 op got_stop = 1;
161 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
162 3baa2617 2022-02-16 op case IMSG_PLAY:
163 3baa2617 2022-02-16 op player_enqueue(&imsg);
164 3baa2617 2022-02-16 op ret = IMSG_STOP;
166 3baa2617 2022-02-16 op case IMSG_RESUME:
167 3baa2617 2022-02-16 op case IMSG_PAUSE:
168 3baa2617 2022-02-16 op case IMSG_STOP:
171 3baa2617 2022-02-16 op fatalx("unknown imsg %d", imsg.hdr.type);
178 3baa2617 2022-02-16 op player_senderr(void)
180 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
181 3baa2617 2022-02-16 op imsg_flush(ibuf);
185 3baa2617 2022-02-16 op player_sendeof(void)
187 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
188 3baa2617 2022-02-16 op imsg_flush(ibuf);
192 3baa2617 2022-02-16 op player_playnext(void)
194 3baa2617 2022-02-16 op int fd = nextfd;
196 3baa2617 2022-02-16 op assert(nextfd != -1);
199 3baa2617 2022-02-16 op /* XXX: use magic(5) for this, not file extensions */
200 3baa2617 2022-02-16 op if (strstr(nextpath, ".ogg") != NULL)
201 3baa2617 2022-02-16 op play_oggvorbis(fd);
202 3baa2617 2022-02-16 op else if (strstr(nextpath, ".mp3") != NULL)
203 3baa2617 2022-02-16 op play_mp3(fd);
204 3baa2617 2022-02-16 op else if (strstr(nextpath, ".flac") != NULL)
205 3baa2617 2022-02-16 op play_flac(fd);
206 3baa2617 2022-02-16 op else if (strstr(nextpath, ".opus") != NULL)
207 3baa2617 2022-02-16 op play_opus(fd);
209 3baa2617 2022-02-16 op log_warnx("unknown file type for %s", nextpath);
210 3baa2617 2022-02-16 op player_senderr();
216 3baa2617 2022-02-16 op player_pause(void)
220 3baa2617 2022-02-16 op r = player_dispatch();
221 3baa2617 2022-02-16 op return r == IMSG_RESUME;
225 3baa2617 2022-02-16 op player_shouldstop(void)
227 3baa2617 2022-02-16 op if (!player_pendingimsg())
230 3baa2617 2022-02-16 op switch (player_dispatch()) {
231 3baa2617 2022-02-16 op case IMSG_PAUSE:
232 3baa2617 2022-02-16 op if (player_pause())
234 3baa2617 2022-02-16 op /* fallthrough */
235 3baa2617 2022-02-16 op case IMSG_STOP:
243 3baa2617 2022-02-16 op player(int debug, int verbose)
246 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
247 3baa2617 2022-02-16 op log_setverbose(verbose);
249 3baa2617 2022-02-16 op setproctitle("player");
250 3baa2617 2022-02-16 op log_procinit("player");
254 3baa2617 2022-02-16 op static int attached;
256 3baa2617 2022-02-16 op while (!attached)
261 3baa2617 2022-02-16 op audio_init();
263 3baa2617 2022-02-16 op /* mark fd as blocking i/o mode */
264 3baa2617 2022-02-16 op if ((flags = fcntl(3, F_GETFL)) == -1)
265 3baa2617 2022-02-16 op fatal("fcntl(F_GETFL)");
266 3baa2617 2022-02-16 op if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
267 3baa2617 2022-02-16 op fatal("fcntl F_SETFL O_NONBLOCK");
269 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
270 3baa2617 2022-02-16 op imsg_init(ibuf, 3);
272 3baa2617 2022-02-16 op signal(SIGINT, player_signal_handler);
273 3baa2617 2022-02-16 op signal(SIGTERM, player_signal_handler);
275 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
276 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
278 3baa2617 2022-02-16 op if (pledge("stdio recvfd", NULL) == -1)
279 3baa2617 2022-02-16 op fatal("pledge");
281 3baa2617 2022-02-16 op while (!halted) {
282 3baa2617 2022-02-16 op while (nextfd == -1)
283 1489a53d 2022-02-16 op player_dispatch();
285 3baa2617 2022-02-16 op player_playnext();
287 63f5223a 2022-02-17 op if (!got_stop)
288 63f5223a 2022-02-17 op player_sendeof();
290 63f5223a 2022-02-17 op got_stop = 0;