Debian Codegraph @

formats/format_gsm.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
7 *
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.
13 *
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.
17 */
18
19 /*! \file
20 *
21 * \brief Save to raw, headerless GSM data.
22 * \arg File name extension: gsm
23 * \ingroup formats
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/mod_format.h"
33 #include "asterisk/module.h"
34 #include "asterisk/endian.h"
35 #include "asterisk/format_cache.h"
36
37 #include "msgsm.h"
38
39 /* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */
40
41 /* Portions of the conversion code are by [email protected] */
42
43 #define GSM_FRAME_SIZE 33
44 #define GSM_SAMPLES 160
45
46 /* silent gsm frame */
47 /* begin binary data: */
48 static const char gsm_silence[] = /* 33 */
49 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
50 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
51 ,0x92,0x49,0x24};
52 /* end binary data. size = 33 bytes */
53
54 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
55 {
56 size_t res;
57
58 AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
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 of %s data (expected %d bytes, read %zu): %s\n",
62 ast_format_get_name(s->fr.subclass.format), GSM_FRAME_SIZE, res,
63 strerror(errno));
64 }
65 return NULL;
66 }
67 *whennext = s->fr.samples = GSM_SAMPLES;
68 return &s->fr;
69 }
70
71 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
72 {
73 int res;
74 unsigned char gsm[2*GSM_FRAME_SIZE];
75
76 if (!(f->datalen % 65)) {
77 /* This is in MSGSM format, need to be converted */
78 int len=0;
79 while(len < f->datalen) {
80 conv65(f->data.ptr + len, gsm);
81 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
82 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
83 return -1;
84 }
85 len += 65;
86 }
87 } else {
88 if (f->datalen % GSM_FRAME_SIZE) {
89 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
90 return -1;
91 }
92 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
93 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
94 return -1;
95 }
96 }
97 return 0;
98 }
99
100 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
101 {
102 off_t offset = 0, min = 0, cur, max, distance;
103
104 if ((cur = ftello(fs->f)) < 0) {
105 ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
106 return -1;
107 }
108
109 if (fseeko(fs->f, 0, SEEK_END) < 0) {
110 ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
111 return -1;
112 }
113
114 if ((max = ftello(fs->f)) < 0) {
115 ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
116 return -1;
117 }
118
119 /* have to fudge to frame here, so not fully to sample */
120 distance = (sample_offset / GSM_SAMPLES) * GSM_FRAME_SIZE;
121 if (whence == SEEK_SET) {
122 offset = distance;
123 } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
124 offset = distance + cur;
125 } else if (whence == SEEK_END) {
126 offset = max - distance;
127 }
128
129 /* Always protect against seeking past the begining. */
130 offset = (offset < min)?min:offset;
131 if (whence != SEEK_FORCECUR) {
132 offset = (offset > max)?max:offset;
133 } else if (offset > max) {
134 int i;
135 fseeko(fs->f, 0, SEEK_END);
136 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
137 if (fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f) != GSM_FRAME_SIZE) {
138 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
139 }
140 }
141 }
142 return fseeko(fs->f, offset, SEEK_SET);
143 }
144
145 static int gsm_trunc(struct ast_filestream *fs)
146 {
147 int fd;
148 off_t cur;
149
150 if ((fd = fileno(fs->f)) < 0) {
151 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for gsm filestream %p: %s\n", fs, strerror(errno));
152 return -1;
153 }
154 if ((cur = ftello(fs->f)) < 0) {
155 ast_log(AST_LOG_WARNING, "Unable to determine current position in gsm filestream %p: %s\n", fs, strerror(errno));
156 return -1;
157 }
158 /* Truncate file to current length */
159 return ftruncate(fd, cur);
160 }
161
162 static off_t gsm_tell(struct ast_filestream *fs)
163 {
164 off_t offset = ftello(fs->f);
165
166 if (offset < 0) {
167 ast_log(AST_LOG_WARNING, "Unable to determine offset for gsm filestream %p: %s\n", fs, strerror(errno));
168 return 0;
169 }
170
171 return (offset / GSM_FRAME_SIZE) * GSM_SAMPLES;
172 }
173
174 static struct ast_format_def gsm_f = {
175 .name = "gsm",
176 .exts = "gsm",
177 .mime_types = "audio/gsm",
178 .write = gsm_write,
179 .seek = gsm_seek,
180 .trunc = gsm_trunc,
181 .tell = gsm_tell,
182 .read = gsm_read,
183 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */
184 };
185
186 static int load_module(void)
187 {
188 gsm_f.format = ast_format_gsm;
189 if (ast_format_def_register(&gsm_f))
190 return AST_MODULE_LOAD_DECLINE;
191 return AST_MODULE_LOAD_SUCCESS;
192 }
193
194 static int unload_module(void)
195 {
196 return ast_format_def_unregister(gsm_f.name);
197 }
198
199 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw GSM data",
200 .support_level = AST_MODULE_SUPPORT_CORE,
201 .load = load_module,
202 .unload = unload_module,
203 .load_pri = AST_MODPRI_APP_DEPEND
204 );