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