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: 233693 $")
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 (s->filename) {
00333 update_header(s->f);
00334 }
00335
00336
00337 if (fs->bytes & 0x1) {
00338 if (!fwrite(&zero, 1, 1, s->f)) {
00339 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
00340 }
00341 }
00342 }
00343
00344 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
00345 {
00346 int res;
00347 int samples;
00348 #if __BYTE_ORDER == __BIG_ENDIAN
00349 int x;
00350 #endif
00351 short *tmp;
00352 int bytes = WAV_BUF_SIZE;
00353 off_t here;
00354
00355 struct wav_desc *fs = (struct wav_desc *)s->_private;
00356
00357 here = ftello(s->f);
00358 if (fs->maxlen - here < bytes)
00359 bytes = fs->maxlen - here;
00360 if (bytes < 0)
00361 bytes = 0;
00362
00363 s->fr.frametype = AST_FRAME_VOICE;
00364 s->fr.subclass = AST_FORMAT_SLINEAR;
00365 s->fr.mallocd = 0;
00366 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
00367
00368 if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
00369 if (res)
00370 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
00371 return NULL;
00372 }
00373 s->fr.datalen = res;
00374 s->fr.samples = samples = res / 2;
00375
00376 tmp = (short *)(s->fr.data.ptr);
00377 #if __BYTE_ORDER == __BIG_ENDIAN
00378
00379 for( x = 0; x < samples; x++)
00380 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
00381 #endif
00382
00383 *whennext = samples;
00384 return &s->fr;
00385 }
00386
00387 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
00388 {
00389 #if __BYTE_ORDER == __BIG_ENDIAN
00390 int x;
00391 short tmp[8000], *tmpi;
00392 #endif
00393 struct wav_desc *s = (struct wav_desc *)fs->_private;
00394 int res;
00395
00396 if (f->frametype != AST_FRAME_VOICE) {
00397 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
00398 return -1;
00399 }
00400 if (f->subclass != AST_FORMAT_SLINEAR) {
00401 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
00402 return -1;
00403 }
00404 if (!f->datalen)
00405 return -1;
00406
00407 #if __BYTE_ORDER == __BIG_ENDIAN
00408
00409 if (f->datalen > sizeof(tmp)) {
00410 ast_log(LOG_WARNING, "Data length is too long\n");
00411 return -1;
00412 }
00413 tmpi = f->data.ptr;
00414 for (x=0; x < f->datalen/2; x++)
00415 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
00416
00417 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
00418 #else
00419
00420 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen ) {
00421 #endif
00422 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
00423 return -1;
00424 }
00425
00426 s->bytes += f->datalen;
00427
00428 return 0;
00429
00430 }
00431
00432 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
00433 {
00434 off_t min, max, cur, offset = 0, samples;
00435
00436 samples = sample_offset * 2;
00437 min = 44;
00438 cur = ftello(fs->f);
00439 fseeko(fs->f, 0, SEEK_END);
00440 max = ftello(fs->f);
00441 if (whence == SEEK_SET)
00442 offset = samples + min;
00443 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
00444 offset = samples + cur;
00445 else if (whence == SEEK_END)
00446 offset = max - samples;
00447 if (whence != SEEK_FORCECUR) {
00448 offset = (offset > max)?max:offset;
00449 }
00450
00451 offset = (offset < min)?min:offset;
00452 return fseeko(fs->f, offset, SEEK_SET);
00453 }
00454
00455 static int wav_trunc(struct ast_filestream *fs)
00456 {
00457 if (ftruncate(fileno(fs->f), ftello(fs->f)))
00458 return -1;
00459 return update_header(fs->f);
00460 }
00461
00462 static off_t wav_tell(struct ast_filestream *fs)
00463 {
00464 off_t offset;
00465 offset = ftello(fs->f);
00466
00467 return (offset - 44)/2;
00468 }
00469
00470 static const struct ast_format wav_f = {
00471 .name = "wav",
00472 .exts = "wav",
00473 .format = AST_FORMAT_SLINEAR,
00474 .open = wav_open,
00475 .rewrite = wav_rewrite,
00476 .write = wav_write,
00477 .seek = wav_seek,
00478 .trunc = wav_trunc,
00479 .tell = wav_tell,
00480 .read = wav_read,
00481 .close = wav_close,
00482 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
00483 .desc_size = sizeof(struct wav_desc),
00484 };
00485
00486 static int load_module(void)
00487 {
00488 if (ast_format_register(&wav_f))
00489 return AST_MODULE_LOAD_FAILURE;
00490 return AST_MODULE_LOAD_SUCCESS;
00491 }
00492
00493 static int unload_module(void)
00494 {
00495 return ast_format_unregister(wav_f.name);
00496 }
00497
00498 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV format (8000Hz Signed Linear)",
00499 .load = load_module,
00500 .unload = unload_module,
00501 .load_pri = 10,
00502 );