Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
format_mp3.c File Reference

MP3 Format Handler. More...

#include "asterisk.h"
#include "mp3/mpg123.h"
#include "mp3/mpglib.h"
#include "asterisk/module.h"
#include "asterisk/mod_format.h"
#include "asterisk/logger.h"
#include "asterisk/format_cache.h"
Include dependency graph for format_mp3.c:

Go to the source code of this file.

Data Structures

struct  mp3_private
 

Macros

#define BLOCKSIZE   160
 
#define GAIN   -4 /* 2^GAIN is the multiple to increase the volume by */
 
#define MP3_BUFLEN   320
 
#define MP3_DCACHE   8192
 
#define MP3_SCACHE   16384
 
#define OUTSCALE   4096
 

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]")
 
static int load_module (void)
 
static void mp3_close (struct ast_filestream *s)
 
static int mp3_dqueue (struct ast_filestream *s)
 
static char * mp3_getcomment (struct ast_filestream *s)
 
static int mp3_open (struct ast_filestream *s)
 
static int mp3_queue (struct ast_filestream *s)
 
static struct ast_framemp3_read (struct ast_filestream *s, int *whennext)
 
static int mp3_rewrite (struct ast_filestream *s, const char *comment)
 
static int mp3_seek (struct ast_filestream *s, off_t sample_offset, int whence)
 
static int mp3_squeue (struct ast_filestream *s)
 
static off_t mp3_tell (struct ast_filestream *s)
 
static int mp3_trunc (struct ast_filestream *s)
 
static int mp3_write (struct ast_filestream *fs, struct ast_frame *f)
 
static int unload_module (void)
 

Variables

static struct ast_format_def mp3_f
 
static const char name [] = "mp3"
 

Detailed Description

MP3 Format Handler.

Definition in file format_mp3.c.

Macro Definition Documentation

◆ BLOCKSIZE

#define BLOCKSIZE   160

Definition at line 70 of file format_mp3.c.

◆ GAIN

#define GAIN   -4 /* 2^GAIN is the multiple to increase the volume by */

Definition at line 73 of file format_mp3.c.

◆ MP3_BUFLEN

#define MP3_BUFLEN   320

Definition at line 45 of file format_mp3.c.

Referenced by mp3_read().

◆ MP3_DCACHE

#define MP3_DCACHE   8192

Definition at line 47 of file format_mp3.c.

Referenced by mp3_dqueue(), and mp3_squeue().

◆ MP3_SCACHE

#define MP3_SCACHE   16384

Definition at line 46 of file format_mp3.c.

Referenced by mp3_squeue().

◆ OUTSCALE

#define OUTSCALE   4096

Definition at line 71 of file format_mp3.c.

Referenced by mp3_open(), and mp3_queue().

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"MP3 format [Any rate but 8000hz mono is optimal] 
)

Referenced by unload_module().

◆ load_module()

static int load_module ( void  )
static

Definition at line 317 of file format_mp3.c.

References ast_format_def_register, ast_format_slin, and ast_format_def::format.

318 {
320  InitMP3Constants();
322 }
static struct ast_format_def mp3_f
Definition: format_mp3.c:300
#define ast_format_def_register(f)
Definition: mod_format.h:136
struct ast_format * format
Definition: mod_format.h:48
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41

◆ mp3_close()

static void mp3_close ( struct ast_filestream s)
static

Definition at line 106 of file format_mp3.c.

References ast_filestream::_private, and mp3_private::mp.

107 {
108  struct mp3_private *p = s->_private;
109 
110  ExitMP3(&p->mp);
111  return;
112 }
struct mpstr mp
Definition: format_mp3.c:51
void * _private
Definition: mod_format.h:124

◆ mp3_dqueue()

static int mp3_dqueue ( struct ast_filestream s)
static

Definition at line 135 of file format_mp3.c.

References ast_filestream::_private, mp3_private::dbuf, mp3_private::dbuflen, mp3_private::dbufoffset, mp3_private::mp, MP3_DCACHE, NULL, and mp3_private::sbuflen.

Referenced by mp3_queue().

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 }
struct mpstr mp
Definition: format_mp3.c:51
int dbufoffset
Definition: format_mp3.c:63
#define MP3_DCACHE
Definition: format_mp3.c:47
char dbuf[MP3_DCACHE]
Definition: format_mp3.c:55
#define NULL
Definition: resample.c:96
void * _private
Definition: mod_format.h:124

◆ mp3_getcomment()

static char* mp3_getcomment ( struct ast_filestream s)
static

Definition at line 295 of file format_mp3.c.

References NULL.

296 {
297  return NULL;
298 }
#define NULL
Definition: resample.c:96

◆ mp3_open()

static int mp3_open ( struct ast_filestream s)
static

Definition at line 98 of file format_mp3.c.

References ast_filestream::_private, mp3_private::mp, and OUTSCALE.

99 {
100  struct mp3_private *p = s->_private;
101  InitMP3(&p->mp, OUTSCALE);
102  return 0;
103 }
struct mpstr mp
Definition: format_mp3.c:51
#define OUTSCALE
Definition: format_mp3.c:71
void * _private
Definition: mod_format.h:124

◆ mp3_queue()

static int mp3_queue ( struct ast_filestream s)
static

Definition at line 147 of file format_mp3.c.

References ast_filestream::_private, mp3_private::dbuflen, mp3_private::dbufoffset, ast_filestream::f, mp3_private::mp, mp3_dqueue(), mp3_squeue(), mp3_private::offset, OUTSCALE, mp3_private::sbuflen, and mp3_private::seek.

Referenced by mp3_read().

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 }
struct mpstr mp
Definition: format_mp3.c:51
#define OUTSCALE
Definition: format_mp3.c:71
int dbufoffset
Definition: format_mp3.c:63
static int mp3_squeue(struct ast_filestream *s)
Definition: format_mp3.c:114
static int mp3_dqueue(struct ast_filestream *s)
Definition: format_mp3.c:135
void * _private
Definition: mod_format.h:124

◆ mp3_read()

static struct ast_frame* mp3_read ( struct ast_filestream s,
int *  whennext 
)
static

Definition at line 191 of file format_mp3.c.

References ast_filestream::_private, AST_FRAME_SET_BUFFER, AST_FRIENDLY_OFFSET, ast_filestream::buf, mp3_private::buflen, mp3_private::dbuf, mp3_private::dbuflen, mp3_private::dbufoffset, ast_filestream::fr, MP3_BUFLEN, mp3_queue(), NULL, mp3_private::offset, and ast_frame::samples.

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 }
#define MP3_BUFLEN
Definition: format_mp3.c:45
int dbufoffset
Definition: format_mp3.c:63
char dbuf[MP3_DCACHE]
Definition: format_mp3.c:55
#define NULL
Definition: resample.c:96
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
void * _private
Definition: mod_format.h:124
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
static int mp3_queue(struct ast_filestream *s)
Definition: format_mp3.c:147

◆ mp3_rewrite()

static int mp3_rewrite ( struct ast_filestream s,
const char *  comment 
)
static

Definition at line 275 of file format_mp3.c.

References ast_log, and LOG_ERROR.

276 {
277  ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
278  return -1;
279 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285

◆ mp3_seek()

static int mp3_seek ( struct ast_filestream s,
off_t  sample_offset,
int  whence 
)
static

Definition at line 248 of file format_mp3.c.

References ast_filestream::_private, ast_filestream::f, max, min, mp3_private::offset, mp3_private::seek, and SEEK_FORCECUR.

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 }
void * _private
Definition: mod_format.h:124
#define SEEK_FORCECUR
Definition: file.h:51
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198

◆ mp3_squeue()

static int mp3_squeue ( struct ast_filestream s)
static

Definition at line 114 of file format_mp3.c.

References ast_filestream::_private, ast_log, mp3_private::dbuf, mp3_private::dbuflen, mp3_private::dbufoffset, errno, ast_filestream::f, LOG_WARNING, mp3_private::mp, MP3_DCACHE, MP3_SCACHE, mp3_private::sbuf, and mp3_private::sbuflen.

Referenced by mp3_queue().

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 }
struct mpstr mp
Definition: format_mp3.c:51
#define LOG_WARNING
Definition: logger.h:274
int dbufoffset
Definition: format_mp3.c:63
#define MP3_DCACHE
Definition: format_mp3.c:47
char dbuf[MP3_DCACHE]
Definition: format_mp3.c:55
#define ast_log
Definition: astobj2.c:42
void * _private
Definition: mod_format.h:124
char sbuf[MP3_SCACHE]
Definition: format_mp3.c:53
int errno
#define MP3_SCACHE
Definition: format_mp3.c:46

◆ mp3_tell()

static off_t mp3_tell ( struct ast_filestream s)
static

Definition at line 288 of file format_mp3.c.

References ast_filestream::_private, and mp3_private::offset.

289 {
290  struct mp3_private *p = s->_private;
291 
292  return p->offset/2;
293 }
void * _private
Definition: mod_format.h:124

◆ mp3_trunc()

static int mp3_trunc ( struct ast_filestream s)
static

Definition at line 281 of file format_mp3.c.

References ast_log, and LOG_ERROR.

282 {
283 
284  ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
285  return -1;
286 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285

◆ mp3_write()

static int mp3_write ( struct ast_filestream fs,
struct ast_frame f 
)
static

Definition at line 240 of file format_mp3.c.

References ast_log, and LOG_ERROR.

241 {
242  ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
243  return -1;
244 
245 }
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 324 of file format_mp3.c.

References ast_format_def_unregister(), AST_MODULE_INFO_STANDARD_EXTENDED(), ASTERISK_GPL_KEY, and name.

325 {
327 }
static const char name[]
Definition: format_mp3.c:68
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162

Variable Documentation

◆ mp3_f

struct ast_format_def mp3_f
static

Definition at line 300 of file format_mp3.c.

◆ name

const char name[] = "mp3"
static

Definition at line 68 of file format_mp3.c.

Referenced by unload_module().