2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * Save to raw, headerless GSM data.
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/lock.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/file.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/sched.h"
43 #include "asterisk/module.h"
44 #include "asterisk/endian.h"
48 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
50 /* Portions of the conversion code are by guido@sienanet.it */
52 /* silent gsm frame */
53 /* begin binary data: */
54 char gsm_silence[] = /* 33 */
55 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
56 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
58 /* end binary data. size = 33 bytes */
60 struct ast_filestream {
61 void *reserved[AST_RESERVED_POINTERS];
62 /* Believe it or not, we must decode/recode to account for the
64 /* This is what a filestream means to us */
65 int fd; /* Descriptor */
66 struct ast_frame fr; /* Frame information */
67 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
68 char empty; /* Empty character */
69 unsigned char gsm[66]; /* Two Real GSM Frames */
73 AST_MUTEX_DEFINE_STATIC(gsm_lock);
74 static int glistcnt = 0;
76 static char *name = "gsm";
77 static char *desc = "Raw GSM data";
78 static char *exts = "gsm";
80 static struct ast_filestream *gsm_open(int fd)
82 /* We don't have any header to read or anything really, but
83 if we did, it would go here. We also might want to check
84 and be sure it's a valid file. */
85 struct ast_filestream *tmp;
86 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
87 memset(tmp, 0, sizeof(struct ast_filestream));
88 if (ast_mutex_lock(&gsm_lock)) {
89 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
94 tmp->fr.data = tmp->gsm;
95 tmp->fr.frametype = AST_FRAME_VOICE;
96 tmp->fr.subclass = AST_FORMAT_GSM;
97 /* datalen will vary for each frame */
101 ast_mutex_unlock(&gsm_lock);
102 ast_update_use_count();
107 static struct ast_filestream *gsm_rewrite(int fd, const char *comment)
109 /* We don't have any header to read or anything really, but
110 if we did, it would go here. We also might want to check
111 and be sure it's a valid file. */
112 struct ast_filestream *tmp;
113 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
114 memset(tmp, 0, sizeof(struct ast_filestream));
115 if (ast_mutex_lock(&gsm_lock)) {
116 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
122 ast_mutex_unlock(&gsm_lock);
123 ast_update_use_count();
125 ast_log(LOG_WARNING, "Out of memory\n");
129 static void gsm_close(struct ast_filestream *s)
131 if (ast_mutex_lock(&gsm_lock)) {
132 ast_log(LOG_WARNING, "Unable to lock gsm list\n");
136 ast_mutex_unlock(&gsm_lock);
137 ast_update_use_count();
142 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
145 s->fr.frametype = AST_FRAME_VOICE;
146 s->fr.subclass = AST_FORMAT_GSM;
147 s->fr.offset = AST_FRIENDLY_OFFSET;
152 if ((res = read(s->fd, s->gsm, 33)) != 33) {
154 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
161 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
164 unsigned char gsm[66];
165 if (f->frametype != AST_FRAME_VOICE) {
166 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
169 if (f->subclass != AST_FORMAT_GSM) {
170 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
173 if (!(f->datalen % 65)) {
174 /* This is in MSGSM format, need to be converted */
176 while(len < f->datalen) {
177 conv65(f->data + len, gsm);
178 if ((res = write(fs->fd, gsm, 66)) != 66) {
179 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
185 if (f->datalen % 33) {
186 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
189 if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
190 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
197 static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
199 off_t offset=0,min,cur,max,distance;
202 cur = lseek(fs->fd, 0, SEEK_CUR);
203 max = lseek(fs->fd, 0, SEEK_END);
204 /* have to fudge to frame here, so not fully to sample */
205 distance = (sample_offset/160) * 33;
206 if(whence == SEEK_SET)
208 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
209 offset = distance + cur;
210 else if(whence == SEEK_END)
211 offset = max - distance;
212 /* Always protect against seeking past the begining. */
213 offset = (offset < min)?min:offset;
214 if (whence != SEEK_FORCECUR) {
215 offset = (offset > max)?max:offset;
216 } else if (offset > max) {
218 lseek(fs->fd, 0, SEEK_END);
219 for (i=0; i< (offset - max) / 33; i++) {
220 write(fs->fd, gsm_silence, 33);
223 return lseek(fs->fd, offset, SEEK_SET);
226 static int gsm_trunc(struct ast_filestream *fs)
228 return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
231 static long gsm_tell(struct ast_filestream *fs)
234 offset = lseek(fs->fd, 0, SEEK_CUR);
235 return (offset/33)*160;
238 static char *gsm_getcomment(struct ast_filestream *s)
245 return ast_format_register(name, exts, AST_FORMAT_GSM,
261 return ast_format_unregister(name);
277 return ASTERISK_GPL_KEY;