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 void
49 player_signal_handler(int signo)
50 {
51 halted = 1;
52 }
54 void
55 player_init(void)
56 {
57 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
58 fatal("sio_open");
60 if (!sio_start(hdl))
61 fatal("sio_start");
62 }
64 int
65 player_setup(int bits, int rate, int channels)
66 {
67 struct sio_par par;
69 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
70 bits, rate, channels);
72 sio_stop(hdl);
74 sio_initpar(&par);
75 par.bits = bits;
76 par.rate = rate;
77 par.pchan = channels;
78 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
79 log_warnx("invalid params (bits=%d, rate=%d, channels=%d",
80 bits, rate, channels);
81 return -1;
82 }
84 if (par.bits != bits || par.pchan != channels) {
85 log_warnx("failed to set params");
86 return -1;
87 }
89 /* TODO: check the sample rate? */
91 if (!sio_start(hdl)) {
92 log_warn("sio_start");
93 return -1;
94 }
95 return 0;
96 }
98 int
99 player_pendingimsg(void)
101 struct pollfd pfd;
102 int r;
104 if (halted != 0)
105 return 1;
107 pfd.fd = ibuf->fd;
108 pfd.events = POLLIN;
110 r = poll(&pfd, 1, 0);
111 if (r == -1)
112 fatal("poll");
113 return r;
116 void
117 player_enqueue(struct imsg *imsg)
119 if (nextfd != -1)
120 fatalx("track already enqueued");
122 if ((nextfd = imsg->fd) == -1)
123 fatalx("%s: got invalid file descriptor", __func__);
124 log_debug("song enqueued");
127 /* process only one message */
128 int
129 player_dispatch(void)
131 struct pollfd pfd;
132 struct imsg imsg;
133 ssize_t n;
134 int ret;
136 if (halted != 0)
137 return IMSG_STOP;
139 again:
140 if ((n = imsg_get(ibuf, &imsg)) == -1)
141 fatal("imsg_get");
142 if (n == 0) {
143 pfd.fd = ibuf->fd;
144 pfd.events = POLLIN;
145 if (poll(&pfd, 1, INFTIM) == -1)
146 fatal("poll");
147 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
148 fatal("imsg_read");
149 if (n == 0)
150 fatalx("pipe closed");
151 goto again;
154 ret = imsg.hdr.type;
155 switch (imsg.hdr.type) {
156 case IMSG_PLAY:
157 player_enqueue(&imsg);
158 ret = IMSG_STOP;
159 break;
160 case IMSG_RESUME:
161 case IMSG_PAUSE:
162 case IMSG_STOP:
163 break;
164 default:
165 fatalx("unknown imsg %d", imsg.hdr.type);
168 return ret;
171 void
172 player_senderr(void)
174 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
175 imsg_flush(ibuf);
178 void
179 player_sendeof(void)
181 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
182 imsg_flush(ibuf);
185 int
186 player_playnext(void)
188 static char buf[512];
189 ssize_t r;
190 int fd = nextfd;
192 assert(nextfd != -1);
193 nextfd = -1;
195 r = read(fd, buf, sizeof(buf));
197 /* 8 byte is the larger magic number */
198 if (r < 8) {
199 log_warn("read failed");
200 goto err;
203 if (lseek(fd, 0, SEEK_SET) == -1) {
204 log_warn("lseek failed");
205 goto err;
208 if (memcmp(buf, "fLaC", 4) == 0)
209 return play_flac(fd);
210 if (memcmp(buf, "ID3", 3) == 0 ||
211 memcmp(buf, "\xFF\xFB", 2) == 0)
212 return play_mp3(fd);
213 if (memmem(buf, r, "OpusHead", 8) != NULL)
214 return play_opus(fd);
215 if (memmem(buf, r, "OggS", 4) != NULL)
216 return play_oggvorbis(fd);
218 log_warnx("unknown file type");
219 err:
220 close(fd);
221 return -1;
224 int
225 player_pause(void)
227 int r;
229 r = player_dispatch();
230 return r == IMSG_RESUME;
233 int
234 player_shouldstop(void)
236 if (!player_pendingimsg())
237 return 0;
239 switch (player_dispatch()) {
240 case IMSG_PAUSE:
241 if (player_pause())
242 break;
243 /* fallthrough */
244 case IMSG_STOP:
245 return 1;
248 return 0;
251 int
252 play(const void *buf, size_t len)
254 if (player_shouldstop())
255 return 0;
256 sio_write(hdl, buf, len);
257 return 1;
260 int
261 player(int debug, int verbose)
263 int r;
265 log_init(debug, LOG_DAEMON);
266 log_setverbose(verbose);
268 setproctitle("player");
269 log_procinit("player");
271 #if 0
273 static int attached;
275 while (!attached)
276 sleep(1);
278 #endif
280 player_init();
282 ibuf = xmalloc(sizeof(*ibuf));
283 imsg_init(ibuf, 3);
285 signal(SIGINT, player_signal_handler);
286 signal(SIGTERM, player_signal_handler);
288 signal(SIGHUP, SIG_IGN);
289 signal(SIGPIPE, SIG_IGN);
291 if (pledge("stdio recvfd audio", NULL) == -1)
292 fatal("pledge");
294 while (!halted) {
295 while (nextfd == -1)
296 player_dispatch();
298 r = player_playnext();
299 if (r == -1)
300 player_senderr();
301 if (r == 0)
302 player_sendeof();
305 return 0;