Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
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.

Macros

#define GSM_FRAME_SIZE   33
 
#define GSM_SAMPLES   160
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND }
 
static const 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.

Macro Definition Documentation

◆ GSM_FRAME_SIZE

#define GSM_FRAME_SIZE   33

Definition at line 43 of file format_gsm.c.

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

◆ GSM_SAMPLES

#define GSM_SAMPLES   160

Definition at line 44 of file format_gsm.c.

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

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 203 of file format_gsm.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 203 of file format_gsm.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 203 of file format_gsm.c.

◆ gsm_read()

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

Definition at line 54 of file format_gsm.c.

References ast_format_get_name(), AST_FRAME_SET_BUFFER, AST_FRIENDLY_OFFSET, ast_log, ast_filestream::buf, ast_frame::data, errno, ast_filestream::f, ast_frame_subclass::format, ast_filestream::fr, GSM_FRAME_SIZE, GSM_SAMPLES, LOG_WARNING, NULL, ast_frame::ptr, ast_frame::samples, and ast_frame::subclass.

55 {
56  size_t res;
57 
59  if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
60  if (res) {
61  ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
63  strerror(errno));
64  }
65  return NULL;
66  }
67  *whennext = s->fr.samples = GSM_SAMPLES;
68  return &s->fr;
69 }
#define GSM_FRAME_SIZE
Definition: format_gsm.c:43
#define LOG_WARNING
Definition: logger.h:274
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
#define NULL
Definition: resample.c:96
struct ast_frame_subclass subclass
#define ast_log
Definition: astobj2.c:42
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
int errno
union ast_frame::@263 data
struct ast_format * format
#define GSM_SAMPLES
Definition: format_gsm.c:44

◆ gsm_seek()

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

Definition at line 100 of file format_gsm.c.

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

101 {
102  off_t offset = 0, min = 0, cur, max, distance;
103 
104  if ((cur = ftello(fs->f)) < 0) {
105  ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
106  return -1;
107  }
108 
109  if (fseeko(fs->f, 0, SEEK_END) < 0) {
110  ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
111  return -1;
112  }
113 
114  if ((max = ftello(fs->f)) < 0) {
115  ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
116  return -1;
117  }
118 
119  /* have to fudge to frame here, so not fully to sample */
120  distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
121  if (whence == SEEK_SET) {
122  offset = distance;
123  } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
124  offset = distance + cur;
125  } else if (whence == SEEK_END) {
126  offset = max - distance;
127  }
128 
129  /* Always protect against seeking past the begining. */
130  offset = (offset < min)?min:offset;
131  if (whence != SEEK_FORCECUR) {
132  offset = (offset > max)?max:offset;
133  } else if (offset > max) {
134  int i;
135  fseeko(fs->f, 0, SEEK_END);
136  for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
137  if (fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f) != GSM_FRAME_SIZE) {
138  ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
139  }
140  }
141  }
142  return fseeko(fs->f, offset, SEEK_SET);
143 }
#define GSM_FRAME_SIZE
Definition: format_gsm.c:43
#define LOG_WARNING
Definition: logger.h:274
#define AST_LOG_WARNING
Definition: logger.h:279
#define ast_log
Definition: astobj2.c:42
int errno
#define SEEK_FORCECUR
Definition: file.h:51
static const char gsm_silence[]
Definition: format_gsm.c:48
#define GSM_SAMPLES
Definition: format_gsm.c:44
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198

◆ gsm_tell()

static off_t gsm_tell ( struct ast_filestream fs)
static

Definition at line 162 of file format_gsm.c.

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

163 {
164  off_t offset = ftello(fs->f);
165 
166  if (offset < 0) {
167  ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
168  return 0;
169  }
170 
171  return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
172 }
#define GSM_FRAME_SIZE
Definition: format_gsm.c:43
#define AST_LOG_WARNING
Definition: logger.h:279
#define ast_log
Definition: astobj2.c:42
int errno
#define GSM_SAMPLES
Definition: format_gsm.c:44

◆ gsm_trunc()

static int gsm_trunc ( struct ast_filestream fs)
static

Definition at line 145 of file format_gsm.c.

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

146 {
147  int fd;
148  off_t cur;
149 
150  if ((fd = fileno(fs->f)) < 0) {
151  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
152  return -1;
153  }
154  if ((cur = ftello(fs->f)) < 0) {
155  ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
156  return -1;
157  }
158  /* Truncate file to current length */
159  return ftruncate(fd, cur);
160 }
#define AST_LOG_WARNING
Definition: logger.h:279
#define ast_log
Definition: astobj2.c:42
int errno

◆ gsm_write()

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

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

72 {
73  int res;
74  unsigned char gsm[2*GSM_FRAME_SIZE];
75 
76  if (!(f->datalen % 65)) {
77  /* This is in MSGSM format, need to be converted */
78  int len=0;
79  while(len < f->datalen) {
80  conv65(f->data.ptr + len, gsm);
81  if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
82  ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
83  return -1;
84  }
85  len += 65;
86  }
87  } else {
88  if (f->datalen % GSM_FRAME_SIZE) {
89  ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
90  return -1;
91  }
92  if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
93  ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
94  return -1;
95  }
96  }
97  return 0;
98 }
#define GSM_FRAME_SIZE
Definition: format_gsm.c:43
#define LOG_WARNING
Definition: logger.h:274
#define ast_log
Definition: astobj2.c:42
static void conv65(wav_byte *c, gsm_byte *d)
Definition: msgsm.h:457
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno
union ast_frame::@263 data

◆ load_module()

static int load_module ( void  )
static

Definition at line 185 of file format_gsm.c.

References ast_format_def_register, ast_format_gsm, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and ast_format_def::format.

Referenced by unload_module().

186 {
191 }
#define ast_format_def_register(f)
Definition: mod_format.h:136
struct ast_format * ast_format_gsm
Built-in cached gsm format.
Definition: format_cache.c:101
struct ast_format * format
Definition: mod_format.h:48
static struct ast_format_def gsm_f
Definition: format_gsm.c:174
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 193 of file format_gsm.c.

References ast_format_def_unregister(), AST_MODFLAG_LOAD_ORDER, AST_MODPRI_APP_DEPEND, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, load_module(), and ast_format_def::name.

194 {
196 }
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
char name[80]
Definition: mod_format.h:44
static struct ast_format_def gsm_f
Definition: format_gsm.c:174

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND }
static

Definition at line 203 of file format_gsm.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 203 of file format_gsm.c.

◆ gsm_f

struct ast_format_def gsm_f
static

Definition at line 174 of file format_gsm.c.

◆ gsm_silence

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

Referenced by gsm_seek().