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("switching to sample rate %d", rate);
83 sio_stop(hdl);
85 sio_initpar(&par);
86 par.bits = bits;
87 par.rate = rate;
88 par.pchan = channels;
89 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
90 log_warnx("invalid params");
91 return -1;
92 }
94 if (par.bits != bits || par.pchan != channels) {
95 log_warnx("failed to set params");
96 return -1;
97 }
99 /* TODO: check the sample rate? */
101 if (!sio_start(hdl)) {
102 log_warn("sio_start");
103 return -1;
105 return 0;
108 int
109 player_pendingimsg(void)
111 struct pollfd pfd;
112 int r;
114 if (halted != 0)
115 return 1;
117 pfd.fd = ibuf->fd;
118 pfd.events = POLLIN;
120 r = poll(&pfd, 1, 0);
121 if (r == -1)
122 fatal("poll");
123 return r;
126 void
127 player_enqueue(struct imsg *imsg)
129 size_t datalen;
131 if (nextfd != -1)
132 fatalx("track already enqueued");
134 datalen = IMSG_DATA_SIZE(*imsg);
135 if (datalen != sizeof(nextpath))
136 fatalx("%s: size mismatch", __func__);
137 memcpy(nextpath, imsg->data, sizeof(nextpath));
138 if (nextpath[datalen-1] != '\0')
139 fatalx("%s: corrupted path", __func__);
140 if ((nextfd = imsg->fd) == -1)
141 fatalx("%s: got invalid file descriptor", __func__);
142 log_debug("enqueued %s", nextpath);
145 /* process only one message */
146 int
147 player_dispatch(void)
149 struct imsg imsg;
150 ssize_t n;
151 int ret;
153 if (halted != 0)
154 return IMSG_STOP;
156 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
157 fatalx("imsg_read");
158 if (n == 0)
159 fatalx("pipe closed");
161 if ((n = imsg_get(ibuf, &imsg)) == -1)
162 fatal("imsg_get");
163 if (n == 0) /* no more messages */
164 fatalx("expected at least a message");
166 ret = imsg.hdr.type;
167 if (ret == IMSG_STOP)
168 got_stop = 1;
169 switch (imsg.hdr.type) {
170 case IMSG_PLAY:
171 player_enqueue(&imsg);
172 ret = IMSG_STOP;
173 break;
174 case IMSG_RESUME:
175 case IMSG_PAUSE:
176 case IMSG_STOP:
177 break;
178 default:
179 fatalx("unknown imsg %d", imsg.hdr.type);
182 return ret;
185 void
186 player_senderr(void)
188 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
189 imsg_flush(ibuf);
192 void
193 player_sendeof(void)
195 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
196 imsg_flush(ibuf);
199 void
200 player_playnext(void)
202 int fd = nextfd;
204 assert(nextfd != -1);
205 nextfd = -1;
207 /* XXX: use magic(5) for this, not file extensions */
208 if (strstr(nextpath, ".ogg") != NULL)
209 play_oggvorbis(fd);
210 else if (strstr(nextpath, ".mp3") != NULL)
211 play_mp3(fd);
212 else if (strstr(nextpath, ".flac") != NULL)
213 play_flac(fd);
214 else if (strstr(nextpath, ".opus") != NULL)
215 play_opus(fd);
216 else {
217 log_warnx("unknown file type for %s", nextpath);
218 player_senderr();
219 return;
223 int
224 player_pause(void)
226 int r;
228 r = player_dispatch();
229 return r == IMSG_RESUME;
232 int
233 player_shouldstop(void)
235 if (!player_pendingimsg())
236 return 0;
238 switch (player_dispatch()) {
239 case IMSG_PAUSE:
240 if (player_pause())
241 break;
242 /* fallthrough */
243 case IMSG_STOP:
244 return 1;
247 return 0;
250 int
251 player(int debug, int verbose)
253 int flags;
254 log_init(debug, LOG_DAEMON);
255 log_setverbose(verbose);
257 setproctitle("player");
258 log_procinit("player");
260 #if 0
262 static int attached;
264 while (!attached)
265 sleep(1);
267 #endif
269 audio_init();
271 /* mark fd as blocking i/o mode */
272 if ((flags = fcntl(3, F_GETFL)) == -1)
273 fatal("fcntl(F_GETFL)");
274 if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
275 fatal("fcntl F_SETFL O_NONBLOCK");
277 ibuf = xmalloc(sizeof(*ibuf));
278 imsg_init(ibuf, 3);
280 signal(SIGINT, player_signal_handler);
281 signal(SIGTERM, player_signal_handler);
283 signal(SIGHUP, SIG_IGN);
284 signal(SIGPIPE, SIG_IGN);
286 if (pledge("stdio recvfd", NULL) == -1)
287 fatal("pledge");
289 while (!halted) {
290 while (nextfd == -1)
291 player_dispatch();
293 player_playnext();
295 if (!got_stop)
296 player_sendeof();
297 else
298 got_stop = 0;
301 return 0;