test_json: Fix test names with reserved words
[asterisk/asterisk.git] / formats / format_g726.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004 - 2005, inAccess Networks
5  *
6  * Michael Manousos <manousos@inaccessnetworks.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 Headerless G.726 (16/24/32/40kbps) data format for Asterisk.
22  * 
23  * File name extensions:
24  * \arg 40 kbps: g726-40
25  * \arg 32 kbps: g726-32
26  * \arg 24 kbps: g726-24
27  * \arg 16 kbps: g726-16
28  * \ingroup formats
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34  
35 #include "asterisk.h"
36
37 #include "asterisk/mod_format.h"
38 #include "asterisk/module.h"
39 #include "asterisk/endian.h"
40 #include "asterisk/format_cache.h"
41
42 #define RATE_40         0
43 #define RATE_32         1
44 #define RATE_24         2
45 #define RATE_16         3
46
47 /* We can only read/write chunks of FRAME_TIME ms G.726 data */
48 #define FRAME_TIME      10      /* 10 ms size */
49
50 #define BUF_SIZE        (5*FRAME_TIME)  /* max frame size in bytes ? */
51 /* Frame sizes in bytes */
52 static int frame_size[4] = { 
53                 FRAME_TIME * 5,
54                 FRAME_TIME * 4,
55                 FRAME_TIME * 3,
56                 FRAME_TIME * 2
57 };
58
59 struct g726_desc  {
60         int rate;       /* RATE_* defines */
61 };
62
63 /*
64  * Rate dependant format functions (open, rewrite)
65  */
66 static int g726_open(struct ast_filestream *tmp, int rate)
67 {
68         struct g726_desc *s = (struct g726_desc *)tmp->_private;
69         s->rate = rate;
70         ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
71         return 0;
72 }
73
74 static int g726_40_open(struct ast_filestream *s)
75 {
76         return g726_open(s, RATE_40);
77 }
78
79 static int g726_32_open(struct ast_filestream *s)
80 {
81         return g726_open(s, RATE_32);
82 }
83
84 static int g726_24_open(struct ast_filestream *s)
85 {
86         return g726_open(s, RATE_24);
87 }
88
89 static int g726_16_open(struct ast_filestream *s)
90 {
91         return g726_open(s, RATE_16);
92 }
93
94 static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
95 {
96         return g726_open(s, RATE_40);
97 }
98
99 static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
100 {
101         return g726_open(s, RATE_32);
102 }
103
104 static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
105 {
106         return g726_open(s, RATE_24);
107 }
108
109 static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
110 {
111         return g726_open(s, RATE_16);
112 }
113
114 /*
115  * Rate independent format functions (read, write)
116  */
117
118 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
119 {
120         int res;
121         struct g726_desc *fs = (struct g726_desc *)s->_private;
122
123         /* Send a frame from the file to the appropriate channel */
124         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
125         s->fr.samples = 8 * FRAME_TIME;
126         if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
127                 if (feof(s->f)) {
128                         if (res) {
129                                 ast_debug(3, "Incomplete frame data at end of %s file "
130                                                   "(expected %d bytes, read %d)\n",
131                                                   ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
132                         }
133                 } else {
134                         ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
135                                         ast_format_get_name(s->fr.subclass.format), strerror(errno));
136                 }
137                 return NULL;
138         }
139         *whennext = s->fr.samples;
140         return &s->fr;
141 }
142
143 static int g726_write(struct ast_filestream *s, struct ast_frame *f)
144 {
145         int res;
146         struct g726_desc *fs = (struct g726_desc *)s->_private;
147
148         if (f->datalen % frame_size[fs->rate]) {
149                 ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", 
150                                                 f->datalen, frame_size[fs->rate]);
151                 return -1;
152         }
153         if ((res = fwrite(f->data.ptr, 1, f->datalen, s->f)) != f->datalen) {
154                 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", 
155                                 res, frame_size[fs->rate], strerror(errno));
156                         return -1;
157         }
158         return 0;
159 }
160
161 static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
162 {
163         return -1;
164 }
165
166 static int g726_trunc(struct ast_filestream *fs)
167 {
168         return -1;
169 }
170
171 static off_t g726_tell(struct ast_filestream *fs)
172 {
173         return -1;
174 }
175
176 static struct ast_format_def f[] = {
177         {
178                 .name = "g726-40",
179                 .exts = "g726-40",
180                 .open = g726_40_open,
181                 .rewrite = g726_40_rewrite,
182                 .write = g726_write,
183                 .seek = g726_seek,
184                 .trunc = g726_trunc,
185                 .tell = g726_tell,
186                 .read = g726_read,
187                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
188                 .desc_size = sizeof(struct g726_desc),
189         },
190         {
191                 .name = "g726-32",
192                 .exts = "g726-32",
193                 .open = g726_32_open,
194                 .rewrite = g726_32_rewrite,
195                 .write = g726_write,
196                 .seek = g726_seek,
197                 .trunc = g726_trunc,
198                 .tell = g726_tell,
199                 .read = g726_read,
200                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
201                 .desc_size = sizeof(struct g726_desc),
202         },
203         {
204                 .name = "g726-24",
205                 .exts = "g726-24",
206                 .open = g726_24_open,
207                 .rewrite = g726_24_rewrite,
208                 .write = g726_write,
209                 .seek = g726_seek,
210                 .trunc = g726_trunc,
211                 .tell = g726_tell,
212                 .read = g726_read,
213                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
214                 .desc_size = sizeof(struct g726_desc),
215         },
216         {
217                 .name = "g726-16",
218                 .exts = "g726-16",
219                 .open = g726_16_open,
220                 .rewrite = g726_16_rewrite,
221                 .write = g726_write,
222                 .seek = g726_seek,
223                 .trunc = g726_trunc,
224                 .tell = g726_tell,
225                 .read = g726_read,
226                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
227                 .desc_size = sizeof(struct g726_desc),
228         },
229         {       .desc_size = 0 }        /* terminator */
230 };
231
232 static int unload_module(void)
233 {
234         int i;
235
236         for (i = 0; f[i].desc_size ; i++) {
237                 if (ast_format_def_unregister(f[i].name))
238                         ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
239         }
240         return(0);
241 }
242
243 static int load_module(void)
244 {
245         int i;
246
247         for (i = 0; f[i].desc_size ; i++) {
248                 f[i].format = ast_format_g726;
249                 if (ast_format_def_register(&f[i])) {   /* errors are fatal */
250                         ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
251                         unload_module();
252                         return AST_MODULE_LOAD_DECLINE;
253                 }
254         }
255         return AST_MODULE_LOAD_SUCCESS;
256 }
257
258 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw G.726 (16/24/32/40kbps) data",
259         .support_level = AST_MODULE_SUPPORT_CORE,
260         .load = load_module,
261         .unload = unload_module,
262         .load_pri = AST_MODPRI_APP_DEPEND
263 );