2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@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 Flat, binary, ADPCM vox file format.
22 * \arg File name extensions: vox
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
48 #define BUF_SIZE 80 /* 160 samples */
50 struct ast_filestream {
51 void *reserved[AST_RESERVED_POINTERS];
52 /* This is what a filestream means to us */
53 FILE *f; /* Descriptor */
54 struct ast_frame fr; /* Frame information */
55 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
56 char empty; /* Empty character */
57 unsigned char buf[BUF_SIZE]; /* Output Buffer */
60 short signal; /* Signal level (file side) */
61 short ssindex; /* Signal ssindex (file side) */
62 unsigned char zero_count; /* counter of consecutive zero samples */
63 unsigned char next_flag;
67 AST_MUTEX_DEFINE_STATIC(vox_lock);
68 static int glistcnt = 0;
70 static char *name = "vox";
71 static char *desc = "Dialogic VOX (ADPCM) File Format";
72 static char *exts = "vox";
74 static struct ast_filestream *vox_open(FILE *f)
76 /* We don't have any header to read or anything really, but
77 if we did, it would go here. We also might want to check
78 and be sure it's a valid file. */
79 struct ast_filestream *tmp;
80 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
81 memset(tmp, 0, sizeof(struct ast_filestream));
82 if (ast_mutex_lock(&vox_lock)) {
83 ast_log(LOG_WARNING, "Unable to lock vox list\n");
88 tmp->fr.data = tmp->buf;
89 tmp->fr.frametype = AST_FRAME_VOICE;
90 tmp->fr.subclass = AST_FORMAT_ADPCM;
91 /* datalen will vary for each frame */
94 tmp->lasttimeout = -1;
96 ast_mutex_unlock(&vox_lock);
97 ast_update_use_count();
102 static struct ast_filestream *vox_rewrite(FILE *f, const char *comment)
104 /* We don't have any header to read or anything really, but
105 if we did, it would go here. We also might want to check
106 and be sure it's a valid file. */
107 struct ast_filestream *tmp;
108 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
109 memset(tmp, 0, sizeof(struct ast_filestream));
110 if (ast_mutex_lock(&vox_lock)) {
111 ast_log(LOG_WARNING, "Unable to lock vox list\n");
117 ast_mutex_unlock(&vox_lock);
118 ast_update_use_count();
120 ast_log(LOG_WARNING, "Out of memory\n");
124 static void vox_close(struct ast_filestream *s)
126 if (ast_mutex_lock(&vox_lock)) {
127 ast_log(LOG_WARNING, "Unable to lock vox list\n");
131 ast_mutex_unlock(&vox_lock);
132 ast_update_use_count();
138 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
141 /* Send a frame from the file to the appropriate channel */
142 s->fr.frametype = AST_FRAME_VOICE;
143 s->fr.subclass = AST_FORMAT_ADPCM;
144 s->fr.offset = AST_FRIENDLY_OFFSET;
147 if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
149 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
152 s->fr.samples = res * 2;
154 *whennext = s->fr.samples;
158 static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
161 if (f->frametype != AST_FRAME_VOICE) {
162 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
165 if (f->subclass != AST_FORMAT_ADPCM) {
166 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
169 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
170 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
176 static char *vox_getcomment(struct ast_filestream *s)
181 static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
183 off_t offset=0,min,cur,max,distance;
187 fseek(fs->f, 0, SEEK_END);
190 /* have to fudge to frame here, so not fully to sample */
191 distance = sample_offset/2;
192 if(whence == SEEK_SET)
194 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
195 offset = distance + cur;
196 else if(whence == SEEK_END)
197 offset = max - distance;
198 if (whence != SEEK_FORCECUR) {
199 offset = (offset > max)?max:offset;
200 offset = (offset < min)?min:offset;
202 fseek(fs->f, offset, SEEK_SET);
206 static int vox_trunc(struct ast_filestream *fs)
208 return ftruncate(fileno(fs->f), ftell(fs->f));
211 static long vox_tell(struct ast_filestream *fs)
214 offset = ftell(fs->f) << 1;
220 return ast_format_register(name, exts, AST_FORMAT_ADPCM,
236 return ast_format_unregister(name);
252 return ASTERISK_GPL_KEY;