|
0 |
/*
|
|
1 |
* Squeezelite - lightweight headless squeezebox emulator
|
|
2 |
*
|
|
3 |
* (c) Adrian Smith 2012, 2013, triode1@btinternet.com
|
|
4 |
*
|
|
5 |
* This program is free software: you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
|
8 |
* (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 |
*
|
|
18 |
*/
|
|
19 |
|
|
20 |
#include "squeezelite.h"
|
|
21 |
|
|
22 |
#include <mpg123.h>
|
|
23 |
|
|
24 |
#define READ_SIZE 512
|
|
25 |
#define WRITE_SIZE 32 * 1024
|
|
26 |
|
|
27 |
struct mpg {
|
|
28 |
mpg123_handle *h;
|
|
29 |
// mpg symbols to be dynamically loaded
|
|
30 |
int (* mpg123_init)(void);
|
|
31 |
int (* mpg123_feature)(const enum mpg123_feature_set);
|
|
32 |
void (* mpg123_rates)(const long **, size_t *);
|
|
33 |
int (* mpg123_format_none)(mpg123_handle *);
|
|
34 |
int (* mpg123_format)(mpg123_handle *, long, int, int);
|
|
35 |
mpg123_handle *(* mpg123_new)(const char*, int *);
|
|
36 |
void (* mpg123_delete)(mpg123_handle *);
|
|
37 |
int (* mpg123_open_feed)(mpg123_handle *);
|
|
38 |
int (* mpg123_decode)(mpg123_handle *, const unsigned char *, size_t, unsigned char *, size_t, size_t *);
|
|
39 |
int (* mpg123_getformat)(mpg123_handle *, long *, int *, int *);
|
|
40 |
const char* (* mpg123_plain_strerror)(int);
|
|
41 |
};
|
|
42 |
|
|
43 |
static struct mpg *m;
|
|
44 |
|
|
45 |
extern log_level loglevel;
|
|
46 |
|
|
47 |
extern struct buffer *streambuf;
|
|
48 |
extern struct buffer *outputbuf;
|
|
49 |
extern struct streamstate stream;
|
|
50 |
extern struct outputstate output;
|
|
51 |
extern struct decodestate decode;
|
|
52 |
|
|
53 |
#define LOCK_S mutex_lock(streambuf->mutex)
|
|
54 |
#define UNLOCK_S mutex_unlock(streambuf->mutex)
|
|
55 |
#define LOCK_O mutex_lock(outputbuf->mutex)
|
|
56 |
#define UNLOCK_O mutex_unlock(outputbuf->mutex)
|
|
57 |
|
|
58 |
static decode_state mpg_decode(void) {
|
|
59 |
size_t bytes, space, size;
|
|
60 |
int ret;
|
|
61 |
|
|
62 |
LOCK_S;
|
|
63 |
LOCK_O;
|
|
64 |
bytes = min(_buf_used(streambuf), _buf_cont_read(streambuf));
|
|
65 |
space = min(_buf_space(outputbuf), _buf_cont_write(outputbuf));
|
|
66 |
bytes = min(bytes, READ_SIZE);
|
|
67 |
space = min(space, WRITE_SIZE);
|
|
68 |
|
|
69 |
if (stream.state <= DISCONNECT && bytes == 0) {
|
|
70 |
UNLOCK_O;
|
|
71 |
UNLOCK_S;
|
|
72 |
return DECODE_COMPLETE;
|
|
73 |
}
|
|
74 |
|
|
75 |
ret = m->mpg123_decode(m->h, streambuf->readp, bytes, outputbuf->writep, space, &size);
|
|
76 |
|
|
77 |
if (ret == MPG123_NEW_FORMAT) {
|
|
78 |
|
|
79 |
if (decode.new_stream) {
|
|
80 |
long rate;
|
|
81 |
int channels, enc;
|
|
82 |
|
|
83 |
m->mpg123_getformat(m->h, &rate, &channels, &enc);
|
|
84 |
|
|
85 |
LOG_INFO("setting track_start");
|
|
86 |
output.next_sample_rate = rate;
|
|
87 |
output.track_start = outputbuf->writep;
|
|
88 |
if (output.fade_mode) _checkfade(true);
|
|
89 |
decode.new_stream = false;
|
|
90 |
|
|
91 |
} else {
|
|
92 |
LOG_WARN("format change mid stream - not supported");
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
_buf_inc_readp(streambuf, bytes);
|
|
97 |
_buf_inc_writep(outputbuf, size);
|
|
98 |
|
|
99 |
UNLOCK_O;
|
|
100 |
UNLOCK_S;
|
|
101 |
|
|
102 |
LOG_SDEBUG("write %u frames", size / BYTES_PER_FRAME);
|
|
103 |
|
|
104 |
if (ret == MPG123_DONE) {
|
|
105 |
LOG_INFO("stream complete");
|
|
106 |
return DECODE_COMPLETE;
|
|
107 |
}
|
|
108 |
|
|
109 |
if (ret == MPG123_ERR) {
|
|
110 |
LOG_WARN("Error");
|
|
111 |
return DECODE_COMPLETE;
|
|
112 |
}
|
|
113 |
|
|
114 |
// OK and NEED_MORE keep running
|
|
115 |
return DECODE_RUNNING;
|
|
116 |
}
|
|
117 |
|
|
118 |
static void mpg_open(u8_t size, u8_t rate, u8_t chan, u8_t endianness) {
|
|
119 |
int err;
|
|
120 |
const long *list;
|
|
121 |
size_t count, i;
|
|
122 |
|
|
123 |
if (m->h) {
|
|
124 |
m->mpg123_delete(m->h);
|
|
125 |
}
|
|
126 |
|
|
127 |
m->h = m->mpg123_new(NULL, &err);
|
|
128 |
|
|
129 |
if (m->h == NULL) {
|
|
130 |
LOG_WARN("new error: %s", m->mpg123_plain_strerror(err));
|
|
131 |
}
|
|
132 |
|
|
133 |
// restrict output to 32bit signed 2 channel for all supported sample rates
|
|
134 |
m->mpg123_rates(&list, &count);
|
|
135 |
m->mpg123_format_none(m->h);
|
|
136 |
for (i = 0; i < count; i++) {
|
|
137 |
m->mpg123_format(m->h, list[i], 2, MPG123_ENC_SIGNED_32);
|
|
138 |
}
|
|
139 |
|
|
140 |
err = m->mpg123_open_feed(m->h);
|
|
141 |
|
|
142 |
if (err) {
|
|
143 |
LOG_WARN("open feed error: %s", m->mpg123_plain_strerror(err));
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
static void mpg_close(void) {
|
|
148 |
m->mpg123_delete(m->h);
|
|
149 |
m->h = NULL;
|
|
150 |
}
|
|
151 |
|
|
152 |
static bool load_mpg() {
|
|
153 |
void *handle = dlopen(LIBMPG, RTLD_NOW);
|
|
154 |
char *err;
|
|
155 |
|
|
156 |
if (!handle) {
|
|
157 |
LOG_INFO("dlerror: %s", dlerror());
|
|
158 |
return false;
|
|
159 |
}
|
|
160 |
|
|
161 |
m = malloc(sizeof(struct mpg));
|
|
162 |
|
|
163 |
m->h = NULL;
|
|
164 |
m->mpg123_init = dlsym(handle, "mpg123_init");
|
|
165 |
m->mpg123_feature = dlsym(handle, "mpg123_feature");
|
|
166 |
m->mpg123_rates = dlsym(handle, "mpg123_rates");
|
|
167 |
m->mpg123_format_none = dlsym(handle, "mpg123_format_none");
|
|
168 |
m->mpg123_format = dlsym(handle, "mpg123_format");
|
|
169 |
m->mpg123_new = dlsym(handle, "mpg123_new");
|
|
170 |
m->mpg123_delete = dlsym(handle, "mpg123_delete");
|
|
171 |
m->mpg123_open_feed = dlsym(handle, "mpg123_open_feed");
|
|
172 |
m->mpg123_decode = dlsym(handle, "mpg123_decode");
|
|
173 |
m->mpg123_getformat = dlsym(handle, "mpg123_getformat");
|
|
174 |
m->mpg123_plain_strerror = dlsym(handle, "mpg123_plain_strerror");
|
|
175 |
|
|
176 |
if ((err = dlerror()) != NULL) {
|
|
177 |
LOG_INFO("dlerror: %s", err);
|
|
178 |
return false;
|
|
179 |
}
|
|
180 |
|
|
181 |
m->mpg123_init();
|
|
182 |
|
|
183 |
if (!m->mpg123_feature(MPG123_FEATURE_OUTPUT_32BIT)) {
|
|
184 |
LOG_WARN("32 bit output not supported - disabled");
|
|
185 |
return false;
|
|
186 |
}
|
|
187 |
|
|
188 |
LOG_INFO("loaded "LIBMPG);
|
|
189 |
return true;
|
|
190 |
}
|
|
191 |
|
|
192 |
struct codec *register_mpg(void) {
|
|
193 |
static struct codec ret = {
|
|
194 |
'm', // id
|
|
195 |
"mp3", // types
|
|
196 |
READ_SIZE, // min read
|
|
197 |
WRITE_SIZE, // min space
|
|
198 |
mpg_open, // open
|
|
199 |
mpg_close, // close
|
|
200 |
mpg_decode, // decode
|
|
201 |
};
|
|
202 |
|
|
203 |
if (!load_mpg()) {
|
|
204 |
return NULL;
|
|
205 |
}
|
|
206 |
|
|
207 |
return &ret;
|
|
208 |
}
|