Version 0.1.12 from FTP
[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 #include <endian.h>
29
30 /* Some Ideas for this code came from makegsme.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 gsm[33];                          /* Two Real GSM 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 gsm_lock = AST_MUTEX_INITIALIZER;
54 static int glistcnt = 0;
55
56 static char *name = "gsm";
57 static char *desc = "Raw GSM data";
58 static char *exts = "gsm";
59
60 static struct ast_filestream *gsm_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(&gsm_lock)) {
69                         ast_log(LOG_WARNING, "Unable to lock gsm 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->gsm;
78                 tmp->fr.frametype = AST_FRAME_VOICE;
79                 tmp->fr.subclass = AST_FORMAT_GSM;
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(&gsm_lock);
86                 ast_update_use_count();
87         }
88         return tmp;
89 }
90
91 static struct ast_filestream *gsm_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(&gsm_lock)) {
100                         ast_log(LOG_WARNING, "Unable to lock gsm 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(&gsm_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 *gsm_read(struct ast_filestream *s)
118 {
119         return NULL;
120 }
121
122 static void gsm_close(struct ast_filestream *s)
123 {
124         struct ast_filestream *tmp, *tmpl = NULL;
125         if (ast_pthread_mutex_lock(&gsm_lock)) {
126                 ast_log(LOG_WARNING, "Unable to lock gsm 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(&gsm_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 }
155
156 static int ast_read_callback(void *data)
157 {
158         int retval = 0;
159         int res;
160         int delay = 20;
161         struct ast_filestream *s = data;
162         struct timeval tv;
163         /* Send a frame from the file to the appropriate channel */
164
165         s->fr.frametype = AST_FRAME_VOICE;
166         s->fr.subclass = AST_FORMAT_GSM;
167         s->fr.offset = AST_FRIENDLY_OFFSET;
168         s->fr.timelen = 20;
169         s->fr.datalen = 33;
170         s->fr.mallocd = 0;
171         s->fr.data = s->gsm;
172         if ((res = read(s->fd, s->gsm, 33)) != 33) {
173                 if (res)
174                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
175                 s->owner->streamid = -1;
176                 return 0;
177         }
178         /* Lastly, process the frame */
179         if (ast_write(s->owner, &s->fr)) {
180                 ast_log(LOG_WARNING, "Failed to write frame\n");
181                 s->owner->streamid = -1;
182                 return 0;
183         }
184         if (s->last.tv_usec || s->last.tv_usec) {
185                 int ms;
186                 gettimeofday(&tv, NULL);
187                 ms = 1000 * (tv.tv_sec - s->last.tv_sec) + 
188                         (tv.tv_usec - s->last.tv_usec) / 1000;
189                 s->last.tv_sec = tv.tv_sec;
190                 s->last.tv_usec = tv.tv_usec;
191                 if ((ms - delay) * (ms - delay) > 4) {
192                         /* Compensate if we're more than 2 ms off */
193                         s->adj -= (ms - delay);
194                 }
195 #if 0
196                 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
197 #endif
198                 delay += s->adj;
199                 if (delay < 1)
200                         delay = 1;
201         } else
202                 gettimeofday(&s->last, NULL);
203         if (s->lasttimeout != delay) {
204                 /* We'll install the next timeout now. */
205                 s->owner->streamid = ast_sched_add(s->owner->sched,
206                                 delay, ast_read_callback, s); 
207                 s->lasttimeout = delay;
208         } else {
209                 /* Just come back again at the same time */
210                 retval = -1;
211         }
212         return retval;
213 }
214
215 static int gsm_apply(struct ast_channel *c, struct ast_filestream *s)
216 {
217         /* Select our owner for this stream, and get the ball rolling. */
218         s->owner = c;
219         ast_read_callback(s);
220         return 0;
221 }
222
223 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
224 {
225         int res;
226         if (f->frametype != AST_FRAME_VOICE) {
227                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
228                 return -1;
229         }
230         if (f->subclass != AST_FORMAT_GSM) {
231                 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
232                 return -1;
233         }
234         if (f->datalen % 33) {
235                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
236                 return -1;
237         }
238         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
239                         ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
240                         return -1;
241         }
242         return 0;
243 }
244
245 static char *gsm_getcomment(struct ast_filestream *s)
246 {
247         return NULL;
248 }
249
250 int load_module()
251 {
252         return ast_format_register(name, exts, AST_FORMAT_GSM,
253                                                                 gsm_open,
254                                                                 gsm_rewrite,
255                                                                 gsm_apply,
256                                                                 gsm_write,
257                                                                 gsm_read,
258                                                                 gsm_close,
259                                                                 gsm_getcomment);
260                                                                 
261                                                                 
262 }
263
264 int unload_module()
265 {
266         struct ast_filestream *tmp, *tmpl;
267         if (ast_pthread_mutex_lock(&gsm_lock)) {
268                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
269                 return -1;
270         }
271         tmp = glist;
272         while(tmp) {
273                 if (tmp->owner)
274                         ast_softhangup(tmp->owner);
275                 tmpl = tmp;
276                 tmp = tmp->next;
277                 free(tmpl);
278         }
279         ast_pthread_mutex_unlock(&gsm_lock);
280         return ast_format_unregister(name);
281 }       
282
283 int usecount()
284 {
285         int res;
286         if (ast_pthread_mutex_lock(&gsm_lock)) {
287                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
288                 return -1;
289         }
290         res = glistcnt;
291         ast_pthread_mutex_unlock(&gsm_lock);
292         return res;
293 }
294
295 char *description()
296 {
297         return desc;
298 }
299
300
301 char *key()
302 {
303         return ASTERISK_GPL_KEY;
304 }