Wed Jan 8 2020 09:50:12

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.

Macros

#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 = "ac1f6a56484a8820659555499174e588" , .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.

Macro Definition 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 211 of file format_gsm.c.

static void __unreg_module ( void  )
static

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

56 {
57  int res;
58 
62  s->fr.mallocd = 0;
63  if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
64  if (res)
65  ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
66  return NULL;
67  }
68  *whennext = s->fr.samples = GSM_SAMPLES;
69  return &s->fr;
70 }
union ast_frame_subclass subclass
Definition: frame.h:146
#define GSM_FRAME_SIZE
Definition: format_gsm.c:44
#define LOG_WARNING
Definition: logger.h:144
format_t codec
Definition: frame.h:137
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
Definition: frame.h:183
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
Definition: frame.h:204
struct ast_frame fr
Definition: mod_format.h:117
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
static struct ast_format f[]
Definition: format_g726.c:181
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
enum ast_frame_type frametype
Definition: frame.h:144
#define GSM_SAMPLES
Definition: format_gsm.c:45
#define AST_FORMAT_GSM
Definition: frame.h:244
int samples
Definition: frame.h:150
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.

110 {
111  off_t offset = 0, min = 0, cur, max, distance;
112 
113  if ((cur = ftello(fs->f)) < 0) {
114  ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
115  return -1;
116  }
117 
118  if (fseeko(fs->f, 0, SEEK_END) < 0) {
119  ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
120  return -1;
121  }
122 
123  if ((max = ftello(fs->f)) < 0) {
124  ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
125  return -1;
126  }
127 
128  /* have to fudge to frame here, so not fully to sample */
129  distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
130  if (whence == SEEK_SET) {
131  offset = distance;
132  } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
133  offset = distance + cur;
134  } else if (whence == SEEK_END) {
135  offset = max - distance;
136  }
137 
138  /* Always protect against seeking past the begining. */
139  offset = (offset < min)?min:offset;
140  if (whence != SEEK_FORCECUR) {
141  offset = (offset > max)?max:offset;
142  } else if (offset > max) {
143  int i;
144  fseeko(fs->f, 0, SEEK_END);
145  for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
146  if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
147  ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
148  }
149  }
150  }
151  return fseeko(fs->f, offset, SEEK_SET);
152 }
#define GSM_FRAME_SIZE
Definition: format_gsm.c:44
#define LOG_WARNING
Definition: logger.h:144
#define AST_LOG_WARNING
Definition: logger.h:149
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
#define SEEK_FORCECUR
Definition: file.h:50
static const char gsm_silence[]
Definition: format_gsm.c:49
#define GSM_SAMPLES
Definition: format_gsm.c:45
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.

172 {
173  off_t offset = ftello(fs->f);
174 
175  if (offset < 0) {
176  ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
177  return 0;
178  }
179 
180  return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
181 }
#define GSM_FRAME_SIZE
Definition: format_gsm.c:44
#define AST_LOG_WARNING
Definition: logger.h:149
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
#define GSM_SAMPLES
Definition: format_gsm.c:45
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.

155 {
156  int fd;
157  off_t cur;
158 
159  if ((fd = fileno(fs->f)) < 0) {
160  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
161  return -1;
162  }
163  if ((cur = ftello(fs->f)) < 0) {
164  ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
165  return -1;
166  }
167  /* Truncate file to current length */
168  return ftruncate(fd, cur);
169 }
#define AST_LOG_WARNING
Definition: logger.h:149
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
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.

73 {
74  int res;
75  unsigned char gsm[2*GSM_FRAME_SIZE];
76 
77  if (f->frametype != AST_FRAME_VOICE) {
78  ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
79  return -1;
80  }
81  if (f->subclass.codec != AST_FORMAT_GSM) {
82  ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(f->subclass.codec));
83  return -1;
84  }
85  if (!(f->datalen % 65)) {
86  /* This is in MSGSM format, need to be converted */
87  int len=0;
88  while(len < f->datalen) {
89  conv65(f->data.ptr + len, gsm);
90  if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
91  ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
92  return -1;
93  }
94  len += 65;
95  }
96  } else {
97  if (f->datalen % GSM_FRAME_SIZE) {
98  ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
99  return -1;
100  }
101  if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
102  ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
103  return -1;
104  }
105  }
106  return 0;
107 }
union ast_frame_subclass subclass
Definition: frame.h:146
#define GSM_FRAME_SIZE
Definition: format_gsm.c:44
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
format_t codec
Definition: frame.h:137
int datalen
Definition: frame.h:148
static void conv65(wav_byte *c, gsm_byte *d)
Definition: msgsm.h:455
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
int errno
enum ast_frame_type frametype
Definition: frame.h:144
union ast_frame::@172 data
#define AST_FORMAT_GSM
Definition: frame.h:244
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.

196 {
200 }
#define ast_format_register(f)
Definition: mod_format.h:131
static struct ast_format gsm_f
Definition: format_gsm.c:183
static int unload_module ( void  )
static

Definition at line 202 of file format_gsm.c.

References ast_format_unregister(), and ast_format::name.

203 {
205 }
int ast_format_unregister(const char *name)
Unregisters a file format.
Definition: file.c:104
char name[80]
Definition: mod_format.h:44
static struct ast_format gsm_f
Definition: format_gsm.c:183

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

Definition at line 211 of file format_gsm.c.

Definition at line 211 of file format_gsm.c.

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.