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