2001-03-22 22:25:06 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2001-03-22 22:25:06 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
2001-03-22 22:25:06 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
2001-03-22 22:25:06 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-26 13:03:17 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-26 13:03:17 +00:00
|
|
|
* \brief Flat, binary, ADPCM vox file format.
|
2005-11-06 15:09:47 +00:00
|
|
|
* \arg File name extensions: vox
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-11-06 15:09:47 +00:00
|
|
|
* \ingroup formats
|
2001-03-22 22:25:06 +00:00
|
|
|
*/
|
2011-07-14 20:28:54 +00:00
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>extended</support_level>
|
|
|
|
***/
|
2001-03-22 22:25:06 +00:00
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2007-11-22 00:53:49 +00:00
|
|
|
#include "asterisk/mod_format.h"
|
2005-06-06 22:12:19 +00:00
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/endian.h"
|
|
|
|
|
2006-04-04 12:59:25 +00:00
|
|
|
#define BUF_SIZE 80 /* 80 bytes, 160 samples */
|
|
|
|
#define VOX_SAMPLES 160
|
2001-03-22 22:25:06 +00:00
|
|
|
|
2003-06-28 22:50:47 +00:00
|
|
|
static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
|
2001-03-22 22:25:06 +00:00
|
|
|
{
|
|
|
|
int res;
|
2006-04-04 12:59:25 +00:00
|
|
|
|
2001-03-22 22:25:06 +00:00
|
|
|
/* Send a frame from the file to the appropriate channel */
|
|
|
|
s->fr.frametype = AST_FRAME_VOICE;
|
2011-02-03 16:22:10 +00:00
|
|
|
ast_format_set(&s->fr.subclass.format, AST_FORMAT_ADPCM, 0);
|
2001-03-22 22:25:06 +00:00
|
|
|
s->fr.mallocd = 0;
|
2006-04-09 22:31:38 +00:00
|
|
|
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
|
2008-05-22 16:29:54 +00:00
|
|
|
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
|
2001-03-22 22:25:06 +00:00
|
|
|
if (res)
|
|
|
|
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
|
2003-06-28 22:50:47 +00:00
|
|
|
return NULL;
|
2001-03-22 22:25:06 +00:00
|
|
|
}
|
2006-04-04 12:59:25 +00:00
|
|
|
*whennext = s->fr.samples = res * 2;
|
2004-03-11 19:57:10 +00:00
|
|
|
s->fr.datalen = res;
|
2003-06-28 22:50:47 +00:00
|
|
|
return &s->fr;
|
2001-03-22 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2006-04-04 12:59:25 +00:00
|
|
|
static int vox_write(struct ast_filestream *s, struct ast_frame *f)
|
2001-03-22 22:25:06 +00:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
if (f->frametype != AST_FRAME_VOICE) {
|
|
|
|
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-03 16:22:10 +00:00
|
|
|
if (f->subclass.format.id != AST_FORMAT_ADPCM) {
|
|
|
|
ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%s)!\n", ast_getformatname(&f->subclass.format));
|
2001-03-22 22:25:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-05-22 16:29:54 +00:00
|
|
|
if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
|
2001-03-22 22:25:06 +00:00
|
|
|
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-20 23:35:12 +00:00
|
|
|
static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
|
2003-02-06 22:11:43 +00:00
|
|
|
{
|
2012-04-16 20:17:03 +00:00
|
|
|
off_t offset = 0, min = 0, cur, max, distance;
|
|
|
|
|
|
|
|
if ((cur = ftello(fs->f)) < 0) {
|
|
|
|
ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fseeko(fs->f, 0, SEEK_END) < 0) {
|
|
|
|
ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-17 18:29:51 +00:00
|
|
|
if ((max = ftello(fs->f)) < 0) {
|
2012-04-16 20:17:03 +00:00
|
|
|
ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* have to fudge to frame here, so not fully to sample */
|
|
|
|
distance = sample_offset/2;
|
|
|
|
if (whence == SEEK_SET) {
|
|
|
|
offset = distance;
|
|
|
|
} else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
|
|
|
|
offset = distance + cur;
|
|
|
|
} else if (whence == SEEK_END) {
|
|
|
|
offset = max - distance;
|
|
|
|
}
|
|
|
|
if (whence != SEEK_FORCECUR) {
|
|
|
|
offset = (offset > max)?max:offset;
|
|
|
|
offset = (offset < min)?min:offset;
|
|
|
|
}
|
|
|
|
return fseeko(fs->f, offset, SEEK_SET);
|
2003-02-06 22:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vox_trunc(struct ast_filestream *fs)
|
|
|
|
{
|
2012-04-16 20:17:03 +00:00
|
|
|
int fd;
|
|
|
|
off_t cur;
|
|
|
|
|
|
|
|
if ((fd = fileno(fs->f)) < 0) {
|
|
|
|
ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox filestream %p: %s\n", fs, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2012-04-29 19:50:57 +00:00
|
|
|
if ((cur = ftello(fs->f)) < 0) {
|
2012-04-16 20:17:03 +00:00
|
|
|
ast_log(AST_LOG_WARNING, "Unable to determine current position in vox filestream %p: %s\n", fs, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Truncate file to current length */
|
|
|
|
return ftruncate(fd, cur);}
|
2003-02-06 22:11:43 +00:00
|
|
|
|
2006-02-20 23:35:12 +00:00
|
|
|
static off_t vox_tell(struct ast_filestream *fs)
|
2003-02-06 22:11:43 +00:00
|
|
|
{
|
2004-06-09 20:55:20 +00:00
|
|
|
off_t offset;
|
2006-02-20 23:35:12 +00:00
|
|
|
offset = ftello(fs->f) << 1;
|
2004-06-09 20:55:20 +00:00
|
|
|
return offset;
|
2003-02-06 22:11:43 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 16:22:10 +00:00
|
|
|
static struct ast_format_def vox_f = {
|
2006-04-04 12:59:25 +00:00
|
|
|
.name = "vox",
|
|
|
|
.exts = "vox",
|
|
|
|
.write = vox_write,
|
|
|
|
.seek = vox_seek,
|
|
|
|
.trunc = vox_trunc,
|
|
|
|
.tell = vox_tell,
|
|
|
|
.read = vox_read,
|
|
|
|
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
|
|
|
|
};
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2001-03-22 22:25:06 +00:00
|
|
|
{
|
2011-02-03 16:22:10 +00:00
|
|
|
ast_format_set(&vox_f.format, AST_FORMAT_ADPCM, 0);
|
|
|
|
if (ast_format_def_register(&vox_f))
|
2007-10-31 19:24:29 +00:00
|
|
|
return AST_MODULE_LOAD_FAILURE;
|
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
2001-03-22 22:25:06 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2001-03-22 22:25:06 +00:00
|
|
|
{
|
2011-02-03 16:22:10 +00:00
|
|
|
return ast_format_def_unregister(vox_f.name);
|
2010-07-20 19:35:02 +00:00
|
|
|
}
|
2001-03-22 22:25:06 +00:00
|
|
|
|
2010-07-26 03:28:02 +00:00
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialogic VOX (ADPCM) File Format",
|
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
|
|
|
.load_pri = AST_MODPRI_APP_DEPEND
|
|
|
|
);
|