Fri May 26 01:45:34 2006

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 #include <unistd.h>
00027 #include <netinet/in.h>
00028 #include <arpa/inet.h>
00029 #include <stdlib.h>
00030 #include <sys/time.h>
00031 #include <stdio.h>
00032 #include <errno.h>
00033 #include <string.h>
00034 
00035 #include "asterisk.h"
00036 
00037 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
00038 
00039 #include "asterisk/lock.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/file.h"
00042 #include "asterisk/logger.h"
00043 #include "asterisk/sched.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/endian.h"
00046 
00047 #include "msgsm.h"
00048 
00049 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
00050 
00051 /* Portions of the conversion code are by guido@sienanet.it */
00052 
00053 /* silent gsm frame */
00054 /* begin binary data: */
00055 char gsm_silence[] = /* 33 */
00056 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
00057 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
00058 ,0x92,0x49,0x24};
00059 /* end binary data. size = 33 bytes */
00060 
00061 struct ast_filestream {
00062    void *reserved[AST_RESERVED_POINTERS];
00063    /* Believe it or not, we must decode/recode to account for the
00064       weird MS format */
00065    /* This is what a filestream means to us */
00066    FILE *f; /* Descriptor */
00067    struct ast_frame fr;          /* Frame information */
00068    char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
00069    char empty;                   /* Empty character */
00070    unsigned char gsm[66];           /* Two Real GSM Frames */
00071 };
00072 
00073 
00074 AST_MUTEX_DEFINE_STATIC(gsm_lock);
00075 static int glistcnt = 0;
00076 
00077 static char *name = "gsm";
00078 static char *desc = "Raw GSM data";
00079 static char *exts = "gsm";
00080 
00081 static struct ast_filestream *gsm_open(FILE *f)
00082 {
00083    /* We don't have any header to read or anything really, but
00084       if we did, it would go here.  We also might want to check
00085       and be sure it's a valid file.  */
00086    struct ast_filestream *tmp;
00087    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
00088       memset(tmp, 0, sizeof(struct ast_filestream));
00089       if (ast_mutex_lock(&gsm_lock)) {
00090          ast_log(LOG_WARNING, "Unable to lock gsm list\n");
00091          free(tmp);
00092          return NULL;
00093       }
00094       tmp->f = f;
00095       tmp->fr.data = tmp->gsm;
00096       tmp->fr.frametype = AST_FRAME_VOICE;
00097       tmp->fr.subclass = AST_FORMAT_GSM;
00098       /* datalen will vary for each frame */
00099       tmp->fr.src = name;
00100       tmp->fr.mallocd = 0;
00101       glistcnt++;
00102       ast_mutex_unlock(&gsm_lock);
00103       ast_update_use_count();
00104    }
00105    return tmp;
00106 }
00107 
00108 static struct ast_filestream *gsm_rewrite(FILE *f, const char *comment)
00109 {
00110    /* We don't have any header to read or anything really, but
00111       if we did, it would go here.  We also might want to check
00112       and be sure it's a valid file.  */
00113    struct ast_filestream *tmp;
00114    if ((tmp = malloc(sizeof(struct ast_filestream)))) {
00115       memset(tmp, 0, sizeof(struct ast_filestream));
00116       if (ast_mutex_lock(&gsm_lock)) {
00117          ast_log(LOG_WARNING, "Unable to lock gsm list\n");
00118          free(tmp);
00119          return NULL;
00120       }
00121       tmp->f = f;
00122       glistcnt++;
00123       ast_mutex_unlock(&gsm_lock);
00124       ast_update_use_count();
00125    } else
00126       ast_log(LOG_WARNING, "Out of memory\n");
00127    return tmp;
00128 }
00129 
00130 static void gsm_close(struct ast_filestream *s)
00131 {
00132    if (ast_mutex_lock(&gsm_lock)) {
00133       ast_log(LOG_WARNING, "Unable to lock gsm list\n");
00134       return;
00135    }
00136    glistcnt--;
00137    ast_mutex_unlock(&gsm_lock);
00138    ast_update_use_count();
00139    fclose(s->f);
00140    free(s);
00141 }
00142 
00143 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
00144 {
00145    int res;
00146    s->fr.frametype = AST_FRAME_VOICE;
00147    s->fr.subclass = AST_FORMAT_GSM;
00148    s->fr.offset = AST_FRIENDLY_OFFSET;
00149    s->fr.samples = 160;
00150    s->fr.datalen = 33;
00151    s->fr.mallocd = 0;
00152    s->fr.data = s->gsm;
00153    if ((res = fread(s->gsm, 1, 33, s->f)) != 33) {
00154       if (res)
00155          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00156       return NULL;
00157    }
00158    *whennext = 160;
00159    return &s->fr;
00160 }
00161 
00162 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
00163 {
00164    int res;
00165    unsigned char gsm[66];
00166    if (f->frametype != AST_FRAME_VOICE) {
00167       ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00168       return -1;
00169    }
00170    if (f->subclass != AST_FORMAT_GSM) {
00171       ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
00172       return -1;
00173    }
00174    if (!(f->datalen % 65)) {
00175       /* This is in MSGSM format, need to be converted */
00176       int len=0;
00177       while(len < f->datalen) {
00178          conv65(f->data + len, gsm);
00179          if ((res = fwrite(gsm, 1, 66, fs->f)) != 66) {
00180             ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
00181             return -1;
00182          }
00183          len += 65;
00184       }
00185    } else {
00186       if (f->datalen % 33) {
00187          ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
00188          return -1;
00189       }
00190       if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00191             ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
00192             return -1;
00193       }
00194    }
00195    return 0;
00196 }
00197 
00198 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
00199 {
00200    off_t offset=0,min,cur,max,distance;
00201    
00202    min = 0;
00203    cur = ftell(fs->f);
00204    fseek(fs->f, 0, SEEK_END);
00205    max = ftell(fs->f);
00206    /* have to fudge to frame here, so not fully to sample */
00207    distance = (sample_offset/160) * 33;
00208    if(whence == SEEK_SET)
00209       offset = distance;
00210    else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
00211       offset = distance + cur;
00212    else if(whence == SEEK_END)
00213       offset = max - distance;
00214    /* Always protect against seeking past the begining. */
00215    offset = (offset < min)?min:offset;
00216    if (whence != SEEK_FORCECUR) {
00217       offset = (offset > max)?max:offset;
00218    } else if (offset > max) {
00219       int i;
00220       fseek(fs->f, 0, SEEK_END);
00221       for (i=0; i< (offset - max) / 33; i++) {
00222          fwrite(gsm_silence, 1, 33, fs->f);
00223       }
00224    }
00225    return fseek(fs->f, offset, SEEK_SET);
00226 }
00227 
00228 static int gsm_trunc(struct ast_filestream *fs)
00229 {
00230    return ftruncate(fileno(fs->f), ftell(fs->f));
00231 }
00232 
00233 static long gsm_tell(struct ast_filestream *fs)
00234 {
00235    off_t offset;
00236    offset = ftell(fs->f);
00237    return (offset/33)*160;
00238 }
00239 
00240 static char *gsm_getcomment(struct ast_filestream *s)
00241 {
00242    return NULL;
00243 }
00244 
00245 int load_module()
00246 {
00247    return ast_format_register(name, exts, AST_FORMAT_GSM,
00248                         gsm_open,
00249                         gsm_rewrite,
00250                         gsm_write,
00251                         gsm_seek,
00252                         gsm_trunc,
00253                         gsm_tell,
00254                         gsm_read,
00255                         gsm_close,
00256                         gsm_getcomment);
00257                         
00258                         
00259 }
00260 
00261 int unload_module()
00262 {
00263    return ast_format_unregister(name);
00264 }  
00265 
00266 int usecount()
00267 {
00268    return glistcnt;
00269 }
00270 
00271 char *description()
00272 {
00273    return desc;
00274 }
00275 
00276 
00277 char *key()
00278 {
00279    return ASTERISK_GPL_KEY;
00280 }

Generated on Fri May 26 01:45:34 2006 for Asterisk - the Open Source PBX by  doxygen 1.4.6