Blame
Date:
Wed Mar 9 09:13:06 2022 UTC
Message:
refactor the player_shouldstop/sio_write dance in a function
01
2022-02-23
op
/*
02
2022-02-23
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
03
2022-02-23
op
*
04
2022-02-23
op
* Permission to use, copy, modify, and distribute this software for any
05
2022-02-23
op
* purpose with or without fee is hereby granted, provided that the above
06
2022-02-23
op
* copyright notice and this permission notice appear in all copies.
07
2022-02-23
op
*
08
2022-02-23
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
09
2022-02-23
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
2022-02-23
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
2022-02-23
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
2022-02-23
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
2022-02-23
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
2022-02-23
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
2022-02-23
op
*/
16
2022-02-23
op
17
2022-02-23
op
#include <sys/mman.h>
18
2022-02-23
op
#include <sys/stat.h>
19
2022-02-23
op
#include <sys/types.h>
20
2022-02-23
op
#include <sys/queue.h>
21
2022-02-23
op
#include <sys/uio.h>
22
2022-02-23
op
23
2022-02-23
op
#include <err.h>
24
2022-02-23
op
#include <event.h>
25
2022-02-23
op
#include <limits.h>
26
2022-02-23
op
#include <sndio.h>
27
2022-02-23
op
#include <imsg.h>
28
2022-02-23
op
#include <unistd.h>
29
2022-02-23
op
30
2022-02-23
op
#include <mpg123.h>
31
2022-02-23
op
32
2022-02-23
op
#include "amused.h"
33
2022-02-23
op
#include "log.h"
34
2022-02-23
op
35
2022-02-23
op
static int
36
2022-02-23
op
setup(mpg123_handle *mh)
37
2022-02-23
op
{
38
2022-02-23
op
long rate;
39
2022-02-23
op
int chan, enc;
40
2022-02-23
op
41
2022-02-23
op
if (mpg123_getformat(mh, &rate, &chan, &enc) != MPG123_OK) {
42
2022-02-23
op
log_warnx("mpg123_getformat failed");
43
2022-02-23
op
return 0;
44
2022-02-23
op
}
45
2022-02-23
op
46
2022-02-23
op
if (player_setup(mpg123_encsize(enc) * 8, rate, chan) == -1)
47
2022-02-23
op
err(1, "player_setrate");
48
2022-02-23
op
49
2022-02-23
op
return 1;
50
2022-02-23
op
}
51
2022-02-23
op
52
2022-02-23
op
void
53
2022-02-23
op
play_mp3(int fd)
54
2022-02-23
op
{
55
2022-02-23
op
static char buf[4096];
56
2022-02-23
op
size_t len;
57
2022-02-23
op
mpg123_handle *mh;
58
2022-02-23
op
int err;
59
2022-02-23
op
60
2022-02-23
op
if ((mh = mpg123_new(NULL, NULL)) == NULL)
61
2022-02-23
op
fatal("mpg123_new");
62
2022-02-23
op
63
2022-02-23
op
if (mpg123_open_fd(mh, fd) != MPG123_OK) {
64
2022-02-23
op
log_warnx("mpg123_open_fd failed");
65
2022-02-23
op
close(fd);
66
2022-02-23
op
return;
67
2022-02-23
op
}
68
2022-02-23
op
69
2022-02-23
op
if (!setup(mh))
70
2022-02-23
op
goto done;
71
2022-02-23
op
72
2022-02-23
op
for (;;) {
73
2022-02-23
op
err = mpg123_read(mh, buf, sizeof(buf), &len);
74
2022-02-23
op
switch (err) {
75
2022-02-23
op
case MPG123_DONE:
76
2022-02-23
op
goto done;
77
2022-02-23
op
case MPG123_NEW_FORMAT:
78
2022-02-23
op
if (!setup(mh))
79
2022-02-23
op
goto done;
80
2022-02-23
op
break;
81
2022-02-23
op
case MPG123_OK:
82
2022-03-09
op
if (!play(buf, len))
83
2022-03-09
op
goto done;
84
2022-02-23
op
break;
85
2022-02-23
op
default:
86
2022-02-23
op
log_warnx("error %d decoding mp3", err);
87
2022-02-23
op
goto done;
88
2022-02-23
op
}
89
2022-02-23
op
}
90
2022-02-23
op
91
2022-02-23
op
done:
92
2022-02-23
op
mpg123_delete(mh);
93
2022-02-23
op
close(fd);
94
2022-02-23
op
}
Omar Polo