Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
format_g726.c File Reference

Headerless G.726 (16/24/32/40kbps) data format for Asterisk. More...

#include "asterisk.h"
#include "asterisk/mod_format.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"
#include "asterisk/format_cache.h"
Include dependency graph for format_g726.c:

Go to the source code of this file.

Data Structures

struct  g726_desc
 

Macros

#define BUF_SIZE   (5*FRAME_TIME) /* max frame size in bytes ? */
 
#define FRAME_TIME   10 /* 10 ms size */
 
#define RATE_16   3
 
#define RATE_24   2
 
#define RATE_32   1
 
#define RATE_40   0
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int g726_16_open (struct ast_filestream *s)
 
static int g726_16_rewrite (struct ast_filestream *s, const char *comment)
 
static int g726_24_open (struct ast_filestream *s)
 
static int g726_24_rewrite (struct ast_filestream *s, const char *comment)
 
static int g726_32_open (struct ast_filestream *s)
 
static int g726_32_rewrite (struct ast_filestream *s, const char *comment)
 
static int g726_40_open (struct ast_filestream *s)
 
static int g726_40_rewrite (struct ast_filestream *s, const char *comment)
 
static int g726_open (struct ast_filestream *tmp, int rate)
 
static struct ast_frameg726_read (struct ast_filestream *s, int *whennext)
 
static int g726_seek (struct ast_filestream *fs, off_t sample_offset, int whence)
 
static off_t g726_tell (struct ast_filestream *fs)
 
static int g726_trunc (struct ast_filestream *fs)
 
static int g726_write (struct ast_filestream *s, 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 G.726 (16/24/32/40kbps) 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 f_def []
 
static int frame_size [4]
 

Detailed Description

Headerless G.726 (16/24/32/40kbps) data format for Asterisk.

File name extensions:

Definition in file format_g726.c.

Macro Definition Documentation

◆ BUF_SIZE

#define BUF_SIZE   (5*FRAME_TIME) /* max frame size in bytes ? */

Definition at line 50 of file format_g726.c.

◆ FRAME_TIME

#define FRAME_TIME   10 /* 10 ms size */

Definition at line 48 of file format_g726.c.

Referenced by g726_read().

◆ RATE_16

#define RATE_16   3

Definition at line 45 of file format_g726.c.

Referenced by g726_16_open(), and g726_16_rewrite().

◆ RATE_24

#define RATE_24   2

Definition at line 44 of file format_g726.c.

Referenced by g726_24_open(), and g726_24_rewrite().

◆ RATE_32

#define RATE_32   1

Definition at line 43 of file format_g726.c.

Referenced by g726_32_open(), and g726_32_rewrite().

◆ RATE_40

#define RATE_40   0

Definition at line 42 of file format_g726.c.

Referenced by g726_40_open(), and g726_40_rewrite().

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 289 of file format_g726.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 289 of file format_g726.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 289 of file format_g726.c.

◆ g726_16_open()

static int g726_16_open ( struct ast_filestream s)
static

Definition at line 89 of file format_g726.c.

References g726_open(), and RATE_16.

90 {
91  return g726_open(s, RATE_16);
92 }
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66
#define RATE_16
Definition: format_g726.c:45

◆ g726_16_rewrite()

static int g726_16_rewrite ( struct ast_filestream s,
const char *  comment 
)
static

Definition at line 109 of file format_g726.c.

References g726_open(), and RATE_16.

110 {
111  return g726_open(s, RATE_16);
112 }
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66
#define RATE_16
Definition: format_g726.c:45

◆ g726_24_open()

static int g726_24_open ( struct ast_filestream s)
static

Definition at line 84 of file format_g726.c.

References g726_open(), and RATE_24.

85 {
86  return g726_open(s, RATE_24);
87 }
#define RATE_24
Definition: format_g726.c:44
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66

◆ g726_24_rewrite()

static int g726_24_rewrite ( struct ast_filestream s,
const char *  comment 
)
static

Definition at line 104 of file format_g726.c.

References g726_open(), and RATE_24.

105 {
106  return g726_open(s, RATE_24);
107 }
#define RATE_24
Definition: format_g726.c:44
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66

◆ g726_32_open()

static int g726_32_open ( struct ast_filestream s)
static

Definition at line 79 of file format_g726.c.

References g726_open(), and RATE_32.

80 {
81  return g726_open(s, RATE_32);
82 }
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66
#define RATE_32
Definition: format_g726.c:43

◆ g726_32_rewrite()

static int g726_32_rewrite ( struct ast_filestream s,
const char *  comment 
)
static

Definition at line 99 of file format_g726.c.

References g726_open(), and RATE_32.

100 {
101  return g726_open(s, RATE_32);
102 }
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66
#define RATE_32
Definition: format_g726.c:43

◆ g726_40_open()

static int g726_40_open ( struct ast_filestream s)
static

Definition at line 74 of file format_g726.c.

References g726_open(), and RATE_40.

75 {
76  return g726_open(s, RATE_40);
77 }
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66
#define RATE_40
Definition: format_g726.c:42

◆ g726_40_rewrite()

static int g726_40_rewrite ( struct ast_filestream s,
const char *  comment 
)
static

Definition at line 94 of file format_g726.c.

References g726_open(), and RATE_40.

95 {
96  return g726_open(s, RATE_40);
97 }
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:66
#define RATE_40
Definition: format_g726.c:42

◆ g726_open()

static int g726_open ( struct ast_filestream tmp,
int  rate 
)
static

Definition at line 66 of file format_g726.c.

References ast_filestream::_private, ast_debug, and g726_desc::rate.

Referenced by g726_16_open(), g726_16_rewrite(), g726_24_open(), g726_24_rewrite(), g726_32_open(), g726_32_rewrite(), g726_40_open(), and g726_40_rewrite().

67 {
68  struct g726_desc *s = (struct g726_desc *)tmp->_private;
69  s->rate = rate;
70  ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
71  return 0;
72 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
void * _private
Definition: mod_format.h:124

◆ g726_read()

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

Definition at line 118 of file format_g726.c.

References ast_filestream::_private, ast_format_get_name(), AST_FRAME_SET_BUFFER, AST_FRIENDLY_OFFSET, ast_log, ast_filestream::buf, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_frame_subclass::format, ast_filestream::fr, frame_size, FRAME_TIME, LOG_WARNING, NULL, ast_frame::ptr, g726_desc::rate, ast_frame::samples, and ast_frame::subclass.

119 {
120  size_t res;
121  struct g726_desc *fs = (struct g726_desc *)s->_private;
122 
123  /* Send a frame from the file to the appropriate channel */
125  s->fr.samples = 8 * FRAME_TIME;
126  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
127  if (res) {
128  ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
130  strerror(errno));
131  }
132  return NULL;
133  }
134  *whennext = s->fr.samples;
135  return &s->fr;
136 }
#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
void * _private
Definition: mod_format.h:124
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
int errno
union ast_frame::@263 data
struct ast_format * format
#define FRAME_TIME
Definition: format_g726.c:48
static int frame_size[4]
Definition: format_g726.c:52

◆ g726_seek()

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

Definition at line 156 of file format_g726.c.

References ast_log, AST_LOG_WARNING, errno, ast_filestream::f, max, min, and SEEK_FORCECUR.

157 {
158  off_t offset = 0, min = 0, cur, max, distance;
159 
160  if ((cur = ftello(fs->f)) < 0) {
161  ast_log(AST_LOG_WARNING, "Unable to determine current position in g726 filestream %p: %s\n", fs, strerror(errno));
162  return -1;
163  }
164 
165  if (fseeko(fs->f, 0, SEEK_END) < 0) {
166  ast_log(AST_LOG_WARNING, "Unable to seek to end of g726 filestream %p: %s\n", fs, strerror(errno));
167  return -1;
168  }
169 
170  if ((max = ftello(fs->f)) < 0) {
171  ast_log(AST_LOG_WARNING, "Unable to determine max position in g726 filestream %p: %s\n", fs, strerror(errno));
172  return -1;
173  }
174 
175  /* have to fudge to frame here, so not fully to sample */
176  distance = sample_offset / 2;
177  if (whence == SEEK_SET) {
178  offset = distance;
179  } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
180  offset = distance + cur;
181  } else if (whence == SEEK_END) {
182  offset = max - distance;
183  }
184 
185  if (whence != SEEK_FORCECUR) {
186  offset = offset > max ? max : offset;
187  offset = offset < min ? min : offset;
188  }
189  return fseeko(fs->f, offset, SEEK_SET);
190 }
#define AST_LOG_WARNING
Definition: logger.h:279
#define ast_log
Definition: astobj2.c:42
int errno
#define SEEK_FORCECUR
Definition: file.h:51
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198

◆ g726_tell()

static off_t g726_tell ( struct ast_filestream fs)
static

Definition at line 197 of file format_g726.c.

References ast_filestream::f.

198 {
199  return ftello(fs->f) << 1;
200 }

◆ g726_trunc()

static int g726_trunc ( struct ast_filestream fs)
static

Definition at line 192 of file format_g726.c.

193 {
194  return -1;
195 }

◆ g726_write()

static int g726_write ( struct ast_filestream s,
struct ast_frame f 
)
static

Definition at line 138 of file format_g726.c.

References ast_filestream::_private, ast_log, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, frame_size, if(), LOG_WARNING, ast_frame::ptr, and g726_desc::rate.

139 {
140  int res;
141  struct g726_desc *fs = (struct g726_desc *)s->_private;
142 
143  if (f->datalen % frame_size[fs->rate]) {
144  ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
145  f->datalen, frame_size[fs->rate]);
146  return -1;
147  }
148  if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
149  ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
150  res, frame_size[fs->rate], strerror(errno));
151  return -1;
152  }
153  return 0;
154 }
#define LOG_WARNING
Definition: logger.h:274
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
#define ast_log
Definition: astobj2.c:42
void * _private
Definition: mod_format.h:124
int errno
union ast_frame::@263 data
static int frame_size[4]
Definition: format_g726.c:52

◆ load_module()

static int load_module ( void  )
static

Definition at line 269 of file format_g726.c.

References ast_format_def_register, ast_format_g726, ast_log, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_APP_DEPEND, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, ast_format_def::desc_size, ast_format_def::format, LOG_WARNING, name, and unload_module().

270 {
271  int i;
272 
273  for (i = 0; f_def[i].desc_size ; i++) {
275  if (ast_format_def_register(&f_def[i])) { /* errors are fatal */
276  ast_log(LOG_WARNING, "Failed to register format %s.\n", f_def[i].name);
277  unload_module();
279  }
280  }
282 }
struct ast_format * ast_format_g726
Built-in cached g726 format.
Definition: format_cache.c:116
#define LOG_WARNING
Definition: logger.h:274
#define ast_log
Definition: astobj2.c:42
#define ast_format_def_register(f)
Definition: mod_format.h:136
struct ast_format * format
Definition: mod_format.h:48
static const char name[]
Definition: cdr_mysql.c:74
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_format_def f_def[]
Definition: format_g726.c:202
static int unload_module(void)
Definition: format_g726.c:258

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 258 of file format_g726.c.

References ast_format_def_unregister(), ast_log, ast_format_def::desc_size, LOG_WARNING, and name.

Referenced by load_module().

259 {
260  int i;
261 
262  for (i = 0; f_def[i].desc_size ; i++) {
264  ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f_def[i].name);
265  }
266  return(0);
267 }
#define LOG_WARNING
Definition: logger.h:274
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_log
Definition: astobj2.c:42
static const char name[]
Definition: cdr_mysql.c:74
static struct ast_format_def f_def[]
Definition: format_g726.c:202

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Raw G.726 (16/24/32/40kbps) 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 289 of file format_g726.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 289 of file format_g726.c.

◆ f_def

struct ast_format_def f_def[]
static

Definition at line 202 of file format_g726.c.

◆ frame_size

int frame_size[4]
static