00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 364578 $")
00035
00036 #include "asterisk/mod_format.h"
00037 #include "asterisk/module.h"
00038 #include "asterisk/endian.h"
00039
00040
00041
00042
00043
00044 #define ILBC_BUF_SIZE 50
00045 #define ILBC_SAMPLES 240
00046
00047 static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
00048 {
00049 int res;
00050
00051 s->fr.frametype = AST_FRAME_VOICE;
00052 s->fr.subclass.codec = AST_FORMAT_ILBC;
00053 s->fr.mallocd = 0;
00054 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
00055 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
00056 if (res)
00057 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00058 return NULL;
00059 }
00060 *whennext = s->fr.samples = ILBC_SAMPLES;
00061 return &s->fr;
00062 }
00063
00064 static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
00065 {
00066 int res;
00067 if (f->frametype != AST_FRAME_VOICE) {
00068 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00069 return -1;
00070 }
00071 if (f->subclass.codec != AST_FORMAT_ILBC) {
00072 ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%s)!\n", ast_getformatname(f->subclass.codec));
00073 return -1;
00074 }
00075 if (f->datalen % 50) {
00076 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
00077 return -1;
00078 }
00079 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00080 ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
00081 return -1;
00082 }
00083 return 0;
00084 }
00085
00086 static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00087 {
00088 long bytes;
00089 off_t min,cur,max,offset=0;
00090 min = 0;
00091 cur = ftello(fs->f);
00092 fseeko(fs->f, 0, SEEK_END);
00093 max = ftello(fs->f);
00094
00095 bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES);
00096 if (whence == SEEK_SET)
00097 offset = bytes;
00098 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00099 offset = cur + bytes;
00100 else if (whence == SEEK_END)
00101 offset = max - bytes;
00102 if (whence != SEEK_FORCECUR) {
00103 offset = (offset > max)?max:offset;
00104 }
00105
00106 offset = (offset < min)?min:offset;
00107 if (fseeko(fs->f, offset, SEEK_SET) < 0)
00108 return -1;
00109 return 0;
00110 }
00111
00112 static int ilbc_trunc(struct ast_filestream *fs)
00113 {
00114 int fd;
00115 off_t cur;
00116
00117 if ((fd = fileno(fs->f)) < 0) {
00118 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for iLBC filestream %p: %s\n", fs, strerror(errno));
00119 return -1;
00120 }
00121 if ((cur = ftello(fs->f)) < 0) {
00122 ast_log(AST_LOG_WARNING, "Unable to determine current position in iLBC filestream %p: %s\n", fs, strerror(errno));
00123 return -1;
00124 }
00125
00126 return ftruncate(fd, cur);
00127 }
00128
00129 static off_t ilbc_tell(struct ast_filestream *fs)
00130 {
00131 off_t offset = ftello(fs->f);
00132 return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES;
00133 }
00134
00135 static const struct ast_format ilbc_f = {
00136 .name = "iLBC",
00137 .exts = "ilbc",
00138 .format = AST_FORMAT_ILBC,
00139 .write = ilbc_write,
00140 .seek = ilbc_seek,
00141 .trunc = ilbc_trunc,
00142 .tell = ilbc_tell,
00143 .read = ilbc_read,
00144 .buf_size = ILBC_BUF_SIZE + AST_FRIENDLY_OFFSET,
00145 };
00146
00147 static int load_module(void)
00148 {
00149 if (ast_format_register(&ilbc_f))
00150 return AST_MODULE_LOAD_FAILURE;
00151 return AST_MODULE_LOAD_SUCCESS;
00152 }
00153
00154 static int unload_module(void)
00155 {
00156 return ast_format_unregister(ilbc_f.name);
00157 }
00158
00159 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw iLBC data",
00160 .load = load_module,
00161 .unload = unload_module,
00162 .load_pri = AST_MODPRI_APP_DEPEND
00163 );