2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004 - 2005, inAccess Networks
6 * Michael Manousos <manousos@inaccessnetworks.com>
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.
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.
21 * \brief Headerless G.726 (16/24/32/40kbps) data format for Asterisk.
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
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/lock.h"
45 #include "asterisk/options.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/file.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/sched.h"
50 #include "asterisk/module.h"
51 #include "asterisk/endian.h"
58 /* We can only read/write chunks of FRAME_TIME ms G.726 data */
59 #define FRAME_TIME 10 /* 10 ms size */
61 #define BUF_SIZE (5*FRAME_TIME) /* max frame size in bytes ? */
62 /* Frame sizes in bytes */
63 static int frame_size[4] = {
71 int rate; /* RATE_* defines */
75 * Rate dependant format functions (open, rewrite)
77 static int g726_open(struct ast_filestream *tmp, int rate)
79 struct g726_desc *s = (struct g726_desc *)tmp->private;
82 ast_log(LOG_DEBUG, "Created filestream G.726-%dk.\n",
87 static int g726_40_open(struct ast_filestream *s)
89 return g726_open(s, RATE_40);
92 static int g726_32_open(struct ast_filestream *s)
94 return g726_open(s, RATE_32);
97 static int g726_24_open(struct ast_filestream *s)
99 return g726_open(s, RATE_24);
102 static int g726_16_open(struct ast_filestream *s)
104 return g726_open(s, RATE_16);
107 static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
109 return g726_open(s, RATE_40);
112 static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
114 return g726_open(s, RATE_32);
117 static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
119 return g726_open(s, RATE_24);
122 static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
124 return g726_open(s, RATE_16);
128 * Rate independent format functions (read, write)
131 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
134 struct g726_desc *fs = (struct g726_desc *)s->private;
136 /* Send a frame from the file to the appropriate channel */
137 s->fr.frametype = AST_FRAME_VOICE;
138 s->fr.subclass = AST_FORMAT_G726;
140 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
141 s->fr.samples = 8 * FRAME_TIME;
142 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
144 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
147 *whennext = s->fr.samples;
151 static int g726_write(struct ast_filestream *s, struct ast_frame *f)
154 struct g726_desc *fs = (struct g726_desc *)s->private;
156 if (f->frametype != AST_FRAME_VOICE) {
157 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
160 if (f->subclass != AST_FORMAT_G726) {
161 ast_log(LOG_WARNING, "Asked to write non-G726 frame (%d)!\n",
165 if (f->datalen % frame_size[fs->rate]) {
166 ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n",
167 f->datalen, frame_size[fs->rate]);
170 if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
171 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
172 res, frame_size[fs->rate], strerror(errno));
178 static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
183 static int g726_trunc(struct ast_filestream *fs)
188 static off_t g726_tell(struct ast_filestream *fs)
193 static const struct ast_format f[] = {
197 .format = AST_FORMAT_G726,
198 .open = g726_40_open,
199 .rewrite = g726_40_rewrite,
205 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
206 .desc_size = sizeof(struct g726_desc),
207 .module = &mod_data, /* XXX */
212 .format = AST_FORMAT_G726,
213 .open = g726_32_open,
214 .rewrite = g726_32_rewrite,
220 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
221 .desc_size = sizeof(struct g726_desc),
222 .module = &mod_data, /* XXX */
227 .format = AST_FORMAT_G726,
228 .open = g726_24_open,
229 .rewrite = g726_24_rewrite,
235 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
236 .desc_size = sizeof(struct g726_desc),
237 .module = &mod_data, /* XXX */
242 .format = AST_FORMAT_G726,
243 .open = g726_16_open,
244 .rewrite = g726_16_rewrite,
250 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
251 .desc_size = sizeof(struct g726_desc),
252 .module = &mod_data, /* XXX */
254 { .format = 0 } /* terminator */
258 * Module interface (load_module, unload_module, usecount, description, key)
260 static int load_module(void *mod)
264 for (i = 0; f[i].format ; i++) {
265 if (ast_format_register(&f[i])) { /* errors are fatal */
266 ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
273 static int unload_module(void *mod)
277 for (i = 0; f[i].format ; i++) {
278 if (ast_format_unregister(f[i].name))
279 ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
284 static const char *description(void)
286 return "Raw G.726 (16/24/32/40kbps) data";
289 static const char *key(void)
291 return ASTERISK_GPL_KEY;