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 return ret;
164 void
165 player_senderr(void)
167 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
168 imsg_flush(ibuf);
171 void
172 player_sendeof(void)
174 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
175 imsg_flush(ibuf);
178 int
179 player_playnext(void)
181 static char buf[512];
182 ssize_t r;
183 int fd = nextfd;
185 assert(nextfd != -1);
186 nextfd = -1;
188 r = read(fd, buf, sizeof(buf));
190 /* 8 byte is the larger magic number */
191 if (r < 8) {
192 log_warn("read failed");
193 goto err;
196 if (lseek(fd, 0, SEEK_SET) == -1) {
197 log_warn("lseek failed");
198 goto err;
201 if (memcmp(buf, "fLaC", 4) == 0)
202 return play_flac(fd);
203 if (memcmp(buf, "ID3", 3) == 0 ||
204 memcmp(buf, "\xFF\xFB", 2) == 0)
205 return play_mp3(fd);
206 if (memmem(buf, r, "OpusHead", 8) != NULL)
207 return play_opus(fd);
208 if (memmem(buf, r, "OggS", 4) != NULL)
209 return play_oggvorbis(fd);
211 log_warnx("unknown file type");
212 err:
213 close(fd);
214 return -1;
217 int
218 player_pause(void)
220 int r;
222 r = player_dispatch();
223 return r == IMSG_RESUME;
226 int
227 player_shouldstop(void)
229 if (!player_pendingimsg())
230 return 0;
232 switch (player_dispatch()) {
233 case IMSG_PAUSE:
234 if (player_pause())
235 break;
236 /* fallthrough */
237 case IMSG_STOP:
238 return 1;
241 return 0;
244 int
245 play(const void *buf, size_t len)
247 if (player_shouldstop())
248 return 0;
249 sio_write(hdl, buf, len);
250 return 1;
253 int
254 player(int debug, int verbose)
256 int r;
258 log_init(debug, LOG_DAEMON);
259 log_setverbose(verbose);
261 setproctitle("player");
262 log_procinit("player");
264 #if 0
266 static int attached;
268 while (!attached)
269 sleep(1);
271 #endif
273 player_init();
275 ibuf = xmalloc(sizeof(*ibuf));
276 imsg_init(ibuf, 3);
278 signal(SIGINT, player_signal_handler);
279 signal(SIGTERM, player_signal_handler);
281 signal(SIGHUP, SIG_IGN);
282 signal(SIGPIPE, SIG_IGN);
284 if (pledge("stdio recvfd audio", NULL) == -1)
285 fatal("pledge");
287 while (!halted) {
288 while (nextfd == -1)
289 player_dispatch();
291 r = player_playnext();
292 if (r == -1)
293 player_senderr();
294 if (r == 0)
295 player_sendeof();
298 return 0;