Blob


1 /*
2 * Copyright (c) 2022, 2023 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 int player_nfds;
38 static struct imsgbuf *imsgbuf;
40 static int nextfd = -1;
41 static int64_t samples;
42 static int64_t duration;
43 static unsigned int current_rate;
45 volatile sig_atomic_t halted;
47 static void
48 player_signal_handler(int signo)
49 {
50 halted = 1;
51 }
53 int
54 player_setup(unsigned int bits, unsigned int rate, unsigned int channels)
55 {
56 log_debug("%s: bits=%u, rate=%u, channels=%u", __func__,
57 bits, rate, channels);
59 current_rate = rate;
60 return audio_setup(bits, rate, channels, player_pfds + 1, player_nfds);
61 }
63 void
64 player_setduration(int64_t d)
65 {
66 int64_t seconds;
68 duration = d;
69 seconds = duration / current_rate;
70 imsg_compose(imsgbuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
71 imsg_flush(imsgbuf);
72 }
74 static void
75 player_onmove(void *arg, int delta)
76 {
77 static int64_t reported;
78 int64_t sec;
80 samples += delta;
81 if (llabs(samples - reported) >= current_rate) {
82 reported = samples;
83 sec = samples / current_rate;
85 imsg_compose(imsgbuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
86 imsg_flush(imsgbuf);
87 }
88 }
90 void
91 player_setpos(int64_t pos)
92 {
93 samples = pos;
94 player_onmove(NULL, 0);
95 }
97 /* process only one message */
98 static int
99 player_dispatch(int64_t *s, int wait)
101 struct player_seek seek;
102 struct pollfd pfd;
103 struct imsg imsg;
104 ssize_t n;
105 int ret;
107 if (halted != 0)
108 return IMSG_STOP;
110 again:
111 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
112 fatal("imsg_get");
113 if (n == 0) {
114 if (!wait)
115 return -1;
117 pfd.fd = imsgbuf->fd;
118 pfd.events = POLLIN;
119 if (poll(&pfd, 1, INFTIM) == -1)
120 fatal("poll");
121 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
122 fatal("imsg_read");
123 if (n == 0)
124 fatalx("pipe closed");
125 goto again;
128 ret = imsg_get_type(&imsg);
129 switch (ret) {
130 case IMSG_PLAY:
131 if (nextfd != -1)
132 fatalx("track already enqueued");
133 if ((nextfd = imsg_get_fd(&imsg)) == -1)
134 fatalx("%s: got invalid file descriptor", __func__);
135 log_debug("song enqueued");
136 ret = IMSG_STOP;
137 break;
138 case IMSG_RESUME:
139 case IMSG_PAUSE:
140 case IMSG_STOP:
141 break;
142 case IMSG_CTL_SEEK:
143 if (s == NULL)
144 break;
145 if (imsg_get_data(&imsg, &seek, sizeof(seek)) == -1)
146 fatalx("wrong size for seek ctl");
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;
153 if (*s < 0)
154 *s = 0;
155 break;
156 default:
157 fatalx("unknown imsg %d", ret);
160 imsg_free(&imsg);
161 return ret;
164 static void
165 player_senderr(const char *errstr)
167 size_t len = 0;
169 if (errstr != NULL)
170 len = strlen(errstr) + 1;
172 imsg_compose(imsgbuf, IMSG_ERR, 0, 0, -1, errstr, len);
173 imsg_flush(imsgbuf);
176 static void
177 player_sendeof(void)
179 imsg_compose(imsgbuf, IMSG_EOF, 0, 0, -1, NULL, 0);
180 imsg_flush(imsgbuf);
183 static int
184 player_playnext(const char **errstr)
186 static char buf[512];
187 ssize_t r;
188 int fd = nextfd;
190 assert(nextfd != -1);
191 nextfd = -1;
193 /* reset samples and set position to zero */
194 samples = 0;
195 imsg_compose(imsgbuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
196 imsg_flush(imsgbuf);
198 r = read(fd, buf, sizeof(buf));
200 /* 8 byte is the larger magic number */
201 if (r < 8) {
202 *errstr = "read failed";
203 goto err;
206 if (lseek(fd, 0, SEEK_SET) == -1) {
207 *errstr = "lseek failed";
208 goto err;
211 if (memcmp(buf, "fLaC", 4) == 0)
212 return play_flac(fd, errstr);
213 if (memcmp(buf, "ID3", 3) == 0 ||
214 memcmp(buf, "\xFF\xFB", 2) == 0)
215 return play_mp3(fd, errstr);
216 if (memmem(buf, r, "OpusHead", 8) != NULL)
217 return play_opus(fd, errstr);
218 if (memmem(buf, r, "OggS", 4) != NULL)
219 return play_oggvorbis(fd, errstr);
221 *errstr = "unknown file type";
222 err:
223 close(fd);
224 return -1;
227 static int
228 player_pause(int64_t *s)
230 int r;
232 r = player_dispatch(s, 1);
233 return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
236 static int
237 player_shouldstop(int64_t *s, int wait)
239 switch (player_dispatch(s, wait)) {
240 case IMSG_PAUSE:
241 if (player_pause(s))
242 break;
243 /* fallthrough */
244 case IMSG_STOP:
245 return 1;
248 return 0;
251 int
252 play(const void *buf, size_t len, int64_t *s)
254 size_t w;
255 int revents, r, wait;
257 *s = -1;
258 while (len != 0) {
259 audio_pollfd(player_pfds + 1, player_nfds, POLLOUT);
260 r = poll(player_pfds, player_nfds + 1, INFTIM);
261 if (r == -1)
262 fatal("poll");
264 wait = player_pfds[0].revents & (POLLHUP|POLLIN);
265 if (player_shouldstop(s, wait)) {
266 audio_flush();
267 return 0;
270 revents = audio_revents(player_pfds + 1, player_nfds);
271 if (revents & POLLHUP) {
272 if (errno == EAGAIN)
273 continue;
274 fatal("audio hang-up");
276 if (revents & POLLOUT) {
277 w = audio_write(buf, len);
278 len -= w;
279 buf += w;
283 return 1;
286 int
287 player(int debug, int verbose)
289 int r;
291 log_init(debug, LOG_DAEMON);
292 log_setverbose(verbose);
294 setproctitle("player");
295 log_procinit("player");
297 #if 0
299 static int attached;
301 while (!attached)
302 sleep(1);
304 #endif
306 if (audio_open(player_onmove) == -1)
307 fatal("audio_open");
309 if ((player_nfds = audio_nfds()) <= 0)
310 fatal("audio_nfds: invalid number of file descriptors: %d",
311 player_nfds);
313 /* allocate one extra for imsg */
314 player_pfds = calloc(player_nfds + 1, sizeof(*player_pfds));
315 if (player_pfds == NULL)
316 fatal("calloc");
318 player_pfds[0].events = POLLIN;
319 player_pfds[0].fd = 3;
321 imsgbuf = xmalloc(sizeof(*imsgbuf));
322 imsg_init(imsgbuf, 3);
324 signal(SIGINT, player_signal_handler);
325 signal(SIGTERM, player_signal_handler);
327 signal(SIGHUP, SIG_IGN);
328 signal(SIGPIPE, SIG_IGN);
330 if (pledge("stdio recvfd audio", NULL) == -1)
331 fatal("pledge");
333 while (!halted) {
334 const char *errstr = NULL;
336 while (nextfd == -1)
337 player_dispatch(NULL, 1);
339 r = player_playnext(&errstr);
340 if (r == -1)
341 player_senderr(errstr);
342 if (r == 0)
343 player_sendeof();
346 return 0;