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