Thu Jul 9 13:40:34 2009

Asterisk developer's documentation


format_pcm.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Flat, binary, ulaw PCM file format.
00022  * \arg File name extension: pcm, ulaw, ul, mu
00023  * 
00024  * \ingroup formats
00025  */
00026  
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 143905 $")
00030 
00031 #include "asterisk/mod_format.h"
00032 #include "asterisk/module.h"
00033 #include "asterisk/endian.h"
00034 #include "asterisk/ulaw.h"
00035 #include "asterisk/alaw.h"
00036 
00037 #define BUF_SIZE 160    /* 160 bytes, and same number of samples */
00038 
00039 static char ulaw_silence[BUF_SIZE];
00040 static char alaw_silence[BUF_SIZE];
00041 
00042 /* #define REALTIME_WRITE */  /* XXX does it work at all ? */
00043 
00044 #ifdef REALTIME_WRITE
00045 struct pcm_desc {
00046    unsigned long start_time;
00047 };
00048 
00049 /* Returns time in msec since system boot. */
00050 static unsigned long get_time(void)
00051 {
00052    struct tms buf;
00053    clock_t cur;
00054 
00055    cur = times( &buf );
00056    if( cur < 0 ) {
00057       ast_log( LOG_WARNING, "Cannot get current time\n" );
00058       return 0;
00059    }
00060    return cur * 1000 / sysconf( _SC_CLK_TCK );
00061 }
00062 
00063 static int pcma_open(struct ast_filestream *s)
00064 {
00065    if (s->fmt->format == AST_FORMAT_ALAW)
00066       pd->starttime = get_time();
00067    return 0;
00068 }
00069 
00070 static int pcma_rewrite(struct ast_filestream *s, const char *comment)
00071 {
00072    return pcma_open(s);
00073 }
00074 #endif
00075 
00076 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
00077 {
00078    int res;
00079    
00080    /* Send a frame from the file to the appropriate channel */
00081 
00082    s->fr.frametype = AST_FRAME_VOICE;
00083    s->fr.subclass = s->fmt->format;
00084    s->fr.mallocd = 0;
00085    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
00086    if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
00087       if (res)
00088          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00089       return NULL;
00090    }
00091    s->fr.datalen = res;
00092    if (s->fmt->format == AST_FORMAT_G722)
00093       *whennext = s->fr.samples = res * 2;
00094    else
00095       *whennext = s->fr.samples = res;
00096    return &s->fr;
00097 }
00098 
00099 static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00100 {
00101    off_t cur, max, offset = 0;
00102    int ret = -1;  /* assume error */
00103 
00104    cur = ftello(fs->f);
00105    fseeko(fs->f, 0, SEEK_END);
00106    max = ftello(fs->f);
00107 
00108    switch (whence) {
00109    case SEEK_SET:
00110       offset = sample_offset;
00111       break;
00112    case SEEK_END:
00113       offset = max - sample_offset;
00114       break;
00115    case SEEK_CUR:
00116    case SEEK_FORCECUR:
00117       offset = cur + sample_offset;
00118       break;
00119    default:
00120       ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
00121       offset = sample_offset;
00122    }
00123    if (offset < 0) {
00124       ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
00125       offset = 0;
00126    }
00127    if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
00128       size_t left = offset - max;
00129       const char *src = (fs->fmt->format == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence;
00130 
00131       while (left) {
00132          size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
00133          if (written == -1)
00134             break;   /* error */
00135          left -= written;
00136       }
00137       ret = 0; /* successful */
00138    } else {
00139       if (offset > max) {
00140          ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
00141          offset = max;
00142       }
00143       ret = fseeko(fs->f, offset, SEEK_SET);
00144    }
00145    return ret;
00146 }
00147 
00148 static int pcm_trunc(struct ast_filestream *fs)
00149 {
00150    return ftruncate(fileno(fs->f), ftello(fs->f));
00151 }
00152 
00153 static off_t pcm_tell(struct ast_filestream *fs)
00154 {
00155    return ftello(fs->f);
00156 }
00157 
00158 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
00159 {
00160    int res;
00161 
00162    if (f->frametype != AST_FRAME_VOICE) {
00163       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00164       return -1;
00165    }
00166    if (f->subclass != fs->fmt->format) {
00167       ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass);
00168       return -1;
00169    }
00170 
00171 #ifdef REALTIME_WRITE
00172    if (s->fmt->format == AST_FORMAT_ALAW) {
00173       struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
00174       struct stat stat_buf;
00175       unsigned long cur_time = get_time();
00176       unsigned long fpos = ( cur_time - pd->start_time ) * 8;  /* 8 bytes per msec */
00177       /* Check if we have written to this position yet. If we have, then increment pos by one frame
00178       *  for some degree of protection against receiving packets in the same clock tick.
00179       */
00180       
00181       fstat(fileno(fs->f), &stat_buf );
00182       if (stat_buf.st_size > fpos )
00183          fpos += f->datalen;  /* Incrementing with the size of this current frame */
00184 
00185       if (stat_buf.st_size < fpos) {
00186          /* fill the gap with 0x55 rather than 0. */
00187          char buf[1024];
00188          unsigned long cur, to_write;
00189 
00190          cur = stat_buf.st_size;
00191          if (fseek(fs->f, cur, SEEK_SET) < 0) {
00192             ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
00193             return -1;
00194          }
00195          memset(buf, 0x55, 512);
00196          while (cur < fpos) {
00197             to_write = fpos - cur;
00198             if (to_write > sizeof(buf))
00199                to_write = sizeof(buf);
00200             fwrite(buf, 1, to_write, fs->f);
00201             cur += to_write;
00202          }
00203       }
00204 
00205       if (fseek(s->f, fpos, SEEK_SET) < 0) {
00206          ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
00207          return -1;
00208       }
00209    }
00210 #endif   /* REALTIME_WRITE */
00211    
00212    if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00213       ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00214       return -1;
00215    }
00216    return 0;
00217 }
00218 
00219 /* SUN .au support routines */
00220 
00221 #define AU_HEADER_SIZE     24
00222 #define AU_HEADER(var)     uint32_t var[6]
00223 
00224 #define AU_HDR_MAGIC_OFF   0
00225 #define AU_HDR_HDR_SIZE_OFF   1
00226 #define AU_HDR_DATA_SIZE_OFF  2
00227 #define AU_HDR_ENCODING_OFF   3
00228 #define AU_HDR_SAMPLE_RATE_OFF   4
00229 #define AU_HDR_CHANNELS_OFF   5
00230 
00231 #define AU_ENC_8BIT_ULAW   1
00232 
00233 #define AU_MAGIC 0x2e736e64
00234 #if __BYTE_ORDER == __BIG_ENDIAN
00235 #define htoll(b) (b)
00236 #define htols(b) (b)
00237 #define ltohl(b) (b)
00238 #define ltohs(b) (b)
00239 #else
00240 #if __BYTE_ORDER == __LITTLE_ENDIAN
00241 #define htoll(b)  \
00242           (((((b)      ) & 0xFF) << 24) | \
00243           ((((b) >>  8) & 0xFF) << 16) | \
00244          ((((b) >> 16) & 0xFF) <<  8) | \
00245          ((((b) >> 24) & 0xFF)      ))
00246 #define htols(b) \
00247           (((((b)      ) & 0xFF) << 8) | \
00248          ((((b) >> 8) & 0xFF)      ))
00249 #define ltohl(b) htoll(b)
00250 #define ltohs(b) htols(b)
00251 #else
00252 #error "Endianess not defined"
00253 #endif
00254 #endif
00255 
00256 static int check_header(FILE *f)
00257 {
00258    AU_HEADER(header);
00259    uint32_t magic;
00260    uint32_t hdr_size;
00261    uint32_t data_size;
00262    uint32_t encoding;
00263    uint32_t sample_rate;
00264    uint32_t channels;
00265 
00266    if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
00267       ast_log(LOG_WARNING, "Read failed (header)\n");
00268       return -1;
00269    }
00270    magic = ltohl(header[AU_HDR_MAGIC_OFF]);
00271    if (magic != (uint32_t) AU_MAGIC) {
00272       ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
00273    }
00274    hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
00275    if (hdr_size < AU_HEADER_SIZE) {
00276       hdr_size = AU_HEADER_SIZE;
00277    }
00278 /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
00279    encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
00280    if (encoding != AU_ENC_8BIT_ULAW) {
00281       ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
00282       return -1;
00283    }
00284    sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
00285    if (sample_rate != DEFAULT_SAMPLE_RATE) {
00286       ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
00287       return -1;
00288    }
00289    channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
00290    if (channels != 1) {
00291       ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
00292       return -1;
00293    }
00294    /* Skip to data */
00295    fseek(f, 0, SEEK_END);
00296    data_size = ftell(f) - hdr_size;
00297    if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
00298       ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
00299       return -1;
00300    }
00301    return data_size;
00302 }
00303 
00304 static int update_header(FILE *f)
00305 {
00306    off_t cur, end;
00307    uint32_t datalen;
00308    int bytes;
00309 
00310    cur = ftell(f);
00311    fseek(f, 0, SEEK_END);
00312    end = ftell(f);
00313    /* data starts 24 bytes in */
00314    bytes = end - AU_HEADER_SIZE;
00315    datalen = htoll(bytes);
00316 
00317    if (cur < 0) {
00318       ast_log(LOG_WARNING, "Unable to find our position\n");
00319       return -1;
00320    }
00321    if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
00322       ast_log(LOG_WARNING, "Unable to set our position\n");
00323       return -1;
00324    }
00325    if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
00326       ast_log(LOG_WARNING, "Unable to set write file size\n");
00327       return -1;
00328    }
00329    if (fseek(f, cur, SEEK_SET)) {
00330       ast_log(LOG_WARNING, "Unable to return to position\n");
00331       return -1;
00332    }
00333    return 0;
00334 }
00335 
00336 static int write_header(FILE *f)
00337 {
00338    AU_HEADER(header);
00339 
00340    header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
00341    header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
00342    header[AU_HDR_DATA_SIZE_OFF] = 0;
00343    header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
00344    header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
00345    header[AU_HDR_CHANNELS_OFF] = htoll(1);
00346 
00347    /* Write an au header, ignoring sizes which will be filled in later */
00348    fseek(f, 0, SEEK_SET);
00349    if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
00350       ast_log(LOG_WARNING, "Unable to write header\n");
00351       return -1;
00352    }
00353    return 0;
00354 }
00355 
00356 static int au_open(struct ast_filestream *s)
00357 {
00358    if (check_header(s->f) < 0)
00359       return -1;
00360    return 0;
00361 }
00362 
00363 static int au_rewrite(struct ast_filestream *s, const char *comment)
00364 {
00365    if (write_header(s->f))
00366       return -1;
00367    return 0;
00368 }
00369 
00370 /* XXX check this, probably incorrect */
00371 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00372 {
00373    off_t min, max, cur;
00374    long offset = 0, bytes;
00375 
00376    if (fs->fmt->format == AST_FORMAT_G722)
00377       bytes = sample_offset / 2;
00378    else
00379       bytes = sample_offset;
00380 
00381    min = AU_HEADER_SIZE;
00382    cur = ftello(fs->f);
00383    fseek(fs->f, 0, SEEK_END);
00384    max = ftello(fs->f);
00385 
00386    if (whence == SEEK_SET)
00387       offset = bytes + min;
00388    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00389       offset = bytes + cur;
00390    else if (whence == SEEK_END)
00391       offset = max - bytes;
00392 
00393    if (whence != SEEK_FORCECUR) {
00394       offset = (offset > max) ? max : offset;
00395    }
00396 
00397    /* always protect the header space. */
00398    offset = (offset < min) ? min : offset;
00399 
00400    return fseeko(fs->f, offset, SEEK_SET);
00401 }
00402 
00403 static int au_trunc(struct ast_filestream *fs)
00404 {
00405    if (ftruncate(fileno(fs->f), ftell(fs->f)))
00406       return -1;
00407    return update_header(fs->f);
00408 }
00409 
00410 static off_t au_tell(struct ast_filestream *fs)
00411 {
00412    off_t offset = ftello(fs->f);
00413    return offset - AU_HEADER_SIZE;
00414 }
00415 
00416 static const struct ast_format alaw_f = {
00417    .name = "alaw",
00418    .exts = "alaw|al",
00419    .format = AST_FORMAT_ALAW,
00420    .write = pcm_write,
00421    .seek = pcm_seek,
00422    .trunc = pcm_trunc,
00423    .tell = pcm_tell,
00424    .read = pcm_read,
00425    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00426 #ifdef REALTIME_WRITE
00427    .open = pcma_open,
00428    .rewrite = pcma_rewrite,
00429    .desc_size = sizeof(struct pcm_desc),
00430 #endif
00431 };
00432 
00433 static const struct ast_format pcm_f = {
00434    .name = "pcm",
00435    .exts = "pcm|ulaw|ul|mu",
00436    .format = AST_FORMAT_ULAW,
00437    .write = pcm_write,
00438    .seek = pcm_seek,
00439    .trunc = pcm_trunc,
00440    .tell = pcm_tell,
00441    .read = pcm_read,
00442    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00443 };
00444 
00445 static const struct ast_format g722_f = {
00446    .name = "g722",
00447    .exts = "g722",
00448    .format = AST_FORMAT_G722,
00449    .write = pcm_write,
00450    .seek = pcm_seek,
00451    .trunc = pcm_trunc,
00452    .tell = pcm_tell,
00453    .read = pcm_read,
00454    .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
00455 };
00456 
00457 static const struct ast_format au_f = {
00458    .name = "au",
00459    .exts = "au",
00460    .format = AST_FORMAT_ULAW,
00461    .open = au_open,
00462    .rewrite = au_rewrite,
00463    .write = pcm_write,
00464    .seek = au_seek,
00465    .trunc = au_trunc,
00466    .tell = au_tell,
00467    .read = pcm_read,
00468    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,  /* this many shorts */
00469 };
00470 
00471 static int load_module(void)
00472 {
00473    int index;
00474 
00475    /* XXX better init ? */
00476    for (index = 0; index < ARRAY_LEN(ulaw_silence); index++)
00477       ulaw_silence[index] = AST_LIN2MU(0);
00478    for (index = 0; index < ARRAY_LEN(alaw_silence); index++)
00479       alaw_silence[index] = AST_LIN2A(0);
00480 
00481    if ( ast_format_register(&pcm_f)
00482       || ast_format_register(&alaw_f)
00483       || ast_format_register(&au_f)
00484       || ast_format_register(&g722_f) )
00485       return AST_MODULE_LOAD_FAILURE;
00486    return AST_MODULE_LOAD_SUCCESS;
00487 }
00488 
00489 static int unload_module(void)
00490 {
00491    return ast_format_unregister(pcm_f.name)
00492       || ast_format_unregister(alaw_f.name)
00493       || ast_format_unregister(au_f.name)
00494       || ast_format_unregister(g722_f.name);
00495 }  
00496 
00497 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz");

Generated on Thu Jul 9 13:40:34 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7