Version 0.1.8 from FTP
[asterisk/asterisk.git] / frame.c
diff --git a/frame.c b/frame.c
index 856e7fd..9f1c3dd 100755 (executable)
--- 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,108 @@ 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, "adpcm"))
+               return AST_FORMAT_ADPCM;
+       else if (!strcasecmp(name, "all"))
+               return 0x7FFFFFFF;
+       return 0;
+}