#include "asterisk.h"
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/sched.h"
#include "asterisk/module.h"
#include "asterisk/endian.h"

Go to the source code of this file.
Data Structures | |
| struct | wav_desc |
Defines | |
| #define | BLOCKSIZE 160 |
| #define | GAIN 0 |
| #define | htoll(b) (b) |
| #define | htols(b) (b) |
| #define | ltohl(b) (b) |
| #define | ltohs(b) (b) |
| #define | WAV_BUF_SIZE 320 |
Functions | |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static int | check_header (FILE *f) |
| static int | load_module (void) |
| static int | unload_module (void) |
| static int | update_header (FILE *f) |
| static void | wav_close (struct ast_filestream *s) |
| static int | wav_open (struct ast_filestream *s) |
| static struct ast_frame * | wav_read (struct ast_filestream *s, int *whennext) |
| static int | wav_rewrite (struct ast_filestream *s, const char *comment) |
| static int | wav_seek (struct ast_filestream *fs, off_t sample_offset, int whence) |
| static off_t | wav_tell (struct ast_filestream *fs) |
| static int | wav_trunc (struct ast_filestream *fs) |
| static int | wav_write (struct ast_filestream *fs, struct ast_frame *f) |
| static int | write_header (FILE *f) |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Microsoft WAV format (8000Hz Signed Linear)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } |
| static const struct ast_module_info * | ast_module_info = &__mod_info |
| static struct ast_format | wav_f |
Definition in file format_wav.c.
| #define BLOCKSIZE 160 |
Definition at line 61 of file format_wav.c.
| #define GAIN 0 |
Definition at line 63 of file format_wav.c.
| #define htoll | ( | b | ) | (b) |
Definition at line 67 of file format_wav.c.
| #define htols | ( | b | ) | (b) |
Definition at line 68 of file format_wav.c.
| #define ltohl | ( | b | ) | (b) |
Definition at line 69 of file format_wav.c.
| #define ltohs | ( | b | ) | (b) |
Definition at line 70 of file format_wav.c.
| #define WAV_BUF_SIZE 320 |
| static void __reg_module | ( | void | ) | [static] |
Definition at line 504 of file format_wav.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 504 of file format_wav.c.
| static int check_header | ( | FILE * | f | ) | [static] |
Definition at line 89 of file format_wav.c.
References ast_log(), DEFAULT_SAMPLE_RATE, format, LOG_WARNING, ltohl, ltohs, and type.
Referenced by wav_open().
00090 { 00091 int type, size, formtype; 00092 int fmt, hsize; 00093 short format, chans, bysam, bisam; 00094 int bysec; 00095 int freq; 00096 int data; 00097 if (fread(&type, 1, 4, f) != 4) { 00098 ast_log(LOG_WARNING, "Read failed (type)\n"); 00099 return -1; 00100 } 00101 if (fread(&size, 1, 4, f) != 4) { 00102 ast_log(LOG_WARNING, "Read failed (size)\n"); 00103 return -1; 00104 } 00105 size = ltohl(size); 00106 if (fread(&formtype, 1, 4, f) != 4) { 00107 ast_log(LOG_WARNING, "Read failed (formtype)\n"); 00108 return -1; 00109 } 00110 if (memcmp(&type, "RIFF", 4)) { 00111 ast_log(LOG_WARNING, "Does not begin with RIFF\n"); 00112 return -1; 00113 } 00114 if (memcmp(&formtype, "WAVE", 4)) { 00115 ast_log(LOG_WARNING, "Does not contain WAVE\n"); 00116 return -1; 00117 } 00118 if (fread(&fmt, 1, 4, f) != 4) { 00119 ast_log(LOG_WARNING, "Read failed (fmt)\n"); 00120 return -1; 00121 } 00122 if (memcmp(&fmt, "fmt ", 4)) { 00123 ast_log(LOG_WARNING, "Does not say fmt\n"); 00124 return -1; 00125 } 00126 if (fread(&hsize, 1, 4, f) != 4) { 00127 ast_log(LOG_WARNING, "Read failed (formtype)\n"); 00128 return -1; 00129 } 00130 if (ltohl(hsize) < 16) { 00131 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize)); 00132 return -1; 00133 } 00134 if (fread(&format, 1, 2, f) != 2) { 00135 ast_log(LOG_WARNING, "Read failed (format)\n"); 00136 return -1; 00137 } 00138 if (ltohs(format) != 1) { 00139 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format)); 00140 return -1; 00141 } 00142 if (fread(&chans, 1, 2, f) != 2) { 00143 ast_log(LOG_WARNING, "Read failed (format)\n"); 00144 return -1; 00145 } 00146 if (ltohs(chans) != 1) { 00147 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans)); 00148 return -1; 00149 } 00150 if (fread(&freq, 1, 4, f) != 4) { 00151 ast_log(LOG_WARNING, "Read failed (freq)\n"); 00152 return -1; 00153 } 00154 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) { 00155 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq)); 00156 return -1; 00157 } 00158 /* Ignore the byte frequency */ 00159 if (fread(&bysec, 1, 4, f) != 4) { 00160 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n"); 00161 return -1; 00162 } 00163 /* Check bytes per sample */ 00164 if (fread(&bysam, 1, 2, f) != 2) { 00165 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n"); 00166 return -1; 00167 } 00168 if (ltohs(bysam) != 2) { 00169 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam)); 00170 return -1; 00171 } 00172 if (fread(&bisam, 1, 2, f) != 2) { 00173 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam)); 00174 return -1; 00175 } 00176 /* Skip any additional header */ 00177 if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) { 00178 ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 ); 00179 return -1; 00180 } 00181 /* Skip any facts and get the first data block */ 00182 for(;;) 00183 { 00184 char buf[4]; 00185 00186 /* Begin data chunk */ 00187 if (fread(&buf, 1, 4, f) != 4) { 00188 ast_log(LOG_WARNING, "Read failed (data)\n"); 00189 return -1; 00190 } 00191 /* Data has the actual length of data in it */ 00192 if (fread(&data, 1, 4, f) != 4) { 00193 ast_log(LOG_WARNING, "Read failed (data)\n"); 00194 return -1; 00195 } 00196 data = ltohl(data); 00197 if(memcmp(buf, "data", 4) == 0 ) 00198 break; 00199 if(memcmp(buf, "fact", 4) != 0 ) { 00200 ast_log(LOG_WARNING, "Unknown block - not fact or data\n"); 00201 return -1; 00202 } 00203 if (fseek(f,data,SEEK_CUR) == -1 ) { 00204 ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data ); 00205 return -1; 00206 } 00207 } 00208 #if 0 00209 curpos = lseek(fd, 0, SEEK_CUR); 00210 truelength = lseek(fd, 0, SEEK_END); 00211 lseek(fd, curpos, SEEK_SET); 00212 truelength -= curpos; 00213 #endif 00214 return data; 00215 }
| static int load_module | ( | void | ) | [static] |
Definition at line 494 of file format_wav.c.
References ast_format_register.
00495 { 00496 return ast_format_register(&wav_f); 00497 }
| static int unload_module | ( | void | ) | [static] |
Definition at line 499 of file format_wav.c.
References ast_format_unregister(), and ast_format::name.
00500 { 00501 return ast_format_unregister(wav_f.name); 00502 }
| static int update_header | ( | FILE * | f | ) | [static] |
Definition at line 217 of file format_wav.c.
References ast_log(), htoll, and LOG_WARNING.
Referenced by wav_trunc(), and wav_write().
00218 { 00219 off_t cur,end; 00220 int datalen,filelen,bytes; 00221 00222 cur = ftello(f); 00223 fseek(f, 0, SEEK_END); 00224 end = ftello(f); 00225 /* data starts 44 bytes in */ 00226 bytes = end - 44; 00227 datalen = htoll(bytes); 00228 /* chunk size is bytes of data plus 36 bytes of header */ 00229 filelen = htoll(36 + bytes); 00230 00231 if (cur < 0) { 00232 ast_log(LOG_WARNING, "Unable to find our position\n"); 00233 return -1; 00234 } 00235 if (fseek(f, 4, SEEK_SET)) { 00236 ast_log(LOG_WARNING, "Unable to set our position\n"); 00237 return -1; 00238 } 00239 if (fwrite(&filelen, 1, 4, f) != 4) { 00240 ast_log(LOG_WARNING, "Unable to set write file size\n"); 00241 return -1; 00242 } 00243 if (fseek(f, 40, SEEK_SET)) { 00244 ast_log(LOG_WARNING, "Unable to set our position\n"); 00245 return -1; 00246 } 00247 if (fwrite(&datalen, 1, 4, f) != 4) { 00248 ast_log(LOG_WARNING, "Unable to set write datalen\n"); 00249 return -1; 00250 } 00251 if (fseeko(f, cur, SEEK_SET)) { 00252 ast_log(LOG_WARNING, "Unable to return to position\n"); 00253 return -1; 00254 } 00255 return 0; 00256 }
| static void wav_close | ( | struct ast_filestream * | s | ) | [static] |
Definition at line 343 of file format_wav.c.
References ast_filestream::_private, ast_log(), wav_desc::bytes, errno, ast_filestream::f, and LOG_WARNING.
00344 { 00345 char zero = 0; 00346 struct wav_desc *fs = (struct wav_desc *)s->_private; 00347 /* Pad to even length */ 00348 if (fs->bytes & 0x1) { 00349 if (!fwrite(&zero, 1, 1, s->f)) { 00350 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno)); 00351 } 00352 } 00353 }
| static int wav_open | ( | struct ast_filestream * | s | ) | [static] |
Definition at line 321 of file format_wav.c.
References ast_filestream::_private, check_header(), ast_filestream::f, and wav_desc::maxlen.
00322 { 00323 /* We don't have any header to read or anything really, but 00324 if we did, it would go here. We also might want to check 00325 and be sure it's a valid file. */ 00326 struct wav_desc *tmp = (struct wav_desc *)s->_private; 00327 if ((tmp->maxlen = check_header(s->f)) < 0) 00328 return -1; 00329 return 0; 00330 }
| static struct ast_frame* wav_read | ( | struct ast_filestream * | s, | |
| int * | whennext | |||
| ) | [static, read] |
Definition at line 355 of file format_wav.c.
References ast_filestream::_private, AST_FORMAT_SLINEAR, AST_FRAME_SET_BUFFER, AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), ast_filestream::buf, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_filestream::fr, ast_frame::frametype, LOG_WARNING, ast_frame::mallocd, wav_desc::maxlen, ast_frame::samples, ast_frame::subclass, and WAV_BUF_SIZE.
00356 { 00357 int res; 00358 int samples; /* actual samples read */ 00359 short *tmp; 00360 int bytes = WAV_BUF_SIZE; /* in bytes */ 00361 off_t here; 00362 /* Send a frame from the file to the appropriate channel */ 00363 struct wav_desc *fs = (struct wav_desc *)s->_private; 00364 00365 here = ftello(s->f); 00366 if (fs->maxlen - here < bytes) /* truncate if necessary */ 00367 bytes = fs->maxlen - here; 00368 if (bytes < 0) 00369 bytes = 0; 00370 /* ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */ 00371 s->fr.frametype = AST_FRAME_VOICE; 00372 s->fr.subclass = AST_FORMAT_SLINEAR; 00373 s->fr.mallocd = 0; 00374 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes); 00375 00376 if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) { 00377 if (res) 00378 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); 00379 return NULL; 00380 } 00381 s->fr.datalen = res; 00382 s->fr.samples = samples = res / 2; 00383 00384 tmp = (short *)(s->fr.data); 00385 #if __BYTE_ORDER == __BIG_ENDIAN 00386 /* file format is little endian so we need to swap */ 00387 for( x = 0; x < samples; x++) 00388 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8); 00389 #endif 00390 *whennext = samples; 00391 return &s->fr; 00392 }
| static int wav_rewrite | ( | struct ast_filestream * | s, | |
| const char * | comment | |||
| ) | [static] |
Definition at line 332 of file format_wav.c.
References ast_filestream::f, and write_header().
00333 { 00334 /* We don't have any header to read or anything really, but 00335 if we did, it would go here. We also might want to check 00336 and be sure it's a valid file. */ 00337 00338 if (write_header(s->f)) 00339 return -1; 00340 return 0; 00341 }
| static int wav_seek | ( | struct ast_filestream * | fs, | |
| off_t | sample_offset, | |||
| int | whence | |||
| ) | [static] |
Definition at line 440 of file format_wav.c.
References ast_filestream::f, offset, and SEEK_FORCECUR.
00441 { 00442 off_t min, max, cur, offset = 0, samples; 00443 00444 samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */ 00445 min = 44; /* wav header is 44 bytes */ 00446 cur = ftello(fs->f); 00447 fseeko(fs->f, 0, SEEK_END); 00448 max = ftello(fs->f); 00449 if (whence == SEEK_SET) 00450 offset = samples + min; 00451 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) 00452 offset = samples + cur; 00453 else if (whence == SEEK_END) 00454 offset = max - samples; 00455 if (whence != SEEK_FORCECUR) { 00456 offset = (offset > max)?max:offset; 00457 } 00458 /* always protect the header space. */ 00459 offset = (offset < min)?min:offset; 00460 return fseeko(fs->f, offset, SEEK_SET); 00461 }
| static off_t wav_tell | ( | struct ast_filestream * | fs | ) | [static] |
Definition at line 470 of file format_wav.c.
References ast_filestream::f, and offset.
00471 { 00472 off_t offset; 00473 offset = ftello(fs->f); 00474 /* subtract header size to get samples, then divide by 2 for 16 bit samples */ 00475 return (offset - 44)/2; 00476 }
| static int wav_trunc | ( | struct ast_filestream * | fs | ) | [static] |
Definition at line 463 of file format_wav.c.
References ast_filestream::f, and update_header().
00464 { 00465 if (ftruncate(fileno(fs->f), ftello(fs->f))) 00466 return -1; 00467 return update_header(fs->f); 00468 }
| static int wav_write | ( | struct ast_filestream * | fs, | |
| struct ast_frame * | f | |||
| ) | [static] |
Definition at line 394 of file format_wav.c.
References ast_filestream::_private, AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), wav_desc::bytes, ast_frame::data, ast_frame::datalen, errno, ast_filestream::f, ast_frame::frametype, LOG_WARNING, s, ast_frame::subclass, and update_header().
00395 { 00396 int x; 00397 short tmp[8000], *tmpi; 00398 struct wav_desc *s = (struct wav_desc *)fs->_private; 00399 int res; 00400 00401 if (f->frametype != AST_FRAME_VOICE) { 00402 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); 00403 return -1; 00404 } 00405 if (f->subclass != AST_FORMAT_SLINEAR) { 00406 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass); 00407 return -1; 00408 } 00409 if (f->datalen > sizeof(tmp)) { 00410 ast_log(LOG_WARNING, "Data length is too long\n"); 00411 return -1; 00412 } 00413 if (!f->datalen) 00414 return -1; 00415 00416 #if 0 00417 printf("Data Length: %d\n", f->datalen); 00418 #endif 00419 00420 tmpi = f->data; 00421 /* Volume adjust here to accomodate */ 00422 for (x=0;x<f->datalen/2;x++) { 00423 tmp[x] = tmpi[x]; 00424 #if __BYTE_ORDER == __BIG_ENDIAN 00425 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8); 00426 #endif 00427 } 00428 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) { 00429 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno)); 00430 return -1; 00431 } 00432 00433 s->bytes += f->datalen; 00434 update_header(fs->f); 00435 00436 return 0; 00437 00438 }
| static int write_header | ( | FILE * | f | ) | [static] |
Definition at line 258 of file format_wav.c.
References ast_log(), htoll, htols, and LOG_WARNING.
Referenced by wav_rewrite().
00259 { 00260 unsigned int hz=htoll(8000); 00261 unsigned int bhz = htoll(16000); 00262 unsigned int hs = htoll(16); 00263 unsigned short fmt = htols(1); 00264 unsigned short chans = htols(1); 00265 unsigned short bysam = htols(2); 00266 unsigned short bisam = htols(16); 00267 unsigned int size = htoll(0); 00268 /* Write a wav header, ignoring sizes which will be filled in later */ 00269 fseek(f,0,SEEK_SET); 00270 if (fwrite("RIFF", 1, 4, f) != 4) { 00271 ast_log(LOG_WARNING, "Unable to write header\n"); 00272 return -1; 00273 } 00274 if (fwrite(&size, 1, 4, f) != 4) { 00275 ast_log(LOG_WARNING, "Unable to write header\n"); 00276 return -1; 00277 } 00278 if (fwrite("WAVEfmt ", 1, 8, f) != 8) { 00279 ast_log(LOG_WARNING, "Unable to write header\n"); 00280 return -1; 00281 } 00282 if (fwrite(&hs, 1, 4, f) != 4) { 00283 ast_log(LOG_WARNING, "Unable to write header\n"); 00284 return -1; 00285 } 00286 if (fwrite(&fmt, 1, 2, f) != 2) { 00287 ast_log(LOG_WARNING, "Unable to write header\n"); 00288 return -1; 00289 } 00290 if (fwrite(&chans, 1, 2, f) != 2) { 00291 ast_log(LOG_WARNING, "Unable to write header\n"); 00292 return -1; 00293 } 00294 if (fwrite(&hz, 1, 4, f) != 4) { 00295 ast_log(LOG_WARNING, "Unable to write header\n"); 00296 return -1; 00297 } 00298 if (fwrite(&bhz, 1, 4, f) != 4) { 00299 ast_log(LOG_WARNING, "Unable to write header\n"); 00300 return -1; 00301 } 00302 if (fwrite(&bysam, 1, 2, f) != 2) { 00303 ast_log(LOG_WARNING, "Unable to write header\n"); 00304 return -1; 00305 } 00306 if (fwrite(&bisam, 1, 2, f) != 2) { 00307 ast_log(LOG_WARNING, "Unable to write header\n"); 00308 return -1; 00309 } 00310 if (fwrite("data", 1, 4, f) != 4) { 00311 ast_log(LOG_WARNING, "Unable to write header\n"); 00312 return -1; 00313 } 00314 if (fwrite(&size, 1, 4, f) != 4) { 00315 ast_log(LOG_WARNING, "Unable to write header\n"); 00316 return -1; 00317 } 00318 return 0; 00319 }
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .description = "Microsoft WAV format (8000Hz Signed Linear)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static] |
Definition at line 504 of file format_wav.c.
const struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 504 of file format_wav.c.
struct ast_format wav_f [static] |
Definition at line 478 of file format_wav.c.
1.5.9