Version 0.1.0 from FTP
[asterisk/asterisk.git] / formats / format_g723.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Old-style G.723 frame/timestamp format.
5  * 
6  * Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
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>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <pthread.h>
26 #include <sys/time.h>
27 #include "../channels/adtranvofr.h"
28
29
30 #define G723_MAX_SIZE 1024
31
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 */
43 };
44
45
46 static struct ast_filestream *glist = NULL;
47 static pthread_mutex_t g723_lock = PTHREAD_MUTEX_INITIALIZER;
48 static int glistcnt = 0;
49
50 static char *name = "g723sf";
51 static char *desc = "G.723.1 Simple Timestamp File Format";
52 static char *exts = "g723";
53
54 static struct ast_filestream *g723_open(int fd)
55 {
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");
63                         free(tmp);
64                         return NULL;
65                 }
66                 tmp->next = glist;
67                 glist = tmp;
68                 tmp->fd = fd;
69                 tmp->owner = NULL;
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 */
75                 tmp->fr->src = name;
76                 tmp->fr->mallocd = 0;
77                 tmp->lasttimeout = -1;
78                 tmp->orig.tv_usec = 0;
79                 tmp->orig.tv_sec = 0;
80                 glistcnt++;
81                 pthread_mutex_unlock(&g723_lock);
82                 ast_update_use_count();
83         }
84         return tmp;
85 }
86
87 static struct ast_filestream *g723_rewrite(int fd, char *comment)
88 {
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");
96                         free(tmp);
97                         return NULL;
98                 }
99                 tmp->next = glist;
100                 glist = tmp;
101                 tmp->fd = fd;
102                 tmp->owner = NULL;
103                 tmp->fr = NULL;
104                 tmp->lasttimeout = -1;
105                 glistcnt++;
106                 pthread_mutex_unlock(&g723_lock);
107                 ast_update_use_count();
108         } else
109                 ast_log(LOG_WARNING, "Out of memory\n");
110         return tmp;
111 }
112
113 static struct ast_frame *g723_read(struct ast_filestream *s)
114 {
115         return NULL;
116 }
117
118 static void g723_close(struct ast_filestream *s)
119 {
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");
123                 return;
124         }
125         tmp = glist;
126         while(tmp) {
127                 if (tmp == s) {
128                         if (tmpl)
129                                 tmpl->next = tmp->next;
130                         else
131                                 glist = tmp->next;
132                         break;
133                 }
134                 tmpl = tmp;
135                 tmp = tmp->next;
136         }
137         glistcnt--;
138         if (s->owner) {
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;
143         }
144         pthread_mutex_unlock(&g723_lock);
145         ast_update_use_count();
146         if (!tmp) 
147                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
148         close(s->fd);
149         free(s);
150 }
151
152 static int ast_read_callback(void *data)
153 {
154         u_int16_t size;
155         u_int32_t delay = -1;
156         int looper = 1;
157         int retval = 0;
158         int res;
159         struct ast_filestream *s = data;
160         /* Send a frame from the file to the appropriate channel */
161         while(looper) {
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;
166                         return 0;
167                 }
168                 /* Looks like we have a frame to read from here */
169                 size = ntohs(size);
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 
174                            size.  */
175                         s->owner->streamid = -1;
176                         return 0;
177                 }
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;
185                         return 0;
186                 }
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);
190                 else
191                         delay = -1;
192                 /* Average out frames <= 40 ms */
193                 if (delay < 41)
194                         s->fr->timelen = 30;
195                 else
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) {
200                         looper = 0;
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, 
205                                                                                                           delay - VOFR_FUDGE, 
206                                                                                                           ast_read_callback, s);
207                                 
208                                 s->lasttimeout = delay;
209                         } else
210                                 /* Just come back again at the same time */
211                                 retval = -1;
212                 }
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;
217                         return 0;
218                 }
219         }
220         return retval;
221 }
222
223 static int g723_apply(struct ast_channel *c, struct ast_filestream *s)
224 {
225         u_int32_t delay;
226         /* Select our owner for this stream, and get the ball rolling. */
227         s->owner = c;
228         /* Read and ignore the first delay */
229         if (read(s->fd, &delay, 4) != 4) {
230                 ast_log(LOG_WARNING, "Bad stream?\n");
231                 return -1;
232         }
233         ast_read_callback(s);
234         return 0;
235 }
236
237 static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
238 {
239         struct timeval now;
240         u_int32_t delay;
241         u_int16_t size;
242         int res;
243         if (fs->fr) {
244                 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
245                 return -1;
246         }
247         if (f->frametype != AST_FRAME_VOICE) {
248                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
249                 return -1;
250         }
251         if (f->subclass != AST_FORMAT_G723_1) {
252                 ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
253                 return -1;
254         }
255         if (!(fs->orig.tv_usec || fs->orig.tv_sec)) {
256                 /* First frame should have zeros for delay */
257                 delay = 0;
258                 if (gettimeofday(&fs->orig, NULL)) {
259                         ast_log(LOG_WARNING, "gettimeofday() failed??  What is this?  Y2k?\n");
260                         return -1;
261                 }
262         } else {
263                 if (gettimeofday(&now, NULL)) {
264                         ast_log(LOG_WARNING, "gettimeofday() failed??  What is this?  Y2k?\n");
265                         return -1;
266                 }
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;
271         }
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));
274                 return -1;
275         }
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));
279                 return -1;
280         }
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));
283                 return -1;
284         }       
285         return 0;
286 }
287
288 char *g723_getcomment(struct ast_filestream *s)
289 {
290         return NULL;
291 }
292
293 int load_module()
294 {
295         return ast_format_register(name, exts, AST_FORMAT_G723_1,
296                                                                 g723_open,
297                                                                 g723_rewrite,
298                                                                 g723_apply,
299                                                                 g723_write,
300                                                                 g723_read,
301                                                                 g723_close,
302                                                                 g723_getcomment);
303                                                                 
304                                                                 
305 }
306
307 int unload_module()
308 {
309         struct ast_filestream *tmp, *tmpl;
310         if (pthread_mutex_lock(&g723_lock)) {
311                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
312                 return -1;
313         }
314         tmp = glist;
315         while(tmp) {
316                 if (tmp->owner)
317                         ast_softhangup(tmp->owner);
318                 tmpl = tmp;
319                 tmp = tmp->next;
320                 free(tmpl);
321         }
322         pthread_mutex_unlock(&g723_lock);
323         return ast_format_unregister(name);
324 }       
325
326 int usecount()
327 {
328         int res;
329         if (pthread_mutex_lock(&g723_lock)) {
330                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
331                 return -1;
332         }
333         res = glistcnt;
334         pthread_mutex_unlock(&g723_lock);
335         return res;
336 }
337
338 char *description()
339 {
340         return desc;
341 }
342