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 "solaris-compat/compat.h"
34 #include <machine/endian.h>
39 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
41 /* Portions of the conversion code are by guido@sienanet.it */
43 /* silent gsm frame */
44 /* begin binary data: */
45 char gsm_silence[] = /* 33 */
46 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
47 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
49 /* end binary data. size = 33 bytes */
51 struct ast_filestream {
52 void *reserved[AST_RESERVED_POINTERS];
53 /* Believe it or not, we must decode/recode to account for the
55 /* This is what a filestream means to us */
56 int fd; /* Descriptor */
57 struct ast_frame fr; /* Frame information */
58 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
59 char empty; /* Empty character */
60 unsigned char gsm[66]; /* Two Real GSM Frames */
64 AST_MUTEX_DEFINE_STATIC(gsm_lock);
65 static int glistcnt = 0;
67 static char *name = "gsm";
68 static char *desc = "Raw GSM data";
69 static char *exts = "gsm";
71 static struct ast_filestream *gsm_open(int fd)
73 /* We don't have any header to read or anything really, but
74 if we did, it would go here. We also might want to check
75 and be sure it's a valid file. */
76 struct ast_filestream *tmp;
77 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
78 memset(tmp, 0, sizeof(struct ast_filestream));
79 if (ast_mutex_lock(&gsm_lock)) {
80 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
85 tmp->fr.data = tmp->gsm;
86 tmp->fr.frametype = AST_FRAME_VOICE;
87 tmp->fr.subclass = AST_FORMAT_GSM;
88 /* datalen will vary for each frame */
92 ast_mutex_unlock(&gsm_lock);
93 ast_update_use_count();
98 static struct ast_filestream *gsm_rewrite(int fd, const char *comment)
100 /* We don't have any header to read or anything really, but
101 if we did, it would go here. We also might want to check
102 and be sure it's a valid file. */
103 struct ast_filestream *tmp;
104 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
105 memset(tmp, 0, sizeof(struct ast_filestream));
106 if (ast_mutex_lock(&gsm_lock)) {
107 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
113 ast_mutex_unlock(&gsm_lock);
114 ast_update_use_count();
116 ast_log(LOG_WARNING, "Out of memory\n");
120 static void gsm_close(struct ast_filestream *s)
122 if (ast_mutex_lock(&gsm_lock)) {
123 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
127 ast_mutex_unlock(&gsm_lock);
128 ast_update_use_count();
133 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
136 s->fr.frametype = AST_FRAME_VOICE;
137 s->fr.subclass = AST_FORMAT_GSM;
138 s->fr.offset = AST_FRIENDLY_OFFSET;
143 if ((res = read(s->fd, s->gsm, 33)) != 33) {
145 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
152 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
155 unsigned char gsm[66];
156 if (f->frametype != AST_FRAME_VOICE) {
157 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
160 if (f->subclass != AST_FORMAT_GSM) {
161 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
164 if (!(f->datalen % 65)) {
165 /* This is in MSGSM format, need to be converted */
167 while(len < f->datalen) {
168 conv65(f->data + len, gsm);
169 if ((res = write(fs->fd, gsm, 66)) != 66) {
170 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
176 if (f->datalen % 33) {
177 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
180 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
181 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
188 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
190 off_t offset=0,min,cur,max,distance;
193 cur = lseek(fs->fd, 0, SEEK_CUR);
194 max = lseek(fs->fd, 0, SEEK_END);
195 /* have to fudge to frame here, so not fully to sample */
196 distance = (sample_offset/160) * 33;
197 if(whence == SEEK_SET)
199 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
200 offset = distance + cur;
201 else if(whence == SEEK_END)
202 offset = max - distance;
203 // Always protect against seeking past the begining.
204 offset = (offset < min)?min:offset;
205 if (whence != SEEK_FORCECUR) {
206 offset = (offset > max)?max:offset;
207 } else if (offset > max) {
209 lseek(fs->fd, 0, SEEK_END);
210 for (i=0; i< (offset - max) / 33; i++) {
211 write(fs->fd, gsm_silence, 33);
214 return lseek(fs->fd, offset, SEEK_SET);
217 static int gsm_trunc(struct ast_filestream *fs)
219 return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
222 static long gsm_tell(struct ast_filestream *fs)
225 offset = lseek(fs->fd, 0, SEEK_CUR);
226 return (offset/33)*160;
229 static char *gsm_getcomment(struct ast_filestream *s)
236 return ast_format_register(name, exts, AST_FORMAT_GSM,
252 return ast_format_unregister(name);
258 if (ast_mutex_lock(&gsm_lock)) {
259 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
263 ast_mutex_unlock(&gsm_lock);
275 return ASTERISK_GPL_KEY;