More include fixes
[asterisk/asterisk.git] / formats / format_gsm.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Save to raw, headerless GSM 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 #ifdef __linux__
29 #include <endian.h>
30 #else
31 #include <machine/endian.h>
32 #endif
33
34 /* Some Ideas for this code came from makegsme.c by Jeffery Chilton */
35
36 /* Portions of the conversion code are by guido@sienanet.it */
37
38 struct ast_filestream {
39         void *reserved[AST_RESERVED_POINTERS];
40         /* Believe it or not, we must decode/recode to account for the
41            weird MS format */
42         /* This is what a filestream means to us */
43         int fd; /* Descriptor */
44         struct ast_channel *owner;
45         struct ast_frame fr;                            /* Frame information */
46         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
47         char empty;                                                     /* Empty character */
48         unsigned char gsm[33];                          /* Two Real GSM Frames */
49         int lasttimeout;
50         struct timeval last;
51         int adj;
52         struct ast_filestream *next;
53 };
54
55
56 static struct ast_filestream *glist = NULL;
57 static pthread_mutex_t gsm_lock = AST_MUTEX_INITIALIZER;
58 static int glistcnt = 0;
59
60 static char *name = "gsm";
61 static char *desc = "Raw GSM data";
62 static char *exts = "gsm";
63
64 static struct ast_filestream *gsm_open(int fd)
65 {
66         /* We don't have any header to read or anything really, but
67            if we did, it would go here.  We also might want to check
68            and be sure it's a valid file.  */
69         struct ast_filestream *tmp;
70         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
71                 memset(tmp, 0, sizeof(struct ast_filestream));
72                 if (ast_pthread_mutex_lock(&gsm_lock)) {
73                         ast_log(LOG_WARNING, "Unable to lock gsm list\n");
74                         free(tmp);
75                         return NULL;
76                 }
77                 tmp->next = glist;
78                 glist = tmp;
79                 tmp->fd = fd;
80                 tmp->owner = NULL;
81                 tmp->fr.data = tmp->gsm;
82                 tmp->fr.frametype = AST_FRAME_VOICE;
83                 tmp->fr.subclass = AST_FORMAT_GSM;
84                 /* datalen will vary for each frame */
85                 tmp->fr.src = name;
86                 tmp->fr.mallocd = 0;
87                 tmp->lasttimeout = -1;
88                 glistcnt++;
89                 ast_pthread_mutex_unlock(&gsm_lock);
90                 ast_update_use_count();
91         }
92         return tmp;
93 }
94
95 static struct ast_filestream *gsm_rewrite(int fd, char *comment)
96 {
97         /* We don't have any header to read or anything really, but
98            if we did, it would go here.  We also might want to check
99            and be sure it's a valid file.  */
100         struct ast_filestream *tmp;
101         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
102                 memset(tmp, 0, sizeof(struct ast_filestream));
103                 if (ast_pthread_mutex_lock(&gsm_lock)) {
104                         ast_log(LOG_WARNING, "Unable to lock gsm list\n");
105                         free(tmp);
106                         return NULL;
107                 }
108                 tmp->next = glist;
109                 glist = tmp;
110                 tmp->fd = fd;
111                 tmp->owner = NULL;
112                 tmp->lasttimeout = -1;
113                 glistcnt++;
114                 ast_pthread_mutex_unlock(&gsm_lock);
115                 ast_update_use_count();
116         } else
117                 ast_log(LOG_WARNING, "Out of memory\n");
118         return tmp;
119 }
120
121 static struct ast_frame *gsm_read(struct ast_filestream *s)
122 {
123         return NULL;
124 }
125
126 static void gsm_close(struct ast_filestream *s)
127 {
128         struct ast_filestream *tmp, *tmpl = NULL;
129         if (ast_pthread_mutex_lock(&gsm_lock)) {
130                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
131                 return;
132         }
133         tmp = glist;
134         while(tmp) {
135                 if (tmp == s) {
136                         if (tmpl)
137                                 tmpl->next = tmp->next;
138                         else
139                                 glist = tmp->next;
140                         break;
141                 }
142                 tmpl = tmp;
143                 tmp = tmp->next;
144         }
145         glistcnt--;
146         if (s->owner) {
147                 s->owner->stream = NULL;
148                 if (s->owner->streamid > -1)
149                         ast_sched_del(s->owner->sched, s->owner->streamid);
150                 s->owner->streamid = -1;
151         }
152         ast_pthread_mutex_unlock(&gsm_lock);
153         ast_update_use_count();
154         if (!tmp) 
155                 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
156         close(s->fd);
157         free(s);
158         s = NULL;
159 }
160
161 static int ast_read_callback(void *data)
162 {
163         int retval = 0;
164         int res;
165         int delay = 20;
166         struct ast_filestream *s = data;
167         struct timeval tv;
168         /* Send a frame from the file to the appropriate channel */
169
170         s->fr.frametype = AST_FRAME_VOICE;
171         s->fr.subclass = AST_FORMAT_GSM;
172         s->fr.offset = AST_FRIENDLY_OFFSET;
173         s->fr.samples = 160;
174         s->fr.datalen = 33;
175         s->fr.mallocd = 0;
176         s->fr.data = s->gsm;
177         if ((res = read(s->fd, s->gsm, 33)) != 33) {
178                 if (res)
179                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
180                 s->owner->streamid = -1;
181                 return 0;
182         }
183         /* Lastly, process the frame */
184         if (ast_write(s->owner, &s->fr)) {
185                 ast_log(LOG_WARNING, "Failed to write frame\n");
186                 s->owner->streamid = -1;
187                 return 0;
188         }
189         if (s->last.tv_usec || s->last.tv_usec) {
190                 int ms;
191                 gettimeofday(&tv, NULL);
192                 ms = 1000 * (tv.tv_sec - s->last.tv_sec) + 
193                         (tv.tv_usec - s->last.tv_usec) / 1000;
194                 s->last.tv_sec = tv.tv_sec;
195                 s->last.tv_usec = tv.tv_usec;
196                 if ((ms - delay) * (ms - delay) > 4) {
197                         /* Compensate if we're more than 2 ms off */
198                         s->adj -= (ms - delay);
199                 }
200 #if 0
201                 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
202 #endif
203                 delay += s->adj;
204                 if (delay < 1)
205                         delay = 1;
206         } else
207                 gettimeofday(&s->last, NULL);
208         if (s->lasttimeout != delay) {
209                 /* We'll install the next timeout now. */
210                 s->owner->streamid = ast_sched_add(s->owner->sched,
211                                 delay, ast_read_callback, s); 
212                 s->lasttimeout = delay;
213         } else {
214                 /* Just come back again at the same time */
215                 retval = -1;
216         }
217         return retval;
218 }
219
220 static int gsm_apply(struct ast_channel *c, struct ast_filestream *s)
221 {
222         /* Select our owner for this stream, and get the ball rolling. */
223         s->owner = c;
224         return 0;
225 }
226
227 static int gsm_play(struct ast_filestream *s)
228 {
229         ast_read_callback(s);
230         return 0;
231 }
232
233 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
234 {
235         int res;
236         if (f->frametype != AST_FRAME_VOICE) {
237                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
238                 return -1;
239         }
240         if (f->subclass != AST_FORMAT_GSM) {
241                 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
242                 return -1;
243         }
244         if (f->datalen % 33) {
245                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
246                 return -1;
247         }
248         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
249                         ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
250                         return -1;
251         }
252         return 0;
253 }
254
255 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
256 {
257         off_t offset,min,cur,max,distance;
258         
259         min = 0;
260         cur = lseek(fs->fd, 0, SEEK_CUR);
261         max = lseek(fs->fd, 0, SEEK_END);
262         /* have to fudge to frame here, so not fully to sample */
263         distance = (sample_offset/160) * 33;
264         if(whence == SEEK_SET)
265                 offset = distance;
266         if(whence == SEEK_CUR)
267                 offset = distance + cur;
268         if(whence == SEEK_END)
269                 offset = max - distance;
270         offset = (offset > max)?max:offset;
271         offset = (offset < min)?min:offset;
272         return lseek(fs->fd, offset, SEEK_SET);
273 }
274
275 static int gsm_trunc(struct ast_filestream *fs)
276 {
277         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
278 }
279
280 static long gsm_tell(struct ast_filestream *fs)
281 {
282         off_t offset;
283         offset = lseek(fs->fd, 0, SEEK_CUR);
284         return (offset/33)*160;
285 }
286
287 static char *gsm_getcomment(struct ast_filestream *s)
288 {
289         return NULL;
290 }
291
292 int load_module()
293 {
294         return ast_format_register(name, exts, AST_FORMAT_GSM,
295                                                                 gsm_open,
296                                                                 gsm_rewrite,
297                                                                 gsm_apply,
298                                                                 gsm_play,
299                                                                 gsm_write,
300                                                                 gsm_seek,
301                                                                 gsm_trunc,
302                                                                 gsm_tell,
303                                                                 gsm_read,
304                                                                 gsm_close,
305                                                                 gsm_getcomment);
306                                                                 
307                                                                 
308 }
309
310 int unload_module()
311 {
312         struct ast_filestream *tmp, *tmpl;
313         if (ast_pthread_mutex_lock(&gsm_lock)) {
314                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
315                 return -1;
316         }
317         tmp = glist;
318         while(tmp) {
319                 if (tmp->owner)
320                         ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
321                 tmpl = tmp;
322                 tmp = tmp->next;
323                 free(tmpl);
324         }
325         ast_pthread_mutex_unlock(&gsm_lock);
326         return ast_format_unregister(name);
327 }       
328
329 int usecount()
330 {
331         int res;
332         if (ast_pthread_mutex_lock(&gsm_lock)) {
333                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
334                 return -1;
335         }
336         res = glistcnt;
337         ast_pthread_mutex_unlock(&gsm_lock);
338         return res;
339 }
340
341 char *description()
342 {
343         return desc;
344 }
345
346
347 char *key()
348 {
349         return ASTERISK_GPL_KEY;
350 }