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