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 got_stop;
44 static int nextfd = -1;
45 static char nextpath[PATH_MAX];
47 volatile sig_atomic_t halted;
49 static void
50 player_signal_handler(int signo)
51 {
52 halted = 1;
53 }
55 static void
56 audio_init(void)
57 {
58 struct sio_par par;
60 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
61 fatal("sio_open");
63 sio_initpar(&par);
64 par.bits = 16;
65 par.appbufsz = 50 * par.rate / 1000;
66 par.pchan = 2;
67 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
68 fatal("couldn't set audio params");
69 if (par.bits != 16 || par.le != SIO_LE_NATIVE)
70 fatalx("unsupported audio params");
71 if (!sio_start(hdl))
72 fatal("sio_start");
73 }
75 int
76 player_setrate(int rate)
77 {
78 struct sio_par par;
80 log_debug("switching to sample rate %d", rate);
82 sio_stop(hdl);
84 sio_initpar(&par);
85 par.rate = rate;
86 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
87 log_warnx("invalid params");
88 return -1;
89 }
91 /* TODO: check the sample rate? */
93 if (!sio_start(hdl)) {
94 log_warn("sio_start");
95 return -1;
96 }
97 return 0;
98 }
100 int
101 player_pendingimsg(void)
103 struct pollfd pfd;
104 int r;
106 if (halted != 0)
107 return 1;
109 pfd.fd = ibuf->fd;
110 pfd.events = POLLIN;
112 r = poll(&pfd, 1, 0);
113 if (r == -1)
114 fatal("poll");
115 return r;
118 void
119 player_enqueue(struct imsg *imsg)
121 size_t datalen;
123 if (nextfd != -1)
124 fatalx("track already enqueued");
126 datalen = IMSG_DATA_SIZE(*imsg);
127 if (datalen != sizeof(nextpath))
128 fatalx("%s: size mismatch", __func__);
129 memcpy(nextpath, imsg->data, sizeof(nextpath));
130 if (nextpath[datalen-1] != '\0')
131 fatalx("%s: corrupted path", __func__);
132 if ((nextfd = imsg->fd) == -1)
133 fatalx("%s: got invalid file descriptor", __func__);
134 log_debug("enqueued %s", nextpath);
137 /* process only one message */
138 int
139 player_dispatch(void)
141 struct imsg imsg;
142 ssize_t n;
143 int ret;
145 if (halted != 0)
146 return IMSG_STOP;
148 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
149 fatalx("imsg_read");
150 if (n == 0)
151 fatalx("pipe closed");
153 if ((n = imsg_get(ibuf, &imsg)) == -1)
154 fatal("imsg_get");
155 if (n == 0) /* no more messages */
156 fatalx("expected at least a message");
158 ret = imsg.hdr.type;
159 if (ret == IMSG_STOP)
160 got_stop = 1;
161 switch (imsg.hdr.type) {
162 case IMSG_PLAY:
163 player_enqueue(&imsg);
164 ret = IMSG_STOP;
165 break;
166 case IMSG_RESUME:
167 case IMSG_PAUSE:
168 case IMSG_STOP:
169 break;
170 default:
171 fatalx("unknown imsg %d", imsg.hdr.type);
174 return ret;
177 void
178 player_senderr(void)
180 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
181 imsg_flush(ibuf);
184 void
185 player_sendeof(void)
187 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
188 imsg_flush(ibuf);
191 void
192 player_playnext(void)
194 int fd = nextfd;
196 assert(nextfd != -1);
197 nextfd = -1;
199 /* XXX: use magic(5) for this, not file extensions */
200 if (strstr(nextpath, ".ogg") != NULL)
201 play_oggvorbis(fd);
202 else if (strstr(nextpath, ".mp3") != NULL)
203 play_mp3(fd);
204 else if (strstr(nextpath, ".flac") != NULL)
205 play_flac(fd);
206 else if (strstr(nextpath, ".opus") != NULL)
207 play_opus(fd);
208 else {
209 log_warnx("unknown file type for %s", nextpath);
210 player_senderr();
211 return;
215 int
216 player_pause(void)
218 int r;
220 r = player_dispatch();
221 return r == IMSG_RESUME;
224 int
225 player_shouldstop(void)
227 if (!player_pendingimsg())
228 return 0;
230 switch (player_dispatch()) {
231 case IMSG_PAUSE:
232 if (player_pause())
233 break;
234 /* fallthrough */
235 case IMSG_STOP:
236 return 1;
239 return 0;
242 int
243 player(int debug, int verbose)
245 int flags;
246 log_init(debug, LOG_DAEMON);
247 log_setverbose(verbose);
249 setproctitle("player");
250 log_procinit("player");
252 #if 0
254 static int attached;
256 while (!attached)
257 sleep(1);
259 #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 player_dispatch();
285 player_playnext();
287 if (!got_stop)
288 player_sendeof();
289 else
290 got_stop = 0;
293 return 0;