2 * Asterisk -- A telephony toolkit for Linux.
4 * Old-style G.723 frame/timestamp format.
6 * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
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/channel.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/sched.h>
18 #include <asterisk/module.h>
19 #include <arpa/inet.h>
27 #include "../channels/adtranvofr.h"
30 #define G723_MAX_SIZE 1024
32 struct ast_filestream {
33 /* First entry MUST be reserved for the channel type */
34 void *reserved[AST_RESERVED_POINTERS];
35 /* This is what a filestream means to us */
36 int fd; /* Descriptor */
37 u_int32_t lasttimeout; /* Last amount of timeout */
38 struct ast_channel *owner;
39 struct ast_filestream *next;
40 struct ast_frame *fr; /* Frame representation of buf */
41 struct timeval orig; /* Original frame time */
42 char buf[G723_MAX_SIZE + AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
46 static struct ast_filestream *glist = NULL;
47 static pthread_mutex_t g723_lock = PTHREAD_MUTEX_INITIALIZER;
48 static int glistcnt = 0;
50 static char *name = "g723sf";
51 static char *desc = "G.723.1 Simple Timestamp File Format";
52 static char *exts = "g723";
54 static struct ast_filestream *g723_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 if (pthread_mutex_lock(&g723_lock)) {
62 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
70 tmp->fr = (struct ast_frame *)tmp->buf;
71 tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
72 tmp->fr->frametype = AST_FRAME_VOICE;
73 tmp->fr->subclass = AST_FORMAT_G723_1;
74 /* datalen will vary for each frame */
77 tmp->lasttimeout = -1;
78 tmp->orig.tv_usec = 0;
81 pthread_mutex_unlock(&g723_lock);
82 ast_update_use_count();
87 static struct ast_filestream *g723_rewrite(int fd, char *comment)
89 /* We don't have any header to read or anything really, but
90 if we did, it would go here. We also might want to check
91 and be sure it's a valid file. */
92 struct ast_filestream *tmp;
93 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
94 if (pthread_mutex_lock(&g723_lock)) {
95 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
104 tmp->lasttimeout = -1;
106 pthread_mutex_unlock(&g723_lock);
107 ast_update_use_count();
109 ast_log(LOG_WARNING, "Out of memory\n");
113 static struct ast_frame *g723_read(struct ast_filestream *s)
118 static void g723_close(struct ast_filestream *s)
120 struct ast_filestream *tmp, *tmpl = NULL;
121 if (pthread_mutex_lock(&g723_lock)) {
122 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
129 tmpl->next = tmp->next;
139 s->owner->stream = NULL;
140 if (s->owner->streamid > -1)
141 ast_sched_del(s->owner->sched, s->owner->streamid);
142 s->owner->streamid = -1;
144 pthread_mutex_unlock(&g723_lock);
145 ast_update_use_count();
147 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
152 static int ast_read_callback(void *data)
155 u_int32_t delay = -1;
159 struct ast_filestream *s = data;
160 /* Send a frame from the file to the appropriate channel */
162 if (read(s->fd, &size, 2) != 2) {
163 /* Out of data, or the file is no longer valid. In any case
164 go ahead and stop the stream */
165 s->owner->streamid = -1;
168 /* Looks like we have a frame to read from here */
170 if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
171 ast_log(LOG_WARNING, "Size %d is invalid\n", size);
172 /* The file is apparently no longer any good, as we
173 shouldn't ever get frames even close to this
175 s->owner->streamid = -1;
178 /* Read the data into the buffer */
179 s->fr->offset = AST_FRIENDLY_OFFSET;
180 s->fr->datalen = size;
181 s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
182 if ((res = read(s->fd, s->fr->data , size)) != size) {
183 ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
184 s->owner->streamid = -1;
187 /* Read the delay for the next packet, and schedule again if necessary */
188 if (read(s->fd, &delay, 4) == 4)
189 delay = ntohl(delay);
192 /* Average out frames <= 40 ms */
196 s->fr->timelen = delay;
197 /* Unless there is no delay, we're going to exit out as soon as we
198 have processed the current frame. */
199 if (delay > VOFR_FUDGE) {
201 /* If there is a delay, lets schedule the next event */
202 if (delay != s->lasttimeout) {
203 /* We'll install the next timeout now. */
204 s->owner->streamid = ast_sched_add(s->owner->sched,
206 ast_read_callback, s);
208 s->lasttimeout = delay;
210 /* Just come back again at the same time */
213 /* Lastly, process the frame */
214 if (ast_write(s->owner, s->fr)) {
215 ast_log(LOG_WARNING, "Failed to write frame\n");
216 s->owner->streamid = -1;
223 static int g723_apply(struct ast_channel *c, struct ast_filestream *s)
226 /* Select our owner for this stream, and get the ball rolling. */
228 /* Read and ignore the first delay */
229 if (read(s->fd, &delay, 4) != 4) {
230 ast_log(LOG_WARNING, "Bad stream?\n");
233 ast_read_callback(s);
237 static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
244 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
247 if (f->frametype != AST_FRAME_VOICE) {
248 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
251 if (f->subclass != AST_FORMAT_G723_1) {
252 ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
255 if (!(fs->orig.tv_usec || fs->orig.tv_sec)) {
256 /* First frame should have zeros for delay */
258 if (gettimeofday(&fs->orig, NULL)) {
259 ast_log(LOG_WARNING, "gettimeofday() failed?? What is this? Y2k?\n");
263 if (gettimeofday(&now, NULL)) {
264 ast_log(LOG_WARNING, "gettimeofday() failed?? What is this? Y2k?\n");
267 delay = (now.tv_sec - fs->orig.tv_sec) * 1000 + (now.tv_usec - fs->orig.tv_usec) / 1000;
268 delay = htonl(delay);
269 fs->orig.tv_sec = now.tv_sec;
270 fs->orig.tv_usec = now.tv_usec;
272 if ((res = write(fs->fd, &delay, 4)) != 4) {
273 ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
276 size = htons(f->datalen);
277 if ((res =write(fs->fd, &size, 2)) != 2) {
278 ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
281 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
282 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
288 char *g723_getcomment(struct ast_filestream *s)
295 return ast_format_register(name, exts, AST_FORMAT_G723_1,
309 struct ast_filestream *tmp, *tmpl;
310 if (pthread_mutex_lock(&g723_lock)) {
311 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
317 ast_softhangup(tmp->owner);
322 pthread_mutex_unlock(&g723_lock);
323 return ast_format_unregister(name);
329 if (pthread_mutex_lock(&g723_lock)) {
330 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
334 pthread_mutex_unlock(&g723_lock);