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: 328209 $")
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
00115 if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0)
00116 return -1;
00117 return 0;
00118 }
00119
00120 static off_t ilbc_tell(struct ast_filestream *fs)
00121 {
00122 off_t offset = ftello(fs->f);
00123 return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES;
00124 }
00125
00126 static const struct ast_format ilbc_f = {
00127 .name = "iLBC",
00128 .exts = "ilbc",
00129 .format = AST_FORMAT_ILBC,
00130 .write = ilbc_write,
00131 .seek = ilbc_seek,
00132 .trunc = ilbc_trunc,
00133 .tell = ilbc_tell,
00134 .read = ilbc_read,
00135 .buf_size = ILBC_BUF_SIZE + AST_FRIENDLY_OFFSET,
00136 };
00137
00138 static int load_module(void)
00139 {
00140 if (ast_format_register(&ilbc_f))
00141 return AST_MODULE_LOAD_FAILURE;
00142 return AST_MODULE_LOAD_SUCCESS;
00143 }
00144
00145 static int unload_module(void)
00146 {
00147 return ast_format_unregister(ilbc_f.name);
00148 }
00149
00150 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw iLBC data",
00151 .load = load_module,
00152 .unload = unload_module,
00153 .load_pri = AST_MODPRI_APP_DEPEND
00154 );