2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.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 Save to raw, headerless G729 data.
22 * \note This is not an encoder/decoder. The codec fo g729 is only
23 * available with a commercial license from Digium, due to patent
24 * restrictions. Check http://www.digium.com for information.
25 * \arg Extensions: g729
30 <support_level>core</support_level>
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38 #include "asterisk/format_cache.h"
40 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
42 /* Portions of the conversion code are by guido@sienanet.it */
44 #define BUF_SIZE 20 /* two G729 frames */
45 #define G729A_SAMPLES 160
47 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
50 /* Send a frame from the file to the appropriate channel */
51 s->fr.samples = G729A_SAMPLES;
52 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
53 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
54 if (res && (res != 10)) /* XXX what for ? */
55 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
58 *whennext = s->fr.samples;
62 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
66 if (f->datalen % 10) {
67 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
70 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
71 ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
77 static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
80 off_t min,cur,max,offset=0;
83 fseeko(fs->f, 0, SEEK_END);
86 bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
87 if (whence == SEEK_SET)
89 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
91 else if (whence == SEEK_END)
93 if (whence != SEEK_FORCECUR) {
94 offset = (offset > max)?max:offset;
96 /* protect against seeking beyond begining. */
97 offset = (offset < min)?min:offset;
98 if (fseeko(fs->f, offset, SEEK_SET) < 0)
103 static int g729_trunc(struct ast_filestream *fs)
108 if ((fd = fileno(fs->f)) < 0) {
109 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno));
112 if ((cur = ftello(fs->f)) < 0) {
113 ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno));
116 /* Truncate file to current length */
117 return ftruncate(fd, cur);
120 static off_t g729_tell(struct ast_filestream *fs)
122 off_t offset = ftello(fs->f);
123 return (offset/BUF_SIZE)*G729A_SAMPLES;
126 static struct ast_format_def g729_f = {
134 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
137 static int load_module(void)
139 g729_f.format = ast_format_g729;
140 if (ast_format_def_register(&g729_f))
141 return AST_MODULE_LOAD_FAILURE;
142 return AST_MODULE_LOAD_SUCCESS;
145 static int unload_module(void)
147 return ast_format_def_unregister(g729_f.name);
150 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.729 data",
151 .support_level = AST_MODULE_SUPPORT_CORE,
153 .unload = unload_module,
154 .load_pri = AST_MODPRI_APP_DEPEND