cf5228f8beec7b9d26e1818db1143f4256811fcb
[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/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>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <sys/time.h>
28 #include "../channels/adtranvofr.h"
29
30
31 #define G723_MAX_SIZE 1024
32
33 struct ast_filestream {
34         /* First entry MUST be reserved for the channel type */
35         void *reserved[AST_RESERVED_POINTERS];
36         /* This is what a filestream means to us */
37         int fd; /* Descriptor */
38         struct ast_filestream *next;
39         struct ast_frame *fr;   /* Frame representation of buf */
40         struct timeval orig;    /* Original frame time */
41         char buf[G723_MAX_SIZE + AST_FRIENDLY_OFFSET];  /* Buffer for sending frames, etc */
42 };
43
44
45 AST_MUTEX_DEFINE_STATIC(g723_lock);
46 static int glistcnt = 0;
47
48 static char *name = "g723sf";
49 static char *desc = "G.723.1 Simple Timestamp File Format";
50 static char *exts = "g723|g723sf";
51
52 static struct ast_filestream *g723_open(int fd)
53 {
54         /* We don't have any header to read or anything really, but
55            if we did, it would go here.  We also might want to check
56            and be sure it's a valid file.  */
57         struct ast_filestream *tmp;
58         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
59                 if (ast_mutex_lock(&g723_lock)) {
60                         ast_log(LOG_WARNING, "Unable to lock g723 list\n");
61                         free(tmp);
62                         return NULL;
63                 }
64                 tmp->fd = fd;
65                 tmp->fr = (struct ast_frame *)tmp->buf;
66                 tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
67                 tmp->fr->frametype = AST_FRAME_VOICE;
68                 tmp->fr->subclass = AST_FORMAT_G723_1;
69                 /* datalen will vary for each frame */
70                 tmp->fr->src = name;
71                 tmp->fr->mallocd = 0;
72                 tmp->lasttimeout = -1;
73                 tmp->orig.tv_usec = 0;
74                 tmp->orig.tv_sec = 0;
75                 glistcnt++;
76                 ast_mutex_unlock(&g723_lock);
77                 ast_update_use_count();
78         }
79         return tmp;
80 }
81
82 static struct ast_filestream *g723_rewrite(int fd, char *comment)
83 {
84         /* We don't have any header to read or anything really, but
85            if we did, it would go here.  We also might want to check
86            and be sure it's a valid file.  */
87         struct ast_filestream *tmp;
88         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
89                 if (ast_mutex_lock(&g723_lock)) {
90                         ast_log(LOG_WARNING, "Unable to lock g723 list\n");
91                         free(tmp);
92                         return NULL;
93                 }
94                 tmp->fd = fd;
95                 tmp->owner = NULL;
96                 tmp->fr = NULL;
97                 tmp->lasttimeout = -1;
98                 tmp->orig.tv_usec = 0;
99                 tmp->orig.tv_sec = 0;
100                 glistcnt++;
101                 ast_mutex_unlock(&g723_lock);
102                 ast_update_use_count();
103         } else
104                 ast_log(LOG_WARNING, "Out of memory\n");
105         return tmp;
106 }
107
108 static struct ast_frame *g723_read(struct ast_filestream *s)
109 {
110         return NULL;
111 }
112
113 static void g723_close(struct ast_filestream *s)
114 {
115         struct ast_filestream *tmp, *tmpl = NULL;
116         if (ast_mutex_lock(&g723_lock)) {
117                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
118                 return;
119         }
120         tmp = glist;
121         while(tmp) {
122                 if (tmp == s) {
123                         if (tmpl)
124                                 tmpl->next = tmp->next;
125                         else
126                                 glist = tmp->next;
127                         break;
128                 }
129                 tmpl = tmp;
130                 tmp = tmp->next;
131         }
132         glistcnt--;
133         if (s->owner) {
134                 s->owner->stream = NULL;
135                 if (s->owner->streamid > -1)
136                         ast_sched_del(s->owner->sched, s->owner->streamid);
137                 s->owner->streamid = -1;
138         }
139         ast_mutex_unlock(&g723_lock);
140         ast_update_use_count();
141         if (!tmp) 
142                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
143         close(s->fd);
144         free(s);
145         s = NULL;
146 }
147
148 static int ast_read_callback(void *data)
149 {
150         u_int16_t size;
151         u_int32_t delay = -1;
152         int looper = 1;
153         int retval = 0;
154         int res;
155         struct ast_filestream *s = data;
156         /* Send a frame from the file to the appropriate channel */
157         while(looper) {
158                 if (read(s->fd, &size, 2) != 2) {
159                         /* Out of data, or the file is no longer valid.  In any case
160                            go ahead and stop the stream */
161                         s->owner->streamid = -1;
162                         return 0;
163                 }
164                 /* Looks like we have a frame to read from here */
165                 size = ntohs(size);
166                 if (size > G723_MAX_SIZE - sizeof(struct ast_frame)) {
167                         ast_log(LOG_WARNING, "Size %d is invalid\n", size);
168                         /* The file is apparently no longer any good, as we
169                            shouldn't ever get frames even close to this 
170                            size.  */
171                         s->owner->streamid = -1;
172                         return 0;
173                 }
174                 /* Read the data into the buffer */
175                 s->fr->offset = AST_FRIENDLY_OFFSET;
176                 s->fr->datalen = size;
177                 s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
178                 if ((res = read(s->fd, s->fr->data , size)) != size) {
179                         ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
180                         s->owner->streamid = -1;
181                         return 0;
182                 }
183                 /* Read the delay for the next packet, and schedule again if necessary */
184                 if (read(s->fd, &delay, 4) == 4) 
185                         delay = ntohl(delay);
186                 else
187                         delay = -1;
188 #if 0
189                 /* Average out frames <= 50 ms */
190                 if (delay < 50)
191                         s->fr->timelen = 30;
192                 else
193                         s->fr->timelen = delay;
194 #else
195                 s->fr->samples = 240;
196 #endif
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         /* Select our owner for this stream, and get the ball rolling. */
226         s->owner = c;
227         return 0;
228 }
229
230 static int g723_play(struct ast_filestream *s)
231 {
232         u_int32_t delay;
233         /* Read and ignore the first delay */
234         if (read(s->fd, &delay, 4) != 4) {
235                 /* Empty file */
236                 return 0;
237         }
238         ast_read_callback(s);
239         return 0;
240 }
241
242 static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
243 {
244         struct timeval now;
245         u_int32_t delay;
246         u_int16_t size;
247         int res;
248         if (fs->fr) {
249                 ast_log(LOG_WARNING, "Asked to write on a read stream??\n");
250                 return -1;
251         }
252         if (f->frametype != AST_FRAME_VOICE) {
253                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
254                 return -1;
255         }
256         if (f->subclass != AST_FORMAT_G723_1) {
257                 ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n");
258                 return -1;
259         }
260         if (!(fs->orig.tv_usec || fs->orig.tv_sec)) {
261                 /* First frame should have zeros for delay */
262                 delay = 0;
263                 if (gettimeofday(&fs->orig, NULL)) {
264                         ast_log(LOG_WARNING, "gettimeofday() failed??  What is this?  Y2k?\n");
265                         return -1;
266                 }
267         } else {
268                 if (gettimeofday(&now, NULL)) {
269                         ast_log(LOG_WARNING, "gettimeofday() failed??  What is this?  Y2k?\n");
270                         return -1;
271                 }
272                 delay = (now.tv_sec - fs->orig.tv_sec) * 1000 + (now.tv_usec - fs->orig.tv_usec) / 1000;
273                 delay = htonl(delay);
274                 fs->orig.tv_sec = now.tv_sec;
275                 fs->orig.tv_usec = now.tv_usec;
276         }
277         if (f->datalen <= 0) {
278                 ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
279                 return 0;
280         }
281         if ((res = write(fs->fd, &delay, 4)) != 4) {
282                 ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
283                 return -1;
284         }
285         size = htons(f->datalen);
286         if ((res =write(fs->fd, &size, 2)) != 2) {
287                 ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
288                 return -1;
289         }
290         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
291                 ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
292                 return -1;
293         }       
294         return 0;
295 }
296
297 static int g723_seek(struct ast_filestream *fs, long sample_offset, int whence)
298 {
299         return -1;
300 }
301
302 static int g723_trunc(struct ast_filestream *fs)
303 {
304         return -1;
305 }
306
307 static long g723_tell(struct ast_filestream *fs)
308 {
309         return -1;
310 }
311
312 static char *g723_getcomment(struct ast_filestream *s)
313 {
314         return NULL;
315 }
316
317 int load_module()
318 {
319         return ast_format_register(name, exts, AST_FORMAT_G723_1,
320                                                                 g723_open,
321                                                                 g723_rewrite,
322                                                                 g723_apply,
323                                                                 g723_play,
324                                                                 g723_write,
325                                                                 g723_seek,
326                                                                 g723_trunc,
327                                                                 g723_tell,
328                                                                 g723_read,
329                                                                 g723_close,
330                                                                 g723_getcomment);
331                                                                 
332                                                                 
333 }
334
335 int unload_module()
336 {
337         struct ast_filestream *tmp, *tmpl;
338         if (ast_mutex_lock(&g723_lock)) {
339                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
340                 return -1;
341         }
342         tmp = glist;
343         while(tmp) {
344                 if (tmp->owner)
345                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
346                 tmpl = tmp;
347                 tmp = tmp->next;
348                 free(tmpl);
349         }
350         ast_mutex_unlock(&g723_lock);
351         return ast_format_unregister(name);
352 }       
353
354 int usecount()
355 {
356         int res;
357         if (ast_mutex_lock(&g723_lock)) {
358                 ast_log(LOG_WARNING, "Unable to lock g723 list\n");
359                 return -1;
360         }
361         res = glistcnt;
362         ast_mutex_unlock(&g723_lock);
363         return res;
364 }
365
366 char *description()
367 {
368         return desc;
369 }
370
371
372 char *key()
373 {
374         return ASTERISK_GPL_KEY;
375 }