2 * Asterisk -- A telephony toolkit for Linux.
4 * Save to raw, headerless GSM data.
6 * Copyright (C) 1999 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
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 "asterisk/endian.h"
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
31 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
33 /* Portions of the conversion code are by guido@sienanet.it */
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
41 /* end binary data. size = 33 bytes */
43 struct ast_filestream {
44 void *reserved[AST_RESERVED_POINTERS];
45 /* Believe it or not, we must decode/recode to account for the
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 */
56 AST_MUTEX_DEFINE_STATIC(gsm_lock);
57 static int glistcnt = 0;
59 static char *name = "gsm";
60 static char *desc = "Raw GSM data";
61 static char *exts = "gsm";
63 static struct ast_filestream *gsm_open(int fd)
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");
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 */
84 ast_mutex_unlock(&gsm_lock);
85 ast_update_use_count();
90 static struct ast_filestream *gsm_rewrite(int fd, const char *comment)
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");
105 ast_mutex_unlock(&gsm_lock);
106 ast_update_use_count();
108 ast_log(LOG_WARNING, "Out of memory\n");
112 static void gsm_close(struct ast_filestream *s)
114 if (ast_mutex_lock(&gsm_lock)) {
115 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
119 ast_mutex_unlock(&gsm_lock);
120 ast_update_use_count();
125 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
128 s->fr.frametype = AST_FRAME_VOICE;
129 s->fr.subclass = AST_FORMAT_GSM;
130 s->fr.offset = AST_FRIENDLY_OFFSET;
135 if ((res = read(s->fd, s->gsm, 33)) != 33) {
137 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
144 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
147 unsigned char gsm[66];
148 if (f->frametype != AST_FRAME_VOICE) {
149 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
152 if (f->subclass != AST_FORMAT_GSM) {
153 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
156 if (!(f->datalen % 65)) {
157 /* This is in MSGSM format, need to be converted */
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));
168 if (f->datalen % 33) {
169 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
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));
180 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
182 off_t offset=0,min,cur,max,distance;
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)
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) {
201 lseek(fs->fd, 0, SEEK_END);
202 for (i=0; i< (offset - max) / 33; i++) {
203 write(fs->fd, gsm_silence, 33);
206 return lseek(fs->fd, offset, SEEK_SET);
209 static int gsm_trunc(struct ast_filestream *fs)
211 return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
214 static long gsm_tell(struct ast_filestream *fs)
217 offset = lseek(fs->fd, 0, SEEK_CUR);
218 return (offset/33)*160;
221 static char *gsm_getcomment(struct ast_filestream *s)
228 return ast_format_register(name, exts, AST_FORMAT_GSM,
244 return ast_format_unregister(name);
250 if (ast_mutex_lock(&gsm_lock)) {
251 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
255 ast_mutex_unlock(&gsm_lock);
267 return ASTERISK_GPL_KEY;