Blame


1 3baa2617 2022-02-16 op /*
2 578f8d0c 2023-05-02 op * Copyright (c) 2022, 2023 Omar Polo <op@omarpolo.com>
3 3baa2617 2022-02-16 op *
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
7 3baa2617 2022-02-16 op *
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 f36fd90a 2022-07-09 op #include "config.h"
18 3baa2617 2022-02-16 op
19 3baa2617 2022-02-16 op #include <limits.h>
20 3baa2617 2022-02-16 op
21 3baa2617 2022-02-16 op #include <assert.h>
22 3baa2617 2022-02-16 op #include <errno.h>
23 3baa2617 2022-02-16 op #include <poll.h>
24 3baa2617 2022-02-16 op #include <signal.h>
25 14be015f 2022-02-17 op #include <stdio.h>
26 3baa2617 2022-02-16 op #include <stdlib.h>
27 3baa2617 2022-02-16 op #include <stdint.h>
28 3baa2617 2022-02-16 op #include <string.h>
29 3baa2617 2022-02-16 op #include <syslog.h>
30 3baa2617 2022-02-16 op #include <unistd.h>
31 3baa2617 2022-02-16 op
32 3baa2617 2022-02-16 op #include "amused.h"
33 3baa2617 2022-02-16 op #include "log.h"
34 3baa2617 2022-02-16 op #include "xmalloc.h"
35 3baa2617 2022-02-16 op
36 c0180200 2022-06-10 op struct pollfd *player_pfds;
37 b02dadd3 2023-05-13 op int player_nfds;
38 5a4b3030 2022-03-09 op static struct imsgbuf *ibuf;
39 3baa2617 2022-02-16 op
40 3baa2617 2022-02-16 op static int nextfd = -1;
41 ff06024f 2022-07-08 op static int64_t samples;
42 60a09ce7 2022-07-09 op static int64_t duration;
43 06ceb376 2023-03-23 op static unsigned int current_rate;
44 3baa2617 2022-02-16 op
45 3baa2617 2022-02-16 op volatile sig_atomic_t halted;
46 3baa2617 2022-02-16 op
47 ae335651 2022-07-08 op static void
48 3baa2617 2022-02-16 op player_signal_handler(int signo)
49 3baa2617 2022-02-16 op {
50 3baa2617 2022-02-16 op halted = 1;
51 3baa2617 2022-02-16 op }
52 3baa2617 2022-02-16 op
53 3baa2617 2022-02-16 op int
54 b9d2a697 2022-07-08 op player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
55 3baa2617 2022-02-16 op {
56 069e653b 2023-03-23 op log_debug("%s: bits=%u, rate=%u, channels=%u", __func__,
57 a728254f 2022-02-23 op bits, rate, channels);
58 4d8a06d4 2022-06-10 op
59 06ceb376 2023-03-23 op current_rate = rate;
60 b02dadd3 2023-05-13 op return audio_setup(bits, rate, channels, player_pfds + 1, player_nfds);
61 ff06024f 2022-07-08 op }
62 ff06024f 2022-07-08 op
63 ff06024f 2022-07-08 op void
64 60a09ce7 2022-07-09 op player_setduration(int64_t d)
65 ff06024f 2022-07-08 op {
66 ff06024f 2022-07-08 op int64_t seconds;
67 ff06024f 2022-07-08 op
68 60a09ce7 2022-07-09 op duration = d;
69 06ceb376 2023-03-23 op seconds = duration / current_rate;
70 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
71 ff06024f 2022-07-08 op imsg_flush(ibuf);
72 ff06024f 2022-07-08 op }
73 ff06024f 2022-07-08 op
74 ae335651 2022-07-08 op static void
75 ff06024f 2022-07-08 op player_onmove(void *arg, int delta)
76 ff06024f 2022-07-08 op {
77 ff06024f 2022-07-08 op static int64_t reported;
78 ff06024f 2022-07-08 op int64_t sec;
79 ff06024f 2022-07-08 op
80 ff06024f 2022-07-08 op samples += delta;
81 06ceb376 2023-03-23 op if (llabs(samples - reported) >= current_rate) {
82 ff06024f 2022-07-08 op reported = samples;
83 06ceb376 2023-03-23 op sec = samples / current_rate;
84 ff06024f 2022-07-08 op
85 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
86 ff06024f 2022-07-08 op imsg_flush(ibuf);
87 ff06024f 2022-07-08 op }
88 3baa2617 2022-02-16 op }
89 3baa2617 2022-02-16 op
90 791d3db3 2022-07-09 op void
91 791d3db3 2022-07-09 op player_setpos(int64_t pos)
92 791d3db3 2022-07-09 op {
93 791d3db3 2022-07-09 op samples = pos;
94 791d3db3 2022-07-09 op player_onmove(NULL, 0);
95 791d3db3 2022-07-09 op }
96 791d3db3 2022-07-09 op
97 3baa2617 2022-02-16 op /* process only one message */
98 ae335651 2022-07-08 op static int
99 c2f554ff 2022-07-09 op player_dispatch(int64_t *s, int wait)
100 3baa2617 2022-02-16 op {
101 791d3db3 2022-07-09 op struct player_seek seek;
102 fc4a38af 2022-05-19 op struct pollfd pfd;
103 3baa2617 2022-02-16 op struct imsg imsg;
104 3baa2617 2022-02-16 op ssize_t n;
105 3baa2617 2022-02-16 op int ret;
106 3baa2617 2022-02-16 op
107 3baa2617 2022-02-16 op if (halted != 0)
108 3baa2617 2022-02-16 op return IMSG_STOP;
109 fc4a38af 2022-05-19 op
110 926ee836 2022-05-19 op again:
111 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
112 3baa2617 2022-02-16 op fatal("imsg_get");
113 926ee836 2022-05-19 op if (n == 0) {
114 c2f554ff 2022-07-09 op if (!wait)
115 c2f554ff 2022-07-09 op return -1;
116 c2f554ff 2022-07-09 op
117 926ee836 2022-05-19 op pfd.fd = ibuf->fd;
118 926ee836 2022-05-19 op pfd.events = POLLIN;
119 926ee836 2022-05-19 op if (poll(&pfd, 1, INFTIM) == -1)
120 926ee836 2022-05-19 op fatal("poll");
121 926ee836 2022-05-19 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
122 926ee836 2022-05-19 op fatal("imsg_read");
123 926ee836 2022-05-19 op if (n == 0)
124 926ee836 2022-05-19 op fatalx("pipe closed");
125 926ee836 2022-05-19 op goto again;
126 926ee836 2022-05-19 op }
127 3baa2617 2022-02-16 op
128 3baa2617 2022-02-16 op ret = imsg.hdr.type;
129 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
130 3baa2617 2022-02-16 op case IMSG_PLAY:
131 6e4f8947 2022-06-09 op if (nextfd != -1)
132 6e4f8947 2022-06-09 op fatalx("track already enqueued");
133 6e4f8947 2022-06-09 op if ((nextfd = imsg.fd) == -1)
134 6e4f8947 2022-06-09 op fatalx("%s: got invalid file descriptor", __func__);
135 6e4f8947 2022-06-09 op log_debug("song enqueued");
136 3baa2617 2022-02-16 op ret = IMSG_STOP;
137 3baa2617 2022-02-16 op break;
138 3baa2617 2022-02-16 op case IMSG_RESUME:
139 3baa2617 2022-02-16 op case IMSG_PAUSE:
140 3baa2617 2022-02-16 op case IMSG_STOP:
141 3baa2617 2022-02-16 op break;
142 791d3db3 2022-07-09 op case IMSG_CTL_SEEK:
143 791d3db3 2022-07-09 op if (s == NULL)
144 791d3db3 2022-07-09 op break;
145 791d3db3 2022-07-09 op if (IMSG_DATA_SIZE(imsg) != sizeof(seek))
146 791d3db3 2022-07-09 op fatalx("wrong size for seek ctl");
147 791d3db3 2022-07-09 op memcpy(&seek, imsg.data, sizeof(seek));
148 60a09ce7 2022-07-09 op if (seek.percent) {
149 60a09ce7 2022-07-09 op *s = (double)seek.offset * (double)duration / 100.0;
150 60a09ce7 2022-07-09 op } else {
151 06ceb376 2023-03-23 op *s = seek.offset * current_rate;
152 60a09ce7 2022-07-09 op if (seek.relative)
153 60a09ce7 2022-07-09 op *s += samples;
154 60a09ce7 2022-07-09 op }
155 791d3db3 2022-07-09 op if (*s < 0)
156 3aa75c64 2022-07-09 op *s = 0;
157 791d3db3 2022-07-09 op break;
158 3baa2617 2022-02-16 op default:
159 3baa2617 2022-02-16 op fatalx("unknown imsg %d", imsg.hdr.type);
160 3baa2617 2022-02-16 op }
161 3baa2617 2022-02-16 op
162 fd90976c 2022-06-09 op imsg_free(&imsg);
163 3baa2617 2022-02-16 op return ret;
164 3baa2617 2022-02-16 op }
165 3baa2617 2022-02-16 op
166 ae335651 2022-07-08 op static void
167 17ef54d6 2022-06-22 op player_senderr(const char *errstr)
168 3baa2617 2022-02-16 op {
169 17ef54d6 2022-06-22 op size_t len = 0;
170 17ef54d6 2022-06-22 op
171 17ef54d6 2022-06-22 op if (errstr != NULL)
172 17ef54d6 2022-06-22 op len = strlen(errstr) + 1;
173 17ef54d6 2022-06-22 op
174 17ef54d6 2022-06-22 op imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
175 3baa2617 2022-02-16 op imsg_flush(ibuf);
176 3baa2617 2022-02-16 op }
177 3baa2617 2022-02-16 op
178 ae335651 2022-07-08 op static void
179 3baa2617 2022-02-16 op player_sendeof(void)
180 3baa2617 2022-02-16 op {
181 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
182 3baa2617 2022-02-16 op imsg_flush(ibuf);
183 3baa2617 2022-02-16 op }
184 3baa2617 2022-02-16 op
185 ae335651 2022-07-08 op static int
186 17ef54d6 2022-06-22 op player_playnext(const char **errstr)
187 3baa2617 2022-02-16 op {
188 bf19b03e 2022-05-09 op static char buf[512];
189 bf19b03e 2022-05-09 op ssize_t r;
190 3baa2617 2022-02-16 op int fd = nextfd;
191 3baa2617 2022-02-16 op
192 3baa2617 2022-02-16 op assert(nextfd != -1);
193 3baa2617 2022-02-16 op nextfd = -1;
194 3baa2617 2022-02-16 op
195 ff06024f 2022-07-08 op /* reset samples and set position to zero */
196 ff06024f 2022-07-08 op samples = 0;
197 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
198 ff06024f 2022-07-08 op imsg_flush(ibuf);
199 ff06024f 2022-07-08 op
200 bf19b03e 2022-05-09 op r = read(fd, buf, sizeof(buf));
201 bf19b03e 2022-05-09 op
202 bf19b03e 2022-05-09 op /* 8 byte is the larger magic number */
203 bf19b03e 2022-05-09 op if (r < 8) {
204 17ef54d6 2022-06-22 op *errstr = "read failed";
205 bf19b03e 2022-05-09 op goto err;
206 bf19b03e 2022-05-09 op }
207 bf19b03e 2022-05-09 op
208 bf19b03e 2022-05-09 op if (lseek(fd, 0, SEEK_SET) == -1) {
209 17ef54d6 2022-06-22 op *errstr = "lseek failed";
210 bf19b03e 2022-05-09 op goto err;
211 bf19b03e 2022-05-09 op }
212 bf19b03e 2022-05-09 op
213 bf19b03e 2022-05-09 op if (memcmp(buf, "fLaC", 4) == 0)
214 17ef54d6 2022-06-22 op return play_flac(fd, errstr);
215 bf19b03e 2022-05-09 op if (memcmp(buf, "ID3", 3) == 0 ||
216 bf19b03e 2022-05-09 op memcmp(buf, "\xFF\xFB", 2) == 0)
217 17ef54d6 2022-06-22 op return play_mp3(fd, errstr);
218 bf19b03e 2022-05-09 op if (memmem(buf, r, "OpusHead", 8) != NULL)
219 17ef54d6 2022-06-22 op return play_opus(fd, errstr);
220 bf19b03e 2022-05-09 op if (memmem(buf, r, "OggS", 4) != NULL)
221 17ef54d6 2022-06-22 op return play_oggvorbis(fd, errstr);
222 25cb72fb 2022-02-22 op
223 17ef54d6 2022-06-22 op *errstr = "unknown file type";
224 bf19b03e 2022-05-09 op err:
225 184600a8 2022-05-09 op close(fd);
226 06412529 2022-05-09 op return -1;
227 3baa2617 2022-02-16 op }
228 3baa2617 2022-02-16 op
229 ae335651 2022-07-08 op static int
230 791d3db3 2022-07-09 op player_pause(int64_t *s)
231 3baa2617 2022-02-16 op {
232 3baa2617 2022-02-16 op int r;
233 3baa2617 2022-02-16 op
234 c2f554ff 2022-07-09 op r = player_dispatch(s, 1);
235 3ad8bae8 2022-07-09 op return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
236 3baa2617 2022-02-16 op }
237 3baa2617 2022-02-16 op
238 ae335651 2022-07-08 op static int
239 c2f554ff 2022-07-09 op player_shouldstop(int64_t *s, int wait)
240 3baa2617 2022-02-16 op {
241 c2f554ff 2022-07-09 op switch (player_dispatch(s, wait)) {
242 3baa2617 2022-02-16 op case IMSG_PAUSE:
243 791d3db3 2022-07-09 op if (player_pause(s))
244 3baa2617 2022-02-16 op break;
245 3baa2617 2022-02-16 op /* fallthrough */
246 3baa2617 2022-02-16 op case IMSG_STOP:
247 3baa2617 2022-02-16 op return 1;
248 3baa2617 2022-02-16 op }
249 3baa2617 2022-02-16 op
250 3baa2617 2022-02-16 op return 0;
251 3baa2617 2022-02-16 op }
252 3baa2617 2022-02-16 op
253 3baa2617 2022-02-16 op int
254 791d3db3 2022-07-09 op play(const void *buf, size_t len, int64_t *s)
255 2139c525 2022-03-09 op {
256 c0180200 2022-06-10 op size_t w;
257 b02dadd3 2023-05-13 op int revents, r, wait;
258 c0180200 2022-06-10 op
259 791d3db3 2022-07-09 op *s = -1;
260 c0180200 2022-06-10 op while (len != 0) {
261 b02dadd3 2023-05-13 op audio_pollfd(player_pfds + 1, player_nfds, POLLOUT);
262 b02dadd3 2023-05-13 op r = poll(player_pfds, player_nfds + 1, INFTIM);
263 c0180200 2022-06-10 op if (r == -1)
264 c0180200 2022-06-10 op fatal("poll");
265 c0180200 2022-06-10 op
266 c2f554ff 2022-07-09 op wait = player_pfds[0].revents & (POLLHUP|POLLIN);
267 c2f554ff 2022-07-09 op if (player_shouldstop(s, wait)) {
268 06ceb376 2023-03-23 op audio_flush();
269 c2f554ff 2022-07-09 op return 0;
270 c0180200 2022-06-10 op }
271 c0180200 2022-06-10 op
272 b02dadd3 2023-05-13 op revents = audio_revents(player_pfds + 1, player_nfds);
273 bc946090 2023-02-26 op if (revents & POLLHUP) {
274 bc946090 2023-02-26 op if (errno == EAGAIN)
275 bc946090 2023-02-26 op continue;
276 06ceb376 2023-03-23 op fatal("audio hang-up");
277 bc946090 2023-02-26 op }
278 c0180200 2022-06-10 op if (revents & POLLOUT) {
279 06ceb376 2023-03-23 op w = audio_write(buf, len);
280 c0180200 2022-06-10 op len -= w;
281 c0180200 2022-06-10 op buf += w;
282 c0180200 2022-06-10 op }
283 c0180200 2022-06-10 op }
284 c0180200 2022-06-10 op
285 2139c525 2022-03-09 op return 1;
286 2139c525 2022-03-09 op }
287 2139c525 2022-03-09 op
288 2139c525 2022-03-09 op int
289 3baa2617 2022-02-16 op player(int debug, int verbose)
290 3baa2617 2022-02-16 op {
291 fc4a38af 2022-05-19 op int r;
292 fc4a38af 2022-05-19 op
293 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
294 3baa2617 2022-02-16 op log_setverbose(verbose);
295 3baa2617 2022-02-16 op
296 3baa2617 2022-02-16 op setproctitle("player");
297 3baa2617 2022-02-16 op log_procinit("player");
298 3baa2617 2022-02-16 op
299 3baa2617 2022-02-16 op #if 0
300 3baa2617 2022-02-16 op {
301 3baa2617 2022-02-16 op static int attached;
302 3baa2617 2022-02-16 op
303 3baa2617 2022-02-16 op while (!attached)
304 3baa2617 2022-02-16 op sleep(1);
305 3baa2617 2022-02-16 op }
306 3baa2617 2022-02-16 op #endif
307 3baa2617 2022-02-16 op
308 06ceb376 2023-03-23 op if (audio_open(player_onmove) == -1)
309 06ceb376 2023-03-23 op fatal("audio_open");
310 3baa2617 2022-02-16 op
311 b02dadd3 2023-05-13 op if ((player_nfds = audio_nfds()) <= 0)
312 b02dadd3 2023-05-13 op fatal("audio_nfds: invalid number of file descriptors: %d",
313 b02dadd3 2023-05-13 op player_nfds);
314 b02dadd3 2023-05-13 op
315 c0180200 2022-06-10 op /* allocate one extra for imsg */
316 b02dadd3 2023-05-13 op player_pfds = calloc(player_nfds + 1, sizeof(*player_pfds));
317 c0180200 2022-06-10 op if (player_pfds == NULL)
318 c0180200 2022-06-10 op fatal("calloc");
319 c0180200 2022-06-10 op
320 c0180200 2022-06-10 op player_pfds[0].events = POLLIN;
321 c0180200 2022-06-10 op player_pfds[0].fd = 3;
322 c0180200 2022-06-10 op
323 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
324 3baa2617 2022-02-16 op imsg_init(ibuf, 3);
325 3baa2617 2022-02-16 op
326 3baa2617 2022-02-16 op signal(SIGINT, player_signal_handler);
327 3baa2617 2022-02-16 op signal(SIGTERM, player_signal_handler);
328 3baa2617 2022-02-16 op
329 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
330 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
331 3baa2617 2022-02-16 op
332 251e00ff 2022-03-10 op if (pledge("stdio recvfd audio", NULL) == -1)
333 3baa2617 2022-02-16 op fatal("pledge");
334 3baa2617 2022-02-16 op
335 3baa2617 2022-02-16 op while (!halted) {
336 17ef54d6 2022-06-22 op const char *errstr = NULL;
337 17ef54d6 2022-06-22 op
338 3baa2617 2022-02-16 op while (nextfd == -1)
339 c2f554ff 2022-07-09 op player_dispatch(NULL, 1);
340 06412529 2022-05-09 op
341 17ef54d6 2022-06-22 op r = player_playnext(&errstr);
342 239029b6 2022-05-09 op if (r == -1)
343 17ef54d6 2022-06-22 op player_senderr(errstr);
344 239029b6 2022-05-09 op if (r == 0)
345 06412529 2022-05-09 op player_sendeof();
346 3baa2617 2022-02-16 op }
347 3baa2617 2022-02-16 op
348 3baa2617 2022-02-16 op return 0;
349 3baa2617 2022-02-16 op }