format_g729.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, 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 Save to raw, headerless G729 data.
00022  * \note This is not an encoder/decoder. The codec fo g729 is only
00023  * available with a commercial license from Digium, due to patent
00024  * restrictions. Check http://www.digium.com for information.
00025  * \arg Extensions: g729 
00026  * \ingroup formats
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031  ***/
00032  
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00036 
00037 #include "asterisk/mod_format.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/endian.h"
00040 #include "asterisk/format_cache.h"
00041 
00042 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
00043 
00044 /* Portions of the conversion code are by guido@sienanet.it */
00045 
00046 #define  BUF_SIZE 20 /* two G729 frames */
00047 #define  G729A_SAMPLES  160
00048 
00049 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
00050 {
00051    int res;
00052    /* Send a frame from the file to the appropriate channel */
00053    s->fr.samples = G729A_SAMPLES;
00054    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
00055    if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00056       if (res && (res != 10)) /* XXX what for ? */
00057          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00058       return NULL;
00059    }
00060    *whennext = s->fr.samples;
00061    return &s->fr;
00062 }
00063 
00064 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
00065 {
00066    int res;
00067 
00068    if (f->datalen % 10) {
00069       ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
00070       return -1;
00071    }
00072    if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00073          ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
00074          return -1;
00075    }
00076    return 0;
00077 }
00078 
00079 static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00080 {
00081    long bytes;
00082    off_t min,cur,max,offset=0;
00083    min = 0;
00084    cur = ftello(fs->f);
00085    fseeko(fs->f, 0, SEEK_END);
00086    max = ftello(fs->f);
00087    
00088    bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
00089    if (whence == SEEK_SET)
00090       offset = bytes;
00091    else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00092       offset = cur + bytes;
00093    else if (whence == SEEK_END)
00094       offset = max - bytes;
00095    if (whence != SEEK_FORCECUR) {
00096       offset = (offset > max)?max:offset;
00097    }
00098    /* protect against seeking beyond begining. */
00099    offset = (offset < min)?min:offset;
00100    if (fseeko(fs->f, offset, SEEK_SET) < 0)
00101       return -1;
00102    return 0;
00103 }
00104 
00105 static int g729_trunc(struct ast_filestream *fs)
00106 {
00107    int fd;
00108    off_t cur;
00109 
00110    if ((fd = fileno(fs->f)) < 0) {
00111       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno));
00112       return -1;
00113    }
00114    if ((cur = ftello(fs->f)) < 0) {
00115       ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno));
00116       return -1;
00117    }
00118    /* Truncate file to current length */
00119    return ftruncate(fd, cur);
00120 }
00121 
00122 static off_t g729_tell(struct ast_filestream *fs)
00123 {
00124    off_t offset = ftello(fs->f);
00125    return (offset/BUF_SIZE)*G729A_SAMPLES;
00126 }
00127 
00128 static struct ast_format_def g729_f = {
00129    .name = "g729",
00130    .exts = "g729",
00131    .write = g729_write,
00132    .seek = g729_seek,
00133    .trunc = g729_trunc,
00134    .tell = g729_tell,
00135    .read = g729_read,
00136    .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00137 };
00138 
00139 static int load_module(void)
00140 {
00141    g729_f.format = ast_format_g729;
00142    if (ast_format_def_register(&g729_f))
00143       return AST_MODULE_LOAD_FAILURE;
00144    return AST_MODULE_LOAD_SUCCESS;
00145 }
00146 
00147 static int unload_module(void)
00148 {
00149    return ast_format_def_unregister(g729_f.name);
00150 }
00151 
00152 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.729 data",
00153    .support_level = AST_MODULE_SUPPORT_CORE,
00154    .load = load_module,
00155    .unload = unload_module,
00156    .load_pri = AST_MODPRI_APP_DEPEND
00157 );

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