Fri Jul 3 20:38:01 2009

Asterisk developer's documentation


format_mp3.c File Reference

#include <asterisk.h>
#include "mp3/mpg123.h"
#include "mp3/mpglib.h"
#include <asterisk/lock.h>
#include <asterisk/channel.h>
#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/sched.h>
#include <asterisk/module.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <machine/endian.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 htoll(b)   (b)
#define htols(b)   (b)
#define ltohl(b)   (b)
#define ltohs(b)   (b)
#define MP3_BUFLEN   320
#define MP3_DCACHE   8192
#define MP3_SCACHE   16384
#define OUTSCALE   4096

Functions

static void __reg_module (void)
static void __unreg_module (void)
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_frame * mp3_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_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "MP3 format [Any rate but 8000hz mono is optimal]" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, }
static const struct
ast_module_info * 
ast_module_info = &__mod_info
static struct ast_format mp3_f
static char * name = "mp3"


Define Documentation

#define BLOCKSIZE   160

Definition at line 69 of file format_mp3.c.

#define GAIN   -4

Definition at line 72 of file format_mp3.c.

#define htoll ( b   )     (b)

Definition at line 75 of file format_mp3.c.

Referenced by update_header(), and write_header().

#define htols ( b   )     (b)

Definition at line 76 of file format_mp3.c.

Referenced by write_header().

#define ltohl ( b   )     (b)

Definition at line 77 of file format_mp3.c.

Referenced by check_header().

#define ltohs ( b   )     (b)

Definition at line 78 of file format_mp3.c.

Referenced by check_header().

#define MP3_BUFLEN   320

Definition at line 42 of file format_mp3.c.

Referenced by mp3_read().

#define MP3_DCACHE   8192

Definition at line 44 of file format_mp3.c.

Referenced by mp3_dqueue(), and mp3_squeue().

#define MP3_SCACHE   16384

Definition at line 43 of file format_mp3.c.

Referenced by mp3_squeue().

#define OUTSCALE   4096

Definition at line 70 of file format_mp3.c.

Referenced by mp3_open(), and mp3_queue().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 335 of file format_mp3.c.

static void __unreg_module ( void   )  [static]

Definition at line 335 of file format_mp3.c.

static int load_module ( void   )  [static]

Definition at line 324 of file format_mp3.c.

References ast_format_register.

00325 {
00326    InitMP3Constants();
00327    return ast_format_register(&mp3_f);
00328 }

static void mp3_close ( struct ast_filestream *  s  )  [static]

Definition at line 114 of file format_mp3.c.

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

00115 {
00116    struct mp3_private *p = s->_private;
00117    
00118    ExitMP3(&p->mp);
00119    return;
00120 }

static int mp3_dqueue ( struct ast_filestream *  s  )  [static]

Definition at line 141 of file format_mp3.c.

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

Referenced by mp3_queue().

00142 {
00143    struct mp3_private *p = s->_private;
00144    int res=0;
00145    
00146    if((res = decodeMP3(&p->mp,NULL,0,p->dbuf,MP3_DCACHE,&p->dbuflen)) == MP3_OK) {
00147       p->sbuflen -= p->dbuflen;
00148       p->dbufoffset = 0;
00149    }
00150    return res;
00151 }

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

Definition at line 301 of file format_mp3.c.

00302 {
00303    return NULL;
00304 }

static int mp3_open ( struct ast_filestream *  s  )  [static]

Definition at line 97 of file format_mp3.c.

References ast_filestream::_private, AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_filestream::buf, ast_frame::data, mp3_private::dbuflen, ast_filestream::fr, ast_frame::frametype, ast_frame::mallocd, mp3_private::mp, name, mp3_private::offset, OUTSCALE, ast_frame::src, and ast_frame::subclass.

00098 {
00099    struct mp3_private *p = s->_private;
00100    
00101    InitMP3(&p->mp, OUTSCALE);
00102    p->dbuflen = 0;
00103    s->fr.data = s->buf;
00104    s->fr.frametype = AST_FRAME_VOICE;
00105    s->fr.subclass = AST_FORMAT_SLINEAR;
00106    /* datalen will vary for each frame */
00107    s->fr.src = name;
00108    s->fr.mallocd = 0;
00109    p->offset = 0;
00110    return 0;
00111 }

static int mp3_queue ( struct ast_filestream *  s  )  [static]

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

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

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

Definition at line 197 of file format_mp3.c.

References ast_filestream::_private, AST_FORMAT_SLINEAR, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_filestream::buf, mp3_private::buflen, ast_frame::data, ast_frame::datalen, mp3_private::dbuf, mp3_private::dbuflen, mp3_private::dbufoffset, ast_filestream::fr, ast_frame::frametype, ast_frame::mallocd, MP3_BUFLEN, mp3_queue(), ast_frame::offset, mp3_private::offset, ast_frame::samples, mp3_private::sbufoffset, and ast_frame::subclass.

00198 {
00199 
00200    struct mp3_private *p = s->_private;
00201    int delay =0;
00202    int save=0;
00203 
00204    /* Send a frame from the file to the appropriate channel */
00205 
00206    if(mp3_queue(s))
00207       return NULL;
00208 
00209    if(p->dbuflen) {
00210       for(p->buflen=0; p->buflen < MP3_BUFLEN && p->buflen < p->dbuflen; p->buflen++) {
00211          s->buf[p->buflen] = p->dbuf[p->buflen+p->dbufoffset];
00212          p->sbufoffset++;
00213       }
00214       p->dbufoffset += p->buflen;
00215       p->dbuflen -= p->buflen;
00216 
00217       if(p->buflen < MP3_BUFLEN) {
00218          if(mp3_queue(s))
00219             return NULL;
00220 
00221          for(save = p->buflen; p->buflen < MP3_BUFLEN; p->buflen++) {
00222             s->buf[p->buflen] = p->dbuf[(p->buflen-save)+p->dbufoffset];
00223             p->sbufoffset++;
00224          }
00225          p->dbufoffset += (MP3_BUFLEN - save);
00226          p->dbuflen -= (MP3_BUFLEN - save);
00227 
00228       } 
00229 
00230    }
00231    
00232    p->offset += p->buflen;
00233    delay = p->buflen/2;
00234    s->fr.frametype = AST_FRAME_VOICE;
00235    s->fr.subclass = AST_FORMAT_SLINEAR;
00236    s->fr.offset = AST_FRIENDLY_OFFSET;
00237    s->fr.datalen = p->buflen;
00238    s->fr.data = s->buf;
00239    s->fr.mallocd = 0;
00240    s->fr.samples = delay;
00241    *whennext = delay;
00242    return &s->fr;
00243 }

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

Definition at line 281 of file format_mp3.c.

References ast_log(), and LOG_ERROR.

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

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

Definition at line 254 of file format_mp3.c.

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

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

static int mp3_squeue ( struct ast_filestream *  s  )  [static]

Definition at line 122 of file format_mp3.c.

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

Referenced by mp3_queue().

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

static off_t mp3_tell ( struct ast_filestream *  s  )  [static]

Definition at line 294 of file format_mp3.c.

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

00295 {
00296    struct mp3_private *p = s->_private;
00297    
00298    return p->offset/2;
00299 }

static int mp3_trunc ( struct ast_filestream *  s  )  [static]

Definition at line 287 of file format_mp3.c.

References ast_log(), and LOG_ERROR.

00288 {
00289 
00290    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00291    return -1;
00292 }

static int mp3_write ( struct ast_filestream *  fs,
struct ast_frame *  f 
) [static]

Definition at line 246 of file format_mp3.c.

References ast_log(), and LOG_ERROR.

00247 {
00248    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00249    return -1;
00250 
00251 }

static int unload_module ( void   )  [static]

Definition at line 330 of file format_mp3.c.

References ast_format_unregister(), and name.

00331 {
00332    return ast_format_unregister(name);
00333 }  


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "MP3 format [Any rate but 8000hz mono is optimal]" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static]

Definition at line 335 of file format_mp3.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 335 of file format_mp3.c.

struct ast_format mp3_f [static]

Definition at line 306 of file format_mp3.c.

char* name = "mp3" [static]

Definition at line 67 of file format_mp3.c.


Generated on Fri Jul 3 20:38:01 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.9