2 * Asterisk -- A telephony toolkit for Linux.
4 * Save to raw, headerless G729 data.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/file.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/sched.h>
19 #include <asterisk/module.h>
20 #include <asterisk/endian.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
30 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
32 /* Portions of the conversion code are by guido@sienanet.it */
34 struct ast_filestream {
35 void *reserved[AST_RESERVED_POINTERS];
36 /* Believe it or not, we must decode/recode to account for the
38 /* This is what a filestream means to us */
39 int fd; /* Descriptor */
40 struct ast_frame fr; /* Frame information */
41 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
42 char empty; /* Empty character */
43 unsigned char g729[20]; /* Two Real G729 Frames */
47 AST_MUTEX_DEFINE_STATIC(g729_lock);
48 static int glistcnt = 0;
50 static char *name = "g729";
51 static char *desc = "Raw G729 data";
52 static char *exts = "g729";
54 static struct ast_filestream *g729_open(int fd)
56 /* We don't have any header to read or anything really, but
57 if we did, it would go here. We also might want to check
58 and be sure it's a valid file. */
59 struct ast_filestream *tmp;
60 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
61 memset(tmp, 0, sizeof(struct ast_filestream));
62 if (ast_mutex_lock(&g729_lock)) {
63 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
68 tmp->fr.data = tmp->g729;
69 tmp->fr.frametype = AST_FRAME_VOICE;
70 tmp->fr.subclass = AST_FORMAT_G729A;
71 /* datalen will vary for each frame */
75 ast_mutex_unlock(&g729_lock);
76 ast_update_use_count();
81 static struct ast_filestream *g729_rewrite(int fd, const char *comment)
83 /* We don't have any header to read or anything really, but
84 if we did, it would go here. We also might want to check
85 and be sure it's a valid file. */
86 struct ast_filestream *tmp;
87 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
88 memset(tmp, 0, sizeof(struct ast_filestream));
89 if (ast_mutex_lock(&g729_lock)) {
90 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
96 ast_mutex_unlock(&g729_lock);
97 ast_update_use_count();
99 ast_log(LOG_WARNING, "Out of memory\n");
103 static void g729_close(struct ast_filestream *s)
105 if (ast_mutex_lock(&g729_lock)) {
106 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
110 ast_mutex_unlock(&g729_lock);
111 ast_update_use_count();
117 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
120 /* Send a frame from the file to the appropriate channel */
121 s->fr.frametype = AST_FRAME_VOICE;
122 s->fr.subclass = AST_FORMAT_G729A;
123 s->fr.offset = AST_FRIENDLY_OFFSET;
127 s->fr.data = s->g729;
128 if ((res = read(s->fd, s->g729, 20)) != 20) {
129 if (res && (res != 10))
130 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
133 *whennext = s->fr.samples;
137 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
140 if (f->frametype != AST_FRAME_VOICE) {
141 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
144 if (f->subclass != AST_FORMAT_G729A) {
145 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
148 if (f->datalen % 10) {
149 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
152 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
153 ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
159 static char *g729_getcomment(struct ast_filestream *s)
164 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
167 off_t min,cur,max,offset=0;
169 cur = lseek(fs->fd, 0, SEEK_CUR);
170 max = lseek(fs->fd, 0, SEEK_END);
172 bytes = 20 * (sample_offset / 160);
173 if (whence == SEEK_SET)
175 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
176 offset = cur + bytes;
177 else if (whence == SEEK_END)
178 offset = max - bytes;
179 if (whence != SEEK_FORCECUR) {
180 offset = (offset > max)?max:offset;
182 /* protect against seeking beyond begining. */
183 offset = (offset < min)?min:offset;
184 if (lseek(fs->fd, offset, SEEK_SET) < 0)
189 static int g729_trunc(struct ast_filestream *fs)
191 /* Truncate file to current length */
192 if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
197 static long g729_tell(struct ast_filestream *fs)
200 offset = lseek(fs->fd, 0, SEEK_CUR);
201 return (offset/20)*160;
206 return ast_format_register(name, exts, AST_FORMAT_G729A,
222 return ast_format_unregister(name);
228 if (ast_mutex_lock(&g729_lock)) {
229 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
233 ast_mutex_unlock(&g729_lock);
245 return ASTERISK_GPL_KEY;