5d4aeab4acd67358a04058e5de00186d4c80e997
[asterisk/asterisk.git] / formats / format_g729.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Save to raw, headerless G729 data.
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 <sys/time.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <endian.h>
29
30 /* Some Ideas for this code came from makeg729e.c by Jeffery Chilton */
31
32 /* Portions of the conversion code are by guido@sienanet.it */
33
34 struct ast_filestream {
35         void *reserved[AST_RESERVED_POINTERS];
36         /* Believe it or not, we must decode/recode to account for the
37            weird MS format */
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 */
45         int lasttimeout;
46         struct timeval last;
47         int adj;
48         struct ast_filestream *next;
49 };
50
51
52 static struct ast_filestream *glist = NULL;
53 static pthread_mutex_t g729_lock = AST_MUTEX_INITIALIZER;
54 static int glistcnt = 0;
55
56 static char *name = "g729";
57 static char *desc = "Raw G729 data";
58 static char *exts = "g729";
59
60 static struct ast_filestream *g729_open(int fd)
61 {
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");
70                         free(tmp);
71                         return NULL;
72                 }
73                 tmp->next = glist;
74                 glist = tmp;
75                 tmp->fd = fd;
76                 tmp->owner = NULL;
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 */
81                 tmp->fr.src = name;
82                 tmp->fr.mallocd = 0;
83                 tmp->lasttimeout = -1;
84                 glistcnt++;
85                 ast_pthread_mutex_unlock(&g729_lock);
86                 ast_update_use_count();
87         }
88         return tmp;
89 }
90
91 static struct ast_filestream *g729_rewrite(int fd, char *comment)
92 {
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");
101                         free(tmp);
102                         return NULL;
103                 }
104                 tmp->next = glist;
105                 glist = tmp;
106                 tmp->fd = fd;
107                 tmp->owner = NULL;
108                 tmp->lasttimeout = -1;
109                 glistcnt++;
110                 ast_pthread_mutex_unlock(&g729_lock);
111                 ast_update_use_count();
112         } else
113                 ast_log(LOG_WARNING, "Out of memory\n");
114         return tmp;
115 }
116
117 static struct ast_frame *g729_read(struct ast_filestream *s)
118 {
119         return NULL;
120 }
121
122 static void g729_close(struct ast_filestream *s)
123 {
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");
127                 return;
128         }
129         tmp = glist;
130         while(tmp) {
131                 if (tmp == s) {
132                         if (tmpl)
133                                 tmpl->next = tmp->next;
134                         else
135                                 glist = tmp->next;
136                         break;
137                 }
138                 tmpl = tmp;
139                 tmp = tmp->next;
140         }
141         glistcnt--;
142         if (s->owner) {
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;
147         }
148         ast_pthread_mutex_unlock(&g729_lock);
149         ast_update_use_count();
150         if (!tmp) 
151                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
152         close(s->fd);
153         free(s);
154         s = NULL;
155 }
156
157 static int ast_read_callback(void *data)
158 {
159         int retval = 0;
160         int res;
161         int delay = 20;
162         struct ast_filestream *s = data;
163         struct timeval tv;
164         /* Send a frame from the file to the appropriate channel */
165
166         s->fr.frametype = AST_FRAME_VOICE;
167         s->fr.subclass = AST_FORMAT_G729A;
168         s->fr.offset = AST_FRIENDLY_OFFSET;
169         s->fr.samples = 160;
170         s->fr.datalen = 20;
171         s->fr.mallocd = 0;
172         s->fr.data = s->g729;
173         if ((res = read(s->fd, s->g729, 20)) != 20) {
174                 if (res)
175                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
176                 s->owner->streamid = -1;
177                 return 0;
178         }
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;
183                 return 0;
184         }
185         if (s->last.tv_usec || s->last.tv_usec) {
186                 int ms;
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);
195                 }
196 #if 0
197                 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
198 #endif
199                 delay += s->adj;
200                 if (delay < 1)
201                         delay = 1;
202         } else
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;
209         } else {
210                 /* Just come back again at the same time */
211                 retval = -1;
212         }
213         return retval;
214 }
215
216 static int g729_apply(struct ast_channel *c, struct ast_filestream *s)
217 {
218         /* Select our owner for this stream, and get the ball rolling. */
219         s->owner = c;
220         return 0;
221 }
222
223 static int g729_play(struct ast_filestream *s)
224 {
225         ast_read_callback(s);
226         return 0;
227 }
228
229 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
230 {
231         int res;
232         if (f->frametype != AST_FRAME_VOICE) {
233                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
234                 return -1;
235         }
236         if (f->subclass != AST_FORMAT_G729A) {
237                 ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
238                 return -1;
239         }
240         if (f->datalen % 20) {
241                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 20\n", f->datalen);
242                 return -1;
243         }
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));
246                         return -1;
247         }
248         return 0;
249 }
250
251 static char *g729_getcomment(struct ast_filestream *s)
252 {
253         return NULL;
254 }
255
256 static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
257 {
258         long bytes;
259         off_t min,cur,max,offset;
260         min = 0;
261         cur = lseek(fs->fd, 0, SEEK_CUR);
262         max = lseek(fs->fd, 0, SEEK_END);
263         
264         bytes = 20 * (sample_offset / 160);
265         if(whence == SEEK_SET)
266                 offset = bytes;
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)
274                 return -1;
275         return 0;
276 }
277
278 static int g729_trunc(struct ast_filestream *fs)
279 {
280         /* Truncate file to current length */
281         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
282                 return -1;
283         return 0;
284 }
285
286 static long g729_tell(struct ast_filestream *fs)
287 {
288         off_t offset;
289         offset = lseek(fs->fd, 0, SEEK_CUR);
290         return (offset/20)*160;
291 }
292
293 int load_module()
294 {
295         return ast_format_register(name, exts, AST_FORMAT_G729A,
296                                                                 g729_open,
297                                                                 g729_rewrite,
298                                                                 g729_apply,
299                                                                 g729_play,
300                                                                 g729_write,
301                                                                 g729_seek,
302                                                                 g729_trunc,
303                                                                 g729_tell,
304                                                                 g729_read,
305                                                                 g729_close,
306                                                                 g729_getcomment);
307                                                                 
308                                                                 
309 }
310
311 int unload_module()
312 {
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");
316                 return -1;
317         }
318         tmp = glist;
319         while(tmp) {
320                 if (tmp->owner)
321                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
322                 tmpl = tmp;
323                 tmp = tmp->next;
324                 free(tmpl);
325         }
326         ast_pthread_mutex_unlock(&g729_lock);
327         return ast_format_unregister(name);
328 }       
329
330 int usecount()
331 {
332         int res;
333         if (ast_pthread_mutex_lock(&g729_lock)) {
334                 ast_log(LOG_WARNING, "Unable to lock g729 list\n");
335                 return -1;
336         }
337         res = glistcnt;
338         ast_pthread_mutex_unlock(&g729_lock);
339         return res;
340 }
341
342 char *description()
343 {
344         return desc;
345 }
346
347
348 char *key()
349 {
350         return ASTERISK_GPL_KEY;
351 }