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 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 186843 $")
00030
00031 #include "asterisk/mod_format.h"
00032 #include "asterisk/module.h"
00033 #include "asterisk/endian.h"
00034
00035
00036
00037
00038
00039 #define WAV_BUF_SIZE 320
00040
00041 struct wav_desc {
00042 int bytes;
00043 int lasttimeout;
00044 int maxlen;
00045 struct timeval last;
00046 };
00047
00048 #define BLOCKSIZE 160
00049
00050 #if __BYTE_ORDER == __LITTLE_ENDIAN
00051 #define htoll(b) (b)
00052 #define htols(b) (b)
00053 #define ltohl(b) (b)
00054 #define ltohs(b) (b)
00055 #else
00056 #if __BYTE_ORDER == __BIG_ENDIAN
00057 #define htoll(b) \
00058 (((((b) ) & 0xFF) << 24) | \
00059 ((((b) >> 8) & 0xFF) << 16) | \
00060 ((((b) >> 16) & 0xFF) << 8) | \
00061 ((((b) >> 24) & 0xFF) ))
00062 #define htols(b) \
00063 (((((b) ) & 0xFF) << 8) | \
00064 ((((b) >> 8) & 0xFF) ))
00065 #define ltohl(b) htoll(b)
00066 #define ltohs(b) htols(b)
00067 #else
00068 #error "Endianess not defined"
00069 #endif
00070 #endif
00071
00072
00073 static int check_header(FILE *f)
00074 {
00075 int type, size, formtype;
00076 int fmt, hsize;
00077 short format, chans, bysam, bisam;
00078 int bysec;
00079 int freq;
00080 int data;
00081 if (fread(&type, 1, 4, f) != 4) {
00082 ast_log(LOG_WARNING, "Read failed (type)\n");
00083 return -1;
00084 }
00085 if (fread(&size, 1, 4, f) != 4) {
00086 ast_log(LOG_WARNING, "Read failed (size)\n");
00087 return -1;
00088 }
00089 size = ltohl(size);
00090 if (fread(&formtype, 1, 4, f) != 4) {
00091 ast_log(LOG_WARNING, "Read failed (formtype)\n");
00092 return -1;
00093 }
00094 if (memcmp(&type, "RIFF", 4)) {
00095 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
00096 return -1;
00097 }
00098 if (memcmp(&formtype, "WAVE", 4)) {
00099 ast_log(LOG_WARNING, "Does not contain WAVE\n");
00100 return -1;
00101 }
00102 if (fread(&fmt, 1, 4, f) != 4) {
00103 ast_log(LOG_WARNING, "Read failed (fmt)\n");
00104 return -1;
00105 }
00106 if (memcmp(&fmt, "fmt ", 4)) {
00107 ast_log(LOG_WARNING, "Does not say fmt\n");
00108 return -1;
00109 }
00110 if (fread(&hsize, 1, 4, f) != 4) {
00111 ast_log(LOG_WARNING, "Read failed (formtype)\n");
00112 return -1;
00113 }
00114 if (ltohl(hsize) < 16) {
00115 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
00116 return -1;
00117 }
00118 if (fread(&format, 1, 2, f) != 2) {
00119 ast_log(LOG_WARNING, "Read failed (format)\n");
00120 return -1;
00121 }
00122 if (ltohs(format) != 1) {
00123 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
00124 return -1;
00125 }
00126 if (fread(&chans, 1, 2, f) != 2) {
00127 ast_log(LOG_WARNING, "Read failed (format)\n");
00128 return -1;
00129 }
00130 if (ltohs(chans) != 1) {
00131 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
00132 return -1;
00133 }
00134 if (fread(&freq, 1, 4, f) != 4) {
00135 ast_log(LOG_WARNING, "Read failed (freq)\n");
00136 return -1;
00137 }
00138 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
00139 ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
00140 return -1;
00141 }
00142
00143 if (fread(&bysec, 1, 4, f) != 4) {
00144 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
00145 return -1;
00146 }
00147
00148 if (fread(&bysam, 1, 2, f) != 2) {
00149 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
00150 return -1;
00151 }
00152 if (ltohs(bysam) != 2) {
00153 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
00154 return -1;
00155 }
00156 if (fread(&bisam, 1, 2, f) != 2) {
00157 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
00158 return -1;
00159 }
00160
00161 if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
00162 ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
00163 return -1;
00164 }
00165
00166 for(;;)
00167 {
00168 char buf[4];
00169
00170
00171 if (fread(&buf, 1, 4, f) != 4) {
00172 ast_log(LOG_WARNING, "Read failed (data)\n");
00173 return -1;
00174 }
00175
00176 if (fread(&data, 1, 4, f) != 4) {
00177 ast_log(LOG_WARNING, "Read failed (data)\n");
00178 return -1;
00179 }
00180 data = ltohl(data);
00181 if(memcmp(buf, "data", 4) == 0 )
00182 break;
00183 if(memcmp(buf, "fact", 4) != 0 ) {
00184 ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
00185 return -1;
00186 }
00187 if (fseek(f,data,SEEK_CUR) == -1 ) {
00188 ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
00189 return -1;
00190 }
00191 }
00192 #if 0
00193 curpos = lseek(fd, 0, SEEK_CUR);
00194 truelength = lseek(fd, 0, SEEK_END);
00195 lseek(fd, curpos, SEEK_SET);
00196 truelength -= curpos;
00197 #endif
00198 return data;
00199 }
00200
00201 static int update_header(FILE *f)
00202 {
00203 off_t cur,end;
00204 int datalen,filelen,bytes;
00205
00206 cur = ftello(f);
00207 fseek(f, 0, SEEK_END);
00208 end = ftello(f);
00209
00210 bytes = end - 44;
00211 datalen = htoll(bytes);
00212
00213 filelen = htoll(36 + bytes);
00214
00215 if (cur < 0) {
00216 ast_log(LOG_WARNING, "Unable to find our position\n");
00217 return -1;
00218 }
00219 if (fseek(f, 4, SEEK_SET)) {
00220 ast_log(LOG_WARNING, "Unable to set our position\n");
00221 return -1;
00222 }
00223 if (fwrite(&filelen, 1, 4, f) != 4) {
00224 ast_log(LOG_WARNING, "Unable to set write file size\n");
00225 return -1;
00226 }
00227 if (fseek(f, 40, SEEK_SET)) {
00228 ast_log(LOG_WARNING, "Unable to set our position\n");
00229 return -1;
00230 }
00231 if (fwrite(&datalen, 1, 4, f) != 4) {
00232 ast_log(LOG_WARNING, "Unable to set write datalen\n");
00233 return -1;
00234 }
00235 if (fseeko(f, cur, SEEK_SET)) {
00236 ast_log(LOG_WARNING, "Unable to return to position\n");
00237 return -1;
00238 }
00239 return 0;
00240 }
00241
00242 static int write_header(FILE *f)
00243 {
00244 unsigned int hz=htoll(8000);
00245 unsigned int bhz = htoll(16000);
00246 unsigned int hs = htoll(16);
00247 unsigned short fmt = htols(1);
00248 unsigned short chans = htols(1);
00249 unsigned short bysam = htols(2);
00250 unsigned short bisam = htols(16);
00251 unsigned int size = htoll(0);
00252
00253 fseek(f,0,SEEK_SET);
00254 if (fwrite("RIFF", 1, 4, f) != 4) {
00255 ast_log(LOG_WARNING, "Unable to write header\n");
00256 return -1;
00257 }
00258 if (fwrite(&size, 1, 4, f) != 4) {
00259 ast_log(LOG_WARNING, "Unable to write header\n");
00260 return -1;
00261 }
00262 if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
00263 ast_log(LOG_WARNING, "Unable to write header\n");
00264 return -1;
00265 }
00266 if (fwrite(&hs, 1, 4, f) != 4) {
00267 ast_log(LOG_WARNING, "Unable to write header\n");
00268 return -1;
00269 }
00270 if (fwrite(&fmt, 1, 2, f) != 2) {
00271 ast_log(LOG_WARNING, "Unable to write header\n");
00272 return -1;
00273 }
00274 if (fwrite(&chans, 1, 2, f) != 2) {
00275 ast_log(LOG_WARNING, "Unable to write header\n");
00276 return -1;
00277 }
00278 if (fwrite(&hz, 1, 4, f) != 4) {
00279 ast_log(LOG_WARNING, "Unable to write header\n");
00280 return -1;
00281 }
00282 if (fwrite(&bhz, 1, 4, f) != 4) {
00283 ast_log(LOG_WARNING, "Unable to write header\n");
00284 return -1;
00285 }
00286 if (fwrite(&bysam, 1, 2, f) != 2) {
00287 ast_log(LOG_WARNING, "Unable to write header\n");
00288 return -1;
00289 }
00290 if (fwrite(&bisam, 1, 2, f) != 2) {
00291 ast_log(LOG_WARNING, "Unable to write header\n");
00292 return -1;
00293 }
00294 if (fwrite("data", 1, 4, f) != 4) {
00295 ast_log(LOG_WARNING, "Unable to write header\n");
00296 return -1;
00297 }
00298 if (fwrite(&size, 1, 4, f) != 4) {
00299 ast_log(LOG_WARNING, "Unable to write header\n");
00300 return -1;
00301 }
00302 return 0;
00303 }
00304
00305 static int wav_open(struct ast_filestream *s)
00306 {
00307
00308
00309
00310 struct wav_desc *tmp = (struct wav_desc *)s->_private;
00311 if ((tmp->maxlen = check_header(s->f)) < 0)
00312 return -1;
00313 return 0;
00314 }
00315
00316 static int wav_rewrite(struct ast_filestream *s, const char *comment)
00317 {
00318
00319
00320
00321
00322 if (write_header(s->f))
00323 return -1;
00324 return 0;
00325 }
00326
00327 static void wav_close(struct ast_filestream *s)
00328 {
00329 char zero = 0;
00330 struct wav_desc *fs = (struct wav_desc *)s->_private;
00331
00332 if (fs->bytes & 0x1) {
00333 if (!fwrite(&zero, 1, 1, s->f)) {
00334 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
00335 }
00336 }
00337 }
00338
00339 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
00340 {
00341 int res;
00342 int samples;
00343 #if __BYTE_ORDER == __BIG_ENDIAN
00344 int x;
00345 #endif
00346 short *tmp;
00347 int bytes = WAV_BUF_SIZE;
00348 off_t here;
00349
00350 struct wav_desc *fs = (struct wav_desc *)s->_private;
00351
00352 here = ftello(s->f);
00353 if (fs->maxlen - here < bytes)
00354 bytes = fs->maxlen - here;
00355 if (bytes < 0)
00356 bytes = 0;
00357
00358 s->fr.frametype = AST_FRAME_VOICE;
00359 s->fr.subclass = AST_FORMAT_SLINEAR;
00360 s->fr.mallocd = 0;
00361 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
00362
00363 if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) {
00364 if (res)
00365 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00366 return NULL;
00367 }
00368 s->fr.datalen = res;
00369 s->fr.samples = samples = res / 2;
00370
00371 tmp = (short *)(s->fr.data);
00372 #if __BYTE_ORDER == __BIG_ENDIAN
00373
00374 for( x = 0; x < samples; x++)
00375 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
00376 #endif
00377
00378 *whennext = samples;
00379 return &s->fr;
00380 }
00381
00382 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
00383 {
00384 #if __BYTE_ORDER == __BIG_ENDIAN
00385 int x;
00386 short tmp[8000], *tmpi;
00387 #endif
00388 struct wav_desc *s = (struct wav_desc *)fs->_private;
00389 int res;
00390
00391 if (f->frametype != AST_FRAME_VOICE) {
00392 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00393 return -1;
00394 }
00395 if (f->subclass != AST_FORMAT_SLINEAR) {
00396 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
00397 return -1;
00398 }
00399 if (!f->datalen)
00400 return -1;
00401
00402 #if __BYTE_ORDER == __BIG_ENDIAN
00403
00404 if (f->datalen > sizeof(tmp)) {
00405 ast_log(LOG_WARNING, "Data length is too long\n");
00406 return -1;
00407 }
00408 tmpi = f->data;
00409 for (x=0; x < f->datalen/2; x++)
00410 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
00411
00412 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
00413 #else
00414
00415 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen ) {
00416 #endif
00417 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
00418 return -1;
00419 }
00420
00421 s->bytes += f->datalen;
00422 update_header(fs->f);
00423
00424 return 0;
00425
00426 }
00427
00428 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00429 {
00430 off_t min, max, cur, offset = 0, samples;
00431
00432 samples = sample_offset * 2;
00433 min = 44;
00434 cur = ftello(fs->f);
00435 fseeko(fs->f, 0, SEEK_END);
00436 max = ftello(fs->f);
00437 if (whence == SEEK_SET)
00438 offset = samples + min;
00439 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00440 offset = samples + cur;
00441 else if (whence == SEEK_END)
00442 offset = max - samples;
00443 if (whence != SEEK_FORCECUR) {
00444 offset = (offset > max)?max:offset;
00445 }
00446
00447 offset = (offset < min)?min:offset;
00448 return fseeko(fs->f, offset, SEEK_SET);
00449 }
00450
00451 static int wav_trunc(struct ast_filestream *fs)
00452 {
00453 if (ftruncate(fileno(fs->f), ftello(fs->f)))
00454 return -1;
00455 return update_header(fs->f);
00456 }
00457
00458 static off_t wav_tell(struct ast_filestream *fs)
00459 {
00460 off_t offset;
00461 offset = ftello(fs->f);
00462
00463 return (offset - 44)/2;
00464 }
00465
00466 static const struct ast_format wav_f = {
00467 .name = "wav",
00468 .exts = "wav",
00469 .format = AST_FORMAT_SLINEAR,
00470 .open = wav_open,
00471 .rewrite = wav_rewrite,
00472 .write = wav_write,
00473 .seek = wav_seek,
00474 .trunc = wav_trunc,
00475 .tell = wav_tell,
00476 .read = wav_read,
00477 .close = wav_close,
00478 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
00479 .desc_size = sizeof(struct wav_desc),
00480 };
00481
00482 static int load_module(void)
00483 {
00484 if (ast_format_register(&wav_f))
00485 return AST_MODULE_LOAD_FAILURE;
00486 return AST_MODULE_LOAD_SUCCESS;
00487 }
00488
00489 static int unload_module(void)
00490 {
00491 return ast_format_unregister(wav_f.name);
00492 }
00493
00494 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (8000Hz Signed Linear)");