Merged revisions 140817 via svnmerge from
[asterisk-bristuff.git] / formats / format_gsm.c
blob3506f563e1b1371c8c5795868bfc18f6f35a300f
1 /*
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.
19 /*! \file
21 * \brief Save to raw, headerless GSM data.
22 * \arg File name extension: gsm
23 * \ingroup formats
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/mod_format.h"
31 #include "asterisk/module.h"
32 #include "asterisk/endian.h"
34 #include "msgsm.h"
36 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
38 /* Portions of the conversion code are by guido@sienanet.it */
40 #define GSM_FRAME_SIZE 33
41 #define GSM_SAMPLES 160
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
48 ,0x92,0x49,0x24};
49 /* end binary data. size = 33 bytes */
51 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
53 int res;
55 s->fr.frametype = AST_FRAME_VOICE;
56 s->fr.subclass = AST_FORMAT_GSM;
57 AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
58 s->fr.mallocd = 0;
59 if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
60 if (res)
61 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
62 return NULL;
64 *whennext = s->fr.samples = GSM_SAMPLES;
65 return &s->fr;
68 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
70 int res;
71 unsigned char gsm[2*GSM_FRAME_SIZE];
73 if (f->frametype != AST_FRAME_VOICE) {
74 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
75 return -1;
77 if (f->subclass != AST_FORMAT_GSM) {
78 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
79 return -1;
81 if (!(f->datalen % 65)) {
82 /* This is in MSGSM format, need to be converted */
83 int len=0;
84 while(len < f->datalen) {
85 conv65(f->data.ptr + len, gsm);
86 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
87 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
88 return -1;
90 len += 65;
92 } else {
93 if (f->datalen % GSM_FRAME_SIZE) {
94 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
95 return -1;
97 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
98 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
99 return -1;
102 return 0;
105 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
107 off_t offset=0,min,cur,max,distance;
109 min = 0;
110 cur = ftello(fs->f);
111 fseeko(fs->f, 0, SEEK_END);
112 max = ftello(fs->f);
113 /* have to fudge to frame here, so not fully to sample */
114 distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
115 if(whence == SEEK_SET)
116 offset = distance;
117 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
118 offset = distance + cur;
119 else if(whence == SEEK_END)
120 offset = max - distance;
121 /* Always protect against seeking past the begining. */
122 offset = (offset < min)?min:offset;
123 if (whence != SEEK_FORCECUR) {
124 offset = (offset > max)?max:offset;
125 } else if (offset > max) {
126 int i;
127 fseeko(fs->f, 0, SEEK_END);
128 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
129 fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f);
132 return fseeko(fs->f, offset, SEEK_SET);
135 static int gsm_trunc(struct ast_filestream *fs)
137 return ftruncate(fileno(fs->f), ftello(fs->f));
140 static off_t gsm_tell(struct ast_filestream *fs)
142 off_t offset = ftello(fs->f);
143 return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
146 static const struct ast_format gsm_f = {
147 .name = "gsm",
148 .exts = "gsm",
149 .format = AST_FORMAT_GSM,
150 .write = gsm_write,
151 .seek = gsm_seek,
152 .trunc = gsm_trunc,
153 .tell = gsm_tell,
154 .read = gsm_read,
155 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
158 static int load_module(void)
160 if (ast_format_register(&gsm_f))
161 return AST_MODULE_LOAD_FAILURE;
162 return AST_MODULE_LOAD_SUCCESS;
165 static int unload_module(void)
167 return ast_format_unregister(gsm_f.name);
170 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw GSM data");