Merge FreeBSD locking fixes (bug #1411)
[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 <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <stdlib.h>
23 #include <sys/time.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <pthread.h>
29 #ifdef __linux__
30 #include <endian.h>
31 #else
32 #include <machine/endian.h>
33 #endif
34 #include "msgsm.h"
35
36 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
37
38 /* Portions of the conversion code are by guido@sienanet.it */
39
40 /* silent gsm frame */
41 /* begin binary data: */
42 char gsm_silence[] = /* 33 */
43 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
44 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
45 ,0x92,0x49,0x24};
46 /* end binary data. size = 33 bytes */
47
48 struct ast_filestream {
49         void *reserved[AST_RESERVED_POINTERS];
50         /* Believe it or not, we must decode/recode to account for the
51            weird MS format */
52         /* This is what a filestream means to us */
53         int fd; /* Descriptor */
54         struct ast_frame fr;                            /* Frame information */
55         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
56         char empty;                                                     /* Empty character */
57         unsigned char gsm[66];                          /* Two Real GSM Frames */
58 };
59
60
61 AST_MUTEX_DEFINE_STATIC(gsm_lock);
62 static int glistcnt = 0;
63
64 static char *name = "gsm";
65 static char *desc = "Raw GSM data";
66 static char *exts = "gsm";
67
68 static struct ast_filestream *gsm_open(int fd)
69 {
70         /* We don't have any header to read or anything really, but
71            if we did, it would go here.  We also might want to check
72            and be sure it's a valid file.  */
73         struct ast_filestream *tmp;
74         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
75                 memset(tmp, 0, sizeof(struct ast_filestream));
76                 if (ast_mutex_lock(&gsm_lock)) {
77                         ast_log(LOG_WARNING, "Unable to lock gsm list\n");
78                         free(tmp);
79                         return NULL;
80                 }
81                 tmp->fd = fd;
82                 tmp->fr.data = tmp->gsm;
83                 tmp->fr.frametype = AST_FRAME_VOICE;
84                 tmp->fr.subclass = AST_FORMAT_GSM;
85                 /* datalen will vary for each frame */
86                 tmp->fr.src = name;
87                 tmp->fr.mallocd = 0;
88                 glistcnt++;
89                 ast_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_mutex_lock(&gsm_lock)) {
104                         ast_log(LOG_WARNING, "Unable to lock gsm list\n");
105                         free(tmp);
106                         return NULL;
107                 }
108                 tmp->fd = fd;
109                 glistcnt++;
110                 ast_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 void gsm_close(struct ast_filestream *s)
118 {
119         if (ast_mutex_lock(&gsm_lock)) {
120                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
121                 return;
122         }
123         glistcnt--;
124         ast_mutex_unlock(&gsm_lock);
125         ast_update_use_count();
126         close(s->fd);
127         free(s);
128 }
129
130 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
131 {
132         int res;
133         s->fr.frametype = AST_FRAME_VOICE;
134         s->fr.subclass = AST_FORMAT_GSM;
135         s->fr.offset = AST_FRIENDLY_OFFSET;
136         s->fr.samples = 160;
137         s->fr.datalen = 33;
138         s->fr.mallocd = 0;
139         s->fr.data = s->gsm;
140         if ((res = read(s->fd, s->gsm, 33)) != 33) {
141                 if (res)
142                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
143                 return NULL;
144         }
145         *whennext = 160;
146         return &s->fr;
147 }
148
149 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
150 {
151         int res;
152         unsigned char gsm[66];
153         if (f->frametype != AST_FRAME_VOICE) {
154                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
155                 return -1;
156         }
157         if (f->subclass != AST_FORMAT_GSM) {
158                 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
159                 return -1;
160         }
161         if (!(f->datalen % 65)) {
162                 /* This is in MSGSM format, need to be converted */
163                 int len=0;
164                 while(len < f->datalen) {
165                         conv65(f->data + len, gsm);
166                         if ((res = write(fs->fd, gsm, 66)) != 66) {
167                                 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
168                                 return -1;
169                         }
170                         len += 65;
171                 }
172         } else {
173                 if (f->datalen % 33) {
174                         ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
175                         return -1;
176                 }
177                 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
178                                 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
179                                 return -1;
180                 }
181         }
182         return 0;
183 }
184
185 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
186 {
187         off_t offset=0,min,cur,max,distance;
188         
189         min = 0;
190         cur = lseek(fs->fd, 0, SEEK_CUR);
191         max = lseek(fs->fd, 0, SEEK_END);
192         /* have to fudge to frame here, so not fully to sample */
193         distance = (sample_offset/160) * 33;
194         if(whence == SEEK_SET)
195                 offset = distance;
196         else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
197                 offset = distance + cur;
198         else if(whence == SEEK_END)
199                 offset = max - distance;
200         // Always protect against seeking past the begining.
201         offset = (offset < min)?min:offset;
202         if (whence != SEEK_FORCECUR) {
203                 offset = (offset > max)?max:offset;
204         } else if (offset > max) {
205                 int i;
206                 lseek(fs->fd, 0, SEEK_END);
207                 for (i=0; i< (offset - max) / 33; i++) {
208                         write(fs->fd, gsm_silence, 33);
209                 }
210         }
211         return lseek(fs->fd, offset, SEEK_SET);
212 }
213
214 static int gsm_trunc(struct ast_filestream *fs)
215 {
216         return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
217 }
218
219 static long gsm_tell(struct ast_filestream *fs)
220 {
221         off_t offset;
222         offset = lseek(fs->fd, 0, SEEK_CUR);
223         return (offset/33)*160;
224 }
225
226 static char *gsm_getcomment(struct ast_filestream *s)
227 {
228         return NULL;
229 }
230
231 int load_module()
232 {
233         return ast_format_register(name, exts, AST_FORMAT_GSM,
234                                                                 gsm_open,
235                                                                 gsm_rewrite,
236                                                                 gsm_write,
237                                                                 gsm_seek,
238                                                                 gsm_trunc,
239                                                                 gsm_tell,
240                                                                 gsm_read,
241                                                                 gsm_close,
242                                                                 gsm_getcomment);
243                                                                 
244                                                                 
245 }
246
247 int unload_module()
248 {
249         return ast_format_unregister(name);
250 }       
251
252 int usecount()
253 {
254         int res;
255         if (ast_mutex_lock(&gsm_lock)) {
256                 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
257                 return -1;
258         }
259         res = glistcnt;
260         ast_mutex_unlock(&gsm_lock);
261         return res;
262 }
263
264 char *description()
265 {
266         return desc;
267 }
268
269
270 char *key()
271 {
272         return ASTERISK_GPL_KEY;
273 }