aboutsummaryrefslogtreecommitdiffstats
path: root/formats
diff options
context:
space:
mode:
authormarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2003-02-06 22:11:43 +0000
committermarkster <markster@f38db490-d61c-443f-a65b-d21fe96a405b>2003-02-06 22:11:43 +0000
commit8b1f06abb435a2aa00d7b5d1e8dd1588bf895483 (patch)
tree8220ad163713cb38240eb18c24be25aa88b28865 /formats
parent12b57158d66873fd353d3ae66662e205bd96debb (diff)
Version 0.3.0 from FTP
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@608 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'formats')
-rwxr-xr-xformats/format_g723.c29
-rwxr-xr-xformats/format_g729.c351
-rwxr-xr-xformats/format_gsm.c44
-rwxr-xr-xformats/format_pcm.c44
-rwxr-xr-xformats/format_vox.c29
-rwxr-xr-xformats/format_wav.c86
-rwxr-xr-xformats/format_wav_gsm.c63
7 files changed, 620 insertions, 26 deletions
diff --git a/formats/format_g723.c b/formats/format_g723.c
index 195815a46..520aeb7ef 100755
--- a/formats/format_g723.c
+++ b/formats/format_g723.c
@@ -150,6 +150,7 @@ static void g723_close(struct ast_filestream *s)
ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
close(s->fd);
free(s);
+ s = NULL;
}
static int ast_read_callback(void *data)
@@ -199,7 +200,7 @@ static int ast_read_callback(void *data)
else
s->fr->timelen = delay;
#else
- s->fr->timelen = 30;
+ s->fr->samples = 240;
#endif
/* Unless there is no delay, we're going to exit out as soon as we
have processed the current frame. */
@@ -229,9 +230,14 @@ static int ast_read_callback(void *data)
static int g723_apply(struct ast_channel *c, struct ast_filestream *s)
{
- u_int32_t delay;
/* Select our owner for this stream, and get the ball rolling. */
s->owner = c;
+ return 0;
+}
+
+static int g723_play(struct ast_filestream *s)
+{
+ u_int32_t delay;
/* Read and ignore the first delay */
if (read(s->fd, &delay, 4) != 4) {
/* Empty file */
@@ -296,6 +302,21 @@ static int g723_write(struct ast_filestream *fs, struct ast_frame *f)
return 0;
}
+static int g723_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ return -1;
+}
+
+static int g723_trunc(struct ast_filestream *fs)
+{
+ return -1;
+}
+
+static long g723_tell(struct ast_filestream *fs)
+{
+ return -1;
+}
+
static char *g723_getcomment(struct ast_filestream *s)
{
return NULL;
@@ -307,7 +328,11 @@ int load_module()
g723_open,
g723_rewrite,
g723_apply,
+ g723_play,
g723_write,
+ g723_seek,
+ g723_trunc,
+ g723_tell,
g723_read,
g723_close,
g723_getcomment);
diff --git a/formats/format_g729.c b/formats/format_g729.c
new file mode 100755
index 000000000..5d4aeab4a
--- /dev/null
+++ b/formats/format_g729.c
@@ -0,0 +1,351 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * Save to raw, headerless G729 data.
+ *
+ * Copyright (C) 1999, Mark Spencer
+ *
+ * Mark Spencer <markster@linux-support.net>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+#include <asterisk/lock.h>
+#include <asterisk/channel.h>
+#include <asterisk/file.h>
+#include <asterisk/logger.h>
+#include <asterisk/sched.h>
+#include <asterisk/module.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include <pthread.h>
+#include <endian.h>
+
+/* Some Ideas for this code came from makeg729e.c by Jeffery Chilton */
+
+/* Portions of the conversion code are by guido@sienanet.it */
+
+struct ast_filestream {
+ void *reserved[AST_RESERVED_POINTERS];
+ /* 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 */
+ struct ast_channel *owner;
+ struct ast_frame fr; /* Frame information */
+ char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
+ char empty; /* Empty character */
+ unsigned char g729[20]; /* Two Real G729 Frames */
+ int lasttimeout;
+ struct timeval last;
+ int adj;
+ struct ast_filestream *next;
+};
+
+
+static struct ast_filestream *glist = NULL;
+static pthread_mutex_t g729_lock = AST_MUTEX_INITIALIZER;
+static int glistcnt = 0;
+
+static char *name = "g729";
+static char *desc = "Raw G729 data";
+static char *exts = "g729";
+
+static struct ast_filestream *g729_open(int fd)
+{
+ /* 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
+ and be sure it's a valid file. */
+ struct ast_filestream *tmp;
+ if ((tmp = malloc(sizeof(struct ast_filestream)))) {
+ memset(tmp, 0, sizeof(struct ast_filestream));
+ if (ast_pthread_mutex_lock(&g729_lock)) {
+ ast_log(LOG_WARNING, "Unable to lock g729 list\n");
+ free(tmp);
+ return NULL;
+ }
+ tmp->next = glist;
+ glist = tmp;
+ tmp->fd = fd;
+ tmp->owner = NULL;
+ tmp->fr.data = tmp->g729;
+ tmp->fr.frametype = AST_FRAME_VOICE;
+ tmp->fr.subclass = AST_FORMAT_G729A;
+ /* datalen will vary for each frame */
+ tmp->fr.src = name;
+ tmp->fr.mallocd = 0;
+ tmp->lasttimeout = -1;
+ glistcnt++;
+ ast_pthread_mutex_unlock(&g729_lock);
+ ast_update_use_count();
+ }
+ return tmp;
+}
+
+static struct ast_filestream *g729_rewrite(int fd, 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
+ and be sure it's a valid file. */
+ struct ast_filestream *tmp;
+ if ((tmp = malloc(sizeof(struct ast_filestream)))) {
+ memset(tmp, 0, sizeof(struct ast_filestream));
+ if (ast_pthread_mutex_lock(&g729_lock)) {
+ ast_log(LOG_WARNING, "Unable to lock g729 list\n");
+ free(tmp);
+ return NULL;
+ }
+ tmp->next = glist;
+ glist = tmp;
+ tmp->fd = fd;
+ tmp->owner = NULL;
+ tmp->lasttimeout = -1;
+ glistcnt++;
+ ast_pthread_mutex_unlock(&g729_lock);
+ ast_update_use_count();
+ } else
+ ast_log(LOG_WARNING, "Out of memory\n");
+ return tmp;
+}
+
+static struct ast_frame *g729_read(struct ast_filestream *s)
+{
+ return NULL;
+}
+
+static void g729_close(struct ast_filestream *s)
+{
+ struct ast_filestream *tmp, *tmpl = NULL;
+ if (ast_pthread_mutex_lock(&g729_lock)) {
+ ast_log(LOG_WARNING, "Unable to lock g729 list\n");
+ return;
+ }
+ tmp = glist;
+ while(tmp) {
+ if (tmp == s) {
+ if (tmpl)
+ tmpl->next = tmp->next;
+ else
+ glist = tmp->next;
+ break;
+ }
+ tmpl = tmp;
+ tmp = tmp->next;
+ }
+ glistcnt--;
+ if (s->owner) {
+ s->owner->stream = NULL;
+ if (s->owner->streamid > -1)
+ ast_sched_del(s->owner->sched, s->owner->streamid);
+ s->owner->streamid = -1;
+ }
+ ast_pthread_mutex_unlock(&g729_lock);
+ ast_update_use_count();
+ if (!tmp)
+ ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
+ close(s->fd);
+ free(s);
+ s = NULL;
+}
+
+static int ast_read_callback(void *data)
+{
+ int retval = 0;
+ int res;
+ int delay = 20;
+ struct ast_filestream *s = data;
+ struct timeval tv;
+ /* Send a frame from the file to the appropriate channel */
+
+ s->fr.frametype = AST_FRAME_VOICE;
+ s->fr.subclass = AST_FORMAT_G729A;
+ s->fr.offset = AST_FRIENDLY_OFFSET;
+ s->fr.samples = 160;
+ s->fr.datalen = 20;
+ s->fr.mallocd = 0;
+ s->fr.data = s->g729;
+ if ((res = read(s->fd, s->g729, 20)) != 20) {
+ if (res)
+ ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+ s->owner->streamid = -1;
+ return 0;
+ }
+ /* Lastly, process the frame */
+ if (ast_write(s->owner, &s->fr)) {
+ ast_log(LOG_WARNING, "Failed to write frame\n");
+ s->owner->streamid = -1;
+ return 0;
+ }
+ if (s->last.tv_usec || s->last.tv_usec) {
+ int ms;
+ gettimeofday(&tv, NULL);
+ ms = 1000 * (tv.tv_sec - s->last.tv_sec) +
+ (tv.tv_usec - s->last.tv_usec) / 1000;
+ s->last.tv_sec = tv.tv_sec;
+ s->last.tv_usec = tv.tv_usec;
+ if ((ms - delay) * (ms - delay) > 4) {
+ /* Compensate if we're more than 2 ms off */
+ s->adj -= (ms - delay);
+ }
+#if 0
+ fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
+#endif
+ delay += s->adj;
+ if (delay < 1)
+ delay = 1;
+ } else
+ gettimeofday(&s->last, NULL);
+ if (s->lasttimeout != delay) {
+ /* We'll install the next timeout now. */
+ s->owner->streamid = ast_sched_add(s->owner->sched,
+ delay, ast_read_callback, s);
+ s->lasttimeout = delay;
+ } else {
+ /* Just come back again at the same time */
+ retval = -1;
+ }
+ return retval;
+}
+
+static int g729_apply(struct ast_channel *c, struct ast_filestream *s)
+{
+ /* Select our owner for this stream, and get the ball rolling. */
+ s->owner = c;
+ return 0;
+}
+
+static int g729_play(struct ast_filestream *s)
+{
+ ast_read_callback(s);
+ return 0;
+}
+
+static int g729_write(struct ast_filestream *fs, struct ast_frame *f)
+{
+ int res;
+ if (f->frametype != AST_FRAME_VOICE) {
+ ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
+ return -1;
+ }
+ if (f->subclass != AST_FORMAT_G729A) {
+ ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass);
+ return -1;
+ }
+ if (f->datalen % 20) {
+ ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 20\n", f->datalen);
+ return -1;
+ }
+ if ((res = write(fs->fd, f->data, f->datalen)) != f->datalen) {
+ ast_log(LOG_WARNING, "Bad write (%d/20): %s\n", res, strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
+static char *g729_getcomment(struct ast_filestream *s)
+{
+ return NULL;
+}
+
+static int g729_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ long bytes;
+ off_t min,cur,max,offset;
+ min = 0;
+ cur = lseek(fs->fd, 0, SEEK_CUR);
+ max = lseek(fs->fd, 0, SEEK_END);
+
+ bytes = 20 * (sample_offset / 160);
+ if(whence == SEEK_SET)
+ offset = bytes;
+ if(whence == SEEK_CUR)
+ offset = cur + bytes;
+ if(whence == SEEK_END)
+ offset = max - bytes;
+ offset = (offset > max)?max:offset;
+ offset = (offset < min)?min:offset;
+ if (lseek(fs->fd, 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)
+ return -1;
+ return 0;
+}
+
+static long g729_tell(struct ast_filestream *fs)
+{
+ off_t offset;
+ offset = lseek(fs->fd, 0, SEEK_CUR);
+ return (offset/20)*160;
+}
+
+int load_module()
+{
+ return ast_format_register(name, exts, AST_FORMAT_G729A,
+ g729_open,
+ g729_rewrite,
+ g729_apply,
+ g729_play,
+ g729_write,
+ g729_seek,
+ g729_trunc,
+ g729_tell,
+ g729_read,
+ g729_close,
+ g729_getcomment);
+
+
+}
+
+int unload_module()
+{
+ struct ast_filestream *tmp, *tmpl;
+ if (ast_pthread_mutex_lock(&g729_lock)) {
+ ast_log(LOG_WARNING, "Unable to lock g729 list\n");
+ return -1;
+ }
+ tmp = glist;
+ while(tmp) {
+ if (tmp->owner)
+ ast_softhangup(tmp->owner, AST_SOFTHANGUP_APPUNLOAD);
+ tmpl = tmp;
+ tmp = tmp->next;
+ free(tmpl);
+ }
+ ast_pthread_mutex_unlock(&g729_lock);
+ return ast_format_unregister(name);
+}
+
+int usecount()
+{
+ int res;
+ if (ast_pthread_mutex_lock(&g729_lock)) {
+ ast_log(LOG_WARNING, "Unable to lock g729 list\n");
+ return -1;
+ }
+ res = glistcnt;
+ ast_pthread_mutex_unlock(&g729_lock);
+ return res;
+}
+
+char *description()
+{
+ return desc;
+}
+
+
+char *key()
+{
+ return ASTERISK_GPL_KEY;
+}
diff --git a/formats/format_gsm.c b/formats/format_gsm.c
index 1ac981f7f..87bd91bc4 100755
--- a/formats/format_gsm.c
+++ b/formats/format_gsm.c
@@ -151,6 +151,7 @@ static void gsm_close(struct ast_filestream *s)
ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
close(s->fd);
free(s);
+ s = NULL;
}
static int ast_read_callback(void *data)
@@ -165,7 +166,7 @@ static int ast_read_callback(void *data)
s->fr.frametype = AST_FRAME_VOICE;
s->fr.subclass = AST_FORMAT_GSM;
s->fr.offset = AST_FRIENDLY_OFFSET;
- s->fr.timelen = 20;
+ s->fr.samples = 160;
s->fr.datalen = 33;
s->fr.mallocd = 0;
s->fr.data = s->gsm;
@@ -216,6 +217,11 @@ static int gsm_apply(struct ast_channel *c, struct ast_filestream *s)
{
/* Select our owner for this stream, and get the ball rolling. */
s->owner = c;
+ return 0;
+}
+
+static int gsm_play(struct ast_filestream *s)
+{
ast_read_callback(s);
return 0;
}
@@ -242,6 +248,38 @@ static int gsm_write(struct ast_filestream *fs, struct ast_frame *f)
return 0;
}
+static int gsm_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ off_t offset,min,cur,max,distance;
+
+ min = 0;
+ cur = lseek(fs->fd, 0, SEEK_CUR);
+ max = lseek(fs->fd, 0, SEEK_END);
+ /* have to fudge to frame here, so not fully to sample */
+ distance = (sample_offset/160) * 33;
+ if(whence == SEEK_SET)
+ offset = distance;
+ if(whence == SEEK_CUR)
+ offset = distance + cur;
+ if(whence == SEEK_END)
+ offset = max - distance;
+ offset = (offset > max)?max:offset;
+ offset = (offset < min)?min:offset;
+ return lseek(fs->fd, offset, SEEK_SET);
+}
+
+static int gsm_trunc(struct ast_filestream *fs)
+{
+ return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
+}
+
+static long gsm_tell(struct ast_filestream *fs)
+{
+ off_t offset;
+ offset = lseek(fs->fd, 0, SEEK_CUR);
+ return (offset/33)*160;
+}
+
static char *gsm_getcomment(struct ast_filestream *s)
{
return NULL;
@@ -253,7 +291,11 @@ int load_module()
gsm_open,
gsm_rewrite,
gsm_apply,
+ gsm_play,
gsm_write,
+ gsm_seek,
+ gsm_trunc,
+ gsm_tell,
gsm_read,
gsm_close,
gsm_getcomment);
diff --git a/formats/format_pcm.c b/formats/format_pcm.c
index 8576f6995..28daed68f 100755
--- a/formats/format_pcm.c
+++ b/formats/format_pcm.c
@@ -149,6 +149,7 @@ static void pcm_close(struct ast_filestream *s)
ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
close(s->fd);
free(s);
+ s = NULL;
}
static int ast_read_callback(void *data)
@@ -171,9 +172,9 @@ static int ast_read_callback(void *data)
s->owner->streamid = -1;
return 0;
}
- s->fr.timelen = res / 8;
+ s->fr.samples = res;
s->fr.datalen = res;
- delay = s->fr.timelen;
+ delay = s->fr.samples/8;
/* Lastly, process the frame */
if (ast_write(s->owner, &s->fr)) {
ast_log(LOG_WARNING, "Failed to write frame\n");
@@ -215,6 +216,11 @@ static int pcm_apply(struct ast_channel *c, struct ast_filestream *s)
{
/* Select our owner for this stream, and get the ball rolling. */
s->owner = c;
+ return 0;
+}
+
+static int pcm_play(struct ast_filestream *s)
+{
ast_read_callback(s);
return 0;
}
@@ -237,6 +243,36 @@ static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
return 0;
}
+static int pcm_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ off_t offset,min,cur,max;
+
+ min = 0;
+ cur = lseek(fs->fd, 0, SEEK_CUR);
+ max = lseek(fs->fd, 0, SEEK_END);
+ if(whence == SEEK_SET)
+ offset = sample_offset;
+ if(whence == SEEK_CUR)
+ offset = sample_offset + cur;
+ if(whence == SEEK_END)
+ offset = max - sample_offset;
+ offset = (offset > max)?max:offset;
+ offset = (offset < min)?min:offset;
+ return lseek(fs->fd, offset, SEEK_SET);
+}
+
+static int pcm_trunc(struct ast_filestream *fs)
+{
+ return ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR));
+}
+
+static long pcm_tell(struct ast_filestream *fs)
+{
+ off_t offset;
+ offset = lseek(fs->fd, 0, SEEK_CUR);
+ return offset;
+}
+
static char *pcm_getcomment(struct ast_filestream *s)
{
return NULL;
@@ -248,7 +284,11 @@ int load_module()
pcm_open,
pcm_rewrite,
pcm_apply,
+ pcm_play,
pcm_write,
+ pcm_seek,
+ pcm_trunc,
+ pcm_tell,
pcm_read,
pcm_close,
pcm_getcomment);
diff --git a/formats/format_vox.c b/formats/format_vox.c
index 94957e7d7..0a4c7c025 100755
--- a/formats/format_vox.c
+++ b/formats/format_vox.c
@@ -259,6 +259,7 @@ static void vox_close(struct ast_filestream *s)
ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
close(s->fd);
free(s);
+ s = NULL;
}
static int ast_read_callback(void *data)
@@ -291,9 +292,9 @@ static int ast_read_callback(void *data)
s->signal += decode(s->buf[x] >> 4, &s->ssindex);
s->signal += decode(s->buf[x] & 0xf, &s->ssindex);
}
- s->fr.timelen = res / 4;
+ s->fr.samples = res * 2;
s->fr.datalen = res + 3;
- delay = s->fr.timelen;
+ delay = s->fr.samples / 8;
/* Lastly, process the frame */
if (ast_write(s->owner, &s->fr)) {
ast_log(LOG_WARNING, "Failed to write frame\n");
@@ -335,6 +336,11 @@ static int vox_apply(struct ast_channel *c, struct ast_filestream *s)
{
/* Select our owner for this stream, and get the ball rolling. */
s->owner = c;
+ return 0;
+}
+
+static int vox_play(struct ast_filestream *s)
+{
ast_read_callback(s);
return 0;
}
@@ -366,13 +372,32 @@ static char *vox_getcomment(struct ast_filestream *s)
return NULL;
}
+static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ return -1;
+}
+
+static int vox_trunc(struct ast_filestream *fs)
+{
+ return -1;
+}
+
+static long vox_tell(struct ast_filestream *fs)
+{
+ return -1;
+}
+
int load_module()
{
return ast_format_register(name, exts, AST_FORMAT_ADPCM,
vox_open,
vox_rewrite,
vox_apply,
+ vox_play,
vox_write,
+ vox_seek,
+ vox_trunc,
+ vox_tell,
vox_read,
vox_close,
vox_getcomment);
diff --git a/formats/format_wav.c b/formats/format_wav.c
index 48a36ad1f..876714ad7 100755
--- a/formats/format_wav.c
+++ b/formats/format_wav.c
@@ -33,8 +33,6 @@
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
- /* 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 */
int bytes;
@@ -43,7 +41,7 @@ struct ast_filestream {
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
char empty; /* Empty character */
- short buf[160]; /* Two Real GSM Frames */
+ short buf[160];
int foffset;
int lasttimeout;
struct timeval last;
@@ -191,14 +189,20 @@ static int check_header(int fd)
return 0;
}
-static int update_header(int fd, int bytes)
+static int update_header(int fd)
{
- int cur;
- int datalen = htoll(bytes);
- /* int filelen = htoll(52 + ((bytes + 1) & ~0x1)); */
- int filelen = htoll(36 + bytes);
+ off_t cur,end;
+ int datalen,filelen,bytes;
+
cur = lseek(fd, 0, SEEK_CUR);
+ end = lseek(fd, 0, SEEK_END);
+ /* data starts 44 bytes in */
+ bytes = end - 44;
+ datalen = htoll(bytes);
+ /* chunk size is bytes of data plus 36 bytes of header */
+ filelen = htoll(36 + bytes);
+
if (cur < 0) {
ast_log(LOG_WARNING, "Unable to find our position\n");
return -1;
@@ -237,6 +241,7 @@ static int write_header(int fd)
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) {
ast_log(LOG_WARNING, "Unable to write header\n");
return -1;
@@ -396,6 +401,7 @@ static void wav_close(struct ast_filestream *s)
write(s->fd, &zero, 1);
close(s->fd);
free(s);
+ s = NULL;
#if 0
printf("bytes = %d\n", s->bytes);
#endif
@@ -418,6 +424,11 @@ static int ast_read_callback(void *data)
s->owner->streamid = -1;
return 0;
}
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+ for( x = 0; x < sizeof(tmp)/2; x++) tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
+#endif
+
if (s->needsgain) {
for (x=0;x<sizeof(tmp)/2;x++)
if (tmp[x] & ((1 << GAIN) - 1)) {
@@ -434,15 +445,15 @@ static int ast_read_callback(void *data)
memcpy(s->buf, tmp, sizeof(s->buf));
}
- delay = res / 16;
+ delay = res / 2;
s->fr.frametype = AST_FRAME_VOICE;
s->fr.subclass = AST_FORMAT_SLINEAR;
s->fr.offset = AST_FRIENDLY_OFFSET;
s->fr.datalen = res;
s->fr.data = s->buf;
s->fr.mallocd = 0;
- s->fr.timelen = delay;
-
+ s->fr.samples = delay;
+ delay /= 8;
/* Lastly, process the frame */
if (delay != s->lasttimeout) {
s->owner->streamid = ast_sched_add(s->owner->sched, delay, ast_read_callback, s);
@@ -465,6 +476,11 @@ static int wav_apply(struct ast_channel *c, struct ast_filestream *s)
{
/* Select our owner for this stream, and get the ball rolling. */
s->owner = c;
+ return 0;
+}
+
+static int wav_play(struct ast_filestream *s)
+{
ast_read_callback(s);
return 0;
}
@@ -480,7 +496,7 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
return -1;
}
if (f->subclass != AST_FORMAT_SLINEAR) {
- ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
+ ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
return -1;
}
if (f->datalen > sizeof(tmp)) {
@@ -505,6 +521,11 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
tmpf = -32768.0;
tmp[x] = tmpf;
tmp[x] &= ~((1 << GAIN) - 1);
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+ tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
+#endif
+
}
if ((write (fs->fd, tmp, f->datalen) != f->datalen) ) {
ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
@@ -516,12 +537,47 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
}
fs->bytes += f->datalen;
- update_header(fs->fd, fs->bytes);
+ update_header(fs->fd);
return 0;
}
+static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ off_t min,max,cur;
+ long offset,samples;
+
+ 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);
+ if(whence == SEEK_SET)
+ offset = samples + min;
+ if(whence == SEEK_CUR)
+ offset = samples + cur;
+ if(whence == SEEK_END)
+ offset = max - samples;
+ offset = (offset > max)?max:offset;
+ offset = (offset < min)?min:offset;
+ return lseek(fs->fd,offset,SEEK_SET);
+}
+
+static int wav_trunc(struct ast_filestream *fs)
+{
+ if(ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)))
+ return -1;
+ return update_header(fs->fd);
+}
+
+static long wav_tell(struct ast_filestream *fs)
+{
+ off_t offset;
+ offset = lseek(fs->fd, 0, SEEK_CUR);
+ /* subtract header size to get samples, then divide by 2 for 16 bit samples */
+ return (offset - 44)/2;
+}
+
static char *wav_getcomment(struct ast_filestream *s)
{
return NULL;
@@ -533,7 +589,11 @@ int load_module()
wav_open,
wav_rewrite,
wav_apply,
+ wav_play,
wav_write,
+ wav_seek,
+ wav_trunc,
+ wav_tell,
wav_read,
wav_close,
wav_getcomment);
diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c
index d7f718f6d..527a4014a 100755
--- a/formats/format_wav_gsm.c
+++ b/formats/format_wav_gsm.c
@@ -202,12 +202,16 @@ static int check_header(int fd)
return 0;
}
-static int update_header(int fd, int bytes)
+static int update_header(int fd)
{
- int cur;
- int datalen = htoll(bytes);
- int filelen = htoll(52 + ((bytes + 1) & ~0x1));
+ off_t cur,end,bytes;
+ int datalen,filelen;
+
cur = lseek(fd, 0, SEEK_CUR);
+ end = lseek(fd, 0, SEEK_END);
+ bytes = end - 52;
+ datalen = htoll(bytes);
+ filelen = htoll(52 + ((bytes + 1) & ~0x1));
if (cur < 0) {
ast_log(LOG_WARNING, "Unable to find our position\n");
return -1;
@@ -423,6 +427,7 @@ static void wav_close(struct ast_filestream *s)
write(s->fd, &zero, 1);
close(s->fd);
free(s);
+ s = NULL;
}
static int ast_read_callback(void *data)
@@ -438,7 +443,7 @@ static int ast_read_callback(void *data)
s->fr.frametype = AST_FRAME_VOICE;
s->fr.subclass = AST_FORMAT_GSM;
s->fr.offset = AST_FRIENDLY_OFFSET;
- s->fr.timelen = 20;
+ s->fr.samples = 160;
s->fr.datalen = 33;
s->fr.mallocd = 0;
if (s->secondhalf) {
@@ -497,6 +502,11 @@ static int wav_apply(struct ast_channel *c, struct ast_filestream *s)
{
/* Select our owner for this stream, and get the ball rolling. */
s->owner = c;
+ return 0;
+}
+
+static int wav_play(struct ast_filestream *s)
+{
ast_read_callback(s);
return 0;
}
@@ -523,7 +533,7 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
return -1;
}
fs->bytes += 65;
- update_header(fs->fd, fs->bytes);
+ update_header(fs->fd);
} else {
/* Copy the data and do nothing */
memcpy(fs->gsm, f->data + len, 33);
@@ -534,6 +544,43 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
return 0;
}
+static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
+{
+ off_t offset,distance,cur,min,max;
+ min = 52;
+ cur = lseek(fs->fd, 0, SEEK_CUR);
+ max = lseek(fs->fd, 0, SEEK_END);
+ /* 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;
+ if(whence == SEEK_SET)
+ offset = distance + min;
+ if(whence == SEEK_CUR)
+ offset = distance + cur;
+ if(whence == SEEK_END)
+ offset = max - distance;
+ offset = (offset < min)?min:offset;
+ offset = (offset > max)?max:offset;
+ fs->secondhalf = 0;
+ return lseek(fs->fd, offset, SEEK_SET);
+}
+
+static int wav_trunc(struct ast_filestream *fs)
+{
+ if(ftruncate(fs->fd, lseek(fs->fd, 0, SEEK_CUR)))
+ return -1;
+ return update_header(fs->fd);
+}
+
+static long wav_tell(struct ast_filestream *fs)
+{
+ off_t offset;
+ offset = lseek(fs->fd, 0, SEEK_CUR);
+ /* 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;
+}
+
static char *wav_getcomment(struct ast_filestream *s)
{
return NULL;
@@ -545,7 +592,11 @@ int load_module()
wav_open,
wav_rewrite,
wav_apply,
+ wav_play,
wav_write,
+ wav_seek,
+ wav_trunc,
+ wav_tell,
wav_read,
wav_close,
wav_getcomment);