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