update MANY more files with proper copyright/license info (thanks Ian!)
[asterisk/asterisk.git] / formats / format_ilbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Brian K. West <brian@bkw.org>
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*
22  *
23  * Save to raw, headerless iLBC data.
24  *
25  */
26  
27 #include <unistd.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <string.h>
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
47
48 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
49
50 /* Portions of the conversion code are by guido@sienanet.it */
51
52 struct ast_filestream {
53         void *reserved[AST_RESERVED_POINTERS];
54         /* Believe it or not, we must decode/recode to account for the
55            weird MS format */
56         /* This is what a filestream means to us */
57         int fd; /* Descriptor */
58         struct ast_frame fr;                            /* Frame information */
59         char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
60         char empty;                                                     /* Empty character */
61         unsigned char ilbc[50];                         /* One Real iLBC Frame */
62 };
63
64
65 AST_MUTEX_DEFINE_STATIC(ilbc_lock);
66 static int glistcnt = 0;
67
68 static char *name = "iLBC";
69 static char *desc = "Raw iLBC data";
70 static char *exts = "ilbc";
71
72 static struct ast_filestream *ilbc_open(int fd)
73 {
74         /* We don't have any header to read or anything really, but
75            if we did, it would go here.  We also might want to check
76            and be sure it's a valid file.  */
77         struct ast_filestream *tmp;
78         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
79                 memset(tmp, 0, sizeof(struct ast_filestream));
80                 if (ast_mutex_lock(&ilbc_lock)) {
81                         ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
82                         free(tmp);
83                         return NULL;
84                 }
85                 tmp->fd = fd;
86                 tmp->fr.data = tmp->ilbc;
87                 tmp->fr.frametype = AST_FRAME_VOICE;
88                 tmp->fr.subclass = AST_FORMAT_ILBC;
89                 /* datalen will vary for each frame */
90                 tmp->fr.src = name;
91                 tmp->fr.mallocd = 0;
92                 glistcnt++;
93                 ast_mutex_unlock(&ilbc_lock);
94                 ast_update_use_count();
95         }
96         return tmp;
97 }
98
99 static struct ast_filestream *ilbc_rewrite(int fd, const char *comment)
100 {
101         /* We don't have any header to read or anything really, but
102            if we did, it would go here.  We also might want to check
103            and be sure it's a valid file.  */
104         struct ast_filestream *tmp;
105         if ((tmp = malloc(sizeof(struct ast_filestream)))) {
106                 memset(tmp, 0, sizeof(struct ast_filestream));
107                 if (ast_mutex_lock(&ilbc_lock)) {
108                         ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
109                         free(tmp);
110                         return NULL;
111                 }
112                 tmp->fd = fd;
113                 glistcnt++;
114                 ast_mutex_unlock(&ilbc_lock);
115                 ast_update_use_count();
116         } else
117                 ast_log(LOG_WARNING, "Out of memory\n");
118         return tmp;
119 }
120
121 static void ilbc_close(struct ast_filestream *s)
122 {
123         if (ast_mutex_lock(&ilbc_lock)) {
124                 ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
125                 return;
126         }
127         glistcnt--;
128         ast_mutex_unlock(&ilbc_lock);
129         ast_update_use_count();
130         close(s->fd);
131         free(s);
132         s = NULL;
133 }
134
135 static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
136 {
137         int res;
138         /* Send a frame from the file to the appropriate channel */
139         s->fr.frametype = AST_FRAME_VOICE;
140         s->fr.subclass = AST_FORMAT_ILBC;
141         s->fr.offset = AST_FRIENDLY_OFFSET;
142         s->fr.samples = 240;
143         s->fr.datalen = 50;
144         s->fr.mallocd = 0;
145         s->fr.data = s->ilbc;
146         if ((res = read(s->fd, s->ilbc, 50)) != 50) {
147                 if (res)
148                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
149                 return NULL;
150         }
151         *whennext = s->fr.samples;
152         return &s->fr;
153 }
154
155 static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
156 {
157         int res;
158         if (f->frametype != AST_FRAME_VOICE) {
159                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
160                 return -1;
161         }
162         if (f->subclass != AST_FORMAT_ILBC) {
163                 ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass);
164                 return -1;
165         }
166         if (f->datalen % 50) {
167                 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
168                 return -1;
169         }
170         if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
171                         ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
172                         return -1;
173         }
174         return 0;
175 }
176
177 static char *ilbc_getcomment(struct ast_filestream *s)
178 {
179         return NULL;
180 }
181
182 static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
183 {
184         long bytes;
185         off_t min,cur,max,offset=0;
186         min = 0;
187         cur = lseek(fs->fd, 0, SEEK_CUR);
188         max = lseek(fs->fd, 0, SEEK_END);
189         
190         bytes = 50 * (sample_offset / 240);
191         if (whence == SEEK_SET)
192                 offset = bytes;
193         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
194                 offset = cur + bytes;
195         else if (whence == SEEK_END)
196                 offset = max - bytes;
197         if (whence != SEEK_FORCECUR) {
198                 offset = (offset > max)?max:offset;
199         }
200         /* protect against seeking beyond begining. */
201         offset = (offset < min)?min:offset;
202         if (lseek(fs->fd, offset, SEEK_SET) < 0)
203                 return -1;
204         return 0;
205 }
206
207 static int ilbc_trunc(struct ast_filestream *fs)
208 {
209         /* Truncate file to current length */
210         if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
211                 return -1;
212         return 0;
213 }
214
215 static long ilbc_tell(struct ast_filestream *fs)
216 {
217         off_t offset;
218         offset = lseek(fs->fd, 0, SEEK_CUR);
219         return (offset/50)*240;
220 }
221
222 int load_module()
223 {
224         return ast_format_register(name, exts, AST_FORMAT_ILBC,
225                                                                 ilbc_open,
226                                                                 ilbc_rewrite,
227                                                                 ilbc_write,
228                                                                 ilbc_seek,
229                                                                 ilbc_trunc,
230                                                                 ilbc_tell,
231                                                                 ilbc_read,
232                                                                 ilbc_close,
233                                                                 ilbc_getcomment);
234                                                                 
235                                                                 
236 }
237
238 int unload_module()
239 {
240         return ast_format_unregister(name);
241 }       
242
243 int usecount()
244 {
245         return glistcnt;
246 }
247
248 char *description()
249 {
250         return desc;
251 }
252
253
254 char *key()
255 {
256         return ASTERISK_GPL_KEY;
257 }