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

View File

@@ -54,7 +54,7 @@ struct ast_filestream {
/* Believe it or not, we must decode/recode to account for the
weird MS format */
/* This is what a filestream means to us */
int fd; /* Descriptor */
FILE *f; /* Descriptor */
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
char empty; /* Empty character */
@@ -69,7 +69,7 @@ static char *name = "iLBC";
static char *desc = "Raw iLBC data";
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
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);
return NULL;
}
tmp->fd = fd;
tmp->f = f;
tmp->fr.data = tmp->ilbc;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ILBC;
@@ -96,7 +96,7 @@ static struct ast_filestream *ilbc_open(int fd)
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
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);
return NULL;
}
tmp->fd = fd;
tmp->f = f;
glistcnt++;
ast_mutex_unlock(&ilbc_lock);
ast_update_use_count();
@@ -127,7 +127,7 @@ static void ilbc_close(struct ast_filestream *s)
glistcnt--;
ast_mutex_unlock(&ilbc_lock);
ast_update_use_count();
close(s->fd);
fclose(s->f);
free(s);
s = NULL;
}
@@ -143,7 +143,7 @@ static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
s->fr.datalen = 50;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
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);
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));
return -1;
}
@@ -184,8 +184,9 @@ static int ilbc_seek(struct ast_filestream *fs, long sample_offset, int whence)
long bytes;
off_t min,cur,max,offset=0;
min = 0;
cur = lseek(fs->fd, 0, SEEK_CUR);
max = lseek(fs->fd, 0, SEEK_END);
cur = ftell(fs->f);
fseek(fs->f, 0, SEEK_END);
max = ftell(fs->f);
bytes = 50 * (sample_offset / 240);
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. */
offset = (offset < min)?min:offset;
if (lseek(fs->fd, offset, SEEK_SET) < 0)
if (fseek(fs->f, offset, SEEK_SET) < 0)
return -1;
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)
{
/* 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 0;
}
@@ -215,7 +216,7 @@ static int ilbc_trunc(struct ast_filestream *fs)
static long ilbc_tell(struct ast_filestream *fs)
{
off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR);
offset = ftell(fs->f);
return (offset/50)*240;
}