Fri Jul 3 20:35:38 2009

Asterisk developer's documentation


format_mp3.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- A telephony toolkit for Linux.
00003  *
00004  * format_mp3.c
00005  * Anthony Minessale <anthmct@yahoo.com>
00006  *
00007  * Derived from other asterisk sound formats by
00008  * Mark Spencer <markster@linux-support.net>
00009  *
00010  * Thanks to mpglib from http://www.mpg123.org/
00011  * and Chris Stenton [jacs@gnome.co.uk]
00012  * for coding the ability to play stereo and non-8khz files
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License
00016  */
00017 
00018 #include <asterisk.h>
00019 
00020 #include "mp3/mpg123.h" 
00021 #include "mp3/mpglib.h" 
00022 #include <asterisk/lock.h>
00023 #include <asterisk/channel.h>
00024 #include <asterisk/file.h>
00025 #include <asterisk/logger.h>
00026 #include <asterisk/sched.h>
00027 #include <asterisk/module.h>
00028 #include <netinet/in.h>
00029 #include <arpa/inet.h>
00030 #include <stdlib.h>
00031 #include <sys/time.h>
00032 #include <stdio.h>
00033 #include <unistd.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #ifdef __linux__
00037 #include <endian.h>
00038 #else
00039 #include <machine/endian.h>
00040 #endif
00041 
00042 #define MP3_BUFLEN 320
00043 #define MP3_SCACHE 16384
00044 #define MP3_DCACHE 8192
00045 
00046 /* Based on format_wav.c */
00047 
00048 struct mp3_private {
00049    char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
00050    char empty;          /* Empty character */
00051    int lasttimeout;
00052    int maxlen;
00053    struct timeval last;
00054    struct mpstr mp;
00055    char sbuf[MP3_SCACHE];
00056    char dbuf[MP3_DCACHE];
00057    int buflen;
00058    int sbuflen;
00059    int dbuflen;
00060    int dbufoffset;
00061    int sbufoffset;
00062    int lastseek;
00063    int offset;
00064    long seek;
00065 };
00066 
00067 static char *name = "mp3";
00068 
00069 #define BLOCKSIZE 160
00070 #define OUTSCALE 4096
00071 
00072 #define GAIN -4      /* 2^GAIN is the multiple to increase the volume by */
00073 
00074 #if __BYTE_ORDER == __LITTLE_ENDIAN
00075 #define htoll(b) (b)
00076 #define htols(b) (b)
00077 #define ltohl(b) (b)
00078 #define ltohs(b) (b)
00079 #else
00080 #if __BYTE_ORDER == __BIG_ENDIAN
00081 #define htoll(b)  \
00082           (((((b)      ) & 0xFF) << 24) | \
00083           ((((b) >>  8) & 0xFF) << 16) | \
00084          ((((b) >> 16) & 0xFF) <<  8) | \
00085          ((((b) >> 24) & 0xFF)      ))
00086 #define htols(b) \
00087           (((((b)      ) & 0xFF) << 8) | \
00088          ((((b) >> 8) & 0xFF)      ))
00089 #define ltohl(b) htoll(b)
00090 #define ltohs(b) htols(b)
00091 #else
00092 #error "Endianess not defined"
00093 #endif
00094 #endif
00095 
00096 
00097 static int mp3_open(struct ast_filestream *s)
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 }
00112 
00113 
00114 static void mp3_close(struct ast_filestream *s)
00115 {
00116    struct mp3_private *p = s->_private;
00117    
00118    ExitMP3(&p->mp);
00119    return;
00120 }
00121 
00122 static int mp3_squeue(struct ast_filestream *s) 
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 }
00140 
00141 static int mp3_dqueue(struct ast_filestream *s) 
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 }
00152 
00153 static int mp3_queue(struct ast_filestream *s)
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 }
00196 
00197 static struct ast_frame *mp3_read(struct ast_filestream *s, int *whennext)
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 }
00244 
00245 
00246 static int mp3_write(struct ast_filestream *fs, struct ast_frame *f)
00247 {
00248    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00249    return -1;
00250 
00251 }
00252 
00253 
00254 static int mp3_seek(struct ast_filestream *s, off_t sample_offset, int whence)
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 }
00280 
00281 static int mp3_rewrite(struct ast_filestream *s, const char *comment) 
00282 {
00283    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00284    return -1;
00285 }
00286 
00287 static int mp3_trunc(struct ast_filestream *s) 
00288 {
00289 
00290    ast_log(LOG_ERROR,"I Can't write MP3 only read them.\n");
00291    return -1;
00292 }
00293 
00294 static off_t mp3_tell(struct ast_filestream *s)
00295 {
00296    struct mp3_private *p = s->_private;
00297    
00298    return p->offset/2;
00299 }
00300 
00301 static char *mp3_getcomment(struct ast_filestream *s)
00302 {
00303    return NULL;
00304 }
00305 
00306 static const struct ast_format mp3_f = {
00307    .name = "mp3",
00308    .exts = "mp3",
00309    .format = AST_FORMAT_SLINEAR,
00310    .open = mp3_open,
00311    .write = mp3_write,
00312    .rewrite = mp3_rewrite,
00313    .seek =  mp3_seek,
00314    .trunc = mp3_trunc,
00315    .tell =  mp3_tell,
00316    .read =  mp3_read,
00317    .close = mp3_close,
00318    .getcomment = mp3_getcomment,
00319    .buf_size = MP3_BUFLEN + AST_FRIENDLY_OFFSET,
00320    .desc_size = sizeof(struct mp3_private),
00321 };
00322 
00323 
00324 static int load_module(void)
00325 {
00326    InitMP3Constants();
00327    return ast_format_register(&mp3_f);
00328 }
00329 
00330 static int unload_module(void)
00331 {
00332    return ast_format_unregister(name);
00333 }  
00334 
00335 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "MP3 format [Any rate but 8000hz mono is optimal]");

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