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 "asterisk/format_cache.h"
#include "msgsm.h"

Include dependency graph for format_gsm.c:

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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .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_def 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 45 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 46 of file format_gsm.c.


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 202 of file format_gsm.c.

static void __unreg_module ( void   )  [static]

Definition at line 202 of file format_gsm.c.

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

Definition at line 56 of file format_gsm.c.

References AST_FRAME_SET_BUFFER, AST_FRIENDLY_OFFSET, ast_log, ast_filestream::buf, ast_frame::data, errno, ast_filestream::f, ast_filestream::fr, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, NULL, ast_frame::ptr, and ast_frame::samples.

00057 {
00058    int res;
00059 
00060    AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
00061    if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
00062       if (res)
00063          ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00064       return NULL;
00065    }
00066    *whennext = s->fr.samples = GSM_SAMPLES;
00067    return &s->fr;
00068 }

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

Definition at line 99 of file format_gsm.c.

References ast_log, AST_LOG_WARNING, errno, ast_filestream::f, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, max, min, ast_frame::offset, and SEEK_FORCECUR.

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

static off_t gsm_tell ( struct ast_filestream fs  )  [static]

Definition at line 161 of file format_gsm.c.

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

00162 {
00163    off_t offset = ftello(fs->f);
00164 
00165    if (offset < 0) {
00166       ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
00167       return 0;
00168    }
00169 
00170    return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
00171 }

static int gsm_trunc ( struct ast_filestream fs  )  [static]

Definition at line 144 of file format_gsm.c.

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

00145 {
00146    int fd;
00147    off_t cur;
00148 
00149    if ((fd = fileno(fs->f)) < 0) {
00150       ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
00151       return -1;
00152    }
00153    if ((cur = ftello(fs->f)) < 0) {
00154       ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
00155       return -1;
00156    }
00157    /* Truncate file to current length */
00158    return ftruncate(fd, cur);
00159 }

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

Definition at line 70 of file format_gsm.c.

References ast_log, conv65(), ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, GSM_FRAME_SIZE, len(), LOG_WARNING, and ast_frame::ptr.

00071 {
00072    int res;
00073    unsigned char gsm[2*GSM_FRAME_SIZE];
00074 
00075    if (!(f->datalen % 65)) {
00076       /* This is in MSGSM format, need to be converted */
00077       int len=0;
00078       while(len < f->datalen) {
00079          conv65(f->data.ptr + len, gsm);
00080          if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
00081             ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
00082             return -1;
00083          }
00084          len += 65;
00085       }
00086    } else {
00087       if (f->datalen % GSM_FRAME_SIZE) {
00088          ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
00089          return -1;
00090       }
00091       if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00092             ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
00093             return -1;
00094       }
00095    }
00096    return 0;
00097 }

static int load_module ( void   )  [static]

static int unload_module ( void   )  [static]

Definition at line 192 of file format_gsm.c.

References ast_format_def_unregister(), and ast_format_def::name.

00193 {
00194    return ast_format_def_unregister(gsm_f.name);
00195 }


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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND } [static]

Definition at line 202 of file format_gsm.c.

Definition at line 202 of file format_gsm.c.

struct ast_format_def gsm_f [static]

Definition at line 173 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 50 of file format_gsm.c.


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