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 <arpa/inet.h>
31 #include <machine/endian.h>
34 /* Some Ideas for this code came from makeg729e.c by Jeffery Chilton */
36 /* Portions of the conversion code are by guido@sienanet.it */
38 struct ast_filestream {
39 void *reserved[AST_RESERVED_POINTERS];
40 /* Believe it or not, we must decode/recode to account for the
42 /* This is what a filestream means to us */
43 int fd; /* Descriptor */
44 struct ast_frame fr; /* Frame information */
45 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
46 char empty; /* Empty character */
47 unsigned char g729[20]; /* Two Real G729 Frames */
51 static pthread_mutex_t g729_lock = AST_MUTEX_INITIALIZER;
52 static int glistcnt = 0;
54 static char *name = "g729";
55 static char *desc = "Raw G729 data";
56 static char *exts = "g729";
58 static struct ast_filestream *g729_open(int fd)
60 /* We don't have any header to read or anything really, but
61 if we did, it would go here. We also might want to check
62 and be sure it's a valid file. */
63 struct ast_filestream *tmp;
64 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
65 memset(tmp, 0, sizeof(struct ast_filestream));
66 if (ast_pthread_mutex_lock(&g729_lock)) {
67 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
72 tmp->fr.data = tmp->g729;
73 tmp->fr.frametype = AST_FRAME_VOICE;
74 tmp->fr.subclass = AST_FORMAT_G729A;
75 /* datalen will vary for each frame */
79 ast_pthread_mutex_unlock(&g729_lock);
80 ast_update_use_count();
85 static struct ast_filestream *g729_rewrite(int fd, char *comment)
87 /* We don't have any header to read or anything really, but
88 if we did, it would go here. We also might want to check
89 and be sure it's a valid file. */
90 struct ast_filestream *tmp;
91 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
92 memset(tmp, 0, sizeof(struct ast_filestream));
93 if (ast_pthread_mutex_lock(&g729_lock)) {
94 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
100 ast_pthread_mutex_unlock(&g729_lock);
101 ast_update_use_count();
103 ast_log(LOG_WARNING, "Out of memory\n");
107 static void g729_close(struct ast_filestream *s)
109 if (ast_pthread_mutex_lock(&g729_lock)) {
110 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
114 ast_pthread_mutex_unlock(&g729_lock);
115 ast_update_use_count();
121 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
124 /* Send a frame from the file to the appropriate channel */
125 s->fr.frametype = AST_FRAME_VOICE;
126 s->fr.subclass = AST_FORMAT_G729A;
127 s->fr.offset = AST_FRIENDLY_OFFSET;
131 s->fr.data = s->g729;
132 if ((res = read(s->fd, s->g729, 20)) != 20) {
134 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
137 *whennext = s->fr.samples;
141 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
144 if (f->frametype != AST_FRAME_VOICE) {
145 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
148 if (f->subclass != AST_FORMAT_G729A) {
149 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
152 if (f->datalen % 20) {
153 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 20\n", f->datalen);
156 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
157 ast_log(LOG_WARNING, "Bad write (%d/20): %s\n", res, strerror(errno));
163 static char *g729_getcomment(struct ast_filestream *s)
168 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
171 off_t min,cur,max,offset;
173 cur = lseek(fs->fd, 0, SEEK_CUR);
174 max = lseek(fs->fd, 0, SEEK_END);
176 bytes = 20 * (sample_offset / 160);
177 if(whence == SEEK_SET)
179 if(whence == SEEK_CUR)
180 offset = cur + bytes;
181 if(whence == SEEK_END)
182 offset = max - bytes;
183 offset = (offset > max)?max:offset;
184 offset = (offset < min)?min:offset;
185 if (lseek(fs->fd, offset, SEEK_SET) < 0)
190 static int g729_trunc(struct ast_filestream *fs)
192 /* Truncate file to current length */
193 if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
198 static long g729_tell(struct ast_filestream *fs)
201 offset = lseek(fs->fd, 0, SEEK_CUR);
202 return (offset/20)*160;
207 return ast_format_register(name, exts, AST_FORMAT_G729A,
223 return ast_format_unregister(name);
229 if (ast_pthread_mutex_lock(&g729_lock)) {
230 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
234 ast_pthread_mutex_unlock(&g729_lock);
246 return ASTERISK_GPL_KEY;