Sat Nov 1 06:29:05 2008

Asterisk developer's documentation


frame.c File Reference

Frame and codec manipulation routines. More...

#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 "asterisk/translate.h"
#include "asterisk/dsp.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_prepend (struct ast_codec_pref *pref, int format, int only_if_existing)
 Prepend an audio codec to a preference list, removing it first if it was already there.
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.
void 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, frame_cache_init, 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_codec_n_deprecated (int fd, int argc, char *argv[])
static int show_codecs (int fd, int argc, char *argv[])
static int show_codecs_deprecated (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 struct ast_cli_entry cli_show_audio_codecs
static struct ast_cli_entry cli_show_codec
static struct ast_cli_entry cli_show_codecs
static struct ast_cli_entry cli_show_image_codecs
static struct ast_cli_entry cli_show_video_codecs
static char frame_show_codec_n_usage []
static char frame_show_codecs_usage []
static struct ast_cli_entry my_clis []


Detailed Description

Frame and codec manipulation routines.

Author:
Mark Spencer <markster@digium.com>

Definition in file frame.c.


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 69 of file frame.c.

Referenced by ast_frame_free(), and iax_frame_free().

#define SMOOTHER_SIZE   8000

Definition at line 81 of file frame.c.

Referenced by __ast_smoother_feed().

#define TYPE_MASK   0x3

Definition at line 90 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 83 of file frame.c.

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


Function Documentation

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

Definition at line 172 of file frame.c.

References AST_FRAME_VOICE, ast_log(), AST_MIN_OFFSET, AST_SMOOTHER_FLAG_G729, ast_swapcopy_samples(), f, LOG_NOTICE, LOG_WARNING, s, and SMOOTHER_SIZE.

00173 {
00174    if (f->frametype != AST_FRAME_VOICE) {
00175       ast_log(LOG_WARNING, "Huh?  Can't smooth a non-voice frame!\n");
00176       return -1;
00177    }
00178    if (!s->format) {
00179       s->format = f->subclass;
00180       s->samplesperbyte = (float)f->samples / (float)f->datalen;
00181    } else if (s->format != f->subclass) {
00182       ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
00183       return -1;
00184    }
00185    if (s->len + f->datalen > SMOOTHER_SIZE) {
00186       ast_log(LOG_WARNING, "Out of smoother space\n");
00187       return -1;
00188    }
00189    if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
00190              && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
00191       if (!s->len) {
00192          /* Optimize by sending the frame we just got
00193             on the next read, thus eliminating the douple
00194             copy */
00195          if (swap)
00196             ast_swapcopy_samples(f->data, f->data, f->samples);
00197          s->opt = f;
00198          return 0;
00199       }
00200    }
00201    if (s->flags & AST_SMOOTHER_FLAG_G729) {
00202       if (s->len % 10) {
00203          ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
00204          return 0;
00205       }
00206    }
00207    if (swap)
00208       ast_swapcopy_samples(s->data+s->len, f->data, f->samples);
00209    else
00210       memcpy(s->data + s->len, f->data, f->datalen);
00211    /* If either side is empty, reset the delivery time */
00212    if (!s->len || ast_tvzero(f->delivery) || ast_tvzero(s->delivery))   /* XXX really ? */
00213       s->delivery = f->delivery;
00214    s->len += f->datalen;
00215    return 0;
00216 }

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 600 of file frame.c.

References AST_FORMAT_LIST, and desc.

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

00601 {
00602    int x;
00603    char *ret = "unknown";
00604    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
00605       if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == codec) {
00606          ret = AST_FORMAT_LIST[x].desc;
00607          break;
00608       }
00609    }
00610    return ret;
00611 }

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 1263 of file frame.c.

References ast_best_codec(), AST_FORMAT_AUDIO_MASK, AST_FORMAT_LIST, ast_log(), ast_format_list::bits, LOG_DEBUG, option_debug, and ast_codec_pref::order.

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

01264 {
01265    int x, ret = 0, slot;
01266 
01267    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01268       slot = pref->order[x];
01269 
01270       if (!slot)
01271          break;
01272       if (formats & AST_FORMAT_LIST[slot-1].bits) {
01273          ret = AST_FORMAT_LIST[slot-1].bits;
01274          break;
01275       }
01276    }
01277    if(ret & AST_FORMAT_AUDIO_MASK)
01278       return ret;
01279 
01280    if (option_debug > 3)
01281       ast_log(LOG_DEBUG, "Could not find preferred codec - %s\n", find_best ? "Going for the best codec" : "Returning zero codec");
01282 
01283       return find_best ? ast_best_codec(formats) : 0;
01284 }

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 1522 of file frame.c.

References AST_FORMAT_ADPCM, AST_FORMAT_ALAW, AST_FORMAT_G722, 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 moh_generate(), and monmp3thread().

01523 {
01524    int len = 0;
01525 
01526    /* XXX Still need speex, g723, and lpc10 XXX */ 
01527    switch(format) {
01528    case AST_FORMAT_ILBC:
01529       len = (samples / 240) * 50;
01530       break;
01531    case AST_FORMAT_GSM:
01532       len = (samples / 160) * 33;
01533       break;
01534    case AST_FORMAT_G729A:
01535       len = samples / 8;
01536       break;
01537    case AST_FORMAT_SLINEAR:
01538       len = samples * 2;
01539       break;
01540    case AST_FORMAT_ULAW:
01541    case AST_FORMAT_ALAW:
01542       len = samples;
01543       break;
01544    case AST_FORMAT_G722:
01545    case AST_FORMAT_ADPCM:
01546    case AST_FORMAT_G726:
01547    case AST_FORMAT_G726_AAL2:
01548       len = samples / 2;
01549       break;
01550    default:
01551       ast_log(LOG_WARNING, "Unable to calculate sample length for format %s\n", ast_getformatname(format));
01552    }
01553 
01554    return len;
01555 }

int ast_codec_get_samples ( struct ast_frame f  ) 

Returns the number of samples contained in the frame.

Definition at line 1479 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(), f, g723_samples(), LOG_WARNING, and speex_samples().

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

01480 {
01481    int samples=0;
01482    switch(f->subclass) {
01483    case AST_FORMAT_SPEEX:
01484       samples = speex_samples(f->data, f->datalen);
01485       break;
01486    case AST_FORMAT_G723_1:
01487                 samples = g723_samples(f->data, f->datalen);
01488       break;
01489    case AST_FORMAT_ILBC:
01490       samples = 240 * (f->datalen / 50);
01491       break;
01492    case AST_FORMAT_GSM:
01493       samples = 160 * (f->datalen / 33);
01494       break;
01495    case AST_FORMAT_G729A:
01496       samples = f->datalen * 8;
01497       break;
01498    case AST_FORMAT_SLINEAR:
01499       samples = f->datalen / 2;
01500       break;
01501    case AST_FORMAT_LPC10:
01502                 /* assumes that the RTP packet contains one LPC10 frame */
01503       samples = 22 * 8;
01504       samples += (((char *)(f->data))[7] & 0x1) * 8;
01505       break;
01506    case AST_FORMAT_ULAW:
01507    case AST_FORMAT_ALAW:
01508       samples = f->datalen;
01509       break;
01510    case AST_FORMAT_G722:
01511    case AST_FORMAT_ADPCM:
01512    case AST_FORMAT_G726:
01513    case AST_FORMAT_G726_AAL2:
01514       samples = f->datalen * 2;
01515       break;
01516    default:
01517       ast_log(LOG_WARNING, "Unable to calculate samples for format %s\n", ast_getformatname(f->subclass));
01518    }
01519    return samples;
01520 }

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 1122 of file frame.c.

References ast_codec_pref_remove(), AST_FORMAT_LIST, and ast_codec_pref::order.

Referenced by ast_parse_allow_disallow().

01123 {
01124    int x, newindex = 0;
01125 
01126    ast_codec_pref_remove(pref, format);
01127 
01128    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01129       if(AST_FORMAT_LIST[x].bits == format) {
01130          newindex = x + 1;
01131          break;
01132       }
01133    }
01134 
01135    if(newindex) {
01136       for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01137          if(!pref->order[x]) {
01138             pref->order[x] = newindex;
01139             break;
01140          }
01141       }
01142    }
01143 
01144    return x;
01145 }

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 1024 of file frame.c.

References ast_codec_pref::order.

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

01025 {
01026    int x, differential = (int) 'A', mem;
01027    char *from, *to;
01028 
01029    if(right) {
01030       from = pref->order;
01031       to = buf;
01032       mem = size;
01033    } else {
01034       to = pref->order;
01035       from = buf;
01036       mem = 32;
01037    }
01038 
01039    memset(to, 0, mem);
01040    for (x = 0; x < 32 ; x++) {
01041       if(!from[x])
01042          break;
01043       to[x] = right ? (from[x] + differential) : (from[x] - differential);
01044    }
01045 }

struct ast_format_list ast_codec_pref_getsize ( struct ast_codec_pref pref,
int  format 
)

Get packet size for codec.

Definition at line 1224 of file frame.c.

References AST_FORMAT_LIST, ast_format_list::bits, fmt, and format.

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

01225 {
01226    int x, index = -1, framems = 0;
01227    struct ast_format_list fmt = {0};
01228 
01229    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01230       if(AST_FORMAT_LIST[x].bits == format) {
01231          fmt = AST_FORMAT_LIST[x];
01232          index = x;
01233          break;
01234       }
01235    }
01236 
01237    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01238       if(pref->order[x] == (index + 1)) {
01239          framems = pref->framing[x];
01240          break;
01241       }
01242    }
01243 
01244    /* size validation */
01245    if(!framems)
01246       framems = AST_FORMAT_LIST[index].def_ms;
01247 
01248    if(AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
01249       framems -= framems % AST_FORMAT_LIST[index].inc_ms;
01250 
01251    if(framems < AST_FORMAT_LIST[index].min_ms)
01252       framems = AST_FORMAT_LIST[index].min_ms;
01253 
01254    if(framems > AST_FORMAT_LIST[index].max_ms)
01255       framems = AST_FORMAT_LIST[index].max_ms;
01256 
01257    fmt.cur_ms = framems;
01258 
01259    return fmt;
01260 }

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 1082 of file frame.c.

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

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

01083 {
01084    int slot = 0;
01085 
01086    
01087    if((index >= 0) && (index < sizeof(pref->order))) {
01088       slot = pref->order[index];
01089    }
01090 
01091    return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
01092 }

void ast_codec_pref_prepend ( struct ast_codec_pref pref,
int  format,
int  only_if_existing 
)

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

Definition at line 1148 of file frame.c.

References ARRAY_LEN, AST_FORMAT_LIST, ast_codec_pref::framing, and ast_codec_pref::order.

Referenced by create_addr().

01149 {
01150    int x, newindex = 0;
01151 
01152    /* First step is to get the codecs "index number" */
01153    for (x = 0; x < ARRAY_LEN(AST_FORMAT_LIST); x++) {
01154       if (AST_FORMAT_LIST[x].bits == format) {
01155          newindex = x + 1;
01156          break;
01157       }
01158    }
01159    /* Done if its unknown */
01160    if (!newindex)
01161       return;
01162 
01163    /* Now find any existing occurrence, or the end */
01164    for (x = 0; x < 32; x++) {
01165       if (!pref->order[x] || pref->order[x] == newindex)
01166          break;
01167    }
01168 
01169    if (only_if_existing && !pref->order[x])
01170       return;
01171 
01172    /* Move down to make space to insert - either all the way to the end,
01173       or as far as the existing location (which will be overwritten) */
01174    for (; x > 0; x--) {
01175       pref->order[x] = pref->order[x - 1];
01176       pref->framing[x] = pref->framing[x - 1];
01177    }
01178 
01179    /* And insert the new entry */
01180    pref->order[0] = newindex;
01181    pref->framing[0] = 0; /* ? */
01182 }

void ast_codec_pref_remove ( struct ast_codec_pref pref,
int  format 
)

Remove audio a codec from a preference list.

Definition at line 1095 of file frame.c.

References AST_FORMAT_LIST, and ast_codec_pref::order.

Referenced by ast_codec_pref_append(), and ast_parse_allow_disallow().

01096 {
01097    struct ast_codec_pref oldorder;
01098    int x, y = 0;
01099    int slot;
01100    int size;
01101 
01102    if(!pref->order[0])
01103       return;
01104 
01105    memcpy(&oldorder, pref, sizeof(oldorder));
01106    memset(pref, 0, sizeof(*pref));
01107 
01108    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01109       slot = oldorder.order[x];
01110       size = oldorder.framing[x];
01111       if(! slot)
01112          break;
01113       if(AST_FORMAT_LIST[slot-1].bits != format) {
01114          pref->order[y] = slot;
01115          pref->framing[y++] = size;
01116       }
01117    }
01118    
01119 }

int ast_codec_pref_setsize ( struct ast_codec_pref pref,
int  format,
int  framems 
)

Set packet size for codec.

Definition at line 1185 of file frame.c.

References AST_FORMAT_LIST, ast_codec_pref::framing, and ast_codec_pref::order.

Referenced by ast_parse_allow_disallow(), and process_sdp().

01186 {
01187    int x, index = -1;
01188 
01189    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01190       if(AST_FORMAT_LIST[x].bits == format) {
01191          index = x;
01192          break;
01193       }
01194    }
01195 
01196    if(index < 0)
01197       return -1;
01198 
01199    /* size validation */
01200    if(!framems)
01201       framems = AST_FORMAT_LIST[index].def_ms;
01202 
01203    if(AST_FORMAT_LIST[index].inc_ms && framems % AST_FORMAT_LIST[index].inc_ms) /* avoid division by zero */
01204       framems -= framems % AST_FORMAT_LIST[index].inc_ms;
01205 
01206    if(framems < AST_FORMAT_LIST[index].min_ms)
01207       framems = AST_FORMAT_LIST[index].min_ms;
01208 
01209    if(framems > AST_FORMAT_LIST[index].max_ms)
01210       framems = AST_FORMAT_LIST[index].max_ms;
01211 
01212 
01213    for (x = 0; x < sizeof(AST_FORMAT_LIST) / sizeof(AST_FORMAT_LIST[0]); x++) {
01214       if(pref->order[x] == (index + 1)) {
01215          pref->framing[x] = framems;
01216          break;
01217       }
01218    }
01219 
01220    return x;
01221 }

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 1047 of file frame.c.

References ast_codec_pref_index(), and ast_getformatname().

Referenced by dump_prefs(), and socket_process().

01048 {
01049    int x, codec; 
01050    size_t total_len, slen;
01051    char *formatname;
01052    
01053    memset(buf,0,size);
01054    total_len = size;
01055    buf[0] = '(';
01056    total_len--;
01057    for(x = 0; x < 32 ; x++) {
01058       if(total_len <= 0)
01059          break;
01060       if(!(codec = ast_codec_pref_index(pref,x)))
01061          break;
01062       if((formatname = ast_getformatname(codec))) {
01063          slen = strlen(formatname);
01064          if(slen > total_len)
01065             break;
01066          strncat(buf, formatname, total_len - 1); /* safe */
01067          total_len -= slen;
01068       }
01069       if(total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
01070          strncat(buf, "|", total_len - 1); /* safe */
01071          total_len--;
01072       }
01073    }
01074    if(total_len) {
01075       strncat(buf, ")", total_len - 1); /* safe */
01076       total_len--;
01077    }
01078 
01079    return size - total_len;
01080 }

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

Definition at line 571 of file frame.c.

Referenced by ast_getformatbyname().

00572 {
00573    int x;
00574 
00575    for (x = 0; x < sizeof(ast_codec_alias_table) / sizeof(ast_codec_alias_table[0]); x++) {
00576       if(!strcmp(in,ast_codec_alias_table[x].alias))
00577          return ast_codec_alias_table[x].realname;
00578    }
00579    return in;
00580 }

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 1557 of file frame.c.

References AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_slinear_saturated_divide(), ast_slinear_saturated_multiply(), and f.

Referenced by audiohook_read_frame_single(), and conf_run().

01558 {
01559    int count;
01560    short *fdata = f->data;
01561    short adjust_value = abs(adjustment);
01562 
01563    if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
01564       return -1;
01565 
01566    if (!adjustment)
01567       return 0;
01568 
01569    for (count = 0; count < f->samples; count++) {
01570       if (adjustment > 0) {
01571          ast_slinear_saturated_multiply(&fdata[count], &adjust_value);
01572       } else if (adjustment < 0) {
01573          ast_slinear_saturated_divide(&fdata[count], &adjust_value);
01574       }
01575    }
01576 
01577    return 0;
01578 }

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

Dump a frame for debugging purposes

Definition at line 754 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, f, and term_color().

Referenced by __ast_read(), and ast_write().

00755 {
00756    const char noname[] = "unknown";
00757    char ftype[40] = "Unknown Frametype";
00758    char cft[80];
00759    char subclass[40] = "Unknown Subclass";
00760    char csub[80];
00761    char moreinfo[40] = "";
00762    char cn[60];
00763    char cp[40];
00764    char cmn[40];
00765 
00766    if (!name)
00767       name = noname;
00768 
00769 
00770    if (!f) {
00771       ast_verbose("%s [ %s (NULL) ] [%s]\n", 
00772          term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00773          term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)), 
00774          term_color(cn, name, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00775       return;
00776    }
00777    /* XXX We should probably print one each of voice and video when the format changes XXX */
00778    if (f->frametype == AST_FRAME_VOICE)
00779       return;
00780    if (f->frametype == AST_FRAME_VIDEO)
00781       return;
00782    switch(f->frametype) {
00783    case AST_FRAME_DTMF_BEGIN:
00784       strcpy(ftype, "DTMF Begin");
00785       subclass[0] = f->subclass;
00786       subclass[1] = '\0';
00787       break;
00788    case AST_FRAME_DTMF_END:
00789       strcpy(ftype, "DTMF End");
00790       subclass[0] = f->subclass;
00791       subclass[1] = '\0';
00792       break;
00793    case AST_FRAME_CONTROL:
00794       strcpy(ftype, "Control");
00795       switch(f->subclass) {
00796       case AST_CONTROL_HANGUP:
00797          strcpy(subclass, "Hangup");
00798          break;
00799       case AST_CONTROL_RING:
00800          strcpy(subclass, "Ring");
00801          break;
00802       case AST_CONTROL_RINGING:
00803          strcpy(subclass, "Ringing");
00804          break;
00805       case AST_CONTROL_ANSWER:
00806          strcpy(subclass, "Answer");
00807          break;
00808       case AST_CONTROL_BUSY:
00809          strcpy(subclass, "Busy");
00810          break;
00811       case AST_CONTROL_TAKEOFFHOOK:
00812          strcpy(subclass, "Take Off Hook");
00813          break;
00814       case AST_CONTROL_OFFHOOK:
00815          strcpy(subclass, "Line Off Hook");
00816          break;
00817       case AST_CONTROL_CONGESTION:
00818          strcpy(subclass, "Congestion");
00819          break;
00820       case AST_CONTROL_FLASH:
00821          strcpy(subclass, "Flash");
00822          break;
00823       case AST_CONTROL_WINK:
00824          strcpy(subclass, "Wink");
00825          break;
00826       case AST_CONTROL_OPTION:
00827          strcpy(subclass, "Option");
00828          break;
00829       case AST_CONTROL_RADIO_KEY:
00830          strcpy(subclass, "Key Radio");
00831          break;
00832       case AST_CONTROL_RADIO_UNKEY:
00833          strcpy(subclass, "Unkey Radio");
00834          break;
00835       case -1:
00836          strcpy(subclass, "Stop generators");
00837          break;
00838       default:
00839          snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
00840       }
00841       break;
00842    case AST_FRAME_NULL:
00843       strcpy(ftype, "Null Frame");
00844       strcpy(subclass, "N/A");
00845       break;
00846    case AST_FRAME_IAX:
00847       /* Should never happen */
00848       strcpy(ftype, "IAX Specific");
00849       snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
00850       break;
00851    case AST_FRAME_TEXT:
00852       strcpy(ftype, "Text");
00853       strcpy(subclass, "N/A");
00854       ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
00855       break;
00856    case AST_FRAME_IMAGE:
00857       strcpy(ftype, "Image");
00858       snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
00859       break;
00860    case AST_FRAME_HTML:
00861       strcpy(ftype, "HTML");
00862       switch(f->subclass) {
00863       case AST_HTML_URL:
00864          strcpy(subclass, "URL");
00865          ast_copy_string(moreinfo, f->data, sizeof(moreinfo));
00866          break;
00867       case AST_HTML_DATA:
00868          strcpy(subclass, "Data");
00869          break;
00870       case AST_HTML_BEGIN:
00871          strcpy(subclass, "Begin");
00872          break;
00873       case AST_HTML_END:
00874          strcpy(subclass, "End");
00875          break;
00876       case AST_HTML_LDCOMPLETE:
00877          strcpy(subclass, "Load Complete");
00878          break;
00879       case AST_HTML_NOSUPPORT:
00880          strcpy(subclass, "No Support");
00881          break;
00882