002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
004
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
005
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
006
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
008
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2022-02-16
op
#include <sys/types.h>
018
2022-02-16
op
#include <sys/queue.h>
019
2022-02-16
op
#include <sys/uio.h>
021
2022-02-16
op
#include <limits.h>
023
2022-02-16
op
#include <assert.h>
024
2022-02-16
op
#include <errno.h>
025
2022-02-16
op
#include <event.h>
026
2022-02-16
op
#include <fcntl.h>
027
2022-02-16
op
#include <poll.h>
028
2022-02-16
op
#include <signal.h>
029
2022-02-16
op
#include <sndio.h>
030
2022-02-17
op
#include <stdio.h>
031
2022-02-16
op
#include <stdlib.h>
032
2022-02-16
op
#include <stdint.h>
033
2022-02-16
op
#include <imsg.h>
034
2022-02-16
op
#include <string.h>
035
2022-02-16
op
#include <syslog.h>
036
2022-02-16
op
#include <unistd.h>
038
2022-02-16
op
#include "amused.h"
039
2022-02-16
op
#include "log.h"
040
2022-02-16
op
#include "xmalloc.h"
042
2022-02-16
op
static struct imsgbuf *ibuf;
044
2022-02-17
op
static int got_stop;
045
2022-02-16
op
static int nextfd = -1;
046
2022-02-16
op
static char nextpath[PATH_MAX];
048
2022-02-16
op
volatile sig_atomic_t halted;
050
2022-02-16
op
static void
051
2022-02-16
op
player_signal_handler(int signo)
053
2022-02-16
op
halted = 1;
056
2022-02-16
op
static void
057
2022-02-16
op
audio_init(void)
059
2022-02-16
op
struct sio_par par;
061
2022-02-16
op
if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
062
2022-02-16
op
fatal("sio_open");
064
2022-02-16
op
sio_initpar(&par);
065
2022-02-16
op
par.bits = 16;
066
2022-02-16
op
par.appbufsz = 50 * par.rate / 1000;
067
2022-02-16
op
par.pchan = 2;
068
2022-02-16
op
if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par))
069
2022-02-16
op
fatal("couldn't set audio params");
070
2022-02-16
op
if (par.bits != 16 || par.le != SIO_LE_NATIVE)
071
2022-02-16
op
fatalx("unsupported audio params");
072
2022-02-16
op
if (!sio_start(hdl))
073
2022-02-16
op
fatal("sio_start");
077
2022-02-18
op
player_setup(int rate, int channels)
079
2022-02-16
op
struct sio_par par;
081
2022-02-16
op
log_debug("switching to sample rate %d", rate);
083
2022-02-16
op
sio_stop(hdl);
085
2022-02-16
op
sio_initpar(&par);
086
2022-02-16
op
par.rate = rate;
087
2022-02-18
op
par.pchan = channels;
088
2022-02-16
op
if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
089
2022-02-16
op
log_warnx("invalid params");
090
2022-02-18
op
return -1;
093
2022-02-18
op
if (par.pchan != channels) {
094
2022-02-18
op
log_warnx("failed to set params");
095
2022-02-16
op
return -1;
098
2022-02-16
op
/* TODO: check the sample rate? */
100
2022-02-16
op
if (!sio_start(hdl)) {
101
2022-02-16
op
log_warn("sio_start");
102
2022-02-16
op
return -1;
104
2022-02-16
op
return 0;
108
2022-02-16
op
player_pendingimsg(void)
110
2022-02-16
op
struct pollfd pfd;
113
2022-02-16
op
if (halted != 0)
114
2022-02-16
op
return 1;
116
2022-02-16
op
pfd.fd = ibuf->fd;
117
2022-02-16
op
pfd.events = POLLIN;
119
2022-02-16
op
r = poll(&pfd, 1, 0);
120
2022-02-16
op
if (r == -1)
121
2022-02-16
op
fatal("poll");
122
2022-02-16
op
return r;
126
2022-02-16
op
player_enqueue(struct imsg *imsg)
128
2022-02-16
op
size_t datalen;
130
2022-02-16
op
if (nextfd != -1)
131
2022-02-16
op
fatalx("track already enqueued");
133
2022-02-16
op
datalen = IMSG_DATA_SIZE(*imsg);
134
2022-02-16
op
if (datalen != sizeof(nextpath))
135
2022-02-16
op
fatalx("%s: size mismatch", __func__);
136
2022-02-16
op
memcpy(nextpath, imsg->data, sizeof(nextpath));
137
2022-02-16
op
if (nextpath[datalen-1] != '\0')
138
2022-02-16
op
fatalx("%s: corrupted path", __func__);
139
2022-02-16
op
if ((nextfd = imsg->fd) == -1)
140
2022-02-16
op
fatalx("%s: got invalid file descriptor", __func__);
141
2022-02-16
op
log_debug("enqueued %s", nextpath);
144
2022-02-16
op
/* process only one message */
146
2022-02-16
op
player_dispatch(void)
148
2022-02-16
op
struct imsg imsg;
149
2022-02-16
op
ssize_t n;
152
2022-02-16
op
if (halted != 0)
153
2022-02-16
op
return IMSG_STOP;
155
2022-02-16
op
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
156
2022-02-16
op
fatalx("imsg_read");
157
2022-02-16
op
if (n == 0)
158
2022-02-16
op
fatalx("pipe closed");
160
2022-02-16
op
if ((n = imsg_get(ibuf, &imsg)) == -1)
161
2022-02-16
op
fatal("imsg_get");
162
2022-02-16
op
if (n == 0) /* no more messages */
163
2022-02-16
op
fatalx("expected at least a message");
165
2022-02-16
op
ret = imsg.hdr.type;
166
2022-02-17
op
if (ret == IMSG_STOP)
167
2022-02-17
op
got_stop = 1;
168
2022-02-16
op
switch (imsg.hdr.type) {
169
2022-02-16
op
case IMSG_PLAY:
170
2022-02-16
op
player_enqueue(&imsg);
171
2022-02-16
op
ret = IMSG_STOP;
173
2022-02-16
op
case IMSG_RESUME:
174
2022-02-16
op
case IMSG_PAUSE:
175
2022-02-16
op
case IMSG_STOP:
178
2022-02-16
op
fatalx("unknown imsg %d", imsg.hdr.type);
181
2022-02-16
op
return ret;
185
2022-02-16
op
player_senderr(void)
187
2022-02-16
op
imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
188
2022-02-16
op
imsg_flush(ibuf);
192
2022-02-16
op
player_sendeof(void)
194
2022-02-16
op
imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
195
2022-02-16
op
imsg_flush(ibuf);
199
2022-02-16
op
player_playnext(void)
201
2022-02-16
op
int fd = nextfd;
203
2022-02-16
op
assert(nextfd != -1);
204
2022-02-16
op
nextfd = -1;
206
2022-02-16
op
/* XXX: use magic(5) for this, not file extensions */
207
2022-02-16
op
if (strstr(nextpath, ".ogg") != NULL)
208
2022-02-16
op
play_oggvorbis(fd);
209
2022-02-16
op
else if (strstr(nextpath, ".mp3") != NULL)
210
2022-02-16
op
play_mp3(fd);
211
2022-02-16
op
else if (strstr(nextpath, ".flac") != NULL)
212
2022-02-16
op
play_flac(fd);
213
2022-02-16
op
else if (strstr(nextpath, ".opus") != NULL)
214
2022-02-16
op
play_opus(fd);
216
2022-02-16
op
log_warnx("unknown file type for %s", nextpath);
217
2022-02-16
op
player_senderr();
223
2022-02-16
op
player_pause(void)
227
2022-02-16
op
r = player_dispatch();
228
2022-02-16
op
return r == IMSG_RESUME;
232
2022-02-16
op
player_shouldstop(void)
234
2022-02-16
op
if (!player_pendingimsg())
235
2022-02-16
op
return 0;
237
2022-02-16
op
switch (player_dispatch()) {
238
2022-02-16
op
case IMSG_PAUSE:
239
2022-02-16
op
if (player_pause())
241
2022-02-16
op
/* fallthrough */
242
2022-02-16
op
case IMSG_STOP:
243
2022-02-16
op
return 1;
246
2022-02-16
op
return 0;
250
2022-02-16
op
player(int debug, int verbose)
252
2022-02-16
op
int flags;
253
2022-02-16
op
log_init(debug, LOG_DAEMON);
254
2022-02-16
op
log_setverbose(verbose);
256
2022-02-16
op
setproctitle("player");
257
2022-02-16
op
log_procinit("player");
261
2022-02-16
op
static int attached;
263
2022-02-16
op
while (!attached)
264
2022-02-16
op
sleep(1);
268
2022-02-16
op
audio_init();
270
2022-02-16
op
/* mark fd as blocking i/o mode */
271
2022-02-16
op
if ((flags = fcntl(3, F_GETFL)) == -1)
272
2022-02-16
op
fatal("fcntl(F_GETFL)");
273
2022-02-16
op
if (fcntl(3, F_SETFL, flags & ~O_NONBLOCK) == -1)
274
2022-02-16
op
fatal("fcntl F_SETFL O_NONBLOCK");
276
2022-02-16
op
ibuf = xmalloc(sizeof(*ibuf));
277
2022-02-16
op
imsg_init(ibuf, 3);
279
2022-02-16
op
signal(SIGINT, player_signal_handler);
280
2022-02-16
op
signal(SIGTERM, player_signal_handler);
282
2022-02-16
op
signal(SIGHUP, SIG_IGN);
283
2022-02-16
op
signal(SIGPIPE, SIG_IGN);
285
2022-02-16
op
if (pledge("stdio recvfd", NULL) == -1)
286
2022-02-16
op
fatal("pledge");
288
2022-02-16
op
while (!halted) {
289
2022-02-16
op
while (nextfd == -1)
290
2022-02-16
op
player_dispatch();
292
2022-02-16
op
player_playnext();
294
2022-02-17
op
if (!got_stop)
295
2022-02-17
op
player_sendeof();
297
2022-02-17
op
got_stop = 0;
300
2022-02-16
op
return 0;