Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


format_g726.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004 - 2005, inAccess Networks
5  *
6  * Michael Manousos <manousos@inaccessnetworks.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!\file
20  *
21  * \brief Headerless G.726 (16/24/32/40kbps) data format for Asterisk.
22  *
23  * File name extensions:
24  * \arg 40 kbps: g726-40
25  * \arg 32 kbps: g726-32
26  * \arg 24 kbps: g726-24
27  * \arg 16 kbps: g726-16
28  * \ingroup formats
29  */
30 
31 /*** MODULEINFO
32  <support_level>core</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
38 
39 #include "asterisk/mod_format.h"
40 #include "asterisk/module.h"
41 #include "asterisk/endian.h"
42 
43 #define RATE_40 0
44 #define RATE_32 1
45 #define RATE_24 2
46 #define RATE_16 3
47 
48 /* We can only read/write chunks of FRAME_TIME ms G.726 data */
49 #define FRAME_TIME 10 /* 10 ms size */
50 
51 #define BUF_SIZE (5*FRAME_TIME) /* max frame size in bytes ? */
52 /* Frame sizes in bytes */
53 static int frame_size[4] = {
54  FRAME_TIME * 5,
55  FRAME_TIME * 4,
56  FRAME_TIME * 3,
57  FRAME_TIME * 2
58 };
59 
60 struct g726_desc {
61  int rate; /* RATE_* defines */
62 };
63 
64 /*
65  * Rate dependant format functions (open, rewrite)
66  */
67 static int g726_open(struct ast_filestream *tmp, int rate)
68 {
69  struct g726_desc *s = (struct g726_desc *)tmp->_private;
70  s->rate = rate;
71  ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
72  return 0;
73 }
74 
75 static int g726_40_open(struct ast_filestream *s)
76 {
77  return g726_open(s, RATE_40);
78 }
79 
80 static int g726_32_open(struct ast_filestream *s)
81 {
82  return g726_open(s, RATE_32);
83 }
84 
85 static int g726_24_open(struct ast_filestream *s)
86 {
87  return g726_open(s, RATE_24);
88 }
89 
90 static int g726_16_open(struct ast_filestream *s)
91 {
92  return g726_open(s, RATE_16);
93 }
94 
95 static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
96 {
97  return g726_open(s, RATE_40);
98 }
99 
100 static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
101 {
102  return g726_open(s, RATE_32);
103 }
104 
105 static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
106 {
107  return g726_open(s, RATE_24);
108 }
109 
110 static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
111 {
112  return g726_open(s, RATE_16);
113 }
114 
115 /*
116  * Rate independent format functions (read, write)
117  */
118 
119 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
120 {
121  int res;
122  struct g726_desc *fs = (struct g726_desc *)s->_private;
123 
124  /* Send a frame from the file to the appropriate channel */
127  s->fr.mallocd = 0;
128  AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
129  s->fr.samples = 8 * FRAME_TIME;
130  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
131  if (res)
132  ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
133  return NULL;
134  }
135  *whennext = s->fr.samples;
136  return &s->fr;
137 }
138 
139 static int g726_write(struct ast_filestream *s, struct ast_frame *f)
140 {
141  int res;
142  struct g726_desc *fs = (struct g726_desc *)s->_private;
143 
144  if (f->frametype != AST_FRAME_VOICE) {
145  ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
146  return -1;
147  }
148  if (f->subclass.codec != AST_FORMAT_G726) {
149  ast_log(LOG_WARNING, "Asked to write non-G726 frame (%s)!\n",
151  return -1;
152  }
153  if (f->datalen % frame_size[fs->rate]) {
154  ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
155  f->datalen, frame_size[fs->rate]);
156  return -1;
157  }
158  if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
159  ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
160  res, frame_size[fs->rate], strerror(errno));
161  return -1;
162  }
163  return 0;
164 }
165 
166 static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
167 {
168  return -1;
169 }
170 
171 static int g726_trunc(struct ast_filestream *fs)
172 {
173  return -1;
174 }
175 
176 static off_t g726_tell(struct ast_filestream *fs)
177 {
178  return -1;
179 }
180 
181 static const struct ast_format f[] = {
182  {
183  .name = "g726-40",
184  .exts = "g726-40",
185  .format = AST_FORMAT_G726,
186  .open = g726_40_open,
187  .rewrite = g726_40_rewrite,
188  .write = g726_write,
189  .seek = g726_seek,
190  .trunc = g726_trunc,
191  .tell = g726_tell,
192  .read = g726_read,
193  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
194  .desc_size = sizeof(struct g726_desc),
195  },
196  {
197  .name = "g726-32",
198  .exts = "g726-32",
199  .format = AST_FORMAT_G726,
200  .open = g726_32_open,
201  .rewrite = g726_32_rewrite,
202  .write = g726_write,
203  .seek = g726_seek,
204  .trunc = g726_trunc,
205  .tell = g726_tell,
206  .read = g726_read,
207  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
208  .desc_size = sizeof(struct g726_desc),
209  },
210  {
211  .name = "g726-24",
212  .exts = "g726-24",
213  .format = AST_FORMAT_G726,
214  .open = g726_24_open,
215  .rewrite = g726_24_rewrite,
216  .write = g726_write,
217  .seek = g726_seek,
218  .trunc = g726_trunc,
219  .tell = g726_tell,
220  .read = g726_read,
221  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
222  .desc_size = sizeof(struct g726_desc),
223  },
224  {
225  .name = "g726-16",
226  .exts = "g726-16",
227  .format = AST_FORMAT_G726,
228  .open = g726_16_open,
229  .rewrite = g726_16_rewrite,
230  .write = g726_write,
231  .seek = g726_seek,
232  .trunc = g726_trunc,
233  .tell = g726_tell,
234  .read = g726_read,
235  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
236  .desc_size = sizeof(struct g726_desc),
237  },
238  { .format = 0 } /* terminator */
239 };
240 
241 static int load_module(void)
242 {
243  int i;
244 
245  for (i = 0; f[i].format ; i++) {
246  if (ast_format_register(&f[i])) { /* errors are fatal */
247  ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
249  }
250  }
252 }
253 
254 static int unload_module(void)
255 {
256  int i;
257 
258  for (i = 0; f[i].format ; i++) {
259  if (ast_format_unregister(f[i].name))
260  ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
261  }
262  return(0);
263 }
264 
265 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data",
266  .load = load_module,
267  .unload = unload_module,
268  .load_pri = AST_MODPRI_APP_DEPEND
269 );
union ast_frame_subclass subclass
Definition: frame.h:146
Asterisk main include file. File version handling, generic pbx functions.
#define ast_format_register(f)
Definition: mod_format.h:131
static int load_module(void)
Definition: format_g726.c:241
#define RATE_24
Definition: format_g726.c:45
static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
Definition: format_g726.c:110
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
static int g726_open(struct ast_filestream *tmp, int rate)
Definition: format_g726.c:67
static int g726_16_open(struct ast_filestream *s)
Definition: format_g726.c:90
Each supported file format is described by the following structure.
Definition: mod_format.h:43
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
format_t codec
Definition: frame.h:137
static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_g726.c:166
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
Definition: frame.h:183
format_t format
Definition: mod_format.h:47
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static int g726_trunc(struct ast_filestream *fs)
Definition: format_g726.c:171
static int g726_write(struct ast_filestream *s, struct ast_frame *f)
Definition: format_g726.c:139
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
Asterisk architecture endianess compatibility definitions.
int datalen
Definition: frame.h:148
#define AST_FORMAT_G726
Definition: frame.h:264
struct ast_frame fr
Definition: mod_format.h:117
#define BUF_SIZE
Definition: format_g726.c:51
static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
Definition: format_g726.c:100
static int g726_32_open(struct ast_filestream *s)
Definition: format_g726.c:80
void * _private
Definition: mod_format.h:119
int ast_format_unregister(const char *name)
Unregisters a file format.
Definition: file.c:104
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
char name[80]
Definition: mod_format.h:44
static int g726_40_open(struct ast_filestream *s)
Definition: format_g726.c:75
static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
Definition: format_g726.c:105
static struct ast_frame * g726_read(struct ast_filestream *s, int *whennext)
Definition: format_g726.c:119
#define RATE_40
Definition: format_g726.c:43
#define comment
Definition: ael_lex.c:961
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 const char name[]
static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
Definition: format_g726.c:95
static struct ast_format f[]
Definition: format_g726.c:181
#define RATE_32
Definition: format_g726.c:44
if(yyss+yystacksize-1<=yyssp)
Definition: ast_expr2.c:1874
int mallocd
Definition: frame.h:152
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
static off_t g726_tell(struct ast_filestream *fs)
Definition: format_g726.c:176
Data structure associated with a single frame of data.
Definition: frame.h:142
enum ast_frame_type frametype
Definition: frame.h:144
static int g726_24_open(struct ast_filestream *s)
Definition: format_g726.c:85
static int unload_module(void)
Definition: format_g726.c:254
#define FRAME_TIME
Definition: format_g726.c:49
#define RATE_16
Definition: format_g726.c:46
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150
static int frame_size[4]
Definition: format_g726.c:53