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 got_stop;
46 static int nextfd = -1;
47 static char nextpath[PATH_MAX];
49 volatile sig_atomic_t halted;
51 static void
52 player_signal_handler(int signo)
53 {
54 halted = 1;
55 }
57 static void
58 audio_init(void)
59 {
60 struct sio_par par;
62 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
63 fatal("sio_open");
65 sio_initpar(&par);
66 par.bits = 16;
67 par.appbufsz = 50 * par.rate / 1000;
68 par.pchan = 2;
69 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
70 fatal("couldn't set audio params");
71 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
72 fatalx("unsupported audio params");
73 if (!sio_start(hdl))
74 fatal("sio_start");
75 }
77 int
78 player_setup(int bits, int rate, int channels)
79 {
80 struct sio_par par;
82 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
83 bits, rate, channels);
85 sio_stop(hdl);
87 sio_initpar(&par);
88 par.bits = bits;
89 par.rate = rate;
90 par.pchan = channels;
91 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
92 log_warnx("invalid params");
93 return -1;
94 }
96 if (par.bits != bits || par.pchan != channels) {
97 log_warnx("failed to set params");
98 return -1;
99 }
101 /* TODO: check the sample rate? */
103 if (!sio_start(hdl)) {
104 log_warn("sio_start");
105 return -1;
107 return 0;
110 int
111 player_pendingimsg(void)
113 struct pollfd pfd;
114 int r;
116 if (halted != 0)
117 return 1;
119 pfd.fd = ibuf->fd;
120 pfd.events = POLLIN;
122 r = poll(&pfd, 1, 0);
123 if (r == -1)
124 fatal("poll");
125 return r;
128 void
129 player_enqueue(struct imsg *imsg)
131 size_t datalen;
133 if (nextfd != -1)
134 fatalx("track already enqueued");
136 datalen = IMSG_DATA_SIZE(*imsg);
137 if (datalen != sizeof(nextpath))
138 fatalx("%s: size mismatch", __func__);
139 memcpy(nextpath, imsg->data, sizeof(nextpath));
140 if (nextpath[datalen-1] != '\0')
141 fatalx("%s: corrupted path", __func__);
142 if ((nextfd = imsg->fd) == -1)
143 fatalx("%s: got invalid file descriptor", __func__);
144 log_debug("enqueued %s", nextpath);
147 /* process only one message */
148 int
149 player_dispatch(void)
151 struct imsg imsg;
152 ssize_t n;
153 int ret;
155 if (halted != 0)
156 return IMSG_STOP;
158 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
159 fatalx("imsg_read");
160 if (n == 0)
161 fatalx("pipe closed");
163 if ((n = imsg_get(ibuf, &imsg)) == -1)
164 fatal("imsg_get");
165 if (n == 0) /* no more messages */
166 fatalx("expected at least a message");
168 ret = imsg.hdr.type;
169 if (ret == IMSG_STOP)
170 got_stop = 1;
171 switch (imsg.hdr.type) {
172 case IMSG_PLAY:
173 player_enqueue(&imsg);
174 ret = IMSG_STOP;
175 break;
176 case IMSG_RESUME:
177 case IMSG_PAUSE:
178 case IMSG_STOP:
179 break;
180 default:
181 fatalx("unknown imsg %d", imsg.hdr.type);
184 return ret;
187 void
188 player_senderr(void)
190 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
191 imsg_flush(ibuf);
194 void
195 player_sendeof(void)
197 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
198 imsg_flush(ibuf);
201 int
202 player_playnext(void)
204 int fd = nextfd;
206 assert(nextfd != -1);
207 nextfd = -1;
209 /* XXX: use magic(5) for this, not file extensions */
210 if (strstr(nextpath, ".ogg") != NULL)
211 play_oggvorbis(fd);
212 else if (strstr(nextpath, ".mp3") != NULL)
213 play_mp3(fd);
214 else if (strstr(nextpath, ".flac") != NULL)
215 play_flac(fd);
216 else if (strstr(nextpath, ".opus") != NULL)
217 play_opus(fd);
218 else {
219 log_warnx("unknown file type for %s", nextpath);
220 player_senderr();
221 return 0;
224 return 1;
227 int
228 player_pause(void)
230 int r;
232 r = player_dispatch();
233 return r == IMSG_RESUME;
236 int
237 player_shouldstop(void)
239 if (!player_pendingimsg())
240 return 0;
242 switch (player_dispatch()) {
243 case IMSG_PAUSE:
244 if (player_pause())
245 break;
246 /* fallthrough */
247 case IMSG_STOP:
248 return 1;
251 return 0;
254 int
255 play(const void *buf, size_t len)
257 if (player_shouldstop())
258 return 0;
259 sio_write(hdl, buf, len);
260 return 1;
263 int
264 player(int debug, int verbose)
266 int flags;
267 log_init(debug, LOG_DAEMON);
268 log_setverbose(verbose);
270 setproctitle("player");
271 log_procinit("player");
273 #if 0
275 static int attached;
277 while (!attached)
278 sleep(1);
280 #endif
282 audio_init();
284 /* mark fd as blocking i/o mode */
285 if ((flags = fcntl(3, F_GETFL)) == -1)
286 fatal("fcntl(F_GETFL)");
287 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
288 fatal("fcntl F_SETFL O_NONBLOCK");
290 ibuf = xmalloc(sizeof(*ibuf));
291 imsg_init(ibuf, 3);
293 signal(SIGINT, player_signal_handler);
294 signal(SIGTERM, player_signal_handler);
296 signal(SIGHUP, SIG_IGN);
297 signal(SIGPIPE, SIG_IGN);
299 if (pledge("stdio recvfd", NULL) == -1)
300 fatal("pledge");
302 while (!halted) {
303 while (nextfd == -1)
304 player_dispatch();
306 if (player_playnext() && !got_stop)
307 player_sendeof();
308 else
309 got_stop = 0;
312 return 0;