/* Format of frames it uses/provides (one only) */
int format;
/* 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 */
- struct ast_filestream * (*rewrite)(int fd, const char *comment);
+ struct ast_filestream * (*rewrite)(FILE *f, const char *comment);
/* Write a frame to a channel */
int (*write)(struct ast_filestream *, struct ast_frame *);
/* seek num samples into file, whence(think normal seek) */
static struct ast_format *formats = NULL;
int ast_format_register(const char *name, const char *exts, int format,
- struct ast_filestream * (*open)(int fd),
- struct ast_filestream * (*rewrite)(int fd, const char *comment),
+ struct ast_filestream * (*open)(FILE *f),
+ struct ast_filestream * (*rewrite)(FILE *f, const char *comment),
int (*write)(struct ast_filestream *, struct ast_frame *),
int (*seek)(struct ast_filestream *, long sample_offset, int whence),
int (*trunc)(struct ast_filestream *),
struct ast_filestream *s;
int res=0, ret = 0;
char *ext=NULL, *exts, *fn, *nfn;
+ FILE *bfile;
struct ast_channel *chan = (struct ast_channel *)filename2;
/* Start with negative response */
case ACTION_OPEN:
if ((ret < 0) && ((chan->writeformat & f->format) ||
((f->format >= AST_FORMAT_MAX_AUDIO) && fmt))) {
- ret = open(fn, O_RDONLY);
- if (ret >= 0) {
- s = f->open(ret);
+ bfile = fopen(fn, "r");
+ if (bfile) {
+ ret = 1;
+ s = f->open(bfile);
if (s) {
s->lasttimeout = -1;
s->fmt = f;
else
chan->vstream = s;
} else {
- close(ret);
- ast_log(LOG_WARNING, "Unable to open fd on %s\n", fn);
+ fclose(bfile);
+ 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);
+ ret = -1;
+ }
}
break;
default:
set it up.
*/
- int fd = -1;
int fmts = -1;
char filename2[256]="";
char filename3[256];
/* Set the channel to a format we can work with */
res = ast_set_write_format(chan, fmts);
- fd = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
- if (fd >= 0)
+ res = ast_filehelper(filename2, (char *)chan, NULL, ACTION_OPEN);
+ if (res >= 0)
return chan->stream;
return NULL;
}
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_filestream *fs = NULL;
char *fn;
continue;
fn = build_filename(filename, type);
- fd = open(fn, flags);
- if (fd >= 0) {
+ bfile = fopen(fn, "r");
+ if (bfile) {
errno = 0;
- if (!(fs = f->open(fd))) {
+ if (!(fs = f->open(bfile))) {
ast_log(LOG_WARNING, "Unable to open %s\n", fn);
- close(fd);
+ fclose(bfile);
free(fn);
continue;
}
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;
+ FILE *bfile;
struct ast_format *f;
struct ast_filestream *fs = NULL;
char *fn, *orig_fn = NULL;
fn = build_filename(filename, type);
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) {
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.
What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
free(fn);
fn = buf;
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) {
errno = 0;
- if ((fs = f->rewrite(fd, comment))) {
+ if ((fs = f->rewrite(bfile, comment))) {
fs->trans = NULL;
fs->fmt = f;
fs->flags = flags;
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */
- int fd; /* Descriptor */
+ FILE *f; /* Descriptor */
struct ast_channel *owner;
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
#endif
-static int check_header(int fd)
+static int check_header(FILE *f)
{
AU_HEADER(header);
u_int32_t magic;
u_int32_t sample_rate;
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");
return -1;
}
return -1;
}
/* Skip to data */
- data_size = lseek(fd, 0, SEEK_END) - hdr_size;
- if (lseek(fd, hdr_size, SEEK_SET) == -1 ) {
+ fseek(f, 0, SEEK_END);
+ 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);
return -1;
}
return data_size;
}
-static int update_header(int fd)
+static int update_header(FILE *f)
{
off_t cur, end;
u_int32_t datalen;
int bytes;
- cur = lseek(fd, 0, SEEK_CUR);
- end = lseek(fd, 0, SEEK_END);
+ cur = ftell(f);
+ fseek(f, 0, SEEK_END);
+ end = ftell(f);
/* data starts 24 bytes in */
bytes = end - AU_HEADER_SIZE;
datalen = htoll(bytes);
ast_log(LOG_WARNING, "Unable to find our position\n");
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");
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");
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");
return -1;
}
return 0;
}
-static int write_header(int fd)
+static int write_header(FILE *f)
{
AU_HEADER(header);
header[AU_HDR_CHANNELS_OFF] = htoll(1);
/* Write an au header, ignoring sizes which will be filled in later */
- lseek(fd, 0, SEEK_SET);
- if (write(fd, header, AU_HEADER_SIZE) != AU_HEADER_SIZE) {
+ fseek(f, 0, SEEK_SET);
+ if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
return 0;
}
-static struct ast_filestream *au_open(int fd)
+static struct ast_filestream *au_open(FILE *f)
{
struct ast_filestream *tmp;
}
memset(tmp, 0, sizeof(struct ast_filestream));
- if (check_header(fd) < 0) {
+ if (check_header(f) < 0) {
free(tmp);
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ULAW;
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;
}
memset(tmp, 0, sizeof(struct ast_filestream));
- if (write_header(fd)) {
+ if (write_header(f)) {
free(tmp);
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
localusecnt++;
ast_mutex_unlock(&au_lock);
ast_update_use_count();
localusecnt--;
ast_mutex_unlock(&au_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
}
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
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));
return -1;
}
- update_header(fs->fd);
+ update_header(fs->f);
return 0;
}
samples = sample_offset;
min = AU_HEADER_SIZE;
- 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);
if (whence == SEEK_SET)
offset = samples + min;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
}
/* always protect the header space. */
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)
{
- if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
+ if (ftruncate(fileno(fs->f), ftell(fs->f)))
return -1;
- return update_header(fs->fd);
+ return update_header(fs->f);
}
static long au_tell(struct ast_filestream *fs)
{
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f);
return offset - AU_HEADER_SIZE;
}
/* First entry MUST be reserved for the channel type */
void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */
- int fd; /* Descriptor */
+ FILE *f; /* Descriptor */
struct ast_filestream *next;
struct ast_frame *fr; /* Frame representation of buf */
struct timeval orig; /* Original frame time */
static char *desc = "G.723.1 Simple Timestamp File Format";
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr = (struct ast_frame *)tmp->buf;
tmp->fr->data = tmp->buf + sizeof(struct ast_frame);
tmp->fr->frametype = AST_FRAME_VOICE;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&g723_lock);
ast_update_use_count();
int res;
int delay;
/* 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);
else
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
go ahead and stop the stream */
return NULL;
s->fr->offset = AST_FRIENDLY_OFFSET;
s->fr->datalen = size;
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));
return NULL;
}
glistcnt--;
ast_mutex_unlock(&g723_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen);
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));
return -1;
}
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));
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));
return -1;
}
static int g723_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;
}
/* Do not place anything before "reserved" */
void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */
- int fd; /* Open file descriptor */
+ FILE *f; /* Open file descriptor */
int rate; /* RATE_* defines */
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
/*
* 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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_40;
tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_32;
tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_24;
tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_16;
tmp->fr.data = tmp->g726;
tmp->fr.frametype = AST_FRAME_VOICE;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_40;
glistcnt++;
if (option_debug)
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_32;
glistcnt++;
if (option_debug)
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_24;
glistcnt++;
if (option_debug)
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->rate = RATE_16;
glistcnt++;
if (option_debug)
ast_log(LOG_DEBUG, "Closed filestream G.726-%dk.\n", 40 - s->rate * 8);
ast_mutex_unlock(&g726_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
s->fr.datalen = frame_size[s->rate];
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
f->datalen, frame_size[fs->rate]);
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, frame_size[fs->rate], strerror(errno));
return -1;
/* 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 */
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
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;
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
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&g729_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&g729_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
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;
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;
}
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)
}
/* 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;
}
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;
}
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;
}
/* 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 */
static char *desc = "Raw GSM data";
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->gsm;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_GSM;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&gsm_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&gsm_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
}
s->fr.datalen = 33;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
int len=0;
while(len < f->datalen) {
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));
return -1;
}
ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\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/33): %s\n", res, strerror(errno));
return -1;
}
off_t offset=0,min,cur,max,distance;
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);
/* have to fudge to frame here, so not fully to sample */
distance = (sample_offset/160) * 33;
if(whence == SEEK_SET)
offset = (offset > max)?max:offset;
} else if (offset > max) {
int i;
- lseek(fs->fd, 0, SEEK_END);
+ fseek(fs->f, 0, SEEK_END);
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)
{
- 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)
{
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f);
return (offset/33)*160;
}
/* 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 */
unsigned int lastts;
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
static char *desc = "Raw h263 data";
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
if we did, it would go here. We also might want to check
struct ast_filestream *tmp;
unsigned int ts;
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");
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->h263;
tmp->fr.frametype = AST_FRAME_VIDEO;
tmp->fr.subclass = AST_FORMAT_H263;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&h263_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&h263_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0;
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;
}
len = ntohs(len);
if (len > sizeof(s->h263)) {
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
s->fr.subclass |= mark;
s->fr.delivery.tv_sec = 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);
*whennext = s->lastts * 4/45;
} else
return -1;
}
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));
return -1;
}
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));
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));
return -1;
}
static int h263_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;
}
static long h263_tell(struct ast_filestream *fs)
{
+ /* XXX This is totally bogus XXX */
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f);
return (offset/20)*160;
}
/* 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 */
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
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;
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
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&ilbc_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&ilbc_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
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;
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;
}
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)
}
/* 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;
}
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;
}
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;
}
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
- int fd;
+ FILE *f;
/* structures for handling the Ogg container */
ogg_sync_state oy;
/*!
* \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.
*/
-static struct ast_filestream *ogg_vorbis_open(int fd)
+static struct ast_filestream *ogg_vorbis_open(FILE *f)
{
int i;
int bytes;
memset(tmp, 0, sizeof(struct ast_filestream));
tmp->writing = 0;
- tmp->fd = fd;
+ tmp->f = f;
ogg_sync_init(&tmp->oy);
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);
result = ogg_sync_pageout(&tmp->oy, &tmp->og);
} else {
ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n");
}
- close(fd);
+ fclose(f);
ogg_sync_clear(&tmp->oy);
free(tmp);
return NULL;
if(ogg_stream_pagein(&tmp->os, &tmp->og) < 0) {
ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi);
if(ogg_stream_packetout(&tmp->os, &tmp->op) != 1) {
ast_log(LOG_ERROR, "Error reading initial header packet.\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi);
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");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi);
break;
if(result < 0) {
ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi);
}
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) {
ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_comment_clear(&tmp->vc);
vorbis_info_clear(&tmp->vi);
if(tmp->vi.rate != 8000) {
ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_block_clear(&tmp->vb);
vorbis_dsp_clear(&tmp->vd);
if(ast_mutex_lock(&ogg_vorbis_lock)) {
ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_block_clear(&tmp->vb);
vorbis_dsp_clear(&tmp->vd);
/*!
* \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.
* \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_comm;
memset(tmp, 0, sizeof(struct ast_filestream));
tmp->writing = 1;
- tmp->fd = fd;
+ tmp->f = f;
vorbis_info_init(&tmp->vi);
while(!tmp->eos) {
if(ogg_stream_flush(&tmp->os, &tmp->og) == 0)
break;
- write(tmp->fd, tmp->og.header, tmp->og.header_len);
- write(tmp->fd, tmp->og.body, tmp->og.body_len);
+ fwrite(tmp->og.header, 1, tmp->og.header_len, tmp->f);
+ fwrite(tmp->og.body, 1, tmp->og.body_len, tmp->f);
if(ogg_page_eos(&tmp->og))
tmp->eos = 1;
}
if(ast_mutex_lock(&ogg_vorbis_lock)) {
ast_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n");
- close(fd);
+ fclose(f);
ogg_stream_clear(&tmp->os);
vorbis_block_clear(&tmp->vb);
vorbis_dsp_clear(&tmp->vd);
if(ogg_stream_pageout(&s->os, &s->og) == 0) {
break;
}
- write(s->fd, s->og.header, s->og.header_len);
- write(s->fd, s->og.body, s->og.body_len);
+ fwrite(s->og.header, 1, s->og.header_len, s->f);
+ fwrite(s->og.body, 1, s->og.body_len, s->f);
if(ogg_page_eos(&s->og)) {
s->eos = 1;
}
ogg_sync_clear(&s->oy);
}
- close(s->fd);
+ fclose(s->f);
free(s);
}
/* get a buffer from OGG to read the data into */
buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE);
/* 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 */
ogg_sync_wrote(&s->oy, bytes);
if(bytes == 0) {
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */
- int fd; /* Descriptor */
+ FILE *f; /* Descriptor */
struct ast_channel *owner;
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
static char *desc = "Raw uLaw 8khz Audio support (PCM)";
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ULAW;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&pcm_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&pcm_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
ast_log(LOG_WARNING, "Asked to write non-ulaw frame (%d)!\n", f->subclass);
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));
return -1;
}
off_t offset=0,min,cur,max;
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);
if (whence == SEEK_SET)
offset = sample_offset;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
}
/* always protect against seeking past begining. */
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)
{
- 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)
{
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f);
return offset;
}
/* 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 */
}
#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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ALAW;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
#ifdef REALTIME_WRITE
tmp->start_time = get_time();
#endif
glistcnt--;
ast_mutex_unlock(&pcm_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
/* 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.
*/
- 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 */
}
- if( stat_buf.st_size < fpos )
- {
+ if (stat_buf.st_size < fpos) {
/* fill the gap with 0x55 rather than 0. */
char buf[ 512 ];
unsigned long cur, to_write;
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) );
return -1;
}
- memset( buf, 0x55, 512 );
- while( cur < fpos )
- {
+ memset(buf, 0x55, 512);
+ while (cur < fpos) {
to_write = fpos - cur;
- if( to_write > 512 )
- {
+ if (to_write > 512) {
to_write = 512;
}
- write( fs->fd, buf, to_write );
+ fwrite(buf, 1, to_write, fs->f);
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) );
return -1;
}
#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));
return -1;
}
off_t offset=0,min,cur,max;
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);
if (whence == SEEK_SET)
offset = sample_offset;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
}
/* Always protect against seeking past begining */
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)
{
- 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)
{
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f);
return offset;
}
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */
- int fd; /* Descriptor */
+ FILE *f; /* Descriptor */
struct ast_channel *owner;
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
static char *desc = "Raw Signed Linear Audio support (SLN)";
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_SLINEAR;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&slinear_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&slinear_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
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));
return -1;
}
min = 0;
sample_offset <<= 1;
- 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);
if (whence == SEEK_SET)
offset = sample_offset;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
}
/* always protect against seeking past begining. */
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)
{
- 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)
{
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f);
return offset / 2;
}
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
/* 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 */
static char *desc = "Dialogic VOX (ADPCM) File Format";
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_ADPCM;
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
if we did, it would go here. We also might want to check
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&vox_lock);
ast_update_use_count();
glistcnt--;
ast_mutex_unlock(&vox_lock);
ast_update_use_count();
- close(s->fd);
+ fclose(s->f);
free(s);
s = NULL;
}
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.mallocd = 0;
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)
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
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));
return -1;
}
off_t offset=0,min,cur,max,distance;
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);
+
/* have to fudge to frame here, so not fully to sample */
distance = sample_offset/2;
if(whence == SEEK_SET)
offset = (offset > max)?max: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)
{
- 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)
{
off_t offset;
- offset = lseek(fs->fd, 0, SEEK_CUR);
+ offset = ftell(fs->f) << 1;
return offset;
}
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
/* This is what a filestream means to us */
- int fd; /* Descriptor */
+ FILE *f; /* Descriptor */
int bytes;
int needsgain;
struct ast_frame fr; /* Frame information */
#endif
-static int check_header(int fd)
+static int check_header(FILE *f)
{
int type, size, formtype;
int fmt, hsize;
int bysec;
int freq;
int data;
- if (read(fd, &type, 4) != 4) {
+ if (fread(&type, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (type)\n");
return -1;
}
- if (read(fd, &size, 4) != 4) {
+ if (fread(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (size)\n");
return -1;
}
size = ltohl(size);
- if (read(fd, &formtype, 4) != 4) {
+ if (fread(&formtype, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1;
}
ast_log(LOG_WARNING, "Does not contain WAVE\n");
return -1;
}
- if (read(fd, &fmt, 4) != 4) {
+ if (fread(&fmt, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fmt)\n");
return -1;
}
ast_log(LOG_WARNING, "Does not say fmt\n");
return -1;
}
- if (read(fd, &hsize, 4) != 4) {
+ if (fread(&hsize, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1;
}
ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
return -1;
}
- if (read(fd, &format, 2) != 2) {
+ if (fread(&format, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n");
return -1;
}
ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
return -1;
}
- if (read(fd, &chans, 2) != 2) {
+ if (fread(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n");
return -1;
}
ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
return -1;
}
- if (read(fd, &freq, 4) != 4) {
+ if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (freq)\n");
return -1;
}
return -1;
}
/* 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");
return -1;
}
/* 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");
return -1;
}
ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
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));
return -1;
}
/* 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 );
return -1;
}
/* Skip any facts and get the first data block */
for(;;)
{
- char buf[4];
+ char buf[4];
/* Begin data chunk */
- if (read(fd, &buf, 4) != 4) {
- ast_log(LOG_WARNING, "Read failed (data)\n");
- return -1;
+ if (fread(&buf, 1, 4, f) != 4) {
+ ast_log(LOG_WARNING, "Read failed (data)\n");
+ return -1;
}
/* Data has the actual length of data in it */
- if (read(fd, &data, 4) != 4) {
- ast_log(LOG_WARNING, "Read failed (data)\n");
- return -1;
+ if (fread(&data, 1, 4, f) != 4) {
+ ast_log(LOG_WARNING, "Read failed (data)\n");
+ return -1;
}
data = ltohl(data);
- if( memcmp(buf, "data", 4) == 0 ) break;
- if( memcmp(buf, "fact", 4) != 0 ) {
- ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
- return -1;
+ if(memcmp(buf, "data", 4) == 0 )
+ break;
+ if(memcmp(buf, "fact", 4) != 0 ) {
+ ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
+ return -1;
}
- if ( lseek(fd,data,SEEK_CUR) == -1 ) {
- ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
- return -1;
+ if (fseek(f,data,SEEK_CUR) == -1 ) {
+ ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
+ return -1;
}
}
#if 0
return data;
}
-static int update_header(int fd)
+static int update_header(FILE *f)
{
off_t cur,end;
int datalen,filelen,bytes;
- cur = lseek(fd, 0, SEEK_CUR);
- end = lseek(fd, 0, SEEK_END);
+ cur = ftell(f);
+ fseek(f, 0, SEEK_END);
+ end = ftell(f);
/* data starts 44 bytes in */
bytes = end - 44;
datalen = htoll(bytes);
ast_log(LOG_WARNING, "Unable to find our position\n");
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");
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");
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");
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");
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");
return -1;
}
return 0;
}
-static int write_header(int fd)
+static int write_header(FILE *f)
{
unsigned int hz=htoll(8000);
unsigned int bhz = htoll(16000);
unsigned short bisam = htols(16);
unsigned int size = htoll(0);
/* Write a wav header, ignoring sizes which will be filled in later */
- lseek(fd,0,SEEK_SET);
- if (write(fd, "RIFF", 4) != 4) {
+ fseek(f,0,SEEK_SET);
+ if (fwrite("RIFF", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &size, 4) != 4) {
+ if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, "WAVEfmt ", 8) != 8) {
+ if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &hs, 4) != 4) {
+ if (fwrite(&hs, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &fmt, 2) != 2) {
+ if (fwrite(&fmt, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &chans, 2) != 2) {
+ if (fwrite(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &hz, 4) != 4) {
+ if (fwrite(&hz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &bhz, 4) != 4) {
+ if (fwrite(&bhz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &bysam, 2) != 2) {
+ if (fwrite(&bysam, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &bisam, 2) != 2) {
+ if (fwrite(&bisam, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, "data", 4) != 4) {
+ if (fwrite("data", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &size, 4) != 4) {
+ if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
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
if we did, it would go here. We also might want to check
struct ast_filestream *tmp;
if ((tmp = malloc(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);
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->needsgain = 1;
tmp->fr.data = tmp->buf;
tmp->fr.frametype = AST_FRAME_VOICE;
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
if we did, it would go here. We also might want to check
struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream));
- if (write_header(fd)) {
+ if (write_header(f)) {
free(tmp);
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&wav_lock);
ast_update_use_count();
ast_update_use_count();
/* Pad to even length */
if (s->bytes & 0x1)
- write(s->fd, &zero, 1);
- close(s->fd);
+ fwrite(&zero, 1, 1, s->f);
+ fclose(s->f);
free(s);
s = NULL;
}
int bytes = sizeof(tmp);
off_t here;
/* 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)
bytes = s->maxlen - here;
if (bytes < 0)
bytes = 0;
/* 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) {
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
}
#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));
return -1;
}
}
fs->bytes += f->datalen;
- update_header(fs->fd);
+ update_header(fs->f);
return 0;
samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
min = 44; /* wav header is 44 bytes */
- 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);
if (whence == SEEK_SET)
offset = samples + min;
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
}
/* always protect the header space. */
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)
{
- if(ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)))
+ if (ftruncate(fileno(fs->f), ftell(fs->f)))
return -1;
- return update_header(fs->fd);
+ return update_header(fs->f);
}
static long wav_tell(struct ast_filestream *fs)
{
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 */
return (offset - 44)/2;
}
/* 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 */
#endif
-static int check_header(int fd)
+static int check_header(FILE *f)
{
int type, size, formtype;
int fmt, hsize, fact;
short format, chans;
int freq;
int data;
- if (read(fd, &type, 4) != 4) {
+ if (fread(&type, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (type)\n");
return -1;
}
- if (read(fd, &size, 4) != 4) {
+ if (fread(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (size)\n");
return -1;
}
size = ltohl(size);
- if (read(fd, &formtype, 4) != 4) {
+ if (fread(&formtype, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1;
}
ast_log(LOG_WARNING, "Does not contain WAVE\n");
return -1;
}
- if (read(fd, &fmt, 4) != 4) {
+ if (fread(&fmt, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fmt)\n");
return -1;
}
ast_log(LOG_WARNING, "Does not say fmt\n");
return -1;
}
- if (read(fd, &hsize, 4) != 4) {
+ if (fread(&hsize, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (formtype)\n");
return -1;
}
ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
return -1;
}
- if (read(fd, &format, 2) != 2) {
+ if (fread(&format, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n");
return -1;
}
ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
return -1;
}
- if (read(fd, &chans, 2) != 2) {
+ if (fread(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Read failed (format)\n");
return -1;
}
ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
return -1;
}
- if (read(fd, &freq, 4) != 4) {
+ if (fread(&freq, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (freq)\n");
return -1;
}
return -1;
}
/* 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");
return -1;
}
/* 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");
return -1;
}
/* 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");
return -1;
}
/* 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");
return -1;
}
return -1;
}
/* 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");
return -1;
}
- if (read(fd, &fact, 4) != 4) {
+ if (fread(&fact, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Read failed (fact value)\n");
return -1;
}
/* 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");
return -1;
}
return -1;
}
/* 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");
return -1;
}
return 0;
}
-static int update_header(int fd)
+static int update_header(FILE *f)
{
off_t cur,end,bytes;
int datalen,filelen;
- cur = lseek(fd, 0, SEEK_CUR);
- end = lseek(fd, 0, SEEK_END);
+ cur = ftell(f);
+ fseek(f, 0, SEEK_END);
+ end = ftell(f);
/* in a gsm WAV, data starts 60 bytes in */
bytes = end - 60;
datalen = htoll((bytes + 1) & ~0x1);
ast_log(LOG_WARNING, "Unable to find our position\n");
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");
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");
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");
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");
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");
return -1;
}
return 0;
}
-static int write_header(int fd)
+static int write_header(FILE *f)
{
unsigned int hz=htoll(8000);
unsigned int bhz = htoll(1625);
unsigned int y_1 = htoll(20160);
unsigned int size = htoll(0);
/* 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");
return -1;
}
- if (write(fd, &size, 4) != 4) {
+ if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, "WAVEfmt ", 8) != 8) {
+ if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &hs, 4) != 4) {
+ if (fwrite(&hs, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &fmt, 2) != 2) {
+ if (fwrite(&fmt, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &chans, 2) != 2) {
+ if (fwrite(&chans, 1, 2, f) != 2) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &hz, 4) != 4) {
+ if (fwrite(&hz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &bhz, 4) != 4) {
+ if (fwrite(&bhz, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
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");
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");
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");
return -1;
}
- if (write(fd, "fact", 4) != 4) {
+ if (fwrite("fact", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &fhs, 4) != 4) {
+ if (fwrite(&fhs, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
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");
return -1;
}
- if (write(fd, "data", 4) != 4) {
+ if (fwrite("data", 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
- if (write(fd, &size, 4) != 4) {
+ if (fwrite(&size, 1, 4, f) != 4) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
}
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
if we did, it would go here. We also might want to check
struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream));
- if (check_header(fd)) {
+ if (check_header(f)) {
free(tmp);
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
tmp->fr.data = tmp->gsm;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_GSM;
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
if we did, it would go here. We also might want to check
struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream));
- if (write_header(fd)) {
+ if (write_header(f)) {
free(tmp);
return NULL;
}
free(tmp);
return NULL;
}
- tmp->fd = fd;
+ tmp->f = f;
glistcnt++;
ast_mutex_unlock(&wav_lock);
ast_update_use_count();
ast_mutex_unlock(&wav_lock);
ast_update_use_count();
/* Pad to even length */
- if (lseek(s->fd, 0, SEEK_END) & 0x1)
- write(s->fd, &zero, 1);
- close(s->fd);
+ fseek(s->f, 0, SEEK_END);
+ if (ftell(s->f) & 0x1)
+ fwrite(&zero, 1, 1, s->f);
+ fclose(s->f);
free(s);
s = NULL;
}
/* Just return a frame based on the second GSM frame */
s->fr.data = s->gsm + 33;
} else {
- if ((res = read(s->fd, msdata, 65)) != 65) {
+ if ((res = fread(msdata, 1, 65, s->f)) != 65) {
if (res && (res != 1))
ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
return NULL;
while(len < f->datalen) {
if (alreadyms) {
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));
return -1;
}
- update_header(fs->fd);
+ update_header(fs->f);
len += 65;
} else {
if (fs->secondhalf) {
memcpy(fs->gsm + 33, f->data + len, 33);
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));
return -1;
}
- update_header(fs->fd);
+ update_header(fs->f);
} else {
/* Copy the data and do nothing */
memcpy(fs->gsm, f->data + len, 33);
{
off_t offset=0,distance,cur,min,max;
min = 60;
- 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);
/* 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 */
distance = (sample_offset/320) * 65;
offset = (offset > max)?max:offset;
} else if (offset > max) {
int i;
- lseek(fs->fd, 0, SEEK_END);
+ fseek(fs->f, 0, SEEK_END);
for (i=0; i< (offset - max) / 65; i++) {
- write(fs->fd, msgsm_silence, 65);
+ fwrite(msgsm_silence, 1, 65, fs->f);
}
}
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)
{
- if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
+ if (ftruncate(fileno(fs->f), ftell(fs->f)))
return -1;
- return update_header(fs->fd);
+ return update_header(fs->f);
}
static long wav_tell(struct ast_filestream *fs)
{
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
* to that level of resolution, just even frames boundaries */
return (offset - 52)/65*320;
/*! Get channel by name prefix (locks channel) */
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 */
struct ast_channel *ast_get_channel_by_exten_locked(const char *exten, const char *context);
* returns 0 on success, -1 on failure
*/
int ast_format_register(const char *name, const char *exts, int format,
- struct ast_filestream * (*open)(int fd),
- struct ast_filestream * (*rewrite)(int fd, const char *comment),
+ struct ast_filestream * (*open)(FILE *f),
+ struct ast_filestream * (*rewrite)(FILE *f, const char *comment),
int (*write)(struct ast_filestream *, struct ast_frame *),
int (*seek)(struct ast_filestream *, long offset, int whence),
int (*trunc)(struct ast_filestream *),