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 <fcntl.h>
27 #include <poll.h>
28 #include <signal.h>
29 #include <sndio.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <imsg.h>
34 #include <string.h>
35 #include <syslog.h>
36 #include <unistd.h>
38 #include "amused.h"
39 #include "log.h"
40 #include "xmalloc.h"
42 struct sio_hdl *hdl;
43 static struct imsgbuf *ibuf;
45 static int nextfd = -1;
47 volatile sig_atomic_t halted;
49 static void
50 player_signal_handler(int signo)
51 {
52 halted = 1;
53 }
55 static void
56 audio_init(void)
57 {
58 struct sio_par par;
60 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
61 fatal("sio_open");
63 sio_initpar(&par);
64 par.bits = 16;
65 par.appbufsz = 50 * par.rate / 1000;
66 par.pchan = 2;
67 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
68 fatal("couldn't set audio params");
69 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
70 fatalx("unsupported audio params");
71 if (!sio_start(hdl))
72 fatal("sio_start");
73 }
75 int
76 player_setup(int bits, int rate, int channels)
77 {
78 struct sio_par par;
80 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
81 bits, rate, channels);
83 sio_stop(hdl);
85 sio_initpar(&par);
86 par.bits = bits;
87 par.rate = rate;
88 par.pchan = channels;
89 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
90 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
91 bits, rate, channels);
92 return -1;
93 }
95 if (par.bits != bits || par.pchan != channels) {
96 log_warnx("failed to set params");
97 return -1;
98 }
100 /* TODO: check the sample rate? */
102 if (!sio_start(hdl)) {
103 log_warn("sio_start");
104 return -1;
106 return 0;
109 int
110 player_pendingimsg(void)
112 struct pollfd pfd;
113 int r;
115 if (halted != 0)
116 return 1;
118 pfd.fd = ibuf->fd;
119 pfd.events = POLLIN;
121 r = poll(&pfd, 1, 0);
122 if (r == -1)
123 fatal("poll");
124 return r;
127 void
128 player_enqueue(struct imsg *imsg)
130 if (nextfd != -1)
131 fatalx("track already enqueued");
133 if ((nextfd = imsg->fd) == -1)
134 fatalx("%s: got invalid file descriptor", __func__);
135 log_debug("song enqueued");
138 /* process only one message */
139 int
140 player_dispatch(void)
142 struct imsg imsg;
143 ssize_t n;
144 int ret;
146 if (halted != 0)
147 return IMSG_STOP;
149 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
150 fatalx("imsg_read");
151 if (n == 0)
152 fatalx("pipe closed");
154 if ((n = imsg_get(ibuf, &imsg)) == -1)
155 fatal("imsg_get");
156 if (n == 0) /* no more messages */
157 fatalx("expected at least a message");
159 ret = imsg.hdr.type;
160 switch (imsg.hdr.type) {
161 case IMSG_PLAY:
162 player_enqueue(&imsg);
163 ret = IMSG_STOP;
164 break;
165 case IMSG_RESUME:
166 case IMSG_PAUSE:
167 case IMSG_STOP:
168 break;
169 default:
170 fatalx("unknown imsg %d", imsg.hdr.type);
173 return ret;
176 void
177 player_senderr(void)
179 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
180 imsg_flush(ibuf);
183 void
184 player_sendeof(void)
186 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
187 imsg_flush(ibuf);
190 int
191 player_playnext(void)
193 static char buf[512];
194 ssize_t r;
195 int fd = nextfd;
197 assert(nextfd != -1);
198 nextfd = -1;
200 r = read(fd, buf, sizeof(buf));
202 /* 8 byte is the larger magic number */
203 if (r < 8) {
204 log_warn("read failed");
205 goto err;
208 if (lseek(fd, 0, SEEK_SET) == -1) {
209 log_warn("lseek failed");
210 goto err;
213 if (memcmp(buf, "fLaC", 4) == 0)
214 return play_flac(fd);
215 if (memcmp(buf, "ID3", 3) == 0 ||
216 memcmp(buf, "\xFF\xFB", 2) == 0)
217 return play_mp3(fd);
218 if (memmem(buf, r, "OpusHead", 8) != NULL)
219 return play_opus(fd);
220 if (memmem(buf, r, "OggS", 4) != NULL)
221 return play_oggvorbis(fd);
223 log_warnx("unknown file type");
224 err:
225 close(fd);
226 return -1;
229 int
230 player_pause(void)
232 int r;
234 r = player_dispatch();
235 return r == IMSG_RESUME;
238 int
239 player_shouldstop(void)
241 if (!player_pendingimsg())
242 return 0;
244 switch (player_dispatch()) {
245 case IMSG_PAUSE:
246 if (player_pause())
247 break;
248 /* fallthrough */
249 case IMSG_STOP:
250 return 1;
253 return 0;
256 int
257 play(const void *buf, size_t len)
259 if (player_shouldstop())
260 return 0;
261 sio_write(hdl, buf, len);
262 return 1;
265 int
266 player(int debug, int verbose)
268 int flags, r;
269 log_init(debug, LOG_DAEMON);
270 log_setverbose(verbose);
272 setproctitle("player");
273 log_procinit("player");
275 #if 0
277 static int attached;
279 while (!attached)
280 sleep(1);
282 #endif
284 audio_init();
286 /* mark fd as blocking i/o mode */
287 if ((flags = fcntl(3, F_GETFL)) == -1)
288 fatal("fcntl(F_GETFL)");
289 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
290 fatal("fcntl F_SETFL O_NONBLOCK");
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;