Files
asterisk/formats/format_vox.c
T

147 lines
3.6 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
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include <unistd.h>
2003-09-08 16:48:07 +00:00
#include <netinet/in.h>
2001-03-22 22:25:06 +00:00
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
2005-06-06 22:12:19 +00:00
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/sched.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"
#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;
2001-03-22 22:25:06 +00:00
/* Send a frame from the file to the appropriate channel */
s->fr.frametype = AST_FRAME_VOICE;
s->fr.subclass = AST_FORMAT_ADPCM;
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);
if ((res = fread(s->fr.data, 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
}
*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 (f->frametype != AST_FRAME_VOICE) {
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
return -1;
}
if (f->subclass != AST_FORMAT_ADPCM) {
ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
return -1;
}
if ((res = fwrite(f->data, 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
{
2004-06-09 20:55:20 +00:00
off_t offset=0,min,cur,max,distance;
min = 0;
2006-02-20 23:35:12 +00:00
cur = ftello(fs->f);
fseeko(fs->f, 0, SEEK_END);
max = ftello(fs->f);
2004-06-09 20:55:20 +00:00
/* 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)
{
2006-02-20 23:35:12 +00:00
return ftruncate(fileno(fs->f), ftello(fs->f));
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
}
static const struct ast_format vox_f = {
.name = "vox",
.exts = "vox",
.format = AST_FORMAT_ADPCM,
.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
{
return ast_format_register(&vox_f);
2001-03-22 22:25:06 +00:00
}
static int unload_module(void)
2001-03-22 22:25:06 +00:00
{
return ast_format_unregister(vox_f.name);
2001-03-22 22:25:06 +00:00
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialogic VOX (ADPCM) File Format");