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 * \brief Save to raw, headerless GSM data.
22 * \arg File name extension: gsm
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
35 #include "asterisk/lock.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/sched.h"
40 #include "asterisk/module.h"
41 #include "asterisk/endian.h"
45 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
47 /* Portions of the conversion code are by guido@sienanet.it */
49 #define GSM_FRAME_SIZE 33
50 #define GSM_SAMPLES 160
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 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
64 s->fr.frametype = AST_FRAME_VOICE;
65 s->fr.subclass = AST_FORMAT_GSM;
66 AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
68 if ((res = fread(s->fr.data, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
70 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
73 *whennext = s->fr.samples = GSM_SAMPLES;
77 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
80 unsigned char gsm[2*GSM_FRAME_SIZE];
82 if (f->frametype != AST_FRAME_VOICE) {
83 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
86 if (f->subclass != AST_FORMAT_GSM) {
87 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
90 if (!(f->datalen % 65)) {
91 /* This is in MSGSM format, need to be converted */
93 while(len < f->datalen) {
94 conv65(f->data + len, gsm);
95 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
96 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
102 if (f->datalen % GSM_FRAME_SIZE) {
103 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
106 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
107 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
114 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
116 off_t offset=0,min,cur,max,distance;
120 fseeko(fs->f, 0, SEEK_END);
122 /* have to fudge to frame here, so not fully to sample */
123 distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
124 if(whence == SEEK_SET)
126 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
127 offset = distance + cur;
128 else if(whence == SEEK_END)
129 offset = max - distance;
130 /* Always protect against seeking past the begining. */
131 offset = (offset < min)?min:offset;
132 if (whence != SEEK_FORCECUR) {
133 offset = (offset > max)?max:offset;
134 } else if (offset > max) {
136 fseeko(fs->f, 0, SEEK_END);
137 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
138 fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f);
141 return fseeko(fs->f, offset, SEEK_SET);
144 static int gsm_trunc(struct ast_filestream *fs)
146 return ftruncate(fileno(fs->f), ftello(fs->f));
149 static off_t gsm_tell(struct ast_filestream *fs)
151 off_t offset = ftello(fs->f);
152 return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
155 static const struct ast_format gsm_f = {
158 .format = AST_FORMAT_GSM,
164 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
167 static int load_module(void)
169 if (ast_format_register(&gsm_f))
170 return AST_MODULE_LOAD_FAILURE;
171 return AST_MODULE_LOAD_SUCCESS;
174 static int unload_module(void)
176 return ast_format_unregister(gsm_f.name);
179 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw GSM data");