Files
asterisk/formats/format_vox.c
T

159 lines
4.1 KiB
C
Raw Normal View History

2001-03-22 22:25:06 +00:00
/*
* Asterisk -- An open source telephony toolkit.
2001-03-22 22:25:06 +00:00
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
2001-03-22 22:25:06 +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
* 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-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-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>
***/
#include "asterisk.h"
#include "asterisk/mod_format.h"
2005-06-06 22:12:19 +00:00
#include "asterisk/module.h"
#include "asterisk/endian.h"
#include "asterisk/format_cache.h"
2005-06-06 22:12:19 +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
{
2017-09-05 11:05:48 -04:00
size_t res;
2001-03-22 22:25:06 +00:00
/* Send a frame from the file to the appropriate channel */
2006-04-09 22:31:38 +00:00
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
2017-09-05 11:05:48 -04:00
if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
if (res) {
ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
strerror(errno));
}
2003-06-28 22:50:47 +00:00
return NULL;
2001-03-22 22:25:06 +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
}
static int vox_write(struct ast_filestream *s, struct ast_frame *f)
2001-03-22 22:25:06 +00:00
{
int res;
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
{
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;
}
if ((max = ftello(fs->f)) < 0) {
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)
{
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;
}
if ((cur = ftello(fs->f)) < 0) {
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;
return offset;
2003-02-06 22:11:43 +00:00
}
static struct ast_format_def vox_f = {
.name = "vox",
.exts = "vox",
.mime_types = "audio/x-vox",
.write = vox_write,
.seek = vox_seek,
.trunc = vox_trunc,
.tell = vox_tell,
.read = vox_read,
.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
};
static int load_module(void)
2001-03-22 22:25:06 +00:00
{
vox_f.format = ast_format_adpcm;
if (ast_format_def_register(&vox_f))
return AST_MODULE_LOAD_DECLINE;
return AST_MODULE_LOAD_SUCCESS;
2001-03-22 22:25:06 +00:00
}
static int unload_module(void)
2001-03-22 22:25:06 +00:00
{
return ast_format_def_unregister(vox_f.name);
}
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",
.support_level = AST_MODULE_SUPPORT_EXTENDED,
2010-07-26 03:28:02 +00:00
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_APP_DEPEND
);