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_setrate(int rate)
78 {
79 struct sio_par par;
81 log_debug("switching to sample rate %d", rate);
83 sio_stop(hdl);
85 sio_initpar(&par);
86 par.rate = rate;
87 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
88 log_warnx("invalid params");
89 return -1;
90 }
92 /* TODO: check the sample rate? */
94 if (!sio_start(hdl)) {
95 log_warn("sio_start");
96 return -1;
97 }
98 return 0;
99 }
101 int
102 player_pendingimsg(void)
104 struct pollfd pfd;
105 int r;
107 if (halted != 0)
108 return 1;
110 pfd.fd = ibuf->fd;
111 pfd.events = POLLIN;
113 r = poll(&pfd, 1, 0);
114 if (r == -1)
115 fatal("poll");
116 return r;
119 void
120 player_enqueue(struct imsg *imsg)
122 size_t datalen;
124 if (nextfd != -1)
125 fatalx("track already enqueued");
127 datalen = IMSG_DATA_SIZE(*imsg);
128 if (datalen != sizeof(nextpath))
129 fatalx("%s: size mismatch", __func__);
130 memcpy(nextpath, imsg->data, sizeof(nextpath));
131 if (nextpath[datalen-1] != '\0')
132 fatalx("%s: corrupted path", __func__);
133 if ((nextfd = imsg->fd) == -1)
134 fatalx("%s: got invalid file descriptor", __func__);
135 log_debug("enqueued %s", nextpath);
138 /* process only one message */
139 int
140 player_dispatch(void)
142 struct imsg imsg;
143 ssize_t n;
144 int ret;
146 if (halted != 0)
147 return IMSG_STOP;
149 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
150 fatalx("imsg_read");
151 if (n == 0)
152 fatalx("pipe closed");
154 if ((n = imsg_get(ibuf, &imsg)) == -1)
155 fatal("imsg_get");
156 if (n == 0) /* no more messages */
157 fatalx("expected at least a message");
159 ret = imsg.hdr.type;
160 if (ret == IMSG_STOP)
161 got_stop = 1;
162 switch (imsg.hdr.type) {
163 case IMSG_PLAY:
164 player_enqueue(&imsg);
165 ret = IMSG_STOP;
166 break;
167 case IMSG_RESUME:
168 case IMSG_PAUSE:
169 case IMSG_STOP:
170 break;
171 default:
172 fatalx("unknown imsg %d", imsg.hdr.type);
175 return ret;
178 void
179 player_senderr(void)
181 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
182 imsg_flush(ibuf);
185 void
186 player_sendeof(void)
188 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
189 imsg_flush(ibuf);
192 void
193 player_playnext(void)
195 int fd = nextfd;
197 assert(nextfd != -1);
198 nextfd = -1;
200 /* XXX: use magic(5) for this, not file extensions */
201 if (strstr(nextpath, ".ogg") != NULL)
202 play_oggvorbis(fd);
203 else if (strstr(nextpath, ".mp3") != NULL)
204 play_mp3(fd);
205 else if (strstr(nextpath, ".flac") != NULL)
206 play_flac(fd);
207 else if (strstr(nextpath, ".opus") != NULL)
208 play_opus(fd);
209 else {
210 log_warnx("unknown file type for %s", nextpath);
211 player_senderr();
212 return;
216 int
217 player_pause(void)
219 int r;
221 r = player_dispatch();
222 return r == IMSG_RESUME;
225 int
226 player_shouldstop(void)
228 if (!player_pendingimsg())
229 return 0;
231 switch (player_dispatch()) {
232 case IMSG_PAUSE:
233 if (player_pause())
234 break;
235 /* fallthrough */
236 case IMSG_STOP:
237 return 1;
240 return 0;
243 int
244 player(int debug, int verbose)
246 int flags;
247 log_init(debug, LOG_DAEMON);
248 log_setverbose(verbose);
250 setproctitle("player");
251 log_procinit("player");
253 #if 0
255 static int attached;
257 while (!attached)
258 sleep(1);
260 #endif
262 audio_init();
264 /* mark fd as blocking i/o mode */
265 if ((flags = fcntl(3, F_GETFL)) == -1)
266 fatal("fcntl(F_GETFL)");
267 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
268 fatal("fcntl F_SETFL O_NONBLOCK");
270 ibuf = xmalloc(sizeof(*ibuf));
271 imsg_init(ibuf, 3);
273 signal(SIGINT, player_signal_handler);
274 signal(SIGTERM, player_signal_handler);
276 signal(SIGHUP, SIG_IGN);
277 signal(SIGPIPE, SIG_IGN);
279 if (pledge("stdio recvfd", NULL) == -1)
280 fatal("pledge");
282 while (!halted) {
283 while (nextfd == -1)
284 player_dispatch();
286 player_playnext();
288 if (!got_stop)
289 player_sendeof();
290 else
291 got_stop = 0;
294 return 0;