Merged revisions 284701 via svnmerge from

https://origsvn.digium.com/svn/asterisk/branches/1.8

........
  r284701 | qwell | 2010-09-02 11:43:09 -0500 (Thu, 02 Sep 2010) | 8 lines
  
  Add slin16 support for format_wav (new wav16 file extension)
  
  (closes issue #15029)
  Reported by: andrew
  Patches: 
        wav16.patch uploaded by andrew (license 240)
  Tested by: qwell, andrew
........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@284702 65c4cc65-6c06-0410-ace0-fbb531ad65f3
remotes/origin/10-digiumphones
Jason Parker 14 years ago
parent c04536bf2e
commit b5f2e2acef

@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#define WAV_BUF_SIZE 320
struct wav_desc { /* format-specific parameters */
int hz;
int bytes;
int lasttimeout;
int maxlen;
@ -70,7 +71,7 @@ struct wav_desc { /* format-specific parameters */
#endif
static int check_header(FILE *f)
static int check_header(FILE *f, int hz)
{
int type, size, formtype;
int fmt, hsize;
@ -135,8 +136,10 @@ static int check_header(FILE *f)
ast_log(LOG_WARNING, "Read failed (freq)\n");
return -1;
}
if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
if (((ltohl(freq) != 8000) && (ltohl(freq) != 16000)) ||
((ltohl(freq) == 8000) && (hz != 8000)) ||
((ltohl(freq) == 16000) && (hz != 16000))) {
ast_log(LOG_WARNING, "Unexpected frequency mismatch %d (expecting %d)\n", ltohl(freq),hz);
return -1;
}
/* Ignore the byte frequency */
@ -239,16 +242,24 @@ static int update_header(FILE *f)
return 0;
}
static int write_header(FILE *f)
static int write_header(FILE *f, int writehz)
{
unsigned int hz=htoll(8000);
unsigned int bhz = htoll(16000);
unsigned int hz;
unsigned int bhz;
unsigned int hs = htoll(16);
unsigned short fmt = htols(1);
unsigned short chans = htols(1);
unsigned short bysam = htols(2);
unsigned short bisam = htols(16);
unsigned int size = htoll(0);
if (writehz == 16000) {
hz = htoll(16000);
bhz = htoll(32000);
} else {
hz = htoll(8000);
bhz = htoll(16000);
}
/* Write a wav header, ignoring sizes which will be filled in later */
fseek(f,0,SEEK_SET);
if (fwrite("RIFF", 1, 4, f) != 4) {
@ -308,7 +319,7 @@ static int wav_open(struct ast_filestream *s)
if we did, it would go here. We also might want to check
and be sure it's a valid file. */
struct wav_desc *tmp = (struct wav_desc *)s->_private;
if ((tmp->maxlen = check_header(s->f)) < 0)
if ((tmp->maxlen = check_header(s->f, (s->fmt->format == AST_FORMAT_SLINEAR16 ? 16000 : 8000))) < 0)
return -1;
return 0;
}
@ -319,7 +330,9 @@ static int wav_rewrite(struct ast_filestream *s, const char *comment)
if we did, it would go here. We also might want to check
and be sure it's a valid file. */
if (write_header(s->f))
struct wav_desc *tmp = (struct wav_desc *)s->_private;
tmp->hz = (s->fmt->format == AST_FORMAT_SLINEAR16 ? 16000 : 8000);
if (write_header(s->f,tmp->hz))
return -1;
return 0;
}
@ -349,11 +362,13 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
int x;
#endif
short *tmp;
int bytes = WAV_BUF_SIZE; /* in bytes */
int bytes;
off_t here;
/* Send a frame from the file to the appropriate channel */
struct wav_desc *fs = (struct wav_desc *)s->_private;
bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
here = ftello(s->f);
if (fs->maxlen - here < bytes) /* truncate if necessary */
bytes = fs->maxlen - here;
@ -361,7 +376,7 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
bytes = 0;
/* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
s->fr.frametype = AST_FRAME_VOICE;
s->fr.subclass.codec = AST_FORMAT_SLINEAR;
s->fr.subclass.codec = (fs->hz == 16000 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR);
s->fr.mallocd = 0;
AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
@ -388,7 +403,7 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
{
#if __BYTE_ORDER == __BIG_ENDIAN
int x;
short tmp[8000], *tmpi;
short tmp[16000], *tmpi;
#endif
struct wav_desc *s = (struct wav_desc *)fs->_private;
int res;
@ -397,8 +412,12 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
return -1;
}
if (f->subclass.codec != AST_FORMAT_SLINEAR) {
ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%s)!\n", ast_getformatname(f->subclass.codec));
if ((f->subclass.codec != AST_FORMAT_SLINEAR) && (f->subclass.codec != AST_FORMAT_SLINEAR16)) {
ast_log(LOG_WARNING, "Asked to write non-SLINEAR%s frame (%s)!\n", s->hz == 16000 ? "16" : "", ast_getformatname(f->subclass.codec));
return -1;
}
if (f->subclass.codec != fs->fmt->format) {
ast_log(LOG_WARNING, "Can't change SLINEAR frequency during write\n");
return -1;
}
if (!f->datalen)
@ -467,6 +486,22 @@ static off_t wav_tell(struct ast_filestream *fs)
return (offset - 44)/2;
}
static const struct ast_format wav16_f = {
.name = "wav16",
.exts = "wav16",
.format = AST_FORMAT_SLINEAR16,
.open = wav_open,
.rewrite = wav_rewrite,
.write = wav_write,
.seek = wav_seek,
.trunc = wav_trunc,
.tell = wav_tell,
.read = wav_read,
.close = wav_close,
.buf_size = (WAV_BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
.desc_size = sizeof(struct wav_desc),
};
static const struct ast_format wav_f = {
.name = "wav",
.exts = "wav",
@ -485,17 +520,19 @@ static const struct ast_format wav_f = {
static int load_module(void)
{
if (ast_format_register(&wav_f))
if (ast_format_register(&wav_f)
|| ast_format_register(&wav16_f))
return AST_MODULE_LOAD_FAILURE;
return AST_MODULE_LOAD_SUCCESS;
}
static int unload_module(void)
{
return ast_format_unregister(wav_f.name);
return ast_format_unregister(wav_f.name)
|| ast_format_unregister(wav16_f.name);
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV format (8000Hz Signed Linear)",
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_APP_DEPEND

Loading…
Cancel
Save