2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Anthony Minessale and Digium, Inc.
5 * Anthony Minessale (anthmct@yahoo.com)
6 * Kevin P. Fleming <kpfleming@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 ITU G.722.1 (Siren7, licensed from Polycom) format, 32kbps bitrate only
22 * \arg File name extensions: siren7
27 <support_level>core</support_level>
32 #include "asterisk/mod_format.h"
33 #include "asterisk/module.h"
34 #include "asterisk/endian.h"
35 #include "asterisk/format_cache.h"
37 #define BUF_SIZE 80 /* 20 milliseconds == 80 bytes, 320 samples */
38 #define SAMPLES_TO_BYTES(x) x / (320 / 80)
39 #define BYTES_TO_SAMPLES(x) x * (320 / 80)
41 static struct ast_frame *siren7read(struct ast_filestream *s, int *whennext)
45 /* Send a frame from the file to the appropriate channel */
46 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
47 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
49 ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
50 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
55 *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
59 static int siren7write(struct ast_filestream *fs, struct ast_frame *f)
63 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
64 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
70 static int siren7seek(struct ast_filestream *fs, off_t sample_offset, int whence)
72 off_t offset = 0, min = 0, cur, max;
74 sample_offset = SAMPLES_TO_BYTES(sample_offset);
76 if ((cur = ftello(fs->f)) < 0) {
77 ast_log(AST_LOG_WARNING, "Unable to determine current position in siren7 filestream %p: %s\n", fs, strerror(errno));
81 if (fseeko(fs->f, 0, SEEK_END) < 0) {
82 ast_log(AST_LOG_WARNING, "Unable to seek to end of siren7 filestream %p: %s\n", fs, strerror(errno));
86 if ((max = ftello(fs->f)) < 0) {
87 ast_log(AST_LOG_WARNING, "Unable to determine max position in siren7 filestream %p: %s\n", fs, strerror(errno));
91 if (whence == SEEK_SET)
92 offset = sample_offset;
93 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
94 offset = sample_offset + cur;
95 else if (whence == SEEK_END)
96 offset = max - sample_offset;
98 if (whence != SEEK_FORCECUR)
99 offset = (offset > max) ? max : offset;
101 /* always protect against seeking past begining. */
102 offset = (offset < min) ? min : offset;
104 return fseeko(fs->f, offset, SEEK_SET);
107 static int siren7trunc(struct ast_filestream *fs)
112 if ((fd = fileno(fs->f)) < 0) {
113 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for siren7 filestream %p: %s\n", fs, strerror(errno));
116 if ((cur = ftello(fs->f)) < 0) {
117 ast_log(AST_LOG_WARNING, "Unable to determine current position in siren7 filestream %p: %s\n", fs, strerror(errno));
120 /* Truncate file to current length */
121 return ftruncate(fd, cur);
124 static off_t siren7tell(struct ast_filestream *fs)
126 return BYTES_TO_SAMPLES(ftello(fs->f));
129 static struct ast_format_def siren7_f = {
132 .write = siren7write,
134 .trunc = siren7trunc,
137 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
140 static int load_module(void)
142 siren7_f.format = ast_format_siren7;
143 if (ast_format_def_register(&siren7_f))
144 return AST_MODULE_LOAD_DECLINE;
146 return AST_MODULE_LOAD_SUCCESS;
149 static int unload_module(void)
151 return ast_format_def_unregister(siren7_f.name);
154 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.722.1 (Siren7, licensed from Polycom)",
155 .support_level = AST_MODULE_SUPPORT_CORE,
157 .unload = unload_module,
158 .load_pri = AST_MODPRI_APP_DEPEND