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