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 <arpa/inet.h>
31 #include <machine/endian.h>
36 /* Some Ideas for this code came from makegsme.c by Jeffery Chilton */
38 /* Portions of the conversion code are by guido@sienanet.it */
40 struct ast_filestream {
41 void *reserved[AST_RESERVED_POINTERS];
42 /* Believe it or not, we must decode/recode to account for the
44 /* This is what a filestream means to us */
45 int fd; /* Descriptor */
46 struct ast_frame fr; /* Frame information */
47 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
48 char empty; /* Empty character */
49 unsigned char gsm[66]; /* Two Real GSM Frames */
53 static ast_mutex_t gsm_lock = AST_MUTEX_INITIALIZER;
54 static int glistcnt = 0;
56 static char *name = "gsm";
57 static char *desc = "Raw GSM data";
58 static char *exts = "gsm";
60 static struct ast_filestream *gsm_open(int fd)
62 /* We don't have any header to read or anything really, but
63 if we did, it would go here. We also might want to check
64 and be sure it's a valid file. */
65 struct ast_filestream *tmp;
66 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
67 memset(tmp, 0, sizeof(struct ast_filestream));
68 if (ast_mutex_lock(&gsm_lock)) {
69 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
74 tmp->fr.data = tmp->gsm;
75 tmp->fr.frametype = AST_FRAME_VOICE;
76 tmp->fr.subclass = AST_FORMAT_GSM;
77 /* datalen will vary for each frame */
81 ast_mutex_unlock(&gsm_lock);
82 ast_update_use_count();
87 static struct ast_filestream *gsm_rewrite(int fd, char *comment)
89 /* We don't have any header to read or anything really, but
90 if we did, it would go here. We also might want to check
91 and be sure it's a valid file. */
92 struct ast_filestream *tmp;
93 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
94 memset(tmp, 0, sizeof(struct ast_filestream));
95 if (ast_mutex_lock(&gsm_lock)) {
96 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
102 ast_mutex_unlock(&gsm_lock);
103 ast_update_use_count();
105 ast_log(LOG_WARNING, "Out of memory\n");
109 static void gsm_close(struct ast_filestream *s)
111 if (ast_mutex_lock(&gsm_lock)) {
112 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
116 ast_mutex_unlock(&gsm_lock);
117 ast_update_use_count();
122 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
125 s->fr.frametype = AST_FRAME_VOICE;
126 s->fr.subclass = AST_FORMAT_GSM;
127 s->fr.offset = AST_FRIENDLY_OFFSET;
132 if ((res = read(s->fd, s->gsm, 33)) != 33) {
134 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
141 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
144 unsigned char gsm[66];
145 if (f->frametype != AST_FRAME_VOICE) {
146 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
149 if (f->subclass != AST_FORMAT_GSM) {
150 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
153 if (!(f->datalen % 65)) {
154 /* This is in MSGSM format, need to be converted */
156 while(len < f->datalen) {
157 conv65(f->data + len, gsm);
158 if ((res = write(fs->fd, gsm, 66)) != 66) {
159 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
165 if (f->datalen % 33) {
166 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
169 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
170 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
177 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
179 off_t offset=0,min,cur,max,distance;
182 cur = lseek(fs->fd, 0, SEEK_CUR);
183 max = lseek(fs->fd, 0, SEEK_END);
184 /* have to fudge to frame here, so not fully to sample */
185 distance = (sample_offset/160) * 33;
186 if(whence == SEEK_SET)
188 if(whence == SEEK_CUR)
189 offset = distance + cur;
190 if(whence == SEEK_END)
191 offset = max - distance;
192 offset = (offset > max)?max:offset;
193 offset = (offset < min)?min:offset;
194 return lseek(fs->fd, offset, SEEK_SET);
197 static int gsm_trunc(struct ast_filestream *fs)
199 return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
202 static long gsm_tell(struct ast_filestream *fs)
205 offset = lseek(fs->fd, 0, SEEK_CUR);
206 return (offset/33)*160;
209 static char *gsm_getcomment(struct ast_filestream *s)
216 return ast_format_register(name, exts, AST_FORMAT_GSM,
232 return ast_format_unregister(name);
238 if (ast_mutex_lock(&gsm_lock)) {
239 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
243 ast_mutex_unlock(&gsm_lock);
255 return ASTERISK_GPL_KEY;