Blame
Date:
Fri Feb 18 08:29:17 2022 UTC
Message:
support setting the number of channels for flac and vorbis
001
2022-02-16
op
/*
002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
003
2022-02-16
op
*
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.
007
2022-02-16
op
*
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.
015
2022-02-16
op
*/
016
2022-02-16
op
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>
020
2022-02-16
op
021
2022-02-16
op
#include <limits.h>
022
2022-02-16
op
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>
037
2022-02-16
op
038
2022-02-16
op
#include "amused.h"
039
2022-02-16
op
#include "log.h"
040
2022-02-16
op
#include "xmalloc.h"
041
2022-02-16
op
042
2022-02-16
op
static struct imsgbuf *ibuf;
043
2022-02-16
op
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];
047
2022-02-16
op
048
2022-02-16
op
volatile sig_atomic_t halted;
049
2022-02-16
op
050
2022-02-16
op
static void
051
2022-02-16
op
player_signal_handler(int signo)
052
2022-02-16
op
{
053
2022-02-16
op
halted = 1;
054
2022-02-16
op
}
055
2022-02-16
op
056
2022-02-16
op
static void
057
2022-02-16
op
audio_init(void)
058
2022-02-16
op
{
059
2022-02-16
op
struct sio_par par;
060
2022-02-16
op
061
2022-02-16
op
if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0)) == NULL)
062
2022-02-16
op
fatal("sio_open");
063
2022-02-16
op
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");
074
2022-02-16
op
}
075
2022-02-16
op
076
2022-02-16
op
int
077
2022-02-18
op
player_setup(int rate, int channels)
078
2022-02-16
op
{
079
2022-02-16
op
struct sio_par par;
080
2022-02-16
op
081
2022-02-16
op
log_debug("switching to sample rate %d", rate);
082
2022-02-16
op
083
2022-02-16
op
sio_stop(hdl);
084
2022-02-16
op
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;
091
2022-02-18
op
}
092
2022-02-18
op
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;
096
2022-02-16
op
}
097
2022-02-16
op
098
2022-02-16
op
/* TODO: check the sample rate? */
099
2022-02-16
op
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;
103
2022-02-16
op
}
104
2022-02-16
op
return 0;
105
2022-02-16
op
}
106
2022-02-16
op
107
2022-02-16
op
int
108
2022-02-16
op
player_pendingimsg(void)
109
2022-02-16
op
{
110
2022-02-16
op
struct pollfd pfd;
111
2022-02-16
op
int r;
112
2022-02-16
op
113
2022-02-16
op
if (halted != 0)
114
2022-02-16
op
return 1;
115
2022-02-16
op
116
2022-02-16
op
pfd.fd = ibuf->fd;
117
2022-02-16
op
pfd.events = POLLIN;
118
2022-02-16
op
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;
123
2022-02-16
op
}
124
2022-02-16
op
125
2022-02-16
op
void
126
2022-02-16
op
player_enqueue(struct imsg *imsg)
127
2022-02-16
op
{
128
2022-02-16
op
size_t datalen;
129
2022-02-16
op
130
2022-02-16
op
if (nextfd != -1)
131
2022-02-16
op
fatalx("track already enqueued");
132
2022-02-16
op
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);
142
2022-02-16
op
}
143
2022-02-16
op
144
2022-02-16
op
/* process only one message */
145
2022-02-16
op
int
146
2022-02-16
op
player_dispatch(void)
147
2022-02-16
op
{
148
2022-02-16
op
struct imsg imsg;
149
2022-02-16
op
ssize_t n;
150
2022-02-16
op
int ret;
151
2022-02-16
op
152
2022-02-16
op
if (halted != 0)
153
2022-02-16
op
return IMSG_STOP;
154
2022-02-16
op
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");
159
2022-02-16
op
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");
164
2022-02-16
op
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;
172
2022-02-16
op
break;
173
2022-02-16
op
case IMSG_RESUME:
174
2022-02-16
op
case IMSG_PAUSE:
175
2022-02-16
op
case IMSG_STOP:
176
2022-02-16
op
break;
177
2022-02-16
op
default:
178
2022-02-16
op
fatalx("unknown imsg %d", imsg.hdr.type);
179
2022-02-16
op
}
180
2022-02-16
op
181
2022-02-16
op
return ret;
182
2022-02-16
op
}
183
2022-02-16
op
184
2022-02-16
op
void
185
2022-02-16
op
player_senderr(void)
186
2022-02-16
op
{
187
2022-02-16
op
imsg_compose(ibuf, IMSG_ERR, 0, 0, -1, NULL, 0);
188
2022-02-16
op
imsg_flush(ibuf);
189
2022-02-16
op
}
190
2022-02-16
op
191
2022-02-16
op
void
192
2022-02-16
op
player_sendeof(void)
193
2022-02-16
op
{
194
2022-02-16
op
imsg_compose(ibuf, IMSG_EOF, 0, 0, -1, NULL, 0);
195
2022-02-16
op
imsg_flush(ibuf);
196
2022-02-16
op
}
197
2022-02-16
op
198
2022-02-16
op
void
199
2022-02-16
op
player_playnext(void)
200
2022-02-16
op
{
201
2022-02-16
op
int fd = nextfd;
202
2022-02-16
op
203
2022-02-16
op
assert(nextfd != -1);
204
2022-02-16
op
nextfd = -1;
205
2022-02-16
op
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);
215
2022-02-16
op
else {
216
2022-02-16
op
log_warnx("unknown file type for %s", nextpath);
217
2022-02-16
op
player_senderr();
218
2022-02-16
op
return;
219
2022-02-16
op
}
220
2022-02-16
op
}
221
2022-02-16
op
222
2022-02-16
op
int
223
2022-02-16
op
player_pause(void)
224
2022-02-16
op
{
225
2022-02-16
op
int r;
226
2022-02-16
op
227
2022-02-16
op
r = player_dispatch();
228
2022-02-16
op
return r == IMSG_RESUME;
229
2022-02-16
op
}
230
2022-02-16
op
231
2022-02-16
op
int
232
2022-02-16
op
player_shouldstop(void)
233
2022-02-16
op
{
234
2022-02-16
op
if (!player_pendingimsg())
235
2022-02-16
op
return 0;
236
2022-02-16
op
237
2022-02-16
op
switch (player_dispatch()) {
238
2022-02-16
op
case IMSG_PAUSE:
239
2022-02-16
op
if (player_pause())
240
2022-02-16
op
break;
241
2022-02-16
op
/* fallthrough */
242
2022-02-16
op
case IMSG_STOP:
243
2022-02-16
op
return 1;
244
2022-02-16
op
}
245
2022-02-16
op
246
2022-02-16
op
return 0;
247
2022-02-16
op
}
248
2022-02-16
op
249
2022-02-16
op
int
250
2022-02-16
op
player(int debug, int verbose)
251
2022-02-16
op
{
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);
255
2022-02-16
op
256
2022-02-16
op
setproctitle("player");
257
2022-02-16
op
log_procinit("player");
258
2022-02-16
op
259
2022-02-16
op
#if 0
260
2022-02-16
op
{
261
2022-02-16
op
static int attached;
262
2022-02-16
op
263
2022-02-16
op
while (!attached)
264
2022-02-16
op
sleep(1);
265
2022-02-16
op
}
266
2022-02-16
op
#endif
267
2022-02-16
op
268
2022-02-16
op
audio_init();
269
2022-02-16
op
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");
275
2022-02-16
op
276
2022-02-16
op
ibuf = xmalloc(sizeof(*ibuf));
277
2022-02-16
op
imsg_init(ibuf, 3);
278
2022-02-16
op
279
2022-02-16
op
signal(SIGINT, player_signal_handler);
280
2022-02-16
op
signal(SIGTERM, player_signal_handler);
281
2022-02-16
op
282
2022-02-16
op
signal(SIGHUP, SIG_IGN);
283
2022-02-16
op
signal(SIGPIPE, SIG_IGN);
284
2022-02-16
op
285
2022-02-16
op
if (pledge("stdio recvfd", NULL) == -1)
286
2022-02-16
op
fatal("pledge");
287
2022-02-16
op
288
2022-02-16
op
while (!halted) {
289
2022-02-16
op
while (nextfd == -1)
290
2022-02-16
op
player_dispatch();
291
2022-02-16
op
292
2022-02-16
op
player_playnext();
293
2022-02-17
op
294
2022-02-17
op
if (!got_stop)
295
2022-02-17
op
player_sendeof();
296
2022-02-17
op
else
297
2022-02-17
op
got_stop = 0;
298
2022-02-16
op
}
299
2022-02-16
op
300
2022-02-16
op
return 0;
301
2022-02-16
op
}
Omar Polo