Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 364578 $")
34 
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38 
39 #define BUF_SIZE 80 /* 80 bytes, 160 samples */
40 #define VOX_SAMPLES 160
41 
42 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
43 {
44  int res;
45 
46  /* Send a frame from the file to the appropriate channel */
49  s->fr.mallocd = 0;
51  if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
52  if (res)
53  ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
54  return NULL;
55  }
56  *whennext = s->fr.samples = res * 2;
57  s->fr.datalen = res;
58  return &s->fr;
59 }
60 
61 static int vox_write(struct ast_filestream *s, struct ast_frame *f)
62 {
63  int res;
64  if (f->frametype != AST_FRAME_VOICE) {
65  ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
66  return -1;
67  }
68  if (f->subclass.codec != AST_FORMAT_ADPCM) {
69  ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%s)!\n", ast_getformatname(f->subclass.codec));
70  return -1;
71  }
72  if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
73  ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
74  return -1;
75  }
76  return 0;
77 }
78 
79 static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
80 {
81  off_t offset = 0, min = 0, cur, max, distance;
82 
83  if ((cur = ftello(fs->f)) < 0) {
84  ast_log(AST_LOG_WARNING, "Unable to determine current position in g719 filestream %p: %s\n", fs, strerror(errno));
85  return -1;
86  }
87 
88  if (fseeko(fs->f, 0, SEEK_END) < 0) {
89  ast_log(AST_LOG_WARNING, "Unable to seek to end of g719 filestream %p: %s\n", fs, strerror(errno));
90  return -1;
91  }
92 
93  if ((max = ftello(fs->f)) < 0) {
94  ast_log(AST_LOG_WARNING, "Unable to determine max position in g719 filestream %p: %s\n", fs, strerror(errno));
95  return -1;
96  }
97 
98  /* have to fudge to frame here, so not fully to sample */
99  distance = sample_offset/2;
100  if (whence == SEEK_SET) {
101  offset = distance;
102  } else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) {
103  offset = distance + cur;
104  } else if (whence == SEEK_END) {
105  offset = max - distance;
106  }
107  if (whence != SEEK_FORCECUR) {
108  offset = (offset > max)?max:offset;
109  offset = (offset < min)?min:offset;
110  }
111  return fseeko(fs->f, offset, SEEK_SET);
112 }
113 
114 static int vox_trunc(struct ast_filestream *fs)
115 {
116  int fd;
117  off_t cur;
118 
119  if ((fd = fileno(fs->f)) < 0) {
120  ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for vox filestream %p: %s\n", fs, strerror(errno));
121  return -1;
122  }
123  if ((cur = ftello(fs->f)) < 0) {
124  ast_log(AST_LOG_WARNING, "Unable to determine current position in vox filestream %p: %s\n", fs, strerror(errno));
125  return -1;
126  }
127  /* Truncate file to current length */
128  return ftruncate(fd, cur);}
129 
130 static off_t vox_tell(struct ast_filestream *fs)
131 {
132  off_t offset;
133  offset = ftello(fs->f) << 1;
134  return offset;
135 }
136 
137 static const struct ast_format vox_f = {
138  .name = "vox",
139  .exts = "vox",
140  .format = AST_FORMAT_ADPCM,
141  .write = vox_write,
142  .seek = vox_seek,
143  .trunc = vox_trunc,
144  .tell = vox_tell,
145  .read = vox_read,
146  .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
147 };
148 
149 static int load_module(void)
150 {
151  if (ast_format_register(&vox_f))
154 }
155 
156 static int unload_module(void)
157 {
158  return ast_format_unregister(vox_f.name);
159 }
160 
161 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialogic VOX (ADPCM) File Format",
162  .load = load_module,
163  .unload = unload_module,
164  .load_pri = AST_MODPRI_APP_DEPEND
165 );
static struct ast_frame * vox_read(struct ast_filestream *s, int *whennext)
Definition: format_vox.c:42
union ast_frame_subclass subclass
Definition: frame.h:146
Asterisk main include file. File version handling, generic pbx functions.
#define ast_format_register(f)
Definition: mod_format.h:131
int offset
Definition: frame.h:156
void * ptr
Definition: frame.h:160
#define LOG_WARNING
Definition: logger.h:144
static int load_module(void)
Definition: format_vox.c:149
#define AST_LOG_WARNING
Definition: logger.h:149
static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
Definition: format_vox.c:79
Each supported file format is described by the following structure.
Definition: mod_format.h:43
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
format_t codec
Definition: frame.h:137
#define AST_FRAME_SET_BUFFER(fr, _base, _ofs, _datalen)
Definition: frame.h:183
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
Asterisk architecture endianess compatibility definitions.
int datalen
Definition: frame.h:148
struct ast_frame fr
Definition: mod_format.h:117
int ast_format_unregister(const char *name)
Unregisters a file format.
Definition: file.c:104
static struct ast_format vox_f
Definition: format_vox.c:137
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
char name[80]
Definition: mod_format.h:44
static int unload_module(void)
Definition: format_vox.c:156
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int vox_trunc(struct ast_filestream *fs)
Definition: format_vox.c:114
static off_t vox_tell(struct ast_filestream *fs)
Definition: format_vox.c:130
int errno
#define SEEK_FORCECUR
Definition: file.h:50
static struct ast_format f[]
Definition: format_g726.c:181
int mallocd
Definition: frame.h:152
This structure is allocated by file.c in one chunk, together with buf_size and desc_size bytes of mem...
Definition: mod_format.h:100
#define AST_FORMAT_ADPCM
Definition: frame.h:252
Data structure associated with a single frame of data.
Definition: frame.h:142
enum ast_frame_type frametype
Definition: frame.h:144
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
static int vox_write(struct ast_filestream *s, struct ast_frame *f)
Definition: format_vox.c:61
Asterisk module definitions.
union ast_frame::@172 data
#define BUF_SIZE
Definition: format_vox.c:39
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
int samples
Definition: frame.h:150