aboutsummaryrefslogtreecommitdiffstats
path: root/frame.c
diff options
context:
space:
mode:
author(no author) <(no author)@f38db490-d61c-443f-a65b-d21fe96a405b>2000-01-07 11:02:42 +0000
committer(no author) <(no author)@f38db490-d61c-443f-a65b-d21fe96a405b>2000-01-07 11:02:42 +0000
commit19c91752e018ddfdec305793389170c34bab05af (patch)
treeffd13e39bf481d25b687694fbe66ee4514da8041 /frame.c
parent758d7ac6f9c4db1450b46c6efd2f98280e193ca8 (diff)
This commit was manufactured by cvs2svn to create tag 'v0-1-2'.
git-svn-id: http://svn.digium.com/svn/asterisk/tags/v0-1-2@200 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'frame.c')
-rwxr-xr-xframe.c102
1 files changed, 97 insertions, 5 deletions
diff --git a/frame.c b/frame.c
index 856e7fd89..9e593bfbb 100755
--- a/frame.c
+++ b/frame.c
@@ -13,7 +13,9 @@
#include <asterisk/frame.h>
#include <asterisk/logger.h>
+#include <asterisk/options.h>
#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
/*
@@ -74,16 +76,106 @@ struct ast_frame *ast_frisolate(struct ast_frame *fr)
} else
out->src = fr->src;
if (!(fr->mallocd & AST_MALLOCD_DATA)) {
- out->data = malloc(fr->datalen + fr->offset);
- out->data += fr->offset;
- out->offset = fr->offset;
- out->datalen = fr->datalen;
- memcpy(out->data, fr->data, fr->datalen);
+ out->data = malloc(fr->datalen + AST_FRIENDLY_OFFSET);
if (!out->data) {
+ free(out);
ast_log(LOG_WARNING, "Out of memory\n");
return NULL;
}
+ out->data += AST_FRIENDLY_OFFSET;
+ out->offset = AST_FRIENDLY_OFFSET;
+ out->datalen = fr->datalen;
+ memcpy(out->data, fr->data, fr->datalen);
}
out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
return out;
}
+
+struct ast_frame *ast_frdup(struct ast_frame *f)
+{
+ struct ast_frame *ret;
+ int p;
+ p = f->mallocd;
+ f->mallocd = 0;
+ /* Make frisolate think this is a 100% static frame, and make a duplicate */
+ ret = ast_frisolate(f);
+ /* Restore its true malloc status */
+ f->mallocd = p;
+ return ret;
+}
+
+struct ast_frame *ast_fr_fdread(int fd)
+{
+ char buf[4096];
+ int res;
+ struct ast_frame *f = (struct ast_frame *)buf;
+ /* Read a frame directly from there. They're always in the
+ right format. */
+
+ if (read(fd, buf, sizeof(struct ast_frame))
+ == sizeof(struct ast_frame)) {
+ /* read the frame header */
+ f->mallocd = 0;
+ /* Re-write data position */
+ f->data = buf + sizeof(struct ast_frame);
+ f->offset = 0;
+ /* Forget about being mallocd */
+ f->mallocd = 0;
+ /* Re-write the source */
+ f->src = __FUNCTION__;
+ if (f->datalen > sizeof(buf) - sizeof(struct ast_frame)) {
+ /* Really bad read */
+ ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
+ return NULL;
+ }
+ if (f->datalen) {
+ if ((res = read(fd, f->data, f->datalen)) != f->datalen) {
+ /* Bad read */
+ ast_log(LOG_WARNING, "How very strange, expected %d, got %d\n", f->datalen, res);
+ return NULL;
+ }
+ }
+ return ast_frisolate(f);
+ } else if (option_debug)
+ ast_log(LOG_DEBUG, "NULL or invalid header\n");
+ /* Null if there was an error */
+ return NULL;
+}
+
+/* Some convenient routines for sending frames to/from stream or datagram
+ sockets, pipes, etc (maybe even files) */
+
+int ast_fr_fdwrite(int fd, struct ast_frame *frame)
+{
+ /* Write the frame exactly */
+ if (write(fd, frame, sizeof(struct ast_frame)) != sizeof(struct ast_frame)) {
+ ast_log(LOG_WARNING, "Write error\n");
+ return -1;
+ }
+ if (write(fd, frame->data, frame->datalen) != frame->datalen) {
+ ast_log(LOG_WARNING, "Write error\n");
+ return -1;
+ }
+ return 0;
+}
+
+int ast_getformatbyname(char *name)
+{
+ if (!strcasecmp(name, "g723.1"))
+ return AST_FORMAT_G723_1;
+ else if (!strcasecmp(name, "gsm"))
+ return AST_FORMAT_GSM;
+ else if (!strcasecmp(name, "ulaw"))
+ return AST_FORMAT_ULAW;
+ else if (!strcasecmp(name, "alaw"))
+ return AST_FORMAT_ALAW;
+ else if (!strcasecmp(name, "mp3"))
+ return AST_FORMAT_MP3;
+ else if (!strcasecmp(name, "slinear"))
+ return AST_FORMAT_SLINEAR;
+ else if (!strcasecmp(name, "lpc10"))
+ return AST_FORMAT_LPC10;
+ else if (!strcasecmp(name, "all"))
+ return 0x7FFFFFFF;
+ return 0;
+}