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>
37 /* Some Ideas for this code came from makegsme.c by Jeffery Chilton */
39 /* Portions of the conversion code are by guido@sienanet.it */
41 struct ast_filestream {
42 void *reserved[AST_RESERVED_POINTERS];
43 /* Believe it or not, we must decode/recode to account for the
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 */
54 static ast_mutex_t gsm_lock = AST_MUTEX_INITIALIZER;
55 static int glistcnt = 0;
57 static char *name = "gsm";
58 static char *desc = "Raw GSM data";
59 static char *exts = "gsm";
61 static struct ast_filestream *gsm_open(int fd)
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");
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 */
82 ast_mutex_unlock(&gsm_lock);
83 ast_update_use_count();
88 static struct ast_filestream *gsm_rewrite(int fd, char *comment)
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");
103 ast_mutex_unlock(&gsm_lock);
104 ast_update_use_count();
106 ast_log(LOG_WARNING, "Out of memory\n");
110 static void gsm_close(struct ast_filestream *s)
112 if (ast_mutex_lock(&gsm_lock)) {
113 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
117 ast_mutex_unlock(&gsm_lock);
118 ast_update_use_count();
123 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
126 s->fr.frametype = AST_FRAME_VOICE;
127 s->fr.subclass = AST_FORMAT_GSM;
128 s->fr.offset = AST_FRIENDLY_OFFSET;
133 if ((res = read(s->fd, s->gsm, 33)) != 33) {
135 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
142 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
145 unsigned char gsm[66];
146 if (f->frametype != AST_FRAME_VOICE) {
147 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
150 if (f->subclass != AST_FORMAT_GSM) {
151 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
154 if (!(f->datalen % 65)) {
155 /* This is in MSGSM format, need to be converted */
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));
166 if (f->datalen % 33) {
167 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
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));
178 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
180 off_t offset=0,min,cur,max,distance;
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)
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;
197 return lseek(fs->fd, offset, SEEK_SET);
200 static int gsm_trunc(struct ast_filestream *fs)
202 return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
205 static long gsm_tell(struct ast_filestream *fs)
208 offset = lseek(fs->fd, 0, SEEK_CUR);
209 return (offset/33)*160;
212 static char *gsm_getcomment(struct ast_filestream *s)
219 return ast_format_register(name, exts, AST_FORMAT_GSM,
235 return ast_format_unregister(name);
241 if (ast_mutex_lock(&gsm_lock)) {
242 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
246 ast_mutex_unlock(&gsm_lock);
258 return ASTERISK_GPL_KEY;