ensure that the private structure for pseudo channels is created without 'leaking...
[asterisk-bristuff.git] / formats / format_vox.c
blobc729f3b5cbf5aaf1a83bc13f24bcbda420ac69c2
1 /*
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.
19 /*! \file
21 * \brief Flat, binary, ADPCM vox file format.
22 * \arg File name extensions: vox
24 * \ingroup formats
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <unistd.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.h>
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 /* 80 bytes, 160 samples */
49 #define VOX_SAMPLES 160
51 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
53 int res;
55 /* Send a frame from the file to the appropriate channel */
56 s->fr.frametype = AST_FRAME_VOICE;
57 s->fr.subclass = AST_FORMAT_ADPCM;
58 s->fr.mallocd = 0;
59 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
60 if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) {
61 if (res)
62 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
63 return NULL;
65 *whennext = s->fr.samples = res * 2;
66 s->fr.datalen = res;
67 return &s->fr;
70 static int vox_write(struct ast_filestream *s, struct ast_frame *f)
72 int res;
73 if (f->frametype != AST_FRAME_VOICE) {
74 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
75 return -1;
77 if (f->subclass != AST_FORMAT_ADPCM) {
78 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
79 return -1;
81 if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
82 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
83 return -1;
85 return 0;
88 static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
90 off_t offset=0,min,cur,max,distance;
92 min = 0;
93 cur = ftello(fs->f);
94 fseeko(fs->f, 0, SEEK_END);
95 max = ftello(fs->f);
97 /* have to fudge to frame here, so not fully to sample */
98 distance = sample_offset/2;
99 if(whence == SEEK_SET)
100 offset = distance;
101 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
102 offset = distance + cur;
103 else if(whence == SEEK_END)
104 offset = max - distance;
105 if (whence != SEEK_FORCECUR) {
106 offset = (offset > max)?max:offset;
107 offset = (offset < min)?min:offset;
109 return fseeko(fs->f, offset, SEEK_SET);
112 static int vox_trunc(struct ast_filestream *fs)
114 return ftruncate(fileno(fs->f), ftello(fs->f));
117 static off_t vox_tell(struct ast_filestream *fs)
119 off_t offset;
120 offset = ftello(fs->f) << 1;
121 return offset;
124 static const struct ast_format vox_f = {
125 .name = "vox",
126 .exts = "vox",
127 .format = AST_FORMAT_ADPCM,
128 .write = vox_write,
129 .seek = vox_seek,
130 .trunc = vox_trunc,
131 .tell = vox_tell,
132 .read = vox_read,
133 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
136 static int load_module(void)
138 return ast_format_register(&vox_f);
141 static int unload_module(void)
143 return ast_format_unregister(vox_f.name);
146 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialogic VOX (ADPCM) File Format");