Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:01:32 2007

Asterisk developer's documentation :: Codename Pineapple


frame.c File Reference


Detailed Description

Frame and codec manipulation routines.

Author:
Mark Spencer <markster@digium.com>

Definition in file frame.c.

#include "asterisk.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "asterisk/lock.h"
#include "asterisk/frame.h"
#include "asterisk/logger.h"
#include "asterisk/options.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 dependency graph for frame.c:

Go to the source code of this file.

Data Structures

struct  ast_codec_alias_table
struct  ast_frame_cache
struct  ast_smoother

Defines

#define FRAME_CACHE_MAX_SIZE   10
 Maximum ast_frame cache size.
#define SMOOTHER_SIZE   8000
#define TYPE_MASK   0x3

Enumerations

enum  frame_type { TYPE_HIGH, TYPE_LOW, TYPE_SILENCE, TYPE_DONTSEND }

Functions

int __ast_smoother_feed (struct ast_smoother *s, struct ast_frame *f, int swap)
char * ast_codec2str (int codec)
 Get a name from a format Gets a name from a format.
int ast_codec_choose (struct ast_codec_pref *pref, int formats, int find_best)
 Select the best audio format according to preference list from supplied options. If "find_best" is non-zero then if nothing is found, the "Best" format of the format list is selected, otherwise 0 is returned.
int ast_codec_get_len (int format, int samples)
 Returns the number of bytes for the number of samples of the given format.
int ast_codec_get_samples (struct ast_frame *f)
 Returns the number of samples contained in the frame.
int ast_codec_pref_append (struct ast_codec_pref *pref, int format)
 Append a audio codec to a preference list, removing it first if it was already there.
void ast_codec_pref_convert (struct ast_codec_pref *pref, char *buf, size_t size, int right)
 Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string.
ast_format_list ast_codec_pref_getsize (struct ast_codec_pref *pref, int format)
 Get packet size for codec.
int ast_codec_pref_index (struct ast_codec_pref *pref, int index)
 Codec located at a particular place in the preference index See Audio Codec Preferences.
void ast_codec_pref_remove (struct ast_codec_pref *pref, int format)
 Remove audio a codec from a preference list.
int ast_codec_pref_setsize (struct ast_codec_pref *pref, int format, int framems)
 Set packet size for codec.
int ast_codec_pref_string (struct ast_codec_pref *pref, char *buf, size_t size)
 Dump audio codec preference list into a string.
static const char * ast_expand_codec_alias (const char *in)
int ast_frame_adjust_volume (struct ast_frame *f, int adjustment)
 Adjusts the volume of the audio samples contained in a frame.
void ast_frame_dump (const char *name, struct ast_frame *f, char *prefix)
void ast_frame_free (struct ast_frame *fr, int cache)
 Requests a frame to be allocated Frees a frame.
static struct ast_frameast_frame_header_new (void)
int ast_frame_slinear_sum (struct ast_frame *f1, struct ast_frame *f2)
 Sums two frames of audio samples.
ast_frameast_frdup (const struct ast_frame *f)
 Copies a frame.
ast_frameast_frisolate (struct ast_frame *fr)
 Makes a frame independent of any static storage.
ast_format_listast_get_format_list (size_t *size)
ast_format_listast_get_format_list_index (int index)
int ast_getformatbyname (const char *name)
 Gets a format from a name.
char * ast_getformatname (int format)
 Get the name of a format.
char * ast_getformatname_multiple (char *buf, size_t size, int format)
 Get the names of a set of formats.
 AST_LIST_HEAD_NOLOCK (ast_frames, ast_frame)
 This is just so ast_frames, a list head struct for holding a list of ast_frame structures, is defined.
int ast_parse_allow_disallow (struct ast_codec_pref *pref, int *mask, const char *list, int allowing)
 Parse an "allow" or "deny" line in a channel or device configuration and update the capabilities mask and pref if provided. Video codecs are not added to codec preference lists, since we can not transcode.
void ast_smoother_free (struct ast_smoother *s)
int ast_smoother_get_flags (struct ast_smoother *s)
ast_smootherast_smoother_new (int size)
ast_frameast_smoother_read (struct ast_smoother *s)
void ast_smoother_reset (struct ast_smoother *s, int size)
void ast_smoother_set_flags (struct ast_smoother *s, int flags)
int ast_smoother_test_flag (struct ast_smoother *s, int flag)
void ast_swapcopy_samples (void *dst, const void *src, int samples)
 AST_THREADSTORAGE_CUSTOM (frame_cache, NULL, frame_cache_cleanup)
 A per-thread cache of frame headers.
static void frame_cache_cleanup (void *data)
static int g723_len (unsigned char buf)
static int g723_samples (unsigned char *buf, int maxlen)
static unsigned char get_n_bits_at (unsigned char *data, int n, int bit)
int init_framer (void)
static int show_codec_n (int fd, int argc, char *argv[])
static int show_codecs (int fd, int argc, char *argv[])
static int speex_get_wb_sz_at (unsigned char *data, int len, int bit)
static int speex_samples (unsigned char *data, int len)

Variables

static struct ast_format_list AST_FORMAT_LIST []
 Definition of supported media formats (codecs).
ast_frame ast_null_frame = { AST_FRAME_NULL, }
static char frame_show_codec_n_usage []
static const char frame_show_codecs_usage []
static struct ast_cli_entry my_clis []


Define Documentation

#define FRAME_CACHE_MAX_SIZE   10
 

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.

Definition at line 67 of file frame.c.

Referenced by ast_frame_free().

#define SMOOTHER_SIZE   8000
 

Definition at line 79 of file frame.c.

Referenced by __ast_smoother_feed().

#define TYPE_MASK   0x3
 

Definition at line 88 of file frame.c.

Referenced by g723_len().


Enumeration Type Documentation

enum frame_type
 

Enumerator:
TYPE_HIGH 
TYPE_LOW 
TYPE_SILENCE 
TYPE_DONTSEND 

Definition at line 81 of file frame.c.

00081                 {
00082    TYPE_HIGH,     /* 0x0 */
00083    TYPE_LOW,      /* 0x1 */
00084    TYPE_SILENCE,  /* 0x2 */
00085    TYPE_DONTSEND  /* 0x3 */
00086 };


Function Documentation

int __ast_smoother_feed struct ast_smoother s,
struct ast_frame f,
int  swap
 

Definition at line 175 of file frame.c.

References AST_FRAME_VOICE, ast_log(), AST_MIN_OFFSET, AST_SMOOTHER_FLAG_G729, ast_swapcopy_samples(), ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_smoother::f, ast_frame::frametype, LOG_NOTICE, LOG_WARNING, ast_frame::offset, s, ast_frame::samples, SMOOTHER_SIZE, and ast_frame::subclass.

00176 {
00177    if (f->frametype != AST_FRAME_VOICE) {
00178       ast_log(LOG_WARNING, "Huh?  Can't smooth a non-voice frame!\n");
00179       return -1;
00180    }
00181    if (!s->format) {
00182       s->format = f->subclass;
00183       s->samplesperbyte = (float)f->samples / (float)f->datalen;
00184    } else if (s->format != f->subclass) {
00185       ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
00186       return -1;
00187    }
00188    if (s->len + f->datalen > SMOOTHER_SIZE) {
00189       ast_log(LOG_WARNING, "Out of smoother space\n");
00190       return -1;
00191    }
00192    if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
00193              && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
00194       if (!s->len) {
00195          /* Optimize by sending the frame we just got
00196             on the next read, thus eliminating the douple
00197             copy */
00198          if (swap)
00199             ast_swapcopy_samples(f->data, f->data, f->samples);
00200          s->opt = f;
00201          return 0;
00202       } else {
00203          s->optimizablestream++;
00204          if (s->optimizablestream > 10) {
00205             /* For the past 10 rounds, we have input and output
00206                frames of the correct size for this smoother, yet
00207                we were unable to optimize because there was still
00208                some cruft left over.  Lets just drop the cruft so
00209                we can move to a fully optimized path */
00210             if (swap)
00211                ast_swapcopy_samples(f->data, f->data, f->samples);
00212             s->len = 0;
00213             s->opt = f;
00214             return 0;
00215          }
00216       }
00217    } else 
00218       s->optimizablestream = 0;
00219    if (s->flags & AST_SMOOTHER_FLAG_G729) {
00220       if (s->len % 10) {
00221          ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
00222          return 0;
00223       }
00224    }
00225    if (swap)
00226       ast_swapcopy_samples(s->data+s->len, f->data, f->samples);
00227    else
00228       memcpy(s->data + s->len, f->data, f->datalen);
00229    /* If either side is empty, reset the delivery time */
00230    if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery))   /* XXX really ? */
00231       s->delivery = f->delivery;
00232    s->len += f->datalen;
00233    return 0;
00234 }

char* ast_codec2str int  codec  ) 
 

Get a name from a format Gets a name from a format.

Parameters:
codec codec number (1,2,4,8,16,etc.)
Returns:
This returns a static string identifying the format on success, 0 on error.

Definition at line 610 of file frame.c.

References AST_FORMAT_LIST, and desc.

Referenced by moh_alloc(), show_codec_n(), and show_codecs().

00611 {
00612    int x;
00613    char *ret = "unknown";
00614    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
00615       if (AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == codec) {
00616          ret = AST_FORMAT_LIST[x].desc;
00617          break;
00618       }
00619    }
00620    return ret;
00621 }

int ast_codec_choose struct ast_codec_pref pref,
int  formats,
int  find_best
 

Select the best audio format according to preference list from supplied options. If "find_best" is non-zero then if nothing is found, the "Best" format of the format list is selected, otherwise 0 is returned.

Definition at line 1139 of file frame.c.

References AST_FORMAT_LIST, ast_format_list::bits, and ast_codec_pref::order.

Referenced by __oh323_new(), gtalk_new(), jingle_new(), sip_new(), and socket_process().

01140 {
01141    int x, ret = 0, slot;
01142 
01143    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01144       slot = pref->order[x];
01145 
01146       if (!slot)
01147          break;
01148       if (formats & AST_FORMAT_LIST[slot-1].bits) {
01149          ret = AST_FORMAT_LIST[slot-1].bits;
01150          break;
01151       }
01152    }
01153    if (ret & AST_FORMAT_AUDIO_MASK)
01154       return ret;
01155 
01156    if (option_debug > 3)
01157       ast_log(LOG_DEBUG, "Could not find preferred codec - %s\n", find_best ? "Going for the best codec" : "Returning zero codec");
01158 
01159       return find_best ? ast_best_codec(formats) : 0;
01160 }

int ast_codec_get_len int  format,
int  samples
 

Returns the number of bytes for the number of samples of the given format.

Definition at line 1404 of file frame.c.

References AST_FORMAT_ADPCM, AST_FORMAT_ALAW, AST_FORMAT_G723_1, AST_FORMAT_G726, AST_FORMAT_G726_AAL2, AST_FORMAT_G729A, AST_FORMAT_GSM, AST_FORMAT_ILBC, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, ast_getformatname(), ast_log(), len, and LOG_WARNING.

Referenced by ast_channel_spy_read_frame(), copy_data_from_queue(), and moh_generate().

01405 {
01406    int len = 0;
01407 
01408    /* XXX Still need speex, g723, and lpc10 XXX */ 
01409    switch(format) {
01410    case AST_FORMAT_G723_1:
01411       len = (samples / 240) * 20;
01412       break;
01413    case AST_FORMAT_ILBC:
01414       len = (samples / 240) * 50;
01415       break;
01416    case AST_FORMAT_GSM:
01417       len = (samples / 160) * 33;
01418       break;
01419    case AST_FORMAT_G729A:
01420       len = samples / 8;
01421       break;
01422    case AST_FORMAT_SLINEAR:
01423       len = samples * 2;
01424       break;
01425    case AST_FORMAT_ULAW:
01426    case AST_FORMAT_ALAW:
01427       len = samples;
01428       break;
01429    case AST_FORMAT_ADPCM:
01430    case AST_FORMAT_G726:
01431    case AST_FORMAT_G726_AAL2:
01432       len = samples / 2;
01433       break;
01434    default:
01435       ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
01436    }
01437 
01438    return len;
01439 }

int ast_codec_get_samples struct ast_frame f  ) 
 

Returns the number of samples contained in the frame.

Definition at line 1361 of file frame.c.

References AST_FORMAT_ADPCM, AST_FORMAT_ALAW, AST_FORMAT_G722, AST_FORMAT_G723_1, AST_FORMAT_G726, AST_FORMAT_G726_AAL2, AST_FORMAT_G729A, AST_FORMAT_GSM, AST_FORMAT_ILBC, AST_FORMAT_LPC10, AST_FORMAT_SLINEAR, AST_FORMAT_SPEEX, AST_FORMAT_ULAW, ast_getformatname(), ast_log(), ast_frame::data, ast_frame::datalen, g723_samples(), LOG_WARNING, speex_samples(), and ast_frame::subclass.

Referenced by ast_rtp_read(), moh_generate(), schedule_delivery(), socket_process(), and socket_process_meta().

01362 {
01363    int samples=0;
01364    switch(f->subclass) {
01365    case AST_FORMAT_SPEEX:
01366       samples = speex_samples(f->data, f->datalen);
01367       break;
01368    case AST_FORMAT_G723_1:
01369                 samples = g723_samples(f->data, f->datalen);
01370       break;
01371    case AST_FORMAT_ILBC:
01372       samples = 240 * (f->datalen / 50);
01373       break;
01374    case AST_FORMAT_GSM:
01375       samples = 160 * (f->datalen / 33);
01376       break;
01377    case AST_FORMAT_G729A:
01378       samples = f->datalen * 8;
01379       break;
01380    case AST_FORMAT_SLINEAR:
01381       samples = f->datalen / 2;
01382       break;
01383    case AST_FORMAT_LPC10:
01384                 /* assumes that the RTP packet contains one LPC10 frame */
01385       samples = 22 * 8;
01386       samples += (((char *)(f->data))[7] & 0x1) * 8;
01387       break;
01388    case AST_FORMAT_ULAW:
01389    case AST_FORMAT_ALAW:
01390    case AST_FORMAT_G722:
01391       samples = f->datalen;
01392       break;
01393    case AST_FORMAT_ADPCM:
01394    case AST_FORMAT_G726:
01395    case AST_FORMAT_G726_AAL2:
01396       samples = f->datalen * 2;
01397       break;
01398    default:
01399       ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
01400    }
01401    return samples;
01402 }

int ast_codec_pref_append struct ast_codec_pref pref,
int  format
 

Append a audio codec to a preference list, removing it first if it was already there.

Definition at line 1034 of file frame.c.

References ast_codec_pref_remove(), and AST_FORMAT_LIST.

Referenced by ast_parse_allow_disallow().

01035 {
01036    int x, newindex = -1;
01037 
01038    ast_codec_pref_remove(pref, format);
01039 
01040    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01041       if (AST_FORMAT_LIST[x].bits == format) {
01042          newindex = x + 1;
01043          break;
01044       }
01045    }
01046 
01047    if (newindex) {
01048       for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01049          if (!pref->order[x]) {
01050             pref->order[x] = newindex;
01051             break;
01052          }
01053       }
01054    }
01055 
01056    return x;
01057 }

void ast_codec_pref_convert struct ast_codec_pref pref,
char *  buf,
size_t  size,
int  right
 

Shift an audio codec preference list up or down 65 bytes so that it becomes an ASCII string.

Definition at line 936 of file frame.c.

References ast_codec_pref::order.

Referenced by check_access(), create_addr(), dump_prefs(), and socket_process().

00937 {
00938    int x, differential = (int) 'A', mem;
00939    char *from, *to;
00940 
00941    if (right) {
00942       from = pref->order;
00943       to = buf;
00944       mem = size;
00945    } else {
00946       to = pref->order;
00947       from = buf;
00948       mem = 32;
00949    }
00950 
00951    memset(to, 0, mem);
00952    for (x = 0; x < 32 ; x++) {
00953       if (!from[x])
00954          break;
00955       to[x] = right ? (from[x] + differential) : (from[x] - differential);
00956    }
00957 }

struct ast_format_list ast_codec_pref_getsize struct ast_codec_pref pref,
int  format
 

Get packet size for codec.

Definition at line 1100 of file frame.c.

Referenced by add_codec_to_sdp(), ast_rtp_write(), handle_open_receive_channel_ack_message(), and transmit_connect().

01101 {
01102    int x, index = -1, framems = 0;
01103    struct ast_format_list fmt = { 0, };
01104 
01105    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01106       if (AST_FORMAT_LIST[x].bits == format) {
01107          fmt = AST_FORMAT_LIST[x];
01108          index = x;
01109          break;
01110       }
01111    }
01112 
01113    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01114       if (pref->order[x] == (index + 1)) {
01115          framems = pref->framing[x];
01116          break;
01117       }
01118    }
01119 
01120    /* size validation */
01121    if (!framems)
01122       framems = AST_FORMAT_LIST[index].def_ms;
01123 
01124    if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
01125       framems -= framems % AST_FORMAT_LIST[index].inc_ms;
01126 
01127    if (framems < AST_FORMAT_LIST[index].min_ms)
01128       framems = AST_FORMAT_LIST[index].min_ms;
01129 
01130    if (framems > AST_FORMAT_LIST[index].max_ms)
01131       framems = AST_FORMAT_LIST[index].max_ms;
01132 
01133    fmt.cur_ms = framems;
01134 
01135    return fmt;
01136 }

int ast_codec_pref_index struct ast_codec_pref pref,
int  index
 

Codec located at a particular place in the preference index See Audio Codec Preferences.

Definition at line 994 of file frame.c.

References AST_FORMAT_LIST, ast_format_list::bits, and ast_codec_pref::order.

Referenced by _sip_show_device(), add_sdp(), ast_codec_pref_string(), function_iaxpeer(), function_sippeer(), gtalk_invite(), iax2_show_peer(), jingle_accept_call(), print_codec_to_cli(), and socket_process().

00995 {
00996    int slot = 0;
00997 
00998    
00999    if ((index >= 0) && (index < sizeof(pref->order))) {
01000       slot = pref->order[index];
01001    }
01002 
01003    return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
01004 }

void ast_codec_pref_remove struct ast_codec_pref pref,
int  format
 

Remove audio a codec from a preference list.

Definition at line 1007 of file frame.c.

References AST_FORMAT_LIST, and ast_codec_pref::order.

Referenced by ast_codec_pref_append(), and ast_parse_allow_disallow().

01008 {
01009    struct ast_codec_pref oldorder;
01010    int x, y = 0;
01011    int slot;
01012    int size;
01013 
01014    if (!pref->order[0])
01015       return;
01016 
01017    memcpy(&oldorder, pref, sizeof(oldorder));
01018    memset(pref, 0, sizeof(*pref));
01019 
01020    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01021       slot = oldorder.order[x];
01022       size = oldorder.framing[x];
01023       if (! slot)
01024          break;
01025       if (AST_FORMAT_LIST[slot-1].bits != format) {
01026          pref->order[y] = slot;
01027          pref->framing[y++] = size;
01028       }
01029    }
01030    
01031 }

int ast_codec_pref_setsize struct ast_codec_pref pref,
int  format,
int  framems
 

Set packet size for codec.

Definition at line 1061 of file frame.c.

References AST_FORMAT_LIST.

Referenced by ast_parse_allow_disallow().

01062 {
01063    int x, index = -1;
01064 
01065    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01066       if (AST_FORMAT_LIST[x].bits == format) {
01067          index = x;
01068          break;
01069       }
01070    }
01071 
01072    if (index < 0)
01073       return -1;
01074 
01075    /* size validation */
01076    if (!framems)
01077       framems = AST_FORMAT_LIST[index].def_ms;
01078 
01079    if (AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
01080       framems -= framems % AST_FORMAT_LIST[index].inc_ms;
01081 
01082    if (framems < AST_FORMAT_LIST[index].min_ms)
01083       framems = AST_FORMAT_LIST[index].min_ms;
01084 
01085    if (framems > AST_FORMAT_LIST[index].max_ms)
01086       framems = AST_FORMAT_LIST[index].max_ms;
01087 
01088 
01089    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01090       if (pref->order[x] == (index + 1)) {
01091          pref->framing[x] = framems;
01092          break;
01093       }
01094    }
01095 
01096    return x;
01097 }

int ast_codec_pref_string struct ast_codec_pref pref,
char *  buf,
size_t  size
 

Dump audio codec preference list into a string.

Definition at line 959 of file frame.c.

References ast_codec_pref_index(), and ast_getformatname().

Referenced by dump_prefs(), and socket_process().

00960 {
00961    int x, codec; 
00962    size_t total_len, slen;
00963    char *formatname;
00964    
00965    memset(buf,0,size);
00966    total_len = size;
00967    buf[0] = '(';
00968    total_len--;
00969    for(x = 0; x < 32 ; x++) {
00970       if (total_len <= 0)
00971          break;
00972       if (!(codec = ast_codec_pref_index(pref,x)))
00973          break;
00974       if ((formatname = ast_getformatname(codec))) {
00975          slen = strlen(formatname);
00976          if (slen > total_len)
00977             break;
00978          strncat(buf,formatname,total_len);
00979          total_len -= slen;
00980       }
00981       if (total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
00982          strncat(buf,"|",total_len);
00983          total_len--;
00984       }
00985    }
00986    if (total_len) {
00987       strncat(buf,")",total_len);
00988       total_len--;
00989    }
00990 
00991    return size - total_len;
00992 }

static const char* ast_expand_codec_alias const char *  in  )  [static]
 

Definition at line 581 of file frame.c.

Referenced by ast_getformatbyname().

00582 {
00583    int x;
00584 
00585    for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(ast_codec_alias_table[0]); x++) {
00586       if (!strcmp(in,ast_codec_alias_table[x].alias))
00587          return ast_codec_alias_table[x].realname;
00588    }
00589    return in;
00590 }

int ast_frame_adjust_volume struct ast_frame f,
int  adjustment
 

Adjusts the volume of the audio samples contained in a frame.

Parameters:
f The frame containing the samples (must be AST_FRAME_VOICE and AST_FORMAT_SLINEAR)
adjustment The number of dB to adjust up or down.
Returns:
0 for success, non-zero for an error

Definition at line 1441 of file frame.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_slinear_saturated_multiply(), ast_frame::data, ast_frame::frametype, ast_frame::samples, and ast_frame::subclass.

Referenced by ast_channel_spy_read_frame(), and ast_write().

01442 {
01443    int count;
01444    short *fdata = f->data;
01445    short adjust_value = abs(adjustment);
01446 
01447    if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
01448       return -1;
01449 
01450    if (!adjustment)
01451       return 0;
01452 
01453    for (count = 0; count < f->samples; count++) {
01454       if (adjustment > 0) {
01455          ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
01456       } else if (adjustment < 0) {
01457          ast_slinear_saturated_divide(&fdata[count], &adjust_value);
01458       }
01459    }
01460 
01461    return 0;
01462 }

void ast_frame_dump const char *  name,
struct ast_frame f,
char *  prefix
 

Dump a frame for debugging purposes

Definition at line 698 of file frame.c.

References AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_FLASH, AST_CONTROL_HANGUP, AST_CONTROL_OFFHOOK, AST_CONTROL_OPTION, AST_CONTROL_RADIO_KEY, AST_CONTROL_RADIO_UNKEY, AST_CONTROL_RING, AST_CONTROL_RINGING, AST_CONTROL_TAKEOFFHOOK, AST_CONTROL_WINK, AST_FRAME_CONTROL, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_HTML, AST_FRAME_IAX, AST_FRAME_IMAGE, AST_FRAME_MODEM, AST_FRAME_NULL, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_getformatname(), AST_HTML_BEGIN, AST_HTML_DATA, AST_HTML_END, AST_HTML_LDCOMPLETE, AST_HTML_LINKREJECT, AST_HTML_LINKURL, AST_HTML_NOSUPPORT, AST_HTML_UNLINK, AST_HTML_URL, AST_MODEM_T38, AST_MODEM_V150, ast_strlen_zero(), ast_verbose(), COLOR_BLACK, COLOR_BRCYAN, COLOR_BRGREEN, COLOR_BRMAGENTA, COLOR_BRRED, COLOR_YELLOW, ast_frame::data, ast_frame::frametype, ast_frame::subclass, and term_color().

Referenced by __ast_read(), and ast_write().

00699 {
00700    const char noname[] = "unknown";
00701    char ftype[40] = "Unknown Frametype";
00702    char cft[80];
00703    char subclass[40] = "Unknown Subclass";
00704    char csub[80];
00705    char moreinfo[40] = "";
00706    char cn[60];
00707    char cp[40];
00708    char cmn[40];
00709 
00710    if (!name)
00711       name = noname;
00712 
00713 
00714    if (!f) {
00715       ast_verbose("%s [ %s (NULL) ] [%s]\n", 
00716          term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00717          term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)), 
00718          term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00719       return;
00720    }
00721    /* XXX We should probably print one each of voice and video when the format changes XXX */
00722    if (f->frametype == AST_FRAME_VOICE)
00723       return;
00724    if (f->frametype == AST_FRAME_VIDEO)
00725       return;
00726    switch(f->frametype) {
00727    case AST_FRAME_DTMF_BEGIN:
00728       strcpy(ftype, "DTMF Begin");
00729       subclass[0] = f->subclass;
00730       subclass[1] = '\0';
00731       break;
00732    case AST_FRAME_DTMF_END:
00733       strcpy(ftype, "DTMF End");
00734       subclass[0] = f->subclass;
00735       subclass[1] = '\0';
00736       break;
00737    case AST_FRAME_CONTROL:
00738       strcpy(ftype, "Control");
00739       switch(f->subclass) {
00740       case AST_CONTROL_HANGUP:
00741          strcpy(subclass, "Hangup");
00742          break;
00743       case AST_CONTROL_RING:
00744          strcpy(subclass, "Ring");
00745          break;
00746       case AST_CONTROL_RINGING:
00747          strcpy(subclass, "Ringing");
00748          break;
00749       case AST_CONTROL_ANSWER:
00750          strcpy(subclass, "Answer");
00751          break;
00752       case AST_CONTROL_BUSY:
00753          strcpy(subclass, "Busy");
00754          break;
00755       case AST_CONTROL_TAKEOFFHOOK:
00756          strcpy(subclass, "Take Off Hook");
00757          break;
00758       case AST_CONTROL_OFFHOOK:
00759          strcpy(subclass, "Line Off Hook");
00760          break;
00761       case AST_CONTROL_CONGESTION:
00762          strcpy(subclass, "Congestion");
00763          break;
00764       case AST_CONTROL_FLASH:
00765          strcpy(subclass, "Flash");
00766          break;
00767       case AST_CONTROL_WINK:
00768          strcpy(subclass, "Wink");
00769          break;
00770       case AST_CONTROL_OPTION:
00771          strcpy(subclass, "Option");
00772          break;
00773       case AST_CONTROL_RADIO_KEY:
00774          strcpy(subclass, "Key Radio");
00775          break;
00776       case AST_CONTROL_RADIO_UNKEY:
00777          strcpy(subclass, "Unkey Radio");
00778          break;
00779       case -1:
00780          strcpy(subclass, "Stop generators");
00781          break;
00782       default:
00783          snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
00784       }
00785       break;
00786    case AST_FRAME_NULL:
00787       strcpy(ftype, "Null Frame");
00788       strcpy(subclass, "N/A");
00789       break;
00790    case AST_FRAME_IAX:
00791       /* Should never happen */
00792       strcpy(ftype, "IAX Specific");
00793       snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
00794       break;
00795    case AST_FRAME_TEXT:
00796       strcpy(ftype, "Text");
00797       strcpy(subclass, "N/A");
00798       ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
00799       break;
00800    case AST_FRAME_IMAGE:
00801       strcpy(ftype, "Image");
00802       snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
00803       break;
00804    case AST_FRAME_HTML:
00805       strcpy(ftype, "HTML");
00806       switch(f->subclass) {
00807       case AST_HTML_URL:
00808          strcpy(subclass, "URL");
00809          ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
00810          break;
00811       case AST_HTML_DATA:
00812          strcpy(subclass, "Data");
00813          break;
00814       case AST_HTML_BEGIN:
00815          strcpy(subclass, "Begin");
00816          break;
00817       case AST_HTML_END:
00818          strcpy(subclass, "End");
00819          break;
00820       case AST_HTML_LDCOMPLETE:
00821          strcpy(subclass, "Load Complete");
00822          break;
00823       case AST_HTML_NOSUPPORT:
00824          strcpy(subclass, "No Support");
00825          break;
00826       case AST_HTML_LINKURL:
00827          strcpy(subclass, "Link URL");
00828          ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
00829          break;
00830       case AST_HTML_UNLINK:
00831          strcpy(subclass, "Unlink");
00832          break;
00833       case AST_HTML_LINKREJECT:
00834          strcpy(subclass, "Link Reject");
00835          break;
00836       default:
00837          snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
00838          break;
00839       }
00840       break;
00841    case AST_FRAME_MODEM:
00842       strcpy(ftype, "Modem");
00843       switch (f->subclass) {
00844       case AST_MODEM_T38:
00845          strcpy(subclass, "T.38");
00846          break;
00847       case AST_MODEM_V150:
00848          strcpy(subclass, "V.150");
00849          break;
00850       default:
00851          snprintf(subclass, sizeof(subclass), "Unknown MODEM frame '%d'\n", f->subclass);
00852          break;
00853       }
00854       break;
00855    default:
00856       snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
00857    }
00858    if (!ast_strlen_zero(moreinfo))
00859       ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",  
00860              term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00861              term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
00862              f->frametype, 
00863              term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
00864              f->subclass, 
00865              term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
00866              term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00867    else
00868       ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",  
00869              term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00870              term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
00871              f->frametype, 
00872              term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
00873              f->subclass, 
00874              term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00875 }

void ast_frame_free struct ast_frame fr,
int  cache
 

Requests a frame to be allocated Frees a frame.

Parameters:
fr Frame to free
cache Whether to consider this frame for frame caching

Definition at line 339 of file frame.c.

References AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE, AST_LIST_UNLOCK, AST_MALLOCD_DATA, AST_MALLOCD_HDR, AST_MALLOCD_SRC, ast_frame::data, FRAME_CACHE_MAX_SIZE, frames, free, ast_frame::mallocd, ast_frame::offset, and ast_frame::src.

Referenced by ast_frfree().

00340 {
00341    if (!fr->mallocd)
00342       return;
00343 
00344 #if !defined(LOW_MEMORY)
00345    if (cache && fr->mallocd == AST_MALLOCD_HDR) {
00346       /* Cool, only the header is malloc'd, let's just cache those for now 
00347        * to keep things simple... */
00348       struct ast_frame_cache *frames;
00349 
00350       if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames))) 
00351           && frames->size < FRAME_CACHE_MAX_SIZE) {
00352          AST_LIST_INSERT_HEAD(&frames->list, fr, frame_list);
00353          frames->size++;
00354          return;
00355       }
00356    }
00357 #endif
00358    
00359    if (fr->mallocd & AST_MALLOCD_DATA) {
00360       if (fr->data) 
00361          free(fr->data - fr->offset);
00362    }
00363    if (fr->mallocd & AST_MALLOCD_SRC) {
00364       if (fr->src)
00365          free((char *)fr->src);
00366    }
00367    if (fr->mallocd & AST_MALLOCD_HDR) {
00368 #ifdef TRACE_FRAMES
00369       AST_LIST_LOCK(&headerlist);
00370       headers--;
00371       AST_LIST_REMOVE(&headerlist, fr, frame_list);
00372       AST_LIST_UNLOCK(&headerlist);
00373 #endif         
00374       free(fr);
00375    }
00376 }

static struct ast_frame* ast_frame_header_new void   )  [static]
 

Definition at line 291 of file frame.c.

References ast_calloc, ast_calloc_cache, AST_LIST_INSERT_HEAD, AST_LIST_LOCK, AST_LIST_REMOVE_HEAD, AST_LIST_UNLOCK, AST_MALLOCD_HDR, frames, and ast_frame::mallocd_hdr_len.

Referenced by ast_frisolate().

00292 {
00293    struct ast_frame *f;
00294 
00295 #if !defined(LOW_MEMORY)
00296    struct ast_frame_cache *frames;
00297 
00298    if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
00299       if ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list))) {
00300          size_t mallocd_len = f->mallocd_hdr_len;
00301          memset(f, 0, sizeof(*f));
00302          f->mallocd_hdr_len = mallocd_len;
00303          f->mallocd = AST_MALLOCD_HDR;
00304          frames->size--;
00305          return f;
00306       }
00307    }
00308    if (!(f = ast_calloc_cache(1, sizeof(*f))))
00309       return NULL;
00310 #else
00311    if (!(f = ast_calloc(1, sizeof(*f))))
00312       return NULL;
00313 #endif
00314 
00315    f->mallocd_hdr_len = sizeof(*f);
00316 #ifdef TRACE_FRAMES
00317    AST_LIST_LOCK(&headerlist);
00318    headers++;
00319    AST_LIST_INSERT_HEAD(&headerlist, f, frame_list);
00320    AST_LIST_UNLOCK(&headerlist);
00321 #endif   
00322    
00323    return f;
00324 }

int ast_frame_slinear_sum struct ast_frame f1,
struct ast_frame f2
 

Sums two frames of audio samples.

Parameters:
f1 The first frame (which will contain the result)
f2 The second frame
Returns:
0 for success, non-zero for an error
The frames must be AST_FRAME_VOICE and must contain AST_FORMAT_SLINEAR samples, and must contain the same number of samples.

Definition at line 1464 of file frame.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_slinear_saturated_add(), ast_frame::data, ast_frame::frametype, ast_frame::samples, and ast_frame::subclass.

Referenced by ast_channel_spy_read_frame(), and ast_write().

01465 {
01466    int count;
01467    short *data1, *data2;
01468 
01469    if ((f1->frametype != AST_FRAME_VOICE) || (f1->subclass != AST_FORMAT_SLINEAR))
01470       return -1;
01471 
01472    if ((f2->frametype != AST_FRAME_VOICE) || (f2->subclass != AST_FORMAT_SLINEAR))
01473       return -1;
01474 
01475    if (f1->samples != f2->samples)
01476       return -1;
01477 
01478    for (count = 0, data1 = f1->data, data2 = f2->data;
01479         count < f1->samples;
01480         count++, data1++, data2++)
01481       ast_slinear_saturated_add(data1, data2);
01482 
01483    return 0;
01484 }

struct ast_frame* ast_frdup const struct ast_frame fr  ) 
 

Copies a frame.

Parameters:
fr frame to copy Duplicates a frame -- should only rarely be used, typically frisolate is good enough
Returns:
Returns a frame on success, NULL on error

Definition at line 439 of file frame.c.

References ast_calloc_cache, AST_FRIENDLY_OFFSET, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_MALLOCD_HDR, ast_frame::data, ast_frame::datalen, ast_frame::delivery, frames, ast_frame::frametype, ast_frame::has_timing_info, len, ast_frame::len, ast_frame::mallocd, ast_frame::mallocd_hdr_len, ast_frame::offset, ast_frame::samples, ast_frame::seqno, ast_frame::src, ast_frame::subclass, and ast_frame::ts.

Referenced by ast_channel_spy_read_frame(), ast_jb_put(), ast_queue_frame(), ast_rtp_write(), ast_slinfactory_feed(), and queue_frame_to_spies().

00440 {
00441    struct ast_frame *out = NULL;
00442    int len, srclen = 0;
00443    void *buf = NULL;
00444 
00445 #if !defined(LOW_MEMORY)
00446    struct ast_frame_cache *frames;
00447 #endif
00448 
00449    /* Start with standard stuff */
00450    len = sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
00451    /* If we have a source, add space for it */
00452    /*
00453     * XXX Watch out here - if we receive a src which is not terminated
00454     * properly, we can be easily attacked. Should limit the size we deal with.
00455     */
00456    if (f->src)
00457       srclen = strlen(f->src);
00458    if (srclen > 0)
00459       len += srclen + 1;
00460    
00461 #if !defined(LOW_MEMORY)
00462    if ((frames = ast_threadstorage_get(&frame_cache, sizeof(*frames)))) {
00463       AST_LIST_TRAVERSE_SAFE_BEGIN(&frames->list, out, frame_list) {
00464          if (out->mallocd_hdr_len >= len) {
00465             size_t mallocd_len = out->mallocd_hdr_len;
00466             AST_LIST_REMOVE_CURRENT(&frames->list, frame_list);
00467             memset(out, 0, sizeof(*out));
00468             out->mallocd_hdr_len = mallocd_len;
00469             buf = out;
00470             frames->size--;
00471             break;
00472          }
00473       }
00474       AST_LIST_TRAVERSE_SAFE_END
00475    }
00476 #endif
00477 
00478    if (!buf) {
00479       if (!(buf = ast_calloc_cache(1, len)))
00480          return NULL;
00481       out = buf;
00482       out->mallocd_hdr_len = len;
00483    }
00484 
00485    out->frametype = f->frametype;
00486    out->subclass = f->subclass;
00487    out->datalen = f->datalen;
00488    out->samples = f->samples;
00489    out->delivery = f->delivery;
00490    /* Set us as having malloc'd header only, so it will eventually
00491       get freed. */
00492    out->mallocd = AST_MALLOCD_HDR;
00493    out->offset = AST_FRIENDLY_OFFSET;
00494    if (out->datalen) {
00495       out->data = buf + sizeof(*out) + AST_FRIENDLY_OFFSET;
00496       memcpy(out->data, f->data, out->datalen); 
00497    }
00498    if (srclen > 0) {
00499       out->src = buf + sizeof(*out) + AST_FRIENDLY_OFFSET + f->datalen;
00500       /* Must have space since we allocated for it */
00501       strcpy((char *)out->src, f->src);
00502    }
00503    out->has_timing_info = f->has_timing_info;
00504    out->ts = f->ts;
00505    out->len = f->len;
00506    out->seqno = f->seqno;
00507    return out;
00508 }

struct ast_frame* ast_frisolate struct ast_frame fr  ) 
 

Makes a frame independent of any static storage.

Parameters:
fr frame to act upon Take a frame, and if it's not been malloc'd, make a malloc'd copy and if the data hasn't been malloced then make the data malloc'd. If you need to store frames, say for queueing, then you should call this function.
Returns:
Returns a frame on success, NULL on error

Definition at line 383 of file frame.c.

References ast_frame_header_new(), AST_FRIENDLY_OFFSET, ast_malloc, AST_MALLOCD_DATA, AST_MALLOCD_HDR, AST_MALLOCD_SRC, ast_strdup, ast_frame::data, ast_frame::datalen, ast_frame::frametype, free, ast_frame::has_timing_info, ast_frame::len, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, ast_frame::seqno, ast_frame::src, ast_frame::subclass, and ast_frame::ts.

00384 {
00385    struct ast_frame *out;
00386    void *newdata;
00387    
00388    if (!(fr->mallocd & AST_MALLOCD_HDR)) {
00389       /* Allocate a new header if needed */
00390       if (!(out = ast_frame_header_new()))
00391          return NULL;
00392       out->frametype = fr->frametype;
00393       out->subclass = fr->subclass;
00394       out->datalen = fr->datalen;
00395       out->samples = fr->samples;
00396       out->offset = fr->offset;
00397       out->data = fr->data;
00398       /* Copy the timing data */
00399       out->has_timing_info = fr->has_timing_info;
00400       if (fr->has_timing_info) {
00401          out->ts = fr->ts;
00402          out->len = fr->len;
00403          out->seqno = fr->seqno;
00404       }
00405    } else
00406       out = fr;
00407    
00408    if (!(fr->mallocd & AST_MALLOCD_SRC)) {
00409       if (fr->src) {
00410          if (!(out->src = ast_strdup(fr->src))) {
00411             if (out != fr)
00412                free(out);
00413             return NULL;
00414          }
00415       }
00416    } else
00417       out->src = fr->src;
00418    
00419    if (!(fr->mallocd & AST_MALLOCD_DATA))  {
00420       if (!(newdata = ast_malloc(fr->datalen + AST_FRIENDLY_OFFSET))) {
00421          if (out->src != fr->src)
00422             free((void *) out->src);
00423          if (out != fr)
00424             free(out);
00425          return NULL;
00426       }
00427       newdata += AST_FRIENDLY_OFFSET;
00428       out->offset = AST_FRIENDLY_OFFSET;
00429       out->datalen = fr->datalen;
00430       memcpy(newdata, fr->data, fr->datalen);
00431       out->data = newdata;
00432    }
00433 
00434    out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
00435    
00436    return out;
00437 }

struct ast_format_list* ast_get_format_list size_t *  size  ) 
 

Definition at line 526 of file frame.c.

References AST_FORMAT_LIST.

00527 {
00528    *size = (sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]));
00529    return AST_FORMAT_LIST;
00530 }

struct ast_format_list* ast_get_format_list_index int  index  ) 
 

Definition at line 521 of file frame.c.

References AST_FORMAT_LIST.

00522 {
00523    return &AST_FORMAT_LIST[index];
00524 }

int ast_getformatbyname const char *  name  ) 
 

Gets a format from a name.

Parameters:
name string of format
Returns:
This returns the form of the format in binary on success, 0 on error.

Definition at line 592 of file frame.c.

References ast_expand_codec_alias(), AST_FORMAT_LIST, and format.

Referenced by ast_parse_allow_disallow(), iax_template_parse(), load_moh_classes(), reload_config(), and try_suggested_sip_codec().

00593 {
00594    int x, all, format = 0;
00595 
00596    all = strcasecmp(name, "all") ? 0 : 1;
00597    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
00598       if (AST_FORMAT_LIST[x].visible && (all || 
00599            !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
00600            !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name)))) {
00601          format |= AST_FORMAT_LIST[x].bits;
00602          if (!all)
00603             break;
00604       }
00605    }
00606 
00607    return format;
00608 }

char* ast_getformatname int  format  ) 
 

Get the name of a format.

Parameters:
format id of format
Returns:
A static string containing the name of the format or "UNKN" if unknown.

Definition at line 532 of file frame.c.

References AST_FORMAT_LIST, ast_format_list::bits, name, and ast_format_list::visible.

Referenced by __ast_read(), __ast_register_translator(), __login_exec(), __sip_show_channels(), _sip_show_device(), add_codec_to_answer(), add_codec_to_sdp(), agent_call(), ast_channel_spy_add(), ast_codec_get_len(), ast_codec_get_samples(), ast_codec_pref_string(), ast_dsp_process(), ast_frame_dump(), ast_openvstream(), ast_rtp_write(), ast_slinfactory_feed(), ast_streamfile(), ast_translator_build_path(), ast_unregister_translator(), ast_writestream(), eagi_exec(), function_iaxpeer(), function_sippeer(), iax2_request(), iax2_show_peer(), iax_show_provisioning(), moh_classes_show(), moh_release(), oh323_rtp_read(), phone_setup(), print_codec_to_cli(), queue_frame_to_spies(), rebuild_matrix(), set_format(), set_peer_capabilities(), show_codecs(), show_file_formats(), show_image_formats(), show_translation(), sip_request_call(), socket_process(), and zt_read().

00533 {
00534    int x;
00535    char *ret = "unknown";
00536    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
00537       if (AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == format) {
00538          ret = AST_FORMAT_LIST[x].name;
00539          break;
00540       }
00541    }
00542    return ret;
00543 }

char* ast_getformatname_multiple char *  buf,
size_t  size,
int  format
 

Get the names of a set of formats.

Parameters:
buf a buffer for the output string
size size of buf (bytes)
format the format (combined IDs of codecs) Prints a list of readable codec names corresponding to "format". ex: for format=AST_FORMAT_GSM|AST_FORMAT_SPEEX|AST_FORMAT_ILBC it will return "0x602 (GSM|SPEEX|ILBC)"
Returns:
The return value is buf.

Definition at line 545 of file frame.c.

References AST_FORMAT_LIST, ast_format_list::bits, len, name, and ast_format_list::visible.

Referenced by _sip_show_device(), _sip_show_peer(), add_sdp(), ast_streamfile(), function_iaxpeer(), function_sippeer(), handle_showchan(), iax2_show_peer(), sip_new(), sip_request_call(), sip_show_channel(), sip_show_settings(), and sip_write().

00546 {
00547    int x;
00548    unsigned len;
00549    char *start, *end = buf;
00550 
00551    if (!size)
00552       return buf;
00553    snprintf(end, size, "0x%x (", format);
00554    len = strlen(end);
00555    end += len;
00556    size -= len;
00557    start = end;
00558    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
00559       if (AST_FORMAT_LIST[x].visible && (AST_FORMAT_LIST[x].bits & format)) {
00560          snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
00561          len = strlen(end);
00562          end += len;
00563          size -= len;
00564       }
00565    }
00566    if (start == end)
00567       snprintf(start, size, "nothing)");
00568    else if (size > 1)
00569       *(end -1) = ')';
00570    return buf;
00571 }

AST_LIST_HEAD_NOLOCK ast_frames  ,
ast_frame 
 

This is just so ast_frames, a list head struct for holding a list of ast_frame structures, is defined.

int ast_parse_allow_disallow struct ast_codec_pref pref,
int *  mask,
const char *  list,
int  allowing
 

Parse an "allow" or "deny" line in a channel or device configuration and update the capabilities mask and pref if provided. Video codecs are not added to codec preference lists, since we can not transcode.

Returns:
Returns number of errors encountered during parsing

Definition at line 1162 of file frame.c.

References ast_codec_pref_append(), ast_codec_pref_remove(), ast_codec_pref_setsize(), ast_getformatbyname(), ast_log(), ast_strdupa, format, LOG_DEBUG, LOG_WARNING, option_debug, parse(), and strsep().

Referenced by build_device(), build_peer(), build_user(), gtalk_create_member(), gtalk_load_config(), jingle_create_member(), jingle_load_config(), reload_config(), set_config(), and update_common_options().

01163 {
01164    int errors = 0;
01165    char *parse = NULL, *this = NULL, *psize = NULL;
01166    int format = 0, framems = 0;
01167 
01168    parse = ast_strdupa(list);
01169    while ((this = strsep(&parse, ","))) {
01170       framems = 0;
01171       if ((psize = strrchr(this, ':'))) {
01172          *psize++ = '\0';
01173          if (option_debug)
01174             ast_log(LOG_DEBUG,"Packetization for codec: %s is %s\n", this, psize);
01175          framems = atoi(psize);
01176          if (framems < 0) {
01177             framems = 0;
01178             errors++;
01179             ast_log(LOG_WARNING, "Bad packetization value for codec %s\n", this);
01180          }
01181       }
01182       if (!(format = ast_getformatbyname(this))) {
01183          ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", this);
01184          errors++;
01185          continue;
01186       }
01187 
01188       if (mask) {
01189          if (allowing)
01190             *mask |= format;
01191          else
01192             *mask &= ~format;
01193       }
01194 
01195       /* Set up a preference list for audio. Do not include video in preferences 
01196          since we can not transcode video and have to use whatever is offered
01197        */
01198       if (pref && (format & AST_FORMAT_AUDIO_MASK)) {
01199          if (strcasecmp(this, "all")) {
01200             if (allowing) {
01201                ast_codec_pref_append(pref, format);
01202                ast_codec_pref_setsize(pref, format, framems);
01203             }
01204             else
01205                ast_codec_pref_remove(pref, format);
01206          } else if (!allowing) {
01207             memset(pref, 0, sizeof(*pref));
01208          }
01209       }
01210    }
01211    return errors;
01212 }

void ast_smoother_free struct ast_smoother s  ) 
 

Definition at line 286 of file frame.c.

References free, and s.

Referenced by ast_rtp_destroy(), and ast_rtp_write().

00287 {
00288    free(s);
00289 }

int ast_smoother_get_flags struct ast_smoother s  ) 
 

Definition at line 160 of file frame.c.

References s.

00161 {
00162    return s->flags;
00163 }

struct ast_smoother* ast_smoother_new int  size  ) 
 

Definition at line 150 of file frame.c.

References ast_malloc, ast_smoother_reset(), and s.

Referenced by ast_rtp_write().

00151 {
00152    struct ast_smoother *s;
00153    if (size < 1)
00154       return NULL;
00155    if ((s = ast_malloc(sizeof(*s))))
00156       ast_smoother_reset(s, size);
00157    return s;
00158 }

struct ast_frame* ast_smoother_read struct ast_smoother s  ) 
 

Definition at line 236 of file frame.c.

References AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), AST_SMOOTHER_FLAG_G729, ast_tvadd(), len, LOG_WARNING, and s.

Referenced by ast_rtp_write().

00237 {
00238    struct ast_frame *opt;
00239    int len;
00240 
00241    /* IF we have an optimization frame, send it */
00242    if (s->opt) {
00243       if (s->opt->offset < AST_FRIENDLY_OFFSET)
00244          ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).\n",
00245                      s->opt->offset);
00246       opt = s->opt;
00247       s->opt = NULL;
00248       return opt;
00249    }
00250 
00251    /* Make sure we have enough data */
00252    if (s->len < s->size) {
00253       /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
00254       if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
00255          return NULL;
00256    }
00257    len = s->size;
00258    if (len > s->len)
00259       len = s->len;
00260    /* Make frame */
00261    s->f.frametype = AST_FRAME_VOICE;
00262    s->f.subclass = s->format;
00263    s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
00264    s->f.offset = AST_FRIENDLY_OFFSET;
00265    s->f.datalen = len;
00266    /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
00267    s->f.samples = len * s->samplesperbyte;   /* XXX rounding */
00268    s->f.delivery = s->delivery;
00269    /* Fill Data */
00270    memcpy(s->f.data, s->data, len);
00271    s->len -= len;
00272    /* Move remaining data to the front if applicable */
00273    if (s->len) {
00274       /* In principle this should all be fine because if we are sending
00275          G.729 VAD, the next timestamp will take over anyawy */
00276       memmove(s->data, s->data + len, s->len);
00277       if (!ast_tvzero(s->delivery)) {
00278          /* If we have delivery time, increment it, otherwise, leave it at 0 */
00279          s->delivery = ast_tvadd(s->delivery, ast_samp2tv(s->f.samples, 8000));
00280       }
00281    }
00282    /* Return frame */
00283    return &s->f;
00284 }

void ast_smoother_reset struct ast_smoother s,
int  size
 

Definition at line 144 of file frame.c.

References s.

Referenced by ast_smoother_new().

00145 {
00146    memset(s, 0, sizeof(*s));
00147    s->size = size;
00148 }

void ast_smoother_set_flags struct ast_smoother s,
int  flags
 

Definition at line 165 of file frame.c.

References s.

Referenced by ast_rtp_write().

00166 {
00167    s->flags = flags;
00168 }

int ast_smoother_test_flag struct ast_smoother s,
int  flag
 

Definition at line 170 of file frame.c.

References s.

Referenced by ast_rtp_write().

00171 {
00172    return (s->flags & flag);
00173 }

void ast_swapcopy_samples void *  dst,
const void *  src,
int  samples
 

Definition at line 510 of file frame.c.

Referenced by __ast_smoother_feed(), iax_frame_wrap(), and phone_write_buf().

00511 {
00512    int i;
00513    unsigned short *dst_s = dst;
00514    const unsigned short *src_s = src;
00515 
00516    for (i = 0; i < samples; i++)
00517       dst_s[i] = (src_s[i]<<8) | (src_s[i]>>8);
00518 }

AST_THREADSTORAGE_CUSTOM frame_cache  ,
NULL  ,
frame_cache_cleanup 
 

A per-thread cache of frame headers.

static void frame_cache_cleanup void *  data  )  [static]
 

Definition at line 327 of file frame.c.

References AST_LIST_REMOVE_HEAD, frames, and free.

00328 {
00329    struct ast_frame_cache *frames = data;
00330    struct ast_frame *f;
00331 
00332    while ((f = AST_LIST_REMOVE_HEAD(&frames->list, frame_list)))
00333       free(f);
00334    
00335    free(frames);
00336 }

static int g723_len unsigned char  buf  )  [static]
 

Definition at line 1214 of file frame.c.

References ast_log(), LOG_WARNING, type, TYPE_DONTSEND, TYPE_HIGH, TYPE_LOW, TYPE_MASK, and TYPE_SILENCE.

Referenced by g723_samples().

01215 {
01216    enum frame_type type = buf & TYPE_MASK;
01217 
01218    switch(type) {
01219    case TYPE_DONTSEND:
01220       return 0;
01221       break;
01222    case TYPE_SILENCE:
01223       return 4;
01224       break;
01225    case TYPE_HIGH:
01226       return 24;
01227       break;
01228    case TYPE_LOW:
01229       return 20;
01230       break;
01231    default:
01232       ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", type);
01233    }
01234    return -1;
01235 }

static int g723_samples unsigned char *  buf,
int  maxlen
[static]
 

Definition at line 1237 of file frame.c.

References g723_len().

Referenced by ast_codec_get_samples().

01238 {
01239    int pos = 0;
01240    int samples = 0;
01241    int res;
01242    while(pos < maxlen) {
01243       res = g723_len(buf[pos]);
01244       if (res <= 0)
01245          break;
01246       samples += 240;
01247       pos += res;
01248    }
01249    return samples;
01250 }

static unsigned char get_n_bits_at unsigned char *  data,
int  n,
int  bit
[static]
 

Definition at line 1252 of file frame.c.

Referenced by speex_get_wb_sz_at(), and speex_samples().

01253 {
01254    int byte = bit / 8;       /* byte containing first bit */
01255    int rem = 8 - (bit % 8);  /* remaining bits in first byte */
01256    unsigned char ret = 0;
01257    
01258    if (n <= 0 || n > 8)
01259       return 0;
01260 
01261    if (rem < n) {
01262       ret = (data[byte] << (n - rem));
01263       ret |= (data[byte + 1] >> (8 - n + rem));
01264    } else {
01265       ret = (data[byte] >> (rem - n));
01266    }
01267 
01268    return (ret & (0xff >> (8 - n)));
01269 }

int init_framer void   ) 
 

Provided by frame.c

Definition at line 930 of file frame.c.

References ast_cli_register_multiple(), and my_clis.

00931 {
00932    ast_cli_register_multiple(my_clis, sizeof(my_clis) / sizeof(struct ast_cli_entry));
00933    return 0;   
00934 }

static int show_codec_n int  fd,
int  argc,
char *  argv[]
[static]
 

Definition at line 671 of file frame.c.

References ast_cli(), ast_codec2str(), and RESULT_SHOWUSAGE.

00672 {
00673    int codec, i, found=0;
00674 
00675    if (argc != 4)
00676       return RESULT_SHOWUSAGE;
00677 
00678    if (sscanf(argv[3],"%d",&codec) != 1)
00679       return RESULT_SHOWUSAGE;
00680 
00681    for (i = 0; i < 32; i++)
00682       if (codec & (1 << i)) {
00683          found = 1;
00684          ast_cli(fd, "%11u (1 << %2d)  %s\n",1 << i,i,ast_codec2str(1<<i));
00685       }
00686 
00687    if (!found)
00688       ast_cli(fd, "Codec %d not found\n", codec);
00689 
00690    return RESULT_SUCCESS;
00691 }

static int show_codecs int  fd,
int  argc,
char *  argv[]
[static]
 

Definition at line 623 of file frame.c.

References ast_cli(), ast_codec2str(), ast_getformatname(), ast_opt_dont_warn, and RESULT_SHOWUSAGE.

00624 {
00625    int i, found=0;
00626    char hex[25];
00627    
00628    if ((argc < 3) || (argc > 4))
00629       return RESULT_SHOWUSAGE;
00630 
00631    if (!ast_opt_dont_warn)
00632       ast_cli(fd, "Disclaimer: this command is for informational purposes only.\n"
00633             "\tIt does not indicate anything about your configuration.\n");
00634 
00635    ast_cli(fd, "%11s %9s %10s   TYPE   %8s   %s\n","INT","BINARY","HEX","NAME","DESC");
00636    ast_cli(fd, "--------------------------------------------------------------------------------\n");
00637    if ((argc == 3) || (!strcasecmp(argv[3],"audio"))) {
00638       found = 1;
00639       for (i=0;i<13;i++) {
00640          snprintf(hex,25,"(0x%x)",1<<i);
00641          ast_cli(fd, "%11u (1 << %2d) %10s  audio   %8s   (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
00642       }
00643    }
00644 
00645    if ((argc == 3) || (!strcasecmp(argv[3],"image"))) {
00646       found = 1;
00647       for (i=16;i<18;i++) {
00648          snprintf(hex,25,"(0x%x)",1<<i);
00649          ast_cli(fd, "%11u (1 << %2d) %10s  image   %8s   (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
00650       }
00651    }
00652 
00653    if ((argc == 3) || (!strcasecmp(argv[3],"video"))) {
00654       found = 1;
00655       for (i=18;i<22;i++) {
00656          snprintf(hex,25,"(0x%x)",1<<i);
00657          ast_cli(fd, "%11u (1 << %2d) %10s  video   %8s   (%s)\n",1 << i,i,hex,ast_getformatname(1<<i),ast_codec2str(1<<i));
00658       }
00659    }
00660 
00661    if (! found)
00662       return RESULT_SHOWUSAGE;
00663    else
00664       return RESULT_SUCCESS;
00665 }

static int speex_get_wb_sz_at unsigned char *  data,
int  len,
int  bit
[static]
 

Definition at line 1271 of file frame.c.

References ast_log(), get_n_bits_at(), and LOG_WARNING.

Referenced by speex_samples().

01272 {
01273    static int SpeexWBSubModeSz[] = {
01274       0, 36, 112, 192,
01275       352, 0, 0, 0 };
01276    int off = bit;
01277    unsigned char c;
01278 
01279    /* skip up to two wideband frames */
01280    if (((len * 8 - off) >= 5) && 
01281       get_n_bits_at(data, 1, off)) {
01282       c = get_n_bits_at(data, 3, off + 1);
01283       off += SpeexWBSubModeSz[c];
01284 
01285       if (((len * 8 - off) >= 5) && 
01286          get_n_bits_at(data, 1, off)) {
01287          c = get_n_bits_at(data, 3, off + 1);
01288          off += SpeexWBSubModeSz[c];
01289 
01290          if (((len * 8 - off) >= 5) && 
01291             get_n_bits_at(data, 1, off)) {
01292             ast_log(LOG_WARNING, "Encountered corrupt speex frame; too many wideband frames in a row.\n");
01293             return -1;
01294          }
01295       }
01296 
01297    }
01298    return off - bit;
01299 }

static int speex_samples unsigned char *  data,
int  len
[static]
 

Definition at line 1301 of file frame.c.

References ast_log(), get_n_bits_at(), LOG_WARNING, and speex_get_wb_sz_at().

Referenced by ast_codec_get_samples().

01302 {
01303    static int SpeexSubModeSz[] = {
01304                5, 43, 119, 160,
01305       220, 300, 364, 492, 
01306       79, 0, 0, 0,
01307       0, 0, 0, 0 };
01308    static int SpeexInBandSz[] = { 
01309       1, 1, 4, 4,
01310       4, 4, 4, 4,
01311       8, 8, 16, 16,
01312       32, 32, 64, 64 };
01313    int bit = 0;
01314    int cnt = 0;
01315    int off;
01316    unsigned char c;
01317 
01318    while ((len * 8 - bit) >= 5) {
01319       /* skip wideband frames */
01320       off = speex_get_wb_sz_at(data, len, bit);
01321       if (off < 0)  {
01322          ast_log(LOG_WARNING, "Had error while reading wideband frames for speex samples\n");
01323          break;
01324       }
01325       bit += off;
01326 
01327       if ((len * 8 - bit) < 5) {
01328          ast_log(LOG_WARNING, "Not enough bits remaining after wide band for speex samples.\n");
01329          break;
01330       }
01331 
01332       /* get control bits */
01333       c = get_n_bits_at(data, 5, bit);
01334       bit += 5;
01335 
01336       if (c == 15) { 
01337          /* terminator */
01338          break; 
01339       } else if (c == 14) {
01340          /* in-band signal; next 4 bits contain signal id */
01341          c = get_n_bits_at(data, 4, bit);
01342          bit += 4;
01343          bit += SpeexInBandSz[c];
01344       } else if (c == 13) {
01345          /* user in-band; next 5 bits contain msg len */
01346          c = get_n_bits_at(data, 5, bit);
01347          bit += 5;
01348          bit += c * 8;
01349       } else if (c > 8) {
01350          /* unknown */
01351          break;
01352       } else {
01353          /* skip number bits for submode (less the 5 control bits) */
01354          bit += SpeexSubModeSz[c] - 5;
01355          cnt += 160; /* new frame */
01356       }
01357    }
01358    return cnt;
01359 }


Variable Documentation

struct ast_format_list AST_FORMAT_LIST[] [static]
 

Definition of supported media formats (codecs).

Definition at line 106 of file frame.c.

Referenced by ast_codec2str(), ast_codec_choose(), ast_codec_pref_append(), ast_codec_pref_index(), ast_codec_pref_remove(), ast_codec_pref_setsize(), ast_get_format_list(), ast_get_format_list_index(), ast_getformatbyname(), ast_getformatname(), and ast_getformatname_multiple().

struct ast_frame ast_null_frame = { AST_FRAME_NULL, }
 

Queueing a null frame is fairly common, so we declare a global null frame object for this purpose instead of having to declare one on the stack

Definition at line 142 of file frame.c.

Referenced by __ast_read(), __oh323_rtp_create(), __oh323_update_info(), agent_new(), agent_read(), ast_channel_masquerade(), ast_channel_setwhentohangup(), ast_rtcp_read(), ast_rtp_read(), ast_softhangup_nolock(), ast_udptl_read(), features_read(), gtalk_rtp_read(), handle_request_invite(), handle_response_answer(), handle_response_invite(), iax2_read(), jingle_rtp_read(), local_read(), mgcp_rtp_read(), oh323_read(), oh323_rtp_read(), send_dtmf(), sip_rtp_read(), and skinny_rtp_read().

char frame_show_codec_n_usage[] [static]
 

Initial value:

"Usage: core show codec <number>\n"
"       Displays codec mapping\n"

Definition at line 693 of file frame.c.

const char frame_show_codecs_usage[] [static]
 

Initial value:

"Usage: core show codecs [audio|video|image]\n"
"       Displays codec mapping\n"

Definition at line 667 of file frame.c.

struct ast_cli_entry my_clis[] [static]
 

Definition at line 902 of file frame.c.

Referenced by init_framer().


Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen