include "logger.h" and errno.h from asterisk.h - usage shows that they
[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 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <sys/time.h>
38
39 #include "asterisk/lock.h"
40 #include "asterisk/options.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/sched.h"
44 #include "asterisk/module.h"
45 #include "asterisk/endian.h"
46
47 #define RATE_40         0
48 #define RATE_32         1
49 #define RATE_24         2
50 #define RATE_16         3
51
52 /* We can only read/write chunks of FRAME_TIME ms G.726 data */
53 #define FRAME_TIME      10      /* 10 ms size */
54
55 #define BUF_SIZE        (5*FRAME_TIME)  /* max frame size in bytes ? */
56 /* Frame sizes in bytes */
57 static int frame_size[4] = { 
58                 FRAME_TIME * 5,
59                 FRAME_TIME * 4,
60                 FRAME_TIME * 3,
61                 FRAME_TIME * 2
62 };
63
64 struct g726_desc  {
65         int rate;       /* RATE_* defines */
66 };
67
68 /*
69  * Rate dependant format functions (open, rewrite)
70  */
71 static int g726_open(struct ast_filestream *tmp, int rate)
72 {
73         struct g726_desc *s = (struct g726_desc *)tmp->private;
74         s->rate = rate;
75         ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8);
76         return 0;
77 }
78
79 static int g726_40_open(struct ast_filestream *s)
80 {
81         return g726_open(s, RATE_40);
82 }
83
84 static int g726_32_open(struct ast_filestream *s)
85 {
86         return g726_open(s, RATE_32);
87 }
88
89 static int g726_24_open(struct ast_filestream *s)
90 {
91         return g726_open(s, RATE_24);
92 }
93
94 static int g726_16_open(struct ast_filestream *s)
95 {
96         return g726_open(s, RATE_16);
97 }
98
99 static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
100 {
101         return g726_open(s, RATE_40);
102 }
103
104 static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
105 {
106         return g726_open(s, RATE_32);
107 }
108
109 static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
110 {
111         return g726_open(s, RATE_24);
112 }
113
114 static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
115 {
116         return g726_open(s, RATE_16);
117 }
118
119 /*
120  * Rate independent format functions (read, write)
121  */
122
123 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
124 {
125         int res;
126         struct g726_desc *fs = (struct g726_desc *)s->private;
127
128         /* Send a frame from the file to the appropriate channel */
129         s->fr.frametype = AST_FRAME_VOICE;
130         s->fr.subclass = AST_FORMAT_G726;
131         s->fr.mallocd = 0;
132         AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
133         s->fr.samples = 8 * FRAME_TIME;
134         if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
135                 if (res)
136                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
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->frametype != AST_FRAME_VOICE) {
149                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
150                 return -1;
151         }
152         if (f->subclass != AST_FORMAT_G726) {
153                 ast_log(LOG_WARNING, "Asked to write non-G726 frame (%d)!\n", 
154                                                 f->subclass);
155                 return -1;
156         }
157         if (f->datalen % frame_size[fs->rate]) {
158                 ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", 
159                                                 f->datalen, frame_size[fs->rate]);
160                 return -1;
161         }
162         if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
163                 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", 
164                                 res, frame_size[fs->rate], strerror(errno));
165                         return -1;
166         }
167         return 0;
168 }
169
170 static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
171 {
172         return -1;
173 }
174
175 static int g726_trunc(struct ast_filestream *fs)
176 {
177         return -1;
178 }
179
180 static off_t g726_tell(struct ast_filestream *fs)
181 {
182         return -1;
183 }
184
185 static const struct ast_format f[] = {
186         {
187                 .name = "g726-40",
188                 .exts = "g726-40",
189                 .format = AST_FORMAT_G726,
190                 .open = g726_40_open,
191                 .rewrite = g726_40_rewrite,
192                 .write = g726_write,
193                 .seek = g726_seek,
194                 .trunc = g726_trunc,
195                 .tell = g726_tell,
196                 .read = g726_read,
197                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
198                 .desc_size = sizeof(struct g726_desc),
199         },
200         {
201                 .name = "g726-32",
202                 .exts = "g726-32",
203                 .format = AST_FORMAT_G726,
204                 .open = g726_32_open,
205                 .rewrite = g726_32_rewrite,
206                 .write = g726_write,
207                 .seek = g726_seek,
208                 .trunc = g726_trunc,
209                 .tell = g726_tell,
210                 .read = g726_read,
211                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
212                 .desc_size = sizeof(struct g726_desc),
213         },
214         {
215                 .name = "g726-24",
216                 .exts = "g726-24",
217                 .format = AST_FORMAT_G726,
218                 .open = g726_24_open,
219                 .rewrite = g726_24_rewrite,
220                 .write = g726_write,
221                 .seek = g726_seek,
222                 .trunc = g726_trunc,
223                 .tell = g726_tell,
224                 .read = g726_read,
225                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
226                 .desc_size = sizeof(struct g726_desc),
227         },
228         {
229                 .name = "g726-16",
230                 .exts = "g726-16",
231                 .format = AST_FORMAT_G726,
232                 .open = g726_16_open,
233                 .rewrite = g726_16_rewrite,
234                 .write = g726_write,
235                 .seek = g726_seek,
236                 .trunc = g726_trunc,
237                 .tell = g726_tell,
238                 .read = g726_read,
239                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
240                 .desc_size = sizeof(struct g726_desc),
241         },
242         {       .format = 0 }   /* terminator */
243 };
244
245 static int load_module(void)
246 {
247         int i;
248
249         for (i = 0; f[i].format ; i++) {
250                 if (ast_format_register(&f[i])) {       /* errors are fatal */
251                         ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
252                         return AST_MODULE_LOAD_FAILURE;
253                 }
254         }
255         return AST_MODULE_LOAD_SUCCESS;
256 }
257
258 static int unload_module(void)
259 {
260         int i;
261
262         for (i = 0; f[i].format ; i++) {
263                 if (ast_format_unregister(f[i].name))
264                         ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
265         }
266         return(0);
267 }       
268
269 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw G.726 (16/24/32/40kbps) data");