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 #include "asterisk.h"
00026
00027 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 279472 $")
00028
00029 #include "asterisk/mod_format.h"
00030 #include "asterisk/module.h"
00031 #include "asterisk/endian.h"
00032
00033 #define BUF_SIZE 320
00034 #define SLIN_SAMPLES 160
00035
00036 static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
00037 {
00038 int res;
00039
00040
00041 s->fr.frametype = AST_FRAME_VOICE;
00042 s->fr.subclass.codec = AST_FORMAT_SLINEAR;
00043 s->fr.mallocd = 0;
00044 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
00045 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
00046 if (res)
00047 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00048 return NULL;
00049 }
00050 *whennext = s->fr.samples = res/2;
00051 s->fr.datalen = res;
00052 return &s->fr;
00053 }
00054
00055 static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
00056 {
00057 int res;
00058 if (f->frametype != AST_FRAME_VOICE) {
00059 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00060 return -1;
00061 }
00062 if (f->subclass.codec != AST_FORMAT_SLINEAR) {
00063 ast_log(LOG_WARNING, "Asked to write non-slinear frame (%s)!\n", ast_getformatname(f->subclass.codec));
00064 return -1;
00065 }
00066 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
00067 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
00068 return -1;
00069 }
00070 return 0;
00071 }
00072
00073 static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00074 {
00075 off_t offset=0,min,cur,max;
00076
00077 min = 0;
00078 sample_offset <<= 1;
00079 cur = ftello(fs->f);
00080 fseeko(fs->f, 0, SEEK_END);
00081 max = ftello(fs->f);
00082 if (whence == SEEK_SET)
00083 offset = sample_offset;
00084 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00085 offset = sample_offset + cur;
00086 else if (whence == SEEK_END)
00087 offset = max - sample_offset;
00088 if (whence != SEEK_FORCECUR) {
00089 offset = (offset > max)?max:offset;
00090 }
00091
00092 offset = (offset < min)?min:offset;
00093 return fseeko(fs->f, offset, SEEK_SET);
00094 }
00095
00096 static int slinear_trunc(struct ast_filestream *fs)
00097 {
00098 return ftruncate(fileno(fs->f), ftello(fs->f));
00099 }
00100
00101 static off_t slinear_tell(struct ast_filestream *fs)
00102 {
00103 return ftello(fs->f) / 2;
00104 }
00105
00106 static const struct ast_format slin_f = {
00107 .name = "sln",
00108 .exts = "sln|raw",
00109 .format = AST_FORMAT_SLINEAR,
00110 .write = slinear_write,
00111 .seek = slinear_seek,
00112 .trunc = slinear_trunc,
00113 .tell = slinear_tell,
00114 .read = slinear_read,
00115 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
00116 };
00117
00118 static int load_module(void)
00119 {
00120 if (ast_format_register(&slin_f))
00121 return AST_MODULE_LOAD_FAILURE;
00122 return AST_MODULE_LOAD_SUCCESS;
00123 }
00124
00125 static int unload_module(void)
00126 {
00127 return ast_format_unregister(slin_f.name);
00128 }
00129
00130 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw Signed Linear Audio support (SLN)",
00131 .load = load_module,
00132 .unload = unload_module,
00133 .load_pri = AST_MODPRI_APP_DEPEND
00134 );