Skip to content
Snippets Groups Projects
frame.c 39.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2005, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * 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.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * 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.
     */
    
    
    Olle Johansson's avatar
    Olle Johansson committed
     * \brief Frame and codec manipulation routines
    
     *
     * \author Mark Spencer <markster@digium.com> 
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include "asterisk/_private.h"
    
    #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"
    
    #include "asterisk/dsp.h"
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    #ifdef TRACE_FRAMES
    
    static int headers;
    
    static AST_LIST_HEAD_STATIC(headerlist, ast_frame);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    
    #if !defined(LOW_MEMORY)
    
    static void frame_cache_cleanup(void *data);
    
    /*! \brief A per-thread cache of frame headers */
    
    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;
    };
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define SMOOTHER_SIZE 8000
    
    
    enum frame_type {
    	TYPE_HIGH,     /* 0x0 */
    	TYPE_LOW,      /* 0x1 */
    	TYPE_SILENCE,  /* 0x2 */
    	TYPE_DONTSEND  /* 0x3 */
    };
    
    #define TYPE_MASK 0x3
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_smoother {
    	int size;
    	int format;
    	int readdata;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	float samplesperbyte;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct ast_frame f;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	char data[SMOOTHER_SIZE];
    	char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
    
    	struct ast_frame *opt;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int len;
    };
    
    
    /*! \brief Definition of supported media formats (codecs) */
    
    Joshua Colp's avatar
    Joshua Colp committed
    static 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 */
    	{ AST_FORMAT_SLINEAR16, "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20 },    /*!< 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 */
    
    	{ 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 */
    
    struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    void ast_smoother_reset(struct ast_smoother *s, int size)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	s->size = size;
    }
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_smoother *ast_smoother_new(int size)
    {
    	struct ast_smoother *s;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (size < 1)
    		return NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_smoother_reset(s, size);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 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);
    }
    
    
    int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	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;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		s->samplesperbyte = (float)f->samples / (float)f->datalen;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	} else if (s->format != f->subclass) {
    		ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
    		return -1;
    	}
    	if (s->len + f->datalen > SMOOTHER_SIZE) {
    		ast_log(LOG_WARNING, "Out of smoother space\n");
    		return -1;
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
    
    				 && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
    
    		if (!s->len) {
    			/* Optimize by sending the frame we just got
    			   on the next read, thus eliminating the douple
    			   copy */
    
    			if (swap)
    				ast_swapcopy_samples(f->data, f->data, f->samples);
    
    			s->opt = f;
    			return 0;
    		}
    
    	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, f->samples);
    	else
    		memcpy(s->data + s->len, f->data, f->datalen);
    
    	/* If either side is empty, reset the delivery time */
    
    	if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery))	/* XXX really ? */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	s->len += f->datalen;
    	return 0;
    }
    
    struct ast_frame *ast_smoother_read(struct ast_smoother *s)
    {
    
    	struct ast_frame *opt;
    
    Olle Johansson's avatar
    Olle Johansson committed
    
    
    	/* 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",
    
    							s->opt->offset);
    
    		opt = s->opt;
    		s->opt = NULL;
    		return opt;
    	}
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Make sure we have enough data */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (s->len < s->size) {
    
    		/* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
    		if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
    			return NULL;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	len = s->size;
    	if (len > s->len)
    		len = s->len;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Make frame */
    	s->f.frametype = AST_FRAME_VOICE;
    	s->f.subclass = s->format;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	s->f.offset = AST_FRIENDLY_OFFSET;
    
    	s->f.datalen = len;
    	/* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
    
    	s->f.samples = len * s->samplesperbyte;	/* XXX rounding */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Fill Data */
    
    	memcpy(s->f.data, s->data, len);
    	s->len -= len;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Move remaining data to the front if applicable */
    
    		/* 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);
    
    			/* If we have delivery time, increment it, otherwise, leave it at 0 */
    
    			s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, 8000));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Return frame */
    	return &s->f;
    }
    
    void ast_smoother_free(struct ast_smoother *s)
    {
    
    Mark Spencer's avatar
    Mark Spencer committed
    static struct ast_frame *ast_frame_header_new(void)
    {
    
    	struct ast_frame *f;
    
    	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;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	if (!(f = ast_calloc_cache(1, sizeof(*f))))
    
    		return NULL;
    
    #else
    	if (!(f = ast_calloc(1, sizeof(*f))))
    		return NULL;
    #endif
    
    
    	f->mallocd_hdr_len = sizeof(*f);
    #ifdef TRACE_FRAMES
    	AST_LIST_LOCK(&headerlist);
    	headers++;
    	AST_LIST_INSERT_HEAD(&headerlist, f, frame_list);
    	AST_LIST_UNLOCK(&headerlist);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif	
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return f;
    }
    
    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)))
    
    void ast_frame_free(struct ast_frame *fr, int cache)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	if (ast_test_flag(fr, AST_FRFLAG_FROM_TRANSLATOR))
    		ast_translate_frame_freed(fr);
    
    	else if (ast_test_flag(fr, AST_FRFLAG_FROM_DSP))
    		ast_dsp_frame_freed(fr);
    
    	if (!fr->mallocd)
    		return;
    
    
    	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;
    		}
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (fr->mallocd & AST_MALLOCD_DATA) {
    		if (fr->data) 
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	if (fr->mallocd & AST_MALLOCD_SRC) {
    		if (fr->src)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	if (fr->mallocd & AST_MALLOCD_HDR) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    #ifdef TRACE_FRAMES
    
    		AST_LIST_LOCK(&headerlist);
    
    		AST_LIST_REMOVE(&headerlist, fr, frame_list);
    		AST_LIST_UNLOCK(&headerlist);
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif			
    
    /*!
     * \brief 'isolates' a frame by duplicating non-malloc'ed components
    
     * (header, src, data).
     * On return all components are malloc'ed
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    struct ast_frame *ast_frisolate(struct ast_frame *fr)
    {
    	struct ast_frame *out;
    
    
    	ast_clear_flag(fr, AST_FRFLAG_FROM_TRANSLATOR);
    
    	ast_clear_flag(fr, AST_FRFLAG_FROM_DSP);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!(fr->mallocd & AST_MALLOCD_HDR)) {
    		/* Allocate a new header if needed */
    
    		if (!(out = ast_frame_header_new()))
    
    Mark Spencer's avatar
    Mark Spencer committed
    			return NULL;
    		out->frametype = fr->frametype;
    		out->subclass = fr->subclass;
    
    		out->datalen = fr->datalen;
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out->samples = fr->samples;
    
    		out->offset = fr->offset;
    		out->data = fr->data;
    
    		ast_copy_flags(out, fr, AST_FRFLAG_HAS_TIMING_INFO);
    		if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
    
    			out->ts = fr->ts;
    			out->len = fr->len;
    			out->seqno = fr->seqno;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out = fr;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!(fr->mallocd & AST_MALLOCD_SRC)) {
    
    		if (fr->src) {
    			if (!(out->src = ast_strdup(fr->src))) {
    				if (out != fr)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	} else
    		out->src = fr->src;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (!(fr->mallocd & AST_MALLOCD_DATA))  {
    
    		if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			return NULL;
    		}
    
    Mark Spencer's avatar
    Mark Spencer committed
    		out->offset = AST_FRIENDLY_OFFSET;
    		out->datalen = fr->datalen;
    
    		memcpy(newdata, fr->data, fr->datalen);
    		out->data = newdata;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return out;
    }
    
    struct ast_frame *ast_frdup(const struct ast_frame *f)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	struct ast_frame *out = NULL;
    
    	void *buf = NULL;
    
    
    #if !defined(LOW_MEMORY)
    	struct ast_frame_cache *frames;
    #endif
    
    
    	/* Start with standard stuff */
    
    	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 ((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;
    			}
    		}
    
    		AST_LIST_TRAVERSE_SAFE_END;
    
    	if (!buf) {
    
    		if (!(buf = ast_calloc_cache(1, len)))
    
    			return NULL;
    		out = buf;
    		out->mallocd_hdr_len = len;
    	}
    
    
    	out->frametype = f->frametype;
    	out->subclass = f->subclass;
    	out->datalen = f->datalen;
    	out->samples = f->samples;
    
    	/* 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) {
    		out->data = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
    		memcpy(out->data, f->data, out->datalen);	
    	}
    
    		out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
    
    		/* Must have space since we allocated for it */
    
    		strcpy((char *)out->src, f->src);
    
    	ast_copy_flags(out, f, AST_FRFLAG_HAS_TIMING_INFO);
    
    	out->ts = f->ts;
    	out->len = f->len;
    	out->seqno = f->seqno;
    
    	return out;
    
    void ast_swapcopy_samples(void *dst, const void *src, int samples)
    
    	const unsigned short *src_s = src;
    
    Olle Johansson's avatar
    Olle Johansson committed
    	for (i = 0; i < samples; i++)
    
    struct ast_format_list *ast_get_format_list_index(int index) 
    {
    
    struct ast_format_list *ast_get_format_list(size_t *size) 
    {
    
    	*size = (sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]));
    
    char* ast_getformatname(int format)
    {
    
    	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
    
    		if (AST_FORMAT_LIST[x].bits == format) {
    
    Olle Johansson's avatar
    Olle Johansson committed
    char *ast_getformatname_multiple(char *buf, size_t size, int format)
    {
    
    Olle Johansson's avatar
    Olle Johansson committed
    
    	if (!size)
    		return buf;
    
    	snprintf(end, size, "0x%x (", format);
    	len = strlen(end);
    	end += len;
    	size -= len;
    	start = end;
    
    	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
    
    		if (AST_FORMAT_LIST[x].bits & format) {
    
    			snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
    			len = strlen(end);
    			end += len;
    			size -= len;
    
    		ast_copy_string(start, "nothing)", size);
    
    static struct ast_codec_alias_table {
    	char *alias;
    	char *realname;
    } ast_codec_alias_table[] = {
    
    Olle Johansson's avatar
    Olle Johansson committed
    	{ "slinear", "slin"},
    
    	{ "slinear16", "slin16"},
    
    Olle Johansson's avatar
    Olle Johansson committed
    	{ "g723.1", "g723"},
    
    Olle Johansson's avatar
    Olle Johansson committed
    static const char *ast_expand_codec_alias(const char *in)
    {
    
    	for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(ast_codec_alias_table[0]); x++) {
    
    		if (!strcmp(in,ast_codec_alias_table[x].alias))
    
    			return ast_codec_alias_table[x].realname;
    	}
    	return in;
    }
    
    
    int ast_getformatbyname(const char *name)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	all = strcasecmp(name, "all") ? 0 : 1;
    
    	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
    
    Olle Johansson's avatar
    Olle Johansson committed
    			  !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
    
    			  !strcasecmp(AST_FORMAT_LIST[x].name, ast_expand_codec_alias(name))) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    }
    
    Olle Johansson's avatar
    Olle Johansson committed
    char *ast_codec2str(int codec)
    {
    
    	for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
    
    		if (AST_FORMAT_LIST[x].bits == codec) {
    
    			ret = AST_FORMAT_LIST[x].desc;
    			break;
    		}
    	}
    	return ret;
    
    Jason Parker's avatar
    Jason Parker committed
    static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
    
    {
    	int i, found=0;
    	char hex[25];
    
    Jason Parker's avatar
    Jason Parker committed
    
    	switch (cmd) {
    	case CLI_INIT:
    		e->command = "core show codecs [audio|video|image]";
    		e->usage = 
    			"Usage: core show codecs [audio|video|image]\n"
    			"       Displays codec mapping\n";
    		return NULL;
    	case CLI_GENERATE:
    		return NULL;
    	}
    
    Jason Parker's avatar
    Jason Parker committed
    	if ((a->argc < 3) || (a->argc > 4))
    		return CLI_SHOWUSAGE;
    
    
    	if (!ast_opt_dont_warn)
    
    Jason Parker's avatar
    Jason Parker committed
    		ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
    
    				"\tIt does not indicate anything about your configuration.\n");
    
    
    Jason Parker's avatar
    Jason Parker committed
    	ast_cli(a->fd, "%11s %9s %10s   TYPE   %8s   %s\n","INT","BINARY","HEX","NAME","DESC");
    	ast_cli(a->fd, "--------------------------------------------------------------------------------\n");
    	if ((a->argc == 3) || (!strcasecmp(a->argv[3],"audio"))) {
    
    		for (i=0;i<13;i++) {
    
    			snprintf(hex,25,"(0x%x)",1<<i);
    
    Jason Parker's avatar
    Jason Parker committed
    			ast_cli(a->fd, "%11u (1 << %2d) %10s  audio   %8s   (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
    
    Jason Parker's avatar
    Jason Parker committed
    	if ((a->argc == 3) || (!strcasecmp(a->argv[3],"image"))) {
    
    		found = 1;
    		for (i=16;i<18;i++) {
    			snprintf(hex,25,"(0x%x)",1<<i);
    
    Jason Parker's avatar
    Jason Parker committed
    			ast_cli(a->fd, "%11u (1 << %2d) %10s  image   %8s   (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
    
    Jason Parker's avatar
    Jason Parker committed
    	if ((a->argc == 3) || (!strcasecmp(a->argv[3],"video"))) {
    
    		found = 1;
    		for (i=18;i<22;i++) {
    			snprintf(hex,25,"(0x%x)",1<<i);
    
    Jason Parker's avatar
    Jason Parker committed
    			ast_cli(a->fd, "%11u (1 << %2d) %10s  video   %8s   (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
    
    Jason Parker's avatar
    Jason Parker committed
    	if (!found)
    		return CLI_SHOWUSAGE;
    
    Jason Parker's avatar
    Jason Parker committed
    		return CLI_SUCCESS;
    
    Jason Parker's avatar
    Jason Parker committed
    static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
    
    {
    	int codec, i, found=0;
    
    
    Jason Parker's avatar
    Jason Parker committed
    	switch (cmd) {
    	case CLI_INIT:
    		e->command = "core show codec";
    		e->usage = 
    			"Usage: core show codec <number>\n"
    			"       Displays codec mapping\n";
    		return NULL;
    	case CLI_GENERATE:
    		return NULL;
    	}
    
    Jason Parker's avatar
    Jason Parker committed
    	if (a->argc != 4)
    		return CLI_SHOWUSAGE;
    
    	if (sscanf(a->argv[3],"%d",&codec) != 1)
    		return CLI_SHOWUSAGE;
    
    
    	for (i = 0; i < 32; i++)
    		if (codec & (1 << i)) {
    			found = 1;
    
    Jason Parker's avatar
    Jason Parker committed
    			ast_cli(a->fd, "%11u (1 << %2d)  %s\n",1 << i,i,ast_codec2str(1<<i));
    
    Jason Parker's avatar
    Jason Parker committed
    		ast_cli(a->fd, "Codec %d not found\n", codec);
    
    Jason Parker's avatar
    Jason Parker committed
    	return CLI_SUCCESS;
    
    /*! 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] = "";
    
    	char cp[40];
    	char cmn[40];
    
    	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;
    		subclass[1] = '\0';
    		break;
    	case AST_FRAME_DTMF_END:
    		strcpy(ftype, "DTMF End");
    
    		subclass[0] = f->subclass;
    		subclass[1] = '\0';
    		break;
    	case AST_FRAME_CONTROL:
    		strcpy(ftype, "Control");
    		switch(f->subclass) {
    		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;
    
    		case AST_CONTROL_T38:
    			if (f->datalen != sizeof(enum ast_control_t38)) {
    				message = "Invalid";
    			} else {
    				enum ast_control_t38 state = *((enum ast_control_t38 *) f->data);
    				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/%s", message);
    			break;
    
    		default:
    			snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
    		}
    
    	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);
    		break;
    	case AST_FRAME_TEXT:
    		strcpy(ftype, "Text");
    		strcpy(subclass, "N/A");
    
    		ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
    
    		break;
    	case AST_FRAME_IMAGE:
    		strcpy(ftype, "Image");
    
    		snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
    
    		break;
    	case AST_FRAME_HTML:
    		strcpy(ftype, "HTML");
    		switch(f->subclass) {
    		case AST_HTML_URL:
    			strcpy(subclass, "URL");
    
    			ast_copy_string(moreinfo, f->data, 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");
    
    			ast_copy_string(moreinfo, f->data, 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);
    			break;
    		}
    		break;
    
    	case AST_FRAME_MODEM:
    		strcpy(ftype, "Modem");
    		switch (f->subclass) {
    		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);
    			break;
    		}
    		break;
    
    	default:
    		snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
    	}
    
    	if (!ast_strlen_zero(moreinfo))
    
    		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)),
    			    f->subclass, 
    			    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)),
    			    f->subclass, 
    			    term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
    
    Mark Spencer's avatar
    Mark Spencer committed
    #ifdef TRACE_FRAMES
    
    Jason Parker's avatar
    Jason Parker committed
    static char *show_frame_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
    
    {
    	struct ast_frame *f;
    	int x=1;
    
    Jason Parker's avatar
    Jason Parker committed
    
    	switch (cmd) {
    	case CLI_INIT:
    		e->command = "core show frame stats";
    		e->usage = 
    			"Usage: core show frame stats\n"
    			"       Displays debugging statistics from framer\n";
    		return NULL;
    	case CLI_GENERATE:
    		return NULL;	
    	}
    
    	if (a->argc != 4)
    		return CLI_SHOWUSAGE;
    
    	AST_LIST_LOCK(&headerlist);
    
    Jason Parker's avatar
    Jason Parker committed
    	ast_cli(a->fd, "     Framer Statistics     \n");
    	ast_cli(a->fd, "---------------------------\n");
    	ast_cli(a->fd, "Total allocated headers: %d\n", headers);
    	ast_cli(a->fd, "Queue Dump:\n");
    
    	AST_LIST_TRAVERSE(&headerlist, f, frame_list)
    
    Jason Parker's avatar
    Jason Parker committed
    		ast_cli(a->fd, "%d.  Type %d, subclass %d from %s\n", x++, f->frametype, f->subclass, f->src ? f->src : "<Unknown>");
    
    	AST_LIST_UNLOCK(&headerlist);
    
    Jason Parker's avatar
    Jason Parker committed
    	return CLI_SUCCESS;
    
    /* Builtin Asterisk CLI-commands for debugging */
    
    static struct ast_cli_entry my_clis[] = {
    
    	AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
    	AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
    
    	AST_CLI_DEFINE(show_frame_stats, "Shows frame statistics"),
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    int init_framer(void)
    {
    
    	ast_cli_register_multiple(my_clis, sizeof(my_clis) / sizeof(struct ast_cli_entry));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;	
    }
    
    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;
    
    		from = pref->order;
    		to = buf;
    		mem = size;
    	} else {
    		to = pref->order;
    		from = buf;
    		mem = 32;
    	}
    
    	memset(to, 0, mem);
    	for (x = 0; x < 32 ; x++) {
    
    			break;
    		to[x] = right ? (from[x] + differential) : (from[x] - differential);
    	}
    }
    
    int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size) 
    {
    
    	int x, codec; 
    	size_t total_len, slen;
    	char *formatname;
    
    	
    	memset(buf,0,size);
    	total_len = size;
    	buf[0] = '(';
    	total_len--;
    	for(x = 0; x < 32 ; x++) {
    
    		if (!(codec = ast_codec_pref_index(pref,x)))
    
    		if ((formatname = ast_getformatname(codec))) {
    
    			strncat(buf, formatname, total_len - 1); /* safe */
    
    		if (total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
    
    			strncat(buf, "|", total_len - 1); /* safe */
    
    		strncat(buf, ")", total_len - 1); /* safe */
    
    		total_len--;
    	}
    
    	return size - total_len;
    }
    
    int ast_codec_pref_index(struct ast_codec_pref *pref, int index)