2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Frame and codec manipulation routines
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/_private.h"
31 #include "asterisk/lock.h"
32 #include "asterisk/frame.h"
33 #include "asterisk/channel.h"
34 #include "asterisk/cli.h"
35 #include "asterisk/term.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/threadstorage.h"
38 #include "asterisk/linkedlists.h"
39 #include "asterisk/translate.h"
43 static AST_LIST_HEAD_STATIC(headerlist, ast_frame);
46 #if !defined(LOW_MEMORY)
47 static void frame_cache_cleanup(void *data);
49 /*! \brief A per-thread cache of frame headers */
50 AST_THREADSTORAGE_CUSTOM(frame_cache, NULL, frame_cache_cleanup);
53 * \brief Maximum ast_frame cache size
55 * In most cases where the frame header cache will be useful, the size
56 * of the cache will stay very small. However, it is not always the case that
57 * the same thread that allocates the frame will be the one freeing them, so
58 * sometimes a thread will never have any frames in its cache, or the cache
59 * will never be pulled from. For the latter case, we limit the maximum size.
61 #define FRAME_CACHE_MAX_SIZE 10
63 /*! \brief This is just so ast_frames, a list head struct for holding a list of
64 * ast_frame structures, is defined. */
65 AST_LIST_HEAD_NOLOCK(ast_frames, ast_frame);
67 struct ast_frame_cache {
68 struct ast_frames list;
73 #define SMOOTHER_SIZE 8000
78 TYPE_SILENCE, /* 0x2 */
79 TYPE_DONTSEND /* 0x3 */
88 int optimizablestream;
92 struct timeval delivery;
93 char data[SMOOTHER_SIZE];
94 char framedata[SMOOTHER_SIZE + AST_FRIENDLY_OFFSET];
95 struct ast_frame *opt;
99 /*! \brief Definition of supported media formats (codecs) */
100 static struct ast_format_list AST_FORMAT_LIST[] = {
101 { AST_FORMAT_G723_1 , "g723", 8000, "G.723.1", 20, 30, 300, 30, 30 }, /*!< G723.1 */
102 { AST_FORMAT_GSM, "gsm", 8000, "GSM", 33, 20, 300, 20, 20 }, /*!< codec_gsm.c */
103 { AST_FORMAT_ULAW, "ulaw", 8000, "G.711 u-law", 80, 10, 150, 10, 20 }, /*!< codec_ulaw.c */
104 { AST_FORMAT_ALAW, "alaw", 8000, "G.711 A-law", 80, 10, 150, 10, 20 }, /*!< codec_alaw.c */
105 { AST_FORMAT_G726, "g726", 8000, "G.726 RFC3551", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
106 { AST_FORMAT_ADPCM, "adpcm" , 8000, "ADPCM", 40, 10, 300, 10, 20 }, /*!< codec_adpcm.c */
107 { AST_FORMAT_SLINEAR, "slin", 8000, "16 bit Signed Linear PCM", 160, 10, 70, 10, 20, AST_SMOOTHER_FLAG_BE }, /*!< Signed linear */
108 { AST_FORMAT_LPC10, "lpc10", 8000, "LPC10", 7, 20, 20, 20, 20 }, /*!< codec_lpc10.c */
109 { AST_FORMAT_G729A, "g729", 8000, "G.729A", 10, 10, 230, 10, 20, AST_SMOOTHER_FLAG_G729 }, /*!< Binary commercial distribution */
110 { AST_FORMAT_SPEEX, "speex", 8000, "SpeeX", 10, 10, 60, 10, 20 }, /*!< codec_speex.c */
111 { AST_FORMAT_ILBC, "ilbc", 8000, "iLBC", 50, 30, 30, 30, 30 }, /*!< codec_ilbc.c */ /* inc=30ms - workaround */
112 { AST_FORMAT_G726_AAL2, "g726aal2", 8000, "G.726 AAL2", 40, 10, 300, 10, 20 }, /*!< codec_g726.c */
113 { AST_FORMAT_G722, "g722", 16000, "G722", 80, 10, 150, 10, 20 }, /*!< codec_g722.c */
114 { AST_FORMAT_SLINEAR16, "slin16", 16000, "16 bit Signed Linear PCM (16kHz)", 320, 10, 70, 10, 20 }, /*!< Signed linear (16kHz) */
115 { AST_FORMAT_JPEG, "jpeg", 0, "JPEG image"}, /*!< See format_jpeg.c */
116 { AST_FORMAT_PNG, "png", 0, "PNG image"}, /*!< PNG Image format */
117 { AST_FORMAT_H261, "h261", 0, "H.261 Video" }, /*!< H.261 Video Passthrough */
118 { AST_FORMAT_H263, "h263", 0, "H.263 Video" }, /*!< H.263 Passthrough support, see format_h263.c */
119 { AST_FORMAT_H263_PLUS, "h263p", 0, "H.263+ Video" }, /*!< H.263plus passthrough support See format_h263.c */
120 { AST_FORMAT_H264, "h264", 0, "H.264 Video" }, /*!< Passthrough support, see format_h263.c */
121 { AST_FORMAT_MP4_VIDEO, "mpeg4", 0, "MPEG4 Video" }, /*!< Passthrough support for MPEG4 */
122 { AST_FORMAT_T140, "t140", 0, "Passthrough T.140 Realtime Text" }, /*!< Passthrough support for T.140 Realtime Text */
125 struct ast_frame ast_null_frame = { AST_FRAME_NULL, };
127 void ast_smoother_reset(struct ast_smoother *s, int size)
129 memset(s, 0, sizeof(*s));
133 struct ast_smoother *ast_smoother_new(int size)
135 struct ast_smoother *s;
138 if ((s = ast_malloc(sizeof(*s))))
139 ast_smoother_reset(s, size);
143 int ast_smoother_get_flags(struct ast_smoother *s)
148 void ast_smoother_set_flags(struct ast_smoother *s, int flags)
153 int ast_smoother_test_flag(struct ast_smoother *s, int flag)
155 return (s->flags & flag);
158 int __ast_smoother_feed(struct ast_smoother *s, struct ast_frame *f, int swap)
160 if (f->frametype != AST_FRAME_VOICE) {
161 ast_log(LOG_WARNING, "Huh? Can't smooth a non-voice frame!\n");
165 s->format = f->subclass;
166 s->samplesperbyte = (float)f->samples / (float)f->datalen;
167 } else if (s->format != f->subclass) {
168 ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
171 if (s->len + f->datalen > SMOOTHER_SIZE) {
172 ast_log(LOG_WARNING, "Out of smoother space\n");
175 if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
176 && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
178 /* Optimize by sending the frame we just got
179 on the next read, thus eliminating the douple
182 ast_swapcopy_samples(f->data, f->data, f->samples);
187 if (s->flags & AST_SMOOTHER_FLAG_G729) {
189 ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
194 ast_swapcopy_samples(s->data+s->len, f->data, f->samples);
196 memcpy(s->data + s->len, f->data, f->datalen);
197 /* If either side is empty, reset the delivery time */
198 if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery)) /* XXX really ? */
199 s->delivery = f->delivery;
200 s->len += f->datalen;
204 struct ast_frame *ast_smoother_read(struct ast_smoother *s)
206 struct ast_frame *opt;
209 /* IF we have an optimization frame, send it */
211 if (s->opt->offset < AST_FRIENDLY_OFFSET)
212 ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
219 /* Make sure we have enough data */
220 if (s->len < s->size) {
221 /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
222 if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
229 s->f.frametype = AST_FRAME_VOICE;
230 s->f.subclass = s->format;
231 s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
232 s->f.offset = AST_FRIENDLY_OFFSET;
234 /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
235 s->f.samples = len * s->samplesperbyte; /* XXX rounding */
236 s->f.delivery = s->delivery;
238 memcpy(s->f.data, s->data, len);
240 /* Move remaining data to the front if applicable */
242 /* In principle this should all be fine because if we are sending
243 G.729 VAD, the next timestamp will take over anyawy */
244 memmove(s->data, s->data + len, s->len);
245 if (!ast_tvzero(s->delivery)) {
246 /* If we have delivery time, increment it, otherwise, leave it at 0 */
247 s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, 8000));
254 void ast_smoother_free(struct ast_smoother *s)
259 static struct ast_frame *ast_frame_header_new(void)
263 #if !defined(LOW_MEMORY)
264 struct ast_frame_cache *frames;
266 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
267 if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
268 size_t mallocd_len = f->mallocd_hdr_len;
269 memset(f, 0, sizeof(*f));
270 f->mallocd_hdr_len = mallocd_len;
271 f->mallocd = AST_MALLOCD_HDR;
276 if (!(f = ast_calloc_cache(1, sizeof(*f))))
279 if (!(f = ast_calloc(1, sizeof(*f))))
283 f->mallocd_hdr_len = sizeof(*f);
285 AST_LIST_LOCK(&headerlist);
287 AST_LIST_INSERT_HEAD(&headerlist, f, frame_list);
288 AST_LIST_UNLOCK(&headerlist);
294 #if !defined(LOW_MEMORY)
295 static void frame_cache_cleanup(void *data)
297 struct ast_frame_cache *frames = data;
300 while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
307 void ast_frame_free(struct ast_frame *fr, int cache)
309 if (ast_test_flag(fr, AST_FRFLAG_FROM_TRANSLATOR))
310 ast_translate_frame_freed(fr);
315 #if !defined(LOW_MEMORY)
316 if (cache && fr->mallocd == AST_MALLOCD_HDR) {
317 /* Cool, only the header is malloc'd, let's just cache those for now
318 * to keep things simple... */
319 struct ast_frame_cache *frames;
321 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))
322 && frames->size < FRAME_CACHE_MAX_SIZE) {
323 AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
330 if (fr->mallocd & AST_MALLOCD_DATA) {
332 ast_free(fr->data - fr->offset);
334 if (fr->mallocd & AST_MALLOCD_SRC) {
336 ast_free((char *)fr->src);
338 if (fr->mallocd & AST_MALLOCD_HDR) {
340 AST_LIST_LOCK(&headerlist);
342 AST_LIST_REMOVE(&headerlist, fr, frame_list);
343 AST_LIST_UNLOCK(&headerlist);
350 * \brief 'isolates' a frame by duplicating non-malloc'ed components
351 * (header, src, data).
352 * On return all components are malloc'ed
354 struct ast_frame *ast_frisolate(struct ast_frame *fr)
356 struct ast_frame *out;
359 ast_clear_flag(fr, AST_FRFLAG_FROM_TRANSLATOR);
361 if (!(fr->mallocd & AST_MALLOCD_HDR)) {
362 /* Allocate a new header if needed */
363 if (!(out = ast_frame_header_new()))
365 out->frametype = fr->frametype;
366 out->subclass = fr->subclass;
367 out->datalen = fr->datalen;
368 out->samples = fr->samples;
369 out->offset = fr->offset;
370 out->data = fr->data;
371 /* Copy the timing data */
372 ast_copy_flags(out, fr, AST_FRFLAG_HAS_TIMING_INFO);
373 if (ast_test_flag(fr, AST_FRFLAG_HAS_TIMING_INFO)) {
376 out->seqno = fr->seqno;
381 if (!(fr->mallocd & AST_MALLOCD_SRC)) {
383 if (!(out->src = ast_strdup(fr->src))) {
392 if (!(fr->mallocd & AST_MALLOCD_DATA)) {
393 if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
394 if (out->src != fr->src)
395 ast_free((void *) out->src);
400 newdata += AST_FRIENDLY_OFFSET;
401 out->offset = AST_FRIENDLY_OFFSET;
402 out->datalen = fr->datalen;
403 memcpy(newdata, fr->data, fr->datalen);
407 out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
412 struct ast_frame *ast_frdup(const struct ast_frame *f)
414 struct ast_frame *out = NULL;
418 #if !defined(LOW_MEMORY)
419 struct ast_frame_cache *frames;
422 /* Start with standard stuff */
423 len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
424 /* If we have a source, add space for it */
426 * XXX Watch out here - if we receive a src which is not terminated
427 * properly, we can be easily attacked. Should limit the size we deal with.
430 srclen = strlen(f->src);
434 #if !defined(LOW_MEMORY)
435 if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
436 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
437 if (out->mallocd_hdr_len >= len) {
438 size_t mallocd_len = out->mallocd_hdr_len;
440 AST_LIST_REMOVE_CURRENT(frame_list);
441 memset(out, 0, sizeof(*out));
442 out->mallocd_hdr_len = mallocd_len;
448 AST_LIST_TRAVERSE_SAFE_END;
453 if (!(buf = ast_calloc_cache(1, len)))
456 out->mallocd_hdr_len = len;
459 out->frametype = f->frametype;
460 out->subclass = f->subclass;
461 out->datalen = f->datalen;
462 out->samples = f->samples;
463 out->delivery = f->delivery;
464 /* Set us as having malloc'd header only, so it will eventually
466 out->mallocd = AST_MALLOCD_HDR;
467 out->offset = AST_FRIENDLY_OFFSET;
469 out->data = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
470 memcpy(out->data, f->data, out->datalen);
473 out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
474 /* Must have space since we allocated for it */
475 strcpy((char *)out->src, f->src);
477 ast_copy_flags(out, f, AST_FRFLAG_HAS_TIMING_INFO);
480 out->seqno = f->seqno;
484 void ast_swapcopy_samples(void *dst, const void *src, int samples)
487 unsigned short *dst_s = dst;
488 const unsigned short *src_s = src;
490 for (i = 0; i < samples; i++)
491 dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
495 struct ast_format_list *ast_get_format_list_index(int index)
497 return &AST_FORMAT_LIST[index];
500 struct ast_format_list *ast_get_format_list(size_t *size)
502 *size = (sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]));
503 return AST_FORMAT_LIST;
506 char* ast_getformatname(int format)
509 char *ret = "unknown";
510 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
511 if (AST_FORMAT_LIST[x].bits == format) {
512 ret = AST_FORMAT_LIST[x].name;
519 char *ast_getformatname_multiple(char *buf, size_t size, int format)
523 char *start, *end = buf;
527 snprintf(end, size, "0x%x (", format);
532 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
533 if (AST_FORMAT_LIST[x].bits & format) {
534 snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
541 ast_copy_string(start, "nothing)", size);
547 static struct ast_codec_alias_table {
550 } ast_codec_alias_table[] = {
551 { "slinear", "slin"},
552 { "slinear16", "slin16"},
556 static const char *ast_expand_codec_alias(const char *in)
560 for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(ast_codec_alias_table[0]); x++) {
561 if (!strcmp(in,ast_codec_alias_table[x].alias))
562 return ast_codec_alias_table[x].realname;
567 int ast_getformatbyname(const char *name)
569 int x, all, format = 0;
571 all = strcasecmp(name, "all") ? 0 : 1;
572 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
574 !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
575 !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name))) {
576 format |= AST_FORMAT_LIST[x].bits;
585 char *ast_codec2str(int codec)
588 char *ret = "unknown";
589 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
590 if (AST_FORMAT_LIST[x].bits == codec) {
591 ret = AST_FORMAT_LIST[x].desc;
598 static char *show_codecs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
605 e->command = "core show codecs [audio|video|image]";
607 "Usage: core show codecs [audio|video|image]\n"
608 " Displays codec mapping\n";
614 if ((a->argc < 3) || (a->argc > 4))
615 return CLI_SHOWUSAGE;
617 if (!ast_opt_dont_warn)
618 ast_cli(a->fd, "Disclaimer: this command is for informational purposes only.\n"
619 "\tIt does not indicate anything about your configuration.\n");
621 ast_cli(a->fd, "%11s %9s %10s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESC");
622 ast_cli(a->fd, "--------------------------------------------------------------------------------\n");
623 if ((a->argc == 3) || (!strcasecmp(a->argv[3],"audio"))) {
626 snprintf(hex,25,"(0x%x)",1<<i);
627 ast_cli(a->fd, "%11u (1 << %2d) %10s audio %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
631 if ((a->argc == 3) || (!strcasecmp(a->argv[3],"image"))) {
633 for (i=16;i<18;i++) {
634 snprintf(hex,25,"(0x%x)",1<<i);
635 ast_cli(a->fd, "%11u (1 << %2d) %10s image %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
639 if ((a->argc == 3) || (!strcasecmp(a->argv[3],"video"))) {
641 for (i=18;i<22;i++) {
642 snprintf(hex,25,"(0x%x)",1<<i);
643 ast_cli(a->fd, "%11u (1 << %2d) %10s video %8s (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
648 return CLI_SHOWUSAGE;
653 static char *show_codec_n(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
655 int codec, i, found=0;
659 e->command = "core show codec";
661 "Usage: core show codec <number>\n"
662 " Displays codec mapping\n";
669 return CLI_SHOWUSAGE;
671 if (sscanf(a->argv[3],"%d",&codec) != 1)
672 return CLI_SHOWUSAGE;
674 for (i = 0; i < 32; i++)
675 if (codec & (1 << i)) {
677 ast_cli(a->fd, "%11u (1 << %2d) %s\n",1 << i,i,ast_codec2str(1<<i));
681 ast_cli(a->fd, "Codec %d not found\n", codec);
686 /*! Dump a frame for debugging purposes */
687 void ast_frame_dump(const char *name, struct ast_frame *f, char *prefix)
689 const char noname[] = "unknown";
690 char ftype[40] = "Unknown Frametype";
692 char subclass[40] = "Unknown Subclass";
694 char moreinfo[40] = "";
698 const char *message = "Unknown";
705 ast_verbose("%s [ %s (NULL) ] [%s]\n",
706 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
707 term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
708 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
711 /* XXX We should probably print one each of voice and video when the format changes XXX */
712 if (f->frametype == AST_FRAME_VOICE)
714 if (f->frametype == AST_FRAME_VIDEO)
716 switch(f->frametype) {
717 case AST_FRAME_DTMF_BEGIN:
718 strcpy(ftype, "DTMF Begin");
719 subclass[0] = f->subclass;
722 case AST_FRAME_DTMF_END:
723 strcpy(ftype, "DTMF End");
724 subclass[0] = f->subclass;
727 case AST_FRAME_CONTROL:
728 strcpy(ftype, "Control");
729 switch(f->subclass) {
730 case AST_CONTROL_HANGUP:
731 strcpy(subclass, "Hangup");
733 case AST_CONTROL_RING:
734 strcpy(subclass, "Ring");
736 case AST_CONTROL_RINGING:
737 strcpy(subclass, "Ringing");
739 case AST_CONTROL_ANSWER:
740 strcpy(subclass, "Answer");
742 case AST_CONTROL_BUSY:
743 strcpy(subclass, "Busy");
745 case AST_CONTROL_TAKEOFFHOOK:
746 strcpy(subclass, "Take Off Hook");
748 case AST_CONTROL_OFFHOOK:
749 strcpy(subclass, "Line Off Hook");
751 case AST_CONTROL_CONGESTION:
752 strcpy(subclass, "Congestion");
754 case AST_CONTROL_FLASH:
755 strcpy(subclass, "Flash");
757 case AST_CONTROL_WINK:
758 strcpy(subclass, "Wink");
760 case AST_CONTROL_OPTION:
761 strcpy(subclass, "Option");
763 case AST_CONTROL_RADIO_KEY:
764 strcpy(subclass, "Key Radio");
766 case AST_CONTROL_RADIO_UNKEY:
767 strcpy(subclass, "Unkey Radio");
769 case AST_CONTROL_HOLD:
770 strcpy(subclass, "Hold");
772 case AST_CONTROL_UNHOLD:
773 strcpy(subclass, "Unhold");
775 case AST_CONTROL_T38:
776 if (f->datalen != sizeof(enum ast_control_t38)) {
779 enum ast_control_t38 state = *((enum ast_control_t38 *) f->data);
780 if (state == AST_T38_REQUEST_NEGOTIATE)
781 message = "Negotiation Requested";
782 else if (state == AST_T38_REQUEST_TERMINATE)
783 message = "Negotiation Request Terminated";
784 else if (state == AST_T38_NEGOTIATED)
785 message = "Negotiated";
786 else if (state == AST_T38_TERMINATED)
787 message = "Terminated";
788 else if (state == AST_T38_REFUSED)
791 snprintf(subclass, sizeof(subclass), "T38/%s", message);
794 strcpy(subclass, "Stop generators");
797 snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
801 strcpy(ftype, "Null Frame");
802 strcpy(subclass, "N/A");
805 /* Should never happen */
806 strcpy(ftype, "IAX Specific");
807 snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
810 strcpy(ftype, "Text");
811 strcpy(subclass, "N/A");
812 ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
814 case AST_FRAME_IMAGE:
815 strcpy(ftype, "Image");
816 snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
819 strcpy(ftype, "HTML");
820 switch(f->subclass) {
822 strcpy(subclass, "URL");
823 ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
826 strcpy(subclass, "Data");
829 strcpy(subclass, "Begin");
832 strcpy(subclass, "End");
834 case AST_HTML_LDCOMPLETE:
835 strcpy(subclass, "Load Complete");
837 case AST_HTML_NOSUPPORT:
838 strcpy(subclass, "No Support");
840 case AST_HTML_LINKURL:
841 strcpy(subclass, "Link URL");
842 ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
844 case AST_HTML_UNLINK:
845 strcpy(subclass, "Unlink");
847 case AST_HTML_LINKREJECT:
848 strcpy(subclass, "Link Reject");
851 snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
855 case AST_FRAME_MODEM:
856 strcpy(ftype, "Modem");
857 switch (f->subclass) {
859 strcpy(subclass, "T.38");
862 strcpy(subclass, "V.150");
865 snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass);
870 snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
872 if (!ast_strlen_zero(moreinfo))
873 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
874 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
875 term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
877 term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
879 term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
880 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
882 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
883 term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
884 term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
886 term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
888 term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
893 static char *show_frame_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
900 e->command = "core show frame stats";
902 "Usage: core show frame stats\n"
903 " Displays debugging statistics from framer\n";
910 return CLI_SHOWUSAGE;
911 AST_LIST_LOCK(&headerlist);
912 ast_cli(a->fd, " Framer Statistics \n");
913 ast_cli(a->fd, "---------------------------\n");
914 ast_cli(a->fd, "Total allocated headers: %d\n", headers);
915 ast_cli(a->fd, "Queue Dump:\n");
916 AST_LIST_TRAVERSE(&headerlist, f, frame_list)
917 ast_cli(a->fd, "%d. Type %d, subclass %d from %s\n", x++, f->frametype, f->subclass, f->src ? f->src : "<Unknown>");
918 AST_LIST_UNLOCK(&headerlist);
923 /* Builtin Asterisk CLI-commands for debugging */
924 static struct ast_cli_entry my_clis[] = {
925 AST_CLI_DEFINE(show_codecs, "Displays a list of codecs"),
926 AST_CLI_DEFINE(show_codec_n, "Shows a specific codec"),
928 AST_CLI_DEFINE(show_frame_stats, "Shows frame statistics"),
932 int init_framer(void)
934 ast_cli_register_multiple(my_clis, sizeof(my_clis) / sizeof(struct ast_cli_entry));
938 void ast_codec_pref_convert(struct ast_codec_pref *pref, char *buf, size_t size, int right)
940 int x, differential = (int) 'A', mem;
954 for (x = 0; x < 32 ; x++) {
957 to[x] = right ? (from[x] + differential) : (from[x] - differential);
961 int ast_codec_pref_string(struct ast_codec_pref *pref, char *buf, size_t size)
964 size_t total_len, slen;
971 for(x = 0; x < 32 ; x++) {
974 if (!(codec = ast_codec_pref_index(pref,x)))
976 if ((formatname = ast_getformatname(codec))) {
977 slen = strlen(formatname);
978 if (slen > total_len)
980 strncat(buf, formatname, total_len - 1); /* safe */
983 if (total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
984 strncat(buf, "|", total_len - 1); /* safe */
989 strncat(buf, ")", total_len - 1); /* safe */
993 return size - total_len;
996 int ast_codec_pref_index(struct ast_codec_pref *pref, int index)
1001 if ((index >= 0) && (index < sizeof(pref->order))) {
1002 slot = pref->order[index];
1005 return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
1008 /*! \brief Remove codec from pref list */
1009 void ast_codec_pref_remove(struct ast_codec_pref *pref, int format)
1011 struct ast_codec_pref oldorder;
1016 if (!pref->order[0])
1019 memcpy(&oldorder, pref, sizeof(oldorder));
1020 memset(pref, 0, sizeof(*pref));
1022 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1023 slot = oldorder.order[x];
1024 size = oldorder.framing[x];
1027 if (AST_FORMAT_LIST[slot-1].bits != format) {
1028 pref->order[y] = slot;
1029 pref->framing[y++] = size;
1035 /*! \brief Append codec to list */
1036 int ast_codec_pref_append(struct ast_codec_pref *pref, int format)
1038 int x, newindex = 0;
1040 ast_codec_pref_remove(pref, format);
1042 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1043 if (AST_FORMAT_LIST[x].bits == format) {
1050 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1051 if (!pref->order[x]) {
1052 pref->order[x] = newindex;
1061 /*! \brief Prepend codec to list */
1062 void ast_codec_pref_prepend(struct ast_codec_pref *pref, int format, int only_if_existing)
1064 int x, newindex = 0;
1066 /* First step is to get the codecs "index number" */
1067 for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
1068 if (AST_FORMAT_LIST[x].bits == format) {
1073 /* Done if its unknown */
1077 /* Now find any existing occurrence, or the end */
1078 for (x = 0; x < 32; x++) {
1079 if (!pref->order[x] || pref->order[x] == newindex)
1083 if (only_if_existing && !pref->order[x])
1086 /* Move down to make space to insert - either all the way to the end,
1087 or as far as the existing location (which will be overwritten) */
1088 for (; x > 0; x--) {
1089 pref->order[x] = pref->order[x - 1];
1090 pref->framing[x] = pref->framing[x - 1];
1093 /* And insert the new entry */
1094 pref->order[0] = newindex;
1095 pref->framing[0] = 0; /* ? */
1098 /*! \brief Set packet size for codec */
1099 int ast_codec_pref_setsize(struct ast_codec_pref *pref, int format, int framems)
1103 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1104 if (AST_FORMAT_LIST[x].bits == format) {
1113 /* size validation */
1115 framems = AST_FORMAT_LIST[index].def_ms;
1117 if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
1118 framems -= framems % AST_FORMAT_LIST[index].inc_ms;
1120 if (framems < AST_FORMAT_LIST[index].min_ms)
1121 framems = AST_FORMAT_LIST[index].min_ms;
1123 if (framems > AST_FORMAT_LIST[index].max_ms)
1124 framems = AST_FORMAT_LIST[index].max_ms;
1127 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1128 if (pref->order[x] == (index + 1)) {
1129 pref->framing[x] = framems;
1137 /*! \brief Get packet size for codec */
1138 struct ast_format_list ast_codec_pref_getsize(struct ast_codec_pref *pref, int format)
1140 int x, index = -1, framems = 0;
1141 struct ast_format_list fmt = { 0, };
1143 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1144 if (AST_FORMAT_LIST[x].bits == format) {
1145 fmt = AST_FORMAT_LIST[x];
1151 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1152 if (pref->order[x] == (index + 1)) {
1153 framems = pref->framing[x];
1158 /* size validation */
1160 framems = AST_FORMAT_LIST[index].def_ms;
1162 if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
1163 framems -= framems % AST_FORMAT_LIST[index].inc_ms;
1165 if (framems < AST_FORMAT_LIST[index].min_ms)
1166 framems = AST_FORMAT_LIST[index].min_ms;
1168 if (framems > AST_FORMAT_LIST[index].max_ms)
1169 framems = AST_FORMAT_LIST[index].max_ms;
1171 fmt.cur_ms = framems;
1176 /*! \brief Pick a codec */
1177 int ast_codec_choose(struct ast_codec_pref *pref, int formats, int find_best)
1179 int x, ret = 0, slot;
1181 for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
1182 slot = pref->order[x];
1186 if (formats & AST_FORMAT_LIST[slot-1].bits) {
1187 ret = AST_FORMAT_LIST[slot-1].bits;
1191 if (ret & AST_FORMAT_AUDIO_MASK)
1194 ast_debug(4, "Could not find preferred codec - %s\n", find_best ? "Going for the best codec" : "Returning zero codec");
1196 return find_best ? ast_best_codec(formats) : 0;
1199 int ast_parse_allow_disallow(struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
1202 char *parse = NULL, *this = NULL, *psize = NULL;
1203 int format = 0, framems = 0;
1205 parse = ast_strdupa(list);
1206 while ((this = strsep(&parse, ","))) {
1208 if ((psize = strrchr(this, ':'))) {
1210 ast_debug(1, "Packetization for codec: %s is %s\n", this, psize);
1211 framems = atoi(psize);
1215 ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
1218 if (!(format = ast_getformatbyname(this))) {
1219 ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
1231 /* Set up a preference list for audio. Do not include video in preferences
1232 since we can not transcode video and have to use whatever is offered
1234 if (pref && (format & AST_FORMAT_AUDIO_MASK)) {
1235 if (strcasecmp(this, "all")) {
1237 ast_codec_pref_append(pref, format);
1238 ast_codec_pref_setsize(pref, format, framems);
1241 ast_codec_pref_remove(pref, format);
1242 } else if (!allowing) {
1243 memset(pref, 0, sizeof(*pref));
1250 static int g723_len(unsigned char buf)
1252 enum frame_type type = buf & TYPE_MASK;
1268 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
1273 static int g723_samples(unsigned char *buf, int maxlen)
1278 while(pos < maxlen) {
1279 res = g723_len(buf[pos]);
1288 static unsigned char get_n_bits_at(unsigned char *data, int n, int bit)
1290 int byte = bit / 8; /* byte containing first bit */
1291 int rem = 8 - (bit % 8); /* remaining bits in first byte */
1292 unsigned char ret = 0;
1294 if (n <= 0 || n > 8)
1298 ret = (data[byte] << (n - rem));
1299 ret |= (data[byte + 1] >> (8 - n + rem));
1301 ret = (data[byte] >> (rem - n));
1304 return (ret & (0xff >> (8 - n)));
1307 static int speex_get_wb_sz_at(unsigned char *data, int len, int bit)
1309 static int SpeexWBSubModeSz[] = {
1315 /* skip up to two wideband frames */
1316 if (((len * 8 - off) >= 5) &&
1317 get_n_bits_at(data, 1, off)) {
1318 c = get_n_bits_at(data, 3, off + 1);
1319 off += SpeexWBSubModeSz[c];
1321 if (((len * 8 - off) >= 5) &&
1322 get_n_bits_at(data, 1, off)) {
1323 c = get_n_bits_at(data, 3, off + 1);
1324 off += SpeexWBSubModeSz[c];
1326 if (((len * 8 - off) >= 5) &&
1327 get_n_bits_at(data, 1, off)) {
1328 ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
1337 static int speex_samples(unsigned char *data, int len)
1339 static int SpeexSubModeSz[] = {
1344 static int SpeexInBandSz[] = {
1354 while ((len * 8 - bit) >= 5) {
1355 /* skip wideband frames */
1356 off = speex_get_wb_sz_at(data, len, bit);
1358 ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
1363 if ((len * 8 - bit) < 5) {
1364 ast_log(LOG_WARNING, "Not enough bits remaining after wide band for speex samples.\n");
1368 /* get control bits */
1369 c = get_n_bits_at(data, 5, bit);
1375 } else if (c == 14) {
1376 /* in-band signal; next 4 bits contain signal id */
1377 c = get_n_bits_at(data, 4, bit);
1379 bit += SpeexInBandSz[c];
1380 } else if (c == 13) {
1381 /* user in-band; next 5 bits contain msg len */
1382 c = get_n_bits_at(data, 5, bit);
1389 /* skip number bits for submode (less the 5 control bits) */
1390 bit += SpeexSubModeSz[c] - 5;
1391 cnt += 160; /* new frame */
1397 int ast_codec_get_samples(struct ast_frame *f)
1400 switch(f->subclass) {
1401 case AST_FORMAT_SPEEX:
1402 samples = speex_samples(f->data, f->datalen);
1404 case AST_FORMAT_G723_1:
1405 samples = g723_samples(f->data, f->datalen);
1407 case AST_FORMAT_ILBC:
1408 samples = 240 * (f->datalen / 50);
1410 case AST_FORMAT_GSM:
1411 samples = 160 * (f->datalen / 33);
1413 case AST_FORMAT_G729A:
1414 samples = f->datalen * 8;
1416 case AST_FORMAT_SLINEAR:
1417 case AST_FORMAT_SLINEAR16:
1418 samples = f->datalen / 2;
1420 case AST_FORMAT_LPC10:
1421 /* assumes that the RTP packet contains one LPC10 frame */
1423 samples += (((char *)(f->data))[7] & 0x1) * 8;
1425 case AST_FORMAT_ULAW:
1426 case AST_FORMAT_ALAW:
1427 samples = f->datalen;
1429 case AST_FORMAT_G722:
1430 case AST_FORMAT_ADPCM:
1431 case AST_FORMAT_G726:
1432 case AST_FORMAT_G726_AAL2:
1433 samples = f->datalen * 2;
1436 ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
1441 int ast_codec_get_len(int format, int samples)
1445 /* XXX Still need speex, g723, and lpc10 XXX */
1447 case AST_FORMAT_G723_1:
1448 len = (samples / 240) * 20;
1450 case AST_FORMAT_ILBC:
1451 len = (samples / 240) * 50;
1453 case AST_FORMAT_GSM:
1454 len = (samples / 160) * 33;
1456 case AST_FORMAT_G729A:
1459 case AST_FORMAT_SLINEAR:
1460 case AST_FORMAT_SLINEAR16:
1463 case AST_FORMAT_ULAW:
1464 case AST_FORMAT_ALAW:
1467 case AST_FORMAT_G722:
1468 case AST_FORMAT_ADPCM:
1469 case AST_FORMAT_G726:
1470 case AST_FORMAT_G726_AAL2:
1474 ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
1480 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment)
1483 short *fdata = f->data;
1484 short adjust_value = abs(adjustment);
1486 if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
1492 for (count = 0; count < f->samples; count++) {
1493 if (adjustment > 0) {
1494 ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
1495 } else if (adjustment < 0) {
1496 ast_slinear_saturated_divide(&fdata[count], &adjust_value);
1503 int ast_frame_slinear_sum(struct ast_frame *f1, struct ast_frame *f2)
1506 short *data1, *data2;
1508 if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass != AST_FORMAT_SLINEAR))
1511 if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass != AST_FORMAT_SLINEAR))
1514 if (f1->samples != f2->samples)
1517 for (count = 0, data1 = f1->data, data2 = f2->data;
1518 count < f1->samples;
1519 count++, data1++, data2++)
1520 ast_slinear_saturated_add(data1, data2);