Sat Mar 10 01:54:17 2012

Asterisk developer's documentation


format_gsm.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 GSM data.
00022  * \arg File name extension: gsm
00023  * \ingroup formats
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029  
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00033 
00034 #include "asterisk/mod_format.h"
00035 #include "asterisk/module.h"
00036 #include "asterisk/endian.h"
00037 
00038 #include "msgsm.h"
00039 
00040 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
00041 
00042 /* Portions of the conversion code are by guido@sienanet.it */
00043 
00044 #define  GSM_FRAME_SIZE 33
00045 #define  GSM_SAMPLES 160
00046 
00047 /* silent gsm frame */
00048 /* begin binary data: */
00049 static const char gsm_silence[] = /* 33 */
00050 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
00051 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
00052 ,0x92,0x49,0x24};
00053 /* end binary data. size = 33 bytes */
00054 
00055 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
00056 {
00057    int res;
00058 
00059    s->fr.frametype = AST_FRAME_VOICE;
00060    s->fr.subclass.codec = AST_FORMAT_GSM;
00061    AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
00062    s->fr.mallocd = 0;
00063    if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
00064       if (res)
00065          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00066       return NULL;
00067    }
00068    *whennext = s->fr.samples = GSM_SAMPLES;
00069    return &s->fr;
00070 }
00071 
00072 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
00073 {
00074    int res;
00075    unsigned char gsm[2*GSM_FRAME_SIZE];
00076 
00077    if (f->frametype != AST_FRAME_VOICE) {
00078       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00079       return -1;
00080    }
00081    if (f->subclass.codec != AST_FORMAT_GSM) {
00082       ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(f->subclass.codec));
00083       return -1;
00084    }
00085    if (!(f->datalen % 65)) {
00086       /* This is in MSGSM format, need to be converted */
00087       int len=0;
00088       while(len < f->datalen) {
00089          conv65(f->data.ptr + len, gsm);
00090          if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
00091             ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
00092             return -1;
00093          }
00094          len += 65;
00095       }
00096    } else {
00097       if (f->datalen % GSM_FRAME_SIZE) {
00098          ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
00099          return -1;
00100       }
00101       if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00102             ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
00103             return -1;
00104       }
00105    }
00106    return 0;
00107 }
00108 
00109 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00110 {
00111    off_t offset=0,min,cur,max,distance;
00112    
00113    min = 0;
00114    cur = ftello(fs->f);
00115    fseeko(fs->f, 0, SEEK_END);
00116    max = ftello(fs->f);
00117    /* have to fudge to frame here, so not fully to sample */
00118    distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
00119    if(whence == SEEK_SET)
00120       offset = distance;
00121    else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
00122       offset = distance + cur;
00123    else if(whence == SEEK_END)
00124       offset = max - distance;
00125    /* Always protect against seeking past the begining. */
00126    offset = (offset < min)?min:offset;
00127    if (whence != SEEK_FORCECUR) {
00128       offset = (offset > max)?max:offset;
00129    } else if (offset > max) {
00130       int i;
00131       fseeko(fs->f, 0, SEEK_END);
00132       for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
00133          if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
00134             ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
00135          }
00136       }
00137    }
00138    return fseeko(fs->f, offset, SEEK_SET);
00139 }
00140 
00141 static int gsm_trunc(struct ast_filestream *fs)
00142 {
00143    return ftruncate(fileno(fs->f), ftello(fs->f));
00144 }
00145 
00146 static off_t gsm_tell(struct ast_filestream *fs)
00147 {
00148    off_t offset = ftello(fs->f);
00149    return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
00150 }
00151 
00152 static const struct ast_format gsm_f = {
00153    .name = "gsm",
00154    .exts = "gsm",
00155    .format = AST_FORMAT_GSM,
00156    .write = gsm_write,
00157    .seek =  gsm_seek,
00158    .trunc = gsm_trunc,
00159    .tell =  gsm_tell,
00160    .read =  gsm_read,
00161    .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET,   /* 2 gsm frames */
00162 };
00163 
00164 static int load_module(void)
00165 {
00166    if (ast_format_register(&gsm_f))
00167       return AST_MODULE_LOAD_FAILURE;
00168    return AST_MODULE_LOAD_SUCCESS;
00169 }
00170 
00171 static int unload_module(void)
00172 {
00173    return ast_format_unregister(gsm_f.name);
00174 }
00175 
00176 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
00177    .load = load_module,
00178    .unload = unload_module,
00179    .load_pri = AST_MODPRI_APP_DEPEND
00180 );

Generated on Sat Mar 10 01:54:17 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7