Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 "config.h"
19 #include <limits.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <poll.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <syslog.h>
30 #include <unistd.h>
32 #include "amused.h"
33 #include "log.h"
34 #include "xmalloc.h"
36 struct pollfd *player_pfds;
37 static struct imsgbuf *ibuf;
39 static int nextfd = -1;
40 static int64_t samples;
41 static int64_t duration;
42 static unsigned int current_rate;
44 volatile sig_atomic_t halted;
46 static void
47 player_signal_handler(int signo)
48 {
49 halted = 1;
50 }
52 int
53 player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
54 {
55 log_debug("%s: bits=%u, rate=%u, channels=%u", __func__,
56 bits, rate, channels);
58 current_rate = rate;
59 return audio_setup(bits, rate, channels, player_pfds + 1);
60 }
62 void
63 player_setduration(int64_t d)
64 {
65 int64_t seconds;
67 duration = d;
68 seconds = duration / current_rate;
69 imsg_compose(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
70 imsg_flush(ibuf);
71 }
73 static void
74 player_onmove(void *arg, int delta)
75 {
76 static int64_t reported;
77 int64_t sec;
79 samples += delta;
80 if (llabs(samples - reported) >= current_rate) {
81 reported = samples;
82 sec = samples / current_rate;
84 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
85 imsg_flush(ibuf);
86 }
87 }
89 void
90 player_setpos(int64_t pos)
91 {
92 samples = pos;
93 player_onmove(NULL, 0);
94 }
96 /* process only one message */
97 static int
98 player_dispatch(int64_t *s, int wait)
99 {
100 struct player_seek seek;
101 struct pollfd pfd;
102 struct imsg imsg;
103 ssize_t n;
104 int ret;
106 if (halted != 0)
107 return IMSG_STOP;
109 again:
110 if ((n = imsg_get(ibuf, &imsg)) == -1)
111 fatal("imsg_get");
112 if (n == 0) {
113 if (!wait)
114 return -1;
116 pfd.fd = ibuf->fd;
117 pfd.events = POLLIN;
118 if (poll(&pfd, 1, INFTIM) == -1)
119 fatal("poll");
120 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
121 fatal("imsg_read");
122 if (n == 0)
123 fatalx("pipe closed");
124 goto again;
127 ret = imsg.hdr.type;
128 switch (imsg.hdr.type) {
129 case IMSG_PLAY:
130 if (nextfd != -1)
131 fatalx("track already enqueued");
132 if ((nextfd = imsg.fd) == -1)
133 fatalx("%s: got invalid file descriptor", __func__);
134 log_debug("song enqueued");
135 ret = IMSG_STOP;
136 break;
137 case IMSG_RESUME:
138 case IMSG_PAUSE:
139 case IMSG_STOP:
140 break;
141 case IMSG_CTL_SEEK:
142 if (s == NULL)
143 break;
144 if (IMSG_DATA_SIZE(imsg) != sizeof(seek))
145 fatalx("wrong size for seek ctl");
146 memcpy(&seek, imsg.data, sizeof(seek));
147 if (seek.percent) {
148 *s = (double)seek.offset * (double)duration / 100.0;
149 } else {
150 *s = seek.offset * current_rate;
151 if (seek.relative)
152 *s += samples;
154 if (*s < 0)
155 *s = 0;
156 break;
157 default:
158 fatalx("unknown imsg %d", imsg.hdr.type);
161 imsg_free(&imsg);
162 return ret;
165 static void
166 player_senderr(const char *errstr)
168 size_t len = 0;
170 if (errstr != NULL)
171 len = strlen(errstr) + 1;
173 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
174 imsg_flush(ibuf);
177 static void
178 player_sendeof(void)
180 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
181 imsg_flush(ibuf);
184 static int
185 player_playnext(const char **errstr)
187 static char buf[512];
188 ssize_t r;
189 int fd = nextfd;
191 assert(nextfd != -1);
192 nextfd = -1;
194 /* reset samples and set position to zero */
195 samples = 0;
196 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
197 imsg_flush(ibuf);
199 r = read(fd, buf, sizeof(buf));
201 /* 8 byte is the larger magic number */
202 if (r < 8) {
203 *errstr = "read failed";
204 goto err;
207 if (lseek(fd, 0, SEEK_SET) == -1) {
208 *errstr = "lseek failed";
209 goto err;
212 if (memcmp(buf, "fLaC", 4) == 0)
213 return play_flac(fd, errstr);
214 if (memcmp(buf, "ID3", 3) == 0 ||
215 memcmp(buf, "\xFF\xFB", 2) == 0)
216 return play_mp3(fd, errstr);
217 if (memmem(buf, r, "OpusHead", 8) != NULL)
218 return play_opus(fd, errstr);
219 if (memmem(buf, r, "OggS", 4) != NULL)
220 return play_oggvorbis(fd, errstr);
222 *errstr = "unknown file type";
223 err:
224 close(fd);
225 return -1;
228 static int
229 player_pause(int64_t *s)
231 int r;
233 r = player_dispatch(s, 1);
234 return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
237 static int
238 player_shouldstop(int64_t *s, int wait)
240 switch (player_dispatch(s, wait)) {
241 case IMSG_PAUSE:
242 if (player_pause(s))
243 break;
244 /* fallthrough */
245 case IMSG_STOP:
246 return 1;
249 return 0;
252 int
253 play(const void *buf, size_t len, int64_t *s)
255 size_t w;
256 int nfds, revents, r, wait;
258 *s = -1;
259 while (len != 0) {
260 nfds = audio_pollfd(player_pfds + 1, POLLOUT);
261 r = poll(player_pfds, nfds + 1, INFTIM);
262 if (r == -1)
263 fatal("poll");
265 wait = player_pfds[0].revents & (POLLHUP|POLLIN);
266 if (player_shouldstop(s, wait)) {
267 audio_flush();
268 return 0;
271 revents = audio_revents(player_pfds + 1);
272 if (revents & POLLHUP) {
273 if (errno == EAGAIN)
274 continue;
275 fatal("audio hang-up");
277 if (revents & POLLOUT) {
278 w = audio_write(buf, len);
279 len -= w;
280 buf += w;
284 return 1;
287 int
288 player(int debug, int verbose)
290 int r;
292 log_init(debug, LOG_DAEMON);
293 log_setverbose(verbose);
295 setproctitle("player");
296 log_procinit("player");
298 #if 0
300 static int attached;
302 while (!attached)
303 sleep(1);
305 #endif
307 if (audio_open(player_onmove) == -1)
308 fatal("audio_open");
310 /* allocate one extra for imsg */
311 player_pfds = calloc(audio_nfds() + 1, sizeof(*player_pfds));
312 if (player_pfds == NULL)
313 fatal("calloc");
315 player_pfds[0].events = POLLIN;
316 player_pfds[0].fd = 3;
318 ibuf = xmalloc(sizeof(*ibuf));
319 imsg_init(ibuf, 3);
321 signal(SIGINT, player_signal_handler);
322 signal(SIGTERM, player_signal_handler);
324 signal(SIGHUP, SIG_IGN);
325 signal(SIGPIPE, SIG_IGN);
327 if (pledge("stdio recvfd audio", NULL) == -1)
328 fatal("pledge");
330 while (!halted) {
331 const char *errstr = NULL;
333 while (nextfd == -1)
334 player_dispatch(NULL, 1);
336 r = player_playnext(&errstr);
337 if (r == -1)
338 player_senderr(errstr);
339 if (r == 0)
340 player_sendeof();
343 return 0;