2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
17 f36fd90a 2022-07-09 op #include "config.h"
19 3baa2617 2022-02-16 op #include <limits.h>
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>
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"
36 c0180200 2022-06-10 op struct pollfd *player_pfds;
37 5a4b3030 2022-03-09 op static struct imsgbuf *ibuf;
39 3baa2617 2022-02-16 op static int nextfd = -1;
40 ff06024f 2022-07-08 op static int64_t samples;
41 60a09ce7 2022-07-09 op static int64_t duration;
42 06ceb376 2023-03-23 op static unsigned int current_rate;
44 3baa2617 2022-02-16 op volatile sig_atomic_t halted;
47 3baa2617 2022-02-16 op player_signal_handler(int signo)
53 b9d2a697 2022-07-08 op player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
55 069e653b 2023-03-23 op log_debug("%s: bits=%u, rate=%u, channels=%u", __func__,
56 a728254f 2022-02-23 op bits, rate, channels);
58 06ceb376 2023-03-23 op current_rate = rate;
59 06ceb376 2023-03-23 op return audio_setup(bits, rate, channels, player_pfds + 1);
63 60a09ce7 2022-07-09 op player_setduration(int64_t d)
65 ff06024f 2022-07-08 op int64_t seconds;
68 06ceb376 2023-03-23 op seconds = duration / current_rate;
69 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
70 ff06024f 2022-07-08 op imsg_flush(ibuf);
74 ff06024f 2022-07-08 op player_onmove(void *arg, int delta)
76 ff06024f 2022-07-08 op static int64_t reported;
79 ff06024f 2022-07-08 op samples += delta;
80 06ceb376 2023-03-23 op if (llabs(samples - reported) >= current_rate) {
81 ff06024f 2022-07-08 op reported = samples;
82 06ceb376 2023-03-23 op sec = samples / current_rate;
84 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
85 ff06024f 2022-07-08 op imsg_flush(ibuf);
90 791d3db3 2022-07-09 op player_setpos(int64_t pos)
92 791d3db3 2022-07-09 op samples = pos;
93 791d3db3 2022-07-09 op player_onmove(NULL, 0);
96 3baa2617 2022-02-16 op /* process only one message */
98 c2f554ff 2022-07-09 op player_dispatch(int64_t *s, int wait)
100 791d3db3 2022-07-09 op struct player_seek seek;
101 fc4a38af 2022-05-19 op struct pollfd pfd;
102 3baa2617 2022-02-16 op struct imsg imsg;
106 3baa2617 2022-02-16 op if (halted != 0)
107 3baa2617 2022-02-16 op return IMSG_STOP;
110 3baa2617 2022-02-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
111 3baa2617 2022-02-16 op fatal("imsg_get");
112 926ee836 2022-05-19 op if (n == 0) {
116 926ee836 2022-05-19 op pfd.fd = ibuf->fd;
117 926ee836 2022-05-19 op pfd.events = POLLIN;
118 926ee836 2022-05-19 op if (poll(&pfd, 1, INFTIM) == -1)
119 926ee836 2022-05-19 op fatal("poll");
120 926ee836 2022-05-19 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
121 926ee836 2022-05-19 op fatal("imsg_read");
123 926ee836 2022-05-19 op fatalx("pipe closed");
127 3baa2617 2022-02-16 op ret = imsg.hdr.type;
128 3baa2617 2022-02-16 op switch (imsg.hdr.type) {
129 3baa2617 2022-02-16 op case IMSG_PLAY:
130 6e4f8947 2022-06-09 op if (nextfd != -1)
131 6e4f8947 2022-06-09 op fatalx("track already enqueued");
132 6e4f8947 2022-06-09 op if ((nextfd = imsg.fd) == -1)
133 6e4f8947 2022-06-09 op fatalx("%s: got invalid file descriptor", __func__);
134 6e4f8947 2022-06-09 op log_debug("song enqueued");
135 3baa2617 2022-02-16 op ret = IMSG_STOP;
137 3baa2617 2022-02-16 op case IMSG_RESUME:
138 3baa2617 2022-02-16 op case IMSG_PAUSE:
139 3baa2617 2022-02-16 op case IMSG_STOP:
141 791d3db3 2022-07-09 op case IMSG_CTL_SEEK:
142 791d3db3 2022-07-09 op if (s == NULL)
144 791d3db3 2022-07-09 op if (IMSG_DATA_SIZE(imsg) != sizeof(seek))
145 791d3db3 2022-07-09 op fatalx("wrong size for seek ctl");
146 791d3db3 2022-07-09 op memcpy(&seek, imsg.data, sizeof(seek));
147 60a09ce7 2022-07-09 op if (seek.percent) {
148 60a09ce7 2022-07-09 op *s = (double)seek.offset * (double)duration / 100.0;
150 06ceb376 2023-03-23 op *s = seek.offset * current_rate;
151 60a09ce7 2022-07-09 op if (seek.relative)
152 60a09ce7 2022-07-09 op *s += samples;
158 3baa2617 2022-02-16 op fatalx("unknown imsg %d", imsg.hdr.type);
161 fd90976c 2022-06-09 op imsg_free(&imsg);
166 17ef54d6 2022-06-22 op player_senderr(const char *errstr)
168 17ef54d6 2022-06-22 op size_t len = 0;
170 17ef54d6 2022-06-22 op if (errstr != NULL)
171 17ef54d6 2022-06-22 op len = strlen(errstr) + 1;
173 17ef54d6 2022-06-22 op imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
174 3baa2617 2022-02-16 op imsg_flush(ibuf);
178 3baa2617 2022-02-16 op player_sendeof(void)
180 3baa2617 2022-02-16 op imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
181 3baa2617 2022-02-16 op imsg_flush(ibuf);
185 17ef54d6 2022-06-22 op player_playnext(const char **errstr)
187 bf19b03e 2022-05-09 op static char buf[512];
189 3baa2617 2022-02-16 op int fd = nextfd;
191 3baa2617 2022-02-16 op assert(nextfd != -1);
194 ff06024f 2022-07-08 op /* reset samples and set position to zero */
196 ff06024f 2022-07-08 op imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
197 ff06024f 2022-07-08 op imsg_flush(ibuf);
199 bf19b03e 2022-05-09 op r = read(fd, buf, sizeof(buf));
201 bf19b03e 2022-05-09 op /* 8 byte is the larger magic number */
203 17ef54d6 2022-06-22 op *errstr = "read failed";
207 bf19b03e 2022-05-09 op if (lseek(fd, 0, SEEK_SET) == -1) {
208 17ef54d6 2022-06-22 op *errstr = "lseek failed";
212 bf19b03e 2022-05-09 op if (memcmp(buf, "fLaC", 4) == 0)
213 17ef54d6 2022-06-22 op return play_flac(fd, errstr);
214 bf19b03e 2022-05-09 op if (memcmp(buf, "ID3", 3) == 0 ||
215 bf19b03e 2022-05-09 op memcmp(buf, "\xFF\xFB", 2) == 0)
216 17ef54d6 2022-06-22 op return play_mp3(fd, errstr);
217 bf19b03e 2022-05-09 op if (memmem(buf, r, "OpusHead", 8) != NULL)
218 17ef54d6 2022-06-22 op return play_opus(fd, errstr);
219 bf19b03e 2022-05-09 op if (memmem(buf, r, "OggS", 4) != NULL)
220 17ef54d6 2022-06-22 op return play_oggvorbis(fd, errstr);
222 17ef54d6 2022-06-22 op *errstr = "unknown file type";
229 791d3db3 2022-07-09 op player_pause(int64_t *s)
233 c2f554ff 2022-07-09 op r = player_dispatch(s, 1);
234 3ad8bae8 2022-07-09 op return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
238 c2f554ff 2022-07-09 op player_shouldstop(int64_t *s, int wait)
240 c2f554ff 2022-07-09 op switch (player_dispatch(s, wait)) {
241 3baa2617 2022-02-16 op case IMSG_PAUSE:
242 791d3db3 2022-07-09 op if (player_pause(s))
244 3baa2617 2022-02-16 op /* fallthrough */
245 3baa2617 2022-02-16 op case IMSG_STOP:
253 791d3db3 2022-07-09 op play(const void *buf, size_t len, int64_t *s)
256 c2f554ff 2022-07-09 op int nfds, revents, r, wait;
259 c0180200 2022-06-10 op while (len != 0) {
260 06ceb376 2023-03-23 op nfds = audio_pollfd(player_pfds + 1, POLLOUT);
261 c0180200 2022-06-10 op r = poll(player_pfds, nfds + 1, INFTIM);
263 c0180200 2022-06-10 op fatal("poll");
265 c2f554ff 2022-07-09 op wait = player_pfds[0].revents & (POLLHUP|POLLIN);
266 c2f554ff 2022-07-09 op if (player_shouldstop(s, wait)) {
267 06ceb376 2023-03-23 op audio_flush();
271 06ceb376 2023-03-23 op revents = audio_revents(player_pfds + 1);
272 bc946090 2023-02-26 op if (revents & POLLHUP) {
273 bc946090 2023-02-26 op if (errno == EAGAIN)
275 06ceb376 2023-03-23 op fatal("audio hang-up");
277 c0180200 2022-06-10 op if (revents & POLLOUT) {
278 06ceb376 2023-03-23 op w = audio_write(buf, len);
288 3baa2617 2022-02-16 op player(int debug, int verbose)
292 3baa2617 2022-02-16 op log_init(debug, LOG_DAEMON);
293 3baa2617 2022-02-16 op log_setverbose(verbose);
295 3baa2617 2022-02-16 op setproctitle("player");
296 3baa2617 2022-02-16 op log_procinit("player");
300 3baa2617 2022-02-16 op static int attached;
302 3baa2617 2022-02-16 op while (!attached)
307 06ceb376 2023-03-23 op if (audio_open(player_onmove) == -1)
308 06ceb376 2023-03-23 op fatal("audio_open");
310 c0180200 2022-06-10 op /* allocate one extra for imsg */
311 06ceb376 2023-03-23 op player_pfds = calloc(audio_nfds() + 1, sizeof(*player_pfds));
312 c0180200 2022-06-10 op if (player_pfds == NULL)
313 c0180200 2022-06-10 op fatal("calloc");
315 c0180200 2022-06-10 op player_pfds[0].events = POLLIN;
316 c0180200 2022-06-10 op player_pfds[0].fd = 3;
318 3baa2617 2022-02-16 op ibuf = xmalloc(sizeof(*ibuf));
319 3baa2617 2022-02-16 op imsg_init(ibuf, 3);
321 3baa2617 2022-02-16 op signal(SIGINT, player_signal_handler);
322 3baa2617 2022-02-16 op signal(SIGTERM, player_signal_handler);
324 3baa2617 2022-02-16 op signal(SIGHUP, SIG_IGN);
325 3baa2617 2022-02-16 op signal(SIGPIPE, SIG_IGN);
327 251e00ff 2022-03-10 op if (pledge("stdio recvfd audio", NULL) == -1)
328 3baa2617 2022-02-16 op fatal("pledge");
330 3baa2617 2022-02-16 op while (!halted) {
331 17ef54d6 2022-06-22 op const char *errstr = NULL;
333 3baa2617 2022-02-16 op while (nextfd == -1)
334 c2f554ff 2022-07-09 op player_dispatch(NULL, 1);
336 17ef54d6 2022-06-22 op r = player_playnext(&errstr);
338 17ef54d6 2022-06-22 op player_senderr(errstr);
340 06412529 2022-05-09 op player_sendeof();