2 * Asterisk -- A telephony toolkit for Linux.
4 * Save to raw, headerless GSM data.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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>
32 #include <machine/endian.h>
36 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
38 /* Portions of the conversion code are by guido@sienanet.it */
40 /* silent gsm frame */
41 /* begin binary data: */
42 char gsm_silence[] = /* 33 */
43 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
44 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
46 /* end binary data. size = 33 bytes */
48 struct ast_filestream {
49 void *reserved[AST_RESERVED_POINTERS];
50 /* Believe it or not, we must decode/recode to account for the
52 /* This is what a filestream means to us */
53 int fd; /* Descriptor */
54 struct ast_frame fr; /* Frame information */
55 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
56 char empty; /* Empty character */
57 unsigned char gsm[66]; /* Two Real GSM Frames */
61 AST_MUTEX_DEFINE_STATIC(gsm_lock);
62 static int glistcnt = 0;
64 static char *name = "gsm";
65 static char *desc = "Raw GSM data";
66 static char *exts = "gsm";
68 static struct ast_filestream *gsm_open(int fd)
70 /* We don't have any header to read or anything really, but
71 if we did, it would go here. We also might want to check
72 and be sure it's a valid file. */
73 struct ast_filestream *tmp;
74 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
75 memset(tmp, 0, sizeof(struct ast_filestream));
76 if (ast_mutex_lock(&gsm_lock)) {
77 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
82 tmp->fr.data = tmp->gsm;
83 tmp->fr.frametype = AST_FRAME_VOICE;
84 tmp->fr.subclass = AST_FORMAT_GSM;
85 /* datalen will vary for each frame */
89 ast_mutex_unlock(&gsm_lock);
90 ast_update_use_count();
95 static struct ast_filestream *gsm_rewrite(int fd, char *comment)
97 /* We don't have any header to read or anything really, but
98 if we did, it would go here. We also might want to check
99 and be sure it's a valid file. */
100 struct ast_filestream *tmp;
101 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
102 memset(tmp, 0, sizeof(struct ast_filestream));
103 if (ast_mutex_lock(&gsm_lock)) {
104 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
110 ast_mutex_unlock(&gsm_lock);
111 ast_update_use_count();
113 ast_log(LOG_WARNING, "Out of memory\n");
117 static void gsm_close(struct ast_filestream *s)
119 if (ast_mutex_lock(&gsm_lock)) {
120 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
124 ast_mutex_unlock(&gsm_lock);
125 ast_update_use_count();
130 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
133 s->fr.frametype = AST_FRAME_VOICE;
134 s->fr.subclass = AST_FORMAT_GSM;
135 s->fr.offset = AST_FRIENDLY_OFFSET;
140 if ((res = read(s->fd, s->gsm, 33)) != 33) {
142 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
149 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
152 unsigned char gsm[66];
153 if (f->frametype != AST_FRAME_VOICE) {
154 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
157 if (f->subclass != AST_FORMAT_GSM) {
158 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
161 if (!(f->datalen % 65)) {
162 /* This is in MSGSM format, need to be converted */
164 while(len < f->datalen) {
165 conv65(f->data + len, gsm);
166 if ((res = write(fs->fd, gsm, 66)) != 66) {
167 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
173 if (f->datalen % 33) {
174 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
177 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
178 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
185 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
187 off_t offset=0,min,cur,max,distance;
190 cur = lseek(fs->fd, 0, SEEK_CUR);
191 max = lseek(fs->fd, 0, SEEK_END);
192 /* have to fudge to frame here, so not fully to sample */
193 distance = (sample_offset/160) * 33;
194 if(whence == SEEK_SET)
196 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
197 offset = distance + cur;
198 else if(whence == SEEK_END)
199 offset = max - distance;
200 // Always protect against seeking past the begining.
201 offset = (offset < min)?min:offset;
202 if (whence != SEEK_FORCECUR) {
203 offset = (offset > max)?max:offset;
204 } else if (offset > max) {
206 lseek(fs->fd, 0, SEEK_END);
207 for (i=0; i< (offset - max) / 33; i++) {
208 write(fs->fd, gsm_silence, 33);
211 return lseek(fs->fd, offset, SEEK_SET);
214 static int gsm_trunc(struct ast_filestream *fs)
216 return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
219 static long gsm_tell(struct ast_filestream *fs)
222 offset = lseek(fs->fd, 0, SEEK_CUR);
223 return (offset/33)*160;
226 static char *gsm_getcomment(struct ast_filestream *s)
233 return ast_format_register(name, exts, AST_FORMAT_GSM,
249 return ast_format_unregister(name);
255 if (ast_mutex_lock(&gsm_lock)) {
256 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
260 ast_mutex_unlock(&gsm_lock);
272 return ASTERISK_GPL_KEY;