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
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/mod_format.h"
35 #include "asterisk/module.h"
36 #include "asterisk/endian.h"
40 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
42 /* Portions of the conversion code are by guido@sienanet.it */
44 #define GSM_FRAME_SIZE 33
45 #define GSM_SAMPLES 160
47 /* silent gsm frame */
48 /* begin binary data: */
49 static const char gsm_silence[] = /* 33 */
50 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
51 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
53 /* end binary data. size = 33 bytes */
55 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
59 s->fr.frametype = AST_FRAME_VOICE;
60 ast_format_set(&s->fr.subclass.format, AST_FORMAT_GSM, 0);
61 AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
63 if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
65 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
68 *whennext = s->fr.samples = GSM_SAMPLES;
72 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
75 unsigned char gsm[2*GSM_FRAME_SIZE];
77 if (f->frametype != AST_FRAME_VOICE) {
78 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
81 if (f->subclass.format.id != AST_FORMAT_GSM) {
82 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(&f->subclass.format));
85 if (!(f->datalen % 65)) {
86 /* This is in MSGSM format, need to be converted */
88 while(len < f->datalen) {
89 conv65(f->data.ptr + len, gsm);
90 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
91 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
97 if (f->datalen % GSM_FRAME_SIZE) {
98 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
101 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
102 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
109 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
111 off_t offset=0,min,cur,max,distance;
115 fseeko(fs->f, 0, SEEK_END);
117 /* have to fudge to frame here, so not fully to sample */
118 distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
119 if(whence == SEEK_SET)
121 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
122 offset = distance + cur;
123 else if(whence == SEEK_END)
124 offset = max - distance;
125 /* Always protect against seeking past the begining. */
126 offset = (offset < min)?min:offset;
127 if (whence != SEEK_FORCECUR) {
128 offset = (offset > max)?max:offset;
129 } else if (offset > max) {
131 fseeko(fs->f, 0, SEEK_END);
132 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
133 if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
134 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
138 return fseeko(fs->f, offset, SEEK_SET);
141 static int gsm_trunc(struct ast_filestream *fs)
143 return ftruncate(fileno(fs->f), ftello(fs->f));
146 static off_t gsm_tell(struct ast_filestream *fs)
148 off_t offset = ftello(fs->f);
149 return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
152 static struct ast_format_def gsm_f = {
160 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
163 static int load_module(void)
165 ast_format_set(&gsm_f.format, AST_FORMAT_GSM, 0);
166 if (ast_format_def_register(&gsm_f))
167 return AST_MODULE_LOAD_FAILURE;
168 return AST_MODULE_LOAD_SUCCESS;
171 static int unload_module(void)
173 return ast_format_def_unregister(gsm_f.name);
176 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
178 .unload = unload_module,
179 .load_pri = AST_MODPRI_APP_DEPEND