Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

frame.c File Reference

#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 <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "asterisk.h"

Go to the source code of this file.

Defines

#define SMOOTHER_SIZE   8000

Functions

void ast_smoother_reset (struct ast_smoother *s, int size)
ast_smootherast_smoother_new (int size)
int ast_smoother_get_flags (struct ast_smoother *s)
void ast_smoother_set_flags (struct ast_smoother *s, int flags)
int ast_smoother_feed (struct ast_smoother *s, struct ast_frame *f)
ast_frameast_smoother_read (struct ast_smoother *s)
void ast_smoother_free (struct ast_smoother *s)
void ast_frfree (struct ast_frame *fr)
 Frees a frame.
ast_frameast_frisolate (struct ast_frame *fr)
 Copies a frame.
ast_frameast_frdup (struct ast_frame *f)
 Copies a frame.
ast_frameast_fr_fdread (int fd)
 Reads a frame from an fd.
int ast_fr_fdwrite (int fd, struct ast_frame *frame)
 Writes a frame to an fd.
int ast_fr_fdhangup (int fd)
 Sends a hangup to an fd.
ast_format_listast_get_format_list_index (int index)
ast_format_listast_get_format_list (size_t *size)
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.
int ast_getformatbyname (char *name)
char * ast_codec2str (int codec)
 Get a name from a format.
void ast_frame_dump (char *name, struct ast_frame *f, char *prefix)
int init_framer (void)
void ast_codec_pref_shift (struct ast_codec_pref *pref, char *buf, size_t size, int right)
int ast_codec_pref_string (struct ast_codec_pref *pref, char *buf, size_t size)
int ast_codec_pref_index (struct ast_codec_pref *pref, int index)
void ast_codec_pref_remove (struct ast_codec_pref *pref, int format)
int ast_codec_pref_append (struct ast_codec_pref *pref, int format)
int ast_codec_choose (struct ast_codec_pref *pref, int formats, int find_best)
void ast_parse_allow_disallow (struct ast_codec_pref *pref, int *mask, char *list, int allowing)

Variables

ast_cli_entry cli_show_codecs
ast_cli_entry cli_show_codecs_audio
ast_cli_entry cli_show_codecs_video
ast_cli_entry cli_show_codecs_image
ast_cli_entry cli_show_codec_n


Define Documentation

#define SMOOTHER_SIZE   8000
 

Definition at line 35 of file frame.c.

Referenced by ast_smoother_feed().


Function Documentation

char* ast_codec2str int  codec  ) 
 

Get a name from a format.

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

Definition at line 516 of file frame.c.

00516                                {
00517    int x = 0;
00518    char *ret = "unknown";
00519    for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
00520       if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == codec) {
00521          ret = AST_FORMAT_LIST[x].desc;
00522          break;
00523       }
00524    }
00525    return ret;
00526 }

int ast_codec_choose struct ast_codec_pref pref,
int  formats,
int  find_best
 

Definition at line 939 of file frame.c.

References ast_best_codec(), ast_format_list::bits, and ast_codec_pref::order.

00940 {
00941    size_t size = 0;
00942    int x = 0, ret = 0, slot = 0;
00943 
00944    size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
00945    for (x = 0; x < size; x++) {
00946       slot = pref->order[x];
00947 
00948       if(!slot)
00949          break;
00950       if ( formats & AST_FORMAT_LIST[slot-1].bits ) {
00951          ret = AST_FORMAT_LIST[slot-1].bits;
00952          break;
00953       }
00954    }
00955    if(ret)
00956       return ret;
00957 
00958       return find_best ? ast_best_codec(formats) : 0;
00959 }

int ast_codec_pref_append struct ast_codec_pref pref,
int  format
 

Definition at line 910 of file frame.c.

References ast_codec_pref_remove(), ast_format_list::bits, and ast_codec_pref::order.

Referenced by ast_parse_allow_disallow().

00911 {
00912    size_t size = 0;
00913    int x = 0, newindex = -1;
00914 
00915    ast_codec_pref_remove(pref, format);
00916    size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
00917 
00918    for (x = 0; x < size; x++) {
00919       if(AST_FORMAT_LIST[x].bits == format) {
00920          newindex = x + 1;
00921          break;
00922       }
00923    }
00924 
00925    if(newindex) {
00926       for (x = 0; x < size; x++) {
00927          if(!pref->order[x]) {
00928             pref->order[x] = newindex;
00929             break;
00930          }
00931       }
00932    }
00933 
00934    return x;
00935 }

int ast_codec_pref_index struct ast_codec_pref pref,
int  index
 

Definition at line 871 of file frame.c.

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

Referenced by ast_codec_pref_string().

00872 {
00873    int slot = 0;
00874 
00875    
00876    if((index >= 0) && (index < sizeof(pref->order))) {
00877       slot = pref->order[index];
00878    }
00879 
00880    return slot ? AST_FORMAT_LIST[slot-1].bits : 0;
00881 }

void ast_codec_pref_remove struct ast_codec_pref pref,
int  format
 

Definition at line 884 of file frame.c.

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

Referenced by ast_codec_pref_append(), and ast_parse_allow_disallow().

00885 {
00886    struct ast_codec_pref oldorder;
00887    int x=0, y=0;
00888    size_t size = 0;
00889    int slot = 0;
00890 
00891    if(!pref->order[0])
00892       return;
00893 
00894    size = sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list);
00895 
00896    memcpy(&oldorder,pref,sizeof(struct ast_codec_pref));
00897    memset(pref,0,sizeof(struct ast_codec_pref));
00898 
00899    for (x = 0; x < size; x++) {
00900       slot = oldorder.order[x];
00901       if(! slot)
00902          break;
00903       if(AST_FORMAT_LIST[slot-1].bits != format)
00904          pref->order[y++] = slot;
00905    }
00906    
00907 }

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

Definition at line 813 of file frame.c.

References ast_codec_pref::order.

00814 {
00815    int x = 0, differential = 65, mem = 0;
00816    char *from = NULL, *to = NULL;
00817 
00818    if(right) {
00819       from = pref->order;
00820       to = buf;
00821       mem = size;
00822    } else {
00823       to = pref->order;
00824       from = buf;
00825       mem = 32;
00826    }
00827 
00828    memset(to, 0, mem);
00829    for (x = 0; x < 32 ; x++) {
00830       if(!from[x])
00831          break;
00832       to[x] = right ? (from[x] + differential) : (from[x] - differential);
00833    }
00834 }

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

Definition at line 836 of file frame.c.

References ast_codec_pref_index(), and ast_getformatname().

00837 {
00838    int x = 0, codec = 0; 
00839    size_t total_len = 0, slen = 0;
00840    char *formatname = 0;
00841    
00842    memset(buf,0,size);
00843    total_len = size;
00844    buf[0] = '(';
00845    total_len--;
00846    for(x = 0; x < 32 ; x++) {
00847       if(total_len <= 0)
00848          break;
00849       if(!(codec = ast_codec_pref_index(pref,x)))
00850          break;
00851       if((formatname = ast_getformatname(codec))) {
00852          slen = strlen(formatname);
00853          if(slen > total_len)
00854             break;
00855          strncat(buf,formatname,total_len);
00856          total_len -= slen;
00857       }
00858       if(total_len && x < 31 && ast_codec_pref_index(pref , x + 1)) {
00859          strncat(buf,"|",total_len);
00860          total_len--;
00861       }
00862    }
00863    if(total_len) {
00864       strncat(buf,")",total_len);
00865       total_len--;
00866    }
00867 
00868    return size - total_len;
00869 }

int ast_fr_fdhangup int  fd  ) 
 

Sends a hangup to an fd.

Parameters:
fd fd to write to Send a hangup (NULL equivalent) on an fd Returns 0 on success, -1 on failure

Definition at line 393 of file frame.c.

References AST_CONTROL_HANGUP, ast_fr_fdwrite(), and AST_FRAME_CONTROL.

00394 {
00395    struct ast_frame hangup = {
00396       AST_FRAME_CONTROL,
00397       AST_CONTROL_HANGUP
00398    };
00399    return ast_fr_fdwrite(fd, &hangup);
00400 }

struct ast_frame* ast_fr_fdread int  fd  ) 
 

Reads a frame from an fd.

Parameters:
fd an opened fd to read from Read a frame from a stream or packet fd, as written by fd_write returns a frame on success, NULL on error

Definition at line 331 of file frame.c.

References AST_CONTROL_HANGUP, AST_FRAME_CONTROL, ast_frisolate(), ast_log(), ast_frame::data, ast_frame::datalen, ast_frame::frametype, LOG_WARNING, ast_frame::mallocd, ast_frame::offset, ast_frame::src, and ast_frame::subclass.

00332 {
00333    char buf[65536];
00334    int res;
00335    int ttl = sizeof(struct ast_frame);
00336    struct ast_frame *f = (struct ast_frame *)buf;
00337    /* Read a frame directly from there.  They're always in the
00338       right format. */
00339    
00340    while(ttl) {
00341       res = read(fd, buf, ttl);
00342       if (res < 0) {
00343          ast_log(LOG_WARNING, "Bad read on %d: %s\n", fd, strerror(errno));
00344          return NULL;
00345       }
00346       ttl -= res;
00347    }
00348    
00349    /* read the frame header */
00350    f->mallocd = 0;
00351    /* Re-write data position */
00352    f->data = buf + sizeof(struct ast_frame);
00353    f->offset = 0;
00354    /* Forget about being mallocd */
00355    f->mallocd = 0;
00356    /* Re-write the source */
00357    f->src = __FUNCTION__;
00358    if (f->datalen > sizeof(buf) - sizeof(struct ast_frame)) {
00359       /* Really bad read */
00360       ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
00361       return NULL;
00362    }
00363    if (f->datalen) {
00364       if ((res = read(fd, f->data, f->datalen)) != f->datalen) {
00365          /* Bad read */
00366          ast_log(LOG_WARNING, "How very strange, expected %d, got %d\n", f->datalen, res);
00367          return NULL;
00368       }
00369    }
00370    if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP)) {
00371       return NULL;
00372    }
00373    return ast_frisolate(f);
00374 }

int ast_fr_fdwrite int  fd,
struct ast_frame frame
 

Writes a frame to an fd.

Parameters:
fd Which fd to write to
frame frame to write to the fd Write a frame to an fd Returns 0 on success, -1 on failure

Definition at line 379 of file frame.c.

References ast_log(), ast_frame::data, ast_frame::datalen, and LOG_WARNING.

Referenced by ast_fr_fdhangup().

00380 {
00381    /* Write the frame exactly */
00382    if (write(fd, frame, sizeof(struct ast_frame)) != sizeof(struct ast_frame)) {
00383       ast_log(LOG_WARNING, "Write error: %s\n", strerror(errno));
00384       return -1;
00385    }
00386    if (write(fd, frame->data, frame->datalen) != frame->datalen) {
00387       ast_log(LOG_WARNING, "Write error: %s\n", strerror(errno));
00388       return -1;
00389    }
00390    return 0;
00391 }

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

Definition at line 614 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, AST_FRAME_HTML, AST_FRAME_IAX, AST_FRAME_IMAGE, 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_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().

00615 {
00616    char *n = "unknown";
00617    char ftype[40] = "Unknown Frametype";
00618    char cft[80];
00619    char subclass[40] = "Unknown Subclass";
00620    char csub[80];
00621    char moreinfo[40] = "";
00622    char cn[60];
00623    char cp[40];
00624    char cmn[40];
00625    if (name)
00626       n = name;
00627    if (!f) {
00628       ast_verbose("%s [ %s (NULL) ] [%s]\n", 
00629          term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00630          term_color(cft, "HANGUP", COLOR_BRRED, COLOR_BLACK, sizeof(cft)), 
00631          term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00632       return;
00633    }
00634    /* XXX We should probably print one each of voice and video when the format changes XXX */
00635    if (f->frametype == AST_FRAME_VOICE)
00636       return;
00637    if (f->frametype == AST_FRAME_VIDEO)
00638       return;
00639    switch(f->frametype) {
00640    case AST_FRAME_DTMF:
00641       strcpy(ftype, "DTMF");
00642       subclass[0] = f->subclass;
00643       subclass[1] = '\0';
00644       break;
00645    case AST_FRAME_CONTROL:
00646       strcpy(ftype, "Control");
00647       switch(f->subclass) {
00648       case AST_CONTROL_HANGUP:
00649          strcpy(subclass, "Hangup");
00650          break;
00651       case AST_CONTROL_RING:
00652          strcpy(subclass, "Ring");
00653          break;
00654       case AST_CONTROL_RINGING:
00655          strcpy(subclass, "Ringing");
00656          break;
00657       case AST_CONTROL_ANSWER:
00658          strcpy(subclass, "Answer");
00659          break;
00660       case AST_CONTROL_BUSY:
00661          strcpy(subclass, "Busy");
00662          break;
00663       case AST_CONTROL_TAKEOFFHOOK:
00664          strcpy(subclass, "Take Off Hook");
00665          break;
00666       case AST_CONTROL_OFFHOOK:
00667          strcpy(subclass, "Line Off Hook");
00668          break;
00669       case AST_CONTROL_CONGESTION:
00670          strcpy(subclass, "Congestion");
00671          break;
00672       case AST_CONTROL_FLASH:
00673          strcpy(subclass, "Flash");
00674          break;
00675       case AST_CONTROL_WINK:
00676          strcpy(subclass, "Wink");
00677          break;
00678       case AST_CONTROL_OPTION:
00679          strcpy(subclass, "Option");
00680          break;
00681       case AST_CONTROL_RADIO_KEY:
00682          strcpy(subclass, "Key Radio");
00683          break;
00684       case AST_CONTROL_RADIO_UNKEY:
00685          strcpy(subclass, "Unkey Radio");
00686          break;
00687       case -1:
00688          strcpy(subclass, "Stop generators");
00689          break;
00690       default:
00691          snprintf(subclass, sizeof(subclass), "Unknown control '%d'", f->subclass);
00692       }
00693       break;
00694    case AST_FRAME_NULL:
00695       strcpy(ftype, "Null Frame");
00696       strcpy(subclass, "N/A");
00697       break;
00698    case AST_FRAME_IAX:
00699       /* Should never happen */
00700       strcpy(ftype, "IAX Specific");
00701       snprintf(subclass, sizeof(subclass), "IAX Frametype %d", f->subclass);
00702       break;
00703    case AST_FRAME_TEXT:
00704       strcpy(ftype, "Text");
00705       strcpy(subclass, "N/A");
00706       strncpy(moreinfo, f->data, sizeof(moreinfo) - 1);
00707       break;
00708    case AST_FRAME_IMAGE:
00709       strcpy(ftype, "Image");
00710       snprintf(subclass, sizeof(subclass), "Image format %s\n", ast_getformatname(f->subclass));
00711       break;
00712    case AST_FRAME_HTML:
00713       strcpy(ftype, "HTML");
00714       switch(f->subclass) {
00715       case AST_HTML_URL:
00716          strcpy(subclass, "URL");
00717          strncpy(moreinfo, f->data, sizeof(moreinfo) - 1);
00718          break;
00719       case AST_HTML_DATA:
00720          strcpy(subclass, "Data");
00721          break;
00722       case AST_HTML_BEGIN:
00723          strcpy(subclass, "Begin");
00724          break;
00725       case AST_HTML_END:
00726          strcpy(subclass, "End");
00727          break;
00728       case AST_HTML_LDCOMPLETE:
00729          strcpy(subclass, "Load Complete");
00730          break;
00731       case AST_HTML_NOSUPPORT:
00732          strcpy(subclass, "No Support");
00733          break;
00734       case AST_HTML_LINKURL:
00735          strcpy(subclass, "Link URL");
00736          strncpy(moreinfo, f->data, sizeof(moreinfo) - 1);
00737          break;
00738       case AST_HTML_UNLINK:
00739          strcpy(subclass, "Unlink");
00740          break;
00741       case AST_HTML_LINKREJECT:
00742          strcpy(subclass, "Link Reject");
00743          break;
00744       default:
00745          snprintf(subclass, sizeof(subclass), "Unknown HTML frame '%d'\n", f->subclass);
00746          break;
00747       }
00748       break;
00749    default:
00750       snprintf(ftype, sizeof(ftype), "Unknown Frametype '%d'", f->frametype);
00751    }
00752    if (!ast_strlen_zero(moreinfo))
00753       ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) '%s' ] [%s]\n",  
00754          term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00755          term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
00756          f->frametype, 
00757          term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
00758          f->subclass, 
00759          term_color(cmn, moreinfo, COLOR_BRGREEN, COLOR_BLACK, sizeof(cmn)),
00760          term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00761    else
00762       ast_verbose("%s [ TYPE: %s (%d) SUBCLASS: %s (%d) ] [%s]\n",  
00763          term_color(cp, prefix, COLOR_BRMAGENTA, COLOR_BLACK, sizeof(cp)),
00764          term_color(cft, ftype, COLOR_BRRED, COLOR_BLACK, sizeof(cft)),
00765          f->frametype, 
00766          term_color(csub, subclass, COLOR_BRCYAN, COLOR_BLACK, sizeof(csub)),
00767          f->subclass, 
00768          term_color(cn, n, COLOR_YELLOW, COLOR_BLACK, sizeof(cn)));
00769 
00770 }

struct ast_frame* ast_frdup struct ast_frame fr  ) 
 

Copies a frame.

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

Definition at line 293 of file frame.c.

References AST_MALLOCD_HDR, ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_frame::frametype, malloc, ast_frame::mallocd, ast_frame::next, ast_frame::offset, ast_frame::prev, ast_frame::samples, ast_frame::src, and ast_frame::subclass.

Referenced by ast_queue_frame(), and ast_rtp_write().

00294 {
00295    struct ast_frame *out;
00296    int len, srclen = 0;
00297    void *buf;
00298    /* Start with standard stuff */
00299    len = sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET + f->datalen;
00300    /* If we have a source, add space for it */
00301    if (f->src)
00302       srclen = strlen(f->src);
00303    if (srclen > 0)
00304       len += srclen + 1;
00305    buf = malloc(len);
00306    if (!buf)
00307       return NULL;
00308    out = buf;
00309    /* Set us as having malloc'd header only, so it will eventually
00310       get freed. */
00311    out->frametype = f->frametype;
00312    out->subclass = f->subclass;
00313    out->datalen = f->datalen;
00314    out->samples = f->samples;
00315    out->delivery = f->delivery;
00316    out->mallocd = AST_MALLOCD_HDR;
00317    out->offset = AST_FRIENDLY_OFFSET;
00318    out->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
00319    if (srclen > 0) {
00320       out->src = out->data + f->datalen;
00321       /* Must have space since we allocated for it */
00322       strcpy(out->src, f->src);
00323    } else
00324       out->src = NULL;
00325    out->prev = NULL;
00326    out->next = NULL;
00327    memcpy(out->data, f->data, out->datalen); 
00328    return out;
00329 }

void ast_frfree struct ast_frame fr  ) 
 

Frees a frame.

Parameters:
fr Frame to free Free a frame, and the memory it used if applicable no return.

Definition at line 226 of file frame.c.

References AST_MALLOCD_DATA, AST_MALLOCD_HDR, AST_MALLOCD_SRC, ast_mutex_lock, ast_mutex_unlock, ast_frame::data, free, ast_frame::mallocd, ast_frame::next, ast_frame::offset, ast_frame::prev, and ast_frame::src.

Referenced by __ast_request_and_dial(), ast_app_getvoice(), ast_channel_bridge(), ast_channel_free(), ast_dsp_process(), ast_play_and_prepend(), ast_play_and_record(), ast_queue_frame(), ast_read(), ast_recvchar(), ast_rtp_bridge(), ast_safe_sleep(), ast_safe_sleep_conditional(), ast_send_image(), ast_tonepair(), ast_translate(), ast_waitfordigit(), ast_waitfordigit_full(), ast_waitstream(), ast_waitstream_full(), and ast_write().

00227 {
00228    if (fr->mallocd & AST_MALLOCD_DATA) {
00229       if (fr->data) 
00230          free(fr->data - fr->offset);
00231    }
00232    if (fr->mallocd & AST_MALLOCD_SRC) {
00233       if (fr->src)
00234          free(fr->src);
00235    }
00236    if (fr->mallocd & AST_MALLOCD_HDR) {
00237 #ifdef TRACE_FRAMES
00238       headers--;
00239       ast_mutex_lock(&framelock);
00240       if (fr->next)
00241          fr->next->prev = fr->prev;
00242       if (fr->prev)
00243          fr->prev->next = fr->next;
00244       else
00245          headerlist = fr->next;
00246       ast_mutex_unlock(&framelock);
00247 #endif         
00248       free(fr);
00249    }
00250 }

struct ast_frame* ast_frisolate struct ast_frame fr  ) 
 

Copies a frame.

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 a frame on success, NULL on error

Definition at line 252 of file frame.c.

References AST_FRIENDLY_OFFSET, ast_log(), AST_MALLOCD_DATA, AST_MALLOCD_HDR, AST_MALLOCD_SRC, ast_frame::data, ast_frame::datalen, ast_frame::frametype, free, LOG_WARNING, malloc, ast_frame::mallocd, ast_frame::offset, ast_frame::samples, ast_frame::src, strdup, and ast_frame::subclass.

Referenced by ast_fr_fdread().

00253 {
00254    struct ast_frame *out;
00255    if (!(fr->mallocd & AST_MALLOCD_HDR)) {
00256       /* Allocate a new header if needed */
00257       out = ast_frame_header_new();
00258       if (!out) {
00259          ast_log(LOG_WARNING, "Out of memory\n");
00260          return NULL;
00261       }
00262       out->frametype = fr->frametype;
00263       out->subclass = fr->subclass;
00264       out->datalen = 0;
00265       out->samples = fr->samples;
00266       out->offset = 0;
00267       out->src = NULL;
00268       out->data = NULL;
00269    } else {
00270       out = fr;
00271    }
00272    if (!(fr->mallocd & AST_MALLOCD_SRC)) {
00273       if (fr->src)
00274          out->src = strdup(fr->src);
00275    } else
00276       out->src = fr->src;
00277    if (!(fr->mallocd & AST_MALLOCD_DATA))  {
00278       out->data = malloc(fr->datalen + AST_FRIENDLY_OFFSET);
00279       if (!out->data) {
00280          free(out);
00281          ast_log(LOG_WARNING, "Out of memory\n");
00282          return NULL;
00283       }
00284       out->data += AST_FRIENDLY_OFFSET;
00285       out->offset = AST_FRIENDLY_OFFSET;
00286       out->datalen = fr->datalen;
00287       memcpy(out->data, fr->data, fr->datalen);
00288    }
00289    out->mallocd = AST_MALLOCD_HDR | AST_MALLOCD_SRC | AST_MALLOCD_DATA;
00290    return out;
00291 }

struct ast_format_list* ast_get_format_list size_t *  size  ) 
 

Definition at line 434 of file frame.c.

00434                                                           {
00435    *size = (sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list));
00436    return AST_FORMAT_LIST;
00437 }

struct ast_format_list* ast_get_format_list_index int  index  ) 
 

Definition at line 430 of file frame.c.

00430                                                              {
00431    return &AST_FORMAT_LIST[index];
00432 }

int ast_getformatbyname char *  name  ) 
 

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

Definition at line 498 of file frame.c.

Referenced by ast_parse_allow_disallow().

00499 {
00500    int x = 0, all = 0, format = 0;
00501 
00502    all = strcasecmp(name, "all") ? 0 : 1;
00503    for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
00504       if(AST_FORMAT_LIST[x].visible && (all || 
00505                                 !strcasecmp(AST_FORMAT_LIST[x].name,name) ||
00506                                 !strcasecmp(AST_FORMAT_LIST[x].name,ast_expand_codec_alias(name)))) {
00507          format |= AST_FORMAT_LIST[x].bits;
00508          if(!all)
00509             break;
00510       }
00511    }
00512 
00513    return format;
00514 }

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

Referenced by ast_codec_pref_string(), ast_dsp_process(), ast_frame_dump(), ast_play_and_prepend(), ast_play_and_record(), ast_read(), ast_register_translator(), ast_rtp_read(), ast_rtp_write(), ast_set_read_format(), ast_set_write_format(), ast_streamfile(), ast_translator_build_path(), ast_unregister_translator(), and ast_writestream().

00440 {
00441    int x = 0;
00442    char *ret = "unknown";
00443    for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
00444       if(AST_FORMAT_LIST[x].visible && AST_FORMAT_LIST[x].bits == format) {
00445          ret = AST_FORMAT_LIST[x].name;
00446          break;
00447       }
00448    }
00449    return ret;
00450 }

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

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

00452                                                                      {
00453 
00454    int x = 0;
00455    unsigned len;
00456    char *end = buf;
00457    char *start = buf;
00458    if (!size) return buf;
00459    snprintf(end, size, "0x%x (", format);
00460    len = strlen(end);
00461    end += len;
00462    size -= len;
00463    start = end;
00464    for (x = 0 ; x < sizeof(AST_FORMAT_LIST) / sizeof(struct ast_format_list) ; x++) {
00465       if (AST_FORMAT_LIST[x].visible && (AST_FORMAT_LIST[x].bits & format)) {
00466          snprintf(end, size,"%s|",AST_FORMAT_LIST[x].name);
00467          len = strlen(end);
00468          end += len;
00469          size -= len;
00470       }
00471    }
00472    if (start == end)
00473       snprintf(start, size, "nothing)");
00474    else if (size > 1)
00475       *(end -1) = ')';
00476    return buf;
00477 }

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

Definition at line 961 of file frame.c.

References ast_codec_pref_append(), ast_codec_pref_remove(), ast_getformatbyname(), ast_log(), and LOG_WARNING.

00962 {
00963    int format_i = 0;
00964    char *next_format = NULL, *last_format = NULL;
00965 
00966    last_format = ast_strdupa(list);
00967    while(last_format) {
00968       if((next_format = strchr(last_format, ','))) {
00969          *next_format = '\0';
00970          next_format++;
00971       }
00972       if ((format_i = ast_getformatbyname(last_format)) > 0) {
00973          if (mask) {
00974             if (allowing)
00975                (*mask) |= format_i;
00976             else
00977                (*mask) &= ~format_i;
00978          }
00979          /* can't consider 'all' a prefered codec*/
00980          if(pref && strcasecmp(last_format, "all")) {
00981             if(allowing)
00982                ast_codec_pref_append(pref, format_i);
00983             else
00984                ast_codec_pref_remove(pref, format_i);
00985          } else if(!allowing) /* disallow all must clear your prefs or it makes no sense */
00986             memset(pref, 0, sizeof(struct ast_codec_pref));
00987       } else
00988          ast_log(LOG_WARNING, "Cannot %s unknown format '%s'\n", allowing ? "allow" : "disallow", last_format);
00989 
00990       last_format = next_format;
00991    }
00992 }

int ast_smoother_feed struct ast_smoother s,
struct ast_frame f
 

Definition at line 86 of file frame.c.

References AST_FRAME_VOICE, ast_log(), AST_MIN_OFFSET, AST_SMOOTHER_FLAG_G729, ast_smoother::data, ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_smoother::delivery, ast_smoother::flags, ast_smoother::format, ast_frame::frametype, ast_smoother::len, LOG_NOTICE, LOG_WARNING, ast_frame::offset, ast_smoother::opt, ast_smoother::optimizablestream, ast_frame::samples, ast_smoother::samplesperbyte, ast_smoother::size, SMOOTHER_SIZE, and ast_frame::subclass.

Referenced by ast_rtp_write().

00087 {
00088    if (f->frametype != AST_FRAME_VOICE) {
00089       ast_log(LOG_WARNING, "Huh?  Can't smooth a non-voice frame!\n");
00090       return -1;
00091    }
00092    if (!s->format) {
00093       s->format = f->subclass;
00094       s->samplesperbyte = (float)f->samples / (float)f->datalen;
00095    } else if (s->format != f->subclass) {
00096       ast_log(LOG_WARNING, "Smoother was working on %d format frames, now trying to feed %d?\n", s->format, f->subclass);
00097       return -1;
00098    }
00099    if (s->len + f->datalen > SMOOTHER_SIZE) {
00100       ast_log(LOG_WARNING, "Out of smoother space\n");
00101       return -1;
00102    }
00103    if (((f->datalen == s->size) || ((f->datalen < 10) && (s->flags & AST_SMOOTHER_FLAG_G729)))
00104              && !s->opt && (f->offset >= AST_MIN_OFFSET)) {
00105       if (!s->len) {
00106          /* Optimize by sending the frame we just got
00107             on the next read, thus eliminating the douple
00108             copy */
00109          s->opt = f;
00110          return 0;
00111       } else {
00112          s->optimizablestream++;
00113          if (s->optimizablestream > 10) {
00114             /* For the past 10 rounds, we have input and output
00115                frames of the correct size for this smoother, yet
00116                we were unable to optimize because there was still
00117                some cruft left over.  Lets just drop the cruft so
00118                we can move to a fully optimized path */
00119             s->len = 0;
00120             s->opt = f;
00121             return 0;
00122          }
00123       }
00124    } else 
00125       s->optimizablestream = 0;
00126    if (s->flags & AST_SMOOTHER_FLAG_G729) {
00127       if (s->len % 10) {
00128          ast_log(LOG_NOTICE, "Dropping extra frame of G.729 since we already have a VAD frame at the end\n");
00129          return 0;
00130       }
00131    }
00132    memcpy(s->data + s->len, f->data, f->datalen);
00133    /* If either side is empty, reset the delivery time */
00134    if (!s->len || (!f->delivery.tv_sec && !f->delivery.tv_usec) ||
00135          (!s->delivery.tv_sec && !s->delivery.tv_usec))
00136       s->delivery = f->delivery;
00137    s->len += f->datalen;
00138    return 0;
00139 }

void ast_smoother_free struct ast_smoother s  ) 
 

Definition at line 195 of file frame.c.

References free.

Referenced by ast_rtp_destroy(), and ast_rtp_write().

00196 {
00197    free(s);
00198 }

int ast_smoother_get_flags struct ast_smoother s  ) 
 

Definition at line 76 of file frame.c.

References ast_smoother::flags.

00077 {
00078    return s->flags;
00079 }

struct ast_smoother* ast_smoother_new int  size  ) 
 

Definition at line 65 of file frame.c.

References ast_smoother_reset(), malloc, and s.

Referenced by ast_rtp_write().

00066 {
00067    struct ast_smoother *s;
00068    if (size < 1)
00069       return NULL;
00070    s = malloc(sizeof(struct ast_smoother));
00071    if (s)
00072       ast_smoother_reset(s, size);
00073    return s;
00074 }

struct ast_frame* ast_smoother_read struct ast_smoother s  ) 
 

Definition at line 141 of file frame.c.

References AST_FRAME_VOICE, AST_FRIENDLY_OFFSET, ast_log(), AST_SMOOTHER_FLAG_G729, ast_frame::data, ast_smoother::data, ast_frame::datalen, ast_frame::delivery, ast_smoother::delivery, ast_smoother::f, ast_smoother::flags, ast_smoother::format, ast_smoother::framedata, ast_frame::frametype, ast_smoother::len, LOG_WARNING, ast_frame::offset, ast_smoother::opt, ast_frame::samples, ast_smoother::samplesperbyte, ast_smoother::size, and ast_frame::subclass.

Referenced by ast_rtp_write().

00142 {
00143    struct ast_frame *opt;
00144    int len;
00145    /* IF we have an optimization frame, send it */
00146    if (s->opt) {
00147       if (s->opt->offset < AST_FRIENDLY_OFFSET)
00148          ast_log(LOG_WARNING, "Returning a frame of inappropriate offset (%d).",
00149                      s->opt->offset);
00150       opt = s->opt;
00151       s->opt = NULL;
00152       return opt;
00153    }
00154 
00155    /* Make sure we have enough data */
00156    if (s->len < s->size) {
00157       /* Or, if this is a G.729 frame with VAD on it, send it immediately anyway */
00158       if (!((s->flags & AST_SMOOTHER_FLAG_G729) && (s->size % 10)))
00159          return NULL;
00160    }
00161    len = s->size;
00162    if (len > s->len)
00163       len = s->len;
00164    /* Make frame */
00165    s->f.frametype = AST_FRAME_VOICE;
00166    s->f.subclass = s->format;
00167    s->f.data = s->framedata + AST_FRIENDLY_OFFSET;
00168    s->f.offset = AST_FRIENDLY_OFFSET;
00169    s->f.datalen = len;
00170    /* Samples will be improper given VAD, but with VAD the concept really doesn't even exist */
00171    s->f.samples = len * s->samplesperbyte;
00172    s->f.delivery = s->delivery;
00173    /* Fill Data */
00174    memcpy(s->f.data, s->data, len);
00175    s->len -= len;
00176    /* Move remaining data to the front if applicable */
00177    if (s->len) {
00178       /* In principle this should all be fine because if we are sending
00179          G.729 VAD, the next timestamp will take over anyawy */
00180       memmove(s->data, s->data + len, s->len);
00181       if (s->delivery.tv_sec || s->delivery.tv_usec) {
00182          /* If we have delivery time, increment it, otherwise, leave it at 0 */
00183          s->delivery.tv_sec += (len * s->samplesperbyte) / 8000.0;
00184          s->delivery.tv_usec += (((int)(len * s->samplesperbyte)) % 8000) * 125;
00185          if (s->delivery.tv_usec > 1000000) {
00186             s->delivery.tv_usec -= 1000000;
00187             s->delivery.tv_sec += 1;
00188          }
00189       }
00190    }
00191    /* Return frame */
00192    return &s->f;
00193 }

void ast_smoother_reset struct ast_smoother s,
int  size
 

Definition at line 59 of file frame.c.

References ast_smoother::size.

Referenced by ast_smoother_new().

00060 {
00061    memset(s, 0, sizeof(struct ast_smoother));
00062    s->size = size;
00063 }

void ast_smoother_set_flags struct ast_smoother s,
int  flags
 

Definition at line 81 of file frame.c.

References ast_smoother::flags.

Referenced by ast_rtp_write().

00082 {
00083    s->flags = flags;
00084 }

int init_framer void   ) 
 

Definition at line 800 of file frame.c.

References ast_cli_register().

Referenced by main().

00801 {
00802 #ifdef TRACE_FRAMES
00803    ast_cli_register(&cli_frame_stats);
00804 #endif
00805    ast_cli_register(&cli_show_codecs);
00806    ast_cli_register(&cli_show_codecs_audio);
00807    ast_cli_register(&cli_show_codecs_video);
00808    ast_cli_register(&cli_show_codecs_image);
00809    ast_cli_register(&cli_show_codec_n);
00810    return 0;   
00811 }


Variable Documentation

struct ast_cli_entry cli_show_codec_n
 

Initial value:

{ { "show", "codec", NULL }, show_codec_n, "Shows a specific codec", frame_show_codec_n_usage }

Definition at line 611 of file frame.c.

struct ast_cli_entry cli_show_codecs
 

Initial value:

{ { "show", "codecs", NULL }, show_codecs, "Shows codecs", frame_show_codecs_usage }

Definition at line 576 of file frame.c.

struct ast_cli_entry cli_show_codecs_audio
 

Initial value:

{ { "show", "audio", "codecs", NULL }, show_codecs, "Shows audio codecs", frame_show_codecs_usage }

Definition at line 578 of file frame.c.

struct ast_cli_entry cli_show_codecs_image
 

Initial value:

{ { "show", "image", "codecs", NULL }, show_codecs, "Shows image codecs", frame_show_codecs_usage }

Definition at line 582 of file frame.c.

struct ast_cli_entry cli_show_codecs_video
 

Initial value:

{ { "show", "video", "codecs", NULL }, show_codecs, "Shows video codecs", frame_show_codecs_usage }

Definition at line 580 of file frame.c.


Generated on Thu Nov 29 22:50:26 2007 for Asterisk by  doxygen 1.4.2