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