Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <limits.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <poll.h>
27 #include <signal.h>
28 #include <sndio.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <imsg.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <unistd.h>
37 #include "amused.h"
38 #include "log.h"
39 #include "xmalloc.h"
41 struct sio_hdl *hdl;
42 static struct imsgbuf *ibuf;
44 static int nextfd = -1;
46 volatile sig_atomic_t halted;
48 static void
49 player_signal_handler(int signo)
50 {
51 halted = 1;
52 }
54 static void
55 audio_init(void)
56 {
57 struct sio_par par;
59 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
60 fatal("sio_open");
62 sio_initpar(&par);
63 par.bits = 16;
64 par.appbufsz = 50 * par.rate / 1000;
65 par.pchan = 2;
66 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
67 fatal("couldn't set audio params");
68 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
69 fatalx("unsupported audio params");
70 if (!sio_start(hdl))
71 fatal("sio_start");
72 }
74 int
75 player_setup(int bits, int rate, int channels)
76 {
77 struct sio_par par;
79 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
80 bits, rate, channels);
82 sio_stop(hdl);
84 sio_initpar(&par);
85 par.bits = bits;
86 par.rate = rate;
87 par.pchan = channels;
88 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
89 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
90 bits, rate, channels);
91 return -1;
92 }
94 if (par.bits != bits || par.pchan != channels) {
95 log_warnx("failed to set params");
96 return -1;
97 }
99 /* TODO: check the sample rate? */
101 if (!sio_start(hdl)) {
102 log_warn("sio_start");
103 return -1;
105 return 0;
108 int
109 player_pendingimsg(void)
111 struct pollfd pfd;
112 int r;
114 if (halted != 0)
115 return 1;
117 pfd.fd = ibuf->fd;
118 pfd.events = POLLIN;
120 r = poll(&pfd, 1, 0);
121 if (r == -1)
122 fatal("poll");
123 return r;
126 void
127 player_enqueue(struct imsg *imsg)
129 if (nextfd != -1)
130 fatalx("track already enqueued");
132 if ((nextfd = imsg->fd) == -1)
133 fatalx("%s: got invalid file descriptor", __func__);
134 log_debug("song enqueued");
137 /* process only one message */
138 int
139 player_dispatch(void)
141 struct pollfd pfd;
142 struct imsg imsg;
143 ssize_t n;
144 int ret;
146 if (halted != 0)
147 return IMSG_STOP;
149 again:
150 if ((n = imsg_get(ibuf, &imsg)) == -1)
151 fatal("imsg_get");
152 if (n == 0) {
153 pfd.fd = ibuf->fd;
154 pfd.events = POLLIN;
155 if (poll(&pfd, 1, INFTIM) == -1)
156 fatal("poll");
157 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
158 fatal("imsg_read");
159 if (n == 0)
160 fatalx("pipe closed");
161 goto again;
164 ret = imsg.hdr.type;
165 switch (imsg.hdr.type) {
166 case IMSG_PLAY:
167 player_enqueue(&imsg);
168 ret = IMSG_STOP;
169 break;
170 case IMSG_RESUME:
171 case IMSG_PAUSE:
172 case IMSG_STOP:
173 break;
174 default:
175 fatalx("unknown imsg %d", imsg.hdr.type);
178 return ret;
181 void
182 player_senderr(void)
184 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
185 imsg_flush(ibuf);
188 void
189 player_sendeof(void)
191 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
192 imsg_flush(ibuf);
195 int
196 player_playnext(void)
198 static char buf[512];
199 ssize_t r;
200 int fd = nextfd;
202 assert(nextfd != -1);
203 nextfd = -1;
205 r = read(fd, buf, sizeof(buf));
207 /* 8 byte is the larger magic number */
208 if (r < 8) {
209 log_warn("read failed");
210 goto err;
213 if (lseek(fd, 0, SEEK_SET) == -1) {
214 log_warn("lseek failed");
215 goto err;
218 if (memcmp(buf, "fLaC", 4) == 0)
219 return play_flac(fd);
220 if (memcmp(buf, "ID3", 3) == 0 ||
221 memcmp(buf, "\xFF\xFB", 2) == 0)
222 return play_mp3(fd);
223 if (memmem(buf, r, "OpusHead", 8) != NULL)
224 return play_opus(fd);
225 if (memmem(buf, r, "OggS", 4) != NULL)
226 return play_oggvorbis(fd);
228 log_warnx("unknown file type");
229 err:
230 close(fd);
231 return -1;
234 int
235 player_pause(void)
237 int r;
239 r = player_dispatch();
240 return r == IMSG_RESUME;
243 int
244 player_shouldstop(void)
246 if (!player_pendingimsg())
247 return 0;
249 switch (player_dispatch()) {
250 case IMSG_PAUSE:
251 if (player_pause())
252 break;
253 /* fallthrough */
254 case IMSG_STOP:
255 return 1;
258 return 0;
261 int
262 play(const void *buf, size_t len)
264 if (player_shouldstop())
265 return 0;
266 sio_write(hdl, buf, len);
267 return 1;
270 int
271 player(int debug, int verbose)
273 int r;
275 log_init(debug, LOG_DAEMON);
276 log_setverbose(verbose);
278 setproctitle("player");
279 log_procinit("player");
281 #if 0
283 static int attached;
285 while (!attached)
286 sleep(1);
288 #endif
290 audio_init();
292 ibuf = xmalloc(sizeof(*ibuf));
293 imsg_init(ibuf, 3);
295 signal(SIGINT, player_signal_handler);
296 signal(SIGTERM, player_signal_handler);
298 signal(SIGHUP, SIG_IGN);
299 signal(SIGPIPE, SIG_IGN);
301 if (pledge("stdio recvfd audio", NULL) == -1)
302 fatal("pledge");
304 while (!halted) {
305 while (nextfd == -1)
306 player_dispatch();
308 r = player_playnext();
309 if (r == -1)
310 player_senderr();
311 if (r == 0)
312 player_sendeof();
315 return 0;