Version 0.1.3 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, Mark Spencer
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                 tmp->orig.tv_usec = 0;
106                 tmp->orig.tv_sec = 0;
107                 glistcnt++;
108                 pthread_mutex_unlock(&g723_lock);
109                 ast_update_use_count();
110         } else
111                 ast_log(LOG_WARNING, "Out of memory\n");
112         return tmp;
113 }
114
115 static struct ast_frame *g723_read(struct ast_filestream *s)
116 {
117         return NULL;
118 }
119
120 static void g723_close(struct ast_filestream *s)
121 {
122         struct ast_filestream *tmp, *tmpl = NULL;
123         if (pthread_mutex_lock(&g723_lock)) {
124                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
125                 return;
126         }
127         tmp = glist;
128         while(tmp) {
129                 if (tmp == s) {
130                         if (tmpl)
131                                 tmpl->next = tmp->next;
132                         else
133                                 glist = tmp->next;
134                         break;
135                 }
136                 tmpl = tmp;
137                 tmp = tmp->next;
138         }
139         glistcnt--;
140         if (s->owner) {
141                 s->owner->stream = NULL;
142                 if (s->owner->streamid > -1)
143                         ast_sched_del(s->owner->sched, s->owner->streamid);
144                 s->owner->streamid = -1;
145         }
146         pthread_mutex_unlock(&g723_lock);
147         ast_update_use_count();
148         if (!tmp) 
149                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
150         close(s->fd);
151         free(s);
152 }
153
154 static int ast_read_callback(void *data)
155 {
156         u_int16_t size;
157         u_int32_t delay = -1;
158         int looper = 1;
159         int retval = 0;
160         int res;
161         struct ast_filestream *s = data;
162         /* Send a frame from the file to the appropriate channel */
163         while(looper) {
164                 if (read(s->fd, &size, 2) != 2) {
165                         /* Out of data, or the file is no longer valid.  In any case
166                            go ahead and stop the stream */
167                         s->owner->streamid = -1;
168                         return 0;
169                 }
170                 /* Looks like we have a frame to read from here */
171                 size = ntohs(size);
172                 if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
173                         ast_log(LOG_WARNING, "Size %d is invalid\n", size);
174                         /* The file is apparently no longer any good, as we
175                            shouldn't ever get frames even close to this 
176                            size.  */
177                         s->owner->streamid = -1;
178                         return 0;
179                 }
180                 /* Read the data into the buffer */
181                 s->fr->offset = AST_FRIENDLY_OFFSET;
182                 s->fr->datalen = size;
183                 s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
184                 if ((res = read(s->fd, s->fr->data , size)) != size) {
185                         ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
186                         s->owner->streamid = -1;
187                         return 0;
188                 }
189                 /* Read the delay for the next packet, and schedule again if necessary */
190                 if (read(s->fd, &delay, 4) == 4) 
191                         delay = ntohl(delay);
192                 else
193                         delay = -1;
194                 /* Average out frames <= 50 ms */
195                 if (delay < 50)
196                         s->fr->timelen = 30;
197                 else
198                         s->fr->timelen = delay;
199                 /* Unless there is no delay, we're going to exit out as soon as we
200                    have processed the current frame. */
201                 if (delay > VOFR_FUDGE) {
202                         looper = 0;
203                         /* If there is a delay, lets schedule the next event */
204                         if (delay != s->lasttimeout) {
205                                 /* We'll install the next timeout now. */
206                                 s->owner->streamid = ast_sched_add(s->owner->sched, 
207                                                                                                           delay - VOFR_FUDGE, 
208                                                                                                           ast_read_callback, s);
209                                 
210                                 s->lasttimeout = delay;
211                         } else
212                                 /* Just come back again at the same time */
213                                 retval = -1;
214                 }
215                 /* Lastly, process the frame */
216                 if (ast_write(s->owner, s->fr)) {
217                         ast_log(LOG_WARNING, "Failed to write frame\n");
218                         s->owner->streamid = -1;
219                         return 0;
220                 }
221         }
222         return retval;
223 }
224
225 static int g723_apply(struct ast_channel *c, struct ast_filestream *s)
226 {
227         u_int32_t delay;
228         /* Select our owner for this stream, and get the ball rolling. */
229         s->owner = c;
230         /* Read and ignore the first delay */
231         if (read(s->fd, &delay, 4) != 4) {
232                 /* Empty file */
233                 return 0;
234         }
235         ast_read_callback(s);
236         return 0;
237 }
238
239 static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
240 {
241         struct timeval now;
242         u_int32_t delay;
243         u_int16_t size;
244         int res;
245         if (fs->fr) {
246                 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
247                 return -1;
248         }
249         if (f->frametype != AST_FRAME_VOICE) {
250                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
251                 return -1;
252         }
253         if (f->subclass != AST_FORMAT_G723_1) {
254                 ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
255                 return -1;
256         }
257         if (!(fs->orig.tv_usec || fs->orig.tv_sec)) {
258                 /* First frame should have zeros for delay */
259                 delay = 0;
260                 if (gettimeofday(&fs->orig, NULL)) {
261                         ast_log(LOG_WARNING, "gettimeofday() failed??  What is this?  Y2k?\n");
262                         return -1;
263                 }
264         } else {
265                 if (gettimeofday(&now, NULL)) {
266                         ast_log(LOG_WARNING, "gettimeofday() failed??  What is this?  Y2k?\n");
267                         return -1;
268                 }
269                 delay = (now.tv_sec - fs->orig.tv_sec) * 1000 + (now.tv_usec - fs->orig.tv_usec) / 1000;
270                 delay = htonl(delay);
271                 fs->orig.tv_sec = now.tv_sec;
272                 fs->orig.tv_usec = now.tv_usec;
273         }
274         if ((res = write(fs->fd, &delay, 4)) != 4) {
275                 ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
276                 return -1;
277         }
278         size = htons(f->datalen);
279         if ((res =write(fs->fd, &size, 2)) != 2) {
280                 ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
281                 return -1;
282         }
283         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
284                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
285                 return -1;
286         }       
287         return 0;
288 }
289
290 static char *g723_getcomment(struct ast_filestream *s)
291 {
292         return NULL;
293 }
294
295 int load_module()
296 {
297         return ast_format_register(name, exts, AST_FORMAT_G723_1,
298                                                                 g723_open,
299                                                                 g723_rewrite,
300                                                                 g723_apply,
301                                                                 g723_write,
302                                                                 g723_read,
303                                                                 g723_close,
304                                                                 g723_getcomment);
305                                                                 
306                                                                 
307 }
308
309 int unload_module()
310 {
311         struct ast_filestream *tmp, *tmpl;
312         if (pthread_mutex_lock(&g723_lock)) {
313                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
314                 return -1;
315         }
316         tmp = glist;
317         while(tmp) {
318                 if (tmp->owner)
319                         ast_softhangup(tmp->owner);
320                 tmpl = tmp;
321                 tmp = tmp->next;
322                 free(tmpl);
323         }
324         pthread_mutex_unlock(&g723_lock);
325         return ast_format_unregister(name);
326 }       
327
328 int usecount()
329 {
330         int res;
331         if (pthread_mutex_lock(&g723_lock)) {
332                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
333                 return -1;
334         }
335         res = glistcnt;
336         pthread_mutex_unlock(&g723_lock);
337         return res;
338 }
339
340 char *description()
341 {
342         return desc;
343 }
344