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

Defines

#define BLOCKSIZE   160
#define GAIN   -4
#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.


Define Documentation

#define BLOCKSIZE   160

Definition at line 72 of file format_mp3.c.

#define GAIN   -4

Definition at line 75 of file format_mp3.c.

#define MP3_BUFLEN   320

Definition at line 47 of file format_mp3.c.

Referenced by mp3_read().

#define MP3_DCACHE   8192

Definition at line 49 of file format_mp3.c.

Referenced by mp3_dqueue(), and mp3_squeue().

#define MP3_SCACHE   16384

Definition at line 48 of file format_mp3.c.

Referenced by mp3_squeue().

#define OUTSCALE   4096

Definition at line 73 of file format_mp3.c.

Referenced by mp3_open(), and mp3_queue().


Function Documentation

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

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.

00318 {
00319    mp3_f.format = ast_format_slin;
00320    InitMP3Constants();
00321    return ast_format_def_register(&mp3_f);
00322 }

static void mp3_close ( struct ast_filestream s  )  [static]

Definition at line 108 of file format_mp3.c.

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

00109 {
00110    struct mp3_private *p = s->_private;
00111 
00112    ExitMP3(&p->mp);
00113    return;
00114 }

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().

00136 {
00137    struct mp3_private *p = s->_private;
00138    int res=0;
00139 
00140    if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
00141       p->sbuflen -= p->dbuflen;
00142       p->dbufoffset = 0;
00143    }
00144    return res;
00145 }

static char* mp3_getcomment ( struct ast_filestream s  )  [static]

Definition at line 295 of file format_mp3.c.

References NULL.

00296 {
00297    return NULL;
00298 }

static int mp3_open ( struct ast_filestream s  )  [static]

Definition at line 100 of file format_mp3.c.

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

00101 {
00102    struct mp3_private *p = s->_private;
00103    InitMP3(&p->mp, OUTSCALE);
00104    return 0;
00105 }

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().

00148 {
00149    struct mp3_private *p = s->_private;
00150    int res = 0, bytes = 0;
00151 
00152    if(p->seek) {
00153       ExitMP3(&p->mp);
00154       InitMP3(&p->mp, OUTSCALE);
00155       fseek(s->f, 0, SEEK_SET);
00156       p->sbuflen = p->dbuflen = p->offset = 0;
00157       while(p->offset < p->seek) {
00158          if(mp3_squeue(s))
00159             return -1;
00160          while(p->offset < p->seek && ((res = mp3_dqueue(s))) == MP3_OK) {
00161             for(bytes = 0 ; bytes < p->dbuflen ; bytes++) {
00162                p->dbufoffset++;
00163                p->offset++;
00164                if(p->offset >= p->seek)
00165                   break;
00166             }
00167          }
00168          if(res == MP3_ERR)
00169             return -1;
00170       }
00171 
00172       p->seek = 0;
00173       return 0;
00174    }
00175    if(p->dbuflen == 0) {
00176       if(p->sbuflen) {
00177          res = mp3_dqueue(s);
00178          if(res == MP3_ERR)
00179             return -1;
00180       }
00181       if(! p->sbuflen || res != MP3_OK) {
00182          if(mp3_squeue(s))
00183             return -1;
00184       }
00185 
00186    }
00187 
00188    return 0;
00189 }

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

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.

00192 {
00193 
00194    struct mp3_private *p = s->_private;
00195    int delay =0;
00196    int save=0;
00197 
00198    /* Pre-populate the buffer that holds audio to be returned (dbuf) */
00199    if (mp3_queue(s)) {
00200       return NULL;
00201    }
00202 
00203    if (p->dbuflen) {
00204       /* Read out what's waiting in dbuf */
00205       for (p->buflen = 0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
00206          s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[p->buflen + p->dbufoffset];
00207       }
00208       p->dbufoffset += p->buflen;
00209       p->dbuflen -= p->buflen;
00210    }
00211 
00212    if (p->buflen < MP3_BUFLEN) {
00213       /* dbuf didn't have enough, so reset dbuf, fill it back up and continue */
00214       p->dbuflen = p->dbufoffset = 0;
00215 
00216       if (mp3_queue(s)) {
00217          return NULL;
00218       }
00219 
00220       /* Make sure dbuf has enough to complete this read attempt */
00221       if (p->dbuflen >= (MP3_BUFLEN - p->buflen)) {
00222          for (save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
00223             s->buf[p->buflen + AST_FRIENDLY_OFFSET] = p->dbuf[(p->buflen - save) + p->dbufoffset];
00224          }
00225          p->dbufoffset += (MP3_BUFLEN - save);
00226          p->dbuflen -= (MP3_BUFLEN - save);
00227       }
00228 
00229    }
00230 
00231    p->offset += p->buflen;
00232    delay = p->buflen / 2;
00233    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, p->buflen);
00234    s->fr.samples = delay;
00235    *whennext = delay;
00236    return &s->fr;
00237 }

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.

00276 {
00277    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00278    return -1;
00279 }

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.

00249 {
00250    struct mp3_private *p = s->_private;
00251    off_t min,max,cur;
00252    long offset=0,samples;
00253    samples = sample_offset * 2;
00254 
00255    min = 0;
00256    fseek(s->f, 0, SEEK_END);
00257    max = ftell(s->f) * 100;
00258    cur = p->offset;
00259 
00260    if (whence == SEEK_SET)
00261       offset = samples + min;
00262    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00263       offset = samples + cur;
00264    else if (whence == SEEK_END)
00265       offset = max - samples;
00266    if (whence != SEEK_FORCECUR) {
00267       offset = (offset > max)?max:offset;
00268    }
00269 
00270    p->seek = offset;
00271    return fseek(s->f, offset, SEEK_SET);
00272 
00273 }

static int mp3_squeue ( struct ast_filestream s  )  [static]

Definition at line 116 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().

00117 {
00118    struct mp3_private *p = s->_private;
00119    int res=0;
00120 
00121    res = ftell(s->f);
00122    p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
00123    if(p->sbuflen < 0) {
00124       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", p->sbuflen, strerror(errno));
00125       return -1;
00126    }
00127    res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
00128    if(res != MP3_OK)
00129       return -1;
00130    p->sbuflen -= p->dbuflen;
00131    p->dbufoffset = 0;
00132    return 0;
00133 }

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.

00289 {
00290    struct mp3_private *p = s->_private;
00291 
00292    return p->offset/2;
00293 }

static int mp3_trunc ( struct ast_filestream s  )  [static]

Definition at line 281 of file format_mp3.c.

References ast_log, and LOG_ERROR.

00282 {
00283 
00284    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00285    return -1;
00286 }

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.

00241 {
00242    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00243    return -1;
00244 
00245 }

static int unload_module ( void   )  [static]

Definition at line 324 of file format_mp3.c.

References ast_format_def_unregister().

00325 {
00326    return ast_format_def_unregister(name);
00327 }


Variable Documentation

struct ast_format_def mp3_f [static]

Definition at line 300 of file format_mp3.c.

const char name[] = "mp3" [static]

Definition at line 70 of file format_mp3.c.


Generated on Thu Apr 16 06:31:06 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6