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