more doxygenification (issue #5513)
[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  *
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         FILE *f; /* 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(FILE *f)
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->f = f;
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(FILE *f, 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->f = f;
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         fclose(s->f);
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 = fread(s->ilbc, 1, 50, s->f)) != 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 = fwrite(f->data, 1, f->datalen, fs->f)) != 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 = ftell(fs->f);
188         fseek(fs->f, 0, SEEK_END);
189         max = ftell(fs->f);
190         
191         bytes = 50 * (sample_offset / 240);
192         if (whence == SEEK_SET)
193                 offset = bytes;
194         else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
195                 offset = cur + bytes;
196         else if (whence == SEEK_END)
197                 offset = max - bytes;
198         if (whence != SEEK_FORCECUR) {
199                 offset = (offset > max)?max:offset;
200         }
201         /* protect against seeking beyond begining. */
202         offset = (offset < min)?min:offset;
203         if (fseek(fs->f, offset, SEEK_SET) < 0)
204                 return -1;
205         return 0;
206 }
207
208 static int ilbc_trunc(struct ast_filestream *fs)
209 {
210         /* Truncate file to current length */
211         if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
212                 return -1;
213         return 0;
214 }
215
216 static long ilbc_tell(struct ast_filestream *fs)
217 {
218         off_t offset;
219         offset = ftell(fs->f);
220         return (offset/50)*240;
221 }
222
223 int load_module()
224 {
225         return ast_format_register(name, exts, AST_FORMAT_ILBC,
226                                                                 ilbc_open,
227                                                                 ilbc_rewrite,
228                                                                 ilbc_write,
229                                                                 ilbc_seek,
230                                                                 ilbc_trunc,
231                                                                 ilbc_tell,
232                                                                 ilbc_read,
233                                                                 ilbc_close,
234                                                                 ilbc_getcomment);
235                                                                 
236                                                                 
237 }
238
239 int unload_module()
240 {
241         return ast_format_unregister(name);
242 }       
243
244 int usecount()
245 {
246         return glistcnt;
247 }
248
249 char *description()
250 {
251         return desc;
252 }
253
254
255 char *key()
256 {
257         return ASTERISK_GPL_KEY;
258 }