Mon Mar 12 2012 21:39:37

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"
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, .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.

  • File name extension: gsm

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(), and gsm_write().

#define GSM_SAMPLES   160

Definition at line 45 of file format_gsm.c.

Referenced by gsm_read(), gsm_seek(), and gsm_tell().


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, 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.

{
   int res;

   s->fr.frametype = AST_FRAME_VOICE;
   s->fr.subclass.codec = AST_FORMAT_GSM;
   AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
   s->fr.mallocd = 0;
   if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
      if (res)
         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
      return NULL;
   }
   *whennext = s->fr.samples = GSM_SAMPLES;
   return &s->fr;
}
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.

{
   off_t offset=0,min,cur,max,distance;
   
   min = 0;
   cur = ftello(fs->f);
   fseeko(fs->f, 0, SEEK_END);
   max = ftello(fs->f);
   /* have to fudge to frame here, so not fully to sample */
   distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
   if(whence == SEEK_SET)
      offset = distance;
   else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
      offset = distance + cur;
   else if(whence == SEEK_END)
      offset = max - distance;
   /* Always protect against seeking past the begining. */
   offset = (offset < min)?min:offset;
   if (whence != SEEK_FORCECUR) {
      offset = (offset > max)?max:offset;
   } else if (offset > max) {
      int i;
      fseeko(fs->f, 0, SEEK_END);
      for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
         if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
            ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
         }
      }
   }
   return fseeko(fs->f, offset, SEEK_SET);
}
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.

{
   off_t offset = ftello(fs->f);
   return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
}
static int gsm_trunc ( struct ast_filestream fs) [static]

Definition at line 141 of file format_gsm.c.

References ast_filestream::f.

{
   return ftruncate(fileno(fs->f), ftello(fs->f));
}
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.

{
   int res;
   unsigned char gsm[2*GSM_FRAME_SIZE];

   if (f->frametype != AST_FRAME_VOICE) {
      ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
      return -1;
   }
   if (f->subclass.codec != AST_FORMAT_GSM) {
      ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(f->subclass.codec));
      return -1;
   }
   if (!(f->datalen % 65)) {
      /* This is in MSGSM format, need to be converted */
      int len=0;
      while(len < f->datalen) {
         conv65(f->data.ptr + len, gsm);
         if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
            ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
            return -1;
         }
         len += 65;
      }
   } else {
      if (f->datalen % GSM_FRAME_SIZE) {
         ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
         return -1;
      }
      if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
            ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
            return -1;
      }
   }
   return 0;
}
static int load_module ( void  ) [static]
static int unload_module ( void  ) [static]

Definition at line 171 of file format_gsm.c.

References ast_format_unregister(), and ast_format::name.


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

Definition at line 180 of file format_gsm.c.

Definition at line 180 of file format_gsm.c.

struct ast_format gsm_f [static]

Definition at line 152 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.