Sat Mar 10 01:55:23 2012

Asterisk developer's documentation


format_gsm.c File Reference

Save to raw, headerless GSM data. More...

#include "asterisk.h"
#include "asterisk/mod_format.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"
#include "msgsm.h"

Go to the source code of this file.

Defines

#define GSM_FRAME_SIZE   33
#define GSM_SAMPLES   160

Functions

static void __reg_module (void)
static void __unreg_module (void)
static struct ast_framegsm_read (struct ast_filestream *s, int *whennext)
static int gsm_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
static off_t gsm_tell (struct ast_filestream *fs)
static int gsm_trunc (struct ast_filestream *fs)
static int gsm_write (struct ast_filestream *fs, struct ast_frame *f)
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Raw GSM data" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "88eaa8f5c1bd988bedd71113385e0886" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND }
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_format gsm_f
static const char gsm_silence []


Detailed Description

Save to raw, headerless GSM data.

Definition in file format_gsm.c.


Define Documentation

#define GSM_FRAME_SIZE   33

Definition at line 44 of file format_gsm.c.

Referenced by gsm_read(), gsm_seek(), gsm_tell(), gsm_write(), wav_read(), and wav_write().

#define GSM_SAMPLES   160

Definition at line 45 of file format_gsm.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 180 of file format_gsm.c.

static void __unreg_module ( void   )  [static]

Definition at line 180 of file format_gsm.c.

static struct ast_frame* gsm_read ( struct ast_filestream s,
int *  whennext 
) [static]

Definition at line 55 of file format_gsm.c.

References AST_FORMAT_GSM, AST_FRAME_SET_BUFFER, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_filestream::buf, ast_frame_subclass::codec, ast_frame::data, errno, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, ast_frame::mallocd, ast_frame::ptr, ast_frame::samples, and ast_frame::subclass.

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 }

static int gsm_seek ( struct ast_filestream fs,
off_t  sample_offset,
int  whence 
) [static]

Definition at line 109 of file format_gsm.c.

References ast_log(), errno, ast_filestream::f, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, ast_frame::offset, and SEEK_FORCECUR.

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 }

static off_t gsm_tell ( struct ast_filestream fs  )  [static]

Definition at line 146 of file format_gsm.c.

References ast_filestream::f, GSM_FRAME_SIZE, GSM_SAMPLES, and ast_frame::offset.

00147 {
00148    off_t offset = ftello(fs->f);
00149    return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
00150 }

static int gsm_trunc ( struct ast_filestream fs  )  [static]

Definition at line 141 of file format_gsm.c.

References ast_filestream::f.

00142 {
00143    return ftruncate(fileno(fs->f), ftello(fs->f));
00144 }

static int gsm_write ( struct ast_filestream fs,
struct ast_frame f 
) [static]

Definition at line 72 of file format_gsm.c.

References AST_FORMAT_GSM, AST_FRAME_VOICE, ast_getformatname(), ast_log(), conv65(), ast_frame::datalen, errno, ast_filestream::f, f, GSM_FRAME_SIZE, len(), and LOG_WARNING.

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 }

static int load_module ( void   )  [static]

Definition at line 164 of file format_gsm.c.

References ast_format_register, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, and gsm_f.

00165 {
00166    if (ast_format_register(&gsm_f))
00167       return AST_MODULE_LOAD_FAILURE;
00168    return AST_MODULE_LOAD_SUCCESS;
00169 }

static int unload_module ( void   )  [static]

Definition at line 171 of file format_gsm.c.

References ast_format_unregister(), gsm_f, and ast_format::name.

00172 {
00173    return ast_format_unregister(gsm_f.name);
00174 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Raw GSM data" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "88eaa8f5c1bd988bedd71113385e0886" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND } [static]

Definition at line 180 of file format_gsm.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 180 of file format_gsm.c.

struct ast_format gsm_f [static]

Definition at line 152 of file format_gsm.c.

Referenced by load_module(), and unload_module().

const char gsm_silence[] [static]

Initial value:

 
{0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
,0x92,0x49,0x24}

Definition at line 49 of file format_gsm.c.


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