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 <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 static struct imsgbuf *ibuf;
43 static int nextfd = -1;
44 static char nextpath[PATH_MAX];
46 volatile sig_atomic_t halted;
48 static void
49 player_signal_handler(int signo)
50 {
51 halted = 1;
52 }
54 static void
55 audio_init(void)
56 {
57 struct sio_par par;
59 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
60 fatal("sio_open");
62 sio_initpar(&par);
63 par.bits = 16;
64 par.appbufsz = 50 * par.rate / 1000;
65 par.pchan = 2;
66 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
67 fatal("couldn't set audio params");
68 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
69 fatalx("unsupported audio params");
70 if (!sio_start(hdl))
71 fatal("sio_start");
72 }
74 int
75 player_setrate(int rate)
76 {
77 struct sio_par par;
79 log_debug("switching to sample rate %d", rate);
81 sio_stop(hdl);
83 sio_initpar(&par);
84 par.rate = rate;
85 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
86 log_warnx("invalid params");
87 return -1;
88 }
90 /* TODO: check the sample rate? */
92 if (!sio_start(hdl)) {
93 log_warn("sio_start");
94 return -1;
95 }
96 return 0;
97 }
99 int
100 player_pendingimsg(void)
102 struct pollfd pfd;
103 int r;
105 if (halted != 0)
106 return 1;
108 pfd.fd = ibuf->fd;
109 pfd.revents = POLLIN;
111 r = poll(&pfd, 1, 0);
112 if (r == -1)
113 fatal("poll");
114 return r;
117 void
118 player_enqueue(struct imsg *imsg)
120 size_t datalen;
122 if (nextfd != -1)
123 fatalx("track already enqueued");
125 datalen = IMSG_DATA_SIZE(*imsg);
126 if (datalen != sizeof(nextpath))
127 fatalx("%s: size mismatch", __func__);
128 memcpy(nextpath, imsg->data, sizeof(nextpath));
129 if (nextpath[datalen-1] != '\0')
130 fatalx("%s: corrupted path", __func__);
131 if ((nextfd = imsg->fd) == -1)
132 fatalx("%s: got invalid file descriptor", __func__);
133 log_debug("enqueued %s", nextpath);
136 /* process only one message */
137 int
138 player_dispatch(void)
140 struct imsg imsg;
141 ssize_t n;
142 int ret;
144 if (halted != 0)
145 return IMSG_STOP;
147 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
148 fatalx("imsg_read");
149 if (n == 0)
150 fatalx("pipe closed");
152 if ((n = imsg_get(ibuf, &imsg)) == -1)
153 fatal("imsg_get");
154 if (n == 0) /* no more messages */
155 fatalx("expected at least a message");
157 ret = imsg.hdr.type;
158 switch (imsg.hdr.type) {
159 case IMSG_PLAY:
160 player_enqueue(&imsg);
161 ret = IMSG_STOP;
162 break;
163 case IMSG_RESUME:
164 case IMSG_PAUSE:
165 case IMSG_STOP:
166 break;
167 default:
168 fatalx("unknown imsg %d", imsg.hdr.type);
171 return ret;
174 void
175 player_senderr(void)
177 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
178 imsg_flush(ibuf);
181 void
182 player_sendeof(void)
184 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
185 imsg_flush(ibuf);
188 void
189 player_playnext(void)
191 int fd = nextfd;
193 assert(nextfd != -1);
194 nextfd = -1;
196 /* XXX: use magic(5) for this, not file extensions */
197 if (strstr(nextpath, ".ogg") != NULL)
198 play_oggvorbis(fd);
199 else if (strstr(nextpath, ".mp3") != NULL)
200 play_mp3(fd);
201 else if (strstr(nextpath, ".flac") != NULL)
202 play_flac(fd);
203 else if (strstr(nextpath, ".opus") != NULL)
204 play_opus(fd);
205 else {
206 log_warnx("unknown file type for %s", nextpath);
207 player_senderr();
208 return;
211 player_sendeof();
214 int
215 player_pause(void)
217 int r;
219 r = player_dispatch();
220 return r == IMSG_RESUME;
223 int
224 player_shouldstop(void)
226 if (!player_pendingimsg())
227 return 0;
229 switch (player_dispatch()) {
230 case IMSG_PAUSE:
231 if (player_pause())
232 break;
233 /* fallthrough */
234 case IMSG_STOP:
235 return 1;
238 return 0;
241 int
242 player(int debug, int verbose)
244 int flags;
245 log_init(debug, LOG_DAEMON);
246 log_setverbose(verbose);
248 setproctitle("player");
249 log_procinit("player");
251 #if 0
253 static int attached;
255 while (!attached)
256 sleep(1);
258 #endif
261 audio_init();
263 /* mark fd as blocking i/o mode */
264 if ((flags = fcntl(3, F_GETFL)) == -1)
265 fatal("fcntl(F_GETFL)");
266 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
267 fatal("fcntl F_SETFL O_NONBLOCK");
269 ibuf = xmalloc(sizeof(*ibuf));
270 imsg_init(ibuf, 3);
272 signal(SIGINT, player_signal_handler);
273 signal(SIGTERM, player_signal_handler);
275 signal(SIGHUP, SIG_IGN);
276 signal(SIGPIPE, SIG_IGN);
278 if (pledge("stdio recvfd", NULL) == -1)
279 fatal("pledge");
281 while (!halted) {
282 while (nextfd == -1)
283 assert(player_dispatch() == IMSG_STOP);
285 player_playnext();
288 return 0;