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"
40 #include "asterisk/dsp.h"
44 static AST_LIST_HEAD_STATIC(headerlist
, ast_frame
);
47 #if !defined(LOW_MEMORY)
48 static void frame_cache_cleanup(void *data
);
50 /*! \brief A per-thread cache of frame headers */
51 AST_THREADSTORAGE_CUSTOM(frame_cache
, NULL
, frame_cache_cleanup
);
54 * \brief Maximum ast_frame cache size
56 * In most cases where the frame header cache will be useful, the size
57 * of the cache will stay very small. However, it is not always the case that
58 * the same thread that allocates the frame will be the one freeing them, so
59 * sometimes a thread will never have any frames in its cache, or the cache
60 * will never be pulled from. For the latter case, we limit the maximum size.
62 #define FRAME_CACHE_MAX_SIZE 10
64 /*! \brief This is just so ast_frames, a list head struct for holding a list of
65 * ast_frame structures, is defined. */
66 AST_LIST_HEAD_NOLOCK(ast_frames
, ast_frame
);
68 struct ast_frame_cache
{
69 struct ast_frames list
;
74 #define SMOOTHER_SIZE 8000
79 TYPE_SILENCE
, /* 0x2 */
80 TYPE_DONTSEND
/* 0x3 */
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
);
311 else if (ast_test_flag(fr
, AST_FRFLAG_FROM_DSP
))
312 ast_dsp_frame_freed(fr
);
317 #if !defined(LOW_MEMORY)
318 if (cache
&& fr
->mallocd
== AST_MALLOCD_HDR
) {
319 /* Cool, only the header is malloc'd, let's just cache those for now
320 * to keep things simple... */
321 struct ast_frame_cache
*frames
;
323 if ((frames
= ast_threadstorage_get(&frame_cache
, sizeof(*frames
)))
324 && frames
->size
< FRAME_CACHE_MAX_SIZE
) {
325 AST_LIST_INSERT_HEAD(&frames
->list
, fr
, frame_list
);
332 if (fr
->mallocd
& AST_MALLOCD_DATA
) {
334 ast_free(fr
->data
- fr
->offset
);
336 if (fr
->mallocd
& AST_MALLOCD_SRC
) {
338 ast_free((char *)fr
->src
);
340 if (fr
->mallocd
& AST_MALLOCD_HDR
) {
342 AST_LIST_LOCK(&headerlist
);
344 AST_LIST_REMOVE(&headerlist
, fr
, frame_list
);
345 AST_LIST_UNLOCK(&headerlist
);
352 * \brief 'isolates' a frame by duplicating non-malloc'ed components
353 * (header, src, data).
354 * On return all components are malloc'ed
356 struct ast_frame
*ast_frisolate(struct ast_frame
*fr
)
358 struct ast_frame
*out
;
361 ast_clear_flag(fr
, AST_FRFLAG_FROM_TRANSLATOR
);
362 ast_clear_flag(fr
, AST_FRFLAG_FROM_DSP
);
364 if (!(fr
->mallocd
& AST_MALLOCD_HDR
)) {
365 /* Allocate a new header if needed */
366 if (!(out
= ast_frame_header_new()))
368 out
->frametype
= fr
->frametype
;
369 out
->subclass
= fr
->subclass
;
370 out
->datalen
= fr
->datalen
;
371 out
->samples
= fr
->samples
;
372 out
->offset
= fr
->offset
;
373 out
->data
= fr
->data
;
374 /* Copy the timing data */
375 ast_copy_flags(out
, fr
, AST_FRFLAG_HAS_TIMING_INFO
);
376 if (ast_test_flag(fr
, AST_FRFLAG_HAS_TIMING_INFO
)) {
379 out
->seqno
= fr
->seqno
;
384 if (!(fr
->mallocd
& AST_MALLOCD_SRC
)) {
386 if (!(out
->src
= ast_strdup(fr
->src
))) {
395 if (!(fr
->mallocd
& AST_MALLOCD_DATA
)) {
396 if (!(newdata
= ast_malloc(fr
->datalen
+ AST_FRIENDLY_OFFSET
))) {
397 if (out
->src
!= fr
->src
)
398 ast_free((void *) out
->src
);
403 newdata
+= AST_FRIENDLY_OFFSET
;
404 out
->offset
= AST_FRIENDLY_OFFSET
;
405 out
->datalen
= fr
->datalen
;
406 memcpy(newdata
, fr
->data
, fr
->datalen
);
410 out
->mallocd
= AST_MALLOCD_HDR
| AST_MALLOCD_SRC
| AST_MALLOCD_DATA
;
415 struct ast_frame
*ast_frdup(const struct ast_frame
*f
)
417 struct ast_frame
*out
= NULL
;
421 #if !defined(LOW_MEMORY)
422 struct ast_frame_cache
*frames
;
425 /* Start with standard stuff */
426 len
= sizeof(*out
) + AST_FRIENDLY_OFFSET
+ f
->datalen
;
427 /* If we have a source, add space for it */
429 * XXX Watch out here - if we receive a src which is not terminated
430 * properly, we can be easily attacked. Should limit the size we deal with.
433 srclen
= strlen(f
->src
);
437 #if !defined(LOW_MEMORY)
438 if ((frames
= ast_threadstorage_get(&frame_cache
, sizeof(*frames
)))) {
439 AST_LIST_TRAVERSE_SAFE_BEGIN(&frames
->list
, out
, frame_list
) {
440 if (out
->mallocd_hdr_len
>= len
) {
441 size_t mallocd_len
= out
->mallocd_hdr_len
;
443 AST_LIST_REMOVE_CURRENT(frame_list
);
444 memset(out
, 0, sizeof(*out
));
445 out
->mallocd_hdr_len
= mallocd_len
;
451 AST_LIST_TRAVERSE_SAFE_END
;
456 if (!(buf
= ast_calloc_cache(1, len
)))
459 out
->mallocd_hdr_len
= len
;
462 out
->frametype
= f
->frametype
;
463 out
->subclass
= f
->subclass
;
464 out
->datalen
= f
->datalen
;
465 out
->samples
= f
->samples
;
466 out
->delivery
= f
->delivery
;
467 /* Set us as having malloc'd header only, so it will eventually
469 out
->mallocd
= AST_MALLOCD_HDR
;
470 out
->offset
= AST_FRIENDLY_OFFSET
;
472 out
->data
= buf
+ sizeof(*out
) + AST_FRIENDLY_OFFSET
;
473 memcpy(out
->data
, f
->data
, out
->datalen
);
476 out
->src
= buf
+ sizeof(*out
) + AST_FRIENDLY_OFFSET
+ f
->datalen
;
477 /* Must have space since we allocated for it */
478 strcpy((char *)out
->src
, f
->src
);
480 ast_copy_flags(out
, f
, AST_FRFLAG_HAS_TIMING_INFO
);
483 out
->seqno
= f
->seqno
;
487 void ast_swapcopy_samples(void *dst
, const void *src
, int samples
)
490 unsigned short *dst_s
= dst
;
491 const unsigned short *src_s
= src
;
493 for (i
= 0; i
< samples
; i
++)
494 dst_s
[i
] = (src_s
[i
]<<8) | (src_s
[i
]>>8);
498 struct ast_format_list
*ast_get_format_list_index(int index
)
500 return &AST_FORMAT_LIST
[index
];
503 struct ast_format_list
*ast_get_format_list(size_t *size
)
505 *size
= (sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]));
506 return AST_FORMAT_LIST
;
509 char* ast_getformatname(int format
)
512 char *ret
= "unknown";
513 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
514 if (AST_FORMAT_LIST
[x
].bits
== format
) {
515 ret
= AST_FORMAT_LIST
[x
].name
;
522 char *ast_getformatname_multiple(char *buf
, size_t size
, int format
)
526 char *start
, *end
= buf
;
530 snprintf(end
, size
, "0x%x (", format
);
535 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
536 if (AST_FORMAT_LIST
[x
].bits
& format
) {
537 snprintf(end
, size
,"%s|",AST_FORMAT_LIST
[x
].name
);
544 ast_copy_string(start
, "nothing)", size
);
550 static struct ast_codec_alias_table
{
553 } ast_codec_alias_table
[] = {
554 { "slinear", "slin"},
555 { "slinear16", "slin16"},
559 static const char *ast_expand_codec_alias(const char *in
)
563 for (x
= 0; x
< sizeof(ast_codec_alias_table
) / sizeof(ast_codec_alias_table
[0]); x
++) {
564 if (!strcmp(in
,ast_codec_alias_table
[x
].alias
))
565 return ast_codec_alias_table
[x
].realname
;
570 int ast_getformatbyname(const char *name
)
572 int x
, all
, format
= 0;
574 all
= strcasecmp(name
, "all") ? 0 : 1;
575 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
577 !strcasecmp(AST_FORMAT_LIST
[x
].name
,name
) ||
578 !strcasecmp(AST_FORMAT_LIST
[x
].name
,ast_expand_codec_alias(name
))) {
579 format
|= AST_FORMAT_LIST
[x
].bits
;
588 char *ast_codec2str(int codec
)
591 char *ret
= "unknown";
592 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
593 if (AST_FORMAT_LIST
[x
].bits
== codec
) {
594 ret
= AST_FORMAT_LIST
[x
].desc
;
601 static char *show_codecs(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
608 e
->command
= "core show codecs [audio|video|image]";
610 "Usage: core show codecs [audio|video|image]\n"
611 " Displays codec mapping\n";
617 if ((a
->argc
< 3) || (a
->argc
> 4))
618 return CLI_SHOWUSAGE
;
620 if (!ast_opt_dont_warn
)
621 ast_cli(a
->fd
, "Disclaimer: this command is for informational purposes only.\n"
622 "\tIt does not indicate anything about your configuration.\n");
624 ast_cli(a
->fd
, "%11s %9s %10s TYPE %8s %s\n","INT","BINARY","HEX","NAME","DESC");
625 ast_cli(a
->fd
, "--------------------------------------------------------------------------------\n");
626 if ((a
->argc
== 3) || (!strcasecmp(a
->argv
[3],"audio"))) {
629 snprintf(hex
,25,"(0x%x)",1<<i
);
630 ast_cli(a
->fd
, "%11u (1 << %2d) %10s audio %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
634 if ((a
->argc
== 3) || (!strcasecmp(a
->argv
[3],"image"))) {
636 for (i
=16;i
<18;i
++) {
637 snprintf(hex
,25,"(0x%x)",1<<i
);
638 ast_cli(a
->fd
, "%11u (1 << %2d) %10s image %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
642 if ((a
->argc
== 3) || (!strcasecmp(a
->argv
[3],"video"))) {
644 for (i
=18;i
<22;i
++) {
645 snprintf(hex
,25,"(0x%x)",1<<i
);
646 ast_cli(a
->fd
, "%11u (1 << %2d) %10s video %8s (%s)\n",1 << i
,i
,hex
,ast_getformatname(1<<i
),ast_codec2str(1<<i
));
651 return CLI_SHOWUSAGE
;
656 static char *show_codec_n(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
658 int codec
, i
, found
=0;
662 e
->command
= "core show codec";
664 "Usage: core show codec <number>\n"
665 " Displays codec mapping\n";
672 return CLI_SHOWUSAGE
;
674 if (sscanf(a
->argv
[3],"%d",&codec
) != 1)
675 return CLI_SHOWUSAGE
;
677 for (i
= 0; i
< 32; i
++)
678 if (codec
& (1 << i
)) {
680 ast_cli(a
->fd
, "%11u (1 << %2d) %s\n",1 << i
,i
,ast_codec2str(1<<i
));
684 ast_cli(a
->fd
, "Codec %d not found\n", codec
);
689 /*! Dump a frame for debugging purposes */
690 void ast_frame_dump(const char *name
, struct ast_frame
*f
, char *prefix
)
692 const char noname
[] = "unknown";
693 char ftype
[40] = "Unknown Frametype";
695 char subclass
[40] = "Unknown Subclass";
697 char moreinfo
[40] = "";
701 const char *message
= "Unknown";
708 ast_verbose("%s [ %s (NULL) ] [%s]\n",
709 term_color(cp
, prefix
, COLOR_BRMAGENTA
, COLOR_BLACK
, sizeof(cp
)),
710 term_color(cft
, "HANGUP", COLOR_BRRED
, COLOR_BLACK
, sizeof(cft
)),
711 term_color(cn
, name
, COLOR_YELLOW
, COLOR_BLACK
, sizeof(cn
)));
714 /* XXX We should probably print one each of voice and video when the format changes XXX */
715 if (f
->frametype
== AST_FRAME_VOICE
)
717 if (f
->frametype
== AST_FRAME_VIDEO
)
719 switch(f
->frametype
) {
720 case AST_FRAME_DTMF_BEGIN
:
721 strcpy(ftype
, "DTMF Begin");
722 subclass
[0] = f
->subclass
;
725 case AST_FRAME_DTMF_END
:
726 strcpy(ftype
, "DTMF End");
727 subclass
[0] = f
->subclass
;
730 case AST_FRAME_CONTROL
:
731 strcpy(ftype
, "Control");
732 switch(f
->subclass
) {
733 case AST_CONTROL_HANGUP
:
734 strcpy(subclass
, "Hangup");
736 case AST_CONTROL_RING
:
737 strcpy(subclass
, "Ring");
739 case AST_CONTROL_RINGING
:
740 strcpy(subclass
, "Ringing");
742 case AST_CONTROL_ANSWER
:
743 strcpy(subclass
, "Answer");
745 case AST_CONTROL_BUSY
:
746 strcpy(subclass
, "Busy");
748 case AST_CONTROL_TAKEOFFHOOK
:
749 strcpy(subclass
, "Take Off Hook");
751 case AST_CONTROL_OFFHOOK
:
752 strcpy(subclass
, "Line Off Hook");
754 case AST_CONTROL_CONGESTION
:
755 strcpy(subclass
, "Congestion");
757 case AST_CONTROL_FLASH
:
758 strcpy(subclass
, "Flash");
760 case AST_CONTROL_WINK
:
761 strcpy(subclass
, "Wink");
763 case AST_CONTROL_OPTION
:
764 strcpy(subclass
, "Option");
766 case AST_CONTROL_RADIO_KEY
:
767 strcpy(subclass
, "Key Radio");
769 case AST_CONTROL_RADIO_UNKEY
:
770 strcpy(subclass
, "Unkey Radio");
772 case AST_CONTROL_HOLD
:
773 strcpy(subclass
, "Hold");
775 case AST_CONTROL_UNHOLD
:
776 strcpy(subclass
, "Unhold");
778 case AST_CONTROL_T38
:
779 if (f
->datalen
!= sizeof(enum ast_control_t38
)) {
782 enum ast_control_t38 state
= *((enum ast_control_t38
*) f
->data
);
783 if (state
== AST_T38_REQUEST_NEGOTIATE
)
784 message
= "Negotiation Requested";
785 else if (state
== AST_T38_REQUEST_TERMINATE
)
786 message
= "Negotiation Request Terminated";
787 else if (state
== AST_T38_NEGOTIATED
)
788 message
= "Negotiated";
789 else if (state
== AST_T38_TERMINATED
)
790 message
= "Terminated";
791 else if (state
== AST_T38_REFUSED
)
794 snprintf(subclass
, sizeof(subclass
), "T38/%s", message
);
797 strcpy(subclass
, "Stop generators");
800 snprintf(subclass
, sizeof(subclass
), "Unknown control '%d'", f
->subclass
);
804 strcpy(ftype
, "Null Frame");
805 strcpy(subclass
, "N/A");
808 /* Should never happen */
809 strcpy(ftype
, "IAX Specific");
810 snprintf(subclass
, sizeof(subclass
), "IAX Frametype %d", f
->subclass
);
813 strcpy(ftype
, "Text");
814 strcpy(subclass
, "N/A");
815 ast_copy_string(moreinfo
, f
->data
, sizeof(moreinfo
));
817 case AST_FRAME_IMAGE
:
818 strcpy(ftype
, "Image");
819 snprintf(subclass
, sizeof(subclass
), "Image format %s\n", ast_getformatname(f
->subclass
));
822 strcpy(ftype
, "HTML");
823 switch(f
->subclass
) {
825 strcpy(subclass
, "URL");
826 ast_copy_string(moreinfo
, f
->data
, sizeof(moreinfo
));
829 strcpy(subclass
, "Data");
832 strcpy(subclass
, "Begin");
835 strcpy(subclass
, "End");
837 case AST_HTML_LDCOMPLETE
:
838 strcpy(subclass
, "Load Complete");
840 case AST_HTML_NOSUPPORT
:
841 strcpy(subclass
, "No Support");
843 case AST_HTML_LINKURL
:
844 strcpy(subclass
, "Link URL");
845 ast_copy_string(moreinfo
, f
->data
, sizeof(moreinfo
));
847 case AST_HTML_UNLINK
:
848 strcpy(subclass
, "Unlink");
850 case AST_HTML_LINKREJECT
:
851 strcpy(subclass
, "Link Reject");
854 snprintf(subclass
, sizeof(subclass
), "Unknown HTML frame '%d'\n", f
->subclass
);
858 case AST_FRAME_MODEM
:
859 strcpy(ftype
, "Modem");
860 switch (f
->subclass
) {
862 strcpy(subclass
, "T.38");
865 strcpy(subclass
, "V.150");
868 snprintf(subclass
, sizeof(subclass
), "Unknown MODEM frame '%d'\n", f
->subclass
);
873 snprintf(ftype
, sizeof(ftype
), "Unknown Frametype '%d'", f
->frametype
);
875 if (!ast_strlen_zero(moreinfo
))
876 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",
877 term_color(cp
, prefix
, COLOR_BRMAGENTA
, COLOR_BLACK
, sizeof(cp
)),
878 term_color(cft
, ftype
, COLOR_BRRED
, COLOR_BLACK
, sizeof(cft
)),
880 term_color(csub
, subclass
, COLOR_BRCYAN
, COLOR_BLACK
, sizeof(csub
)),
882 term_color(cmn
, moreinfo
, COLOR_BRGREEN
, COLOR_BLACK
, sizeof(cmn
)),
883 term_color(cn
, name
, COLOR_YELLOW
, COLOR_BLACK
, sizeof(cn
)));
885 ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",
886 term_color(cp
, prefix
, COLOR_BRMAGENTA
, COLOR_BLACK
, sizeof(cp
)),
887 term_color(cft
, ftype
, COLOR_BRRED
, COLOR_BLACK
, sizeof(cft
)),
889 term_color(csub
, subclass
, COLOR_BRCYAN
, COLOR_BLACK
, sizeof(csub
)),
891 term_color(cn
, name
, COLOR_YELLOW
, COLOR_BLACK
, sizeof(cn
)));
896 static char *show_frame_stats(struct ast_cli_entry
*e
, int cmd
, struct ast_cli_args
*a
)
903 e
->command
= "core show frame stats";
905 "Usage: core show frame stats\n"
906 " Displays debugging statistics from framer\n";
913 return CLI_SHOWUSAGE
;
914 AST_LIST_LOCK(&headerlist
);
915 ast_cli(a
->fd
, " Framer Statistics \n");
916 ast_cli(a
->fd
, "---------------------------\n");
917 ast_cli(a
->fd
, "Total allocated headers: %d\n", headers
);
918 ast_cli(a
->fd
, "Queue Dump:\n");
919 AST_LIST_TRAVERSE(&headerlist
, f
, frame_list
)
920 ast_cli(a
->fd
, "%d. Type %d, subclass %d from %s\n", x
++, f
->frametype
, f
->subclass
, f
->src
? f
->src
: "<Unknown>");
921 AST_LIST_UNLOCK(&headerlist
);
926 /* Builtin Asterisk CLI-commands for debugging */
927 static struct ast_cli_entry my_clis
[] = {
928 AST_CLI_DEFINE(show_codecs
, "Displays a list of codecs"),
929 AST_CLI_DEFINE(show_codec_n
, "Shows a specific codec"),
931 AST_CLI_DEFINE(show_frame_stats
, "Shows frame statistics"),
935 int init_framer(void)
937 ast_cli_register_multiple(my_clis
, sizeof(my_clis
) / sizeof(struct ast_cli_entry
));
941 void ast_codec_pref_convert(struct ast_codec_pref
*pref
, char *buf
, size_t size
, int right
)
943 int x
, differential
= (int) 'A', mem
;
957 for (x
= 0; x
< 32 ; x
++) {
960 to
[x
] = right
? (from
[x
] + differential
) : (from
[x
] - differential
);
964 int ast_codec_pref_string(struct ast_codec_pref
*pref
, char *buf
, size_t size
)
967 size_t total_len
, slen
;
974 for(x
= 0; x
< 32 ; x
++) {
977 if (!(codec
= ast_codec_pref_index(pref
,x
)))
979 if ((formatname
= ast_getformatname(codec
))) {
980 slen
= strlen(formatname
);
981 if (slen
> total_len
)
983 strncat(buf
, formatname
, total_len
- 1); /* safe */
986 if (total_len
&& x
< 31 && ast_codec_pref_index(pref
, x
+ 1)) {
987 strncat(buf
, "|", total_len
- 1); /* safe */
992 strncat(buf
, ")", total_len
- 1); /* safe */
996 return size
- total_len
;
999 int ast_codec_pref_index(struct ast_codec_pref
*pref
, int index
)
1004 if ((index
>= 0) && (index
< sizeof(pref
->order
))) {
1005 slot
= pref
->order
[index
];
1008 return slot
? AST_FORMAT_LIST
[slot
-1].bits
: 0;
1011 /*! \brief Remove codec from pref list */
1012 void ast_codec_pref_remove(struct ast_codec_pref
*pref
, int format
)
1014 struct ast_codec_pref oldorder
;
1019 if (!pref
->order
[0])
1022 memcpy(&oldorder
, pref
, sizeof(oldorder
));
1023 memset(pref
, 0, sizeof(*pref
));
1025 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1026 slot
= oldorder
.order
[x
];
1027 size
= oldorder
.framing
[x
];
1030 if (AST_FORMAT_LIST
[slot
-1].bits
!= format
) {
1031 pref
->order
[y
] = slot
;
1032 pref
->framing
[y
++] = size
;
1038 /*! \brief Append codec to list */
1039 int ast_codec_pref_append(struct ast_codec_pref
*pref
, int format
)
1041 int x
, newindex
= 0;
1043 ast_codec_pref_remove(pref
, format
);
1045 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1046 if (AST_FORMAT_LIST
[x
].bits
== format
) {
1053 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1054 if (!pref
->order
[x
]) {
1055 pref
->order
[x
] = newindex
;
1064 /*! \brief Prepend codec to list */
1065 void ast_codec_pref_prepend(struct ast_codec_pref
*pref
, int format
, int only_if_existing
)
1067 int x
, newindex
= 0;
1069 /* First step is to get the codecs "index number" */
1070 for (x
= 0; x
< ARRAY_LEN(AST_FORMAT_LIST
); x
++) {
1071 if (AST_FORMAT_LIST
[x
].bits
== format
) {
1076 /* Done if its unknown */
1080 /* Now find any existing occurrence, or the end */
1081 for (x
= 0; x
< 32; x
++) {
1082 if (!pref
->order
[x
] || pref
->order
[x
] == newindex
)
1086 if (only_if_existing
&& !pref
->order
[x
])
1089 /* Move down to make space to insert - either all the way to the end,
1090 or as far as the existing location (which will be overwritten) */
1091 for (; x
> 0; x
--) {
1092 pref
->order
[x
] = pref
->order
[x
- 1];
1093 pref
->framing
[x
] = pref
->framing
[x
- 1];
1096 /* And insert the new entry */
1097 pref
->order
[0] = newindex
;
1098 pref
->framing
[0] = 0; /* ? */
1101 /*! \brief Set packet size for codec */
1102 int ast_codec_pref_setsize(struct ast_codec_pref
*pref
, int format
, int framems
)
1106 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1107 if (AST_FORMAT_LIST
[x
].bits
== format
) {
1116 /* size validation */
1118 framems
= AST_FORMAT_LIST
[index
].def_ms
;
1120 if (AST_FORMAT_LIST
[index
].inc_ms
&& framems
% AST_FORMAT_LIST
[index
].inc_ms
) /* avoid division by zero */
1121 framems
-= framems
% AST_FORMAT_LIST
[index
].inc_ms
;
1123 if (framems
< AST_FORMAT_LIST
[index
].min_ms
)
1124 framems
= AST_FORMAT_LIST
[index
].min_ms
;
1126 if (framems
> AST_FORMAT_LIST
[index
].max_ms
)
1127 framems
= AST_FORMAT_LIST
[index
].max_ms
;
1130 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1131 if (pref
->order
[x
] == (index
+ 1)) {
1132 pref
->framing
[x
] = framems
;
1140 /*! \brief Get packet size for codec */
1141 struct ast_format_list
ast_codec_pref_getsize(struct ast_codec_pref
*pref
, int format
)
1143 int x
, index
= -1, framems
= 0;
1144 struct ast_format_list fmt
= { 0, };
1146 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1147 if (AST_FORMAT_LIST
[x
].bits
== format
) {
1148 fmt
= AST_FORMAT_LIST
[x
];
1154 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1155 if (pref
->order
[x
] == (index
+ 1)) {
1156 framems
= pref
->framing
[x
];
1161 /* size validation */
1163 framems
= AST_FORMAT_LIST
[index
].def_ms
;
1165 if (AST_FORMAT_LIST
[index
].inc_ms
&& framems
% AST_FORMAT_LIST
[index
].inc_ms
) /* avoid division by zero */
1166 framems
-= framems
% AST_FORMAT_LIST
[index
].inc_ms
;
1168 if (framems
< AST_FORMAT_LIST
[index
].min_ms
)
1169 framems
= AST_FORMAT_LIST
[index
].min_ms
;
1171 if (framems
> AST_FORMAT_LIST
[index
].max_ms
)
1172 framems
= AST_FORMAT_LIST
[index
].max_ms
;
1174 fmt
.cur_ms
= framems
;
1179 /*! \brief Pick a codec */
1180 int ast_codec_choose(struct ast_codec_pref
*pref
, int formats
, int find_best
)
1182 int x
, ret
= 0, slot
;
1184 for (x
= 0; x
< sizeof(AST_FORMAT_LIST
) / sizeof(AST_FORMAT_LIST
[0]); x
++) {
1185 slot
= pref
->order
[x
];
1189 if (formats
& AST_FORMAT_LIST
[slot
-1].bits
) {
1190 ret
= AST_FORMAT_LIST
[slot
-1].bits
;
1194 if (ret
& AST_FORMAT_AUDIO_MASK
)
1197 ast_debug(4, "Could not find preferred codec - %s\n", find_best
? "Going for the best codec" : "Returning zero codec");
1199 return find_best
? ast_best_codec(formats
) : 0;
1202 int ast_parse_allow_disallow(struct ast_codec_pref
*pref
, int *mask
, const char *list
, int allowing
)
1205 char *parse
= NULL
, *this = NULL
, *psize
= NULL
;
1206 int format
= 0, framems
= 0;
1208 parse
= ast_strdupa(list
);
1209 while ((this = strsep(&parse
, ","))) {
1211 if ((psize
= strrchr(this, ':'))) {
1213 ast_debug(1, "Packetization for codec: %s is %s\n", this, psize
);
1214 framems
= atoi(psize
);
1218 ast_log(LOG_WARNING
, "Bad packetization value for codec %s\n", this);
1221 if (!(format
= ast_getformatbyname(this))) {
1222 ast_log(LOG_WARNING
, "Cannot %s unknown format '%s'\n", allowing
? "allow" : "disallow", this);
1234 /* Set up a preference list for audio. Do not include video in preferences
1235 since we can not transcode video and have to use whatever is offered
1237 if (pref
&& (format
& AST_FORMAT_AUDIO_MASK
)) {
1238 if (strcasecmp(this, "all")) {
1240 ast_codec_pref_append(pref
, format
);
1241 ast_codec_pref_setsize(pref
, format
, framems
);
1244 ast_codec_pref_remove(pref
, format
);
1245 } else if (!allowing
) {
1246 memset(pref
, 0, sizeof(*pref
));
1253 static int g723_len(unsigned char buf
)
1255 enum frame_type type
= buf
& TYPE_MASK
;
1271 ast_log(LOG_WARNING
, "Badly encoded frame (%d)\n", type
);
1276 static int g723_samples(unsigned char *buf
, int maxlen
)
1281 while(pos
< maxlen
) {
1282 res
= g723_len(buf
[pos
]);
1291 static unsigned char get_n_bits_at(unsigned char *data
, int n
, int bit
)
1293 int byte
= bit
/ 8; /* byte containing first bit */
1294 int rem
= 8 - (bit
% 8); /* remaining bits in first byte */
1295 unsigned char ret
= 0;
1297 if (n
<= 0 || n
> 8)
1301 ret
= (data
[byte
] << (n
- rem
));
1302 ret
|= (data
[byte
+ 1] >> (8 - n
+ rem
));
1304 ret
= (data
[byte
] >> (rem
- n
));
1307 return (ret
& (0xff >> (8 - n
)));
1310 static int speex_get_wb_sz_at(unsigned char *data
, int len
, int bit
)
1312 static int SpeexWBSubModeSz
[] = {
1318 /* skip up to two wideband frames */
1319 if (((len
* 8 - off
) >= 5) &&
1320 get_n_bits_at(data
, 1, off
)) {
1321 c
= get_n_bits_at(data
, 3, off
+ 1);
1322 off
+= SpeexWBSubModeSz
[c
];
1324 if (((len
* 8 - off
) >= 5) &&
1325 get_n_bits_at(data
, 1, off
)) {
1326 c
= get_n_bits_at(data
, 3, off
+ 1);
1327 off
+= SpeexWBSubModeSz
[c
];
1329 if (((len
* 8 - off
) >= 5) &&
1330 get_n_bits_at(data
, 1, off
)) {
1331 ast_log(LOG_WARNING
, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
1340 static int speex_samples(unsigned char *data
, int len
)
1342 static int SpeexSubModeSz
[] = {
1347 static int SpeexInBandSz
[] = {
1357 while ((len
* 8 - bit
) >= 5) {
1358 /* skip wideband frames */
1359 off
= speex_get_wb_sz_at(data
, len
, bit
);
1361 ast_log(LOG_WARNING
, "Had error while reading wideband frames for speex samples\n");
1366 if ((len
* 8 - bit
) < 5) {
1367 ast_log(LOG_WARNING
, "Not enough bits remaining after wide band for speex samples.\n");
1371 /* get control bits */
1372 c
= get_n_bits_at(data
, 5, bit
);
1378 } else if (c
== 14) {
1379 /* in-band signal; next 4 bits contain signal id */
1380 c
= get_n_bits_at(data
, 4, bit
);
1382 bit
+= SpeexInBandSz
[c
];
1383 } else if (c
== 13) {
1384 /* user in-band; next 5 bits contain msg len */
1385 c
= get_n_bits_at(data
, 5, bit
);
1392 /* skip number bits for submode (less the 5 control bits) */
1393 bit
+= SpeexSubModeSz
[c
] - 5;
1394 cnt
+= 160; /* new frame */
1400 int ast_codec_get_samples(struct ast_frame
*f
)
1403 switch(f
->subclass
) {
1404 case AST_FORMAT_SPEEX
:
1405 samples
= speex_samples(f
->data
, f
->datalen
);
1407 case AST_FORMAT_G723_1
:
1408 samples
= g723_samples(f
->data
, f
->datalen
);
1410 case AST_FORMAT_ILBC
:
1411 samples
= 240 * (f
->datalen
/ 50);
1413 case AST_FORMAT_GSM
:
1414 samples
= 160 * (f
->datalen
/ 33);
1416 case AST_FORMAT_G729A
:
1417 samples
= f
->datalen
* 8;
1419 case AST_FORMAT_SLINEAR
:
1420 case AST_FORMAT_SLINEAR16
:
1421 samples
= f
->datalen
/ 2;
1423 case AST_FORMAT_LPC10
:
1424 /* assumes that the RTP packet contains one LPC10 frame */
1426 samples
+= (((char *)(f
->data
))[7] & 0x1) * 8;
1428 case AST_FORMAT_ULAW
:
1429 case AST_FORMAT_ALAW
:
1430 samples
= f
->datalen
;
1432 case AST_FORMAT_G722
:
1433 case AST_FORMAT_ADPCM
:
1434 case AST_FORMAT_G726
:
1435 case AST_FORMAT_G726_AAL2
:
1436 samples
= f
->datalen
* 2;
1439 ast_log(LOG_WARNING
, "Unable to calculate samples for format %s\n", ast_getformatname(f
->subclass
));
1444 int ast_codec_get_len(int format
, int samples
)
1448 /* XXX Still need speex, g723, and lpc10 XXX */
1450 case AST_FORMAT_G723_1
:
1451 len
= (samples
/ 240) * 20;
1453 case AST_FORMAT_ILBC
:
1454 len
= (samples
/ 240) * 50;
1456 case AST_FORMAT_GSM
:
1457 len
= (samples
/ 160) * 33;
1459 case AST_FORMAT_G729A
:
1462 case AST_FORMAT_SLINEAR
:
1463 case AST_FORMAT_SLINEAR16
:
1466 case AST_FORMAT_ULAW
:
1467 case AST_FORMAT_ALAW
:
1470 case AST_FORMAT_G722
:
1471 case AST_FORMAT_ADPCM
:
1472 case AST_FORMAT_G726
:
1473 case AST_FORMAT_G726_AAL2
:
1477 ast_log(LOG_WARNING
, "Unable to calculate sample length for format %s\n", ast_getformatname(format
));
1483 int ast_frame_adjust_volume(struct ast_frame
*f
, int adjustment
)
1486 short *fdata
= f
->data
;
1487 short adjust_value
= abs(adjustment
);
1489 if ((f
->frametype
!= AST_FRAME_VOICE
) || (f
->subclass
!= AST_FORMAT_SLINEAR
))
1495 for (count
= 0; count
< f
->samples
; count
++) {
1496 if (adjustment
> 0) {
1497 ast_slinear_saturated_multiply(&fdata
[count
], &adjust_value
);
1498 } else if (adjustment
< 0) {
1499 ast_slinear_saturated_divide(&fdata
[count
], &adjust_value
);
1506 int ast_frame_slinear_sum(struct ast_frame
*f1
, struct ast_frame
*f2
)
1509 short *data1
, *data2
;
1511 if ((f1
->frametype
!= AST_FRAME_VOICE
) || (f1
->subclass
!= AST_FORMAT_SLINEAR
))
1514 if ((f2
->frametype
!= AST_FRAME_VOICE
) || (f2
->subclass
!= AST_FORMAT_SLINEAR
))
1517 if (f1
->samples
!= f2
->samples
)
1520 for (count
= 0, data1
= f1
->data
, data2
= f2
->data
;
1521 count
< f1
->samples
;
1522 count
++, data1
++, data2
++)
1523 ast_slinear_saturated_add(data1
, data2
);