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 #include <netinet/in.h>
31 #include <arpa/inet.h>
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include "asterisk/lock.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/file.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/sched.h"
47 #include "asterisk/module.h"
48 #include "asterisk/endian.h"
50 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
52 /* Portions of the conversion code are by guido@sienanet.it */
54 struct ast_filestream {
55 void *reserved[AST_RESERVED_POINTERS];
56 /* Believe it or not, we must decode/recode to account for the
58 /* This is what a filestream means to us */
59 FILE *f; /* Descriptor */
60 struct ast_frame fr; /* Frame information */
61 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
62 char empty; /* Empty character */
63 unsigned char g729[20]; /* Two Real G729 Frames */
67 AST_MUTEX_DEFINE_STATIC(g729_lock);
68 static int glistcnt = 0;
70 static char *name = "g729";
71 static char *desc = "Raw G729 data";
72 static char *exts = "g729";
74 static struct ast_filestream *g729_open(FILE *f)
76 /* We don't have any header to read or anything really, but
77 if we did, it would go here. We also might want to check
78 and be sure it's a valid file. */
79 struct ast_filestream *tmp;
80 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
81 memset(tmp, 0, sizeof(struct ast_filestream));
82 if (ast_mutex_lock(&g729_lock)) {
83 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
88 tmp->fr.data = tmp->g729;
89 tmp->fr.frametype = AST_FRAME_VOICE;
90 tmp->fr.subclass = AST_FORMAT_G729A;
91 /* datalen will vary for each frame */
95 ast_mutex_unlock(&g729_lock);
96 ast_update_use_count();
101 static struct ast_filestream *g729_rewrite(FILE *f, const char *comment)
103 /* We don't have any header to read or anything really, but
104 if we did, it would go here. We also might want to check
105 and be sure it's a valid file. */
106 struct ast_filestream *tmp;
107 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
108 memset(tmp, 0, sizeof(struct ast_filestream));
109 if (ast_mutex_lock(&g729_lock)) {
110 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
116 ast_mutex_unlock(&g729_lock);
117 ast_update_use_count();
119 ast_log(LOG_WARNING, "Out of memory\n");
123 static void g729_close(struct ast_filestream *s)
125 if (ast_mutex_lock(&g729_lock)) {
126 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
130 ast_mutex_unlock(&g729_lock);
131 ast_update_use_count();
137 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
140 /* Send a frame from the file to the appropriate channel */
141 s->fr.frametype = AST_FRAME_VOICE;
142 s->fr.subclass = AST_FORMAT_G729A;
143 s->fr.offset = AST_FRIENDLY_OFFSET;
147 s->fr.data = s->g729;
148 if ((res = fread(s->g729, 1, 20, s->f)) != 20) {
149 if (res && (res != 10))
150 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
153 *whennext = s->fr.samples;
157 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
160 if (f->frametype != AST_FRAME_VOICE) {
161 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
164 if (f->subclass != AST_FORMAT_G729A) {
165 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
168 if (f->datalen % 10) {
169 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
172 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
173 ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
179 static char *g729_getcomment(struct ast_filestream *s)
184 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
187 off_t min,cur,max,offset=0;
190 fseek(fs->f, 0, SEEK_END);
193 bytes = 20 * (sample_offset / 160);
194 if (whence == SEEK_SET)
196 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
197 offset = cur + bytes;
198 else if (whence == SEEK_END)
199 offset = max - bytes;
200 if (whence != SEEK_FORCECUR) {
201 offset = (offset > max)?max:offset;
203 /* protect against seeking beyond begining. */
204 offset = (offset < min)?min:offset;
205 if (fseek(fs->f, offset, SEEK_SET) < 0)
210 static int g729_trunc(struct ast_filestream *fs)
212 /* Truncate file to current length */
213 if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
218 static long g729_tell(struct ast_filestream *fs)
221 offset = ftell(fs->f);
222 return (offset/20)*160;
227 return ast_format_register(name, exts, AST_FORMAT_G729A,
243 return ast_format_unregister(name);
259 return ASTERISK_GPL_KEY;