Asterisk - The Open Source Telephony Project GIT-master-f36a736
format_mp3.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Anthony Minessale <anthmct@yahoo.com>
5 *
6 * Derived from other asterisk sound formats by
7 * Mark Spencer <markster@linux-support.net>
8 *
9 * Thanks to mpglib from http://www.mpg123.org/
10 * and Chris Stenton [jacs@gnome.co.uk]
11 * for coding the ability to play stereo and non-8khz files
12
13 * See http://www.asterisk.org for more information about
14 * the Asterisk project. Please do not directly contact
15 * any of the maintainers of this project for assistance;
16 * the project provides a web site, mailing lists and IRC
17 * channels for your use.
18 *
19 * This program is free software, distributed under the terms of
20 * the GNU General Public License Version 2. See the LICENSE file
21 * at the top of the source tree.
22 */
23
24/*!
25 * \file
26 * \brief MP3 Format Handler
27 * \ingroup formats
28 */
29
30/*** MODULEINFO
31 <defaultenabled>no</defaultenabled>
32 <support_level>extended</support_level>
33 ***/
34
35#include "asterisk.h"
36
37#include "mp3/mpg123.h"
38#include "mp3/mpglib.h"
39
40#include "asterisk/module.h"
41#include "asterisk/mod_format.h"
42#include "asterisk/logger.h"
44
45#define MP3_BUFLEN 320
46#define MP3_SCACHE 16384
47#define MP3_DCACHE 8192
48
50 /*! state for the mp3 decoder */
51 struct mpstr mp;
52 /*! buffer to hold mp3 data after read from disk */
54 /*! buffer for slinear audio after being decoded out of sbuf */
56 /*! how much data has been written to the output buffer in the ast_filestream */
57 int buflen;
58 /*! how much data has been written to sbuf */
60 /*! how much data is left to be read out of dbuf, starting at dbufoffset */
62 /*! current offset for reading data out of dbuf */
64 int offset;
65 long seek;
66};
67
68static const char name[] = "mp3";
69
70#define BLOCKSIZE 160
71#define OUTSCALE 4096
72
73#define GAIN -4 /* 2^GAIN is the multiple to increase the volume by */
74
75#if __BYTE_ORDER == __LITTLE_ENDIAN
76#define htoll(b) (b)
77#define htols(b) (b)
78#define ltohl(b) (b)
79#define ltohs(b) (b)
80#else
81#if __BYTE_ORDER == __BIG_ENDIAN
82#define htoll(b) \
83 (((((b) ) & 0xFF) << 24) | \
84 ((((b) >> 8) & 0xFF) << 16) | \
85 ((((b) >> 16) & 0xFF) << 8) | \
86 ((((b) >> 24) & 0xFF) ))
87#define htols(b) \
88 (((((b) ) & 0xFF) << 8) | \
89 ((((b) >> 8) & 0xFF) ))
90#define ltohl(b) htoll(b)
91#define ltohs(b) htols(b)
92#else
93#error "Endianess not defined"
94#endif
95#endif
96
97
98static int mp3_open(struct ast_filestream *s)
99{
100 struct mp3_private *p = s->_private;
101 InitMP3(&p->mp, OUTSCALE);
102 return 0;
103}
104
105
106static void mp3_close(struct ast_filestream *s)
107{
108 struct mp3_private *p = s->_private;
109
110 ExitMP3(&p->mp);
111 return;
112}
113
114static int mp3_squeue(struct ast_filestream *s)
115{
116 struct mp3_private *p = s->_private;
117 int res=0;
118
119 res = ftell(s->f);
120 p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
121 if (p->sbuflen < MP3_SCACHE) {
122 if (ferror(s->f)) {
123 ast_log(LOG_WARNING, "Error while reading MP3 file: %s\n", strerror(errno));
124 return -1;
125 }
126 }
127 res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
128 if(res != MP3_OK)
129 return -1;
130 p->sbuflen -= p->dbuflen;
131 p->dbufoffset = 0;
132 return 0;
133}
134
135static int mp3_dqueue(struct ast_filestream *s)
136{
137 struct mp3_private *p = s->_private;
138 int res=0;
139
140 if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
141 p->sbuflen -= p->dbuflen;
142 p->dbufoffset = 0;
143 }
144 return res;
145}
146
147static int mp3_queue(struct ast_filestream *s)
148{
149 struct mp3_private *p = s->_private;
150 int res = 0, bytes = 0;
151
152 if(p->seek) {
153 ExitMP3(&p->mp);
154 InitMP3(&p->mp, OUTSCALE);
155 fseek(s->f, 0, SEEK_SET);
156 p->sbuflen = p->dbuflen = p->offset = 0;
157 while(p->offset < p->seek) {
158 if(mp3_squeue(s))
159 return -1;
160 while(p->offset < p->seek && ((res = mp3_dqueue(s))) == MP3_OK) {
161 for(bytes = 0 ; bytes < p->dbuflen ; bytes++) {
162 p->dbufoffset++;
163 p->offset++;
164 if(p->offset >= p->seek)
165 break;
166 }
167 }
168 if(res == MP3_ERR)
169 return -1;
170 }
171
172 p->seek = 0;
173 return 0;
174 }
175 if(p->dbuflen == 0) {
176 if(p->sbuflen) {
177 res = mp3_dqueue(s);
178 if(res == MP3_ERR)
179 return -1;
180 }
181 if(! p->sbuflen || res != MP3_OK) {
182 if(mp3_squeue(s))
183 return -1;
184 }
185
186 }
187
188 return 0;
189}
190
191static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
192{
193
194 struct mp3_private *p = s->_private;
195 int delay =0;
196 int save=0;
197
198 /* Pre-populate the buffer that holds audio to be returned (dbuf) */
199 if (mp3_queue(s)) {
200 return NULL;
201 }
202
203 if (p->dbuflen) {
204 /* Read out what's waiting in dbuf */
205 for (p->buflen = 0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
206 s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[p->buflen + p->dbufoffset];
207 }
208 p->dbufoffset += p->buflen;
209 p->dbuflen -= p->buflen;
210 }
211
212 if (p->buflen < MP3_BUFLEN) {
213 /* dbuf didn't have enough, so reset dbuf, fill it back up and continue */
214 p->dbuflen = p->dbufoffset = 0;
215
216 if (mp3_queue(s)) {
217 return NULL;
218 }
219
220 /* Make sure dbuf has enough to complete this read attempt */
221 if (p->dbuflen >= (MP3_BUFLEN - p->buflen)) {
222 for (save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
223 s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[(p->buflen - save) + p->dbufoffset];
224 }
225 p->dbufoffset += (MP3_BUFLEN - save);
226 p->dbuflen -= (MP3_BUFLEN - save);
227 }
228
229 }
230
231 p->offset += p->buflen;
232 delay = p->buflen / 2;
234 s->fr.samples = delay;
235 *whennext = delay;
236 return &s->fr;
237}
238
239
240static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
241{
242 ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
243 return -1;
244
245}
246
247
248static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
249{
250 struct mp3_private *p = s->_private;
251 off_t min,max,cur;
252 long offset=0,samples;
253 samples = sample_offset * 2;
254
255 min = 0;
256 fseek(s->f, 0, SEEK_END);
257 max = ftell(s->f) * 100;
258 cur = p->offset;
259
260 if (whence == SEEK_SET)
261 offset = samples + min;
262 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
263 offset = samples + cur;
264 else if (whence == SEEK_END)
265 offset = max - samples;
266 if (whence != SEEK_FORCECUR) {
267 offset = (offset > max)?max:offset;
268 }
269
270 p->seek = offset;
271 return fseek(s->f, offset, SEEK_SET);
272
273}
274
275static int mp3_rewrite(struct ast_filestream *s, const char *comment)
276{
277 ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
278 return -1;
279}
280
281static int mp3_trunc(struct ast_filestream *s)
282{
283
284 ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
285 return -1;
286}
287
288static off_t mp3_tell(struct ast_filestream *s)
289{
290 struct mp3_private *p = s->_private;
291
292 return p->offset/2;
293}
294
295static char *mp3_getcomment(struct ast_filestream *s)
296{
297 return NULL;
298}
299
300static struct ast_format_def mp3_f = {
301 .name = "mp3",
302 .exts = "mp3",
303 .open = mp3_open,
304 .write = mp3_write,
305 .rewrite = mp3_rewrite,
306 .seek = mp3_seek,
307 .trunc = mp3_trunc,
308 .tell = mp3_tell,
309 .read = mp3_read,
310 .close = mp3_close,
311 .getcomment = mp3_getcomment,
312 .buf_size = MP3_BUFLEN + AST_FRIENDLY_OFFSET,
313 .desc_size = sizeof(struct mp3_private),
314};
315
316
317static int load_module(void)
318{
320 InitMP3Constants();
322}
323
324static int unload_module(void)
325{
327}
328
329AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]");
#define comment
Definition: ael_lex.c:965
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198
#define SEEK_FORCECUR
Definition: file.h:51
Media Format Cache API.
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
#define MP3_BUFLEN
Definition: format_mp3.c:45
static const char name[]
Definition: format_mp3.c:68
#define MP3_SCACHE
Definition: format_mp3.c:46
static struct ast_format_def mp3_f
Definition: format_mp3.c:300
static int mp3_open(struct ast_filestream *s)
Definition: format_mp3.c:98
static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
Definition: format_mp3.c:248
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]")
#define MP3_DCACHE
Definition: format_mp3.c:47
static off_t mp3_tell(struct ast_filestream *s)
Definition: format_mp3.c:288
static int mp3_squeue(struct ast_filestream *s)
Definition: format_mp3.c:114
static int mp3_queue(struct ast_filestream *s)
Definition: format_mp3.c:147
static char * mp3_getcomment(struct ast_filestream *s)
Definition: format_mp3.c:295
static int mp3_dqueue(struct ast_filestream *s)
Definition: format_mp3.c:135
static struct ast_frame * mp3_read(struct ast_filestream *s, int *whennext)
Definition: format_mp3.c:191
#define OUTSCALE
Definition: format_mp3.c:71
static void mp3_close(struct ast_filestream *s)
Definition: format_mp3.c:106
static int mp3_trunc(struct ast_filestream *s)
Definition: format_mp3.c:281
static int load_module(void)
Definition: format_mp3.c:317
static int unload_module(void)
Definition: format_mp3.c:324
static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
Definition: format_mp3.c:240
static int mp3_rewrite(struct ast_filestream *s, const char *comment)
Definition: format_mp3.c:275
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
Support for logging to various files, console and syslog Configuration in file logger....
#define LOG_ERROR
#define LOG_WARNING
int errno
Header for providers of file and format handling routines. Clients of these routines should include "...
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_format_def_register(f)
Definition: mod_format.h:136
Asterisk module definitions.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
#define NULL
Definition: resample.c:96
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
void * _private
Definition: mod_format.h:124
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char name[80]
Definition: mod_format.h:44
struct ast_format * format
Definition: mod_format.h:48
Data structure associated with a single frame of data.
char sbuf[MP3_SCACHE]
Definition: format_mp3.c:53
char dbuf[MP3_DCACHE]
Definition: format_mp3.c:55
struct mpstr mp
Definition: format_mp3.c:51
int dbufoffset
Definition: format_mp3.c:63