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 rate, int channels)
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 par.pchan = channels;
88 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
89 log_warnx("invalid params");
90 return -1;
91 }
93 if (par.pchan != channels) {
94 log_warnx("failed to set params");
95 return -1;
96 }
98 /* TODO: check the sample rate? */
100 if (!sio_start(hdl)) {
101 log_warn("sio_start");
102 return -1;
104 return 0;
107 int
108 player_pendingimsg(void)
110 struct pollfd pfd;
111 int r;
113 if (halted != 0)
114 return 1;
116 pfd.fd = ibuf->fd;
117 pfd.events = POLLIN;
119 r = poll(&pfd, 1, 0);
120 if (r == -1)
121 fatal("poll");
122 return r;
125 void
126 player_enqueue(struct imsg *imsg)
128 size_t datalen;
130 if (nextfd != -1)
131 fatalx("track already enqueued");
133 datalen = IMSG_DATA_SIZE(*imsg);
134 if (datalen != sizeof(nextpath))
135 fatalx("%s: size mismatch", __func__);
136 memcpy(nextpath, imsg->data, sizeof(nextpath));
137 if (nextpath[datalen-1] != '\0')
138 fatalx("%s: corrupted path", __func__);
139 if ((nextfd = imsg->fd) == -1)
140 fatalx("%s: got invalid file descriptor", __func__);
141 log_debug("enqueued %s", nextpath);
144 /* process only one message */
145 int
146 player_dispatch(void)
148 struct imsg imsg;
149 ssize_t n;
150 int ret;
152 if (halted != 0)
153 return IMSG_STOP;
155 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
156 fatalx("imsg_read");
157 if (n == 0)
158 fatalx("pipe closed");
160 if ((n = imsg_get(ibuf, &imsg)) == -1)
161 fatal("imsg_get");
162 if (n == 0) /* no more messages */
163 fatalx("expected at least a message");
165 ret = imsg.hdr.type;
166 if (ret == IMSG_STOP)
167 got_stop = 1;
168 switch (imsg.hdr.type) {
169 case IMSG_PLAY:
170 player_enqueue(&imsg);
171 ret = IMSG_STOP;
172 break;
173 case IMSG_RESUME:
174 case IMSG_PAUSE:
175 case IMSG_STOP:
176 break;
177 default:
178 fatalx("unknown imsg %d", imsg.hdr.type);
181 return ret;
184 void
185 player_senderr(void)
187 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
188 imsg_flush(ibuf);
191 void
192 player_sendeof(void)
194 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
195 imsg_flush(ibuf);
198 void
199 player_playnext(void)
201 int fd = nextfd;
203 assert(nextfd != -1);
204 nextfd = -1;
206 /* XXX: use magic(5) for this, not file extensions */
207 if (strstr(nextpath, ".ogg") != NULL)
208 play_oggvorbis(fd);
209 else if (strstr(nextpath, ".mp3") != NULL)
210 play_mp3(fd);
211 else if (strstr(nextpath, ".flac") != NULL)
212 play_flac(fd);
213 else if (strstr(nextpath, ".opus") != NULL)
214 play_opus(fd);
215 else {
216 log_warnx("unknown file type for %s", nextpath);
217 player_senderr();
218 return;
222 int
223 player_pause(void)
225 int r;
227 r = player_dispatch();
228 return r == IMSG_RESUME;
231 int
232 player_shouldstop(void)
234 if (!player_pendingimsg())
235 return 0;
237 switch (player_dispatch()) {
238 case IMSG_PAUSE:
239 if (player_pause())
240 break;
241 /* fallthrough */
242 case IMSG_STOP:
243 return 1;
246 return 0;
249 int
250 player(int debug, int verbose)
252 int flags;
253 log_init(debug, LOG_DAEMON);
254 log_setverbose(verbose);
256 setproctitle("player");
257 log_procinit("player");
259 #if 0
261 static int attached;
263 while (!attached)
264 sleep(1);
266 #endif
268 audio_init();
270 /* mark fd as blocking i/o mode */
271 if ((flags = fcntl(3, F_GETFL)) == -1)
272 fatal("fcntl(F_GETFL)");
273 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
274 fatal("fcntl F_SETFL O_NONBLOCK");
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", NULL) == -1)
286 fatal("pledge");
288 while (!halted) {
289 while (nextfd == -1)
290 player_dispatch();
292 player_playnext();
294 if (!got_stop)
295 player_sendeof();
296 else
297 got_stop = 0;
300 return 0;