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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/mod_format.h"
35 #include "asterisk/module.h"
36 #include "asterisk/endian.h"
38 #define BUF_SIZE 80 /* 20 milliseconds == 80 bytes, 320 samples */
39 #define SAMPLES_TO_BYTES(x) x / (320 / 80)
40 #define BYTES_TO_SAMPLES(x) x * (320 / 80)
42 static struct ast_frame *siren7read(struct ast_filestream *s, int *whennext)
45 /* Send a frame from the file to the appropriate channel */
47 s->fr.frametype = AST_FRAME_VOICE;
48 ast_format_set(&s->fr.subclass.format, AST_FORMAT_SIREN7, 0);
50 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
51 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
53 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
56 *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
60 static int siren7write(struct ast_filestream *fs, struct ast_frame *f)
64 if (f->frametype != AST_FRAME_VOICE) {
65 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
68 if (f->subclass.format.id != AST_FORMAT_SIREN7) {
69 ast_log(LOG_WARNING, "Asked to write non-Siren7 frame (%s)!\n", ast_getformatname(&f->subclass.format));
72 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
73 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
79 static int siren7seek(struct ast_filestream *fs, off_t sample_offset, int whence)
81 off_t offset = 0, min = 0, cur, max;
83 sample_offset = SAMPLES_TO_BYTES(sample_offset);
87 fseeko(fs->f, 0, SEEK_END);
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)
109 return ftruncate(fileno(fs->f), ftello(fs->f));
112 static off_t siren7tell(struct ast_filestream *fs)
114 return BYTES_TO_SAMPLES(ftello(fs->f));
117 static struct ast_format_def siren7_f = {
120 .write = siren7write,
122 .trunc = siren7trunc,
125 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
128 static int load_module(void)
130 ast_format_set(&siren7_f.format, AST_FORMAT_SIREN7, 0);
131 if (ast_format_def_register(&siren7_f))
132 return AST_MODULE_LOAD_DECLINE;
134 return AST_MODULE_LOAD_SUCCESS;
137 static int unload_module(void)
139 return ast_format_def_unregister(siren7_f.name);
142 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ITU G.722.1 (Siren7, licensed from Polycom)",
144 .unload = unload_module,
145 .load_pri = AST_MODPRI_APP_DEPEND