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>
30 /* Some Ideas for this code came from makeg729e.c by Jeffery 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_channel *owner;
41 struct ast_frame fr; /* Frame information */
42 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
43 char empty; /* Empty character */
44 unsigned char g729[20]; /* Two Real G729 Frames */
48 struct ast_filestream *next;
52 static struct ast_filestream *glist = NULL;
53 static pthread_mutex_t g729_lock = AST_MUTEX_INITIALIZER;
54 static int glistcnt = 0;
56 static char *name = "g729";
57 static char *desc = "Raw G729 data";
58 static char *exts = "g729";
60 static struct ast_filestream *g729_open(int fd)
62 /* We don't have any header to read or anything really, but
63 if we did, it would go here. We also might want to check
64 and be sure it's a valid file. */
65 struct ast_filestream *tmp;
66 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
67 memset(tmp, 0, sizeof(struct ast_filestream));
68 if (ast_pthread_mutex_lock(&g729_lock)) {
69 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
77 tmp->fr.data = tmp->g729;
78 tmp->fr.frametype = AST_FRAME_VOICE;
79 tmp->fr.subclass = AST_FORMAT_G729A;
80 /* datalen will vary for each frame */
83 tmp->lasttimeout = -1;
85 ast_pthread_mutex_unlock(&g729_lock);
86 ast_update_use_count();
91 static struct ast_filestream *g729_rewrite(int fd, char *comment)
93 /* We don't have any header to read or anything really, but
94 if we did, it would go here. We also might want to check
95 and be sure it's a valid file. */
96 struct ast_filestream *tmp;
97 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
98 memset(tmp, 0, sizeof(struct ast_filestream));
99 if (ast_pthread_mutex_lock(&g729_lock)) {
100 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
108 tmp->lasttimeout = -1;
110 ast_pthread_mutex_unlock(&g729_lock);
111 ast_update_use_count();
113 ast_log(LOG_WARNING, "Out of memory\n");
117 static struct ast_frame *g729_read(struct ast_filestream *s)
122 static void g729_close(struct ast_filestream *s)
124 struct ast_filestream *tmp, *tmpl = NULL;
125 if (ast_pthread_mutex_lock(&g729_lock)) {
126 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
133 tmpl->next = tmp->next;
143 s->owner->stream = NULL;
144 if (s->owner->streamid > -1)
145 ast_sched_del(s->owner->sched, s->owner->streamid);
146 s->owner->streamid = -1;
148 ast_pthread_mutex_unlock(&g729_lock);
149 ast_update_use_count();
151 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
157 static int ast_read_callback(void *data)
162 struct ast_filestream *s = data;
164 /* Send a frame from the file to the appropriate channel */
166 s->fr.frametype = AST_FRAME_VOICE;
167 s->fr.subclass = AST_FORMAT_G729A;
168 s->fr.offset = AST_FRIENDLY_OFFSET;
172 s->fr.data = s->g729;
173 if ((res = read(s->fd, s->g729, 20)) != 20) {
175 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
176 s->owner->streamid = -1;
179 /* Lastly, process the frame */
180 if (ast_write(s->owner, &s->fr)) {
181 ast_log(LOG_WARNING, "Failed to write frame\n");
182 s->owner->streamid = -1;
185 if (s->last.tv_usec || s->last.tv_usec) {
187 gettimeofday(&tv, NULL);
188 ms = 1000 * (tv.tv_sec - s->last.tv_sec) +
189 (tv.tv_usec - s->last.tv_usec) / 1000;
190 s->last.tv_sec = tv.tv_sec;
191 s->last.tv_usec = tv.tv_usec;
192 if ((ms - delay) * (ms - delay) > 4) {
193 /* Compensate if we're more than 2 ms off */
194 s->adj -= (ms - delay);
197 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
203 gettimeofday(&s->last, NULL);
204 if (s->lasttimeout != delay) {
205 /* We'll install the next timeout now. */
206 s->owner->streamid = ast_sched_add(s->owner->sched,
207 delay, ast_read_callback, s);
208 s->lasttimeout = delay;
210 /* Just come back again at the same time */
216 static int g729_apply(struct ast_channel *c, struct ast_filestream *s)
218 /* Select our owner for this stream, and get the ball rolling. */
223 static int g729_play(struct ast_filestream *s)
225 ast_read_callback(s);
229 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
232 if (f->frametype != AST_FRAME_VOICE) {
233 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
236 if (f->subclass != AST_FORMAT_G729A) {
237 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
240 if (f->datalen % 20) {
241 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 20\n", f->datalen);
244 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
245 ast_log(LOG_WARNING, "Bad write (%d/20): %s\n", res, strerror(errno));
251 static char *g729_getcomment(struct ast_filestream *s)
256 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
259 off_t min,cur,max,offset;
261 cur = lseek(fs->fd, 0, SEEK_CUR);
262 max = lseek(fs->fd, 0, SEEK_END);
264 bytes = 20 * (sample_offset / 160);
265 if(whence == SEEK_SET)
267 if(whence == SEEK_CUR)
268 offset = cur + bytes;
269 if(whence == SEEK_END)
270 offset = max - bytes;
271 offset = (offset > max)?max:offset;
272 offset = (offset < min)?min:offset;
273 if (lseek(fs->fd, offset, SEEK_SET) < 0)
278 static int g729_trunc(struct ast_filestream *fs)
280 /* Truncate file to current length */
281 if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
286 static long g729_tell(struct ast_filestream *fs)
289 offset = lseek(fs->fd, 0, SEEK_CUR);
290 return (offset/20)*160;
295 return ast_format_register(name, exts, AST_FORMAT_G729A,
313 struct ast_filestream *tmp, *tmpl;
314 if (ast_pthread_mutex_lock(&g729_lock)) {
315 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
321 ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
326 ast_pthread_mutex_unlock(&g729_lock);
327 return ast_format_unregister(name);
333 if (ast_pthread_mutex_lock(&g729_lock)) {
334 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
338 ast_pthread_mutex_unlock(&g729_lock);
350 return ASTERISK_GPL_KEY;