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