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 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 233782 $")
00029
00030 #include <unistd.h>
00031 #include <netinet/in.h>
00032 #include <arpa/inet.h>
00033 #include <stdlib.h>
00034 #include <sys/time.h>
00035 #include <stdio.h>
00036 #include <errno.h>
00037 #include <string.h>
00038
00039 #include "asterisk/lock.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/file.h"
00042 #include "asterisk/logger.h"
00043 #include "asterisk/sched.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/endian.h"
00046
00047 #include "msgsm.h"
00048
00049
00050
00051
00052
00053 #define GSM_FRAME_SIZE 33
00054 #define GSM_SAMPLES 160
00055
00056
00057
00058 char gsm_silence[] =
00059 {0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49
00060 ,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24
00061 ,0x92,0x49,0x24};
00062
00063
00064 static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
00065 {
00066 int res;
00067
00068 s->fr.frametype = AST_FRAME_VOICE;
00069 s->fr.subclass = AST_FORMAT_GSM;
00070 AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
00071 s->fr.mallocd = 0;
00072 if ((res = fread(s->fr.data, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
00073 if (res)
00074 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00075 return NULL;
00076 }
00077 *whennext = s->fr.samples = GSM_SAMPLES;
00078 return &s->fr;
00079 }
00080
00081 static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
00082 {
00083 int res;
00084 unsigned char gsm[2*GSM_FRAME_SIZE];
00085
00086 if (f->frametype != AST_FRAME_VOICE) {
00087 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00088 return -1;
00089 }
00090 if (f->subclass != AST_FORMAT_GSM) {
00091 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
00092 return -1;
00093 }
00094 if (!(f->datalen % 65)) {
00095
00096 int len=0;
00097 while(len < f->datalen) {
00098 conv65(f->data + len, gsm);
00099 if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) {
00100 ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
00101 return -1;
00102 }
00103 len += 65;
00104 }
00105 } else {
00106 if (f->datalen % GSM_FRAME_SIZE) {
00107 ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
00108 return -1;
00109 }
00110 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
00111 ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
00112 return -1;
00113 }
00114 }
00115 return 0;
00116 }
00117
00118 static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00119 {
00120 off_t offset=0,min,cur,max,distance;
00121
00122 min = 0;
00123 cur = ftello(fs->f);
00124 fseeko(fs->f, 0, SEEK_END);
00125 max = ftello(fs->f);
00126
00127 distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE;
00128 if(whence == SEEK_SET)
00129 offset = distance;
00130 else if(whence == SEEK_CUR || whence == SEEK_FORCECUR)
00131 offset = distance + cur;
00132 else if(whence == SEEK_END)
00133 offset = max - distance;
00134
00135 offset = (offset < min)?min:offset;
00136 if (whence != SEEK_FORCECUR) {
00137 offset = (offset > max)?max:offset;
00138 } else if (offset > max) {
00139 int i;
00140 fseeko(fs->f, 0, SEEK_END);
00141 for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
00142 if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
00143 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
00144 }
00145 }
00146 }
00147 return fseeko(fs->f, offset, SEEK_SET);
00148 }
00149
00150 static int gsm_trunc(struct ast_filestream *fs)
00151 {
00152 return ftruncate(fileno(fs->f), ftello(fs->f));
00153 }
00154
00155 static off_t gsm_tell(struct ast_filestream *fs)
00156 {
00157 off_t offset = ftello(fs->f);
00158 return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES;
00159 }
00160
00161 static const struct ast_format gsm_f = {
00162 .name = "gsm",
00163 .exts = "gsm",
00164 .format = AST_FORMAT_GSM,
00165 .write = gsm_write,
00166 .seek = gsm_seek,
00167 .trunc = gsm_trunc,
00168 .tell = gsm_tell,
00169 .read = gsm_read,
00170 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET,
00171 };
00172
00173 static int load_module(void)
00174 {
00175 return ast_format_register(&gsm_f);
00176 }
00177
00178 static int unload_module(void)
00179 {
00180 return ast_format_unregister(gsm_f.name);
00181 }
00182
00183 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_FIRST, "Raw GSM data",
00184 .load = load_module,
00185 .unload = unload_module,
00186 );