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