format_g723.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 /*! 
00020  * \file
00021  *
00022  * \brief Old-style G.723.1 frame/timestamp format.
00023  * 
00024  * \arg Extensions: g723, g723sf
00025  * \ingroup formats
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031  
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00035 
00036 #include "asterisk/mod_format.h"
00037 #include "asterisk/module.h"
00038 #include "asterisk/format_cache.h"
00039 
00040 #define G723_MAX_SIZE 1024
00041 
00042 static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
00043 {
00044    unsigned short size;
00045    int res;
00046    int delay;
00047    /* Read the delay for the next packet, and schedule again if necessary */
00048    /* XXX is this ignored ? */
00049    if (fread(&delay, 1, 4, s->f) == 4) 
00050       delay = ntohl(delay);
00051    else
00052       delay = -1;
00053    if (fread(&size, 1, 2, s->f) != 2) {
00054       /* Out of data, or the file is no longer valid.  In any case
00055          go ahead and stop the stream */
00056       return NULL;
00057    }
00058    /* Looks like we have a frame to read from here */
00059    size = ntohs(size);
00060    if (size > G723_MAX_SIZE) {
00061       ast_log(LOG_WARNING, "Size %d is invalid\n", size);
00062       /* The file is apparently no longer any good, as we
00063          shouldn't ever get frames even close to this 
00064          size.  */
00065       return NULL;
00066    }
00067    /* Read the data into the buffer */
00068    AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size);
00069    if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) {
00070       ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
00071       return NULL;
00072    }
00073    *whennext = s->fr.samples = 240;
00074    return &s->fr;
00075 }
00076 
00077 static int g723_write(struct ast_filestream *s, struct ast_frame *f)
00078 {
00079    uint32_t delay;
00080    uint16_t size;
00081    int res;
00082    /* XXX there used to be a check s->fr means a read stream */
00083    delay = 0;
00084    if (f->datalen <= 0) {
00085       ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
00086       return 0;
00087    }
00088    if ((res = fwrite(&delay, 1, 4, s->f)) != 4) {
00089       ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
00090       return -1;
00091    }
00092    size = htons(f->datalen);
00093    if ((res = fwrite(&size, 1, 2, s->f)) != 2) {
00094       ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
00095       return -1;
00096    }
00097    if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
00098       ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
00099       return -1;
00100    }  
00101    return 0;
00102 }
00103 
00104 static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00105 {
00106    return -1;
00107 }
00108 
00109 static int g723_trunc(struct ast_filestream *fs)
00110 {
00111    int fd;
00112    off_t cur;
00113 
00114    if ((fd = fileno(fs->f)) < 0) {
00115       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g723 filestream %p: %s\n", fs, strerror(errno));
00116       return -1;
00117    }
00118    if ((cur = ftello(fs->f)) < 0) {
00119       ast_log(AST_LOG_WARNING, "Unable to determine current position in g723 filestream %p: %s\n", fs, strerror(errno));
00120       return -1;
00121    }
00122    /* Truncate file to current length */
00123    return ftruncate(fd, cur);
00124 }
00125 
00126 static off_t g723_tell(struct ast_filestream *fs)
00127 {
00128    return -1;
00129 }
00130 
00131 static struct ast_format_def g723_1_f = {
00132    .name = "g723sf",
00133    .exts = "g723|g723sf",
00134    .write = g723_write,
00135    .seek =  g723_seek,
00136    .trunc = g723_trunc,
00137    .tell =  g723_tell,
00138    .read =  g723_read,
00139    .buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET,
00140 };
00141 
00142 static int load_module(void)
00143 {
00144    g723_1_f.format = ast_format_g723;
00145 
00146    if (ast_format_def_register(&g723_1_f))
00147       return AST_MODULE_LOAD_FAILURE;
00148    return AST_MODULE_LOAD_SUCCESS;
00149 }
00150 
00151 static int unload_module(void)
00152 {
00153    return ast_format_def_unregister(g723_1_f.name);
00154 }
00155 
00156 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "G.723.1 Simple Timestamp File Format",
00157    .support_level = AST_MODULE_SUPPORT_CORE,
00158    .load = load_module,
00159    .unload = unload_module,
00160    .load_pri = AST_MODPRI_APP_DEPEND
00161 );

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