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