Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2005, Digium, Inc.
* Mark Spencer <markster@digium.com>
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
*
*
* \author Mark Spencer <markster@digium.com>
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/_private.h"
Kevin P. Fleming
committed
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/channel.h"
#include "asterisk/cli.h"
#include "asterisk/term.h"
#include "asterisk/utils.h"
#include "asterisk/threadstorage.h"
#include "asterisk/linkedlists.h"
#include "asterisk/translate.h"
#if !defined(LOW_MEMORY)
static void frame_cache_cleanup(void *data);
/*! \brief A per-thread cache of frame headers */
Russell Bryant
committed
AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
/*!
* \brief Maximum ast_frame cache size
*
* In most cases where the frame header cache will be useful, the size
* of the cache will stay very small. However, it is not always the case that
* the same thread that allocates the frame will be the one freeing them, so
* sometimes a thread will never have any frames in its cache, or the cache
* will never be pulled from. For the latter case, we limit the maximum size.
*/
#define FRAME_CACHE_MAX_SIZE 10
/*! \brief This is just so ast_frames, a list head struct for holding a list of
* ast_frame structures, is defined. */
AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
struct ast_frame_cache {
struct ast_frames list;
size_t size;
};
enum frame_type {
TYPE_HIGH, /* 0x0 */
TYPE_LOW, /* 0x1 */
TYPE_SILENCE, /* 0x2 */
TYPE_DONTSEND /* 0x3 */
};
#define TYPE_MASK 0x3
int flags;
unsigned int opt_needs_swap:1;
struct timeval delivery;
char data[SMOOTHER_SIZE];
char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
/*! \brief Definition of supported media formats (codecs) */
Kevin P. Fleming
committed
static const struct ast_format_list AST_FORMAT_LIST[] = {
{ AST_FORMAT_G723_1 , "g723", 8000, "G.723.1", 20, 30, 300, 30, 30 }, /*!< G723.1 */
{ AST_FORMAT_GSM, "gsm", 8000, "GSM", 33, 20, 300, 20, 20 }, /*!< codec_gsm.c */
{ AST_FORMAT_ULAW, "ulaw", 8000, "G.711 u-law", 80, 10, 150, 10, 20 }, /*!< codec_ulaw.c */
{ AST_FORMAT_ALAW, "alaw", 8000, "G.711 A-law", 80, 10, 150, 10, 20 }, /*!< codec_alaw.c */
{ AST_FORMAT_G726, "g726", 8000, "G.726 RFC3551", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
{ AST_FORMAT_ADPCM, "adpcm" , 8000, "ADPCM", 40, 10, 300, 10, 20 }, /*!< codec_adpcm.c */
{ AST_FORMAT_SLINEAR, "slin", 8000, "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear */
{ AST_FORMAT_LPC10, "lpc10", 8000, "LPC10", 7, 20, 20, 20, 20 }, /*!< codec_lpc10.c */
{ AST_FORMAT_G729A, "g729", 8000, "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729 }, /*!< Binary commercial distribution */
{ AST_FORMAT_SPEEX, "speex", 8000, "SpeeX", 10, 10, 60, 10, 20 }, /*!< codec_speex.c */
{ AST_FORMAT_ILBC, "ilbc", 8000, "iLBC", 50, 30, 30, 30, 30 }, /*!< codec_ilbc.c */ /* inc=30ms - workaround */
{ AST_FORMAT_G726_AAL2, "g726aal2", 8000, "G.726 AAL2", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
{ AST_FORMAT_G722, "g722", 16000, "G722", 80, 10, 150, 10, 20 }, /*!< codec_g722.c */
Kevin P. Fleming
committed
{ AST_FORMAT_SLINEAR16, "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear (16kHz) */
{ AST_FORMAT_JPEG, "jpeg", 0, "JPEG image"}, /*!< See format_jpeg.c */
{ AST_FORMAT_PNG, "png", 0, "PNG image"}, /*!< PNG Image format */
{ AST_FORMAT_H261, "h261", 0, "H.261 Video" }, /*!< H.261 Video Passthrough */
{ AST_FORMAT_H263, "h263", 0, "H.263 Video" }, /*!< H.263 Passthrough support, see format_h263.c */
{ AST_FORMAT_H263_PLUS, "h263p", 0, "H.263+ Video" }, /*!< H.263plus passthrough support See format_h263.c */
{ AST_FORMAT_H264, "h264", 0, "H.264 Video" }, /*!< Passthrough support, see format_h263.c */
{ AST_FORMAT_MP4_VIDEO, "mpeg4", 0, "MPEG4 Video" }, /*!< Passthrough support for MPEG4 */
Kevin P. Fleming
committed
{ AST_FORMAT_T140RED, "red", 1, "T.140 Realtime Text with redundancy"}, /*!< Redundant T.140 Realtime Text */
{ AST_FORMAT_T140, "t140", 0, "Passthrough T.140 Realtime Text" }, /*!< Passthrough support for T.140 Realtime Text */
Kevin P. Fleming
committed
{ AST_FORMAT_SIREN7, "siren7", 16000, "ITU G.722.1 (Siren7, licensed from Polycom)", 80, 20, 80, 20, 20 }, /*!< Binary commercial distribution */
{ AST_FORMAT_SIREN14, "siren14", 32000, "ITU G.722.1 Annex C, (Siren14, licensed from Polycom)", 120, 20, 80, 20, 20 }, /*!< Binary commercial distribution */
{ AST_FORMAT_TESTLAW, "testlaw", 8000, "G.711 test-law", 80, 10, 150, 10, 20 }, /*!< codec_ulaw.c */
struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
static int smoother_frame_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
{
if (s->flags & AST_SMOOTHER_FLAG_G729) {
if (s->len % 10) {
ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
return 0;
}
}
if (swap) {
ast_swapcopy_samples(s->data + s->len, f->data.ptr, f->samples);
} else {
memcpy(s->data + s->len, f->data.ptr, f->datalen);
}
/* If either side is empty, reset the delivery time */
if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) { /* XXX really ? */
s->delivery = f->delivery;
}
s->len += f->datalen;
return 0;
}
void ast_smoother_reset(struct ast_smoother *s, int bytes)
memset(s, 0, sizeof(*s));
s->size = bytes;
}
void ast_smoother_reconfigure(struct ast_smoother *s, int bytes)
{
/* if there is no change, then nothing to do */
if (s->size == bytes) {
return;
}
/* set the new desired output size */
s->size = bytes;
/* if there is no 'optimized' frame in the smoother,
* then there is nothing left to do
*/
if (!s->opt) {
return;
}
/* there is an 'optimized' frame here at the old size,
* but it must now be put into the buffer so the data
* can be extracted at the new size
*/
smoother_frame_feed(s, s->opt, s->opt_needs_swap);
s->opt = NULL;
struct ast_smoother *ast_smoother_new(int size)
{
struct ast_smoother *s;
if ((s = ast_malloc(sizeof(*s))))
int ast_smoother_get_flags(struct ast_smoother *s)
{
return s->flags;
}
void ast_smoother_set_flags(struct ast_smoother *s, int flags)
{
s->flags = flags;
}
int ast_smoother_test_flag(struct ast_smoother *s, int flag)
{
return (s->flags & flag);
}
Kevin P. Fleming
committed
int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
{
if (f->frametype != AST_FRAME_VOICE) {
ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
return -1;
}
if (!s->format) {
s->format = f->subclass.codec;
s->samplesperbyte = (float)f->samples / (float)f->datalen;
} else if (s->format != f->subclass.codec) {
ast_log(LOG_WARNING, "Smoother was working on %s format frames, now trying to feed %s?\n",
ast_getformatname(s->format), ast_getformatname(f->subclass.codec));
return -1;
}
if (s->len + f->datalen > SMOOTHER_SIZE) {
ast_log(LOG_WARNING, "Out of smoother space\n");
return -1;
}
if (((f->datalen == s->size) ||
((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729))) &&
!s->opt &&
!s->len &&
(f->offset >= AST_MIN_OFFSET)) {
/* Optimize by sending the frame we just got
on the next read, thus eliminating the douple
copy */
if (swap)
ast_swapcopy_samples(f->data.ptr, f->data.ptr, f->samples);
s->opt = f;
s->opt_needs_swap = swap ? 1 : 0;
return 0;
return smoother_frame_feed(s, f, swap);
}
struct ast_frame *ast_smoother_read(struct ast_smoother *s)
{
/* IF we have an optimization frame, send it */
if (s->opt) {
if (s->opt->offset < AST_FRIENDLY_OFFSET)
ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
opt = s->opt;
s->opt = NULL;
return opt;
}
/* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->len % 10)))
return NULL;
len = s->size;
if (len > s->len)
len = s->len;
/* Make frame */
s->f.frametype = AST_FRAME_VOICE;
s->f.subclass.codec = s->format;
Michiel van Baak
committed
s->f.data.ptr = s->framedata + AST_FRIENDLY_OFFSET;
s->f.datalen = len;
/* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
Kevin P. Fleming
committed
s->f.samples = len * s->samplesperbyte; /* XXX rounding */
s->f.delivery = s->delivery;
Michiel van Baak
committed
memcpy(s->f.data.ptr, s->data, len);
s->len -= len;
/* Move remaining data to the front if applicable */
if (s->len) {
/* In principle this should all be fine because if we are sending
G.729 VAD, the next timestamp will take over anyawy */
memmove(s->data, s->data + len, s->len);
Kevin P. Fleming
committed
if (!ast_tvzero(s->delivery)) {
/* If we have delivery time, increment it, otherwise, leave it at 0 */
Kevin P. Fleming
committed
s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, ast_format_rate(s->format)));
}
}
/* Return frame */
return &s->f;
}
void ast_smoother_free(struct ast_smoother *s)
{
Tilghman Lesher
committed
ast_free(s);
static struct ast_frame *ast_frame_header_new(void)
{
#if !defined(LOW_MEMORY)
struct ast_frame_cache *frames;
if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
size_t mallocd_len = f->mallocd_hdr_len;
memset(f, 0, sizeof(*f));
f->mallocd_hdr_len = mallocd_len;
f->mallocd = AST_MALLOCD_HDR;
frames->size--;
return f;
}
if (!(f = ast_calloc_cache(1, sizeof(*f))))
#else
if (!(f = ast_calloc(1, sizeof(*f))))
return NULL;
#endif
f->mallocd_hdr_len = sizeof(*f);
#if !defined(LOW_MEMORY)
static void frame_cache_cleanup(void *data)
{
struct ast_frame_cache *frames = data;
struct ast_frame *f;
while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
Tilghman Lesher
committed
ast_free(f);
Tilghman Lesher
committed
ast_free(frames);
static void __frame_free(struct ast_frame *fr, int cache)
#if !defined(LOW_MEMORY)
Russell Bryant
committed
if (cache && fr->mallocd == AST_MALLOCD_HDR) {
/* Cool, only the header is malloc'd, let's just cache those for now
* to keep things simple... */
struct ast_frame_cache *frames;
if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) &&
(frames->size < FRAME_CACHE_MAX_SIZE)) {
AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
frames->size++;
return;
}
}
Michiel van Baak
committed
if (fr->data.ptr)
ast_free(fr->data.ptr - fr->offset);
}
if (fr->mallocd & AST_MALLOCD_SRC) {
if (fr->src)
ast_free((void *) fr->src);
Tilghman Lesher
committed
ast_free(fr);
void ast_frame_free(struct ast_frame *frame, int cache)
{
struct ast_frame *next;
for (next = AST_LIST_NEXT(frame, frame_list);
frame;
frame = next, next = frame ? AST_LIST_NEXT(frame, frame_list) : NULL) {
__frame_free(frame, cache);
}
}
/*!
* \brief 'isolates' a frame by duplicating non-malloc'ed components
* (header, src, data).
* On return all components are malloc'ed
*/
struct ast_frame *ast_frisolate(struct ast_frame *fr)
{
struct ast_frame *out;
void *newdata;
/* if none of the existing frame is malloc'd, let ast_frdup() do it
since it is more efficient
*/
if (fr->mallocd == 0) {
return ast_frdup(fr);
}
/* if everything is already malloc'd, we are done */
if ((fr->mallocd & (AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) ==
(AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA)) {
return fr;
}
if (!(fr->mallocd & AST_MALLOCD_HDR)) {
/* Allocate a new header if needed */
if (!(out = ast_frame_header_new())) {
out->subclass.codec = fr->subclass.codec;
Russell Bryant
committed
/* Copy the timing data */
ast_copy_flags(out, fr, AST_FRFLAG_HAS_TIMING_INFO);
if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
Russell Bryant
committed
out->ts = fr->ts;
out->len = fr->len;
out->seqno = fr->seqno;
}
if (!(fr->mallocd & AST_MALLOCD_SRC) && fr->src) {
if (!(out->src = ast_strdup(fr->src))) {
if (out != fr) {
ast_free(out);
Kevin P. Fleming
committed
}
Kevin P. Fleming
committed
}
fr->src = NULL;
fr->mallocd &= ~AST_MALLOCD_SRC;
}
if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
Tilghman Lesher
committed
ast_free((void *) out->src);
Tilghman Lesher
committed
ast_free(out);
newdata += AST_FRIENDLY_OFFSET;
out->offset = AST_FRIENDLY_OFFSET;
out->datalen = fr->datalen;
Michiel van Baak
committed
memcpy(newdata, fr->data.ptr, fr->datalen);
out->data.ptr = newdata;
} else {
out->data = fr->data;
memset(&fr->data, 0, sizeof(fr->data));
fr->mallocd &= ~AST_MALLOCD_DATA;
out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
Kevin P. Fleming
committed
struct ast_frame *ast_frdup(const struct ast_frame *f)
int len, srclen = 0;
#if !defined(LOW_MEMORY)
struct ast_frame_cache *frames;
#endif
len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
/* If we have a source, add space for it */
/*
* XXX Watch out here - if we receive a src which is not terminated
* properly, we can be easily attacked. Should limit the size we deal with.
*/
if (f->src)
srclen = strlen(f->src);
if (srclen > 0)
len += srclen + 1;
#if !defined(LOW_MEMORY)
if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
if (out->mallocd_hdr_len >= len) {
size_t mallocd_len = out->mallocd_hdr_len;
AST_LIST_REMOVE_CURRENT(frame_list);
memset(out, 0, sizeof(*out));
out->mallocd_hdr_len = mallocd_len;
buf = out;
frames->size--;
break;
}
}
if (!(buf = ast_calloc_cache(1, len)))
return NULL;
out = buf;
out->mallocd_hdr_len = len;
}
out->subclass.codec = f->subclass.codec;
out->datalen = f->datalen;
out->samples = f->samples;
out->delivery = f->delivery;
/* Set us as having malloc'd header only, so it will eventually
get freed. */
out->mallocd = AST_MALLOCD_HDR;
out->offset = AST_FRIENDLY_OFFSET;
if (out->datalen) {
Michiel van Baak
committed
out->data.ptr = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
memcpy(out->data.ptr, f->data.ptr, out->datalen);
} else {
out->data.uint32 = f->data.uint32;
}
if (srclen > 0) {
/* This may seem a little strange, but it's to avoid a gcc (4.2.4) compiler warning */
char *src;
out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
/* Must have space since we allocated for it */
}
ast_copy_flags(out, f, AST_FRFLAG_HAS_TIMING_INFO);
out->ts = f->ts;
out->len = f->len;
out->seqno = f->seqno;
void ast_swapcopy_samples(void *dst, const void *src, int samples)
Kevin P. Fleming
committed
{
int i;
unsigned short *dst_s = dst;
const unsigned short *src_s = src;
Kevin P. Fleming
committed
Kevin P. Fleming
committed
dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
}
Kevin P. Fleming
committed
const struct ast_format_list *ast_get_format_list_index(int idx)
return &AST_FORMAT_LIST[idx];
}
Kevin P. Fleming
committed
const struct ast_format_list *ast_get_format_list(size_t *size)
*size = ARRAY_LEN(AST_FORMAT_LIST);
return AST_FORMAT_LIST;
}
char* ast_getformatname(format_t format)
int x;
char *ret = "unknown";
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
if (AST_FORMAT_LIST[x].bits == format) {
ret = AST_FORMAT_LIST[x].name;
break;
}
}
return ret;
}
char *ast_getformatname_multiple(char *buf, size_t size, format_t format)
int x;
char *start, *end = buf;
snprintf(end, size, "0x%llx (", (unsigned long long) format);
len = strlen(end);
end += len;
size -= len;
start = end;
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
if (AST_FORMAT_LIST[x].bits & format) {
snprintf(end, size, "%s|", AST_FORMAT_LIST[x].name);
len = strlen(end);
end += len;
size -= len;
if (start == end)
Russell Bryant
committed
ast_copy_string(start, "nothing)", size);
else if (size > 1)
return buf;
}
static struct ast_codec_alias_table {
char *alias;
char *realname;
} ast_codec_alias_table[] = {
{ "slinear16", "slin16"},
Kevin P. Fleming
committed
{ "g722.1", "siren7"},
{ "g722.1c", "siren14"},
};
static const char *ast_expand_codec_alias(const char *in)
{
int x;
for (x = 0; x < ARRAY_LEN(ast_codec_alias_table); x++) {
Joshua Colp
committed
if (!strcmp(in,ast_codec_alias_table[x].alias))
return ast_codec_alias_table[x].realname;
}
return in;
}
format_t ast_getformatbyname(const char *name)
int x, all;
format_t format = 0;
all = strcasecmp(name, "all") ? 0 : 1;
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
Olle Johansson
committed
!strcasecmp(AST_FORMAT_LIST[x].name, ast_expand_codec_alias(name))) {
format |= AST_FORMAT_LIST[x].bits;
Joshua Colp
committed
if (!all)
break;
}
}
return format;
char *ast_codec2str(format_t codec)
int x;
char *ret = "unknown";
for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
if (AST_FORMAT_LIST[x].bits == codec) {
ret = AST_FORMAT_LIST[x].desc;
break;
}
}
return ret;
static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
int i, found=0;
char hex[25];
e->command = "core show codecs [audio|video|image|text]";
e->usage =
"Usage: core show codecs [audio|video|image|text]\n"
" Displays codec mapping\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if ((a->argc < 3) || (a->argc > 4))
return CLI_SHOWUSAGE;
ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
"\tIt does not indicate anything about your configuration.\n");
ast_cli(a->fd, "%19s %9s %20s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESCRIPTION");
ast_cli(a->fd, "-----------------------------------------------------------------------------------\n");
for (i = 0; i < 63; i++) {
if (a->argc == 4) {
if (!strcasecmp(a->argv[3], "audio")) {
if (!((1LL << i) & AST_FORMAT_AUDIO_MASK)) {
continue;
}
} else if (!strcasecmp(a->argv[3], "video")) {
if (!((1LL << i) & AST_FORMAT_VIDEO_MASK)) {
continue;
}
} else if (!strcasecmp(a->argv[3], "image")) {
if (i != 16 && i != 17) {
continue;
}
} else if (!strcasecmp(a->argv[3], "text")) {
if (!((1LL << i) & AST_FORMAT_TEXT_MASK)) {
continue;
}
} else {
Tilghman Lesher
committed
snprintf(hex, sizeof(hex), "(0x%llx)", 1LL << i);
ast_cli(a->fd, "%19llu (1 << %2d) %20s %5s %8s (%s)\n", 1LL << i, i, hex,
((1LL << i) & AST_FORMAT_AUDIO_MASK) ? "audio" :
i == 16 || i == 17 ? "image" :
((1LL << i) & AST_FORMAT_VIDEO_MASK) ? "video" :
((1LL << i) & AST_FORMAT_TEXT_MASK) ? "text" :
"(unk)",
ast_getformatname(1LL << i), ast_codec2str(1LL << i));
static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
format_t codec;
int i, found = 0;
switch (cmd) {
case CLI_INIT:
e->command = "core show codec";
"Usage: core show codec <number>\n"
" Displays codec mapping\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
Tilghman Lesher
committed
if (sscanf(a->argv[3], "%30lld", &type_punned_codec) != 1) {
for (i = 0; i < 63; i++)
if (codec & (1LL << i)) {
Tilghman Lesher
committed
ast_cli(a->fd, "%11llu (1 << %2d) %s\n", 1LL << i, i, ast_codec2str(1LL << i));
Tilghman Lesher
committed
ast_cli(a->fd, "Codec %lld not found\n", (long long) codec);
/*! Dump a frame for debugging purposes */
void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
const char noname[] = "unknown";
char ftype[40] = "Unknown Frametype";
char cft[80];
char subclass[40] = "Unknown Subclass";
char csub[80];
char moreinfo[40] = "";
Mark Spencer
committed
char cn[60];
Joshua Colp
committed
const char *message = "Unknown";
if (!name)
name = noname;
if (!f) {
ast_verbose("%s [ %s (NULL) ] [%s]\n",
term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
return;
}
/* XXX We should probably print one each of voice and video when the format changes XXX */
if (f->frametype == AST_FRAME_VOICE)
return;
if (f->frametype == AST_FRAME_VIDEO)
return;
switch(f->frametype) {
case AST_FRAME_DTMF_BEGIN:
strcpy(ftype, "DTMF Begin");
subclass[0] = f->subclass.integer;
subclass[1] = '\0';
break;
case AST_FRAME_DTMF_END:
strcpy(ftype, "DTMF End");
subclass[0] = f->subclass.integer;
subclass[1] = '\0';
break;
case AST_FRAME_CONTROL:
strcpy(ftype, "Control");
switch (f->subclass.integer) {
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
case AST_CONTROL_HANGUP:
strcpy(subclass, "Hangup");
break;
case AST_CONTROL_RING:
strcpy(subclass, "Ring");
break;
case AST_CONTROL_RINGING:
strcpy(subclass, "Ringing");
break;
case AST_CONTROL_ANSWER:
strcpy(subclass, "Answer");
break;
case AST_CONTROL_BUSY:
strcpy(subclass, "Busy");
break;
case AST_CONTROL_TAKEOFFHOOK:
strcpy(subclass, "Take Off Hook");
break;
case AST_CONTROL_OFFHOOK:
strcpy(subclass, "Line Off Hook");
break;
case AST_CONTROL_CONGESTION:
strcpy(subclass, "Congestion");
break;
case AST_CONTROL_FLASH:
strcpy(subclass, "Flash");
break;
case AST_CONTROL_WINK:
strcpy(subclass, "Wink");
break;
case AST_CONTROL_OPTION:
strcpy(subclass, "Option");
break;
case AST_CONTROL_RADIO_KEY:
strcpy(subclass, "Key Radio");
break;
case AST_CONTROL_RADIO_UNKEY:
strcpy(subclass, "Unkey Radio");
break;
case AST_CONTROL_HOLD:
strcpy(subclass, "Hold");
break;
case AST_CONTROL_UNHOLD:
strcpy(subclass, "Unhold");
break;
Joshua Colp
committed
case AST_CONTROL_T38_PARAMETERS:
Kevin P. Fleming
committed
if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
Joshua Colp
committed
message = "Invalid";
} else {
struct ast_control_t38_parameters *parameters = f->data.ptr;
enum ast_control_t38 state = parameters->request_response;
if (state == AST_T38_REQUEST_NEGOTIATE)
message = "Negotiation Requested";
else if (state == AST_T38_REQUEST_TERMINATE)
message = "Negotiation Request Terminated";
else if (state == AST_T38_NEGOTIATED)
message = "Negotiated";
else if (state == AST_T38_TERMINATED)
message = "Terminated";
else if (state == AST_T38_REFUSED)
message = "Refused";
}
snprintf(subclass, sizeof(subclass), "T38_Parameters/%s", message);
break;
Mark Spencer
committed
case -1:
strcpy(subclass, "Stop generators");
break;
snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass.integer);
Mark Spencer
committed
break;
case AST_FRAME_NULL:
strcpy(ftype, "Null Frame");
strcpy(subclass, "N/A");
break;
case AST_FRAME_IAX:
/* Should never happen */
strcpy(ftype, "IAX Specific");
snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass.integer);
break;
case AST_FRAME_TEXT:
strcpy(ftype, "Text");
strcpy(subclass, "N/A");
Michiel van Baak
committed
ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
break;
case AST_FRAME_IMAGE:
strcpy(ftype, "Image");
snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass.codec));
break;
case AST_FRAME_HTML:
strcpy(ftype, "HTML");
switch (f->subclass.integer) {
case AST_HTML_URL:
strcpy(subclass, "URL");
Michiel van Baak
committed
ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
break;
case AST_HTML_DATA:
strcpy(subclass, "Data");
break;
case AST_HTML_BEGIN:
strcpy(subclass, "Begin");
break;
case AST_HTML_END:
strcpy(subclass, "End");
break;
case AST_HTML_LDCOMPLETE:
strcpy(subclass, "Load Complete");
break;
case AST_HTML_NOSUPPORT:
strcpy(subclass, "No Support");
break;
case AST_HTML_LINKURL:
strcpy(subclass, "Link URL");
Michiel van Baak
committed
ast_copy_string(moreinfo, f->data.ptr, sizeof(moreinfo));
break;
case AST_HTML_UNLINK:
strcpy(subclass, "Unlink");
break;
case AST_HTML_LINKREJECT:
strcpy(subclass, "Link Reject");
break;
default:
snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass.integer);
Joshua Colp
committed
case AST_FRAME_MODEM:
strcpy(ftype, "Modem");
switch (f->subclass.integer) {
Joshua Colp
committed
case AST_MODEM_T38:
strcpy(subclass, "T.38");
break;
case AST_MODEM_V150:
strcpy(subclass, "V.150");
break;
default:
snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass.integer);
Joshua Colp
committed
break;
}
break;
default:
snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
}
ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
f->frametype,
term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
else
ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
f->frametype,
term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
/* Builtin Asterisk CLI-commands for debugging */
Kevin P. Fleming
committed
static struct ast_cli_entry my_clis[] = {
Jason Parker
committed
AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
Kevin P. Fleming
committed
};
ast_cli_register_multiple(my_clis, ARRAY_LEN(my_clis));
void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right)
{
int x, differential = (int) 'A', mem;
char *from, *to;
Joshua Colp
committed
if (right) {
from = pref->order;
to = buf;
mem = size;
} else {
to = pref->order;
from = buf;
mem = sizeof(format_t) * 8;
}
memset(to, 0, mem);
for (x = 0; x < sizeof(format_t) * 8; x++) {
Joshua Colp
committed
if (!from[x])
break;
to[x] = right ? (from[x] + differential) : (from[x] - differential);
}