diff options
author | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2000-01-07 10:54:40 +0000 |
---|---|---|
committer | markster <markster@f38db490-d61c-443f-a65b-d21fe96a405b> | 2000-01-07 10:54:40 +0000 |
commit | e4b9f6a3431bc99c454ddda3a845629e832b7226 (patch) | |
tree | ed26e276bfaf05fc50c295d18015f65e3a1af2ad /frame.c | |
parent | 075980cea1ff2f36ae0372bb6db0de5c8168b090 (diff) |
Version 0.1.2 from FTP
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@198 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'frame.c')
-rwxr-xr-x | frame.c | 102 |
1 files changed, 97 insertions, 5 deletions
@@ -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; +} |