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 /* process only one message */
117 int
118 player_dispatch(void)
120 struct pollfd pfd;
121 struct imsg imsg;
122 ssize_t n;
123 int ret;
125 if (halted != 0)
126 return IMSG_STOP;
128 again:
129 if ((n = imsg_get(ibuf, &imsg)) == -1)
130 fatal("imsg_get");
131 if (n == 0) {
132 pfd.fd = ibuf->fd;
133 pfd.events = POLLIN;
134 if (poll(&pfd, 1, INFTIM) == -1)
135 fatal("poll");
136 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
137 fatal("imsg_read");
138 if (n == 0)
139 fatalx("pipe closed");
140 goto again;
143 ret = imsg.hdr.type;
144 switch (imsg.hdr.type) {
145 case IMSG_PLAY:
146 if (nextfd != -1)
147 fatalx("track already enqueued");
148 if ((nextfd = imsg.fd) == -1)
149 fatalx("%s: got invalid file descriptor", __func__);
150 log_debug("song enqueued");
151 ret = IMSG_STOP;
152 break;
153 case IMSG_RESUME:
154 case IMSG_PAUSE:
155 case IMSG_STOP:
156 break;
157 default:
158 fatalx("unknown imsg %d", imsg.hdr.type);
161 imsg_free(&imsg);
162 return ret;
165 void
166 player_senderr(void)
168 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
169 imsg_flush(ibuf);
172 void
173 player_sendeof(void)
175 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
176 imsg_flush(ibuf);
179 int
180 player_playnext(void)
182 static char buf[512];
183 ssize_t r;
184 int fd = nextfd;
186 assert(nextfd != -1);
187 nextfd = -1;
189 r = read(fd, buf, sizeof(buf));
191 /* 8 byte is the larger magic number */
192 if (r < 8) {
193 log_warn("read failed");
194 goto err;
197 if (lseek(fd, 0, SEEK_SET) == -1) {
198 log_warn("lseek failed");
199 goto err;
202 if (memcmp(buf, "fLaC", 4) == 0)
203 return play_flac(fd);
204 if (memcmp(buf, "ID3", 3) == 0 ||
205 memcmp(buf, "\xFF\xFB", 2) == 0)
206 return play_mp3(fd);
207 if (memmem(buf, r, "OpusHead", 8) != NULL)
208 return play_opus(fd);
209 if (memmem(buf, r, "OggS", 4) != NULL)
210 return play_oggvorbis(fd);
212 log_warnx("unknown file type");
213 err:
214 close(fd);
215 return -1;
218 int
219 player_pause(void)
221 int r;
223 r = player_dispatch();
224 return r == IMSG_RESUME;
227 int
228 player_shouldstop(void)
230 if (!player_pendingimsg())
231 return 0;
233 switch (player_dispatch()) {
234 case IMSG_PAUSE:
235 if (player_pause())
236 break;
237 /* fallthrough */
238 case IMSG_STOP:
239 return 1;
242 return 0;
245 int
246 play(const void *buf, size_t len)
248 if (player_shouldstop())
249 return 0;
250 sio_write(hdl, buf, len);
251 return 1;
254 int
255 player(int debug, int verbose)
257 int r;
259 log_init(debug, LOG_DAEMON);
260 log_setverbose(verbose);
262 setproctitle("player");
263 log_procinit("player");
265 #if 0
267 static int attached;
269 while (!attached)
270 sleep(1);
272 #endif
274 player_init();
276 ibuf = xmalloc(sizeof(*ibuf));
277 imsg_init(ibuf, 3);
279 signal(SIGINT, player_signal_handler);
280 signal(SIGTERM, player_signal_handler);
282 signal(SIGHUP, SIG_IGN);
283 signal(SIGPIPE, SIG_IGN);
285 if (pledge("stdio recvfd audio", NULL) == -1)
286 fatal("pledge");
288 while (!halted) {
289 while (nextfd == -1)
290 player_dispatch();
292 r = player_playnext();
293 if (r == -1)
294 player_senderr();
295 if (r == 0)
296 player_sendeof();
299 return 0;