2 * Asterisk -- A telephony toolkit for Linux.
4 * Save to raw, headerless iLBC data.
6 * Brian K. West <brian@bkw.org>
8 * Copyright (C) 1999 - 2005, Digium, Inc.
10 * Mark Spencer <markster@digium.com>
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License
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>
32 /* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */
34 /* Portions of the conversion code are by guido@sienanet.it */
36 struct ast_filestream {
37 void *reserved[AST_RESERVED_POINTERS];
38 /* Believe it or not, we must decode/recode to account for the
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 */
49 AST_MUTEX_DEFINE_STATIC(ilbc_lock);
50 static int glistcnt = 0;
52 static char *name = "iLBC";
53 static char *desc = "Raw iLBC data";
54 static char *exts = "ilbc";
56 static struct ast_filestream *ilbc_open(int fd)
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");
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 */
77 ast_mutex_unlock(&ilbc_lock);
78 ast_update_use_count();
83 static struct ast_filestream *ilbc_rewrite(int fd, const char *comment)
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");
98 ast_mutex_unlock(&ilbc_lock);
99 ast_update_use_count();
101 ast_log(LOG_WARNING, "Out of memory\n");
105 static void ilbc_close(struct ast_filestream *s)
107 if (ast_mutex_lock(&ilbc_lock)) {
108 ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
112 ast_mutex_unlock(&ilbc_lock);
113 ast_update_use_count();
119 static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
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;
129 s->fr.data = s->ilbc;
130 if ((res = read(s->fd, s->ilbc, 50)) != 50) {
132 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
135 *whennext = s->fr.samples;
139 static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
142 if (f->frametype != AST_FRAME_VOICE) {
143 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
146 if (f->subclass != AST_FORMAT_ILBC) {
147 ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass);
150 if (f->datalen % 50) {
151 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
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));
161 static char *ilbc_getcomment(struct ast_filestream *s)
166 static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
169 off_t min,cur,max,offset=0;
171 cur = lseek(fs->fd, 0, SEEK_CUR);
172 max = lseek(fs->fd, 0, SEEK_END);
174 bytes = 50 * (sample_offset / 240);
175 if (whence == SEEK_SET)
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;
184 /* protect against seeking beyond begining. */
185 offset = (offset < min)?min:offset;
186 if (lseek(fs->fd, offset, SEEK_SET) < 0)
191 static int ilbc_trunc(struct ast_filestream *fs)
193 /* Truncate file to current length */
194 if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0)
199 static long ilbc_tell(struct ast_filestream *fs)
202 offset = lseek(fs->fd, 0, SEEK_CUR);
203 return (offset/50)*240;
208 return ast_format_register(name, exts, AST_FORMAT_ILBC,
224 return ast_format_unregister(name);
230 if (ast_mutex_lock(&ilbc_lock)) {
231 ast_log(LOG_WARNING, "Unable to lock ilbc list\n");
235 ast_mutex_unlock(&ilbc_lock);
247 return ASTERISK_GPL_KEY;