2 * Asterisk -- An open source telephony toolkit.
4 * Brian K. West <brian@bkw.org>
6 * Copyright (C) 1999 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
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.
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.
23 * \brief Save to raw, headerless iLBC data.
24 * \arg File name extension: ilbc
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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"
49 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
51 /* Portions of the conversion code are by guido@sienanet.it */
53 struct ast_filestream {
54 void *reserved[AST_RESERVED_POINTERS];
55 /* Believe it or not, we must decode/recode to account for the
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 */
66 AST_MUTEX_DEFINE_STATIC(ilbc_lock);
67 static int glistcnt = 0;
69 static char *name = "iLBC";
70 static char *desc = "Raw iLBC data";
71 static char *exts = "ilbc";
73 static struct ast_filestream *ilbc_open(FILE *f)
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");
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 */
94 ast_mutex_unlock(&ilbc_lock);
95 ast_update_use_count();
100 static struct ast_filestream *ilbc_rewrite(FILE *f, const char *comment)
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");
115 ast_mutex_unlock(&ilbc_lock);
116 ast_update_use_count();
118 ast_log(LOG_WARNING, "Out of memory\n");
122 static void ilbc_close(struct ast_filestream *s)
124 if (ast_mutex_lock(&ilbc_lock)) {
125 ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
129 ast_mutex_unlock(&ilbc_lock);
130 ast_update_use_count();
136 static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
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;
146 s->fr.data = s->ilbc;
147 if ((res = fread(s->ilbc, 1, 50, s->f)) != 50) {
149 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
152 *whennext = s->fr.samples;
156 static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
159 if (f->frametype != AST_FRAME_VOICE) {
160 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
163 if (f->subclass != AST_FORMAT_ILBC) {
164 ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass);
167 if (f->datalen % 50) {
168 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
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));
178 static char *ilbc_getcomment(struct ast_filestream *s)
183 static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
186 off_t min,cur,max,offset=0;
189 fseek(fs->f, 0, SEEK_END);
192 bytes = 50 * (sample_offset / 240);
193 if (whence == SEEK_SET)
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;
202 /* protect against seeking beyond begining. */
203 offset = (offset < min)?min:offset;
204 if (fseek(fs->f, offset, SEEK_SET) < 0)
209 static int ilbc_trunc(struct ast_filestream *fs)
211 /* Truncate file to current length */
212 if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
217 static long ilbc_tell(struct ast_filestream *fs)
220 offset = ftell(fs->f);
221 return (offset/50)*240;
226 return ast_format_register(name, exts, AST_FORMAT_ILBC,
242 return ast_format_unregister(name);
258 return ASTERISK_GPL_KEY;