Don't reset the RTP address on a glare re-INVITE
[asterisk/asterisk.git] / formats / format_sln.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Anthony Minessale
5  * Anthony Minessale (anthmct@yahoo.com)
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17
18 /*! \file
19  *
20  * \brief RAW SLINEAR Formats
21  * \ingroup formats
22  */
23
24 /*** MODULEINFO
25         <support_level>core</support_level>
26  ***/
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/mod_format.h"
33 #include "asterisk/module.h"
34 #include "asterisk/endian.h"
35
36 static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, unsigned int buf_size, enum ast_format_id id)
37 {
38         int res;
39         /* Send a frame from the file to the appropriate channel */
40
41         s->fr.frametype = AST_FRAME_VOICE;
42         ast_format_set(&s->fr.subclass.format, id, 0);
43         s->fr.mallocd = 0;
44         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size);
45         if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
46                 if (res)
47                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
48                 return NULL;
49         }
50         *whennext = s->fr.samples = res/2;
51         s->fr.datalen = res;
52         return &s->fr;
53 }
54
55 static int generic_write(struct ast_filestream *fs, struct ast_frame *f, enum ast_format_id id)
56 {
57         int res;
58         if (f->frametype != AST_FRAME_VOICE) {
59                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
60                 return -1;
61         }
62         if (f->subclass.format.id != id) {
63                 ast_log(LOG_WARNING, "Asked to write non-slinear frame (%s)!\n", ast_getformatname(&f->subclass.format));
64                 return -1;
65         }
66         if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
67                         ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
68                         return -1;
69         }
70         return 0;
71 }
72
73 static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
74 {
75         off_t offset=0, min = 0, cur, max;
76
77         sample_offset <<= 1;
78
79         if ((cur = ftello(fs->f)) < 0) {
80                 ast_log(AST_LOG_WARNING, "Unable to determine current position in sln filestream %p: %s\n", fs, strerror(errno));
81                 return -1;
82         }
83
84         if (fseeko(fs->f, 0, SEEK_END) < 0) {
85                 ast_log(AST_LOG_WARNING, "Unable to seek to end of sln filestream %p: %s\n", fs, strerror(errno));
86                 return -1;
87         }
88
89         if ((max = ftello(fs->f)) < 0) {
90                 ast_log(AST_LOG_WARNING, "Unable to determine max position in sln filestream %p: %s\n", fs, strerror(errno));
91                 return -1;
92         }
93
94         if (whence == SEEK_SET)
95                 offset = sample_offset;
96         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
97                 offset = sample_offset + cur;
98         else if (whence == SEEK_END)
99                 offset = max - sample_offset;
100         if (whence != SEEK_FORCECUR) {
101                 offset = (offset > max)?max:offset;
102         }
103         /* always protect against seeking past begining. */
104         offset = (offset < min)?min:offset;
105         return fseeko(fs->f, offset, SEEK_SET);
106 }
107
108 static int slinear_trunc(struct ast_filestream *fs)
109 {
110         int fd;
111         off_t cur;
112
113         if ((fd = fileno(fs->f)) < 0) {
114                 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for sln filestream %p: %s\n", fs, strerror(errno));
115                 return -1;
116         }
117         if ((cur = ftello(fs->f)) < 0) {
118                 ast_log(AST_LOG_WARNING, "Unable to determine current position in sln filestream %p: %s\n", fs, strerror(errno));
119                 return -1;
120         }
121         /* Truncate file to current length */
122         return ftruncate(fd, cur);
123 }
124
125 static off_t slinear_tell(struct ast_filestream *fs)
126 {
127         return ftello(fs->f) / 2;
128 }
129
130 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR);}
131 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 320, AST_FORMAT_SLINEAR);}
132 static struct ast_format_def slin_f = {
133         .name = "sln",
134         .exts = "sln|raw",
135         .write = slinear_write,
136         .seek = slinear_seek,
137         .trunc = slinear_trunc,
138         .tell = slinear_tell,
139         .read = slinear_read,
140         .buf_size = 320 + AST_FRIENDLY_OFFSET,
141 };
142
143 static int slinear12_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR12);}
144 static struct ast_frame *slinear12_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 480, AST_FORMAT_SLINEAR12);}
145 static struct ast_format_def slin12_f = {
146         .name = "sln12",
147         .exts = "sln12",
148         .write = slinear12_write,
149         .seek = slinear_seek,
150         .trunc = slinear_trunc,
151         .tell = slinear_tell,
152         .read = slinear12_read,
153         .buf_size = 480 + AST_FRIENDLY_OFFSET,
154 };
155
156 static int slinear16_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR16);}
157 static struct ast_frame *slinear16_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 640, AST_FORMAT_SLINEAR16);}
158 static struct ast_format_def slin16_f = {
159         .name = "sln16",
160         .exts = "sln16",
161         .write = slinear16_write,
162         .seek = slinear_seek,
163         .trunc = slinear_trunc,
164         .tell = slinear_tell,
165         .read = slinear16_read,
166         .buf_size = 640 + AST_FRIENDLY_OFFSET,
167 };
168
169 static int slinear24_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR24);}
170 static struct ast_frame *slinear24_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 960, AST_FORMAT_SLINEAR24);}
171 static struct ast_format_def slin24_f = {
172         .name = "sln24",
173         .exts = "sln24",
174         .write = slinear24_write,
175         .seek = slinear_seek,
176         .trunc = slinear_trunc,
177         .tell = slinear_tell,
178         .read = slinear24_read,
179         .buf_size = 960 + AST_FRIENDLY_OFFSET,
180 };
181
182 static int slinear32_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR32);}
183 static struct ast_frame *slinear32_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1280, AST_FORMAT_SLINEAR32);}
184 static struct ast_format_def slin32_f = {
185         .name = "sln32",
186         .exts = "sln32",
187         .write = slinear32_write,
188         .seek = slinear_seek,
189         .trunc = slinear_trunc,
190         .tell = slinear_tell,
191         .read = slinear32_read,
192         .buf_size = 1280 + AST_FRIENDLY_OFFSET,
193 };
194
195 static int slinear44_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR44);}
196 static struct ast_frame *slinear44_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1764, AST_FORMAT_SLINEAR44);}
197 static struct ast_format_def slin44_f = {
198         .name = "sln44",
199         .exts = "sln44",
200         .write = slinear44_write,
201         .seek = slinear_seek,
202         .trunc = slinear_trunc,
203         .tell = slinear_tell,
204         .read = slinear44_read,
205         .buf_size = 1764 + AST_FRIENDLY_OFFSET,
206 };
207
208 static int slinear48_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR48);}
209 static struct ast_frame *slinear48_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 1920, AST_FORMAT_SLINEAR48);}
210 static struct ast_format_def slin48_f = {
211         .name = "sln48",
212         .exts = "sln48",
213         .write = slinear48_write,
214         .seek = slinear_seek,
215         .trunc = slinear_trunc,
216         .tell = slinear_tell,
217         .read = slinear48_read,
218         .buf_size = 1920 + AST_FRIENDLY_OFFSET,
219 };
220
221 static int slinear96_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR96);}
222 static struct ast_frame *slinear96_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 3840, AST_FORMAT_SLINEAR96);}
223 static struct ast_format_def slin96_f = {
224         .name = "sln96",
225         .exts = "sln96",
226         .write = slinear96_write,
227         .seek = slinear_seek,
228         .trunc = slinear_trunc,
229         .tell = slinear_tell,
230         .read = slinear96_read,
231         .buf_size = 3840 + AST_FRIENDLY_OFFSET,
232 };
233
234 static int slinear192_write(struct ast_filestream *fs, struct ast_frame *f){return generic_write(fs, f, AST_FORMAT_SLINEAR192);}
235 static struct ast_frame *slinear192_read(struct ast_filestream *s, int *whennext){return generic_read(s, whennext, 7680, AST_FORMAT_SLINEAR192);}
236 static struct ast_format_def slin192_f = {
237         .name = "sln192",
238         .exts = "sln192",
239         .write = slinear192_write,
240         .seek = slinear_seek,
241         .trunc = slinear_trunc,
242         .tell = slinear_tell,
243         .read = slinear192_read,
244         .buf_size = 7680 + AST_FRIENDLY_OFFSET,
245 };
246
247 static struct ast_format_def *slin_list[] = {
248         &slin_f,
249         &slin12_f,
250         &slin16_f,
251         &slin24_f,
252         &slin32_f,
253         &slin44_f,
254         &slin48_f,
255         &slin96_f,
256         &slin192_f,
257 };
258
259 static int load_module(void)
260 {
261         int i;
262         ast_format_set(&slin_f.format, AST_FORMAT_SLINEAR, 0);
263         ast_format_set(&slin12_f.format, AST_FORMAT_SLINEAR12, 0);
264         ast_format_set(&slin16_f.format, AST_FORMAT_SLINEAR16, 0);
265         ast_format_set(&slin24_f.format, AST_FORMAT_SLINEAR24, 0);
266         ast_format_set(&slin32_f.format, AST_FORMAT_SLINEAR32, 0);
267         ast_format_set(&slin44_f.format, AST_FORMAT_SLINEAR44, 0);
268         ast_format_set(&slin48_f.format, AST_FORMAT_SLINEAR48, 0);
269         ast_format_set(&slin96_f.format, AST_FORMAT_SLINEAR96, 0);
270         ast_format_set(&slin192_f.format, AST_FORMAT_SLINEAR192, 0);
271
272         for (i = 0; i < ARRAY_LEN(slin_list); i++) {
273                 if (ast_format_def_register(slin_list[i])) {
274                         return AST_MODULE_LOAD_FAILURE;
275                 }
276         }
277
278         return AST_MODULE_LOAD_SUCCESS;
279 }
280
281 static int unload_module(void)
282 {
283         int res = 0;
284         int i = 0;
285
286         for (i = 0; i < ARRAY_LEN(slin_list); i++) {
287                 if (ast_format_def_unregister(slin_list[i]->name)) {
288                         res |= AST_MODULE_LOAD_FAILURE;
289                 }
290         }
291         return res;
292 }
293
294 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw Signed Linear Audio support (SLN) 8khz-192khz",
295         .load = load_module,
296         .unload = unload_module,
297         .load_pri = AST_MODPRI_APP_DEPEND
298 );