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

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