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 *ibuf;
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(ibuf, IMSG_LEN, 0, 0, -1, &seconds, sizeof(seconds));
71 imsg_flush(ibuf);
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(ibuf, IMSG_POS, 0, 0, -1, &sec, sizeof(sec));
86 imsg_flush(ibuf);
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(ibuf, &imsg)) == -1)
112 fatal("imsg_get");
113 if (n == 0) {
114 if (!wait)
115 return -1;
117 pfd.fd = ibuf->fd;
118 pfd.events = POLLIN;
119 if (poll(&pfd, 1, INFTIM) == -1)
120 fatal("poll");
121 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
122 fatal("imsg_read");
123 if (n == 0)
124 fatalx("pipe closed");
125 goto again;
128 ret = imsg.hdr.type;
129 switch (imsg.hdr.type) {
130 case IMSG_PLAY:
131 if (nextfd != -1)
132 fatalx("track already enqueued");
133 if ((nextfd = imsg.fd) == -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_DATA_SIZE(imsg) != sizeof(seek))
146 fatalx("wrong size for seek ctl");
147 memcpy(&seek, imsg.data, sizeof(seek));
148 if (seek.percent) {
149 *s = (double)seek.offset * (double)duration / 100.0;
150 } else {
151 *s = seek.offset * current_rate;
152 if (seek.relative)
153 *s += samples;
155 if (*s < 0)
156 *s = 0;
157 break;
158 default:
159 fatalx("unknown imsg %d", imsg.hdr.type);
162 imsg_free(&imsg);
163 return ret;
166 static void
167 player_senderr(const char *errstr)
169 size_t len = 0;
171 if (errstr != NULL)
172 len = strlen(errstr) + 1;
174 imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, errstr, len);
175 imsg_flush(ibuf);
178 static void
179 player_sendeof(void)
181 imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
182 imsg_flush(ibuf);
185 static int
186 player_playnext(const char **errstr)
188 static char buf[512];
189 ssize_t r;
190 int fd = nextfd;
192 assert(nextfd != -1);
193 nextfd = -1;
195 /* reset samples and set position to zero */
196 samples = 0;
197 imsg_compose(ibuf, IMSG_POS, 0, 0, -1, &samples, sizeof(samples));
198 imsg_flush(ibuf);
200 r = read(fd, buf, sizeof(buf));
202 /* 8 byte is the larger magic number */
203 if (r < 8) {
204 *errstr = "read failed";
205 goto err;
208 if (lseek(fd, 0, SEEK_SET) == -1) {
209 *errstr = "lseek failed";
210 goto err;
213 if (memcmp(buf, "fLaC", 4) == 0)
214 return play_flac(fd, errstr);
215 if (memcmp(buf, "ID3", 3) == 0 ||
216 memcmp(buf, "\xFF\xFB", 2) == 0)
217 return play_mp3(fd, errstr);
218 if (memmem(buf, r, "OpusHead", 8) != NULL)
219 return play_opus(fd, errstr);
220 if (memmem(buf, r, "OggS", 4) != NULL)
221 return play_oggvorbis(fd, errstr);
223 *errstr = "unknown file type";
224 err:
225 close(fd);
226 return -1;
229 static int
230 player_pause(int64_t *s)
232 int r;
234 r = player_dispatch(s, 1);
235 return r == IMSG_RESUME || r == IMSG_CTL_SEEK;
238 static int
239 player_shouldstop(int64_t *s, int wait)
241 switch (player_dispatch(s, wait)) {
242 case IMSG_PAUSE:
243 if (player_pause(s))
244 break;
245 /* fallthrough */
246 case IMSG_STOP:
247 return 1;
250 return 0;
253 int
254 play(const void *buf, size_t len, int64_t *s)
256 size_t w;
257 int revents, r, wait;
259 *s = -1;
260 while (len != 0) {
261 audio_pollfd(player_pfds + 1, player_nfds, POLLOUT);
262 r = poll(player_pfds, player_nfds + 1, INFTIM);
263 if (r == -1)
264 fatal("poll");
266 wait = player_pfds[0].revents & (POLLHUP|POLLIN);
267 if (player_shouldstop(s, wait)) {
268 audio_flush();
269 return 0;
272 revents = audio_revents(player_pfds + 1, player_nfds);
273 if (revents & POLLHUP) {
274 if (errno == EAGAIN)
275 continue;
276 fatal("audio hang-up");
278 if (revents & POLLOUT) {
279 w = audio_write(buf, len);
280 len -= w;
281 buf += w;
285 return 1;
288 int
289 player(int debug, int verbose)
291 int r;
293 log_init(debug, LOG_DAEMON);
294 log_setverbose(verbose);
296 setproctitle("player");
297 log_procinit("player");
299 #if 0
301 static int attached;
303 while (!attached)
304 sleep(1);
306 #endif
308 if (audio_open(player_onmove) == -1)
309 fatal("audio_open");
311 if ((player_nfds = audio_nfds()) <= 0)
312 fatal("audio_nfds: invalid number of file descriptors: %d",
313 player_nfds);
315 /* allocate one extra for imsg */
316 player_pfds = calloc(player_nfds + 1, sizeof(*player_pfds));
317 if (player_pfds == NULL)
318 fatal("calloc");
320 player_pfds[0].events = POLLIN;
321 player_pfds[0].fd = 3;
323 ibuf = xmalloc(sizeof(*ibuf));
324 imsg_init(ibuf, 3);
326 signal(SIGINT, player_signal_handler);
327 signal(SIGTERM, player_signal_handler);
329 signal(SIGHUP, SIG_IGN);
330 signal(SIGPIPE, SIG_IGN);
332 if (pledge("stdio recvfd audio", NULL) == -1)
333 fatal("pledge");
335 while (!halted) {
336 const char *errstr = NULL;
338 while (nextfd == -1)
339 player_dispatch(NULL, 1);
341 r = player_playnext(&errstr);
342 if (r == -1)
343 player_senderr(errstr);
344 if (r == 0)
345 player_sendeof();
348 return 0;