Use FILE * instead of fd for files to support buffering

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@6801 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2005-10-16 16:12:51 +00:00
parent d2025ad2a1
commit d499a85f05
17 changed files with 406 additions and 366 deletions

63
file.c
View File

@@ -59,9 +59,9 @@ struct ast_format {
/* Format of frames it uses/provides (one only) */ /* Format of frames it uses/provides (one only) */
int format; int format;
/* Open an input stream, and start playback */ /* Open an input stream, and start playback */
struct ast_filestream * (*open)(int fd); struct ast_filestream * (*open)(FILE * f);
/* Open an output stream, of a given file descriptor and comment it appropriately if applicable */ /* Open an output stream, of a given file descriptor and comment it appropriately if applicable */
struct ast_filestream * (*rewrite)(int fd, const char *comment); struct ast_filestream * (*rewrite)(FILE *f, const char *comment);
/* Write a frame to a channel */ /* Write a frame to a channel */
int (*write)(struct ast_filestream *, struct ast_frame *); int (*write)(struct ast_filestream *, struct ast_frame *);
/* seek num samples into file, whence(think normal seek) */ /* seek num samples into file, whence(think normal seek) */
@@ -103,8 +103,8 @@ AST_MUTEX_DEFINE_STATIC(formatlock);
static struct ast_format *formats = NULL; static struct ast_format *formats = NULL;
int ast_format_register(const char *name, const char *exts, int format, int ast_format_register(const char *name, const char *exts, int format,
struct ast_filestream * (*open)(int fd), struct ast_filestream * (*open)(FILE *f),
struct ast_filestream * (*rewrite)(int fd, const char *comment), struct ast_filestream * (*rewrite)(FILE *f, const char *comment),
int (*write)(struct ast_filestream *, struct ast_frame *), int (*write)(struct ast_filestream *, struct ast_frame *),
int (*seek)(struct ast_filestream *, long sample_offset, int whence), int (*seek)(struct ast_filestream *, long sample_offset, int whence),
int (*trunc)(struct ast_filestream *), int (*trunc)(struct ast_filestream *),
@@ -351,6 +351,7 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
struct ast_filestream *s; struct ast_filestream *s;
int res=0, ret = 0; int res=0, ret = 0;
char *ext=NULL, *exts, *fn, *nfn; char *ext=NULL, *exts, *fn, *nfn;
FILE *bfile;
struct ast_channel *chan = (struct ast_channel *)filename2; struct ast_channel *chan = (struct ast_channel *)filename2;
/* Start with negative response */ /* Start with negative response */
@@ -413,9 +414,10 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
case ACTION_OPEN: case ACTION_OPEN:
if ((ret < 0) && ((chan->writeformat & f->format) || if ((ret < 0) && ((chan->writeformat & f->format) ||
((f->format >= AST_FORMAT_MAX_AUDIO) && fmt))) { ((f->format >= AST_FORMAT_MAX_AUDIO) && fmt))) {
ret = open(fn, O_RDONLY); bfile = fopen(fn, "r");
if (ret >= 0) { if (bfile) {
s = f->open(ret); ret = 1;
s = f->open(bfile);
if (s) { if (s) {
s->lasttimeout = -1; s->lasttimeout = -1;
s->fmt = f; s->fmt = f;
@@ -426,11 +428,14 @@ static int ast_filehelper(const char *filename, const char *filename2, const cha
else else
chan->vstream = s; chan->vstream = s;
} else { } else {
close(ret); fclose(bfile);
ast_log(LOG_WARNING, "Unable to open fd on %s\n", fn); ast_log(LOG_WARNING, "Unable to open file on %s\n", fn);
ret = -1;
} }
} else } else{
ast_log(LOG_WARNING, "Couldn't open file %s\n", fn); ast_log(LOG_WARNING, "Couldn't open file %s\n", fn);
ret = -1;
}
} }
break; break;
default: default:
@@ -472,7 +477,6 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
set it up. set it up.
*/ */
int fd = -1;
int fmts = -1; int fmts = -1;
char filename2[256]=""; char filename2[256]="";
char filename3[256]; char filename3[256];
@@ -508,8 +512,8 @@ struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char
/* Set the channel to a format we can work with */ /* Set the channel to a format we can work with */
res = ast_set_write_format(chan, fmts); res = ast_set_write_format(chan, fmts);
fd = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN); res = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
if (fd >= 0) if (res >= 0)
return chan->stream; return chan->stream;
return NULL; return NULL;
} }
@@ -819,7 +823,7 @@ int ast_streamfile(struct ast_channel *chan, const char *filename, const char *p
struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode) struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
{ {
int fd; FILE *bfile;
struct ast_format *f; struct ast_format *f;
struct ast_filestream *fs = NULL; struct ast_filestream *fs = NULL;
char *fn; char *fn;
@@ -834,13 +838,13 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
continue; continue;
fn = build_filename(filename, type); fn = build_filename(filename, type);
fd = open(fn, flags); bfile = fopen(fn, "r");
if (fd >= 0) { if (bfile) {
errno = 0; errno = 0;
if (!(fs = f->open(fd))) { if (!(fs = f->open(bfile))) {
ast_log(LOG_WARNING, "Unable to open %s\n", fn); ast_log(LOG_WARNING, "Unable to open %s\n", fn);
close(fd); fclose(bfile);
free(fn); free(fn);
continue; continue;
} }
@@ -866,6 +870,7 @@ struct ast_filestream *ast_readfile(const char *filename, const char *type, cons
struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode) struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
{ {
int fd, myflags = 0; int fd, myflags = 0;
FILE *bfile;
struct ast_format *f; struct ast_format *f;
struct ast_filestream *fs = NULL; struct ast_filestream *fs = NULL;
char *fn, *orig_fn = NULL; char *fn, *orig_fn = NULL;
@@ -893,11 +898,20 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
fn = build_filename(filename, type); fn = build_filename(filename, type);
fd = open(fn, flags | myflags, mode); fd = open(fn, flags | myflags, mode);
if (fd > -1) {
/* fdopen() the resulting file stream */
bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
if (!bfile) {
ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
close(fd);
fd = -1;
}
}
if (option_cache_record_files && fd >= 0) { if (option_cache_record_files && fd >= 0) {
char *c; char *c;
close(fd); fclose(bfile);
/* /*
We touch orig_fn just as a place-holder so other things (like vmail) see the file is there. We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place. What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
@@ -914,11 +928,20 @@ struct ast_filestream *ast_writefile(const char *filename, const char *type, con
free(fn); free(fn);
fn = buf; fn = buf;
fd = open(fn, flags | myflags, mode); fd = open(fn, flags | myflags, mode);
if (fd > -1) {
/* fdopen() the resulting file stream */
bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
if (!bfile) {
ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
close(fd);
fd = -1;
}
}
} }
if (fd >= 0) { if (fd >= 0) {
errno = 0; errno = 0;
if ((fs = f->rewrite(fd, comment))) { if ((fs = f->rewrite(bfile, comment))) {
fs->trans = NULL; fs->trans = NULL;
fs->fmt = f; fs->fmt = f;
fs->flags = flags; fs->flags = flags;

View File

@@ -57,7 +57,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct ast_filestream { struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
struct ast_channel *owner; struct ast_channel *owner;
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 */
@@ -98,7 +98,7 @@ static char *exts = "au";
#endif #endif
static int check_header(int fd) static int check_header(FILE *f)
{ {
AU_HEADER(header); AU_HEADER(header);
u_int32_t magic; u_int32_t magic;
@@ -108,7 +108,7 @@ static int check_header(int fd)
u_int32_t sample_rate; u_int32_t sample_rate;
u_int32_t channels; u_int32_t channels;
if (read(fd, header, AU_HEADER_SIZE) != AU_HEADER_SIZE) { if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
ast_log(LOG_WARNING, "Read failed (header)\n"); ast_log(LOG_WARNING, "Read failed (header)\n");
return -1; return -1;
} }
@@ -136,22 +136,24 @@ static int check_header(int fd)
return -1; return -1;
} }
/* Skip to data */ /* Skip to data */
data_size = lseek(fd, 0, SEEK_END) - hdr_size; fseek(f, 0, SEEK_END);
if (lseek(fd, hdr_size, SEEK_SET) == -1 ) { data_size = ftell(f) - hdr_size;
if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size); ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
return -1; return -1;
} }
return data_size; return data_size;
} }
static int update_header(int fd) static int update_header(FILE *f)
{ {
off_t cur, end; off_t cur, end;
u_int32_t datalen; u_int32_t datalen;
int bytes; int bytes;
cur = lseek(fd, 0, SEEK_CUR); cur = ftell(f);
end = lseek(fd, 0, SEEK_END); fseek(f, 0, SEEK_END);
end = ftell(f);
/* data starts 24 bytes in */ /* data starts 24 bytes in */
bytes = end - AU_HEADER_SIZE; bytes = end - AU_HEADER_SIZE;
datalen = htoll(bytes); datalen = htoll(bytes);
@@ -160,22 +162,22 @@ static int update_header(int fd)
ast_log(LOG_WARNING, "Unable to find our position\n"); ast_log(LOG_WARNING, "Unable to find our position\n");
return -1; return -1;
} }
if (lseek(fd, AU_HDR_DATA_SIZE_OFF * sizeof(u_int32_t), SEEK_SET) != (AU_HDR_DATA_SIZE_OFF * sizeof(u_int32_t))) { if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(u_int32_t), SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to set our position\n"); ast_log(LOG_WARNING, "Unable to set our position\n");
return -1; return -1;
} }
if (write(fd, &datalen, sizeof(datalen)) != sizeof(datalen)) { if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
ast_log(LOG_WARNING, "Unable to set write file size\n"); ast_log(LOG_WARNING, "Unable to set write file size\n");
return -1; return -1;
} }
if (lseek(fd, cur, SEEK_SET) != cur) { if (fseek(f, cur, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to return to position\n"); ast_log(LOG_WARNING, "Unable to return to position\n");
return -1; return -1;
} }
return 0; return 0;
} }
static int write_header(int fd) static int write_header(FILE *f)
{ {
AU_HEADER(header); AU_HEADER(header);
@@ -187,15 +189,15 @@ static int write_header(int fd)
header[AU_HDR_CHANNELS_OFF] = htoll(1); header[AU_HDR_CHANNELS_OFF] = htoll(1);
/* Write an au header, ignoring sizes which will be filled in later */ /* Write an au header, ignoring sizes which will be filled in later */
lseek(fd, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
if (write(fd, header, AU_HEADER_SIZE) != AU_HEADER_SIZE) { if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
return 0; return 0;
} }
static struct ast_filestream *au_open(int fd) static struct ast_filestream *au_open(FILE *f)
{ {
struct ast_filestream *tmp; struct ast_filestream *tmp;
@@ -205,7 +207,7 @@ static struct ast_filestream *au_open(int fd)
} }
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
if (check_header(fd) < 0) { if (check_header(f) < 0) {
free(tmp); free(tmp);
return NULL; return NULL;
} }
@@ -214,7 +216,7 @@ static struct ast_filestream *au_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->buf; tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ULAW; tmp->fr.subclass = AST_FORMAT_ULAW;
@@ -227,7 +229,7 @@ static struct ast_filestream *au_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *au_rewrite(int fd, const char *comment) static struct ast_filestream *au_rewrite(FILE *f, const char *comment)
{ {
struct ast_filestream *tmp; struct ast_filestream *tmp;
@@ -237,7 +239,7 @@ static struct ast_filestream *au_rewrite(int fd, const char *comment)
} }
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
if (write_header(fd)) { if (write_header(f)) {
free(tmp); free(tmp);
return NULL; return NULL;
} }
@@ -246,7 +248,7 @@ static struct ast_filestream *au_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
localusecnt++; localusecnt++;
ast_mutex_unlock(&au_lock); ast_mutex_unlock(&au_lock);
ast_update_use_count(); ast_update_use_count();
@@ -262,7 +264,7 @@ static void au_close(struct ast_filestream *s)
localusecnt--; localusecnt--;
ast_mutex_unlock(&au_lock); ast_mutex_unlock(&au_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
} }
@@ -277,7 +279,7 @@ static struct ast_frame *au_read(struct ast_filestream *s, int *whennext)
s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->buf; s->fr.data = s->buf;
if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) { if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -301,11 +303,11 @@ static int au_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass); ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
return -1; return -1;
} }
update_header(fs->fd); update_header(fs->f);
return 0; return 0;
} }
@@ -316,8 +318,9 @@ static int au_seek(struct ast_filestream *fs, long sample_offset, int whence)
samples = sample_offset; samples = sample_offset;
min = AU_HEADER_SIZE; min = AU_HEADER_SIZE;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
if (whence == SEEK_SET) if (whence == SEEK_SET)
offset = samples + min; offset = samples + min;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
@@ -329,21 +332,21 @@ static int au_seek(struct ast_filestream *fs, long sample_offset, int whence)
} }
/* always protect the header space. */ /* always protect the header space. */
offset = (offset < min) ? min : offset; offset = (offset < min) ? min : offset;
return lseek(fs->fd, offset, SEEK_SET); return fseek(fs->f, offset, SEEK_SET);
} }
static int au_trunc(struct ast_filestream *fs) static int au_trunc(struct ast_filestream *fs)
{ {
if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR))) if (ftruncate(fileno(fs->f), ftell(fs->f)))
return -1; return -1;
return update_header(fs->fd); return update_header(fs->f);
} }
static long au_tell(struct ast_filestream *fs) static long au_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return offset - AU_HEADER_SIZE; return offset - AU_HEADER_SIZE;
} }

View File

@@ -51,7 +51,7 @@ struct ast_filestream {
/* First entry MUST be reserved for the channel type */ /* First entry MUST be reserved for the channel type */
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
struct ast_filestream *next; struct ast_filestream *next;
struct ast_frame *fr; /* Frame representation of buf */ struct ast_frame *fr; /* Frame representation of buf */
struct timeval orig; /* Original frame time */ struct timeval orig; /* Original frame time */
@@ -66,7 +66,7 @@ static char *name = "g723sf";
static char *desc = "G.723.1 Simple Timestamp File Format"; static char *desc = "G.723.1 Simple Timestamp File Format";
static char *exts = "g723|g723sf"; static char *exts = "g723|g723sf";
static struct ast_filestream *g723_open(int fd) static struct ast_filestream *g723_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -79,7 +79,7 @@ static struct ast_filestream *g723_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr = (struct ast_frame *)tmp->buf; tmp->fr = (struct ast_frame *)tmp->buf;
tmp->fr->data = tmp->buf + sizeof(struct ast_frame); tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
tmp->fr->frametype = AST_FRAME_VOICE; tmp->fr->frametype = AST_FRAME_VOICE;
@@ -94,7 +94,7 @@ static struct ast_filestream *g723_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *g723_rewrite(int fd, const char *comment) static struct ast_filestream *g723_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -107,7 +107,7 @@ static struct ast_filestream *g723_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&g723_lock); ast_mutex_unlock(&g723_lock);
ast_update_use_count(); ast_update_use_count();
@@ -122,11 +122,11 @@ static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
int res; int res;
int delay; int delay;
/* Read the delay for the next packet, and schedule again if necessary */ /* Read the delay for the next packet, and schedule again if necessary */
if (read(s->fd, &delay, 4) == 4) if (fread(&delay, 1, 4, s->f) == 4)
delay = ntohl(delay); delay = ntohl(delay);
else else
delay = -1; delay = -1;
if (read(s->fd, &size, 2) != 2) { if (fread(&size, 1, 2, s->f) != 2) {
/* Out of data, or the file is no longer valid. In any case /* Out of data, or the file is no longer valid. In any case
go ahead and stop the stream */ go ahead and stop the stream */
return NULL; return NULL;
@@ -144,7 +144,7 @@ static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
s->fr->offset = AST_FRIENDLY_OFFSET; s->fr->offset = AST_FRIENDLY_OFFSET;
s->fr->datalen = size; s->fr->datalen = size;
s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET; s->fr->data = s->buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
if ((res = read(s->fd, s->fr->data , size)) != size) { if ((res = fread(s->fr->data, 1, size, s->f)) != size) {
ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
return NULL; return NULL;
} }
@@ -170,7 +170,7 @@ static void g723_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&g723_lock); ast_mutex_unlock(&g723_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -198,16 +198,16 @@ static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen); ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
return 0; return 0;
} }
if ((res = write(fs->fd, &delay, 4)) != 4) { if ((res = fwrite(&delay, 1, 4, fs->f)) != 4) {
ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno)); ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno));
return -1; return -1;
} }
size = htons(f->datalen); size = htons(f->datalen);
if ((res =write(fs->fd, &size, 2)) != 2) { if ((res = fwrite(&size, 1, 2, fs->f)) != 2) {
ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno)); ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno));
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno)); ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno));
return -1; return -1;
} }
@@ -222,7 +222,7 @@ static int g723_seek(struct ast_filestream *fs, long sample_offset, int whence)
static int g723_trunc(struct ast_filestream *fs) static int g723_trunc(struct ast_filestream *fs)
{ {
/* Truncate file to current length */ /* Truncate file to current length */
if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0) if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
return -1; return -1;
return 0; return 0;
} }

View File

@@ -64,7 +64,7 @@ struct ast_filestream {
/* Do not place anything before "reserved" */ /* Do not place anything before "reserved" */
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Open file descriptor */ FILE *f; /* Open file descriptor */
int rate; /* RATE_* defines */ int rate; /* RATE_* defines */
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 */
@@ -88,7 +88,7 @@ static char *exts16 = "g726-16";
/* /*
* Rate dependant format functions (open, rewrite) * Rate dependant format functions (open, rewrite)
*/ */
static struct ast_filestream *g726_40_open(int fd) static struct ast_filestream *g726_40_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -101,7 +101,7 @@ static struct ast_filestream *g726_40_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_40; tmp->rate = RATE_40;
tmp->fr.data = tmp->g726; tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
@@ -119,7 +119,7 @@ static struct ast_filestream *g726_40_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *g726_32_open(int fd) static struct ast_filestream *g726_32_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -132,7 +132,7 @@ static struct ast_filestream *g726_32_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_32; tmp->rate = RATE_32;
tmp->fr.data = tmp->g726; tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
@@ -150,7 +150,7 @@ static struct ast_filestream *g726_32_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *g726_24_open(int fd) static struct ast_filestream *g726_24_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -163,7 +163,7 @@ static struct ast_filestream *g726_24_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_24; tmp->rate = RATE_24;
tmp->fr.data = tmp->g726; tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
@@ -181,7 +181,7 @@ static struct ast_filestream *g726_24_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *g726_16_open(int fd) static struct ast_filestream *g726_16_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -194,7 +194,7 @@ static struct ast_filestream *g726_16_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_16; tmp->rate = RATE_16;
tmp->fr.data = tmp->g726; tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
@@ -212,7 +212,7 @@ static struct ast_filestream *g726_16_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *g726_40_rewrite(int fd, const char *comment) static struct ast_filestream *g726_40_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -225,7 +225,7 @@ static struct ast_filestream *g726_40_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_40; tmp->rate = RATE_40;
glistcnt++; glistcnt++;
if (option_debug) if (option_debug)
@@ -238,7 +238,7 @@ static struct ast_filestream *g726_40_rewrite(int fd, const char *comment)
return tmp; return tmp;
} }
static struct ast_filestream *g726_32_rewrite(int fd, const char *comment) static struct ast_filestream *g726_32_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -251,7 +251,7 @@ static struct ast_filestream *g726_32_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_32; tmp->rate = RATE_32;
glistcnt++; glistcnt++;
if (option_debug) if (option_debug)
@@ -264,7 +264,7 @@ static struct ast_filestream *g726_32_rewrite(int fd, const char *comment)
return tmp; return tmp;
} }
static struct ast_filestream *g726_24_rewrite(int fd, const char *comment) static struct ast_filestream *g726_24_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -277,7 +277,7 @@ static struct ast_filestream *g726_24_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_24; tmp->rate = RATE_24;
glistcnt++; glistcnt++;
if (option_debug) if (option_debug)
@@ -290,7 +290,7 @@ static struct ast_filestream *g726_24_rewrite(int fd, const char *comment)
return tmp; return tmp;
} }
static struct ast_filestream *g726_16_rewrite(int fd, const char *comment) static struct ast_filestream *g726_16_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -303,7 +303,7 @@ static struct ast_filestream *g726_16_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->rate = RATE_16; tmp->rate = RATE_16;
glistcnt++; glistcnt++;
if (option_debug) if (option_debug)
@@ -330,7 +330,7 @@ static void g726_close(struct ast_filestream *s)
ast_log(LOG_DEBUG, "Closed filestream G.726-%dk.\n", 40 - s->rate * 8); ast_log(LOG_DEBUG, "Closed filestream G.726-%dk.\n", 40 - s->rate * 8);
ast_mutex_unlock(&g726_lock); ast_mutex_unlock(&g726_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -346,7 +346,7 @@ static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
s->fr.datalen = frame_size[s->rate]; s->fr.datalen = frame_size[s->rate];
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->g726; s->fr.data = s->g726;
if ((res = read(s->fd, s->g726, s->fr.datalen)) != s->fr.datalen) { if ((res = fread(s->g726, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -372,7 +372,7 @@ static int g726_write(struct ast_filestream *fs, struct ast_frame *f)
f->datalen, frame_size[fs->rate]); f->datalen, frame_size[fs->rate]);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n",
res, frame_size[fs->rate], strerror(errno)); res, frame_size[fs->rate], strerror(errno));
return -1; return -1;

View File

@@ -52,7 +52,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the /* Believe it or not, we must decode/recode to account for the
weird MS format */ weird MS format */
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
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 */
@@ -67,7 +67,7 @@ static char *name = "g729";
static char *desc = "Raw G729 data"; static char *desc = "Raw G729 data";
static char *exts = "g729"; static char *exts = "g729";
static struct ast_filestream *g729_open(int fd) static struct ast_filestream *g729_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -80,7 +80,7 @@ static struct ast_filestream *g729_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->g729; tmp->fr.data = tmp->g729;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_G729A; tmp->fr.subclass = AST_FORMAT_G729A;
@@ -94,7 +94,7 @@ static struct ast_filestream *g729_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *g729_rewrite(int fd, const char *comment) static struct ast_filestream *g729_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -107,7 +107,7 @@ static struct ast_filestream *g729_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&g729_lock); ast_mutex_unlock(&g729_lock);
ast_update_use_count(); ast_update_use_count();
@@ -125,7 +125,7 @@ static void g729_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&g729_lock); ast_mutex_unlock(&g729_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -141,7 +141,7 @@ static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
s->fr.datalen = 20; s->fr.datalen = 20;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->g729; s->fr.data = s->g729;
if ((res = read(s->fd, s->g729, 20)) != 20) { if ((res = fread(s->g729, 1, 20, s->f)) != 20) {
if (res && (res != 10)) if (res && (res != 10))
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -165,7 +165,7 @@ static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen); ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno));
return -1; return -1;
} }
@@ -182,8 +182,9 @@ static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
long bytes; long bytes;
off_t min,cur,max,offset=0; off_t min,cur,max,offset=0;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
bytes = 20 * (sample_offset / 160); bytes = 20 * (sample_offset / 160);
if (whence == SEEK_SET) if (whence == SEEK_SET)
@@ -197,7 +198,7 @@ static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
} }
/* protect against seeking beyond begining. */ /* protect against seeking beyond begining. */
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
if (lseek(fs->fd, offset, SEEK_SET) < 0) if (fseek(fs->f, offset, SEEK_SET) < 0)
return -1; return -1;
return 0; return 0;
} }
@@ -205,7 +206,7 @@ static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
static int g729_trunc(struct ast_filestream *fs) static int g729_trunc(struct ast_filestream *fs)
{ {
/* Truncate file to current length */ /* Truncate file to current length */
if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0) if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
return -1; return -1;
return 0; return 0;
} }
@@ -213,7 +214,7 @@ static int g729_trunc(struct ast_filestream *fs)
static long g729_tell(struct ast_filestream *fs) static long g729_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return (offset/20)*160; return (offset/20)*160;
} }

View File

@@ -62,7 +62,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the /* Believe it or not, we must decode/recode to account for the
weird MS format */ weird MS format */
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
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 */
@@ -77,7 +77,7 @@ static char *name = "gsm";
static char *desc = "Raw GSM data"; static char *desc = "Raw GSM data";
static char *exts = "gsm"; static char *exts = "gsm";
static struct ast_filestream *gsm_open(int fd) static struct ast_filestream *gsm_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -90,7 +90,7 @@ static struct ast_filestream *gsm_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->gsm; tmp->fr.data = tmp->gsm;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_GSM; tmp->fr.subclass = AST_FORMAT_GSM;
@@ -104,7 +104,7 @@ static struct ast_filestream *gsm_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *gsm_rewrite(int fd, const char *comment) static struct ast_filestream *gsm_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -117,7 +117,7 @@ static struct ast_filestream *gsm_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&gsm_lock); ast_mutex_unlock(&gsm_lock);
ast_update_use_count(); ast_update_use_count();
@@ -135,7 +135,7 @@ static void gsm_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&gsm_lock); ast_mutex_unlock(&gsm_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
} }
@@ -149,7 +149,7 @@ static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
s->fr.datalen = 33; s->fr.datalen = 33;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->gsm; s->fr.data = s->gsm;
if ((res = read(s->fd, s->gsm, 33)) != 33) { if ((res = fread(s->gsm, 1, 33, s->f)) != 33) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -175,7 +175,7 @@ static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
int len=0; int len=0;
while(len < f->datalen) { while(len < f->datalen) {
conv65(f->data + len, gsm); conv65(f->data + len, gsm);
if ((res = write(fs->fd, gsm, 66)) != 66) { if ((res = fwrite(gsm, 1, 66, fs->f)) != 66) {
ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno));
return -1; return -1;
} }
@@ -186,7 +186,7 @@ static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen); 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) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno));
return -1; return -1;
} }
@@ -199,8 +199,9 @@ static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
off_t offset=0,min,cur,max,distance; off_t offset=0,min,cur,max,distance;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
/* have to fudge to frame here, so not fully to sample */ /* have to fudge to frame here, so not fully to sample */
distance = (sample_offset/160) * 33; distance = (sample_offset/160) * 33;
if(whence == SEEK_SET) if(whence == SEEK_SET)
@@ -215,23 +216,23 @@ static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
offset = (offset > max)?max:offset; offset = (offset > max)?max:offset;
} else if (offset > max) { } else if (offset > max) {
int i; int i;
lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
for (i=0; i< (offset - max) / 33; i++) { for (i=0; i< (offset - max) / 33; i++) {
write(fs->fd, gsm_silence, 33); fwrite(gsm_silence, 1, 33, fs->f);
} }
} }
return lseek(fs->fd, offset, SEEK_SET); return fseek(fs->f, offset, SEEK_SET);
} }
static int gsm_trunc(struct ast_filestream *fs) static int gsm_trunc(struct ast_filestream *fs)
{ {
return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)); return ftruncate(fileno(fs->f), ftell(fs->f));
} }
static long gsm_tell(struct ast_filestream *fs) static long gsm_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return (offset/33)*160; return (offset/33)*160;
} }

View File

@@ -52,7 +52,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the /* Believe it or not, we must decode/recode to account for the
weird MS format */ weird MS format */
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
unsigned int lastts; unsigned int lastts;
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 */
@@ -68,7 +68,7 @@ static char *name = "h263";
static char *desc = "Raw h263 data"; static char *desc = "Raw h263 data";
static char *exts = "h263"; static char *exts = "h263";
static struct ast_filestream *h263_open(int fd) static struct ast_filestream *h263_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -76,7 +76,7 @@ static struct ast_filestream *h263_open(int fd)
struct ast_filestream *tmp; struct ast_filestream *tmp;
unsigned int ts; unsigned int ts;
int res; int res;
if ((res = read(fd, &ts, sizeof(ts))) < sizeof(ts)) { if ((res = fread(&ts, 1, sizeof(ts), f)) < sizeof(ts)) {
ast_log(LOG_WARNING, "Empty file!\n"); ast_log(LOG_WARNING, "Empty file!\n");
return NULL; return NULL;
} }
@@ -88,7 +88,7 @@ static struct ast_filestream *h263_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->h263; tmp->fr.data = tmp->h263;
tmp->fr.frametype = AST_FRAME_VIDEO; tmp->fr.frametype = AST_FRAME_VIDEO;
tmp->fr.subclass = AST_FORMAT_H263; tmp->fr.subclass = AST_FORMAT_H263;
@@ -102,7 +102,7 @@ static struct ast_filestream *h263_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *h263_rewrite(int fd, const char *comment) static struct ast_filestream *h263_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -115,7 +115,7 @@ static struct ast_filestream *h263_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&h263_lock); ast_mutex_unlock(&h263_lock);
ast_update_use_count(); ast_update_use_count();
@@ -133,7 +133,7 @@ static void h263_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&h263_lock); ast_mutex_unlock(&h263_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -150,7 +150,7 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->h263; s->fr.data = s->h263;
if ((res = read(s->fd, &len, sizeof(len))) < 1) { if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) {
return NULL; return NULL;
} }
len = ntohs(len); len = ntohs(len);
@@ -161,7 +161,7 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
if (len > sizeof(s->h263)) { if (len > sizeof(s->h263)) {
ast_log(LOG_WARNING, "Length %d is too long\n", len); ast_log(LOG_WARNING, "Length %d is too long\n", len);
} }
if ((res = read(s->fd, s->h263, len)) != len) { if ((res = fread(s->h263, 1, len, s->f)) != len) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -171,7 +171,7 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
s->fr.subclass |= mark; s->fr.subclass |= mark;
s->fr.delivery.tv_sec = 0; s->fr.delivery.tv_sec = 0;
s->fr.delivery.tv_usec = 0; s->fr.delivery.tv_usec = 0;
if ((res = read(s->fd, &ts, sizeof(ts))) == sizeof(ts)) { if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) {
s->lastts = ntohl(ts); s->lastts = ntohl(ts);
*whennext = s->lastts * 4/45; *whennext = s->lastts * 4/45;
} else } else
@@ -199,16 +199,16 @@ static int h263_write(struct ast_filestream *fs, struct ast_frame *f)
return -1; return -1;
} }
ts = htonl(f->samples); ts = htonl(f->samples);
if ((res = write(fs->fd, &ts, sizeof(ts))) != sizeof(ts)) { if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) {
ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno));
return -1; return -1;
} }
len = htons(f->datalen | mark); len = htons(f->datalen | mark);
if ((res = write(fs->fd, &len, sizeof(len))) != sizeof(len)) { if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) {
ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno));
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
return -1; return -1;
} }
@@ -229,15 +229,16 @@ static int h263_seek(struct ast_filestream *fs, long sample_offset, int whence)
static int h263_trunc(struct ast_filestream *fs) static int h263_trunc(struct ast_filestream *fs)
{ {
/* Truncate file to current length */ /* Truncate file to current length */
if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0) if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
return -1; return -1;
return 0; return 0;
} }
static long h263_tell(struct ast_filestream *fs) static long h263_tell(struct ast_filestream *fs)
{ {
/* XXX This is totally bogus XXX */
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return (offset/20)*160; return (offset/20)*160;
} }

View File

@@ -54,7 +54,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the /* Believe it or not, we must decode/recode to account for the
weird MS format */ weird MS format */
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
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 */
@@ -69,7 +69,7 @@ static char *name = "iLBC";
static char *desc = "Raw iLBC data"; static char *desc = "Raw iLBC data";
static char *exts = "ilbc"; static char *exts = "ilbc";
static struct ast_filestream *ilbc_open(int fd) static struct ast_filestream *ilbc_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -82,7 +82,7 @@ static struct ast_filestream *ilbc_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->ilbc; tmp->fr.data = tmp->ilbc;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ILBC; tmp->fr.subclass = AST_FORMAT_ILBC;
@@ -96,7 +96,7 @@ static struct ast_filestream *ilbc_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *ilbc_rewrite(int fd, const char *comment) static struct ast_filestream *ilbc_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -109,7 +109,7 @@ static struct ast_filestream *ilbc_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&ilbc_lock); ast_mutex_unlock(&ilbc_lock);
ast_update_use_count(); ast_update_use_count();
@@ -127,7 +127,7 @@ static void ilbc_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&ilbc_lock); ast_mutex_unlock(&ilbc_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -143,7 +143,7 @@ static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
s->fr.datalen = 50; s->fr.datalen = 50;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->ilbc; s->fr.data = s->ilbc;
if ((res = read(s->fd, s->ilbc, 50)) != 50) { if ((res = fread(s->ilbc, 1, 50, s->f)) != 50) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -167,7 +167,7 @@ static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen); ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno));
return -1; return -1;
} }
@@ -184,8 +184,9 @@ static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
long bytes; long bytes;
off_t min,cur,max,offset=0; off_t min,cur,max,offset=0;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
bytes = 50 * (sample_offset / 240); bytes = 50 * (sample_offset / 240);
if (whence == SEEK_SET) if (whence == SEEK_SET)
@@ -199,7 +200,7 @@ static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
} }
/* protect against seeking beyond begining. */ /* protect against seeking beyond begining. */
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
if (lseek(fs->fd, offset, SEEK_SET) < 0) if (fseek(fs->f, offset, SEEK_SET) < 0)
return -1; return -1;
return 0; return 0;
} }
@@ -207,7 +208,7 @@ static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
static int ilbc_trunc(struct ast_filestream *fs) static int ilbc_trunc(struct ast_filestream *fs)
{ {
/* Truncate file to current length */ /* Truncate file to current length */
if (ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)) < 0) if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0)
return -1; return -1;
return 0; return 0;
} }
@@ -215,7 +216,7 @@ static int ilbc_trunc(struct ast_filestream *fs)
static long ilbc_tell(struct ast_filestream *fs) static long ilbc_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return (offset/50)*240; return (offset/50)*240;
} }

View File

@@ -54,7 +54,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct ast_filestream { struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
int fd; FILE *f;
/* structures for handling the Ogg container */ /* structures for handling the Ogg container */
ogg_sync_state oy; ogg_sync_state oy;
@@ -92,10 +92,10 @@ static char *exts = "ogg";
/*! /*!
* \brief Create a new OGG/Vorbis filestream and set it up for reading. * \brief Create a new OGG/Vorbis filestream and set it up for reading.
* \param fd Descriptor that points to on disk storage of the OGG/Vorbis data. * \param f File that points to on disk storage of the OGG/Vorbis data.
* \return The new filestream. * \return The new filestream.
*/ */
static struct ast_filestream *ogg_vorbis_open(int fd) static struct ast_filestream *ogg_vorbis_open(FILE *f)
{ {
int i; int i;
int bytes; int bytes;
@@ -109,12 +109,12 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
tmp->writing = 0; tmp->writing = 0;
tmp->fd = fd; tmp->f = f;
ogg_sync_init(&tmp->oy); ogg_sync_init(&tmp->oy);
buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
bytes = read(tmp->fd, buffer, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, f);
ogg_sync_wrote(&tmp->oy, bytes); ogg_sync_wrote(&tmp->oy, bytes);
result = ogg_sync_pageout(&tmp->oy, &tmp->og); result = ogg_sync_pageout(&tmp->oy, &tmp->og);
@@ -124,7 +124,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
} else { } else {
ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n"); ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
} }
close(fd); fclose(f);
ogg_sync_clear(&tmp->oy); ogg_sync_clear(&tmp->oy);
free(tmp); free(tmp);
return NULL; return NULL;
@@ -136,7 +136,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
if(ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { if(ogg_stream_pagein(&tmp->os, &tmp->og) < 0) {
ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n"); ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc); vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi); vorbis_info_clear(&tmp->vi);
@@ -147,7 +147,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
if(ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { if(ogg_stream_packetout(&tmp->os, &tmp->op) != 1) {
ast_log(LOG_ERROR, "Error reading initial header packet.\n"); ast_log(LOG_ERROR, "Error reading initial header packet.\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc); vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi); vorbis_info_clear(&tmp->vi);
@@ -158,7 +158,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
if(vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { if(vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) {
ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n"); ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc); vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi); vorbis_info_clear(&tmp->vi);
@@ -181,7 +181,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
break; break;
if(result < 0) { if(result < 0) {
ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n"); ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc); vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi); vorbis_info_clear(&tmp->vi);
@@ -196,10 +196,10 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
} }
buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE);
bytes = read(tmp->fd, buffer, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, f);
if(bytes == 0 && i < 2) { if(bytes == 0 && i < 2) {
ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n"); ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc); vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi); vorbis_info_clear(&tmp->vi);
@@ -231,7 +231,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
if(tmp->vi.rate != 8000) { if(tmp->vi.rate != 8000) {
ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n"); ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_block_clear(&tmp->vb); vorbis_block_clear(&tmp->vb);
vorbis_dsp_clear(&tmp->vd); vorbis_dsp_clear(&tmp->vd);
@@ -247,7 +247,7 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
if(ast_mutex_lock(&ogg_vorbis_lock)) { if(ast_mutex_lock(&ogg_vorbis_lock)) {
ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n"); ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_block_clear(&tmp->vb); vorbis_block_clear(&tmp->vb);
vorbis_dsp_clear(&tmp->vd); vorbis_dsp_clear(&tmp->vd);
@@ -266,11 +266,11 @@ static struct ast_filestream *ogg_vorbis_open(int fd)
/*! /*!
* \brief Create a new OGG/Vorbis filestream and set it up for writing. * \brief Create a new OGG/Vorbis filestream and set it up for writing.
* \param fd File descriptor that points to on-disk storage. * \param f File pointer that points to on-disk storage.
* \param comment Comment that should be embedded in the OGG/Vorbis file. * \param comment Comment that should be embedded in the OGG/Vorbis file.
* \return A new filestream. * \return A new filestream.
*/ */
static struct ast_filestream *ogg_vorbis_rewrite(int fd, const char *comment) static struct ast_filestream *ogg_vorbis_rewrite(FILE *f, const char *comment)
{ {
ogg_packet header; ogg_packet header;
ogg_packet header_comm; ogg_packet header_comm;
@@ -282,7 +282,7 @@ static struct ast_filestream *ogg_vorbis_rewrite(int fd, const char *comment)
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
tmp->writing = 1; tmp->writing = 1;
tmp->fd = fd; tmp->f = f;
vorbis_info_init(&tmp->vi); vorbis_info_init(&tmp->vi);
@@ -310,15 +310,15 @@ static struct ast_filestream *ogg_vorbis_rewrite(int fd, const char *comment)
while(!tmp->eos) { while(!tmp->eos) {
if(ogg_stream_flush(&tmp->os, &tmp->og) == 0) if(ogg_stream_flush(&tmp->os, &tmp->og) == 0)
break; break;
write(tmp->fd, tmp->og.header, tmp->og.header_len); fwrite(tmp->og.header, 1, tmp->og.header_len, tmp->f);
write(tmp->fd, tmp->og.body, tmp->og.body_len); fwrite(tmp->og.body, 1, tmp->og.body_len, tmp->f);
if(ogg_page_eos(&tmp->og)) if(ogg_page_eos(&tmp->og))
tmp->eos = 1; tmp->eos = 1;
} }
if(ast_mutex_lock(&ogg_vorbis_lock)) { if(ast_mutex_lock(&ogg_vorbis_lock)) {
ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n"); ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
close(fd); fclose(f);
ogg_stream_clear(&tmp->os); ogg_stream_clear(&tmp->os);
vorbis_block_clear(&tmp->vb); vorbis_block_clear(&tmp->vb);
vorbis_dsp_clear(&tmp->vd); vorbis_dsp_clear(&tmp->vd);
@@ -350,8 +350,8 @@ static void write_stream(struct ast_filestream *s)
if(ogg_stream_pageout(&s->os, &s->og) == 0) { if(ogg_stream_pageout(&s->os, &s->og) == 0) {
break; break;
} }
write(s->fd, s->og.header, s->og.header_len); fwrite(s->og.header, 1, s->og.header_len, s->f);
write(s->fd, s->og.body, s->og.body_len); fwrite(s->og.body, 1, s->og.body_len, s->f);
if(ogg_page_eos(&s->og)) { if(ogg_page_eos(&s->og)) {
s->eos = 1; s->eos = 1;
} }
@@ -434,7 +434,7 @@ static void ogg_vorbis_close(struct ast_filestream *s)
ogg_sync_clear(&s->oy); ogg_sync_clear(&s->oy);
} }
close(s->fd); fclose(s->f);
free(s); free(s);
} }
@@ -503,7 +503,7 @@ static int read_samples(struct ast_filestream *s, float ***pcm)
/* get a buffer from OGG to read the data into */ /* get a buffer from OGG to read the data into */
buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE); buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
/* read more data from the file descriptor */ /* read more data from the file descriptor */
bytes = read(s->fd, buffer, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, s->f);
/* Tell OGG how many bytes we actually read into the buffer */ /* Tell OGG how many bytes we actually read into the buffer */
ogg_sync_wrote(&s->oy, bytes); ogg_sync_wrote(&s->oy, bytes);
if(bytes == 0) { if(bytes == 0) {

View File

@@ -48,7 +48,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct ast_filestream { struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
struct ast_channel *owner; struct ast_channel *owner;
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 */
@@ -65,7 +65,7 @@ static char *name = "pcm";
static char *desc = "Raw uLaw 8khz Audio support (PCM)"; static char *desc = "Raw uLaw 8khz Audio support (PCM)";
static char *exts = "pcm|ulaw|ul|mu"; static char *exts = "pcm|ulaw|ul|mu";
static struct ast_filestream *pcm_open(int fd) static struct ast_filestream *pcm_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -78,7 +78,7 @@ static struct ast_filestream *pcm_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->buf; tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ULAW; tmp->fr.subclass = AST_FORMAT_ULAW;
@@ -92,7 +92,7 @@ static struct ast_filestream *pcm_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *pcm_rewrite(int fd, const char *comment) static struct ast_filestream *pcm_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -105,7 +105,7 @@ static struct ast_filestream *pcm_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&pcm_lock); ast_mutex_unlock(&pcm_lock);
ast_update_use_count(); ast_update_use_count();
@@ -123,7 +123,7 @@ static void pcm_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&pcm_lock); ast_mutex_unlock(&pcm_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -139,7 +139,7 @@ static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->buf; s->fr.data = s->buf;
if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) { if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -162,7 +162,7 @@ static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass); ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
return -1; return -1;
} }
@@ -174,8 +174,9 @@ static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
off_t offset=0,min,cur,max; off_t offset=0,min,cur,max;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
if (whence == SEEK_SET) if (whence == SEEK_SET)
offset = sample_offset; offset = sample_offset;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
@@ -187,18 +188,18 @@ static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
} }
/* always protect against seeking past begining. */ /* always protect against seeking past begining. */
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
return lseek(fs->fd, offset, SEEK_SET); return fseek(fs->f, offset, SEEK_SET);
} }
static int pcm_trunc(struct ast_filestream *fs) static int pcm_trunc(struct ast_filestream *fs)
{ {
return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)); return ftruncate(fileno(fs->f), ftell(fs->f));
} }
static long pcm_tell(struct ast_filestream *fs) static long pcm_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return offset; return offset;
} }

View File

@@ -54,7 +54,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the /* Believe it or not, we must decode/recode to account for the
weird MS format */ weird MS format */
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
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 */
@@ -90,7 +90,7 @@ static unsigned long get_time(void)
} }
#endif #endif
static struct ast_filestream *pcm_open(int fd) static struct ast_filestream *pcm_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -103,7 +103,7 @@ static struct ast_filestream *pcm_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->buf; tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ALAW; tmp->fr.subclass = AST_FORMAT_ALAW;
@@ -120,7 +120,7 @@ static struct ast_filestream *pcm_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *pcm_rewrite(int fd, const char *comment) static struct ast_filestream *pcm_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -133,7 +133,7 @@ static struct ast_filestream *pcm_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
#ifdef REALTIME_WRITE #ifdef REALTIME_WRITE
tmp->start_time = get_time(); tmp->start_time = get_time();
#endif #endif
@@ -154,7 +154,7 @@ static void pcm_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&pcm_lock); ast_mutex_unlock(&pcm_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -169,7 +169,7 @@ static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->buf; s->fr.data = s->buf;
if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) { if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -204,46 +204,41 @@ static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
/* Check if we have written to this position yet. If we have, then increment pos by one frame /* Check if we have written to this position yet. If we have, then increment pos by one frame
* for some degree of protection against receiving packets in the same clock tick. * for some degree of protection against receiving packets in the same clock tick.
*/ */
fstat( fs->fd, &stat_buf );
if( stat_buf.st_size > fpos ) fstat(fileno(fs->f), &stat_buf );
{ if (stat_buf.st_size > fpos ) {
fpos += f->datalen; /* Incrementing with the size of this current frame */ fpos += f->datalen; /* Incrementing with the size of this current frame */
} }
if( stat_buf.st_size < fpos ) if (stat_buf.st_size < fpos) {
{
/* fill the gap with 0x55 rather than 0. */ /* fill the gap with 0x55 rather than 0. */
char buf[ 512 ]; char buf[ 512 ];
unsigned long cur, to_write; unsigned long cur, to_write;
cur = stat_buf.st_size; cur = stat_buf.st_size;
if( lseek( fs->fd, cur, SEEK_SET ) < 0 ) if (fseek(fs->f, cur, SEEK_SET) < 0) {
{
ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) ); ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
return -1; return -1;
} }
memset( buf, 0x55, 512 ); memset(buf, 0x55, 512);
while( cur < fpos ) while (cur < fpos) {
{
to_write = fpos - cur; to_write = fpos - cur;
if( to_write > 512 ) if (to_write > 512) {
{
to_write = 512; to_write = 512;
} }
write( fs->fd, buf, to_write ); fwrite(buf, 1, to_write, fs->f);
cur += to_write; cur += to_write;
} }
} }
if( lseek( fs->fd, fpos, SEEK_SET ) < 0 ) if (fseek(s->f, fpos, SEEK_SET) < 0) {
{
ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) ); ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
return -1; return -1;
} }
#endif /* REALTIME_WRITE */ #endif /* REALTIME_WRITE */
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
return -1; return -1;
} }
@@ -255,8 +250,9 @@ static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
off_t offset=0,min,cur,max; off_t offset=0,min,cur,max;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
if (whence == SEEK_SET) if (whence == SEEK_SET)
offset = sample_offset; offset = sample_offset;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
@@ -268,18 +264,18 @@ static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
} }
/* Always protect against seeking past begining */ /* Always protect against seeking past begining */
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
return lseek(fs->fd, offset, SEEK_SET); return fseek(fs->f, offset, SEEK_SET);
} }
static int pcm_trunc(struct ast_filestream *fs) static int pcm_trunc(struct ast_filestream *fs)
{ {
return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)); return ftruncate(fileno(fs->f), ftell(fs->f));
} }
static long pcm_tell(struct ast_filestream *fs) static long pcm_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return offset; return offset;
} }

View File

@@ -47,7 +47,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct ast_filestream { struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
struct ast_channel *owner; struct ast_channel *owner;
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 */
@@ -64,7 +64,7 @@ static char *name = "sln";
static char *desc = "Raw Signed Linear Audio support (SLN)"; static char *desc = "Raw Signed Linear Audio support (SLN)";
static char *exts = "sln|raw"; static char *exts = "sln|raw";
static struct ast_filestream *slinear_open(int fd) static struct ast_filestream *slinear_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -77,7 +77,7 @@ static struct ast_filestream *slinear_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->buf; tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_SLINEAR; tmp->fr.subclass = AST_FORMAT_SLINEAR;
@@ -91,7 +91,7 @@ static struct ast_filestream *slinear_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *slinear_rewrite(int fd, const char *comment) static struct ast_filestream *slinear_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -104,7 +104,7 @@ static struct ast_filestream *slinear_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&slinear_lock); ast_mutex_unlock(&slinear_lock);
ast_update_use_count(); ast_update_use_count();
@@ -122,7 +122,7 @@ static void slinear_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&slinear_lock); ast_mutex_unlock(&slinear_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -138,7 +138,7 @@ static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext)
s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->buf; s->fr.data = s->buf;
if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) { if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -161,7 +161,7 @@ static int slinear_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass); ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
return -1; return -1;
} }
@@ -174,8 +174,9 @@ static int slinear_seek(struct ast_filestream *fs, long sample_offset, int whenc
min = 0; min = 0;
sample_offset <<= 1; sample_offset <<= 1;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
if (whence == SEEK_SET) if (whence == SEEK_SET)
offset = sample_offset; offset = sample_offset;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
@@ -187,18 +188,18 @@ static int slinear_seek(struct ast_filestream *fs, long sample_offset, int whenc
} }
/* always protect against seeking past begining. */ /* always protect against seeking past begining. */
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
return lseek(fs->fd, offset, SEEK_SET) / 2; return fseek(fs->f, offset, SEEK_SET) / 2;
} }
static int slinear_trunc(struct ast_filestream *fs) static int slinear_trunc(struct ast_filestream *fs)
{ {
return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)); return ftruncate(fileno(fs->f), ftell(fs->f));
} }
static long slinear_tell(struct ast_filestream *fs) static long slinear_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
return offset / 2; return offset / 2;
} }

View File

@@ -48,7 +48,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct ast_filestream { struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
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 */
@@ -69,7 +69,7 @@ static char *name = "vox";
static char *desc = "Dialogic VOX (ADPCM) File Format"; static char *desc = "Dialogic VOX (ADPCM) File Format";
static char *exts = "vox"; static char *exts = "vox";
static struct ast_filestream *vox_open(int fd) static struct ast_filestream *vox_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -82,7 +82,7 @@ static struct ast_filestream *vox_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->buf; tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ADPCM; tmp->fr.subclass = AST_FORMAT_ADPCM;
@@ -97,7 +97,7 @@ static struct ast_filestream *vox_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *vox_rewrite(int fd, const char *comment) static struct ast_filestream *vox_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -110,7 +110,7 @@ static struct ast_filestream *vox_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&vox_lock); ast_mutex_unlock(&vox_lock);
ast_update_use_count(); ast_update_use_count();
@@ -128,7 +128,7 @@ static void vox_close(struct ast_filestream *s)
glistcnt--; glistcnt--;
ast_mutex_unlock(&vox_lock); ast_mutex_unlock(&vox_lock);
ast_update_use_count(); ast_update_use_count();
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -142,7 +142,7 @@ static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
s->fr.offset = AST_FRIENDLY_OFFSET; s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0; s->fr.mallocd = 0;
s->fr.data = s->buf; s->fr.data = s->buf;
if ((res = read(s->fd, s->buf, BUF_SIZE)) < 1) { if ((res = fread(s->buf, 1, BUF_SIZE, s->f)) < 1) {
if (res) if (res)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -164,7 +164,7 @@ static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass); ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
return -1; return -1;
} }
if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) { if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) {
ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
return -1; return -1;
} }
@@ -181,8 +181,10 @@ static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
off_t offset=0,min,cur,max,distance; off_t offset=0,min,cur,max,distance;
min = 0; min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
/* have to fudge to frame here, so not fully to sample */ /* have to fudge to frame here, so not fully to sample */
distance = sample_offset/2; distance = sample_offset/2;
if(whence == SEEK_SET) if(whence == SEEK_SET)
@@ -195,18 +197,19 @@ static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
offset = (offset > max)?max:offset; offset = (offset > max)?max:offset;
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
} }
return lseek(fs->fd, offset, SEEK_SET); fseek(fs->f, offset, SEEK_SET);
return ftell(fs->f);
} }
static int vox_trunc(struct ast_filestream *fs) static int vox_trunc(struct ast_filestream *fs)
{ {
return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)); return ftruncate(fileno(fs->f), ftell(fs->f));
} }
static long vox_tell(struct ast_filestream *fs) static long vox_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f) << 1;
return offset; return offset;
} }

View File

@@ -50,7 +50,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
struct ast_filestream { struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS]; void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
int bytes; int bytes;
int needsgain; int needsgain;
struct ast_frame fr; /* Frame information */ struct ast_frame fr; /* Frame information */
@@ -98,7 +98,7 @@ static char *exts = "wav";
#endif #endif
static int check_header(int fd) static int check_header(FILE *f)
{ {
int type, size, formtype; int type, size, formtype;
int fmt, hsize; int fmt, hsize;
@@ -106,16 +106,16 @@ static int check_header(int fd)
int bysec; int bysec;
int freq; int freq;
int data; int data;
if (read(fd, &type, 4) != 4) { if (fread(&type, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (type)\n"); ast_log(LOG_WARNING, "Read failed (type)\n");
return -1; return -1;
} }
if (read(fd, &size, 4) != 4) { if (fread(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (size)\n"); ast_log(LOG_WARNING, "Read failed (size)\n");
return -1; return -1;
} }
size = ltohl(size); size = ltohl(size);
if (read(fd, &formtype, 4) != 4) { if (fread(&formtype, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n"); ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1; return -1;
} }
@@ -127,7 +127,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Does not contain WAVE\n"); ast_log(LOG_WARNING, "Does not contain WAVE\n");
return -1; return -1;
} }
if (read(fd, &fmt, 4) != 4) { if (fread(&fmt, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fmt)\n"); ast_log(LOG_WARNING, "Read failed (fmt)\n");
return -1; return -1;
} }
@@ -135,7 +135,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Does not say fmt\n"); ast_log(LOG_WARNING, "Does not say fmt\n");
return -1; return -1;
} }
if (read(fd, &hsize, 4) != 4) { if (fread(&hsize, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n"); ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1; return -1;
} }
@@ -143,7 +143,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize)); ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
return -1; return -1;
} }
if (read(fd, &format, 2) != 2) { if (fread(&format, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n"); ast_log(LOG_WARNING, "Read failed (format)\n");
return -1; return -1;
} }
@@ -151,7 +151,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format)); ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
return -1; return -1;
} }
if (read(fd, &chans, 2) != 2) { if (fread(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n"); ast_log(LOG_WARNING, "Read failed (format)\n");
return -1; return -1;
} }
@@ -159,7 +159,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans)); ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
return -1; return -1;
} }
if (read(fd, &freq, 4) != 4) { if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (freq)\n"); ast_log(LOG_WARNING, "Read failed (freq)\n");
return -1; return -1;
} }
@@ -168,12 +168,12 @@ static int check_header(int fd)
return -1; return -1;
} }
/* Ignore the byte frequency */ /* Ignore the byte frequency */
if (read(fd, &bysec, 4) != 4) { if (fread(&bysec, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n"); ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
return -1; return -1;
} }
/* Check bytes per sample */ /* Check bytes per sample */
if (read(fd, &bysam, 2) != 2) { if (fread(&bysam, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n"); ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
return -1; return -1;
} }
@@ -181,39 +181,40 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam)); ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
return -1; return -1;
} }
if (read(fd, &bisam, 2) != 2) { if (fread(&bisam, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam)); ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
return -1; return -1;
} }
/* Skip any additional header */ /* Skip any additional header */
if ( lseek(fd,ltohl(hsize)-16,SEEK_CUR) == -1 ) { if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 ); ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
return -1; return -1;
} }
/* Skip any facts and get the first data block */ /* Skip any facts and get the first data block */
for(;;) for(;;)
{ {
char buf[4]; char buf[4];
/* Begin data chunk */ /* Begin data chunk */
if (read(fd, &buf, 4) != 4) { if (fread(&buf, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (data)\n"); ast_log(LOG_WARNING, "Read failed (data)\n");
return -1; return -1;
} }
/* Data has the actual length of data in it */ /* Data has the actual length of data in it */
if (read(fd, &data, 4) != 4) { if (fread(&data, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (data)\n"); ast_log(LOG_WARNING, "Read failed (data)\n");
return -1; return -1;
} }
data = ltohl(data); data = ltohl(data);
if( memcmp(buf, "data", 4) == 0 ) break; if(memcmp(buf, "data", 4) == 0 )
if( memcmp(buf, "fact", 4) != 0 ) { break;
ast_log(LOG_WARNING, "Unknown block - not fact or data\n"); if(memcmp(buf, "fact", 4) != 0 ) {
return -1; ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
return -1;
} }
if ( lseek(fd,data,SEEK_CUR) == -1 ) { if (fseek(f,data,SEEK_CUR) == -1 ) {
ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data ); ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
return -1; return -1;
} }
} }
#if 0 #if 0
@@ -225,14 +226,15 @@ static int check_header(int fd)
return data; return data;
} }
static int update_header(int fd) static int update_header(FILE *f)
{ {
off_t cur,end; off_t cur,end;
int datalen,filelen,bytes; int datalen,filelen,bytes;
cur = lseek(fd, 0, SEEK_CUR); cur = ftell(f);
end = lseek(fd, 0, SEEK_END); fseek(f, 0, SEEK_END);
end = ftell(f);
/* data starts 44 bytes in */ /* data starts 44 bytes in */
bytes = end - 44; bytes = end - 44;
datalen = htoll(bytes); datalen = htoll(bytes);
@@ -243,30 +245,30 @@ static int update_header(int fd)
ast_log(LOG_WARNING, "Unable to find our position\n"); ast_log(LOG_WARNING, "Unable to find our position\n");
return -1; return -1;
} }
if (lseek(fd, 4, SEEK_SET) != 4) { if (fseek(f, 4, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to set our position\n"); ast_log(LOG_WARNING, "Unable to set our position\n");
return -1; return -1;
} }
if (write(fd, &filelen, 4) != 4) { if (fwrite(&filelen, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to set write file size\n"); ast_log(LOG_WARNING, "Unable to set write file size\n");
return -1; return -1;
} }
if (lseek(fd, 40, SEEK_SET) != 40) { if (fseek(f, 40, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to set our position\n"); ast_log(LOG_WARNING, "Unable to set our position\n");
return -1; return -1;
} }
if (write(fd, &datalen, 4) != 4) { if (fwrite(&datalen, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to set write datalen\n"); ast_log(LOG_WARNING, "Unable to set write datalen\n");
return -1; return -1;
} }
if (lseek(fd, cur, SEEK_SET) != cur) { if (fseek(f, cur, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to return to position\n"); ast_log(LOG_WARNING, "Unable to return to position\n");
return -1; return -1;
} }
return 0; return 0;
} }
static int write_header(int fd) static int write_header(FILE *f)
{ {
unsigned int hz=htoll(8000); unsigned int hz=htoll(8000);
unsigned int bhz = htoll(16000); unsigned int bhz = htoll(16000);
@@ -277,59 +279,59 @@ static int write_header(int fd)
unsigned short bisam = htols(16); unsigned short bisam = htols(16);
unsigned int size = htoll(0); unsigned int size = htoll(0);
/* Write a wav header, ignoring sizes which will be filled in later */ /* Write a wav header, ignoring sizes which will be filled in later */
lseek(fd,0,SEEK_SET); fseek(f,0,SEEK_SET);
if (write(fd, "RIFF", 4) != 4) { if (fwrite("RIFF", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &size, 4) != 4) { if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, "WAVEfmt ", 8) != 8) { if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &hs, 4) != 4) { if (fwrite(&hs, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &fmt, 2) != 2) { if (fwrite(&fmt, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &chans, 2) != 2) { if (fwrite(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &hz, 4) != 4) { if (fwrite(&hz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &bhz, 4) != 4) { if (fwrite(&bhz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &bysam, 2) != 2) { if (fwrite(&bysam, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &bisam, 2) != 2) { if (fwrite(&bisam, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, "data", 4) != 4) { if (fwrite("data", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &size, 4) != 4) { if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
return 0; return 0;
} }
static struct ast_filestream *wav_open(int fd) static struct ast_filestream *wav_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -337,7 +339,7 @@ static struct ast_filestream *wav_open(int fd)
struct ast_filestream *tmp; struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) { if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
if ((tmp->maxlen = check_header(fd)) < 0) { if ((tmp->maxlen = check_header(f)) < 0) {
free(tmp); free(tmp);
return NULL; return NULL;
} }
@@ -346,7 +348,7 @@ static struct ast_filestream *wav_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->needsgain = 1; tmp->needsgain = 1;
tmp->fr.data = tmp->buf; tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
@@ -362,7 +364,7 @@ static struct ast_filestream *wav_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *wav_rewrite(int fd, const char *comment) static struct ast_filestream *wav_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -370,7 +372,7 @@ static struct ast_filestream *wav_rewrite(int fd, const char *comment)
struct ast_filestream *tmp; struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) { if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
if (write_header(fd)) { if (write_header(f)) {
free(tmp); free(tmp);
return NULL; return NULL;
} }
@@ -379,7 +381,7 @@ static struct ast_filestream *wav_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&wav_lock); ast_mutex_unlock(&wav_lock);
ast_update_use_count(); ast_update_use_count();
@@ -400,8 +402,8 @@ static void wav_close(struct ast_filestream *s)
ast_update_use_count(); ast_update_use_count();
/* Pad to even length */ /* Pad to even length */
if (s->bytes & 0x1) if (s->bytes & 0x1)
write(s->fd, &zero, 1); fwrite(&zero, 1, 1, s->f);
close(s->fd); fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -415,14 +417,14 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
int bytes = sizeof(tmp); int bytes = sizeof(tmp);
off_t here; off_t here;
/* Send a frame from the file to the appropriate channel */ /* Send a frame from the file to the appropriate channel */
here = lseek(s->fd, 0, SEEK_CUR); here = ftell(s->f);
if ((s->maxlen - here) < bytes) if ((s->maxlen - here) < bytes)
bytes = s->maxlen - here; bytes = s->maxlen - here;
if (bytes < 0) if (bytes < 0)
bytes = 0; bytes = 0;
/* ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */ /* ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
if ( (res = read(s->fd, tmp, bytes)) <= 0 ) { if ( (res = fread(tmp, 1, bytes, s->f)) <= 0 ) {
if (res) { if (res) {
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
} }
@@ -503,7 +505,7 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
#endif #endif
} }
if ((write (fs->fd, tmp, f->datalen) != f->datalen) ) { if ((fwrite(tmp, 1, f->datalen, fs->f) != f->datalen) ) {
ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno)); ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
return -1; return -1;
} }
@@ -513,7 +515,7 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
} }
fs->bytes += f->datalen; fs->bytes += f->datalen;
update_header(fs->fd); update_header(fs->f);
return 0; return 0;
@@ -526,8 +528,9 @@ static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */ samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
min = 44; /* wav header is 44 bytes */ min = 44; /* wav header is 44 bytes */
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
if (whence == SEEK_SET) if (whence == SEEK_SET)
offset = samples + min; offset = samples + min;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
@@ -539,20 +542,20 @@ static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
} }
/* always protect the header space. */ /* always protect the header space. */
offset = (offset < min)?min:offset; offset = (offset < min)?min:offset;
return lseek(fs->fd,offset,SEEK_SET); return fseek(fs->f,offset,SEEK_SET);
} }
static int wav_trunc(struct ast_filestream *fs) static int wav_trunc(struct ast_filestream *fs)
{ {
if(ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR))) if (ftruncate(fileno(fs->f), ftell(fs->f)))
return -1; return -1;
return update_header(fs->fd); return update_header(fs->f);
} }
static long wav_tell(struct ast_filestream *fs) static long wav_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
/* subtract header size to get samples, then divide by 2 for 16 bit samples */ /* subtract header size to get samples, then divide by 2 for 16 bit samples */
return (offset - 44)/2; return (offset - 44)/2;
} }

View File

@@ -63,7 +63,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the /* Believe it or not, we must decode/recode to account for the
weird MS format */ weird MS format */
/* This is what a filestream means to us */ /* This is what a filestream means to us */
int fd; /* Descriptor */ FILE *f; /* Descriptor */
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 */
@@ -104,23 +104,23 @@ static char *exts = "WAV|wav49";
#endif #endif
static int check_header(int fd) static int check_header(FILE *f)
{ {
int type, size, formtype; int type, size, formtype;
int fmt, hsize, fact; int fmt, hsize, fact;
short format, chans; short format, chans;
int freq; int freq;
int data; int data;
if (read(fd, &type, 4) != 4) { if (fread(&type, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (type)\n"); ast_log(LOG_WARNING, "Read failed (type)\n");
return -1; return -1;
} }
if (read(fd, &size, 4) != 4) { if (fread(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (size)\n"); ast_log(LOG_WARNING, "Read failed (size)\n");
return -1; return -1;
} }
size = ltohl(size); size = ltohl(size);
if (read(fd, &formtype, 4) != 4) { if (fread(&formtype, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n"); ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1; return -1;
} }
@@ -132,7 +132,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Does not contain WAVE\n"); ast_log(LOG_WARNING, "Does not contain WAVE\n");
return -1; return -1;
} }
if (read(fd, &fmt, 4) != 4) { if (fread(&fmt, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fmt)\n"); ast_log(LOG_WARNING, "Read failed (fmt)\n");
return -1; return -1;
} }
@@ -140,7 +140,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Does not say fmt\n"); ast_log(LOG_WARNING, "Does not say fmt\n");
return -1; return -1;
} }
if (read(fd, &hsize, 4) != 4) { if (fread(&hsize, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n"); ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1; return -1;
} }
@@ -148,7 +148,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize)); ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
return -1; return -1;
} }
if (read(fd, &format, 2) != 2) { if (fread(&format, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n"); ast_log(LOG_WARNING, "Read failed (format)\n");
return -1; return -1;
} }
@@ -156,7 +156,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format)); ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
return -1; return -1;
} }
if (read(fd, &chans, 2) != 2) { if (fread(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n"); ast_log(LOG_WARNING, "Read failed (format)\n");
return -1; return -1;
} }
@@ -164,7 +164,7 @@ static int check_header(int fd)
ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans)); ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
return -1; return -1;
} }
if (read(fd, &freq, 4) != 4) { if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (freq)\n"); ast_log(LOG_WARNING, "Read failed (freq)\n");
return -1; return -1;
} }
@@ -173,22 +173,22 @@ static int check_header(int fd)
return -1; return -1;
} }
/* Ignore the byte frequency */ /* Ignore the byte frequency */
if (read(fd, &freq, 4) != 4) { if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (X_1)\n"); ast_log(LOG_WARNING, "Read failed (X_1)\n");
return -1; return -1;
} }
/* Ignore the two weird fields */ /* Ignore the two weird fields */
if (read(fd, &freq, 4) != 4) { if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n"); ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
return -1; return -1;
} }
/* Ignore the byte frequency */ /* Ignore the byte frequency */
if (read(fd, &freq, 4) != 4) { if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (Y_1)\n"); ast_log(LOG_WARNING, "Read failed (Y_1)\n");
return -1; return -1;
} }
/* Check for the word fact */ /* Check for the word fact */
if (read(fd, &fact, 4) != 4) { if (fread(&fact, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fact)\n"); ast_log(LOG_WARNING, "Read failed (fact)\n");
return -1; return -1;
} }
@@ -197,16 +197,16 @@ static int check_header(int fd)
return -1; return -1;
} }
/* Ignore the "fact value" */ /* Ignore the "fact value" */
if (read(fd, &fact, 4) != 4) { if (fread(&fact, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fact header)\n"); ast_log(LOG_WARNING, "Read failed (fact header)\n");
return -1; return -1;
} }
if (read(fd, &fact, 4) != 4) { if (fread(&fact, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fact value)\n"); ast_log(LOG_WARNING, "Read failed (fact value)\n");
return -1; return -1;
} }
/* Check for the word data */ /* Check for the word data */
if (read(fd, &data, 4) != 4) { if (fread(&data, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (data)\n"); ast_log(LOG_WARNING, "Read failed (data)\n");
return -1; return -1;
} }
@@ -215,20 +215,21 @@ static int check_header(int fd)
return -1; return -1;
} }
/* Ignore the data length */ /* Ignore the data length */
if (read(fd, &data, 4) != 4) { if (fread(&data, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (data)\n"); ast_log(LOG_WARNING, "Read failed (data)\n");
return -1; return -1;
} }
return 0; return 0;
} }
static int update_header(int fd) static int update_header(FILE *f)
{ {
off_t cur,end,bytes; off_t cur,end,bytes;
int datalen,filelen; int datalen,filelen;
cur = lseek(fd, 0, SEEK_CUR); cur = ftell(f);
end = lseek(fd, 0, SEEK_END); fseek(f, 0, SEEK_END);
end = ftell(f);
/* in a gsm WAV, data starts 60 bytes in */ /* in a gsm WAV, data starts 60 bytes in */
bytes = end - 60; bytes = end - 60;
datalen = htoll((bytes + 1) & ~0x1); datalen = htoll((bytes + 1) & ~0x1);
@@ -237,30 +238,30 @@ static int update_header(int fd)
ast_log(LOG_WARNING, "Unable to find our position\n"); ast_log(LOG_WARNING, "Unable to find our position\n");
return -1; return -1;
} }
if (lseek(fd, 4, SEEK_SET) != 4) { if (fseek(f, 4, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to set our position\n"); ast_log(LOG_WARNING, "Unable to set our position\n");
return -1; return -1;
} }
if (write(fd, &filelen, 4) != 4) { if (fwrite(&filelen, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to set write file size\n"); ast_log(LOG_WARNING, "Unable to set write file size\n");
return -1; return -1;
} }
if (lseek(fd, 56, SEEK_SET) != 56) { if (fseek(f, 56, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to set our position\n"); ast_log(LOG_WARNING, "Unable to set our position\n");
return -1; return -1;
} }
if (write(fd, &datalen, 4) != 4) { if (fwrite(&datalen, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to set write datalen\n"); ast_log(LOG_WARNING, "Unable to set write datalen\n");
return -1; return -1;
} }
if (lseek(fd, cur, SEEK_SET) != cur) { if (fseek(f, cur, SEEK_SET)) {
ast_log(LOG_WARNING, "Unable to return to position\n"); ast_log(LOG_WARNING, "Unable to return to position\n");
return -1; return -1;
} }
return 0; return 0;
} }
static int write_header(int fd) static int write_header(FILE *f)
{ {
unsigned int hz=htoll(8000); unsigned int hz=htoll(8000);
unsigned int bhz = htoll(1625); unsigned int bhz = htoll(1625);
@@ -274,74 +275,74 @@ static int write_header(int fd)
unsigned int y_1 = htoll(20160); unsigned int y_1 = htoll(20160);
unsigned int size = htoll(0); unsigned int size = htoll(0);
/* Write a GSM header, ignoring sizes which will be filled in later */ /* Write a GSM header, ignoring sizes which will be filled in later */
if (write(fd, "RIFF", 4) != 4) { if (fwrite("RIFF", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &size, 4) != 4) { if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, "WAVEfmt ", 8) != 8) { if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &hs, 4) != 4) { if (fwrite(&hs, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &fmt, 2) != 2) { if (fwrite(&fmt, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &chans, 2) != 2) { if (fwrite(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &hz, 4) != 4) { if (fwrite(&hz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &bhz, 4) != 4) { if (fwrite(&bhz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &x_1, 4) != 4) { if (fwrite(&x_1, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &x_2, 2) != 2) { if (fwrite(&x_2, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &x_3, 2) != 2) { if (fwrite(&x_3, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, "fact", 4) != 4) { if (fwrite("fact", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &fhs, 4) != 4) { if (fwrite(&fhs, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &y_1, 4) != 4) { if (fwrite(&y_1, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, "data", 4) != 4) { if (fwrite("data", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
if (write(fd, &size, 4) != 4) { if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n"); ast_log(LOG_WARNING, "Unable to write header\n");
return -1; return -1;
} }
return 0; return 0;
} }
static struct ast_filestream *wav_open(int fd) static struct ast_filestream *wav_open(FILE *f)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -349,7 +350,7 @@ static struct ast_filestream *wav_open(int fd)
struct ast_filestream *tmp; struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) { if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
if (check_header(fd)) { if (check_header(f)) {
free(tmp); free(tmp);
return NULL; return NULL;
} }
@@ -358,7 +359,7 @@ static struct ast_filestream *wav_open(int fd)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
tmp->fr.data = tmp->gsm; tmp->fr.data = tmp->gsm;
tmp->fr.frametype = AST_FRAME_VOICE; tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_GSM; tmp->fr.subclass = AST_FORMAT_GSM;
@@ -373,7 +374,7 @@ static struct ast_filestream *wav_open(int fd)
return tmp; return tmp;
} }
static struct ast_filestream *wav_rewrite(int fd, const char *comment) static struct ast_filestream *wav_rewrite(FILE *f, const char *comment)
{ {
/* We don't have any header to read or anything really, but /* We don't have any header to read or anything really, but
if we did, it would go here. We also might want to check if we did, it would go here. We also might want to check
@@ -381,7 +382,7 @@ static struct ast_filestream *wav_rewrite(int fd, const char *comment)
struct ast_filestream *tmp; struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) { if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream)); memset(tmp, 0, sizeof(struct ast_filestream));
if (write_header(fd)) { if (write_header(f)) {
free(tmp); free(tmp);
return NULL; return NULL;
} }
@@ -390,7 +391,7 @@ static struct ast_filestream *wav_rewrite(int fd, const char *comment)
free(tmp); free(tmp);
return NULL; return NULL;
} }
tmp->fd = fd; tmp->f = f;
glistcnt++; glistcnt++;
ast_mutex_unlock(&wav_lock); ast_mutex_unlock(&wav_lock);
ast_update_use_count(); ast_update_use_count();
@@ -410,9 +411,10 @@ static void wav_close(struct ast_filestream *s)
ast_mutex_unlock(&wav_lock); ast_mutex_unlock(&wav_lock);
ast_update_use_count(); ast_update_use_count();
/* Pad to even length */ /* Pad to even length */
if (lseek(s->fd, 0, SEEK_END) & 0x1) fseek(s->f, 0, SEEK_END);
write(s->fd, &zero, 1); if (ftell(s->f) & 0x1)
close(s->fd); fwrite(&zero, 1, 1, s->f);
fclose(s->f);
free(s); free(s);
s = NULL; s = NULL;
} }
@@ -433,7 +435,7 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
/* Just return a frame based on the second GSM frame */ /* Just return a frame based on the second GSM frame */
s->fr.data = s->gsm + 33; s->fr.data = s->gsm + 33;
} else { } else {
if ((res = read(s->fd, msdata, 65)) != 65) { if ((res = fread(msdata, 1, 65, s->f)) != 65) {
if (res && (res != 1)) if (res && (res != 1))
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL; return NULL;
@@ -466,21 +468,21 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
while(len < f->datalen) { while(len < f->datalen) {
if (alreadyms) { if (alreadyms) {
fs->secondhalf = 0; fs->secondhalf = 0;
if ((res = write(fs->fd, f->data + len, 65)) != 65) { if ((res = fwrite(f->data + len, 1, 65, fs->f)) != 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;
} }
update_header(fs->fd); update_header(fs->f);
len += 65; len += 65;
} else { } else {
if (fs->secondhalf) { if (fs->secondhalf) {
memcpy(fs->gsm + 33, f->data + len, 33); memcpy(fs->gsm + 33, f->data + len, 33);
conv66(fs->gsm, msdata); conv66(fs->gsm, msdata);
if ((res = write(fs->fd, msdata, 65)) != 65) { if ((res = fwrite(msdata, 1, 65, fs->f)) != 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;
} }
update_header(fs->fd); update_header(fs->f);
} else { } else {
/* Copy the data and do nothing */ /* Copy the data and do nothing */
memcpy(fs->gsm, f->data + len, 33); memcpy(fs->gsm, f->data + len, 33);
@@ -496,8 +498,9 @@ static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
{ {
off_t offset=0,distance,cur,min,max; off_t offset=0,distance,cur,min,max;
min = 60; min = 60;
cur = lseek(fs->fd, 0, SEEK_CUR); cur = ftell(fs->f);
max = lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
/* I'm getting sloppy here, I'm only going to go to even splits of the 2 /* I'm getting sloppy here, I'm only going to go to even splits of the 2
* frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */ * frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
distance = (sample_offset/320) * 65; distance = (sample_offset/320) * 65;
@@ -513,26 +516,26 @@ static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
offset = (offset > max)?max:offset; offset = (offset > max)?max:offset;
} else if (offset > max) { } else if (offset > max) {
int i; int i;
lseek(fs->fd, 0, SEEK_END); fseek(fs->f, 0, SEEK_END);
for (i=0; i< (offset - max) / 65; i++) { for (i=0; i< (offset - max) / 65; i++) {
write(fs->fd, msgsm_silence, 65); fwrite(msgsm_silence, 1, 65, fs->f);
} }
} }
fs->secondhalf = 0; fs->secondhalf = 0;
return lseek(fs->fd, offset, SEEK_SET); return fseek(fs->f, offset, SEEK_SET);
} }
static int wav_trunc(struct ast_filestream *fs) static int wav_trunc(struct ast_filestream *fs)
{ {
if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR))) if (ftruncate(fileno(fs->f), ftell(fs->f)))
return -1; return -1;
return update_header(fs->fd); return update_header(fs->f);
} }
static long wav_tell(struct ast_filestream *fs) static long wav_tell(struct ast_filestream *fs)
{ {
off_t offset; off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR); offset = ftell(fs->f);
/* since this will most likely be used later in play or record, lets stick /* since this will most likely be used later in play or record, lets stick
* to that level of resolution, just even frames boundaries */ * to that level of resolution, just even frames boundaries */
return (offset - 52)/65*320; return (offset - 52)/65*320;

View File

@@ -766,6 +766,9 @@ struct ast_channel *ast_get_channel_by_name_locked(const char *chan);
/*! Get channel by name prefix (locks channel) */ /*! Get channel by name prefix (locks channel) */
struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen); struct ast_channel *ast_get_channel_by_name_prefix_locked(const char *name, const int namelen);
/*! Get channel by name prefix (locks channel) */
struct ast_channel *ast_walk_channel_by_name_prefix_locked(struct ast_channel *chan, const char *name, const int namelen);
/*--- ast_get_channel_by_exten_locked: Get channel by exten (and optionally context) and lock it */ /*--- ast_get_channel_by_exten_locked: Get channel by exten (and optionally context) and lock it */
struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context); struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context);

View File

@@ -49,8 +49,8 @@ struct ast_filestream;
* returns 0 on success, -1 on failure * returns 0 on success, -1 on failure
*/ */
int ast_format_register(const char *name, const char *exts, int format, int ast_format_register(const char *name, const char *exts, int format,
struct ast_filestream * (*open)(int fd), struct ast_filestream * (*open)(FILE *f),
struct ast_filestream * (*rewrite)(int fd, const char *comment), struct ast_filestream * (*rewrite)(FILE *f, const char *comment),
int (*write)(struct ast_filestream *, struct ast_frame *), int (*write)(struct ast_filestream *, struct ast_frame *),
int (*seek)(struct ast_filestream *, long offset, int whence), int (*seek)(struct ast_filestream *, long offset, int whence),
int (*trunc)(struct ast_filestream *), int (*trunc)(struct ast_filestream *),