Mon Aug 31 12:30:36 2015

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

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,"Raw GSM data",.load=load_module,.unload=unload_module,.load_pri=AST_MODPRI_APP_DEPEND)
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_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

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_LOAD_ORDER  ,
"Raw GSM data"  ,
load = load_module,
unload = unload_module,
load_pri = AST_MODPRI_APP_DEPEND 
)
static struct ast_frame* gsm_read ( struct ast_filestream s,
int *  whennext 
) [static, read]

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(), AST_LOG_WARNING, errno, ast_filestream::f, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, ast_frame::offset, and SEEK_FORCECUR.

00110 {
00111    off_t offset = 0, min = 0, cur, max, distance;
00112 
00113    if ((cur = ftello(fs->f)) < 0) {
00114       ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
00115       return -1;
00116    }
00117 
00118    if (fseeko(fs->f, 0, SEEK_END) < 0) {
00119       ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
00120       return -1;
00121    }
00122 
00123    if ((max = ftello(fs->f)) < 0) {
00124       ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
00125       return -1;
00126    }
00127 
00128    /* have to fudge to frame here, so not fully to sample */
00129    distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
00130    if (whence == SEEK_SET) {
00131       offset = distance;
00132    } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
00133       offset = distance + cur;
00134    } else if (whence == SEEK_END) {
00135       offset = max - distance;
00136    }
00137 
00138    /* Always protect against seeking past the begining. */
00139    offset = (offset < min)?min:offset;
00140    if (whence != SEEK_FORCECUR) {
00141       offset = (offset > max)?max:offset;
00142    } else if (offset > max) {
00143       int i;
00144       fseeko(fs->f, 0, SEEK_END);
00145       for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
00146          if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
00147             ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
00148          }
00149       }
00150    }
00151    return fseeko(fs->f, offset, SEEK_SET);
00152 }

static off_t gsm_tell ( struct ast_filestream fs  )  [static]

Definition at line 171 of file format_gsm.c.

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

00172 {
00173    off_t offset = ftello(fs->f);
00174 
00175    if (offset < 0) {
00176       ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
00177       return 0;
00178    }
00179 
00180    return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
00181 }

static int gsm_trunc ( struct ast_filestream fs  )  [static]

Definition at line 154 of file format_gsm.c.

References ast_log(), AST_LOG_WARNING, errno, and ast_filestream::f.

00155 {
00156    int fd;
00157    off_t cur;
00158 
00159    if ((fd = fileno(fs->f)) < 0) {
00160       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
00161       return -1;
00162    }
00163    if ((cur = ftello(fs->f)) < 0) {
00164       ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
00165       return -1;
00166    }
00167    /* Truncate file to current length */
00168    return ftruncate(fd, cur);
00169 }

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(), ast_frame_subclass::codec, conv65(), ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_frame::frametype, GSM_FRAME_SIZE, len(), LOG_WARNING, ast_frame::ptr, and ast_frame::subclass.

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 195 of file format_gsm.c.

References ast_format_register, AST_MODULE_LOAD_FAILURE, and AST_MODULE_LOAD_SUCCESS.

00196 {
00197    if (ast_format_register(&gsm_f))
00198       return AST_MODULE_LOAD_FAILURE;
00199    return AST_MODULE_LOAD_SUCCESS;
00200 }

static int unload_module ( void   )  [static]

Definition at line 202 of file format_gsm.c.

References ast_format_unregister(), and ast_format::name.

00203 {
00204    return ast_format_unregister(gsm_f.name);
00205 }


Variable Documentation

struct ast_format gsm_f [static]

Definition at line 183 of file format_gsm.c.

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 31 Aug 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1