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

@@ -52,7 +52,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 */
@@ -67,7 +67,7 @@ static char *name = "g729";
static char *desc = "Raw G729 data";
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
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);
return NULL;
}
tmp->fd = fd;
tmp->f = f;
tmp->fr.data = tmp->g729;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_G729A;
@@ -94,7 +94,7 @@ static struct ast_filestream *g729_open(int fd)
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
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);
return NULL;
}
tmp->fd = fd;
tmp->f = f;
glistcnt++;
ast_mutex_unlock(&g729_lock);
ast_update_use_count();
@@ -125,7 +125,7 @@ static void g729_close(struct ast_filestream *s)
glistcnt--;
ast_mutex_unlock(&g729_lock);
ast_update_use_count();
close(s->fd);
fclose(s->f);
free(s);
s = NULL;
}
@@ -141,7 +141,7 @@ static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
s->fr.datalen = 20;
s->fr.mallocd = 0;
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))
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
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);
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));
return -1;
}
@@ -182,8 +182,9 @@ static int g729_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 = 20 * (sample_offset / 160);
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. */
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;
}
@@ -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)
{
/* 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;
}
@@ -213,7 +214,7 @@ static int g729_trunc(struct ast_filestream *fs)
static long g729_tell(struct ast_filestream *fs)
{
off_t offset;
offset = lseek(fs->fd, 0, SEEK_CUR);
offset = ftell(fs->f);
return (offset/20)*160;
}