Debian Codegraph @

formats/format_g729.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <[email protected]>
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 Save to raw, headerless G729 data.
22 * \note This is not an encoder/decoder. The codec for g729 is only
23 * available with a commercial license from Digium, due to patent
24 * restrictions. Check http://www.digium.com for information.
25 * \arg Extensions: g729
26 * \ingroup formats
27 */
28
29 /*** MODULEINFO
30 <support_level>core</support_level>
31 ***/
32
33 #include "asterisk.h"
34
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38 #include "asterisk/format_cache.h"
39
40 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
41
42 /* Portions of the conversion code are by [email protected] */
43
44 #define BUF_SIZE 20 /* two G729 frames */
45 #define G729A_SAMPLES 160
46
47 static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
48 {
49 size_t res;
50
51 /* Send a frame from the file to the appropriate channel */
52 s->fr.samples = G729A_SAMPLES;
53 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
54 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
55 if (res && res != 10) /* XXX what for ? */ {
56 ast_log(LOG_WARNING, "Short read of %s data (expected %d bytes, read %zu): %s\n",
57 ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res,
58 strerror(errno));
59 }
60 return NULL;
61 }
62 *whennext = s->fr.samples;
63 return &s->fr;
64 }
65
66 static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
67 {
68 int res;
69
70 if (f->datalen % 10) {
71 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
72 return -1;
73 }
74 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
75 ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
76 return -1;
77 }
78 return 0;
79 }
80
81 static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
82 {
83 long bytes;
84 off_t min,cur,max,offset=0;
85 min = 0;
86 cur = ftello(fs->f);
87 fseeko(fs->f, 0, SEEK_END);
88 max = ftello(fs->f);
89
90 bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES);
91 if (whence == SEEK_SET)
92 offset = bytes;
93 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
94 offset = cur + bytes;
95 else if (whence == SEEK_END)
96 offset = max - bytes;
97 if (whence != SEEK_FORCECUR) {
98 offset = (offset > max)?max:offset;
99 }
100 /* protect against seeking beyond begining. */
101 offset = (offset < min)?min:offset;
102 if (fseeko(fs->f, offset, SEEK_SET) < 0)
103 return -1;
104 return 0;
105 }
106
107 static int g729_trunc(struct ast_filestream *fs)
108 {
109 int fd;
110 off_t cur;
111
112 if ((fd = fileno(fs->f)) < 0) {
113 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for g729 filestream %p: %s\n", fs, strerror(errno));
114 return -1;
115 }
116 if ((cur = ftello(fs->f)) < 0) {
117 ast_log(AST_LOG_WARNING, "Unable to determine current position in g729 filestream %p: %s\n", fs, strerror(errno));
118 return -1;
119 }
120 /* Truncate file to current length */
121 return ftruncate(fd, cur);
122 }
123
124 static off_t g729_tell(struct ast_filestream *fs)
125 {
126 off_t offset = ftello(fs->f);
127 return (offset/BUF_SIZE)*G729A_SAMPLES;
128 }
129
130 static struct ast_format_def g729_f = {
131 .name = "g729",
132 .exts = "g729",
133 .write = g729_write,
134 .seek = g729_seek,
135 .trunc = g729_trunc,
136 .tell = g729_tell,
137 .read = g729_read,
138 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
139 };
140
141 static int load_module(void)
142 {
143 g729_f.format = ast_format_g729;
144 if (ast_format_def_register(&g729_f))
145 return AST_MODULE_LOAD_DECLINE;
146 return AST_MODULE_LOAD_SUCCESS;
147 }
148
149 static int unload_module(void)
150 {
151 return ast_format_def_unregister(g729_f.name);
152 }
153
154 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.729 data",
155 .support_level = AST_MODULE_SUPPORT_CORE,
156 .load = load_module,
157 .unload = unload_module,
158 .load_pri = AST_MODPRI_APP_DEPEND
159 );