Convert between MS and non-MS formats

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@915 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2003-04-26 16:06:23 +00:00
parent 20e1b92862
commit b9d6efa20e
2 changed files with 47 additions and 17 deletions

View File

@@ -30,6 +30,8 @@
#else #else
#include <machine/endian.h> #include <machine/endian.h>
#endif #endif
#include "msgsm.h"
/* Some Ideas for this code came from makegsme.c by Jeffery Chilton */ /* Some Ideas for this code came from makegsme.c by Jeffery Chilton */
@@ -45,7 +47,7 @@ struct ast_filestream {
struct ast_frame fr; /* Frame information */ struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */ char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
char empty; /* Empty character */ char empty; /* Empty character */
unsigned char gsm[33]; /* Two Real GSM Frames */ unsigned char gsm[66]; /* Two Real GSM Frames */
int lasttimeout; int lasttimeout;
struct timeval last; struct timeval last;
int adj; int adj;
@@ -233,6 +235,7 @@ static int gsm_play(struct ast_filestream *s)
static int gsm_write(struct ast_filestream *fs, struct ast_frame *f) static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
{ {
int res; int res;
unsigned char gsm[66];
if (f->frametype != AST_FRAME_VOICE) { if (f->frametype != AST_FRAME_VOICE) {
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
return -1; return -1;
@@ -241,20 +244,33 @@ static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass); ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
return -1; return -1;
} }
if (f->datalen % 33) { if (!(f->datalen % 65)) {
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen); /* This is in MSGSM format, need to be converted */
return -1; int len=0;
} while(len < f->datalen) {
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { conv65(f->data + len, gsm);
ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno)); if ((res = write(fs->fd, gsm, 66)) != 66) {
ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
return -1;
}
len += 65;
}
} else {
if (f->datalen % 33) {
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen);
return -1; return -1;
}
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
return -1;
}
} }
return 0; return 0;
} }
static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence) static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
{ {
off_t offset,min,cur,max,distance; off_t offset=0,min,cur,max,distance;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = lseek(fs->fd, 0, SEEK_CUR);

View File

@@ -520,6 +520,7 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
int res; int res;
char msdata[66]; char msdata[66];
int len =0; int len =0;
int alreadyms=0;
if (f->frametype != AST_FRAME_VOICE) { if (f->frametype != AST_FRAME_VOICE) {
ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
return -1; return -1;
@@ -528,29 +529,42 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass); ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
return -1; return -1;
} }
if (!(f->datalen % 65))
alreadyms = 1;
while(len < f->datalen) { while(len < f->datalen) {
if (fs->secondhalf) { if (alreadyms) {
memcpy(fs->gsm + 33, f->data + len, 33); fs->secondhalf = 0;
conv66(fs->gsm, msdata); if ((res = write(fs->fd, f->data + len, 65)) != 65) {
if ((res = write(fs->fd, msdata, 65)) != 65) {
ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
return -1; return -1;
} }
fs->bytes += 65; fs->bytes += 65;
update_header(fs->fd); update_header(fs->fd);
len += 65;
} else { } else {
/* Copy the data and do nothing */ if (fs->secondhalf) {
memcpy(fs->gsm, f->data + len, 33); memcpy(fs->gsm + 33, f->data + len, 33);
conv66(fs->gsm, msdata);
if ((res = write(fs->fd, msdata, 65)) != 65) {
ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
return -1;
}
fs->bytes += 65;
update_header(fs->fd);
} else {
/* Copy the data and do nothing */
memcpy(fs->gsm, f->data + len, 33);
}
fs->secondhalf = !fs->secondhalf;
len += 33;
} }
fs->secondhalf = !fs->secondhalf;
len += 33;
} }
return 0; return 0;
} }
static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence) static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
{ {
off_t offset,distance,cur,min,max; off_t offset=0,distance,cur,min,max;
min = 52; min = 52;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = lseek(fs->fd, 0, SEEK_CUR);
max = lseek(fs->fd, 0, SEEK_END); max = lseek(fs->fd, 0, SEEK_END);