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 static struct imsgbuf *ibuf;
44 static int got_stop;
45 static int nextfd = -1;
46 static char nextpath[PATH_MAX];
48 volatile sig_atomic_t halted;
50 static void
51 player_signal_handler(int signo)
52 {
53 halted = 1;
54 }
56 static void
57 audio_init(void)
58 {
59 struct sio_par par;
61 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
62 fatal("sio_open");
64 sio_initpar(&par);
65 par.bits = 16;
66 par.appbufsz = 50 * par.rate / 1000;
67 par.pchan = 2;
68 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
69 fatal("couldn't set audio params");
70 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
71 fatalx("unsupported audio params");
72 if (!sio_start(hdl))
73 fatal("sio_start");
74 }
76 int
77 player_setup(int bits, int rate, int channels)
78 {
79 struct sio_par par;
81 log_debug("%s: bits=%d, rate=%d, channels=%d", __func__,
82 bits, rate, channels);
84 sio_stop(hdl);
86 sio_initpar(&par);
87 par.bits = bits;
88 par.rate = rate;
89 par.pchan = channels;
90 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
91 log_warnx("invalid params");
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 size_t datalen;
132 if (nextfd != -1)
133 fatalx("track already enqueued");
135 datalen = IMSG_DATA_SIZE(*imsg);
136 if (datalen != sizeof(nextpath))
137 fatalx("%s: size mismatch", __func__);
138 memcpy(nextpath, imsg->data, sizeof(nextpath));
139 if (nextpath[datalen-1] != '\0')
140 fatalx("%s: corrupted path", __func__);
141 if ((nextfd = imsg->fd) == -1)
142 fatalx("%s: got invalid file descriptor", __func__);
143 log_debug("enqueued %s", nextpath);
146 /* process only one message */
147 int
148 player_dispatch(void)
150 struct imsg imsg;
151 ssize_t n;
152 int ret;
154 if (halted != 0)
155 return IMSG_STOP;
157 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
158 fatalx("imsg_read");
159 if (n == 0)
160 fatalx("pipe closed");
162 if ((n = imsg_get(ibuf, &imsg)) == -1)
163 fatal("imsg_get");
164 if (n == 0) /* no more messages */
165 fatalx("expected at least a message");
167 ret = imsg.hdr.type;
168 if (ret == IMSG_STOP)
169 got_stop = 1;
170 switch (imsg.hdr.type) {
171 case IMSG_PLAY:
172 player_enqueue(&imsg);
173 ret = IMSG_STOP;
174 break;
175 case IMSG_RESUME:
176 case IMSG_PAUSE:
177 case IMSG_STOP:
178 break;
179 default:
180 fatalx("unknown imsg %d", imsg.hdr.type);
183 return ret;
186 void
187 player_senderr(void)
189 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
190 imsg_flush(ibuf);
193 void
194 player_sendeof(void)
196 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
197 imsg_flush(ibuf);
200 int
201 player_playnext(void)
203 int fd = nextfd;
205 assert(nextfd != -1);
206 nextfd = -1;
208 /* XXX: use magic(5) for this, not file extensions */
209 if (strstr(nextpath, ".ogg") != NULL)
210 play_oggvorbis(fd);
211 else if (strstr(nextpath, ".mp3") != NULL)
212 play_mp3(fd);
213 else if (strstr(nextpath, ".flac") != NULL)
214 play_flac(fd);
215 else if (strstr(nextpath, ".opus") != NULL)
216 play_opus(fd);
217 else {
218 log_warnx("unknown file type for %s", nextpath);
219 player_senderr();
220 return 0;
223 return 1;
226 int
227 player_pause(void)
229 int r;
231 r = player_dispatch();
232 return r == IMSG_RESUME;
235 int
236 player_shouldstop(void)
238 if (!player_pendingimsg())
239 return 0;
241 switch (player_dispatch()) {
242 case IMSG_PAUSE:
243 if (player_pause())
244 break;
245 /* fallthrough */
246 case IMSG_STOP:
247 return 1;
250 return 0;
253 int
254 play(const void *buf, size_t len)
256 if (player_shouldstop())
257 return 0;
258 sio_write(hdl, buf, len);
259 return 1;
262 int
263 player(int debug, int verbose)
265 int flags;
266 log_init(debug, LOG_DAEMON);
267 log_setverbose(verbose);
269 setproctitle("player");
270 log_procinit("player");
272 #if 0
274 static int attached;
276 while (!attached)
277 sleep(1);
279 #endif
281 audio_init();
283 /* mark fd as blocking i/o mode */
284 if ((flags = fcntl(3, F_GETFL)) == -1)
285 fatal("fcntl(F_GETFL)");
286 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
287 fatal("fcntl F_SETFL O_NONBLOCK");
289 ibuf = xmalloc(sizeof(*ibuf));
290 imsg_init(ibuf, 3);
292 signal(SIGINT, player_signal_handler);
293 signal(SIGTERM, player_signal_handler);
295 signal(SIGHUP, SIG_IGN);
296 signal(SIGPIPE, SIG_IGN);
298 if (pledge("stdio recvfd", NULL) == -1)
299 fatal("pledge");
301 while (!halted) {
302 while (nextfd == -1)
303 player_dispatch();
305 if (player_playnext() && !got_stop)
306 player_sendeof();
307 else
308 got_stop = 0;
311 return 0;