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 * Save to raw, headerless G729 data.
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/lock.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/file.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/module.h"
44 #include "asterisk/endian.h"
46 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
48 /* Portions of the conversion code are by guido@sienanet.it */
50 struct ast_filestream {
51 void *reserved[AST_RESERVED_POINTERS];
52 /* Believe it or not, we must decode/recode to account for the
54 /* This is what a filestream means to us */
55 int fd; /* Descriptor */
56 struct ast_frame fr; /* Frame information */
57 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
58 char empty; /* Empty character */
59 unsigned char g729[20]; /* Two Real G729 Frames */
63 AST_MUTEX_DEFINE_STATIC(g729_lock);
64 static int glistcnt = 0;
66 static char *name = "g729";
67 static char *desc = "Raw G729 data";
68 static char *exts = "g729";
70 static struct ast_filestream *g729_open(int fd)
72 /* We don't have any header to read or anything really, but
73 if we did, it would go here. We also might want to check
74 and be sure it's a valid file. */
75 struct ast_filestream *tmp;
76 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
77 memset(tmp, 0, sizeof(struct ast_filestream));
78 if (ast_mutex_lock(&g729_lock)) {
79 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
84 tmp->fr.data = tmp->g729;
85 tmp->fr.frametype = AST_FRAME_VOICE;
86 tmp->fr.subclass = AST_FORMAT_G729A;
87 /* datalen will vary for each frame */
91 ast_mutex_unlock(&g729_lock);
92 ast_update_use_count();
97 static struct ast_filestream *g729_rewrite(int fd, const char *comment)
99 /* We don't have any header to read or anything really, but
100 if we did, it would go here. We also might want to check
101 and be sure it's a valid file. */
102 struct ast_filestream *tmp;
103 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
104 memset(tmp, 0, sizeof(struct ast_filestream));
105 if (ast_mutex_lock(&g729_lock)) {
106 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
112 ast_mutex_unlock(&g729_lock);
113 ast_update_use_count();
115 ast_log(LOG_WARNING, "Out of memory\n");
119 static void g729_close(struct ast_filestream *s)
121 if (ast_mutex_lock(&g729_lock)) {
122 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
126 ast_mutex_unlock(&g729_lock);
127 ast_update_use_count();
133 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
136 /* Send a frame from the file to the appropriate channel */
137 s->fr.frametype = AST_FRAME_VOICE;
138 s->fr.subclass = AST_FORMAT_G729A;
139 s->fr.offset = AST_FRIENDLY_OFFSET;
143 s->fr.data = s->g729;
144 if ((res = read(s->fd, s->g729, 20)) != 20) {
145 if (res && (res != 10))
146 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
149 *whennext = s->fr.samples;
153 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
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_G729A) {
161 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
164 if (f->datalen % 10) {
165 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
168 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
169 ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
175 static char *g729_getcomment(struct ast_filestream *s)
180 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
183 off_t min,cur,max,offset=0;
185 cur = lseek(fs->fd, 0, SEEK_CUR);
186 max = lseek(fs->fd, 0, SEEK_END);
188 bytes = 20 * (sample_offset / 160);
189 if (whence == SEEK_SET)
191 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
192 offset = cur + bytes;
193 else if (whence == SEEK_END)
194 offset = max - bytes;
195 if (whence != SEEK_FORCECUR) {
196 offset = (offset > max)?max:offset;
198 /* protect against seeking beyond begining. */
199 offset = (offset < min)?min:offset;
200 if (lseek(fs->fd, offset, SEEK_SET) < 0)
205 static int g729_trunc(struct ast_filestream *fs)
207 /* Truncate file to current length */
208 if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
213 static long g729_tell(struct ast_filestream *fs)
216 offset = lseek(fs->fd, 0, SEEK_CUR);
217 return (offset/20)*160;
222 return ast_format_register(name, exts, AST_FORMAT_G729A,
238 return ast_format_unregister(name);
254 return ASTERISK_GPL_KEY;