Asterisk - The Open Source Telephony Project GIT-master-f36a736
format_vox.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
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.
13 *
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.
17 */
18
19/*! \file
20 *
21 * \brief Flat, binary, ADPCM vox file format.
22 * \arg File name extensions: vox
23 *
24 * \ingroup formats
25 */
26
27/*** MODULEINFO
28 <support_level>extended</support_level>
29 ***/
30
31#include "asterisk.h"
32
33#include "asterisk/mod_format.h"
34#include "asterisk/module.h"
35#include "asterisk/endian.h"
37
38#define BUF_SIZE 80 /* 80 bytes, 160 samples */
39#define VOX_SAMPLES 160
40
41static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
42{
43 size_t res;
44
45 /* Send a frame from the file to the appropriate channel */
47 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
48 if (res) {
49 ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
51 strerror(errno));
52 }
53 return NULL;
54 }
55 *whennext = s->fr.samples = res * 2;
56 s->fr.datalen = res;
57 return &s->fr;
58}
59
60static int vox_write(struct ast_filestream *s, struct ast_frame *f)
61{
62 int res;
63 if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
64 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
65 return -1;
66 }
67 return 0;
68}
69
70static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
71{
72 off_t offset = 0, min = 0, cur, max, distance;
73
74 if ((cur = ftello(fs->f)) < 0) {
75 ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
76 return -1;
77 }
78
79 if (fseeko(fs->f, 0, SEEK_END) < 0) {
80 ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
81 return -1;
82 }
83
84 if ((max = ftello(fs->f)) < 0) {
85 ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
86 return -1;
87 }
88
89 /* have to fudge to frame here, so not fully to sample */
90 distance = sample_offset/2;
91 if (whence == SEEK_SET) {
92 offset = distance;
93 } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
94 offset = distance + cur;
95 } else if (whence == SEEK_END) {
96 offset = max - distance;
97 }
98 if (whence != SEEK_FORCECUR) {
100 offset = (offset < min)?min:offset;
101 }
102 return fseeko(fs->f, offset, SEEK_SET);
103}
104
105static int vox_trunc(struct ast_filestream *fs)
106{
107 int fd;
108 off_t cur;
109
110 if ((fd = fileno(fs->f)) < 0) {
111 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox filestream %p: %s\n", fs, strerror(errno));
112 return -1;
113 }
114 if ((cur = ftello(fs->f)) < 0) {
115 ast_log(AST_LOG_WARNING, "Unable to determine current position in vox filestream %p: %s\n", fs, strerror(errno));
116 return -1;
117 }
118 /* Truncate file to current length */
119 return ftruncate(fd, cur);}
120
121static off_t vox_tell(struct ast_filestream *fs)
122{
123 off_t offset;
124 offset = ftello(fs->f) << 1;
125 return offset;
126}
127
128static struct ast_format_def vox_f = {
129 .name = "vox",
130 .exts = "vox",
131 .mime_types = "audio/x-vox",
132 .write = vox_write,
133 .seek = vox_seek,
134 .trunc = vox_trunc,
135 .tell = vox_tell,
136 .read = vox_read,
137 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
138};
139
140static int load_module(void)
141{
146}
147
148static int unload_module(void)
149{
151}
152
153AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialogic VOX (ADPCM) File Format",
154 .support_level = AST_MODULE_SUPPORT_EXTENDED,
155 .load = load_module,
156 .unload = unload_module,
157 .load_pri = AST_MODPRI_APP_DEPEND
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
Asterisk architecture endianess compatibility definitions.
#define min(a, b)
Definition: f2c.h:197
#define max(a, b)
Definition: f2c.h:198
#define SEEK_FORCECUR
Definition: file.h:51
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
struct ast_format * ast_format_adpcm
Built-in cached adpcm format.
Definition: format_cache.c:101
static int vox_trunc(struct ast_filestream *fs)
Definition: format_vox.c:105
static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_vox.c:70
static struct ast_frame * vox_read(struct ast_filestream *s, int *whennext)
Definition: format_vox.c:41
#define BUF_SIZE
Definition: format_vox.c:38
static off_t vox_tell(struct ast_filestream *fs)
Definition: format_vox.c:121
static struct ast_format_def vox_f
Definition: format_vox.c:128
static int load_module(void)
Definition: format_vox.c:140
static int unload_module(void)
Definition: format_vox.c:148
static int vox_write(struct ast_filestream *s, struct ast_frame *f)
Definition: format_vox.c:60
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
#define AST_LOG_WARNING
#define LOG_WARNING
int errno
Header for providers of file and format handling routines. Clients of these routines should include "...
int ast_format_def_unregister(const char *name)
Unregisters a file format.
Definition: file.c:162
#define ast_format_def_register(f)
Definition: mod_format.h:136
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_APP_DEPEND
Definition: module.h:342
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
#define NULL
Definition: resample.c:96
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:101
struct ast_frame fr
frame produced by read, typically
Definition: mod_format.h:122
Each supported file format is described by the following structure.
Definition: mod_format.h:43
char name[80]
Definition: mod_format.h:44
struct ast_format * format
Definition: mod_format.h:48
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data