2 * Asterisk -- A telephony toolkit for Linux.
4 * Frame manipulation routines
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/frame.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/options.h>
22 * Important: I should be made more efficient. Frame headers should
23 * most definitely be cached
26 void ast_frfree(struct ast_frame *fr)
28 if (fr->mallocd & AST_MALLOCD_DATA) {
30 free(fr->data - fr->offset);
32 if (fr->mallocd & AST_MALLOCD_SRC) {
36 if (fr->mallocd & AST_MALLOCD_HDR) {
41 void ast_frchain(struct ast_frame_chain *fc)
43 struct ast_frame_chain *last;
53 struct ast_frame *ast_frisolate(struct ast_frame *fr)
55 struct ast_frame *out;
56 if (!(fr->mallocd & AST_MALLOCD_HDR)) {
57 /* Allocate a new header if needed */
58 out = malloc(sizeof(struct ast_frame));
60 ast_log(LOG_WARNING, "Out of memory\n");
63 out->frametype = fr->frametype;
64 out->subclass = fr->subclass;
66 out->timelen = fr->timelen;
73 if (!(fr->mallocd & AST_MALLOCD_SRC)) {
75 out->src = strdup(fr->src);
78 if (!(fr->mallocd & AST_MALLOCD_DATA)) {
79 out->data = malloc(fr->datalen + AST_FRIENDLY_OFFSET);
82 ast_log(LOG_WARNING, "Out of memory\n");
85 out->data += AST_FRIENDLY_OFFSET;
86 out->offset = AST_FRIENDLY_OFFSET;
87 out->datalen = fr->datalen;
88 memcpy(out->data, fr->data, fr->datalen);
90 out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
94 struct ast_frame *ast_frdup(struct ast_frame *f)
96 struct ast_frame *ret;
100 /* Make frisolate think this is a 100% static frame, and make a duplicate */
101 ret = ast_frisolate(f);
102 /* Restore its true malloc status */
107 struct ast_frame *ast_fr_fdread(int fd)
111 struct ast_frame *f = (struct ast_frame *)buf;
112 /* Read a frame directly from there. They're always in the
115 if (read(fd, buf, sizeof(struct ast_frame))
116 == sizeof(struct ast_frame)) {
117 /* read the frame header */
119 /* Re-write data position */
120 f->data = buf + sizeof(struct ast_frame);
122 /* Forget about being mallocd */
124 /* Re-write the source */
125 f->src = __FUNCTION__;
126 if (f->datalen > sizeof(buf) - sizeof(struct ast_frame)) {
127 /* Really bad read */
128 ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
132 if ((res = read(fd, f->data, f->datalen)) != f->datalen) {
134 ast_log(LOG_WARNING, "How very strange, expected %d, got %d\n", f->datalen, res);
138 return ast_frisolate(f);
139 } else if (option_debug)
140 ast_log(LOG_DEBUG, "NULL or invalid header\n");
141 /* Null if there was an error */
145 /* Some convenient routines for sending frames to/from stream or datagram
146 sockets, pipes, etc (maybe even files) */
148 int ast_fr_fdwrite(int fd, struct ast_frame *frame)
150 /* Write the frame exactly */
151 if (write(fd, frame, sizeof(struct ast_frame)) != sizeof(struct ast_frame)) {
152 ast_log(LOG_WARNING, "Write error\n");
155 if (write(fd, frame->data, frame->datalen) != frame->datalen) {
156 ast_log(LOG_WARNING, "Write error\n");
162 int ast_getformatbyname(char *name)
164 if (!strcasecmp(name, "g723.1"))
165 return AST_FORMAT_G723_1;
166 else if (!strcasecmp(name, "gsm"))
167 return AST_FORMAT_GSM;
168 else if (!strcasecmp(name, "ulaw"))
169 return AST_FORMAT_ULAW;
170 else if (!strcasecmp(name, "alaw"))
171 return AST_FORMAT_ALAW;
172 else if (!strcasecmp(name, "mp3"))
173 return AST_FORMAT_MP3;
174 else if (!strcasecmp(name, "slinear"))
175 return AST_FORMAT_SLINEAR;
176 else if (!strcasecmp(name, "lpc10"))
177 return AST_FORMAT_LPC10;
178 else if (!strcasecmp(name, "all"))