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