Largely simplify format handlers (for file copy etc.)
[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 <unistd.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <string.h>
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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         if (option_debug)
82                 ast_log(LOG_DEBUG, "Created filestream G.726-%dk.\n", 
83                                 40 - s->rate * 8);
84         return 0;
85 }
86
87 static int g726_40_open(struct ast_filestream *s)
88 {
89         return g726_open(s, RATE_40);
90 }
91
92 static int g726_32_open(struct ast_filestream *s)
93 {
94         return g726_open(s, RATE_32);
95 }
96
97 static int g726_24_open(struct ast_filestream *s)
98 {
99         return g726_open(s, RATE_24);
100 }
101
102 static int g726_16_open(struct ast_filestream *s)
103 {
104         return g726_open(s, RATE_16);
105 }
106
107 static int g726_40_rewrite(struct ast_filestream *s, const char *comment)
108 {
109         return g726_open(s, RATE_40);
110 }
111
112 static int g726_32_rewrite(struct ast_filestream *s, const char *comment)
113 {
114         return g726_open(s, RATE_32);
115 }
116
117 static int g726_24_rewrite(struct ast_filestream *s, const char *comment)
118 {
119         return g726_open(s, RATE_24);
120 }
121
122 static int g726_16_rewrite(struct ast_filestream *s, const char *comment)
123 {
124         return g726_open(s, RATE_16);
125 }
126
127 /*
128  * Rate independent format functions (read, write)
129  */
130
131 static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
132 {
133         int res;
134         struct g726_desc *fs = (struct g726_desc *)s->private;
135
136         /* Send a frame from the file to the appropriate channel */
137         s->fr.frametype = AST_FRAME_VOICE;
138         s->fr.subclass = AST_FORMAT_G726;
139         s->fr.mallocd = 0;
140         FR_SET_BUF(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
141         s->fr.samples = 8 * FRAME_TIME;
142         if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
143                 if (res)
144                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
145                 return NULL;
146         }
147         *whennext = s->fr.samples;
148         return &s->fr;
149 }
150
151 static int g726_write(struct ast_filestream *s, struct ast_frame *f)
152 {
153         int res;
154         struct g726_desc *fs = (struct g726_desc *)s->private;
155
156         if (f->frametype != AST_FRAME_VOICE) {
157                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
158                 return -1;
159         }
160         if (f->subclass != AST_FORMAT_G726) {
161                 ast_log(LOG_WARNING, "Asked to write non-G726 frame (%d)!\n", 
162                                                 f->subclass);
163                 return -1;
164         }
165         if (f->datalen % frame_size[fs->rate]) {
166                 ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", 
167                                                 f->datalen, frame_size[fs->rate]);
168                 return -1;
169         }
170         if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) {
171                 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", 
172                                 res, frame_size[fs->rate], strerror(errno));
173                         return -1;
174         }
175         return 0;
176 }
177
178 static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
179 {
180         return -1;
181 }
182
183 static int g726_trunc(struct ast_filestream *fs)
184 {
185         return -1;
186 }
187
188 static off_t g726_tell(struct ast_filestream *fs)
189 {
190         return -1;
191 }
192
193 static struct ast_format_lock me = { .usecnt = -1 };
194
195 static const struct ast_format f[] = {
196         {
197                 .name = "g726-40",
198                 .exts = "g726-40",
199                 .format = AST_FORMAT_G726,
200                 .open = g726_40_open,
201                 .rewrite = g726_40_rewrite,
202                 .write = g726_write,
203                 .seek = g726_seek,
204                 .trunc = g726_trunc,
205                 .tell = g726_tell,
206                 .read = g726_read,
207                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
208                 .desc_size = sizeof(struct g726_desc),
209                 .lockp = &me,
210         },
211         {
212                 .name = "g726-32",
213                 .exts = "g726-32",
214                 .format = AST_FORMAT_G726,
215                 .open = g726_32_open,
216                 .rewrite = g726_32_rewrite,
217                 .write = g726_write,
218                 .seek = g726_seek,
219                 .trunc = g726_trunc,
220                 .tell = g726_tell,
221                 .read = g726_read,
222                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
223                 .desc_size = sizeof(struct g726_desc),
224                 .lockp = &me,
225         },
226         {
227                 .name = "g726-24",
228                 .exts = "g726-24",
229                 .format = AST_FORMAT_G726,
230                 .open = g726_24_open,
231                 .rewrite = g726_24_rewrite,
232                 .write = g726_write,
233                 .seek = g726_seek,
234                 .trunc = g726_trunc,
235                 .tell = g726_tell,
236                 .read = g726_read,
237                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
238                 .desc_size = sizeof(struct g726_desc),
239                 .lockp = &me,
240         },
241         {
242                 .name = "g726-16",
243                 .exts = "g726-16",
244                 .format = AST_FORMAT_G726,
245                 .open = g726_16_open,
246                 .rewrite = g726_16_rewrite,
247                 .write = g726_write,
248                 .seek = g726_seek,
249                 .trunc = g726_trunc,
250                 .tell = g726_tell,
251                 .read = g726_read,
252                 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
253                 .desc_size = sizeof(struct g726_desc),
254                 .lockp = &me,
255         },
256         {       .format = 0 }   /* terminator */
257 };
258
259 /*
260  * Module interface (load_module, unload_module, usecount, description, key)
261  */
262 int load_module()
263 {
264         int i;
265
266         for (i = 0; f[i].format ; i++) {
267                 if (ast_format_register(&f[i])) {       /* errors are fatal */
268                         ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name);
269                         return -1;
270                 }
271         }
272         return 0;
273 }
274
275 int unload_module()
276 {
277         int i;
278
279         for (i = 0; f[i].format ; i++) {
280                 if (ast_format_unregister(f[i].name))
281                         ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name);
282         }
283         return(0);
284 }       
285
286 int usecount()
287 {
288         return me.usecnt;
289 }
290
291 char *description()
292 {
293         return "Raw G.726 (16/24/32/40kbps) data";
294 }
295
296 char *key()
297 {
298         return ASTERISK_GPL_KEY;
299 }
300