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
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
25 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
27 #include "asterisk/lock.h"
28 #include "asterisk/channel.h"
29 #include "asterisk/file.h"
30 #include "asterisk/logger.h"
31 #include "asterisk/sched.h"
32 #include "asterisk/module.h"
33 #include "asterisk/endian.h"
35 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
37 /* Portions of the conversion code are by guido@sienanet.it */
39 struct ast_filestream {
40 void *reserved[AST_RESERVED_POINTERS];
41 /* Believe it or not, we must decode/recode to account for the
43 /* This is what a filestream means to us */
44 int fd; /* Descriptor */
45 struct ast_frame fr; /* Frame information */
46 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
47 char empty; /* Empty character */
48 unsigned char g729[20]; /* Two Real G729 Frames */
52 AST_MUTEX_DEFINE_STATIC(g729_lock);
53 static int glistcnt = 0;
55 static char *name = "g729";
56 static char *desc = "Raw G729 data";
57 static char *exts = "g729";
59 static struct ast_filestream *g729_open(int fd)
61 /* We don't have any header to read or anything really, but
62 if we did, it would go here. We also might want to check
63 and be sure it's a valid file. */
64 struct ast_filestream *tmp;
65 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
66 memset(tmp, 0, sizeof(struct ast_filestream));
67 if (ast_mutex_lock(&g729_lock)) {
68 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
73 tmp->fr.data = tmp->g729;
74 tmp->fr.frametype = AST_FRAME_VOICE;
75 tmp->fr.subclass = AST_FORMAT_G729A;
76 /* datalen will vary for each frame */
80 ast_mutex_unlock(&g729_lock);
81 ast_update_use_count();
86 static struct ast_filestream *g729_rewrite(int fd, const char *comment)
88 /* We don't have any header to read or anything really, but
89 if we did, it would go here. We also might want to check
90 and be sure it's a valid file. */
91 struct ast_filestream *tmp;
92 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
93 memset(tmp, 0, sizeof(struct ast_filestream));
94 if (ast_mutex_lock(&g729_lock)) {
95 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
101 ast_mutex_unlock(&g729_lock);
102 ast_update_use_count();
104 ast_log(LOG_WARNING, "Out of memory\n");
108 static void g729_close(struct ast_filestream *s)
110 if (ast_mutex_lock(&g729_lock)) {
111 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
115 ast_mutex_unlock(&g729_lock);
116 ast_update_use_count();
122 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
125 /* Send a frame from the file to the appropriate channel */
126 s->fr.frametype = AST_FRAME_VOICE;
127 s->fr.subclass = AST_FORMAT_G729A;
128 s->fr.offset = AST_FRIENDLY_OFFSET;
132 s->fr.data = s->g729;
133 if ((res = read(s->fd, s->g729, 20)) != 20) {
134 if (res && (res != 10))
135 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
138 *whennext = s->fr.samples;
142 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
145 if (f->frametype != AST_FRAME_VOICE) {
146 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
149 if (f->subclass != AST_FORMAT_G729A) {
150 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
153 if (f->datalen % 10) {
154 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
157 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
158 ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
164 static char *g729_getcomment(struct ast_filestream *s)
169 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
172 off_t min,cur,max,offset=0;
174 cur = lseek(fs->fd, 0, SEEK_CUR);
175 max = lseek(fs->fd, 0, SEEK_END);
177 bytes = 20 * (sample_offset / 160);
178 if (whence == SEEK_SET)
180 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
181 offset = cur + bytes;
182 else if (whence == SEEK_END)
183 offset = max - bytes;
184 if (whence != SEEK_FORCECUR) {
185 offset = (offset > max)?max:offset;
187 /* protect against seeking beyond begining. */
188 offset = (offset < min)?min:offset;
189 if (lseek(fs->fd, offset, SEEK_SET) < 0)
194 static int g729_trunc(struct ast_filestream *fs)
196 /* Truncate file to current length */
197 if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
202 static long g729_tell(struct ast_filestream *fs)
205 offset = lseek(fs->fd, 0, SEEK_CUR);
206 return (offset/20)*160;
211 return ast_format_register(name, exts, AST_FORMAT_G729A,
227 return ast_format_unregister(name);
243 return ASTERISK_GPL_KEY;